| 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 | 
 | 31 |  * along with GNU CC; see the file COPYING.  If not, write to | 
 | 32 |  * the Free Software Foundation, 59 Temple Place - Suite 330, | 
 | 33 |  * Boston, MA 02111-1307, USA. | 
 | 34 |  * | 
 | 35 |  * Please send any bug reports or fixes you make to the | 
 | 36 |  * email address(es): | 
 | 37 |  *    lksctp developers <lksctp-developers@lists.sourceforge.net> | 
 | 38 |  * | 
 | 39 |  * Or submit a bug report through the following website: | 
 | 40 |  *    http://www.sf.net/projects/lksctp | 
 | 41 |  * | 
 | 42 |  * Written or modified by: | 
 | 43 |  *    La Monte H.P. Yarroll <piggy@acm.org> | 
 | 44 |  *    Narasimha Budihal     <narsi@refcode.org> | 
 | 45 |  *    Karl Knutson          <karl@athena.chicago.il.us> | 
 | 46 |  *    Jon Grimm             <jgrimm@us.ibm.com> | 
 | 47 |  *    Xingang Guo           <xingang.guo@intel.com> | 
 | 48 |  *    Daisy Chang           <daisyc@us.ibm.com> | 
 | 49 |  *    Sridhar Samudrala     <samudrala@us.ibm.com> | 
 | 50 |  *    Inaky Perez-Gonzalez  <inaky.gonzalez@intel.com> | 
 | 51 |  *    Ardelle Fan	    <ardelle.fan@intel.com> | 
 | 52 |  *    Ryan Layer	    <rmlayer@us.ibm.com> | 
 | 53 |  *    Anup Pemmaiah         <pemmaiah@cc.usu.edu> | 
 | 54 |  *    Kevin Gao             <kevin.gao@intel.com> | 
 | 55 |  * | 
 | 56 |  * Any bugs reported given to us we will try to fix... any fixes shared will | 
 | 57 |  * be incorporated into the next SCTP release. | 
 | 58 |  */ | 
 | 59 |  | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 60 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
 | 61 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include <linux/types.h> | 
 | 63 | #include <linux/kernel.h> | 
 | 64 | #include <linux/wait.h> | 
 | 65 | #include <linux/time.h> | 
 | 66 | #include <linux/ip.h> | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 67 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/fcntl.h> | 
 | 69 | #include <linux/poll.h> | 
 | 70 | #include <linux/init.h> | 
 | 71 | #include <linux/crypto.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 72 | #include <linux/slab.h> | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 73 | #include <linux/file.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 |  | 
 | 75 | #include <net/ip.h> | 
 | 76 | #include <net/icmp.h> | 
 | 77 | #include <net/route.h> | 
 | 78 | #include <net/ipv6.h> | 
 | 79 | #include <net/inet_common.h> | 
 | 80 |  | 
 | 81 | #include <linux/socket.h> /* for sa_family_t */ | 
| Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 82 | #include <linux/export.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <net/sock.h> | 
 | 84 | #include <net/sctp/sctp.h> | 
 | 85 | #include <net/sctp/sm.h> | 
 | 86 |  | 
 | 87 | /* WARNING:  Please do not remove the SCTP_STATIC attribute to | 
 | 88 |  * any of the functions below as they are used to export functions | 
 | 89 |  * used by a project regression testsuite. | 
 | 90 |  */ | 
 | 91 |  | 
 | 92 | /* Forward declarations for internal helper functions. */ | 
 | 93 | static int sctp_writeable(struct sock *sk); | 
 | 94 | static void sctp_wfree(struct sk_buff *skb); | 
 | 95 | static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p, | 
 | 96 | 				size_t msg_len); | 
 | 97 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p); | 
 | 98 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); | 
 | 99 | static int sctp_wait_for_accept(struct sock *sk, long timeo); | 
 | 100 | static void sctp_wait_for_close(struct sock *sk, long timeo); | 
 | 101 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 
 | 102 | 					union sctp_addr *addr, int len); | 
 | 103 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); | 
 | 104 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); | 
 | 105 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); | 
 | 106 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); | 
 | 107 | static int sctp_send_asconf(struct sctp_association *asoc, | 
 | 108 | 			    struct sctp_chunk *chunk); | 
 | 109 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); | 
 | 110 | static int sctp_autobind(struct sock *sk); | 
 | 111 | static void sctp_sock_migrate(struct sock *, struct sock *, | 
 | 112 | 			      struct sctp_association *, sctp_socket_type_t); | 
 | 113 | static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG; | 
 | 114 |  | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 115 | extern struct kmem_cache *sctp_bucket_cachep; | 
| Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 116 | extern long sysctl_sctp_mem[3]; | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 117 | extern int sysctl_sctp_rmem[3]; | 
 | 118 | extern int sysctl_sctp_wmem[3]; | 
 | 119 |  | 
| Adrian Bunk | b6fa1a4 | 2007-09-12 15:18:00 +0200 | [diff] [blame] | 120 | static int sctp_memory_pressure; | 
| Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 121 | static atomic_long_t sctp_memory_allocated; | 
| Eric Dumazet | 1748376 | 2008-11-25 21:16:35 -0800 | [diff] [blame] | 122 | struct percpu_counter sctp_sockets_allocated; | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 123 |  | 
| Pavel Emelyanov | 5c52ba1 | 2008-07-16 20:28:10 -0700 | [diff] [blame] | 124 | static void sctp_enter_memory_pressure(struct sock *sk) | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 125 | { | 
 | 126 | 	sctp_memory_pressure = 1; | 
 | 127 | } | 
 | 128 |  | 
 | 129 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* Get the sndbuf space available at the time on the association.  */ | 
 | 131 | static inline int sctp_wspace(struct sctp_association *asoc) | 
 | 132 | { | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 133 | 	int amt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 |  | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 135 | 	if (asoc->ep->sndbuf_policy) | 
 | 136 | 		amt = asoc->sndbuf_used; | 
 | 137 | 	else | 
| Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 138 | 		amt = sk_wmem_alloc_get(asoc->base.sk); | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 139 |  | 
 | 140 | 	if (amt >= asoc->base.sk->sk_sndbuf) { | 
 | 141 | 		if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK) | 
 | 142 | 			amt = 0; | 
 | 143 | 		else { | 
 | 144 | 			amt = sk_stream_wspace(asoc->base.sk); | 
 | 145 | 			if (amt < 0) | 
 | 146 | 				amt = 0; | 
 | 147 | 		} | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 148 | 	} else { | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 149 | 		amt = asoc->base.sk->sk_sndbuf - amt; | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 150 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 	return amt; | 
 | 152 | } | 
 | 153 |  | 
 | 154 | /* Increment the used sndbuf space count of the corresponding association by | 
 | 155 |  * the size of the outgoing data chunk. | 
 | 156 |  * Also, set the skb destructor for sndbuf accounting later. | 
 | 157 |  * | 
 | 158 |  * Since it is always 1-1 between chunk and skb, and also a new skb is always | 
 | 159 |  * allocated for chunk bundling in sctp_packet_transmit(), we can use the | 
 | 160 |  * destructor in the data chunk skb for the purpose of the sndbuf space | 
 | 161 |  * tracking. | 
 | 162 |  */ | 
 | 163 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) | 
 | 164 | { | 
 | 165 | 	struct sctp_association *asoc = chunk->asoc; | 
 | 166 | 	struct sock *sk = asoc->base.sk; | 
 | 167 |  | 
 | 168 | 	/* The sndbuf space is tracked per association.  */ | 
 | 169 | 	sctp_association_hold(asoc); | 
 | 170 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 171 | 	skb_set_owner_w(chunk->skb, sk); | 
 | 172 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | 	chunk->skb->destructor = sctp_wfree; | 
 | 174 | 	/* Save the chunk pointer in skb for sctp_wfree to use later.  */ | 
 | 175 | 	*((struct sctp_chunk **)(chunk->skb->cb)) = chunk; | 
 | 176 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 177 | 	asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + | 
 | 178 | 				sizeof(struct sk_buff) + | 
 | 179 | 				sizeof(struct sctp_chunk); | 
 | 180 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 181 | 	atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 182 | 	sk->sk_wmem_queued += chunk->skb->truesize; | 
 | 183 | 	sk_mem_charge(sk, chunk->skb->truesize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } | 
 | 185 |  | 
 | 186 | /* Verify that this is a valid address. */ | 
 | 187 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, | 
 | 188 | 				   int len) | 
 | 189 | { | 
 | 190 | 	struct sctp_af *af; | 
 | 191 |  | 
 | 192 | 	/* Verify basic sockaddr. */ | 
 | 193 | 	af = sctp_sockaddr_af(sctp_sk(sk), addr, len); | 
 | 194 | 	if (!af) | 
 | 195 | 		return -EINVAL; | 
 | 196 |  | 
 | 197 | 	/* Is this a valid SCTP address?  */ | 
| Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 198 | 	if (!af->addr_valid(addr, sctp_sk(sk), NULL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 		return -EINVAL; | 
 | 200 |  | 
 | 201 | 	if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) | 
 | 202 | 		return -EINVAL; | 
 | 203 |  | 
 | 204 | 	return 0; | 
 | 205 | } | 
 | 206 |  | 
 | 207 | /* Look up the association by its id.  If this is not a UDP-style | 
 | 208 |  * socket, the ID field is always ignored. | 
 | 209 |  */ | 
 | 210 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) | 
 | 211 | { | 
 | 212 | 	struct sctp_association *asoc = NULL; | 
 | 213 |  | 
 | 214 | 	/* If this is not a UDP-style socket, assoc id should be ignored. */ | 
 | 215 | 	if (!sctp_style(sk, UDP)) { | 
 | 216 | 		/* Return NULL if the socket state is not ESTABLISHED. It | 
 | 217 | 		 * could be a TCP-style listening socket or a socket which | 
 | 218 | 		 * hasn't yet called connect() to establish an association. | 
 | 219 | 		 */ | 
 | 220 | 		if (!sctp_sstate(sk, ESTABLISHED)) | 
 | 221 | 			return NULL; | 
 | 222 |  | 
 | 223 | 		/* Get the first and the only association from the list. */ | 
 | 224 | 		if (!list_empty(&sctp_sk(sk)->ep->asocs)) | 
 | 225 | 			asoc = list_entry(sctp_sk(sk)->ep->asocs.next, | 
 | 226 | 					  struct sctp_association, asocs); | 
 | 227 | 		return asoc; | 
 | 228 | 	} | 
 | 229 |  | 
 | 230 | 	/* Otherwise this is a UDP-style socket. */ | 
 | 231 | 	if (!id || (id == (sctp_assoc_t)-1)) | 
 | 232 | 		return NULL; | 
 | 233 |  | 
 | 234 | 	spin_lock_bh(&sctp_assocs_id_lock); | 
 | 235 | 	asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); | 
 | 236 | 	spin_unlock_bh(&sctp_assocs_id_lock); | 
 | 237 |  | 
 | 238 | 	if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) | 
 | 239 | 		return NULL; | 
 | 240 |  | 
 | 241 | 	return asoc; | 
 | 242 | } | 
 | 243 |  | 
 | 244 | /* Look up the transport from an address and an assoc id. If both address and | 
 | 245 |  * id are specified, the associations matching the address and the id should be | 
 | 246 |  * the same. | 
 | 247 |  */ | 
 | 248 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, | 
 | 249 | 					      struct sockaddr_storage *addr, | 
 | 250 | 					      sctp_assoc_t id) | 
 | 251 | { | 
 | 252 | 	struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; | 
 | 253 | 	struct sctp_transport *transport; | 
 | 254 | 	union sctp_addr *laddr = (union sctp_addr *)addr; | 
 | 255 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | 	addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, | 
| Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 257 | 					       laddr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | 					       &transport); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 |  | 
 | 260 | 	if (!addr_asoc) | 
 | 261 | 		return NULL; | 
 | 262 |  | 
 | 263 | 	id_asoc = sctp_id2assoc(sk, id); | 
 | 264 | 	if (id_asoc && (id_asoc != addr_asoc)) | 
 | 265 | 		return NULL; | 
 | 266 |  | 
 | 267 | 	sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 
 | 268 | 						(union sctp_addr *)addr); | 
 | 269 |  | 
 | 270 | 	return transport; | 
 | 271 | } | 
 | 272 |  | 
 | 273 | /* API 3.1.2 bind() - UDP Style Syntax | 
 | 274 |  * The syntax of bind() is, | 
 | 275 |  * | 
 | 276 |  *   ret = bind(int sd, struct sockaddr *addr, int addrlen); | 
 | 277 |  * | 
 | 278 |  *   sd      - the socket descriptor returned by socket(). | 
 | 279 |  *   addr    - the address structure (struct sockaddr_in or struct | 
 | 280 |  *             sockaddr_in6 [RFC 2553]), | 
 | 281 |  *   addr_len - the size of the address structure. | 
 | 282 |  */ | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 283 | SCTP_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] | 284 | { | 
 | 285 | 	int retval = 0; | 
 | 286 |  | 
 | 287 | 	sctp_lock_sock(sk); | 
 | 288 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 289 | 	SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n", | 
 | 290 | 			  sk, addr, addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
 | 292 | 	/* Disallow binding twice. */ | 
 | 293 | 	if (!sctp_sk(sk)->ep->base.bind_addr.port) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 294 | 		retval = sctp_do_bind(sk, (union sctp_addr *)addr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 				      addr_len); | 
 | 296 | 	else | 
 | 297 | 		retval = -EINVAL; | 
 | 298 |  | 
 | 299 | 	sctp_release_sock(sk); | 
 | 300 |  | 
 | 301 | 	return retval; | 
 | 302 | } | 
 | 303 |  | 
 | 304 | static long sctp_get_port_local(struct sock *, union sctp_addr *); | 
 | 305 |  | 
 | 306 | /* Verify this is a valid sockaddr. */ | 
 | 307 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 
 | 308 | 					union sctp_addr *addr, int len) | 
 | 309 | { | 
 | 310 | 	struct sctp_af *af; | 
 | 311 |  | 
 | 312 | 	/* Check minimum size.  */ | 
 | 313 | 	if (len < sizeof (struct sockaddr)) | 
 | 314 | 		return NULL; | 
 | 315 |  | 
| Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 316 | 	/* V4 mapped address are really of AF_INET family */ | 
 | 317 | 	if (addr->sa.sa_family == AF_INET6 && | 
 | 318 | 	    ipv6_addr_v4mapped(&addr->v6.sin6_addr)) { | 
 | 319 | 		if (!opt->pf->af_supported(AF_INET, opt)) | 
 | 320 | 			return NULL; | 
 | 321 | 	} else { | 
 | 322 | 		/* Does this PF support this AF? */ | 
 | 323 | 		if (!opt->pf->af_supported(addr->sa.sa_family, opt)) | 
 | 324 | 			return NULL; | 
 | 325 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 |  | 
 | 327 | 	/* If we get this far, af is valid. */ | 
 | 328 | 	af = sctp_get_af_specific(addr->sa.sa_family); | 
 | 329 |  | 
 | 330 | 	if (len < af->sockaddr_len) | 
 | 331 | 		return NULL; | 
 | 332 |  | 
 | 333 | 	return af; | 
 | 334 | } | 
 | 335 |  | 
 | 336 | /* Bind a local address either to an endpoint or to an association.  */ | 
 | 337 | SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) | 
 | 338 | { | 
 | 339 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 340 | 	struct sctp_endpoint *ep = sp->ep; | 
 | 341 | 	struct sctp_bind_addr *bp = &ep->base.bind_addr; | 
 | 342 | 	struct sctp_af *af; | 
 | 343 | 	unsigned short snum; | 
 | 344 | 	int ret = 0; | 
 | 345 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 	/* Common sockaddr verification. */ | 
 | 347 | 	af = sctp_sockaddr_af(sp, addr, len); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 348 | 	if (!af) { | 
 | 349 | 		SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n", | 
 | 350 | 				  sk, addr, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | 		return -EINVAL; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 352 | 	} | 
 | 353 |  | 
 | 354 | 	snum = ntohs(addr->v4.sin_port); | 
 | 355 |  | 
 | 356 | 	SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ", | 
 | 357 | 				 ", port: %d, new port: %d, len: %d)\n", | 
 | 358 | 				 sk, | 
 | 359 | 				 addr, | 
 | 360 | 				 bp->port, snum, | 
 | 361 | 				 len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 |  | 
 | 363 | 	/* PF specific bind() address verification. */ | 
 | 364 | 	if (!sp->pf->bind_verify(sp, addr)) | 
 | 365 | 		return -EADDRNOTAVAIL; | 
 | 366 |  | 
| Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 367 | 	/* We must either be unbound, or bind to the same port. | 
 | 368 | 	 * It's OK to allow 0 ports if we are already bound. | 
 | 369 | 	 * We'll just inhert an already bound port in this case | 
 | 370 | 	 */ | 
 | 371 | 	if (bp->port) { | 
 | 372 | 		if (!snum) | 
 | 373 | 			snum = bp->port; | 
 | 374 | 		else if (snum != bp->port) { | 
 | 375 | 			SCTP_DEBUG_PRINTK("sctp_do_bind:" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | 				  " New port %d does not match existing port " | 
 | 377 | 				  "%d.\n", snum, bp->port); | 
| Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 378 | 			return -EINVAL; | 
 | 379 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | 	} | 
 | 381 |  | 
 | 382 | 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) | 
 | 383 | 		return -EACCES; | 
 | 384 |  | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 385 | 	/* See if the address matches any of the addresses we may have | 
 | 386 | 	 * already bound before checking against other endpoints. | 
 | 387 | 	 */ | 
 | 388 | 	if (sctp_bind_addr_match(bp, addr, sp)) | 
 | 389 | 		return -EINVAL; | 
 | 390 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | 	/* Make sure we are allowed to bind here. | 
 | 392 | 	 * The function sctp_get_port_local() does duplicate address | 
 | 393 | 	 * detection. | 
 | 394 | 	 */ | 
| Vlad Yasevich | 2772b49 | 2007-08-21 14:24:30 +0900 | [diff] [blame] | 395 | 	addr->v4.sin_port = htons(snum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | 	if ((ret = sctp_get_port_local(sk, addr))) { | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 397 | 		return -EADDRINUSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | 	} | 
 | 399 |  | 
 | 400 | 	/* Refresh ephemeral port.  */ | 
 | 401 | 	if (!bp->port) | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 402 | 		bp->port = inet_sk(sk)->inet_num; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 404 | 	/* Add the address to the bind address list. | 
 | 405 | 	 * Use GFP_ATOMIC since BHs will be disabled. | 
 | 406 | 	 */ | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 407 | 	ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 |  | 
 | 409 | 	/* Copy back into socket for getsockname() use. */ | 
 | 410 | 	if (!ret) { | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 411 | 		inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | 		af->to_sk_saddr(addr, sk); | 
 | 413 | 	} | 
 | 414 |  | 
 | 415 | 	return ret; | 
 | 416 | } | 
 | 417 |  | 
 | 418 |  /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks | 
 | 419 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 420 |  * 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] | 421 |  * 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] | 422 |  * it needs to transfer another ASCONF Chunk, it MUST wait until the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 |  * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 424 |  * subsequent ASCONF. Note this restriction binds each side, so at any | 
 | 425 |  * 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] | 426 |  * from each endpoint). | 
 | 427 |  */ | 
 | 428 | static int sctp_send_asconf(struct sctp_association *asoc, | 
 | 429 | 			    struct sctp_chunk *chunk) | 
 | 430 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 431 | 	struct net 	*net = sock_net(asoc->base.sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | 	int		retval = 0; | 
 | 433 |  | 
 | 434 | 	/* If there is an outstanding ASCONF chunk, queue it for later | 
 | 435 | 	 * transmission. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 436 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | 	if (asoc->addip_last_asconf) { | 
| David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 438 | 		list_add_tail(&chunk->list, &asoc->addip_chunk_list); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 439 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | 	} | 
 | 441 |  | 
 | 442 | 	/* Hold the chunk until an ASCONF_ACK is received. */ | 
 | 443 | 	sctp_chunk_hold(chunk); | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 444 | 	retval = sctp_primitive_ASCONF(net, asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | 	if (retval) | 
 | 446 | 		sctp_chunk_free(chunk); | 
 | 447 | 	else | 
 | 448 | 		asoc->addip_last_asconf = chunk; | 
 | 449 |  | 
 | 450 | out: | 
 | 451 | 	return retval; | 
 | 452 | } | 
 | 453 |  | 
 | 454 | /* Add a list of addresses as bind addresses to local endpoint or | 
 | 455 |  * association. | 
 | 456 |  * | 
 | 457 |  * Basically run through each address specified in the addrs/addrcnt | 
 | 458 |  * array/length pair, determine if it is IPv6 or IPv4 and call | 
 | 459 |  * sctp_do_bind() on it. | 
 | 460 |  * | 
 | 461 |  * If any of them fails, then the operation will be reversed and the | 
 | 462 |  * ones that were added will be removed. | 
 | 463 |  * | 
 | 464 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 465 |  */ | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 466 | 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] | 467 | { | 
 | 468 | 	int cnt; | 
 | 469 | 	int retval = 0; | 
 | 470 | 	void *addr_buf; | 
 | 471 | 	struct sockaddr *sa_addr; | 
 | 472 | 	struct sctp_af *af; | 
 | 473 |  | 
 | 474 | 	SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n", | 
 | 475 | 			  sk, addrs, addrcnt); | 
 | 476 |  | 
 | 477 | 	addr_buf = addrs; | 
 | 478 | 	for (cnt = 0; cnt < addrcnt; cnt++) { | 
 | 479 | 		/* The list may contain either IPv4 or IPv6 address; | 
 | 480 | 		 * determine the address length for walking thru the list. | 
 | 481 | 		 */ | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 482 | 		sa_addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | 		af = sctp_get_af_specific(sa_addr->sa_family); | 
 | 484 | 		if (!af) { | 
 | 485 | 			retval = -EINVAL; | 
 | 486 | 			goto err_bindx_add; | 
 | 487 | 		} | 
 | 488 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 489 | 		retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 				      af->sockaddr_len); | 
 | 491 |  | 
 | 492 | 		addr_buf += af->sockaddr_len; | 
 | 493 |  | 
 | 494 | err_bindx_add: | 
 | 495 | 		if (retval < 0) { | 
 | 496 | 			/* Failed. Cleanup the ones that have been added */ | 
 | 497 | 			if (cnt > 0) | 
 | 498 | 				sctp_bindx_rem(sk, addrs, cnt); | 
 | 499 | 			return retval; | 
 | 500 | 		} | 
 | 501 | 	} | 
 | 502 |  | 
 | 503 | 	return retval; | 
 | 504 | } | 
 | 505 |  | 
 | 506 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the | 
 | 507 |  * associations that are part of the endpoint indicating that a list of local | 
 | 508 |  * addresses are added to the endpoint. | 
 | 509 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 510 |  * 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] | 511 |  * association, we do not send the chunk for that association.  But it will not | 
 | 512 |  * affect other associations. | 
 | 513 |  * | 
 | 514 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 515 |  */ | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 516 | static int sctp_send_asconf_add_ip(struct sock		*sk, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | 				   struct sockaddr	*addrs, | 
 | 518 | 				   int 			addrcnt) | 
 | 519 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 520 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | 	struct sctp_sock		*sp; | 
 | 522 | 	struct sctp_endpoint		*ep; | 
 | 523 | 	struct sctp_association		*asoc; | 
 | 524 | 	struct sctp_bind_addr		*bp; | 
 | 525 | 	struct sctp_chunk		*chunk; | 
 | 526 | 	struct sctp_sockaddr_entry	*laddr; | 
 | 527 | 	union sctp_addr			*addr; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 528 | 	union sctp_addr			saveaddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | 	void				*addr_buf; | 
 | 530 | 	struct sctp_af			*af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | 	struct list_head		*p; | 
 | 532 | 	int 				i; | 
 | 533 | 	int 				retval = 0; | 
 | 534 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 535 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | 		return retval; | 
 | 537 |  | 
 | 538 | 	sp = sctp_sk(sk); | 
 | 539 | 	ep = sp->ep; | 
 | 540 |  | 
 | 541 | 	SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 542 | 			  __func__, sk, addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 544 | 	list_for_each_entry(asoc, &ep->asocs, asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 |  | 
 | 546 | 		if (!asoc->peer.asconf_capable) | 
 | 547 | 			continue; | 
 | 548 |  | 
 | 549 | 		if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) | 
 | 550 | 			continue; | 
 | 551 |  | 
 | 552 | 		if (!sctp_state(asoc, ESTABLISHED)) | 
 | 553 | 			continue; | 
 | 554 |  | 
 | 555 | 		/* Check if any address in the packed array of addresses is | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 556 | 		 * in the bind address list of the association. If so, | 
 | 557 | 		 * do not send the asconf chunk to its peer, but continue with | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | 		 * other associations. | 
 | 559 | 		 */ | 
 | 560 | 		addr_buf = addrs; | 
 | 561 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 562 | 			addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | 			af = sctp_get_af_specific(addr->v4.sin_family); | 
 | 564 | 			if (!af) { | 
 | 565 | 				retval = -EINVAL; | 
 | 566 | 				goto out; | 
 | 567 | 			} | 
 | 568 |  | 
 | 569 | 			if (sctp_assoc_lookup_laddr(asoc, addr)) | 
 | 570 | 				break; | 
 | 571 |  | 
 | 572 | 			addr_buf += af->sockaddr_len; | 
 | 573 | 		} | 
 | 574 | 		if (i < addrcnt) | 
 | 575 | 			continue; | 
 | 576 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 577 | 		/* Use the first valid address in bind addr list of | 
 | 578 | 		 * association as Address Parameter of ASCONF CHUNK. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | 		bp = &asoc->base.bind_addr; | 
 | 581 | 		p = bp->address_list.next; | 
 | 582 | 		laddr = list_entry(p, struct sctp_sockaddr_entry, list); | 
| Al Viro | 5ae955c | 2006-11-20 17:22:08 -0800 | [diff] [blame] | 583 | 		chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | 						   addrcnt, SCTP_PARAM_ADD_IP); | 
 | 585 | 		if (!chunk) { | 
 | 586 | 			retval = -ENOMEM; | 
 | 587 | 			goto out; | 
 | 588 | 		} | 
 | 589 |  | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 590 | 		/* Add the new addresses to the bind address list with | 
 | 591 | 		 * use_as_src set to 0. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | 		 */ | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 593 | 		addr_buf = addrs; | 
 | 594 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 595 | 			addr = addr_buf; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 596 | 			af = sctp_get_af_specific(addr->v4.sin_family); | 
 | 597 | 			memcpy(&saveaddr, addr, af->sockaddr_len); | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 598 | 			retval = sctp_add_bind_addr(bp, &saveaddr, | 
 | 599 | 						    SCTP_ADDR_NEW, GFP_ATOMIC); | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 600 | 			addr_buf += af->sockaddr_len; | 
 | 601 | 		} | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 602 | 		if (asoc->src_out_of_asoc_ok) { | 
 | 603 | 			struct sctp_transport *trans; | 
 | 604 |  | 
 | 605 | 			list_for_each_entry(trans, | 
 | 606 | 			    &asoc->peer.transport_addr_list, transports) { | 
 | 607 | 				/* Clear the source and route cache */ | 
 | 608 | 				dst_release(trans->dst); | 
 | 609 | 				trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, | 
 | 610 | 				    2*asoc->pathmtu, 4380)); | 
 | 611 | 				trans->ssthresh = asoc->peer.i.a_rwnd; | 
 | 612 | 				trans->rto = asoc->rto_initial; | 
 | 613 | 				trans->rtt = trans->srtt = trans->rttvar = 0; | 
 | 614 | 				sctp_transport_route(trans, NULL, | 
 | 615 | 				    sctp_sk(asoc->base.sk)); | 
 | 616 | 			} | 
 | 617 | 		} | 
 | 618 | 		retval = sctp_send_asconf(asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | 	} | 
 | 620 |  | 
 | 621 | out: | 
 | 622 | 	return retval; | 
 | 623 | } | 
 | 624 |  | 
 | 625 | /* Remove a list of addresses from bind addresses list.  Do not remove the | 
 | 626 |  * last address. | 
 | 627 |  * | 
 | 628 |  * Basically run through each address specified in the addrs/addrcnt | 
 | 629 |  * array/length pair, determine if it is IPv6 or IPv4 and call | 
 | 630 |  * sctp_del_bind() on it. | 
 | 631 |  * | 
 | 632 |  * If any of them fails, then the operation will be reversed and the | 
 | 633 |  * ones that were removed will be added back. | 
 | 634 |  * | 
 | 635 |  * At least one address has to be left; if only one address is | 
 | 636 |  * available, the operation will return -EBUSY. | 
 | 637 |  * | 
 | 638 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 639 |  */ | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 640 | 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] | 641 | { | 
 | 642 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 643 | 	struct sctp_endpoint *ep = sp->ep; | 
 | 644 | 	int cnt; | 
 | 645 | 	struct sctp_bind_addr *bp = &ep->base.bind_addr; | 
 | 646 | 	int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 	void *addr_buf; | 
| Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 648 | 	union sctp_addr *sa_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | 	struct sctp_af *af; | 
 | 650 |  | 
 | 651 | 	SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n", | 
 | 652 | 			  sk, addrs, addrcnt); | 
 | 653 |  | 
 | 654 | 	addr_buf = addrs; | 
 | 655 | 	for (cnt = 0; cnt < addrcnt; cnt++) { | 
 | 656 | 		/* If the bind address list is empty or if there is only one | 
 | 657 | 		 * bind address, there is nothing more to be removed (we need | 
 | 658 | 		 * at least one address here). | 
 | 659 | 		 */ | 
 | 660 | 		if (list_empty(&bp->address_list) || | 
 | 661 | 		    (sctp_list_single_entry(&bp->address_list))) { | 
 | 662 | 			retval = -EBUSY; | 
 | 663 | 			goto err_bindx_rem; | 
 | 664 | 		} | 
 | 665 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 666 | 		sa_addr = addr_buf; | 
| Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 667 | 		af = sctp_get_af_specific(sa_addr->sa.sa_family); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | 		if (!af) { | 
 | 669 | 			retval = -EINVAL; | 
 | 670 | 			goto err_bindx_rem; | 
 | 671 | 		} | 
| Paolo Galtieri | 0304ff8a | 2007-04-17 12:52:36 -0700 | [diff] [blame] | 672 |  | 
 | 673 | 		if (!af->addr_valid(sa_addr, sp, NULL)) { | 
 | 674 | 			retval = -EADDRNOTAVAIL; | 
 | 675 | 			goto err_bindx_rem; | 
 | 676 | 		} | 
 | 677 |  | 
| Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 678 | 		if (sa_addr->v4.sin_port && | 
 | 679 | 		    sa_addr->v4.sin_port != htons(bp->port)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | 			retval = -EINVAL; | 
 | 681 | 			goto err_bindx_rem; | 
 | 682 | 		} | 
 | 683 |  | 
| Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 684 | 		if (!sa_addr->v4.sin_port) | 
 | 685 | 			sa_addr->v4.sin_port = htons(bp->port); | 
 | 686 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | 		/* FIXME - There is probably a need to check if sk->sk_saddr and | 
 | 688 | 		 * sk->sk_rcv_addr are currently set to one of the addresses to | 
 | 689 | 		 * be removed. This is something which needs to be looked into | 
 | 690 | 		 * when we are fixing the outstanding issues with multi-homing | 
 | 691 | 		 * socket routing and failover schemes. Refer to comments in | 
 | 692 | 		 * sctp_do_bind(). -daisy | 
 | 693 | 		 */ | 
| Vlad Yasevich | 0ed90fb | 2007-10-24 16:10:00 -0400 | [diff] [blame] | 694 | 		retval = sctp_del_bind_addr(bp, sa_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 |  | 
 | 696 | 		addr_buf += af->sockaddr_len; | 
 | 697 | err_bindx_rem: | 
 | 698 | 		if (retval < 0) { | 
 | 699 | 			/* Failed. Add the ones that has been removed back */ | 
 | 700 | 			if (cnt > 0) | 
 | 701 | 				sctp_bindx_add(sk, addrs, cnt); | 
 | 702 | 			return retval; | 
 | 703 | 		} | 
 | 704 | 	} | 
 | 705 |  | 
 | 706 | 	return retval; | 
 | 707 | } | 
 | 708 |  | 
 | 709 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of | 
 | 710 |  * the associations that are part of the endpoint indicating that a list of | 
 | 711 |  * local addresses are removed from the endpoint. | 
 | 712 |  * | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 713 |  * 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] | 714 |  * association, we do not send the chunk for that association.  But it will not | 
 | 715 |  * affect other associations. | 
 | 716 |  * | 
 | 717 |  * Only sctp_setsockopt_bindx() is supposed to call this function. | 
 | 718 |  */ | 
 | 719 | static int sctp_send_asconf_del_ip(struct sock		*sk, | 
 | 720 | 				   struct sockaddr	*addrs, | 
 | 721 | 				   int			addrcnt) | 
 | 722 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 723 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | 	struct sctp_sock	*sp; | 
 | 725 | 	struct sctp_endpoint	*ep; | 
 | 726 | 	struct sctp_association	*asoc; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 727 | 	struct sctp_transport	*transport; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | 	struct sctp_bind_addr	*bp; | 
 | 729 | 	struct sctp_chunk	*chunk; | 
 | 730 | 	union sctp_addr		*laddr; | 
 | 731 | 	void			*addr_buf; | 
 | 732 | 	struct sctp_af		*af; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 733 | 	struct sctp_sockaddr_entry *saddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | 	int 			i; | 
 | 735 | 	int 			retval = 0; | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 736 | 	int			stored = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 738 | 	chunk = NULL; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 739 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | 		return retval; | 
 | 741 |  | 
 | 742 | 	sp = sctp_sk(sk); | 
 | 743 | 	ep = sp->ep; | 
 | 744 |  | 
 | 745 | 	SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 746 | 			  __func__, sk, addrs, addrcnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 748 | 	list_for_each_entry(asoc, &ep->asocs, asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 |  | 
 | 750 | 		if (!asoc->peer.asconf_capable) | 
 | 751 | 			continue; | 
 | 752 |  | 
 | 753 | 		if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) | 
 | 754 | 			continue; | 
 | 755 |  | 
 | 756 | 		if (!sctp_state(asoc, ESTABLISHED)) | 
 | 757 | 			continue; | 
 | 758 |  | 
 | 759 | 		/* Check if any address in the packed array of addresses is | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 760 | 		 * not present in the bind address list of the association. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | 		 * If so, do not send the asconf chunk to its peer, but | 
 | 762 | 		 * continue with other associations. | 
 | 763 | 		 */ | 
 | 764 | 		addr_buf = addrs; | 
 | 765 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 766 | 			laddr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | 			af = sctp_get_af_specific(laddr->v4.sin_family); | 
 | 768 | 			if (!af) { | 
 | 769 | 				retval = -EINVAL; | 
 | 770 | 				goto out; | 
 | 771 | 			} | 
 | 772 |  | 
 | 773 | 			if (!sctp_assoc_lookup_laddr(asoc, laddr)) | 
 | 774 | 				break; | 
 | 775 |  | 
 | 776 | 			addr_buf += af->sockaddr_len; | 
 | 777 | 		} | 
 | 778 | 		if (i < addrcnt) | 
 | 779 | 			continue; | 
 | 780 |  | 
 | 781 | 		/* Find one address in the association's bind address list | 
 | 782 | 		 * that is not in the packed array of addresses. This is to | 
 | 783 | 		 * make sure that we do not delete all the addresses in the | 
 | 784 | 		 * association. | 
 | 785 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | 		bp = &asoc->base.bind_addr; | 
 | 787 | 		laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, | 
 | 788 | 					       addrcnt, sp); | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 789 | 		if ((laddr == NULL) && (addrcnt == 1)) { | 
 | 790 | 			if (asoc->asconf_addr_del_pending) | 
 | 791 | 				continue; | 
 | 792 | 			asoc->asconf_addr_del_pending = | 
 | 793 | 			    kzalloc(sizeof(union sctp_addr), GFP_ATOMIC); | 
| Michio Honda | 6d65e5e | 2011-06-10 16:42:14 +0900 | [diff] [blame] | 794 | 			if (asoc->asconf_addr_del_pending == NULL) { | 
 | 795 | 				retval = -ENOMEM; | 
 | 796 | 				goto out; | 
 | 797 | 			} | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 798 | 			asoc->asconf_addr_del_pending->sa.sa_family = | 
 | 799 | 				    addrs->sa_family; | 
 | 800 | 			asoc->asconf_addr_del_pending->v4.sin_port = | 
 | 801 | 				    htons(bp->port); | 
 | 802 | 			if (addrs->sa_family == AF_INET) { | 
 | 803 | 				struct sockaddr_in *sin; | 
 | 804 |  | 
 | 805 | 				sin = (struct sockaddr_in *)addrs; | 
 | 806 | 				asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; | 
 | 807 | 			} else if (addrs->sa_family == AF_INET6) { | 
 | 808 | 				struct sockaddr_in6 *sin6; | 
 | 809 |  | 
 | 810 | 				sin6 = (struct sockaddr_in6 *)addrs; | 
| Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 811 | 				asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 812 | 			} | 
 | 813 | 			SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ", | 
 | 814 | 			    " at %p\n", asoc, asoc->asconf_addr_del_pending, | 
 | 815 | 			    asoc->asconf_addr_del_pending); | 
 | 816 | 			asoc->src_out_of_asoc_ok = 1; | 
 | 817 | 			stored = 1; | 
 | 818 | 			goto skip_mkasconf; | 
 | 819 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 |  | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 821 | 		/* We do not need RCU protection throughout this loop | 
 | 822 | 		 * because this is done under a socket lock from the | 
 | 823 | 		 * setsockopt call. | 
 | 824 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | 		chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, | 
 | 826 | 						   SCTP_PARAM_DEL_IP); | 
 | 827 | 		if (!chunk) { | 
 | 828 | 			retval = -ENOMEM; | 
 | 829 | 			goto out; | 
 | 830 | 		} | 
 | 831 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 832 | skip_mkasconf: | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 833 | 		/* Reset use_as_src flag for the addresses in the bind address | 
 | 834 | 		 * list that are to be deleted. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | 		 */ | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 836 | 		addr_buf = addrs; | 
 | 837 | 		for (i = 0; i < addrcnt; i++) { | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 838 | 			laddr = addr_buf; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 839 | 			af = sctp_get_af_specific(laddr->v4.sin_family); | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 840 | 			list_for_each_entry(saddr, &bp->address_list, list) { | 
| Al Viro | 5f242a1 | 2006-11-20 17:05:23 -0800 | [diff] [blame] | 841 | 				if (sctp_cmp_addr_exact(&saddr->a, laddr)) | 
| Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 842 | 					saddr->state = SCTP_ADDR_DEL; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 843 | 			} | 
 | 844 | 			addr_buf += af->sockaddr_len; | 
 | 845 | 		} | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 846 |  | 
 | 847 | 		/* Update the route and saddr entries for all the transports | 
 | 848 | 		 * as some of the addresses in the bind address list are | 
 | 849 | 		 * about to be deleted and cannot be used as source addresses. | 
 | 850 | 		 */ | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 851 | 		list_for_each_entry(transport, &asoc->peer.transport_addr_list, | 
 | 852 | 					transports) { | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 853 | 			dst_release(transport->dst); | 
 | 854 | 			sctp_transport_route(transport, NULL, | 
 | 855 | 					     sctp_sk(asoc->base.sk)); | 
 | 856 | 		} | 
 | 857 |  | 
| Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 858 | 		if (stored) | 
 | 859 | 			/* We don't need to transmit ASCONF */ | 
 | 860 | 			continue; | 
| Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 861 | 		retval = sctp_send_asconf(asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | 	} | 
 | 863 | out: | 
 | 864 | 	return retval; | 
 | 865 | } | 
 | 866 |  | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 867 | /* set addr events to assocs in the endpoint.  ep and addr_wq must be locked */ | 
 | 868 | int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) | 
 | 869 | { | 
 | 870 | 	struct sock *sk = sctp_opt2sk(sp); | 
 | 871 | 	union sctp_addr *addr; | 
 | 872 | 	struct sctp_af *af; | 
 | 873 |  | 
 | 874 | 	/* It is safe to write port space in caller. */ | 
 | 875 | 	addr = &addrw->a; | 
 | 876 | 	addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); | 
 | 877 | 	af = sctp_get_af_specific(addr->sa.sa_family); | 
 | 878 | 	if (!af) | 
 | 879 | 		return -EINVAL; | 
 | 880 | 	if (sctp_verify_addr(sk, addr, af->sockaddr_len)) | 
 | 881 | 		return -EINVAL; | 
 | 882 |  | 
 | 883 | 	if (addrw->state == SCTP_ADDR_NEW) | 
 | 884 | 		return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); | 
 | 885 | 	else | 
 | 886 | 		return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); | 
 | 887 | } | 
 | 888 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() | 
 | 890 |  * | 
 | 891 |  * API 8.1 | 
 | 892 |  * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, | 
 | 893 |  *                int flags); | 
 | 894 |  * | 
 | 895 |  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 
 | 896 |  * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 
 | 897 |  * or IPv6 addresses. | 
 | 898 |  * | 
 | 899 |  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 
 | 900 |  * Section 3.1.2 for this usage. | 
 | 901 |  * | 
 | 902 |  * addrs is a pointer to an array of one or more socket addresses. Each | 
 | 903 |  * address is contained in its appropriate structure (i.e. struct | 
 | 904 |  * sockaddr_in or struct sockaddr_in6) the family of the address type | 
| Ville Nuorvala | 23c435f | 2006-10-16 22:08:28 -0700 | [diff] [blame] | 905 |  * must be used to distinguish the address length (note that this | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 |  * representation is termed a "packed array" of addresses). The caller | 
 | 907 |  * specifies the number of addresses in the array with addrcnt. | 
 | 908 |  * | 
 | 909 |  * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns | 
 | 910 |  * -1, and sets errno to the appropriate error code. | 
 | 911 |  * | 
 | 912 |  * For SCTP, the port given in each socket address must be the same, or | 
 | 913 |  * sctp_bindx() will fail, setting errno to EINVAL. | 
 | 914 |  * | 
 | 915 |  * The flags parameter is formed from the bitwise OR of zero or more of | 
 | 916 |  * the following currently defined flags: | 
 | 917 |  * | 
 | 918 |  * SCTP_BINDX_ADD_ADDR | 
 | 919 |  * | 
 | 920 |  * SCTP_BINDX_REM_ADDR | 
 | 921 |  * | 
 | 922 |  * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the | 
 | 923 |  * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given | 
 | 924 |  * addresses from the association. The two flags are mutually exclusive; | 
 | 925 |  * if both are given, sctp_bindx() will fail with EINVAL. A caller may | 
 | 926 |  * not remove all addresses from an association; sctp_bindx() will | 
 | 927 |  * reject such an attempt with EINVAL. | 
 | 928 |  * | 
 | 929 |  * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate | 
 | 930 |  * additional addresses with an endpoint after calling bind().  Or use | 
 | 931 |  * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening | 
 | 932 |  * socket is associated with so that no new association accepted will be | 
 | 933 |  * associated with those addresses. If the endpoint supports dynamic | 
 | 934 |  * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a | 
 | 935 |  * endpoint to send the appropriate message to the peer to change the | 
 | 936 |  * peers address lists. | 
 | 937 |  * | 
 | 938 |  * Adding and removing addresses from a connected association is | 
 | 939 |  * optional functionality. Implementations that do not support this | 
 | 940 |  * functionality should return EOPNOTSUPP. | 
 | 941 |  * | 
 | 942 |  * Basically do nothing but copying the addresses from user to kernel | 
 | 943 |  * 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] | 944 |  * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() | 
 | 945 |  * from userspace. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 |  * | 
 | 947 |  * We don't use copy_from_user() for optimization: we first do the | 
 | 948 |  * sanity checks (buffer size -fast- and access check-healthy | 
 | 949 |  * pointer); if all of those succeed, then we can alloc the memory | 
 | 950 |  * (expensive operation) needed to copy the data to kernel. Then we do | 
 | 951 |  * the copying without checking the user space area | 
 | 952 |  * (__copy_from_user()). | 
 | 953 |  * | 
 | 954 |  * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 
 | 955 |  * it. | 
 | 956 |  * | 
 | 957 |  * sk        The sk of the socket | 
 | 958 |  * addrs     The pointer to the addresses in user land | 
 | 959 |  * addrssize Size of the addrs buffer | 
 | 960 |  * op        Operation to perform (add or remove, see the flags of | 
 | 961 |  *           sctp_bindx) | 
 | 962 |  * | 
 | 963 |  * Returns 0 if ok, <0 errno code on error. | 
 | 964 |  */ | 
 | 965 | SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, | 
 | 966 | 				      struct sockaddr __user *addrs, | 
 | 967 | 				      int addrs_size, int op) | 
 | 968 | { | 
 | 969 | 	struct sockaddr *kaddrs; | 
 | 970 | 	int err; | 
 | 971 | 	int addrcnt = 0; | 
 | 972 | 	int walk_size = 0; | 
 | 973 | 	struct sockaddr *sa_addr; | 
 | 974 | 	void *addr_buf; | 
 | 975 | 	struct sctp_af *af; | 
 | 976 |  | 
 | 977 | 	SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p" | 
 | 978 | 			  " addrs_size %d opt %d\n", sk, addrs, addrs_size, op); | 
 | 979 |  | 
 | 980 | 	if (unlikely(addrs_size <= 0)) | 
 | 981 | 		return -EINVAL; | 
 | 982 |  | 
 | 983 | 	/* Check the user passed a healthy pointer.  */ | 
 | 984 | 	if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) | 
 | 985 | 		return -EFAULT; | 
 | 986 |  | 
 | 987 | 	/* Alloc space for the address array in kernel memory.  */ | 
| Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 988 | 	kaddrs = kmalloc(addrs_size, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | 	if (unlikely(!kaddrs)) | 
 | 990 | 		return -ENOMEM; | 
 | 991 |  | 
 | 992 | 	if (__copy_from_user(kaddrs, addrs, addrs_size)) { | 
 | 993 | 		kfree(kaddrs); | 
 | 994 | 		return -EFAULT; | 
 | 995 | 	} | 
 | 996 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 997 | 	/* Walk through the addrs buffer and count the number of addresses. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | 	addr_buf = kaddrs; | 
 | 999 | 	while (walk_size < addrs_size) { | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1000 | 		if (walk_size + sizeof(sa_family_t) > addrs_size) { | 
 | 1001 | 			kfree(kaddrs); | 
 | 1002 | 			return -EINVAL; | 
 | 1003 | 		} | 
 | 1004 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1005 | 		sa_addr = addr_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | 		af = sctp_get_af_specific(sa_addr->sa_family); | 
 | 1007 |  | 
 | 1008 | 		/* If the address family is not supported or if this address | 
 | 1009 | 		 * causes the address buffer to overflow return EINVAL. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1010 | 		 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | 		if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 
 | 1012 | 			kfree(kaddrs); | 
 | 1013 | 			return -EINVAL; | 
 | 1014 | 		} | 
 | 1015 | 		addrcnt++; | 
 | 1016 | 		addr_buf += af->sockaddr_len; | 
 | 1017 | 		walk_size += af->sockaddr_len; | 
 | 1018 | 	} | 
 | 1019 |  | 
 | 1020 | 	/* Do the work. */ | 
 | 1021 | 	switch (op) { | 
 | 1022 | 	case SCTP_BINDX_ADD_ADDR: | 
 | 1023 | 		err = sctp_bindx_add(sk, kaddrs, addrcnt); | 
 | 1024 | 		if (err) | 
 | 1025 | 			goto out; | 
 | 1026 | 		err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); | 
 | 1027 | 		break; | 
 | 1028 |  | 
 | 1029 | 	case SCTP_BINDX_REM_ADDR: | 
 | 1030 | 		err = sctp_bindx_rem(sk, kaddrs, addrcnt); | 
 | 1031 | 		if (err) | 
 | 1032 | 			goto out; | 
 | 1033 | 		err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); | 
 | 1034 | 		break; | 
 | 1035 |  | 
 | 1036 | 	default: | 
 | 1037 | 		err = -EINVAL; | 
 | 1038 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1039 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 |  | 
 | 1041 | out: | 
 | 1042 | 	kfree(kaddrs); | 
 | 1043 |  | 
 | 1044 | 	return err; | 
 | 1045 | } | 
 | 1046 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1047 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) | 
 | 1048 |  * | 
 | 1049 |  * Common routine for handling connect() and sctp_connectx(). | 
 | 1050 |  * Connect will come in with just a single address. | 
 | 1051 |  */ | 
 | 1052 | static int __sctp_connect(struct sock* sk, | 
 | 1053 | 			  struct sockaddr *kaddrs, | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1054 | 			  int addrs_size, | 
 | 1055 | 			  sctp_assoc_t *assoc_id) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1056 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1057 | 	struct net *net = sock_net(sk); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1058 | 	struct sctp_sock *sp; | 
 | 1059 | 	struct sctp_endpoint *ep; | 
 | 1060 | 	struct sctp_association *asoc = NULL; | 
 | 1061 | 	struct sctp_association *asoc2; | 
 | 1062 | 	struct sctp_transport *transport; | 
 | 1063 | 	union sctp_addr to; | 
 | 1064 | 	struct sctp_af *af; | 
 | 1065 | 	sctp_scope_t scope; | 
 | 1066 | 	long timeo; | 
 | 1067 | 	int err = 0; | 
 | 1068 | 	int addrcnt = 0; | 
 | 1069 | 	int walk_size = 0; | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1070 | 	union sctp_addr *sa_addr = NULL; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1071 | 	void *addr_buf; | 
| Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1072 | 	unsigned short port; | 
| Vlad Yasevich | f50f95c | 2007-07-03 12:47:40 -0400 | [diff] [blame] | 1073 | 	unsigned int f_flags = 0; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1074 |  | 
 | 1075 | 	sp = sctp_sk(sk); | 
 | 1076 | 	ep = sp->ep; | 
 | 1077 |  | 
 | 1078 | 	/* connect() cannot be done on a socket that is already in ESTABLISHED | 
 | 1079 | 	 * state - UDP-style peeled off socket or a TCP-style socket that | 
 | 1080 | 	 * is already connected. | 
 | 1081 | 	 * It cannot be done even on a TCP-style listening socket. | 
 | 1082 | 	 */ | 
 | 1083 | 	if (sctp_sstate(sk, ESTABLISHED) || | 
 | 1084 | 	    (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { | 
 | 1085 | 		err = -EISCONN; | 
 | 1086 | 		goto out_free; | 
 | 1087 | 	} | 
 | 1088 |  | 
 | 1089 | 	/* Walk through the addrs buffer and count the number of addresses. */ | 
 | 1090 | 	addr_buf = kaddrs; | 
 | 1091 | 	while (walk_size < addrs_size) { | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1092 | 		if (walk_size + sizeof(sa_family_t) > addrs_size) { | 
 | 1093 | 			err = -EINVAL; | 
 | 1094 | 			goto out_free; | 
 | 1095 | 		} | 
 | 1096 |  | 
| Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1097 | 		sa_addr = addr_buf; | 
| Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 1098 | 		af = sctp_get_af_specific(sa_addr->sa.sa_family); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1099 |  | 
 | 1100 | 		/* If the address family is not supported or if this address | 
 | 1101 | 		 * causes the address buffer to overflow return EINVAL. | 
 | 1102 | 		 */ | 
 | 1103 | 		if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 
 | 1104 | 			err = -EINVAL; | 
 | 1105 | 			goto out_free; | 
 | 1106 | 		} | 
 | 1107 |  | 
| Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1108 | 		port = ntohs(sa_addr->v4.sin_port); | 
 | 1109 |  | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1110 | 		/* Save current address so we can work with it */ | 
 | 1111 | 		memcpy(&to, sa_addr, af->sockaddr_len); | 
 | 1112 |  | 
 | 1113 | 		err = sctp_verify_addr(sk, &to, af->sockaddr_len); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1114 | 		if (err) | 
 | 1115 | 			goto out_free; | 
 | 1116 |  | 
| Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1117 | 		/* Make sure the destination port is correctly set | 
 | 1118 | 		 * in all addresses. | 
 | 1119 | 		 */ | 
 | 1120 | 		if (asoc && asoc->peer.port && asoc->peer.port != port) | 
 | 1121 | 			goto out_free; | 
 | 1122 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1123 |  | 
 | 1124 | 		/* Check if there already is a matching association on the | 
 | 1125 | 		 * endpoint (other than the one created here). | 
 | 1126 | 		 */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1127 | 		asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1128 | 		if (asoc2 && asoc2 != asoc) { | 
 | 1129 | 			if (asoc2->state >= SCTP_STATE_ESTABLISHED) | 
 | 1130 | 				err = -EISCONN; | 
 | 1131 | 			else | 
 | 1132 | 				err = -EALREADY; | 
 | 1133 | 			goto out_free; | 
 | 1134 | 		} | 
 | 1135 |  | 
 | 1136 | 		/* If we could not find a matching association on the endpoint, | 
 | 1137 | 		 * make sure that there is no peeled-off association matching | 
 | 1138 | 		 * the peer address even on another socket. | 
 | 1139 | 		 */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1140 | 		if (sctp_endpoint_is_peeled_off(ep, &to)) { | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1141 | 			err = -EADDRNOTAVAIL; | 
 | 1142 | 			goto out_free; | 
 | 1143 | 		} | 
 | 1144 |  | 
 | 1145 | 		if (!asoc) { | 
 | 1146 | 			/* If a bind() or sctp_bindx() is not called prior to | 
 | 1147 | 			 * an sctp_connectx() call, the system picks an | 
 | 1148 | 			 * ephemeral port and will choose an address set | 
 | 1149 | 			 * equivalent to binding with a wildcard address. | 
 | 1150 | 			 */ | 
 | 1151 | 			if (!ep->base.bind_addr.port) { | 
 | 1152 | 				if (sctp_autobind(sk)) { | 
 | 1153 | 					err = -EAGAIN; | 
 | 1154 | 					goto out_free; | 
 | 1155 | 				} | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1156 | 			} else { | 
 | 1157 | 				/* | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1158 | 				 * If an unprivileged user inherits a 1-many | 
 | 1159 | 				 * style socket with open associations on a | 
 | 1160 | 				 * privileged port, it MAY be permitted to | 
 | 1161 | 				 * accept new associations, but it SHOULD NOT | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1162 | 				 * be permitted to open new associations. | 
 | 1163 | 				 */ | 
 | 1164 | 				if (ep->base.bind_addr.port < PROT_SOCK && | 
 | 1165 | 				    !capable(CAP_NET_BIND_SERVICE)) { | 
 | 1166 | 					err = -EACCES; | 
 | 1167 | 					goto out_free; | 
 | 1168 | 				} | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1169 | 			} | 
 | 1170 |  | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1171 | 			scope = sctp_scope(&to); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1172 | 			asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 
 | 1173 | 			if (!asoc) { | 
 | 1174 | 				err = -ENOMEM; | 
 | 1175 | 				goto out_free; | 
 | 1176 | 			} | 
| Vlad Yasevich | 409b95a | 2009-11-10 08:57:34 +0000 | [diff] [blame] | 1177 |  | 
 | 1178 | 			err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, | 
 | 1179 | 							      GFP_KERNEL); | 
 | 1180 | 			if (err < 0) { | 
 | 1181 | 				goto out_free; | 
 | 1182 | 			} | 
 | 1183 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1184 | 		} | 
 | 1185 |  | 
 | 1186 | 		/* Prime the peer's transport structures.  */ | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1187 | 		transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1188 | 						SCTP_UNKNOWN); | 
 | 1189 | 		if (!transport) { | 
 | 1190 | 			err = -ENOMEM; | 
 | 1191 | 			goto out_free; | 
 | 1192 | 		} | 
 | 1193 |  | 
 | 1194 | 		addrcnt++; | 
 | 1195 | 		addr_buf += af->sockaddr_len; | 
 | 1196 | 		walk_size += af->sockaddr_len; | 
 | 1197 | 	} | 
 | 1198 |  | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1199 | 	/* In case the user of sctp_connectx() wants an association | 
 | 1200 | 	 * id back, assign one now. | 
 | 1201 | 	 */ | 
 | 1202 | 	if (assoc_id) { | 
 | 1203 | 		err = sctp_assoc_set_id(asoc, GFP_KERNEL); | 
 | 1204 | 		if (err < 0) | 
 | 1205 | 			goto out_free; | 
 | 1206 | 	} | 
 | 1207 |  | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1208 | 	err = sctp_primitive_ASSOCIATE(net, asoc, NULL); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1209 | 	if (err < 0) { | 
 | 1210 | 		goto out_free; | 
 | 1211 | 	} | 
 | 1212 |  | 
 | 1213 | 	/* Initialize sk's dport and daddr for getpeername() */ | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1214 | 	inet_sk(sk)->inet_dport = htons(asoc->peer.port); | 
| Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1215 | 	af = sctp_get_af_specific(sa_addr->sa.sa_family); | 
 | 1216 | 	af->to_sk_daddr(sa_addr, sk); | 
| Sridhar Samudrala | 8de8c87 | 2006-05-19 10:58:12 -0700 | [diff] [blame] | 1217 | 	sk->sk_err = 0; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1218 |  | 
| Vlad Yasevich | f50f95c | 2007-07-03 12:47:40 -0400 | [diff] [blame] | 1219 | 	/* in-kernel sockets don't generally have a file allocated to them | 
 | 1220 | 	 * if all they do is call sock_create_kern(). | 
 | 1221 | 	 */ | 
 | 1222 | 	if (sk->sk_socket->file) | 
 | 1223 | 		f_flags = sk->sk_socket->file->f_flags; | 
 | 1224 |  | 
 | 1225 | 	timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); | 
 | 1226 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1227 | 	err = sctp_wait_for_connect(asoc, &timeo); | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1228 | 	if ((err == 0 || err == -EINPROGRESS) && assoc_id) | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1229 | 		*assoc_id = asoc->assoc_id; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1230 |  | 
 | 1231 | 	/* Don't free association on exit. */ | 
 | 1232 | 	asoc = NULL; | 
 | 1233 |  | 
 | 1234 | out_free: | 
 | 1235 |  | 
 | 1236 | 	SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p" | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1237 | 			  " kaddrs: %p err: %d\n", | 
 | 1238 | 			  asoc, kaddrs, err); | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1239 | 	if (asoc) { | 
 | 1240 | 		/* sctp_primitive_ASSOCIATE may have added this association | 
 | 1241 | 		 * To the hash table, try to unhash it, just in case, its a noop | 
 | 1242 | 		 * if it wasn't hashed so we're safe | 
 | 1243 | 		 */ | 
 | 1244 | 		sctp_unhash_established(asoc); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1245 | 		sctp_association_free(asoc); | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1246 | 	} | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1247 | 	return err; | 
 | 1248 | } | 
 | 1249 |  | 
 | 1250 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() | 
 | 1251 |  * | 
 | 1252 |  * API 8.9 | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1253 |  * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, | 
 | 1254 |  * 			sctp_assoc_t *asoc); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1255 |  * | 
 | 1256 |  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 
 | 1257 |  * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 
 | 1258 |  * or IPv6 addresses. | 
 | 1259 |  * | 
 | 1260 |  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 
 | 1261 |  * Section 3.1.2 for this usage. | 
 | 1262 |  * | 
 | 1263 |  * addrs is a pointer to an array of one or more socket addresses. Each | 
 | 1264 |  * address is contained in its appropriate structure (i.e. struct | 
 | 1265 |  * sockaddr_in or struct sockaddr_in6) the family of the address type | 
 | 1266 |  * must be used to distengish the address length (note that this | 
 | 1267 |  * representation is termed a "packed array" of addresses). The caller | 
 | 1268 |  * specifies the number of addresses in the array with addrcnt. | 
 | 1269 |  * | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1270 |  * On success, sctp_connectx() returns 0. It also sets the assoc_id to | 
 | 1271 |  * the association id of the new association.  On failure, sctp_connectx() | 
 | 1272 |  * returns -1, and sets errno to the appropriate error code.  The assoc_id | 
 | 1273 |  * is not touched by the kernel. | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1274 |  * | 
 | 1275 |  * For SCTP, the port given in each socket address must be the same, or | 
 | 1276 |  * sctp_connectx() will fail, setting errno to EINVAL. | 
 | 1277 |  * | 
 | 1278 |  * An application can use sctp_connectx to initiate an association with | 
 | 1279 |  * an endpoint that is multi-homed.  Much like sctp_bindx() this call | 
 | 1280 |  * allows a caller to specify multiple addresses at which a peer can be | 
 | 1281 |  * 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] | 1282 |  * the association is implementation dependent.  This function only | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1283 |  * specifies that the stack will try to make use of all the addresses in | 
 | 1284 |  * the list when needed. | 
 | 1285 |  * | 
 | 1286 |  * Note that the list of addresses passed in is only used for setting up | 
 | 1287 |  * the association.  It does not necessarily equal the set of addresses | 
 | 1288 |  * the peer uses for the resulting association.  If the caller wants to | 
 | 1289 |  * find out the set of peer addresses, it must use sctp_getpaddrs() to | 
 | 1290 |  * retrieve them after the association has been set up. | 
 | 1291 |  * | 
 | 1292 |  * Basically do nothing but copying the addresses from user to kernel | 
 | 1293 |  * land and invoking either sctp_connectx(). This is used for tunneling | 
 | 1294 |  * the sctp_connectx() request through sctp_setsockopt() from userspace. | 
 | 1295 |  * | 
 | 1296 |  * We don't use copy_from_user() for optimization: we first do the | 
 | 1297 |  * sanity checks (buffer size -fast- and access check-healthy | 
 | 1298 |  * pointer); if all of those succeed, then we can alloc the memory | 
 | 1299 |  * (expensive operation) needed to copy the data to kernel. Then we do | 
 | 1300 |  * the copying without checking the user space area | 
 | 1301 |  * (__copy_from_user()). | 
 | 1302 |  * | 
 | 1303 |  * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 
 | 1304 |  * it. | 
 | 1305 |  * | 
 | 1306 |  * sk        The sk of the socket | 
 | 1307 |  * addrs     The pointer to the addresses in user land | 
 | 1308 |  * addrssize Size of the addrs buffer | 
 | 1309 |  * | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1310 |  * Returns >=0 if ok, <0 errno code on error. | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1311 |  */ | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1312 | SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk, | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1313 | 				      struct sockaddr __user *addrs, | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1314 | 				      int addrs_size, | 
 | 1315 | 				      sctp_assoc_t *assoc_id) | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1316 | { | 
 | 1317 | 	int err = 0; | 
 | 1318 | 	struct sockaddr *kaddrs; | 
 | 1319 |  | 
 | 1320 | 	SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 1321 | 			  __func__, sk, addrs, addrs_size); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1322 |  | 
 | 1323 | 	if (unlikely(addrs_size <= 0)) | 
 | 1324 | 		return -EINVAL; | 
 | 1325 |  | 
 | 1326 | 	/* Check the user passed a healthy pointer.  */ | 
 | 1327 | 	if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) | 
 | 1328 | 		return -EFAULT; | 
 | 1329 |  | 
 | 1330 | 	/* Alloc space for the address array in kernel memory.  */ | 
| Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 1331 | 	kaddrs = kmalloc(addrs_size, GFP_KERNEL); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1332 | 	if (unlikely(!kaddrs)) | 
 | 1333 | 		return -ENOMEM; | 
 | 1334 |  | 
 | 1335 | 	if (__copy_from_user(kaddrs, addrs, addrs_size)) { | 
 | 1336 | 		err = -EFAULT; | 
 | 1337 | 	} else { | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1338 | 		err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1339 | 	} | 
 | 1340 |  | 
 | 1341 | 	kfree(kaddrs); | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1342 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1343 | 	return err; | 
 | 1344 | } | 
 | 1345 |  | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1346 | /* | 
 | 1347 |  * This is an older interface.  It's kept for backward compatibility | 
 | 1348 |  * to the option that doesn't provide association id. | 
 | 1349 |  */ | 
 | 1350 | SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk, | 
 | 1351 | 				      struct sockaddr __user *addrs, | 
 | 1352 | 				      int addrs_size) | 
 | 1353 | { | 
 | 1354 | 	return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); | 
 | 1355 | } | 
 | 1356 |  | 
 | 1357 | /* | 
 | 1358 |  * New interface for the API.  The since the API is done with a socket | 
 | 1359 |  * option, to make it simple we feed back the association id is as a return | 
 | 1360 |  * indication to the call.  Error is always negative and association id is | 
 | 1361 |  * always positive. | 
 | 1362 |  */ | 
 | 1363 | SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk, | 
 | 1364 | 				      struct sockaddr __user *addrs, | 
 | 1365 | 				      int addrs_size) | 
 | 1366 | { | 
 | 1367 | 	sctp_assoc_t assoc_id = 0; | 
 | 1368 | 	int err = 0; | 
 | 1369 |  | 
 | 1370 | 	err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); | 
 | 1371 |  | 
 | 1372 | 	if (err) | 
 | 1373 | 		return err; | 
 | 1374 | 	else | 
 | 1375 | 		return assoc_id; | 
 | 1376 | } | 
 | 1377 |  | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1378 | /* | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1379 |  * New (hopefully final) interface for the API. | 
 | 1380 |  * We use the sctp_getaddrs_old structure so that use-space library | 
 | 1381 |  * can avoid any unnecessary allocations.   The only defferent part | 
 | 1382 |  * is that we store the actual length of the address buffer into the | 
 | 1383 |  * addrs_num structure member.  That way we can re-use the existing | 
 | 1384 |  * code. | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1385 |  */ | 
 | 1386 | SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len, | 
 | 1387 | 					char __user *optval, | 
 | 1388 | 					int __user *optlen) | 
 | 1389 | { | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1390 | 	struct sctp_getaddrs_old param; | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1391 | 	sctp_assoc_t assoc_id = 0; | 
 | 1392 | 	int err = 0; | 
 | 1393 |  | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1394 | 	if (len < sizeof(param)) | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1395 | 		return -EINVAL; | 
 | 1396 |  | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1397 | 	if (copy_from_user(¶m, optval, sizeof(param))) | 
 | 1398 | 		return -EFAULT; | 
 | 1399 |  | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1400 | 	err = __sctp_setsockopt_connectx(sk, | 
| Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1401 | 			(struct sockaddr __user *)param.addrs, | 
 | 1402 | 			param.addr_num, &assoc_id); | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1403 |  | 
 | 1404 | 	if (err == 0 || err == -EINPROGRESS) { | 
 | 1405 | 		if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) | 
 | 1406 | 			return -EFAULT; | 
 | 1407 | 		if (put_user(sizeof(assoc_id), optlen)) | 
 | 1408 | 			return -EFAULT; | 
 | 1409 | 	} | 
 | 1410 |  | 
 | 1411 | 	return err; | 
 | 1412 | } | 
 | 1413 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | /* API 3.1.4 close() - UDP Style Syntax | 
 | 1415 |  * Applications use close() to perform graceful shutdown (as described in | 
 | 1416 |  * Section 10.1 of [SCTP]) on ALL the associations currently represented | 
 | 1417 |  * by a UDP-style socket. | 
 | 1418 |  * | 
 | 1419 |  * The syntax is | 
 | 1420 |  * | 
 | 1421 |  *   ret = close(int sd); | 
 | 1422 |  * | 
 | 1423 |  *   sd      - the socket descriptor of the associations to be closed. | 
 | 1424 |  * | 
 | 1425 |  * To gracefully shutdown a specific association represented by the | 
 | 1426 |  * UDP-style socket, an application should use the sendmsg() call, | 
 | 1427 |  * passing no user data, but including the appropriate flag in the | 
 | 1428 |  * ancillary data (see Section xxxx). | 
 | 1429 |  * | 
 | 1430 |  * If sd in the close() call is a branched-off socket representing only | 
 | 1431 |  * one association, the shutdown is performed on that association only. | 
 | 1432 |  * | 
 | 1433 |  * 4.1.6 close() - TCP Style Syntax | 
 | 1434 |  * | 
 | 1435 |  * Applications use close() to gracefully close down an association. | 
 | 1436 |  * | 
 | 1437 |  * The syntax is: | 
 | 1438 |  * | 
 | 1439 |  *    int close(int sd); | 
 | 1440 |  * | 
 | 1441 |  *      sd      - the socket descriptor of the association to be closed. | 
 | 1442 |  * | 
 | 1443 |  * After an application calls close() on a socket descriptor, no further | 
 | 1444 |  * socket operations will succeed on that descriptor. | 
 | 1445 |  * | 
 | 1446 |  * API 7.1.4 SO_LINGER | 
 | 1447 |  * | 
 | 1448 |  * An application using the TCP-style socket can use this option to | 
 | 1449 |  * perform the SCTP ABORT primitive.  The linger option structure is: | 
 | 1450 |  * | 
 | 1451 |  *  struct  linger { | 
 | 1452 |  *     int     l_onoff;                // option on/off | 
 | 1453 |  *     int     l_linger;               // linger time | 
 | 1454 |  * }; | 
 | 1455 |  * | 
 | 1456 |  * To enable the option, set l_onoff to 1.  If the l_linger value is set | 
 | 1457 |  * to 0, calling close() is the same as the ABORT primitive.  If the | 
 | 1458 |  * value is set to a negative value, the setsockopt() call will return | 
 | 1459 |  * an error.  If the value is set to a positive value linger_time, the | 
 | 1460 |  * close() can be blocked for at most linger_time ms.  If the graceful | 
 | 1461 |  * shutdown phase does not finish during this period, close() will | 
 | 1462 |  * return but the graceful shutdown phase continues in the system. | 
 | 1463 |  */ | 
 | 1464 | SCTP_STATIC void sctp_close(struct sock *sk, long timeout) | 
 | 1465 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1466 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | 	struct sctp_endpoint *ep; | 
 | 1468 | 	struct sctp_association *asoc; | 
 | 1469 | 	struct list_head *pos, *temp; | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1470 | 	unsigned int data_was_unread; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 |  | 
 | 1472 | 	SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout); | 
 | 1473 |  | 
 | 1474 | 	sctp_lock_sock(sk); | 
 | 1475 | 	sk->sk_shutdown = SHUTDOWN_MASK; | 
| Vlad Yasevich | bec9640 | 2009-07-30 18:08:28 -0400 | [diff] [blame] | 1476 | 	sk->sk_state = SCTP_SS_CLOSING; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 |  | 
 | 1478 | 	ep = sctp_sk(sk)->ep; | 
 | 1479 |  | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1480 | 	/* Clean up any skbs sitting on the receive queue.  */ | 
 | 1481 | 	data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); | 
 | 1482 | 	data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); | 
 | 1483 |  | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 1484 | 	/* Walk all associations on an endpoint.  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | 	list_for_each_safe(pos, temp, &ep->asocs) { | 
 | 1486 | 		asoc = list_entry(pos, struct sctp_association, asocs); | 
 | 1487 |  | 
 | 1488 | 		if (sctp_style(sk, TCP)) { | 
 | 1489 | 			/* A closed association can still be in the list if | 
 | 1490 | 			 * it belongs to a TCP-style listening socket that is | 
 | 1491 | 			 * not yet accepted. If so, free it. If not, send an | 
 | 1492 | 			 * ABORT or SHUTDOWN based on the linger options. | 
 | 1493 | 			 */ | 
 | 1494 | 			if (sctp_state(asoc, CLOSED)) { | 
 | 1495 | 				sctp_unhash_established(asoc); | 
 | 1496 | 				sctp_association_free(asoc); | 
| Vladislav Yasevich | b89498a | 2006-05-19 14:32:06 -0700 | [diff] [blame] | 1497 | 				continue; | 
 | 1498 | 			} | 
 | 1499 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 |  | 
| Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1501 | 		if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || | 
 | 1502 | 		    !skb_queue_empty(&asoc->ulpq.reasm) || | 
 | 1503 | 		    (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { | 
| Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1504 | 			struct sctp_chunk *chunk; | 
 | 1505 |  | 
 | 1506 | 			chunk = sctp_make_abort_user(asoc, NULL, 0); | 
 | 1507 | 			if (chunk) | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1508 | 				sctp_primitive_ABORT(net, asoc, chunk); | 
| Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1509 | 		} else | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1510 | 			sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | 	} | 
 | 1512 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | 	/* On a TCP-style socket, block for at most linger_time if set. */ | 
 | 1514 | 	if (sctp_style(sk, TCP) && timeout) | 
 | 1515 | 		sctp_wait_for_close(sk, timeout); | 
 | 1516 |  | 
 | 1517 | 	/* This will run the backlog queue.  */ | 
 | 1518 | 	sctp_release_sock(sk); | 
 | 1519 |  | 
 | 1520 | 	/* Supposedly, no process has access to the socket, but | 
 | 1521 | 	 * the net layers still may. | 
 | 1522 | 	 */ | 
 | 1523 | 	sctp_local_bh_disable(); | 
 | 1524 | 	sctp_bh_lock_sock(sk); | 
 | 1525 |  | 
 | 1526 | 	/* Hold the sock, since sk_common_release() will put sock_put() | 
 | 1527 | 	 * and we have just a little more cleanup. | 
 | 1528 | 	 */ | 
 | 1529 | 	sock_hold(sk); | 
 | 1530 | 	sk_common_release(sk); | 
 | 1531 |  | 
 | 1532 | 	sctp_bh_unlock_sock(sk); | 
 | 1533 | 	sctp_local_bh_enable(); | 
 | 1534 |  | 
 | 1535 | 	sock_put(sk); | 
 | 1536 |  | 
 | 1537 | 	SCTP_DBG_OBJCNT_DEC(sock); | 
 | 1538 | } | 
 | 1539 |  | 
 | 1540 | /* Handle EPIPE error. */ | 
 | 1541 | static int sctp_error(struct sock *sk, int flags, int err) | 
 | 1542 | { | 
 | 1543 | 	if (err == -EPIPE) | 
 | 1544 | 		err = sock_error(sk) ? : -EPIPE; | 
 | 1545 | 	if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) | 
 | 1546 | 		send_sig(SIGPIPE, current, 0); | 
 | 1547 | 	return err; | 
 | 1548 | } | 
 | 1549 |  | 
 | 1550 | /* API 3.1.3 sendmsg() - UDP Style Syntax | 
 | 1551 |  * | 
 | 1552 |  * An application uses sendmsg() and recvmsg() calls to transmit data to | 
 | 1553 |  * and receive data from its peer. | 
 | 1554 |  * | 
 | 1555 |  *  ssize_t sendmsg(int socket, const struct msghdr *message, | 
 | 1556 |  *                  int flags); | 
 | 1557 |  * | 
 | 1558 |  *  socket  - the socket descriptor of the endpoint. | 
 | 1559 |  *  message - pointer to the msghdr structure which contains a single | 
 | 1560 |  *            user message and possibly some ancillary data. | 
 | 1561 |  * | 
 | 1562 |  *            See Section 5 for complete description of the data | 
 | 1563 |  *            structures. | 
 | 1564 |  * | 
 | 1565 |  *  flags   - flags sent or received with the user message, see Section | 
 | 1566 |  *            5 for complete description of the flags. | 
 | 1567 |  * | 
 | 1568 |  * Note:  This function could use a rewrite especially when explicit | 
 | 1569 |  * connect support comes in. | 
 | 1570 |  */ | 
 | 1571 | /* BUG:  We do not implement the equivalent of sk_stream_wait_memory(). */ | 
 | 1572 |  | 
 | 1573 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); | 
 | 1574 |  | 
 | 1575 | SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, | 
 | 1576 | 			     struct msghdr *msg, size_t msg_len) | 
 | 1577 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1578 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | 	struct sctp_sock *sp; | 
 | 1580 | 	struct sctp_endpoint *ep; | 
 | 1581 | 	struct sctp_association *new_asoc=NULL, *asoc=NULL; | 
 | 1582 | 	struct sctp_transport *transport, *chunk_tp; | 
 | 1583 | 	struct sctp_chunk *chunk; | 
| Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1584 | 	union sctp_addr to; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | 	struct sockaddr *msg_name = NULL; | 
| Joe Perches | 517aa0b | 2011-05-12 11:27:20 +0000 | [diff] [blame] | 1586 | 	struct sctp_sndrcvinfo default_sinfo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | 	struct sctp_sndrcvinfo *sinfo; | 
 | 1588 | 	struct sctp_initmsg *sinit; | 
 | 1589 | 	sctp_assoc_t associd = 0; | 
 | 1590 | 	sctp_cmsgs_t cmsgs = { NULL }; | 
 | 1591 | 	int err; | 
 | 1592 | 	sctp_scope_t scope; | 
 | 1593 | 	long timeo; | 
 | 1594 | 	__u16 sinfo_flags = 0; | 
 | 1595 | 	struct sctp_datamsg *datamsg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | 	int msg_flags = msg->msg_flags; | 
 | 1597 |  | 
 | 1598 | 	SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n", | 
 | 1599 | 			  sk, msg, msg_len); | 
 | 1600 |  | 
 | 1601 | 	err = 0; | 
 | 1602 | 	sp = sctp_sk(sk); | 
 | 1603 | 	ep = sp->ep; | 
 | 1604 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1605 | 	SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 |  | 
 | 1607 | 	/* We cannot send a message over a TCP-style listening socket. */ | 
 | 1608 | 	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { | 
 | 1609 | 		err = -EPIPE; | 
 | 1610 | 		goto out_nounlock; | 
 | 1611 | 	} | 
 | 1612 |  | 
 | 1613 | 	/* Parse out the SCTP CMSGs.  */ | 
 | 1614 | 	err = sctp_msghdr_parse(msg, &cmsgs); | 
 | 1615 |  | 
 | 1616 | 	if (err) { | 
 | 1617 | 		SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err); | 
 | 1618 | 		goto out_nounlock; | 
 | 1619 | 	} | 
 | 1620 |  | 
 | 1621 | 	/* Fetch the destination address for this packet.  This | 
 | 1622 | 	 * address only selects the association--it is not necessarily | 
 | 1623 | 	 * the address we will send to. | 
 | 1624 | 	 * For a peeled-off socket, msg_name is ignored. | 
 | 1625 | 	 */ | 
 | 1626 | 	if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { | 
 | 1627 | 		int msg_namelen = msg->msg_namelen; | 
 | 1628 |  | 
 | 1629 | 		err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, | 
 | 1630 | 				       msg_namelen); | 
 | 1631 | 		if (err) | 
 | 1632 | 			return err; | 
 | 1633 |  | 
 | 1634 | 		if (msg_namelen > sizeof(to)) | 
 | 1635 | 			msg_namelen = sizeof(to); | 
 | 1636 | 		memcpy(&to, msg->msg_name, msg_namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | 		msg_name = msg->msg_name; | 
 | 1638 | 	} | 
 | 1639 |  | 
 | 1640 | 	sinfo = cmsgs.info; | 
 | 1641 | 	sinit = cmsgs.init; | 
 | 1642 |  | 
 | 1643 | 	/* Did the user specify SNDRCVINFO?  */ | 
 | 1644 | 	if (sinfo) { | 
 | 1645 | 		sinfo_flags = sinfo->sinfo_flags; | 
 | 1646 | 		associd = sinfo->sinfo_assoc_id; | 
 | 1647 | 	} | 
 | 1648 |  | 
 | 1649 | 	SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n", | 
 | 1650 | 			  msg_len, sinfo_flags); | 
 | 1651 |  | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1652 | 	/* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ | 
 | 1653 | 	if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | 		err = -EINVAL; | 
 | 1655 | 		goto out_nounlock; | 
 | 1656 | 	} | 
 | 1657 |  | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1658 | 	/* If SCTP_EOF is set, no data can be sent. Disallow sending zero | 
 | 1659 | 	 * length messages when SCTP_EOF|SCTP_ABORT is not set. | 
 | 1660 | 	 * If SCTP_ABORT is set, the message length could be non zero with | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | 	 * the msg_iov set to the user abort reason. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1662 | 	 */ | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1663 | 	if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || | 
 | 1664 | 	    (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | 		err = -EINVAL; | 
 | 1666 | 		goto out_nounlock; | 
 | 1667 | 	} | 
 | 1668 |  | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1669 | 	/* If SCTP_ADDR_OVER is set, there must be an address | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | 	 * specified in msg_name. | 
 | 1671 | 	 */ | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1672 | 	if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | 		err = -EINVAL; | 
 | 1674 | 		goto out_nounlock; | 
 | 1675 | 	} | 
 | 1676 |  | 
 | 1677 | 	transport = NULL; | 
 | 1678 |  | 
 | 1679 | 	SCTP_DEBUG_PRINTK("About to look up association.\n"); | 
 | 1680 |  | 
 | 1681 | 	sctp_lock_sock(sk); | 
 | 1682 |  | 
 | 1683 | 	/* If a msg_name has been specified, assume this is to be used.  */ | 
 | 1684 | 	if (msg_name) { | 
 | 1685 | 		/* Look for a matching association on the endpoint. */ | 
| Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1686 | 		asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | 		if (!asoc) { | 
 | 1688 | 			/* If we could not find a matching association on the | 
 | 1689 | 			 * endpoint, make sure that it is not a TCP-style | 
 | 1690 | 			 * socket that already has an association or there is | 
 | 1691 | 			 * no peeled-off association on another socket. | 
 | 1692 | 			 */ | 
 | 1693 | 			if ((sctp_style(sk, TCP) && | 
 | 1694 | 			     sctp_sstate(sk, ESTABLISHED)) || | 
| Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1695 | 			    sctp_endpoint_is_peeled_off(ep, &to)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | 				err = -EADDRNOTAVAIL; | 
 | 1697 | 				goto out_unlock; | 
 | 1698 | 			} | 
 | 1699 | 		} | 
 | 1700 | 	} else { | 
 | 1701 | 		asoc = sctp_id2assoc(sk, associd); | 
 | 1702 | 		if (!asoc) { | 
 | 1703 | 			err = -EPIPE; | 
 | 1704 | 			goto out_unlock; | 
 | 1705 | 		} | 
 | 1706 | 	} | 
 | 1707 |  | 
 | 1708 | 	if (asoc) { | 
 | 1709 | 		SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc); | 
 | 1710 |  | 
 | 1711 | 		/* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED | 
 | 1712 | 		 * socket that has an association in CLOSED state. This can | 
 | 1713 | 		 * happen when an accepted socket has an association that is | 
 | 1714 | 		 * already CLOSED. | 
 | 1715 | 		 */ | 
 | 1716 | 		if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { | 
 | 1717 | 			err = -EPIPE; | 
 | 1718 | 			goto out_unlock; | 
 | 1719 | 		} | 
 | 1720 |  | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1721 | 		if (sinfo_flags & SCTP_EOF) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | 			SCTP_DEBUG_PRINTK("Shutting down association: %p\n", | 
 | 1723 | 					  asoc); | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1724 | 			sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | 			err = 0; | 
 | 1726 | 			goto out_unlock; | 
 | 1727 | 		} | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1728 | 		if (sinfo_flags & SCTP_ABORT) { | 
| Sridhar Samudrala | c164a9b | 2006-08-22 11:50:39 -0700 | [diff] [blame] | 1729 |  | 
 | 1730 | 			chunk = sctp_make_abort_user(asoc, msg, msg_len); | 
 | 1731 | 			if (!chunk) { | 
 | 1732 | 				err = -ENOMEM; | 
 | 1733 | 				goto out_unlock; | 
 | 1734 | 			} | 
 | 1735 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | 			SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc); | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1737 | 			sctp_primitive_ABORT(net, asoc, chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | 			err = 0; | 
 | 1739 | 			goto out_unlock; | 
 | 1740 | 		} | 
 | 1741 | 	} | 
 | 1742 |  | 
 | 1743 | 	/* Do we need to create the association?  */ | 
 | 1744 | 	if (!asoc) { | 
 | 1745 | 		SCTP_DEBUG_PRINTK("There is no association yet.\n"); | 
 | 1746 |  | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1747 | 		if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | 			err = -EINVAL; | 
 | 1749 | 			goto out_unlock; | 
 | 1750 | 		} | 
 | 1751 |  | 
 | 1752 | 		/* Check for invalid stream against the stream counts, | 
 | 1753 | 		 * either the default or the user specified stream counts. | 
 | 1754 | 		 */ | 
 | 1755 | 		if (sinfo) { | 
 | 1756 | 			if (!sinit || (sinit && !sinit->sinit_num_ostreams)) { | 
 | 1757 | 				/* Check against the defaults. */ | 
 | 1758 | 				if (sinfo->sinfo_stream >= | 
 | 1759 | 				    sp->initmsg.sinit_num_ostreams) { | 
 | 1760 | 					err = -EINVAL; | 
 | 1761 | 					goto out_unlock; | 
 | 1762 | 				} | 
 | 1763 | 			} else { | 
 | 1764 | 				/* Check against the requested.  */ | 
 | 1765 | 				if (sinfo->sinfo_stream >= | 
 | 1766 | 				    sinit->sinit_num_ostreams) { | 
 | 1767 | 					err = -EINVAL; | 
 | 1768 | 					goto out_unlock; | 
 | 1769 | 				} | 
 | 1770 | 			} | 
 | 1771 | 		} | 
 | 1772 |  | 
 | 1773 | 		/* | 
 | 1774 | 		 * API 3.1.2 bind() - UDP Style Syntax | 
 | 1775 | 		 * If a bind() or sctp_bindx() is not called prior to a | 
 | 1776 | 		 * sendmsg() call that initiates a new association, the | 
 | 1777 | 		 * system picks an ephemeral port and will choose an address | 
 | 1778 | 		 * set equivalent to binding with a wildcard address. | 
 | 1779 | 		 */ | 
 | 1780 | 		if (!ep->base.bind_addr.port) { | 
 | 1781 | 			if (sctp_autobind(sk)) { | 
 | 1782 | 				err = -EAGAIN; | 
 | 1783 | 				goto out_unlock; | 
 | 1784 | 			} | 
| Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1785 | 		} else { | 
 | 1786 | 			/* | 
 | 1787 | 			 * If an unprivileged user inherits a one-to-many | 
 | 1788 | 			 * style socket with open associations on a privileged | 
 | 1789 | 			 * port, it MAY be permitted to accept new associations, | 
 | 1790 | 			 * but it SHOULD NOT be permitted to open new | 
 | 1791 | 			 * associations. | 
 | 1792 | 			 */ | 
 | 1793 | 			if (ep->base.bind_addr.port < PROT_SOCK && | 
 | 1794 | 			    !capable(CAP_NET_BIND_SERVICE)) { | 
 | 1795 | 				err = -EACCES; | 
 | 1796 | 				goto out_unlock; | 
 | 1797 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | 		} | 
 | 1799 |  | 
 | 1800 | 		scope = sctp_scope(&to); | 
 | 1801 | 		new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 
 | 1802 | 		if (!new_asoc) { | 
 | 1803 | 			err = -ENOMEM; | 
 | 1804 | 			goto out_unlock; | 
 | 1805 | 		} | 
 | 1806 | 		asoc = new_asoc; | 
| Vlad Yasevich | 409b95a | 2009-11-10 08:57:34 +0000 | [diff] [blame] | 1807 | 		err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); | 
 | 1808 | 		if (err < 0) { | 
 | 1809 | 			err = -ENOMEM; | 
 | 1810 | 			goto out_free; | 
 | 1811 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 |  | 
 | 1813 | 		/* If the SCTP_INIT ancillary data is specified, set all | 
 | 1814 | 		 * the association init values accordingly. | 
 | 1815 | 		 */ | 
 | 1816 | 		if (sinit) { | 
 | 1817 | 			if (sinit->sinit_num_ostreams) { | 
 | 1818 | 				asoc->c.sinit_num_ostreams = | 
 | 1819 | 					sinit->sinit_num_ostreams; | 
 | 1820 | 			} | 
 | 1821 | 			if (sinit->sinit_max_instreams) { | 
 | 1822 | 				asoc->c.sinit_max_instreams = | 
 | 1823 | 					sinit->sinit_max_instreams; | 
 | 1824 | 			} | 
 | 1825 | 			if (sinit->sinit_max_attempts) { | 
 | 1826 | 				asoc->max_init_attempts | 
 | 1827 | 					= sinit->sinit_max_attempts; | 
 | 1828 | 			} | 
 | 1829 | 			if (sinit->sinit_max_init_timeo) { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1830 | 				asoc->max_init_timeo = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | 				 msecs_to_jiffies(sinit->sinit_max_init_timeo); | 
 | 1832 | 			} | 
 | 1833 | 		} | 
 | 1834 |  | 
 | 1835 | 		/* Prime the peer's transport structures.  */ | 
| Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1836 | 		transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | 		if (!transport) { | 
 | 1838 | 			err = -ENOMEM; | 
 | 1839 | 			goto out_free; | 
 | 1840 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | 	} | 
 | 1842 |  | 
 | 1843 | 	/* ASSERT: we have a valid association at this point.  */ | 
 | 1844 | 	SCTP_DEBUG_PRINTK("We have a valid association.\n"); | 
 | 1845 |  | 
 | 1846 | 	if (!sinfo) { | 
 | 1847 | 		/* If the user didn't specify SNDRCVINFO, make up one with | 
 | 1848 | 		 * some defaults. | 
 | 1849 | 		 */ | 
| Joe Perches | 517aa0b | 2011-05-12 11:27:20 +0000 | [diff] [blame] | 1850 | 		memset(&default_sinfo, 0, sizeof(default_sinfo)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | 		default_sinfo.sinfo_stream = asoc->default_stream; | 
 | 1852 | 		default_sinfo.sinfo_flags = asoc->default_flags; | 
 | 1853 | 		default_sinfo.sinfo_ppid = asoc->default_ppid; | 
 | 1854 | 		default_sinfo.sinfo_context = asoc->default_context; | 
 | 1855 | 		default_sinfo.sinfo_timetolive = asoc->default_timetolive; | 
 | 1856 | 		default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); | 
 | 1857 | 		sinfo = &default_sinfo; | 
 | 1858 | 	} | 
 | 1859 |  | 
 | 1860 | 	/* API 7.1.7, the sndbuf size per association bounds the | 
 | 1861 | 	 * maximum size of data that can be sent in a single send call. | 
 | 1862 | 	 */ | 
 | 1863 | 	if (msg_len > sk->sk_sndbuf) { | 
 | 1864 | 		err = -EMSGSIZE; | 
 | 1865 | 		goto out_free; | 
 | 1866 | 	} | 
 | 1867 |  | 
| Vlad Yasevich | 8a47949 | 2007-06-07 14:21:05 -0400 | [diff] [blame] | 1868 | 	if (asoc->pmtu_pending) | 
| David S. Miller | 02f3d4c | 2012-07-16 03:57:14 -0700 | [diff] [blame] | 1869 | 		sctp_assoc_pending_pmtu(sk, asoc); | 
| Vlad Yasevich | 8a47949 | 2007-06-07 14:21:05 -0400 | [diff] [blame] | 1870 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | 	/* If fragmentation is disabled and the message length exceeds the | 
 | 1872 | 	 * association fragmentation point, return EMSGSIZE.  The I-D | 
 | 1873 | 	 * does not specify what this error is, but this looks like | 
 | 1874 | 	 * a great fit. | 
 | 1875 | 	 */ | 
 | 1876 | 	if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { | 
 | 1877 | 		err = -EMSGSIZE; | 
 | 1878 | 		goto out_free; | 
 | 1879 | 	} | 
 | 1880 |  | 
| Joe Perches | afd7614 | 2011-05-12 09:19:10 +0000 | [diff] [blame] | 1881 | 	/* Check for invalid stream. */ | 
 | 1882 | 	if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) { | 
 | 1883 | 		err = -EINVAL; | 
 | 1884 | 		goto out_free; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | 	} | 
 | 1886 |  | 
 | 1887 | 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | 
 | 1888 | 	if (!sctp_wspace(asoc)) { | 
 | 1889 | 		err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); | 
 | 1890 | 		if (err) | 
 | 1891 | 			goto out_free; | 
 | 1892 | 	} | 
 | 1893 |  | 
 | 1894 | 	/* If an address is passed with the sendto/sendmsg call, it is used | 
 | 1895 | 	 * to override the primary destination address in the TCP model, or | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1896 | 	 * when SCTP_ADDR_OVER flag is set in the UDP model. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | 	 */ | 
 | 1898 | 	if ((sctp_style(sk, TCP) && msg_name) || | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1899 | 	    (sinfo_flags & SCTP_ADDR_OVER)) { | 
| Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1900 | 		chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | 		if (!chunk_tp) { | 
 | 1902 | 			err = -EINVAL; | 
 | 1903 | 			goto out_free; | 
 | 1904 | 		} | 
 | 1905 | 	} else | 
 | 1906 | 		chunk_tp = NULL; | 
 | 1907 |  | 
 | 1908 | 	/* Auto-connect, if we aren't connected already. */ | 
 | 1909 | 	if (sctp_state(asoc, CLOSED)) { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1910 | 		err = sctp_primitive_ASSOCIATE(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | 		if (err < 0) | 
 | 1912 | 			goto out_free; | 
 | 1913 | 		SCTP_DEBUG_PRINTK("We associated primitively.\n"); | 
 | 1914 | 	} | 
 | 1915 |  | 
 | 1916 | 	/* Break the message into multiple chunks of maximum size. */ | 
 | 1917 | 	datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len); | 
 | 1918 | 	if (!datamsg) { | 
 | 1919 | 		err = -ENOMEM; | 
 | 1920 | 		goto out_free; | 
 | 1921 | 	} | 
 | 1922 |  | 
 | 1923 | 	/* Now send the (possibly) fragmented message. */ | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 1924 | 	list_for_each_entry(chunk, &datamsg->chunks, frag_list) { | 
| Florian Westphal | 80445cf | 2008-03-23 22:47:08 -0700 | [diff] [blame] | 1925 | 		sctp_chunk_hold(chunk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 |  | 
 | 1927 | 		/* Do accounting for the write space.  */ | 
 | 1928 | 		sctp_set_owner_w(chunk); | 
 | 1929 |  | 
 | 1930 | 		chunk->transport = chunk_tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | 	} | 
 | 1932 |  | 
| Vlad Yasevich | 9c5c62b | 2009-08-10 13:51:03 -0400 | [diff] [blame] | 1933 | 	/* Send it to the lower layers.  Note:  all chunks | 
 | 1934 | 	 * must either fail or succeed.   The lower layer | 
 | 1935 | 	 * works that way today.  Keep it that way or this | 
 | 1936 | 	 * breaks. | 
 | 1937 | 	 */ | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1938 | 	err = sctp_primitive_SEND(net, asoc, datamsg); | 
| Vlad Yasevich | 9c5c62b | 2009-08-10 13:51:03 -0400 | [diff] [blame] | 1939 | 	/* Did the lower layer accept the chunk? */ | 
 | 1940 | 	if (err) | 
 | 1941 | 		sctp_datamsg_free(datamsg); | 
 | 1942 | 	else | 
 | 1943 | 		sctp_datamsg_put(datamsg); | 
 | 1944 |  | 
 | 1945 | 	SCTP_DEBUG_PRINTK("We sent primitively.\n"); | 
 | 1946 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | 	if (err) | 
 | 1948 | 		goto out_free; | 
 | 1949 | 	else | 
 | 1950 | 		err = msg_len; | 
 | 1951 |  | 
 | 1952 | 	/* If we are already past ASSOCIATE, the lower | 
 | 1953 | 	 * layers are responsible for association cleanup. | 
 | 1954 | 	 */ | 
 | 1955 | 	goto out_unlock; | 
 | 1956 |  | 
 | 1957 | out_free: | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1958 | 	if (new_asoc) { | 
 | 1959 | 		sctp_unhash_established(asoc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | 		sctp_association_free(asoc); | 
| Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1961 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | out_unlock: | 
 | 1963 | 	sctp_release_sock(sk); | 
 | 1964 |  | 
 | 1965 | out_nounlock: | 
 | 1966 | 	return sctp_error(sk, msg_flags, err); | 
 | 1967 |  | 
 | 1968 | #if 0 | 
 | 1969 | do_sock_err: | 
 | 1970 | 	if (msg_len) | 
 | 1971 | 		err = msg_len; | 
 | 1972 | 	else | 
 | 1973 | 		err = sock_error(sk); | 
 | 1974 | 	goto out; | 
 | 1975 |  | 
 | 1976 | do_interrupted: | 
 | 1977 | 	if (msg_len) | 
 | 1978 | 		err = msg_len; | 
 | 1979 | 	goto out; | 
 | 1980 | #endif /* 0 */ | 
 | 1981 | } | 
 | 1982 |  | 
 | 1983 | /* This is an extended version of skb_pull() that removes the data from the | 
 | 1984 |  * start of a skb even when data is spread across the list of skb's in the | 
 | 1985 |  * frag_list. len specifies the total amount of data that needs to be removed. | 
 | 1986 |  * when 'len' bytes could be removed from the skb, it returns 0. | 
 | 1987 |  * If 'len' exceeds the total skb length,  it returns the no. of bytes that | 
 | 1988 |  * could not be removed. | 
 | 1989 |  */ | 
 | 1990 | static int sctp_skb_pull(struct sk_buff *skb, int len) | 
 | 1991 | { | 
 | 1992 | 	struct sk_buff *list; | 
 | 1993 | 	int skb_len = skb_headlen(skb); | 
 | 1994 | 	int rlen; | 
 | 1995 |  | 
 | 1996 | 	if (len <= skb_len) { | 
 | 1997 | 		__skb_pull(skb, len); | 
 | 1998 | 		return 0; | 
 | 1999 | 	} | 
 | 2000 | 	len -= skb_len; | 
 | 2001 | 	__skb_pull(skb, skb_len); | 
 | 2002 |  | 
| David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 2003 | 	skb_walk_frags(skb, list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | 		rlen = sctp_skb_pull(list, len); | 
 | 2005 | 		skb->len -= (len-rlen); | 
 | 2006 | 		skb->data_len -= (len-rlen); | 
 | 2007 |  | 
 | 2008 | 		if (!rlen) | 
 | 2009 | 			return 0; | 
 | 2010 |  | 
 | 2011 | 		len = rlen; | 
 | 2012 | 	} | 
 | 2013 |  | 
 | 2014 | 	return len; | 
 | 2015 | } | 
 | 2016 |  | 
 | 2017 | /* API 3.1.3  recvmsg() - UDP Style Syntax | 
 | 2018 |  * | 
 | 2019 |  *  ssize_t recvmsg(int socket, struct msghdr *message, | 
 | 2020 |  *                    int flags); | 
 | 2021 |  * | 
 | 2022 |  *  socket  - the socket descriptor of the endpoint. | 
 | 2023 |  *  message - pointer to the msghdr structure which contains a single | 
 | 2024 |  *            user message and possibly some ancillary data. | 
 | 2025 |  * | 
 | 2026 |  *            See Section 5 for complete description of the data | 
 | 2027 |  *            structures. | 
 | 2028 |  * | 
 | 2029 |  *  flags   - flags sent or received with the user message, see Section | 
 | 2030 |  *            5 for complete description of the flags. | 
 | 2031 |  */ | 
 | 2032 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *); | 
 | 2033 |  | 
 | 2034 | SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, | 
 | 2035 | 			     struct msghdr *msg, size_t len, int noblock, | 
 | 2036 | 			     int flags, int *addr_len) | 
 | 2037 | { | 
 | 2038 | 	struct sctp_ulpevent *event = NULL; | 
 | 2039 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2040 | 	struct sk_buff *skb; | 
 | 2041 | 	int copied; | 
 | 2042 | 	int err = 0; | 
 | 2043 | 	int skb_len; | 
 | 2044 |  | 
 | 2045 | 	SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: " | 
 | 2046 | 			  "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg, | 
 | 2047 | 			  "len", len, "knoblauch", noblock, | 
 | 2048 | 			  "flags", flags, "addr_len", addr_len); | 
 | 2049 |  | 
 | 2050 | 	sctp_lock_sock(sk); | 
 | 2051 |  | 
 | 2052 | 	if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) { | 
 | 2053 | 		err = -ENOTCONN; | 
 | 2054 | 		goto out; | 
 | 2055 | 	} | 
 | 2056 |  | 
 | 2057 | 	skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); | 
 | 2058 | 	if (!skb) | 
 | 2059 | 		goto out; | 
 | 2060 |  | 
 | 2061 | 	/* Get the total length of the skb including any skb's in the | 
 | 2062 | 	 * frag_list. | 
 | 2063 | 	 */ | 
 | 2064 | 	skb_len = skb->len; | 
 | 2065 |  | 
 | 2066 | 	copied = skb_len; | 
 | 2067 | 	if (copied > len) | 
 | 2068 | 		copied = len; | 
 | 2069 |  | 
 | 2070 | 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | 
 | 2071 |  | 
 | 2072 | 	event = sctp_skb2event(skb); | 
 | 2073 |  | 
 | 2074 | 	if (err) | 
 | 2075 | 		goto out_free; | 
 | 2076 |  | 
| Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 2077 | 	sock_recv_ts_and_drops(msg, sk, skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | 	if (sctp_ulpevent_is_notification(event)) { | 
 | 2079 | 		msg->msg_flags |= MSG_NOTIFICATION; | 
 | 2080 | 		sp->pf->event_msgname(event, msg->msg_name, addr_len); | 
 | 2081 | 	} else { | 
 | 2082 | 		sp->pf->skb_msgname(skb, msg->msg_name, addr_len); | 
 | 2083 | 	} | 
 | 2084 |  | 
 | 2085 | 	/* Check if we allow SCTP_SNDRCVINFO. */ | 
 | 2086 | 	if (sp->subscribe.sctp_data_io_event) | 
 | 2087 | 		sctp_ulpevent_read_sndrcvinfo(event, msg); | 
 | 2088 | #if 0 | 
 | 2089 | 	/* FIXME: we should be calling IP/IPv6 layers.  */ | 
 | 2090 | 	if (sk->sk_protinfo.af_inet.cmsg_flags) | 
 | 2091 | 		ip_cmsg_recv(msg, skb); | 
 | 2092 | #endif | 
 | 2093 |  | 
 | 2094 | 	err = copied; | 
 | 2095 |  | 
 | 2096 | 	/* If skb's length exceeds the user's buffer, update the skb and | 
 | 2097 | 	 * push it back to the receive_queue so that the next call to | 
 | 2098 | 	 * recvmsg() will return the remaining data. Don't set MSG_EOR. | 
 | 2099 | 	 */ | 
 | 2100 | 	if (skb_len > copied) { | 
 | 2101 | 		msg->msg_flags &= ~MSG_EOR; | 
 | 2102 | 		if (flags & MSG_PEEK) | 
 | 2103 | 			goto out_free; | 
 | 2104 | 		sctp_skb_pull(skb, copied); | 
 | 2105 | 		skb_queue_head(&sk->sk_receive_queue, skb); | 
 | 2106 |  | 
 | 2107 | 		/* When only partial message is copied to the user, increase | 
 | 2108 | 		 * rwnd by that amount. If all the data in the skb is read, | 
 | 2109 | 		 * rwnd is updated when the event is freed. | 
 | 2110 | 		 */ | 
| Vlad Yasevich | 0eca8fe | 2008-01-11 10:12:56 -0500 | [diff] [blame] | 2111 | 		if (!sctp_ulpevent_is_notification(event)) | 
 | 2112 | 			sctp_assoc_rwnd_increase(event->asoc, copied); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | 		goto out; | 
 | 2114 | 	} else if ((event->msg_flags & MSG_NOTIFICATION) || | 
 | 2115 | 		   (event->msg_flags & MSG_EOR)) | 
 | 2116 | 		msg->msg_flags |= MSG_EOR; | 
 | 2117 | 	else | 
 | 2118 | 		msg->msg_flags &= ~MSG_EOR; | 
 | 2119 |  | 
 | 2120 | out_free: | 
 | 2121 | 	if (flags & MSG_PEEK) { | 
 | 2122 | 		/* Release the skb reference acquired after peeking the skb in | 
 | 2123 | 		 * sctp_skb_recv_datagram(). | 
 | 2124 | 		 */ | 
 | 2125 | 		kfree_skb(skb); | 
 | 2126 | 	} else { | 
 | 2127 | 		/* Free the event which includes releasing the reference to | 
 | 2128 | 		 * the owner of the skb, freeing the skb and updating the | 
 | 2129 | 		 * rwnd. | 
 | 2130 | 		 */ | 
 | 2131 | 		sctp_ulpevent_free(event); | 
 | 2132 | 	} | 
 | 2133 | out: | 
 | 2134 | 	sctp_release_sock(sk); | 
 | 2135 | 	return err; | 
 | 2136 | } | 
 | 2137 |  | 
 | 2138 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 
 | 2139 |  * | 
 | 2140 |  * This option is a on/off flag.  If enabled no SCTP message | 
 | 2141 |  * fragmentation will be performed.  Instead if a message being sent | 
 | 2142 |  * exceeds the current PMTU size, the message will NOT be sent and | 
 | 2143 |  * instead a error will be indicated to the user. | 
 | 2144 |  */ | 
 | 2145 | static int sctp_setsockopt_disable_fragments(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2146 | 					     char __user *optval, | 
 | 2147 | 					     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | { | 
 | 2149 | 	int val; | 
 | 2150 |  | 
 | 2151 | 	if (optlen < sizeof(int)) | 
 | 2152 | 		return -EINVAL; | 
 | 2153 |  | 
 | 2154 | 	if (get_user(val, (int __user *)optval)) | 
 | 2155 | 		return -EFAULT; | 
 | 2156 |  | 
 | 2157 | 	sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; | 
 | 2158 |  | 
 | 2159 | 	return 0; | 
 | 2160 | } | 
 | 2161 |  | 
 | 2162 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2163 | 				  unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | { | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2165 | 	struct sctp_association *asoc; | 
 | 2166 | 	struct sctp_ulpevent *event; | 
 | 2167 |  | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 2168 | 	if (optlen > sizeof(struct sctp_event_subscribe)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | 		return -EINVAL; | 
 | 2170 | 	if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) | 
 | 2171 | 		return -EFAULT; | 
| Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2172 |  | 
 | 2173 | 	/* | 
 | 2174 | 	 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT, | 
 | 2175 | 	 * if there is no data to be sent or retransmit, the stack will | 
 | 2176 | 	 * immediately send up this notification. | 
 | 2177 | 	 */ | 
 | 2178 | 	if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT, | 
 | 2179 | 				       &sctp_sk(sk)->subscribe)) { | 
 | 2180 | 		asoc = sctp_id2assoc(sk, 0); | 
 | 2181 |  | 
 | 2182 | 		if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { | 
 | 2183 | 			event = sctp_ulpevent_make_sender_dry_event(asoc, | 
 | 2184 | 					GFP_ATOMIC); | 
 | 2185 | 			if (!event) | 
 | 2186 | 				return -ENOMEM; | 
 | 2187 |  | 
 | 2188 | 			sctp_ulpq_tail_event(&asoc->ulpq, event); | 
 | 2189 | 		} | 
 | 2190 | 	} | 
 | 2191 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | 	return 0; | 
 | 2193 | } | 
 | 2194 |  | 
 | 2195 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 
 | 2196 |  * | 
 | 2197 |  * This socket option is applicable to the UDP-style socket only.  When | 
 | 2198 |  * set it will cause associations that are idle for more than the | 
 | 2199 |  * specified number of seconds to automatically close.  An association | 
 | 2200 |  * being idle is defined an association that has NOT sent or received | 
 | 2201 |  * user data.  The special value of '0' indicates that no automatic | 
 | 2202 |  * close of any associations should be performed.  The option expects an | 
 | 2203 |  * integer defining the number of seconds of idle time before an | 
 | 2204 |  * association is closed. | 
 | 2205 |  */ | 
 | 2206 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2207 | 				     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { | 
 | 2209 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2210 |  | 
 | 2211 | 	/* Applicable to UDP-style socket only */ | 
 | 2212 | 	if (sctp_style(sk, TCP)) | 
 | 2213 | 		return -EOPNOTSUPP; | 
 | 2214 | 	if (optlen != sizeof(int)) | 
 | 2215 | 		return -EINVAL; | 
 | 2216 | 	if (copy_from_user(&sp->autoclose, optval, optlen)) | 
 | 2217 | 		return -EFAULT; | 
 | 2218 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | 	return 0; | 
 | 2220 | } | 
 | 2221 |  | 
 | 2222 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 
 | 2223 |  * | 
 | 2224 |  * Applications can enable or disable heartbeats for any peer address of | 
 | 2225 |  * an association, modify an address's heartbeat interval, force a | 
 | 2226 |  * heartbeat to be sent immediately, and adjust the address's maximum | 
 | 2227 |  * number of retransmissions sent before an address is considered | 
 | 2228 |  * unreachable.  The following structure is used to access and modify an | 
 | 2229 |  * address's parameters: | 
 | 2230 |  * | 
 | 2231 |  *  struct sctp_paddrparams { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2232 |  *     sctp_assoc_t            spp_assoc_id; | 
 | 2233 |  *     struct sockaddr_storage spp_address; | 
 | 2234 |  *     uint32_t                spp_hbinterval; | 
 | 2235 |  *     uint16_t                spp_pathmaxrxt; | 
 | 2236 |  *     uint32_t                spp_pathmtu; | 
 | 2237 |  *     uint32_t                spp_sackdelay; | 
 | 2238 |  *     uint32_t                spp_flags; | 
 | 2239 |  * }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2241 |  *   spp_assoc_id    - (one-to-many style socket) This is filled in the | 
 | 2242 |  *                     application, and identifies the association for | 
 | 2243 |  *                     this query. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 |  *   spp_address     - This specifies which address is of interest. | 
 | 2245 |  *   spp_hbinterval  - This contains the value of the heartbeat interval, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2246 |  *                     in milliseconds.  If a  value of zero | 
 | 2247 |  *                     is present in this field then no changes are to | 
 | 2248 |  *                     be made to this parameter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 |  *   spp_pathmaxrxt  - This contains the maximum number of | 
 | 2250 |  *                     retransmissions before this address shall be | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2251 |  *                     considered unreachable. If a  value of zero | 
 | 2252 |  *                     is present in this field then no changes are to | 
 | 2253 |  *                     be made to this parameter. | 
 | 2254 |  *   spp_pathmtu     - When Path MTU discovery is disabled the value | 
 | 2255 |  *                     specified here will be the "fixed" path mtu. | 
 | 2256 |  *                     Note that if the spp_address field is empty | 
 | 2257 |  *                     then all associations on this address will | 
 | 2258 |  *                     have this fixed path mtu set upon them. | 
 | 2259 |  * | 
 | 2260 |  *   spp_sackdelay   - When delayed sack is enabled, this value specifies | 
 | 2261 |  *                     the number of milliseconds that sacks will be delayed | 
 | 2262 |  *                     for. This value will apply to all addresses of an | 
 | 2263 |  *                     association if the spp_address field is empty. Note | 
 | 2264 |  *                     also, that if delayed sack is enabled and this | 
 | 2265 |  *                     value is set to 0, no change is made to the last | 
 | 2266 |  *                     recorded delayed sack timer value. | 
 | 2267 |  * | 
 | 2268 |  *   spp_flags       - These flags are used to control various features | 
 | 2269 |  *                     on an association. The flag field may contain | 
 | 2270 |  *                     zero or more of the following options. | 
 | 2271 |  * | 
 | 2272 |  *                     SPP_HB_ENABLE  - Enable heartbeats on the | 
 | 2273 |  *                     specified address. Note that if the address | 
 | 2274 |  *                     field is empty all addresses for the association | 
 | 2275 |  *                     have heartbeats enabled upon them. | 
 | 2276 |  * | 
 | 2277 |  *                     SPP_HB_DISABLE - Disable heartbeats on the | 
 | 2278 |  *                     speicifed address. Note that if the address | 
 | 2279 |  *                     field is empty all addresses for the association | 
 | 2280 |  *                     will have their heartbeats disabled. Note also | 
 | 2281 |  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are | 
 | 2282 |  *                     mutually exclusive, only one of these two should | 
 | 2283 |  *                     be specified. Enabling both fields will have | 
 | 2284 |  *                     undetermined results. | 
 | 2285 |  * | 
 | 2286 |  *                     SPP_HB_DEMAND - Request a user initiated heartbeat | 
 | 2287 |  *                     to be made immediately. | 
 | 2288 |  * | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2289 |  *                     SPP_HB_TIME_IS_ZERO - Specify's that the time for | 
 | 2290 |  *                     heartbeat delayis to be set to the value of 0 | 
 | 2291 |  *                     milliseconds. | 
 | 2292 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2293 |  *                     SPP_PMTUD_ENABLE - This field will enable PMTU | 
 | 2294 |  *                     discovery upon the specified address. Note that | 
 | 2295 |  *                     if the address feild is empty then all addresses | 
 | 2296 |  *                     on the association are effected. | 
 | 2297 |  * | 
 | 2298 |  *                     SPP_PMTUD_DISABLE - This field will disable PMTU | 
 | 2299 |  *                     discovery upon the specified address. Note that | 
 | 2300 |  *                     if the address feild is empty then all addresses | 
 | 2301 |  *                     on the association are effected. Not also that | 
 | 2302 |  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 
 | 2303 |  *                     exclusive. Enabling both will have undetermined | 
 | 2304 |  *                     results. | 
 | 2305 |  * | 
 | 2306 |  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns | 
 | 2307 |  *                     on delayed sack. The time specified in spp_sackdelay | 
 | 2308 |  *                     is used to specify the sack delay for this address. Note | 
 | 2309 |  *                     that if spp_address is empty then all addresses will | 
 | 2310 |  *                     enable delayed sack and take on the sack delay | 
 | 2311 |  *                     value specified in spp_sackdelay. | 
 | 2312 |  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns | 
 | 2313 |  *                     off delayed sack. If the spp_address field is blank then | 
 | 2314 |  *                     delayed sack is disabled for the entire association. Note | 
 | 2315 |  *                     also that this field is mutually exclusive to | 
 | 2316 |  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined | 
 | 2317 |  *                     results. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 |  */ | 
| Adrian Bunk | 1616436 | 2006-09-18 00:40:38 -0700 | [diff] [blame] | 2319 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, | 
 | 2320 | 				       struct sctp_transport   *trans, | 
 | 2321 | 				       struct sctp_association *asoc, | 
 | 2322 | 				       struct sctp_sock        *sp, | 
 | 2323 | 				       int                      hb_change, | 
 | 2324 | 				       int                      pmtud_change, | 
 | 2325 | 				       int                      sackdelay_change) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | 	int error; | 
 | 2328 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2329 | 	if (params->spp_flags & SPP_HB_DEMAND && trans) { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 2330 | 		struct net *net = sock_net(trans->asoc->base.sk); | 
 | 2331 |  | 
 | 2332 | 		error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | 		if (error) | 
 | 2334 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | 	} | 
 | 2336 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2337 | 	/* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of | 
 | 2338 | 	 * this field is ignored.  Note also that a value of zero indicates | 
 | 2339 | 	 * the current setting should be left unchanged. | 
 | 2340 | 	 */ | 
 | 2341 | 	if (params->spp_flags & SPP_HB_ENABLE) { | 
 | 2342 |  | 
 | 2343 | 		/* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is | 
 | 2344 | 		 * set.  This lets us use 0 value when this flag | 
 | 2345 | 		 * is set. | 
 | 2346 | 		 */ | 
 | 2347 | 		if (params->spp_flags & SPP_HB_TIME_IS_ZERO) | 
 | 2348 | 			params->spp_hbinterval = 0; | 
 | 2349 |  | 
 | 2350 | 		if (params->spp_hbinterval || | 
 | 2351 | 		    (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { | 
 | 2352 | 			if (trans) { | 
 | 2353 | 				trans->hbinterval = | 
 | 2354 | 				    msecs_to_jiffies(params->spp_hbinterval); | 
 | 2355 | 			} else if (asoc) { | 
 | 2356 | 				asoc->hbinterval = | 
 | 2357 | 				    msecs_to_jiffies(params->spp_hbinterval); | 
 | 2358 | 			} else { | 
 | 2359 | 				sp->hbinterval = params->spp_hbinterval; | 
 | 2360 | 			} | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2361 | 		} | 
 | 2362 | 	} | 
 | 2363 |  | 
 | 2364 | 	if (hb_change) { | 
 | 2365 | 		if (trans) { | 
 | 2366 | 			trans->param_flags = | 
 | 2367 | 				(trans->param_flags & ~SPP_HB) | hb_change; | 
 | 2368 | 		} else if (asoc) { | 
 | 2369 | 			asoc->param_flags = | 
 | 2370 | 				(asoc->param_flags & ~SPP_HB) | hb_change; | 
 | 2371 | 		} else { | 
 | 2372 | 			sp->param_flags = | 
 | 2373 | 				(sp->param_flags & ~SPP_HB) | hb_change; | 
 | 2374 | 		} | 
 | 2375 | 	} | 
 | 2376 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2377 | 	/* When Path MTU discovery is disabled the value specified here will | 
 | 2378 | 	 * be the "fixed" path mtu (i.e. the value of the spp_flags field must | 
 | 2379 | 	 * include the flag SPP_PMTUD_DISABLE for this field to have any | 
 | 2380 | 	 * effect). | 
 | 2381 | 	 */ | 
 | 2382 | 	if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2383 | 		if (trans) { | 
 | 2384 | 			trans->pathmtu = params->spp_pathmtu; | 
| David S. Miller | 02f3d4c | 2012-07-16 03:57:14 -0700 | [diff] [blame] | 2385 | 			sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2386 | 		} else if (asoc) { | 
 | 2387 | 			asoc->pathmtu = params->spp_pathmtu; | 
| Vlad Yasevich | f68b2e0 | 2009-09-04 18:21:00 -0400 | [diff] [blame] | 2388 | 			sctp_frag_point(asoc, params->spp_pathmtu); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2389 | 		} else { | 
 | 2390 | 			sp->pathmtu = params->spp_pathmtu; | 
 | 2391 | 		} | 
 | 2392 | 	} | 
 | 2393 |  | 
 | 2394 | 	if (pmtud_change) { | 
 | 2395 | 		if (trans) { | 
 | 2396 | 			int update = (trans->param_flags & SPP_PMTUD_DISABLE) && | 
 | 2397 | 				(params->spp_flags & SPP_PMTUD_ENABLE); | 
 | 2398 | 			trans->param_flags = | 
 | 2399 | 				(trans->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2400 | 			if (update) { | 
| Vlad Yasevich | 9914ae3 | 2011-04-26 21:51:31 +0000 | [diff] [blame] | 2401 | 				sctp_transport_pmtu(trans, sctp_opt2sk(sp)); | 
| David S. Miller | 02f3d4c | 2012-07-16 03:57:14 -0700 | [diff] [blame] | 2402 | 				sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2403 | 			} | 
 | 2404 | 		} else if (asoc) { | 
 | 2405 | 			asoc->param_flags = | 
 | 2406 | 				(asoc->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2407 | 		} else { | 
 | 2408 | 			sp->param_flags = | 
 | 2409 | 				(sp->param_flags & ~SPP_PMTUD) | pmtud_change; | 
 | 2410 | 		} | 
 | 2411 | 	} | 
 | 2412 |  | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2413 | 	/* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the | 
 | 2414 | 	 * value of this field is ignored.  Note also that a value of zero | 
 | 2415 | 	 * indicates the current setting should be left unchanged. | 
 | 2416 | 	 */ | 
 | 2417 | 	if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2418 | 		if (trans) { | 
 | 2419 | 			trans->sackdelay = | 
 | 2420 | 				msecs_to_jiffies(params->spp_sackdelay); | 
 | 2421 | 		} else if (asoc) { | 
 | 2422 | 			asoc->sackdelay = | 
 | 2423 | 				msecs_to_jiffies(params->spp_sackdelay); | 
 | 2424 | 		} else { | 
 | 2425 | 			sp->sackdelay = params->spp_sackdelay; | 
 | 2426 | 		} | 
 | 2427 | 	} | 
 | 2428 |  | 
 | 2429 | 	if (sackdelay_change) { | 
 | 2430 | 		if (trans) { | 
 | 2431 | 			trans->param_flags = | 
 | 2432 | 				(trans->param_flags & ~SPP_SACKDELAY) | | 
 | 2433 | 				sackdelay_change; | 
 | 2434 | 		} else if (asoc) { | 
 | 2435 | 			asoc->param_flags = | 
 | 2436 | 				(asoc->param_flags & ~SPP_SACKDELAY) | | 
 | 2437 | 				sackdelay_change; | 
 | 2438 | 		} else { | 
 | 2439 | 			sp->param_flags = | 
 | 2440 | 				(sp->param_flags & ~SPP_SACKDELAY) | | 
 | 2441 | 				sackdelay_change; | 
 | 2442 | 		} | 
 | 2443 | 	} | 
 | 2444 |  | 
| Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2445 | 	/* Note that a value of zero indicates the current setting should be | 
 | 2446 | 	   left unchanged. | 
| Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2447 | 	 */ | 
| Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2448 | 	if (params->spp_pathmaxrxt) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2449 | 		if (trans) { | 
 | 2450 | 			trans->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2451 | 		} else if (asoc) { | 
 | 2452 | 			asoc->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2453 | 		} else { | 
 | 2454 | 			sp->pathmaxrxt = params->spp_pathmaxrxt; | 
 | 2455 | 		} | 
 | 2456 | 	} | 
 | 2457 |  | 
 | 2458 | 	return 0; | 
 | 2459 | } | 
 | 2460 |  | 
 | 2461 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2462 | 					    char __user *optval, | 
 | 2463 | 					    unsigned int optlen) | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2464 | { | 
 | 2465 | 	struct sctp_paddrparams  params; | 
 | 2466 | 	struct sctp_transport   *trans = NULL; | 
 | 2467 | 	struct sctp_association *asoc = NULL; | 
 | 2468 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 2469 | 	int error; | 
 | 2470 | 	int hb_change, pmtud_change, sackdelay_change; | 
 | 2471 |  | 
 | 2472 | 	if (optlen != sizeof(struct sctp_paddrparams)) | 
 | 2473 | 		return - EINVAL; | 
 | 2474 |  | 
 | 2475 | 	if (copy_from_user(¶ms, optval, optlen)) | 
 | 2476 | 		return -EFAULT; | 
 | 2477 |  | 
 | 2478 | 	/* Validate flags and value parameters. */ | 
 | 2479 | 	hb_change        = params.spp_flags & SPP_HB; | 
 | 2480 | 	pmtud_change     = params.spp_flags & SPP_PMTUD; | 
 | 2481 | 	sackdelay_change = params.spp_flags & SPP_SACKDELAY; | 
 | 2482 |  | 
 | 2483 | 	if (hb_change        == SPP_HB || | 
 | 2484 | 	    pmtud_change     == SPP_PMTUD || | 
 | 2485 | 	    sackdelay_change == SPP_SACKDELAY || | 
 | 2486 | 	    params.spp_sackdelay > 500 || | 
| Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 2487 | 	    (params.spp_pathmtu && | 
 | 2488 | 	     params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2489 | 		return -EINVAL; | 
 | 2490 |  | 
 | 2491 | 	/* If an address other than INADDR_ANY is specified, and | 
 | 2492 | 	 * no transport is found, then the request is invalid. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | 	 */ | 
| Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 2494 | 	if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2495 | 		trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 
 | 2496 | 					       params.spp_assoc_id); | 
 | 2497 | 		if (!trans) | 
 | 2498 | 			return -EINVAL; | 
 | 2499 | 	} | 
 | 2500 |  | 
 | 2501 | 	/* Get association, if assoc_id != 0 and the socket is a one | 
 | 2502 | 	 * to many style socket, and an association was not found, then | 
 | 2503 | 	 * the id was invalid. | 
 | 2504 | 	 */ | 
 | 2505 | 	asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 
 | 2506 | 	if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) | 
 | 2507 | 		return -EINVAL; | 
 | 2508 |  | 
 | 2509 | 	/* Heartbeat demand can only be sent on a transport or | 
 | 2510 | 	 * association, but not a socket. | 
 | 2511 | 	 */ | 
 | 2512 | 	if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) | 
 | 2513 | 		return -EINVAL; | 
 | 2514 |  | 
 | 2515 | 	/* Process parameters. */ | 
 | 2516 | 	error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 
 | 2517 | 					    hb_change, pmtud_change, | 
 | 2518 | 					    sackdelay_change); | 
 | 2519 |  | 
 | 2520 | 	if (error) | 
 | 2521 | 		return error; | 
 | 2522 |  | 
 | 2523 | 	/* If changes are for association, also apply parameters to each | 
 | 2524 | 	 * transport. | 
 | 2525 | 	 */ | 
 | 2526 | 	if (!trans && asoc) { | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2527 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 2528 | 				transports) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2529 | 			sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 
 | 2530 | 						    hb_change, pmtud_change, | 
 | 2531 | 						    sackdelay_change); | 
 | 2532 | 		} | 
 | 2533 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 |  | 
 | 2535 | 	return 0; | 
 | 2536 | } | 
 | 2537 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2538 | /* | 
 | 2539 |  * 7.1.23.  Get or set delayed ack timer (SCTP_DELAYED_SACK) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2540 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2541 |  * This option will effect the way delayed acks are performed.  This | 
 | 2542 |  * option allows you to get or set the delayed ack time, in | 
 | 2543 |  * milliseconds.  It also allows changing the delayed ack frequency. | 
 | 2544 |  * Changing the frequency to 1 disables the delayed sack algorithm.  If | 
 | 2545 |  * the assoc_id is 0, then this sets or gets the endpoints default | 
 | 2546 |  * values.  If the assoc_id field is non-zero, then the set or get | 
 | 2547 |  * effects the specified association for the one to many model (the | 
 | 2548 |  * assoc_id field is ignored by the one to one model).  Note that if | 
 | 2549 |  * sack_delay or sack_freq are 0 when setting this option, then the | 
 | 2550 |  * current values will remain unchanged. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2551 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2552 |  * struct sctp_sack_info { | 
 | 2553 |  *     sctp_assoc_t            sack_assoc_id; | 
 | 2554 |  *     uint32_t                sack_delay; | 
 | 2555 |  *     uint32_t                sack_freq; | 
 | 2556 |  * }; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2557 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2558 |  * sack_assoc_id -  This parameter, indicates which association the user | 
 | 2559 |  *    is performing an action upon.  Note that if this field's value is | 
 | 2560 |  *    zero then the endpoints default value is changed (effecting future | 
 | 2561 |  *    associations only). | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2562 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2563 |  * sack_delay -  This parameter contains the number of milliseconds that | 
 | 2564 |  *    the user is requesting the delayed ACK timer be set to.  Note that | 
 | 2565 |  *    this value is defined in the standard to be between 200 and 500 | 
 | 2566 |  *    milliseconds. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2567 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2568 |  * sack_freq -  This parameter contains the number of packets that must | 
 | 2569 |  *    be received before a sack is sent without waiting for the delay | 
 | 2570 |  *    timer to expire.  The default value for this is 2, setting this | 
 | 2571 |  *    value to 1 will disable the delayed sack algorithm. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2572 |  */ | 
 | 2573 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2574 | static int sctp_setsockopt_delayed_ack(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2575 | 				       char __user *optval, unsigned int optlen) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2576 | { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2577 | 	struct sctp_sack_info    params; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2578 | 	struct sctp_transport   *trans = NULL; | 
 | 2579 | 	struct sctp_association *asoc = NULL; | 
 | 2580 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 2581 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2582 | 	if (optlen == sizeof(struct sctp_sack_info)) { | 
 | 2583 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2584 | 			return -EFAULT; | 
 | 2585 |  | 
 | 2586 | 		if (params.sack_delay == 0 && params.sack_freq == 0) | 
 | 2587 | 			return 0; | 
 | 2588 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 2589 | 		pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); | 
 | 2590 | 		pr_warn("Use struct sctp_sack_info instead\n"); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2591 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 2592 | 			return -EFAULT; | 
 | 2593 |  | 
 | 2594 | 		if (params.sack_delay == 0) | 
 | 2595 | 			params.sack_freq = 1; | 
 | 2596 | 		else | 
 | 2597 | 			params.sack_freq = 0; | 
 | 2598 | 	} else | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2599 | 		return - EINVAL; | 
 | 2600 |  | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2601 | 	/* Validate value parameter. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2602 | 	if (params.sack_delay > 500) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2603 | 		return -EINVAL; | 
 | 2604 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2605 | 	/* 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] | 2606 | 	 * to many style socket, and an association was not found, then | 
 | 2607 | 	 * the id was invalid. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2608 | 	 */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2609 | 	asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 
 | 2610 | 	if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2611 | 		return -EINVAL; | 
 | 2612 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2613 | 	if (params.sack_delay) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2614 | 		if (asoc) { | 
 | 2615 | 			asoc->sackdelay = | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2616 | 				msecs_to_jiffies(params.sack_delay); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2617 | 			asoc->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2618 | 				(asoc->param_flags & ~SPP_SACKDELAY) | | 
 | 2619 | 				SPP_SACKDELAY_ENABLE; | 
 | 2620 | 		} else { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2621 | 			sp->sackdelay = params.sack_delay; | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2622 | 			sp->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2623 | 				(sp->param_flags & ~SPP_SACKDELAY) | | 
 | 2624 | 				SPP_SACKDELAY_ENABLE; | 
 | 2625 | 		} | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2626 | 	} | 
 | 2627 |  | 
 | 2628 | 	if (params.sack_freq == 1) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2629 | 		if (asoc) { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2630 | 			asoc->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2631 | 				(asoc->param_flags & ~SPP_SACKDELAY) | | 
 | 2632 | 				SPP_SACKDELAY_DISABLE; | 
 | 2633 | 		} else { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2634 | 			sp->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2635 | 				(sp->param_flags & ~SPP_SACKDELAY) | | 
 | 2636 | 				SPP_SACKDELAY_DISABLE; | 
 | 2637 | 		} | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2638 | 	} else if (params.sack_freq > 1) { | 
 | 2639 | 		if (asoc) { | 
 | 2640 | 			asoc->sackfreq = params.sack_freq; | 
 | 2641 | 			asoc->param_flags = | 
 | 2642 | 				(asoc->param_flags & ~SPP_SACKDELAY) | | 
 | 2643 | 				SPP_SACKDELAY_ENABLE; | 
 | 2644 | 		} else { | 
 | 2645 | 			sp->sackfreq = params.sack_freq; | 
 | 2646 | 			sp->param_flags = | 
 | 2647 | 				(sp->param_flags & ~SPP_SACKDELAY) | | 
 | 2648 | 				SPP_SACKDELAY_ENABLE; | 
 | 2649 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2650 | 	} | 
 | 2651 |  | 
 | 2652 | 	/* If change is for association, also apply to each transport. */ | 
 | 2653 | 	if (asoc) { | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2654 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 2655 | 				transports) { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2656 | 			if (params.sack_delay) { | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2657 | 				trans->sackdelay = | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2658 | 					msecs_to_jiffies(params.sack_delay); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2659 | 				trans->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2660 | 					(trans->param_flags & ~SPP_SACKDELAY) | | 
 | 2661 | 					SPP_SACKDELAY_ENABLE; | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2662 | 			} | 
| Vlad Yasevich | 7bfe8bd | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 2663 | 			if (params.sack_freq == 1) { | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2664 | 				trans->param_flags = | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2665 | 					(trans->param_flags & ~SPP_SACKDELAY) | | 
 | 2666 | 					SPP_SACKDELAY_DISABLE; | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2667 | 			} else if (params.sack_freq > 1) { | 
 | 2668 | 				trans->sackfreq = params.sack_freq; | 
 | 2669 | 				trans->param_flags = | 
 | 2670 | 					(trans->param_flags & ~SPP_SACKDELAY) | | 
 | 2671 | 					SPP_SACKDELAY_ENABLE; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2672 | 			} | 
 | 2673 | 		} | 
 | 2674 | 	} | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2675 |  | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2676 | 	return 0; | 
 | 2677 | } | 
 | 2678 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 
 | 2680 |  * | 
 | 2681 |  * Applications can specify protocol parameters for the default association | 
 | 2682 |  * initialization.  The option name argument to setsockopt() and getsockopt() | 
 | 2683 |  * is SCTP_INITMSG. | 
 | 2684 |  * | 
 | 2685 |  * Setting initialization parameters is effective only on an unconnected | 
 | 2686 |  * socket (for UDP-style sockets only future associations are effected | 
 | 2687 |  * by the change).  With TCP-style sockets, this option is inherited by | 
 | 2688 |  * sockets derived from a listener socket. | 
 | 2689 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2690 | 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] | 2691 | { | 
 | 2692 | 	struct sctp_initmsg sinit; | 
 | 2693 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2694 |  | 
 | 2695 | 	if (optlen != sizeof(struct sctp_initmsg)) | 
 | 2696 | 		return -EINVAL; | 
 | 2697 | 	if (copy_from_user(&sinit, optval, optlen)) | 
 | 2698 | 		return -EFAULT; | 
 | 2699 |  | 
 | 2700 | 	if (sinit.sinit_num_ostreams) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2701 | 		sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | 	if (sinit.sinit_max_instreams) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2703 | 		sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | 	if (sinit.sinit_max_attempts) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2705 | 		sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | 	if (sinit.sinit_max_init_timeo) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2707 | 		sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 |  | 
 | 2709 | 	return 0; | 
 | 2710 | } | 
 | 2711 |  | 
 | 2712 | /* | 
 | 2713 |  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 
 | 2714 |  * | 
 | 2715 |  *   Applications that wish to use the sendto() system call may wish to | 
 | 2716 |  *   specify a default set of parameters that would normally be supplied | 
 | 2717 |  *   through the inclusion of ancillary data.  This socket option allows | 
 | 2718 |  *   such an application to set the default sctp_sndrcvinfo structure. | 
 | 2719 |  *   The application that wishes to use this socket option simply passes | 
 | 2720 |  *   in to this call the sctp_sndrcvinfo structure defined in Section | 
 | 2721 |  *   5.2.2) The input parameters accepted by this call include | 
 | 2722 |  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 
 | 2723 |  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in | 
 | 2724 |  *   to this call if the caller is using the UDP model. | 
 | 2725 |  */ | 
 | 2726 | static int sctp_setsockopt_default_send_param(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2727 | 					      char __user *optval, | 
 | 2728 | 					      unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | { | 
 | 2730 | 	struct sctp_sndrcvinfo info; | 
 | 2731 | 	struct sctp_association *asoc; | 
 | 2732 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2733 |  | 
 | 2734 | 	if (optlen != sizeof(struct sctp_sndrcvinfo)) | 
 | 2735 | 		return -EINVAL; | 
 | 2736 | 	if (copy_from_user(&info, optval, optlen)) | 
 | 2737 | 		return -EFAULT; | 
 | 2738 |  | 
 | 2739 | 	asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 
 | 2740 | 	if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 
 | 2741 | 		return -EINVAL; | 
 | 2742 |  | 
 | 2743 | 	if (asoc) { | 
 | 2744 | 		asoc->default_stream = info.sinfo_stream; | 
 | 2745 | 		asoc->default_flags = info.sinfo_flags; | 
 | 2746 | 		asoc->default_ppid = info.sinfo_ppid; | 
 | 2747 | 		asoc->default_context = info.sinfo_context; | 
 | 2748 | 		asoc->default_timetolive = info.sinfo_timetolive; | 
 | 2749 | 	} else { | 
 | 2750 | 		sp->default_stream = info.sinfo_stream; | 
 | 2751 | 		sp->default_flags = info.sinfo_flags; | 
 | 2752 | 		sp->default_ppid = info.sinfo_ppid; | 
 | 2753 | 		sp->default_context = info.sinfo_context; | 
 | 2754 | 		sp->default_timetolive = info.sinfo_timetolive; | 
 | 2755 | 	} | 
 | 2756 |  | 
 | 2757 | 	return 0; | 
 | 2758 | } | 
 | 2759 |  | 
 | 2760 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 
 | 2761 |  * | 
 | 2762 |  * Requests that the local SCTP stack use the enclosed peer address as | 
 | 2763 |  * the association primary.  The enclosed address must be one of the | 
 | 2764 |  * association peer's addresses. | 
 | 2765 |  */ | 
 | 2766 | 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] | 2767 | 					unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | { | 
 | 2769 | 	struct sctp_prim prim; | 
 | 2770 | 	struct sctp_transport *trans; | 
 | 2771 |  | 
 | 2772 | 	if (optlen != sizeof(struct sctp_prim)) | 
 | 2773 | 		return -EINVAL; | 
 | 2774 |  | 
 | 2775 | 	if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) | 
 | 2776 | 		return -EFAULT; | 
 | 2777 |  | 
 | 2778 | 	trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); | 
 | 2779 | 	if (!trans) | 
 | 2780 | 		return -EINVAL; | 
 | 2781 |  | 
 | 2782 | 	sctp_assoc_set_primary(trans->asoc, trans); | 
 | 2783 |  | 
 | 2784 | 	return 0; | 
 | 2785 | } | 
 | 2786 |  | 
 | 2787 | /* | 
 | 2788 |  * 7.1.5 SCTP_NODELAY | 
 | 2789 |  * | 
 | 2790 |  * Turn on/off any Nagle-like algorithm.  This means that packets are | 
 | 2791 |  * generally sent as soon as possible and no unnecessary delays are | 
 | 2792 |  * introduced, at the cost of more packets in the network.  Expects an | 
 | 2793 |  *  integer boolean flag. | 
 | 2794 |  */ | 
 | 2795 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2796 | 				   unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | { | 
 | 2798 | 	int val; | 
 | 2799 |  | 
 | 2800 | 	if (optlen < sizeof(int)) | 
 | 2801 | 		return -EINVAL; | 
 | 2802 | 	if (get_user(val, (int __user *)optval)) | 
 | 2803 | 		return -EFAULT; | 
 | 2804 |  | 
 | 2805 | 	sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; | 
 | 2806 | 	return 0; | 
 | 2807 | } | 
 | 2808 |  | 
 | 2809 | /* | 
 | 2810 |  * | 
 | 2811 |  * 7.1.1 SCTP_RTOINFO | 
 | 2812 |  * | 
 | 2813 |  * The protocol parameters used to initialize and bound retransmission | 
 | 2814 |  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 
 | 2815 |  * and modify these parameters. | 
 | 2816 |  * All parameters are time values, in milliseconds.  A value of 0, when | 
 | 2817 |  * modifying the parameters, indicates that the current value should not | 
 | 2818 |  * be changed. | 
 | 2819 |  * | 
 | 2820 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2821 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) | 
 | 2822 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | 	struct sctp_rtoinfo rtoinfo; | 
 | 2824 | 	struct sctp_association *asoc; | 
 | 2825 |  | 
 | 2826 | 	if (optlen != sizeof (struct sctp_rtoinfo)) | 
 | 2827 | 		return -EINVAL; | 
 | 2828 |  | 
 | 2829 | 	if (copy_from_user(&rtoinfo, optval, optlen)) | 
 | 2830 | 		return -EFAULT; | 
 | 2831 |  | 
 | 2832 | 	asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 
 | 2833 |  | 
 | 2834 | 	/* Set the values to the specific association */ | 
 | 2835 | 	if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 
 | 2836 | 		return -EINVAL; | 
 | 2837 |  | 
 | 2838 | 	if (asoc) { | 
 | 2839 | 		if (rtoinfo.srto_initial != 0) | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2840 | 			asoc->rto_initial = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | 				msecs_to_jiffies(rtoinfo.srto_initial); | 
 | 2842 | 		if (rtoinfo.srto_max != 0) | 
 | 2843 | 			asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); | 
 | 2844 | 		if (rtoinfo.srto_min != 0) | 
 | 2845 | 			asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min); | 
 | 2846 | 	} else { | 
 | 2847 | 		/* If there is no association or the association-id = 0 | 
 | 2848 | 		 * set the values to the endpoint. | 
 | 2849 | 		 */ | 
 | 2850 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 2851 |  | 
 | 2852 | 		if (rtoinfo.srto_initial != 0) | 
 | 2853 | 			sp->rtoinfo.srto_initial = rtoinfo.srto_initial; | 
 | 2854 | 		if (rtoinfo.srto_max != 0) | 
 | 2855 | 			sp->rtoinfo.srto_max = rtoinfo.srto_max; | 
 | 2856 | 		if (rtoinfo.srto_min != 0) | 
 | 2857 | 			sp->rtoinfo.srto_min = rtoinfo.srto_min; | 
 | 2858 | 	} | 
 | 2859 |  | 
 | 2860 | 	return 0; | 
 | 2861 | } | 
 | 2862 |  | 
 | 2863 | /* | 
 | 2864 |  * | 
 | 2865 |  * 7.1.2 SCTP_ASSOCINFO | 
 | 2866 |  * | 
| Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 2867 |  * This option is used to tune the maximum retransmission attempts | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 |  * of the association. | 
 | 2869 |  * Returns an error if the new association retransmission value is | 
 | 2870 |  * greater than the sum of the retransmission value  of the peer. | 
 | 2871 |  * See [SCTP] for more information. | 
 | 2872 |  * | 
 | 2873 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2874 | 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] | 2875 | { | 
 | 2876 |  | 
 | 2877 | 	struct sctp_assocparams assocparams; | 
 | 2878 | 	struct sctp_association *asoc; | 
 | 2879 |  | 
 | 2880 | 	if (optlen != sizeof(struct sctp_assocparams)) | 
 | 2881 | 		return -EINVAL; | 
 | 2882 | 	if (copy_from_user(&assocparams, optval, optlen)) | 
 | 2883 | 		return -EFAULT; | 
 | 2884 |  | 
 | 2885 | 	asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 
 | 2886 |  | 
 | 2887 | 	if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 
 | 2888 | 		return -EINVAL; | 
 | 2889 |  | 
 | 2890 | 	/* Set the values to the specific association */ | 
 | 2891 | 	if (asoc) { | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2892 | 		if (assocparams.sasoc_asocmaxrxt != 0) { | 
 | 2893 | 			__u32 path_sum = 0; | 
 | 2894 | 			int   paths = 0; | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2895 | 			struct sctp_transport *peer_addr; | 
 | 2896 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2897 | 			list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, | 
 | 2898 | 					transports) { | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2899 | 				path_sum += peer_addr->pathmaxrxt; | 
 | 2900 | 				paths++; | 
 | 2901 | 			} | 
 | 2902 |  | 
| Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 2903 | 			/* Only validate asocmaxrxt if we have more than | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2904 | 			 * one path/transport.  We do this because path | 
 | 2905 | 			 * retransmissions are only counted when we have more | 
 | 2906 | 			 * then one path. | 
 | 2907 | 			 */ | 
 | 2908 | 			if (paths > 1 && | 
 | 2909 | 			    assocparams.sasoc_asocmaxrxt > path_sum) | 
 | 2910 | 				return -EINVAL; | 
 | 2911 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | 			asoc->max_retrans = assocparams.sasoc_asocmaxrxt; | 
| Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2913 | 		} | 
 | 2914 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | 		if (assocparams.sasoc_cookie_life != 0) { | 
 | 2916 | 			asoc->cookie_life.tv_sec = | 
 | 2917 | 					assocparams.sasoc_cookie_life / 1000; | 
 | 2918 | 			asoc->cookie_life.tv_usec = | 
 | 2919 | 					(assocparams.sasoc_cookie_life % 1000) | 
 | 2920 | 					* 1000; | 
 | 2921 | 		} | 
 | 2922 | 	} else { | 
 | 2923 | 		/* Set the values to the endpoint */ | 
 | 2924 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 2925 |  | 
 | 2926 | 		if (assocparams.sasoc_asocmaxrxt != 0) | 
 | 2927 | 			sp->assocparams.sasoc_asocmaxrxt = | 
 | 2928 | 						assocparams.sasoc_asocmaxrxt; | 
 | 2929 | 		if (assocparams.sasoc_cookie_life != 0) | 
 | 2930 | 			sp->assocparams.sasoc_cookie_life = | 
 | 2931 | 						assocparams.sasoc_cookie_life; | 
 | 2932 | 	} | 
 | 2933 | 	return 0; | 
 | 2934 | } | 
 | 2935 |  | 
 | 2936 | /* | 
 | 2937 |  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 
 | 2938 |  * | 
 | 2939 |  * This socket option is a boolean flag which turns on or off mapped V4 | 
 | 2940 |  * addresses.  If this option is turned on and the socket is type | 
 | 2941 |  * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 
 | 2942 |  * If this option is turned off, then no mapping will be done of V4 | 
 | 2943 |  * addresses and a user will receive both PF_INET6 and PF_INET type | 
 | 2944 |  * addresses on the socket. | 
 | 2945 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2946 | 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] | 2947 | { | 
 | 2948 | 	int val; | 
 | 2949 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2950 |  | 
 | 2951 | 	if (optlen < sizeof(int)) | 
 | 2952 | 		return -EINVAL; | 
 | 2953 | 	if (get_user(val, (int __user *)optval)) | 
 | 2954 | 		return -EFAULT; | 
 | 2955 | 	if (val) | 
 | 2956 | 		sp->v4mapped = 1; | 
 | 2957 | 	else | 
 | 2958 | 		sp->v4mapped = 0; | 
 | 2959 |  | 
 | 2960 | 	return 0; | 
 | 2961 | } | 
 | 2962 |  | 
 | 2963 | /* | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 2964 |  * 8.1.16.  Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 
 | 2965 |  * This option will get or set the maximum size to put in any outgoing | 
 | 2966 |  * 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] | 2967 |  * fragmented by SCTP into the specified size.  Note that the underlying | 
 | 2968 |  * SCTP implementation may fragment into smaller sized chunks when the | 
 | 2969 |  * PMTU of the underlying association is smaller than the value set by | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 2970 |  * the user.  The default value for this option is '0' which indicates | 
 | 2971 |  * the user is NOT limiting fragmentation and only the PMTU will effect | 
 | 2972 |  * SCTP's choice of DATA chunk size.  Note also that values set larger | 
 | 2973 |  * than the maximum size of an IP datagram will effectively let SCTP | 
 | 2974 |  * control fragmentation (i.e. the same as setting this option to 0). | 
 | 2975 |  * | 
 | 2976 |  * The following structure is used to access and modify this parameter: | 
 | 2977 |  * | 
 | 2978 |  * struct sctp_assoc_value { | 
 | 2979 |  *   sctp_assoc_t assoc_id; | 
 | 2980 |  *   uint32_t assoc_value; | 
 | 2981 |  * }; | 
 | 2982 |  * | 
 | 2983 |  * assoc_id:  This parameter is ignored for one-to-one style sockets. | 
 | 2984 |  *    For one-to-many style sockets this parameter indicates which | 
 | 2985 |  *    association the user is performing an action upon.  Note that if | 
 | 2986 |  *    this field's value is zero then the endpoints default value is | 
 | 2987 |  *    changed (effecting future associations only). | 
 | 2988 |  * assoc_value:  This parameter specifies the maximum size in bytes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 |  */ | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2990 | 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] | 2991 | { | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 2992 | 	struct sctp_assoc_value params; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 2995 | 	int val; | 
 | 2996 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 2997 | 	if (optlen == sizeof(int)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 2998 | 		pr_warn("Use of int in maxseg socket option deprecated\n"); | 
 | 2999 | 		pr_warn("Use struct sctp_assoc_value instead\n"); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3000 | 		if (copy_from_user(&val, optval, optlen)) | 
 | 3001 | 			return -EFAULT; | 
 | 3002 | 		params.assoc_id = 0; | 
 | 3003 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
 | 3004 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 3005 | 			return -EFAULT; | 
 | 3006 | 		val = params.assoc_value; | 
 | 3007 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | 		return -EINVAL; | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3009 |  | 
| Ivan Skytte Jorgensen | 96a3399 | 2005-10-28 15:36:12 -0700 | [diff] [blame] | 3010 | 	if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3013 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 3014 | 	if (!asoc && params.assoc_id && sctp_style(sk, UDP)) | 
 | 3015 | 		return -EINVAL; | 
 | 3016 |  | 
 | 3017 | 	if (asoc) { | 
 | 3018 | 		if (val == 0) { | 
 | 3019 | 			val = asoc->pathmtu; | 
 | 3020 | 			val -= sp->pf->af->net_header_len; | 
 | 3021 | 			val -= sizeof(struct sctphdr) + | 
 | 3022 | 					sizeof(struct sctp_data_chunk); | 
 | 3023 | 		} | 
| Vlad Yasevich | f68b2e0 | 2009-09-04 18:21:00 -0400 | [diff] [blame] | 3024 | 		asoc->user_frag = val; | 
 | 3025 | 		asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3026 | 	} else { | 
 | 3027 | 		sp->user_frag = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | 	} | 
 | 3029 |  | 
 | 3030 | 	return 0; | 
 | 3031 | } | 
 | 3032 |  | 
 | 3033 |  | 
 | 3034 | /* | 
 | 3035 |  *  7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) | 
 | 3036 |  * | 
 | 3037 |  *   Requests that the peer mark the enclosed address as the association | 
 | 3038 |  *   primary. The enclosed address must be one of the association's | 
 | 3039 |  *   locally bound addresses. The following structure is used to make a | 
 | 3040 |  *   set primary request: | 
 | 3041 |  */ | 
 | 3042 | 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] | 3043 | 					     unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3045 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | 	struct sctp_sock	*sp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | 	struct sctp_association	*asoc = NULL; | 
 | 3048 | 	struct sctp_setpeerprim	prim; | 
 | 3049 | 	struct sctp_chunk	*chunk; | 
| Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3050 | 	struct sctp_af		*af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | 	int 			err; | 
 | 3052 |  | 
 | 3053 | 	sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3055 | 	if (!net->sctp.addip_enable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | 		return -EPERM; | 
 | 3057 |  | 
 | 3058 | 	if (optlen != sizeof(struct sctp_setpeerprim)) | 
 | 3059 | 		return -EINVAL; | 
 | 3060 |  | 
 | 3061 | 	if (copy_from_user(&prim, optval, optlen)) | 
 | 3062 | 		return -EFAULT; | 
 | 3063 |  | 
 | 3064 | 	asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3065 | 	if (!asoc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | 		return -EINVAL; | 
 | 3067 |  | 
 | 3068 | 	if (!asoc->peer.asconf_capable) | 
 | 3069 | 		return -EPERM; | 
 | 3070 |  | 
 | 3071 | 	if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) | 
 | 3072 | 		return -EPERM; | 
 | 3073 |  | 
 | 3074 | 	if (!sctp_state(asoc, ESTABLISHED)) | 
 | 3075 | 		return -ENOTCONN; | 
 | 3076 |  | 
| Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3077 | 	af = sctp_get_af_specific(prim.sspp_addr.ss_family); | 
 | 3078 | 	if (!af) | 
 | 3079 | 		return -EINVAL; | 
 | 3080 |  | 
 | 3081 | 	if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL)) | 
 | 3082 | 		return -EADDRNOTAVAIL; | 
 | 3083 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | 	if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) | 
 | 3085 | 		return -EADDRNOTAVAIL; | 
 | 3086 |  | 
 | 3087 | 	/* Create an ASCONF chunk with SET_PRIMARY parameter	*/ | 
 | 3088 | 	chunk = sctp_make_asconf_set_prim(asoc, | 
 | 3089 | 					  (union sctp_addr *)&prim.sspp_addr); | 
 | 3090 | 	if (!chunk) | 
 | 3091 | 		return -ENOMEM; | 
 | 3092 |  | 
 | 3093 | 	err = sctp_send_asconf(asoc, chunk); | 
 | 3094 |  | 
 | 3095 | 	SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n"); | 
 | 3096 |  | 
 | 3097 | 	return err; | 
 | 3098 | } | 
 | 3099 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3100 | 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] | 3101 | 					    unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | { | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3103 | 	struct sctp_setadaptation adaptation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3105 | 	if (optlen != sizeof(struct sctp_setadaptation)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | 		return -EINVAL; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3107 | 	if (copy_from_user(&adaptation, optval, optlen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | 		return -EFAULT; | 
 | 3109 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3110 | 	sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 |  | 
 | 3112 | 	return 0; | 
 | 3113 | } | 
 | 3114 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3115 | /* | 
 | 3116 |  * 7.1.29.  Set or Get the default context (SCTP_CONTEXT) | 
 | 3117 |  * | 
 | 3118 |  * The context field in the sctp_sndrcvinfo structure is normally only | 
 | 3119 |  * used when a failed message is retrieved holding the value that was | 
 | 3120 |  * sent down on the actual send call.  This option allows the setting of | 
 | 3121 |  * a default context on an association basis that will be received on | 
 | 3122 |  * reading messages from the peer.  This is especially helpful in the | 
 | 3123 |  * one-2-many model for an application to keep some reference to an | 
 | 3124 |  * internal state machine that is processing messages on the | 
 | 3125 |  * association.  Note that the setting of this value only effects | 
 | 3126 |  * received messages from the peer and does not effect the value that is | 
 | 3127 |  * saved with outbound messages. | 
 | 3128 |  */ | 
 | 3129 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3130 | 				   unsigned int optlen) | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3131 | { | 
 | 3132 | 	struct sctp_assoc_value params; | 
 | 3133 | 	struct sctp_sock *sp; | 
 | 3134 | 	struct sctp_association *asoc; | 
 | 3135 |  | 
 | 3136 | 	if (optlen != sizeof(struct sctp_assoc_value)) | 
 | 3137 | 		return -EINVAL; | 
 | 3138 | 	if (copy_from_user(¶ms, optval, optlen)) | 
 | 3139 | 		return -EFAULT; | 
 | 3140 |  | 
 | 3141 | 	sp = sctp_sk(sk); | 
 | 3142 |  | 
 | 3143 | 	if (params.assoc_id != 0) { | 
 | 3144 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 3145 | 		if (!asoc) | 
 | 3146 | 			return -EINVAL; | 
 | 3147 | 		asoc->default_rcv_context = params.assoc_value; | 
 | 3148 | 	} else { | 
 | 3149 | 		sp->default_rcv_context = params.assoc_value; | 
 | 3150 | 	} | 
 | 3151 |  | 
 | 3152 | 	return 0; | 
 | 3153 | } | 
 | 3154 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3155 | /* | 
 | 3156 |  * 7.1.24.  Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 
 | 3157 |  * | 
 | 3158 |  * This options will at a minimum specify if the implementation is doing | 
 | 3159 |  * fragmented interleave.  Fragmented interleave, for a one to many | 
 | 3160 |  * socket, is when subsequent calls to receive a message may return | 
 | 3161 |  * parts of messages from different associations.  Some implementations | 
 | 3162 |  * may allow you to turn this value on or off.  If so, when turned off, | 
 | 3163 |  * no fragment interleave will occur (which will cause a head of line | 
 | 3164 |  * blocking amongst multiple associations sharing the same one to many | 
 | 3165 |  * socket).  When this option is turned on, then each receive call may | 
 | 3166 |  * come from a different association (thus the user must receive data | 
 | 3167 |  * with the extended calls (e.g. sctp_recvmsg) to keep track of which | 
 | 3168 |  * association each receive belongs to. | 
 | 3169 |  * | 
 | 3170 |  * This option takes a boolean value.  A non-zero value indicates that | 
 | 3171 |  * fragmented interleave is on.  A value of zero indicates that | 
 | 3172 |  * fragmented interleave is off. | 
 | 3173 |  * | 
 | 3174 |  * Note that it is important that an implementation that allows this | 
 | 3175 |  * option to be turned on, have it off by default.  Otherwise an unaware | 
 | 3176 |  * application using the one to many model may become confused and act | 
 | 3177 |  * incorrectly. | 
 | 3178 |  */ | 
 | 3179 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, | 
 | 3180 | 					       char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3181 | 					       unsigned int optlen) | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3182 | { | 
 | 3183 | 	int val; | 
 | 3184 |  | 
 | 3185 | 	if (optlen != sizeof(int)) | 
 | 3186 | 		return -EINVAL; | 
 | 3187 | 	if (get_user(val, (int __user *)optval)) | 
 | 3188 | 		return -EFAULT; | 
 | 3189 |  | 
 | 3190 | 	sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1; | 
 | 3191 |  | 
 | 3192 | 	return 0; | 
 | 3193 | } | 
 | 3194 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3195 | /* | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3196 |  * 8.1.21.  Set or Get the SCTP Partial Delivery Point | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3197 |  *       (SCTP_PARTIAL_DELIVERY_POINT) | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3198 |  * | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3199 |  * This option will set or get the SCTP partial delivery point.  This | 
 | 3200 |  * point is the size of a message where the partial delivery API will be | 
 | 3201 |  * 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] | 3202 |  * lower value will cause partial deliveries to happen more often.  The | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3203 |  * calls argument is an integer that sets or gets the partial delivery | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3204 |  * point.  Note also that the call will fail if the user attempts to set | 
 | 3205 |  * this value larger than the socket receive buffer size. | 
 | 3206 |  * | 
 | 3207 |  * Note that any single message having a length smaller than or equal to | 
 | 3208 |  * the SCTP partial delivery point will be delivered in one single read | 
 | 3209 |  * call as long as the user provided buffer is large enough to hold the | 
 | 3210 |  * message. | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3211 |  */ | 
 | 3212 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, | 
 | 3213 | 						  char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3214 | 						  unsigned int optlen) | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3215 | { | 
 | 3216 | 	u32 val; | 
 | 3217 |  | 
 | 3218 | 	if (optlen != sizeof(u32)) | 
 | 3219 | 		return -EINVAL; | 
 | 3220 | 	if (get_user(val, (int __user *)optval)) | 
 | 3221 | 		return -EFAULT; | 
 | 3222 |  | 
| Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3223 | 	/* Note: We double the receive buffer from what the user sets | 
 | 3224 | 	 * it to be, also initial rwnd is based on rcvbuf/2. | 
 | 3225 | 	 */ | 
 | 3226 | 	if (val > (sk->sk_rcvbuf >> 1)) | 
 | 3227 | 		return -EINVAL; | 
 | 3228 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3229 | 	sctp_sk(sk)->pd_point = val; | 
 | 3230 |  | 
 | 3231 | 	return 0; /* is this the right error code? */ | 
 | 3232 | } | 
 | 3233 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3234 | /* | 
 | 3235 |  * 7.1.28.  Set or Get the maximum burst (SCTP_MAX_BURST) | 
 | 3236 |  * | 
 | 3237 |  * This option will allow a user to change the maximum burst of packets | 
 | 3238 |  * that can be emitted by this association.  Note that the default value | 
 | 3239 |  * is 4, and some implementations may restrict this setting so that it | 
 | 3240 |  * can only be lowered. | 
 | 3241 |  * | 
 | 3242 |  * NOTE: This text doesn't seem right.  Do this on a socket basis with | 
 | 3243 |  * future associations inheriting the socket value. | 
 | 3244 |  */ | 
 | 3245 | static int sctp_setsockopt_maxburst(struct sock *sk, | 
 | 3246 | 				    char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3247 | 				    unsigned int optlen) | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3248 | { | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3249 | 	struct sctp_assoc_value params; | 
 | 3250 | 	struct sctp_sock *sp; | 
 | 3251 | 	struct sctp_association *asoc; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3252 | 	int val; | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3253 | 	int assoc_id = 0; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3254 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3255 | 	if (optlen == sizeof(int)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 3256 | 		pr_warn("Use of int in max_burst socket option deprecated\n"); | 
 | 3257 | 		pr_warn("Use struct sctp_assoc_value instead\n"); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3258 | 		if (copy_from_user(&val, optval, optlen)) | 
 | 3259 | 			return -EFAULT; | 
 | 3260 | 	} else if (optlen == sizeof(struct sctp_assoc_value)) { | 
 | 3261 | 		if (copy_from_user(¶ms, optval, optlen)) | 
 | 3262 | 			return -EFAULT; | 
 | 3263 | 		val = params.assoc_value; | 
 | 3264 | 		assoc_id = params.assoc_id; | 
 | 3265 | 	} else | 
 | 3266 | 		return -EINVAL; | 
 | 3267 |  | 
 | 3268 | 	sp = sctp_sk(sk); | 
 | 3269 |  | 
 | 3270 | 	if (assoc_id != 0) { | 
 | 3271 | 		asoc = sctp_id2assoc(sk, assoc_id); | 
 | 3272 | 		if (!asoc) | 
 | 3273 | 			return -EINVAL; | 
 | 3274 | 		asoc->max_burst = val; | 
 | 3275 | 	} else | 
 | 3276 | 		sp->max_burst = val; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3277 |  | 
 | 3278 | 	return 0; | 
 | 3279 | } | 
 | 3280 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3281 | /* | 
 | 3282 |  * 7.1.18.  Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) | 
 | 3283 |  * | 
 | 3284 |  * This set option adds a chunk type that the user is requesting to be | 
 | 3285 |  * received only in an authenticated way.  Changes to the list of chunks | 
 | 3286 |  * will only effect future associations on the socket. | 
 | 3287 |  */ | 
 | 3288 | static int sctp_setsockopt_auth_chunk(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3289 | 				      char __user *optval, | 
 | 3290 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3291 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3292 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3293 | 	struct sctp_authchunk val; | 
 | 3294 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3295 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3296 | 		return -EACCES; | 
 | 3297 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3298 | 	if (optlen != sizeof(struct sctp_authchunk)) | 
 | 3299 | 		return -EINVAL; | 
 | 3300 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3301 | 		return -EFAULT; | 
 | 3302 |  | 
 | 3303 | 	switch (val.sauth_chunk) { | 
| Joe Perches | 7fd71b1 | 2011-07-01 09:43:11 +0000 | [diff] [blame] | 3304 | 	case SCTP_CID_INIT: | 
 | 3305 | 	case SCTP_CID_INIT_ACK: | 
 | 3306 | 	case SCTP_CID_SHUTDOWN_COMPLETE: | 
 | 3307 | 	case SCTP_CID_AUTH: | 
 | 3308 | 		return -EINVAL; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3309 | 	} | 
 | 3310 |  | 
 | 3311 | 	/* add this chunk id to the endpoint */ | 
 | 3312 | 	return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk); | 
 | 3313 | } | 
 | 3314 |  | 
 | 3315 | /* | 
 | 3316 |  * 7.1.19.  Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) | 
 | 3317 |  * | 
 | 3318 |  * This option gets or sets the list of HMAC algorithms that the local | 
 | 3319 |  * endpoint requires the peer to use. | 
 | 3320 |  */ | 
 | 3321 | static int sctp_setsockopt_hmac_ident(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3322 | 				      char __user *optval, | 
 | 3323 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3324 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3325 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3326 | 	struct sctp_hmacalgo *hmacs; | 
| Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3327 | 	u32 idents; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3328 | 	int err; | 
 | 3329 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3330 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3331 | 		return -EACCES; | 
 | 3332 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3333 | 	if (optlen < sizeof(struct sctp_hmacalgo)) | 
 | 3334 | 		return -EINVAL; | 
 | 3335 |  | 
| Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3336 | 	hmacs= memdup_user(optval, optlen); | 
 | 3337 | 	if (IS_ERR(hmacs)) | 
 | 3338 | 		return PTR_ERR(hmacs); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3339 |  | 
| Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3340 | 	idents = hmacs->shmac_num_idents; | 
 | 3341 | 	if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || | 
 | 3342 | 	    (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3343 | 		err = -EINVAL; | 
 | 3344 | 		goto out; | 
 | 3345 | 	} | 
 | 3346 |  | 
 | 3347 | 	err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs); | 
 | 3348 | out: | 
 | 3349 | 	kfree(hmacs); | 
 | 3350 | 	return err; | 
 | 3351 | } | 
 | 3352 |  | 
 | 3353 | /* | 
 | 3354 |  * 7.1.20.  Set a shared key (SCTP_AUTH_KEY) | 
 | 3355 |  * | 
 | 3356 |  * This option will set a shared secret key which is used to build an | 
 | 3357 |  * association shared key. | 
 | 3358 |  */ | 
 | 3359 | static int sctp_setsockopt_auth_key(struct sock *sk, | 
 | 3360 | 				    char __user *optval, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3361 | 				    unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3362 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3363 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3364 | 	struct sctp_authkey *authkey; | 
 | 3365 | 	struct sctp_association *asoc; | 
 | 3366 | 	int ret; | 
 | 3367 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3368 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3369 | 		return -EACCES; | 
 | 3370 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3371 | 	if (optlen <= sizeof(struct sctp_authkey)) | 
 | 3372 | 		return -EINVAL; | 
 | 3373 |  | 
| Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3374 | 	authkey= memdup_user(optval, optlen); | 
 | 3375 | 	if (IS_ERR(authkey)) | 
 | 3376 | 		return PTR_ERR(authkey); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3377 |  | 
| Vlad Yasevich | 328fc47 | 2008-08-27 16:08:54 -0700 | [diff] [blame] | 3378 | 	if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { | 
| Vlad Yasevich | 30c2235 | 2008-08-25 15:16:19 -0700 | [diff] [blame] | 3379 | 		ret = -EINVAL; | 
 | 3380 | 		goto out; | 
 | 3381 | 	} | 
 | 3382 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3383 | 	asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); | 
 | 3384 | 	if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { | 
 | 3385 | 		ret = -EINVAL; | 
 | 3386 | 		goto out; | 
 | 3387 | 	} | 
 | 3388 |  | 
 | 3389 | 	ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey); | 
 | 3390 | out: | 
 | 3391 | 	kfree(authkey); | 
 | 3392 | 	return ret; | 
 | 3393 | } | 
 | 3394 |  | 
 | 3395 | /* | 
 | 3396 |  * 7.1.21.  Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) | 
 | 3397 |  * | 
 | 3398 |  * This option will get or set the active shared key to be used to build | 
 | 3399 |  * the association shared key. | 
 | 3400 |  */ | 
 | 3401 | static int sctp_setsockopt_active_key(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3402 | 				      char __user *optval, | 
 | 3403 | 				      unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3404 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3405 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3406 | 	struct sctp_authkeyid val; | 
 | 3407 | 	struct sctp_association *asoc; | 
 | 3408 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3409 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3410 | 		return -EACCES; | 
 | 3411 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3412 | 	if (optlen != sizeof(struct sctp_authkeyid)) | 
 | 3413 | 		return -EINVAL; | 
 | 3414 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3415 | 		return -EFAULT; | 
 | 3416 |  | 
 | 3417 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 3418 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 3419 | 		return -EINVAL; | 
 | 3420 |  | 
 | 3421 | 	return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc, | 
 | 3422 | 					val.scact_keynumber); | 
 | 3423 | } | 
 | 3424 |  | 
 | 3425 | /* | 
 | 3426 |  * 7.1.22.  Delete a shared key (SCTP_AUTH_DELETE_KEY) | 
 | 3427 |  * | 
 | 3428 |  * This set option will delete a shared secret key from use. | 
 | 3429 |  */ | 
 | 3430 | static int sctp_setsockopt_del_key(struct sock *sk, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3431 | 				   char __user *optval, | 
 | 3432 | 				   unsigned int optlen) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3433 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3434 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3435 | 	struct sctp_authkeyid val; | 
 | 3436 | 	struct sctp_association *asoc; | 
 | 3437 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3438 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3439 | 		return -EACCES; | 
 | 3440 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3441 | 	if (optlen != sizeof(struct sctp_authkeyid)) | 
 | 3442 | 		return -EINVAL; | 
 | 3443 | 	if (copy_from_user(&val, optval, optlen)) | 
 | 3444 | 		return -EFAULT; | 
 | 3445 |  | 
 | 3446 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 3447 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 3448 | 		return -EINVAL; | 
 | 3449 |  | 
 | 3450 | 	return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc, | 
 | 3451 | 				    val.scact_keynumber); | 
 | 3452 |  | 
 | 3453 | } | 
 | 3454 |  | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3455 | /* | 
 | 3456 |  * 8.1.23 SCTP_AUTO_ASCONF | 
 | 3457 |  * | 
 | 3458 |  * This option will enable or disable the use of the automatic generation of | 
 | 3459 |  * ASCONF chunks to add and delete addresses to an existing association.  Note | 
 | 3460 |  * that this option has two caveats namely: a) it only affects sockets that | 
 | 3461 |  * are bound to all addresses available to the SCTP stack, and b) the system | 
 | 3462 |  * administrator may have an overriding control that turns the ASCONF feature | 
 | 3463 |  * off no matter what setting the socket option may have. | 
 | 3464 |  * This option expects an integer boolean flag, where a non-zero value turns on | 
 | 3465 |  * the option, and a zero value turns off the option. | 
 | 3466 |  * Note. In this implementation, socket operation overrides default parameter | 
 | 3467 |  * being set by sysctl as well as FreeBSD implementation | 
 | 3468 |  */ | 
 | 3469 | static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval, | 
 | 3470 | 					unsigned int optlen) | 
 | 3471 | { | 
 | 3472 | 	int val; | 
 | 3473 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 3474 |  | 
 | 3475 | 	if (optlen < sizeof(int)) | 
 | 3476 | 		return -EINVAL; | 
 | 3477 | 	if (get_user(val, (int __user *)optval)) | 
 | 3478 | 		return -EFAULT; | 
 | 3479 | 	if (!sctp_is_ep_boundall(sk) && val) | 
 | 3480 | 		return -EINVAL; | 
 | 3481 | 	if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf)) | 
 | 3482 | 		return 0; | 
 | 3483 |  | 
 | 3484 | 	if (val == 0 && sp->do_auto_asconf) { | 
 | 3485 | 		list_del(&sp->auto_asconf_list); | 
 | 3486 | 		sp->do_auto_asconf = 0; | 
 | 3487 | 	} else if (val && !sp->do_auto_asconf) { | 
 | 3488 | 		list_add_tail(&sp->auto_asconf_list, | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 3489 | 		    &sock_net(sk)->sctp.auto_asconf_splist); | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3490 | 		sp->do_auto_asconf = 1; | 
 | 3491 | 	} | 
 | 3492 | 	return 0; | 
 | 3493 | } | 
 | 3494 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3495 |  | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3496 | /* | 
 | 3497 |  * SCTP_PEER_ADDR_THLDS | 
 | 3498 |  * | 
 | 3499 |  * This option allows us to alter the partially failed threshold for one or all | 
 | 3500 |  * transports in an association.  See Section 6.1 of: | 
 | 3501 |  * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt | 
 | 3502 |  */ | 
 | 3503 | static int sctp_setsockopt_paddr_thresholds(struct sock *sk, | 
 | 3504 | 					    char __user *optval, | 
 | 3505 | 					    unsigned int optlen) | 
 | 3506 | { | 
 | 3507 | 	struct sctp_paddrthlds val; | 
 | 3508 | 	struct sctp_transport *trans; | 
 | 3509 | 	struct sctp_association *asoc; | 
 | 3510 |  | 
 | 3511 | 	if (optlen < sizeof(struct sctp_paddrthlds)) | 
 | 3512 | 		return -EINVAL; | 
 | 3513 | 	if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, | 
 | 3514 | 			   sizeof(struct sctp_paddrthlds))) | 
 | 3515 | 		return -EFAULT; | 
 | 3516 |  | 
 | 3517 |  | 
 | 3518 | 	if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { | 
 | 3519 | 		asoc = sctp_id2assoc(sk, val.spt_assoc_id); | 
 | 3520 | 		if (!asoc) | 
 | 3521 | 			return -ENOENT; | 
 | 3522 | 		list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 
 | 3523 | 				    transports) { | 
 | 3524 | 			if (val.spt_pathmaxrxt) | 
 | 3525 | 				trans->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3526 | 			trans->pf_retrans = val.spt_pathpfthld; | 
 | 3527 | 		} | 
 | 3528 |  | 
 | 3529 | 		if (val.spt_pathmaxrxt) | 
 | 3530 | 			asoc->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3531 | 		asoc->pf_retrans = val.spt_pathpfthld; | 
 | 3532 | 	} else { | 
 | 3533 | 		trans = sctp_addr_id2transport(sk, &val.spt_address, | 
 | 3534 | 					       val.spt_assoc_id); | 
 | 3535 | 		if (!trans) | 
 | 3536 | 			return -ENOENT; | 
 | 3537 |  | 
 | 3538 | 		if (val.spt_pathmaxrxt) | 
 | 3539 | 			trans->pathmaxrxt = val.spt_pathmaxrxt; | 
 | 3540 | 		trans->pf_retrans = val.spt_pathpfthld; | 
 | 3541 | 	} | 
 | 3542 |  | 
 | 3543 | 	return 0; | 
 | 3544 | } | 
 | 3545 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | /* API 6.2 setsockopt(), getsockopt() | 
 | 3547 |  * | 
 | 3548 |  * Applications use setsockopt() and getsockopt() to set or retrieve | 
 | 3549 |  * socket options.  Socket options are used to change the default | 
 | 3550 |  * behavior of sockets calls.  They are described in Section 7. | 
 | 3551 |  * | 
 | 3552 |  * The syntax is: | 
 | 3553 |  * | 
 | 3554 |  *   ret = getsockopt(int sd, int level, int optname, void __user *optval, | 
 | 3555 |  *                    int __user *optlen); | 
 | 3556 |  *   ret = setsockopt(int sd, int level, int optname, const void __user *optval, | 
 | 3557 |  *                    int optlen); | 
 | 3558 |  * | 
 | 3559 |  *   sd      - the socket descript. | 
 | 3560 |  *   level   - set to IPPROTO_SCTP for all SCTP options. | 
 | 3561 |  *   optname - the option name. | 
 | 3562 |  *   optval  - the buffer to store the value of the option. | 
 | 3563 |  *   optlen  - the size of the buffer. | 
 | 3564 |  */ | 
 | 3565 | SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname, | 
| David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3566 | 				char __user *optval, unsigned int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | { | 
 | 3568 | 	int retval = 0; | 
 | 3569 |  | 
 | 3570 | 	SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n", | 
 | 3571 | 			  sk, optname); | 
 | 3572 |  | 
 | 3573 | 	/* I can hardly begin to describe how wrong this is.  This is | 
 | 3574 | 	 * so broken as to be worse than useless.  The API draft | 
 | 3575 | 	 * REALLY is NOT helpful here...  I am not convinced that the | 
 | 3576 | 	 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP | 
 | 3577 | 	 * are at all well-founded. | 
 | 3578 | 	 */ | 
 | 3579 | 	if (level != SOL_SCTP) { | 
 | 3580 | 		struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 3581 | 		retval = af->setsockopt(sk, level, optname, optval, optlen); | 
 | 3582 | 		goto out_nounlock; | 
 | 3583 | 	} | 
 | 3584 |  | 
 | 3585 | 	sctp_lock_sock(sk); | 
 | 3586 |  | 
 | 3587 | 	switch (optname) { | 
 | 3588 | 	case SCTP_SOCKOPT_BINDX_ADD: | 
 | 3589 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 3590 | 		retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 
 | 3591 | 					       optlen, SCTP_BINDX_ADD_ADDR); | 
 | 3592 | 		break; | 
 | 3593 |  | 
 | 3594 | 	case SCTP_SOCKOPT_BINDX_REM: | 
 | 3595 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 3596 | 		retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 
 | 3597 | 					       optlen, SCTP_BINDX_REM_ADDR); | 
 | 3598 | 		break; | 
 | 3599 |  | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 3600 | 	case SCTP_SOCKOPT_CONNECTX_OLD: | 
 | 3601 | 		/* 'optlen' is the size of the addresses buffer. */ | 
 | 3602 | 		retval = sctp_setsockopt_connectx_old(sk, | 
 | 3603 | 					    (struct sockaddr __user *)optval, | 
 | 3604 | 					    optlen); | 
 | 3605 | 		break; | 
 | 3606 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3607 | 	case SCTP_SOCKOPT_CONNECTX: | 
 | 3608 | 		/* 'optlen' is the size of the addresses buffer. */ | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 3609 | 		retval = sctp_setsockopt_connectx(sk, | 
 | 3610 | 					    (struct sockaddr __user *)optval, | 
 | 3611 | 					    optlen); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3612 | 		break; | 
 | 3613 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | 	case SCTP_DISABLE_FRAGMENTS: | 
 | 3615 | 		retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); | 
 | 3616 | 		break; | 
 | 3617 |  | 
 | 3618 | 	case SCTP_EVENTS: | 
 | 3619 | 		retval = sctp_setsockopt_events(sk, optval, optlen); | 
 | 3620 | 		break; | 
 | 3621 |  | 
 | 3622 | 	case SCTP_AUTOCLOSE: | 
 | 3623 | 		retval = sctp_setsockopt_autoclose(sk, optval, optlen); | 
 | 3624 | 		break; | 
 | 3625 |  | 
 | 3626 | 	case SCTP_PEER_ADDR_PARAMS: | 
 | 3627 | 		retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); | 
 | 3628 | 		break; | 
 | 3629 |  | 
| Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 3630 | 	case SCTP_DELAYED_SACK: | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 3631 | 		retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3632 | 		break; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3633 | 	case SCTP_PARTIAL_DELIVERY_POINT: | 
 | 3634 | 		retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); | 
 | 3635 | 		break; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3636 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | 	case SCTP_INITMSG: | 
 | 3638 | 		retval = sctp_setsockopt_initmsg(sk, optval, optlen); | 
 | 3639 | 		break; | 
 | 3640 | 	case SCTP_DEFAULT_SEND_PARAM: | 
 | 3641 | 		retval = sctp_setsockopt_default_send_param(sk, optval, | 
 | 3642 | 							    optlen); | 
 | 3643 | 		break; | 
 | 3644 | 	case SCTP_PRIMARY_ADDR: | 
 | 3645 | 		retval = sctp_setsockopt_primary_addr(sk, optval, optlen); | 
 | 3646 | 		break; | 
 | 3647 | 	case SCTP_SET_PEER_PRIMARY_ADDR: | 
 | 3648 | 		retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); | 
 | 3649 | 		break; | 
 | 3650 | 	case SCTP_NODELAY: | 
 | 3651 | 		retval = sctp_setsockopt_nodelay(sk, optval, optlen); | 
 | 3652 | 		break; | 
 | 3653 | 	case SCTP_RTOINFO: | 
 | 3654 | 		retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); | 
 | 3655 | 		break; | 
 | 3656 | 	case SCTP_ASSOCINFO: | 
 | 3657 | 		retval = sctp_setsockopt_associnfo(sk, optval, optlen); | 
 | 3658 | 		break; | 
 | 3659 | 	case SCTP_I_WANT_MAPPED_V4_ADDR: | 
 | 3660 | 		retval = sctp_setsockopt_mappedv4(sk, optval, optlen); | 
 | 3661 | 		break; | 
 | 3662 | 	case SCTP_MAXSEG: | 
 | 3663 | 		retval = sctp_setsockopt_maxseg(sk, optval, optlen); | 
 | 3664 | 		break; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3665 | 	case SCTP_ADAPTATION_LAYER: | 
 | 3666 | 		retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | 		break; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3668 | 	case SCTP_CONTEXT: | 
 | 3669 | 		retval = sctp_setsockopt_context(sk, optval, optlen); | 
 | 3670 | 		break; | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3671 | 	case SCTP_FRAGMENT_INTERLEAVE: | 
 | 3672 | 		retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); | 
 | 3673 | 		break; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3674 | 	case SCTP_MAX_BURST: | 
 | 3675 | 		retval = sctp_setsockopt_maxburst(sk, optval, optlen); | 
 | 3676 | 		break; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3677 | 	case SCTP_AUTH_CHUNK: | 
 | 3678 | 		retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); | 
 | 3679 | 		break; | 
 | 3680 | 	case SCTP_HMAC_IDENT: | 
 | 3681 | 		retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); | 
 | 3682 | 		break; | 
 | 3683 | 	case SCTP_AUTH_KEY: | 
 | 3684 | 		retval = sctp_setsockopt_auth_key(sk, optval, optlen); | 
 | 3685 | 		break; | 
 | 3686 | 	case SCTP_AUTH_ACTIVE_KEY: | 
 | 3687 | 		retval = sctp_setsockopt_active_key(sk, optval, optlen); | 
 | 3688 | 		break; | 
 | 3689 | 	case SCTP_AUTH_DELETE_KEY: | 
 | 3690 | 		retval = sctp_setsockopt_del_key(sk, optval, optlen); | 
 | 3691 | 		break; | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3692 | 	case SCTP_AUTO_ASCONF: | 
 | 3693 | 		retval = sctp_setsockopt_auto_asconf(sk, optval, optlen); | 
 | 3694 | 		break; | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3695 | 	case SCTP_PEER_ADDR_THLDS: | 
 | 3696 | 		retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen); | 
 | 3697 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | 	default: | 
 | 3699 | 		retval = -ENOPROTOOPT; | 
 | 3700 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3701 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 |  | 
 | 3703 | 	sctp_release_sock(sk); | 
 | 3704 |  | 
 | 3705 | out_nounlock: | 
 | 3706 | 	return retval; | 
 | 3707 | } | 
 | 3708 |  | 
 | 3709 | /* API 3.1.6 connect() - UDP Style Syntax | 
 | 3710 |  * | 
 | 3711 |  * An application may use the connect() call in the UDP model to initiate an | 
 | 3712 |  * association without sending data. | 
 | 3713 |  * | 
 | 3714 |  * The syntax is: | 
 | 3715 |  * | 
 | 3716 |  * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); | 
 | 3717 |  * | 
 | 3718 |  * sd: the socket descriptor to have a new association added to. | 
 | 3719 |  * | 
 | 3720 |  * nam: the address structure (either struct sockaddr_in or struct | 
 | 3721 |  *    sockaddr_in6 defined in RFC2553 [7]). | 
 | 3722 |  * | 
 | 3723 |  * len: the size of the address. | 
 | 3724 |  */ | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3725 | SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | 			     int addr_len) | 
 | 3727 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | 	int err = 0; | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3729 | 	struct sctp_af *af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 |  | 
 | 3731 | 	sctp_lock_sock(sk); | 
 | 3732 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3733 | 	SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 3734 | 			  __func__, sk, addr, addr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3736 | 	/* Validate addr_len before calling common connect/connectx routine. */ | 
 | 3737 | 	af = sctp_get_af_specific(addr->sa_family); | 
 | 3738 | 	if (!af || addr_len < af->sockaddr_len) { | 
 | 3739 | 		err = -EINVAL; | 
 | 3740 | 	} else { | 
 | 3741 | 		/* Pass correct addr len to common routine (so it knows there | 
 | 3742 | 		 * is only one address being passed. | 
 | 3743 | 		 */ | 
| Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 3744 | 		err = __sctp_connect(sk, addr, af->sockaddr_len, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | 	} | 
 | 3746 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | 	sctp_release_sock(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | 	return err; | 
 | 3749 | } | 
 | 3750 |  | 
 | 3751 | /* FIXME: Write comments. */ | 
 | 3752 | SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags) | 
 | 3753 | { | 
 | 3754 | 	return -EOPNOTSUPP; /* STUB */ | 
 | 3755 | } | 
 | 3756 |  | 
 | 3757 | /* 4.1.4 accept() - TCP Style Syntax | 
 | 3758 |  * | 
 | 3759 |  * Applications use accept() call to remove an established SCTP | 
 | 3760 |  * association from the accept queue of the endpoint.  A new socket | 
 | 3761 |  * descriptor will be returned from accept() to represent the newly | 
 | 3762 |  * formed association. | 
 | 3763 |  */ | 
 | 3764 | SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err) | 
 | 3765 | { | 
 | 3766 | 	struct sctp_sock *sp; | 
 | 3767 | 	struct sctp_endpoint *ep; | 
 | 3768 | 	struct sock *newsk = NULL; | 
 | 3769 | 	struct sctp_association *asoc; | 
 | 3770 | 	long timeo; | 
 | 3771 | 	int error = 0; | 
 | 3772 |  | 
 | 3773 | 	sctp_lock_sock(sk); | 
 | 3774 |  | 
 | 3775 | 	sp = sctp_sk(sk); | 
 | 3776 | 	ep = sp->ep; | 
 | 3777 |  | 
 | 3778 | 	if (!sctp_style(sk, TCP)) { | 
 | 3779 | 		error = -EOPNOTSUPP; | 
 | 3780 | 		goto out; | 
 | 3781 | 	} | 
 | 3782 |  | 
 | 3783 | 	if (!sctp_sstate(sk, LISTENING)) { | 
 | 3784 | 		error = -EINVAL; | 
 | 3785 | 		goto out; | 
 | 3786 | 	} | 
 | 3787 |  | 
| Sridhar Samudrala | 8abfedd | 2006-08-22 00:24:09 -0700 | [diff] [blame] | 3788 | 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 |  | 
 | 3790 | 	error = sctp_wait_for_accept(sk, timeo); | 
 | 3791 | 	if (error) | 
 | 3792 | 		goto out; | 
 | 3793 |  | 
 | 3794 | 	/* We treat the list of associations on the endpoint as the accept | 
 | 3795 | 	 * queue and pick the first association on the list. | 
 | 3796 | 	 */ | 
 | 3797 | 	asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); | 
 | 3798 |  | 
 | 3799 | 	newsk = sp->pf->create_accept_sk(sk, asoc); | 
 | 3800 | 	if (!newsk) { | 
 | 3801 | 		error = -ENOMEM; | 
 | 3802 | 		goto out; | 
 | 3803 | 	} | 
 | 3804 |  | 
 | 3805 | 	/* Populate the fields of the newsk from the oldsk and migrate the | 
 | 3806 | 	 * asoc to the newsk. | 
 | 3807 | 	 */ | 
 | 3808 | 	sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); | 
 | 3809 |  | 
 | 3810 | out: | 
 | 3811 | 	sctp_release_sock(sk); | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3812 | 	*err = error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | 	return newsk; | 
 | 3814 | } | 
 | 3815 |  | 
 | 3816 | /* The SCTP ioctl handler. */ | 
 | 3817 | SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) | 
 | 3818 | { | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 3819 | 	int rc = -ENOTCONN; | 
 | 3820 |  | 
 | 3821 | 	sctp_lock_sock(sk); | 
 | 3822 |  | 
 | 3823 | 	/* | 
 | 3824 | 	 * SEQPACKET-style sockets in LISTENING state are valid, for | 
 | 3825 | 	 * SCTP, so only discard TCP-style sockets in LISTENING state. | 
 | 3826 | 	 */ | 
 | 3827 | 	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | 
 | 3828 | 		goto out; | 
 | 3829 |  | 
 | 3830 | 	switch (cmd) { | 
 | 3831 | 	case SIOCINQ: { | 
 | 3832 | 		struct sk_buff *skb; | 
 | 3833 | 		unsigned int amount = 0; | 
 | 3834 |  | 
 | 3835 | 		skb = skb_peek(&sk->sk_receive_queue); | 
 | 3836 | 		if (skb != NULL) { | 
 | 3837 | 			/* | 
 | 3838 | 			 * We will only return the amount of this packet since | 
 | 3839 | 			 * that is all that will be read. | 
 | 3840 | 			 */ | 
 | 3841 | 			amount = skb->len; | 
 | 3842 | 		} | 
 | 3843 | 		rc = put_user(amount, (int __user *)arg); | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 3844 | 		break; | 
| David S. Miller | 9a7241c | 2010-10-03 22:14:37 -0700 | [diff] [blame] | 3845 | 	} | 
| Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 3846 | 	default: | 
 | 3847 | 		rc = -ENOIOCTLCMD; | 
 | 3848 | 		break; | 
 | 3849 | 	} | 
 | 3850 | out: | 
 | 3851 | 	sctp_release_sock(sk); | 
 | 3852 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | } | 
 | 3854 |  | 
 | 3855 | /* This is the function which gets called during socket creation to | 
 | 3856 |  * initialized the SCTP-specific portion of the sock. | 
 | 3857 |  * The sock structure should already be zero-filled memory. | 
 | 3858 |  */ | 
 | 3859 | SCTP_STATIC int sctp_init_sock(struct sock *sk) | 
 | 3860 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3861 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | 	struct sctp_endpoint *ep; | 
 | 3863 | 	struct sctp_sock *sp; | 
 | 3864 |  | 
 | 3865 | 	SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk); | 
 | 3866 |  | 
 | 3867 | 	sp = sctp_sk(sk); | 
 | 3868 |  | 
 | 3869 | 	/* Initialize the SCTP per socket area.  */ | 
 | 3870 | 	switch (sk->sk_type) { | 
 | 3871 | 	case SOCK_SEQPACKET: | 
 | 3872 | 		sp->type = SCTP_SOCKET_UDP; | 
 | 3873 | 		break; | 
 | 3874 | 	case SOCK_STREAM: | 
 | 3875 | 		sp->type = SCTP_SOCKET_TCP; | 
 | 3876 | 		break; | 
 | 3877 | 	default: | 
 | 3878 | 		return -ESOCKTNOSUPPORT; | 
 | 3879 | 	} | 
 | 3880 |  | 
 | 3881 | 	/* Initialize default send parameters. These parameters can be | 
 | 3882 | 	 * modified with the SCTP_DEFAULT_SEND_PARAM socket option. | 
 | 3883 | 	 */ | 
 | 3884 | 	sp->default_stream = 0; | 
 | 3885 | 	sp->default_ppid = 0; | 
 | 3886 | 	sp->default_flags = 0; | 
 | 3887 | 	sp->default_context = 0; | 
 | 3888 | 	sp->default_timetolive = 0; | 
 | 3889 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3890 | 	sp->default_rcv_context = 0; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3891 | 	sp->max_burst = net->sctp.max_burst; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3892 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | 	/* Initialize default setup parameters. These parameters | 
 | 3894 | 	 * can be modified with the SCTP_INITMSG socket option or | 
 | 3895 | 	 * overridden by the SCTP_INIT CMSG. | 
 | 3896 | 	 */ | 
 | 3897 | 	sp->initmsg.sinit_num_ostreams   = sctp_max_outstreams; | 
 | 3898 | 	sp->initmsg.sinit_max_instreams  = sctp_max_instreams; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3899 | 	sp->initmsg.sinit_max_attempts   = net->sctp.max_retrans_init; | 
 | 3900 | 	sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 |  | 
 | 3902 | 	/* Initialize default RTO related parameters.  These parameters can | 
 | 3903 | 	 * be modified for with the SCTP_RTOINFO socket option. | 
 | 3904 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3905 | 	sp->rtoinfo.srto_initial = net->sctp.rto_initial; | 
 | 3906 | 	sp->rtoinfo.srto_max     = net->sctp.rto_max; | 
 | 3907 | 	sp->rtoinfo.srto_min     = net->sctp.rto_min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 |  | 
 | 3909 | 	/* Initialize default association related parameters. These parameters | 
 | 3910 | 	 * can be modified with the SCTP_ASSOCINFO socket option. | 
 | 3911 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3912 | 	sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | 	sp->assocparams.sasoc_number_peer_destinations = 0; | 
 | 3914 | 	sp->assocparams.sasoc_peer_rwnd = 0; | 
 | 3915 | 	sp->assocparams.sasoc_local_rwnd = 0; | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3916 | 	sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 |  | 
 | 3918 | 	/* Initialize default event subscriptions. By default, all the | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3919 | 	 * options are off. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | 	 */ | 
 | 3921 | 	memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); | 
 | 3922 |  | 
 | 3923 | 	/* Default Peer Address Parameters.  These defaults can | 
 | 3924 | 	 * be modified via SCTP_PEER_ADDR_PARAMS | 
 | 3925 | 	 */ | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3926 | 	sp->hbinterval  = net->sctp.hb_interval; | 
 | 3927 | 	sp->pathmaxrxt  = net->sctp.max_retrans_path; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3928 | 	sp->pathmtu     = 0; // allow default discovery | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3929 | 	sp->sackdelay   = net->sctp.sack_timeout; | 
| Vlad Yasevich | 7bfe8bd | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 3930 | 	sp->sackfreq	= 2; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3931 | 	sp->param_flags = SPP_HB_ENABLE | | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3932 | 			  SPP_PMTUD_ENABLE | | 
 | 3933 | 			  SPP_SACKDELAY_ENABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 |  | 
 | 3935 | 	/* If enabled no SCTP message fragmentation will be performed. | 
 | 3936 | 	 * Configure through SCTP_DISABLE_FRAGMENTS socket option. | 
 | 3937 | 	 */ | 
 | 3938 | 	sp->disable_fragments = 0; | 
 | 3939 |  | 
| Sridhar Samudrala | 208edef | 2006-09-29 17:08:01 -0700 | [diff] [blame] | 3940 | 	/* Enable Nagle algorithm by default.  */ | 
 | 3941 | 	sp->nodelay           = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 |  | 
 | 3943 | 	/* Enable by default. */ | 
 | 3944 | 	sp->v4mapped          = 1; | 
 | 3945 |  | 
 | 3946 | 	/* Auto-close idle associations after the configured | 
 | 3947 | 	 * number of seconds.  A value of 0 disables this | 
 | 3948 | 	 * feature.  Configure through the SCTP_AUTOCLOSE socket option, | 
 | 3949 | 	 * for UDP-style sockets only. | 
 | 3950 | 	 */ | 
 | 3951 | 	sp->autoclose         = 0; | 
 | 3952 |  | 
 | 3953 | 	/* User specified fragmentation limit. */ | 
 | 3954 | 	sp->user_frag         = 0; | 
 | 3955 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3956 | 	sp->adaptation_ind = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 |  | 
 | 3958 | 	sp->pf = sctp_get_pf_specific(sk->sk_family); | 
 | 3959 |  | 
 | 3960 | 	/* Control variables for partial data delivery. */ | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3961 | 	atomic_set(&sp->pd_mode, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | 	skb_queue_head_init(&sp->pd_lobby); | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3963 | 	sp->frag_interleave = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 |  | 
 | 3965 | 	/* Create a per socket endpoint structure.  Even if we | 
 | 3966 | 	 * change the data structure relationships, this may still | 
 | 3967 | 	 * be useful for storing pre-connect address information. | 
 | 3968 | 	 */ | 
 | 3969 | 	ep = sctp_endpoint_new(sk, GFP_KERNEL); | 
 | 3970 | 	if (!ep) | 
 | 3971 | 		return -ENOMEM; | 
 | 3972 |  | 
 | 3973 | 	sp->ep = ep; | 
 | 3974 | 	sp->hmac = NULL; | 
 | 3975 |  | 
 | 3976 | 	SCTP_DBG_OBJCNT_INC(sock); | 
| David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 3977 |  | 
 | 3978 | 	local_bh_disable(); | 
| Vlad Yasevich | 81419d8 | 2010-04-28 08:47:20 +0000 | [diff] [blame] | 3979 | 	percpu_counter_inc(&sctp_sockets_allocated); | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3980 | 	sock_prot_inuse_add(net, sk->sk_prot, 1); | 
 | 3981 | 	if (net->sctp.default_auto_asconf) { | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 3982 | 		list_add_tail(&sp->auto_asconf_list, | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3983 | 		    &net->sctp.auto_asconf_splist); | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 3984 | 		sp->do_auto_asconf = 1; | 
 | 3985 | 	} else | 
 | 3986 | 		sp->do_auto_asconf = 0; | 
| David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 3987 | 	local_bh_enable(); | 
 | 3988 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | 	return 0; | 
 | 3990 | } | 
 | 3991 |  | 
 | 3992 | /* Cleanup any SCTP per socket resources.  */ | 
| Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 3993 | SCTP_STATIC void sctp_destroy_sock(struct sock *sk) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | { | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 3995 | 	struct sctp_sock *sp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 |  | 
 | 3997 | 	SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk); | 
 | 3998 |  | 
 | 3999 | 	/* Release our hold on the endpoint. */ | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4000 | 	sp = sctp_sk(sk); | 
 | 4001 | 	if (sp->do_auto_asconf) { | 
 | 4002 | 		sp->do_auto_asconf = 0; | 
 | 4003 | 		list_del(&sp->auto_asconf_list); | 
 | 4004 | 	} | 
 | 4005 | 	sctp_endpoint_free(sp->ep); | 
| Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4006 | 	local_bh_disable(); | 
| Vlad Yasevich | 81419d8 | 2010-04-28 08:47:20 +0000 | [diff] [blame] | 4007 | 	percpu_counter_dec(&sctp_sockets_allocated); | 
| Eric Dumazet | 9a57f7f | 2008-11-17 02:41:00 -0800 | [diff] [blame] | 4008 | 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 
| Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4009 | 	local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | } | 
 | 4011 |  | 
 | 4012 | /* API 4.1.7 shutdown() - TCP Style Syntax | 
 | 4013 |  *     int shutdown(int socket, int how); | 
 | 4014 |  * | 
 | 4015 |  *     sd      - the socket descriptor of the association to be closed. | 
 | 4016 |  *     how     - Specifies the type of shutdown.  The  values  are | 
 | 4017 |  *               as follows: | 
 | 4018 |  *               SHUT_RD | 
 | 4019 |  *                     Disables further receive operations. No SCTP | 
 | 4020 |  *                     protocol action is taken. | 
 | 4021 |  *               SHUT_WR | 
 | 4022 |  *                     Disables further send operations, and initiates | 
 | 4023 |  *                     the SCTP shutdown sequence. | 
 | 4024 |  *               SHUT_RDWR | 
 | 4025 |  *                     Disables further send  and  receive  operations | 
 | 4026 |  *                     and initiates the SCTP shutdown sequence. | 
 | 4027 |  */ | 
 | 4028 | SCTP_STATIC void sctp_shutdown(struct sock *sk, int how) | 
 | 4029 | { | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 4030 | 	struct net *net = sock_net(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | 	struct sctp_endpoint *ep; | 
 | 4032 | 	struct sctp_association *asoc; | 
 | 4033 |  | 
 | 4034 | 	if (!sctp_style(sk, TCP)) | 
 | 4035 | 		return; | 
 | 4036 |  | 
 | 4037 | 	if (how & SEND_SHUTDOWN) { | 
 | 4038 | 		ep = sctp_sk(sk)->ep; | 
 | 4039 | 		if (!list_empty(&ep->asocs)) { | 
 | 4040 | 			asoc = list_entry(ep->asocs.next, | 
 | 4041 | 					  struct sctp_association, asocs); | 
| Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 4042 | 			sctp_primitive_SHUTDOWN(net, asoc, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | 		} | 
 | 4044 | 	} | 
 | 4045 | } | 
 | 4046 |  | 
 | 4047 | /* 7.2.1 Association Status (SCTP_STATUS) | 
 | 4048 |  | 
 | 4049 |  * Applications can retrieve current status information about an | 
 | 4050 |  * association, including association state, peer receiver window size, | 
 | 4051 |  * number of unacked data chunks, and number of data chunks pending | 
 | 4052 |  * receipt.  This information is read-only. | 
 | 4053 |  */ | 
 | 4054 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, | 
 | 4055 | 				       char __user *optval, | 
 | 4056 | 				       int __user *optlen) | 
 | 4057 | { | 
 | 4058 | 	struct sctp_status status; | 
 | 4059 | 	struct sctp_association *asoc = NULL; | 
 | 4060 | 	struct sctp_transport *transport; | 
 | 4061 | 	sctp_assoc_t associd; | 
 | 4062 | 	int retval = 0; | 
 | 4063 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4064 | 	if (len < sizeof(status)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | 		retval = -EINVAL; | 
 | 4066 | 		goto out; | 
 | 4067 | 	} | 
 | 4068 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4069 | 	len = sizeof(status); | 
 | 4070 | 	if (copy_from_user(&status, optval, len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | 		retval = -EFAULT; | 
 | 4072 | 		goto out; | 
 | 4073 | 	} | 
 | 4074 |  | 
 | 4075 | 	associd = status.sstat_assoc_id; | 
 | 4076 | 	asoc = sctp_id2assoc(sk, associd); | 
 | 4077 | 	if (!asoc) { | 
 | 4078 | 		retval = -EINVAL; | 
 | 4079 | 		goto out; | 
 | 4080 | 	} | 
 | 4081 |  | 
 | 4082 | 	transport = asoc->peer.primary_path; | 
 | 4083 |  | 
 | 4084 | 	status.sstat_assoc_id = sctp_assoc2id(asoc); | 
 | 4085 | 	status.sstat_state = asoc->state; | 
 | 4086 | 	status.sstat_rwnd =  asoc->peer.rwnd; | 
 | 4087 | 	status.sstat_unackdata = asoc->unack_data; | 
 | 4088 |  | 
 | 4089 | 	status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); | 
 | 4090 | 	status.sstat_instrms = asoc->c.sinit_max_instreams; | 
 | 4091 | 	status.sstat_outstrms = asoc->c.sinit_num_ostreams; | 
 | 4092 | 	status.sstat_fragmentation_point = asoc->frag_point; | 
 | 4093 | 	status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 
| Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 4094 | 	memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, | 
 | 4095 | 			transport->af_specific->sockaddr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | 	/* Map ipv4 address into v4-mapped-on-v6 address.  */ | 
 | 4097 | 	sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 
 | 4098 | 		(union sctp_addr *)&status.sstat_primary.spinfo_address); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4099 | 	status.sstat_primary.spinfo_state = transport->state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | 	status.sstat_primary.spinfo_cwnd = transport->cwnd; | 
 | 4101 | 	status.sstat_primary.spinfo_srtt = transport->srtt; | 
 | 4102 | 	status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4103 | 	status.sstat_primary.spinfo_mtu = transport->pathmtu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4105 | 	if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) | 
 | 4106 | 		status.sstat_primary.spinfo_state = SCTP_ACTIVE; | 
 | 4107 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | 	if (put_user(len, optlen)) { | 
 | 4109 | 		retval = -EFAULT; | 
 | 4110 | 		goto out; | 
 | 4111 | 	} | 
 | 4112 |  | 
 | 4113 | 	SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n", | 
 | 4114 | 			  len, status.sstat_state, status.sstat_rwnd, | 
 | 4115 | 			  status.sstat_assoc_id); | 
 | 4116 |  | 
 | 4117 | 	if (copy_to_user(optval, &status, len)) { | 
 | 4118 | 		retval = -EFAULT; | 
 | 4119 | 		goto out; | 
 | 4120 | 	} | 
 | 4121 |  | 
 | 4122 | out: | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 4123 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | } | 
 | 4125 |  | 
 | 4126 |  | 
 | 4127 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) | 
 | 4128 |  * | 
 | 4129 |  * Applications can retrieve information about a specific peer address | 
 | 4130 |  * of an association, including its reachability state, congestion | 
 | 4131 |  * window, and retransmission timer values.  This information is | 
 | 4132 |  * read-only. | 
 | 4133 |  */ | 
 | 4134 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, | 
 | 4135 | 					  char __user *optval, | 
 | 4136 | 					  int __user *optlen) | 
 | 4137 | { | 
 | 4138 | 	struct sctp_paddrinfo pinfo; | 
 | 4139 | 	struct sctp_transport *transport; | 
 | 4140 | 	int retval = 0; | 
 | 4141 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4142 | 	if (len < sizeof(pinfo)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | 		retval = -EINVAL; | 
 | 4144 | 		goto out; | 
 | 4145 | 	} | 
 | 4146 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4147 | 	len = sizeof(pinfo); | 
 | 4148 | 	if (copy_from_user(&pinfo, optval, len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | 		retval = -EFAULT; | 
 | 4150 | 		goto out; | 
 | 4151 | 	} | 
 | 4152 |  | 
 | 4153 | 	transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, | 
 | 4154 | 					   pinfo.spinfo_assoc_id); | 
 | 4155 | 	if (!transport) | 
 | 4156 | 		return -EINVAL; | 
 | 4157 |  | 
 | 4158 | 	pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4159 | 	pinfo.spinfo_state = transport->state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | 	pinfo.spinfo_cwnd = transport->cwnd; | 
 | 4161 | 	pinfo.spinfo_srtt = transport->srtt; | 
 | 4162 | 	pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4163 | 	pinfo.spinfo_mtu = transport->pathmtu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4165 | 	if (pinfo.spinfo_state == SCTP_UNKNOWN) | 
 | 4166 | 		pinfo.spinfo_state = SCTP_ACTIVE; | 
 | 4167 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | 	if (put_user(len, optlen)) { | 
 | 4169 | 		retval = -EFAULT; | 
 | 4170 | 		goto out; | 
 | 4171 | 	} | 
 | 4172 |  | 
 | 4173 | 	if (copy_to_user(optval, &pinfo, len)) { | 
 | 4174 | 		retval = -EFAULT; | 
 | 4175 | 		goto out; | 
 | 4176 | 	} | 
 | 4177 |  | 
 | 4178 | out: | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 4179 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | } | 
 | 4181 |  | 
 | 4182 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 
 | 4183 |  * | 
 | 4184 |  * This option is a on/off flag.  If enabled no SCTP message | 
 | 4185 |  * fragmentation will be performed.  Instead if a message being sent | 
 | 4186 |  * exceeds the current PMTU size, the message will NOT be sent and | 
 | 4187 |  * instead a error will be indicated to the user. | 
 | 4188 |  */ | 
 | 4189 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, | 
 | 4190 | 					char __user *optval, int __user *optlen) | 
 | 4191 | { | 
 | 4192 | 	int val; | 
 | 4193 |  | 
 | 4194 | 	if (len < sizeof(int)) | 
 | 4195 | 		return -EINVAL; | 
 | 4196 |  | 
 | 4197 | 	len = sizeof(int); | 
 | 4198 | 	val = (sctp_sk(sk)->disable_fragments == 1); | 
 | 4199 | 	if (put_user(len, optlen)) | 
 | 4200 | 		return -EFAULT; | 
 | 4201 | 	if (copy_to_user(optval, &val, len)) | 
 | 4202 | 		return -EFAULT; | 
 | 4203 | 	return 0; | 
 | 4204 | } | 
 | 4205 |  | 
 | 4206 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) | 
 | 4207 |  * | 
 | 4208 |  * This socket option is used to specify various notifications and | 
 | 4209 |  * ancillary data the user wishes to receive. | 
 | 4210 |  */ | 
 | 4211 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, | 
 | 4212 | 				  int __user *optlen) | 
 | 4213 | { | 
| Thomas Graf | acdd598 | 2012-04-03 22:17:53 +0000 | [diff] [blame] | 4214 | 	if (len <= 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | 		return -EINVAL; | 
| Thomas Graf | acdd598 | 2012-04-03 22:17:53 +0000 | [diff] [blame] | 4216 | 	if (len > sizeof(struct sctp_event_subscribe)) | 
 | 4217 | 		len = sizeof(struct sctp_event_subscribe); | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4218 | 	if (put_user(len, optlen)) | 
 | 4219 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | 	if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) | 
 | 4221 | 		return -EFAULT; | 
 | 4222 | 	return 0; | 
 | 4223 | } | 
 | 4224 |  | 
 | 4225 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 
 | 4226 |  * | 
 | 4227 |  * This socket option is applicable to the UDP-style socket only.  When | 
 | 4228 |  * set it will cause associations that are idle for more than the | 
 | 4229 |  * specified number of seconds to automatically close.  An association | 
 | 4230 |  * being idle is defined an association that has NOT sent or received | 
 | 4231 |  * user data.  The special value of '0' indicates that no automatic | 
 | 4232 |  * close of any associations should be performed.  The option expects an | 
 | 4233 |  * integer defining the number of seconds of idle time before an | 
 | 4234 |  * association is closed. | 
 | 4235 |  */ | 
 | 4236 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 4237 | { | 
 | 4238 | 	/* Applicable to UDP-style socket only */ | 
 | 4239 | 	if (sctp_style(sk, TCP)) | 
 | 4240 | 		return -EOPNOTSUPP; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4241 | 	if (len < sizeof(int)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4243 | 	len = sizeof(int); | 
 | 4244 | 	if (put_user(len, optlen)) | 
 | 4245 | 		return -EFAULT; | 
 | 4246 | 	if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | 		return -EFAULT; | 
 | 4248 | 	return 0; | 
 | 4249 | } | 
 | 4250 |  | 
 | 4251 | /* Helper routine to branch off an association to a new socket.  */ | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4252 | 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] | 4253 | { | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4254 | 	struct sctp_association *asoc = sctp_id2assoc(sk, id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | 	struct socket *sock; | 
| Vlad Yasevich | d570ee4 | 2007-05-15 16:32:39 -0400 | [diff] [blame] | 4256 | 	struct sctp_af *af; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | 	int err = 0; | 
 | 4258 |  | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4259 | 	if (!asoc) | 
 | 4260 | 		return -EINVAL; | 
 | 4261 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | 	/* An association cannot be branched off from an already peeled-off | 
 | 4263 | 	 * socket, nor is this supported for tcp style sockets. | 
 | 4264 | 	 */ | 
 | 4265 | 	if (!sctp_style(sk, UDP)) | 
 | 4266 | 		return -EINVAL; | 
 | 4267 |  | 
 | 4268 | 	/* Create a new socket.  */ | 
 | 4269 | 	err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); | 
 | 4270 | 	if (err < 0) | 
 | 4271 | 		return err; | 
 | 4272 |  | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 4273 | 	sctp_copy_sock(sock->sk, sk, asoc); | 
| Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 4274 |  | 
 | 4275 | 	/* Make peeled-off sockets more like 1-1 accepted sockets. | 
 | 4276 | 	 * Set the daddr and initialize id to something more random | 
 | 4277 | 	 */ | 
| Vlad Yasevich | d570ee4 | 2007-05-15 16:32:39 -0400 | [diff] [blame] | 4278 | 	af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family); | 
 | 4279 | 	af->to_sk_daddr(&asoc->peer.primary_addr, sk); | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 4280 |  | 
 | 4281 | 	/* Populate the fields of the newsk from the oldsk and migrate the | 
 | 4282 | 	 * asoc to the newsk. | 
 | 4283 | 	 */ | 
 | 4284 | 	sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); | 
| Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 4285 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | 	*sockp = sock; | 
 | 4287 |  | 
 | 4288 | 	return err; | 
 | 4289 | } | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4290 | EXPORT_SYMBOL(sctp_do_peeloff); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 |  | 
 | 4292 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 4293 | { | 
 | 4294 | 	sctp_peeloff_arg_t peeloff; | 
 | 4295 | 	struct socket *newsock; | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 4296 | 	struct file *newfile; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | 	int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4299 | 	if (len < sizeof(sctp_peeloff_arg_t)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4301 | 	len = sizeof(sctp_peeloff_arg_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | 	if (copy_from_user(&peeloff, optval, len)) | 
 | 4303 | 		return -EFAULT; | 
 | 4304 |  | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4305 | 	retval = sctp_do_peeloff(sk, peeloff.associd, &newsock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | 	if (retval < 0) | 
 | 4307 | 		goto out; | 
 | 4308 |  | 
 | 4309 | 	/* Map the socket to an unused fd that can be returned to the user.  */ | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 4310 | 	retval = get_unused_fd(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | 	if (retval < 0) { | 
 | 4312 | 		sock_release(newsock); | 
 | 4313 | 		goto out; | 
 | 4314 | 	} | 
 | 4315 |  | 
| Linus Torvalds | aab174f | 2012-10-02 20:25:04 -0700 | [diff] [blame] | 4316 | 	newfile = sock_alloc_file(newsock, 0, NULL); | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 4317 | 	if (unlikely(IS_ERR(newfile))) { | 
 | 4318 | 		put_unused_fd(retval); | 
 | 4319 | 		sock_release(newsock); | 
 | 4320 | 		return PTR_ERR(newfile); | 
 | 4321 | 	} | 
 | 4322 |  | 
| Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 4323 | 	SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n", | 
 | 4324 | 			  __func__, sk, newsock->sk, retval); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 |  | 
 | 4326 | 	/* Return the fd mapped to the new socket.  */ | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 4327 | 	if (put_user(len, optlen)) { | 
 | 4328 | 		fput(newfile); | 
 | 4329 | 		put_unused_fd(retval); | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4330 | 		return -EFAULT; | 
| Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 4331 | 	} | 
 | 4332 | 	peeloff.sd = retval; | 
 | 4333 | 	if (copy_to_user(optval, &peeloff, len)) { | 
 | 4334 | 		fput(newfile); | 
 | 4335 | 		put_unused_fd(retval); | 
 | 4336 | 		return -EFAULT; | 
 | 4337 | 	} | 
 | 4338 | 	fd_install(retval, newfile); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | out: | 
 | 4340 | 	return retval; | 
 | 4341 | } | 
 | 4342 |  | 
 | 4343 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 
 | 4344 |  * | 
 | 4345 |  * Applications can enable or disable heartbeats for any peer address of | 
 | 4346 |  * an association, modify an address's heartbeat interval, force a | 
 | 4347 |  * heartbeat to be sent immediately, and adjust the address's maximum | 
 | 4348 |  * number of retransmissions sent before an address is considered | 
 | 4349 |  * unreachable.  The following structure is used to access and modify an | 
 | 4350 |  * address's parameters: | 
 | 4351 |  * | 
 | 4352 |  *  struct sctp_paddrparams { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4353 |  *     sctp_assoc_t            spp_assoc_id; | 
 | 4354 |  *     struct sockaddr_storage spp_address; | 
 | 4355 |  *     uint32_t                spp_hbinterval; | 
 | 4356 |  *     uint16_t                spp_pathmaxrxt; | 
 | 4357 |  *     uint32_t                spp_pathmtu; | 
 | 4358 |  *     uint32_t                spp_sackdelay; | 
 | 4359 |  *     uint32_t                spp_flags; | 
 | 4360 |  * }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 |  * | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4362 |  *   spp_assoc_id    - (one-to-many style socket) This is filled in the | 
 | 4363 |  *                     application, and identifies the association for | 
 | 4364 |  *                     this query. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 |  *   spp_address     - This specifies which address is of interest. | 
 | 4366 |  *   spp_hbinterval  - This contains the value of the heartbeat interval, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4367 |  *                     in milliseconds.  If a  value of zero | 
 | 4368 |  *                     is present in this field then no changes are to | 
 | 4369 |  *                     be made to this parameter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 |  *   spp_pathmaxrxt  - This contains the maximum number of | 
 | 4371 |  *                     retransmissions before this address shall be | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4372 |  *                     considered unreachable. If a  value of zero | 
 | 4373 |  *                     is present in this field then no changes are to | 
 | 4374 |  *                     be made to this parameter. | 
 | 4375 |  *   spp_pathmtu     - When Path MTU discovery is disabled the value | 
 | 4376 |  *                     specified here will be the "fixed" path mtu. | 
 | 4377 |  *                     Note that if the spp_address field is empty | 
 | 4378 |  *                     then all associations on this address will | 
 | 4379 |  *                     have this fixed path mtu set upon them. | 
 | 4380 |  * | 
 | 4381 |  *   spp_sackdelay   - When delayed sack is enabled, this value specifies | 
 | 4382 |  *                     the number of milliseconds that sacks will be delayed | 
 | 4383 |  *                     for. This value will apply to all addresses of an | 
 | 4384 |  *                     association if the spp_address field is empty. Note | 
 | 4385 |  *                     also, that if delayed sack is enabled and this | 
 | 4386 |  *                     value is set to 0, no change is made to the last | 
 | 4387 |  *                     recorded delayed sack timer value. | 
 | 4388 |  * | 
 | 4389 |  *   spp_flags       - These flags are used to control various features | 
 | 4390 |  *                     on an association. The flag field may contain | 
 | 4391 |  *                     zero or more of the following options. | 
 | 4392 |  * | 
 | 4393 |  *                     SPP_HB_ENABLE  - Enable heartbeats on the | 
 | 4394 |  *                     specified address. Note that if the address | 
 | 4395 |  *                     field is empty all addresses for the association | 
 | 4396 |  *                     have heartbeats enabled upon them. | 
 | 4397 |  * | 
 | 4398 |  *                     SPP_HB_DISABLE - Disable heartbeats on the | 
 | 4399 |  *                     speicifed address. Note that if the address | 
 | 4400 |  *                     field is empty all addresses for the association | 
 | 4401 |  *                     will have their heartbeats disabled. Note also | 
 | 4402 |  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are | 
 | 4403 |  *                     mutually exclusive, only one of these two should | 
 | 4404 |  *                     be specified. Enabling both fields will have | 
 | 4405 |  *                     undetermined results. | 
 | 4406 |  * | 
 | 4407 |  *                     SPP_HB_DEMAND - Request a user initiated heartbeat | 
 | 4408 |  *                     to be made immediately. | 
 | 4409 |  * | 
 | 4410 |  *                     SPP_PMTUD_ENABLE - This field will enable PMTU | 
 | 4411 |  *                     discovery upon the specified address. Note that | 
 | 4412 |  *                     if the address feild is empty then all addresses | 
 | 4413 |  *                     on the association are effected. | 
 | 4414 |  * | 
 | 4415 |  *                     SPP_PMTUD_DISABLE - This field will disable PMTU | 
 | 4416 |  *                     discovery upon the specified address. Note that | 
 | 4417 |  *                     if the address feild is empty then all addresses | 
 | 4418 |  *                     on the association are effected. Not also that | 
 | 4419 |  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 
 | 4420 |  *                     exclusive. Enabling both will have undetermined | 
 | 4421 |  *                     results. | 
 | 4422 |  * | 
 | 4423 |  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns | 
 | 4424 |  *                     on delayed sack. The time specified in spp_sackdelay | 
 | 4425 |  *                     is used to specify the sack delay for this address. Note | 
 | 4426 |  *                     that if spp_address is empty then all addresses will | 
 | 4427 |  *                     enable delayed sack and take on the sack delay | 
 | 4428 |  *                     value specified in spp_sackdelay. | 
 | 4429 |  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns | 
 | 4430 |  *                     off delayed sack. If the spp_address field is blank then | 
 | 4431 |  *                     delayed sack is disabled for the entire association. Note | 
 | 4432 |  *                     also that this field is mutually exclusive to | 
 | 4433 |  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined | 
 | 4434 |  *                     results. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 |  */ | 
 | 4436 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4437 | 					    char __user *optval, int __user *optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4439 | 	struct sctp_paddrparams  params; | 
 | 4440 | 	struct sctp_transport   *trans = NULL; | 
 | 4441 | 	struct sctp_association *asoc = NULL; | 
 | 4442 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4444 | 	if (len < sizeof(struct sctp_paddrparams)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4446 | 	len = sizeof(struct sctp_paddrparams); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 4448 | 		return -EFAULT; | 
 | 4449 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4450 | 	/* If an address other than INADDR_ANY is specified, and | 
 | 4451 | 	 * no transport is found, then the request is invalid. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | 	 */ | 
| Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 4453 | 	if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4454 | 		trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 
 | 4455 | 					       params.spp_assoc_id); | 
 | 4456 | 		if (!trans) { | 
 | 4457 | 			SCTP_DEBUG_PRINTK("Failed no transport\n"); | 
 | 4458 | 			return -EINVAL; | 
 | 4459 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | 	} | 
 | 4461 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4462 | 	/* Get association, if assoc_id != 0 and the socket is a one | 
 | 4463 | 	 * to many style socket, and an association was not found, then | 
 | 4464 | 	 * the id was invalid. | 
 | 4465 | 	 */ | 
 | 4466 | 	asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 
 | 4467 | 	if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { | 
 | 4468 | 		SCTP_DEBUG_PRINTK("Failed no association\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | 		return -EINVAL; | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4470 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4472 | 	if (trans) { | 
 | 4473 | 		/* Fetch transport values. */ | 
 | 4474 | 		params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); | 
 | 4475 | 		params.spp_pathmtu    = trans->pathmtu; | 
 | 4476 | 		params.spp_pathmaxrxt = trans->pathmaxrxt; | 
 | 4477 | 		params.spp_sackdelay  = jiffies_to_msecs(trans->sackdelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4479 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 4480 | 		params.spp_flags      = trans->param_flags; | 
 | 4481 | 	} else if (asoc) { | 
 | 4482 | 		/* Fetch association values. */ | 
 | 4483 | 		params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); | 
 | 4484 | 		params.spp_pathmtu    = asoc->pathmtu; | 
 | 4485 | 		params.spp_pathmaxrxt = asoc->pathmaxrxt; | 
 | 4486 | 		params.spp_sackdelay  = jiffies_to_msecs(asoc->sackdelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 |  | 
| Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4488 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 4489 | 		params.spp_flags      = asoc->param_flags; | 
 | 4490 | 	} else { | 
 | 4491 | 		/* Fetch socket values. */ | 
 | 4492 | 		params.spp_hbinterval = sp->hbinterval; | 
 | 4493 | 		params.spp_pathmtu    = sp->pathmtu; | 
 | 4494 | 		params.spp_sackdelay  = sp->sackdelay; | 
 | 4495 | 		params.spp_pathmaxrxt = sp->pathmaxrxt; | 
 | 4496 |  | 
 | 4497 | 		/*draft-11 doesn't say what to return in spp_flags*/ | 
 | 4498 | 		params.spp_flags      = sp->param_flags; | 
 | 4499 | 	} | 
 | 4500 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 4502 | 		return -EFAULT; | 
 | 4503 |  | 
 | 4504 | 	if (put_user(len, optlen)) | 
 | 4505 | 		return -EFAULT; | 
 | 4506 |  | 
 | 4507 | 	return 0; | 
 | 4508 | } | 
 | 4509 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4510 | /* | 
 | 4511 |  * 7.1.23.  Get or set delayed ack timer (SCTP_DELAYED_SACK) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4512 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4513 |  * This option will effect the way delayed acks are performed.  This | 
 | 4514 |  * option allows you to get or set the delayed ack time, in | 
 | 4515 |  * milliseconds.  It also allows changing the delayed ack frequency. | 
 | 4516 |  * Changing the frequency to 1 disables the delayed sack algorithm.  If | 
 | 4517 |  * the assoc_id is 0, then this sets or gets the endpoints default | 
 | 4518 |  * values.  If the assoc_id field is non-zero, then the set or get | 
 | 4519 |  * effects the specified association for the one to many model (the | 
 | 4520 |  * assoc_id field is ignored by the one to one model).  Note that if | 
 | 4521 |  * sack_delay or sack_freq are 0 when setting this option, then the | 
 | 4522 |  * current values will remain unchanged. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4523 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4524 |  * struct sctp_sack_info { | 
 | 4525 |  *     sctp_assoc_t            sack_assoc_id; | 
 | 4526 |  *     uint32_t                sack_delay; | 
 | 4527 |  *     uint32_t                sack_freq; | 
 | 4528 |  * }; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4529 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4530 |  * sack_assoc_id -  This parameter, indicates which association the user | 
 | 4531 |  *    is performing an action upon.  Note that if this field's value is | 
 | 4532 |  *    zero then the endpoints default value is changed (effecting future | 
 | 4533 |  *    associations only). | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4534 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4535 |  * sack_delay -  This parameter contains the number of milliseconds that | 
 | 4536 |  *    the user is requesting the delayed ACK timer be set to.  Note that | 
 | 4537 |  *    this value is defined in the standard to be between 200 and 500 | 
 | 4538 |  *    milliseconds. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4539 |  * | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4540 |  * sack_freq -  This parameter contains the number of packets that must | 
 | 4541 |  *    be received before a sack is sent without waiting for the delay | 
 | 4542 |  *    timer to expire.  The default value for this is 2, setting this | 
 | 4543 |  *    value to 1 will disable the delayed sack algorithm. | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4544 |  */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4545 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4546 | 					    char __user *optval, | 
 | 4547 | 					    int __user *optlen) | 
 | 4548 | { | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4549 | 	struct sctp_sack_info    params; | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4550 | 	struct sctp_association *asoc = NULL; | 
 | 4551 | 	struct sctp_sock        *sp = sctp_sk(sk); | 
 | 4552 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4553 | 	if (len >= sizeof(struct sctp_sack_info)) { | 
 | 4554 | 		len = sizeof(struct sctp_sack_info); | 
 | 4555 |  | 
 | 4556 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 4557 | 			return -EFAULT; | 
 | 4558 | 	} else if (len == sizeof(struct sctp_assoc_value)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 4559 | 		pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); | 
 | 4560 | 		pr_warn("Use struct sctp_sack_info instead\n"); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4561 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 4562 | 			return -EFAULT; | 
 | 4563 | 	} else | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4564 | 		return - EINVAL; | 
 | 4565 |  | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4566 | 	/* 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] | 4567 | 	 * to many style socket, and an association was not found, then | 
 | 4568 | 	 * the id was invalid. | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4569 | 	 */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4570 | 	asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 
 | 4571 | 	if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4572 | 		return -EINVAL; | 
 | 4573 |  | 
 | 4574 | 	if (asoc) { | 
 | 4575 | 		/* Fetch association values. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4576 | 		if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { | 
 | 4577 | 			params.sack_delay = jiffies_to_msecs( | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4578 | 				asoc->sackdelay); | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4579 | 			params.sack_freq = asoc->sackfreq; | 
 | 4580 |  | 
 | 4581 | 		} else { | 
 | 4582 | 			params.sack_delay = 0; | 
 | 4583 | 			params.sack_freq = 1; | 
 | 4584 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4585 | 	} else { | 
 | 4586 | 		/* Fetch socket values. */ | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4587 | 		if (sp->param_flags & SPP_SACKDELAY_ENABLE) { | 
 | 4588 | 			params.sack_delay  = sp->sackdelay; | 
 | 4589 | 			params.sack_freq = sp->sackfreq; | 
 | 4590 | 		} else { | 
 | 4591 | 			params.sack_delay  = 0; | 
 | 4592 | 			params.sack_freq = 1; | 
 | 4593 | 		} | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4594 | 	} | 
 | 4595 |  | 
 | 4596 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 4597 | 		return -EFAULT; | 
 | 4598 |  | 
 | 4599 | 	if (put_user(len, optlen)) | 
 | 4600 | 		return -EFAULT; | 
 | 4601 |  | 
 | 4602 | 	return 0; | 
 | 4603 | } | 
 | 4604 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 
 | 4606 |  * | 
 | 4607 |  * Applications can specify protocol parameters for the default association | 
 | 4608 |  * initialization.  The option name argument to setsockopt() and getsockopt() | 
 | 4609 |  * is SCTP_INITMSG. | 
 | 4610 |  * | 
 | 4611 |  * Setting initialization parameters is effective only on an unconnected | 
 | 4612 |  * socket (for UDP-style sockets only future associations are effected | 
 | 4613 |  * by the change).  With TCP-style sockets, this option is inherited by | 
 | 4614 |  * sockets derived from a listener socket. | 
 | 4615 |  */ | 
 | 4616 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) | 
 | 4617 | { | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4618 | 	if (len < sizeof(struct sctp_initmsg)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4620 | 	len = sizeof(struct sctp_initmsg); | 
 | 4621 | 	if (put_user(len, optlen)) | 
 | 4622 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | 	if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) | 
 | 4624 | 		return -EFAULT; | 
 | 4625 | 	return 0; | 
 | 4626 | } | 
 | 4627 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4629 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, | 
 | 4630 | 				      char __user *optval, int __user *optlen) | 
 | 4631 | { | 
 | 4632 | 	struct sctp_association *asoc; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4633 | 	int cnt = 0; | 
 | 4634 | 	struct sctp_getaddrs getaddrs; | 
 | 4635 | 	struct sctp_transport *from; | 
 | 4636 | 	void __user *to; | 
 | 4637 | 	union sctp_addr temp; | 
 | 4638 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4639 | 	int addrlen; | 
 | 4640 | 	size_t space_left; | 
 | 4641 | 	int bytes_copied; | 
 | 4642 |  | 
 | 4643 | 	if (len < sizeof(struct sctp_getaddrs)) | 
 | 4644 | 		return -EINVAL; | 
 | 4645 |  | 
 | 4646 | 	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 
 | 4647 | 		return -EFAULT; | 
 | 4648 |  | 
 | 4649 | 	/* For UDP-style sockets, id specifies the association to query.  */ | 
 | 4650 | 	asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 
 | 4651 | 	if (!asoc) | 
 | 4652 | 		return -EINVAL; | 
 | 4653 |  | 
 | 4654 | 	to = optval + offsetof(struct sctp_getaddrs,addrs); | 
| Neil Horman | 186e234 | 2007-06-18 19:59:16 -0400 | [diff] [blame] | 4655 | 	space_left = len - offsetof(struct sctp_getaddrs,addrs); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4656 |  | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 4657 | 	list_for_each_entry(from, &asoc->peer.transport_addr_list, | 
 | 4658 | 				transports) { | 
| Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 4659 | 		memcpy(&temp, &from->ipaddr, sizeof(temp)); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4660 | 		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); | 
| Vlad Yasevich | a5f4cea | 2010-04-30 21:42:42 -0400 | [diff] [blame] | 4661 | 		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4662 | 		if (space_left < addrlen) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4663 | 			return -ENOMEM; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4664 | 		if (copy_to_user(to, &temp, addrlen)) | 
 | 4665 | 			return -EFAULT; | 
 | 4666 | 		to += addrlen; | 
 | 4667 | 		cnt++; | 
 | 4668 | 		space_left -= addrlen; | 
 | 4669 | 	} | 
 | 4670 |  | 
 | 4671 | 	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) | 
 | 4672 | 		return -EFAULT; | 
 | 4673 | 	bytes_copied = ((char __user *)to) - optval; | 
 | 4674 | 	if (put_user(bytes_copied, optlen)) | 
 | 4675 | 		return -EFAULT; | 
 | 4676 |  | 
 | 4677 | 	return 0; | 
 | 4678 | } | 
 | 4679 |  | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4680 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, | 
 | 4681 | 			    size_t space_left, int *bytes_copied) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4682 | { | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4683 | 	struct sctp_sockaddr_entry *addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4684 | 	union sctp_addr temp; | 
 | 4685 | 	int cnt = 0; | 
 | 4686 | 	int addrlen; | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 4687 | 	struct net *net = sock_net(sk); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4688 |  | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 4689 | 	rcu_read_lock(); | 
| Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 4690 | 	list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 4691 | 		if (!addr->valid) | 
 | 4692 | 			continue; | 
 | 4693 |  | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4694 | 		if ((PF_INET == sk->sk_family) && | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4695 | 		    (AF_INET6 == addr->a.sa.sa_family)) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4696 | 			continue; | 
| Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 4697 | 		if ((PF_INET6 == sk->sk_family) && | 
 | 4698 | 		    inet_v6_ipv6only(sk) && | 
 | 4699 | 		    (AF_INET == addr->a.sa.sa_family)) | 
 | 4700 | 			continue; | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4701 | 		memcpy(&temp, &addr->a, sizeof(temp)); | 
| Vlad Yasevich | b46ae36 | 2008-01-28 14:25:36 -0500 | [diff] [blame] | 4702 | 		if (!temp.v4.sin_port) | 
 | 4703 | 			temp.v4.sin_port = htons(port); | 
 | 4704 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4705 | 		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 
 | 4706 | 								&temp); | 
 | 4707 | 		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 4708 | 		if (space_left < addrlen) { | 
 | 4709 | 			cnt =  -ENOMEM; | 
 | 4710 | 			break; | 
 | 4711 | 		} | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4712 | 		memcpy(to, &temp, addrlen); | 
| Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4713 |  | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4714 | 		to += addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4715 | 		cnt ++; | 
 | 4716 | 		space_left -= addrlen; | 
| Vlad Yasevich | 3663c30 | 2007-07-03 12:43:12 -0400 | [diff] [blame] | 4717 | 		*bytes_copied += addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4718 | 	} | 
| Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 4719 | 	rcu_read_unlock(); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4720 |  | 
 | 4721 | 	return cnt; | 
 | 4722 | } | 
 | 4723 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4725 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, | 
 | 4726 | 				       char __user *optval, int __user *optlen) | 
 | 4727 | { | 
 | 4728 | 	struct sctp_bind_addr *bp; | 
 | 4729 | 	struct sctp_association *asoc; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4730 | 	int cnt = 0; | 
 | 4731 | 	struct sctp_getaddrs getaddrs; | 
 | 4732 | 	struct sctp_sockaddr_entry *addr; | 
 | 4733 | 	void __user *to; | 
 | 4734 | 	union sctp_addr temp; | 
 | 4735 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4736 | 	int addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4737 | 	int err = 0; | 
 | 4738 | 	size_t space_left; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4739 | 	int bytes_copied = 0; | 
 | 4740 | 	void *addrs; | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 4741 | 	void *buf; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4742 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4743 | 	if (len < sizeof(struct sctp_getaddrs)) | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4744 | 		return -EINVAL; | 
 | 4745 |  | 
 | 4746 | 	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 
 | 4747 | 		return -EFAULT; | 
 | 4748 |  | 
 | 4749 | 	/* | 
 | 4750 | 	 *  For UDP-style sockets, id specifies the association to query. | 
 | 4751 | 	 *  If the id field is set to the value '0' then the locally bound | 
 | 4752 | 	 *  addresses are returned without regard to any particular | 
 | 4753 | 	 *  association. | 
 | 4754 | 	 */ | 
 | 4755 | 	if (0 == getaddrs.assoc_id) { | 
 | 4756 | 		bp = &sctp_sk(sk)->ep->base.bind_addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4757 | 	} else { | 
 | 4758 | 		asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 
 | 4759 | 		if (!asoc) | 
 | 4760 | 			return -EINVAL; | 
 | 4761 | 		bp = &asoc->base.bind_addr; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4762 | 	} | 
 | 4763 |  | 
 | 4764 | 	to = optval + offsetof(struct sctp_getaddrs,addrs); | 
| Neil Horman | 186e234 | 2007-06-18 19:59:16 -0400 | [diff] [blame] | 4765 | 	space_left = len - offsetof(struct sctp_getaddrs,addrs); | 
 | 4766 |  | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4767 | 	addrs = kmalloc(space_left, GFP_KERNEL); | 
 | 4768 | 	if (!addrs) | 
 | 4769 | 		return -ENOMEM; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4770 |  | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4771 | 	/* If the endpoint is bound to 0.0.0.0 or ::0, get the valid | 
 | 4772 | 	 * addresses from the global local address list. | 
 | 4773 | 	 */ | 
 | 4774 | 	if (sctp_list_single_entry(&bp->address_list)) { | 
 | 4775 | 		addr = list_entry(bp->address_list.next, | 
 | 4776 | 				  struct sctp_sockaddr_entry, list); | 
| Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 4777 | 		if (sctp_is_any(sk, &addr->a)) { | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4778 | 			cnt = sctp_copy_laddrs(sk, bp->port, addrs, | 
 | 4779 | 						space_left, &bytes_copied); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4780 | 			if (cnt < 0) { | 
 | 4781 | 				err = cnt; | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 4782 | 				goto out; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4783 | 			} | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4784 | 			goto copy_getaddrs; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4785 | 		} | 
 | 4786 | 	} | 
 | 4787 |  | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 4788 | 	buf = addrs; | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 4789 | 	/* Protection on the bound address list is not needed since | 
 | 4790 | 	 * in the socket option context we hold a socket lock and | 
 | 4791 | 	 * thus the bound address list can't change. | 
 | 4792 | 	 */ | 
 | 4793 | 	list_for_each_entry(addr, &bp->address_list, list) { | 
| Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4794 | 		memcpy(&temp, &addr->a, sizeof(temp)); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4795 | 		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); | 
 | 4796 | 		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4797 | 		if (space_left < addrlen) { | 
 | 4798 | 			err =  -ENOMEM; /*fixme: right error?*/ | 
| Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 4799 | 			goto out; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4800 | 		} | 
| Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 4801 | 		memcpy(buf, &temp, addrlen); | 
 | 4802 | 		buf += addrlen; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4803 | 		bytes_copied += addrlen; | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4804 | 		cnt ++; | 
 | 4805 | 		space_left -= addrlen; | 
 | 4806 | 	} | 
 | 4807 |  | 
 | 4808 | copy_getaddrs: | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4809 | 	if (copy_to_user(to, addrs, bytes_copied)) { | 
 | 4810 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 4811 | 		goto out; | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4812 | 	} | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 4813 | 	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { | 
 | 4814 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 4815 | 		goto out; | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 4816 | 	} | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4817 | 	if (put_user(bytes_copied, optlen)) | 
| Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 4818 | 		err = -EFAULT; | 
| Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 4819 | out: | 
| Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4820 | 	kfree(addrs); | 
| Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4821 | 	return err; | 
 | 4822 | } | 
 | 4823 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 
 | 4825 |  * | 
 | 4826 |  * Requests that the local SCTP stack use the enclosed peer address as | 
 | 4827 |  * the association primary.  The enclosed address must be one of the | 
 | 4828 |  * association peer's addresses. | 
 | 4829 |  */ | 
 | 4830 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, | 
 | 4831 | 					char __user *optval, int __user *optlen) | 
 | 4832 | { | 
 | 4833 | 	struct sctp_prim prim; | 
 | 4834 | 	struct sctp_association *asoc; | 
 | 4835 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4836 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4837 | 	if (len < sizeof(struct sctp_prim)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | 		return -EINVAL; | 
 | 4839 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4840 | 	len = sizeof(struct sctp_prim); | 
 | 4841 |  | 
 | 4842 | 	if (copy_from_user(&prim, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | 		return -EFAULT; | 
 | 4844 |  | 
 | 4845 | 	asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); | 
 | 4846 | 	if (!asoc) | 
 | 4847 | 		return -EINVAL; | 
 | 4848 |  | 
 | 4849 | 	if (!asoc->peer.primary_path) | 
 | 4850 | 		return -ENOTCONN; | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4851 |  | 
| Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 4852 | 	memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, | 
 | 4853 | 		asoc->peer.primary_path->af_specific->sockaddr_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 |  | 
 | 4855 | 	sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, | 
 | 4856 | 			(union sctp_addr *)&prim.ssp_addr); | 
 | 4857 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4858 | 	if (put_user(len, optlen)) | 
 | 4859 | 		return -EFAULT; | 
 | 4860 | 	if (copy_to_user(optval, &prim, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | 		return -EFAULT; | 
 | 4862 |  | 
 | 4863 | 	return 0; | 
 | 4864 | } | 
 | 4865 |  | 
 | 4866 | /* | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4867 |  * 7.1.11  Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 |  * | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4869 |  * Requests that the local endpoint set the specified Adaptation Layer | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 |  * Indication parameter for all future INIT and INIT-ACK exchanges. | 
 | 4871 |  */ | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4872 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | 				  char __user *optval, int __user *optlen) | 
 | 4874 | { | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4875 | 	struct sctp_setadaptation adaptation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4877 | 	if (len < sizeof(struct sctp_setadaptation)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | 		return -EINVAL; | 
 | 4879 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4880 | 	len = sizeof(struct sctp_setadaptation); | 
 | 4881 |  | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4882 | 	adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4883 |  | 
 | 4884 | 	if (put_user(len, optlen)) | 
 | 4885 | 		return -EFAULT; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4886 | 	if (copy_to_user(optval, &adaptation, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | 		return -EFAULT; | 
| Ivan Skytte Jorgensen | a1ab358 | 2005-10-28 15:33:24 -0700 | [diff] [blame] | 4888 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | 	return 0; | 
 | 4890 | } | 
 | 4891 |  | 
 | 4892 | /* | 
 | 4893 |  * | 
 | 4894 |  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 
 | 4895 |  * | 
 | 4896 |  *   Applications that wish to use the sendto() system call may wish to | 
 | 4897 |  *   specify a default set of parameters that would normally be supplied | 
 | 4898 |  *   through the inclusion of ancillary data.  This socket option allows | 
 | 4899 |  *   such an application to set the default sctp_sndrcvinfo structure. | 
 | 4900 |  | 
 | 4901 |  | 
 | 4902 |  *   The application that wishes to use this socket option simply passes | 
 | 4903 |  *   in to this call the sctp_sndrcvinfo structure defined in Section | 
 | 4904 |  *   5.2.2) The input parameters accepted by this call include | 
 | 4905 |  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 
 | 4906 |  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in | 
 | 4907 |  *   to this call if the caller is using the UDP model. | 
 | 4908 |  * | 
 | 4909 |  *   For getsockopt, it get the default sctp_sndrcvinfo structure. | 
 | 4910 |  */ | 
 | 4911 | static int sctp_getsockopt_default_send_param(struct sock *sk, | 
 | 4912 | 					int len, char __user *optval, | 
 | 4913 | 					int __user *optlen) | 
 | 4914 | { | 
 | 4915 | 	struct sctp_sndrcvinfo info; | 
 | 4916 | 	struct sctp_association *asoc; | 
 | 4917 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 4918 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4919 | 	if (len < sizeof(struct sctp_sndrcvinfo)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | 		return -EINVAL; | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4921 |  | 
 | 4922 | 	len = sizeof(struct sctp_sndrcvinfo); | 
 | 4923 |  | 
 | 4924 | 	if (copy_from_user(&info, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | 		return -EFAULT; | 
 | 4926 |  | 
 | 4927 | 	asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 
 | 4928 | 	if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 
 | 4929 | 		return -EINVAL; | 
 | 4930 |  | 
 | 4931 | 	if (asoc) { | 
 | 4932 | 		info.sinfo_stream = asoc->default_stream; | 
 | 4933 | 		info.sinfo_flags = asoc->default_flags; | 
 | 4934 | 		info.sinfo_ppid = asoc->default_ppid; | 
 | 4935 | 		info.sinfo_context = asoc->default_context; | 
 | 4936 | 		info.sinfo_timetolive = asoc->default_timetolive; | 
 | 4937 | 	} else { | 
 | 4938 | 		info.sinfo_stream = sp->default_stream; | 
 | 4939 | 		info.sinfo_flags = sp->default_flags; | 
 | 4940 | 		info.sinfo_ppid = sp->default_ppid; | 
 | 4941 | 		info.sinfo_context = sp->default_context; | 
 | 4942 | 		info.sinfo_timetolive = sp->default_timetolive; | 
 | 4943 | 	} | 
 | 4944 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4945 | 	if (put_user(len, optlen)) | 
 | 4946 | 		return -EFAULT; | 
 | 4947 | 	if (copy_to_user(optval, &info, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | 		return -EFAULT; | 
 | 4949 |  | 
 | 4950 | 	return 0; | 
 | 4951 | } | 
 | 4952 |  | 
 | 4953 | /* | 
 | 4954 |  * | 
 | 4955 |  * 7.1.5 SCTP_NODELAY | 
 | 4956 |  * | 
 | 4957 |  * Turn on/off any Nagle-like algorithm.  This means that packets are | 
 | 4958 |  * generally sent as soon as possible and no unnecessary delays are | 
 | 4959 |  * introduced, at the cost of more packets in the network.  Expects an | 
 | 4960 |  * integer boolean flag. | 
 | 4961 |  */ | 
 | 4962 |  | 
 | 4963 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, | 
 | 4964 | 				   char __user *optval, int __user *optlen) | 
 | 4965 | { | 
 | 4966 | 	int val; | 
 | 4967 |  | 
 | 4968 | 	if (len < sizeof(int)) | 
 | 4969 | 		return -EINVAL; | 
 | 4970 |  | 
 | 4971 | 	len = sizeof(int); | 
 | 4972 | 	val = (sctp_sk(sk)->nodelay == 1); | 
 | 4973 | 	if (put_user(len, optlen)) | 
 | 4974 | 		return -EFAULT; | 
 | 4975 | 	if (copy_to_user(optval, &val, len)) | 
 | 4976 | 		return -EFAULT; | 
 | 4977 | 	return 0; | 
 | 4978 | } | 
 | 4979 |  | 
 | 4980 | /* | 
 | 4981 |  * | 
 | 4982 |  * 7.1.1 SCTP_RTOINFO | 
 | 4983 |  * | 
 | 4984 |  * The protocol parameters used to initialize and bound retransmission | 
 | 4985 |  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 
 | 4986 |  * and modify these parameters. | 
 | 4987 |  * All parameters are time values, in milliseconds.  A value of 0, when | 
 | 4988 |  * modifying the parameters, indicates that the current value should not | 
 | 4989 |  * be changed. | 
 | 4990 |  * | 
 | 4991 |  */ | 
 | 4992 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, | 
 | 4993 | 				char __user *optval, | 
 | 4994 | 				int __user *optlen) { | 
 | 4995 | 	struct sctp_rtoinfo rtoinfo; | 
 | 4996 | 	struct sctp_association *asoc; | 
 | 4997 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4998 | 	if (len < sizeof (struct sctp_rtoinfo)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | 		return -EINVAL; | 
 | 5000 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5001 | 	len = sizeof(struct sctp_rtoinfo); | 
 | 5002 |  | 
 | 5003 | 	if (copy_from_user(&rtoinfo, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | 		return -EFAULT; | 
 | 5005 |  | 
 | 5006 | 	asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 
 | 5007 |  | 
 | 5008 | 	if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 
 | 5009 | 		return -EINVAL; | 
 | 5010 |  | 
 | 5011 | 	/* Values corresponding to the specific association. */ | 
 | 5012 | 	if (asoc) { | 
 | 5013 | 		rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); | 
 | 5014 | 		rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); | 
 | 5015 | 		rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); | 
 | 5016 | 	} else { | 
 | 5017 | 		/* Values corresponding to the endpoint. */ | 
 | 5018 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 5019 |  | 
 | 5020 | 		rtoinfo.srto_initial = sp->rtoinfo.srto_initial; | 
 | 5021 | 		rtoinfo.srto_max = sp->rtoinfo.srto_max; | 
 | 5022 | 		rtoinfo.srto_min = sp->rtoinfo.srto_min; | 
 | 5023 | 	} | 
 | 5024 |  | 
 | 5025 | 	if (put_user(len, optlen)) | 
 | 5026 | 		return -EFAULT; | 
 | 5027 |  | 
 | 5028 | 	if (copy_to_user(optval, &rtoinfo, len)) | 
 | 5029 | 		return -EFAULT; | 
 | 5030 |  | 
 | 5031 | 	return 0; | 
 | 5032 | } | 
 | 5033 |  | 
 | 5034 | /* | 
 | 5035 |  * | 
 | 5036 |  * 7.1.2 SCTP_ASSOCINFO | 
 | 5037 |  * | 
| Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 5038 |  * This option is used to tune the maximum retransmission attempts | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 |  * of the association. | 
 | 5040 |  * Returns an error if the new association retransmission value is | 
 | 5041 |  * greater than the sum of the retransmission value  of the peer. | 
 | 5042 |  * See [SCTP] for more information. | 
 | 5043 |  * | 
 | 5044 |  */ | 
 | 5045 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, | 
 | 5046 | 				     char __user *optval, | 
 | 5047 | 				     int __user *optlen) | 
 | 5048 | { | 
 | 5049 |  | 
 | 5050 | 	struct sctp_assocparams assocparams; | 
 | 5051 | 	struct sctp_association *asoc; | 
 | 5052 | 	struct list_head *pos; | 
 | 5053 | 	int cnt = 0; | 
 | 5054 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5055 | 	if (len < sizeof (struct sctp_assocparams)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | 		return -EINVAL; | 
 | 5057 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5058 | 	len = sizeof(struct sctp_assocparams); | 
 | 5059 |  | 
 | 5060 | 	if (copy_from_user(&assocparams, optval, len)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | 		return -EFAULT; | 
 | 5062 |  | 
 | 5063 | 	asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 
 | 5064 |  | 
 | 5065 | 	if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 
 | 5066 | 		return -EINVAL; | 
 | 5067 |  | 
 | 5068 | 	/* Values correspoinding to the specific association */ | 
| Vladislav Yasevich | 1733721 | 2005-04-28 11:57:54 -0700 | [diff] [blame] | 5069 | 	if (asoc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | 		assocparams.sasoc_asocmaxrxt = asoc->max_retrans; | 
 | 5071 | 		assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; | 
 | 5072 | 		assocparams.sasoc_local_rwnd = asoc->a_rwnd; | 
 | 5073 | 		assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec | 
 | 5074 | 						* 1000) + | 
 | 5075 | 						(asoc->cookie_life.tv_usec | 
 | 5076 | 						/ 1000); | 
 | 5077 |  | 
 | 5078 | 		list_for_each(pos, &asoc->peer.transport_addr_list) { | 
 | 5079 | 			cnt ++; | 
 | 5080 | 		} | 
 | 5081 |  | 
 | 5082 | 		assocparams.sasoc_number_peer_destinations = cnt; | 
 | 5083 | 	} else { | 
 | 5084 | 		/* Values corresponding to the endpoint */ | 
 | 5085 | 		struct sctp_sock *sp = sctp_sk(sk); | 
 | 5086 |  | 
 | 5087 | 		assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; | 
 | 5088 | 		assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; | 
 | 5089 | 		assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; | 
 | 5090 | 		assocparams.sasoc_cookie_life = | 
 | 5091 | 					sp->assocparams.sasoc_cookie_life; | 
 | 5092 | 		assocparams.sasoc_number_peer_destinations = | 
 | 5093 | 					sp->assocparams. | 
 | 5094 | 					sasoc_number_peer_destinations; | 
 | 5095 | 	} | 
 | 5096 |  | 
 | 5097 | 	if (put_user(len, optlen)) | 
 | 5098 | 		return -EFAULT; | 
 | 5099 |  | 
 | 5100 | 	if (copy_to_user(optval, &assocparams, len)) | 
 | 5101 | 		return -EFAULT; | 
 | 5102 |  | 
 | 5103 | 	return 0; | 
 | 5104 | } | 
 | 5105 |  | 
 | 5106 | /* | 
 | 5107 |  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 
 | 5108 |  * | 
 | 5109 |  * This socket option is a boolean flag which turns on or off mapped V4 | 
 | 5110 |  * addresses.  If this option is turned on and the socket is type | 
 | 5111 |  * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 
 | 5112 |  * If this option is turned off, then no mapping will be done of V4 | 
 | 5113 |  * addresses and a user will receive both PF_INET6 and PF_INET type | 
 | 5114 |  * addresses on the socket. | 
 | 5115 |  */ | 
 | 5116 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, | 
 | 5117 | 				    char __user *optval, int __user *optlen) | 
 | 5118 | { | 
 | 5119 | 	int val; | 
 | 5120 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5121 |  | 
 | 5122 | 	if (len < sizeof(int)) | 
 | 5123 | 		return -EINVAL; | 
 | 5124 |  | 
 | 5125 | 	len = sizeof(int); | 
 | 5126 | 	val = sp->v4mapped; | 
 | 5127 | 	if (put_user(len, optlen)) | 
 | 5128 | 		return -EFAULT; | 
 | 5129 | 	if (copy_to_user(optval, &val, len)) | 
 | 5130 | 		return -EFAULT; | 
 | 5131 |  | 
 | 5132 | 	return 0; | 
 | 5133 | } | 
 | 5134 |  | 
 | 5135 | /* | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5136 |  * 7.1.29.  Set or Get the default context (SCTP_CONTEXT) | 
 | 5137 |  * (chapter and verse is quoted at sctp_setsockopt_context()) | 
 | 5138 |  */ | 
 | 5139 | static int sctp_getsockopt_context(struct sock *sk, int len, | 
 | 5140 | 				   char __user *optval, int __user *optlen) | 
 | 5141 | { | 
 | 5142 | 	struct sctp_assoc_value params; | 
 | 5143 | 	struct sctp_sock *sp; | 
 | 5144 | 	struct sctp_association *asoc; | 
 | 5145 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5146 | 	if (len < sizeof(struct sctp_assoc_value)) | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5147 | 		return -EINVAL; | 
 | 5148 |  | 
| Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5149 | 	len = sizeof(struct sctp_assoc_value); | 
 | 5150 |  | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5151 | 	if (copy_from_user(¶ms, optval, len)) | 
 | 5152 | 		return -EFAULT; | 
 | 5153 |  | 
 | 5154 | 	sp = sctp_sk(sk); | 
 | 5155 |  | 
 | 5156 | 	if (params.assoc_id != 0) { | 
 | 5157 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 5158 | 		if (!asoc) | 
 | 5159 | 			return -EINVAL; | 
 | 5160 | 		params.assoc_value = asoc->default_rcv_context; | 
 | 5161 | 	} else { | 
 | 5162 | 		params.assoc_value = sp->default_rcv_context; | 
 | 5163 | 	} | 
 | 5164 |  | 
 | 5165 | 	if (put_user(len, optlen)) | 
 | 5166 | 		return -EFAULT; | 
 | 5167 | 	if (copy_to_user(optval, ¶ms, len)) | 
 | 5168 | 		return -EFAULT; | 
 | 5169 |  | 
 | 5170 | 	return 0; | 
 | 5171 | } | 
 | 5172 |  | 
 | 5173 | /* | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5174 |  * 8.1.16.  Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 
 | 5175 |  * This option will get or set the maximum size to put in any outgoing | 
 | 5176 |  * 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] | 5177 |  * fragmented by SCTP into the specified size.  Note that the underlying | 
 | 5178 |  * SCTP implementation may fragment into smaller sized chunks when the | 
 | 5179 |  * PMTU of the underlying association is smaller than the value set by | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5180 |  * the user.  The default value for this option is '0' which indicates | 
 | 5181 |  * the user is NOT limiting fragmentation and only the PMTU will effect | 
 | 5182 |  * SCTP's choice of DATA chunk size.  Note also that values set larger | 
 | 5183 |  * than the maximum size of an IP datagram will effectively let SCTP | 
 | 5184 |  * control fragmentation (i.e. the same as setting this option to 0). | 
 | 5185 |  * | 
 | 5186 |  * The following structure is used to access and modify this parameter: | 
 | 5187 |  * | 
 | 5188 |  * struct sctp_assoc_value { | 
 | 5189 |  *   sctp_assoc_t assoc_id; | 
 | 5190 |  *   uint32_t assoc_value; | 
 | 5191 |  * }; | 
 | 5192 |  * | 
 | 5193 |  * assoc_id:  This parameter is ignored for one-to-one style sockets. | 
 | 5194 |  *    For one-to-many style sockets this parameter indicates which | 
 | 5195 |  *    association the user is performing an action upon.  Note that if | 
 | 5196 |  *    this field's value is zero then the endpoints default value is | 
 | 5197 |  *    changed (effecting future associations only). | 
 | 5198 |  * assoc_value:  This parameter specifies the maximum size in bytes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 |  */ | 
 | 5200 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, | 
 | 5201 | 				  char __user *optval, int __user *optlen) | 
 | 5202 | { | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5203 | 	struct sctp_assoc_value params; | 
 | 5204 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5206 | 	if (len == sizeof(int)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 5207 | 		pr_warn("Use of int in maxseg socket option deprecated\n"); | 
 | 5208 | 		pr_warn("Use struct sctp_assoc_value instead\n"); | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5209 | 		params.assoc_id = 0; | 
 | 5210 | 	} else if (len >= sizeof(struct sctp_assoc_value)) { | 
 | 5211 | 		len = sizeof(struct sctp_assoc_value); | 
 | 5212 | 		if (copy_from_user(¶ms, optval, sizeof(params))) | 
 | 5213 | 			return -EFAULT; | 
 | 5214 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | 		return -EINVAL; | 
 | 5216 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5217 | 	asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 5218 | 	if (!asoc && params.assoc_id && sctp_style(sk, UDP)) | 
 | 5219 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 |  | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5221 | 	if (asoc) | 
 | 5222 | 		params.assoc_value = asoc->frag_point; | 
 | 5223 | 	else | 
 | 5224 | 		params.assoc_value = sctp_sk(sk)->user_frag; | 
 | 5225 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | 	if (put_user(len, optlen)) | 
 | 5227 | 		return -EFAULT; | 
| Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 5228 | 	if (len == sizeof(int)) { | 
 | 5229 | 		if (copy_to_user(optval, ¶ms.assoc_value, len)) | 
 | 5230 | 			return -EFAULT; | 
 | 5231 | 	} else { | 
 | 5232 | 		if (copy_to_user(optval, ¶ms, len)) | 
 | 5233 | 			return -EFAULT; | 
 | 5234 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 |  | 
 | 5236 | 	return 0; | 
 | 5237 | } | 
 | 5238 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 5239 | /* | 
 | 5240 |  * 7.1.24.  Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 
 | 5241 |  * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) | 
 | 5242 |  */ | 
 | 5243 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, | 
 | 5244 | 					       char __user *optval, int __user *optlen) | 
 | 5245 | { | 
 | 5246 | 	int val; | 
 | 5247 |  | 
 | 5248 | 	if (len < sizeof(int)) | 
 | 5249 | 		return -EINVAL; | 
 | 5250 |  | 
 | 5251 | 	len = sizeof(int); | 
 | 5252 |  | 
 | 5253 | 	val = sctp_sk(sk)->frag_interleave; | 
 | 5254 | 	if (put_user(len, optlen)) | 
 | 5255 | 		return -EFAULT; | 
 | 5256 | 	if (copy_to_user(optval, &val, len)) | 
 | 5257 | 		return -EFAULT; | 
 | 5258 |  | 
 | 5259 | 	return 0; | 
 | 5260 | } | 
 | 5261 |  | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 5262 | /* | 
 | 5263 |  * 7.1.25.  Set or Get the sctp partial delivery point | 
 | 5264 |  * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) | 
 | 5265 |  */ | 
 | 5266 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, | 
 | 5267 | 						  char __user *optval, | 
 | 5268 | 						  int __user *optlen) | 
 | 5269 | { | 
| YOSHIFUJI Hideaki | 9cbcbf4 | 2007-07-19 10:44:50 +0900 | [diff] [blame] | 5270 | 	u32 val; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 5271 |  | 
 | 5272 | 	if (len < sizeof(u32)) | 
 | 5273 | 		return -EINVAL; | 
 | 5274 |  | 
 | 5275 | 	len = sizeof(u32); | 
 | 5276 |  | 
 | 5277 | 	val = sctp_sk(sk)->pd_point; | 
 | 5278 | 	if (put_user(len, optlen)) | 
 | 5279 | 		return -EFAULT; | 
 | 5280 | 	if (copy_to_user(optval, &val, len)) | 
 | 5281 | 		return -EFAULT; | 
 | 5282 |  | 
| Wei Yongjun | 7d743b7 | 2010-12-14 16:10:41 +0000 | [diff] [blame] | 5283 | 	return 0; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 5284 | } | 
 | 5285 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5286 | /* | 
 | 5287 |  * 7.1.28.  Set or Get the maximum burst (SCTP_MAX_BURST) | 
 | 5288 |  * (chapter and verse is quoted at sctp_setsockopt_maxburst()) | 
 | 5289 |  */ | 
 | 5290 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, | 
 | 5291 | 				    char __user *optval, | 
 | 5292 | 				    int __user *optlen) | 
 | 5293 | { | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5294 | 	struct sctp_assoc_value params; | 
 | 5295 | 	struct sctp_sock *sp; | 
 | 5296 | 	struct sctp_association *asoc; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5297 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5298 | 	if (len == sizeof(int)) { | 
| Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 5299 | 		pr_warn("Use of int in max_burst socket option deprecated\n"); | 
 | 5300 | 		pr_warn("Use struct sctp_assoc_value instead\n"); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5301 | 		params.assoc_id = 0; | 
| Wei Yongjun | c6db93a | 2009-03-02 09:46:12 +0000 | [diff] [blame] | 5302 | 	} else if (len >= sizeof(struct sctp_assoc_value)) { | 
 | 5303 | 		len = sizeof(struct sctp_assoc_value); | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5304 | 		if (copy_from_user(¶ms, optval, len)) | 
 | 5305 | 			return -EFAULT; | 
 | 5306 | 	} else | 
 | 5307 | 		return -EINVAL; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5308 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5309 | 	sp = sctp_sk(sk); | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5310 |  | 
| Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 5311 | 	if (params.assoc_id != 0) { | 
 | 5312 | 		asoc = sctp_id2assoc(sk, params.assoc_id); | 
 | 5313 | 		if (!asoc) | 
 | 5314 | 			return -EINVAL; | 
 | 5315 | 		params.assoc_value = asoc->max_burst; | 
 | 5316 | 	} else | 
 | 5317 | 		params.assoc_value = sp->max_burst; | 
 | 5318 |  | 
 | 5319 | 	if (len == sizeof(int)) { | 
 | 5320 | 		if (copy_to_user(optval, ¶ms.assoc_value, len)) | 
 | 5321 | 			return -EFAULT; | 
 | 5322 | 	} else { | 
 | 5323 | 		if (copy_to_user(optval, ¶ms, len)) | 
 | 5324 | 			return -EFAULT; | 
 | 5325 | 	} | 
 | 5326 |  | 
 | 5327 | 	return 0; | 
 | 5328 |  | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5329 | } | 
 | 5330 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5331 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, | 
 | 5332 | 				    char __user *optval, int __user *optlen) | 
 | 5333 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5334 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5335 | 	struct sctp_hmacalgo  __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5336 | 	struct sctp_hmac_algo_param *hmacs; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5337 | 	__u16 data_len = 0; | 
 | 5338 | 	u32 num_idents; | 
 | 5339 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5340 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5341 | 		return -EACCES; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5342 |  | 
 | 5343 | 	hmacs = sctp_sk(sk)->ep->auth_hmacs_list; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5344 | 	data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5345 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5346 | 	if (len < sizeof(struct sctp_hmacalgo) + data_len) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5347 | 		return -EINVAL; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5348 |  | 
 | 5349 | 	len = sizeof(struct sctp_hmacalgo) + data_len; | 
 | 5350 | 	num_idents = data_len / sizeof(u16); | 
 | 5351 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5352 | 	if (put_user(len, optlen)) | 
 | 5353 | 		return -EFAULT; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5354 | 	if (put_user(num_idents, &p->shmac_num_idents)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5355 | 		return -EFAULT; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5356 | 	if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len)) | 
 | 5357 | 		return -EFAULT; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5358 | 	return 0; | 
 | 5359 | } | 
 | 5360 |  | 
 | 5361 | static int sctp_getsockopt_active_key(struct sock *sk, int len, | 
 | 5362 | 				    char __user *optval, int __user *optlen) | 
 | 5363 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5364 | 	struct net *net = sock_net(sk); | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5365 | 	struct sctp_authkeyid val; | 
 | 5366 | 	struct sctp_association *asoc; | 
 | 5367 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5368 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5369 | 		return -EACCES; | 
 | 5370 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5371 | 	if (len < sizeof(struct sctp_authkeyid)) | 
 | 5372 | 		return -EINVAL; | 
 | 5373 | 	if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid))) | 
 | 5374 | 		return -EFAULT; | 
 | 5375 |  | 
 | 5376 | 	asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 
 | 5377 | 	if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 
 | 5378 | 		return -EINVAL; | 
 | 5379 |  | 
 | 5380 | 	if (asoc) | 
 | 5381 | 		val.scact_keynumber = asoc->active_key_id; | 
 | 5382 | 	else | 
 | 5383 | 		val.scact_keynumber = sctp_sk(sk)->ep->active_key_id; | 
 | 5384 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5385 | 	len = sizeof(struct sctp_authkeyid); | 
 | 5386 | 	if (put_user(len, optlen)) | 
 | 5387 | 		return -EFAULT; | 
 | 5388 | 	if (copy_to_user(optval, &val, len)) | 
 | 5389 | 		return -EFAULT; | 
 | 5390 |  | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5391 | 	return 0; | 
 | 5392 | } | 
 | 5393 |  | 
 | 5394 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, | 
 | 5395 | 				    char __user *optval, int __user *optlen) | 
 | 5396 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5397 | 	struct net *net = sock_net(sk); | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 5398 | 	struct sctp_authchunks __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5399 | 	struct sctp_authchunks val; | 
 | 5400 | 	struct sctp_association *asoc; | 
 | 5401 | 	struct sctp_chunks_param *ch; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5402 | 	u32    num_chunks = 0; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5403 | 	char __user *to; | 
 | 5404 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5405 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5406 | 		return -EACCES; | 
 | 5407 |  | 
 | 5408 | 	if (len < sizeof(struct sctp_authchunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5409 | 		return -EINVAL; | 
 | 5410 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5411 | 	if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5412 | 		return -EFAULT; | 
 | 5413 |  | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 5414 | 	to = p->gauth_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5415 | 	asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 
 | 5416 | 	if (!asoc) | 
 | 5417 | 		return -EINVAL; | 
 | 5418 |  | 
 | 5419 | 	ch = asoc->peer.peer_chunks; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5420 | 	if (!ch) | 
 | 5421 | 		goto num; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5422 |  | 
 | 5423 | 	/* See if the user provided enough room for all the data */ | 
| Vlad Yasevich | b40db68 | 2008-02-27 14:40:37 -0500 | [diff] [blame] | 5424 | 	num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); | 
 | 5425 | 	if (len < num_chunks) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5426 | 		return -EINVAL; | 
 | 5427 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5428 | 	if (copy_to_user(to, ch->chunks, num_chunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5429 | 		return -EFAULT; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5430 | num: | 
 | 5431 | 	len = sizeof(struct sctp_authchunks) + num_chunks; | 
 | 5432 | 	if (put_user(len, optlen)) return -EFAULT; | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 5433 | 	if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 
 | 5434 | 		return -EFAULT; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5435 | 	return 0; | 
 | 5436 | } | 
 | 5437 |  | 
 | 5438 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, | 
 | 5439 | 				    char __user *optval, int __user *optlen) | 
 | 5440 | { | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5441 | 	struct net *net = sock_net(sk); | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 5442 | 	struct sctp_authchunks __user *p = (void __user *)optval; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5443 | 	struct sctp_authchunks val; | 
 | 5444 | 	struct sctp_association *asoc; | 
 | 5445 | 	struct sctp_chunks_param *ch; | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5446 | 	u32    num_chunks = 0; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5447 | 	char __user *to; | 
 | 5448 |  | 
| Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5449 | 	if (!net->sctp.auth_enable) | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5450 | 		return -EACCES; | 
 | 5451 |  | 
 | 5452 | 	if (len < sizeof(struct sctp_authchunks)) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5453 | 		return -EINVAL; | 
 | 5454 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5455 | 	if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5456 | 		return -EFAULT; | 
 | 5457 |  | 
| Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 5458 | 	to = p->gauth_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5459 | 	asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 
 | 5460 | 	if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) | 
 | 5461 | 		return -EINVAL; | 
 | 5462 |  | 
 | 5463 | 	if (asoc) | 
 | 5464 | 		ch = (struct sctp_chunks_param*)asoc->c.auth_chunks; | 
 | 5465 | 	else | 
 | 5466 | 		ch = sctp_sk(sk)->ep->auth_chunk_list; | 
 | 5467 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5468 | 	if (!ch) | 
 | 5469 | 		goto num; | 
 | 5470 |  | 
| Vlad Yasevich | b40db68 | 2008-02-27 14:40:37 -0500 | [diff] [blame] | 5471 | 	num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5472 | 	if (len < sizeof(struct sctp_authchunks) + num_chunks) | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5473 | 		return -EINVAL; | 
 | 5474 |  | 
| Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 5475 | 	if (copy_to_user(to, ch->chunks, num_chunks)) | 
 | 5476 | 		return -EFAULT; | 
 | 5477 | num: | 
 | 5478 | 	len = sizeof(struct sctp_authchunks) + num_chunks; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5479 | 	if (put_user(len, optlen)) | 
 | 5480 | 		return -EFAULT; | 
| Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 5481 | 	if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 
 | 5482 | 		return -EFAULT; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5483 |  | 
 | 5484 | 	return 0; | 
 | 5485 | } | 
 | 5486 |  | 
| Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 5487 | /* | 
 | 5488 |  * 8.2.5.  Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) | 
 | 5489 |  * This option gets the current number of associations that are attached | 
 | 5490 |  * to a one-to-many style socket.  The option value is an uint32_t. | 
 | 5491 |  */ | 
 | 5492 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, | 
 | 5493 | 				    char __user *optval, int __user *optlen) | 
 | 5494 | { | 
 | 5495 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5496 | 	struct sctp_association *asoc; | 
 | 5497 | 	u32 val = 0; | 
 | 5498 |  | 
 | 5499 | 	if (sctp_style(sk, TCP)) | 
 | 5500 | 		return -EOPNOTSUPP; | 
 | 5501 |  | 
 | 5502 | 	if (len < sizeof(u32)) | 
 | 5503 | 		return -EINVAL; | 
 | 5504 |  | 
 | 5505 | 	len = sizeof(u32); | 
 | 5506 |  | 
 | 5507 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 5508 | 		val++; | 
 | 5509 | 	} | 
 | 5510 |  | 
 | 5511 | 	if (put_user(len, optlen)) | 
 | 5512 | 		return -EFAULT; | 
 | 5513 | 	if (copy_to_user(optval, &val, len)) | 
 | 5514 | 		return -EFAULT; | 
 | 5515 |  | 
 | 5516 | 	return 0; | 
 | 5517 | } | 
 | 5518 |  | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 5519 | /* | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 5520 |  * 8.1.23 SCTP_AUTO_ASCONF | 
 | 5521 |  * See the corresponding setsockopt entry as description | 
 | 5522 |  */ | 
 | 5523 | static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, | 
 | 5524 | 				   char __user *optval, int __user *optlen) | 
 | 5525 | { | 
 | 5526 | 	int val = 0; | 
 | 5527 |  | 
 | 5528 | 	if (len < sizeof(int)) | 
 | 5529 | 		return -EINVAL; | 
 | 5530 |  | 
 | 5531 | 	len = sizeof(int); | 
 | 5532 | 	if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) | 
 | 5533 | 		val = 1; | 
 | 5534 | 	if (put_user(len, optlen)) | 
 | 5535 | 		return -EFAULT; | 
 | 5536 | 	if (copy_to_user(optval, &val, len)) | 
 | 5537 | 		return -EFAULT; | 
 | 5538 | 	return 0; | 
 | 5539 | } | 
 | 5540 |  | 
 | 5541 | /* | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 5542 |  * 8.2.6. Get the Current Identifiers of Associations | 
 | 5543 |  *        (SCTP_GET_ASSOC_ID_LIST) | 
 | 5544 |  * | 
 | 5545 |  * This option gets the current list of SCTP association identifiers of | 
 | 5546 |  * the SCTP associations handled by a one-to-many style socket. | 
 | 5547 |  */ | 
 | 5548 | static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, | 
 | 5549 | 				    char __user *optval, int __user *optlen) | 
 | 5550 | { | 
 | 5551 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5552 | 	struct sctp_association *asoc; | 
 | 5553 | 	struct sctp_assoc_ids *ids; | 
 | 5554 | 	u32 num = 0; | 
 | 5555 |  | 
 | 5556 | 	if (sctp_style(sk, TCP)) | 
 | 5557 | 		return -EOPNOTSUPP; | 
 | 5558 |  | 
 | 5559 | 	if (len < sizeof(struct sctp_assoc_ids)) | 
 | 5560 | 		return -EINVAL; | 
 | 5561 |  | 
 | 5562 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 5563 | 		num++; | 
 | 5564 | 	} | 
 | 5565 |  | 
 | 5566 | 	if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num) | 
 | 5567 | 		return -EINVAL; | 
 | 5568 |  | 
 | 5569 | 	len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num; | 
 | 5570 |  | 
 | 5571 | 	ids = kmalloc(len, GFP_KERNEL); | 
 | 5572 | 	if (unlikely(!ids)) | 
 | 5573 | 		return -ENOMEM; | 
 | 5574 |  | 
 | 5575 | 	ids->gaids_number_of_ids = num; | 
 | 5576 | 	num = 0; | 
 | 5577 | 	list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 
 | 5578 | 		ids->gaids_assoc_id[num++] = asoc->assoc_id; | 
 | 5579 | 	} | 
 | 5580 |  | 
 | 5581 | 	if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { | 
 | 5582 | 		kfree(ids); | 
 | 5583 | 		return -EFAULT; | 
 | 5584 | 	} | 
 | 5585 |  | 
 | 5586 | 	kfree(ids); | 
 | 5587 | 	return 0; | 
 | 5588 | } | 
 | 5589 |  | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 5590 | /* | 
 | 5591 |  * SCTP_PEER_ADDR_THLDS | 
 | 5592 |  * | 
 | 5593 |  * This option allows us to fetch the partially failed threshold for one or all | 
 | 5594 |  * transports in an association.  See Section 6.1 of: | 
 | 5595 |  * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt | 
 | 5596 |  */ | 
 | 5597 | static int sctp_getsockopt_paddr_thresholds(struct sock *sk, | 
 | 5598 | 					    char __user *optval, | 
 | 5599 | 					    int len, | 
 | 5600 | 					    int __user *optlen) | 
 | 5601 | { | 
 | 5602 | 	struct sctp_paddrthlds val; | 
 | 5603 | 	struct sctp_transport *trans; | 
 | 5604 | 	struct sctp_association *asoc; | 
 | 5605 |  | 
 | 5606 | 	if (len < sizeof(struct sctp_paddrthlds)) | 
 | 5607 | 		return -EINVAL; | 
 | 5608 | 	len = sizeof(struct sctp_paddrthlds); | 
 | 5609 | 	if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len)) | 
 | 5610 | 		return -EFAULT; | 
 | 5611 |  | 
 | 5612 | 	if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { | 
 | 5613 | 		asoc = sctp_id2assoc(sk, val.spt_assoc_id); | 
 | 5614 | 		if (!asoc) | 
 | 5615 | 			return -ENOENT; | 
 | 5616 |  | 
 | 5617 | 		val.spt_pathpfthld = asoc->pf_retrans; | 
 | 5618 | 		val.spt_pathmaxrxt = asoc->pathmaxrxt; | 
 | 5619 | 	} else { | 
 | 5620 | 		trans = sctp_addr_id2transport(sk, &val.spt_address, | 
 | 5621 | 					       val.spt_assoc_id); | 
 | 5622 | 		if (!trans) | 
 | 5623 | 			return -ENOENT; | 
 | 5624 |  | 
 | 5625 | 		val.spt_pathmaxrxt = trans->pathmaxrxt; | 
 | 5626 | 		val.spt_pathpfthld = trans->pf_retrans; | 
 | 5627 | 	} | 
 | 5628 |  | 
 | 5629 | 	if (put_user(len, optlen) || copy_to_user(optval, &val, len)) | 
 | 5630 | 		return -EFAULT; | 
 | 5631 |  | 
 | 5632 | 	return 0; | 
 | 5633 | } | 
 | 5634 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname, | 
 | 5636 | 				char __user *optval, int __user *optlen) | 
 | 5637 | { | 
 | 5638 | 	int retval = 0; | 
 | 5639 | 	int len; | 
 | 5640 |  | 
| Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5641 | 	SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n", | 
 | 5642 | 			  sk, optname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 |  | 
 | 5644 | 	/* I can hardly begin to describe how wrong this is.  This is | 
 | 5645 | 	 * so broken as to be worse than useless.  The API draft | 
 | 5646 | 	 * REALLY is NOT helpful here...  I am not convinced that the | 
 | 5647 | 	 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP | 
 | 5648 | 	 * are at all well-founded. | 
 | 5649 | 	 */ | 
 | 5650 | 	if (level != SOL_SCTP) { | 
 | 5651 | 		struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 5652 |  | 
 | 5653 | 		retval = af->getsockopt(sk, level, optname, optval, optlen); | 
 | 5654 | 		return retval; | 
 | 5655 | 	} | 
 | 5656 |  | 
 | 5657 | 	if (get_user(len, optlen)) | 
 | 5658 | 		return -EFAULT; | 
 | 5659 |  | 
 | 5660 | 	sctp_lock_sock(sk); | 
 | 5661 |  | 
 | 5662 | 	switch (optname) { | 
 | 5663 | 	case SCTP_STATUS: | 
 | 5664 | 		retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); | 
 | 5665 | 		break; | 
 | 5666 | 	case SCTP_DISABLE_FRAGMENTS: | 
 | 5667 | 		retval = sctp_getsockopt_disable_fragments(sk, len, optval, | 
 | 5668 | 							   optlen); | 
 | 5669 | 		break; | 
 | 5670 | 	case SCTP_EVENTS: | 
 | 5671 | 		retval = sctp_getsockopt_events(sk, len, optval, optlen); | 
 | 5672 | 		break; | 
 | 5673 | 	case SCTP_AUTOCLOSE: | 
 | 5674 | 		retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); | 
 | 5675 | 		break; | 
 | 5676 | 	case SCTP_SOCKOPT_PEELOFF: | 
 | 5677 | 		retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); | 
 | 5678 | 		break; | 
 | 5679 | 	case SCTP_PEER_ADDR_PARAMS: | 
 | 5680 | 		retval = sctp_getsockopt_peer_addr_params(sk, len, optval, | 
 | 5681 | 							  optlen); | 
 | 5682 | 		break; | 
| Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 5683 | 	case SCTP_DELAYED_SACK: | 
| Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5684 | 		retval = sctp_getsockopt_delayed_ack(sk, len, optval, | 
| Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5685 | 							  optlen); | 
 | 5686 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | 	case SCTP_INITMSG: | 
 | 5688 | 		retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); | 
 | 5689 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | 	case SCTP_GET_PEER_ADDRS: | 
 | 5691 | 		retval = sctp_getsockopt_peer_addrs(sk, len, optval, | 
 | 5692 | 						    optlen); | 
 | 5693 | 		break; | 
 | 5694 | 	case SCTP_GET_LOCAL_ADDRS: | 
 | 5695 | 		retval = sctp_getsockopt_local_addrs(sk, len, optval, | 
 | 5696 | 						     optlen); | 
 | 5697 | 		break; | 
| Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 5698 | 	case SCTP_SOCKOPT_CONNECTX3: | 
 | 5699 | 		retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); | 
 | 5700 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | 	case SCTP_DEFAULT_SEND_PARAM: | 
 | 5702 | 		retval = sctp_getsockopt_default_send_param(sk, len, | 
 | 5703 | 							    optval, optlen); | 
 | 5704 | 		break; | 
 | 5705 | 	case SCTP_PRIMARY_ADDR: | 
 | 5706 | 		retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); | 
 | 5707 | 		break; | 
 | 5708 | 	case SCTP_NODELAY: | 
 | 5709 | 		retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); | 
 | 5710 | 		break; | 
 | 5711 | 	case SCTP_RTOINFO: | 
 | 5712 | 		retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); | 
 | 5713 | 		break; | 
 | 5714 | 	case SCTP_ASSOCINFO: | 
 | 5715 | 		retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); | 
 | 5716 | 		break; | 
 | 5717 | 	case SCTP_I_WANT_MAPPED_V4_ADDR: | 
 | 5718 | 		retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); | 
 | 5719 | 		break; | 
 | 5720 | 	case SCTP_MAXSEG: | 
 | 5721 | 		retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); | 
 | 5722 | 		break; | 
 | 5723 | 	case SCTP_GET_PEER_ADDR_INFO: | 
 | 5724 | 		retval = sctp_getsockopt_peer_addr_info(sk, len, optval, | 
 | 5725 | 							optlen); | 
 | 5726 | 		break; | 
| Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5727 | 	case SCTP_ADAPTATION_LAYER: | 
 | 5728 | 		retval = sctp_getsockopt_adaptation_layer(sk, len, optval, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | 							optlen); | 
 | 5730 | 		break; | 
| Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5731 | 	case SCTP_CONTEXT: | 
 | 5732 | 		retval = sctp_getsockopt_context(sk, len, optval, optlen); | 
 | 5733 | 		break; | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 5734 | 	case SCTP_FRAGMENT_INTERLEAVE: | 
 | 5735 | 		retval = sctp_getsockopt_fragment_interleave(sk, len, optval, | 
 | 5736 | 							     optlen); | 
 | 5737 | 		break; | 
| Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 5738 | 	case SCTP_PARTIAL_DELIVERY_POINT: | 
 | 5739 | 		retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, | 
 | 5740 | 								optlen); | 
 | 5741 | 		break; | 
| Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 5742 | 	case SCTP_MAX_BURST: | 
 | 5743 | 		retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); | 
 | 5744 | 		break; | 
| Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 5745 | 	case SCTP_AUTH_KEY: | 
 | 5746 | 	case SCTP_AUTH_CHUNK: | 
 | 5747 | 	case SCTP_AUTH_DELETE_KEY: | 
 | 5748 | 		retval = -EOPNOTSUPP; | 
 | 5749 | 		break; | 
 | 5750 | 	case SCTP_HMAC_IDENT: | 
 | 5751 | 		retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); | 
 | 5752 | 		break; | 
 | 5753 | 	case SCTP_AUTH_ACTIVE_KEY: | 
 | 5754 | 		retval = sctp_getsockopt_active_key(sk, len, optval, optlen); | 
 | 5755 | 		break; | 
 | 5756 | 	case SCTP_PEER_AUTH_CHUNKS: | 
 | 5757 | 		retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, | 
 | 5758 | 							optlen); | 
 | 5759 | 		break; | 
 | 5760 | 	case SCTP_LOCAL_AUTH_CHUNKS: | 
 | 5761 | 		retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, | 
 | 5762 | 							optlen); | 
 | 5763 | 		break; | 
| Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 5764 | 	case SCTP_GET_ASSOC_NUMBER: | 
 | 5765 | 		retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); | 
 | 5766 | 		break; | 
| Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 5767 | 	case SCTP_GET_ASSOC_ID_LIST: | 
 | 5768 | 		retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); | 
 | 5769 | 		break; | 
| Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 5770 | 	case SCTP_AUTO_ASCONF: | 
 | 5771 | 		retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); | 
 | 5772 | 		break; | 
| Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 5773 | 	case SCTP_PEER_ADDR_THLDS: | 
 | 5774 | 		retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen); | 
 | 5775 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | 	default: | 
 | 5777 | 		retval = -ENOPROTOOPT; | 
 | 5778 | 		break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 5779 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 |  | 
 | 5781 | 	sctp_release_sock(sk); | 
 | 5782 | 	return retval; | 
 | 5783 | } | 
 | 5784 |  | 
 | 5785 | static void sctp_hash(struct sock *sk) | 
 | 5786 | { | 
 | 5787 | 	/* STUB */ | 
 | 5788 | } | 
 | 5789 |  | 
 | 5790 | static void sctp_unhash(struct sock *sk) | 
 | 5791 | { | 
 | 5792 | 	/* STUB */ | 
 | 5793 | } | 
 | 5794 |  | 
 | 5795 | /* Check if port is acceptable.  Possibly find first available port. | 
 | 5796 |  * | 
 | 5797 |  * The port hash table (contained in the 'global' SCTP protocol storage | 
 | 5798 |  * returned by struct sctp_protocol *sctp_get_protocol()). The hash | 
 | 5799 |  * table is an array of 4096 lists (sctp_bind_hashbucket). Each | 
 | 5800 |  * list (the list number is the port number hashed out, so as you | 
 | 5801 |  * would expect from a hash function, all the ports in a given list have | 
 | 5802 |  * such a number that hashes out to the same list number; you were | 
 | 5803 |  * expecting that, right?); so each list has a set of ports, with a | 
 | 5804 |  * link to the socket (struct sock) that uses it, the port number and | 
 | 5805 |  * a fastreuse flag (FIXME: NPI ipg). | 
 | 5806 |  */ | 
 | 5807 | static struct sctp_bind_bucket *sctp_bucket_create( | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5808 | 	struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 |  | 
 | 5810 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) | 
 | 5811 | { | 
 | 5812 | 	struct sctp_bind_hashbucket *head; /* hash list */ | 
 | 5813 | 	struct sctp_bind_bucket *pp; /* hash list port iterator */ | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 5814 | 	struct hlist_node *node; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | 	unsigned short snum; | 
 | 5816 | 	int ret; | 
 | 5817 |  | 
| Al Viro | 04afd8b | 2006-11-20 17:02:01 -0800 | [diff] [blame] | 5818 | 	snum = ntohs(addr->v4.sin_port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 |  | 
 | 5820 | 	SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum); | 
 | 5821 | 	sctp_local_bh_disable(); | 
 | 5822 |  | 
 | 5823 | 	if (snum == 0) { | 
| Stephen Hemminger | 0639300 | 2007-10-10 17:30:18 -0700 | [diff] [blame] | 5824 | 		/* Search for an available port. */ | 
| Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 5825 | 		int low, high, remaining, index; | 
 | 5826 | 		unsigned int rover; | 
 | 5827 |  | 
 | 5828 | 		inet_get_local_port_range(&low, &high); | 
 | 5829 | 		remaining = (high - low) + 1; | 
 | 5830 | 		rover = net_random() % remaining + low; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | 		do { | 
 | 5833 | 			rover++; | 
 | 5834 | 			if ((rover < low) || (rover > high)) | 
 | 5835 | 				rover = low; | 
| Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 5836 | 			if (inet_is_reserved_local_port(rover)) | 
 | 5837 | 				continue; | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5838 | 			index = sctp_phashfn(sock_net(sk), rover); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | 			head = &sctp_port_hashtable[index]; | 
 | 5840 | 			sctp_spin_lock(&head->lock); | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 5841 | 			sctp_for_each_hentry(pp, node, &head->chain) | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5842 | 				if ((pp->port == rover) && | 
 | 5843 | 				    net_eq(sock_net(sk), pp->net)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | 					goto next; | 
 | 5845 | 			break; | 
 | 5846 | 		next: | 
 | 5847 | 			sctp_spin_unlock(&head->lock); | 
 | 5848 | 		} while (--remaining > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 |  | 
 | 5850 | 		/* Exhausted local port range during search? */ | 
 | 5851 | 		ret = 1; | 
 | 5852 | 		if (remaining <= 0) | 
 | 5853 | 			goto fail; | 
 | 5854 |  | 
 | 5855 | 		/* OK, here is the one we will use.  HEAD (the port | 
 | 5856 | 		 * hash table list entry) is non-NULL and we hold it's | 
 | 5857 | 		 * mutex. | 
 | 5858 | 		 */ | 
 | 5859 | 		snum = rover; | 
 | 5860 | 	} else { | 
 | 5861 | 		/* We are given an specific port number; we verify | 
 | 5862 | 		 * that it is not being used. If it is used, we will | 
 | 5863 | 		 * exahust the search in the hash list corresponding | 
 | 5864 | 		 * to the port number (snum) - we detect that with the | 
 | 5865 | 		 * port iterator, pp being NULL. | 
 | 5866 | 		 */ | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5867 | 		head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | 		sctp_spin_lock(&head->lock); | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 5869 | 		sctp_for_each_hentry(pp, node, &head->chain) { | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5870 | 			if ((pp->port == snum) && net_eq(pp->net, sock_net(sk))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | 				goto pp_found; | 
 | 5872 | 		} | 
 | 5873 | 	} | 
 | 5874 | 	pp = NULL; | 
 | 5875 | 	goto pp_not_found; | 
 | 5876 | pp_found: | 
 | 5877 | 	if (!hlist_empty(&pp->owner)) { | 
 | 5878 | 		/* We had a port hash table hit - there is an | 
 | 5879 | 		 * available port (pp != NULL) and it is being | 
 | 5880 | 		 * used by other socket (pp->owner not empty); that other | 
 | 5881 | 		 * socket is going to be sk2. | 
 | 5882 | 		 */ | 
 | 5883 | 		int reuse = sk->sk_reuse; | 
 | 5884 | 		struct sock *sk2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 |  | 
 | 5886 | 		SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n"); | 
| Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 5887 | 		if (pp->fastreuse && sk->sk_reuse && | 
 | 5888 | 			sk->sk_state != SCTP_SS_LISTENING) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | 			goto success; | 
 | 5890 |  | 
 | 5891 | 		/* Run through the list of sockets bound to the port | 
 | 5892 | 		 * (pp->port) [via the pointers bind_next and | 
 | 5893 | 		 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, | 
 | 5894 | 		 * we get the endpoint they describe and run through | 
 | 5895 | 		 * the endpoint's list of IP (v4 or v6) addresses, | 
 | 5896 | 		 * comparing each of the addresses with the address of | 
 | 5897 | 		 * the socket sk. If we find a match, then that means | 
 | 5898 | 		 * that this port/socket (sk) combination are already | 
 | 5899 | 		 * in an endpoint. | 
 | 5900 | 		 */ | 
 | 5901 | 		sk_for_each_bound(sk2, node, &pp->owner) { | 
 | 5902 | 			struct sctp_endpoint *ep2; | 
 | 5903 | 			ep2 = sctp_sk(sk2)->ep; | 
 | 5904 |  | 
| Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 5905 | 			if (sk == sk2 || | 
 | 5906 | 			    (reuse && sk2->sk_reuse && | 
 | 5907 | 			     sk2->sk_state != SCTP_SS_LISTENING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | 				continue; | 
 | 5909 |  | 
| Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 5910 | 			if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, | 
 | 5911 | 						 sctp_sk(sk2), sctp_sk(sk))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | 				ret = (long)sk2; | 
 | 5913 | 				goto fail_unlock; | 
 | 5914 | 			} | 
 | 5915 | 		} | 
 | 5916 | 		SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n"); | 
 | 5917 | 	} | 
 | 5918 | pp_not_found: | 
 | 5919 | 	/* If there was a hash table miss, create a new port.  */ | 
 | 5920 | 	ret = 1; | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 5921 | 	if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | 		goto fail_unlock; | 
 | 5923 |  | 
 | 5924 | 	/* In either case (hit or miss), make sure fastreuse is 1 only | 
 | 5925 | 	 * if sk->sk_reuse is too (that is, if the caller requested | 
 | 5926 | 	 * SO_REUSEADDR on this socket -sk-). | 
 | 5927 | 	 */ | 
| Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 5928 | 	if (hlist_empty(&pp->owner)) { | 
 | 5929 | 		if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) | 
 | 5930 | 			pp->fastreuse = 1; | 
 | 5931 | 		else | 
 | 5932 | 			pp->fastreuse = 0; | 
 | 5933 | 	} else if (pp->fastreuse && | 
 | 5934 | 		(!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | 		pp->fastreuse = 0; | 
 | 5936 |  | 
 | 5937 | 	/* We are set, so fill up all the data in the hash table | 
 | 5938 | 	 * entry, tie the socket list information with the rest of the | 
 | 5939 | 	 * sockets FIXME: Blurry, NPI (ipg). | 
 | 5940 | 	 */ | 
 | 5941 | success: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | 	if (!sctp_sk(sk)->bind_hash) { | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 5943 | 		inet_sk(sk)->inet_num = snum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | 		sk_add_bind_node(sk, &pp->owner); | 
 | 5945 | 		sctp_sk(sk)->bind_hash = pp; | 
 | 5946 | 	} | 
 | 5947 | 	ret = 0; | 
 | 5948 |  | 
 | 5949 | fail_unlock: | 
 | 5950 | 	sctp_spin_unlock(&head->lock); | 
 | 5951 |  | 
 | 5952 | fail: | 
 | 5953 | 	sctp_local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | 	return ret; | 
 | 5955 | } | 
 | 5956 |  | 
 | 5957 | /* Assign a 'snum' port to the socket.  If snum == 0, an ephemeral | 
 | 5958 |  * port is requested. | 
 | 5959 |  */ | 
 | 5960 | static int sctp_get_port(struct sock *sk, unsigned short snum) | 
 | 5961 | { | 
 | 5962 | 	long ret; | 
 | 5963 | 	union sctp_addr addr; | 
 | 5964 | 	struct sctp_af *af = sctp_sk(sk)->pf->af; | 
 | 5965 |  | 
 | 5966 | 	/* Set up a dummy address struct from the sk. */ | 
 | 5967 | 	af->from_sk(&addr, sk); | 
 | 5968 | 	addr.v4.sin_port = htons(snum); | 
 | 5969 |  | 
 | 5970 | 	/* Note: sk->sk_num gets filled in if ephemeral port request. */ | 
 | 5971 | 	ret = sctp_get_port_local(sk, &addr); | 
 | 5972 |  | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 5973 | 	return ret ? 1 : 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | } | 
 | 5975 |  | 
 | 5976 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 |  *  Move a socket to LISTENING state. | 
 | 5978 |  */ | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 5979 | SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | { | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 5981 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 5982 | 	struct sctp_endpoint *ep = sp->ep; | 
| Herbert Xu | 1b489e1 | 2006-08-20 15:07:14 +1000 | [diff] [blame] | 5983 | 	struct crypto_hash *tfm = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 |  | 
 | 5985 | 	/* Allocate HMAC for generating cookie. */ | 
| Vlad Yasevich | 23b29ed | 2008-07-18 23:06:07 -0700 | [diff] [blame] | 5986 | 	if (!sctp_sk(sk)->hmac && sctp_hmac_alg) { | 
| Herbert Xu | 1b489e1 | 2006-08-20 15:07:14 +1000 | [diff] [blame] | 5987 | 		tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC); | 
| Vlad Yasevich | 8dc4984 | 2007-05-09 13:50:20 -0700 | [diff] [blame] | 5988 | 		if (IS_ERR(tfm)) { | 
| Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 5989 | 			net_info_ratelimited("failed to load transform for %s: %ld\n", | 
 | 5990 | 					     sctp_hmac_alg, PTR_ERR(tfm)); | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 5991 | 			return -ENOSYS; | 
 | 5992 | 		} | 
 | 5993 | 		sctp_sk(sk)->hmac = tfm; | 
 | 5994 | 	} | 
 | 5995 |  | 
 | 5996 | 	/* | 
 | 5997 | 	 * If a bind() or sctp_bindx() is not called prior to a listen() | 
 | 5998 | 	 * call that allows new associations to be accepted, the system | 
 | 5999 | 	 * picks an ephemeral port and will choose an address set equivalent | 
 | 6000 | 	 * to binding with a wildcard address. | 
 | 6001 | 	 * | 
 | 6002 | 	 * This is not currently spelled out in the SCTP sockets | 
 | 6003 | 	 * extensions draft, but follows the practice as seen in TCP | 
 | 6004 | 	 * sockets. | 
 | 6005 | 	 * | 
 | 6006 | 	 */ | 
 | 6007 | 	sk->sk_state = SCTP_SS_LISTENING; | 
 | 6008 | 	if (!ep->base.bind_addr.port) { | 
 | 6009 | 		if (sctp_autobind(sk)) | 
 | 6010 | 			return -EAGAIN; | 
 | 6011 | 	} else { | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6012 | 		if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 6013 | 			sk->sk_state = SCTP_SS_CLOSED; | 
 | 6014 | 			return -EADDRINUSE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | 		} | 
 | 6016 | 	} | 
 | 6017 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 6018 | 	sk->sk_max_ack_backlog = backlog; | 
 | 6019 | 	sctp_hash_endpoint(ep); | 
 | 6020 | 	return 0; | 
 | 6021 | } | 
 | 6022 |  | 
 | 6023 | /* | 
 | 6024 |  * 4.1.3 / 5.1.3 listen() | 
 | 6025 |  * | 
 | 6026 |  *   By default, new associations are not accepted for UDP style sockets. | 
 | 6027 |  *   An application uses listen() to mark a socket as being able to | 
 | 6028 |  *   accept new associations. | 
 | 6029 |  * | 
 | 6030 |  *   On TCP style sockets, applications use listen() to ready the SCTP | 
 | 6031 |  *   endpoint for accepting inbound associations. | 
 | 6032 |  * | 
 | 6033 |  *   On both types of endpoints a backlog of '0' disables listening. | 
 | 6034 |  * | 
 | 6035 |  *  Move a socket to LISTENING state. | 
 | 6036 |  */ | 
 | 6037 | int sctp_inet_listen(struct socket *sock, int backlog) | 
 | 6038 | { | 
 | 6039 | 	struct sock *sk = sock->sk; | 
 | 6040 | 	struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 
 | 6041 | 	int err = -EINVAL; | 
 | 6042 |  | 
 | 6043 | 	if (unlikely(backlog < 0)) | 
 | 6044 | 		return err; | 
 | 6045 |  | 
 | 6046 | 	sctp_lock_sock(sk); | 
 | 6047 |  | 
 | 6048 | 	/* Peeled-off sockets are not allowed to listen().  */ | 
 | 6049 | 	if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) | 
 | 6050 | 		goto out; | 
 | 6051 |  | 
 | 6052 | 	if (sock->state != SS_UNCONNECTED) | 
 | 6053 | 		goto out; | 
 | 6054 |  | 
 | 6055 | 	/* If backlog is zero, disable listening. */ | 
 | 6056 | 	if (!backlog) { | 
 | 6057 | 		if (sctp_sstate(sk, CLOSED)) | 
 | 6058 | 			goto out; | 
 | 6059 |  | 
 | 6060 | 		err = 0; | 
 | 6061 | 		sctp_unhash_endpoint(ep); | 
 | 6062 | 		sk->sk_state = SCTP_SS_CLOSED; | 
 | 6063 | 		if (sk->sk_reuse) | 
 | 6064 | 			sctp_sk(sk)->bind_hash->fastreuse = 1; | 
 | 6065 | 		goto out; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 6066 | 	} | 
 | 6067 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 6068 | 	/* If we are already listening, just update the backlog */ | 
 | 6069 | 	if (sctp_sstate(sk, LISTENING)) | 
 | 6070 | 		sk->sk_max_ack_backlog = backlog; | 
 | 6071 | 	else { | 
 | 6072 | 		err = sctp_listen_start(sk, backlog); | 
 | 6073 | 		if (err) | 
 | 6074 | 			goto out; | 
 | 6075 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 |  | 
| Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 6077 | 	err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | out: | 
 | 6079 | 	sctp_release_sock(sk); | 
 | 6080 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | } | 
 | 6082 |  | 
 | 6083 | /* | 
 | 6084 |  * This function is done by modeling the current datagram_poll() and the | 
 | 6085 |  * tcp_poll().  Note that, based on these implementations, we don't | 
 | 6086 |  * lock the socket in this function, even though it seems that, | 
 | 6087 |  * ideally, locking or some other mechanisms can be used to ensure | 
| Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 6088 |  * the integrity of the counters (sndbuf and wmem_alloc) used | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 |  * in this place.  We assume that we don't need locks either until proven | 
 | 6090 |  * otherwise. | 
 | 6091 |  * | 
 | 6092 |  * Another thing to note is that we include the Async I/O support | 
 | 6093 |  * here, again, by modeling the current TCP/UDP code.  We don't have | 
 | 6094 |  * a good way to test with it yet. | 
 | 6095 |  */ | 
 | 6096 | unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) | 
 | 6097 | { | 
 | 6098 | 	struct sock *sk = sock->sk; | 
 | 6099 | 	struct sctp_sock *sp = sctp_sk(sk); | 
 | 6100 | 	unsigned int mask; | 
 | 6101 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6102 | 	poll_wait(file, sk_sleep(sk), wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 |  | 
 | 6104 | 	/* A TCP-style listening socket becomes readable when the accept queue | 
 | 6105 | 	 * is not empty. | 
 | 6106 | 	 */ | 
 | 6107 | 	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | 
 | 6108 | 		return (!list_empty(&sp->ep->asocs)) ? | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6109 | 			(POLLIN | POLLRDNORM) : 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 |  | 
 | 6111 | 	mask = 0; | 
 | 6112 |  | 
 | 6113 | 	/* Is there any exceptional events?  */ | 
 | 6114 | 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 
 | 6115 | 		mask |= POLLERR; | 
| Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 6116 | 	if (sk->sk_shutdown & RCV_SHUTDOWN) | 
| Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 6117 | 		mask |= POLLRDHUP | POLLIN | POLLRDNORM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | 	if (sk->sk_shutdown == SHUTDOWN_MASK) | 
 | 6119 | 		mask |= POLLHUP; | 
 | 6120 |  | 
 | 6121 | 	/* Is it readable?  Reconsider this code with TCP-style support.  */ | 
| Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 6122 | 	if (!skb_queue_empty(&sk->sk_receive_queue)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | 		mask |= POLLIN | POLLRDNORM; | 
 | 6124 |  | 
 | 6125 | 	/* The association is either gone or not ready.  */ | 
 | 6126 | 	if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) | 
 | 6127 | 		return mask; | 
 | 6128 |  | 
 | 6129 | 	/* Is it writable?  */ | 
 | 6130 | 	if (sctp_writeable(sk)) { | 
 | 6131 | 		mask |= POLLOUT | POLLWRNORM; | 
 | 6132 | 	} else { | 
 | 6133 | 		set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | 
 | 6134 | 		/* | 
 | 6135 | 		 * Since the socket is not locked, the buffer | 
 | 6136 | 		 * might be made available after the writeable check and | 
 | 6137 | 		 * before the bit is set.  This could cause a lost I/O | 
 | 6138 | 		 * signal.  tcp_poll() has a race breaker for this race | 
 | 6139 | 		 * condition.  Based on their implementation, we put | 
 | 6140 | 		 * in the following code to cover it as well. | 
 | 6141 | 		 */ | 
 | 6142 | 		if (sctp_writeable(sk)) | 
 | 6143 | 			mask |= POLLOUT | POLLWRNORM; | 
 | 6144 | 	} | 
 | 6145 | 	return mask; | 
 | 6146 | } | 
 | 6147 |  | 
 | 6148 | /******************************************************************** | 
 | 6149 |  * 2nd Level Abstractions | 
 | 6150 |  ********************************************************************/ | 
 | 6151 |  | 
 | 6152 | static struct sctp_bind_bucket *sctp_bucket_create( | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 6153 | 	struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | { | 
 | 6155 | 	struct sctp_bind_bucket *pp; | 
 | 6156 |  | 
| Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 6157 | 	pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | 	if (pp) { | 
| Li Zefan | 935a7f6 | 2008-04-10 01:58:06 -0700 | [diff] [blame] | 6159 | 		SCTP_DBG_OBJCNT_INC(bind_bucket); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | 		pp->port = snum; | 
 | 6161 | 		pp->fastreuse = 0; | 
 | 6162 | 		INIT_HLIST_HEAD(&pp->owner); | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 6163 | 		pp->net = net; | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 6164 | 		hlist_add_head(&pp->node, &head->chain); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | 	} | 
 | 6166 | 	return pp; | 
 | 6167 | } | 
 | 6168 |  | 
 | 6169 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ | 
 | 6170 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) | 
 | 6171 | { | 
| Sridhar Samudrala | 37fa687 | 2006-07-21 14:45:47 -0700 | [diff] [blame] | 6172 | 	if (pp && hlist_empty(&pp->owner)) { | 
| Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 6173 | 		__hlist_del(&pp->node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | 		kmem_cache_free(sctp_bucket_cachep, pp); | 
 | 6175 | 		SCTP_DBG_OBJCNT_DEC(bind_bucket); | 
 | 6176 | 	} | 
 | 6177 | } | 
 | 6178 |  | 
 | 6179 | /* Release this socket's reference to a local port.  */ | 
 | 6180 | static inline void __sctp_put_port(struct sock *sk) | 
 | 6181 | { | 
 | 6182 | 	struct sctp_bind_hashbucket *head = | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 6183 | 		&sctp_port_hashtable[sctp_phashfn(sock_net(sk), | 
 | 6184 | 						  inet_sk(sk)->inet_num)]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | 	struct sctp_bind_bucket *pp; | 
 | 6186 |  | 
 | 6187 | 	sctp_spin_lock(&head->lock); | 
 | 6188 | 	pp = sctp_sk(sk)->bind_hash; | 
 | 6189 | 	__sk_del_bind_node(sk); | 
 | 6190 | 	sctp_sk(sk)->bind_hash = NULL; | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6191 | 	inet_sk(sk)->inet_num = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | 	sctp_bucket_destroy(pp); | 
 | 6193 | 	sctp_spin_unlock(&head->lock); | 
 | 6194 | } | 
 | 6195 |  | 
 | 6196 | void sctp_put_port(struct sock *sk) | 
 | 6197 | { | 
 | 6198 | 	sctp_local_bh_disable(); | 
 | 6199 | 	__sctp_put_port(sk); | 
 | 6200 | 	sctp_local_bh_enable(); | 
 | 6201 | } | 
 | 6202 |  | 
 | 6203 | /* | 
 | 6204 |  * The system picks an ephemeral port and choose an address set equivalent | 
 | 6205 |  * to binding with a wildcard address. | 
 | 6206 |  * One of those addresses will be the primary address for the association. | 
 | 6207 |  * This automatically enables the multihoming capability of SCTP. | 
 | 6208 |  */ | 
 | 6209 | static int sctp_autobind(struct sock *sk) | 
 | 6210 | { | 
 | 6211 | 	union sctp_addr autoaddr; | 
 | 6212 | 	struct sctp_af *af; | 
| Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 6213 | 	__be16 port; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 |  | 
 | 6215 | 	/* Initialize a local sockaddr structure to INADDR_ANY. */ | 
 | 6216 | 	af = sctp_sk(sk)->pf->af; | 
 | 6217 |  | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6218 | 	port = htons(inet_sk(sk)->inet_num); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | 	af->inaddr_any(&autoaddr, port); | 
 | 6220 |  | 
 | 6221 | 	return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); | 
 | 6222 | } | 
 | 6223 |  | 
 | 6224 | /* Parse out IPPROTO_SCTP CMSG headers.  Perform only minimal validation. | 
 | 6225 |  * | 
 | 6226 |  * From RFC 2292 | 
 | 6227 |  * 4.2 The cmsghdr Structure * | 
 | 6228 |  * | 
 | 6229 |  * When ancillary data is sent or received, any number of ancillary data | 
 | 6230 |  * objects can be specified by the msg_control and msg_controllen members of | 
 | 6231 |  * the msghdr structure, because each object is preceded by | 
 | 6232 |  * a cmsghdr structure defining the object's length (the cmsg_len member). | 
 | 6233 |  * Historically Berkeley-derived implementations have passed only one object | 
 | 6234 |  * at a time, but this API allows multiple objects to be | 
 | 6235 |  * passed in a single call to sendmsg() or recvmsg(). The following example | 
 | 6236 |  * shows two ancillary data objects in a control buffer. | 
 | 6237 |  * | 
 | 6238 |  *   |<--------------------------- msg_controllen -------------------------->| | 
 | 6239 |  *   |                                                                       | | 
 | 6240 |  * | 
 | 6241 |  *   |<----- ancillary data object ----->|<----- ancillary data object ----->| | 
 | 6242 |  * | 
 | 6243 |  *   |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| | 
 | 6244 |  *   |                                   |                                   | | 
 | 6245 |  * | 
 | 6246 |  *   |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  | | 
 | 6247 |  * | 
 | 6248 |  *   |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  | | 
 | 6249 |  *   |                                |  |                                |  | | 
 | 6250 |  * | 
 | 6251 |  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 
 | 6252 |  *   |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX| | 
 | 6253 |  * | 
 | 6254 |  *   |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX| | 
 | 6255 |  * | 
 | 6256 |  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 
 | 6257 |  *    ^ | 
 | 6258 |  *    | | 
 | 6259 |  * | 
 | 6260 |  * msg_control | 
 | 6261 |  * points here | 
 | 6262 |  */ | 
 | 6263 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg, | 
 | 6264 | 				  sctp_cmsgs_t *cmsgs) | 
 | 6265 | { | 
 | 6266 | 	struct cmsghdr *cmsg; | 
| Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 6267 | 	struct msghdr *my_msg = (struct msghdr *)msg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 |  | 
 | 6269 | 	for (cmsg = CMSG_FIRSTHDR(msg); | 
 | 6270 | 	     cmsg != NULL; | 
| Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 6271 | 	     cmsg = CMSG_NXTHDR(my_msg, cmsg)) { | 
 | 6272 | 		if (!CMSG_OK(my_msg, cmsg)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | 			return -EINVAL; | 
 | 6274 |  | 
 | 6275 | 		/* Should we parse this header or ignore?  */ | 
 | 6276 | 		if (cmsg->cmsg_level != IPPROTO_SCTP) | 
 | 6277 | 			continue; | 
 | 6278 |  | 
 | 6279 | 		/* Strictly check lengths following example in SCM code.  */ | 
 | 6280 | 		switch (cmsg->cmsg_type) { | 
 | 6281 | 		case SCTP_INIT: | 
 | 6282 | 			/* SCTP Socket API Extension | 
 | 6283 | 			 * 5.2.1 SCTP Initiation Structure (SCTP_INIT) | 
 | 6284 | 			 * | 
 | 6285 | 			 * This cmsghdr structure provides information for | 
 | 6286 | 			 * initializing new SCTP associations with sendmsg(). | 
 | 6287 | 			 * The SCTP_INITMSG socket option uses this same data | 
 | 6288 | 			 * structure.  This structure is not used for | 
 | 6289 | 			 * recvmsg(). | 
 | 6290 | 			 * | 
 | 6291 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 6292 | 			 * ------------  ------------   ---------------------- | 
 | 6293 | 			 * IPPROTO_SCTP  SCTP_INIT      struct sctp_initmsg | 
 | 6294 | 			 */ | 
 | 6295 | 			if (cmsg->cmsg_len != | 
 | 6296 | 			    CMSG_LEN(sizeof(struct sctp_initmsg))) | 
 | 6297 | 				return -EINVAL; | 
 | 6298 | 			cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg); | 
 | 6299 | 			break; | 
 | 6300 |  | 
 | 6301 | 		case SCTP_SNDRCV: | 
 | 6302 | 			/* SCTP Socket API Extension | 
 | 6303 | 			 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV) | 
 | 6304 | 			 * | 
 | 6305 | 			 * This cmsghdr structure specifies SCTP options for | 
 | 6306 | 			 * sendmsg() and describes SCTP header information | 
 | 6307 | 			 * about a received message through recvmsg(). | 
 | 6308 | 			 * | 
 | 6309 | 			 * cmsg_level    cmsg_type      cmsg_data[] | 
 | 6310 | 			 * ------------  ------------   ---------------------- | 
 | 6311 | 			 * IPPROTO_SCTP  SCTP_SNDRCV    struct sctp_sndrcvinfo | 
 | 6312 | 			 */ | 
 | 6313 | 			if (cmsg->cmsg_len != | 
 | 6314 | 			    CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) | 
 | 6315 | 				return -EINVAL; | 
 | 6316 |  | 
 | 6317 | 			cmsgs->info = | 
 | 6318 | 				(struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); | 
 | 6319 |  | 
 | 6320 | 			/* Minimally, validate the sinfo_flags. */ | 
 | 6321 | 			if (cmsgs->info->sinfo_flags & | 
| Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 6322 | 			    ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 
 | 6323 | 			      SCTP_ABORT | SCTP_EOF)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | 				return -EINVAL; | 
 | 6325 | 			break; | 
 | 6326 |  | 
 | 6327 | 		default: | 
 | 6328 | 			return -EINVAL; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 6329 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | 	} | 
 | 6331 | 	return 0; | 
 | 6332 | } | 
 | 6333 |  | 
 | 6334 | /* | 
 | 6335 |  * Wait for a packet.. | 
 | 6336 |  * Note: This function is the same function as in core/datagram.c | 
 | 6337 |  * with a few modifications to make lksctp work. | 
 | 6338 |  */ | 
 | 6339 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p) | 
 | 6340 | { | 
 | 6341 | 	int error; | 
 | 6342 | 	DEFINE_WAIT(wait); | 
 | 6343 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6344 | 	prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 |  | 
 | 6346 | 	/* Socket errors? */ | 
 | 6347 | 	error = sock_error(sk); | 
 | 6348 | 	if (error) | 
 | 6349 | 		goto out; | 
 | 6350 |  | 
 | 6351 | 	if (!skb_queue_empty(&sk->sk_receive_queue)) | 
 | 6352 | 		goto ready; | 
 | 6353 |  | 
 | 6354 | 	/* Socket shut down?  */ | 
 | 6355 | 	if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 6356 | 		goto out; | 
 | 6357 |  | 
 | 6358 | 	/* Sequenced packets can come disconnected.  If so we report the | 
 | 6359 | 	 * problem. | 
 | 6360 | 	 */ | 
 | 6361 | 	error = -ENOTCONN; | 
 | 6362 |  | 
 | 6363 | 	/* Is there a good reason to think that we may receive some data?  */ | 
 | 6364 | 	if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) | 
 | 6365 | 		goto out; | 
 | 6366 |  | 
 | 6367 | 	/* Handle signals.  */ | 
 | 6368 | 	if (signal_pending(current)) | 
 | 6369 | 		goto interrupted; | 
 | 6370 |  | 
 | 6371 | 	/* Let another process have a go.  Since we are going to sleep | 
 | 6372 | 	 * anyway.  Note: This may cause odd behaviors if the message | 
 | 6373 | 	 * does not fit in the user's buffer, but this seems to be the | 
 | 6374 | 	 * only way to honor MSG_DONTWAIT realistically. | 
 | 6375 | 	 */ | 
 | 6376 | 	sctp_release_sock(sk); | 
 | 6377 | 	*timeo_p = schedule_timeout(*timeo_p); | 
 | 6378 | 	sctp_lock_sock(sk); | 
 | 6379 |  | 
 | 6380 | ready: | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6381 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | 	return 0; | 
 | 6383 |  | 
 | 6384 | interrupted: | 
 | 6385 | 	error = sock_intr_errno(*timeo_p); | 
 | 6386 |  | 
 | 6387 | out: | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6388 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | 	*err = error; | 
 | 6390 | 	return error; | 
 | 6391 | } | 
 | 6392 |  | 
 | 6393 | /* Receive a datagram. | 
 | 6394 |  * Note: This is pretty much the same routine as in core/datagram.c | 
 | 6395 |  * with a few changes to make lksctp work. | 
 | 6396 |  */ | 
 | 6397 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, | 
 | 6398 | 					      int noblock, int *err) | 
 | 6399 | { | 
 | 6400 | 	int error; | 
 | 6401 | 	struct sk_buff *skb; | 
 | 6402 | 	long timeo; | 
 | 6403 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | 	timeo = sock_rcvtimeo(sk, noblock); | 
 | 6405 |  | 
 | 6406 | 	SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n", | 
 | 6407 | 			  timeo, MAX_SCHEDULE_TIMEOUT); | 
 | 6408 |  | 
 | 6409 | 	do { | 
 | 6410 | 		/* Again only user level code calls this function, | 
 | 6411 | 		 * so nothing interrupt level | 
 | 6412 | 		 * will suddenly eat the receive_queue. | 
 | 6413 | 		 * | 
 | 6414 | 		 *  Look at current nfs client by the way... | 
| David Shwatrz | 8917a3c | 2010-12-02 09:01:55 +0000 | [diff] [blame] | 6415 | 		 *  However, this function was correct in any case. 8) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | 		 */ | 
 | 6417 | 		if (flags & MSG_PEEK) { | 
| Herbert Xu | 1e061ab | 2005-06-18 22:56:42 -0700 | [diff] [blame] | 6418 | 			spin_lock_bh(&sk->sk_receive_queue.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | 			skb = skb_peek(&sk->sk_receive_queue); | 
 | 6420 | 			if (skb) | 
 | 6421 | 				atomic_inc(&skb->users); | 
| Herbert Xu | 1e061ab | 2005-06-18 22:56:42 -0700 | [diff] [blame] | 6422 | 			spin_unlock_bh(&sk->sk_receive_queue.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | 		} else { | 
 | 6424 | 			skb = skb_dequeue(&sk->sk_receive_queue); | 
 | 6425 | 		} | 
 | 6426 |  | 
 | 6427 | 		if (skb) | 
 | 6428 | 			return skb; | 
 | 6429 |  | 
| Neil Horman | 6736dc3 | 2005-12-02 20:30:06 -0800 | [diff] [blame] | 6430 | 		/* Caller is allowed not to check sk->sk_err before calling. */ | 
 | 6431 | 		error = sock_error(sk); | 
 | 6432 | 		if (error) | 
 | 6433 | 			goto no_packet; | 
 | 6434 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | 		if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 6436 | 			break; | 
 | 6437 |  | 
 | 6438 | 		/* User doesn't want to wait.  */ | 
 | 6439 | 		error = -EAGAIN; | 
 | 6440 | 		if (!timeo) | 
 | 6441 | 			goto no_packet; | 
 | 6442 | 	} while (sctp_wait_for_packet(sk, err, &timeo) == 0); | 
 | 6443 |  | 
 | 6444 | 	return NULL; | 
 | 6445 |  | 
 | 6446 | no_packet: | 
 | 6447 | 	*err = error; | 
 | 6448 | 	return NULL; | 
 | 6449 | } | 
 | 6450 |  | 
 | 6451 | /* If sndbuf has changed, wake up per association sndbuf waiters.  */ | 
 | 6452 | static void __sctp_write_space(struct sctp_association *asoc) | 
 | 6453 | { | 
 | 6454 | 	struct sock *sk = asoc->base.sk; | 
 | 6455 | 	struct socket *sock = sk->sk_socket; | 
 | 6456 |  | 
 | 6457 | 	if ((sctp_wspace(asoc) > 0) && sock) { | 
 | 6458 | 		if (waitqueue_active(&asoc->wait)) | 
 | 6459 | 			wake_up_interruptible(&asoc->wait); | 
 | 6460 |  | 
 | 6461 | 		if (sctp_writeable(sk)) { | 
| Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 6462 | 			wait_queue_head_t *wq = sk_sleep(sk); | 
 | 6463 |  | 
 | 6464 | 			if (wq && waitqueue_active(wq)) | 
 | 6465 | 				wake_up_interruptible(wq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 |  | 
 | 6467 | 			/* Note that we try to include the Async I/O support | 
 | 6468 | 			 * here by modeling from the current TCP/UDP code. | 
 | 6469 | 			 * We have not tested with it yet. | 
 | 6470 | 			 */ | 
| Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 6471 | 			if (!(sk->sk_shutdown & SEND_SHUTDOWN)) | 
| Pavel Emelyanov | 8d8ad9d | 2007-11-26 20:10:50 +0800 | [diff] [blame] | 6472 | 				sock_wake_async(sock, | 
 | 6473 | 						SOCK_WAKE_SPACE, POLL_OUT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | 		} | 
 | 6475 | 	} | 
 | 6476 | } | 
 | 6477 |  | 
 | 6478 | /* Do accounting for the sndbuf space. | 
 | 6479 |  * Decrement the used sndbuf space of the corresponding association by the | 
 | 6480 |  * data size which was just transmitted(freed). | 
 | 6481 |  */ | 
 | 6482 | static void sctp_wfree(struct sk_buff *skb) | 
 | 6483 | { | 
 | 6484 | 	struct sctp_association *asoc; | 
 | 6485 | 	struct sctp_chunk *chunk; | 
 | 6486 | 	struct sock *sk; | 
 | 6487 |  | 
 | 6488 | 	/* Get the saved chunk pointer.  */ | 
 | 6489 | 	chunk = *((struct sctp_chunk **)(skb->cb)); | 
 | 6490 | 	asoc = chunk->asoc; | 
 | 6491 | 	sk = asoc->base.sk; | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 6492 | 	asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + | 
 | 6493 | 				sizeof(struct sk_buff) + | 
 | 6494 | 				sizeof(struct sctp_chunk); | 
 | 6495 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 6496 | 	atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); | 
 | 6497 |  | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6498 | 	/* | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 6499 | 	 * This undoes what is done via sctp_set_owner_w and sk_mem_charge | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6500 | 	 */ | 
 | 6501 | 	sk->sk_wmem_queued   -= skb->truesize; | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 6502 | 	sk_mem_uncharge(sk, skb->truesize); | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6503 |  | 
| Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 6504 | 	sock_wfree(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | 	__sctp_write_space(asoc); | 
 | 6506 |  | 
 | 6507 | 	sctp_association_put(asoc); | 
 | 6508 | } | 
 | 6509 |  | 
| Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 6510 | /* Do accounting for the receive space on the socket. | 
 | 6511 |  * Accounting for the association is done in ulpevent.c | 
 | 6512 |  * We set this as a destructor for the cloned data skbs so that | 
 | 6513 |  * accounting is done at the correct time. | 
 | 6514 |  */ | 
 | 6515 | void sctp_sock_rfree(struct sk_buff *skb) | 
 | 6516 | { | 
 | 6517 | 	struct sock *sk = skb->sk; | 
 | 6518 | 	struct sctp_ulpevent *event = sctp_skb2event(skb); | 
 | 6519 |  | 
 | 6520 | 	atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6521 |  | 
 | 6522 | 	/* | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 6523 | 	 * Mimic the behavior of sock_rfree | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6524 | 	 */ | 
| Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 6525 | 	sk_mem_uncharge(sk, event->rmem_len); | 
| Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 6526 | } | 
 | 6527 |  | 
 | 6528 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | /* Helper function to wait for space in the sndbuf.  */ | 
 | 6530 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, | 
 | 6531 | 				size_t msg_len) | 
 | 6532 | { | 
 | 6533 | 	struct sock *sk = asoc->base.sk; | 
 | 6534 | 	int err = 0; | 
 | 6535 | 	long current_timeo = *timeo_p; | 
 | 6536 | 	DEFINE_WAIT(wait); | 
 | 6537 |  | 
 | 6538 | 	SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n", | 
| YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6539 | 			  asoc, (long)(*timeo_p), msg_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 |  | 
 | 6541 | 	/* Increment the association's refcnt.  */ | 
 | 6542 | 	sctp_association_hold(asoc); | 
 | 6543 |  | 
 | 6544 | 	/* Wait on the association specific sndbuf space. */ | 
 | 6545 | 	for (;;) { | 
 | 6546 | 		prepare_to_wait_exclusive(&asoc->wait, &wait, | 
 | 6547 | 					  TASK_INTERRUPTIBLE); | 
 | 6548 | 		if (!*timeo_p) | 
 | 6549 | 			goto do_nonblock; | 
 | 6550 | 		if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || | 
 | 6551 | 		    asoc->base.dead) | 
 | 6552 | 			goto do_error; | 
 | 6553 | 		if (signal_pending(current)) | 
 | 6554 | 			goto do_interrupted; | 
 | 6555 | 		if (msg_len <= sctp_wspace(asoc)) | 
 | 6556 | 			break; | 
 | 6557 |  | 
 | 6558 | 		/* Let another process have a go.  Since we are going | 
 | 6559 | 		 * to sleep anyway. | 
 | 6560 | 		 */ | 
 | 6561 | 		sctp_release_sock(sk); | 
 | 6562 | 		current_timeo = schedule_timeout(current_timeo); | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6563 | 		BUG_ON(sk != asoc->base.sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | 		sctp_lock_sock(sk); | 
 | 6565 |  | 
 | 6566 | 		*timeo_p = current_timeo; | 
 | 6567 | 	} | 
 | 6568 |  | 
 | 6569 | out: | 
 | 6570 | 	finish_wait(&asoc->wait, &wait); | 
 | 6571 |  | 
 | 6572 | 	/* Release the association's refcnt.  */ | 
 | 6573 | 	sctp_association_put(asoc); | 
 | 6574 |  | 
 | 6575 | 	return err; | 
 | 6576 |  | 
 | 6577 | do_error: | 
 | 6578 | 	err = -EPIPE; | 
 | 6579 | 	goto out; | 
 | 6580 |  | 
 | 6581 | do_interrupted: | 
 | 6582 | 	err = sock_intr_errno(*timeo_p); | 
 | 6583 | 	goto out; | 
 | 6584 |  | 
 | 6585 | do_nonblock: | 
 | 6586 | 	err = -EAGAIN; | 
 | 6587 | 	goto out; | 
 | 6588 | } | 
 | 6589 |  | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 6590 | void sctp_data_ready(struct sock *sk, int len) | 
 | 6591 | { | 
| David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 6592 | 	struct socket_wq *wq; | 
 | 6593 |  | 
 | 6594 | 	rcu_read_lock(); | 
 | 6595 | 	wq = rcu_dereference(sk->sk_wq); | 
 | 6596 | 	if (wq_has_sleeper(wq)) | 
 | 6597 | 		wake_up_interruptible_sync_poll(&wq->wait, POLLIN | | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 6598 | 						POLLRDNORM | POLLRDBAND); | 
 | 6599 | 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); | 
| David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 6600 | 	rcu_read_unlock(); | 
| Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 6601 | } | 
 | 6602 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | /* If socket sndbuf has changed, wake up all per association waiters.  */ | 
 | 6604 | void sctp_write_space(struct sock *sk) | 
 | 6605 | { | 
 | 6606 | 	struct sctp_association *asoc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 |  | 
 | 6608 | 	/* Wake up the tasks in each wait queue.  */ | 
| Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 6609 | 	list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | 		__sctp_write_space(asoc); | 
 | 6611 | 	} | 
 | 6612 | } | 
 | 6613 |  | 
 | 6614 | /* Is there any sndbuf space available on the socket? | 
 | 6615 |  * | 
| Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 6616 |  * 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] | 6617 |  * associations on the same socket.  For a UDP-style socket with | 
 | 6618 |  * multiple associations, it is possible for it to be "unwriteable" | 
 | 6619 |  * prematurely.  I assume that this is acceptable because | 
 | 6620 |  * a premature "unwriteable" is better than an accidental "writeable" which | 
 | 6621 |  * would cause an unwanted block under certain circumstances.  For the 1-1 | 
 | 6622 |  * UDP-style sockets or TCP-style sockets, this code should work. | 
 | 6623 |  *  - Daisy | 
 | 6624 |  */ | 
 | 6625 | static int sctp_writeable(struct sock *sk) | 
 | 6626 | { | 
 | 6627 | 	int amt = 0; | 
 | 6628 |  | 
| Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 6629 | 	amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | 	if (amt < 0) | 
 | 6631 | 		amt = 0; | 
 | 6632 | 	return amt; | 
 | 6633 | } | 
 | 6634 |  | 
 | 6635 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, | 
 | 6636 |  * returns immediately with EINPROGRESS. | 
 | 6637 |  */ | 
 | 6638 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) | 
 | 6639 | { | 
 | 6640 | 	struct sock *sk = asoc->base.sk; | 
 | 6641 | 	int err = 0; | 
 | 6642 | 	long current_timeo = *timeo_p; | 
 | 6643 | 	DEFINE_WAIT(wait); | 
 | 6644 |  | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 6645 | 	SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | 			  (long)(*timeo_p)); | 
 | 6647 |  | 
 | 6648 | 	/* Increment the association's refcnt.  */ | 
 | 6649 | 	sctp_association_hold(asoc); | 
 | 6650 |  | 
 | 6651 | 	for (;;) { | 
 | 6652 | 		prepare_to_wait_exclusive(&asoc->wait, &wait, | 
 | 6653 | 					  TASK_INTERRUPTIBLE); | 
 | 6654 | 		if (!*timeo_p) | 
 | 6655 | 			goto do_nonblock; | 
 | 6656 | 		if (sk->sk_shutdown & RCV_SHUTDOWN) | 
 | 6657 | 			break; | 
 | 6658 | 		if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || | 
 | 6659 | 		    asoc->base.dead) | 
 | 6660 | 			goto do_error; | 
 | 6661 | 		if (signal_pending(current)) | 
 | 6662 | 			goto do_interrupted; | 
 | 6663 |  | 
 | 6664 | 		if (sctp_state(asoc, ESTABLISHED)) | 
 | 6665 | 			break; | 
 | 6666 |  | 
 | 6667 | 		/* Let another process have a go.  Since we are going | 
 | 6668 | 		 * to sleep anyway. | 
 | 6669 | 		 */ | 
 | 6670 | 		sctp_release_sock(sk); | 
 | 6671 | 		current_timeo = schedule_timeout(current_timeo); | 
 | 6672 | 		sctp_lock_sock(sk); | 
 | 6673 |  | 
 | 6674 | 		*timeo_p = current_timeo; | 
 | 6675 | 	} | 
 | 6676 |  | 
 | 6677 | out: | 
 | 6678 | 	finish_wait(&asoc->wait, &wait); | 
 | 6679 |  | 
 | 6680 | 	/* Release the association's refcnt.  */ | 
 | 6681 | 	sctp_association_put(asoc); | 
 | 6682 |  | 
 | 6683 | 	return err; | 
 | 6684 |  | 
 | 6685 | do_error: | 
| Vlad Yasevich | 81845c2 | 2006-01-30 15:59:54 -0800 | [diff] [blame] | 6686 | 	if (asoc->init_err_counter + 1 > asoc->max_init_attempts) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6687 | 		err = -ETIMEDOUT; | 
 | 6688 | 	else | 
 | 6689 | 		err = -ECONNREFUSED; | 
 | 6690 | 	goto out; | 
 | 6691 |  | 
 | 6692 | do_interrupted: | 
 | 6693 | 	err = sock_intr_errno(*timeo_p); | 
 | 6694 | 	goto out; | 
 | 6695 |  | 
 | 6696 | do_nonblock: | 
 | 6697 | 	err = -EINPROGRESS; | 
 | 6698 | 	goto out; | 
 | 6699 | } | 
 | 6700 |  | 
 | 6701 | static int sctp_wait_for_accept(struct sock *sk, long timeo) | 
 | 6702 | { | 
 | 6703 | 	struct sctp_endpoint *ep; | 
 | 6704 | 	int err = 0; | 
 | 6705 | 	DEFINE_WAIT(wait); | 
 | 6706 |  | 
 | 6707 | 	ep = sctp_sk(sk)->ep; | 
 | 6708 |  | 
 | 6709 |  | 
 | 6710 | 	for (;;) { | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6711 | 		prepare_to_wait_exclusive(sk_sleep(sk), &wait, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | 					  TASK_INTERRUPTIBLE); | 
 | 6713 |  | 
 | 6714 | 		if (list_empty(&ep->asocs)) { | 
 | 6715 | 			sctp_release_sock(sk); | 
 | 6716 | 			timeo = schedule_timeout(timeo); | 
 | 6717 | 			sctp_lock_sock(sk); | 
 | 6718 | 		} | 
 | 6719 |  | 
 | 6720 | 		err = -EINVAL; | 
 | 6721 | 		if (!sctp_sstate(sk, LISTENING)) | 
 | 6722 | 			break; | 
 | 6723 |  | 
 | 6724 | 		err = 0; | 
 | 6725 | 		if (!list_empty(&ep->asocs)) | 
 | 6726 | 			break; | 
 | 6727 |  | 
 | 6728 | 		err = sock_intr_errno(timeo); | 
 | 6729 | 		if (signal_pending(current)) | 
 | 6730 | 			break; | 
 | 6731 |  | 
 | 6732 | 		err = -EAGAIN; | 
 | 6733 | 		if (!timeo) | 
 | 6734 | 			break; | 
 | 6735 | 	} | 
 | 6736 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6737 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 |  | 
 | 6739 | 	return err; | 
 | 6740 | } | 
 | 6741 |  | 
| sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 6742 | static void sctp_wait_for_close(struct sock *sk, long timeout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | { | 
 | 6744 | 	DEFINE_WAIT(wait); | 
 | 6745 |  | 
 | 6746 | 	do { | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6747 | 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | 		if (list_empty(&sctp_sk(sk)->ep->asocs)) | 
 | 6749 | 			break; | 
 | 6750 | 		sctp_release_sock(sk); | 
 | 6751 | 		timeout = schedule_timeout(timeout); | 
 | 6752 | 		sctp_lock_sock(sk); | 
 | 6753 | 	} while (!signal_pending(current) && timeout); | 
 | 6754 |  | 
| Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 6755 | 	finish_wait(sk_sleep(sk), &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | } | 
 | 6757 |  | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6758 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) | 
 | 6759 | { | 
 | 6760 | 	struct sk_buff *frag; | 
 | 6761 |  | 
 | 6762 | 	if (!skb->data_len) | 
 | 6763 | 		goto done; | 
 | 6764 |  | 
 | 6765 | 	/* Don't forget the fragments. */ | 
| David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 6766 | 	skb_walk_frags(skb, frag) | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6767 | 		sctp_skb_set_owner_r_frag(frag, sk); | 
 | 6768 |  | 
 | 6769 | done: | 
 | 6770 | 	sctp_skb_set_owner_r(skb, sk); | 
 | 6771 | } | 
 | 6772 |  | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 6773 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, | 
 | 6774 | 		    struct sctp_association *asoc) | 
 | 6775 | { | 
 | 6776 | 	struct inet_sock *inet = inet_sk(sk); | 
| Julia Lawall | 09cb47a | 2010-01-21 02:43:20 -0800 | [diff] [blame] | 6777 | 	struct inet_sock *newinet; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 6778 |  | 
 | 6779 | 	newsk->sk_type = sk->sk_type; | 
 | 6780 | 	newsk->sk_bound_dev_if = sk->sk_bound_dev_if; | 
 | 6781 | 	newsk->sk_flags = sk->sk_flags; | 
 | 6782 | 	newsk->sk_no_check = sk->sk_no_check; | 
 | 6783 | 	newsk->sk_reuse = sk->sk_reuse; | 
 | 6784 |  | 
 | 6785 | 	newsk->sk_shutdown = sk->sk_shutdown; | 
 | 6786 | 	newsk->sk_destruct = inet_sock_destruct; | 
 | 6787 | 	newsk->sk_family = sk->sk_family; | 
 | 6788 | 	newsk->sk_protocol = IPPROTO_SCTP; | 
 | 6789 | 	newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; | 
 | 6790 | 	newsk->sk_sndbuf = sk->sk_sndbuf; | 
 | 6791 | 	newsk->sk_rcvbuf = sk->sk_rcvbuf; | 
 | 6792 | 	newsk->sk_lingertime = sk->sk_lingertime; | 
 | 6793 | 	newsk->sk_rcvtimeo = sk->sk_rcvtimeo; | 
 | 6794 | 	newsk->sk_sndtimeo = sk->sk_sndtimeo; | 
 | 6795 |  | 
 | 6796 | 	newinet = inet_sk(newsk); | 
 | 6797 |  | 
 | 6798 | 	/* Initialize sk's sport, dport, rcv_saddr and daddr for | 
 | 6799 | 	 * getsockname() and getpeername() | 
 | 6800 | 	 */ | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6801 | 	newinet->inet_sport = inet->inet_sport; | 
 | 6802 | 	newinet->inet_saddr = inet->inet_saddr; | 
 | 6803 | 	newinet->inet_rcv_saddr = inet->inet_rcv_saddr; | 
 | 6804 | 	newinet->inet_dport = htons(asoc->peer.port); | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 6805 | 	newinet->pmtudisc = inet->pmtudisc; | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6806 | 	newinet->inet_id = asoc->next_tsn ^ jiffies; | 
| Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 6807 |  | 
 | 6808 | 	newinet->uc_ttl = inet->uc_ttl; | 
 | 6809 | 	newinet->mc_loop = 1; | 
 | 6810 | 	newinet->mc_ttl = 1; | 
 | 6811 | 	newinet->mc_index = 0; | 
 | 6812 | 	newinet->mc_list = NULL; | 
 | 6813 | } | 
 | 6814 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | /* Populate the fields of the newsk from the oldsk and migrate the assoc | 
 | 6816 |  * and its messages to the newsk. | 
 | 6817 |  */ | 
 | 6818 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, | 
 | 6819 | 			      struct sctp_association *assoc, | 
 | 6820 | 			      sctp_socket_type_t type) | 
 | 6821 | { | 
 | 6822 | 	struct sctp_sock *oldsp = sctp_sk(oldsk); | 
 | 6823 | 	struct sctp_sock *newsp = sctp_sk(newsk); | 
 | 6824 | 	struct sctp_bind_bucket *pp; /* hash list port iterator */ | 
 | 6825 | 	struct sctp_endpoint *newep = newsp->ep; | 
 | 6826 | 	struct sk_buff *skb, *tmp; | 
 | 6827 | 	struct sctp_ulpevent *event; | 
| Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 6828 | 	struct sctp_bind_hashbucket *head; | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 6829 | 	struct list_head tmplist; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 |  | 
 | 6831 | 	/* Migrate socket buffer sizes and all the socket level options to the | 
 | 6832 | 	 * new socket. | 
 | 6833 | 	 */ | 
 | 6834 | 	newsk->sk_sndbuf = oldsk->sk_sndbuf; | 
 | 6835 | 	newsk->sk_rcvbuf = oldsk->sk_rcvbuf; | 
 | 6836 | 	/* Brute force copy old sctp opt. */ | 
| Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 6837 | 	if (oldsp->do_auto_asconf) { | 
 | 6838 | 		memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist)); | 
 | 6839 | 		inet_sk_copy_descendant(newsk, oldsk); | 
 | 6840 | 		memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist)); | 
 | 6841 | 	} else | 
 | 6842 | 		inet_sk_copy_descendant(newsk, oldsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 |  | 
 | 6844 | 	/* Restore the ep value that was overwritten with the above structure | 
 | 6845 | 	 * copy. | 
 | 6846 | 	 */ | 
 | 6847 | 	newsp->ep = newep; | 
 | 6848 | 	newsp->hmac = NULL; | 
 | 6849 |  | 
 | 6850 | 	/* Hook this new socket in to the bind_hash list. */ | 
| Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 6851 | 	head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), | 
 | 6852 | 						 inet_sk(oldsk)->inet_num)]; | 
| Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 6853 | 	sctp_local_bh_disable(); | 
 | 6854 | 	sctp_spin_lock(&head->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | 	pp = sctp_sk(oldsk)->bind_hash; | 
 | 6856 | 	sk_add_bind_node(newsk, &pp->owner); | 
 | 6857 | 	sctp_sk(newsk)->bind_hash = pp; | 
| Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 6858 | 	inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; | 
| Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 6859 | 	sctp_spin_unlock(&head->lock); | 
 | 6860 | 	sctp_local_bh_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 |  | 
| Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 6862 | 	/* Copy the bind_addr list from the original endpoint to the new | 
 | 6863 | 	 * endpoint so that we can handle restarts properly | 
 | 6864 | 	 */ | 
| Vlad Yasevich | 8e71a11 | 2007-12-06 22:50:54 -0800 | [diff] [blame] | 6865 | 	sctp_bind_addr_dup(&newsp->ep->base.bind_addr, | 
 | 6866 | 				&oldsp->ep->base.bind_addr, GFP_KERNEL); | 
| Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 6867 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | 	/* Move any messages in the old socket's receive queue that are for the | 
 | 6869 | 	 * peeled off association to the new socket's receive queue. | 
 | 6870 | 	 */ | 
 | 6871 | 	sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { | 
 | 6872 | 		event = sctp_skb2event(skb); | 
 | 6873 | 		if (event->asoc == assoc) { | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 6874 | 			__skb_unlink(skb, &oldsk->sk_receive_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | 			__skb_queue_tail(&newsk->sk_receive_queue, skb); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6876 | 			sctp_skb_set_owner_r_frag(skb, newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | 		} | 
 | 6878 | 	} | 
 | 6879 |  | 
 | 6880 | 	/* Clean up any messages pending delivery due to partial | 
 | 6881 | 	 * delivery.   Three cases: | 
 | 6882 | 	 * 1) No partial deliver;  no work. | 
 | 6883 | 	 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. | 
 | 6884 | 	 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. | 
 | 6885 | 	 */ | 
 | 6886 | 	skb_queue_head_init(&newsp->pd_lobby); | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6887 | 	atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 |  | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6889 | 	if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | 		struct sk_buff_head *queue; | 
 | 6891 |  | 
 | 6892 | 		/* Decide which queue to move pd_lobby skbs to. */ | 
 | 6893 | 		if (assoc->ulpq.pd_mode) { | 
 | 6894 | 			queue = &newsp->pd_lobby; | 
 | 6895 | 		} else | 
 | 6896 | 			queue = &newsk->sk_receive_queue; | 
 | 6897 |  | 
 | 6898 | 		/* Walk through the pd_lobby, looking for skbs that | 
 | 6899 | 		 * need moved to the new socket. | 
 | 6900 | 		 */ | 
 | 6901 | 		sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { | 
 | 6902 | 			event = sctp_skb2event(skb); | 
 | 6903 | 			if (event->asoc == assoc) { | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 6904 | 				__skb_unlink(skb, &oldsp->pd_lobby); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | 				__skb_queue_tail(queue, skb); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6906 | 				sctp_skb_set_owner_r_frag(skb, newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | 			} | 
 | 6908 | 		} | 
 | 6909 |  | 
 | 6910 | 		/* Clear up any skbs waiting for the partial | 
 | 6911 | 		 * delivery to finish. | 
 | 6912 | 		 */ | 
 | 6913 | 		if (assoc->ulpq.pd_mode) | 
| Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6914 | 			sctp_clear_pd(oldsk, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 |  | 
 | 6916 | 	} | 
 | 6917 |  | 
| Wei Yongjun | 1bc4ee4 | 2009-07-05 19:45:48 +0000 | [diff] [blame] | 6918 | 	sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6919 | 		sctp_skb_set_owner_r_frag(skb, newsk); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6920 |  | 
| Wei Yongjun | 1bc4ee4 | 2009-07-05 19:45:48 +0000 | [diff] [blame] | 6921 | 	sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6922 | 		sctp_skb_set_owner_r_frag(skb, newsk); | 
| Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6923 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | 	/* Set the type of socket to indicate that it is peeled off from the | 
 | 6925 | 	 * original UDP-style socket or created with the accept() call on a | 
 | 6926 | 	 * TCP-style socket.. | 
 | 6927 | 	 */ | 
 | 6928 | 	newsp->type = type; | 
 | 6929 |  | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6930 | 	/* Mark the new socket "in-use" by the user so that any packets | 
 | 6931 | 	 * that may arrive on the association after we've moved it are | 
 | 6932 | 	 * queued to the backlog.  This prevents a potential race between | 
 | 6933 | 	 * backlog processing on the old socket and new-packet processing | 
 | 6934 | 	 * on the new socket. | 
| Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 6935 | 	 * | 
 | 6936 | 	 * The caller has just allocated newsk so we can guarantee that other | 
 | 6937 | 	 * paths won't try to lock it and then oldsk. | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6938 | 	 */ | 
| Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 6939 | 	lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | 	sctp_assoc_migrate(assoc, newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 |  | 
 | 6942 | 	/* If the association on the newsk is already closed before accept() | 
 | 6943 | 	 * is called, set RCV_SHUTDOWN flag. | 
 | 6944 | 	 */ | 
 | 6945 | 	if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) | 
 | 6946 | 		newsk->sk_shutdown |= RCV_SHUTDOWN; | 
 | 6947 |  | 
 | 6948 | 	newsk->sk_state = SCTP_SS_ESTABLISHED; | 
| Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6949 | 	sctp_release_sock(newsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | } | 
 | 6951 |  | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6952 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | /* This proto struct describes the ULP interface for SCTP.  */ | 
 | 6954 | struct proto sctp_prot = { | 
 | 6955 | 	.name        =	"SCTP", | 
 | 6956 | 	.owner       =	THIS_MODULE, | 
 | 6957 | 	.close       =	sctp_close, | 
 | 6958 | 	.connect     =	sctp_connect, | 
 | 6959 | 	.disconnect  =	sctp_disconnect, | 
 | 6960 | 	.accept      =	sctp_accept, | 
 | 6961 | 	.ioctl       =	sctp_ioctl, | 
 | 6962 | 	.init        =	sctp_init_sock, | 
 | 6963 | 	.destroy     =	sctp_destroy_sock, | 
 | 6964 | 	.shutdown    =	sctp_shutdown, | 
 | 6965 | 	.setsockopt  =	sctp_setsockopt, | 
 | 6966 | 	.getsockopt  =	sctp_getsockopt, | 
 | 6967 | 	.sendmsg     =	sctp_sendmsg, | 
 | 6968 | 	.recvmsg     =	sctp_recvmsg, | 
 | 6969 | 	.bind        =	sctp_bind, | 
 | 6970 | 	.backlog_rcv =	sctp_backlog_rcv, | 
 | 6971 | 	.hash        =	sctp_hash, | 
 | 6972 | 	.unhash      =	sctp_unhash, | 
 | 6973 | 	.get_port    =	sctp_get_port, | 
 | 6974 | 	.obj_size    =  sizeof(struct sctp_sock), | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 6975 | 	.sysctl_mem  =  sysctl_sctp_mem, | 
 | 6976 | 	.sysctl_rmem =  sysctl_sctp_rmem, | 
 | 6977 | 	.sysctl_wmem =  sysctl_sctp_wmem, | 
 | 6978 | 	.memory_pressure = &sctp_memory_pressure, | 
 | 6979 | 	.enter_memory_pressure = sctp_enter_memory_pressure, | 
 | 6980 | 	.memory_allocated = &sctp_memory_allocated, | 
| Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 6981 | 	.sockets_allocated = &sctp_sockets_allocated, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | }; | 
 | 6983 |  | 
| Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 6984 | #if IS_ENABLED(CONFIG_IPV6) | 
| Eric Dumazet | 8295b6d | 2007-11-05 23:40:28 -0800 | [diff] [blame] | 6985 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | struct proto sctpv6_prot = { | 
 | 6987 | 	.name		= "SCTPv6", | 
 | 6988 | 	.owner		= THIS_MODULE, | 
 | 6989 | 	.close		= sctp_close, | 
 | 6990 | 	.connect	= sctp_connect, | 
 | 6991 | 	.disconnect	= sctp_disconnect, | 
 | 6992 | 	.accept		= sctp_accept, | 
 | 6993 | 	.ioctl		= sctp_ioctl, | 
 | 6994 | 	.init		= sctp_init_sock, | 
 | 6995 | 	.destroy	= sctp_destroy_sock, | 
 | 6996 | 	.shutdown	= sctp_shutdown, | 
 | 6997 | 	.setsockopt	= sctp_setsockopt, | 
 | 6998 | 	.getsockopt	= sctp_getsockopt, | 
 | 6999 | 	.sendmsg	= sctp_sendmsg, | 
 | 7000 | 	.recvmsg	= sctp_recvmsg, | 
 | 7001 | 	.bind		= sctp_bind, | 
 | 7002 | 	.backlog_rcv	= sctp_backlog_rcv, | 
 | 7003 | 	.hash		= sctp_hash, | 
 | 7004 | 	.unhash		= sctp_unhash, | 
 | 7005 | 	.get_port	= sctp_get_port, | 
 | 7006 | 	.obj_size	= sizeof(struct sctp6_sock), | 
| Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 7007 | 	.sysctl_mem	= sysctl_sctp_mem, | 
 | 7008 | 	.sysctl_rmem	= sysctl_sctp_rmem, | 
 | 7009 | 	.sysctl_wmem	= sysctl_sctp_wmem, | 
 | 7010 | 	.memory_pressure = &sctp_memory_pressure, | 
 | 7011 | 	.enter_memory_pressure = sctp_enter_memory_pressure, | 
 | 7012 | 	.memory_allocated = &sctp_memory_allocated, | 
| Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 7013 | 	.sockets_allocated = &sctp_sockets_allocated, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | }; | 
| Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 7015 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |