| Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 1 | /* SCTP kernel implementation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * (C) Copyright IBM Corp. 2001, 2004 | 
 | 3 |  * Copyright (c) 1999-2000 Cisco, Inc. | 
 | 4 |  * Copyright (c) 1999-2001 Motorola, Inc. | 
 | 5 |  * Copyright (c) 2001-2003 Intel Corp. | 
 | 6 |  * Copyright (c) 2001-2002 Nokia, Inc. | 
 | 7 |  * Copyright (c) 2001 La Monte H.P. Yarroll | 
 | 8 |  * | 
| Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 9 |  * This file is part of the SCTP kernel implementation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 |  * | 
 | 11 |  * These functions interface with the sockets layer to implement the | 
 | 12 |  * SCTP Extensions for the Sockets API. | 
 | 13 |  * | 
 | 14 |  * Note that the descriptions from the specification are USER level | 
 | 15 |  * functions--this file is the functions which populate the struct proto | 
 | 16 |  * for SCTP which is the BOTTOM of the sockets interface. | 
 | 17 |  * | 
| Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 18 |  * This SCTP implementation is free software; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  * you can redistribute it and/or modify it under the terms of | 
 | 20 |  * the GNU General Public License as published by | 
 | 21 |  * the Free Software Foundation; either version 2, or (at your option) | 
 | 22 |  * any later version. | 
 | 23 |  * | 
| Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 24 |  * This SCTP implementation is distributed in the hope that it | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 |  * will be useful, but WITHOUT ANY WARRANTY; without even the implied | 
 | 26 |  *                 ************************ | 
 | 27 |  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
 | 28 |  * See the GNU General Public License for more details. | 
 | 29 |  * | 
 | 30 |  * You should have received a copy of the GNU General Public License | 
| Jeff Kirsher | 4b2f13a | 2013-12-06 06:28:48 -0800 | [diff] [blame] | 31 |  * along with GNU CC; see the file COPYING.  If not, see | 
 | 32 |  * <http://www.gnu.org/licenses/>. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  * | 
 | 34 |  * Please send any bug reports or fixes you make to the | 
 | 35 |  * email address(es): | 
| Daniel Borkmann | 91705c6 | 2013-07-23 14:51:47 +0200 | [diff] [blame] | 36 |  *    lksctp developers <linux-sctp@vger.kernel.org> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  * Written or modified by: | 
 | 39 |  *    La Monte H.P. Yarroll <piggy@acm.org> | 
 | 40 |  *    Narasimha Budihal     <narsi@refcode.org> | 
 | 41 |  *    Karl Knutson          <karl@athena.chicago.il.us> | 
 | 42 |  *    Jon Grimm             <jgrimm@us.ibm.com> | 
 | 43 |  *    Xingang Guo           <xingang.guo@intel.com> | 
 | 44 |  *    Daisy Chang           <daisyc@us.ibm.com> | 
 | 45 |  *    Sridhar Samudrala     <samudrala@us.ibm.com> | 
 | 46 |  *    Inaky Perez-Gonzalez  <inaky.gonzalez@intel.com> | 
 | 47 |  *    Ardelle Fan	    <ardelle.fan@intel.com> | 
 | 48 |  *    Ryan Layer	    <rmlayer@us.ibm.com> | 
 | 49 |  *    Anup Pemmaiah         <pemmaiah@cc.usu.edu> | 
 | 50 |  *    Kevin Gao             <kevin.gao@intel.com> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  */ | 
 | 52 |  | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 53 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
 | 54 |  | 
| Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 55 | #include <crypto/hash.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/types.h> | 
 | 57 | #include <linux/kernel.h> | 
 | 58 | #include <linux/wait.h> | 
 | 59 | #include <linux/time.h> | 
| Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 60 | #include <linux/sched/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/ip.h> | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 62 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/fcntl.h> | 
 | 64 | #include <linux/poll.h> | 
 | 65 | #include <linux/init.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 66 | #include <linux/slab.h> | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 67 | #include <linux/file.h> | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 68 | #include <linux/compat.h> | 
| NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 69 | #include <linux/rhashtable.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  | 
 | 71 | #include <net/ip.h> | 
 | 72 | #include <net/icmp.h> | 
 | 73 | #include <net/route.h> | 
 | 74 | #include <net/ipv6.h> | 
 | 75 | #include <net/inet_common.h> | 
| Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 76 | #include <net/busy_poll.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |  | 
 | 78 | #include <linux/socket.h> /* for sa_family_t */ | 
| Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 79 | #include <linux/export.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <net/sock.h> | 
 | 81 | #include <net/sctp/sctp.h> | 
 | 82 | #include <net/sctp/sm.h> | 
| Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 83 | #include <net/sctp/stream_sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* Forward declarations for internal helper functions. */ | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 86 | static bool sctp_writeable(struct sock *sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static void sctp_wfree(struct sk_buff *skb); | 
| Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 88 | 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] | 89 | 				size_t msg_len); | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 90 | 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] | 91 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); | 
 | 92 | static int sctp_wait_for_accept(struct sock *sk, long timeo); | 
 | 93 | static void sctp_wait_for_close(struct sock *sk, long timeo); | 
| Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 94 | static void sctp_destruct_sock(struct sock *sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 
 | 96 | 					union sctp_addr *addr, int len); | 
 | 97 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); | 
 | 98 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); | 
 | 99 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); | 
 | 100 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); | 
 | 101 | static int sctp_send_asconf(struct sctp_association *asoc, | 
 | 102 | 			    struct sctp_chunk *chunk); | 
 | 103 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); | 
 | 104 | static int sctp_autobind(struct sock *sk); | 
| Xin Long | b7ef261 | 2017-08-11 10:23:50 +0800 | [diff] [blame] | 105 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, | 
 | 106 | 			      struct sctp_association *assoc, | 
 | 107 | 			      enum sctp_socket_type type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 |  | 
| Eric Dumazet | 0604475 | 2017-06-07 13:29:12 -0700 | [diff] [blame] | 109 | static unsigned long sctp_memory_pressure; | 
| Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 110 | static atomic_long_t sctp_memory_allocated; | 
| Eric Dumazet | 1748376 | 2008-11-25 21:16:35 -0800 | [diff] [blame] | 111 | struct percpu_counter sctp_sockets_allocated; | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 112 |  | 
| Pavel Emelyanov | 5c52ba1 | 2008-07-16 20:28:10 -0700 | [diff] [blame] | 113 | static void sctp_enter_memory_pressure(struct sock *sk) | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 114 | { | 
 | 115 | 	sctp_memory_pressure = 1; | 
 | 116 | } | 
 | 117 |  | 
 | 118 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* Get the sndbuf space available at the time on the association.  */ | 
 | 120 | static inline int sctp_wspace(struct sctp_association *asoc) | 
 | 121 | { | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 122 | 	struct sock *sk = asoc->base.sk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 |  | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 124 | 	return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used | 
 | 125 | 				       : sk_stream_wspace(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } | 
 | 127 |  | 
 | 128 | /* Increment the used sndbuf space count of the corresponding association by | 
 | 129 |  * the size of the outgoing data chunk. | 
 | 130 |  * Also, set the skb destructor for sndbuf accounting later. | 
 | 131 |  * | 
 | 132 |  * Since it is always 1-1 between chunk and skb, and also a new skb is always | 
 | 133 |  * allocated for chunk bundling in sctp_packet_transmit(), we can use the | 
 | 134 |  * destructor in the data chunk skb for the purpose of the sndbuf space | 
 | 135 |  * tracking. | 
 | 136 |  */ | 
 | 137 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) | 
 | 138 | { | 
 | 139 | 	struct sctp_association *asoc = chunk->asoc; | 
 | 140 | 	struct sock *sk = asoc->base.sk; | 
 | 141 |  | 
 | 142 | 	/* The sndbuf space is tracked per association.  */ | 
 | 143 | 	sctp_association_hold(asoc); | 
 | 144 |  | 
| Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 145 | 	if (chunk->shkey) | 
 | 146 | 		sctp_auth_shkey_hold(chunk->shkey); | 
 | 147 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 148 | 	skb_set_owner_w(chunk->skb, sk); | 
 | 149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 	chunk->skb->destructor = sctp_wfree; | 
 | 151 | 	/* Save the chunk pointer in skb for sctp_wfree to use later.  */ | 
| Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 152 | 	skb_shinfo(chunk->skb)->destructor_arg = chunk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 |  | 
| Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 154 | 	refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); | 
| Xin Long | 605c0ac | 2018-10-17 03:07:50 +0800 | [diff] [blame] | 155 | 	asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk); | 
 | 156 | 	sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk); | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 157 | 	sk_mem_charge(sk, chunk->skb->truesize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } | 
 | 159 |  | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 160 | static void sctp_clear_owner_w(struct sctp_chunk *chunk) | 
 | 161 | { | 
 | 162 | 	skb_orphan(chunk->skb); | 
 | 163 | } | 
 | 164 |  | 
 | 165 | static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, | 
 | 166 | 				       void (*cb)(struct sctp_chunk *)) | 
 | 167 |  | 
 | 168 | { | 
 | 169 | 	struct sctp_outq *q = &asoc->outqueue; | 
 | 170 | 	struct sctp_transport *t; | 
 | 171 | 	struct sctp_chunk *chunk; | 
 | 172 |  | 
 | 173 | 	list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) | 
 | 174 | 		list_for_each_entry(chunk, &t->transmitted, transmitted_list) | 
 | 175 | 			cb(chunk); | 
 | 176 |  | 
| Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 177 | 	list_for_each_entry(chunk, &q->retransmit, transmitted_list) | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 178 | 		cb(chunk); | 
 | 179 |  | 
| Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 180 | 	list_for_each_entry(chunk, &q->sacked, transmitted_list) | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 181 | 		cb(chunk); | 
 | 182 |  | 
| Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 183 | 	list_for_each_entry(chunk, &q->abandoned, transmitted_list) | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 184 | 		cb(chunk); | 
 | 185 |  | 
 | 186 | 	list_for_each_entry(chunk, &q->out_chunk_list, list) | 
 | 187 | 		cb(chunk); | 
 | 188 | } | 
 | 189 |  | 
| Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 190 | static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk, | 
 | 191 | 				 void (*cb)(struct sk_buff *, struct sock *)) | 
 | 192 |  | 
 | 193 | { | 
 | 194 | 	struct sk_buff *skb, *tmp; | 
 | 195 |  | 
 | 196 | 	sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp) | 
 | 197 | 		cb(skb, sk); | 
 | 198 |  | 
 | 199 | 	sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp) | 
 | 200 | 		cb(skb, sk); | 
 | 201 |  | 
 | 202 | 	sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp) | 
 | 203 | 		cb(skb, sk); | 
 | 204 | } | 
 | 205 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* Verify that this is a valid address. */ | 
 | 207 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, | 
 | 208 | 				   int len) | 
 | 209 | { | 
 | 210 | 	struct sctp_af *af; | 
 | 211 |  | 
 | 212 | 	/* Verify basic sockaddr. */ | 
 | 213 | 	af = sctp_sockaddr_af(sctp_sk(sk), addr, len); | 
 | 214 | 	if (!af) | 
 | 215 | 		return -EINVAL; | 
 | 216 |  | 
 | 217 | 	/* Is this a valid SCTP address?  */ | 
| Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 218 | 	if (!af->addr_valid(addr, sctp_sk(sk), NULL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | 		return -EINVAL; | 
 | 220 |  | 
 | 221 | 	if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) | 
 | 222 | 		return -EINVAL; | 
 | 223 |  | 
 | 224 | 	return 0; | 
 | 225 | } | 
 | 226 |  | 
 | 227 | /* Look up the association by its id.  If this is not a UDP-style | 
 | 228 |  * socket, the ID field is always ignored. | 
 | 229 |  */ | 
 | 230 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) | 
 | 231 | { | 
 | 232 | 	struct sctp_association *asoc = NULL; | 
 | 233 |  | 
 | 234 | 	/* If this is not a UDP-style socket, assoc id should be ignored. */ | 
 | 235 | 	if (!sctp_style(sk, UDP)) { | 
 | 236 | 		/* Return NULL if the socket state is not ESTABLISHED. It | 
 | 237 | 		 * could be a TCP-style listening socket or a socket which | 
 | 238 | 		 * hasn't yet called connect() to establish an association. | 
 | 239 | 		 */ | 
| Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 240 | 		if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | 			return NULL; | 
 | 242 |  | 
 | 243 | 		/* Get the first and the only association from the list. */ | 
 | 244 | 		if (!list_empty(&sctp_sk(sk)->ep->asocs)) | 
 | 245 | 			asoc = list_entry(sctp_sk(sk)->ep->asocs.next, | 
 | 246 | 					  struct sctp_association, asocs); | 
 | 247 | 		return asoc; | 
 | 248 | 	} | 
 | 249 |  | 
 | 250 | 	/* Otherwise this is a UDP-style socket. */ | 
 | 251 | 	if (!id || (id == (sctp_assoc_t)-1)) | 
 | 252 | 		return NULL; | 
 | 253 |  | 
 | 254 | 	spin_lock_bh(&sctp_assocs_id_lock); | 
 | 255 | 	asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); | 
 | 256 | 	spin_unlock_bh(&sctp_assocs_id_lock); | 
 | 257 |  | 
 | 258 | 	if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) | 
 | 259 | 		return NULL; | 
 | 260 |  | 
 | 261 | 	return asoc; | 
 | 262 | } | 
 | 263 |  | 
 | 264 | /* Look up the transport from an address and an assoc id. If both address and | 
 | 265 |  * id are specified, the associations matching the address and the id should be | 
 | 266 |  * the same. | 
 | 267 |  */ | 
 | 268 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, | 
 | 269 | 					      struct sockaddr_storage *addr, | 
 | 270 | 					      sctp_assoc_t id) | 
 | 271 | { | 
 | 272 | 	struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; | 
| Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 273 | 	struct sctp_af *af = sctp_get_af_specific(addr->ss_family); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | 	union sctp_addr *laddr = (union sctp_addr *)addr; | 
| Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 275 | 	struct sctp_transport *transport; | 
 | 276 |  | 
| Xin Long | 912964e | 2017-02-07 20:56:08 +0800 | [diff] [blame] | 277 | 	if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len)) | 
| Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 278 | 		return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | 	addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, | 
| Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 281 | 					       laddr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 					       &transport); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 |  | 
 | 284 | 	if (!addr_asoc) | 
 | 285 | 		return NULL; | 
 | 286 |  | 
 | 287 | 	id_asoc = sctp_id2assoc(sk, id); | 
 | 288 | 	if (id_asoc && (id_asoc != addr_asoc)) | 
 | 289 | 		return NULL; | 
 | 290 |  | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 291 | 	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] | 292 | 						(union sctp_addr *)addr); | 
 | 293 |  | 
 | 294 | 	return transport; | 
 | 295 | } | 
 | 296 |  | 
 | 297 | /* API 3.1.2 bind() - UDP Style Syntax | 
 | 298 |  * The syntax of bind() is, | 
 | 299 |  * | 
 | 300 |  *   ret = bind(int sd, struct sockaddr *addr, int addrlen); | 
 | 301 |  * | 
 | 302 |  *   sd      - the socket descriptor returned by socket(). | 
 | 303 |  *   addr    - the address structure (struct sockaddr_in or struct | 
 | 304 |  *             sockaddr_in6 [RFC 2553]), | 
 | 305 |  *   addr_len - the size of the address structure. | 
 | 306 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 307 | 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] | 308 | { | 
 | 309 | 	int retval = 0; | 
 | 310 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 311 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 313 | 	pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk, | 
 | 314 | 		 addr, addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 |  | 
 | 316 | 	/* Disallow binding twice. */ | 
 | 317 | 	if (!sctp_sk(sk)->ep->base.bind_addr.port) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 318 | 		retval = sctp_do_bind(sk, (union sctp_addr *)addr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 				      addr_len); | 
 | 320 | 	else | 
 | 321 | 		retval = -EINVAL; | 
 | 322 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 323 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 |  | 
 | 325 | 	return retval; | 
 | 326 | } | 
 | 327 |  | 
 | 328 | static long sctp_get_port_local(struct sock *, union sctp_addr *); | 
 | 329 |  | 
 | 330 | /* Verify this is a valid sockaddr. */ | 
 | 331 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 
 | 332 | 					union sctp_addr *addr, int len) | 
 | 333 | { | 
 | 334 | 	struct sctp_af *af; | 
 | 335 |  | 
 | 336 | 	/* Check minimum size.  */ | 
 | 337 | 	if (len < sizeof (struct sockaddr)) | 
 | 338 | 		return NULL; | 
 | 339 |  | 
| Xin Long | c5006b8 | 2018-01-15 17:02:00 +0800 | [diff] [blame] | 340 | 	if (!opt->pf->af_supported(addr->sa.sa_family, opt)) | 
 | 341 | 		return NULL; | 
 | 342 |  | 
| Eric Dumazet | 81e9837 | 2018-04-08 07:52:08 -0700 | [diff] [blame] | 343 | 	if (addr->sa.sa_family == AF_INET6) { | 
 | 344 | 		if (len < SIN6_LEN_RFC2133) | 
 | 345 | 			return NULL; | 
 | 346 | 		/* V4 mapped address are really of AF_INET family */ | 
 | 347 | 		if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) && | 
 | 348 | 		    !opt->pf->af_supported(AF_INET, opt)) | 
 | 349 | 			return NULL; | 
 | 350 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 |  | 
 | 352 | 	/* If we get this far, af is valid. */ | 
 | 353 | 	af = sctp_get_af_specific(addr->sa.sa_family); | 
 | 354 |  | 
 | 355 | 	if (len < af->sockaddr_len) | 
 | 356 | 		return NULL; | 
 | 357 |  | 
 | 358 | 	return af; | 
 | 359 | } | 
 | 360 |  | 
 | 361 | /* Bind a local address either to an endpoint or to an association.  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 362 | 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] | 363 | { | 
| Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 364 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 366 | 	struct sctp_endpoint *ep = sp->ep; | 
 | 367 | 	struct sctp_bind_addr *bp = &ep->base.bind_addr; | 
 | 368 | 	struct sctp_af *af; | 
 | 369 | 	unsigned short snum; | 
 | 370 | 	int ret = 0; | 
 | 371 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 	/* Common sockaddr verification. */ | 
 | 373 | 	af = sctp_sockaddr_af(sp, addr, len); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 374 | 	if (!af) { | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 375 | 		pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n", | 
 | 376 | 			 __func__, sk, addr, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | 		return -EINVAL; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 378 | 	} | 
 | 379 |  | 
 | 380 | 	snum = ntohs(addr->v4.sin_port); | 
 | 381 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 382 | 	pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n", | 
 | 383 | 		 __func__, sk, &addr->sa, bp->port, snum, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 |  | 
 | 385 | 	/* PF specific bind() address verification. */ | 
 | 386 | 	if (!sp->pf->bind_verify(sp, addr)) | 
 | 387 | 		return -EADDRNOTAVAIL; | 
 | 388 |  | 
| Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 389 | 	/* We must either be unbound, or bind to the same port. | 
 | 390 | 	 * It's OK to allow 0 ports if we are already bound. | 
 | 391 | 	 * We'll just inhert an already bound port in this case | 
 | 392 | 	 */ | 
 | 393 | 	if (bp->port) { | 
 | 394 | 		if (!snum) | 
 | 395 | 			snum = bp->port; | 
 | 396 | 		else if (snum != bp->port) { | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 397 | 			pr_debug("%s: new port %d doesn't match existing port " | 
 | 398 | 				 "%d\n", __func__, snum, bp->port); | 
| Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 399 | 			return -EINVAL; | 
 | 400 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | 	} | 
 | 402 |  | 
| Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 403 | 	if (snum && snum < inet_prot_sock(net) && | 
| Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 404 | 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | 		return -EACCES; | 
 | 406 |  | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 407 | 	/* See if the address matches any of the addresses we may have | 
 | 408 | 	 * already bound before checking against other endpoints. | 
 | 409 | 	 */ | 
 | 410 | 	if (sctp_bind_addr_match(bp, addr, sp)) | 
 | 411 | 		return -EINVAL; | 
 | 412 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | 	/* Make sure we are allowed to bind here. | 
 | 414 | 	 * The function sctp_get_port_local() does duplicate address | 
 | 415 | 	 * detection. | 
 | 416 | 	 */ | 
| Vlad Yasevich | 2772b49 | 2007-08-21 14:24:30 +0900 | [diff] [blame] | 417 | 	addr->v4.sin_port = htons(snum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | 	if ((ret = sctp_get_port_local(sk, addr))) { | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 419 | 		return -EADDRINUSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | 	} | 
 | 421 |  | 
 | 422 | 	/* Refresh ephemeral port.  */ | 
 | 423 | 	if (!bp->port) | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 424 | 		bp->port = inet_sk(sk)->inet_num; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 426 | 	/* Add the address to the bind address list. | 
 | 427 | 	 * Use GFP_ATOMIC since BHs will be disabled. | 
 | 428 | 	 */ | 
| Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 429 | 	ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len, | 
 | 430 | 				 SCTP_ADDR_SRC, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 |  | 
 | 432 | 	/* Copy back into socket for getsockname() use. */ | 
 | 433 | 	if (!ret) { | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 434 | 		inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 435 | 		sp->pf->to_sk_saddr(addr, sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | 	} | 
 | 437 |  | 
 | 438 | 	return ret; | 
 | 439 | } | 
 | 440 |  | 
 | 441 |  /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks | 
 | 442 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 443 |  * 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] | 444 |  * 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] | 445 |  * it needs to transfer another ASCONF Chunk, it MUST wait until the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 |  * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 447 |  * subsequent ASCONF. Note this restriction binds each side, so at any | 
 | 448 |  * 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] | 449 |  * from each endpoint). | 
 | 450 |  */ | 
 | 451 | static int sctp_send_asconf(struct sctp_association *asoc, | 
 | 452 | 			    struct sctp_chunk *chunk) | 
 | 453 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 454 | 	struct net 	*net = sock_net(asoc->base.sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | 	int		retval = 0; | 
 | 456 |  | 
 | 457 | 	/* If there is an outstanding ASCONF chunk, queue it for later | 
 | 458 | 	 * transmission. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 459 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | 	if (asoc->addip_last_asconf) { | 
| David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 461 | 		list_add_tail(&chunk->list, &asoc->addip_chunk_list); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 462 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | 	} | 
 | 464 |  | 
 | 465 | 	/* Hold the chunk until an ASCONF_ACK is received. */ | 
 | 466 | 	sctp_chunk_hold(chunk); | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 467 | 	retval = sctp_primitive_ASCONF(net, asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | 	if (retval) | 
 | 469 | 		sctp_chunk_free(chunk); | 
 | 470 | 	else | 
 | 471 | 		asoc->addip_last_asconf = chunk; | 
 | 472 |  | 
 | 473 | out: | 
 | 474 | 	return retval; | 
 | 475 | } | 
 | 476 |  | 
 | 477 | /* Add a list of addresses as bind addresses to local endpoint or | 
 | 478 |  * association. | 
 | 479 |  * | 
 | 480 |  * Basically run through each address specified in the addrs/addrcnt | 
 | 481 |  * array/length pair, determine if it is IPv6 or IPv4 and call | 
 | 482 |  * sctp_do_bind() on it. | 
 | 483 |  * | 
 | 484 |  * If any of them fails, then the operation will be reversed and the | 
 | 485 |  * ones that were added will be removed. | 
 | 486 |  * | 
 | 487 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 488 |  */ | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 489 | 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] | 490 | { | 
 | 491 | 	int cnt; | 
 | 492 | 	int retval = 0; | 
 | 493 | 	void *addr_buf; | 
 | 494 | 	struct sockaddr *sa_addr; | 
 | 495 | 	struct sctp_af *af; | 
 | 496 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 497 | 	pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk, | 
 | 498 | 		 addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 |  | 
 | 500 | 	addr_buf = addrs; | 
 | 501 | 	for (cnt = 0; cnt < addrcnt; cnt++) { | 
 | 502 | 		/* The list may contain either IPv4 or IPv6 address; | 
 | 503 | 		 * determine the address length for walking thru the list. | 
 | 504 | 		 */ | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 505 | 		sa_addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | 		af = sctp_get_af_specific(sa_addr->sa_family); | 
 | 507 | 		if (!af) { | 
 | 508 | 			retval = -EINVAL; | 
 | 509 | 			goto err_bindx_add; | 
 | 510 | 		} | 
 | 511 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 512 | 		retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | 				      af->sockaddr_len); | 
 | 514 |  | 
 | 515 | 		addr_buf += af->sockaddr_len; | 
 | 516 |  | 
 | 517 | err_bindx_add: | 
 | 518 | 		if (retval < 0) { | 
 | 519 | 			/* Failed. Cleanup the ones that have been added */ | 
 | 520 | 			if (cnt > 0) | 
 | 521 | 				sctp_bindx_rem(sk, addrs, cnt); | 
 | 522 | 			return retval; | 
 | 523 | 		} | 
 | 524 | 	} | 
 | 525 |  | 
 | 526 | 	return retval; | 
 | 527 | } | 
 | 528 |  | 
 | 529 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the | 
 | 530 |  * associations that are part of the endpoint indicating that a list of local | 
 | 531 |  * addresses are added to the endpoint. | 
 | 532 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 533 |  * 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] | 534 |  * association, we do not send the chunk for that association.  But it will not | 
 | 535 |  * affect other associations. | 
 | 536 |  * | 
 | 537 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 538 |  */ | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 539 | static int sctp_send_asconf_add_ip(struct sock		*sk, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | 				   struct sockaddr	*addrs, | 
 | 541 | 				   int 			addrcnt) | 
 | 542 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 543 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | 	struct sctp_sock		*sp; | 
 | 545 | 	struct sctp_endpoint		*ep; | 
 | 546 | 	struct sctp_association		*asoc; | 
 | 547 | 	struct sctp_bind_addr		*bp; | 
 | 548 | 	struct sctp_chunk		*chunk; | 
 | 549 | 	struct sctp_sockaddr_entry	*laddr; | 
 | 550 | 	union sctp_addr			*addr; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 551 | 	union sctp_addr			saveaddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | 	void				*addr_buf; | 
 | 553 | 	struct sctp_af			*af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | 	struct list_head		*p; | 
 | 555 | 	int 				i; | 
 | 556 | 	int 				retval = 0; | 
 | 557 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 558 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | 		return retval; | 
 | 560 |  | 
 | 561 | 	sp = sctp_sk(sk); | 
 | 562 | 	ep = sp->ep; | 
 | 563 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 564 | 	pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", | 
 | 565 | 		 __func__, sk, addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 567 | 	list_for_each_entry(asoc, &ep->asocs, asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | 		if (!asoc->peer.asconf_capable) | 
 | 569 | 			continue; | 
 | 570 |  | 
 | 571 | 		if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) | 
 | 572 | 			continue; | 
 | 573 |  | 
 | 574 | 		if (!sctp_state(asoc, ESTABLISHED)) | 
 | 575 | 			continue; | 
 | 576 |  | 
 | 577 | 		/* Check if any address in the packed array of addresses is | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 578 | 		 * in the bind address list of the association. If so, | 
 | 579 | 		 * do not send the asconf chunk to its peer, but continue with | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | 		 * other associations. | 
 | 581 | 		 */ | 
 | 582 | 		addr_buf = addrs; | 
 | 583 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 584 | 			addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | 			af = sctp_get_af_specific(addr->v4.sin_family); | 
 | 586 | 			if (!af) { | 
 | 587 | 				retval = -EINVAL; | 
 | 588 | 				goto out; | 
 | 589 | 			} | 
 | 590 |  | 
 | 591 | 			if (sctp_assoc_lookup_laddr(asoc, addr)) | 
 | 592 | 				break; | 
 | 593 |  | 
 | 594 | 			addr_buf += af->sockaddr_len; | 
 | 595 | 		} | 
 | 596 | 		if (i < addrcnt) | 
 | 597 | 			continue; | 
 | 598 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 599 | 		/* Use the first valid address in bind addr list of | 
 | 600 | 		 * association as Address Parameter of ASCONF CHUNK. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | 		bp = &asoc->base.bind_addr; | 
 | 603 | 		p = bp->address_list.next; | 
 | 604 | 		laddr = list_entry(p, struct sctp_sockaddr_entry, list); | 
| Al Viro | 5ae955c | 2006-11-20 17:22:08 -0800 | [diff] [blame] | 605 | 		chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | 						   addrcnt, SCTP_PARAM_ADD_IP); | 
 | 607 | 		if (!chunk) { | 
 | 608 | 			retval = -ENOMEM; | 
 | 609 | 			goto out; | 
 | 610 | 		} | 
 | 611 |  | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 612 | 		/* Add the new addresses to the bind address list with | 
 | 613 | 		 * use_as_src set to 0. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | 		 */ | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 615 | 		addr_buf = addrs; | 
 | 616 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 617 | 			addr = addr_buf; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 618 | 			af = sctp_get_af_specific(addr->v4.sin_family); | 
 | 619 | 			memcpy(&saveaddr, addr, af->sockaddr_len); | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 620 | 			retval = sctp_add_bind_addr(bp, &saveaddr, | 
| Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 621 | 						    sizeof(saveaddr), | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 622 | 						    SCTP_ADDR_NEW, GFP_ATOMIC); | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 623 | 			addr_buf += af->sockaddr_len; | 
 | 624 | 		} | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 625 | 		if (asoc->src_out_of_asoc_ok) { | 
 | 626 | 			struct sctp_transport *trans; | 
 | 627 |  | 
 | 628 | 			list_for_each_entry(trans, | 
 | 629 | 			    &asoc->peer.transport_addr_list, transports) { | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 630 | 				trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, | 
 | 631 | 				    2*asoc->pathmtu, 4380)); | 
 | 632 | 				trans->ssthresh = asoc->peer.i.a_rwnd; | 
 | 633 | 				trans->rto = asoc->rto_initial; | 
| Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 634 | 				sctp_max_rto(asoc, trans); | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 635 | 				trans->rtt = trans->srtt = trans->rttvar = 0; | 
| Marcelo Ricardo Leitner | 6e91b57 | 2018-04-26 16:58:59 -0300 | [diff] [blame] | 636 | 				/* Clear the source and route cache */ | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 637 | 				sctp_transport_route(trans, NULL, | 
| Marcelo Ricardo Leitner | 6e91b57 | 2018-04-26 16:58:59 -0300 | [diff] [blame] | 638 | 						     sctp_sk(asoc->base.sk)); | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 639 | 			} | 
 | 640 | 		} | 
 | 641 | 		retval = sctp_send_asconf(asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | 	} | 
 | 643 |  | 
 | 644 | out: | 
 | 645 | 	return retval; | 
 | 646 | } | 
 | 647 |  | 
 | 648 | /* Remove a list of addresses from bind addresses list.  Do not remove the | 
 | 649 |  * last address. | 
 | 650 |  * | 
 | 651 |  * Basically run through each address specified in the addrs/addrcnt | 
 | 652 |  * array/length pair, determine if it is IPv6 or IPv4 and call | 
 | 653 |  * sctp_del_bind() on it. | 
 | 654 |  * | 
 | 655 |  * If any of them fails, then the operation will be reversed and the | 
 | 656 |  * ones that were removed will be added back. | 
 | 657 |  * | 
 | 658 |  * At least one address has to be left; if only one address is | 
 | 659 |  * available, the operation will return -EBUSY. | 
 | 660 |  * | 
 | 661 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 662 |  */ | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 663 | 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] | 664 | { | 
 | 665 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 666 | 	struct sctp_endpoint *ep = sp->ep; | 
 | 667 | 	int cnt; | 
 | 668 | 	struct sctp_bind_addr *bp = &ep->base.bind_addr; | 
 | 669 | 	int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | 	void *addr_buf; | 
| Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 671 | 	union sctp_addr *sa_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | 	struct sctp_af *af; | 
 | 673 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 674 | 	pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", | 
 | 675 | 		 __func__, sk, addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 |  | 
 | 677 | 	addr_buf = addrs; | 
 | 678 | 	for (cnt = 0; cnt < addrcnt; cnt++) { | 
 | 679 | 		/* If the bind address list is empty or if there is only one | 
 | 680 | 		 * bind address, there is nothing more to be removed (we need | 
 | 681 | 		 * at least one address here). | 
 | 682 | 		 */ | 
 | 683 | 		if (list_empty(&bp->address_list) || | 
 | 684 | 		    (sctp_list_single_entry(&bp->address_list))) { | 
 | 685 | 			retval = -EBUSY; | 
 | 686 | 			goto err_bindx_rem; | 
 | 687 | 		} | 
 | 688 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 689 | 		sa_addr = addr_buf; | 
| Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 690 | 		af = sctp_get_af_specific(sa_addr->sa.sa_family); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | 		if (!af) { | 
 | 692 | 			retval = -EINVAL; | 
 | 693 | 			goto err_bindx_rem; | 
 | 694 | 		} | 
| Paolo Galtieri | 0304ff8a | 2007-04-17 12:52:36 -0700 | [diff] [blame] | 695 |  | 
 | 696 | 		if (!af->addr_valid(sa_addr, sp, NULL)) { | 
 | 697 | 			retval = -EADDRNOTAVAIL; | 
 | 698 | 			goto err_bindx_rem; | 
 | 699 | 		} | 
 | 700 |  | 
| Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 701 | 		if (sa_addr->v4.sin_port && | 
 | 702 | 		    sa_addr->v4.sin_port != htons(bp->port)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | 			retval = -EINVAL; | 
 | 704 | 			goto err_bindx_rem; | 
 | 705 | 		} | 
 | 706 |  | 
| Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 707 | 		if (!sa_addr->v4.sin_port) | 
 | 708 | 			sa_addr->v4.sin_port = htons(bp->port); | 
 | 709 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | 		/* FIXME - There is probably a need to check if sk->sk_saddr and | 
 | 711 | 		 * sk->sk_rcv_addr are currently set to one of the addresses to | 
 | 712 | 		 * be removed. This is something which needs to be looked into | 
 | 713 | 		 * when we are fixing the outstanding issues with multi-homing | 
 | 714 | 		 * socket routing and failover schemes. Refer to comments in | 
 | 715 | 		 * sctp_do_bind(). -daisy | 
 | 716 | 		 */ | 
| Vlad Yasevich | 0ed90fb | 2007-10-24 16:10:00 -0400 | [diff] [blame] | 717 | 		retval = sctp_del_bind_addr(bp, sa_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 |  | 
 | 719 | 		addr_buf += af->sockaddr_len; | 
 | 720 | err_bindx_rem: | 
 | 721 | 		if (retval < 0) { | 
 | 722 | 			/* Failed. Add the ones that has been removed back */ | 
 | 723 | 			if (cnt > 0) | 
 | 724 | 				sctp_bindx_add(sk, addrs, cnt); | 
 | 725 | 			return retval; | 
 | 726 | 		} | 
 | 727 | 	} | 
 | 728 |  | 
 | 729 | 	return retval; | 
 | 730 | } | 
 | 731 |  | 
 | 732 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of | 
 | 733 |  * the associations that are part of the endpoint indicating that a list of | 
 | 734 |  * local addresses are removed from the endpoint. | 
 | 735 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 736 |  * 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] | 737 |  * association, we do not send the chunk for that association.  But it will not | 
 | 738 |  * affect other associations. | 
 | 739 |  * | 
 | 740 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 741 |  */ | 
 | 742 | static int sctp_send_asconf_del_ip(struct sock		*sk, | 
 | 743 | 				   struct sockaddr	*addrs, | 
 | 744 | 				   int			addrcnt) | 
 | 745 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 746 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | 	struct sctp_sock	*sp; | 
 | 748 | 	struct sctp_endpoint	*ep; | 
 | 749 | 	struct sctp_association	*asoc; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 750 | 	struct sctp_transport	*transport; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | 	struct sctp_bind_addr	*bp; | 
 | 752 | 	struct sctp_chunk	*chunk; | 
 | 753 | 	union sctp_addr		*laddr; | 
 | 754 | 	void			*addr_buf; | 
 | 755 | 	struct sctp_af		*af; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 756 | 	struct sctp_sockaddr_entry *saddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | 	int 			i; | 
 | 758 | 	int 			retval = 0; | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 759 | 	int			stored = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 761 | 	chunk = NULL; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 762 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | 		return retval; | 
 | 764 |  | 
 | 765 | 	sp = sctp_sk(sk); | 
 | 766 | 	ep = sp->ep; | 
 | 767 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 768 | 	pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", | 
 | 769 | 		 __func__, sk, addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 771 | 	list_for_each_entry(asoc, &ep->asocs, asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 |  | 
 | 773 | 		if (!asoc->peer.asconf_capable) | 
 | 774 | 			continue; | 
 | 775 |  | 
 | 776 | 		if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) | 
 | 777 | 			continue; | 
 | 778 |  | 
 | 779 | 		if (!sctp_state(asoc, ESTABLISHED)) | 
 | 780 | 			continue; | 
 | 781 |  | 
 | 782 | 		/* Check if any address in the packed array of addresses is | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 783 | 		 * not present in the bind address list of the association. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | 		 * If so, do not send the asconf chunk to its peer, but | 
 | 785 | 		 * continue with other associations. | 
 | 786 | 		 */ | 
 | 787 | 		addr_buf = addrs; | 
 | 788 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 789 | 			laddr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | 			af = sctp_get_af_specific(laddr->v4.sin_family); | 
 | 791 | 			if (!af) { | 
 | 792 | 				retval = -EINVAL; | 
 | 793 | 				goto out; | 
 | 794 | 			} | 
 | 795 |  | 
 | 796 | 			if (!sctp_assoc_lookup_laddr(asoc, laddr)) | 
 | 797 | 				break; | 
 | 798 |  | 
 | 799 | 			addr_buf += af->sockaddr_len; | 
 | 800 | 		} | 
 | 801 | 		if (i < addrcnt) | 
 | 802 | 			continue; | 
 | 803 |  | 
 | 804 | 		/* Find one address in the association's bind address list | 
 | 805 | 		 * that is not in the packed array of addresses. This is to | 
 | 806 | 		 * make sure that we do not delete all the addresses in the | 
 | 807 | 		 * association. | 
 | 808 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | 		bp = &asoc->base.bind_addr; | 
 | 810 | 		laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, | 
 | 811 | 					       addrcnt, sp); | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 812 | 		if ((laddr == NULL) && (addrcnt == 1)) { | 
 | 813 | 			if (asoc->asconf_addr_del_pending) | 
 | 814 | 				continue; | 
 | 815 | 			asoc->asconf_addr_del_pending = | 
 | 816 | 			    kzalloc(sizeof(union sctp_addr), GFP_ATOMIC); | 
| Michio Honda | 6d65e5e | 2011-06-10 16:42:14 +0900 | [diff] [blame] | 817 | 			if (asoc->asconf_addr_del_pending == NULL) { | 
 | 818 | 				retval = -ENOMEM; | 
 | 819 | 				goto out; | 
 | 820 | 			} | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 821 | 			asoc->asconf_addr_del_pending->sa.sa_family = | 
 | 822 | 				    addrs->sa_family; | 
 | 823 | 			asoc->asconf_addr_del_pending->v4.sin_port = | 
 | 824 | 				    htons(bp->port); | 
 | 825 | 			if (addrs->sa_family == AF_INET) { | 
 | 826 | 				struct sockaddr_in *sin; | 
 | 827 |  | 
 | 828 | 				sin = (struct sockaddr_in *)addrs; | 
 | 829 | 				asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; | 
 | 830 | 			} else if (addrs->sa_family == AF_INET6) { | 
 | 831 | 				struct sockaddr_in6 *sin6; | 
 | 832 |  | 
 | 833 | 				sin6 = (struct sockaddr_in6 *)addrs; | 
| Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 834 | 				asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 835 | 			} | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 836 |  | 
 | 837 | 			pr_debug("%s: keep the last address asoc:%p %pISc at %p\n", | 
 | 838 | 				 __func__, asoc, &asoc->asconf_addr_del_pending->sa, | 
 | 839 | 				 asoc->asconf_addr_del_pending); | 
 | 840 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 841 | 			asoc->src_out_of_asoc_ok = 1; | 
 | 842 | 			stored = 1; | 
 | 843 | 			goto skip_mkasconf; | 
 | 844 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 |  | 
| Daniel Borkmann | 88362ad | 2013-09-07 20:51:21 +0200 | [diff] [blame] | 846 | 		if (laddr == NULL) | 
 | 847 | 			return -EINVAL; | 
 | 848 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 849 | 		/* We do not need RCU protection throughout this loop | 
 | 850 | 		 * because this is done under a socket lock from the | 
 | 851 | 		 * setsockopt call. | 
 | 852 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | 		chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, | 
 | 854 | 						   SCTP_PARAM_DEL_IP); | 
 | 855 | 		if (!chunk) { | 
 | 856 | 			retval = -ENOMEM; | 
 | 857 | 			goto out; | 
 | 858 | 		} | 
 | 859 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 860 | skip_mkasconf: | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 861 | 		/* Reset use_as_src flag for the addresses in the bind address | 
 | 862 | 		 * list that are to be deleted. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | 		 */ | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 864 | 		addr_buf = addrs; | 
 | 865 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 866 | 			laddr = addr_buf; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 867 | 			af = sctp_get_af_specific(laddr->v4.sin_family); | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 868 | 			list_for_each_entry(saddr, &bp->address_list, list) { | 
| Al Viro | 5f242a1 | 2006-11-20 17:05:23 -0800 | [diff] [blame] | 869 | 				if (sctp_cmp_addr_exact(&saddr->a, laddr)) | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 870 | 					saddr->state = SCTP_ADDR_DEL; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 871 | 			} | 
 | 872 | 			addr_buf += af->sockaddr_len; | 
 | 873 | 		} | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 874 |  | 
 | 875 | 		/* Update the route and saddr entries for all the transports | 
 | 876 | 		 * as some of the addresses in the bind address list are | 
 | 877 | 		 * about to be deleted and cannot be used as source addresses. | 
 | 878 | 		 */ | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 879 | 		list_for_each_entry(transport, &asoc->peer.transport_addr_list, | 
 | 880 | 					transports) { | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 881 | 			sctp_transport_route(transport, NULL, | 
 | 882 | 					     sctp_sk(asoc->base.sk)); | 
 | 883 | 		} | 
 | 884 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 885 | 		if (stored) | 
 | 886 | 			/* We don't need to transmit ASCONF */ | 
 | 887 | 			continue; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 888 | 		retval = sctp_send_asconf(asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | 	} | 
 | 890 | out: | 
 | 891 | 	return retval; | 
 | 892 | } | 
 | 893 |  | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 894 | /* set addr events to assocs in the endpoint.  ep and addr_wq must be locked */ | 
 | 895 | int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) | 
 | 896 | { | 
 | 897 | 	struct sock *sk = sctp_opt2sk(sp); | 
 | 898 | 	union sctp_addr *addr; | 
 | 899 | 	struct sctp_af *af; | 
 | 900 |  | 
 | 901 | 	/* It is safe to write port space in caller. */ | 
 | 902 | 	addr = &addrw->a; | 
 | 903 | 	addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); | 
 | 904 | 	af = sctp_get_af_specific(addr->sa.sa_family); | 
 | 905 | 	if (!af) | 
 | 906 | 		return -EINVAL; | 
 | 907 | 	if (sctp_verify_addr(sk, addr, af->sockaddr_len)) | 
 | 908 | 		return -EINVAL; | 
 | 909 |  | 
 | 910 | 	if (addrw->state == SCTP_ADDR_NEW) | 
 | 911 | 		return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); | 
 | 912 | 	else | 
 | 913 | 		return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); | 
 | 914 | } | 
 | 915 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() | 
 | 917 |  * | 
 | 918 |  * API 8.1 | 
 | 919 |  * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, | 
 | 920 |  *                int flags); | 
 | 921 |  * | 
 | 922 |  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 
 | 923 |  * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 
 | 924 |  * or IPv6 addresses. | 
 | 925 |  * | 
 | 926 |  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 
 | 927 |  * Section 3.1.2 for this usage. | 
 | 928 |  * | 
 | 929 |  * addrs is a pointer to an array of one or more socket addresses. Each | 
 | 930 |  * address is contained in its appropriate structure (i.e. struct | 
 | 931 |  * sockaddr_in or struct sockaddr_in6) the family of the address type | 
| Ville Nuorvala | 23c435f | 2006-10-16 22:08:28 -0700 | [diff] [blame] | 932 |  * must be used to distinguish the address length (note that this | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 |  * representation is termed a "packed array" of addresses). The caller | 
 | 934 |  * specifies the number of addresses in the array with addrcnt. | 
 | 935 |  * | 
 | 936 |  * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns | 
 | 937 |  * -1, and sets errno to the appropriate error code. | 
 | 938 |  * | 
 | 939 |  * For SCTP, the port given in each socket address must be the same, or | 
 | 940 |  * sctp_bindx() will fail, setting errno to EINVAL. | 
 | 941 |  * | 
 | 942 |  * The flags parameter is formed from the bitwise OR of zero or more of | 
 | 943 |  * the following currently defined flags: | 
 | 944 |  * | 
 | 945 |  * SCTP_BINDX_ADD_ADDR | 
 | 946 |  * | 
 | 947 |  * SCTP_BINDX_REM_ADDR | 
 | 948 |  * | 
 | 949 |  * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the | 
 | 950 |  * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given | 
 | 951 |  * addresses from the association. The two flags are mutually exclusive; | 
 | 952 |  * if both are given, sctp_bindx() will fail with EINVAL. A caller may | 
 | 953 |  * not remove all addresses from an association; sctp_bindx() will | 
 | 954 |  * reject such an attempt with EINVAL. | 
 | 955 |  * | 
 | 956 |  * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate | 
 | 957 |  * additional addresses with an endpoint after calling bind().  Or use | 
 | 958 |  * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening | 
 | 959 |  * socket is associated with so that no new association accepted will be | 
 | 960 |  * associated with those addresses. If the endpoint supports dynamic | 
 | 961 |  * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a | 
 | 962 |  * endpoint to send the appropriate message to the peer to change the | 
 | 963 |  * peers address lists. | 
 | 964 |  * | 
 | 965 |  * Adding and removing addresses from a connected association is | 
 | 966 |  * optional functionality. Implementations that do not support this | 
 | 967 |  * functionality should return EOPNOTSUPP. | 
 | 968 |  * | 
 | 969 |  * Basically do nothing but copying the addresses from user to kernel | 
 | 970 |  * 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] | 971 |  * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() | 
 | 972 |  * from userspace. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 |  * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 
 | 975 |  * it. | 
 | 976 |  * | 
 | 977 |  * sk        The sk of the socket | 
 | 978 |  * addrs     The pointer to the addresses in user land | 
 | 979 |  * addrssize Size of the addrs buffer | 
 | 980 |  * op        Operation to perform (add or remove, see the flags of | 
 | 981 |  *           sctp_bindx) | 
 | 982 |  * | 
 | 983 |  * Returns 0 if ok, <0 errno code on error. | 
 | 984 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 985 | static int sctp_setsockopt_bindx(struct sock *sk, | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 986 | 				 struct sockaddr __user *addrs, | 
 | 987 | 				 int addrs_size, int op) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { | 
 | 989 | 	struct sockaddr *kaddrs; | 
 | 990 | 	int err; | 
 | 991 | 	int addrcnt = 0; | 
 | 992 | 	int walk_size = 0; | 
 | 993 | 	struct sockaddr *sa_addr; | 
 | 994 | 	void *addr_buf; | 
 | 995 | 	struct sctp_af *af; | 
 | 996 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 997 | 	pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n", | 
 | 998 | 		 __func__, sk, addrs, addrs_size, op); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 |  | 
 | 1000 | 	if (unlikely(addrs_size <= 0)) | 
 | 1001 | 		return -EINVAL; | 
 | 1002 |  | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1003 | 	kaddrs = vmemdup_user(addrs, addrs_size); | 
 | 1004 | 	if (unlikely(IS_ERR(kaddrs))) | 
 | 1005 | 		return PTR_ERR(kaddrs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1007 | 	/* Walk through the addrs buffer and count the number of addresses. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | 	addr_buf = kaddrs; | 
 | 1009 | 	while (walk_size < addrs_size) { | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1010 | 		if (walk_size + sizeof(sa_family_t) > addrs_size) { | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1011 | 			kvfree(kaddrs); | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1012 | 			return -EINVAL; | 
 | 1013 | 		} | 
 | 1014 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1015 | 		sa_addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | 		af = sctp_get_af_specific(sa_addr->sa_family); | 
 | 1017 |  | 
 | 1018 | 		/* If the address family is not supported or if this address | 
 | 1019 | 		 * causes the address buffer to overflow return EINVAL. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1020 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | 		if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1022 | 			kvfree(kaddrs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | 			return -EINVAL; | 
 | 1024 | 		} | 
 | 1025 | 		addrcnt++; | 
 | 1026 | 		addr_buf += af->sockaddr_len; | 
 | 1027 | 		walk_size += af->sockaddr_len; | 
 | 1028 | 	} | 
 | 1029 |  | 
 | 1030 | 	/* Do the work. */ | 
 | 1031 | 	switch (op) { | 
 | 1032 | 	case SCTP_BINDX_ADD_ADDR: | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1033 | 		/* Allow security module to validate bindx addresses. */ | 
 | 1034 | 		err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD, | 
 | 1035 | 						 (struct sockaddr *)kaddrs, | 
 | 1036 | 						 addrs_size); | 
 | 1037 | 		if (err) | 
 | 1038 | 			goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | 		err = sctp_bindx_add(sk, kaddrs, addrcnt); | 
 | 1040 | 		if (err) | 
 | 1041 | 			goto out; | 
 | 1042 | 		err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); | 
 | 1043 | 		break; | 
 | 1044 |  | 
 | 1045 | 	case SCTP_BINDX_REM_ADDR: | 
 | 1046 | 		err = sctp_bindx_rem(sk, kaddrs, addrcnt); | 
 | 1047 | 		if (err) | 
 | 1048 | 			goto out; | 
 | 1049 | 		err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); | 
 | 1050 | 		break; | 
 | 1051 |  | 
 | 1052 | 	default: | 
 | 1053 | 		err = -EINVAL; | 
 | 1054 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1055 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 |  | 
 | 1057 | out: | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1058 | 	kvfree(kaddrs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 |  | 
 | 1060 | 	return err; | 
 | 1061 | } | 
 | 1062 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1063 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) | 
 | 1064 |  * | 
 | 1065 |  * Common routine for handling connect() and sctp_connectx(). | 
 | 1066 |  * Connect will come in with just a single address. | 
 | 1067 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1068 | static int __sctp_connect(struct sock *sk, | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1069 | 			  struct sockaddr *kaddrs, | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1070 | 			  int addrs_size, int flags, | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1071 | 			  sctp_assoc_t *assoc_id) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1072 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1073 | 	struct net *net = sock_net(sk); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1074 | 	struct sctp_sock *sp; | 
 | 1075 | 	struct sctp_endpoint *ep; | 
 | 1076 | 	struct sctp_association *asoc = NULL; | 
 | 1077 | 	struct sctp_association *asoc2; | 
 | 1078 | 	struct sctp_transport *transport; | 
 | 1079 | 	union sctp_addr to; | 
| Xin Long | 1c66201 | 2017-08-05 19:59:54 +0800 | [diff] [blame] | 1080 | 	enum sctp_scope scope; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1081 | 	long timeo; | 
 | 1082 | 	int err = 0; | 
 | 1083 | 	int addrcnt = 0; | 
 | 1084 | 	int walk_size = 0; | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1085 | 	union sctp_addr *sa_addr = NULL; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1086 | 	void *addr_buf; | 
| Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1087 | 	unsigned short port; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1088 |  | 
 | 1089 | 	sp = sctp_sk(sk); | 
 | 1090 | 	ep = sp->ep; | 
 | 1091 |  | 
 | 1092 | 	/* connect() cannot be done on a socket that is already in ESTABLISHED | 
 | 1093 | 	 * state - UDP-style peeled off socket or a TCP-style socket that | 
 | 1094 | 	 * is already connected. | 
 | 1095 | 	 * It cannot be done even on a TCP-style listening socket. | 
 | 1096 | 	 */ | 
| Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 1097 | 	if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) || | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1098 | 	    (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { | 
 | 1099 | 		err = -EISCONN; | 
 | 1100 | 		goto out_free; | 
 | 1101 | 	} | 
 | 1102 |  | 
 | 1103 | 	/* Walk through the addrs buffer and count the number of addresses. */ | 
 | 1104 | 	addr_buf = kaddrs; | 
 | 1105 | 	while (walk_size < addrs_size) { | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 1106 | 		struct sctp_af *af; | 
 | 1107 |  | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1108 | 		if (walk_size + sizeof(sa_family_t) > addrs_size) { | 
 | 1109 | 			err = -EINVAL; | 
 | 1110 | 			goto out_free; | 
 | 1111 | 		} | 
 | 1112 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1113 | 		sa_addr = addr_buf; | 
| Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 1114 | 		af = sctp_get_af_specific(sa_addr->sa.sa_family); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1115 |  | 
 | 1116 | 		/* If the address family is not supported or if this address | 
 | 1117 | 		 * causes the address buffer to overflow return EINVAL. | 
 | 1118 | 		 */ | 
 | 1119 | 		if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 
 | 1120 | 			err = -EINVAL; | 
 | 1121 | 			goto out_free; | 
 | 1122 | 		} | 
 | 1123 |  | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1124 | 		port = ntohs(sa_addr->v4.sin_port); | 
 | 1125 |  | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1126 | 		/* Save current address so we can work with it */ | 
 | 1127 | 		memcpy(&to, sa_addr, af->sockaddr_len); | 
 | 1128 |  | 
 | 1129 | 		err = sctp_verify_addr(sk, &to, af->sockaddr_len); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1130 | 		if (err) | 
 | 1131 | 			goto out_free; | 
 | 1132 |  | 
| Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1133 | 		/* Make sure the destination port is correctly set | 
 | 1134 | 		 * in all addresses. | 
 | 1135 | 		 */ | 
| Wei Yongjun | 524fba6 | 2013-04-03 03:02:28 +0000 | [diff] [blame] | 1136 | 		if (asoc && asoc->peer.port && asoc->peer.port != port) { | 
 | 1137 | 			err = -EINVAL; | 
| Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1138 | 			goto out_free; | 
| Wei Yongjun | 524fba6 | 2013-04-03 03:02:28 +0000 | [diff] [blame] | 1139 | 		} | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1140 |  | 
 | 1141 | 		/* Check if there already is a matching association on the | 
 | 1142 | 		 * endpoint (other than the one created here). | 
 | 1143 | 		 */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1144 | 		asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1145 | 		if (asoc2 && asoc2 != asoc) { | 
 | 1146 | 			if (asoc2->state >= SCTP_STATE_ESTABLISHED) | 
 | 1147 | 				err = -EISCONN; | 
 | 1148 | 			else | 
 | 1149 | 				err = -EALREADY; | 
 | 1150 | 			goto out_free; | 
 | 1151 | 		} | 
 | 1152 |  | 
 | 1153 | 		/* If we could not find a matching association on the endpoint, | 
 | 1154 | 		 * make sure that there is no peeled-off association matching | 
 | 1155 | 		 * the peer address even on another socket. | 
 | 1156 | 		 */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1157 | 		if (sctp_endpoint_is_peeled_off(ep, &to)) { | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1158 | 			err = -EADDRNOTAVAIL; | 
 | 1159 | 			goto out_free; | 
 | 1160 | 		} | 
 | 1161 |  | 
 | 1162 | 		if (!asoc) { | 
 | 1163 | 			/* If a bind() or sctp_bindx() is not called prior to | 
 | 1164 | 			 * an sctp_connectx() call, the system picks an | 
 | 1165 | 			 * ephemeral port and will choose an address set | 
 | 1166 | 			 * equivalent to binding with a wildcard address. | 
 | 1167 | 			 */ | 
 | 1168 | 			if (!ep->base.bind_addr.port) { | 
 | 1169 | 				if (sctp_autobind(sk)) { | 
 | 1170 | 					err = -EAGAIN; | 
 | 1171 | 					goto out_free; | 
 | 1172 | 				} | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1173 | 			} else { | 
 | 1174 | 				/* | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1175 | 				 * If an unprivileged user inherits a 1-many | 
 | 1176 | 				 * style socket with open associations on a | 
 | 1177 | 				 * privileged port, it MAY be permitted to | 
 | 1178 | 				 * accept new associations, but it SHOULD NOT | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1179 | 				 * be permitted to open new associations. | 
 | 1180 | 				 */ | 
| Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 1181 | 				if (ep->base.bind_addr.port < | 
 | 1182 | 				    inet_prot_sock(net) && | 
 | 1183 | 				    !ns_capable(net->user_ns, | 
 | 1184 | 				    CAP_NET_BIND_SERVICE)) { | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1185 | 					err = -EACCES; | 
 | 1186 | 					goto out_free; | 
 | 1187 | 				} | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1188 | 			} | 
 | 1189 |  | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1190 | 			scope = sctp_scope(&to); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1191 | 			asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 
 | 1192 | 			if (!asoc) { | 
 | 1193 | 				err = -ENOMEM; | 
 | 1194 | 				goto out_free; | 
 | 1195 | 			} | 
| Vlad Yasevich | 409b95a | 2009-11-10 08:57:34 +0000 | [diff] [blame] | 1196 |  | 
 | 1197 | 			err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, | 
 | 1198 | 							      GFP_KERNEL); | 
 | 1199 | 			if (err < 0) { | 
 | 1200 | 				goto out_free; | 
 | 1201 | 			} | 
 | 1202 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1203 | 		} | 
 | 1204 |  | 
 | 1205 | 		/* Prime the peer's transport structures.  */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1206 | 		transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1207 | 						SCTP_UNKNOWN); | 
 | 1208 | 		if (!transport) { | 
 | 1209 | 			err = -ENOMEM; | 
 | 1210 | 			goto out_free; | 
 | 1211 | 		} | 
 | 1212 |  | 
 | 1213 | 		addrcnt++; | 
 | 1214 | 		addr_buf += af->sockaddr_len; | 
 | 1215 | 		walk_size += af->sockaddr_len; | 
 | 1216 | 	} | 
 | 1217 |  | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1218 | 	/* In case the user of sctp_connectx() wants an association | 
 | 1219 | 	 * id back, assign one now. | 
 | 1220 | 	 */ | 
 | 1221 | 	if (assoc_id) { | 
 | 1222 | 		err = sctp_assoc_set_id(asoc, GFP_KERNEL); | 
 | 1223 | 		if (err < 0) | 
 | 1224 | 			goto out_free; | 
 | 1225 | 	} | 
 | 1226 |  | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1227 | 	err = sctp_primitive_ASSOCIATE(net, asoc, NULL); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1228 | 	if (err < 0) { | 
 | 1229 | 		goto out_free; | 
 | 1230 | 	} | 
 | 1231 |  | 
 | 1232 | 	/* Initialize sk's dport and daddr for getpeername() */ | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1233 | 	inet_sk(sk)->inet_dport = htons(asoc->peer.port); | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 1234 | 	sp->pf->to_sk_daddr(sa_addr, sk); | 
| Sridhar Samudrala | 8de8c87 | 2006-05-19 10:58:12 -0700 | [diff] [blame] | 1235 | 	sk->sk_err = 0; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1236 |  | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1237 | 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); | 
| Vlad Yasevich | f50f95c | 2007-07-03 12:47:40 -0400 | [diff] [blame] | 1238 |  | 
| Marcelo Ricardo Leitner | 7233bc8 | 2016-11-03 17:03:41 -0200 | [diff] [blame] | 1239 | 	if (assoc_id) | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1240 | 		*assoc_id = asoc->assoc_id; | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1241 |  | 
| Marcelo Ricardo Leitner | 7233bc8 | 2016-11-03 17:03:41 -0200 | [diff] [blame] | 1242 | 	err = sctp_wait_for_connect(asoc, &timeo); | 
 | 1243 | 	/* Note: the asoc may be freed after the return of | 
 | 1244 | 	 * sctp_wait_for_connect. | 
 | 1245 | 	 */ | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1246 |  | 
 | 1247 | 	/* Don't free association on exit. */ | 
 | 1248 | 	asoc = NULL; | 
 | 1249 |  | 
 | 1250 | out_free: | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1251 | 	pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n", | 
 | 1252 | 		 __func__, asoc, kaddrs, err); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1253 |  | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1254 | 	if (asoc) { | 
 | 1255 | 		/* sctp_primitive_ASSOCIATE may have added this association | 
 | 1256 | 		 * To the hash table, try to unhash it, just in case, its a noop | 
 | 1257 | 		 * if it wasn't hashed so we're safe | 
 | 1258 | 		 */ | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1259 | 		sctp_association_free(asoc); | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1260 | 	} | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1261 | 	return err; | 
 | 1262 | } | 
 | 1263 |  | 
 | 1264 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() | 
 | 1265 |  * | 
 | 1266 |  * API 8.9 | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1267 |  * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, | 
 | 1268 |  * 			sctp_assoc_t *asoc); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1269 |  * | 
 | 1270 |  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 
 | 1271 |  * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 
 | 1272 |  * or IPv6 addresses. | 
 | 1273 |  * | 
 | 1274 |  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 
 | 1275 |  * Section 3.1.2 for this usage. | 
 | 1276 |  * | 
 | 1277 |  * addrs is a pointer to an array of one or more socket addresses. Each | 
 | 1278 |  * address is contained in its appropriate structure (i.e. struct | 
 | 1279 |  * sockaddr_in or struct sockaddr_in6) the family of the address type | 
 | 1280 |  * must be used to distengish the address length (note that this | 
 | 1281 |  * representation is termed a "packed array" of addresses). The caller | 
 | 1282 |  * specifies the number of addresses in the array with addrcnt. | 
 | 1283 |  * | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1284 |  * On success, sctp_connectx() returns 0. It also sets the assoc_id to | 
 | 1285 |  * the association id of the new association.  On failure, sctp_connectx() | 
 | 1286 |  * returns -1, and sets errno to the appropriate error code.  The assoc_id | 
 | 1287 |  * is not touched by the kernel. | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1288 |  * | 
 | 1289 |  * For SCTP, the port given in each socket address must be the same, or | 
 | 1290 |  * sctp_connectx() will fail, setting errno to EINVAL. | 
 | 1291 |  * | 
 | 1292 |  * An application can use sctp_connectx to initiate an association with | 
 | 1293 |  * an endpoint that is multi-homed.  Much like sctp_bindx() this call | 
 | 1294 |  * allows a caller to specify multiple addresses at which a peer can be | 
 | 1295 |  * 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] | 1296 |  * the association is implementation dependent.  This function only | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1297 |  * specifies that the stack will try to make use of all the addresses in | 
 | 1298 |  * the list when needed. | 
 | 1299 |  * | 
 | 1300 |  * Note that the list of addresses passed in is only used for setting up | 
 | 1301 |  * the association.  It does not necessarily equal the set of addresses | 
 | 1302 |  * the peer uses for the resulting association.  If the caller wants to | 
 | 1303 |  * find out the set of peer addresses, it must use sctp_getpaddrs() to | 
 | 1304 |  * retrieve them after the association has been set up. | 
 | 1305 |  * | 
 | 1306 |  * Basically do nothing but copying the addresses from user to kernel | 
 | 1307 |  * land and invoking either sctp_connectx(). This is used for tunneling | 
 | 1308 |  * the sctp_connectx() request through sctp_setsockopt() from userspace. | 
 | 1309 |  * | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1310 |  * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 
 | 1311 |  * it. | 
 | 1312 |  * | 
 | 1313 |  * sk        The sk of the socket | 
 | 1314 |  * addrs     The pointer to the addresses in user land | 
 | 1315 |  * addrssize Size of the addrs buffer | 
 | 1316 |  * | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1317 |  * Returns >=0 if ok, <0 errno code on error. | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1318 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1319 | static int __sctp_setsockopt_connectx(struct sock *sk, | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1320 | 				      struct sockaddr __user *addrs, | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1321 | 				      int addrs_size, | 
 | 1322 | 				      sctp_assoc_t *assoc_id) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1323 | { | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1324 | 	struct sockaddr *kaddrs; | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1325 | 	int err = 0, flags = 0; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1326 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1327 | 	pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n", | 
 | 1328 | 		 __func__, sk, addrs, addrs_size); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1329 |  | 
 | 1330 | 	if (unlikely(addrs_size <= 0)) | 
 | 1331 | 		return -EINVAL; | 
 | 1332 |  | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1333 | 	kaddrs = vmemdup_user(addrs, addrs_size); | 
 | 1334 | 	if (unlikely(IS_ERR(kaddrs))) | 
 | 1335 | 		return PTR_ERR(kaddrs); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1336 |  | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1337 | 	/* Allow security module to validate connectx addresses. */ | 
 | 1338 | 	err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX, | 
 | 1339 | 					 (struct sockaddr *)kaddrs, | 
 | 1340 | 					  addrs_size); | 
 | 1341 | 	if (err) | 
 | 1342 | 		goto out_free; | 
 | 1343 |  | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1344 | 	/* in-kernel sockets don't generally have a file allocated to them | 
 | 1345 | 	 * if all they do is call sock_create_kern(). | 
 | 1346 | 	 */ | 
 | 1347 | 	if (sk->sk_socket->file) | 
 | 1348 | 		flags = sk->sk_socket->file->f_flags; | 
 | 1349 |  | 
 | 1350 | 	err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id); | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1351 |  | 
 | 1352 | out_free: | 
| Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1353 | 	kvfree(kaddrs); | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1354 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1355 | 	return err; | 
 | 1356 | } | 
 | 1357 |  | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1358 | /* | 
 | 1359 |  * This is an older interface.  It's kept for backward compatibility | 
 | 1360 |  * to the option that doesn't provide association id. | 
 | 1361 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1362 | static int sctp_setsockopt_connectx_old(struct sock *sk, | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1363 | 					struct sockaddr __user *addrs, | 
 | 1364 | 					int addrs_size) | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1365 | { | 
 | 1366 | 	return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); | 
 | 1367 | } | 
 | 1368 |  | 
 | 1369 | /* | 
 | 1370 |  * New interface for the API.  The since the API is done with a socket | 
 | 1371 |  * option, to make it simple we feed back the association id is as a return | 
 | 1372 |  * indication to the call.  Error is always negative and association id is | 
 | 1373 |  * always positive. | 
 | 1374 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1375 | static int sctp_setsockopt_connectx(struct sock *sk, | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1376 | 				    struct sockaddr __user *addrs, | 
 | 1377 | 				    int addrs_size) | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1378 | { | 
 | 1379 | 	sctp_assoc_t assoc_id = 0; | 
 | 1380 | 	int err = 0; | 
 | 1381 |  | 
 | 1382 | 	err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); | 
 | 1383 |  | 
 | 1384 | 	if (err) | 
 | 1385 | 		return err; | 
 | 1386 | 	else | 
 | 1387 | 		return assoc_id; | 
 | 1388 | } | 
 | 1389 |  | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1390 | /* | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1391 |  * New (hopefully final) interface for the API. | 
 | 1392 |  * We use the sctp_getaddrs_old structure so that use-space library | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1393 |  * can avoid any unnecessary allocations. The only different part | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1394 |  * 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] | 1395 |  * addrs_num structure member. That way we can re-use the existing | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1396 |  * code. | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1397 |  */ | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1398 | #ifdef CONFIG_COMPAT | 
 | 1399 | struct compat_sctp_getaddrs_old { | 
 | 1400 | 	sctp_assoc_t	assoc_id; | 
 | 1401 | 	s32		addr_num; | 
 | 1402 | 	compat_uptr_t	addrs;		/* struct sockaddr * */ | 
 | 1403 | }; | 
 | 1404 | #endif | 
 | 1405 |  | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1406 | static int sctp_getsockopt_connectx3(struct sock *sk, int len, | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1407 | 				     char __user *optval, | 
 | 1408 | 				     int __user *optlen) | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1409 | { | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1410 | 	struct sctp_getaddrs_old param; | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1411 | 	sctp_assoc_t assoc_id = 0; | 
 | 1412 | 	int err = 0; | 
 | 1413 |  | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1414 | #ifdef CONFIG_COMPAT | 
| Andy Lutomirski | 96c0e0a | 2016-03-22 14:25:07 -0700 | [diff] [blame] | 1415 | 	if (in_compat_syscall()) { | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1416 | 		struct compat_sctp_getaddrs_old param32; | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1417 |  | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1418 | 		if (len < sizeof(param32)) | 
 | 1419 | 			return -EINVAL; | 
 | 1420 | 		if (copy_from_user(¶m32, optval, sizeof(param32))) | 
 | 1421 | 			return -EFAULT; | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1422 |  | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1423 | 		param.assoc_id = param32.assoc_id; | 
 | 1424 | 		param.addr_num = param32.addr_num; | 
 | 1425 | 		param.addrs = compat_ptr(param32.addrs); | 
 | 1426 | 	} else | 
 | 1427 | #endif | 
 | 1428 | 	{ | 
 | 1429 | 		if (len < sizeof(param)) | 
 | 1430 | 			return -EINVAL; | 
 | 1431 | 		if (copy_from_user(¶m, optval, sizeof(param))) | 
 | 1432 | 			return -EFAULT; | 
 | 1433 | 	} | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1434 |  | 
| Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1435 | 	err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *) | 
 | 1436 | 					 param.addrs, param.addr_num, | 
 | 1437 | 					 &assoc_id); | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1438 | 	if (err == 0 || err == -EINPROGRESS) { | 
 | 1439 | 		if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) | 
 | 1440 | 			return -EFAULT; | 
 | 1441 | 		if (put_user(sizeof(assoc_id), optlen)) | 
 | 1442 | 			return -EFAULT; | 
 | 1443 | 	} | 
 | 1444 |  | 
 | 1445 | 	return err; | 
 | 1446 | } | 
 | 1447 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | /* API 3.1.4 close() - UDP Style Syntax | 
 | 1449 |  * Applications use close() to perform graceful shutdown (as described in | 
 | 1450 |  * Section 10.1 of [SCTP]) on ALL the associations currently represented | 
 | 1451 |  * by a UDP-style socket. | 
 | 1452 |  * | 
 | 1453 |  * The syntax is | 
 | 1454 |  * | 
 | 1455 |  *   ret = close(int sd); | 
 | 1456 |  * | 
 | 1457 |  *   sd      - the socket descriptor of the associations to be closed. | 
 | 1458 |  * | 
 | 1459 |  * To gracefully shutdown a specific association represented by the | 
 | 1460 |  * UDP-style socket, an application should use the sendmsg() call, | 
 | 1461 |  * passing no user data, but including the appropriate flag in the | 
 | 1462 |  * ancillary data (see Section xxxx). | 
 | 1463 |  * | 
 | 1464 |  * If sd in the close() call is a branched-off socket representing only | 
 | 1465 |  * one association, the shutdown is performed on that association only. | 
 | 1466 |  * | 
 | 1467 |  * 4.1.6 close() - TCP Style Syntax | 
 | 1468 |  * | 
 | 1469 |  * Applications use close() to gracefully close down an association. | 
 | 1470 |  * | 
 | 1471 |  * The syntax is: | 
 | 1472 |  * | 
 | 1473 |  *    int close(int sd); | 
 | 1474 |  * | 
 | 1475 |  *      sd      - the socket descriptor of the association to be closed. | 
 | 1476 |  * | 
 | 1477 |  * After an application calls close() on a socket descriptor, no further | 
 | 1478 |  * socket operations will succeed on that descriptor. | 
 | 1479 |  * | 
 | 1480 |  * API 7.1.4 SO_LINGER | 
 | 1481 |  * | 
 | 1482 |  * An application using the TCP-style socket can use this option to | 
 | 1483 |  * perform the SCTP ABORT primitive.  The linger option structure is: | 
 | 1484 |  * | 
 | 1485 |  *  struct  linger { | 
 | 1486 |  *     int     l_onoff;                // option on/off | 
 | 1487 |  *     int     l_linger;               // linger time | 
 | 1488 |  * }; | 
 | 1489 |  * | 
 | 1490 |  * To enable the option, set l_onoff to 1.  If the l_linger value is set | 
 | 1491 |  * to 0, calling close() is the same as the ABORT primitive.  If the | 
 | 1492 |  * value is set to a negative value, the setsockopt() call will return | 
 | 1493 |  * an error.  If the value is set to a positive value linger_time, the | 
 | 1494 |  * close() can be blocked for at most linger_time ms.  If the graceful | 
 | 1495 |  * shutdown phase does not finish during this period, close() will | 
 | 1496 |  * return but the graceful shutdown phase continues in the system. | 
 | 1497 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1498 | static void sctp_close(struct sock *sk, long timeout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1500 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | 	struct sctp_endpoint *ep; | 
 | 1502 | 	struct sctp_association *asoc; | 
 | 1503 | 	struct list_head *pos, *temp; | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1504 | 	unsigned int data_was_unread; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1506 | 	pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 |  | 
| Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1508 | 	lock_sock_nested(sk, SINGLE_DEPTH_NESTING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | 	sk->sk_shutdown = SHUTDOWN_MASK; | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 1510 | 	inet_sk_set_state(sk, SCTP_SS_CLOSING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 |  | 
 | 1512 | 	ep = sctp_sk(sk)->ep; | 
 | 1513 |  | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1514 | 	/* Clean up any skbs sitting on the receive queue.  */ | 
 | 1515 | 	data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); | 
 | 1516 | 	data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); | 
 | 1517 |  | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 1518 | 	/* Walk all associations on an endpoint.  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | 	list_for_each_safe(pos, temp, &ep->asocs) { | 
 | 1520 | 		asoc = list_entry(pos, struct sctp_association, asocs); | 
 | 1521 |  | 
 | 1522 | 		if (sctp_style(sk, TCP)) { | 
 | 1523 | 			/* A closed association can still be in the list if | 
 | 1524 | 			 * it belongs to a TCP-style listening socket that is | 
 | 1525 | 			 * not yet accepted. If so, free it. If not, send an | 
 | 1526 | 			 * ABORT or SHUTDOWN based on the linger options. | 
 | 1527 | 			 */ | 
 | 1528 | 			if (sctp_state(asoc, CLOSED)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | 				sctp_association_free(asoc); | 
| Vladislav Yasevich | b89498a | 2006-05-19 14:32:06 -0700 | [diff] [blame] | 1530 | 				continue; | 
 | 1531 | 			} | 
 | 1532 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 |  | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1534 | 		if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || | 
 | 1535 | 		    !skb_queue_empty(&asoc->ulpq.reasm) || | 
| Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 1536 | 		    !skb_queue_empty(&asoc->ulpq.reasm_uo) || | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1537 | 		    (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { | 
| Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1538 | 			struct sctp_chunk *chunk; | 
 | 1539 |  | 
 | 1540 | 			chunk = sctp_make_abort_user(asoc, NULL, 0); | 
| Xin Long | 068d8bd | 2015-12-29 17:49:25 +0800 | [diff] [blame] | 1541 | 			sctp_primitive_ABORT(net, asoc, chunk); | 
| Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1542 | 		} else | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1543 | 			sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | 	} | 
 | 1545 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | 	/* On a TCP-style socket, block for at most linger_time if set. */ | 
 | 1547 | 	if (sctp_style(sk, TCP) && timeout) | 
 | 1548 | 		sctp_wait_for_close(sk, timeout); | 
 | 1549 |  | 
 | 1550 | 	/* This will run the backlog queue.  */ | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 1551 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 |  | 
 | 1553 | 	/* Supposedly, no process has access to the socket, but | 
 | 1554 | 	 * the net layers still may. | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1555 | 	 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock | 
 | 1556 | 	 * held and that should be grabbed before socket lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | 	 */ | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1558 | 	spin_lock_bh(&net->sctp.addr_wq_lock); | 
| Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1559 | 	bh_lock_sock_nested(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 |  | 
 | 1561 | 	/* Hold the sock, since sk_common_release() will put sock_put() | 
 | 1562 | 	 * and we have just a little more cleanup. | 
 | 1563 | 	 */ | 
 | 1564 | 	sock_hold(sk); | 
 | 1565 | 	sk_common_release(sk); | 
 | 1566 |  | 
| wangweidong | 5bc1d1b | 2014-01-21 15:44:12 +0800 | [diff] [blame] | 1567 | 	bh_unlock_sock(sk); | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1568 | 	spin_unlock_bh(&net->sctp.addr_wq_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 |  | 
 | 1570 | 	sock_put(sk); | 
 | 1571 |  | 
 | 1572 | 	SCTP_DBG_OBJCNT_DEC(sock); | 
 | 1573 | } | 
 | 1574 |  | 
 | 1575 | /* Handle EPIPE error. */ | 
 | 1576 | static int sctp_error(struct sock *sk, int flags, int err) | 
 | 1577 | { | 
 | 1578 | 	if (err == -EPIPE) | 
 | 1579 | 		err = sock_error(sk) ? : -EPIPE; | 
 | 1580 | 	if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) | 
 | 1581 | 		send_sig(SIGPIPE, current, 0); | 
 | 1582 | 	return err; | 
 | 1583 | } | 
 | 1584 |  | 
 | 1585 | /* API 3.1.3 sendmsg() - UDP Style Syntax | 
 | 1586 |  * | 
 | 1587 |  * An application uses sendmsg() and recvmsg() calls to transmit data to | 
 | 1588 |  * and receive data from its peer. | 
 | 1589 |  * | 
 | 1590 |  *  ssize_t sendmsg(int socket, const struct msghdr *message, | 
 | 1591 |  *                  int flags); | 
 | 1592 |  * | 
 | 1593 |  *  socket  - the socket descriptor of the endpoint. | 
 | 1594 |  *  message - pointer to the msghdr structure which contains a single | 
 | 1595 |  *            user message and possibly some ancillary data. | 
 | 1596 |  * | 
 | 1597 |  *            See Section 5 for complete description of the data | 
 | 1598 |  *            structures. | 
 | 1599 |  * | 
 | 1600 |  *  flags   - flags sent or received with the user message, see Section | 
 | 1601 |  *            5 for complete description of the flags. | 
 | 1602 |  * | 
 | 1603 |  * Note:  This function could use a rewrite especially when explicit | 
 | 1604 |  * connect support comes in. | 
 | 1605 |  */ | 
 | 1606 | /* BUG:  We do not implement the equivalent of sk_stream_wait_memory(). */ | 
 | 1607 |  | 
| Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 1608 | static int sctp_msghdr_parse(const struct msghdr *msg, | 
 | 1609 | 			     struct sctp_cmsgs *cmsgs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 |  | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1611 | static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs, | 
 | 1612 | 			      struct sctp_sndrcvinfo *srinfo, | 
 | 1613 | 			      const struct msghdr *msg, size_t msg_len) | 
 | 1614 | { | 
 | 1615 | 	__u16 sflags; | 
 | 1616 | 	int err; | 
 | 1617 |  | 
 | 1618 | 	if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP)) | 
 | 1619 | 		return -EPIPE; | 
 | 1620 |  | 
 | 1621 | 	if (msg_len > sk->sk_sndbuf) | 
 | 1622 | 		return -EMSGSIZE; | 
 | 1623 |  | 
 | 1624 | 	memset(cmsgs, 0, sizeof(*cmsgs)); | 
 | 1625 | 	err = sctp_msghdr_parse(msg, cmsgs); | 
 | 1626 | 	if (err) { | 
 | 1627 | 		pr_debug("%s: msghdr parse err:%x\n", __func__, err); | 
 | 1628 | 		return err; | 
 | 1629 | 	} | 
 | 1630 |  | 
 | 1631 | 	memset(srinfo, 0, sizeof(*srinfo)); | 
 | 1632 | 	if (cmsgs->srinfo) { | 
 | 1633 | 		srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream; | 
 | 1634 | 		srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags; | 
 | 1635 | 		srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid; | 
 | 1636 | 		srinfo->sinfo_context = cmsgs->srinfo->sinfo_context; | 
 | 1637 | 		srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id; | 
 | 1638 | 		srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive; | 
 | 1639 | 	} | 
 | 1640 |  | 
 | 1641 | 	if (cmsgs->sinfo) { | 
 | 1642 | 		srinfo->sinfo_stream = cmsgs->sinfo->snd_sid; | 
 | 1643 | 		srinfo->sinfo_flags = cmsgs->sinfo->snd_flags; | 
 | 1644 | 		srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid; | 
 | 1645 | 		srinfo->sinfo_context = cmsgs->sinfo->snd_context; | 
 | 1646 | 		srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id; | 
 | 1647 | 	} | 
 | 1648 |  | 
| Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 1649 | 	if (cmsgs->prinfo) { | 
 | 1650 | 		srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value; | 
 | 1651 | 		SCTP_PR_SET_POLICY(srinfo->sinfo_flags, | 
 | 1652 | 				   cmsgs->prinfo->pr_policy); | 
 | 1653 | 	} | 
 | 1654 |  | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1655 | 	sflags = srinfo->sinfo_flags; | 
 | 1656 | 	if (!sflags && msg_len) | 
 | 1657 | 		return 0; | 
 | 1658 |  | 
 | 1659 | 	if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT))) | 
 | 1660 | 		return -EINVAL; | 
 | 1661 |  | 
 | 1662 | 	if (((sflags & SCTP_EOF) && msg_len > 0) || | 
 | 1663 | 	    (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0)) | 
 | 1664 | 		return -EINVAL; | 
 | 1665 |  | 
 | 1666 | 	if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name) | 
 | 1667 | 		return -EINVAL; | 
 | 1668 |  | 
 | 1669 | 	return 0; | 
 | 1670 | } | 
 | 1671 |  | 
| Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1672 | static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, | 
 | 1673 | 				 struct sctp_cmsgs *cmsgs, | 
 | 1674 | 				 union sctp_addr *daddr, | 
 | 1675 | 				 struct sctp_transport **tp) | 
 | 1676 | { | 
 | 1677 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
 | 1678 | 	struct net *net = sock_net(sk); | 
 | 1679 | 	struct sctp_association *asoc; | 
 | 1680 | 	enum sctp_scope scope; | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1681 | 	struct cmsghdr *cmsg; | 
| Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1682 | 	__be32 flowinfo = 0; | 
| Linus Torvalds | 9eda2d2 | 2018-04-06 15:39:26 -0700 | [diff] [blame] | 1683 | 	struct sctp_af *af; | 
| Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1684 | 	int err; | 
| Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1685 |  | 
 | 1686 | 	*tp = NULL; | 
 | 1687 |  | 
 | 1688 | 	if (sflags & (SCTP_EOF | SCTP_ABORT)) | 
 | 1689 | 		return -EINVAL; | 
 | 1690 |  | 
 | 1691 | 	if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) || | 
 | 1692 | 				    sctp_sstate(sk, CLOSING))) | 
 | 1693 | 		return -EADDRNOTAVAIL; | 
 | 1694 |  | 
 | 1695 | 	if (sctp_endpoint_is_peeled_off(ep, daddr)) | 
 | 1696 | 		return -EADDRNOTAVAIL; | 
 | 1697 |  | 
 | 1698 | 	if (!ep->base.bind_addr.port) { | 
 | 1699 | 		if (sctp_autobind(sk)) | 
 | 1700 | 			return -EAGAIN; | 
 | 1701 | 	} else { | 
 | 1702 | 		if (ep->base.bind_addr.port < inet_prot_sock(net) && | 
 | 1703 | 		    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) | 
 | 1704 | 			return -EACCES; | 
 | 1705 | 	} | 
 | 1706 |  | 
 | 1707 | 	scope = sctp_scope(daddr); | 
 | 1708 |  | 
| Linus Torvalds | 9eda2d2 | 2018-04-06 15:39:26 -0700 | [diff] [blame] | 1709 | 	/* Label connection socket for first association 1-to-many | 
 | 1710 | 	 * style for client sequence socket()->sendmsg(). This | 
 | 1711 | 	 * needs to be done before sctp_assoc_add_peer() as that will | 
 | 1712 | 	 * set up the initial packet that needs to account for any | 
 | 1713 | 	 * security ip options (CIPSO/CALIPSO) added to the packet. | 
 | 1714 | 	 */ | 
 | 1715 | 	af = sctp_get_af_specific(daddr->sa.sa_family); | 
 | 1716 | 	if (!af) | 
 | 1717 | 		return -EINVAL; | 
 | 1718 | 	err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT, | 
 | 1719 | 					 (struct sockaddr *)daddr, | 
 | 1720 | 					 af->sockaddr_len); | 
 | 1721 | 	if (err < 0) | 
 | 1722 | 		return err; | 
 | 1723 |  | 
| Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1724 | 	asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 
 | 1725 | 	if (!asoc) | 
 | 1726 | 		return -ENOMEM; | 
 | 1727 |  | 
 | 1728 | 	if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) { | 
 | 1729 | 		err = -ENOMEM; | 
 | 1730 | 		goto free; | 
 | 1731 | 	} | 
 | 1732 |  | 
 | 1733 | 	if (cmsgs->init) { | 
 | 1734 | 		struct sctp_initmsg *init = cmsgs->init; | 
 | 1735 |  | 
 | 1736 | 		if (init->sinit_num_ostreams) { | 
 | 1737 | 			__u16 outcnt = init->sinit_num_ostreams; | 
 | 1738 |  | 
 | 1739 | 			asoc->c.sinit_num_ostreams = outcnt; | 
 | 1740 | 			/* outcnt has been changed, need to re-init stream */ | 
 | 1741 | 			err = sctp_stream_init(&asoc->stream, outcnt, 0, | 
 | 1742 | 					       GFP_KERNEL); | 
 | 1743 | 			if (err) | 
 | 1744 | 				goto free; | 
 | 1745 | 		} | 
 | 1746 |  | 
 | 1747 | 		if (init->sinit_max_instreams) | 
 | 1748 | 			asoc->c.sinit_max_instreams = init->sinit_max_instreams; | 
 | 1749 |  | 
 | 1750 | 		if (init->sinit_max_attempts) | 
 | 1751 | 			asoc->max_init_attempts = init->sinit_max_attempts; | 
 | 1752 |  | 
 | 1753 | 		if (init->sinit_max_init_timeo) | 
 | 1754 | 			asoc->max_init_timeo = | 
 | 1755 | 				msecs_to_jiffies(init->sinit_max_init_timeo); | 
 | 1756 | 	} | 
 | 1757 |  | 
 | 1758 | 	*tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); | 
 | 1759 | 	if (!*tp) { | 
 | 1760 | 		err = -ENOMEM; | 
 | 1761 | 		goto free; | 
 | 1762 | 	} | 
 | 1763 |  | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1764 | 	if (!cmsgs->addrs_msg) | 
 | 1765 | 		return 0; | 
 | 1766 |  | 
| Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1767 | 	if (daddr->sa.sa_family == AF_INET6) | 
 | 1768 | 		flowinfo = daddr->v6.sin6_flowinfo; | 
 | 1769 |  | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1770 | 	/* sendv addr list parse */ | 
 | 1771 | 	for_each_cmsghdr(cmsg, cmsgs->addrs_msg) { | 
 | 1772 | 		struct sctp_transport *transport; | 
 | 1773 | 		struct sctp_association *old; | 
 | 1774 | 		union sctp_addr _daddr; | 
 | 1775 | 		int dlen; | 
 | 1776 |  | 
 | 1777 | 		if (cmsg->cmsg_level != IPPROTO_SCTP || | 
 | 1778 | 		    (cmsg->cmsg_type != SCTP_DSTADDRV4 && | 
 | 1779 | 		     cmsg->cmsg_type != SCTP_DSTADDRV6)) | 
 | 1780 | 			continue; | 
 | 1781 |  | 
 | 1782 | 		daddr = &_daddr; | 
 | 1783 | 		memset(daddr, 0, sizeof(*daddr)); | 
 | 1784 | 		dlen = cmsg->cmsg_len - sizeof(struct cmsghdr); | 
 | 1785 | 		if (cmsg->cmsg_type == SCTP_DSTADDRV4) { | 
| Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1786 | 			if (dlen < sizeof(struct in_addr)) { | 
 | 1787 | 				err = -EINVAL; | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1788 | 				goto free; | 
| Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1789 | 			} | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1790 |  | 
 | 1791 | 			dlen = sizeof(struct in_addr); | 
 | 1792 | 			daddr->v4.sin_family = AF_INET; | 
 | 1793 | 			daddr->v4.sin_port = htons(asoc->peer.port); | 
 | 1794 | 			memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen); | 
 | 1795 | 		} else { | 
| Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1796 | 			if (dlen < sizeof(struct in6_addr)) { | 
 | 1797 | 				err = -EINVAL; | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1798 | 				goto free; | 
| Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1799 | 			} | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1800 |  | 
 | 1801 | 			dlen = sizeof(struct in6_addr); | 
| Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1802 | 			daddr->v6.sin6_flowinfo = flowinfo; | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1803 | 			daddr->v6.sin6_family = AF_INET6; | 
 | 1804 | 			daddr->v6.sin6_port = htons(asoc->peer.port); | 
 | 1805 | 			memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen); | 
 | 1806 | 		} | 
 | 1807 | 		err = sctp_verify_addr(sk, daddr, sizeof(*daddr)); | 
 | 1808 | 		if (err) | 
 | 1809 | 			goto free; | 
 | 1810 |  | 
 | 1811 | 		old = sctp_endpoint_lookup_assoc(ep, daddr, &transport); | 
 | 1812 | 		if (old && old != asoc) { | 
 | 1813 | 			if (old->state >= SCTP_STATE_ESTABLISHED) | 
 | 1814 | 				err = -EISCONN; | 
 | 1815 | 			else | 
 | 1816 | 				err = -EALREADY; | 
 | 1817 | 			goto free; | 
 | 1818 | 		} | 
 | 1819 |  | 
 | 1820 | 		if (sctp_endpoint_is_peeled_off(ep, daddr)) { | 
 | 1821 | 			err = -EADDRNOTAVAIL; | 
 | 1822 | 			goto free; | 
 | 1823 | 		} | 
 | 1824 |  | 
 | 1825 | 		transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, | 
 | 1826 | 						SCTP_UNKNOWN); | 
 | 1827 | 		if (!transport) { | 
 | 1828 | 			err = -ENOMEM; | 
 | 1829 | 			goto free; | 
 | 1830 | 		} | 
 | 1831 | 	} | 
 | 1832 |  | 
| Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1833 | 	return 0; | 
 | 1834 |  | 
 | 1835 | free: | 
 | 1836 | 	sctp_association_free(asoc); | 
 | 1837 | 	return err; | 
 | 1838 | } | 
 | 1839 |  | 
| Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 1840 | static int sctp_sendmsg_check_sflags(struct sctp_association *asoc, | 
 | 1841 | 				     __u16 sflags, struct msghdr *msg, | 
 | 1842 | 				     size_t msg_len) | 
 | 1843 | { | 
 | 1844 | 	struct sock *sk = asoc->base.sk; | 
 | 1845 | 	struct net *net = sock_net(sk); | 
 | 1846 |  | 
 | 1847 | 	if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) | 
 | 1848 | 		return -EPIPE; | 
 | 1849 |  | 
| Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 1850 | 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) && | 
 | 1851 | 	    !sctp_state(asoc, ESTABLISHED)) | 
 | 1852 | 		return 0; | 
 | 1853 |  | 
| Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 1854 | 	if (sflags & SCTP_EOF) { | 
 | 1855 | 		pr_debug("%s: shutting down association:%p\n", __func__, asoc); | 
 | 1856 | 		sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
 | 1857 |  | 
 | 1858 | 		return 0; | 
 | 1859 | 	} | 
 | 1860 |  | 
 | 1861 | 	if (sflags & SCTP_ABORT) { | 
 | 1862 | 		struct sctp_chunk *chunk; | 
 | 1863 |  | 
 | 1864 | 		chunk = sctp_make_abort_user(asoc, msg, msg_len); | 
 | 1865 | 		if (!chunk) | 
 | 1866 | 			return -ENOMEM; | 
 | 1867 |  | 
 | 1868 | 		pr_debug("%s: aborting association:%p\n", __func__, asoc); | 
 | 1869 | 		sctp_primitive_ABORT(net, asoc, chunk); | 
 | 1870 |  | 
 | 1871 | 		return 0; | 
 | 1872 | 	} | 
 | 1873 |  | 
 | 1874 | 	return 1; | 
 | 1875 | } | 
 | 1876 |  | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1877 | static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, | 
 | 1878 | 				struct msghdr *msg, size_t msg_len, | 
 | 1879 | 				struct sctp_transport *transport, | 
 | 1880 | 				struct sctp_sndrcvinfo *sinfo) | 
 | 1881 | { | 
 | 1882 | 	struct sock *sk = asoc->base.sk; | 
| Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1883 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1884 | 	struct net *net = sock_net(sk); | 
 | 1885 | 	struct sctp_datamsg *datamsg; | 
 | 1886 | 	bool wait_connect = false; | 
 | 1887 | 	struct sctp_chunk *chunk; | 
 | 1888 | 	long timeo; | 
 | 1889 | 	int err; | 
 | 1890 |  | 
 | 1891 | 	if (sinfo->sinfo_stream >= asoc->stream.outcnt) { | 
 | 1892 | 		err = -EINVAL; | 
 | 1893 | 		goto err; | 
 | 1894 | 	} | 
 | 1895 |  | 
| Konstantin Khorenko | 05364ca | 2018-08-10 20:11:42 +0300 | [diff] [blame] | 1896 | 	if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) { | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1897 | 		err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); | 
 | 1898 | 		if (err) | 
 | 1899 | 			goto err; | 
 | 1900 | 	} | 
 | 1901 |  | 
| Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1902 | 	if (sp->disable_fragments && msg_len > asoc->frag_point) { | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1903 | 		err = -EMSGSIZE; | 
 | 1904 | 		goto err; | 
 | 1905 | 	} | 
 | 1906 |  | 
| Marcelo Ricardo Leitner | 2521680 | 2018-04-26 16:58:56 -0300 | [diff] [blame] | 1907 | 	if (asoc->pmtu_pending) { | 
| Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1908 | 		if (sp->param_flags & SPP_PMTUD_ENABLE) | 
 | 1909 | 			sctp_assoc_sync_pmtu(asoc); | 
| Marcelo Ricardo Leitner | 2521680 | 2018-04-26 16:58:56 -0300 | [diff] [blame] | 1910 | 		asoc->pmtu_pending = 0; | 
 | 1911 | 	} | 
| Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1912 |  | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 1913 | 	if (sctp_wspace(asoc) < (int)msg_len) | 
| Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1914 | 		sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); | 
 | 1915 |  | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 1916 | 	if (sctp_wspace(asoc) <= 0) { | 
| Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1917 | 		timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | 
 | 1918 | 		err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); | 
 | 1919 | 		if (err) | 
 | 1920 | 			goto err; | 
 | 1921 | 	} | 
 | 1922 |  | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1923 | 	if (sctp_state(asoc, CLOSED)) { | 
 | 1924 | 		err = sctp_primitive_ASSOCIATE(net, asoc, NULL); | 
 | 1925 | 		if (err) | 
 | 1926 | 			goto err; | 
 | 1927 |  | 
| Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1928 | 		if (sp->strm_interleave) { | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1929 | 			timeo = sock_sndtimeo(sk, 0); | 
 | 1930 | 			err = sctp_wait_for_connect(asoc, &timeo); | 
 | 1931 | 			if (err) | 
 | 1932 | 				goto err; | 
 | 1933 | 		} else { | 
 | 1934 | 			wait_connect = true; | 
 | 1935 | 		} | 
 | 1936 |  | 
 | 1937 | 		pr_debug("%s: we associated primitively\n", __func__); | 
 | 1938 | 	} | 
 | 1939 |  | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1940 | 	datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); | 
 | 1941 | 	if (IS_ERR(datamsg)) { | 
 | 1942 | 		err = PTR_ERR(datamsg); | 
 | 1943 | 		goto err; | 
 | 1944 | 	} | 
 | 1945 |  | 
 | 1946 | 	asoc->force_delay = !!(msg->msg_flags & MSG_MORE); | 
 | 1947 |  | 
 | 1948 | 	list_for_each_entry(chunk, &datamsg->chunks, frag_list) { | 
 | 1949 | 		sctp_chunk_hold(chunk); | 
 | 1950 | 		sctp_set_owner_w(chunk); | 
 | 1951 | 		chunk->transport = transport; | 
 | 1952 | 	} | 
 | 1953 |  | 
 | 1954 | 	err = sctp_primitive_SEND(net, asoc, datamsg); | 
 | 1955 | 	if (err) { | 
 | 1956 | 		sctp_datamsg_free(datamsg); | 
 | 1957 | 		goto err; | 
 | 1958 | 	} | 
 | 1959 |  | 
 | 1960 | 	pr_debug("%s: we sent primitively\n", __func__); | 
 | 1961 |  | 
 | 1962 | 	sctp_datamsg_put(datamsg); | 
 | 1963 |  | 
 | 1964 | 	if (unlikely(wait_connect)) { | 
 | 1965 | 		timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | 
 | 1966 | 		sctp_wait_for_connect(asoc, &timeo); | 
 | 1967 | 	} | 
 | 1968 |  | 
 | 1969 | 	err = msg_len; | 
 | 1970 |  | 
 | 1971 | err: | 
 | 1972 | 	return err; | 
 | 1973 | } | 
 | 1974 |  | 
| Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1975 | static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk, | 
 | 1976 | 					       const struct msghdr *msg, | 
 | 1977 | 					       struct sctp_cmsgs *cmsgs) | 
 | 1978 | { | 
 | 1979 | 	union sctp_addr *daddr = NULL; | 
 | 1980 | 	int err; | 
 | 1981 |  | 
 | 1982 | 	if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { | 
 | 1983 | 		int len = msg->msg_namelen; | 
 | 1984 |  | 
 | 1985 | 		if (len > sizeof(*daddr)) | 
 | 1986 | 			len = sizeof(*daddr); | 
 | 1987 |  | 
 | 1988 | 		daddr = (union sctp_addr *)msg->msg_name; | 
 | 1989 |  | 
 | 1990 | 		err = sctp_verify_addr(sk, daddr, len); | 
 | 1991 | 		if (err) | 
 | 1992 | 			return ERR_PTR(err); | 
 | 1993 | 	} | 
 | 1994 |  | 
 | 1995 | 	return daddr; | 
 | 1996 | } | 
 | 1997 |  | 
| Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 1998 | static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc, | 
 | 1999 | 				      struct sctp_sndrcvinfo *sinfo, | 
 | 2000 | 				      struct sctp_cmsgs *cmsgs) | 
 | 2001 | { | 
 | 2002 | 	if (!cmsgs->srinfo && !cmsgs->sinfo) { | 
 | 2003 | 		sinfo->sinfo_stream = asoc->default_stream; | 
 | 2004 | 		sinfo->sinfo_ppid = asoc->default_ppid; | 
 | 2005 | 		sinfo->sinfo_context = asoc->default_context; | 
 | 2006 | 		sinfo->sinfo_assoc_id = sctp_assoc2id(asoc); | 
| Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 2007 |  | 
 | 2008 | 		if (!cmsgs->prinfo) | 
 | 2009 | 			sinfo->sinfo_flags = asoc->default_flags; | 
| Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 2010 | 	} | 
 | 2011 |  | 
| Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 2012 | 	if (!cmsgs->srinfo && !cmsgs->prinfo) | 
| Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 2013 | 		sinfo->sinfo_timetolive = asoc->default_timetolive; | 
| Xin Long | 3ff547c | 2018-03-14 19:05:31 +0800 | [diff] [blame] | 2014 |  | 
 | 2015 | 	if (cmsgs->authinfo) { | 
 | 2016 | 		/* Reuse sinfo_tsn to indicate that authinfo was set and | 
 | 2017 | 		 * sinfo_ssn to save the keyid on tx path. | 
 | 2018 | 		 */ | 
 | 2019 | 		sinfo->sinfo_tsn = 1; | 
 | 2020 | 		sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber; | 
 | 2021 | 	} | 
| Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 2022 | } | 
 | 2023 |  | 
| Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2024 | 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] | 2025 | { | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 2026 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 2027 | 	struct sctp_transport *transport = NULL; | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 2028 | 	struct sctp_sndrcvinfo _sinfo, *sinfo; | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2029 | 	struct sctp_association *asoc; | 
 | 2030 | 	struct sctp_cmsgs cmsgs; | 
| Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 2031 | 	union sctp_addr *daddr; | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2032 | 	bool new = false; | 
 | 2033 | 	__u16 sflags; | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 2034 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 |  | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 2036 | 	/* Parse and get snd_info */ | 
 | 2037 | 	err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len); | 
 | 2038 | 	if (err) | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2039 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 |  | 
| Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 2041 | 	sinfo  = &_sinfo; | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2042 | 	sflags = sinfo->sinfo_flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 |  | 
| Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 2044 | 	/* Get daddr from msg */ | 
 | 2045 | 	daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs); | 
 | 2046 | 	if (IS_ERR(daddr)) { | 
 | 2047 | 		err = PTR_ERR(daddr); | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2048 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | 	} | 
 | 2050 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2051 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 |  | 
| Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 2053 | 	/* SCTP_SENDALL process */ | 
 | 2054 | 	if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) { | 
 | 2055 | 		list_for_each_entry(asoc, &ep->asocs, asocs) { | 
 | 2056 | 			err = sctp_sendmsg_check_sflags(asoc, sflags, msg, | 
 | 2057 | 							msg_len); | 
 | 2058 | 			if (err == 0) | 
 | 2059 | 				continue; | 
 | 2060 | 			if (err < 0) | 
 | 2061 | 				goto out_unlock; | 
 | 2062 |  | 
 | 2063 | 			sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); | 
 | 2064 |  | 
 | 2065 | 			err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, | 
 | 2066 | 						   NULL, sinfo); | 
 | 2067 | 			if (err < 0) | 
 | 2068 | 				goto out_unlock; | 
 | 2069 |  | 
 | 2070 | 			iov_iter_revert(&msg->msg_iter, err); | 
 | 2071 | 		} | 
 | 2072 |  | 
 | 2073 | 		goto out_unlock; | 
 | 2074 | 	} | 
 | 2075 |  | 
| Xin Long | 0a3920d | 2018-03-01 23:05:18 +0800 | [diff] [blame] | 2076 | 	/* Get and check or create asoc */ | 
| Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 2077 | 	if (daddr) { | 
| Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 2078 | 		asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); | 
| Xin Long | 0a3920d | 2018-03-01 23:05:18 +0800 | [diff] [blame] | 2079 | 		if (asoc) { | 
 | 2080 | 			err = sctp_sendmsg_check_sflags(asoc, sflags, msg, | 
 | 2081 | 							msg_len); | 
 | 2082 | 			if (err <= 0) | 
 | 2083 | 				goto out_unlock; | 
 | 2084 | 		} else { | 
 | 2085 | 			err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr, | 
 | 2086 | 						    &transport); | 
 | 2087 | 			if (err) | 
 | 2088 | 				goto out_unlock; | 
 | 2089 |  | 
 | 2090 | 			asoc = transport->asoc; | 
 | 2091 | 			new = true; | 
 | 2092 | 		} | 
 | 2093 |  | 
 | 2094 | 		if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER)) | 
 | 2095 | 			transport = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | 	} else { | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2097 | 		asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | 		if (!asoc) { | 
 | 2099 | 			err = -EPIPE; | 
 | 2100 | 			goto out_unlock; | 
 | 2101 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 |  | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2103 | 		err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len); | 
| Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 2104 | 		if (err <= 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | 			goto out_unlock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | 	} | 
 | 2107 |  | 
| Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 2108 | 	/* Update snd_info with the asoc */ | 
 | 2109 | 	sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 |  | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 2111 | 	/* Send msg to the asoc */ | 
| Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 2112 | 	err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo); | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2113 | 	if (err < 0 && err != -ESRCH && new) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | 		sctp_association_free(asoc); | 
| Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 2115 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | out_unlock: | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2117 | 	release_sock(sk); | 
| Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2118 | out: | 
| Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 2119 | 	return sctp_error(sk, msg->msg_flags, err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } | 
 | 2121 |  | 
 | 2122 | /* This is an extended version of skb_pull() that removes the data from the | 
 | 2123 |  * start of a skb even when data is spread across the list of skb's in the | 
 | 2124 |  * frag_list. len specifies the total amount of data that needs to be removed. | 
 | 2125 |  * when 'len' bytes could be removed from the skb, it returns 0. | 
 | 2126 |  * If 'len' exceeds the total skb length,  it returns the no. of bytes that | 
 | 2127 |  * could not be removed. | 
 | 2128 |  */ | 
 | 2129 | static int sctp_skb_pull(struct sk_buff *skb, int len) | 
 | 2130 | { | 
 | 2131 | 	struct sk_buff *list; | 
 | 2132 | 	int skb_len = skb_headlen(skb); | 
 | 2133 | 	int rlen; | 
 | 2134 |  | 
 | 2135 | 	if (len <= skb_len) { | 
 | 2136 | 		__skb_pull(skb, len); | 
 | 2137 | 		return 0; | 
 | 2138 | 	} | 
 | 2139 | 	len -= skb_len; | 
 | 2140 | 	__skb_pull(skb, skb_len); | 
 | 2141 |  | 
| David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 2142 | 	skb_walk_frags(skb, list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | 		rlen = sctp_skb_pull(list, len); | 
 | 2144 | 		skb->len -= (len-rlen); | 
 | 2145 | 		skb->data_len -= (len-rlen); | 
 | 2146 |  | 
 | 2147 | 		if (!rlen) | 
 | 2148 | 			return 0; | 
 | 2149 |  | 
 | 2150 | 		len = rlen; | 
 | 2151 | 	} | 
 | 2152 |  | 
 | 2153 | 	return len; | 
 | 2154 | } | 
 | 2155 |  | 
 | 2156 | /* API 3.1.3  recvmsg() - UDP Style Syntax | 
 | 2157 |  * | 
 | 2158 |  *  ssize_t recvmsg(int socket, struct msghdr *message, | 
 | 2159 |  *                    int flags); | 
 | 2160 |  * | 
 | 2161 |  *  socket  - the socket descriptor of the endpoint. | 
 | 2162 |  *  message - pointer to the msghdr structure which contains a single | 
 | 2163 |  *            user message and possibly some ancillary data. | 
 | 2164 |  * | 
 | 2165 |  *            See Section 5 for complete description of the data | 
 | 2166 |  *            structures. | 
 | 2167 |  * | 
 | 2168 |  *  flags   - flags sent or received with the user message, see Section | 
 | 2169 |  *            5 for complete description of the flags. | 
 | 2170 |  */ | 
| Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2171 | static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, | 
 | 2172 | 			int noblock, int flags, int *addr_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { | 
 | 2174 | 	struct sctp_ulpevent *event = NULL; | 
 | 2175 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2176 | 	struct sk_buff *skb, *head_skb; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | 	int copied; | 
 | 2178 | 	int err = 0; | 
 | 2179 | 	int skb_len; | 
 | 2180 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2181 | 	pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, " | 
 | 2182 | 		 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags, | 
 | 2183 | 		 addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2185 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 |  | 
| Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 2187 | 	if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) && | 
| Xin Long | e087869 | 2016-07-30 14:14:41 +0800 | [diff] [blame] | 2188 | 	    !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | 		err = -ENOTCONN; | 
 | 2190 | 		goto out; | 
 | 2191 | 	} | 
 | 2192 |  | 
 | 2193 | 	skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); | 
 | 2194 | 	if (!skb) | 
 | 2195 | 		goto out; | 
 | 2196 |  | 
 | 2197 | 	/* Get the total length of the skb including any skb's in the | 
 | 2198 | 	 * frag_list. | 
 | 2199 | 	 */ | 
 | 2200 | 	skb_len = skb->len; | 
 | 2201 |  | 
 | 2202 | 	copied = skb_len; | 
 | 2203 | 	if (copied > len) | 
 | 2204 | 		copied = len; | 
 | 2205 |  | 
| David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 2206 | 	err = skb_copy_datagram_msg(skb, 0, msg, copied); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 |  | 
 | 2208 | 	event = sctp_skb2event(skb); | 
 | 2209 |  | 
 | 2210 | 	if (err) | 
 | 2211 | 		goto out_free; | 
 | 2212 |  | 
| Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2213 | 	if (event->chunk && event->chunk->head_skb) | 
 | 2214 | 		head_skb = event->chunk->head_skb; | 
 | 2215 | 	else | 
 | 2216 | 		head_skb = skb; | 
 | 2217 | 	sock_recv_ts_and_drops(msg, sk, head_skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | 	if (sctp_ulpevent_is_notification(event)) { | 
 | 2219 | 		msg->msg_flags |= MSG_NOTIFICATION; | 
 | 2220 | 		sp->pf->event_msgname(event, msg->msg_name, addr_len); | 
 | 2221 | 	} else { | 
| Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2222 | 		sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | 	} | 
 | 2224 |  | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 2225 | 	/* Check if we allow SCTP_NXTINFO. */ | 
 | 2226 | 	if (sp->recvnxtinfo) | 
 | 2227 | 		sctp_ulpevent_read_nxtinfo(event, msg, sk); | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2228 | 	/* Check if we allow SCTP_RCVINFO. */ | 
 | 2229 | 	if (sp->recvrcvinfo) | 
 | 2230 | 		sctp_ulpevent_read_rcvinfo(event, msg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | 	/* Check if we allow SCTP_SNDRCVINFO. */ | 
 | 2232 | 	if (sp->subscribe.sctp_data_io_event) | 
 | 2233 | 		sctp_ulpevent_read_sndrcvinfo(event, msg); | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2234 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | 	err = copied; | 
 | 2236 |  | 
 | 2237 | 	/* If skb's length exceeds the user's buffer, update the skb and | 
 | 2238 | 	 * push it back to the receive_queue so that the next call to | 
 | 2239 | 	 * recvmsg() will return the remaining data. Don't set MSG_EOR. | 
 | 2240 | 	 */ | 
 | 2241 | 	if (skb_len > copied) { | 
 | 2242 | 		msg->msg_flags &= ~MSG_EOR; | 
 | 2243 | 		if (flags & MSG_PEEK) | 
 | 2244 | 			goto out_free; | 
 | 2245 | 		sctp_skb_pull(skb, copied); | 
 | 2246 | 		skb_queue_head(&sk->sk_receive_queue, skb); | 
 | 2247 |  | 
| Daniel Borkmann | 362d520 | 2014-04-14 21:45:17 +0200 | [diff] [blame] | 2248 | 		/* When only partial message is copied to the user, increase | 
 | 2249 | 		 * rwnd by that amount. If all the data in the skb is read, | 
 | 2250 | 		 * rwnd is updated when the event is freed. | 
 | 2251 | 		 */ | 
 | 2252 | 		if (!sctp_ulpevent_is_notification(event)) | 
 | 2253 | 			sctp_assoc_rwnd_increase(event->asoc, copied); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | 		goto out; | 
 | 2255 | 	} else if ((event->msg_flags & MSG_NOTIFICATION) || | 
 | 2256 | 		   (event->msg_flags & MSG_EOR)) | 
 | 2257 | 		msg->msg_flags |= MSG_EOR; | 
 | 2258 | 	else | 
 | 2259 | 		msg->msg_flags &= ~MSG_EOR; | 
 | 2260 |  | 
 | 2261 | out_free: | 
 | 2262 | 	if (flags & MSG_PEEK) { | 
 | 2263 | 		/* Release the skb reference acquired after peeking the skb in | 
 | 2264 | 		 * sctp_skb_recv_datagram(). | 
 | 2265 | 		 */ | 
 | 2266 | 		kfree_skb(skb); | 
 | 2267 | 	} else { | 
 | 2268 | 		/* Free the event which includes releasing the reference to | 
 | 2269 | 		 * the owner of the skb, freeing the skb and updating the | 
 | 2270 | 		 * rwnd. | 
 | 2271 | 		 */ | 
 | 2272 | 		sctp_ulpevent_free(event); | 
 | 2273 | 	} | 
 | 2274 | out: | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2275 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | 	return err; | 
 | 2277 | } | 
 | 2278 |  | 
 | 2279 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 
 | 2280 |  * | 
 | 2281 |  * This option is a on/off flag.  If enabled no SCTP message | 
 | 2282 |  * fragmentation will be performed.  Instead if a message being sent | 
 | 2283 |  * exceeds the current PMTU size, the message will NOT be sent and | 
 | 2284 |  * instead a error will be indicated to the user. | 
 | 2285 |  */ | 
 | 2286 | static int sctp_setsockopt_disable_fragments(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2287 | 					     char __user *optval, | 
 | 2288 | 					     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { | 
 | 2290 | 	int val; | 
 | 2291 |  | 
 | 2292 | 	if (optlen < sizeof(int)) | 
 | 2293 | 		return -EINVAL; | 
 | 2294 |  | 
 | 2295 | 	if (get_user(val, (int __user *)optval)) | 
 | 2296 | 		return -EFAULT; | 
 | 2297 |  | 
 | 2298 | 	sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; | 
 | 2299 |  | 
 | 2300 | 	return 0; | 
 | 2301 | } | 
 | 2302 |  | 
 | 2303 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2304 | 				  unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | { | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2306 | 	struct sctp_association *asoc; | 
 | 2307 | 	struct sctp_ulpevent *event; | 
 | 2308 |  | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 2309 | 	if (optlen > sizeof(struct sctp_event_subscribe)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | 		return -EINVAL; | 
 | 2311 | 	if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) | 
 | 2312 | 		return -EFAULT; | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2313 |  | 
| Daniel Borkmann | bbbea41 | 2014-07-12 20:30:40 +0200 | [diff] [blame] | 2314 | 	/* 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] | 2315 | 	 * if there is no data to be sent or retransmit, the stack will | 
 | 2316 | 	 * immediately send up this notification. | 
 | 2317 | 	 */ | 
 | 2318 | 	if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT, | 
 | 2319 | 				       &sctp_sk(sk)->subscribe)) { | 
 | 2320 | 		asoc = sctp_id2assoc(sk, 0); | 
 | 2321 |  | 
 | 2322 | 		if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { | 
 | 2323 | 			event = sctp_ulpevent_make_sender_dry_event(asoc, | 
| Marcelo Ricardo Leitner | 2e83acb | 2018-01-08 19:02:27 -0200 | [diff] [blame] | 2324 | 					GFP_USER | __GFP_NOWARN); | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2325 | 			if (!event) | 
 | 2326 | 				return -ENOMEM; | 
 | 2327 |  | 
| Xin Long | 9162e0e | 2017-12-08 21:04:05 +0800 | [diff] [blame] | 2328 | 			asoc->stream.si->enqueue_event(&asoc->ulpq, event); | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2329 | 		} | 
 | 2330 | 	} | 
 | 2331 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | 	return 0; | 
 | 2333 | } | 
 | 2334 |  | 
 | 2335 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 
 | 2336 |  * | 
 | 2337 |  * This socket option is applicable to the UDP-style socket only.  When | 
 | 2338 |  * set it will cause associations that are idle for more than the | 
 | 2339 |  * specified number of seconds to automatically close.  An association | 
 | 2340 |  * being idle is defined an association that has NOT sent or received | 
 | 2341 |  * user data.  The special value of '0' indicates that no automatic | 
 | 2342 |  * close of any associations should be performed.  The option expects an | 
 | 2343 |  * integer defining the number of seconds of idle time before an | 
 | 2344 |  * association is closed. | 
 | 2345 |  */ | 
 | 2346 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2347 | 				     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | { | 
 | 2349 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2350 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 |  | 
 | 2352 | 	/* Applicable to UDP-style socket only */ | 
 | 2353 | 	if (sctp_style(sk, TCP)) | 
 | 2354 | 		return -EOPNOTSUPP; | 
 | 2355 | 	if (optlen != sizeof(int)) | 
 | 2356 | 		return -EINVAL; | 
 | 2357 | 	if (copy_from_user(&sp->autoclose, optval, optlen)) | 
 | 2358 | 		return -EFAULT; | 
 | 2359 |  | 
| Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2360 | 	if (sp->autoclose > net->sctp.max_autoclose) | 
 | 2361 | 		sp->autoclose = net->sctp.max_autoclose; | 
 | 2362 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | 	return 0; | 
 | 2364 | } | 
 | 2365 |  | 
 | 2366 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 
 | 2367 |  * | 
 | 2368 |  * Applications can enable or disable heartbeats for any peer address of | 
 | 2369 |  * an association, modify an address's heartbeat interval, force a | 
 | 2370 |  * heartbeat to be sent immediately, and adjust the address's maximum | 
 | 2371 |  * number of retransmissions sent before an address is considered | 
 | 2372 |  * unreachable.  The following structure is used to access and modify an | 
 | 2373 |  * address's parameters: | 
 | 2374 |  * | 
 | 2375 |  *  struct sctp_paddrparams { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2376 |  *     sctp_assoc_t            spp_assoc_id; | 
 | 2377 |  *     struct sockaddr_storage spp_address; | 
 | 2378 |  *     uint32_t                spp_hbinterval; | 
 | 2379 |  *     uint16_t                spp_pathmaxrxt; | 
 | 2380 |  *     uint32_t                spp_pathmtu; | 
 | 2381 |  *     uint32_t                spp_sackdelay; | 
 | 2382 |  *     uint32_t                spp_flags; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2383 |  *     uint32_t                spp_ipv6_flowlabel; | 
 | 2384 |  *     uint8_t                 spp_dscp; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2385 |  * }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2387 |  *   spp_assoc_id    - (one-to-many style socket) This is filled in the | 
 | 2388 |  *                     application, and identifies the association for | 
 | 2389 |  *                     this query. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 |  *   spp_address     - This specifies which address is of interest. | 
 | 2391 |  *   spp_hbinterval  - This contains the value of the heartbeat interval, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2392 |  *                     in milliseconds.  If a  value of zero | 
 | 2393 |  *                     is present in this field then no changes are to | 
 | 2394 |  *                     be made to this parameter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 |  *   spp_pathmaxrxt  - This contains the maximum number of | 
 | 2396 |  *                     retransmissions before this address shall be | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2397 |  *                     considered unreachable. If a  value of zero | 
 | 2398 |  *                     is present in this field then no changes are to | 
 | 2399 |  *                     be made to this parameter. | 
 | 2400 |  *   spp_pathmtu     - When Path MTU discovery is disabled the value | 
 | 2401 |  *                     specified here will be the "fixed" path mtu. | 
 | 2402 |  *                     Note that if the spp_address field is empty | 
 | 2403 |  *                     then all associations on this address will | 
 | 2404 |  *                     have this fixed path mtu set upon them. | 
 | 2405 |  * | 
 | 2406 |  *   spp_sackdelay   - When delayed sack is enabled, this value specifies | 
 | 2407 |  *                     the number of milliseconds that sacks will be delayed | 
 | 2408 |  *                     for. This value will apply to all addresses of an | 
 | 2409 |  *                     association if the spp_address field is empty. Note | 
 | 2410 |  *                     also, that if delayed sack is enabled and this | 
 | 2411 |  *                     value is set to 0, no change is made to the last | 
 | 2412 |  *                     recorded delayed sack timer value. | 
 | 2413 |  * | 
 | 2414 |  *   spp_flags       - These flags are used to control various features | 
 | 2415 |  *                     on an association. The flag field may contain | 
 | 2416 |  *                     zero or more of the following options. | 
 | 2417 |  * | 
 | 2418 |  *                     SPP_HB_ENABLE  - Enable heartbeats on the | 
 | 2419 |  *                     specified address. Note that if the address | 
 | 2420 |  *                     field is empty all addresses for the association | 
 | 2421 |  *                     have heartbeats enabled upon them. | 
 | 2422 |  * | 
 | 2423 |  *                     SPP_HB_DISABLE - Disable heartbeats on the | 
 | 2424 |  *                     speicifed address. Note that if the address | 
 | 2425 |  *                     field is empty all addresses for the association | 
 | 2426 |  *                     will have their heartbeats disabled. Note also | 
 | 2427 |  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are | 
 | 2428 |  *                     mutually exclusive, only one of these two should | 
 | 2429 |  *                     be specified. Enabling both fields will have | 
 | 2430 |  *                     undetermined results. | 
 | 2431 |  * | 
 | 2432 |  *                     SPP_HB_DEMAND - Request a user initiated heartbeat | 
 | 2433 |  *                     to be made immediately. | 
 | 2434 |  * | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2435 |  *                     SPP_HB_TIME_IS_ZERO - Specify's that the time for | 
 | 2436 |  *                     heartbeat delayis to be set to the value of 0 | 
 | 2437 |  *                     milliseconds. | 
 | 2438 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2439 |  *                     SPP_PMTUD_ENABLE - This field will enable PMTU | 
 | 2440 |  *                     discovery upon the specified address. Note that | 
 | 2441 |  *                     if the address feild is empty then all addresses | 
 | 2442 |  *                     on the association are effected. | 
 | 2443 |  * | 
 | 2444 |  *                     SPP_PMTUD_DISABLE - This field will disable PMTU | 
 | 2445 |  *                     discovery upon the specified address. Note that | 
 | 2446 |  *                     if the address feild is empty then all addresses | 
 | 2447 |  *                     on the association are effected. Not also that | 
 | 2448 |  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 
 | 2449 |  *                     exclusive. Enabling both will have undetermined | 
 | 2450 |  *                     results. | 
 | 2451 |  * | 
 | 2452 |  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns | 
 | 2453 |  *                     on delayed sack. The time specified in spp_sackdelay | 
 | 2454 |  *                     is used to specify the sack delay for this address. Note | 
 | 2455 |  *                     that if spp_address is empty then all addresses will | 
 | 2456 |  *                     enable delayed sack and take on the sack delay | 
 | 2457 |  *                     value specified in spp_sackdelay. | 
 | 2458 |  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns | 
 | 2459 |  *                     off delayed sack. If the spp_address field is blank then | 
 | 2460 |  *                     delayed sack is disabled for the entire association. Note | 
 | 2461 |  *                     also that this field is mutually exclusive to | 
 | 2462 |  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined | 
 | 2463 |  *                     results. | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2464 |  * | 
 | 2465 |  *                     SPP_IPV6_FLOWLABEL:  Setting this flag enables the | 
 | 2466 |  *                     setting of the IPV6 flow label value.  The value is | 
 | 2467 |  *                     contained in the spp_ipv6_flowlabel field. | 
 | 2468 |  *                     Upon retrieval, this flag will be set to indicate that | 
 | 2469 |  *                     the spp_ipv6_flowlabel field has a valid value returned. | 
 | 2470 |  *                     If a specific destination address is set (in the | 
 | 2471 |  *                     spp_address field), then the value returned is that of | 
 | 2472 |  *                     the address.  If just an association is specified (and | 
 | 2473 |  *                     no address), then the association's default flow label | 
 | 2474 |  *                     is returned.  If neither an association nor a destination | 
 | 2475 |  *                     is specified, then the socket's default flow label is | 
 | 2476 |  *                     returned.  For non-IPv6 sockets, this flag will be left | 
 | 2477 |  *                     cleared. | 
 | 2478 |  * | 
 | 2479 |  *                     SPP_DSCP:  Setting this flag enables the setting of the | 
 | 2480 |  *                     Differentiated Services Code Point (DSCP) value | 
 | 2481 |  *                     associated with either the association or a specific | 
 | 2482 |  *                     address.  The value is obtained in the spp_dscp field. | 
 | 2483 |  *                     Upon retrieval, this flag will be set to indicate that | 
 | 2484 |  *                     the spp_dscp field has a valid value returned.  If a | 
 | 2485 |  *                     specific destination address is set when called (in the | 
 | 2486 |  *                     spp_address field), then that specific destination | 
 | 2487 |  *                     address's DSCP value is returned.  If just an association | 
 | 2488 |  *                     is specified, then the association's default DSCP is | 
 | 2489 |  *                     returned.  If neither an association nor a destination is | 
 | 2490 |  *                     specified, then the socket's default DSCP is returned. | 
 | 2491 |  * | 
 | 2492 |  *   spp_ipv6_flowlabel | 
 | 2493 |  *                   - This field is used in conjunction with the | 
 | 2494 |  *                     SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. | 
 | 2495 |  *                     The 20 least significant bits are used for the flow | 
 | 2496 |  *                     label.  This setting has precedence over any IPv6-layer | 
 | 2497 |  *                     setting. | 
 | 2498 |  * | 
 | 2499 |  *   spp_dscp        - This field is used in conjunction with the SPP_DSCP flag | 
 | 2500 |  *                     and contains the DSCP.  The 6 most significant bits are | 
 | 2501 |  *                     used for the DSCP.  This setting has precedence over any | 
 | 2502 |  *                     IPv4- or IPv6- layer setting. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 |  */ | 
| Adrian Bunk | 1616436 | 2006-09-18 00:40:38 -0700 | [diff] [blame] | 2504 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, | 
 | 2505 | 				       struct sctp_transport   *trans, | 
 | 2506 | 				       struct sctp_association *asoc, | 
 | 2507 | 				       struct sctp_sock        *sp, | 
 | 2508 | 				       int                      hb_change, | 
 | 2509 | 				       int                      pmtud_change, | 
 | 2510 | 				       int                      sackdelay_change) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | 	int error; | 
 | 2513 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2514 | 	if (params->spp_flags & SPP_HB_DEMAND && trans) { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 2515 | 		struct net *net = sock_net(trans->asoc->base.sk); | 
 | 2516 |  | 
 | 2517 | 		error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | 		if (error) | 
 | 2519 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | 	} | 
 | 2521 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2522 | 	/* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of | 
 | 2523 | 	 * this field is ignored.  Note also that a value of zero indicates | 
 | 2524 | 	 * the current setting should be left unchanged. | 
 | 2525 | 	 */ | 
 | 2526 | 	if (params->spp_flags & SPP_HB_ENABLE) { | 
 | 2527 |  | 
 | 2528 | 		/* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is | 
 | 2529 | 		 * set.  This lets us use 0 value when this flag | 
 | 2530 | 		 * is set. | 
 | 2531 | 		 */ | 
 | 2532 | 		if (params->spp_flags & SPP_HB_TIME_IS_ZERO) | 
 | 2533 | 			params->spp_hbinterval = 0; | 
 | 2534 |  | 
 | 2535 | 		if (params->spp_hbinterval || | 
 | 2536 | 		    (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { | 
 | 2537 | 			if (trans) { | 
 | 2538 | 				trans->hbinterval = | 
 | 2539 | 				    msecs_to_jiffies(params->spp_hbinterval); | 
 | 2540 | 			} else if (asoc) { | 
 | 2541 | 				asoc->hbinterval = | 
 | 2542 | 				    msecs_to_jiffies(params->spp_hbinterval); | 
 | 2543 | 			} else { | 
 | 2544 | 				sp->hbinterval = params->spp_hbinterval; | 
 | 2545 | 			} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2546 | 		} | 
 | 2547 | 	} | 
 | 2548 |  | 
 | 2549 | 	if (hb_change) { | 
 | 2550 | 		if (trans) { | 
 | 2551 | 			trans->param_flags = | 
 | 2552 | 				(trans->param_flags & ~SPP_HB) | hb_change; | 
 | 2553 | 		} else if (asoc) { | 
 | 2554 | 			asoc->param_flags = | 
 | 2555 | 				(asoc->param_flags & ~SPP_HB) | hb_change; | 
 | 2556 | 		} else { | 
 | 2557 | 			sp->param_flags = | 
 | 2558 | 				(sp->param_flags & ~SPP_HB) | hb_change; | 
 | 2559 | 		} | 
 | 2560 | 	} | 
 | 2561 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2562 | 	/* When Path MTU discovery is disabled the value specified here will | 
 | 2563 | 	 * be the "fixed" path mtu (i.e. the value of the spp_flags field must | 
 | 2564 | 	 * include the flag SPP_PMTUD_DISABLE for this field to have any | 
 | 2565 | 	 * effect). | 
 | 2566 | 	 */ | 
 | 2567 | 	if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2568 | 		if (trans) { | 
 | 2569 | 			trans->pathmtu = params->spp_pathmtu; | 
| Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2570 | 			sctp_assoc_sync_pmtu(asoc); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2571 | 		} else if (asoc) { | 
| Marcelo Ricardo Leitner | c4b2893 | 2018-04-26 16:58:53 -0300 | [diff] [blame] | 2572 | 			sctp_assoc_set_pmtu(asoc, params->spp_pathmtu); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2573 | 		} else { | 
 | 2574 | 			sp->pathmtu = params->spp_pathmtu; | 
 | 2575 | 		} | 
 | 2576 | 	} | 
 | 2577 |  | 
 | 2578 | 	if (pmtud_change) { | 
 | 2579 | 		if (trans) { | 
 | 2580 | 			int update = (trans->param_flags & SPP_PMTUD_DISABLE) && | 
 | 2581 | 				(params->spp_flags & SPP_PMTUD_ENABLE); | 
 | 2582 | 			trans->param_flags = | 
 | 2583 | 				(trans->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2584 | 			if (update) { | 
| Vlad Yasevich | 9914ae3 | 2011-04-26 21:51:31 +0000 | [diff] [blame] | 2585 | 				sctp_transport_pmtu(trans, sctp_opt2sk(sp)); | 
| Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2586 | 				sctp_assoc_sync_pmtu(asoc); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2587 | 			} | 
 | 2588 | 		} else if (asoc) { | 
 | 2589 | 			asoc->param_flags = | 
 | 2590 | 				(asoc->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2591 | 		} else { | 
 | 2592 | 			sp->param_flags = | 
 | 2593 | 				(sp->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2594 | 		} | 
 | 2595 | 	} | 
 | 2596 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2597 | 	/* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the | 
 | 2598 | 	 * value of this field is ignored.  Note also that a value of zero | 
 | 2599 | 	 * indicates the current setting should be left unchanged. | 
 | 2600 | 	 */ | 
 | 2601 | 	if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2602 | 		if (trans) { | 
 | 2603 | 			trans->sackdelay = | 
 | 2604 | 				msecs_to_jiffies(params->spp_sackdelay); | 
 | 2605 | 		} else if (asoc) { | 
 | 2606 | 			asoc->sackdelay = | 
 | 2607 | 				msecs_to_jiffies(params->spp_sackdelay); | 
 | 2608 | 		} else { | 
 | 2609 | 			sp->sackdelay = params->spp_sackdelay; | 
 | 2610 | 		} | 
 | 2611 | 	} | 
 | 2612 |  | 
 | 2613 | 	if (sackdelay_change) { | 
 | 2614 | 		if (trans) { | 
 | 2615 | 			trans->param_flags = | 
 | 2616 | 				(trans->param_flags & ~SPP_SACKDELAY) | | 
 | 2617 | 				sackdelay_change; | 
 | 2618 | 		} else if (asoc) { | 
 | 2619 | 			asoc->param_flags = | 
 | 2620 | 				(asoc->param_flags & ~SPP_SACKDELAY) | | 
 | 2621 | 				sackdelay_change; | 
 | 2622 | 		} else { | 
 | 2623 | 			sp->param_flags = | 
 | 2624 | 				(sp->param_flags & ~SPP_SACKDELAY) | | 
 | 2625 | 				sackdelay_change; | 
 | 2626 | 		} | 
 | 2627 | 	} | 
 | 2628 |  | 
| Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2629 | 	/* Note that a value of zero indicates the current setting should be | 
 | 2630 | 	   left unchanged. | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2631 | 	 */ | 
| Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2632 | 	if (params->spp_pathmaxrxt) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2633 | 		if (trans) { | 
 | 2634 | 			trans->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2635 | 		} else if (asoc) { | 
 | 2636 | 			asoc->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2637 | 		} else { | 
 | 2638 | 			sp->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2639 | 		} | 
 | 2640 | 	} | 
 | 2641 |  | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2642 | 	if (params->spp_flags & SPP_IPV6_FLOWLABEL) { | 
| Xin Long | 741880e | 2018-09-03 15:47:11 +0800 | [diff] [blame] | 2643 | 		if (trans) { | 
 | 2644 | 			if (trans->ipaddr.sa.sa_family == AF_INET6) { | 
 | 2645 | 				trans->flowlabel = params->spp_ipv6_flowlabel & | 
 | 2646 | 						   SCTP_FLOWLABEL_VAL_MASK; | 
 | 2647 | 				trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK; | 
 | 2648 | 			} | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2649 | 		} else if (asoc) { | 
| Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2650 | 			struct sctp_transport *t; | 
 | 2651 |  | 
 | 2652 | 			list_for_each_entry(t, &asoc->peer.transport_addr_list, | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2653 | 					    transports) { | 
| Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2654 | 				if (t->ipaddr.sa.sa_family != AF_INET6) | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2655 | 					continue; | 
| Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2656 | 				t->flowlabel = params->spp_ipv6_flowlabel & | 
 | 2657 | 					       SCTP_FLOWLABEL_VAL_MASK; | 
 | 2658 | 				t->flowlabel |= SCTP_FLOWLABEL_SET_MASK; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2659 | 			} | 
 | 2660 | 			asoc->flowlabel = params->spp_ipv6_flowlabel & | 
 | 2661 | 					  SCTP_FLOWLABEL_VAL_MASK; | 
 | 2662 | 			asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK; | 
 | 2663 | 		} else if (sctp_opt2sk(sp)->sk_family == AF_INET6) { | 
 | 2664 | 			sp->flowlabel = params->spp_ipv6_flowlabel & | 
 | 2665 | 					SCTP_FLOWLABEL_VAL_MASK; | 
 | 2666 | 			sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK; | 
 | 2667 | 		} | 
 | 2668 | 	} | 
 | 2669 |  | 
 | 2670 | 	if (params->spp_flags & SPP_DSCP) { | 
 | 2671 | 		if (trans) { | 
 | 2672 | 			trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; | 
 | 2673 | 			trans->dscp |= SCTP_DSCP_SET_MASK; | 
 | 2674 | 		} else if (asoc) { | 
| Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2675 | 			struct sctp_transport *t; | 
 | 2676 |  | 
 | 2677 | 			list_for_each_entry(t, &asoc->peer.transport_addr_list, | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2678 | 					    transports) { | 
| Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2679 | 				t->dscp = params->spp_dscp & | 
 | 2680 | 					  SCTP_DSCP_VAL_MASK; | 
 | 2681 | 				t->dscp |= SCTP_DSCP_SET_MASK; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2682 | 			} | 
 | 2683 | 			asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; | 
 | 2684 | 			asoc->dscp |= SCTP_DSCP_SET_MASK; | 
 | 2685 | 		} else { | 
 | 2686 | 			sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; | 
 | 2687 | 			sp->dscp |= SCTP_DSCP_SET_MASK; | 
 | 2688 | 		} | 
 | 2689 | 	} | 
 | 2690 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2691 | 	return 0; | 
 | 2692 | } | 
 | 2693 |  | 
 | 2694 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2695 | 					    char __user *optval, | 
 | 2696 | 					    unsigned int optlen) | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2697 | { | 
 | 2698 | 	struct sctp_paddrparams  params; | 
 | 2699 | 	struct sctp_transport   *trans = NULL; | 
 | 2700 | 	struct sctp_association *asoc = NULL; | 
 | 2701 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 2702 | 	int error; | 
 | 2703 | 	int hb_change, pmtud_change, sackdelay_change; | 
 | 2704 |  | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2705 | 	if (optlen == sizeof(params)) { | 
 | 2706 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2707 | 			return -EFAULT; | 
 | 2708 | 	} else if (optlen == ALIGN(offsetof(struct sctp_paddrparams, | 
 | 2709 | 					    spp_ipv6_flowlabel), 4)) { | 
 | 2710 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2711 | 			return -EFAULT; | 
 | 2712 | 		if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL)) | 
 | 2713 | 			return -EINVAL; | 
 | 2714 | 	} else { | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2715 | 		return -EINVAL; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2716 | 	} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2717 |  | 
 | 2718 | 	/* Validate flags and value parameters. */ | 
 | 2719 | 	hb_change        = params.spp_flags & SPP_HB; | 
 | 2720 | 	pmtud_change     = params.spp_flags & SPP_PMTUD; | 
 | 2721 | 	sackdelay_change = params.spp_flags & SPP_SACKDELAY; | 
 | 2722 |  | 
 | 2723 | 	if (hb_change        == SPP_HB || | 
 | 2724 | 	    pmtud_change     == SPP_PMTUD || | 
 | 2725 | 	    sackdelay_change == SPP_SACKDELAY || | 
 | 2726 | 	    params.spp_sackdelay > 500 || | 
| Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 2727 | 	    (params.spp_pathmtu && | 
 | 2728 | 	     params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2729 | 		return -EINVAL; | 
 | 2730 |  | 
 | 2731 | 	/* If an address other than INADDR_ANY is specified, and | 
 | 2732 | 	 * no transport is found, then the request is invalid. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | 	 */ | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2734 | 	if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2735 | 		trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 
 | 2736 | 					       params.spp_assoc_id); | 
 | 2737 | 		if (!trans) | 
 | 2738 | 			return -EINVAL; | 
 | 2739 | 	} | 
 | 2740 |  | 
 | 2741 | 	/* Get association, if assoc_id != 0 and the socket is a one | 
 | 2742 | 	 * to many style socket, and an association was not found, then | 
 | 2743 | 	 * the id was invalid. | 
 | 2744 | 	 */ | 
 | 2745 | 	asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 
 | 2746 | 	if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) | 
 | 2747 | 		return -EINVAL; | 
 | 2748 |  | 
 | 2749 | 	/* Heartbeat demand can only be sent on a transport or | 
 | 2750 | 	 * association, but not a socket. | 
 | 2751 | 	 */ | 
 | 2752 | 	if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) | 
 | 2753 | 		return -EINVAL; | 
 | 2754 |  | 
 | 2755 | 	/* Process parameters. */ | 
 | 2756 | 	error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 
 | 2757 | 					    hb_change, pmtud_change, | 
 | 2758 | 					    sackdelay_change); | 
 | 2759 |  | 
 | 2760 | 	if (error) | 
 | 2761 | 		return error; | 
 | 2762 |  | 
 | 2763 | 	/* If changes are for association, also apply parameters to each | 
 | 2764 | 	 * transport. | 
 | 2765 | 	 */ | 
 | 2766 | 	if (!trans && asoc) { | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2767 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 2768 | 				transports) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2769 | 			sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 
 | 2770 | 						    hb_change, pmtud_change, | 
 | 2771 | 						    sackdelay_change); | 
 | 2772 | 		} | 
 | 2773 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 |  | 
 | 2775 | 	return 0; | 
 | 2776 | } | 
 | 2777 |  | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2778 | static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags) | 
 | 2779 | { | 
 | 2780 | 	return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE; | 
 | 2781 | } | 
 | 2782 |  | 
 | 2783 | static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags) | 
 | 2784 | { | 
 | 2785 | 	return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE; | 
 | 2786 | } | 
 | 2787 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2788 | /* | 
 | 2789 |  * 7.1.23.  Get or set delayed ack timer (SCTP_DELAYED_SACK) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2790 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2791 |  * This option will effect the way delayed acks are performed.  This | 
 | 2792 |  * option allows you to get or set the delayed ack time, in | 
 | 2793 |  * milliseconds.  It also allows changing the delayed ack frequency. | 
 | 2794 |  * Changing the frequency to 1 disables the delayed sack algorithm.  If | 
 | 2795 |  * the assoc_id is 0, then this sets or gets the endpoints default | 
 | 2796 |  * values.  If the assoc_id field is non-zero, then the set or get | 
 | 2797 |  * effects the specified association for the one to many model (the | 
 | 2798 |  * assoc_id field is ignored by the one to one model).  Note that if | 
 | 2799 |  * sack_delay or sack_freq are 0 when setting this option, then the | 
 | 2800 |  * current values will remain unchanged. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2801 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2802 |  * struct sctp_sack_info { | 
 | 2803 |  *     sctp_assoc_t            sack_assoc_id; | 
 | 2804 |  *     uint32_t                sack_delay; | 
 | 2805 |  *     uint32_t                sack_freq; | 
 | 2806 |  * }; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2807 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2808 |  * sack_assoc_id -  This parameter, indicates which association the user | 
 | 2809 |  *    is performing an action upon.  Note that if this field's value is | 
 | 2810 |  *    zero then the endpoints default value is changed (effecting future | 
 | 2811 |  *    associations only). | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2812 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2813 |  * sack_delay -  This parameter contains the number of milliseconds that | 
 | 2814 |  *    the user is requesting the delayed ACK timer be set to.  Note that | 
 | 2815 |  *    this value is defined in the standard to be between 200 and 500 | 
 | 2816 |  *    milliseconds. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2817 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2818 |  * sack_freq -  This parameter contains the number of packets that must | 
 | 2819 |  *    be received before a sack is sent without waiting for the delay | 
 | 2820 |  *    timer to expire.  The default value for this is 2, setting this | 
 | 2821 |  *    value to 1 will disable the delayed sack algorithm. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2822 |  */ | 
 | 2823 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2824 | static int sctp_setsockopt_delayed_ack(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2825 | 				       char __user *optval, unsigned int optlen) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2826 | { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2827 | 	struct sctp_sack_info    params; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2828 | 	struct sctp_transport   *trans = NULL; | 
 | 2829 | 	struct sctp_association *asoc = NULL; | 
 | 2830 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 2831 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2832 | 	if (optlen == sizeof(struct sctp_sack_info)) { | 
 | 2833 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2834 | 			return -EFAULT; | 
 | 2835 |  | 
 | 2836 | 		if (params.sack_delay == 0 && params.sack_freq == 0) | 
 | 2837 | 			return 0; | 
 | 2838 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2839 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2840 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2841 | 				    "Use of struct sctp_assoc_value in delayed_ack socket option.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2842 | 				    "Use struct sctp_sack_info instead\n", | 
 | 2843 | 				    current->comm, task_pid_nr(current)); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2844 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2845 | 			return -EFAULT; | 
 | 2846 |  | 
 | 2847 | 		if (params.sack_delay == 0) | 
 | 2848 | 			params.sack_freq = 1; | 
 | 2849 | 		else | 
 | 2850 | 			params.sack_freq = 0; | 
 | 2851 | 	} else | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2852 | 		return -EINVAL; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2853 |  | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2854 | 	/* Validate value parameter. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2855 | 	if (params.sack_delay > 500) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2856 | 		return -EINVAL; | 
 | 2857 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2858 | 	/* Get association, if sack_assoc_id != 0 and the socket is a one | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2859 | 	 * to many style socket, and an association was not found, then | 
 | 2860 | 	 * the id was invalid. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2861 | 	 */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2862 | 	asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 
 | 2863 | 	if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2864 | 		return -EINVAL; | 
 | 2865 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2866 | 	if (params.sack_delay) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2867 | 		if (asoc) { | 
 | 2868 | 			asoc->sackdelay = | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2869 | 				msecs_to_jiffies(params.sack_delay); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2870 | 			asoc->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2871 | 				sctp_spp_sackdelay_enable(asoc->param_flags); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2872 | 		} else { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2873 | 			sp->sackdelay = params.sack_delay; | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2874 | 			sp->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2875 | 				sctp_spp_sackdelay_enable(sp->param_flags); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2876 | 		} | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2877 | 	} | 
 | 2878 |  | 
 | 2879 | 	if (params.sack_freq == 1) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2880 | 		if (asoc) { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2881 | 			asoc->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2882 | 				sctp_spp_sackdelay_disable(asoc->param_flags); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2883 | 		} else { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2884 | 			sp->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2885 | 				sctp_spp_sackdelay_disable(sp->param_flags); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2886 | 		} | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2887 | 	} else if (params.sack_freq > 1) { | 
 | 2888 | 		if (asoc) { | 
 | 2889 | 			asoc->sackfreq = params.sack_freq; | 
 | 2890 | 			asoc->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2891 | 				sctp_spp_sackdelay_enable(asoc->param_flags); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2892 | 		} else { | 
 | 2893 | 			sp->sackfreq = params.sack_freq; | 
 | 2894 | 			sp->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2895 | 				sctp_spp_sackdelay_enable(sp->param_flags); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2896 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2897 | 	} | 
 | 2898 |  | 
 | 2899 | 	/* If change is for association, also apply to each transport. */ | 
 | 2900 | 	if (asoc) { | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2901 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 2902 | 				transports) { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2903 | 			if (params.sack_delay) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2904 | 				trans->sackdelay = | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2905 | 					msecs_to_jiffies(params.sack_delay); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2906 | 				trans->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2907 | 					sctp_spp_sackdelay_enable(trans->param_flags); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2908 | 			} | 
| Vlad Yasevich | 7bfe8bdb | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 2909 | 			if (params.sack_freq == 1) { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2910 | 				trans->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2911 | 					sctp_spp_sackdelay_disable(trans->param_flags); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2912 | 			} else if (params.sack_freq > 1) { | 
 | 2913 | 				trans->sackfreq = params.sack_freq; | 
 | 2914 | 				trans->param_flags = | 
| wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2915 | 					sctp_spp_sackdelay_enable(trans->param_flags); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2916 | 			} | 
 | 2917 | 		} | 
 | 2918 | 	} | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2919 |  | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2920 | 	return 0; | 
 | 2921 | } | 
 | 2922 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 
 | 2924 |  * | 
 | 2925 |  * Applications can specify protocol parameters for the default association | 
 | 2926 |  * initialization.  The option name argument to setsockopt() and getsockopt() | 
 | 2927 |  * is SCTP_INITMSG. | 
 | 2928 |  * | 
 | 2929 |  * Setting initialization parameters is effective only on an unconnected | 
 | 2930 |  * socket (for UDP-style sockets only future associations are effected | 
 | 2931 |  * by the change).  With TCP-style sockets, this option is inherited by | 
 | 2932 |  * sockets derived from a listener socket. | 
 | 2933 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2934 | 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] | 2935 | { | 
 | 2936 | 	struct sctp_initmsg sinit; | 
 | 2937 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2938 |  | 
 | 2939 | 	if (optlen != sizeof(struct sctp_initmsg)) | 
 | 2940 | 		return -EINVAL; | 
 | 2941 | 	if (copy_from_user(&sinit, optval, optlen)) | 
 | 2942 | 		return -EFAULT; | 
 | 2943 |  | 
 | 2944 | 	if (sinit.sinit_num_ostreams) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2945 | 		sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | 	if (sinit.sinit_max_instreams) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2947 | 		sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | 	if (sinit.sinit_max_attempts) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2949 | 		sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | 	if (sinit.sinit_max_init_timeo) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2951 | 		sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 |  | 
 | 2953 | 	return 0; | 
 | 2954 | } | 
 | 2955 |  | 
 | 2956 | /* | 
 | 2957 |  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 
 | 2958 |  * | 
 | 2959 |  *   Applications that wish to use the sendto() system call may wish to | 
 | 2960 |  *   specify a default set of parameters that would normally be supplied | 
 | 2961 |  *   through the inclusion of ancillary data.  This socket option allows | 
 | 2962 |  *   such an application to set the default sctp_sndrcvinfo structure. | 
 | 2963 |  *   The application that wishes to use this socket option simply passes | 
 | 2964 |  *   in to this call the sctp_sndrcvinfo structure defined in Section | 
 | 2965 |  *   5.2.2) The input parameters accepted by this call include | 
 | 2966 |  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 
 | 2967 |  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in | 
 | 2968 |  *   to this call if the caller is using the UDP model. | 
 | 2969 |  */ | 
 | 2970 | static int sctp_setsockopt_default_send_param(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2971 | 					      char __user *optval, | 
 | 2972 | 					      unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2975 | 	struct sctp_association *asoc; | 
 | 2976 | 	struct sctp_sndrcvinfo info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 |  | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2978 | 	if (optlen != sizeof(info)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | 		return -EINVAL; | 
 | 2980 | 	if (copy_from_user(&info, optval, optlen)) | 
 | 2981 | 		return -EFAULT; | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2982 | 	if (info.sinfo_flags & | 
 | 2983 | 	    ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 
 | 2984 | 	      SCTP_ABORT | SCTP_EOF)) | 
 | 2985 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 |  | 
 | 2987 | 	asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 
 | 2988 | 	if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 
 | 2989 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | 	if (asoc) { | 
 | 2991 | 		asoc->default_stream = info.sinfo_stream; | 
 | 2992 | 		asoc->default_flags = info.sinfo_flags; | 
 | 2993 | 		asoc->default_ppid = info.sinfo_ppid; | 
 | 2994 | 		asoc->default_context = info.sinfo_context; | 
 | 2995 | 		asoc->default_timetolive = info.sinfo_timetolive; | 
 | 2996 | 	} else { | 
 | 2997 | 		sp->default_stream = info.sinfo_stream; | 
 | 2998 | 		sp->default_flags = info.sinfo_flags; | 
 | 2999 | 		sp->default_ppid = info.sinfo_ppid; | 
 | 3000 | 		sp->default_context = info.sinfo_context; | 
 | 3001 | 		sp->default_timetolive = info.sinfo_timetolive; | 
 | 3002 | 	} | 
 | 3003 |  | 
 | 3004 | 	return 0; | 
 | 3005 | } | 
 | 3006 |  | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 3007 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters | 
 | 3008 |  * (SCTP_DEFAULT_SNDINFO) | 
 | 3009 |  */ | 
 | 3010 | static int sctp_setsockopt_default_sndinfo(struct sock *sk, | 
 | 3011 | 					   char __user *optval, | 
 | 3012 | 					   unsigned int optlen) | 
 | 3013 | { | 
 | 3014 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 3015 | 	struct sctp_association *asoc; | 
 | 3016 | 	struct sctp_sndinfo info; | 
 | 3017 |  | 
 | 3018 | 	if (optlen != sizeof(info)) | 
 | 3019 | 		return -EINVAL; | 
 | 3020 | 	if (copy_from_user(&info, optval, optlen)) | 
 | 3021 | 		return -EFAULT; | 
 | 3022 | 	if (info.snd_flags & | 
 | 3023 | 	    ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 
 | 3024 | 	      SCTP_ABORT | SCTP_EOF)) | 
 | 3025 | 		return -EINVAL; | 
 | 3026 |  | 
 | 3027 | 	asoc = sctp_id2assoc(sk, info.snd_assoc_id); | 
 | 3028 | 	if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) | 
 | 3029 | 		return -EINVAL; | 
 | 3030 | 	if (asoc) { | 
 | 3031 | 		asoc->default_stream = info.snd_sid; | 
 | 3032 | 		asoc->default_flags = info.snd_flags; | 
 | 3033 | 		asoc->default_ppid = info.snd_ppid; | 
 | 3034 | 		asoc->default_context = info.snd_context; | 
 | 3035 | 	} else { | 
 | 3036 | 		sp->default_stream = info.snd_sid; | 
 | 3037 | 		sp->default_flags = info.snd_flags; | 
 | 3038 | 		sp->default_ppid = info.snd_ppid; | 
 | 3039 | 		sp->default_context = info.snd_context; | 
 | 3040 | 	} | 
 | 3041 |  | 
 | 3042 | 	return 0; | 
 | 3043 | } | 
 | 3044 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 
 | 3046 |  * | 
 | 3047 |  * Requests that the local SCTP stack use the enclosed peer address as | 
 | 3048 |  * the association primary.  The enclosed address must be one of the | 
 | 3049 |  * association peer's addresses. | 
 | 3050 |  */ | 
 | 3051 | 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] | 3052 | 					unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | { | 
 | 3054 | 	struct sctp_prim prim; | 
 | 3055 | 	struct sctp_transport *trans; | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3056 | 	struct sctp_af *af; | 
 | 3057 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 |  | 
 | 3059 | 	if (optlen != sizeof(struct sctp_prim)) | 
 | 3060 | 		return -EINVAL; | 
 | 3061 |  | 
 | 3062 | 	if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) | 
 | 3063 | 		return -EFAULT; | 
 | 3064 |  | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3065 | 	/* Allow security module to validate address but need address len. */ | 
 | 3066 | 	af = sctp_get_af_specific(prim.ssp_addr.ss_family); | 
 | 3067 | 	if (!af) | 
 | 3068 | 		return -EINVAL; | 
 | 3069 |  | 
 | 3070 | 	err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR, | 
 | 3071 | 					 (struct sockaddr *)&prim.ssp_addr, | 
 | 3072 | 					 af->sockaddr_len); | 
 | 3073 | 	if (err) | 
 | 3074 | 		return err; | 
 | 3075 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | 	trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); | 
 | 3077 | 	if (!trans) | 
 | 3078 | 		return -EINVAL; | 
 | 3079 |  | 
 | 3080 | 	sctp_assoc_set_primary(trans->asoc, trans); | 
 | 3081 |  | 
 | 3082 | 	return 0; | 
 | 3083 | } | 
 | 3084 |  | 
 | 3085 | /* | 
 | 3086 |  * 7.1.5 SCTP_NODELAY | 
 | 3087 |  * | 
 | 3088 |  * Turn on/off any Nagle-like algorithm.  This means that packets are | 
 | 3089 |  * generally sent as soon as possible and no unnecessary delays are | 
 | 3090 |  * introduced, at the cost of more packets in the network.  Expects an | 
 | 3091 |  *  integer boolean flag. | 
 | 3092 |  */ | 
 | 3093 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3094 | 				   unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | { | 
 | 3096 | 	int val; | 
 | 3097 |  | 
 | 3098 | 	if (optlen < sizeof(int)) | 
 | 3099 | 		return -EINVAL; | 
 | 3100 | 	if (get_user(val, (int __user *)optval)) | 
 | 3101 | 		return -EFAULT; | 
 | 3102 |  | 
 | 3103 | 	sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; | 
 | 3104 | 	return 0; | 
 | 3105 | } | 
 | 3106 |  | 
 | 3107 | /* | 
 | 3108 |  * | 
 | 3109 |  * 7.1.1 SCTP_RTOINFO | 
 | 3110 |  * | 
 | 3111 |  * The protocol parameters used to initialize and bound retransmission | 
 | 3112 |  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 
 | 3113 |  * and modify these parameters. | 
 | 3114 |  * All parameters are time values, in milliseconds.  A value of 0, when | 
 | 3115 |  * modifying the parameters, indicates that the current value should not | 
 | 3116 |  * be changed. | 
 | 3117 |  * | 
 | 3118 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3119 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) | 
 | 3120 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | 	struct sctp_rtoinfo rtoinfo; | 
 | 3122 | 	struct sctp_association *asoc; | 
| wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3123 | 	unsigned long rto_min, rto_max; | 
 | 3124 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 |  | 
 | 3126 | 	if (optlen != sizeof (struct sctp_rtoinfo)) | 
 | 3127 | 		return -EINVAL; | 
 | 3128 |  | 
 | 3129 | 	if (copy_from_user(&rtoinfo, optval, optlen)) | 
 | 3130 | 		return -EFAULT; | 
 | 3131 |  | 
 | 3132 | 	asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 
 | 3133 |  | 
 | 3134 | 	/* Set the values to the specific association */ | 
 | 3135 | 	if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 
 | 3136 | 		return -EINVAL; | 
 | 3137 |  | 
| wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3138 | 	rto_max = rtoinfo.srto_max; | 
 | 3139 | 	rto_min = rtoinfo.srto_min; | 
 | 3140 |  | 
 | 3141 | 	if (rto_max) | 
 | 3142 | 		rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max; | 
 | 3143 | 	else | 
 | 3144 | 		rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max; | 
 | 3145 |  | 
 | 3146 | 	if (rto_min) | 
 | 3147 | 		rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min; | 
 | 3148 | 	else | 
 | 3149 | 		rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min; | 
 | 3150 |  | 
 | 3151 | 	if (rto_min > rto_max) | 
 | 3152 | 		return -EINVAL; | 
 | 3153 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | 	if (asoc) { | 
 | 3155 | 		if (rtoinfo.srto_initial != 0) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3156 | 			asoc->rto_initial = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | 				msecs_to_jiffies(rtoinfo.srto_initial); | 
| wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3158 | 		asoc->rto_max = rto_max; | 
 | 3159 | 		asoc->rto_min = rto_min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | 	} else { | 
 | 3161 | 		/* If there is no association or the association-id = 0 | 
 | 3162 | 		 * set the values to the endpoint. | 
 | 3163 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | 		if (rtoinfo.srto_initial != 0) | 
 | 3165 | 			sp->rtoinfo.srto_initial = rtoinfo.srto_initial; | 
| wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3166 | 		sp->rtoinfo.srto_max = rto_max; | 
 | 3167 | 		sp->rtoinfo.srto_min = rto_min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | 	} | 
 | 3169 |  | 
 | 3170 | 	return 0; | 
 | 3171 | } | 
 | 3172 |  | 
 | 3173 | /* | 
 | 3174 |  * | 
 | 3175 |  * 7.1.2 SCTP_ASSOCINFO | 
 | 3176 |  * | 
| Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 3177 |  * This option is used to tune the maximum retransmission attempts | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 |  * of the association. | 
 | 3179 |  * Returns an error if the new association retransmission value is | 
 | 3180 |  * greater than the sum of the retransmission value  of the peer. | 
 | 3181 |  * See [SCTP] for more information. | 
 | 3182 |  * | 
 | 3183 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3184 | 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] | 3185 | { | 
 | 3186 |  | 
 | 3187 | 	struct sctp_assocparams assocparams; | 
 | 3188 | 	struct sctp_association *asoc; | 
 | 3189 |  | 
 | 3190 | 	if (optlen != sizeof(struct sctp_assocparams)) | 
 | 3191 | 		return -EINVAL; | 
 | 3192 | 	if (copy_from_user(&assocparams, optval, optlen)) | 
 | 3193 | 		return -EFAULT; | 
 | 3194 |  | 
 | 3195 | 	asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 
 | 3196 |  | 
 | 3197 | 	if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 
 | 3198 | 		return -EINVAL; | 
 | 3199 |  | 
 | 3200 | 	/* Set the values to the specific association */ | 
 | 3201 | 	if (asoc) { | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3202 | 		if (assocparams.sasoc_asocmaxrxt != 0) { | 
 | 3203 | 			__u32 path_sum = 0; | 
 | 3204 | 			int   paths = 0; | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3205 | 			struct sctp_transport *peer_addr; | 
 | 3206 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 3207 | 			list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, | 
 | 3208 | 					transports) { | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3209 | 				path_sum += peer_addr->pathmaxrxt; | 
 | 3210 | 				paths++; | 
 | 3211 | 			} | 
 | 3212 |  | 
| Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 3213 | 			/* Only validate asocmaxrxt if we have more than | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3214 | 			 * one path/transport.  We do this because path | 
 | 3215 | 			 * retransmissions are only counted when we have more | 
 | 3216 | 			 * then one path. | 
 | 3217 | 			 */ | 
 | 3218 | 			if (paths > 1 && | 
 | 3219 | 			    assocparams.sasoc_asocmaxrxt > path_sum) | 
 | 3220 | 				return -EINVAL; | 
 | 3221 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | 			asoc->max_retrans = assocparams.sasoc_asocmaxrxt; | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3223 | 		} | 
 | 3224 |  | 
| Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 3225 | 		if (assocparams.sasoc_cookie_life != 0) | 
 | 3226 | 			asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | 	} else { | 
 | 3228 | 		/* Set the values to the endpoint */ | 
 | 3229 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 3230 |  | 
 | 3231 | 		if (assocparams.sasoc_asocmaxrxt != 0) | 
 | 3232 | 			sp->assocparams.sasoc_asocmaxrxt = | 
 | 3233 | 						assocparams.sasoc_asocmaxrxt; | 
 | 3234 | 		if (assocparams.sasoc_cookie_life != 0) | 
 | 3235 | 			sp->assocparams.sasoc_cookie_life = | 
 | 3236 | 						assocparams.sasoc_cookie_life; | 
 | 3237 | 	} | 
 | 3238 | 	return 0; | 
 | 3239 | } | 
 | 3240 |  | 
 | 3241 | /* | 
 | 3242 |  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 
 | 3243 |  * | 
 | 3244 |  * This socket option is a boolean flag which turns on or off mapped V4 | 
 | 3245 |  * addresses.  If this option is turned on and the socket is type | 
 | 3246 |  * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 
 | 3247 |  * If this option is turned off, then no mapping will be done of V4 | 
 | 3248 |  * addresses and a user will receive both PF_INET6 and PF_INET type | 
 | 3249 |  * addresses on the socket. | 
 | 3250 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3251 | 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] | 3252 | { | 
 | 3253 | 	int val; | 
 | 3254 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 3255 |  | 
 | 3256 | 	if (optlen < sizeof(int)) | 
 | 3257 | 		return -EINVAL; | 
 | 3258 | 	if (get_user(val, (int __user *)optval)) | 
 | 3259 | 		return -EFAULT; | 
 | 3260 | 	if (val) | 
 | 3261 | 		sp->v4mapped = 1; | 
 | 3262 | 	else | 
 | 3263 | 		sp->v4mapped = 0; | 
 | 3264 |  | 
 | 3265 | 	return 0; | 
 | 3266 | } | 
 | 3267 |  | 
 | 3268 | /* | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3269 |  * 8.1.16.  Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 
 | 3270 |  * This option will get or set the maximum size to put in any outgoing | 
 | 3271 |  * 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] | 3272 |  * fragmented by SCTP into the specified size.  Note that the underlying | 
 | 3273 |  * SCTP implementation may fragment into smaller sized chunks when the | 
 | 3274 |  * PMTU of the underlying association is smaller than the value set by | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3275 |  * the user.  The default value for this option is '0' which indicates | 
 | 3276 |  * the user is NOT limiting fragmentation and only the PMTU will effect | 
 | 3277 |  * SCTP's choice of DATA chunk size.  Note also that values set larger | 
 | 3278 |  * than the maximum size of an IP datagram will effectively let SCTP | 
 | 3279 |  * control fragmentation (i.e. the same as setting this option to 0). | 
 | 3280 |  * | 
 | 3281 |  * The following structure is used to access and modify this parameter: | 
 | 3282 |  * | 
 | 3283 |  * struct sctp_assoc_value { | 
 | 3284 |  *   sctp_assoc_t assoc_id; | 
 | 3285 |  *   uint32_t assoc_value; | 
 | 3286 |  * }; | 
 | 3287 |  * | 
 | 3288 |  * assoc_id:  This parameter is ignored for one-to-one style sockets. | 
 | 3289 |  *    For one-to-many style sockets this parameter indicates which | 
 | 3290 |  *    association the user is performing an action upon.  Note that if | 
 | 3291 |  *    this field's value is zero then the endpoints default value is | 
 | 3292 |  *    changed (effecting future associations only). | 
 | 3293 |  * assoc_value:  This parameter specifies the maximum size in bytes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3295 | 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] | 3296 | { | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3297 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3298 | 	struct sctp_assoc_value params; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | 	int val; | 
 | 3301 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3302 | 	if (optlen == sizeof(int)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3303 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3304 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3305 | 				    "Use of int in maxseg socket option.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3306 | 				    "Use struct sctp_assoc_value instead\n", | 
 | 3307 | 				    current->comm, task_pid_nr(current)); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3308 | 		if (copy_from_user(&val, optval, optlen)) | 
 | 3309 | 			return -EFAULT; | 
 | 3310 | 		params.assoc_id = 0; | 
 | 3311 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
 | 3312 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 3313 | 			return -EFAULT; | 
 | 3314 | 		val = params.assoc_value; | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3315 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | 		return -EINVAL; | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3317 | 	} | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3318 |  | 
| Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3319 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 3320 |  | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3321 | 	if (val) { | 
 | 3322 | 		int min_len, max_len; | 
| Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3323 | 		__u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) : | 
 | 3324 | 				 sizeof(struct sctp_data_chunk); | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3325 |  | 
| Marcelo Ricardo Leitner | feddd6c | 2018-04-26 16:58:54 -0300 | [diff] [blame] | 3326 | 		min_len = sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT, | 
| Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3327 | 					   datasize); | 
 | 3328 | 		max_len = SCTP_MAX_CHUNK_LEN - datasize; | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3329 |  | 
 | 3330 | 		if (val < min_len || val > max_len) | 
 | 3331 | 			return -EINVAL; | 
 | 3332 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3334 | 	if (asoc) { | 
| Vlad Yasevich | f68b2e0 | 2009-09-04 18:21:00 -0400 | [diff] [blame] | 3335 | 		asoc->user_frag = val; | 
| Marcelo Ricardo Leitner | 2f5e3c9 | 2018-04-26 16:58:55 -0300 | [diff] [blame] | 3336 | 		sctp_assoc_update_frag_point(asoc); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3337 | 	} else { | 
| Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3338 | 		if (params.assoc_id && sctp_style(sk, UDP)) | 
 | 3339 | 			return -EINVAL; | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3340 | 		sp->user_frag = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | 	} | 
 | 3342 |  | 
 | 3343 | 	return 0; | 
 | 3344 | } | 
 | 3345 |  | 
 | 3346 |  | 
 | 3347 | /* | 
 | 3348 |  *  7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) | 
 | 3349 |  * | 
 | 3350 |  *   Requests that the peer mark the enclosed address as the association | 
 | 3351 |  *   primary. The enclosed address must be one of the association's | 
 | 3352 |  *   locally bound addresses. The following structure is used to make a | 
 | 3353 |  *   set primary request: | 
 | 3354 |  */ | 
 | 3355 | 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] | 3356 | 					     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3358 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | 	struct sctp_sock	*sp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | 	struct sctp_association	*asoc = NULL; | 
 | 3361 | 	struct sctp_setpeerprim	prim; | 
 | 3362 | 	struct sctp_chunk	*chunk; | 
| Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3363 | 	struct sctp_af		*af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | 	int 			err; | 
 | 3365 |  | 
 | 3366 | 	sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3368 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | 		return -EPERM; | 
 | 3370 |  | 
 | 3371 | 	if (optlen != sizeof(struct sctp_setpeerprim)) | 
 | 3372 | 		return -EINVAL; | 
 | 3373 |  | 
 | 3374 | 	if (copy_from_user(&prim, optval, optlen)) | 
 | 3375 | 		return -EFAULT; | 
 | 3376 |  | 
 | 3377 | 	asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3378 | 	if (!asoc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | 		return -EINVAL; | 
 | 3380 |  | 
 | 3381 | 	if (!asoc->peer.asconf_capable) | 
 | 3382 | 		return -EPERM; | 
 | 3383 |  | 
 | 3384 | 	if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) | 
 | 3385 | 		return -EPERM; | 
 | 3386 |  | 
 | 3387 | 	if (!sctp_state(asoc, ESTABLISHED)) | 
 | 3388 | 		return -ENOTCONN; | 
 | 3389 |  | 
| Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3390 | 	af = sctp_get_af_specific(prim.sspp_addr.ss_family); | 
 | 3391 | 	if (!af) | 
 | 3392 | 		return -EINVAL; | 
 | 3393 |  | 
 | 3394 | 	if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL)) | 
 | 3395 | 		return -EADDRNOTAVAIL; | 
 | 3396 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | 	if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) | 
 | 3398 | 		return -EADDRNOTAVAIL; | 
 | 3399 |  | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3400 | 	/* Allow security module to validate address. */ | 
 | 3401 | 	err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR, | 
 | 3402 | 					 (struct sockaddr *)&prim.sspp_addr, | 
 | 3403 | 					 af->sockaddr_len); | 
 | 3404 | 	if (err) | 
 | 3405 | 		return err; | 
 | 3406 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | 	/* Create an ASCONF chunk with SET_PRIMARY parameter	*/ | 
 | 3408 | 	chunk = sctp_make_asconf_set_prim(asoc, | 
 | 3409 | 					  (union sctp_addr *)&prim.sspp_addr); | 
 | 3410 | 	if (!chunk) | 
 | 3411 | 		return -ENOMEM; | 
 | 3412 |  | 
 | 3413 | 	err = sctp_send_asconf(asoc, chunk); | 
 | 3414 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 3415 | 	pr_debug("%s: we set peer primary addr primitively\n", __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 |  | 
 | 3417 | 	return err; | 
 | 3418 | } | 
 | 3419 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3420 | 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] | 3421 | 					    unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | { | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3423 | 	struct sctp_setadaptation adaptation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3425 | 	if (optlen != sizeof(struct sctp_setadaptation)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | 		return -EINVAL; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3427 | 	if (copy_from_user(&adaptation, optval, optlen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | 		return -EFAULT; | 
 | 3429 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3430 | 	sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 |  | 
 | 3432 | 	return 0; | 
 | 3433 | } | 
 | 3434 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3435 | /* | 
 | 3436 |  * 7.1.29.  Set or Get the default context (SCTP_CONTEXT) | 
 | 3437 |  * | 
 | 3438 |  * The context field in the sctp_sndrcvinfo structure is normally only | 
 | 3439 |  * used when a failed message is retrieved holding the value that was | 
 | 3440 |  * sent down on the actual send call.  This option allows the setting of | 
 | 3441 |  * a default context on an association basis that will be received on | 
 | 3442 |  * reading messages from the peer.  This is especially helpful in the | 
 | 3443 |  * one-2-many model for an application to keep some reference to an | 
 | 3444 |  * internal state machine that is processing messages on the | 
 | 3445 |  * association.  Note that the setting of this value only effects | 
 | 3446 |  * received messages from the peer and does not effect the value that is | 
 | 3447 |  * saved with outbound messages. | 
 | 3448 |  */ | 
 | 3449 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3450 | 				   unsigned int optlen) | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3451 | { | 
 | 3452 | 	struct sctp_assoc_value params; | 
 | 3453 | 	struct sctp_sock *sp; | 
 | 3454 | 	struct sctp_association *asoc; | 
 | 3455 |  | 
 | 3456 | 	if (optlen != sizeof(struct sctp_assoc_value)) | 
 | 3457 | 		return -EINVAL; | 
 | 3458 | 	if (copy_from_user(¶ms, optval, optlen)) | 
 | 3459 | 		return -EFAULT; | 
 | 3460 |  | 
 | 3461 | 	sp = sctp_sk(sk); | 
 | 3462 |  | 
 | 3463 | 	if (params.assoc_id != 0) { | 
 | 3464 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 3465 | 		if (!asoc) | 
 | 3466 | 			return -EINVAL; | 
 | 3467 | 		asoc->default_rcv_context = params.assoc_value; | 
 | 3468 | 	} else { | 
 | 3469 | 		sp->default_rcv_context = params.assoc_value; | 
 | 3470 | 	} | 
 | 3471 |  | 
 | 3472 | 	return 0; | 
 | 3473 | } | 
 | 3474 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3475 | /* | 
 | 3476 |  * 7.1.24.  Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 
 | 3477 |  * | 
 | 3478 |  * This options will at a minimum specify if the implementation is doing | 
 | 3479 |  * fragmented interleave.  Fragmented interleave, for a one to many | 
 | 3480 |  * socket, is when subsequent calls to receive a message may return | 
 | 3481 |  * parts of messages from different associations.  Some implementations | 
 | 3482 |  * may allow you to turn this value on or off.  If so, when turned off, | 
 | 3483 |  * no fragment interleave will occur (which will cause a head of line | 
 | 3484 |  * blocking amongst multiple associations sharing the same one to many | 
 | 3485 |  * socket).  When this option is turned on, then each receive call may | 
 | 3486 |  * come from a different association (thus the user must receive data | 
 | 3487 |  * with the extended calls (e.g. sctp_recvmsg) to keep track of which | 
 | 3488 |  * association each receive belongs to. | 
 | 3489 |  * | 
 | 3490 |  * This option takes a boolean value.  A non-zero value indicates that | 
 | 3491 |  * fragmented interleave is on.  A value of zero indicates that | 
 | 3492 |  * fragmented interleave is off. | 
 | 3493 |  * | 
 | 3494 |  * Note that it is important that an implementation that allows this | 
 | 3495 |  * option to be turned on, have it off by default.  Otherwise an unaware | 
 | 3496 |  * application using the one to many model may become confused and act | 
 | 3497 |  * incorrectly. | 
 | 3498 |  */ | 
 | 3499 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, | 
 | 3500 | 					       char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3501 | 					       unsigned int optlen) | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3502 | { | 
 | 3503 | 	int val; | 
 | 3504 |  | 
 | 3505 | 	if (optlen != sizeof(int)) | 
 | 3506 | 		return -EINVAL; | 
 | 3507 | 	if (get_user(val, (int __user *)optval)) | 
 | 3508 | 		return -EFAULT; | 
 | 3509 |  | 
| Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 3510 | 	sctp_sk(sk)->frag_interleave = !!val; | 
 | 3511 |  | 
 | 3512 | 	if (!sctp_sk(sk)->frag_interleave) | 
 | 3513 | 		sctp_sk(sk)->strm_interleave = 0; | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3514 |  | 
 | 3515 | 	return 0; | 
 | 3516 | } | 
 | 3517 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3518 | /* | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3519 |  * 8.1.21.  Set or Get the SCTP Partial Delivery Point | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3520 |  *       (SCTP_PARTIAL_DELIVERY_POINT) | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3521 |  * | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3522 |  * This option will set or get the SCTP partial delivery point.  This | 
 | 3523 |  * point is the size of a message where the partial delivery API will be | 
 | 3524 |  * 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] | 3525 |  * lower value will cause partial deliveries to happen more often.  The | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3526 |  * calls argument is an integer that sets or gets the partial delivery | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3527 |  * point.  Note also that the call will fail if the user attempts to set | 
 | 3528 |  * this value larger than the socket receive buffer size. | 
 | 3529 |  * | 
 | 3530 |  * Note that any single message having a length smaller than or equal to | 
 | 3531 |  * the SCTP partial delivery point will be delivered in one single read | 
 | 3532 |  * call as long as the user provided buffer is large enough to hold the | 
 | 3533 |  * message. | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3534 |  */ | 
 | 3535 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, | 
 | 3536 | 						  char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3537 | 						  unsigned int optlen) | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3538 | { | 
 | 3539 | 	u32 val; | 
 | 3540 |  | 
 | 3541 | 	if (optlen != sizeof(u32)) | 
 | 3542 | 		return -EINVAL; | 
 | 3543 | 	if (get_user(val, (int __user *)optval)) | 
 | 3544 | 		return -EFAULT; | 
 | 3545 |  | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3546 | 	/* Note: We double the receive buffer from what the user sets | 
 | 3547 | 	 * it to be, also initial rwnd is based on rcvbuf/2. | 
 | 3548 | 	 */ | 
 | 3549 | 	if (val > (sk->sk_rcvbuf >> 1)) | 
 | 3550 | 		return -EINVAL; | 
 | 3551 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3552 | 	sctp_sk(sk)->pd_point = val; | 
 | 3553 |  | 
 | 3554 | 	return 0; /* is this the right error code? */ | 
 | 3555 | } | 
 | 3556 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3557 | /* | 
 | 3558 |  * 7.1.28.  Set or Get the maximum burst (SCTP_MAX_BURST) | 
 | 3559 |  * | 
 | 3560 |  * This option will allow a user to change the maximum burst of packets | 
 | 3561 |  * that can be emitted by this association.  Note that the default value | 
 | 3562 |  * is 4, and some implementations may restrict this setting so that it | 
 | 3563 |  * can only be lowered. | 
 | 3564 |  * | 
 | 3565 |  * NOTE: This text doesn't seem right.  Do this on a socket basis with | 
 | 3566 |  * future associations inheriting the socket value. | 
 | 3567 |  */ | 
 | 3568 | static int sctp_setsockopt_maxburst(struct sock *sk, | 
 | 3569 | 				    char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3570 | 				    unsigned int optlen) | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3571 | { | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3572 | 	struct sctp_assoc_value params; | 
 | 3573 | 	struct sctp_sock *sp; | 
 | 3574 | 	struct sctp_association *asoc; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3575 | 	int val; | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3576 | 	int assoc_id = 0; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3577 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3578 | 	if (optlen == sizeof(int)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3579 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3580 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3581 | 				    "Use of int in max_burst socket option deprecated.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3582 | 				    "Use struct sctp_assoc_value instead\n", | 
 | 3583 | 				    current->comm, task_pid_nr(current)); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3584 | 		if (copy_from_user(&val, optval, optlen)) | 
 | 3585 | 			return -EFAULT; | 
 | 3586 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
 | 3587 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 3588 | 			return -EFAULT; | 
 | 3589 | 		val = params.assoc_value; | 
 | 3590 | 		assoc_id = params.assoc_id; | 
 | 3591 | 	} else | 
 | 3592 | 		return -EINVAL; | 
 | 3593 |  | 
 | 3594 | 	sp = sctp_sk(sk); | 
 | 3595 |  | 
 | 3596 | 	if (assoc_id != 0) { | 
 | 3597 | 		asoc = sctp_id2assoc(sk, assoc_id); | 
 | 3598 | 		if (!asoc) | 
 | 3599 | 			return -EINVAL; | 
 | 3600 | 		asoc->max_burst = val; | 
 | 3601 | 	} else | 
 | 3602 | 		sp->max_burst = val; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3603 |  | 
 | 3604 | 	return 0; | 
 | 3605 | } | 
 | 3606 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3607 | /* | 
 | 3608 |  * 7.1.18.  Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) | 
 | 3609 |  * | 
 | 3610 |  * This set option adds a chunk type that the user is requesting to be | 
 | 3611 |  * received only in an authenticated way.  Changes to the list of chunks | 
 | 3612 |  * will only effect future associations on the socket. | 
 | 3613 |  */ | 
 | 3614 | static int sctp_setsockopt_auth_chunk(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3615 | 				      char __user *optval, | 
 | 3616 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3617 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3618 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3619 | 	struct sctp_authchunk val; | 
 | 3620 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3621 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3622 | 		return -EACCES; | 
 | 3623 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3624 | 	if (optlen != sizeof(struct sctp_authchunk)) | 
 | 3625 | 		return -EINVAL; | 
 | 3626 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3627 | 		return -EFAULT; | 
 | 3628 |  | 
 | 3629 | 	switch (val.sauth_chunk) { | 
| Joe Perches | 7fd71b1 | 2011-07-01 09:43:11 +0000 | [diff] [blame] | 3630 | 	case SCTP_CID_INIT: | 
 | 3631 | 	case SCTP_CID_INIT_ACK: | 
 | 3632 | 	case SCTP_CID_SHUTDOWN_COMPLETE: | 
 | 3633 | 	case SCTP_CID_AUTH: | 
 | 3634 | 		return -EINVAL; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3635 | 	} | 
 | 3636 |  | 
 | 3637 | 	/* add this chunk id to the endpoint */ | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3638 | 	return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3639 | } | 
 | 3640 |  | 
 | 3641 | /* | 
 | 3642 |  * 7.1.19.  Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) | 
 | 3643 |  * | 
 | 3644 |  * This option gets or sets the list of HMAC algorithms that the local | 
 | 3645 |  * endpoint requires the peer to use. | 
 | 3646 |  */ | 
 | 3647 | static int sctp_setsockopt_hmac_ident(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3648 | 				      char __user *optval, | 
 | 3649 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3650 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3651 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3652 | 	struct sctp_hmacalgo *hmacs; | 
| Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3653 | 	u32 idents; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3654 | 	int err; | 
 | 3655 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3656 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3657 | 		return -EACCES; | 
 | 3658 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3659 | 	if (optlen < sizeof(struct sctp_hmacalgo)) | 
 | 3660 | 		return -EINVAL; | 
| Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 3661 | 	optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) + | 
 | 3662 | 					     SCTP_AUTH_NUM_HMACS * sizeof(u16)); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3663 |  | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3664 | 	hmacs = memdup_user(optval, optlen); | 
| Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3665 | 	if (IS_ERR(hmacs)) | 
 | 3666 | 		return PTR_ERR(hmacs); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3667 |  | 
| Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3668 | 	idents = hmacs->shmac_num_idents; | 
 | 3669 | 	if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || | 
 | 3670 | 	    (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3671 | 		err = -EINVAL; | 
 | 3672 | 		goto out; | 
 | 3673 | 	} | 
 | 3674 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3675 | 	err = sctp_auth_ep_set_hmacs(ep, hmacs); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3676 | out: | 
 | 3677 | 	kfree(hmacs); | 
 | 3678 | 	return err; | 
 | 3679 | } | 
 | 3680 |  | 
 | 3681 | /* | 
 | 3682 |  * 7.1.20.  Set a shared key (SCTP_AUTH_KEY) | 
 | 3683 |  * | 
 | 3684 |  * This option will set a shared secret key which is used to build an | 
 | 3685 |  * association shared key. | 
 | 3686 |  */ | 
 | 3687 | static int sctp_setsockopt_auth_key(struct sock *sk, | 
 | 3688 | 				    char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3689 | 				    unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3690 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3691 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3692 | 	struct sctp_authkey *authkey; | 
 | 3693 | 	struct sctp_association *asoc; | 
 | 3694 | 	int ret; | 
 | 3695 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3696 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3697 | 		return -EACCES; | 
 | 3698 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3699 | 	if (optlen <= sizeof(struct sctp_authkey)) | 
 | 3700 | 		return -EINVAL; | 
| Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 3701 | 	/* authkey->sca_keylength is u16, so optlen can't be bigger than | 
 | 3702 | 	 * this. | 
 | 3703 | 	 */ | 
 | 3704 | 	optlen = min_t(unsigned int, optlen, USHRT_MAX + | 
 | 3705 | 					     sizeof(struct sctp_authkey)); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3706 |  | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3707 | 	authkey = memdup_user(optval, optlen); | 
| Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3708 | 	if (IS_ERR(authkey)) | 
 | 3709 | 		return PTR_ERR(authkey); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3710 |  | 
| Vlad Yasevich | 328fc47 | 2008-08-27 16:08:54 -0700 | [diff] [blame] | 3711 | 	if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { | 
| Vlad Yasevich | 30c2235 | 2008-08-25 15:16:19 -0700 | [diff] [blame] | 3712 | 		ret = -EINVAL; | 
 | 3713 | 		goto out; | 
 | 3714 | 	} | 
 | 3715 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3716 | 	asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); | 
 | 3717 | 	if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { | 
 | 3718 | 		ret = -EINVAL; | 
 | 3719 | 		goto out; | 
 | 3720 | 	} | 
 | 3721 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3722 | 	ret = sctp_auth_set_key(ep, asoc, authkey); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3723 | out: | 
| Daniel Borkmann | 6ba542a | 2013-02-08 03:04:34 +0000 | [diff] [blame] | 3724 | 	kzfree(authkey); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3725 | 	return ret; | 
 | 3726 | } | 
 | 3727 |  | 
 | 3728 | /* | 
 | 3729 |  * 7.1.21.  Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) | 
 | 3730 |  * | 
 | 3731 |  * This option will get or set the active shared key to be used to build | 
 | 3732 |  * the association shared key. | 
 | 3733 |  */ | 
 | 3734 | static int sctp_setsockopt_active_key(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3735 | 				      char __user *optval, | 
 | 3736 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3737 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3738 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3739 | 	struct sctp_authkeyid val; | 
 | 3740 | 	struct sctp_association *asoc; | 
 | 3741 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3742 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3743 | 		return -EACCES; | 
 | 3744 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3745 | 	if (optlen != sizeof(struct sctp_authkeyid)) | 
 | 3746 | 		return -EINVAL; | 
 | 3747 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3748 | 		return -EFAULT; | 
 | 3749 |  | 
 | 3750 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 3751 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 3752 | 		return -EINVAL; | 
 | 3753 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3754 | 	return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3755 | } | 
 | 3756 |  | 
 | 3757 | /* | 
 | 3758 |  * 7.1.22.  Delete a shared key (SCTP_AUTH_DELETE_KEY) | 
 | 3759 |  * | 
 | 3760 |  * This set option will delete a shared secret key from use. | 
 | 3761 |  */ | 
 | 3762 | static int sctp_setsockopt_del_key(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3763 | 				   char __user *optval, | 
 | 3764 | 				   unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3765 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3766 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3767 | 	struct sctp_authkeyid val; | 
 | 3768 | 	struct sctp_association *asoc; | 
 | 3769 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3770 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3771 | 		return -EACCES; | 
 | 3772 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3773 | 	if (optlen != sizeof(struct sctp_authkeyid)) | 
 | 3774 | 		return -EINVAL; | 
 | 3775 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3776 | 		return -EFAULT; | 
 | 3777 |  | 
 | 3778 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 3779 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 3780 | 		return -EINVAL; | 
 | 3781 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3782 | 	return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3783 |  | 
 | 3784 | } | 
 | 3785 |  | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3786 | /* | 
| Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3787 |  * 8.3.4  Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) | 
 | 3788 |  * | 
 | 3789 |  * This set option will deactivate a shared secret key. | 
 | 3790 |  */ | 
 | 3791 | static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval, | 
 | 3792 | 					  unsigned int optlen) | 
 | 3793 | { | 
 | 3794 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
 | 3795 | 	struct sctp_authkeyid val; | 
 | 3796 | 	struct sctp_association *asoc; | 
 | 3797 |  | 
 | 3798 | 	if (!ep->auth_enable) | 
 | 3799 | 		return -EACCES; | 
 | 3800 |  | 
 | 3801 | 	if (optlen != sizeof(struct sctp_authkeyid)) | 
 | 3802 | 		return -EINVAL; | 
 | 3803 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3804 | 		return -EFAULT; | 
 | 3805 |  | 
 | 3806 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 3807 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 3808 | 		return -EINVAL; | 
 | 3809 |  | 
 | 3810 | 	return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber); | 
 | 3811 | } | 
 | 3812 |  | 
 | 3813 | /* | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3814 |  * 8.1.23 SCTP_AUTO_ASCONF | 
 | 3815 |  * | 
 | 3816 |  * This option will enable or disable the use of the automatic generation of | 
 | 3817 |  * ASCONF chunks to add and delete addresses to an existing association.  Note | 
 | 3818 |  * that this option has two caveats namely: a) it only affects sockets that | 
 | 3819 |  * are bound to all addresses available to the SCTP stack, and b) the system | 
 | 3820 |  * administrator may have an overriding control that turns the ASCONF feature | 
 | 3821 |  * off no matter what setting the socket option may have. | 
 | 3822 |  * This option expects an integer boolean flag, where a non-zero value turns on | 
 | 3823 |  * the option, and a zero value turns off the option. | 
 | 3824 |  * Note. In this implementation, socket operation overrides default parameter | 
 | 3825 |  * being set by sysctl as well as FreeBSD implementation | 
 | 3826 |  */ | 
 | 3827 | static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval, | 
 | 3828 | 					unsigned int optlen) | 
 | 3829 | { | 
 | 3830 | 	int val; | 
 | 3831 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 3832 |  | 
 | 3833 | 	if (optlen < sizeof(int)) | 
 | 3834 | 		return -EINVAL; | 
 | 3835 | 	if (get_user(val, (int __user *)optval)) | 
 | 3836 | 		return -EFAULT; | 
 | 3837 | 	if (!sctp_is_ep_boundall(sk) && val) | 
 | 3838 | 		return -EINVAL; | 
 | 3839 | 	if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf)) | 
 | 3840 | 		return 0; | 
 | 3841 |  | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3842 | 	spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3843 | 	if (val == 0 && sp->do_auto_asconf) { | 
 | 3844 | 		list_del(&sp->auto_asconf_list); | 
 | 3845 | 		sp->do_auto_asconf = 0; | 
 | 3846 | 	} else if (val && !sp->do_auto_asconf) { | 
 | 3847 | 		list_add_tail(&sp->auto_asconf_list, | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 3848 | 		    &sock_net(sk)->sctp.auto_asconf_splist); | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3849 | 		sp->do_auto_asconf = 1; | 
 | 3850 | 	} | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3851 | 	spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3852 | 	return 0; | 
 | 3853 | } | 
 | 3854 |  | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3855 | /* | 
 | 3856 |  * SCTP_PEER_ADDR_THLDS | 
 | 3857 |  * | 
 | 3858 |  * This option allows us to alter the partially failed threshold for one or all | 
 | 3859 |  * transports in an association.  See Section 6.1 of: | 
 | 3860 |  * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt | 
 | 3861 |  */ | 
 | 3862 | static int sctp_setsockopt_paddr_thresholds(struct sock *sk, | 
 | 3863 | 					    char __user *optval, | 
 | 3864 | 					    unsigned int optlen) | 
 | 3865 | { | 
 | 3866 | 	struct sctp_paddrthlds val; | 
 | 3867 | 	struct sctp_transport *trans; | 
 | 3868 | 	struct sctp_association *asoc; | 
 | 3869 |  | 
 | 3870 | 	if (optlen < sizeof(struct sctp_paddrthlds)) | 
 | 3871 | 		return -EINVAL; | 
 | 3872 | 	if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, | 
 | 3873 | 			   sizeof(struct sctp_paddrthlds))) | 
 | 3874 | 		return -EFAULT; | 
 | 3875 |  | 
 | 3876 |  | 
 | 3877 | 	if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { | 
 | 3878 | 		asoc = sctp_id2assoc(sk, val.spt_assoc_id); | 
 | 3879 | 		if (!asoc) | 
 | 3880 | 			return -ENOENT; | 
 | 3881 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 3882 | 				    transports) { | 
 | 3883 | 			if (val.spt_pathmaxrxt) | 
 | 3884 | 				trans->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3885 | 			trans->pf_retrans = val.spt_pathpfthld; | 
 | 3886 | 		} | 
 | 3887 |  | 
 | 3888 | 		if (val.spt_pathmaxrxt) | 
 | 3889 | 			asoc->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3890 | 		asoc->pf_retrans = val.spt_pathpfthld; | 
 | 3891 | 	} else { | 
 | 3892 | 		trans = sctp_addr_id2transport(sk, &val.spt_address, | 
 | 3893 | 					       val.spt_assoc_id); | 
 | 3894 | 		if (!trans) | 
 | 3895 | 			return -ENOENT; | 
 | 3896 |  | 
 | 3897 | 		if (val.spt_pathmaxrxt) | 
 | 3898 | 			trans->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3899 | 		trans->pf_retrans = val.spt_pathpfthld; | 
 | 3900 | 	} | 
 | 3901 |  | 
 | 3902 | 	return 0; | 
 | 3903 | } | 
 | 3904 |  | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 3905 | static int sctp_setsockopt_recvrcvinfo(struct sock *sk, | 
 | 3906 | 				       char __user *optval, | 
 | 3907 | 				       unsigned int optlen) | 
 | 3908 | { | 
 | 3909 | 	int val; | 
 | 3910 |  | 
 | 3911 | 	if (optlen < sizeof(int)) | 
 | 3912 | 		return -EINVAL; | 
 | 3913 | 	if (get_user(val, (int __user *) optval)) | 
 | 3914 | 		return -EFAULT; | 
 | 3915 |  | 
 | 3916 | 	sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1; | 
 | 3917 |  | 
 | 3918 | 	return 0; | 
 | 3919 | } | 
 | 3920 |  | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 3921 | static int sctp_setsockopt_recvnxtinfo(struct sock *sk, | 
 | 3922 | 				       char __user *optval, | 
 | 3923 | 				       unsigned int optlen) | 
 | 3924 | { | 
 | 3925 | 	int val; | 
 | 3926 |  | 
 | 3927 | 	if (optlen < sizeof(int)) | 
 | 3928 | 		return -EINVAL; | 
 | 3929 | 	if (get_user(val, (int __user *) optval)) | 
 | 3930 | 		return -EFAULT; | 
 | 3931 |  | 
 | 3932 | 	sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1; | 
 | 3933 |  | 
 | 3934 | 	return 0; | 
 | 3935 | } | 
 | 3936 |  | 
| Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 3937 | static int sctp_setsockopt_pr_supported(struct sock *sk, | 
 | 3938 | 					char __user *optval, | 
 | 3939 | 					unsigned int optlen) | 
 | 3940 | { | 
 | 3941 | 	struct sctp_assoc_value params; | 
 | 3942 | 	struct sctp_association *asoc; | 
 | 3943 | 	int retval = -EINVAL; | 
 | 3944 |  | 
 | 3945 | 	if (optlen != sizeof(params)) | 
 | 3946 | 		goto out; | 
 | 3947 |  | 
 | 3948 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 3949 | 		retval = -EFAULT; | 
 | 3950 | 		goto out; | 
 | 3951 | 	} | 
 | 3952 |  | 
 | 3953 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 3954 | 	if (asoc) { | 
 | 3955 | 		asoc->prsctp_enable = !!params.assoc_value; | 
 | 3956 | 	} else if (!params.assoc_id) { | 
 | 3957 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 3958 |  | 
 | 3959 | 		sp->ep->prsctp_enable = !!params.assoc_value; | 
 | 3960 | 	} else { | 
 | 3961 | 		goto out; | 
 | 3962 | 	} | 
 | 3963 |  | 
 | 3964 | 	retval = 0; | 
 | 3965 |  | 
 | 3966 | out: | 
 | 3967 | 	return retval; | 
 | 3968 | } | 
 | 3969 |  | 
| Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 3970 | static int sctp_setsockopt_default_prinfo(struct sock *sk, | 
 | 3971 | 					  char __user *optval, | 
 | 3972 | 					  unsigned int optlen) | 
 | 3973 | { | 
 | 3974 | 	struct sctp_default_prinfo info; | 
 | 3975 | 	struct sctp_association *asoc; | 
 | 3976 | 	int retval = -EINVAL; | 
 | 3977 |  | 
 | 3978 | 	if (optlen != sizeof(info)) | 
 | 3979 | 		goto out; | 
 | 3980 |  | 
 | 3981 | 	if (copy_from_user(&info, optval, sizeof(info))) { | 
 | 3982 | 		retval = -EFAULT; | 
 | 3983 | 		goto out; | 
 | 3984 | 	} | 
 | 3985 |  | 
 | 3986 | 	if (info.pr_policy & ~SCTP_PR_SCTP_MASK) | 
 | 3987 | 		goto out; | 
 | 3988 |  | 
 | 3989 | 	if (info.pr_policy == SCTP_PR_SCTP_NONE) | 
 | 3990 | 		info.pr_value = 0; | 
 | 3991 |  | 
 | 3992 | 	asoc = sctp_id2assoc(sk, info.pr_assoc_id); | 
 | 3993 | 	if (asoc) { | 
 | 3994 | 		SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy); | 
 | 3995 | 		asoc->default_timetolive = info.pr_value; | 
 | 3996 | 	} else if (!info.pr_assoc_id) { | 
 | 3997 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 3998 |  | 
 | 3999 | 		SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy); | 
 | 4000 | 		sp->default_timetolive = info.pr_value; | 
 | 4001 | 	} else { | 
 | 4002 | 		goto out; | 
 | 4003 | 	} | 
 | 4004 |  | 
 | 4005 | 	retval = 0; | 
 | 4006 |  | 
 | 4007 | out: | 
 | 4008 | 	return retval; | 
 | 4009 | } | 
 | 4010 |  | 
| Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4011 | static int sctp_setsockopt_reconfig_supported(struct sock *sk, | 
 | 4012 | 					      char __user *optval, | 
 | 4013 | 					      unsigned int optlen) | 
 | 4014 | { | 
 | 4015 | 	struct sctp_assoc_value params; | 
 | 4016 | 	struct sctp_association *asoc; | 
 | 4017 | 	int retval = -EINVAL; | 
 | 4018 |  | 
 | 4019 | 	if (optlen != sizeof(params)) | 
 | 4020 | 		goto out; | 
 | 4021 |  | 
 | 4022 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4023 | 		retval = -EFAULT; | 
 | 4024 | 		goto out; | 
 | 4025 | 	} | 
 | 4026 |  | 
 | 4027 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 4028 | 	if (asoc) { | 
 | 4029 | 		asoc->reconf_enable = !!params.assoc_value; | 
 | 4030 | 	} else if (!params.assoc_id) { | 
 | 4031 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 4032 |  | 
 | 4033 | 		sp->ep->reconf_enable = !!params.assoc_value; | 
 | 4034 | 	} else { | 
 | 4035 | 		goto out; | 
 | 4036 | 	} | 
 | 4037 |  | 
 | 4038 | 	retval = 0; | 
 | 4039 |  | 
 | 4040 | out: | 
 | 4041 | 	return retval; | 
 | 4042 | } | 
 | 4043 |  | 
| Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4044 | static int sctp_setsockopt_enable_strreset(struct sock *sk, | 
 | 4045 | 					   char __user *optval, | 
 | 4046 | 					   unsigned int optlen) | 
 | 4047 | { | 
 | 4048 | 	struct sctp_assoc_value params; | 
 | 4049 | 	struct sctp_association *asoc; | 
 | 4050 | 	int retval = -EINVAL; | 
 | 4051 |  | 
 | 4052 | 	if (optlen != sizeof(params)) | 
 | 4053 | 		goto out; | 
 | 4054 |  | 
 | 4055 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4056 | 		retval = -EFAULT; | 
 | 4057 | 		goto out; | 
 | 4058 | 	} | 
 | 4059 |  | 
 | 4060 | 	if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK)) | 
 | 4061 | 		goto out; | 
 | 4062 |  | 
 | 4063 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 4064 | 	if (asoc) { | 
 | 4065 | 		asoc->strreset_enable = params.assoc_value; | 
 | 4066 | 	} else if (!params.assoc_id) { | 
 | 4067 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 4068 |  | 
 | 4069 | 		sp->ep->strreset_enable = params.assoc_value; | 
 | 4070 | 	} else { | 
 | 4071 | 		goto out; | 
 | 4072 | 	} | 
 | 4073 |  | 
 | 4074 | 	retval = 0; | 
 | 4075 |  | 
 | 4076 | out: | 
 | 4077 | 	return retval; | 
 | 4078 | } | 
 | 4079 |  | 
| Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4080 | static int sctp_setsockopt_reset_streams(struct sock *sk, | 
 | 4081 | 					 char __user *optval, | 
 | 4082 | 					 unsigned int optlen) | 
 | 4083 | { | 
 | 4084 | 	struct sctp_reset_streams *params; | 
 | 4085 | 	struct sctp_association *asoc; | 
 | 4086 | 	int retval = -EINVAL; | 
 | 4087 |  | 
| Xin Long | 2342b8d | 2017-12-10 15:40:51 +0800 | [diff] [blame] | 4088 | 	if (optlen < sizeof(*params)) | 
| Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4089 | 		return -EINVAL; | 
| Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 4090 | 	/* srs_number_streams is u16, so optlen can't be bigger than this. */ | 
 | 4091 | 	optlen = min_t(unsigned int, optlen, USHRT_MAX + | 
 | 4092 | 					     sizeof(__u16) * sizeof(*params)); | 
| Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4093 |  | 
 | 4094 | 	params = memdup_user(optval, optlen); | 
 | 4095 | 	if (IS_ERR(params)) | 
 | 4096 | 		return PTR_ERR(params); | 
 | 4097 |  | 
| Xin Long | 2342b8d | 2017-12-10 15:40:51 +0800 | [diff] [blame] | 4098 | 	if (params->srs_number_streams * sizeof(__u16) > | 
 | 4099 | 	    optlen - sizeof(*params)) | 
 | 4100 | 		goto out; | 
 | 4101 |  | 
| Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4102 | 	asoc = sctp_id2assoc(sk, params->srs_assoc_id); | 
 | 4103 | 	if (!asoc) | 
 | 4104 | 		goto out; | 
 | 4105 |  | 
 | 4106 | 	retval = sctp_send_reset_streams(asoc, params); | 
 | 4107 |  | 
 | 4108 | out: | 
 | 4109 | 	kfree(params); | 
 | 4110 | 	return retval; | 
 | 4111 | } | 
 | 4112 |  | 
| Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 4113 | static int sctp_setsockopt_reset_assoc(struct sock *sk, | 
 | 4114 | 				       char __user *optval, | 
 | 4115 | 				       unsigned int optlen) | 
 | 4116 | { | 
 | 4117 | 	struct sctp_association *asoc; | 
 | 4118 | 	sctp_assoc_t associd; | 
 | 4119 | 	int retval = -EINVAL; | 
 | 4120 |  | 
 | 4121 | 	if (optlen != sizeof(associd)) | 
 | 4122 | 		goto out; | 
 | 4123 |  | 
 | 4124 | 	if (copy_from_user(&associd, optval, optlen)) { | 
 | 4125 | 		retval = -EFAULT; | 
 | 4126 | 		goto out; | 
 | 4127 | 	} | 
 | 4128 |  | 
 | 4129 | 	asoc = sctp_id2assoc(sk, associd); | 
 | 4130 | 	if (!asoc) | 
 | 4131 | 		goto out; | 
 | 4132 |  | 
 | 4133 | 	retval = sctp_send_reset_assoc(asoc); | 
 | 4134 |  | 
 | 4135 | out: | 
 | 4136 | 	return retval; | 
 | 4137 | } | 
 | 4138 |  | 
| Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 4139 | static int sctp_setsockopt_add_streams(struct sock *sk, | 
 | 4140 | 				       char __user *optval, | 
 | 4141 | 				       unsigned int optlen) | 
 | 4142 | { | 
 | 4143 | 	struct sctp_association *asoc; | 
 | 4144 | 	struct sctp_add_streams params; | 
 | 4145 | 	int retval = -EINVAL; | 
 | 4146 |  | 
 | 4147 | 	if (optlen != sizeof(params)) | 
 | 4148 | 		goto out; | 
 | 4149 |  | 
 | 4150 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4151 | 		retval = -EFAULT; | 
 | 4152 | 		goto out; | 
 | 4153 | 	} | 
 | 4154 |  | 
 | 4155 | 	asoc = sctp_id2assoc(sk, params.sas_assoc_id); | 
 | 4156 | 	if (!asoc) | 
 | 4157 | 		goto out; | 
 | 4158 |  | 
 | 4159 | 	retval = sctp_send_add_streams(asoc, ¶ms); | 
 | 4160 |  | 
 | 4161 | out: | 
 | 4162 | 	return retval; | 
 | 4163 | } | 
 | 4164 |  | 
| Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4165 | static int sctp_setsockopt_scheduler(struct sock *sk, | 
 | 4166 | 				     char __user *optval, | 
 | 4167 | 				     unsigned int optlen) | 
 | 4168 | { | 
 | 4169 | 	struct sctp_association *asoc; | 
 | 4170 | 	struct sctp_assoc_value params; | 
 | 4171 | 	int retval = -EINVAL; | 
 | 4172 |  | 
 | 4173 | 	if (optlen < sizeof(params)) | 
 | 4174 | 		goto out; | 
 | 4175 |  | 
 | 4176 | 	optlen = sizeof(params); | 
 | 4177 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4178 | 		retval = -EFAULT; | 
 | 4179 | 		goto out; | 
 | 4180 | 	} | 
 | 4181 |  | 
 | 4182 | 	if (params.assoc_value > SCTP_SS_MAX) | 
 | 4183 | 		goto out; | 
 | 4184 |  | 
 | 4185 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 4186 | 	if (!asoc) | 
 | 4187 | 		goto out; | 
 | 4188 |  | 
 | 4189 | 	retval = sctp_sched_set_sched(asoc, params.assoc_value); | 
 | 4190 |  | 
 | 4191 | out: | 
 | 4192 | 	return retval; | 
 | 4193 | } | 
 | 4194 |  | 
| Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4195 | static int sctp_setsockopt_scheduler_value(struct sock *sk, | 
 | 4196 | 					   char __user *optval, | 
 | 4197 | 					   unsigned int optlen) | 
 | 4198 | { | 
 | 4199 | 	struct sctp_association *asoc; | 
 | 4200 | 	struct sctp_stream_value params; | 
 | 4201 | 	int retval = -EINVAL; | 
 | 4202 |  | 
 | 4203 | 	if (optlen < sizeof(params)) | 
 | 4204 | 		goto out; | 
 | 4205 |  | 
 | 4206 | 	optlen = sizeof(params); | 
 | 4207 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4208 | 		retval = -EFAULT; | 
 | 4209 | 		goto out; | 
 | 4210 | 	} | 
 | 4211 |  | 
 | 4212 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 4213 | 	if (!asoc) | 
 | 4214 | 		goto out; | 
 | 4215 |  | 
 | 4216 | 	retval = sctp_sched_set_value(asoc, params.stream_id, | 
 | 4217 | 				      params.stream_value, GFP_KERNEL); | 
 | 4218 |  | 
 | 4219 | out: | 
 | 4220 | 	return retval; | 
 | 4221 | } | 
 | 4222 |  | 
| Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4223 | static int sctp_setsockopt_interleaving_supported(struct sock *sk, | 
 | 4224 | 						  char __user *optval, | 
 | 4225 | 						  unsigned int optlen) | 
 | 4226 | { | 
 | 4227 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4228 | 	struct net *net = sock_net(sk); | 
 | 4229 | 	struct sctp_assoc_value params; | 
 | 4230 | 	int retval = -EINVAL; | 
 | 4231 |  | 
 | 4232 | 	if (optlen < sizeof(params)) | 
 | 4233 | 		goto out; | 
 | 4234 |  | 
 | 4235 | 	optlen = sizeof(params); | 
 | 4236 | 	if (copy_from_user(¶ms, optval, optlen)) { | 
 | 4237 | 		retval = -EFAULT; | 
 | 4238 | 		goto out; | 
 | 4239 | 	} | 
 | 4240 |  | 
 | 4241 | 	if (params.assoc_id) | 
 | 4242 | 		goto out; | 
 | 4243 |  | 
 | 4244 | 	if (!net->sctp.intl_enable || !sp->frag_interleave) { | 
 | 4245 | 		retval = -EPERM; | 
 | 4246 | 		goto out; | 
 | 4247 | 	} | 
 | 4248 |  | 
 | 4249 | 	sp->strm_interleave = !!params.assoc_value; | 
 | 4250 |  | 
 | 4251 | 	retval = 0; | 
 | 4252 |  | 
 | 4253 | out: | 
 | 4254 | 	return retval; | 
 | 4255 | } | 
 | 4256 |  | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 4257 | static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval, | 
 | 4258 | 				      unsigned int optlen) | 
 | 4259 | { | 
 | 4260 | 	int val; | 
 | 4261 |  | 
 | 4262 | 	if (!sctp_style(sk, TCP)) | 
 | 4263 | 		return -EOPNOTSUPP; | 
 | 4264 |  | 
 | 4265 | 	if (sctp_sk(sk)->ep->base.bind_addr.port) | 
 | 4266 | 		return -EFAULT; | 
 | 4267 |  | 
 | 4268 | 	if (optlen < sizeof(int)) | 
 | 4269 | 		return -EINVAL; | 
 | 4270 |  | 
 | 4271 | 	if (get_user(val, (int __user *)optval)) | 
 | 4272 | 		return -EFAULT; | 
 | 4273 |  | 
 | 4274 | 	sctp_sk(sk)->reuse = !!val; | 
 | 4275 |  | 
 | 4276 | 	return 0; | 
 | 4277 | } | 
 | 4278 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | /* API 6.2 setsockopt(), getsockopt() | 
 | 4280 |  * | 
 | 4281 |  * Applications use setsockopt() and getsockopt() to set or retrieve | 
 | 4282 |  * socket options.  Socket options are used to change the default | 
 | 4283 |  * behavior of sockets calls.  They are described in Section 7. | 
 | 4284 |  * | 
 | 4285 |  * The syntax is: | 
 | 4286 |  * | 
 | 4287 |  *   ret = getsockopt(int sd, int level, int optname, void __user *optval, | 
 | 4288 |  *                    int __user *optlen); | 
 | 4289 |  *   ret = setsockopt(int sd, int level, int optname, const void __user *optval, | 
 | 4290 |  *                    int optlen); | 
 | 4291 |  * | 
 | 4292 |  *   sd      - the socket descript. | 
 | 4293 |  *   level   - set to IPPROTO_SCTP for all SCTP options. | 
 | 4294 |  *   optname - the option name. | 
 | 4295 |  *   optval  - the buffer to store the value of the option. | 
 | 4296 |  *   optlen  - the size of the buffer. | 
 | 4297 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4298 | static int sctp_setsockopt(struct sock *sk, int level, int optname, | 
 | 4299 | 			   char __user *optval, unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | { | 
 | 4301 | 	int retval = 0; | 
 | 4302 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4303 | 	pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 |  | 
 | 4305 | 	/* I can hardly begin to describe how wrong this is.  This is | 
 | 4306 | 	 * so broken as to be worse than useless.  The API draft | 
 | 4307 | 	 * REALLY is NOT helpful here...  I am not convinced that the | 
 | 4308 | 	 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP | 
 | 4309 | 	 * are at all well-founded. | 
 | 4310 | 	 */ | 
 | 4311 | 	if (level != SOL_SCTP) { | 
 | 4312 | 		struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 4313 | 		retval = af->setsockopt(sk, level, optname, optval, optlen); | 
 | 4314 | 		goto out_nounlock; | 
 | 4315 | 	} | 
 | 4316 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4317 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 |  | 
 | 4319 | 	switch (optname) { | 
 | 4320 | 	case SCTP_SOCKOPT_BINDX_ADD: | 
 | 4321 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 4322 | 		retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 
 | 4323 | 					       optlen, SCTP_BINDX_ADD_ADDR); | 
 | 4324 | 		break; | 
 | 4325 |  | 
 | 4326 | 	case SCTP_SOCKOPT_BINDX_REM: | 
 | 4327 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 4328 | 		retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 
 | 4329 | 					       optlen, SCTP_BINDX_REM_ADDR); | 
 | 4330 | 		break; | 
 | 4331 |  | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4332 | 	case SCTP_SOCKOPT_CONNECTX_OLD: | 
 | 4333 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 4334 | 		retval = sctp_setsockopt_connectx_old(sk, | 
 | 4335 | 					    (struct sockaddr __user *)optval, | 
 | 4336 | 					    optlen); | 
 | 4337 | 		break; | 
 | 4338 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4339 | 	case SCTP_SOCKOPT_CONNECTX: | 
 | 4340 | 		/* 'optlen' is the size of the addresses buffer. */ | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4341 | 		retval = sctp_setsockopt_connectx(sk, | 
 | 4342 | 					    (struct sockaddr __user *)optval, | 
 | 4343 | 					    optlen); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4344 | 		break; | 
 | 4345 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | 	case SCTP_DISABLE_FRAGMENTS: | 
 | 4347 | 		retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); | 
 | 4348 | 		break; | 
 | 4349 |  | 
 | 4350 | 	case SCTP_EVENTS: | 
 | 4351 | 		retval = sctp_setsockopt_events(sk, optval, optlen); | 
 | 4352 | 		break; | 
 | 4353 |  | 
 | 4354 | 	case SCTP_AUTOCLOSE: | 
 | 4355 | 		retval = sctp_setsockopt_autoclose(sk, optval, optlen); | 
 | 4356 | 		break; | 
 | 4357 |  | 
 | 4358 | 	case SCTP_PEER_ADDR_PARAMS: | 
 | 4359 | 		retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); | 
 | 4360 | 		break; | 
 | 4361 |  | 
| Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 4362 | 	case SCTP_DELAYED_SACK: | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4363 | 		retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4364 | 		break; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 4365 | 	case SCTP_PARTIAL_DELIVERY_POINT: | 
 | 4366 | 		retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); | 
 | 4367 | 		break; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4368 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | 	case SCTP_INITMSG: | 
 | 4370 | 		retval = sctp_setsockopt_initmsg(sk, optval, optlen); | 
 | 4371 | 		break; | 
 | 4372 | 	case SCTP_DEFAULT_SEND_PARAM: | 
 | 4373 | 		retval = sctp_setsockopt_default_send_param(sk, optval, | 
 | 4374 | 							    optlen); | 
 | 4375 | 		break; | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 4376 | 	case SCTP_DEFAULT_SNDINFO: | 
 | 4377 | 		retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen); | 
 | 4378 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | 	case SCTP_PRIMARY_ADDR: | 
 | 4380 | 		retval = sctp_setsockopt_primary_addr(sk, optval, optlen); | 
 | 4381 | 		break; | 
 | 4382 | 	case SCTP_SET_PEER_PRIMARY_ADDR: | 
 | 4383 | 		retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); | 
 | 4384 | 		break; | 
 | 4385 | 	case SCTP_NODELAY: | 
 | 4386 | 		retval = sctp_setsockopt_nodelay(sk, optval, optlen); | 
 | 4387 | 		break; | 
 | 4388 | 	case SCTP_RTOINFO: | 
 | 4389 | 		retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); | 
 | 4390 | 		break; | 
 | 4391 | 	case SCTP_ASSOCINFO: | 
 | 4392 | 		retval = sctp_setsockopt_associnfo(sk, optval, optlen); | 
 | 4393 | 		break; | 
 | 4394 | 	case SCTP_I_WANT_MAPPED_V4_ADDR: | 
 | 4395 | 		retval = sctp_setsockopt_mappedv4(sk, optval, optlen); | 
 | 4396 | 		break; | 
 | 4397 | 	case SCTP_MAXSEG: | 
 | 4398 | 		retval = sctp_setsockopt_maxseg(sk, optval, optlen); | 
 | 4399 | 		break; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4400 | 	case SCTP_ADAPTATION_LAYER: | 
 | 4401 | 		retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | 		break; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4403 | 	case SCTP_CONTEXT: | 
 | 4404 | 		retval = sctp_setsockopt_context(sk, optval, optlen); | 
 | 4405 | 		break; | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4406 | 	case SCTP_FRAGMENT_INTERLEAVE: | 
 | 4407 | 		retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); | 
 | 4408 | 		break; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 4409 | 	case SCTP_MAX_BURST: | 
 | 4410 | 		retval = sctp_setsockopt_maxburst(sk, optval, optlen); | 
 | 4411 | 		break; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 4412 | 	case SCTP_AUTH_CHUNK: | 
 | 4413 | 		retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); | 
 | 4414 | 		break; | 
 | 4415 | 	case SCTP_HMAC_IDENT: | 
 | 4416 | 		retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); | 
 | 4417 | 		break; | 
 | 4418 | 	case SCTP_AUTH_KEY: | 
 | 4419 | 		retval = sctp_setsockopt_auth_key(sk, optval, optlen); | 
 | 4420 | 		break; | 
 | 4421 | 	case SCTP_AUTH_ACTIVE_KEY: | 
 | 4422 | 		retval = sctp_setsockopt_active_key(sk, optval, optlen); | 
 | 4423 | 		break; | 
 | 4424 | 	case SCTP_AUTH_DELETE_KEY: | 
 | 4425 | 		retval = sctp_setsockopt_del_key(sk, optval, optlen); | 
 | 4426 | 		break; | 
| Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 4427 | 	case SCTP_AUTH_DEACTIVATE_KEY: | 
 | 4428 | 		retval = sctp_setsockopt_deactivate_key(sk, optval, optlen); | 
 | 4429 | 		break; | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 4430 | 	case SCTP_AUTO_ASCONF: | 
 | 4431 | 		retval = sctp_setsockopt_auto_asconf(sk, optval, optlen); | 
 | 4432 | 		break; | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 4433 | 	case SCTP_PEER_ADDR_THLDS: | 
 | 4434 | 		retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen); | 
 | 4435 | 		break; | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4436 | 	case SCTP_RECVRCVINFO: | 
 | 4437 | 		retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen); | 
 | 4438 | 		break; | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4439 | 	case SCTP_RECVNXTINFO: | 
 | 4440 | 		retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen); | 
 | 4441 | 		break; | 
| Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4442 | 	case SCTP_PR_SUPPORTED: | 
 | 4443 | 		retval = sctp_setsockopt_pr_supported(sk, optval, optlen); | 
 | 4444 | 		break; | 
| Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4445 | 	case SCTP_DEFAULT_PRINFO: | 
 | 4446 | 		retval = sctp_setsockopt_default_prinfo(sk, optval, optlen); | 
 | 4447 | 		break; | 
| Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4448 | 	case SCTP_RECONFIG_SUPPORTED: | 
 | 4449 | 		retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen); | 
 | 4450 | 		break; | 
| Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4451 | 	case SCTP_ENABLE_STREAM_RESET: | 
 | 4452 | 		retval = sctp_setsockopt_enable_strreset(sk, optval, optlen); | 
 | 4453 | 		break; | 
| Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4454 | 	case SCTP_RESET_STREAMS: | 
 | 4455 | 		retval = sctp_setsockopt_reset_streams(sk, optval, optlen); | 
 | 4456 | 		break; | 
| Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 4457 | 	case SCTP_RESET_ASSOC: | 
 | 4458 | 		retval = sctp_setsockopt_reset_assoc(sk, optval, optlen); | 
 | 4459 | 		break; | 
| Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 4460 | 	case SCTP_ADD_STREAMS: | 
 | 4461 | 		retval = sctp_setsockopt_add_streams(sk, optval, optlen); | 
 | 4462 | 		break; | 
| Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4463 | 	case SCTP_STREAM_SCHEDULER: | 
 | 4464 | 		retval = sctp_setsockopt_scheduler(sk, optval, optlen); | 
 | 4465 | 		break; | 
| Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4466 | 	case SCTP_STREAM_SCHEDULER_VALUE: | 
 | 4467 | 		retval = sctp_setsockopt_scheduler_value(sk, optval, optlen); | 
 | 4468 | 		break; | 
| Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4469 | 	case SCTP_INTERLEAVING_SUPPORTED: | 
 | 4470 | 		retval = sctp_setsockopt_interleaving_supported(sk, optval, | 
 | 4471 | 								optlen); | 
 | 4472 | 		break; | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 4473 | 	case SCTP_REUSE_PORT: | 
 | 4474 | 		retval = sctp_setsockopt_reuse_port(sk, optval, optlen); | 
 | 4475 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | 	default: | 
 | 4477 | 		retval = -ENOPROTOOPT; | 
 | 4478 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4479 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4481 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 |  | 
 | 4483 | out_nounlock: | 
 | 4484 | 	return retval; | 
 | 4485 | } | 
 | 4486 |  | 
 | 4487 | /* API 3.1.6 connect() - UDP Style Syntax | 
 | 4488 |  * | 
 | 4489 |  * An application may use the connect() call in the UDP model to initiate an | 
 | 4490 |  * association without sending data. | 
 | 4491 |  * | 
 | 4492 |  * The syntax is: | 
 | 4493 |  * | 
 | 4494 |  * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); | 
 | 4495 |  * | 
 | 4496 |  * sd: the socket descriptor to have a new association added to. | 
 | 4497 |  * | 
 | 4498 |  * nam: the address structure (either struct sockaddr_in or struct | 
 | 4499 |  *    sockaddr_in6 defined in RFC2553 [7]). | 
 | 4500 |  * | 
 | 4501 |  * len: the size of the address. | 
 | 4502 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4503 | static int sctp_connect(struct sock *sk, struct sockaddr *addr, | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4504 | 			int addr_len, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | { | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4506 | 	struct inet_sock *inet = inet_sk(sk); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4507 | 	struct sctp_af *af; | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4508 | 	int err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4510 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4512 | 	pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk, | 
 | 4513 | 		 addr, addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 |  | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4515 | 	/* We may need to bind the socket. */ | 
 | 4516 | 	if (!inet->inet_num) { | 
 | 4517 | 		if (sk->sk_prot->get_port(sk, 0)) { | 
 | 4518 | 			release_sock(sk); | 
 | 4519 | 			return -EAGAIN; | 
 | 4520 | 		} | 
 | 4521 | 		inet->inet_sport = htons(inet->inet_num); | 
 | 4522 | 	} | 
 | 4523 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4524 | 	/* Validate addr_len before calling common connect/connectx routine. */ | 
 | 4525 | 	af = sctp_get_af_specific(addr->sa_family); | 
 | 4526 | 	if (!af || addr_len < af->sockaddr_len) { | 
 | 4527 | 		err = -EINVAL; | 
 | 4528 | 	} else { | 
 | 4529 | 		/* Pass correct addr len to common routine (so it knows there | 
 | 4530 | 		 * is only one address being passed. | 
 | 4531 | 		 */ | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4532 | 		err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | 	} | 
 | 4534 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4535 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | 	return err; | 
 | 4537 | } | 
 | 4538 |  | 
| Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4539 | int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr, | 
 | 4540 | 		      int addr_len, int flags) | 
 | 4541 | { | 
 | 4542 | 	if (addr_len < sizeof(uaddr->sa_family)) | 
 | 4543 | 		return -EINVAL; | 
 | 4544 |  | 
 | 4545 | 	if (uaddr->sa_family == AF_UNSPEC) | 
 | 4546 | 		return -EOPNOTSUPP; | 
 | 4547 |  | 
 | 4548 | 	return sctp_connect(sock->sk, uaddr, addr_len, flags); | 
 | 4549 | } | 
 | 4550 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | /* FIXME: Write comments. */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4552 | static int sctp_disconnect(struct sock *sk, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | { | 
 | 4554 | 	return -EOPNOTSUPP; /* STUB */ | 
 | 4555 | } | 
 | 4556 |  | 
 | 4557 | /* 4.1.4 accept() - TCP Style Syntax | 
 | 4558 |  * | 
 | 4559 |  * Applications use accept() call to remove an established SCTP | 
 | 4560 |  * association from the accept queue of the endpoint.  A new socket | 
 | 4561 |  * descriptor will be returned from accept() to represent the newly | 
 | 4562 |  * formed association. | 
 | 4563 |  */ | 
| David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4564 | 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] | 4565 | { | 
 | 4566 | 	struct sctp_sock *sp; | 
 | 4567 | 	struct sctp_endpoint *ep; | 
 | 4568 | 	struct sock *newsk = NULL; | 
 | 4569 | 	struct sctp_association *asoc; | 
 | 4570 | 	long timeo; | 
 | 4571 | 	int error = 0; | 
 | 4572 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4573 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 |  | 
 | 4575 | 	sp = sctp_sk(sk); | 
 | 4576 | 	ep = sp->ep; | 
 | 4577 |  | 
 | 4578 | 	if (!sctp_style(sk, TCP)) { | 
 | 4579 | 		error = -EOPNOTSUPP; | 
 | 4580 | 		goto out; | 
 | 4581 | 	} | 
 | 4582 |  | 
 | 4583 | 	if (!sctp_sstate(sk, LISTENING)) { | 
 | 4584 | 		error = -EINVAL; | 
 | 4585 | 		goto out; | 
 | 4586 | 	} | 
 | 4587 |  | 
| Sridhar Samudrala | 8abfedd | 2006-08-22 00:24:09 -0700 | [diff] [blame] | 4588 | 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 |  | 
 | 4590 | 	error = sctp_wait_for_accept(sk, timeo); | 
 | 4591 | 	if (error) | 
 | 4592 | 		goto out; | 
 | 4593 |  | 
 | 4594 | 	/* We treat the list of associations on the endpoint as the accept | 
 | 4595 | 	 * queue and pick the first association on the list. | 
 | 4596 | 	 */ | 
 | 4597 | 	asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); | 
 | 4598 |  | 
| David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4599 | 	newsk = sp->pf->create_accept_sk(sk, asoc, kern); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | 	if (!newsk) { | 
 | 4601 | 		error = -ENOMEM; | 
 | 4602 | 		goto out; | 
 | 4603 | 	} | 
 | 4604 |  | 
 | 4605 | 	/* Populate the fields of the newsk from the oldsk and migrate the | 
 | 4606 | 	 * asoc to the newsk. | 
 | 4607 | 	 */ | 
 | 4608 | 	sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); | 
 | 4609 |  | 
 | 4610 | out: | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4611 | 	release_sock(sk); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4612 | 	*err = error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | 	return newsk; | 
 | 4614 | } | 
 | 4615 |  | 
 | 4616 | /* The SCTP ioctl handler. */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4617 | static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | { | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4619 | 	int rc = -ENOTCONN; | 
 | 4620 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4621 | 	lock_sock(sk); | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4622 |  | 
 | 4623 | 	/* | 
 | 4624 | 	 * SEQPACKET-style sockets in LISTENING state are valid, for | 
 | 4625 | 	 * SCTP, so only discard TCP-style sockets in LISTENING state. | 
 | 4626 | 	 */ | 
 | 4627 | 	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | 
 | 4628 | 		goto out; | 
 | 4629 |  | 
 | 4630 | 	switch (cmd) { | 
 | 4631 | 	case SIOCINQ: { | 
 | 4632 | 		struct sk_buff *skb; | 
 | 4633 | 		unsigned int amount = 0; | 
 | 4634 |  | 
 | 4635 | 		skb = skb_peek(&sk->sk_receive_queue); | 
 | 4636 | 		if (skb != NULL) { | 
 | 4637 | 			/* | 
 | 4638 | 			 * We will only return the amount of this packet since | 
 | 4639 | 			 * that is all that will be read. | 
 | 4640 | 			 */ | 
 | 4641 | 			amount = skb->len; | 
 | 4642 | 		} | 
 | 4643 | 		rc = put_user(amount, (int __user *)arg); | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4644 | 		break; | 
| David S. Miller | 9a7241c | 2010-10-03 22:14:37 -0700 | [diff] [blame] | 4645 | 	} | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4646 | 	default: | 
 | 4647 | 		rc = -ENOIOCTLCMD; | 
 | 4648 | 		break; | 
 | 4649 | 	} | 
 | 4650 | out: | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4651 | 	release_sock(sk); | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4652 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | } | 
 | 4654 |  | 
 | 4655 | /* This is the function which gets called during socket creation to | 
 | 4656 |  * initialized the SCTP-specific portion of the sock. | 
 | 4657 |  * The sock structure should already be zero-filled memory. | 
 | 4658 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4659 | static int sctp_init_sock(struct sock *sk) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4661 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | 	struct sctp_sock *sp; | 
 | 4663 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4664 | 	pr_debug("%s: sk:%p\n", __func__, sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 |  | 
 | 4666 | 	sp = sctp_sk(sk); | 
 | 4667 |  | 
 | 4668 | 	/* Initialize the SCTP per socket area.  */ | 
 | 4669 | 	switch (sk->sk_type) { | 
 | 4670 | 	case SOCK_SEQPACKET: | 
 | 4671 | 		sp->type = SCTP_SOCKET_UDP; | 
 | 4672 | 		break; | 
 | 4673 | 	case SOCK_STREAM: | 
 | 4674 | 		sp->type = SCTP_SOCKET_TCP; | 
 | 4675 | 		break; | 
 | 4676 | 	default: | 
 | 4677 | 		return -ESOCKTNOSUPPORT; | 
 | 4678 | 	} | 
 | 4679 |  | 
| Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 4680 | 	sk->sk_gso_type = SKB_GSO_SCTP; | 
 | 4681 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | 	/* Initialize default send parameters. These parameters can be | 
 | 4683 | 	 * modified with the SCTP_DEFAULT_SEND_PARAM socket option. | 
 | 4684 | 	 */ | 
 | 4685 | 	sp->default_stream = 0; | 
 | 4686 | 	sp->default_ppid = 0; | 
 | 4687 | 	sp->default_flags = 0; | 
 | 4688 | 	sp->default_context = 0; | 
 | 4689 | 	sp->default_timetolive = 0; | 
 | 4690 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4691 | 	sp->default_rcv_context = 0; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4692 | 	sp->max_burst = net->sctp.max_burst; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4693 |  | 
| Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 4694 | 	sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg; | 
 | 4695 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | 	/* Initialize default setup parameters. These parameters | 
 | 4697 | 	 * can be modified with the SCTP_INITMSG socket option or | 
 | 4698 | 	 * overridden by the SCTP_INIT CMSG. | 
 | 4699 | 	 */ | 
 | 4700 | 	sp->initmsg.sinit_num_ostreams   = sctp_max_outstreams; | 
 | 4701 | 	sp->initmsg.sinit_max_instreams  = sctp_max_instreams; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4702 | 	sp->initmsg.sinit_max_attempts   = net->sctp.max_retrans_init; | 
 | 4703 | 	sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 |  | 
 | 4705 | 	/* Initialize default RTO related parameters.  These parameters can | 
 | 4706 | 	 * be modified for with the SCTP_RTOINFO socket option. | 
 | 4707 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4708 | 	sp->rtoinfo.srto_initial = net->sctp.rto_initial; | 
 | 4709 | 	sp->rtoinfo.srto_max     = net->sctp.rto_max; | 
 | 4710 | 	sp->rtoinfo.srto_min     = net->sctp.rto_min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 |  | 
 | 4712 | 	/* Initialize default association related parameters. These parameters | 
 | 4713 | 	 * can be modified with the SCTP_ASSOCINFO socket option. | 
 | 4714 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4715 | 	sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | 	sp->assocparams.sasoc_number_peer_destinations = 0; | 
 | 4717 | 	sp->assocparams.sasoc_peer_rwnd = 0; | 
 | 4718 | 	sp->assocparams.sasoc_local_rwnd = 0; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4719 | 	sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 |  | 
 | 4721 | 	/* Initialize default event subscriptions. By default, all the | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4722 | 	 * options are off. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | 	 */ | 
 | 4724 | 	memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); | 
 | 4725 |  | 
 | 4726 | 	/* Default Peer Address Parameters.  These defaults can | 
 | 4727 | 	 * be modified via SCTP_PEER_ADDR_PARAMS | 
 | 4728 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4729 | 	sp->hbinterval  = net->sctp.hb_interval; | 
 | 4730 | 	sp->pathmaxrxt  = net->sctp.max_retrans_path; | 
| wangweidong | 4e2d52b | 2013-12-23 12:16:54 +0800 | [diff] [blame] | 4731 | 	sp->pathmtu     = 0; /* allow default discovery */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4732 | 	sp->sackdelay   = net->sctp.sack_timeout; | 
| Vlad Yasevich | 7bfe8bdb | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 4733 | 	sp->sackfreq	= 2; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4734 | 	sp->param_flags = SPP_HB_ENABLE | | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4735 | 			  SPP_PMTUD_ENABLE | | 
 | 4736 | 			  SPP_SACKDELAY_ENABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 |  | 
 | 4738 | 	/* If enabled no SCTP message fragmentation will be performed. | 
 | 4739 | 	 * Configure through SCTP_DISABLE_FRAGMENTS socket option. | 
 | 4740 | 	 */ | 
 | 4741 | 	sp->disable_fragments = 0; | 
 | 4742 |  | 
| Sridhar Samudrala | 208edef | 2006-09-29 17:08:01 -0700 | [diff] [blame] | 4743 | 	/* Enable Nagle algorithm by default.  */ | 
 | 4744 | 	sp->nodelay           = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 |  | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4746 | 	sp->recvrcvinfo = 0; | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4747 | 	sp->recvnxtinfo = 0; | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4748 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | 	/* Enable by default. */ | 
 | 4750 | 	sp->v4mapped          = 1; | 
 | 4751 |  | 
 | 4752 | 	/* Auto-close idle associations after the configured | 
 | 4753 | 	 * number of seconds.  A value of 0 disables this | 
 | 4754 | 	 * feature.  Configure through the SCTP_AUTOCLOSE socket option, | 
 | 4755 | 	 * for UDP-style sockets only. | 
 | 4756 | 	 */ | 
 | 4757 | 	sp->autoclose         = 0; | 
 | 4758 |  | 
 | 4759 | 	/* User specified fragmentation limit. */ | 
 | 4760 | 	sp->user_frag         = 0; | 
 | 4761 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4762 | 	sp->adaptation_ind = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 |  | 
 | 4764 | 	sp->pf = sctp_get_pf_specific(sk->sk_family); | 
 | 4765 |  | 
 | 4766 | 	/* Control variables for partial data delivery. */ | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4767 | 	atomic_set(&sp->pd_mode, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | 	skb_queue_head_init(&sp->pd_lobby); | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4769 | 	sp->frag_interleave = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 |  | 
 | 4771 | 	/* Create a per socket endpoint structure.  Even if we | 
 | 4772 | 	 * change the data structure relationships, this may still | 
 | 4773 | 	 * be useful for storing pre-connect address information. | 
 | 4774 | 	 */ | 
| Daniel Borkmann | c164b83 | 2013-06-14 18:24:06 +0200 | [diff] [blame] | 4775 | 	sp->ep = sctp_endpoint_new(sk, GFP_KERNEL); | 
 | 4776 | 	if (!sp->ep) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | 		return -ENOMEM; | 
 | 4778 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | 	sp->hmac = NULL; | 
 | 4780 |  | 
| Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4781 | 	sk->sk_destruct = sctp_destruct_sock; | 
 | 4782 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | 	SCTP_DBG_OBJCNT_INC(sock); | 
| David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 4784 |  | 
 | 4785 | 	local_bh_disable(); | 
| Tonghao Zhang | 8cb38a6 | 2017-12-22 10:15:20 -0800 | [diff] [blame] | 4786 | 	sk_sockets_allocated_inc(sk); | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4787 | 	sock_prot_inuse_add(net, sk->sk_prot, 1); | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4788 |  | 
 | 4789 | 	/* Nothing can fail after this block, otherwise | 
 | 4790 | 	 * sctp_destroy_sock() will be called without addr_wq_lock held | 
 | 4791 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4792 | 	if (net->sctp.default_auto_asconf) { | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4793 | 		spin_lock(&sock_net(sk)->sctp.addr_wq_lock); | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4794 | 		list_add_tail(&sp->auto_asconf_list, | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4795 | 		    &net->sctp.auto_asconf_splist); | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4796 | 		sp->do_auto_asconf = 1; | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4797 | 		spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); | 
 | 4798 | 	} else { | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4799 | 		sp->do_auto_asconf = 0; | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4800 | 	} | 
 | 4801 |  | 
| David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 4802 | 	local_bh_enable(); | 
 | 4803 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | 	return 0; | 
 | 4805 | } | 
 | 4806 |  | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4807 | /* Cleanup any SCTP per socket resources. Must be called with | 
 | 4808 |  * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true | 
 | 4809 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4810 | static void sctp_destroy_sock(struct sock *sk) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | { | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4812 | 	struct sctp_sock *sp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4814 | 	pr_debug("%s: sk:%p\n", __func__, sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 |  | 
 | 4816 | 	/* Release our hold on the endpoint. */ | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4817 | 	sp = sctp_sk(sk); | 
| Daniel Borkmann | 1abd165 | 2013-06-06 15:53:47 +0200 | [diff] [blame] | 4818 | 	/* This could happen during socket init, thus we bail out | 
 | 4819 | 	 * early, since the rest of the below is not setup either. | 
 | 4820 | 	 */ | 
 | 4821 | 	if (sp->ep == NULL) | 
 | 4822 | 		return; | 
 | 4823 |  | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4824 | 	if (sp->do_auto_asconf) { | 
 | 4825 | 		sp->do_auto_asconf = 0; | 
 | 4826 | 		list_del(&sp->auto_asconf_list); | 
 | 4827 | 	} | 
 | 4828 | 	sctp_endpoint_free(sp->ep); | 
| Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4829 | 	local_bh_disable(); | 
| Tonghao Zhang | 8cb38a6 | 2017-12-22 10:15:20 -0800 | [diff] [blame] | 4830 | 	sk_sockets_allocated_dec(sk); | 
| Eric Dumazet | 9a57f7f | 2008-11-17 02:41:00 -0800 | [diff] [blame] | 4831 | 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 
| Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4832 | 	local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | } | 
 | 4834 |  | 
| Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4835 | /* Triggered when there are no references on the socket anymore */ | 
 | 4836 | static void sctp_destruct_sock(struct sock *sk) | 
 | 4837 | { | 
 | 4838 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4839 |  | 
 | 4840 | 	/* Free up the HMAC transform. */ | 
| Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 4841 | 	crypto_free_shash(sp->hmac); | 
| Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4842 |  | 
 | 4843 | 	inet_sock_destruct(sk); | 
 | 4844 | } | 
 | 4845 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | /* API 4.1.7 shutdown() - TCP Style Syntax | 
 | 4847 |  *     int shutdown(int socket, int how); | 
 | 4848 |  * | 
 | 4849 |  *     sd      - the socket descriptor of the association to be closed. | 
 | 4850 |  *     how     - Specifies the type of shutdown.  The  values  are | 
 | 4851 |  *               as follows: | 
 | 4852 |  *               SHUT_RD | 
 | 4853 |  *                     Disables further receive operations. No SCTP | 
 | 4854 |  *                     protocol action is taken. | 
 | 4855 |  *               SHUT_WR | 
 | 4856 |  *                     Disables further send operations, and initiates | 
 | 4857 |  *                     the SCTP shutdown sequence. | 
 | 4858 |  *               SHUT_RDWR | 
 | 4859 |  *                     Disables further send  and  receive  operations | 
 | 4860 |  *                     and initiates the SCTP shutdown sequence. | 
 | 4861 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4862 | static void sctp_shutdown(struct sock *sk, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 4864 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | 	struct sctp_endpoint *ep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 |  | 
 | 4867 | 	if (!sctp_style(sk, TCP)) | 
 | 4868 | 		return; | 
 | 4869 |  | 
| Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 4870 | 	ep = sctp_sk(sk)->ep; | 
 | 4871 | 	if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) { | 
 | 4872 | 		struct sctp_association *asoc; | 
 | 4873 |  | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 4874 | 		inet_sk_set_state(sk, SCTP_SS_CLOSING); | 
| Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 4875 | 		asoc = list_entry(ep->asocs.next, | 
 | 4876 | 				  struct sctp_association, asocs); | 
 | 4877 | 		sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | 	} | 
 | 4879 | } | 
 | 4880 |  | 
| Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4881 | int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc, | 
 | 4882 | 		       struct sctp_info *info) | 
 | 4883 | { | 
 | 4884 | 	struct sctp_transport *prim; | 
 | 4885 | 	struct list_head *pos; | 
 | 4886 | 	int mask; | 
 | 4887 |  | 
 | 4888 | 	memset(info, 0, sizeof(*info)); | 
 | 4889 | 	if (!asoc) { | 
 | 4890 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 4891 |  | 
 | 4892 | 		info->sctpi_s_autoclose = sp->autoclose; | 
 | 4893 | 		info->sctpi_s_adaptation_ind = sp->adaptation_ind; | 
 | 4894 | 		info->sctpi_s_pd_point = sp->pd_point; | 
 | 4895 | 		info->sctpi_s_nodelay = sp->nodelay; | 
 | 4896 | 		info->sctpi_s_disable_fragments = sp->disable_fragments; | 
 | 4897 | 		info->sctpi_s_v4mapped = sp->v4mapped; | 
 | 4898 | 		info->sctpi_s_frag_interleave = sp->frag_interleave; | 
| Xin Long | 40eb90e9 | 2016-05-29 17:42:13 +0800 | [diff] [blame] | 4899 | 		info->sctpi_s_type = sp->type; | 
| Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4900 |  | 
 | 4901 | 		return 0; | 
 | 4902 | 	} | 
 | 4903 |  | 
 | 4904 | 	info->sctpi_tag = asoc->c.my_vtag; | 
 | 4905 | 	info->sctpi_state = asoc->state; | 
 | 4906 | 	info->sctpi_rwnd = asoc->a_rwnd; | 
 | 4907 | 	info->sctpi_unackdata = asoc->unack_data; | 
 | 4908 | 	info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); | 
| Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 4909 | 	info->sctpi_instrms = asoc->stream.incnt; | 
 | 4910 | 	info->sctpi_outstrms = asoc->stream.outcnt; | 
| Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4911 | 	list_for_each(pos, &asoc->base.inqueue.in_chunk_list) | 
 | 4912 | 		info->sctpi_inqueue++; | 
 | 4913 | 	list_for_each(pos, &asoc->outqueue.out_chunk_list) | 
 | 4914 | 		info->sctpi_outqueue++; | 
 | 4915 | 	info->sctpi_overall_error = asoc->overall_error_count; | 
 | 4916 | 	info->sctpi_max_burst = asoc->max_burst; | 
 | 4917 | 	info->sctpi_maxseg = asoc->frag_point; | 
 | 4918 | 	info->sctpi_peer_rwnd = asoc->peer.rwnd; | 
 | 4919 | 	info->sctpi_peer_tag = asoc->c.peer_vtag; | 
 | 4920 |  | 
 | 4921 | 	mask = asoc->peer.ecn_capable << 1; | 
 | 4922 | 	mask = (mask | asoc->peer.ipv4_address) << 1; | 
 | 4923 | 	mask = (mask | asoc->peer.ipv6_address) << 1; | 
 | 4924 | 	mask = (mask | asoc->peer.hostname_address) << 1; | 
 | 4925 | 	mask = (mask | asoc->peer.asconf_capable) << 1; | 
 | 4926 | 	mask = (mask | asoc->peer.prsctp_capable) << 1; | 
 | 4927 | 	mask = (mask | asoc->peer.auth_capable); | 
 | 4928 | 	info->sctpi_peer_capable = mask; | 
 | 4929 | 	mask = asoc->peer.sack_needed << 1; | 
 | 4930 | 	mask = (mask | asoc->peer.sack_generation) << 1; | 
 | 4931 | 	mask = (mask | asoc->peer.zero_window_announced); | 
 | 4932 | 	info->sctpi_peer_sack = mask; | 
 | 4933 |  | 
 | 4934 | 	info->sctpi_isacks = asoc->stats.isacks; | 
 | 4935 | 	info->sctpi_osacks = asoc->stats.osacks; | 
 | 4936 | 	info->sctpi_opackets = asoc->stats.opackets; | 
 | 4937 | 	info->sctpi_ipackets = asoc->stats.ipackets; | 
 | 4938 | 	info->sctpi_rtxchunks = asoc->stats.rtxchunks; | 
 | 4939 | 	info->sctpi_outofseqtsns = asoc->stats.outofseqtsns; | 
 | 4940 | 	info->sctpi_idupchunks = asoc->stats.idupchunks; | 
 | 4941 | 	info->sctpi_gapcnt = asoc->stats.gapcnt; | 
 | 4942 | 	info->sctpi_ouodchunks = asoc->stats.ouodchunks; | 
 | 4943 | 	info->sctpi_iuodchunks = asoc->stats.iuodchunks; | 
 | 4944 | 	info->sctpi_oodchunks = asoc->stats.oodchunks; | 
 | 4945 | 	info->sctpi_iodchunks = asoc->stats.iodchunks; | 
 | 4946 | 	info->sctpi_octrlchunks = asoc->stats.octrlchunks; | 
 | 4947 | 	info->sctpi_ictrlchunks = asoc->stats.ictrlchunks; | 
 | 4948 |  | 
 | 4949 | 	prim = asoc->peer.primary_path; | 
| Stefano Brivio | ee6c88b | 2017-08-23 13:27:13 +0200 | [diff] [blame] | 4950 | 	memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr)); | 
| Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4951 | 	info->sctpi_p_state = prim->state; | 
 | 4952 | 	info->sctpi_p_cwnd = prim->cwnd; | 
 | 4953 | 	info->sctpi_p_srtt = prim->srtt; | 
 | 4954 | 	info->sctpi_p_rto = jiffies_to_msecs(prim->rto); | 
 | 4955 | 	info->sctpi_p_hbinterval = prim->hbinterval; | 
 | 4956 | 	info->sctpi_p_pathmaxrxt = prim->pathmaxrxt; | 
 | 4957 | 	info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay); | 
 | 4958 | 	info->sctpi_p_ssthresh = prim->ssthresh; | 
 | 4959 | 	info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked; | 
 | 4960 | 	info->sctpi_p_flight_size = prim->flight_size; | 
 | 4961 | 	info->sctpi_p_error = prim->error_count; | 
 | 4962 |  | 
 | 4963 | 	return 0; | 
 | 4964 | } | 
 | 4965 | EXPORT_SYMBOL_GPL(sctp_get_sctp_info); | 
 | 4966 |  | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4967 | /* use callback to avoid exporting the core structure */ | 
| Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4968 | void sctp_transport_walk_start(struct rhashtable_iter *iter) | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4969 | { | 
| Xin Long | 7fda702f | 2016-11-15 23:23:11 +0800 | [diff] [blame] | 4970 | 	rhltable_walk_enter(&sctp_transport_hashtable, iter); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4971 |  | 
| Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4972 | 	rhashtable_walk_start(iter); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4973 | } | 
 | 4974 |  | 
 | 4975 | void sctp_transport_walk_stop(struct rhashtable_iter *iter) | 
 | 4976 | { | 
 | 4977 | 	rhashtable_walk_stop(iter); | 
 | 4978 | 	rhashtable_walk_exit(iter); | 
 | 4979 | } | 
 | 4980 |  | 
 | 4981 | struct sctp_transport *sctp_transport_get_next(struct net *net, | 
 | 4982 | 					       struct rhashtable_iter *iter) | 
 | 4983 | { | 
 | 4984 | 	struct sctp_transport *t; | 
 | 4985 |  | 
 | 4986 | 	t = rhashtable_walk_next(iter); | 
 | 4987 | 	for (; t; t = rhashtable_walk_next(iter)) { | 
 | 4988 | 		if (IS_ERR(t)) { | 
 | 4989 | 			if (PTR_ERR(t) == -EAGAIN) | 
 | 4990 | 				continue; | 
 | 4991 | 			break; | 
 | 4992 | 		} | 
 | 4993 |  | 
| Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 4994 | 		if (!sctp_transport_hold(t)) | 
 | 4995 | 			continue; | 
 | 4996 |  | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4997 | 		if (net_eq(sock_net(t->asoc->base.sk), net) && | 
 | 4998 | 		    t->asoc->peer.primary_path == t) | 
 | 4999 | 			break; | 
| Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5000 |  | 
 | 5001 | 		sctp_transport_put(t); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5002 | 	} | 
 | 5003 |  | 
 | 5004 | 	return t; | 
 | 5005 | } | 
 | 5006 |  | 
 | 5007 | struct sctp_transport *sctp_transport_get_idx(struct net *net, | 
 | 5008 | 					      struct rhashtable_iter *iter, | 
 | 5009 | 					      int pos) | 
 | 5010 | { | 
| Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5011 | 	struct sctp_transport *t; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5012 |  | 
| Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5013 | 	if (!pos) | 
 | 5014 | 		return SEQ_START_TOKEN; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5015 |  | 
| Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5016 | 	while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) { | 
 | 5017 | 		if (!--pos) | 
 | 5018 | 			break; | 
 | 5019 | 		sctp_transport_put(t); | 
 | 5020 | 	} | 
 | 5021 |  | 
 | 5022 | 	return t; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5023 | } | 
 | 5024 |  | 
 | 5025 | int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), | 
 | 5026 | 			   void *p) { | 
 | 5027 | 	int err = 0; | 
 | 5028 | 	int hash = 0; | 
 | 5029 | 	struct sctp_ep_common *epb; | 
 | 5030 | 	struct sctp_hashbucket *head; | 
 | 5031 |  | 
 | 5032 | 	for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; | 
 | 5033 | 	     hash++, head++) { | 
| Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 5034 | 		read_lock_bh(&head->lock); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5035 | 		sctp_for_each_hentry(epb, &head->chain) { | 
 | 5036 | 			err = cb(sctp_ep(epb), p); | 
 | 5037 | 			if (err) | 
 | 5038 | 				break; | 
 | 5039 | 		} | 
| Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 5040 | 		read_unlock_bh(&head->lock); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5041 | 	} | 
 | 5042 |  | 
 | 5043 | 	return err; | 
 | 5044 | } | 
 | 5045 | EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); | 
 | 5046 |  | 
 | 5047 | int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *), | 
 | 5048 | 				  struct net *net, | 
 | 5049 | 				  const union sctp_addr *laddr, | 
 | 5050 | 				  const union sctp_addr *paddr, void *p) | 
 | 5051 | { | 
 | 5052 | 	struct sctp_transport *transport; | 
| Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 5053 | 	int err; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5054 |  | 
 | 5055 | 	rcu_read_lock(); | 
 | 5056 | 	transport = sctp_addrs_lookup_transport(net, laddr, paddr); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5057 | 	rcu_read_unlock(); | 
| Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 5058 | 	if (!transport) | 
 | 5059 | 		return -ENOENT; | 
 | 5060 |  | 
| Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 5061 | 	err = cb(transport, p); | 
| Xin Long | cd26da4 | 2016-10-31 20:32:31 +0800 | [diff] [blame] | 5062 | 	sctp_transport_put(transport); | 
| Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 5063 |  | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5064 | 	return err; | 
 | 5065 | } | 
 | 5066 | EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); | 
 | 5067 |  | 
 | 5068 | int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *), | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5069 | 			    int (*cb_done)(struct sctp_transport *, void *), | 
 | 5070 | 			    struct net *net, int *pos, void *p) { | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5071 | 	struct rhashtable_iter hti; | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5072 | 	struct sctp_transport *tsp; | 
 | 5073 | 	int ret; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5074 |  | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5075 | again: | 
| Xin Long | f53d77e | 2018-01-23 18:22:25 +0800 | [diff] [blame] | 5076 | 	ret = 0; | 
| Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 5077 | 	sctp_transport_walk_start(&hti); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5078 |  | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5079 | 	tsp = sctp_transport_get_idx(net, &hti, *pos + 1); | 
 | 5080 | 	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] | 5081 | 		ret = cb(tsp, p); | 
 | 5082 | 		if (ret) | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5083 | 			break; | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5084 | 		(*pos)++; | 
 | 5085 | 		sctp_transport_put(tsp); | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5086 | 	} | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5087 | 	sctp_transport_walk_stop(&hti); | 
| Xin Long | 53fa103 | 2016-04-14 15:35:35 +0800 | [diff] [blame] | 5088 |  | 
| Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5089 | 	if (ret) { | 
 | 5090 | 		if (cb_done && !cb_done(tsp, p)) { | 
 | 5091 | 			(*pos)++; | 
 | 5092 | 			sctp_transport_put(tsp); | 
 | 5093 | 			goto again; | 
 | 5094 | 		} | 
 | 5095 | 		sctp_transport_put(tsp); | 
 | 5096 | 	} | 
 | 5097 |  | 
 | 5098 | 	return ret; | 
| Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5099 | } | 
 | 5100 | EXPORT_SYMBOL_GPL(sctp_for_each_transport); | 
 | 5101 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | /* 7.2.1 Association Status (SCTP_STATUS) | 
 | 5103 |  | 
 | 5104 |  * Applications can retrieve current status information about an | 
 | 5105 |  * association, including association state, peer receiver window size, | 
 | 5106 |  * number of unacked data chunks, and number of data chunks pending | 
 | 5107 |  * receipt.  This information is read-only. | 
 | 5108 |  */ | 
 | 5109 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, | 
 | 5110 | 				       char __user *optval, | 
 | 5111 | 				       int __user *optlen) | 
 | 5112 | { | 
 | 5113 | 	struct sctp_status status; | 
 | 5114 | 	struct sctp_association *asoc = NULL; | 
 | 5115 | 	struct sctp_transport *transport; | 
 | 5116 | 	sctp_assoc_t associd; | 
 | 5117 | 	int retval = 0; | 
 | 5118 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5119 | 	if (len < sizeof(status)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | 		retval = -EINVAL; | 
 | 5121 | 		goto out; | 
 | 5122 | 	} | 
 | 5123 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5124 | 	len = sizeof(status); | 
 | 5125 | 	if (copy_from_user(&status, optval, len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | 		retval = -EFAULT; | 
 | 5127 | 		goto out; | 
 | 5128 | 	} | 
 | 5129 |  | 
 | 5130 | 	associd = status.sstat_assoc_id; | 
 | 5131 | 	asoc = sctp_id2assoc(sk, associd); | 
 | 5132 | 	if (!asoc) { | 
 | 5133 | 		retval = -EINVAL; | 
 | 5134 | 		goto out; | 
 | 5135 | 	} | 
 | 5136 |  | 
 | 5137 | 	transport = asoc->peer.primary_path; | 
 | 5138 |  | 
 | 5139 | 	status.sstat_assoc_id = sctp_assoc2id(asoc); | 
| Daniel Borkmann | 38ab1fa | 2014-08-28 15:28:26 +0200 | [diff] [blame] | 5140 | 	status.sstat_state = sctp_assoc_to_state(asoc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | 	status.sstat_rwnd =  asoc->peer.rwnd; | 
 | 5142 | 	status.sstat_unackdata = asoc->unack_data; | 
 | 5143 |  | 
 | 5144 | 	status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); | 
| Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 5145 | 	status.sstat_instrms = asoc->stream.incnt; | 
 | 5146 | 	status.sstat_outstrms = asoc->stream.outcnt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | 	status.sstat_fragmentation_point = asoc->frag_point; | 
 | 5148 | 	status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 
| Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 5149 | 	memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, | 
 | 5150 | 			transport->af_specific->sockaddr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | 	/* Map ipv4 address into v4-mapped-on-v6 address.  */ | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5152 | 	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] | 5153 | 		(union sctp_addr *)&status.sstat_primary.spinfo_address); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5154 | 	status.sstat_primary.spinfo_state = transport->state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | 	status.sstat_primary.spinfo_cwnd = transport->cwnd; | 
 | 5156 | 	status.sstat_primary.spinfo_srtt = transport->srtt; | 
 | 5157 | 	status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5158 | 	status.sstat_primary.spinfo_mtu = transport->pathmtu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5160 | 	if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) | 
 | 5161 | 		status.sstat_primary.spinfo_state = SCTP_ACTIVE; | 
 | 5162 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | 	if (put_user(len, optlen)) { | 
 | 5164 | 		retval = -EFAULT; | 
 | 5165 | 		goto out; | 
 | 5166 | 	} | 
 | 5167 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5168 | 	pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", | 
 | 5169 | 		 __func__, len, status.sstat_state, status.sstat_rwnd, | 
 | 5170 | 		 status.sstat_assoc_id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 |  | 
 | 5172 | 	if (copy_to_user(optval, &status, len)) { | 
 | 5173 | 		retval = -EFAULT; | 
 | 5174 | 		goto out; | 
 | 5175 | 	} | 
 | 5176 |  | 
 | 5177 | out: | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 5178 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | } | 
 | 5180 |  | 
 | 5181 |  | 
 | 5182 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) | 
 | 5183 |  * | 
 | 5184 |  * Applications can retrieve information about a specific peer address | 
 | 5185 |  * of an association, including its reachability state, congestion | 
 | 5186 |  * window, and retransmission timer values.  This information is | 
 | 5187 |  * read-only. | 
 | 5188 |  */ | 
 | 5189 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, | 
 | 5190 | 					  char __user *optval, | 
 | 5191 | 					  int __user *optlen) | 
 | 5192 | { | 
 | 5193 | 	struct sctp_paddrinfo pinfo; | 
 | 5194 | 	struct sctp_transport *transport; | 
 | 5195 | 	int retval = 0; | 
 | 5196 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5197 | 	if (len < sizeof(pinfo)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | 		retval = -EINVAL; | 
 | 5199 | 		goto out; | 
 | 5200 | 	} | 
 | 5201 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5202 | 	len = sizeof(pinfo); | 
 | 5203 | 	if (copy_from_user(&pinfo, optval, len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | 		retval = -EFAULT; | 
 | 5205 | 		goto out; | 
 | 5206 | 	} | 
 | 5207 |  | 
 | 5208 | 	transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, | 
 | 5209 | 					   pinfo.spinfo_assoc_id); | 
 | 5210 | 	if (!transport) | 
 | 5211 | 		return -EINVAL; | 
 | 5212 |  | 
 | 5213 | 	pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5214 | 	pinfo.spinfo_state = transport->state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | 	pinfo.spinfo_cwnd = transport->cwnd; | 
 | 5216 | 	pinfo.spinfo_srtt = transport->srtt; | 
 | 5217 | 	pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5218 | 	pinfo.spinfo_mtu = transport->pathmtu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5220 | 	if (pinfo.spinfo_state == SCTP_UNKNOWN) | 
 | 5221 | 		pinfo.spinfo_state = SCTP_ACTIVE; | 
 | 5222 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | 	if (put_user(len, optlen)) { | 
 | 5224 | 		retval = -EFAULT; | 
 | 5225 | 		goto out; | 
 | 5226 | 	} | 
 | 5227 |  | 
 | 5228 | 	if (copy_to_user(optval, &pinfo, len)) { | 
 | 5229 | 		retval = -EFAULT; | 
 | 5230 | 		goto out; | 
 | 5231 | 	} | 
 | 5232 |  | 
 | 5233 | out: | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 5234 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | } | 
 | 5236 |  | 
 | 5237 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 
 | 5238 |  * | 
 | 5239 |  * This option is a on/off flag.  If enabled no SCTP message | 
 | 5240 |  * fragmentation will be performed.  Instead if a message being sent | 
 | 5241 |  * exceeds the current PMTU size, the message will NOT be sent and | 
 | 5242 |  * instead a error will be indicated to the user. | 
 | 5243 |  */ | 
 | 5244 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, | 
 | 5245 | 					char __user *optval, int __user *optlen) | 
 | 5246 | { | 
 | 5247 | 	int val; | 
 | 5248 |  | 
 | 5249 | 	if (len < sizeof(int)) | 
 | 5250 | 		return -EINVAL; | 
 | 5251 |  | 
 | 5252 | 	len = sizeof(int); | 
 | 5253 | 	val = (sctp_sk(sk)->disable_fragments == 1); | 
 | 5254 | 	if (put_user(len, optlen)) | 
 | 5255 | 		return -EFAULT; | 
 | 5256 | 	if (copy_to_user(optval, &val, len)) | 
 | 5257 | 		return -EFAULT; | 
 | 5258 | 	return 0; | 
 | 5259 | } | 
 | 5260 |  | 
 | 5261 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) | 
 | 5262 |  * | 
 | 5263 |  * This socket option is used to specify various notifications and | 
 | 5264 |  * ancillary data the user wishes to receive. | 
 | 5265 |  */ | 
 | 5266 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, | 
 | 5267 | 				  int __user *optlen) | 
 | 5268 | { | 
| Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 5269 | 	if (len == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | 		return -EINVAL; | 
| Thomas Graf | acdd598 | 2012-04-03 22:17:53 +0000 | [diff] [blame] | 5271 | 	if (len > sizeof(struct sctp_event_subscribe)) | 
 | 5272 | 		len = sizeof(struct sctp_event_subscribe); | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5273 | 	if (put_user(len, optlen)) | 
 | 5274 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | 	if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) | 
 | 5276 | 		return -EFAULT; | 
 | 5277 | 	return 0; | 
 | 5278 | } | 
 | 5279 |  | 
 | 5280 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 
 | 5281 |  * | 
 | 5282 |  * This socket option is applicable to the UDP-style socket only.  When | 
 | 5283 |  * set it will cause associations that are idle for more than the | 
 | 5284 |  * specified number of seconds to automatically close.  An association | 
 | 5285 |  * being idle is defined an association that has NOT sent or received | 
 | 5286 |  * user data.  The special value of '0' indicates that no automatic | 
 | 5287 |  * close of any associations should be performed.  The option expects an | 
 | 5288 |  * integer defining the number of seconds of idle time before an | 
 | 5289 |  * association is closed. | 
 | 5290 |  */ | 
 | 5291 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 5292 | { | 
 | 5293 | 	/* Applicable to UDP-style socket only */ | 
 | 5294 | 	if (sctp_style(sk, TCP)) | 
 | 5295 | 		return -EOPNOTSUPP; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5296 | 	if (len < sizeof(int)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5298 | 	len = sizeof(int); | 
 | 5299 | 	if (put_user(len, optlen)) | 
 | 5300 | 		return -EFAULT; | 
| David Windsor | b2ce04c | 2017-06-10 22:50:43 -0400 | [diff] [blame] | 5301 | 	if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | 		return -EFAULT; | 
 | 5303 | 	return 0; | 
 | 5304 | } | 
 | 5305 |  | 
 | 5306 | /* Helper routine to branch off an association to a new socket.  */ | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5307 | 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] | 5308 | { | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5309 | 	struct sctp_association *asoc = sctp_id2assoc(sk, id); | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5310 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | 	struct socket *sock; | 
 | 5312 | 	int err = 0; | 
 | 5313 |  | 
| Xin Long | df80cd9 | 2017-10-17 23:26:10 +0800 | [diff] [blame] | 5314 | 	/* Do not peel off from one netns to another one. */ | 
 | 5315 | 	if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) | 
 | 5316 | 		return -EINVAL; | 
 | 5317 |  | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5318 | 	if (!asoc) | 
 | 5319 | 		return -EINVAL; | 
 | 5320 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | 	/* An association cannot be branched off from an already peeled-off | 
 | 5322 | 	 * socket, nor is this supported for tcp style sockets. | 
 | 5323 | 	 */ | 
 | 5324 | 	if (!sctp_style(sk, UDP)) | 
 | 5325 | 		return -EINVAL; | 
 | 5326 |  | 
 | 5327 | 	/* Create a new socket.  */ | 
 | 5328 | 	err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); | 
 | 5329 | 	if (err < 0) | 
 | 5330 | 		return err; | 
 | 5331 |  | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5332 | 	sctp_copy_sock(sock->sk, sk, asoc); | 
| Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5333 |  | 
 | 5334 | 	/* Make peeled-off sockets more like 1-1 accepted sockets. | 
| Richard Haines | b7e10c2 | 2018-02-24 16:18:51 +0000 | [diff] [blame] | 5335 | 	 * Set the daddr and initialize id to something more random and also | 
 | 5336 | 	 * copy over any ip options. | 
| Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5337 | 	 */ | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5338 | 	sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk); | 
| Richard Haines | b7e10c2 | 2018-02-24 16:18:51 +0000 | [diff] [blame] | 5339 | 	sp->pf->copy_ip_options(sk, sock->sk); | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5340 |  | 
 | 5341 | 	/* Populate the fields of the newsk from the oldsk and migrate the | 
 | 5342 | 	 * asoc to the newsk. | 
 | 5343 | 	 */ | 
 | 5344 | 	sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); | 
| Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5345 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | 	*sockp = sock; | 
 | 5347 |  | 
 | 5348 | 	return err; | 
 | 5349 | } | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5350 | EXPORT_SYMBOL(sctp_do_peeloff); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 |  | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5352 | static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, | 
 | 5353 | 					  struct file **newfile, unsigned flags) | 
 | 5354 | { | 
 | 5355 | 	struct socket *newsock; | 
 | 5356 | 	int retval; | 
 | 5357 |  | 
 | 5358 | 	retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); | 
 | 5359 | 	if (retval < 0) | 
 | 5360 | 		goto out; | 
 | 5361 |  | 
 | 5362 | 	/* Map the socket to an unused fd that can be returned to the user.  */ | 
 | 5363 | 	retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); | 
 | 5364 | 	if (retval < 0) { | 
 | 5365 | 		sock_release(newsock); | 
 | 5366 | 		goto out; | 
 | 5367 | 	} | 
 | 5368 |  | 
 | 5369 | 	*newfile = sock_alloc_file(newsock, 0, NULL); | 
 | 5370 | 	if (IS_ERR(*newfile)) { | 
 | 5371 | 		put_unused_fd(retval); | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5372 | 		retval = PTR_ERR(*newfile); | 
 | 5373 | 		*newfile = NULL; | 
 | 5374 | 		return retval; | 
 | 5375 | 	} | 
 | 5376 |  | 
 | 5377 | 	pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, | 
 | 5378 | 		 retval); | 
 | 5379 |  | 
 | 5380 | 	peeloff->sd = retval; | 
 | 5381 |  | 
 | 5382 | 	if (flags & SOCK_NONBLOCK) | 
 | 5383 | 		(*newfile)->f_flags |= O_NONBLOCK; | 
 | 5384 | out: | 
 | 5385 | 	return retval; | 
 | 5386 | } | 
 | 5387 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 5389 | { | 
 | 5390 | 	sctp_peeloff_arg_t peeloff; | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5391 | 	struct file *newfile = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | 	int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5394 | 	if (len < sizeof(sctp_peeloff_arg_t)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5396 | 	len = sizeof(sctp_peeloff_arg_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | 	if (copy_from_user(&peeloff, optval, len)) | 
 | 5398 | 		return -EFAULT; | 
 | 5399 |  | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5400 | 	retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | 	if (retval < 0) | 
 | 5402 | 		goto out; | 
 | 5403 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | 	/* Return the fd mapped to the new socket.  */ | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5405 | 	if (put_user(len, optlen)) { | 
 | 5406 | 		fput(newfile); | 
 | 5407 | 		put_unused_fd(retval); | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5408 | 		return -EFAULT; | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5409 | 	} | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5410 |  | 
 | 5411 | 	if (copy_to_user(optval, &peeloff, len)) { | 
 | 5412 | 		fput(newfile); | 
 | 5413 | 		put_unused_fd(retval); | 
 | 5414 | 		return -EFAULT; | 
 | 5415 | 	} | 
 | 5416 | 	fd_install(retval, newfile); | 
 | 5417 | out: | 
 | 5418 | 	return retval; | 
 | 5419 | } | 
 | 5420 |  | 
 | 5421 | static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, | 
 | 5422 | 					 char __user *optval, int __user *optlen) | 
 | 5423 | { | 
 | 5424 | 	sctp_peeloff_flags_arg_t peeloff; | 
 | 5425 | 	struct file *newfile = NULL; | 
 | 5426 | 	int retval = 0; | 
 | 5427 |  | 
 | 5428 | 	if (len < sizeof(sctp_peeloff_flags_arg_t)) | 
 | 5429 | 		return -EINVAL; | 
 | 5430 | 	len = sizeof(sctp_peeloff_flags_arg_t); | 
 | 5431 | 	if (copy_from_user(&peeloff, optval, len)) | 
 | 5432 | 		return -EFAULT; | 
 | 5433 |  | 
 | 5434 | 	retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, | 
 | 5435 | 						&newfile, peeloff.flags); | 
 | 5436 | 	if (retval < 0) | 
 | 5437 | 		goto out; | 
 | 5438 |  | 
 | 5439 | 	/* Return the fd mapped to the new socket.  */ | 
 | 5440 | 	if (put_user(len, optlen)) { | 
 | 5441 | 		fput(newfile); | 
 | 5442 | 		put_unused_fd(retval); | 
 | 5443 | 		return -EFAULT; | 
 | 5444 | 	} | 
 | 5445 |  | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5446 | 	if (copy_to_user(optval, &peeloff, len)) { | 
 | 5447 | 		fput(newfile); | 
 | 5448 | 		put_unused_fd(retval); | 
 | 5449 | 		return -EFAULT; | 
 | 5450 | 	} | 
 | 5451 | 	fd_install(retval, newfile); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | out: | 
 | 5453 | 	return retval; | 
 | 5454 | } | 
 | 5455 |  | 
 | 5456 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 
 | 5457 |  * | 
 | 5458 |  * Applications can enable or disable heartbeats for any peer address of | 
 | 5459 |  * an association, modify an address's heartbeat interval, force a | 
 | 5460 |  * heartbeat to be sent immediately, and adjust the address's maximum | 
 | 5461 |  * number of retransmissions sent before an address is considered | 
 | 5462 |  * unreachable.  The following structure is used to access and modify an | 
 | 5463 |  * address's parameters: | 
 | 5464 |  * | 
 | 5465 |  *  struct sctp_paddrparams { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5466 |  *     sctp_assoc_t            spp_assoc_id; | 
 | 5467 |  *     struct sockaddr_storage spp_address; | 
 | 5468 |  *     uint32_t                spp_hbinterval; | 
 | 5469 |  *     uint16_t                spp_pathmaxrxt; | 
 | 5470 |  *     uint32_t                spp_pathmtu; | 
 | 5471 |  *     uint32_t                spp_sackdelay; | 
 | 5472 |  *     uint32_t                spp_flags; | 
 | 5473 |  * }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5475 |  *   spp_assoc_id    - (one-to-many style socket) This is filled in the | 
 | 5476 |  *                     application, and identifies the association for | 
 | 5477 |  *                     this query. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 |  *   spp_address     - This specifies which address is of interest. | 
 | 5479 |  *   spp_hbinterval  - This contains the value of the heartbeat interval, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5480 |  *                     in milliseconds.  If a  value of zero | 
 | 5481 |  *                     is present in this field then no changes are to | 
 | 5482 |  *                     be made to this parameter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 |  *   spp_pathmaxrxt  - This contains the maximum number of | 
 | 5484 |  *                     retransmissions before this address shall be | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5485 |  *                     considered unreachable. If a  value of zero | 
 | 5486 |  *                     is present in this field then no changes are to | 
 | 5487 |  *                     be made to this parameter. | 
 | 5488 |  *   spp_pathmtu     - When Path MTU discovery is disabled the value | 
 | 5489 |  *                     specified here will be the "fixed" path mtu. | 
 | 5490 |  *                     Note that if the spp_address field is empty | 
 | 5491 |  *                     then all associations on this address will | 
 | 5492 |  *                     have this fixed path mtu set upon them. | 
 | 5493 |  * | 
 | 5494 |  *   spp_sackdelay   - When delayed sack is enabled, this value specifies | 
 | 5495 |  *                     the number of milliseconds that sacks will be delayed | 
 | 5496 |  *                     for. This value will apply to all addresses of an | 
 | 5497 |  *                     association if the spp_address field is empty. Note | 
 | 5498 |  *                     also, that if delayed sack is enabled and this | 
 | 5499 |  *                     value is set to 0, no change is made to the last | 
 | 5500 |  *                     recorded delayed sack timer value. | 
 | 5501 |  * | 
 | 5502 |  *   spp_flags       - These flags are used to control various features | 
 | 5503 |  *                     on an association. The flag field may contain | 
 | 5504 |  *                     zero or more of the following options. | 
 | 5505 |  * | 
 | 5506 |  *                     SPP_HB_ENABLE  - Enable heartbeats on the | 
 | 5507 |  *                     specified address. Note that if the address | 
 | 5508 |  *                     field is empty all addresses for the association | 
 | 5509 |  *                     have heartbeats enabled upon them. | 
 | 5510 |  * | 
 | 5511 |  *                     SPP_HB_DISABLE - Disable heartbeats on the | 
 | 5512 |  *                     speicifed address. Note that if the address | 
 | 5513 |  *                     field is empty all addresses for the association | 
 | 5514 |  *                     will have their heartbeats disabled. Note also | 
 | 5515 |  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are | 
 | 5516 |  *                     mutually exclusive, only one of these two should | 
 | 5517 |  *                     be specified. Enabling both fields will have | 
 | 5518 |  *                     undetermined results. | 
 | 5519 |  * | 
 | 5520 |  *                     SPP_HB_DEMAND - Request a user initiated heartbeat | 
 | 5521 |  *                     to be made immediately. | 
 | 5522 |  * | 
 | 5523 |  *                     SPP_PMTUD_ENABLE - This field will enable PMTU | 
 | 5524 |  *                     discovery upon the specified address. Note that | 
 | 5525 |  *                     if the address feild is empty then all addresses | 
 | 5526 |  *                     on the association are effected. | 
 | 5527 |  * | 
 | 5528 |  *                     SPP_PMTUD_DISABLE - This field will disable PMTU | 
 | 5529 |  *                     discovery upon the specified address. Note that | 
 | 5530 |  *                     if the address feild is empty then all addresses | 
 | 5531 |  *                     on the association are effected. Not also that | 
 | 5532 |  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 
 | 5533 |  *                     exclusive. Enabling both will have undetermined | 
 | 5534 |  *                     results. | 
 | 5535 |  * | 
 | 5536 |  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns | 
 | 5537 |  *                     on delayed sack. The time specified in spp_sackdelay | 
 | 5538 |  *                     is used to specify the sack delay for this address. Note | 
 | 5539 |  *                     that if spp_address is empty then all addresses will | 
 | 5540 |  *                     enable delayed sack and take on the sack delay | 
 | 5541 |  *                     value specified in spp_sackdelay. | 
 | 5542 |  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns | 
 | 5543 |  *                     off delayed sack. If the spp_address field is blank then | 
 | 5544 |  *                     delayed sack is disabled for the entire association. Note | 
 | 5545 |  *                     also that this field is mutually exclusive to | 
 | 5546 |  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined | 
 | 5547 |  *                     results. | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5548 |  * | 
 | 5549 |  *                     SPP_IPV6_FLOWLABEL:  Setting this flag enables the | 
 | 5550 |  *                     setting of the IPV6 flow label value.  The value is | 
 | 5551 |  *                     contained in the spp_ipv6_flowlabel field. | 
 | 5552 |  *                     Upon retrieval, this flag will be set to indicate that | 
 | 5553 |  *                     the spp_ipv6_flowlabel field has a valid value returned. | 
 | 5554 |  *                     If a specific destination address is set (in the | 
 | 5555 |  *                     spp_address field), then the value returned is that of | 
 | 5556 |  *                     the address.  If just an association is specified (and | 
 | 5557 |  *                     no address), then the association's default flow label | 
 | 5558 |  *                     is returned.  If neither an association nor a destination | 
 | 5559 |  *                     is specified, then the socket's default flow label is | 
 | 5560 |  *                     returned.  For non-IPv6 sockets, this flag will be left | 
 | 5561 |  *                     cleared. | 
 | 5562 |  * | 
 | 5563 |  *                     SPP_DSCP:  Setting this flag enables the setting of the | 
 | 5564 |  *                     Differentiated Services Code Point (DSCP) value | 
 | 5565 |  *                     associated with either the association or a specific | 
 | 5566 |  *                     address.  The value is obtained in the spp_dscp field. | 
 | 5567 |  *                     Upon retrieval, this flag will be set to indicate that | 
 | 5568 |  *                     the spp_dscp field has a valid value returned.  If a | 
 | 5569 |  *                     specific destination address is set when called (in the | 
 | 5570 |  *                     spp_address field), then that specific destination | 
 | 5571 |  *                     address's DSCP value is returned.  If just an association | 
 | 5572 |  *                     is specified, then the association's default DSCP is | 
 | 5573 |  *                     returned.  If neither an association nor a destination is | 
 | 5574 |  *                     specified, then the socket's default DSCP is returned. | 
 | 5575 |  * | 
 | 5576 |  *   spp_ipv6_flowlabel | 
 | 5577 |  *                   - This field is used in conjunction with the | 
 | 5578 |  *                     SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. | 
 | 5579 |  *                     The 20 least significant bits are used for the flow | 
 | 5580 |  *                     label.  This setting has precedence over any IPv6-layer | 
 | 5581 |  *                     setting. | 
 | 5582 |  * | 
 | 5583 |  *   spp_dscp        - This field is used in conjunction with the SPP_DSCP flag | 
 | 5584 |  *                     and contains the DSCP.  The 6 most significant bits are | 
 | 5585 |  *                     used for the DSCP.  This setting has precedence over any | 
 | 5586 |  *                     IPv4- or IPv6- layer setting. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 |  */ | 
 | 5588 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5589 | 					    char __user *optval, int __user *optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5591 | 	struct sctp_paddrparams  params; | 
 | 5592 | 	struct sctp_transport   *trans = NULL; | 
 | 5593 | 	struct sctp_association *asoc = NULL; | 
 | 5594 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 |  | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5596 | 	if (len >= sizeof(params)) | 
 | 5597 | 		len = sizeof(params); | 
 | 5598 | 	else if (len >= ALIGN(offsetof(struct sctp_paddrparams, | 
 | 5599 | 				       spp_ipv6_flowlabel), 4)) | 
 | 5600 | 		len = ALIGN(offsetof(struct sctp_paddrparams, | 
 | 5601 | 				     spp_ipv6_flowlabel), 4); | 
 | 5602 | 	else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | 		return -EINVAL; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5604 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 5606 | 		return -EFAULT; | 
 | 5607 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5608 | 	/* If an address other than INADDR_ANY is specified, and | 
 | 5609 | 	 * no transport is found, then the request is invalid. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | 	 */ | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5611 | 	if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5612 | 		trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 
 | 5613 | 					       params.spp_assoc_id); | 
 | 5614 | 		if (!trans) { | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5615 | 			pr_debug("%s: failed no transport\n", __func__); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5616 | 			return -EINVAL; | 
 | 5617 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | 	} | 
 | 5619 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5620 | 	/* Get association, if assoc_id != 0 and the socket is a one | 
 | 5621 | 	 * to many style socket, and an association was not found, then | 
 | 5622 | 	 * the id was invalid. | 
 | 5623 | 	 */ | 
 | 5624 | 	asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 
 | 5625 | 	if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5626 | 		pr_debug("%s: failed no association\n", __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | 		return -EINVAL; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5628 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5630 | 	if (trans) { | 
 | 5631 | 		/* Fetch transport values. */ | 
 | 5632 | 		params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); | 
 | 5633 | 		params.spp_pathmtu    = trans->pathmtu; | 
 | 5634 | 		params.spp_pathmaxrxt = trans->pathmaxrxt; | 
 | 5635 | 		params.spp_sackdelay  = jiffies_to_msecs(trans->sackdelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5637 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 5638 | 		params.spp_flags      = trans->param_flags; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5639 | 		if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) { | 
 | 5640 | 			params.spp_ipv6_flowlabel = trans->flowlabel & | 
 | 5641 | 						    SCTP_FLOWLABEL_VAL_MASK; | 
 | 5642 | 			params.spp_flags |= SPP_IPV6_FLOWLABEL; | 
 | 5643 | 		} | 
 | 5644 | 		if (trans->dscp & SCTP_DSCP_SET_MASK) { | 
 | 5645 | 			params.spp_dscp	= trans->dscp & SCTP_DSCP_VAL_MASK; | 
 | 5646 | 			params.spp_flags |= SPP_DSCP; | 
 | 5647 | 		} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5648 | 	} else if (asoc) { | 
 | 5649 | 		/* Fetch association values. */ | 
 | 5650 | 		params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); | 
 | 5651 | 		params.spp_pathmtu    = asoc->pathmtu; | 
 | 5652 | 		params.spp_pathmaxrxt = asoc->pathmaxrxt; | 
 | 5653 | 		params.spp_sackdelay  = jiffies_to_msecs(asoc->sackdelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5655 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 5656 | 		params.spp_flags      = asoc->param_flags; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5657 | 		if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) { | 
 | 5658 | 			params.spp_ipv6_flowlabel = asoc->flowlabel & | 
 | 5659 | 						    SCTP_FLOWLABEL_VAL_MASK; | 
 | 5660 | 			params.spp_flags |= SPP_IPV6_FLOWLABEL; | 
 | 5661 | 		} | 
 | 5662 | 		if (asoc->dscp & SCTP_DSCP_SET_MASK) { | 
 | 5663 | 			params.spp_dscp	= asoc->dscp & SCTP_DSCP_VAL_MASK; | 
 | 5664 | 			params.spp_flags |= SPP_DSCP; | 
 | 5665 | 		} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5666 | 	} else { | 
 | 5667 | 		/* Fetch socket values. */ | 
 | 5668 | 		params.spp_hbinterval = sp->hbinterval; | 
 | 5669 | 		params.spp_pathmtu    = sp->pathmtu; | 
 | 5670 | 		params.spp_sackdelay  = sp->sackdelay; | 
 | 5671 | 		params.spp_pathmaxrxt = sp->pathmaxrxt; | 
 | 5672 |  | 
 | 5673 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 5674 | 		params.spp_flags      = sp->param_flags; | 
| Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5675 | 		if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) { | 
 | 5676 | 			params.spp_ipv6_flowlabel = sp->flowlabel & | 
 | 5677 | 						    SCTP_FLOWLABEL_VAL_MASK; | 
 | 5678 | 			params.spp_flags |= SPP_IPV6_FLOWLABEL; | 
 | 5679 | 		} | 
 | 5680 | 		if (sp->dscp & SCTP_DSCP_SET_MASK) { | 
 | 5681 | 			params.spp_dscp	= sp->dscp & SCTP_DSCP_VAL_MASK; | 
 | 5682 | 			params.spp_flags |= SPP_DSCP; | 
 | 5683 | 		} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5684 | 	} | 
 | 5685 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 5687 | 		return -EFAULT; | 
 | 5688 |  | 
 | 5689 | 	if (put_user(len, optlen)) | 
 | 5690 | 		return -EFAULT; | 
 | 5691 |  | 
 | 5692 | 	return 0; | 
 | 5693 | } | 
 | 5694 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5695 | /* | 
 | 5696 |  * 7.1.23.  Get or set delayed ack timer (SCTP_DELAYED_SACK) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5697 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5698 |  * This option will effect the way delayed acks are performed.  This | 
 | 5699 |  * option allows you to get or set the delayed ack time, in | 
 | 5700 |  * milliseconds.  It also allows changing the delayed ack frequency. | 
 | 5701 |  * Changing the frequency to 1 disables the delayed sack algorithm.  If | 
 | 5702 |  * the assoc_id is 0, then this sets or gets the endpoints default | 
 | 5703 |  * values.  If the assoc_id field is non-zero, then the set or get | 
 | 5704 |  * effects the specified association for the one to many model (the | 
 | 5705 |  * assoc_id field is ignored by the one to one model).  Note that if | 
 | 5706 |  * sack_delay or sack_freq are 0 when setting this option, then the | 
 | 5707 |  * current values will remain unchanged. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5708 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5709 |  * struct sctp_sack_info { | 
 | 5710 |  *     sctp_assoc_t            sack_assoc_id; | 
 | 5711 |  *     uint32_t                sack_delay; | 
 | 5712 |  *     uint32_t                sack_freq; | 
 | 5713 |  * }; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5714 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5715 |  * sack_assoc_id -  This parameter, indicates which association the user | 
 | 5716 |  *    is performing an action upon.  Note that if this field's value is | 
 | 5717 |  *    zero then the endpoints default value is changed (effecting future | 
 | 5718 |  *    associations only). | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5719 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5720 |  * sack_delay -  This parameter contains the number of milliseconds that | 
 | 5721 |  *    the user is requesting the delayed ACK timer be set to.  Note that | 
 | 5722 |  *    this value is defined in the standard to be between 200 and 500 | 
 | 5723 |  *    milliseconds. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5724 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5725 |  * sack_freq -  This parameter contains the number of packets that must | 
 | 5726 |  *    be received before a sack is sent without waiting for the delay | 
 | 5727 |  *    timer to expire.  The default value for this is 2, setting this | 
 | 5728 |  *    value to 1 will disable the delayed sack algorithm. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5729 |  */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5730 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5731 | 					    char __user *optval, | 
 | 5732 | 					    int __user *optlen) | 
 | 5733 | { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5734 | 	struct sctp_sack_info    params; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5735 | 	struct sctp_association *asoc = NULL; | 
 | 5736 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 5737 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5738 | 	if (len >= sizeof(struct sctp_sack_info)) { | 
 | 5739 | 		len = sizeof(struct sctp_sack_info); | 
 | 5740 |  | 
 | 5741 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 5742 | 			return -EFAULT; | 
 | 5743 | 	} else if (len == sizeof(struct sctp_assoc_value)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 5744 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 5745 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 5746 | 				    "Use of struct sctp_assoc_value in delayed_ack socket option.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 5747 | 				    "Use struct sctp_sack_info instead\n", | 
 | 5748 | 				    current->comm, task_pid_nr(current)); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5749 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 5750 | 			return -EFAULT; | 
 | 5751 | 	} else | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5752 | 		return -EINVAL; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5753 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5754 | 	/* Get association, if sack_assoc_id != 0 and the socket is a one | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5755 | 	 * to many style socket, and an association was not found, then | 
 | 5756 | 	 * the id was invalid. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5757 | 	 */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5758 | 	asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 
 | 5759 | 	if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5760 | 		return -EINVAL; | 
 | 5761 |  | 
 | 5762 | 	if (asoc) { | 
 | 5763 | 		/* Fetch association values. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5764 | 		if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { | 
 | 5765 | 			params.sack_delay = jiffies_to_msecs( | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5766 | 				asoc->sackdelay); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5767 | 			params.sack_freq = asoc->sackfreq; | 
 | 5768 |  | 
 | 5769 | 		} else { | 
 | 5770 | 			params.sack_delay = 0; | 
 | 5771 | 			params.sack_freq = 1; | 
 | 5772 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5773 | 	} else { | 
 | 5774 | 		/* Fetch socket values. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5775 | 		if (sp->param_flags & SPP_SACKDELAY_ENABLE) { | 
 | 5776 | 			params.sack_delay  = sp->sackdelay; | 
 | 5777 | 			params.sack_freq = sp->sackfreq; | 
 | 5778 | 		} else { | 
 | 5779 | 			params.sack_delay  = 0; | 
 | 5780 | 			params.sack_freq = 1; | 
 | 5781 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5782 | 	} | 
 | 5783 |  | 
 | 5784 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 5785 | 		return -EFAULT; | 
 | 5786 |  | 
 | 5787 | 	if (put_user(len, optlen)) | 
 | 5788 | 		return -EFAULT; | 
 | 5789 |  | 
 | 5790 | 	return 0; | 
 | 5791 | } | 
 | 5792 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 
 | 5794 |  * | 
 | 5795 |  * Applications can specify protocol parameters for the default association | 
 | 5796 |  * initialization.  The option name argument to setsockopt() and getsockopt() | 
 | 5797 |  * is SCTP_INITMSG. | 
 | 5798 |  * | 
 | 5799 |  * Setting initialization parameters is effective only on an unconnected | 
 | 5800 |  * socket (for UDP-style sockets only future associations are effected | 
 | 5801 |  * by the change).  With TCP-style sockets, this option is inherited by | 
 | 5802 |  * sockets derived from a listener socket. | 
 | 5803 |  */ | 
 | 5804 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 5805 | { | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5806 | 	if (len < sizeof(struct sctp_initmsg)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5808 | 	len = sizeof(struct sctp_initmsg); | 
 | 5809 | 	if (put_user(len, optlen)) | 
 | 5810 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | 	if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) | 
 | 5812 | 		return -EFAULT; | 
 | 5813 | 	return 0; | 
 | 5814 | } | 
 | 5815 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5817 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, | 
 | 5818 | 				      char __user *optval, int __user *optlen) | 
 | 5819 | { | 
 | 5820 | 	struct sctp_association *asoc; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5821 | 	int cnt = 0; | 
 | 5822 | 	struct sctp_getaddrs getaddrs; | 
 | 5823 | 	struct sctp_transport *from; | 
 | 5824 | 	void __user *to; | 
 | 5825 | 	union sctp_addr temp; | 
 | 5826 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5827 | 	int addrlen; | 
 | 5828 | 	size_t space_left; | 
 | 5829 | 	int bytes_copied; | 
 | 5830 |  | 
 | 5831 | 	if (len < sizeof(struct sctp_getaddrs)) | 
 | 5832 | 		return -EINVAL; | 
 | 5833 |  | 
 | 5834 | 	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 
 | 5835 | 		return -EFAULT; | 
 | 5836 |  | 
 | 5837 | 	/* For UDP-style sockets, id specifies the association to query.  */ | 
 | 5838 | 	asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 
 | 5839 | 	if (!asoc) | 
 | 5840 | 		return -EINVAL; | 
 | 5841 |  | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5842 | 	to = optval + offsetof(struct sctp_getaddrs, addrs); | 
 | 5843 | 	space_left = len - offsetof(struct sctp_getaddrs, addrs); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5844 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 5845 | 	list_for_each_entry(from, &asoc->peer.transport_addr_list, | 
 | 5846 | 				transports) { | 
| Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 5847 | 		memcpy(&temp, &from->ipaddr, sizeof(temp)); | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5848 | 		addrlen = sctp_get_pf_specific(sk->sk_family) | 
 | 5849 | 			      ->addr_to_user(sp, &temp); | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5850 | 		if (space_left < addrlen) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5851 | 			return -ENOMEM; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5852 | 		if (copy_to_user(to, &temp, addrlen)) | 
 | 5853 | 			return -EFAULT; | 
 | 5854 | 		to += addrlen; | 
 | 5855 | 		cnt++; | 
 | 5856 | 		space_left -= addrlen; | 
 | 5857 | 	} | 
 | 5858 |  | 
 | 5859 | 	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) | 
 | 5860 | 		return -EFAULT; | 
 | 5861 | 	bytes_copied = ((char __user *)to) - optval; | 
 | 5862 | 	if (put_user(bytes_copied, optlen)) | 
 | 5863 | 		return -EFAULT; | 
 | 5864 |  | 
 | 5865 | 	return 0; | 
 | 5866 | } | 
 | 5867 |  | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5868 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, | 
 | 5869 | 			    size_t space_left, int *bytes_copied) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5870 | { | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5871 | 	struct sctp_sockaddr_entry *addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5872 | 	union sctp_addr temp; | 
 | 5873 | 	int cnt = 0; | 
 | 5874 | 	int addrlen; | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 5875 | 	struct net *net = sock_net(sk); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5876 |  | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5877 | 	rcu_read_lock(); | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 5878 | 	list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5879 | 		if (!addr->valid) | 
 | 5880 | 			continue; | 
 | 5881 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5882 | 		if ((PF_INET == sk->sk_family) && | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5883 | 		    (AF_INET6 == addr->a.sa.sa_family)) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5884 | 			continue; | 
| Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 5885 | 		if ((PF_INET6 == sk->sk_family) && | 
 | 5886 | 		    inet_v6_ipv6only(sk) && | 
 | 5887 | 		    (AF_INET == addr->a.sa.sa_family)) | 
 | 5888 | 			continue; | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5889 | 		memcpy(&temp, &addr->a, sizeof(temp)); | 
| Vlad Yasevich | b46ae36 | 2008-01-28 14:25:36 -0500 | [diff] [blame] | 5890 | 		if (!temp.v4.sin_port) | 
 | 5891 | 			temp.v4.sin_port = htons(port); | 
 | 5892 |  | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5893 | 		addrlen = sctp_get_pf_specific(sk->sk_family) | 
 | 5894 | 			      ->addr_to_user(sctp_sk(sk), &temp); | 
 | 5895 |  | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5896 | 		if (space_left < addrlen) { | 
 | 5897 | 			cnt =  -ENOMEM; | 
 | 5898 | 			break; | 
 | 5899 | 		} | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5900 | 		memcpy(to, &temp, addrlen); | 
| Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 5901 |  | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5902 | 		to += addrlen; | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5903 | 		cnt++; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5904 | 		space_left -= addrlen; | 
| Vlad Yasevich | 3663c30 | 2007-07-03 12:43:12 -0400 | [diff] [blame] | 5905 | 		*bytes_copied += addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5906 | 	} | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5907 | 	rcu_read_unlock(); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5908 |  | 
 | 5909 | 	return cnt; | 
 | 5910 | } | 
 | 5911 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5913 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, | 
 | 5914 | 				       char __user *optval, int __user *optlen) | 
 | 5915 | { | 
 | 5916 | 	struct sctp_bind_addr *bp; | 
 | 5917 | 	struct sctp_association *asoc; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5918 | 	int cnt = 0; | 
 | 5919 | 	struct sctp_getaddrs getaddrs; | 
 | 5920 | 	struct sctp_sockaddr_entry *addr; | 
 | 5921 | 	void __user *to; | 
 | 5922 | 	union sctp_addr temp; | 
 | 5923 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5924 | 	int addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5925 | 	int err = 0; | 
 | 5926 | 	size_t space_left; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5927 | 	int bytes_copied = 0; | 
 | 5928 | 	void *addrs; | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5929 | 	void *buf; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5930 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5931 | 	if (len < sizeof(struct sctp_getaddrs)) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5932 | 		return -EINVAL; | 
 | 5933 |  | 
 | 5934 | 	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 
 | 5935 | 		return -EFAULT; | 
 | 5936 |  | 
 | 5937 | 	/* | 
 | 5938 | 	 *  For UDP-style sockets, id specifies the association to query. | 
 | 5939 | 	 *  If the id field is set to the value '0' then the locally bound | 
 | 5940 | 	 *  addresses are returned without regard to any particular | 
 | 5941 | 	 *  association. | 
 | 5942 | 	 */ | 
 | 5943 | 	if (0 == getaddrs.assoc_id) { | 
 | 5944 | 		bp = &sctp_sk(sk)->ep->base.bind_addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5945 | 	} else { | 
 | 5946 | 		asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 
 | 5947 | 		if (!asoc) | 
 | 5948 | 			return -EINVAL; | 
 | 5949 | 		bp = &asoc->base.bind_addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5950 | 	} | 
 | 5951 |  | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5952 | 	to = optval + offsetof(struct sctp_getaddrs, addrs); | 
 | 5953 | 	space_left = len - offsetof(struct sctp_getaddrs, addrs); | 
| Neil Horman | 186e234 | 2007-06-18 19:59:16 -0400 | [diff] [blame] | 5954 |  | 
| Marcelo Ricardo Leitner | cacc062 | 2015-11-30 14:32:54 -0200 | [diff] [blame] | 5955 | 	addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5956 | 	if (!addrs) | 
 | 5957 | 		return -ENOMEM; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5958 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5959 | 	/* If the endpoint is bound to 0.0.0.0 or ::0, get the valid | 
 | 5960 | 	 * addresses from the global local address list. | 
 | 5961 | 	 */ | 
 | 5962 | 	if (sctp_list_single_entry(&bp->address_list)) { | 
 | 5963 | 		addr = list_entry(bp->address_list.next, | 
 | 5964 | 				  struct sctp_sockaddr_entry, list); | 
| Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 5965 | 		if (sctp_is_any(sk, &addr->a)) { | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5966 | 			cnt = sctp_copy_laddrs(sk, bp->port, addrs, | 
 | 5967 | 						space_left, &bytes_copied); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5968 | 			if (cnt < 0) { | 
 | 5969 | 				err = cnt; | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5970 | 				goto out; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5971 | 			} | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5972 | 			goto copy_getaddrs; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5973 | 		} | 
 | 5974 | 	} | 
 | 5975 |  | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5976 | 	buf = addrs; | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5977 | 	/* Protection on the bound address list is not needed since | 
 | 5978 | 	 * in the socket option context we hold a socket lock and | 
 | 5979 | 	 * thus the bound address list can't change. | 
 | 5980 | 	 */ | 
 | 5981 | 	list_for_each_entry(addr, &bp->address_list, list) { | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5982 | 		memcpy(&temp, &addr->a, sizeof(temp)); | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5983 | 		addrlen = sctp_get_pf_specific(sk->sk_family) | 
 | 5984 | 			      ->addr_to_user(sp, &temp); | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5985 | 		if (space_left < addrlen) { | 
 | 5986 | 			err =  -ENOMEM; /*fixme: right error?*/ | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5987 | 			goto out; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5988 | 		} | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5989 | 		memcpy(buf, &temp, addrlen); | 
 | 5990 | 		buf += addrlen; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5991 | 		bytes_copied += addrlen; | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5992 | 		cnt++; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5993 | 		space_left -= addrlen; | 
 | 5994 | 	} | 
 | 5995 |  | 
 | 5996 | copy_getaddrs: | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5997 | 	if (copy_to_user(to, addrs, bytes_copied)) { | 
 | 5998 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 5999 | 		goto out; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6000 | 	} | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6001 | 	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { | 
 | 6002 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 6003 | 		goto out; | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6004 | 	} | 
| Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6005 | 	/* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too, | 
 | 6006 | 	 * but we can't change it anymore. | 
 | 6007 | 	 */ | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6008 | 	if (put_user(bytes_copied, optlen)) | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6009 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 6010 | out: | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6011 | 	kfree(addrs); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6012 | 	return err; | 
 | 6013 | } | 
 | 6014 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 
 | 6016 |  * | 
 | 6017 |  * Requests that the local SCTP stack use the enclosed peer address as | 
 | 6018 |  * the association primary.  The enclosed address must be one of the | 
 | 6019 |  * association peer's addresses. | 
 | 6020 |  */ | 
 | 6021 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, | 
 | 6022 | 					char __user *optval, int __user *optlen) | 
 | 6023 | { | 
 | 6024 | 	struct sctp_prim prim; | 
 | 6025 | 	struct sctp_association *asoc; | 
 | 6026 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6027 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6028 | 	if (len < sizeof(struct sctp_prim)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | 		return -EINVAL; | 
 | 6030 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6031 | 	len = sizeof(struct sctp_prim); | 
 | 6032 |  | 
 | 6033 | 	if (copy_from_user(&prim, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | 		return -EFAULT; | 
 | 6035 |  | 
 | 6036 | 	asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); | 
 | 6037 | 	if (!asoc) | 
 | 6038 | 		return -EINVAL; | 
 | 6039 |  | 
 | 6040 | 	if (!asoc->peer.primary_path) | 
 | 6041 | 		return -ENOTCONN; | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6042 |  | 
| Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 6043 | 	memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, | 
 | 6044 | 		asoc->peer.primary_path->af_specific->sockaddr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 |  | 
| Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 6046 | 	sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | 			(union sctp_addr *)&prim.ssp_addr); | 
 | 6048 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6049 | 	if (put_user(len, optlen)) | 
 | 6050 | 		return -EFAULT; | 
 | 6051 | 	if (copy_to_user(optval, &prim, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | 		return -EFAULT; | 
 | 6053 |  | 
 | 6054 | 	return 0; | 
 | 6055 | } | 
 | 6056 |  | 
 | 6057 | /* | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6058 |  * 7.1.11  Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 |  * | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6060 |  * Requests that the local endpoint set the specified Adaptation Layer | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 |  * Indication parameter for all future INIT and INIT-ACK exchanges. | 
 | 6062 |  */ | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6063 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | 				  char __user *optval, int __user *optlen) | 
 | 6065 | { | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6066 | 	struct sctp_setadaptation adaptation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6068 | 	if (len < sizeof(struct sctp_setadaptation)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | 		return -EINVAL; | 
 | 6070 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6071 | 	len = sizeof(struct sctp_setadaptation); | 
 | 6072 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6073 | 	adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6074 |  | 
 | 6075 | 	if (put_user(len, optlen)) | 
 | 6076 | 		return -EFAULT; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6077 | 	if (copy_to_user(optval, &adaptation, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | 		return -EFAULT; | 
| Ivan Skytte Jorgensen | a1ab358 | 2005-10-28 15:33:24 -0700 | [diff] [blame] | 6079 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | 	return 0; | 
 | 6081 | } | 
 | 6082 |  | 
 | 6083 | /* | 
 | 6084 |  * | 
 | 6085 |  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 
 | 6086 |  * | 
 | 6087 |  *   Applications that wish to use the sendto() system call may wish to | 
 | 6088 |  *   specify a default set of parameters that would normally be supplied | 
 | 6089 |  *   through the inclusion of ancillary data.  This socket option allows | 
 | 6090 |  *   such an application to set the default sctp_sndrcvinfo structure. | 
 | 6091 |  | 
 | 6092 |  | 
 | 6093 |  *   The application that wishes to use this socket option simply passes | 
 | 6094 |  *   in to this call the sctp_sndrcvinfo structure defined in Section | 
 | 6095 |  *   5.2.2) The input parameters accepted by this call include | 
 | 6096 |  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 
 | 6097 |  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in | 
 | 6098 |  *   to this call if the caller is using the UDP model. | 
 | 6099 |  * | 
 | 6100 |  *   For getsockopt, it get the default sctp_sndrcvinfo structure. | 
 | 6101 |  */ | 
 | 6102 | static int sctp_getsockopt_default_send_param(struct sock *sk, | 
 | 6103 | 					int len, char __user *optval, | 
 | 6104 | 					int __user *optlen) | 
 | 6105 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6107 | 	struct sctp_association *asoc; | 
 | 6108 | 	struct sctp_sndrcvinfo info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 |  | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6110 | 	if (len < sizeof(info)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6112 |  | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6113 | 	len = sizeof(info); | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6114 |  | 
 | 6115 | 	if (copy_from_user(&info, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | 		return -EFAULT; | 
 | 6117 |  | 
 | 6118 | 	asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 
 | 6119 | 	if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 
 | 6120 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | 	if (asoc) { | 
 | 6122 | 		info.sinfo_stream = asoc->default_stream; | 
 | 6123 | 		info.sinfo_flags = asoc->default_flags; | 
 | 6124 | 		info.sinfo_ppid = asoc->default_ppid; | 
 | 6125 | 		info.sinfo_context = asoc->default_context; | 
 | 6126 | 		info.sinfo_timetolive = asoc->default_timetolive; | 
 | 6127 | 	} else { | 
 | 6128 | 		info.sinfo_stream = sp->default_stream; | 
 | 6129 | 		info.sinfo_flags = sp->default_flags; | 
 | 6130 | 		info.sinfo_ppid = sp->default_ppid; | 
 | 6131 | 		info.sinfo_context = sp->default_context; | 
 | 6132 | 		info.sinfo_timetolive = sp->default_timetolive; | 
 | 6133 | 	} | 
 | 6134 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6135 | 	if (put_user(len, optlen)) | 
 | 6136 | 		return -EFAULT; | 
 | 6137 | 	if (copy_to_user(optval, &info, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | 		return -EFAULT; | 
 | 6139 |  | 
 | 6140 | 	return 0; | 
 | 6141 | } | 
 | 6142 |  | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6143 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters | 
 | 6144 |  * (SCTP_DEFAULT_SNDINFO) | 
 | 6145 |  */ | 
 | 6146 | static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, | 
 | 6147 | 					   char __user *optval, | 
 | 6148 | 					   int __user *optlen) | 
 | 6149 | { | 
 | 6150 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6151 | 	struct sctp_association *asoc; | 
 | 6152 | 	struct sctp_sndinfo info; | 
 | 6153 |  | 
 | 6154 | 	if (len < sizeof(info)) | 
 | 6155 | 		return -EINVAL; | 
 | 6156 |  | 
 | 6157 | 	len = sizeof(info); | 
 | 6158 |  | 
 | 6159 | 	if (copy_from_user(&info, optval, len)) | 
 | 6160 | 		return -EFAULT; | 
 | 6161 |  | 
 | 6162 | 	asoc = sctp_id2assoc(sk, info.snd_assoc_id); | 
 | 6163 | 	if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) | 
 | 6164 | 		return -EINVAL; | 
 | 6165 | 	if (asoc) { | 
 | 6166 | 		info.snd_sid = asoc->default_stream; | 
 | 6167 | 		info.snd_flags = asoc->default_flags; | 
 | 6168 | 		info.snd_ppid = asoc->default_ppid; | 
 | 6169 | 		info.snd_context = asoc->default_context; | 
 | 6170 | 	} else { | 
 | 6171 | 		info.snd_sid = sp->default_stream; | 
 | 6172 | 		info.snd_flags = sp->default_flags; | 
 | 6173 | 		info.snd_ppid = sp->default_ppid; | 
 | 6174 | 		info.snd_context = sp->default_context; | 
 | 6175 | 	} | 
 | 6176 |  | 
 | 6177 | 	if (put_user(len, optlen)) | 
 | 6178 | 		return -EFAULT; | 
 | 6179 | 	if (copy_to_user(optval, &info, len)) | 
 | 6180 | 		return -EFAULT; | 
 | 6181 |  | 
 | 6182 | 	return 0; | 
 | 6183 | } | 
 | 6184 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | /* | 
 | 6186 |  * | 
 | 6187 |  * 7.1.5 SCTP_NODELAY | 
 | 6188 |  * | 
 | 6189 |  * Turn on/off any Nagle-like algorithm.  This means that packets are | 
 | 6190 |  * generally sent as soon as possible and no unnecessary delays are | 
 | 6191 |  * introduced, at the cost of more packets in the network.  Expects an | 
 | 6192 |  * integer boolean flag. | 
 | 6193 |  */ | 
 | 6194 |  | 
 | 6195 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, | 
 | 6196 | 				   char __user *optval, int __user *optlen) | 
 | 6197 | { | 
 | 6198 | 	int val; | 
 | 6199 |  | 
 | 6200 | 	if (len < sizeof(int)) | 
 | 6201 | 		return -EINVAL; | 
 | 6202 |  | 
 | 6203 | 	len = sizeof(int); | 
 | 6204 | 	val = (sctp_sk(sk)->nodelay == 1); | 
 | 6205 | 	if (put_user(len, optlen)) | 
 | 6206 | 		return -EFAULT; | 
 | 6207 | 	if (copy_to_user(optval, &val, len)) | 
 | 6208 | 		return -EFAULT; | 
 | 6209 | 	return 0; | 
 | 6210 | } | 
 | 6211 |  | 
 | 6212 | /* | 
 | 6213 |  * | 
 | 6214 |  * 7.1.1 SCTP_RTOINFO | 
 | 6215 |  * | 
 | 6216 |  * The protocol parameters used to initialize and bound retransmission | 
 | 6217 |  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 
 | 6218 |  * and modify these parameters. | 
 | 6219 |  * All parameters are time values, in milliseconds.  A value of 0, when | 
 | 6220 |  * modifying the parameters, indicates that the current value should not | 
 | 6221 |  * be changed. | 
 | 6222 |  * | 
 | 6223 |  */ | 
 | 6224 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, | 
 | 6225 | 				char __user *optval, | 
 | 6226 | 				int __user *optlen) { | 
 | 6227 | 	struct sctp_rtoinfo rtoinfo; | 
 | 6228 | 	struct sctp_association *asoc; | 
 | 6229 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6230 | 	if (len < sizeof (struct sctp_rtoinfo)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | 		return -EINVAL; | 
 | 6232 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6233 | 	len = sizeof(struct sctp_rtoinfo); | 
 | 6234 |  | 
 | 6235 | 	if (copy_from_user(&rtoinfo, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6236 | 		return -EFAULT; | 
 | 6237 |  | 
 | 6238 | 	asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 
 | 6239 |  | 
 | 6240 | 	if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 
 | 6241 | 		return -EINVAL; | 
 | 6242 |  | 
 | 6243 | 	/* Values corresponding to the specific association. */ | 
 | 6244 | 	if (asoc) { | 
 | 6245 | 		rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); | 
 | 6246 | 		rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); | 
 | 6247 | 		rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); | 
 | 6248 | 	} else { | 
 | 6249 | 		/* Values corresponding to the endpoint. */ | 
 | 6250 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 6251 |  | 
 | 6252 | 		rtoinfo.srto_initial = sp->rtoinfo.srto_initial; | 
 | 6253 | 		rtoinfo.srto_max = sp->rtoinfo.srto_max; | 
 | 6254 | 		rtoinfo.srto_min = sp->rtoinfo.srto_min; | 
 | 6255 | 	} | 
 | 6256 |  | 
 | 6257 | 	if (put_user(len, optlen)) | 
 | 6258 | 		return -EFAULT; | 
 | 6259 |  | 
 | 6260 | 	if (copy_to_user(optval, &rtoinfo, len)) | 
 | 6261 | 		return -EFAULT; | 
 | 6262 |  | 
 | 6263 | 	return 0; | 
 | 6264 | } | 
 | 6265 |  | 
 | 6266 | /* | 
 | 6267 |  * | 
 | 6268 |  * 7.1.2 SCTP_ASSOCINFO | 
 | 6269 |  * | 
| Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 6270 |  * This option is used to tune the maximum retransmission attempts | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 |  * of the association. | 
 | 6272 |  * Returns an error if the new association retransmission value is | 
 | 6273 |  * greater than the sum of the retransmission value  of the peer. | 
 | 6274 |  * See [SCTP] for more information. | 
 | 6275 |  * | 
 | 6276 |  */ | 
 | 6277 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, | 
 | 6278 | 				     char __user *optval, | 
 | 6279 | 				     int __user *optlen) | 
 | 6280 | { | 
 | 6281 |  | 
 | 6282 | 	struct sctp_assocparams assocparams; | 
 | 6283 | 	struct sctp_association *asoc; | 
 | 6284 | 	struct list_head *pos; | 
 | 6285 | 	int cnt = 0; | 
 | 6286 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6287 | 	if (len < sizeof (struct sctp_assocparams)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | 		return -EINVAL; | 
 | 6289 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6290 | 	len = sizeof(struct sctp_assocparams); | 
 | 6291 |  | 
 | 6292 | 	if (copy_from_user(&assocparams, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | 		return -EFAULT; | 
 | 6294 |  | 
 | 6295 | 	asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 
 | 6296 |  | 
 | 6297 | 	if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 
 | 6298 | 		return -EINVAL; | 
 | 6299 |  | 
 | 6300 | 	/* Values correspoinding to the specific association */ | 
| Vladislav Yasevich | 1733721 | 2005-04-28 11:57:54 -0700 | [diff] [blame] | 6301 | 	if (asoc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | 		assocparams.sasoc_asocmaxrxt = asoc->max_retrans; | 
 | 6303 | 		assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; | 
 | 6304 | 		assocparams.sasoc_local_rwnd = asoc->a_rwnd; | 
| Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 6305 | 		assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 |  | 
 | 6307 | 		list_for_each(pos, &asoc->peer.transport_addr_list) { | 
| wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6308 | 			cnt++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | 		} | 
 | 6310 |  | 
 | 6311 | 		assocparams.sasoc_number_peer_destinations = cnt; | 
 | 6312 | 	} else { | 
 | 6313 | 		/* Values corresponding to the endpoint */ | 
 | 6314 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 6315 |  | 
 | 6316 | 		assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; | 
 | 6317 | 		assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; | 
 | 6318 | 		assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; | 
 | 6319 | 		assocparams.sasoc_cookie_life = | 
 | 6320 | 					sp->assocparams.sasoc_cookie_life; | 
 | 6321 | 		assocparams.sasoc_number_peer_destinations = | 
 | 6322 | 					sp->assocparams. | 
 | 6323 | 					sasoc_number_peer_destinations; | 
 | 6324 | 	} | 
 | 6325 |  | 
 | 6326 | 	if (put_user(len, optlen)) | 
 | 6327 | 		return -EFAULT; | 
 | 6328 |  | 
 | 6329 | 	if (copy_to_user(optval, &assocparams, len)) | 
 | 6330 | 		return -EFAULT; | 
 | 6331 |  | 
 | 6332 | 	return 0; | 
 | 6333 | } | 
 | 6334 |  | 
 | 6335 | /* | 
 | 6336 |  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 
 | 6337 |  * | 
 | 6338 |  * This socket option is a boolean flag which turns on or off mapped V4 | 
 | 6339 |  * addresses.  If this option is turned on and the socket is type | 
 | 6340 |  * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 
 | 6341 |  * If this option is turned off, then no mapping will be done of V4 | 
 | 6342 |  * addresses and a user will receive both PF_INET6 and PF_INET type | 
 | 6343 |  * addresses on the socket. | 
 | 6344 |  */ | 
 | 6345 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, | 
 | 6346 | 				    char __user *optval, int __user *optlen) | 
 | 6347 | { | 
 | 6348 | 	int val; | 
 | 6349 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6350 |  | 
 | 6351 | 	if (len < sizeof(int)) | 
 | 6352 | 		return -EINVAL; | 
 | 6353 |  | 
 | 6354 | 	len = sizeof(int); | 
 | 6355 | 	val = sp->v4mapped; | 
 | 6356 | 	if (put_user(len, optlen)) | 
 | 6357 | 		return -EFAULT; | 
 | 6358 | 	if (copy_to_user(optval, &val, len)) | 
 | 6359 | 		return -EFAULT; | 
 | 6360 |  | 
 | 6361 | 	return 0; | 
 | 6362 | } | 
 | 6363 |  | 
 | 6364 | /* | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6365 |  * 7.1.29.  Set or Get the default context (SCTP_CONTEXT) | 
 | 6366 |  * (chapter and verse is quoted at sctp_setsockopt_context()) | 
 | 6367 |  */ | 
 | 6368 | static int sctp_getsockopt_context(struct sock *sk, int len, | 
 | 6369 | 				   char __user *optval, int __user *optlen) | 
 | 6370 | { | 
 | 6371 | 	struct sctp_assoc_value params; | 
 | 6372 | 	struct sctp_sock *sp; | 
 | 6373 | 	struct sctp_association *asoc; | 
 | 6374 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6375 | 	if (len < sizeof(struct sctp_assoc_value)) | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6376 | 		return -EINVAL; | 
 | 6377 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6378 | 	len = sizeof(struct sctp_assoc_value); | 
 | 6379 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6380 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 6381 | 		return -EFAULT; | 
 | 6382 |  | 
 | 6383 | 	sp = sctp_sk(sk); | 
 | 6384 |  | 
 | 6385 | 	if (params.assoc_id != 0) { | 
 | 6386 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 6387 | 		if (!asoc) | 
 | 6388 | 			return -EINVAL; | 
 | 6389 | 		params.assoc_value = asoc->default_rcv_context; | 
 | 6390 | 	} else { | 
 | 6391 | 		params.assoc_value = sp->default_rcv_context; | 
 | 6392 | 	} | 
 | 6393 |  | 
 | 6394 | 	if (put_user(len, optlen)) | 
 | 6395 | 		return -EFAULT; | 
 | 6396 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 6397 | 		return -EFAULT; | 
 | 6398 |  | 
 | 6399 | 	return 0; | 
 | 6400 | } | 
 | 6401 |  | 
 | 6402 | /* | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6403 |  * 8.1.16.  Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 
 | 6404 |  * This option will get or set the maximum size to put in any outgoing | 
 | 6405 |  * 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] | 6406 |  * fragmented by SCTP into the specified size.  Note that the underlying | 
 | 6407 |  * SCTP implementation may fragment into smaller sized chunks when the | 
 | 6408 |  * PMTU of the underlying association is smaller than the value set by | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6409 |  * the user.  The default value for this option is '0' which indicates | 
 | 6410 |  * the user is NOT limiting fragmentation and only the PMTU will effect | 
 | 6411 |  * SCTP's choice of DATA chunk size.  Note also that values set larger | 
 | 6412 |  * than the maximum size of an IP datagram will effectively let SCTP | 
 | 6413 |  * control fragmentation (i.e. the same as setting this option to 0). | 
 | 6414 |  * | 
 | 6415 |  * The following structure is used to access and modify this parameter: | 
 | 6416 |  * | 
 | 6417 |  * struct sctp_assoc_value { | 
 | 6418 |  *   sctp_assoc_t assoc_id; | 
 | 6419 |  *   uint32_t assoc_value; | 
 | 6420 |  * }; | 
 | 6421 |  * | 
 | 6422 |  * assoc_id:  This parameter is ignored for one-to-one style sockets. | 
 | 6423 |  *    For one-to-many style sockets this parameter indicates which | 
 | 6424 |  *    association the user is performing an action upon.  Note that if | 
 | 6425 |  *    this field's value is zero then the endpoints default value is | 
 | 6426 |  *    changed (effecting future associations only). | 
 | 6427 |  * assoc_value:  This parameter specifies the maximum size in bytes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 |  */ | 
 | 6429 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, | 
 | 6430 | 				  char __user *optval, int __user *optlen) | 
 | 6431 | { | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6432 | 	struct sctp_assoc_value params; | 
 | 6433 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6435 | 	if (len == sizeof(int)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6436 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6437 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6438 | 				    "Use of int in maxseg socket option.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6439 | 				    "Use struct sctp_assoc_value instead\n", | 
 | 6440 | 				    current->comm, task_pid_nr(current)); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6441 | 		params.assoc_id = 0; | 
 | 6442 | 	} else if (len >= sizeof(struct sctp_assoc_value)) { | 
 | 6443 | 		len = sizeof(struct sctp_assoc_value); | 
| Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6444 | 		if (copy_from_user(¶ms, optval, len)) | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6445 | 			return -EFAULT; | 
 | 6446 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | 		return -EINVAL; | 
 | 6448 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6449 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 6450 | 	if (!asoc && params.assoc_id && sctp_style(sk, UDP)) | 
 | 6451 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6453 | 	if (asoc) | 
 | 6454 | 		params.assoc_value = asoc->frag_point; | 
 | 6455 | 	else | 
 | 6456 | 		params.assoc_value = sctp_sk(sk)->user_frag; | 
 | 6457 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | 	if (put_user(len, optlen)) | 
 | 6459 | 		return -EFAULT; | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6460 | 	if (len == sizeof(int)) { | 
 | 6461 | 		if (copy_to_user(optval, ¶ms.assoc_value, len)) | 
 | 6462 | 			return -EFAULT; | 
 | 6463 | 	} else { | 
 | 6464 | 		if (copy_to_user(optval, ¶ms, len)) | 
 | 6465 | 			return -EFAULT; | 
 | 6466 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 |  | 
 | 6468 | 	return 0; | 
 | 6469 | } | 
 | 6470 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6471 | /* | 
 | 6472 |  * 7.1.24.  Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 
 | 6473 |  * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) | 
 | 6474 |  */ | 
 | 6475 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, | 
 | 6476 | 					       char __user *optval, int __user *optlen) | 
 | 6477 | { | 
 | 6478 | 	int val; | 
 | 6479 |  | 
 | 6480 | 	if (len < sizeof(int)) | 
 | 6481 | 		return -EINVAL; | 
 | 6482 |  | 
 | 6483 | 	len = sizeof(int); | 
 | 6484 |  | 
 | 6485 | 	val = sctp_sk(sk)->frag_interleave; | 
 | 6486 | 	if (put_user(len, optlen)) | 
 | 6487 | 		return -EFAULT; | 
 | 6488 | 	if (copy_to_user(optval, &val, len)) | 
 | 6489 | 		return -EFAULT; | 
 | 6490 |  | 
 | 6491 | 	return 0; | 
 | 6492 | } | 
 | 6493 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6494 | /* | 
 | 6495 |  * 7.1.25.  Set or Get the sctp partial delivery point | 
 | 6496 |  * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) | 
 | 6497 |  */ | 
 | 6498 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, | 
 | 6499 | 						  char __user *optval, | 
 | 6500 | 						  int __user *optlen) | 
 | 6501 | { | 
| YOSHIFUJI Hideaki | 9cbcbf4 | 2007-07-19 10:44:50 +0900 | [diff] [blame] | 6502 | 	u32 val; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6503 |  | 
 | 6504 | 	if (len < sizeof(u32)) | 
 | 6505 | 		return -EINVAL; | 
 | 6506 |  | 
 | 6507 | 	len = sizeof(u32); | 
 | 6508 |  | 
 | 6509 | 	val = sctp_sk(sk)->pd_point; | 
 | 6510 | 	if (put_user(len, optlen)) | 
 | 6511 | 		return -EFAULT; | 
 | 6512 | 	if (copy_to_user(optval, &val, len)) | 
 | 6513 | 		return -EFAULT; | 
 | 6514 |  | 
| Wei Yongjun | 7d743b7 | 2010-12-14 16:10:41 +0000 | [diff] [blame] | 6515 | 	return 0; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6516 | } | 
 | 6517 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6518 | /* | 
 | 6519 |  * 7.1.28.  Set or Get the maximum burst (SCTP_MAX_BURST) | 
 | 6520 |  * (chapter and verse is quoted at sctp_setsockopt_maxburst()) | 
 | 6521 |  */ | 
 | 6522 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, | 
 | 6523 | 				    char __user *optval, | 
 | 6524 | 				    int __user *optlen) | 
 | 6525 | { | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6526 | 	struct sctp_assoc_value params; | 
 | 6527 | 	struct sctp_sock *sp; | 
 | 6528 | 	struct sctp_association *asoc; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6529 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6530 | 	if (len == sizeof(int)) { | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6531 | 		pr_warn_ratelimited(DEPRECATED | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6532 | 				    "%s (pid %d) " | 
| Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6533 | 				    "Use of int in max_burst socket option.\n" | 
| Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6534 | 				    "Use struct sctp_assoc_value instead\n", | 
 | 6535 | 				    current->comm, task_pid_nr(current)); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6536 | 		params.assoc_id = 0; | 
| Wei Yongjun | c6db93a | 2009-03-02 09:46:12 +0000 | [diff] [blame] | 6537 | 	} else if (len >= sizeof(struct sctp_assoc_value)) { | 
 | 6538 | 		len = sizeof(struct sctp_assoc_value); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6539 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 6540 | 			return -EFAULT; | 
 | 6541 | 	} else | 
 | 6542 | 		return -EINVAL; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6543 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6544 | 	sp = sctp_sk(sk); | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6545 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6546 | 	if (params.assoc_id != 0) { | 
 | 6547 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 6548 | 		if (!asoc) | 
 | 6549 | 			return -EINVAL; | 
 | 6550 | 		params.assoc_value = asoc->max_burst; | 
 | 6551 | 	} else | 
 | 6552 | 		params.assoc_value = sp->max_burst; | 
 | 6553 |  | 
 | 6554 | 	if (len == sizeof(int)) { | 
 | 6555 | 		if (copy_to_user(optval, ¶ms.assoc_value, len)) | 
 | 6556 | 			return -EFAULT; | 
 | 6557 | 	} else { | 
 | 6558 | 		if (copy_to_user(optval, ¶ms, len)) | 
 | 6559 | 			return -EFAULT; | 
 | 6560 | 	} | 
 | 6561 |  | 
 | 6562 | 	return 0; | 
 | 6563 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6564 | } | 
 | 6565 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6566 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, | 
 | 6567 | 				    char __user *optval, int __user *optlen) | 
 | 6568 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6569 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6570 | 	struct sctp_hmacalgo  __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6571 | 	struct sctp_hmac_algo_param *hmacs; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6572 | 	__u16 data_len = 0; | 
 | 6573 | 	u32 num_idents; | 
| Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6574 | 	int i; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6575 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6576 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6577 | 		return -EACCES; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6578 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6579 | 	hmacs = ep->auth_hmacs_list; | 
| Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6580 | 	data_len = ntohs(hmacs->param_hdr.length) - | 
 | 6581 | 		   sizeof(struct sctp_paramhdr); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6582 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6583 | 	if (len < sizeof(struct sctp_hmacalgo) + data_len) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6584 | 		return -EINVAL; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6585 |  | 
 | 6586 | 	len = sizeof(struct sctp_hmacalgo) + data_len; | 
 | 6587 | 	num_idents = data_len / sizeof(u16); | 
 | 6588 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6589 | 	if (put_user(len, optlen)) | 
 | 6590 | 		return -EFAULT; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6591 | 	if (put_user(num_idents, &p->shmac_num_idents)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6592 | 		return -EFAULT; | 
| Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6593 | 	for (i = 0; i < num_idents; i++) { | 
 | 6594 | 		__u16 hmacid = ntohs(hmacs->hmac_ids[i]); | 
 | 6595 |  | 
 | 6596 | 		if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) | 
 | 6597 | 			return -EFAULT; | 
 | 6598 | 	} | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6599 | 	return 0; | 
 | 6600 | } | 
 | 6601 |  | 
 | 6602 | static int sctp_getsockopt_active_key(struct sock *sk, int len, | 
 | 6603 | 				    char __user *optval, int __user *optlen) | 
 | 6604 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6605 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6606 | 	struct sctp_authkeyid val; | 
 | 6607 | 	struct sctp_association *asoc; | 
 | 6608 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6609 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6610 | 		return -EACCES; | 
 | 6611 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6612 | 	if (len < sizeof(struct sctp_authkeyid)) | 
 | 6613 | 		return -EINVAL; | 
| Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6614 |  | 
 | 6615 | 	len = sizeof(struct sctp_authkeyid); | 
 | 6616 | 	if (copy_from_user(&val, optval, len)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6617 | 		return -EFAULT; | 
 | 6618 |  | 
 | 6619 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 6620 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 6621 | 		return -EINVAL; | 
 | 6622 |  | 
 | 6623 | 	if (asoc) | 
 | 6624 | 		val.scact_keynumber = asoc->active_key_id; | 
 | 6625 | 	else | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6626 | 		val.scact_keynumber = ep->active_key_id; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6627 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6628 | 	if (put_user(len, optlen)) | 
 | 6629 | 		return -EFAULT; | 
 | 6630 | 	if (copy_to_user(optval, &val, len)) | 
 | 6631 | 		return -EFAULT; | 
 | 6632 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6633 | 	return 0; | 
 | 6634 | } | 
 | 6635 |  | 
 | 6636 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, | 
 | 6637 | 				    char __user *optval, int __user *optlen) | 
 | 6638 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6639 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6640 | 	struct sctp_authchunks __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6641 | 	struct sctp_authchunks val; | 
 | 6642 | 	struct sctp_association *asoc; | 
 | 6643 | 	struct sctp_chunks_param *ch; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6644 | 	u32    num_chunks = 0; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6645 | 	char __user *to; | 
 | 6646 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6647 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6648 | 		return -EACCES; | 
 | 6649 |  | 
 | 6650 | 	if (len < sizeof(struct sctp_authchunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6651 | 		return -EINVAL; | 
 | 6652 |  | 
| Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6653 | 	if (copy_from_user(&val, optval, sizeof(val))) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6654 | 		return -EFAULT; | 
 | 6655 |  | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6656 | 	to = p->gauth_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6657 | 	asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 
 | 6658 | 	if (!asoc) | 
 | 6659 | 		return -EINVAL; | 
 | 6660 |  | 
 | 6661 | 	ch = asoc->peer.peer_chunks; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6662 | 	if (!ch) | 
 | 6663 | 		goto num; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6664 |  | 
 | 6665 | 	/* See if the user provided enough room for all the data */ | 
| Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6666 | 	num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); | 
| Vlad Yasevich | b40db68 | 2008-02-27 14:40:37 -0500 | [diff] [blame] | 6667 | 	if (len < num_chunks) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6668 | 		return -EINVAL; | 
 | 6669 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6670 | 	if (copy_to_user(to, ch->chunks, num_chunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6671 | 		return -EFAULT; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6672 | num: | 
 | 6673 | 	len = sizeof(struct sctp_authchunks) + num_chunks; | 
| wangweidong | 8d72651 | 2013-12-23 12:16:53 +0800 | [diff] [blame] | 6674 | 	if (put_user(len, optlen)) | 
 | 6675 | 		return -EFAULT; | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 6676 | 	if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 
 | 6677 | 		return -EFAULT; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6678 | 	return 0; | 
 | 6679 | } | 
 | 6680 |  | 
 | 6681 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, | 
 | 6682 | 				    char __user *optval, int __user *optlen) | 
 | 6683 | { | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6684 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6685 | 	struct sctp_authchunks __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6686 | 	struct sctp_authchunks val; | 
 | 6687 | 	struct sctp_association *asoc; | 
 | 6688 | 	struct sctp_chunks_param *ch; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6689 | 	u32    num_chunks = 0; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6690 | 	char __user *to; | 
 | 6691 |  | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6692 | 	if (!ep->auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6693 | 		return -EACCES; | 
 | 6694 |  | 
 | 6695 | 	if (len < sizeof(struct sctp_authchunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6696 | 		return -EINVAL; | 
 | 6697 |  | 
| Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6698 | 	if (copy_from_user(&val, optval, sizeof(val))) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6699 | 		return -EFAULT; | 
 | 6700 |  | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6701 | 	to = p->gauth_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6702 | 	asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 
 | 6703 | 	if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) | 
 | 6704 | 		return -EINVAL; | 
 | 6705 |  | 
 | 6706 | 	if (asoc) | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 6707 | 		ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6708 | 	else | 
| Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6709 | 		ch = ep->auth_chunk_list; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6710 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6711 | 	if (!ch) | 
 | 6712 | 		goto num; | 
 | 6713 |  | 
| Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6714 | 	num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6715 | 	if (len < sizeof(struct sctp_authchunks) + num_chunks) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6716 | 		return -EINVAL; | 
 | 6717 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6718 | 	if (copy_to_user(to, ch->chunks, num_chunks)) | 
 | 6719 | 		return -EFAULT; | 
 | 6720 | num: | 
 | 6721 | 	len = sizeof(struct sctp_authchunks) + num_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6722 | 	if (put_user(len, optlen)) | 
 | 6723 | 		return -EFAULT; | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 6724 | 	if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 
 | 6725 | 		return -EFAULT; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6726 |  | 
 | 6727 | 	return 0; | 
 | 6728 | } | 
 | 6729 |  | 
| Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 6730 | /* | 
 | 6731 |  * 8.2.5.  Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) | 
 | 6732 |  * This option gets the current number of associations that are attached | 
 | 6733 |  * to a one-to-many style socket.  The option value is an uint32_t. | 
 | 6734 |  */ | 
 | 6735 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, | 
 | 6736 | 				    char __user *optval, int __user *optlen) | 
 | 6737 | { | 
 | 6738 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6739 | 	struct sctp_association *asoc; | 
 | 6740 | 	u32 val = 0; | 
 | 6741 |  | 
 | 6742 | 	if (sctp_style(sk, TCP)) | 
 | 6743 | 		return -EOPNOTSUPP; | 
 | 6744 |  | 
 | 6745 | 	if (len < sizeof(u32)) | 
 | 6746 | 		return -EINVAL; | 
 | 6747 |  | 
 | 6748 | 	len = sizeof(u32); | 
 | 6749 |  | 
 | 6750 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 6751 | 		val++; | 
 | 6752 | 	} | 
 | 6753 |  | 
 | 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 |  | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6762 | /* | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 6763 |  * 8.1.23 SCTP_AUTO_ASCONF | 
 | 6764 |  * See the corresponding setsockopt entry as description | 
 | 6765 |  */ | 
 | 6766 | static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, | 
 | 6767 | 				   char __user *optval, int __user *optlen) | 
 | 6768 | { | 
 | 6769 | 	int val = 0; | 
 | 6770 |  | 
 | 6771 | 	if (len < sizeof(int)) | 
 | 6772 | 		return -EINVAL; | 
 | 6773 |  | 
 | 6774 | 	len = sizeof(int); | 
 | 6775 | 	if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) | 
 | 6776 | 		val = 1; | 
 | 6777 | 	if (put_user(len, optlen)) | 
 | 6778 | 		return -EFAULT; | 
 | 6779 | 	if (copy_to_user(optval, &val, len)) | 
 | 6780 | 		return -EFAULT; | 
 | 6781 | 	return 0; | 
 | 6782 | } | 
 | 6783 |  | 
 | 6784 | /* | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6785 |  * 8.2.6. Get the Current Identifiers of Associations | 
 | 6786 |  *        (SCTP_GET_ASSOC_ID_LIST) | 
 | 6787 |  * | 
 | 6788 |  * This option gets the current list of SCTP association identifiers of | 
 | 6789 |  * the SCTP associations handled by a one-to-many style socket. | 
 | 6790 |  */ | 
 | 6791 | static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, | 
 | 6792 | 				    char __user *optval, int __user *optlen) | 
 | 6793 | { | 
 | 6794 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6795 | 	struct sctp_association *asoc; | 
 | 6796 | 	struct sctp_assoc_ids *ids; | 
 | 6797 | 	u32 num = 0; | 
 | 6798 |  | 
 | 6799 | 	if (sctp_style(sk, TCP)) | 
 | 6800 | 		return -EOPNOTSUPP; | 
 | 6801 |  | 
 | 6802 | 	if (len < sizeof(struct sctp_assoc_ids)) | 
 | 6803 | 		return -EINVAL; | 
 | 6804 |  | 
 | 6805 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 6806 | 		num++; | 
 | 6807 | 	} | 
 | 6808 |  | 
 | 6809 | 	if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num) | 
 | 6810 | 		return -EINVAL; | 
 | 6811 |  | 
 | 6812 | 	len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num; | 
 | 6813 |  | 
| Marcelo Ricardo Leitner | 9ba0b96 | 2015-12-23 16:28:40 -0200 | [diff] [blame] | 6814 | 	ids = kmalloc(len, GFP_USER | __GFP_NOWARN); | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6815 | 	if (unlikely(!ids)) | 
 | 6816 | 		return -ENOMEM; | 
 | 6817 |  | 
 | 6818 | 	ids->gaids_number_of_ids = num; | 
 | 6819 | 	num = 0; | 
 | 6820 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 6821 | 		ids->gaids_assoc_id[num++] = asoc->assoc_id; | 
 | 6822 | 	} | 
 | 6823 |  | 
 | 6824 | 	if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { | 
 | 6825 | 		kfree(ids); | 
 | 6826 | 		return -EFAULT; | 
 | 6827 | 	} | 
 | 6828 |  | 
 | 6829 | 	kfree(ids); | 
 | 6830 | 	return 0; | 
 | 6831 | } | 
 | 6832 |  | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 6833 | /* | 
 | 6834 |  * SCTP_PEER_ADDR_THLDS | 
 | 6835 |  * | 
 | 6836 |  * This option allows us to fetch the partially failed threshold for one or all | 
 | 6837 |  * transports in an association.  See Section 6.1 of: | 
 | 6838 |  * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt | 
 | 6839 |  */ | 
 | 6840 | static int sctp_getsockopt_paddr_thresholds(struct sock *sk, | 
 | 6841 | 					    char __user *optval, | 
 | 6842 | 					    int len, | 
 | 6843 | 					    int __user *optlen) | 
 | 6844 | { | 
 | 6845 | 	struct sctp_paddrthlds val; | 
 | 6846 | 	struct sctp_transport *trans; | 
 | 6847 | 	struct sctp_association *asoc; | 
 | 6848 |  | 
 | 6849 | 	if (len < sizeof(struct sctp_paddrthlds)) | 
 | 6850 | 		return -EINVAL; | 
 | 6851 | 	len = sizeof(struct sctp_paddrthlds); | 
 | 6852 | 	if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len)) | 
 | 6853 | 		return -EFAULT; | 
 | 6854 |  | 
 | 6855 | 	if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { | 
 | 6856 | 		asoc = sctp_id2assoc(sk, val.spt_assoc_id); | 
 | 6857 | 		if (!asoc) | 
 | 6858 | 			return -ENOENT; | 
 | 6859 |  | 
 | 6860 | 		val.spt_pathpfthld = asoc->pf_retrans; | 
 | 6861 | 		val.spt_pathmaxrxt = asoc->pathmaxrxt; | 
 | 6862 | 	} else { | 
 | 6863 | 		trans = sctp_addr_id2transport(sk, &val.spt_address, | 
 | 6864 | 					       val.spt_assoc_id); | 
 | 6865 | 		if (!trans) | 
 | 6866 | 			return -ENOENT; | 
 | 6867 |  | 
 | 6868 | 		val.spt_pathmaxrxt = trans->pathmaxrxt; | 
 | 6869 | 		val.spt_pathpfthld = trans->pf_retrans; | 
 | 6870 | 	} | 
 | 6871 |  | 
 | 6872 | 	if (put_user(len, optlen) || copy_to_user(optval, &val, len)) | 
 | 6873 | 		return -EFAULT; | 
 | 6874 |  | 
 | 6875 | 	return 0; | 
 | 6876 | } | 
 | 6877 |  | 
| Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6878 | /* | 
 | 6879 |  * SCTP_GET_ASSOC_STATS | 
 | 6880 |  * | 
 | 6881 |  * This option retrieves local per endpoint statistics. It is modeled | 
 | 6882 |  * after OpenSolaris' implementation | 
 | 6883 |  */ | 
 | 6884 | static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, | 
 | 6885 | 				       char __user *optval, | 
 | 6886 | 				       int __user *optlen) | 
 | 6887 | { | 
 | 6888 | 	struct sctp_assoc_stats sas; | 
 | 6889 | 	struct sctp_association *asoc = NULL; | 
 | 6890 |  | 
 | 6891 | 	/* User must provide at least the assoc id */ | 
 | 6892 | 	if (len < sizeof(sctp_assoc_t)) | 
 | 6893 | 		return -EINVAL; | 
 | 6894 |  | 
| Guenter Roeck | 726bc6b | 2013-02-27 10:57:31 +0000 | [diff] [blame] | 6895 | 	/* Allow the struct to grow and fill in as much as possible */ | 
 | 6896 | 	len = min_t(size_t, len, sizeof(sas)); | 
 | 6897 |  | 
| Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6898 | 	if (copy_from_user(&sas, optval, len)) | 
 | 6899 | 		return -EFAULT; | 
 | 6900 |  | 
 | 6901 | 	asoc = sctp_id2assoc(sk, sas.sas_assoc_id); | 
 | 6902 | 	if (!asoc) | 
 | 6903 | 		return -EINVAL; | 
 | 6904 |  | 
 | 6905 | 	sas.sas_rtxchunks = asoc->stats.rtxchunks; | 
 | 6906 | 	sas.sas_gapcnt = asoc->stats.gapcnt; | 
 | 6907 | 	sas.sas_outofseqtsns = asoc->stats.outofseqtsns; | 
 | 6908 | 	sas.sas_osacks = asoc->stats.osacks; | 
 | 6909 | 	sas.sas_isacks = asoc->stats.isacks; | 
 | 6910 | 	sas.sas_octrlchunks = asoc->stats.octrlchunks; | 
 | 6911 | 	sas.sas_ictrlchunks = asoc->stats.ictrlchunks; | 
 | 6912 | 	sas.sas_oodchunks = asoc->stats.oodchunks; | 
 | 6913 | 	sas.sas_iodchunks = asoc->stats.iodchunks; | 
 | 6914 | 	sas.sas_ouodchunks = asoc->stats.ouodchunks; | 
 | 6915 | 	sas.sas_iuodchunks = asoc->stats.iuodchunks; | 
 | 6916 | 	sas.sas_idupchunks = asoc->stats.idupchunks; | 
 | 6917 | 	sas.sas_opackets = asoc->stats.opackets; | 
 | 6918 | 	sas.sas_ipackets = asoc->stats.ipackets; | 
 | 6919 |  | 
 | 6920 | 	/* New high max rto observed, will return 0 if not a single | 
 | 6921 | 	 * RTO update took place. obs_rto_ipaddr will be bogus | 
 | 6922 | 	 * in such a case | 
 | 6923 | 	 */ | 
 | 6924 | 	sas.sas_maxrto = asoc->stats.max_obs_rto; | 
 | 6925 | 	memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, | 
 | 6926 | 		sizeof(struct sockaddr_storage)); | 
 | 6927 |  | 
 | 6928 | 	/* Mark beginning of a new observation period */ | 
 | 6929 | 	asoc->stats.max_obs_rto = asoc->rto_min; | 
 | 6930 |  | 
| Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6931 | 	if (put_user(len, optlen)) | 
 | 6932 | 		return -EFAULT; | 
 | 6933 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 6934 | 	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] | 6935 |  | 
 | 6936 | 	if (copy_to_user(optval, &sas, len)) | 
 | 6937 | 		return -EFAULT; | 
 | 6938 |  | 
 | 6939 | 	return 0; | 
 | 6940 | } | 
 | 6941 |  | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 6942 | static int sctp_getsockopt_recvrcvinfo(struct sock *sk,	int len, | 
 | 6943 | 				       char __user *optval, | 
 | 6944 | 				       int __user *optlen) | 
 | 6945 | { | 
 | 6946 | 	int val = 0; | 
 | 6947 |  | 
 | 6948 | 	if (len < sizeof(int)) | 
 | 6949 | 		return -EINVAL; | 
 | 6950 |  | 
 | 6951 | 	len = sizeof(int); | 
 | 6952 | 	if (sctp_sk(sk)->recvrcvinfo) | 
 | 6953 | 		val = 1; | 
 | 6954 | 	if (put_user(len, optlen)) | 
 | 6955 | 		return -EFAULT; | 
 | 6956 | 	if (copy_to_user(optval, &val, len)) | 
 | 6957 | 		return -EFAULT; | 
 | 6958 |  | 
 | 6959 | 	return 0; | 
 | 6960 | } | 
 | 6961 |  | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 6962 | static int sctp_getsockopt_recvnxtinfo(struct sock *sk,	int len, | 
 | 6963 | 				       char __user *optval, | 
 | 6964 | 				       int __user *optlen) | 
 | 6965 | { | 
 | 6966 | 	int val = 0; | 
 | 6967 |  | 
 | 6968 | 	if (len < sizeof(int)) | 
 | 6969 | 		return -EINVAL; | 
 | 6970 |  | 
 | 6971 | 	len = sizeof(int); | 
 | 6972 | 	if (sctp_sk(sk)->recvnxtinfo) | 
 | 6973 | 		val = 1; | 
 | 6974 | 	if (put_user(len, optlen)) | 
 | 6975 | 		return -EFAULT; | 
 | 6976 | 	if (copy_to_user(optval, &val, len)) | 
 | 6977 | 		return -EFAULT; | 
 | 6978 |  | 
 | 6979 | 	return 0; | 
 | 6980 | } | 
 | 6981 |  | 
| Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 6982 | static int sctp_getsockopt_pr_supported(struct sock *sk, int len, | 
 | 6983 | 					char __user *optval, | 
 | 6984 | 					int __user *optlen) | 
 | 6985 | { | 
 | 6986 | 	struct sctp_assoc_value params; | 
 | 6987 | 	struct sctp_association *asoc; | 
 | 6988 | 	int retval = -EFAULT; | 
 | 6989 |  | 
 | 6990 | 	if (len < sizeof(params)) { | 
 | 6991 | 		retval = -EINVAL; | 
 | 6992 | 		goto out; | 
 | 6993 | 	} | 
 | 6994 |  | 
 | 6995 | 	len = sizeof(params); | 
 | 6996 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 6997 | 		goto out; | 
 | 6998 |  | 
 | 6999 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7000 | 	if (asoc) { | 
 | 7001 | 		params.assoc_value = asoc->prsctp_enable; | 
 | 7002 | 	} else if (!params.assoc_id) { | 
 | 7003 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 7004 |  | 
 | 7005 | 		params.assoc_value = sp->ep->prsctp_enable; | 
 | 7006 | 	} else { | 
 | 7007 | 		retval = -EINVAL; | 
 | 7008 | 		goto out; | 
 | 7009 | 	} | 
 | 7010 |  | 
 | 7011 | 	if (put_user(len, optlen)) | 
 | 7012 | 		goto out; | 
 | 7013 |  | 
 | 7014 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 7015 | 		goto out; | 
 | 7016 |  | 
 | 7017 | 	retval = 0; | 
 | 7018 |  | 
 | 7019 | out: | 
 | 7020 | 	return retval; | 
 | 7021 | } | 
 | 7022 |  | 
| Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7023 | static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, | 
 | 7024 | 					  char __user *optval, | 
 | 7025 | 					  int __user *optlen) | 
 | 7026 | { | 
 | 7027 | 	struct sctp_default_prinfo info; | 
 | 7028 | 	struct sctp_association *asoc; | 
 | 7029 | 	int retval = -EFAULT; | 
 | 7030 |  | 
 | 7031 | 	if (len < sizeof(info)) { | 
 | 7032 | 		retval = -EINVAL; | 
 | 7033 | 		goto out; | 
 | 7034 | 	} | 
 | 7035 |  | 
 | 7036 | 	len = sizeof(info); | 
 | 7037 | 	if (copy_from_user(&info, optval, len)) | 
 | 7038 | 		goto out; | 
 | 7039 |  | 
 | 7040 | 	asoc = sctp_id2assoc(sk, info.pr_assoc_id); | 
 | 7041 | 	if (asoc) { | 
 | 7042 | 		info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); | 
 | 7043 | 		info.pr_value = asoc->default_timetolive; | 
 | 7044 | 	} else if (!info.pr_assoc_id) { | 
 | 7045 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 7046 |  | 
 | 7047 | 		info.pr_policy = SCTP_PR_POLICY(sp->default_flags); | 
 | 7048 | 		info.pr_value = sp->default_timetolive; | 
 | 7049 | 	} else { | 
 | 7050 | 		retval = -EINVAL; | 
 | 7051 | 		goto out; | 
 | 7052 | 	} | 
 | 7053 |  | 
 | 7054 | 	if (put_user(len, optlen)) | 
 | 7055 | 		goto out; | 
 | 7056 |  | 
 | 7057 | 	if (copy_to_user(optval, &info, len)) | 
 | 7058 | 		goto out; | 
 | 7059 |  | 
 | 7060 | 	retval = 0; | 
 | 7061 |  | 
 | 7062 | out: | 
 | 7063 | 	return retval; | 
 | 7064 | } | 
 | 7065 |  | 
| Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7066 | static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, | 
 | 7067 | 					  char __user *optval, | 
 | 7068 | 					  int __user *optlen) | 
 | 7069 | { | 
 | 7070 | 	struct sctp_prstatus params; | 
 | 7071 | 	struct sctp_association *asoc; | 
 | 7072 | 	int policy; | 
 | 7073 | 	int retval = -EINVAL; | 
 | 7074 |  | 
 | 7075 | 	if (len < sizeof(params)) | 
 | 7076 | 		goto out; | 
 | 7077 |  | 
 | 7078 | 	len = sizeof(params); | 
 | 7079 | 	if (copy_from_user(¶ms, optval, len)) { | 
 | 7080 | 		retval = -EFAULT; | 
 | 7081 | 		goto out; | 
 | 7082 | 	} | 
 | 7083 |  | 
 | 7084 | 	policy = params.sprstat_policy; | 
 | 7085 | 	if (policy & ~SCTP_PR_SCTP_MASK) | 
 | 7086 | 		goto out; | 
 | 7087 |  | 
 | 7088 | 	asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); | 
 | 7089 | 	if (!asoc) | 
 | 7090 | 		goto out; | 
 | 7091 |  | 
 | 7092 | 	if (policy == SCTP_PR_SCTP_NONE) { | 
 | 7093 | 		params.sprstat_abandoned_unsent = 0; | 
 | 7094 | 		params.sprstat_abandoned_sent = 0; | 
 | 7095 | 		for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { | 
 | 7096 | 			params.sprstat_abandoned_unsent += | 
 | 7097 | 				asoc->abandoned_unsent[policy]; | 
 | 7098 | 			params.sprstat_abandoned_sent += | 
 | 7099 | 				asoc->abandoned_sent[policy]; | 
 | 7100 | 		} | 
 | 7101 | 	} else { | 
 | 7102 | 		params.sprstat_abandoned_unsent = | 
 | 7103 | 			asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; | 
 | 7104 | 		params.sprstat_abandoned_sent = | 
 | 7105 | 			asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; | 
 | 7106 | 	} | 
 | 7107 |  | 
 | 7108 | 	if (put_user(len, optlen)) { | 
 | 7109 | 		retval = -EFAULT; | 
 | 7110 | 		goto out; | 
 | 7111 | 	} | 
 | 7112 |  | 
 | 7113 | 	if (copy_to_user(optval, ¶ms, len)) { | 
 | 7114 | 		retval = -EFAULT; | 
 | 7115 | 		goto out; | 
 | 7116 | 	} | 
 | 7117 |  | 
 | 7118 | 	retval = 0; | 
 | 7119 |  | 
 | 7120 | out: | 
 | 7121 | 	return retval; | 
 | 7122 | } | 
 | 7123 |  | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7124 | static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, | 
 | 7125 | 					   char __user *optval, | 
 | 7126 | 					   int __user *optlen) | 
 | 7127 | { | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7128 | 	struct sctp_stream_out_ext *streamoute; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7129 | 	struct sctp_association *asoc; | 
 | 7130 | 	struct sctp_prstatus params; | 
 | 7131 | 	int retval = -EINVAL; | 
 | 7132 | 	int policy; | 
 | 7133 |  | 
 | 7134 | 	if (len < sizeof(params)) | 
 | 7135 | 		goto out; | 
 | 7136 |  | 
 | 7137 | 	len = sizeof(params); | 
 | 7138 | 	if (copy_from_user(¶ms, optval, len)) { | 
 | 7139 | 		retval = -EFAULT; | 
 | 7140 | 		goto out; | 
 | 7141 | 	} | 
 | 7142 |  | 
 | 7143 | 	policy = params.sprstat_policy; | 
 | 7144 | 	if (policy & ~SCTP_PR_SCTP_MASK) | 
 | 7145 | 		goto out; | 
 | 7146 |  | 
 | 7147 | 	asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); | 
| Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 7148 | 	if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7149 | 		goto out; | 
 | 7150 |  | 
| Konstantin Khorenko | 05364ca | 2018-08-10 20:11:42 +0300 | [diff] [blame] | 7151 | 	streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext; | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7152 | 	if (!streamoute) { | 
 | 7153 | 		/* Not allocated yet, means all stats are 0 */ | 
 | 7154 | 		params.sprstat_abandoned_unsent = 0; | 
 | 7155 | 		params.sprstat_abandoned_sent = 0; | 
 | 7156 | 		retval = 0; | 
 | 7157 | 		goto out; | 
 | 7158 | 	} | 
 | 7159 |  | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7160 | 	if (policy == SCTP_PR_SCTP_NONE) { | 
 | 7161 | 		params.sprstat_abandoned_unsent = 0; | 
 | 7162 | 		params.sprstat_abandoned_sent = 0; | 
 | 7163 | 		for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { | 
 | 7164 | 			params.sprstat_abandoned_unsent += | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7165 | 				streamoute->abandoned_unsent[policy]; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7166 | 			params.sprstat_abandoned_sent += | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7167 | 				streamoute->abandoned_sent[policy]; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7168 | 		} | 
 | 7169 | 	} else { | 
 | 7170 | 		params.sprstat_abandoned_unsent = | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7171 | 			streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7172 | 		params.sprstat_abandoned_sent = | 
| Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7173 | 			streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7174 | 	} | 
 | 7175 |  | 
 | 7176 | 	if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { | 
 | 7177 | 		retval = -EFAULT; | 
 | 7178 | 		goto out; | 
 | 7179 | 	} | 
 | 7180 |  | 
 | 7181 | 	retval = 0; | 
 | 7182 |  | 
 | 7183 | out: | 
 | 7184 | 	return retval; | 
 | 7185 | } | 
 | 7186 |  | 
| Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7187 | static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, | 
 | 7188 | 					      char __user *optval, | 
 | 7189 | 					      int __user *optlen) | 
 | 7190 | { | 
 | 7191 | 	struct sctp_assoc_value params; | 
 | 7192 | 	struct sctp_association *asoc; | 
 | 7193 | 	int retval = -EFAULT; | 
 | 7194 |  | 
 | 7195 | 	if (len < sizeof(params)) { | 
 | 7196 | 		retval = -EINVAL; | 
 | 7197 | 		goto out; | 
 | 7198 | 	} | 
 | 7199 |  | 
 | 7200 | 	len = sizeof(params); | 
 | 7201 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 7202 | 		goto out; | 
 | 7203 |  | 
 | 7204 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7205 | 	if (asoc) { | 
 | 7206 | 		params.assoc_value = asoc->reconf_enable; | 
 | 7207 | 	} else if (!params.assoc_id) { | 
 | 7208 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 7209 |  | 
 | 7210 | 		params.assoc_value = sp->ep->reconf_enable; | 
 | 7211 | 	} else { | 
 | 7212 | 		retval = -EINVAL; | 
 | 7213 | 		goto out; | 
 | 7214 | 	} | 
 | 7215 |  | 
 | 7216 | 	if (put_user(len, optlen)) | 
 | 7217 | 		goto out; | 
 | 7218 |  | 
 | 7219 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 7220 | 		goto out; | 
 | 7221 |  | 
 | 7222 | 	retval = 0; | 
 | 7223 |  | 
 | 7224 | out: | 
 | 7225 | 	return retval; | 
 | 7226 | } | 
 | 7227 |  | 
| Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7228 | static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, | 
 | 7229 | 					   char __user *optval, | 
 | 7230 | 					   int __user *optlen) | 
 | 7231 | { | 
 | 7232 | 	struct sctp_assoc_value params; | 
 | 7233 | 	struct sctp_association *asoc; | 
 | 7234 | 	int retval = -EFAULT; | 
 | 7235 |  | 
 | 7236 | 	if (len < sizeof(params)) { | 
 | 7237 | 		retval = -EINVAL; | 
 | 7238 | 		goto out; | 
 | 7239 | 	} | 
 | 7240 |  | 
 | 7241 | 	len = sizeof(params); | 
 | 7242 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 7243 | 		goto out; | 
 | 7244 |  | 
 | 7245 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7246 | 	if (asoc) { | 
 | 7247 | 		params.assoc_value = asoc->strreset_enable; | 
 | 7248 | 	} else if (!params.assoc_id) { | 
 | 7249 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 7250 |  | 
 | 7251 | 		params.assoc_value = sp->ep->strreset_enable; | 
 | 7252 | 	} else { | 
 | 7253 | 		retval = -EINVAL; | 
 | 7254 | 		goto out; | 
 | 7255 | 	} | 
 | 7256 |  | 
 | 7257 | 	if (put_user(len, optlen)) | 
 | 7258 | 		goto out; | 
 | 7259 |  | 
 | 7260 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 7261 | 		goto out; | 
 | 7262 |  | 
 | 7263 | 	retval = 0; | 
 | 7264 |  | 
 | 7265 | out: | 
 | 7266 | 	return retval; | 
 | 7267 | } | 
 | 7268 |  | 
| Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7269 | static int sctp_getsockopt_scheduler(struct sock *sk, int len, | 
 | 7270 | 				     char __user *optval, | 
 | 7271 | 				     int __user *optlen) | 
 | 7272 | { | 
 | 7273 | 	struct sctp_assoc_value params; | 
 | 7274 | 	struct sctp_association *asoc; | 
 | 7275 | 	int retval = -EFAULT; | 
 | 7276 |  | 
 | 7277 | 	if (len < sizeof(params)) { | 
 | 7278 | 		retval = -EINVAL; | 
 | 7279 | 		goto out; | 
 | 7280 | 	} | 
 | 7281 |  | 
 | 7282 | 	len = sizeof(params); | 
 | 7283 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 7284 | 		goto out; | 
 | 7285 |  | 
 | 7286 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7287 | 	if (!asoc) { | 
 | 7288 | 		retval = -EINVAL; | 
 | 7289 | 		goto out; | 
 | 7290 | 	} | 
 | 7291 |  | 
 | 7292 | 	params.assoc_value = sctp_sched_get_sched(asoc); | 
 | 7293 |  | 
 | 7294 | 	if (put_user(len, optlen)) | 
 | 7295 | 		goto out; | 
 | 7296 |  | 
 | 7297 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 7298 | 		goto out; | 
 | 7299 |  | 
 | 7300 | 	retval = 0; | 
 | 7301 |  | 
 | 7302 | out: | 
 | 7303 | 	return retval; | 
 | 7304 | } | 
 | 7305 |  | 
| Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 7306 | static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, | 
 | 7307 | 					   char __user *optval, | 
 | 7308 | 					   int __user *optlen) | 
 | 7309 | { | 
 | 7310 | 	struct sctp_stream_value params; | 
 | 7311 | 	struct sctp_association *asoc; | 
 | 7312 | 	int retval = -EFAULT; | 
 | 7313 |  | 
 | 7314 | 	if (len < sizeof(params)) { | 
 | 7315 | 		retval = -EINVAL; | 
 | 7316 | 		goto out; | 
 | 7317 | 	} | 
 | 7318 |  | 
 | 7319 | 	len = sizeof(params); | 
 | 7320 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 7321 | 		goto out; | 
 | 7322 |  | 
 | 7323 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7324 | 	if (!asoc) { | 
 | 7325 | 		retval = -EINVAL; | 
 | 7326 | 		goto out; | 
 | 7327 | 	} | 
 | 7328 |  | 
 | 7329 | 	retval = sctp_sched_get_value(asoc, params.stream_id, | 
 | 7330 | 				      ¶ms.stream_value); | 
 | 7331 | 	if (retval) | 
 | 7332 | 		goto out; | 
 | 7333 |  | 
 | 7334 | 	if (put_user(len, optlen)) { | 
 | 7335 | 		retval = -EFAULT; | 
 | 7336 | 		goto out; | 
 | 7337 | 	} | 
 | 7338 |  | 
 | 7339 | 	if (copy_to_user(optval, ¶ms, len)) { | 
 | 7340 | 		retval = -EFAULT; | 
 | 7341 | 		goto out; | 
 | 7342 | 	} | 
 | 7343 |  | 
 | 7344 | out: | 
 | 7345 | 	return retval; | 
 | 7346 | } | 
 | 7347 |  | 
| Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7348 | static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, | 
 | 7349 | 						  char __user *optval, | 
 | 7350 | 						  int __user *optlen) | 
 | 7351 | { | 
 | 7352 | 	struct sctp_assoc_value params; | 
 | 7353 | 	struct sctp_association *asoc; | 
 | 7354 | 	int retval = -EFAULT; | 
 | 7355 |  | 
 | 7356 | 	if (len < sizeof(params)) { | 
 | 7357 | 		retval = -EINVAL; | 
 | 7358 | 		goto out; | 
 | 7359 | 	} | 
 | 7360 |  | 
 | 7361 | 	len = sizeof(params); | 
 | 7362 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 7363 | 		goto out; | 
 | 7364 |  | 
 | 7365 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 7366 | 	if (asoc) { | 
 | 7367 | 		params.assoc_value = asoc->intl_enable; | 
 | 7368 | 	} else if (!params.assoc_id) { | 
 | 7369 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 7370 |  | 
 | 7371 | 		params.assoc_value = sp->strm_interleave; | 
 | 7372 | 	} else { | 
 | 7373 | 		retval = -EINVAL; | 
 | 7374 | 		goto out; | 
 | 7375 | 	} | 
 | 7376 |  | 
 | 7377 | 	if (put_user(len, optlen)) | 
 | 7378 | 		goto out; | 
 | 7379 |  | 
 | 7380 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 7381 | 		goto out; | 
 | 7382 |  | 
 | 7383 | 	retval = 0; | 
 | 7384 |  | 
 | 7385 | out: | 
 | 7386 | 	return retval; | 
 | 7387 | } | 
 | 7388 |  | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7389 | static int sctp_getsockopt_reuse_port(struct sock *sk, int len, | 
 | 7390 | 				      char __user *optval, | 
 | 7391 | 				      int __user *optlen) | 
 | 7392 | { | 
 | 7393 | 	int val; | 
 | 7394 |  | 
 | 7395 | 	if (len < sizeof(int)) | 
 | 7396 | 		return -EINVAL; | 
 | 7397 |  | 
 | 7398 | 	len = sizeof(int); | 
 | 7399 | 	val = sctp_sk(sk)->reuse; | 
 | 7400 | 	if (put_user(len, optlen)) | 
 | 7401 | 		return -EFAULT; | 
 | 7402 |  | 
 | 7403 | 	if (copy_to_user(optval, &val, len)) | 
 | 7404 | 		return -EFAULT; | 
 | 7405 |  | 
 | 7406 | 	return 0; | 
 | 7407 | } | 
 | 7408 |  | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 7409 | static int sctp_getsockopt(struct sock *sk, int level, int optname, | 
 | 7410 | 			   char __user *optval, int __user *optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | { | 
 | 7412 | 	int retval = 0; | 
 | 7413 | 	int len; | 
 | 7414 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7415 | 	pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7416 |  | 
 | 7417 | 	/* I can hardly begin to describe how wrong this is.  This is | 
 | 7418 | 	 * so broken as to be worse than useless.  The API draft | 
 | 7419 | 	 * REALLY is NOT helpful here...  I am not convinced that the | 
 | 7420 | 	 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP | 
 | 7421 | 	 * are at all well-founded. | 
 | 7422 | 	 */ | 
 | 7423 | 	if (level != SOL_SCTP) { | 
 | 7424 | 		struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 7425 |  | 
 | 7426 | 		retval = af->getsockopt(sk, level, optname, optval, optlen); | 
 | 7427 | 		return retval; | 
 | 7428 | 	} | 
 | 7429 |  | 
 | 7430 | 	if (get_user(len, optlen)) | 
 | 7431 | 		return -EFAULT; | 
 | 7432 |  | 
| Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 7433 | 	if (len < 0) | 
 | 7434 | 		return -EINVAL; | 
 | 7435 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7436 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 |  | 
 | 7438 | 	switch (optname) { | 
 | 7439 | 	case SCTP_STATUS: | 
 | 7440 | 		retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); | 
 | 7441 | 		break; | 
 | 7442 | 	case SCTP_DISABLE_FRAGMENTS: | 
 | 7443 | 		retval = sctp_getsockopt_disable_fragments(sk, len, optval, | 
 | 7444 | 							   optlen); | 
 | 7445 | 		break; | 
 | 7446 | 	case SCTP_EVENTS: | 
 | 7447 | 		retval = sctp_getsockopt_events(sk, len, optval, optlen); | 
 | 7448 | 		break; | 
 | 7449 | 	case SCTP_AUTOCLOSE: | 
 | 7450 | 		retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); | 
 | 7451 | 		break; | 
 | 7452 | 	case SCTP_SOCKOPT_PEELOFF: | 
 | 7453 | 		retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); | 
 | 7454 | 		break; | 
| Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 7455 | 	case SCTP_SOCKOPT_PEELOFF_FLAGS: | 
 | 7456 | 		retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); | 
 | 7457 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | 	case SCTP_PEER_ADDR_PARAMS: | 
 | 7459 | 		retval = sctp_getsockopt_peer_addr_params(sk, len, optval, | 
 | 7460 | 							  optlen); | 
 | 7461 | 		break; | 
| Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 7462 | 	case SCTP_DELAYED_SACK: | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 7463 | 		retval = sctp_getsockopt_delayed_ack(sk, len, optval, | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 7464 | 							  optlen); | 
 | 7465 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7466 | 	case SCTP_INITMSG: | 
 | 7467 | 		retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); | 
 | 7468 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | 	case SCTP_GET_PEER_ADDRS: | 
 | 7470 | 		retval = sctp_getsockopt_peer_addrs(sk, len, optval, | 
 | 7471 | 						    optlen); | 
 | 7472 | 		break; | 
 | 7473 | 	case SCTP_GET_LOCAL_ADDRS: | 
 | 7474 | 		retval = sctp_getsockopt_local_addrs(sk, len, optval, | 
 | 7475 | 						     optlen); | 
 | 7476 | 		break; | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 7477 | 	case SCTP_SOCKOPT_CONNECTX3: | 
 | 7478 | 		retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); | 
 | 7479 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | 	case SCTP_DEFAULT_SEND_PARAM: | 
 | 7481 | 		retval = sctp_getsockopt_default_send_param(sk, len, | 
 | 7482 | 							    optval, optlen); | 
 | 7483 | 		break; | 
| Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 7484 | 	case SCTP_DEFAULT_SNDINFO: | 
 | 7485 | 		retval = sctp_getsockopt_default_sndinfo(sk, len, | 
 | 7486 | 							 optval, optlen); | 
 | 7487 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | 	case SCTP_PRIMARY_ADDR: | 
 | 7489 | 		retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); | 
 | 7490 | 		break; | 
 | 7491 | 	case SCTP_NODELAY: | 
 | 7492 | 		retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); | 
 | 7493 | 		break; | 
 | 7494 | 	case SCTP_RTOINFO: | 
 | 7495 | 		retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); | 
 | 7496 | 		break; | 
 | 7497 | 	case SCTP_ASSOCINFO: | 
 | 7498 | 		retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); | 
 | 7499 | 		break; | 
 | 7500 | 	case SCTP_I_WANT_MAPPED_V4_ADDR: | 
 | 7501 | 		retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); | 
 | 7502 | 		break; | 
 | 7503 | 	case SCTP_MAXSEG: | 
 | 7504 | 		retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); | 
 | 7505 | 		break; | 
 | 7506 | 	case SCTP_GET_PEER_ADDR_INFO: | 
 | 7507 | 		retval = sctp_getsockopt_peer_addr_info(sk, len, optval, | 
 | 7508 | 							optlen); | 
 | 7509 | 		break; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 7510 | 	case SCTP_ADAPTATION_LAYER: | 
 | 7511 | 		retval = sctp_getsockopt_adaptation_layer(sk, len, optval, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | 							optlen); | 
 | 7513 | 		break; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 7514 | 	case SCTP_CONTEXT: | 
 | 7515 | 		retval = sctp_getsockopt_context(sk, len, optval, optlen); | 
 | 7516 | 		break; | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 7517 | 	case SCTP_FRAGMENT_INTERLEAVE: | 
 | 7518 | 		retval = sctp_getsockopt_fragment_interleave(sk, len, optval, | 
 | 7519 | 							     optlen); | 
 | 7520 | 		break; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 7521 | 	case SCTP_PARTIAL_DELIVERY_POINT: | 
 | 7522 | 		retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, | 
 | 7523 | 								optlen); | 
 | 7524 | 		break; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 7525 | 	case SCTP_MAX_BURST: | 
 | 7526 | 		retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); | 
 | 7527 | 		break; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7528 | 	case SCTP_AUTH_KEY: | 
 | 7529 | 	case SCTP_AUTH_CHUNK: | 
 | 7530 | 	case SCTP_AUTH_DELETE_KEY: | 
| Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 7531 | 	case SCTP_AUTH_DEACTIVATE_KEY: | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7532 | 		retval = -EOPNOTSUPP; | 
 | 7533 | 		break; | 
 | 7534 | 	case SCTP_HMAC_IDENT: | 
 | 7535 | 		retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); | 
 | 7536 | 		break; | 
 | 7537 | 	case SCTP_AUTH_ACTIVE_KEY: | 
 | 7538 | 		retval = sctp_getsockopt_active_key(sk, len, optval, optlen); | 
 | 7539 | 		break; | 
 | 7540 | 	case SCTP_PEER_AUTH_CHUNKS: | 
 | 7541 | 		retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, | 
 | 7542 | 							optlen); | 
 | 7543 | 		break; | 
 | 7544 | 	case SCTP_LOCAL_AUTH_CHUNKS: | 
 | 7545 | 		retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, | 
 | 7546 | 							optlen); | 
 | 7547 | 		break; | 
| Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 7548 | 	case SCTP_GET_ASSOC_NUMBER: | 
 | 7549 | 		retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); | 
 | 7550 | 		break; | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 7551 | 	case SCTP_GET_ASSOC_ID_LIST: | 
 | 7552 | 		retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); | 
 | 7553 | 		break; | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 7554 | 	case SCTP_AUTO_ASCONF: | 
 | 7555 | 		retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); | 
 | 7556 | 		break; | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7557 | 	case SCTP_PEER_ADDR_THLDS: | 
 | 7558 | 		retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen); | 
 | 7559 | 		break; | 
| Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 7560 | 	case SCTP_GET_ASSOC_STATS: | 
 | 7561 | 		retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); | 
 | 7562 | 		break; | 
| Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 7563 | 	case SCTP_RECVRCVINFO: | 
 | 7564 | 		retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); | 
 | 7565 | 		break; | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 7566 | 	case SCTP_RECVNXTINFO: | 
 | 7567 | 		retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); | 
 | 7568 | 		break; | 
| Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 7569 | 	case SCTP_PR_SUPPORTED: | 
 | 7570 | 		retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); | 
 | 7571 | 		break; | 
| Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7572 | 	case SCTP_DEFAULT_PRINFO: | 
 | 7573 | 		retval = sctp_getsockopt_default_prinfo(sk, len, optval, | 
 | 7574 | 							optlen); | 
 | 7575 | 		break; | 
| Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7576 | 	case SCTP_PR_ASSOC_STATUS: | 
 | 7577 | 		retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, | 
 | 7578 | 							optlen); | 
 | 7579 | 		break; | 
| Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7580 | 	case SCTP_PR_STREAM_STATUS: | 
 | 7581 | 		retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, | 
 | 7582 | 							 optlen); | 
 | 7583 | 		break; | 
| Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7584 | 	case SCTP_RECONFIG_SUPPORTED: | 
 | 7585 | 		retval = sctp_getsockopt_reconfig_supported(sk, len, optval, | 
 | 7586 | 							    optlen); | 
 | 7587 | 		break; | 
| Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7588 | 	case SCTP_ENABLE_STREAM_RESET: | 
 | 7589 | 		retval = sctp_getsockopt_enable_strreset(sk, len, optval, | 
 | 7590 | 							 optlen); | 
 | 7591 | 		break; | 
| Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7592 | 	case SCTP_STREAM_SCHEDULER: | 
 | 7593 | 		retval = sctp_getsockopt_scheduler(sk, len, optval, | 
 | 7594 | 						   optlen); | 
 | 7595 | 		break; | 
| Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 7596 | 	case SCTP_STREAM_SCHEDULER_VALUE: | 
 | 7597 | 		retval = sctp_getsockopt_scheduler_value(sk, len, optval, | 
 | 7598 | 							 optlen); | 
 | 7599 | 		break; | 
| Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7600 | 	case SCTP_INTERLEAVING_SUPPORTED: | 
 | 7601 | 		retval = sctp_getsockopt_interleaving_supported(sk, len, optval, | 
 | 7602 | 								optlen); | 
 | 7603 | 		break; | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7604 | 	case SCTP_REUSE_PORT: | 
 | 7605 | 		retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen); | 
 | 7606 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7607 | 	default: | 
 | 7608 | 		retval = -ENOPROTOOPT; | 
 | 7609 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 7610 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7611 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7612 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7613 | 	return retval; | 
 | 7614 | } | 
 | 7615 |  | 
| Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 7616 | static int sctp_hash(struct sock *sk) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | { | 
 | 7618 | 	/* STUB */ | 
| Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 7619 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7620 | } | 
 | 7621 |  | 
 | 7622 | static void sctp_unhash(struct sock *sk) | 
 | 7623 | { | 
 | 7624 | 	/* STUB */ | 
 | 7625 | } | 
 | 7626 |  | 
 | 7627 | /* Check if port is acceptable.  Possibly find first available port. | 
 | 7628 |  * | 
 | 7629 |  * The port hash table (contained in the 'global' SCTP protocol storage | 
 | 7630 |  * returned by struct sctp_protocol *sctp_get_protocol()). The hash | 
 | 7631 |  * table is an array of 4096 lists (sctp_bind_hashbucket). Each | 
 | 7632 |  * list (the list number is the port number hashed out, so as you | 
 | 7633 |  * would expect from a hash function, all the ports in a given list have | 
 | 7634 |  * such a number that hashes out to the same list number; you were | 
 | 7635 |  * expecting that, right?); so each list has a set of ports, with a | 
 | 7636 |  * link to the socket (struct sock) that uses it, the port number and | 
 | 7637 |  * a fastreuse flag (FIXME: NPI ipg). | 
 | 7638 |  */ | 
 | 7639 | static struct sctp_bind_bucket *sctp_bucket_create( | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7640 | 	struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7641 |  | 
 | 7642 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) | 
 | 7643 | { | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7644 | 	bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7645 | 	struct sctp_bind_hashbucket *head; /* hash list */ | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7646 | 	struct sctp_bind_bucket *pp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7647 | 	unsigned short snum; | 
 | 7648 | 	int ret; | 
 | 7649 |  | 
| Al Viro | 04afd8b | 2006-11-20 17:02:01 -0800 | [diff] [blame] | 7650 | 	snum = ntohs(addr->v4.sin_port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7651 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7652 | 	pr_debug("%s: begins, snum:%d\n", __func__, snum); | 
 | 7653 |  | 
| wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7654 | 	local_bh_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7655 |  | 
 | 7656 | 	if (snum == 0) { | 
| Stephen Hemminger | 0639300 | 2007-10-10 17:30:18 -0700 | [diff] [blame] | 7657 | 		/* Search for an available port. */ | 
| Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7658 | 		int low, high, remaining, index; | 
 | 7659 | 		unsigned int rover; | 
| WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7660 | 		struct net *net = sock_net(sk); | 
| Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7661 |  | 
| WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7662 | 		inet_get_local_port_range(net, &low, &high); | 
| Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7663 | 		remaining = (high - low) + 1; | 
| Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 7664 | 		rover = prandom_u32() % remaining + low; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7665 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7666 | 		do { | 
 | 7667 | 			rover++; | 
 | 7668 | 			if ((rover < low) || (rover > high)) | 
 | 7669 | 				rover = low; | 
| WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7670 | 			if (inet_is_local_reserved_port(net, rover)) | 
| Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 7671 | 				continue; | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7672 | 			index = sctp_phashfn(sock_net(sk), rover); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | 			head = &sctp_port_hashtable[index]; | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7674 | 			spin_lock(&head->lock); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7675 | 			sctp_for_each_hentry(pp, &head->chain) | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7676 | 				if ((pp->port == rover) && | 
 | 7677 | 				    net_eq(sock_net(sk), pp->net)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7678 | 					goto next; | 
 | 7679 | 			break; | 
 | 7680 | 		next: | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7681 | 			spin_unlock(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | 		} while (--remaining > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 |  | 
 | 7684 | 		/* Exhausted local port range during search? */ | 
 | 7685 | 		ret = 1; | 
 | 7686 | 		if (remaining <= 0) | 
 | 7687 | 			goto fail; | 
 | 7688 |  | 
 | 7689 | 		/* OK, here is the one we will use.  HEAD (the port | 
 | 7690 | 		 * hash table list entry) is non-NULL and we hold it's | 
 | 7691 | 		 * mutex. | 
 | 7692 | 		 */ | 
 | 7693 | 		snum = rover; | 
 | 7694 | 	} else { | 
 | 7695 | 		/* We are given an specific port number; we verify | 
 | 7696 | 		 * that it is not being used. If it is used, we will | 
 | 7697 | 		 * exahust the search in the hash list corresponding | 
 | 7698 | 		 * to the port number (snum) - we detect that with the | 
 | 7699 | 		 * port iterator, pp being NULL. | 
 | 7700 | 		 */ | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7701 | 		head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)]; | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7702 | 		spin_lock(&head->lock); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7703 | 		sctp_for_each_hentry(pp, &head->chain) { | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7704 | 			if ((pp->port == snum) && net_eq(pp->net, sock_net(sk))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7705 | 				goto pp_found; | 
 | 7706 | 		} | 
 | 7707 | 	} | 
 | 7708 | 	pp = NULL; | 
 | 7709 | 	goto pp_not_found; | 
 | 7710 | pp_found: | 
 | 7711 | 	if (!hlist_empty(&pp->owner)) { | 
 | 7712 | 		/* We had a port hash table hit - there is an | 
 | 7713 | 		 * available port (pp != NULL) and it is being | 
 | 7714 | 		 * used by other socket (pp->owner not empty); that other | 
 | 7715 | 		 * socket is going to be sk2. | 
 | 7716 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7717 | 		struct sock *sk2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7719 | 		pr_debug("%s: found a possible match\n", __func__); | 
 | 7720 |  | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7721 | 		if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | 			goto success; | 
 | 7723 |  | 
 | 7724 | 		/* Run through the list of sockets bound to the port | 
 | 7725 | 		 * (pp->port) [via the pointers bind_next and | 
 | 7726 | 		 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, | 
 | 7727 | 		 * we get the endpoint they describe and run through | 
 | 7728 | 		 * the endpoint's list of IP (v4 or v6) addresses, | 
 | 7729 | 		 * comparing each of the addresses with the address of | 
 | 7730 | 		 * the socket sk. If we find a match, then that means | 
 | 7731 | 		 * that this port/socket (sk) combination are already | 
 | 7732 | 		 * in an endpoint. | 
 | 7733 | 		 */ | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7734 | 		sk_for_each_bound(sk2, &pp->owner) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7735 | 			struct sctp_endpoint *ep2; | 
 | 7736 | 			ep2 = sctp_sk(sk2)->ep; | 
 | 7737 |  | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 7738 | 			if (sk == sk2 || | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7739 | 			    (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) && | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 7740 | 			     sk2->sk_state != SCTP_SS_LISTENING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7741 | 				continue; | 
 | 7742 |  | 
| Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 7743 | 			if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, | 
 | 7744 | 						 sctp_sk(sk2), sctp_sk(sk))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | 				ret = (long)sk2; | 
 | 7746 | 				goto fail_unlock; | 
 | 7747 | 			} | 
 | 7748 | 		} | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7749 |  | 
 | 7750 | 		pr_debug("%s: found a match\n", __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7751 | 	} | 
 | 7752 | pp_not_found: | 
 | 7753 | 	/* If there was a hash table miss, create a new port.  */ | 
 | 7754 | 	ret = 1; | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7755 | 	if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7756 | 		goto fail_unlock; | 
 | 7757 |  | 
 | 7758 | 	/* In either case (hit or miss), make sure fastreuse is 1 only | 
 | 7759 | 	 * if sk->sk_reuse is too (that is, if the caller requested | 
 | 7760 | 	 * SO_REUSEADDR on this socket -sk-). | 
 | 7761 | 	 */ | 
| Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 7762 | 	if (hlist_empty(&pp->owner)) { | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7763 | 		if (reuse && sk->sk_state != SCTP_SS_LISTENING) | 
| Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 7764 | 			pp->fastreuse = 1; | 
 | 7765 | 		else | 
 | 7766 | 			pp->fastreuse = 0; | 
 | 7767 | 	} else if (pp->fastreuse && | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7768 | 		   (!reuse || sk->sk_state == SCTP_SS_LISTENING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7769 | 		pp->fastreuse = 0; | 
 | 7770 |  | 
 | 7771 | 	/* We are set, so fill up all the data in the hash table | 
 | 7772 | 	 * entry, tie the socket list information with the rest of the | 
 | 7773 | 	 * sockets FIXME: Blurry, NPI (ipg). | 
 | 7774 | 	 */ | 
 | 7775 | success: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7776 | 	if (!sctp_sk(sk)->bind_hash) { | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7777 | 		inet_sk(sk)->inet_num = snum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | 		sk_add_bind_node(sk, &pp->owner); | 
 | 7779 | 		sctp_sk(sk)->bind_hash = pp; | 
 | 7780 | 	} | 
 | 7781 | 	ret = 0; | 
 | 7782 |  | 
 | 7783 | fail_unlock: | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7784 | 	spin_unlock(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7785 |  | 
 | 7786 | fail: | 
| wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7787 | 	local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7788 | 	return ret; | 
 | 7789 | } | 
 | 7790 |  | 
 | 7791 | /* Assign a 'snum' port to the socket.  If snum == 0, an ephemeral | 
 | 7792 |  * port is requested. | 
 | 7793 |  */ | 
 | 7794 | static int sctp_get_port(struct sock *sk, unsigned short snum) | 
 | 7795 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7796 | 	union sctp_addr addr; | 
 | 7797 | 	struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 7798 |  | 
 | 7799 | 	/* Set up a dummy address struct from the sk. */ | 
 | 7800 | 	af->from_sk(&addr, sk); | 
 | 7801 | 	addr.v4.sin_port = htons(snum); | 
 | 7802 |  | 
 | 7803 | 	/* Note: sk->sk_num gets filled in if ephemeral port request. */ | 
| Daniel Borkmann | 62208f1 | 2013-06-25 18:17:30 +0200 | [diff] [blame] | 7804 | 	return !!sctp_get_port_local(sk, &addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7805 | } | 
 | 7806 |  | 
 | 7807 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 |  *  Move a socket to LISTENING state. | 
 | 7809 |  */ | 
| Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 7810 | static int sctp_listen_start(struct sock *sk, int backlog) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7811 | { | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7812 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 7813 | 	struct sctp_endpoint *ep = sp->ep; | 
| Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 7814 | 	struct crypto_shash *tfm = NULL; | 
| Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7815 | 	char alg[32]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7816 |  | 
 | 7817 | 	/* Allocate HMAC for generating cookie. */ | 
| Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7818 | 	if (!sp->hmac && sp->sctp_hmac_alg) { | 
 | 7819 | 		sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg); | 
| Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 7820 | 		tfm = crypto_alloc_shash(alg, 0, 0); | 
| Vlad Yasevich | 8dc4984 | 2007-05-09 13:50:20 -0700 | [diff] [blame] | 7821 | 		if (IS_ERR(tfm)) { | 
| Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 7822 | 			net_info_ratelimited("failed to load transform for %s: %ld\n", | 
| Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7823 | 					     sp->sctp_hmac_alg, PTR_ERR(tfm)); | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7824 | 			return -ENOSYS; | 
 | 7825 | 		} | 
 | 7826 | 		sctp_sk(sk)->hmac = tfm; | 
 | 7827 | 	} | 
 | 7828 |  | 
 | 7829 | 	/* | 
 | 7830 | 	 * If a bind() or sctp_bindx() is not called prior to a listen() | 
 | 7831 | 	 * call that allows new associations to be accepted, the system | 
 | 7832 | 	 * picks an ephemeral port and will choose an address set equivalent | 
 | 7833 | 	 * to binding with a wildcard address. | 
 | 7834 | 	 * | 
 | 7835 | 	 * This is not currently spelled out in the SCTP sockets | 
 | 7836 | 	 * extensions draft, but follows the practice as seen in TCP | 
 | 7837 | 	 * sockets. | 
 | 7838 | 	 * | 
 | 7839 | 	 */ | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 7840 | 	inet_sk_set_state(sk, SCTP_SS_LISTENING); | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7841 | 	if (!ep->base.bind_addr.port) { | 
 | 7842 | 		if (sctp_autobind(sk)) | 
 | 7843 | 			return -EAGAIN; | 
 | 7844 | 	} else { | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7845 | 		if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 7846 | 			inet_sk_set_state(sk, SCTP_SS_CLOSED); | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7847 | 			return -EADDRINUSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7848 | 		} | 
 | 7849 | 	} | 
 | 7850 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7851 | 	sk->sk_max_ack_backlog = backlog; | 
 | 7852 | 	sctp_hash_endpoint(ep); | 
 | 7853 | 	return 0; | 
 | 7854 | } | 
 | 7855 |  | 
 | 7856 | /* | 
 | 7857 |  * 4.1.3 / 5.1.3 listen() | 
 | 7858 |  * | 
 | 7859 |  *   By default, new associations are not accepted for UDP style sockets. | 
 | 7860 |  *   An application uses listen() to mark a socket as being able to | 
 | 7861 |  *   accept new associations. | 
 | 7862 |  * | 
 | 7863 |  *   On TCP style sockets, applications use listen() to ready the SCTP | 
 | 7864 |  *   endpoint for accepting inbound associations. | 
 | 7865 |  * | 
 | 7866 |  *   On both types of endpoints a backlog of '0' disables listening. | 
 | 7867 |  * | 
 | 7868 |  *  Move a socket to LISTENING state. | 
 | 7869 |  */ | 
 | 7870 | int sctp_inet_listen(struct socket *sock, int backlog) | 
 | 7871 | { | 
 | 7872 | 	struct sock *sk = sock->sk; | 
 | 7873 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
 | 7874 | 	int err = -EINVAL; | 
 | 7875 |  | 
 | 7876 | 	if (unlikely(backlog < 0)) | 
 | 7877 | 		return err; | 
 | 7878 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7879 | 	lock_sock(sk); | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7880 |  | 
 | 7881 | 	/* Peeled-off sockets are not allowed to listen().  */ | 
 | 7882 | 	if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) | 
 | 7883 | 		goto out; | 
 | 7884 |  | 
 | 7885 | 	if (sock->state != SS_UNCONNECTED) | 
 | 7886 | 		goto out; | 
 | 7887 |  | 
| Xin Long | 34b2789 | 2017-04-06 13:10:52 +0800 | [diff] [blame] | 7888 | 	if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) | 
 | 7889 | 		goto out; | 
 | 7890 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7891 | 	/* If backlog is zero, disable listening. */ | 
 | 7892 | 	if (!backlog) { | 
 | 7893 | 		if (sctp_sstate(sk, CLOSED)) | 
 | 7894 | 			goto out; | 
 | 7895 |  | 
 | 7896 | 		err = 0; | 
 | 7897 | 		sctp_unhash_endpoint(ep); | 
 | 7898 | 		sk->sk_state = SCTP_SS_CLOSED; | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7899 | 		if (sk->sk_reuse || sctp_sk(sk)->reuse) | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7900 | 			sctp_sk(sk)->bind_hash->fastreuse = 1; | 
 | 7901 | 		goto out; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 7902 | 	} | 
 | 7903 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7904 | 	/* If we are already listening, just update the backlog */ | 
 | 7905 | 	if (sctp_sstate(sk, LISTENING)) | 
 | 7906 | 		sk->sk_max_ack_backlog = backlog; | 
 | 7907 | 	else { | 
 | 7908 | 		err = sctp_listen_start(sk, backlog); | 
 | 7909 | 		if (err) | 
 | 7910 | 			goto out; | 
 | 7911 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7912 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7913 | 	err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7914 | out: | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7915 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7916 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | } | 
 | 7918 |  | 
 | 7919 | /* | 
 | 7920 |  * This function is done by modeling the current datagram_poll() and the | 
 | 7921 |  * tcp_poll().  Note that, based on these implementations, we don't | 
 | 7922 |  * lock the socket in this function, even though it seems that, | 
 | 7923 |  * ideally, locking or some other mechanisms can be used to ensure | 
| Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 7924 |  * the integrity of the counters (sndbuf and wmem_alloc) used | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 |  * in this place.  We assume that we don't need locks either until proven | 
 | 7926 |  * otherwise. | 
 | 7927 |  * | 
 | 7928 |  * Another thing to note is that we include the Async I/O support | 
 | 7929 |  * here, again, by modeling the current TCP/UDP code.  We don't have | 
 | 7930 |  * a good way to test with it yet. | 
 | 7931 |  */ | 
| Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 7932 | __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] | 7933 | { | 
 | 7934 | 	struct sock *sk = sock->sk; | 
 | 7935 | 	struct sctp_sock *sp = sctp_sk(sk); | 
| Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 7936 | 	__poll_t mask; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7937 |  | 
| Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 7938 | 	poll_wait(file, sk_sleep(sk), wait); | 
 | 7939 |  | 
| Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 7940 | 	sock_rps_record_flow(sk); | 
 | 7941 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7942 | 	/* A TCP-style listening socket becomes readable when the accept queue | 
 | 7943 | 	 * is not empty. | 
 | 7944 | 	 */ | 
 | 7945 | 	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | 
 | 7946 | 		return (!list_empty(&sp->ep->asocs)) ? | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7947 | 			(EPOLLIN | EPOLLRDNORM) : 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7948 |  | 
 | 7949 | 	mask = 0; | 
 | 7950 |  | 
 | 7951 | 	/* Is there any exceptional events?  */ | 
 | 7952 | 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7953 | 		mask |= EPOLLERR | | 
 | 7954 | 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); | 
| Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 7955 | 	if (sk->sk_shutdown & RCV_SHUTDOWN) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7956 | 		mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7957 | 	if (sk->sk_shutdown == SHUTDOWN_MASK) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7958 | 		mask |= EPOLLHUP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7959 |  | 
 | 7960 | 	/* Is it readable?  Reconsider this code with TCP-style support.  */ | 
| Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 7961 | 	if (!skb_queue_empty(&sk->sk_receive_queue)) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7962 | 		mask |= EPOLLIN | EPOLLRDNORM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7963 |  | 
 | 7964 | 	/* The association is either gone or not ready.  */ | 
 | 7965 | 	if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) | 
 | 7966 | 		return mask; | 
 | 7967 |  | 
 | 7968 | 	/* Is it writable?  */ | 
 | 7969 | 	if (sctp_writeable(sk)) { | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7970 | 		mask |= EPOLLOUT | EPOLLWRNORM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7971 | 	} else { | 
| Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 7972 | 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7973 | 		/* | 
 | 7974 | 		 * Since the socket is not locked, the buffer | 
 | 7975 | 		 * might be made available after the writeable check and | 
 | 7976 | 		 * before the bit is set.  This could cause a lost I/O | 
 | 7977 | 		 * signal.  tcp_poll() has a race breaker for this race | 
 | 7978 | 		 * condition.  Based on their implementation, we put | 
 | 7979 | 		 * in the following code to cover it as well. | 
 | 7980 | 		 */ | 
 | 7981 | 		if (sctp_writeable(sk)) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 7982 | 			mask |= EPOLLOUT | EPOLLWRNORM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7983 | 	} | 
 | 7984 | 	return mask; | 
 | 7985 | } | 
 | 7986 |  | 
 | 7987 | /******************************************************************** | 
 | 7988 |  * 2nd Level Abstractions | 
 | 7989 |  ********************************************************************/ | 
 | 7990 |  | 
 | 7991 | static struct sctp_bind_bucket *sctp_bucket_create( | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7992 | 	struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | { | 
 | 7994 | 	struct sctp_bind_bucket *pp; | 
 | 7995 |  | 
| Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 7996 | 	pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7997 | 	if (pp) { | 
| Li Zefan | 935a7f6 | 2008-04-10 01:58:06 -0700 | [diff] [blame] | 7998 | 		SCTP_DBG_OBJCNT_INC(bind_bucket); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7999 | 		pp->port = snum; | 
 | 8000 | 		pp->fastreuse = 0; | 
 | 8001 | 		INIT_HLIST_HEAD(&pp->owner); | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8002 | 		pp->net = net; | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 8003 | 		hlist_add_head(&pp->node, &head->chain); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8004 | 	} | 
 | 8005 | 	return pp; | 
 | 8006 | } | 
 | 8007 |  | 
 | 8008 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ | 
 | 8009 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) | 
 | 8010 | { | 
| Sridhar Samudrala | 37fa687 | 2006-07-21 14:45:47 -0700 | [diff] [blame] | 8011 | 	if (pp && hlist_empty(&pp->owner)) { | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 8012 | 		__hlist_del(&pp->node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8013 | 		kmem_cache_free(sctp_bucket_cachep, pp); | 
 | 8014 | 		SCTP_DBG_OBJCNT_DEC(bind_bucket); | 
 | 8015 | 	} | 
 | 8016 | } | 
 | 8017 |  | 
 | 8018 | /* Release this socket's reference to a local port.  */ | 
 | 8019 | static inline void __sctp_put_port(struct sock *sk) | 
 | 8020 | { | 
 | 8021 | 	struct sctp_bind_hashbucket *head = | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8022 | 		&sctp_port_hashtable[sctp_phashfn(sock_net(sk), | 
 | 8023 | 						  inet_sk(sk)->inet_num)]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8024 | 	struct sctp_bind_bucket *pp; | 
 | 8025 |  | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8026 | 	spin_lock(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8027 | 	pp = sctp_sk(sk)->bind_hash; | 
 | 8028 | 	__sk_del_bind_node(sk); | 
 | 8029 | 	sctp_sk(sk)->bind_hash = NULL; | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8030 | 	inet_sk(sk)->inet_num = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8031 | 	sctp_bucket_destroy(pp); | 
| wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8032 | 	spin_unlock(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8033 | } | 
 | 8034 |  | 
 | 8035 | void sctp_put_port(struct sock *sk) | 
 | 8036 | { | 
| wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8037 | 	local_bh_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8038 | 	__sctp_put_port(sk); | 
| wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8039 | 	local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8040 | } | 
 | 8041 |  | 
 | 8042 | /* | 
 | 8043 |  * The system picks an ephemeral port and choose an address set equivalent | 
 | 8044 |  * to binding with a wildcard address. | 
 | 8045 |  * One of those addresses will be the primary address for the association. | 
 | 8046 |  * This automatically enables the multihoming capability of SCTP. | 
 | 8047 |  */ | 
 | 8048 | static int sctp_autobind(struct sock *sk) | 
 | 8049 | { | 
 | 8050 | 	union sctp_addr autoaddr; | 
 | 8051 | 	struct sctp_af *af; | 
| Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 8052 | 	__be16 port; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8053 |  | 
 | 8054 | 	/* Initialize a local sockaddr structure to INADDR_ANY. */ | 
 | 8055 | 	af = sctp_sk(sk)->pf->af; | 
 | 8056 |  | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8057 | 	port = htons(inet_sk(sk)->inet_num); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8058 | 	af->inaddr_any(&autoaddr, port); | 
 | 8059 |  | 
 | 8060 | 	return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); | 
 | 8061 | } | 
 | 8062 |  | 
 | 8063 | /* Parse out IPPROTO_SCTP CMSG headers.  Perform only minimal validation. | 
 | 8064 |  * | 
 | 8065 |  * From RFC 2292 | 
 | 8066 |  * 4.2 The cmsghdr Structure * | 
 | 8067 |  * | 
 | 8068 |  * When ancillary data is sent or received, any number of ancillary data | 
 | 8069 |  * objects can be specified by the msg_control and msg_controllen members of | 
 | 8070 |  * the msghdr structure, because each object is preceded by | 
 | 8071 |  * a cmsghdr structure defining the object's length (the cmsg_len member). | 
 | 8072 |  * Historically Berkeley-derived implementations have passed only one object | 
 | 8073 |  * at a time, but this API allows multiple objects to be | 
 | 8074 |  * passed in a single call to sendmsg() or recvmsg(). The following example | 
 | 8075 |  * shows two ancillary data objects in a control buffer. | 
 | 8076 |  * | 
 | 8077 |  *   |<--------------------------- msg_controllen -------------------------->| | 
 | 8078 |  *   |                                                                       | | 
 | 8079 |  * | 
 | 8080 |  *   |<----- ancillary data object ----->|<----- ancillary data object ----->| | 
 | 8081 |  * | 
 | 8082 |  *   |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| | 
 | 8083 |  *   |                                   |                                   | | 
 | 8084 |  * | 
 | 8085 |  *   |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  | | 
 | 8086 |  * | 
 | 8087 |  *   |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  | | 
 | 8088 |  *   |                                |  |                                |  | | 
 | 8089 |  * | 
 | 8090 |  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 
 | 8091 |  *   |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX| | 
 | 8092 |  * | 
 | 8093 |  *   |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX| | 
 | 8094 |  * | 
 | 8095 |  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 
 | 8096 |  *    ^ | 
 | 8097 |  *    | | 
 | 8098 |  * | 
 | 8099 |  * msg_control | 
 | 8100 |  * points here | 
 | 8101 |  */ | 
| Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 8102 | 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] | 8103 | { | 
| Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 8104 | 	struct msghdr *my_msg = (struct msghdr *)msg; | 
| Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 8105 | 	struct cmsghdr *cmsg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8106 |  | 
| Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 8107 | 	for_each_cmsghdr(cmsg, my_msg) { | 
| Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 8108 | 		if (!CMSG_OK(my_msg, cmsg)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8109 | 			return -EINVAL; | 
 | 8110 |  | 
 | 8111 | 		/* Should we parse this header or ignore?  */ | 
 | 8112 | 		if (cmsg->cmsg_level != IPPROTO_SCTP) | 
 | 8113 | 			continue; | 
 | 8114 |  | 
 | 8115 | 		/* Strictly check lengths following example in SCM code.  */ | 
 | 8116 | 		switch (cmsg->cmsg_type) { | 
 | 8117 | 		case SCTP_INIT: | 
 | 8118 | 			/* SCTP Socket API Extension | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8119 | 			 * 5.3.1 SCTP Initiation Structure (SCTP_INIT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8120 | 			 * | 
 | 8121 | 			 * This cmsghdr structure provides information for | 
 | 8122 | 			 * initializing new SCTP associations with sendmsg(). | 
 | 8123 | 			 * The SCTP_INITMSG socket option uses this same data | 
 | 8124 | 			 * structure.  This structure is not used for | 
 | 8125 | 			 * recvmsg(). | 
 | 8126 | 			 * | 
 | 8127 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 8128 | 			 * ------------  ------------   ---------------------- | 
 | 8129 | 			 * IPPROTO_SCTP  SCTP_INIT      struct sctp_initmsg | 
 | 8130 | 			 */ | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8131 | 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | 				return -EINVAL; | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8133 |  | 
 | 8134 | 			cmsgs->init = CMSG_DATA(cmsg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8135 | 			break; | 
 | 8136 |  | 
 | 8137 | 		case SCTP_SNDRCV: | 
 | 8138 | 			/* SCTP Socket API Extension | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8139 | 			 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8140 | 			 * | 
 | 8141 | 			 * This cmsghdr structure specifies SCTP options for | 
 | 8142 | 			 * sendmsg() and describes SCTP header information | 
 | 8143 | 			 * about a received message through recvmsg(). | 
 | 8144 | 			 * | 
 | 8145 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 8146 | 			 * ------------  ------------   ---------------------- | 
 | 8147 | 			 * IPPROTO_SCTP  SCTP_SNDRCV    struct sctp_sndrcvinfo | 
 | 8148 | 			 */ | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8149 | 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8150 | 				return -EINVAL; | 
 | 8151 |  | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8152 | 			cmsgs->srinfo = CMSG_DATA(cmsg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8153 |  | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8154 | 			if (cmsgs->srinfo->sinfo_flags & | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 8155 | 			    ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 
| Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 8156 | 			      SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | | 
 | 8157 | 			      SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8158 | 				return -EINVAL; | 
 | 8159 | 			break; | 
 | 8160 |  | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8161 | 		case SCTP_SNDINFO: | 
 | 8162 | 			/* SCTP Socket API Extension | 
 | 8163 | 			 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) | 
 | 8164 | 			 * | 
 | 8165 | 			 * This cmsghdr structure specifies SCTP options for | 
 | 8166 | 			 * sendmsg(). This structure and SCTP_RCVINFO replaces | 
 | 8167 | 			 * SCTP_SNDRCV which has been deprecated. | 
 | 8168 | 			 * | 
 | 8169 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 8170 | 			 * ------------  ------------   --------------------- | 
 | 8171 | 			 * IPPROTO_SCTP  SCTP_SNDINFO    struct sctp_sndinfo | 
 | 8172 | 			 */ | 
 | 8173 | 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) | 
 | 8174 | 				return -EINVAL; | 
 | 8175 |  | 
 | 8176 | 			cmsgs->sinfo = CMSG_DATA(cmsg); | 
 | 8177 |  | 
 | 8178 | 			if (cmsgs->sinfo->snd_flags & | 
 | 8179 | 			    ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 
| Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 8180 | 			      SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | | 
 | 8181 | 			      SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8182 | 				return -EINVAL; | 
 | 8183 | 			break; | 
| Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 8184 | 		case SCTP_PRINFO: | 
 | 8185 | 			/* SCTP Socket API Extension | 
 | 8186 | 			 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO) | 
 | 8187 | 			 * | 
 | 8188 | 			 * This cmsghdr structure specifies SCTP options for sendmsg(). | 
 | 8189 | 			 * | 
 | 8190 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 8191 | 			 * ------------  ------------   --------------------- | 
 | 8192 | 			 * IPPROTO_SCTP  SCTP_PRINFO    struct sctp_prinfo | 
 | 8193 | 			 */ | 
 | 8194 | 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo))) | 
 | 8195 | 				return -EINVAL; | 
 | 8196 |  | 
 | 8197 | 			cmsgs->prinfo = CMSG_DATA(cmsg); | 
 | 8198 | 			if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK) | 
 | 8199 | 				return -EINVAL; | 
 | 8200 |  | 
 | 8201 | 			if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE) | 
 | 8202 | 				cmsgs->prinfo->pr_value = 0; | 
 | 8203 | 			break; | 
| Xin Long | 3ff547c | 2018-03-14 19:05:31 +0800 | [diff] [blame] | 8204 | 		case SCTP_AUTHINFO: | 
 | 8205 | 			/* SCTP Socket API Extension | 
 | 8206 | 			 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO) | 
 | 8207 | 			 * | 
 | 8208 | 			 * This cmsghdr structure specifies SCTP options for sendmsg(). | 
 | 8209 | 			 * | 
 | 8210 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 8211 | 			 * ------------  ------------   --------------------- | 
 | 8212 | 			 * IPPROTO_SCTP  SCTP_AUTHINFO  struct sctp_authinfo | 
 | 8213 | 			 */ | 
 | 8214 | 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo))) | 
 | 8215 | 				return -EINVAL; | 
 | 8216 |  | 
 | 8217 | 			cmsgs->authinfo = CMSG_DATA(cmsg); | 
 | 8218 | 			break; | 
| Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 8219 | 		case SCTP_DSTADDRV4: | 
 | 8220 | 		case SCTP_DSTADDRV6: | 
 | 8221 | 			/* SCTP Socket API Extension | 
 | 8222 | 			 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6) | 
 | 8223 | 			 * | 
 | 8224 | 			 * This cmsghdr structure specifies SCTP options for sendmsg(). | 
 | 8225 | 			 * | 
 | 8226 | 			 * cmsg_level    cmsg_type         cmsg_data[] | 
 | 8227 | 			 * ------------  ------------   --------------------- | 
 | 8228 | 			 * IPPROTO_SCTP  SCTP_DSTADDRV4 struct in_addr | 
 | 8229 | 			 * ------------  ------------   --------------------- | 
 | 8230 | 			 * IPPROTO_SCTP  SCTP_DSTADDRV6 struct in6_addr | 
 | 8231 | 			 */ | 
 | 8232 | 			cmsgs->addrs_msg = my_msg; | 
 | 8233 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8234 | 		default: | 
 | 8235 | 			return -EINVAL; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 8236 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8237 | 	} | 
| Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8238 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | 	return 0; | 
 | 8240 | } | 
 | 8241 |  | 
 | 8242 | /* | 
 | 8243 |  * Wait for a packet.. | 
 | 8244 |  * Note: This function is the same function as in core/datagram.c | 
 | 8245 |  * with a few modifications to make lksctp work. | 
 | 8246 |  */ | 
| wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 8247 | 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] | 8248 | { | 
 | 8249 | 	int error; | 
 | 8250 | 	DEFINE_WAIT(wait); | 
 | 8251 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8252 | 	prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 |  | 
 | 8254 | 	/* Socket errors? */ | 
 | 8255 | 	error = sock_error(sk); | 
 | 8256 | 	if (error) | 
 | 8257 | 		goto out; | 
 | 8258 |  | 
 | 8259 | 	if (!skb_queue_empty(&sk->sk_receive_queue)) | 
 | 8260 | 		goto ready; | 
 | 8261 |  | 
 | 8262 | 	/* Socket shut down?  */ | 
 | 8263 | 	if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 8264 | 		goto out; | 
 | 8265 |  | 
 | 8266 | 	/* Sequenced packets can come disconnected.  If so we report the | 
 | 8267 | 	 * problem. | 
 | 8268 | 	 */ | 
 | 8269 | 	error = -ENOTCONN; | 
 | 8270 |  | 
 | 8271 | 	/* Is there a good reason to think that we may receive some data?  */ | 
 | 8272 | 	if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) | 
 | 8273 | 		goto out; | 
 | 8274 |  | 
 | 8275 | 	/* Handle signals.  */ | 
 | 8276 | 	if (signal_pending(current)) | 
 | 8277 | 		goto interrupted; | 
 | 8278 |  | 
 | 8279 | 	/* Let another process have a go.  Since we are going to sleep | 
 | 8280 | 	 * anyway.  Note: This may cause odd behaviors if the message | 
 | 8281 | 	 * does not fit in the user's buffer, but this seems to be the | 
 | 8282 | 	 * only way to honor MSG_DONTWAIT realistically. | 
 | 8283 | 	 */ | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8284 | 	release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8285 | 	*timeo_p = schedule_timeout(*timeo_p); | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8286 | 	lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8287 |  | 
 | 8288 | ready: | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8289 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8290 | 	return 0; | 
 | 8291 |  | 
 | 8292 | interrupted: | 
 | 8293 | 	error = sock_intr_errno(*timeo_p); | 
 | 8294 |  | 
 | 8295 | out: | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8296 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8297 | 	*err = error; | 
 | 8298 | 	return error; | 
 | 8299 | } | 
 | 8300 |  | 
 | 8301 | /* Receive a datagram. | 
 | 8302 |  * Note: This is pretty much the same routine as in core/datagram.c | 
 | 8303 |  * with a few changes to make lksctp work. | 
 | 8304 |  */ | 
| Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 8305 | struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, | 
 | 8306 | 				       int noblock, int *err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8307 | { | 
 | 8308 | 	int error; | 
 | 8309 | 	struct sk_buff *skb; | 
 | 8310 | 	long timeo; | 
 | 8311 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8312 | 	timeo = sock_rcvtimeo(sk, noblock); | 
 | 8313 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8314 | 	pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, | 
 | 8315 | 		 MAX_SCHEDULE_TIMEOUT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8316 |  | 
 | 8317 | 	do { | 
 | 8318 | 		/* Again only user level code calls this function, | 
 | 8319 | 		 * so nothing interrupt level | 
 | 8320 | 		 * will suddenly eat the receive_queue. | 
 | 8321 | 		 * | 
 | 8322 | 		 *  Look at current nfs client by the way... | 
| David Shwatrz | 8917a3c | 2010-12-02 09:01:55 +0000 | [diff] [blame] | 8323 | 		 *  However, this function was correct in any case. 8) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8324 | 		 */ | 
 | 8325 | 		if (flags & MSG_PEEK) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8326 | 			skb = skb_peek(&sk->sk_receive_queue); | 
 | 8327 | 			if (skb) | 
| Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 8328 | 				refcount_inc(&skb->users); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8329 | 		} else { | 
| Marcelo Ricardo Leitner | 311b217 | 2016-04-13 19:12:29 -0300 | [diff] [blame] | 8330 | 			skb = __skb_dequeue(&sk->sk_receive_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8331 | 		} | 
 | 8332 |  | 
 | 8333 | 		if (skb) | 
 | 8334 | 			return skb; | 
 | 8335 |  | 
| Neil Horman | 6736dc3 | 2005-12-02 20:30:06 -0800 | [diff] [blame] | 8336 | 		/* Caller is allowed not to check sk->sk_err before calling. */ | 
 | 8337 | 		error = sock_error(sk); | 
 | 8338 | 		if (error) | 
 | 8339 | 			goto no_packet; | 
 | 8340 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8341 | 		if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 8342 | 			break; | 
 | 8343 |  | 
| Alexander Duyck | 2b5cd0d | 2017-03-24 10:08:12 -0700 | [diff] [blame] | 8344 | 		if (sk_can_busy_loop(sk)) { | 
 | 8345 | 			sk_busy_loop(sk, noblock); | 
 | 8346 |  | 
 | 8347 | 			if (!skb_queue_empty(&sk->sk_receive_queue)) | 
 | 8348 | 				continue; | 
 | 8349 | 		} | 
| Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 8350 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8351 | 		/* User doesn't want to wait.  */ | 
 | 8352 | 		error = -EAGAIN; | 
 | 8353 | 		if (!timeo) | 
 | 8354 | 			goto no_packet; | 
 | 8355 | 	} while (sctp_wait_for_packet(sk, err, &timeo) == 0); | 
 | 8356 |  | 
 | 8357 | 	return NULL; | 
 | 8358 |  | 
 | 8359 | no_packet: | 
 | 8360 | 	*err = error; | 
 | 8361 | 	return NULL; | 
 | 8362 | } | 
 | 8363 |  | 
 | 8364 | /* If sndbuf has changed, wake up per association sndbuf waiters.  */ | 
 | 8365 | static void __sctp_write_space(struct sctp_association *asoc) | 
 | 8366 | { | 
 | 8367 | 	struct sock *sk = asoc->base.sk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8368 |  | 
| Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 8369 | 	if (sctp_wspace(asoc) <= 0) | 
 | 8370 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8371 |  | 
| Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 8372 | 	if (waitqueue_active(&asoc->wait)) | 
 | 8373 | 		wake_up_interruptible(&asoc->wait); | 
| Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 8374 |  | 
| Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 8375 | 	if (sctp_writeable(sk)) { | 
 | 8376 | 		struct socket_wq *wq; | 
 | 8377 |  | 
 | 8378 | 		rcu_read_lock(); | 
 | 8379 | 		wq = rcu_dereference(sk->sk_wq); | 
 | 8380 | 		if (wq) { | 
 | 8381 | 			if (waitqueue_active(&wq->wait)) | 
 | 8382 | 				wake_up_interruptible(&wq->wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8383 |  | 
 | 8384 | 			/* Note that we try to include the Async I/O support | 
 | 8385 | 			 * here by modeling from the current TCP/UDP code. | 
 | 8386 | 			 * We have not tested with it yet. | 
 | 8387 | 			 */ | 
| Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 8388 | 			if (!(sk->sk_shutdown & SEND_SHUTDOWN)) | 
| Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 8389 | 				sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8390 | 		} | 
| Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 8391 | 		rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8392 | 	} | 
 | 8393 | } | 
 | 8394 |  | 
| Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8395 | static void sctp_wake_up_waiters(struct sock *sk, | 
 | 8396 | 				 struct sctp_association *asoc) | 
 | 8397 | { | 
 | 8398 | 	struct sctp_association *tmp = asoc; | 
 | 8399 |  | 
 | 8400 | 	/* We do accounting for the sndbuf space per association, | 
 | 8401 | 	 * so we only need to wake our own association. | 
 | 8402 | 	 */ | 
 | 8403 | 	if (asoc->ep->sndbuf_policy) | 
 | 8404 | 		return __sctp_write_space(asoc); | 
 | 8405 |  | 
| Daniel Borkmann | 1e1cdf8 | 2014-04-09 16:10:20 +0200 | [diff] [blame] | 8406 | 	/* If association goes down and is just flushing its | 
 | 8407 | 	 * outq, then just normally notify others. | 
 | 8408 | 	 */ | 
 | 8409 | 	if (asoc->base.dead) | 
 | 8410 | 		return sctp_write_space(sk); | 
 | 8411 |  | 
| Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8412 | 	/* Accounting for the sndbuf space is per socket, so we | 
 | 8413 | 	 * need to wake up others, try to be fair and in case of | 
 | 8414 | 	 * other associations, let them have a go first instead | 
 | 8415 | 	 * of just doing a sctp_write_space() call. | 
 | 8416 | 	 * | 
 | 8417 | 	 * Note that we reach sctp_wake_up_waiters() only when | 
 | 8418 | 	 * associations free up queued chunks, thus we are under | 
 | 8419 | 	 * lock and the list of associations on a socket is | 
 | 8420 | 	 * guaranteed not to change. | 
 | 8421 | 	 */ | 
 | 8422 | 	for (tmp = list_next_entry(tmp, asocs); 1; | 
 | 8423 | 	     tmp = list_next_entry(tmp, asocs)) { | 
 | 8424 | 		/* Manually skip the head element. */ | 
 | 8425 | 		if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) | 
 | 8426 | 			continue; | 
 | 8427 | 		/* Wake up association. */ | 
 | 8428 | 		__sctp_write_space(tmp); | 
 | 8429 | 		/* We've reached the end. */ | 
 | 8430 | 		if (tmp == asoc) | 
 | 8431 | 			break; | 
 | 8432 | 	} | 
 | 8433 | } | 
 | 8434 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8435 | /* Do accounting for the sndbuf space. | 
 | 8436 |  * Decrement the used sndbuf space of the corresponding association by the | 
 | 8437 |  * data size which was just transmitted(freed). | 
 | 8438 |  */ | 
 | 8439 | static void sctp_wfree(struct sk_buff *skb) | 
 | 8440 | { | 
| Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 8441 | 	struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; | 
 | 8442 | 	struct sctp_association *asoc = chunk->asoc; | 
 | 8443 | 	struct sock *sk = asoc->base.sk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8444 |  | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8445 | 	sk_mem_uncharge(sk, skb->truesize); | 
| Xin Long | 605c0ac | 2018-10-17 03:07:50 +0800 | [diff] [blame] | 8446 | 	sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk); | 
 | 8447 | 	asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk); | 
 | 8448 | 	WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), | 
 | 8449 | 				      &sk->sk_wmem_alloc)); | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8450 |  | 
| Xin Long | ec2e506 | 2018-03-14 19:05:33 +0800 | [diff] [blame] | 8451 | 	if (chunk->shkey) { | 
 | 8452 | 		struct sctp_shared_key *shkey = chunk->shkey; | 
 | 8453 |  | 
 | 8454 | 		/* refcnt == 2 and !list_empty mean after this release, it's | 
 | 8455 | 		 * not being used anywhere, and it's time to notify userland | 
 | 8456 | 		 * that this shkey can be freed if it's been deactivated. | 
 | 8457 | 		 */ | 
 | 8458 | 		if (shkey->deactivated && !list_empty(&shkey->key_list) && | 
 | 8459 | 		    refcount_read(&shkey->refcnt) == 2) { | 
 | 8460 | 			struct sctp_ulpevent *ev; | 
 | 8461 |  | 
 | 8462 | 			ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id, | 
 | 8463 | 							SCTP_AUTH_FREE_KEY, | 
 | 8464 | 							GFP_KERNEL); | 
 | 8465 | 			if (ev) | 
 | 8466 | 				asoc->stream.si->enqueue_event(&asoc->ulpq, ev); | 
 | 8467 | 		} | 
| Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 8468 | 		sctp_auth_shkey_release(chunk->shkey); | 
| Xin Long | ec2e506 | 2018-03-14 19:05:33 +0800 | [diff] [blame] | 8469 | 	} | 
| Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 8470 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 8471 | 	sock_wfree(skb); | 
| Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8472 | 	sctp_wake_up_waiters(sk, asoc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8473 |  | 
 | 8474 | 	sctp_association_put(asoc); | 
 | 8475 | } | 
 | 8476 |  | 
| Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 8477 | /* Do accounting for the receive space on the socket. | 
 | 8478 |  * Accounting for the association is done in ulpevent.c | 
 | 8479 |  * We set this as a destructor for the cloned data skbs so that | 
 | 8480 |  * accounting is done at the correct time. | 
 | 8481 |  */ | 
 | 8482 | void sctp_sock_rfree(struct sk_buff *skb) | 
 | 8483 | { | 
 | 8484 | 	struct sock *sk = skb->sk; | 
 | 8485 | 	struct sctp_ulpevent *event = sctp_skb2event(skb); | 
 | 8486 |  | 
 | 8487 | 	atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8488 |  | 
 | 8489 | 	/* | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8490 | 	 * Mimic the behavior of sock_rfree | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8491 | 	 */ | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8492 | 	sk_mem_uncharge(sk, event->rmem_len); | 
| Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 8493 | } | 
 | 8494 |  | 
 | 8495 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8496 | /* Helper function to wait for space in the sndbuf.  */ | 
 | 8497 | 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] | 8498 | 				size_t msg_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8499 | { | 
 | 8500 | 	struct sock *sk = asoc->base.sk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8501 | 	long current_timeo = *timeo_p; | 
 | 8502 | 	DEFINE_WAIT(wait); | 
| Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 8503 | 	int err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8505 | 	pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, | 
 | 8506 | 		 *timeo_p, msg_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8507 |  | 
 | 8508 | 	/* Increment the association's refcnt.  */ | 
 | 8509 | 	sctp_association_hold(asoc); | 
 | 8510 |  | 
 | 8511 | 	/* Wait on the association specific sndbuf space. */ | 
 | 8512 | 	for (;;) { | 
 | 8513 | 		prepare_to_wait_exclusive(&asoc->wait, &wait, | 
 | 8514 | 					  TASK_INTERRUPTIBLE); | 
| Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8515 | 		if (asoc->base.dead) | 
 | 8516 | 			goto do_dead; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8517 | 		if (!*timeo_p) | 
 | 8518 | 			goto do_nonblock; | 
| Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8519 | 		if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8520 | 			goto do_error; | 
 | 8521 | 		if (signal_pending(current)) | 
 | 8522 | 			goto do_interrupted; | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 8523 | 		if ((int)msg_len <= sctp_wspace(asoc)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8524 | 			break; | 
 | 8525 |  | 
 | 8526 | 		/* Let another process have a go.  Since we are going | 
 | 8527 | 		 * to sleep anyway. | 
 | 8528 | 		 */ | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8529 | 		release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8530 | 		current_timeo = schedule_timeout(current_timeo); | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8531 | 		lock_sock(sk); | 
| Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 8532 | 		if (sk != asoc->base.sk) | 
 | 8533 | 			goto do_error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8534 |  | 
 | 8535 | 		*timeo_p = current_timeo; | 
 | 8536 | 	} | 
 | 8537 |  | 
 | 8538 | out: | 
 | 8539 | 	finish_wait(&asoc->wait, &wait); | 
 | 8540 |  | 
 | 8541 | 	/* Release the association's refcnt.  */ | 
 | 8542 | 	sctp_association_put(asoc); | 
 | 8543 |  | 
 | 8544 | 	return err; | 
 | 8545 |  | 
| Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8546 | do_dead: | 
 | 8547 | 	err = -ESRCH; | 
 | 8548 | 	goto out; | 
 | 8549 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8550 | do_error: | 
 | 8551 | 	err = -EPIPE; | 
 | 8552 | 	goto out; | 
 | 8553 |  | 
 | 8554 | do_interrupted: | 
 | 8555 | 	err = sock_intr_errno(*timeo_p); | 
 | 8556 | 	goto out; | 
 | 8557 |  | 
 | 8558 | do_nonblock: | 
 | 8559 | 	err = -EAGAIN; | 
 | 8560 | 	goto out; | 
 | 8561 | } | 
 | 8562 |  | 
| David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 8563 | void sctp_data_ready(struct sock *sk) | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8564 | { | 
| David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 8565 | 	struct socket_wq *wq; | 
 | 8566 |  | 
 | 8567 | 	rcu_read_lock(); | 
 | 8568 | 	wq = rcu_dereference(sk->sk_wq); | 
| Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 8569 | 	if (skwq_has_sleeper(wq)) | 
| Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8570 | 		wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | | 
 | 8571 | 						EPOLLRDNORM | EPOLLRDBAND); | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8572 | 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); | 
| David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 8573 | 	rcu_read_unlock(); | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8574 | } | 
 | 8575 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8576 | /* If socket sndbuf has changed, wake up all per association waiters.  */ | 
 | 8577 | void sctp_write_space(struct sock *sk) | 
 | 8578 | { | 
 | 8579 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8580 |  | 
 | 8581 | 	/* Wake up the tasks in each wait queue.  */ | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 8582 | 	list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8583 | 		__sctp_write_space(asoc); | 
 | 8584 | 	} | 
 | 8585 | } | 
 | 8586 |  | 
 | 8587 | /* Is there any sndbuf space available on the socket? | 
 | 8588 |  * | 
| Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 8589 |  * 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] | 8590 |  * associations on the same socket.  For a UDP-style socket with | 
 | 8591 |  * multiple associations, it is possible for it to be "unwriteable" | 
 | 8592 |  * prematurely.  I assume that this is acceptable because | 
 | 8593 |  * a premature "unwriteable" is better than an accidental "writeable" which | 
 | 8594 |  * would cause an unwanted block under certain circumstances.  For the 1-1 | 
 | 8595 |  * UDP-style sockets or TCP-style sockets, this code should work. | 
 | 8596 |  *  - Daisy | 
 | 8597 |  */ | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 8598 | static bool sctp_writeable(struct sock *sk) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8599 | { | 
| Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame^] | 8600 | 	return sk->sk_sndbuf > sk->sk_wmem_queued; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8601 | } | 
 | 8602 |  | 
 | 8603 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, | 
 | 8604 |  * returns immediately with EINPROGRESS. | 
 | 8605 |  */ | 
 | 8606 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) | 
 | 8607 | { | 
 | 8608 | 	struct sock *sk = asoc->base.sk; | 
 | 8609 | 	int err = 0; | 
 | 8610 | 	long current_timeo = *timeo_p; | 
 | 8611 | 	DEFINE_WAIT(wait); | 
 | 8612 |  | 
| Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8613 | 	pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8614 |  | 
 | 8615 | 	/* Increment the association's refcnt.  */ | 
 | 8616 | 	sctp_association_hold(asoc); | 
 | 8617 |  | 
 | 8618 | 	for (;;) { | 
 | 8619 | 		prepare_to_wait_exclusive(&asoc->wait, &wait, | 
 | 8620 | 					  TASK_INTERRUPTIBLE); | 
 | 8621 | 		if (!*timeo_p) | 
 | 8622 | 			goto do_nonblock; | 
 | 8623 | 		if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 8624 | 			break; | 
 | 8625 | 		if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || | 
 | 8626 | 		    asoc->base.dead) | 
 | 8627 | 			goto do_error; | 
 | 8628 | 		if (signal_pending(current)) | 
 | 8629 | 			goto do_interrupted; | 
 | 8630 |  | 
 | 8631 | 		if (sctp_state(asoc, ESTABLISHED)) | 
 | 8632 | 			break; | 
 | 8633 |  | 
 | 8634 | 		/* Let another process have a go.  Since we are going | 
 | 8635 | 		 * to sleep anyway. | 
 | 8636 | 		 */ | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8637 | 		release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8638 | 		current_timeo = schedule_timeout(current_timeo); | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8639 | 		lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8640 |  | 
 | 8641 | 		*timeo_p = current_timeo; | 
 | 8642 | 	} | 
 | 8643 |  | 
 | 8644 | out: | 
 | 8645 | 	finish_wait(&asoc->wait, &wait); | 
 | 8646 |  | 
 | 8647 | 	/* Release the association's refcnt.  */ | 
 | 8648 | 	sctp_association_put(asoc); | 
 | 8649 |  | 
 | 8650 | 	return err; | 
 | 8651 |  | 
 | 8652 | do_error: | 
| Vlad Yasevich | 81845c2 | 2006-01-30 15:59:54 -0800 | [diff] [blame] | 8653 | 	if (asoc->init_err_counter + 1 > asoc->max_init_attempts) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8654 | 		err = -ETIMEDOUT; | 
 | 8655 | 	else | 
 | 8656 | 		err = -ECONNREFUSED; | 
 | 8657 | 	goto out; | 
 | 8658 |  | 
 | 8659 | do_interrupted: | 
 | 8660 | 	err = sock_intr_errno(*timeo_p); | 
 | 8661 | 	goto out; | 
 | 8662 |  | 
 | 8663 | do_nonblock: | 
 | 8664 | 	err = -EINPROGRESS; | 
 | 8665 | 	goto out; | 
 | 8666 | } | 
 | 8667 |  | 
 | 8668 | static int sctp_wait_for_accept(struct sock *sk, long timeo) | 
 | 8669 | { | 
 | 8670 | 	struct sctp_endpoint *ep; | 
 | 8671 | 	int err = 0; | 
 | 8672 | 	DEFINE_WAIT(wait); | 
 | 8673 |  | 
 | 8674 | 	ep = sctp_sk(sk)->ep; | 
 | 8675 |  | 
 | 8676 |  | 
 | 8677 | 	for (;;) { | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8678 | 		prepare_to_wait_exclusive(sk_sleep(sk), &wait, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8679 | 					  TASK_INTERRUPTIBLE); | 
 | 8680 |  | 
 | 8681 | 		if (list_empty(&ep->asocs)) { | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8682 | 			release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8683 | 			timeo = schedule_timeout(timeo); | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8684 | 			lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8685 | 		} | 
 | 8686 |  | 
 | 8687 | 		err = -EINVAL; | 
 | 8688 | 		if (!sctp_sstate(sk, LISTENING)) | 
 | 8689 | 			break; | 
 | 8690 |  | 
 | 8691 | 		err = 0; | 
 | 8692 | 		if (!list_empty(&ep->asocs)) | 
 | 8693 | 			break; | 
 | 8694 |  | 
 | 8695 | 		err = sock_intr_errno(timeo); | 
 | 8696 | 		if (signal_pending(current)) | 
 | 8697 | 			break; | 
 | 8698 |  | 
 | 8699 | 		err = -EAGAIN; | 
 | 8700 | 		if (!timeo) | 
 | 8701 | 			break; | 
 | 8702 | 	} | 
 | 8703 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8704 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8705 |  | 
 | 8706 | 	return err; | 
 | 8707 | } | 
 | 8708 |  | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 8709 | static void sctp_wait_for_close(struct sock *sk, long timeout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8710 | { | 
 | 8711 | 	DEFINE_WAIT(wait); | 
 | 8712 |  | 
 | 8713 | 	do { | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8714 | 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8715 | 		if (list_empty(&sctp_sk(sk)->ep->asocs)) | 
 | 8716 | 			break; | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8717 | 		release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8718 | 		timeout = schedule_timeout(timeout); | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8719 | 		lock_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8720 | 	} while (!signal_pending(current) && timeout); | 
 | 8721 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8722 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8723 | } | 
 | 8724 |  | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8725 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) | 
 | 8726 | { | 
 | 8727 | 	struct sk_buff *frag; | 
 | 8728 |  | 
 | 8729 | 	if (!skb->data_len) | 
 | 8730 | 		goto done; | 
 | 8731 |  | 
 | 8732 | 	/* Don't forget the fragments. */ | 
| David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 8733 | 	skb_walk_frags(skb, frag) | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8734 | 		sctp_skb_set_owner_r_frag(frag, sk); | 
 | 8735 |  | 
 | 8736 | done: | 
 | 8737 | 	sctp_skb_set_owner_r(skb, sk); | 
 | 8738 | } | 
 | 8739 |  | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8740 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, | 
 | 8741 | 		    struct sctp_association *asoc) | 
 | 8742 | { | 
 | 8743 | 	struct inet_sock *inet = inet_sk(sk); | 
| Julia Lawall | 09cb47a | 2010-01-21 02:43:20 -0800 | [diff] [blame] | 8744 | 	struct inet_sock *newinet; | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 8745 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 8746 | 	struct sctp_endpoint *ep = sp->ep; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8747 |  | 
 | 8748 | 	newsk->sk_type = sk->sk_type; | 
 | 8749 | 	newsk->sk_bound_dev_if = sk->sk_bound_dev_if; | 
 | 8750 | 	newsk->sk_flags = sk->sk_flags; | 
| Marcelo Ricardo Leitner | 50a5ffb | 2015-12-04 15:14:05 -0200 | [diff] [blame] | 8751 | 	newsk->sk_tsflags = sk->sk_tsflags; | 
| Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 8752 | 	newsk->sk_no_check_tx = sk->sk_no_check_tx; | 
 | 8753 | 	newsk->sk_no_check_rx = sk->sk_no_check_rx; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8754 | 	newsk->sk_reuse = sk->sk_reuse; | 
| Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 8755 | 	sctp_sk(newsk)->reuse = sp->reuse; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8756 |  | 
 | 8757 | 	newsk->sk_shutdown = sk->sk_shutdown; | 
| Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 8758 | 	newsk->sk_destruct = sctp_destruct_sock; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8759 | 	newsk->sk_family = sk->sk_family; | 
 | 8760 | 	newsk->sk_protocol = IPPROTO_SCTP; | 
 | 8761 | 	newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; | 
 | 8762 | 	newsk->sk_sndbuf = sk->sk_sndbuf; | 
 | 8763 | 	newsk->sk_rcvbuf = sk->sk_rcvbuf; | 
 | 8764 | 	newsk->sk_lingertime = sk->sk_lingertime; | 
 | 8765 | 	newsk->sk_rcvtimeo = sk->sk_rcvtimeo; | 
 | 8766 | 	newsk->sk_sndtimeo = sk->sk_sndtimeo; | 
| Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 8767 | 	newsk->sk_rxhash = sk->sk_rxhash; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8768 |  | 
 | 8769 | 	newinet = inet_sk(newsk); | 
 | 8770 |  | 
 | 8771 | 	/* Initialize sk's sport, dport, rcv_saddr and daddr for | 
 | 8772 | 	 * getsockname() and getpeername() | 
 | 8773 | 	 */ | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8774 | 	newinet->inet_sport = inet->inet_sport; | 
 | 8775 | 	newinet->inet_saddr = inet->inet_saddr; | 
 | 8776 | 	newinet->inet_rcv_saddr = inet->inet_rcv_saddr; | 
 | 8777 | 	newinet->inet_dport = htons(asoc->peer.port); | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8778 | 	newinet->pmtudisc = inet->pmtudisc; | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8779 | 	newinet->inet_id = asoc->next_tsn ^ jiffies; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8780 |  | 
 | 8781 | 	newinet->uc_ttl = inet->uc_ttl; | 
 | 8782 | 	newinet->mc_loop = 1; | 
 | 8783 | 	newinet->mc_ttl = 1; | 
 | 8784 | 	newinet->mc_index = 0; | 
 | 8785 | 	newinet->mc_list = NULL; | 
| Marcelo Ricardo Leitner | 01ce63c | 2015-12-04 15:14:04 -0200 | [diff] [blame] | 8786 |  | 
 | 8787 | 	if (newsk->sk_flags & SK_FLAGS_TIMESTAMP) | 
 | 8788 | 		net_enable_timestamp(); | 
| Marcelo Ricardo Leitner | 3538a5c | 2015-12-23 16:44:09 -0200 | [diff] [blame] | 8789 |  | 
| Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 8790 | 	/* Set newsk security attributes from orginal sk and connection | 
 | 8791 | 	 * security attribute from ep. | 
 | 8792 | 	 */ | 
 | 8793 | 	security_sctp_sk_clone(ep, sk, newsk); | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8794 | } | 
 | 8795 |  | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 8796 | static inline void sctp_copy_descendant(struct sock *sk_to, | 
 | 8797 | 					const struct sock *sk_from) | 
 | 8798 | { | 
 | 8799 | 	int ancestor_size = sizeof(struct inet_sock) + | 
 | 8800 | 			    sizeof(struct sctp_sock) - | 
 | 8801 | 			    offsetof(struct sctp_sock, auto_asconf_list); | 
 | 8802 |  | 
 | 8803 | 	if (sk_from->sk_family == PF_INET6) | 
 | 8804 | 		ancestor_size += sizeof(struct ipv6_pinfo); | 
 | 8805 |  | 
 | 8806 | 	__inet_sk_copy_descendant(sk_to, sk_from, ancestor_size); | 
 | 8807 | } | 
 | 8808 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8809 | /* Populate the fields of the newsk from the oldsk and migrate the assoc | 
 | 8810 |  * and its messages to the newsk. | 
 | 8811 |  */ | 
 | 8812 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, | 
 | 8813 | 			      struct sctp_association *assoc, | 
| Xin Long | b7ef261 | 2017-08-11 10:23:50 +0800 | [diff] [blame] | 8814 | 			      enum sctp_socket_type type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8815 | { | 
 | 8816 | 	struct sctp_sock *oldsp = sctp_sk(oldsk); | 
 | 8817 | 	struct sctp_sock *newsp = sctp_sk(newsk); | 
 | 8818 | 	struct sctp_bind_bucket *pp; /* hash list port iterator */ | 
 | 8819 | 	struct sctp_endpoint *newep = newsp->ep; | 
 | 8820 | 	struct sk_buff *skb, *tmp; | 
 | 8821 | 	struct sctp_ulpevent *event; | 
| Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 8822 | 	struct sctp_bind_hashbucket *head; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8823 |  | 
 | 8824 | 	/* Migrate socket buffer sizes and all the socket level options to the | 
 | 8825 | 	 * new socket. | 
 | 8826 | 	 */ | 
 | 8827 | 	newsk->sk_sndbuf = oldsk->sk_sndbuf; | 
 | 8828 | 	newsk->sk_rcvbuf = oldsk->sk_rcvbuf; | 
 | 8829 | 	/* Brute force copy old sctp opt. */ | 
| Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 8830 | 	sctp_copy_descendant(newsk, oldsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8831 |  | 
 | 8832 | 	/* Restore the ep value that was overwritten with the above structure | 
 | 8833 | 	 * copy. | 
 | 8834 | 	 */ | 
 | 8835 | 	newsp->ep = newep; | 
 | 8836 | 	newsp->hmac = NULL; | 
 | 8837 |  | 
 | 8838 | 	/* Hook this new socket in to the bind_hash list. */ | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8839 | 	head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), | 
 | 8840 | 						 inet_sk(oldsk)->inet_num)]; | 
| Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 8841 | 	spin_lock_bh(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8842 | 	pp = sctp_sk(oldsk)->bind_hash; | 
 | 8843 | 	sk_add_bind_node(newsk, &pp->owner); | 
 | 8844 | 	sctp_sk(newsk)->bind_hash = pp; | 
| Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8845 | 	inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; | 
| Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 8846 | 	spin_unlock_bh(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8847 |  | 
| Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 8848 | 	/* Copy the bind_addr list from the original endpoint to the new | 
 | 8849 | 	 * endpoint so that we can handle restarts properly | 
 | 8850 | 	 */ | 
| Vlad Yasevich | 8e71a11 | 2007-12-06 22:50:54 -0800 | [diff] [blame] | 8851 | 	sctp_bind_addr_dup(&newsp->ep->base.bind_addr, | 
 | 8852 | 				&oldsp->ep->base.bind_addr, GFP_KERNEL); | 
| Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 8853 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8854 | 	/* Move any messages in the old socket's receive queue that are for the | 
 | 8855 | 	 * peeled off association to the new socket's receive queue. | 
 | 8856 | 	 */ | 
 | 8857 | 	sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { | 
 | 8858 | 		event = sctp_skb2event(skb); | 
 | 8859 | 		if (event->asoc == assoc) { | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 8860 | 			__skb_unlink(skb, &oldsk->sk_receive_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8861 | 			__skb_queue_tail(&newsk->sk_receive_queue, skb); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8862 | 			sctp_skb_set_owner_r_frag(skb, newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8863 | 		} | 
 | 8864 | 	} | 
 | 8865 |  | 
 | 8866 | 	/* Clean up any messages pending delivery due to partial | 
 | 8867 | 	 * delivery.   Three cases: | 
 | 8868 | 	 * 1) No partial deliver;  no work. | 
 | 8869 | 	 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. | 
 | 8870 | 	 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. | 
 | 8871 | 	 */ | 
 | 8872 | 	skb_queue_head_init(&newsp->pd_lobby); | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8873 | 	atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8874 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8875 | 	if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8876 | 		struct sk_buff_head *queue; | 
 | 8877 |  | 
 | 8878 | 		/* Decide which queue to move pd_lobby skbs to. */ | 
 | 8879 | 		if (assoc->ulpq.pd_mode) { | 
 | 8880 | 			queue = &newsp->pd_lobby; | 
 | 8881 | 		} else | 
 | 8882 | 			queue = &newsk->sk_receive_queue; | 
 | 8883 |  | 
 | 8884 | 		/* Walk through the pd_lobby, looking for skbs that | 
 | 8885 | 		 * need moved to the new socket. | 
 | 8886 | 		 */ | 
 | 8887 | 		sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { | 
 | 8888 | 			event = sctp_skb2event(skb); | 
 | 8889 | 			if (event->asoc == assoc) { | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 8890 | 				__skb_unlink(skb, &oldsp->pd_lobby); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8891 | 				__skb_queue_tail(queue, skb); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8892 | 				sctp_skb_set_owner_r_frag(skb, newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8893 | 			} | 
 | 8894 | 		} | 
 | 8895 |  | 
 | 8896 | 		/* Clear up any skbs waiting for the partial | 
 | 8897 | 		 * delivery to finish. | 
 | 8898 | 		 */ | 
 | 8899 | 		if (assoc->ulpq.pd_mode) | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8900 | 			sctp_clear_pd(oldsk, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8901 |  | 
 | 8902 | 	} | 
 | 8903 |  | 
| Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 8904 | 	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] | 8905 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8906 | 	/* Set the type of socket to indicate that it is peeled off from the | 
 | 8907 | 	 * original UDP-style socket or created with the accept() call on a | 
 | 8908 | 	 * TCP-style socket.. | 
 | 8909 | 	 */ | 
 | 8910 | 	newsp->type = type; | 
 | 8911 |  | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 8912 | 	/* Mark the new socket "in-use" by the user so that any packets | 
 | 8913 | 	 * that may arrive on the association after we've moved it are | 
 | 8914 | 	 * queued to the backlog.  This prevents a potential race between | 
 | 8915 | 	 * backlog processing on the old socket and new-packet processing | 
 | 8916 | 	 * on the new socket. | 
| Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 8917 | 	 * | 
 | 8918 | 	 * The caller has just allocated newsk so we can guarantee that other | 
 | 8919 | 	 * paths won't try to lock it and then oldsk. | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 8920 | 	 */ | 
| Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 8921 | 	lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 8922 | 	sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8923 | 	sctp_assoc_migrate(assoc, newsk); | 
| Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 8924 | 	sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8925 |  | 
 | 8926 | 	/* If the association on the newsk is already closed before accept() | 
 | 8927 | 	 * is called, set RCV_SHUTDOWN flag. | 
 | 8928 | 	 */ | 
| Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 8929 | 	if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 8930 | 		inet_sk_set_state(newsk, SCTP_SS_CLOSED); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8931 | 		newsk->sk_shutdown |= RCV_SHUTDOWN; | 
| Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 8932 | 	} else { | 
| Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 8933 | 		inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED); | 
| Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 8934 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8935 |  | 
| wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8936 | 	release_sock(newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8937 | } | 
 | 8938 |  | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8939 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8940 | /* This proto struct describes the ULP interface for SCTP.  */ | 
 | 8941 | struct proto sctp_prot = { | 
 | 8942 | 	.name        =	"SCTP", | 
 | 8943 | 	.owner       =	THIS_MODULE, | 
 | 8944 | 	.close       =	sctp_close, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8945 | 	.disconnect  =	sctp_disconnect, | 
 | 8946 | 	.accept      =	sctp_accept, | 
 | 8947 | 	.ioctl       =	sctp_ioctl, | 
 | 8948 | 	.init        =	sctp_init_sock, | 
 | 8949 | 	.destroy     =	sctp_destroy_sock, | 
 | 8950 | 	.shutdown    =	sctp_shutdown, | 
 | 8951 | 	.setsockopt  =	sctp_setsockopt, | 
 | 8952 | 	.getsockopt  =	sctp_getsockopt, | 
 | 8953 | 	.sendmsg     =	sctp_sendmsg, | 
 | 8954 | 	.recvmsg     =	sctp_recvmsg, | 
 | 8955 | 	.bind        =	sctp_bind, | 
 | 8956 | 	.backlog_rcv =	sctp_backlog_rcv, | 
 | 8957 | 	.hash        =	sctp_hash, | 
 | 8958 | 	.unhash      =	sctp_unhash, | 
 | 8959 | 	.get_port    =	sctp_get_port, | 
 | 8960 | 	.obj_size    =  sizeof(struct sctp_sock), | 
| David Windsor | ab9ee8e | 2017-08-24 16:57:57 -0700 | [diff] [blame] | 8961 | 	.useroffset  =  offsetof(struct sctp_sock, subscribe), | 
 | 8962 | 	.usersize    =  offsetof(struct sctp_sock, initmsg) - | 
 | 8963 | 				offsetof(struct sctp_sock, subscribe) + | 
 | 8964 | 				sizeof_field(struct sctp_sock, initmsg), | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8965 | 	.sysctl_mem  =  sysctl_sctp_mem, | 
 | 8966 | 	.sysctl_rmem =  sysctl_sctp_rmem, | 
 | 8967 | 	.sysctl_wmem =  sysctl_sctp_wmem, | 
 | 8968 | 	.memory_pressure = &sctp_memory_pressure, | 
 | 8969 | 	.enter_memory_pressure = sctp_enter_memory_pressure, | 
 | 8970 | 	.memory_allocated = &sctp_memory_allocated, | 
| Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 8971 | 	.sockets_allocated = &sctp_sockets_allocated, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8972 | }; | 
 | 8973 |  | 
| Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 8974 | #if IS_ENABLED(CONFIG_IPV6) | 
| Eric Dumazet | 8295b6d | 2007-11-05 23:40:28 -0800 | [diff] [blame] | 8975 |  | 
| Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 8976 | #include <net/transp_v6.h> | 
 | 8977 | static void sctp_v6_destroy_sock(struct sock *sk) | 
 | 8978 | { | 
 | 8979 | 	sctp_destroy_sock(sk); | 
 | 8980 | 	inet6_destroy_sock(sk); | 
 | 8981 | } | 
 | 8982 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8983 | struct proto sctpv6_prot = { | 
 | 8984 | 	.name		= "SCTPv6", | 
 | 8985 | 	.owner		= THIS_MODULE, | 
 | 8986 | 	.close		= sctp_close, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8987 | 	.disconnect	= sctp_disconnect, | 
 | 8988 | 	.accept		= sctp_accept, | 
 | 8989 | 	.ioctl		= sctp_ioctl, | 
 | 8990 | 	.init		= sctp_init_sock, | 
| Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 8991 | 	.destroy	= sctp_v6_destroy_sock, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8992 | 	.shutdown	= sctp_shutdown, | 
 | 8993 | 	.setsockopt	= sctp_setsockopt, | 
 | 8994 | 	.getsockopt	= sctp_getsockopt, | 
 | 8995 | 	.sendmsg	= sctp_sendmsg, | 
 | 8996 | 	.recvmsg	= sctp_recvmsg, | 
 | 8997 | 	.bind		= sctp_bind, | 
 | 8998 | 	.backlog_rcv	= sctp_backlog_rcv, | 
 | 8999 | 	.hash		= sctp_hash, | 
 | 9000 | 	.unhash		= sctp_unhash, | 
 | 9001 | 	.get_port	= sctp_get_port, | 
 | 9002 | 	.obj_size	= sizeof(struct sctp6_sock), | 
| David Windsor | ab9ee8e | 2017-08-24 16:57:57 -0700 | [diff] [blame] | 9003 | 	.useroffset	= offsetof(struct sctp6_sock, sctp.subscribe), | 
 | 9004 | 	.usersize	= offsetof(struct sctp6_sock, sctp.initmsg) - | 
 | 9005 | 				offsetof(struct sctp6_sock, sctp.subscribe) + | 
 | 9006 | 				sizeof_field(struct sctp6_sock, sctp.initmsg), | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9007 | 	.sysctl_mem	= sysctl_sctp_mem, | 
 | 9008 | 	.sysctl_rmem	= sysctl_sctp_rmem, | 
 | 9009 | 	.sysctl_wmem	= sysctl_sctp_wmem, | 
 | 9010 | 	.memory_pressure = &sctp_memory_pressure, | 
 | 9011 | 	.enter_memory_pressure = sctp_enter_memory_pressure, | 
 | 9012 | 	.memory_allocated = &sctp_memory_allocated, | 
| Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 9013 | 	.sockets_allocated = &sctp_sockets_allocated, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9014 | }; | 
| Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 9015 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |