Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* SCTP kernel reference Implementation |
| 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 | * |
| 9 | * This file is part of the SCTP kernel reference Implementation |
| 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 | * |
| 18 | * The SCTP reference implementation is free software; |
| 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 | * |
| 24 | * The SCTP reference implementation is distributed in the hope that it |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/types.h> |
| 61 | #include <linux/kernel.h> |
| 62 | #include <linux/wait.h> |
| 63 | #include <linux/time.h> |
| 64 | #include <linux/ip.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 65 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <linux/fcntl.h> |
| 67 | #include <linux/poll.h> |
| 68 | #include <linux/init.h> |
| 69 | #include <linux/crypto.h> |
| 70 | |
| 71 | #include <net/ip.h> |
| 72 | #include <net/icmp.h> |
| 73 | #include <net/route.h> |
| 74 | #include <net/ipv6.h> |
| 75 | #include <net/inet_common.h> |
| 76 | |
| 77 | #include <linux/socket.h> /* for sa_family_t */ |
| 78 | #include <net/sock.h> |
| 79 | #include <net/sctp/sctp.h> |
| 80 | #include <net/sctp/sm.h> |
| 81 | |
| 82 | /* WARNING: Please do not remove the SCTP_STATIC attribute to |
| 83 | * any of the functions below as they are used to export functions |
| 84 | * used by a project regression testsuite. |
| 85 | */ |
| 86 | |
| 87 | /* Forward declarations for internal helper functions. */ |
| 88 | static int sctp_writeable(struct sock *sk); |
| 89 | static void sctp_wfree(struct sk_buff *skb); |
| 90 | static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p, |
| 91 | size_t msg_len); |
| 92 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p); |
| 93 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); |
| 94 | static int sctp_wait_for_accept(struct sock *sk, long timeo); |
| 95 | static void sctp_wait_for_close(struct sock *sk, long timeo); |
| 96 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 97 | union sctp_addr *addr, int len); |
| 98 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); |
| 99 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); |
| 100 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); |
| 101 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); |
| 102 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 103 | struct sctp_chunk *chunk); |
| 104 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); |
| 105 | static int sctp_autobind(struct sock *sk); |
| 106 | static void sctp_sock_migrate(struct sock *, struct sock *, |
| 107 | struct sctp_association *, sctp_socket_type_t); |
| 108 | static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG; |
| 109 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 110 | extern struct kmem_cache *sctp_bucket_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /* Get the sndbuf space available at the time on the association. */ |
| 113 | static inline int sctp_wspace(struct sctp_association *asoc) |
| 114 | { |
| 115 | struct sock *sk = asoc->base.sk; |
| 116 | int amt = 0; |
| 117 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 118 | if (asoc->ep->sndbuf_policy) { |
| 119 | /* make sure that no association uses more than sk_sndbuf */ |
| 120 | amt = sk->sk_sndbuf - asoc->sndbuf_used; |
| 121 | } else { |
| 122 | /* do socket level accounting */ |
| 123 | amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); |
| 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (amt < 0) |
| 127 | amt = 0; |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return amt; |
| 130 | } |
| 131 | |
| 132 | /* Increment the used sndbuf space count of the corresponding association by |
| 133 | * the size of the outgoing data chunk. |
| 134 | * Also, set the skb destructor for sndbuf accounting later. |
| 135 | * |
| 136 | * Since it is always 1-1 between chunk and skb, and also a new skb is always |
| 137 | * allocated for chunk bundling in sctp_packet_transmit(), we can use the |
| 138 | * destructor in the data chunk skb for the purpose of the sndbuf space |
| 139 | * tracking. |
| 140 | */ |
| 141 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) |
| 142 | { |
| 143 | struct sctp_association *asoc = chunk->asoc; |
| 144 | struct sock *sk = asoc->base.sk; |
| 145 | |
| 146 | /* The sndbuf space is tracked per association. */ |
| 147 | sctp_association_hold(asoc); |
| 148 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 149 | skb_set_owner_w(chunk->skb, sk); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | chunk->skb->destructor = sctp_wfree; |
| 152 | /* Save the chunk pointer in skb for sctp_wfree to use later. */ |
| 153 | *((struct sctp_chunk **)(chunk->skb->cb)) = chunk; |
| 154 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 155 | asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + |
| 156 | sizeof(struct sk_buff) + |
| 157 | sizeof(struct sctp_chunk); |
| 158 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 159 | atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Verify that this is a valid address. */ |
| 163 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, |
| 164 | int len) |
| 165 | { |
| 166 | struct sctp_af *af; |
| 167 | |
| 168 | /* Verify basic sockaddr. */ |
| 169 | af = sctp_sockaddr_af(sctp_sk(sk), addr, len); |
| 170 | if (!af) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | /* Is this a valid SCTP address? */ |
Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 174 | if (!af->addr_valid(addr, sctp_sk(sk), NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | |
| 177 | if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* Look up the association by its id. If this is not a UDP-style |
| 184 | * socket, the ID field is always ignored. |
| 185 | */ |
| 186 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) |
| 187 | { |
| 188 | struct sctp_association *asoc = NULL; |
| 189 | |
| 190 | /* If this is not a UDP-style socket, assoc id should be ignored. */ |
| 191 | if (!sctp_style(sk, UDP)) { |
| 192 | /* Return NULL if the socket state is not ESTABLISHED. It |
| 193 | * could be a TCP-style listening socket or a socket which |
| 194 | * hasn't yet called connect() to establish an association. |
| 195 | */ |
| 196 | if (!sctp_sstate(sk, ESTABLISHED)) |
| 197 | return NULL; |
| 198 | |
| 199 | /* Get the first and the only association from the list. */ |
| 200 | if (!list_empty(&sctp_sk(sk)->ep->asocs)) |
| 201 | asoc = list_entry(sctp_sk(sk)->ep->asocs.next, |
| 202 | struct sctp_association, asocs); |
| 203 | return asoc; |
| 204 | } |
| 205 | |
| 206 | /* Otherwise this is a UDP-style socket. */ |
| 207 | if (!id || (id == (sctp_assoc_t)-1)) |
| 208 | return NULL; |
| 209 | |
| 210 | spin_lock_bh(&sctp_assocs_id_lock); |
| 211 | asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); |
| 212 | spin_unlock_bh(&sctp_assocs_id_lock); |
| 213 | |
| 214 | if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) |
| 215 | return NULL; |
| 216 | |
| 217 | return asoc; |
| 218 | } |
| 219 | |
| 220 | /* Look up the transport from an address and an assoc id. If both address and |
| 221 | * id are specified, the associations matching the address and the id should be |
| 222 | * the same. |
| 223 | */ |
| 224 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, |
| 225 | struct sockaddr_storage *addr, |
| 226 | sctp_assoc_t id) |
| 227 | { |
| 228 | struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; |
| 229 | struct sctp_transport *transport; |
| 230 | union sctp_addr *laddr = (union sctp_addr *)addr; |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, |
Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 233 | laddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | &transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | if (!addr_asoc) |
| 237 | return NULL; |
| 238 | |
| 239 | id_asoc = sctp_id2assoc(sk, id); |
| 240 | if (id_asoc && (id_asoc != addr_asoc)) |
| 241 | return NULL; |
| 242 | |
| 243 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
| 244 | (union sctp_addr *)addr); |
| 245 | |
| 246 | return transport; |
| 247 | } |
| 248 | |
| 249 | /* API 3.1.2 bind() - UDP Style Syntax |
| 250 | * The syntax of bind() is, |
| 251 | * |
| 252 | * ret = bind(int sd, struct sockaddr *addr, int addrlen); |
| 253 | * |
| 254 | * sd - the socket descriptor returned by socket(). |
| 255 | * addr - the address structure (struct sockaddr_in or struct |
| 256 | * sockaddr_in6 [RFC 2553]), |
| 257 | * addr_len - the size of the address structure. |
| 258 | */ |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 259 | 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] | 260 | { |
| 261 | int retval = 0; |
| 262 | |
| 263 | sctp_lock_sock(sk); |
| 264 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 265 | SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n", |
| 266 | sk, addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | /* Disallow binding twice. */ |
| 269 | if (!sctp_sk(sk)->ep->base.bind_addr.port) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 270 | retval = sctp_do_bind(sk, (union sctp_addr *)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | addr_len); |
| 272 | else |
| 273 | retval = -EINVAL; |
| 274 | |
| 275 | sctp_release_sock(sk); |
| 276 | |
| 277 | return retval; |
| 278 | } |
| 279 | |
| 280 | static long sctp_get_port_local(struct sock *, union sctp_addr *); |
| 281 | |
| 282 | /* Verify this is a valid sockaddr. */ |
| 283 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 284 | union sctp_addr *addr, int len) |
| 285 | { |
| 286 | struct sctp_af *af; |
| 287 | |
| 288 | /* Check minimum size. */ |
| 289 | if (len < sizeof (struct sockaddr)) |
| 290 | return NULL; |
| 291 | |
| 292 | /* Does this PF support this AF? */ |
| 293 | if (!opt->pf->af_supported(addr->sa.sa_family, opt)) |
| 294 | return NULL; |
| 295 | |
| 296 | /* If we get this far, af is valid. */ |
| 297 | af = sctp_get_af_specific(addr->sa.sa_family); |
| 298 | |
| 299 | if (len < af->sockaddr_len) |
| 300 | return NULL; |
| 301 | |
| 302 | return af; |
| 303 | } |
| 304 | |
| 305 | /* Bind a local address either to an endpoint or to an association. */ |
| 306 | SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) |
| 307 | { |
| 308 | struct sctp_sock *sp = sctp_sk(sk); |
| 309 | struct sctp_endpoint *ep = sp->ep; |
| 310 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 311 | struct sctp_af *af; |
| 312 | unsigned short snum; |
| 313 | int ret = 0; |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* Common sockaddr verification. */ |
| 316 | af = sctp_sockaddr_af(sp, addr, len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 317 | if (!af) { |
| 318 | SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n", |
| 319 | sk, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return -EINVAL; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | snum = ntohs(addr->v4.sin_port); |
| 324 | |
| 325 | SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ", |
| 326 | ", port: %d, new port: %d, len: %d)\n", |
| 327 | sk, |
| 328 | addr, |
| 329 | bp->port, snum, |
| 330 | len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | /* PF specific bind() address verification. */ |
| 333 | if (!sp->pf->bind_verify(sp, addr)) |
| 334 | return -EADDRNOTAVAIL; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* We must either be unbound, or bind to the same port. */ |
| 337 | if (bp->port && (snum != bp->port)) { |
| 338 | SCTP_DEBUG_PRINTK("sctp_do_bind:" |
| 339 | " New port %d does not match existing port " |
| 340 | "%d.\n", snum, bp->port); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) |
| 345 | return -EACCES; |
| 346 | |
| 347 | /* Make sure we are allowed to bind here. |
| 348 | * The function sctp_get_port_local() does duplicate address |
| 349 | * detection. |
| 350 | */ |
| 351 | if ((ret = sctp_get_port_local(sk, addr))) { |
| 352 | if (ret == (long) sk) { |
| 353 | /* This endpoint has a conflicting address. */ |
| 354 | return -EINVAL; |
| 355 | } else { |
| 356 | return -EADDRINUSE; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Refresh ephemeral port. */ |
| 361 | if (!bp->port) |
| 362 | bp->port = inet_sk(sk)->num; |
| 363 | |
| 364 | /* Add the address to the bind address list. */ |
| 365 | sctp_local_bh_disable(); |
| 366 | sctp_write_lock(&ep->base.addr_lock); |
| 367 | |
| 368 | /* Use GFP_ATOMIC since BHs are disabled. */ |
Al Viro | 5ab7b85 | 2006-11-20 17:10:38 -0800 | [diff] [blame] | 369 | ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | sctp_write_unlock(&ep->base.addr_lock); |
| 371 | sctp_local_bh_enable(); |
| 372 | |
| 373 | /* Copy back into socket for getsockname() use. */ |
| 374 | if (!ret) { |
| 375 | inet_sk(sk)->sport = htons(inet_sk(sk)->num); |
| 376 | af->to_sk_saddr(addr, sk); |
| 377 | } |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks |
| 383 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 384 | * 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] | 385 | * 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] | 386 | * it needs to transfer another ASCONF Chunk, it MUST wait until the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 388 | * subsequent ASCONF. Note this restriction binds each side, so at any |
| 389 | * 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] | 390 | * from each endpoint). |
| 391 | */ |
| 392 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 393 | struct sctp_chunk *chunk) |
| 394 | { |
| 395 | int retval = 0; |
| 396 | |
| 397 | /* If there is an outstanding ASCONF chunk, queue it for later |
| 398 | * transmission. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 399 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (asoc->addip_last_asconf) { |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 401 | list_add_tail(&chunk->list, &asoc->addip_chunk_list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 402 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* Hold the chunk until an ASCONF_ACK is received. */ |
| 406 | sctp_chunk_hold(chunk); |
| 407 | retval = sctp_primitive_ASCONF(asoc, chunk); |
| 408 | if (retval) |
| 409 | sctp_chunk_free(chunk); |
| 410 | else |
| 411 | asoc->addip_last_asconf = chunk; |
| 412 | |
| 413 | out: |
| 414 | return retval; |
| 415 | } |
| 416 | |
| 417 | /* Add a list of addresses as bind addresses to local endpoint or |
| 418 | * association. |
| 419 | * |
| 420 | * Basically run through each address specified in the addrs/addrcnt |
| 421 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 422 | * sctp_do_bind() on it. |
| 423 | * |
| 424 | * If any of them fails, then the operation will be reversed and the |
| 425 | * ones that were added will be removed. |
| 426 | * |
| 427 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 428 | */ |
| 429 | int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
| 430 | { |
| 431 | int cnt; |
| 432 | int retval = 0; |
| 433 | void *addr_buf; |
| 434 | struct sockaddr *sa_addr; |
| 435 | struct sctp_af *af; |
| 436 | |
| 437 | SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n", |
| 438 | sk, addrs, addrcnt); |
| 439 | |
| 440 | addr_buf = addrs; |
| 441 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 442 | /* The list may contain either IPv4 or IPv6 address; |
| 443 | * determine the address length for walking thru the list. |
| 444 | */ |
| 445 | sa_addr = (struct sockaddr *)addr_buf; |
| 446 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 447 | if (!af) { |
| 448 | retval = -EINVAL; |
| 449 | goto err_bindx_add; |
| 450 | } |
| 451 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 452 | retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | af->sockaddr_len); |
| 454 | |
| 455 | addr_buf += af->sockaddr_len; |
| 456 | |
| 457 | err_bindx_add: |
| 458 | if (retval < 0) { |
| 459 | /* Failed. Cleanup the ones that have been added */ |
| 460 | if (cnt > 0) |
| 461 | sctp_bindx_rem(sk, addrs, cnt); |
| 462 | return retval; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return retval; |
| 467 | } |
| 468 | |
| 469 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the |
| 470 | * associations that are part of the endpoint indicating that a list of local |
| 471 | * addresses are added to the endpoint. |
| 472 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 473 | * 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] | 474 | * association, we do not send the chunk for that association. But it will not |
| 475 | * affect other associations. |
| 476 | * |
| 477 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 478 | */ |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 479 | static int sctp_send_asconf_add_ip(struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct sockaddr *addrs, |
| 481 | int addrcnt) |
| 482 | { |
| 483 | struct sctp_sock *sp; |
| 484 | struct sctp_endpoint *ep; |
| 485 | struct sctp_association *asoc; |
| 486 | struct sctp_bind_addr *bp; |
| 487 | struct sctp_chunk *chunk; |
| 488 | struct sctp_sockaddr_entry *laddr; |
| 489 | union sctp_addr *addr; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 490 | union sctp_addr saveaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | void *addr_buf; |
| 492 | struct sctp_af *af; |
| 493 | struct list_head *pos; |
| 494 | struct list_head *p; |
| 495 | int i; |
| 496 | int retval = 0; |
| 497 | |
| 498 | if (!sctp_addip_enable) |
| 499 | return retval; |
| 500 | |
| 501 | sp = sctp_sk(sk); |
| 502 | ep = sp->ep; |
| 503 | |
| 504 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", |
| 505 | __FUNCTION__, sk, addrs, addrcnt); |
| 506 | |
| 507 | list_for_each(pos, &ep->asocs) { |
| 508 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 509 | |
| 510 | if (!asoc->peer.asconf_capable) |
| 511 | continue; |
| 512 | |
| 513 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) |
| 514 | continue; |
| 515 | |
| 516 | if (!sctp_state(asoc, ESTABLISHED)) |
| 517 | continue; |
| 518 | |
| 519 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 520 | * in the bind address list of the association. If so, |
| 521 | * do not send the asconf chunk to its peer, but continue with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | * other associations. |
| 523 | */ |
| 524 | addr_buf = addrs; |
| 525 | for (i = 0; i < addrcnt; i++) { |
| 526 | addr = (union sctp_addr *)addr_buf; |
| 527 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 528 | if (!af) { |
| 529 | retval = -EINVAL; |
| 530 | goto out; |
| 531 | } |
| 532 | |
| 533 | if (sctp_assoc_lookup_laddr(asoc, addr)) |
| 534 | break; |
| 535 | |
| 536 | addr_buf += af->sockaddr_len; |
| 537 | } |
| 538 | if (i < addrcnt) |
| 539 | continue; |
| 540 | |
| 541 | /* Use the first address in bind addr list of association as |
| 542 | * Address Parameter of ASCONF CHUNK. |
| 543 | */ |
| 544 | sctp_read_lock(&asoc->base.addr_lock); |
| 545 | bp = &asoc->base.bind_addr; |
| 546 | p = bp->address_list.next; |
| 547 | laddr = list_entry(p, struct sctp_sockaddr_entry, list); |
| 548 | sctp_read_unlock(&asoc->base.addr_lock); |
| 549 | |
Al Viro | 5ae955c | 2006-11-20 17:22:08 -0800 | [diff] [blame] | 550 | chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | addrcnt, SCTP_PARAM_ADD_IP); |
| 552 | if (!chunk) { |
| 553 | retval = -ENOMEM; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | retval = sctp_send_asconf(asoc, chunk); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 558 | if (retval) |
| 559 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 561 | /* Add the new addresses to the bind address list with |
| 562 | * use_as_src set to 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 564 | sctp_local_bh_disable(); |
| 565 | sctp_write_lock(&asoc->base.addr_lock); |
| 566 | addr_buf = addrs; |
| 567 | for (i = 0; i < addrcnt; i++) { |
| 568 | addr = (union sctp_addr *)addr_buf; |
| 569 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 570 | memcpy(&saveaddr, addr, af->sockaddr_len); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 571 | retval = sctp_add_bind_addr(bp, &saveaddr, 0, |
| 572 | GFP_ATOMIC); |
| 573 | addr_buf += af->sockaddr_len; |
| 574 | } |
| 575 | sctp_write_unlock(&asoc->base.addr_lock); |
| 576 | sctp_local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | out: |
| 580 | return retval; |
| 581 | } |
| 582 | |
| 583 | /* Remove a list of addresses from bind addresses list. Do not remove the |
| 584 | * last address. |
| 585 | * |
| 586 | * Basically run through each address specified in the addrs/addrcnt |
| 587 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 588 | * sctp_del_bind() on it. |
| 589 | * |
| 590 | * If any of them fails, then the operation will be reversed and the |
| 591 | * ones that were removed will be added back. |
| 592 | * |
| 593 | * At least one address has to be left; if only one address is |
| 594 | * available, the operation will return -EBUSY. |
| 595 | * |
| 596 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 597 | */ |
| 598 | int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
| 599 | { |
| 600 | struct sctp_sock *sp = sctp_sk(sk); |
| 601 | struct sctp_endpoint *ep = sp->ep; |
| 602 | int cnt; |
| 603 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 604 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | void *addr_buf; |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 606 | union sctp_addr *sa_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | struct sctp_af *af; |
| 608 | |
| 609 | SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n", |
| 610 | sk, addrs, addrcnt); |
| 611 | |
| 612 | addr_buf = addrs; |
| 613 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 614 | /* If the bind address list is empty or if there is only one |
| 615 | * bind address, there is nothing more to be removed (we need |
| 616 | * at least one address here). |
| 617 | */ |
| 618 | if (list_empty(&bp->address_list) || |
| 619 | (sctp_list_single_entry(&bp->address_list))) { |
| 620 | retval = -EBUSY; |
| 621 | goto err_bindx_rem; |
| 622 | } |
| 623 | |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 624 | sa_addr = (union sctp_addr *)addr_buf; |
| 625 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (!af) { |
| 627 | retval = -EINVAL; |
| 628 | goto err_bindx_rem; |
| 629 | } |
Paolo Galtieri | 0304ff8a | 2007-04-17 12:52:36 -0700 | [diff] [blame] | 630 | |
| 631 | if (!af->addr_valid(sa_addr, sp, NULL)) { |
| 632 | retval = -EADDRNOTAVAIL; |
| 633 | goto err_bindx_rem; |
| 634 | } |
| 635 | |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 636 | if (sa_addr->v4.sin_port != htons(bp->port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | retval = -EINVAL; |
| 638 | goto err_bindx_rem; |
| 639 | } |
| 640 | |
| 641 | /* FIXME - There is probably a need to check if sk->sk_saddr and |
| 642 | * sk->sk_rcv_addr are currently set to one of the addresses to |
| 643 | * be removed. This is something which needs to be looked into |
| 644 | * when we are fixing the outstanding issues with multi-homing |
| 645 | * socket routing and failover schemes. Refer to comments in |
| 646 | * sctp_do_bind(). -daisy |
| 647 | */ |
| 648 | sctp_local_bh_disable(); |
| 649 | sctp_write_lock(&ep->base.addr_lock); |
| 650 | |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 651 | retval = sctp_del_bind_addr(bp, sa_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | sctp_write_unlock(&ep->base.addr_lock); |
| 654 | sctp_local_bh_enable(); |
| 655 | |
| 656 | addr_buf += af->sockaddr_len; |
| 657 | err_bindx_rem: |
| 658 | if (retval < 0) { |
| 659 | /* Failed. Add the ones that has been removed back */ |
| 660 | if (cnt > 0) |
| 661 | sctp_bindx_add(sk, addrs, cnt); |
| 662 | return retval; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return retval; |
| 667 | } |
| 668 | |
| 669 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of |
| 670 | * the associations that are part of the endpoint indicating that a list of |
| 671 | * local addresses are removed from the endpoint. |
| 672 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 673 | * 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] | 674 | * association, we do not send the chunk for that association. But it will not |
| 675 | * affect other associations. |
| 676 | * |
| 677 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 678 | */ |
| 679 | static int sctp_send_asconf_del_ip(struct sock *sk, |
| 680 | struct sockaddr *addrs, |
| 681 | int addrcnt) |
| 682 | { |
| 683 | struct sctp_sock *sp; |
| 684 | struct sctp_endpoint *ep; |
| 685 | struct sctp_association *asoc; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 686 | struct sctp_transport *transport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | struct sctp_bind_addr *bp; |
| 688 | struct sctp_chunk *chunk; |
| 689 | union sctp_addr *laddr; |
| 690 | void *addr_buf; |
| 691 | struct sctp_af *af; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 692 | struct list_head *pos, *pos1; |
| 693 | struct sctp_sockaddr_entry *saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | int i; |
| 695 | int retval = 0; |
| 696 | |
| 697 | if (!sctp_addip_enable) |
| 698 | return retval; |
| 699 | |
| 700 | sp = sctp_sk(sk); |
| 701 | ep = sp->ep; |
| 702 | |
| 703 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", |
| 704 | __FUNCTION__, sk, addrs, addrcnt); |
| 705 | |
| 706 | list_for_each(pos, &ep->asocs) { |
| 707 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 708 | |
| 709 | if (!asoc->peer.asconf_capable) |
| 710 | continue; |
| 711 | |
| 712 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) |
| 713 | continue; |
| 714 | |
| 715 | if (!sctp_state(asoc, ESTABLISHED)) |
| 716 | continue; |
| 717 | |
| 718 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 719 | * not present in the bind address list of the association. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | * If so, do not send the asconf chunk to its peer, but |
| 721 | * continue with other associations. |
| 722 | */ |
| 723 | addr_buf = addrs; |
| 724 | for (i = 0; i < addrcnt; i++) { |
| 725 | laddr = (union sctp_addr *)addr_buf; |
| 726 | af = sctp_get_af_specific(laddr->v4.sin_family); |
| 727 | if (!af) { |
| 728 | retval = -EINVAL; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | if (!sctp_assoc_lookup_laddr(asoc, laddr)) |
| 733 | break; |
| 734 | |
| 735 | addr_buf += af->sockaddr_len; |
| 736 | } |
| 737 | if (i < addrcnt) |
| 738 | continue; |
| 739 | |
| 740 | /* Find one address in the association's bind address list |
| 741 | * that is not in the packed array of addresses. This is to |
| 742 | * make sure that we do not delete all the addresses in the |
| 743 | * association. |
| 744 | */ |
| 745 | sctp_read_lock(&asoc->base.addr_lock); |
| 746 | bp = &asoc->base.bind_addr; |
| 747 | laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, |
| 748 | addrcnt, sp); |
| 749 | sctp_read_unlock(&asoc->base.addr_lock); |
| 750 | if (!laddr) |
| 751 | continue; |
| 752 | |
| 753 | chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, |
| 754 | SCTP_PARAM_DEL_IP); |
| 755 | if (!chunk) { |
| 756 | retval = -ENOMEM; |
| 757 | goto out; |
| 758 | } |
| 759 | |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 760 | /* Reset use_as_src flag for the addresses in the bind address |
| 761 | * list that are to be deleted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 763 | sctp_local_bh_disable(); |
| 764 | sctp_write_lock(&asoc->base.addr_lock); |
| 765 | addr_buf = addrs; |
| 766 | for (i = 0; i < addrcnt; i++) { |
| 767 | laddr = (union sctp_addr *)addr_buf; |
| 768 | af = sctp_get_af_specific(laddr->v4.sin_family); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 769 | list_for_each(pos1, &bp->address_list) { |
| 770 | saddr = list_entry(pos1, |
| 771 | struct sctp_sockaddr_entry, |
| 772 | list); |
Al Viro | 5f242a1 | 2006-11-20 17:05:23 -0800 | [diff] [blame] | 773 | if (sctp_cmp_addr_exact(&saddr->a, laddr)) |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 774 | saddr->use_as_src = 0; |
| 775 | } |
| 776 | addr_buf += af->sockaddr_len; |
| 777 | } |
| 778 | sctp_write_unlock(&asoc->base.addr_lock); |
| 779 | sctp_local_bh_enable(); |
| 780 | |
| 781 | /* Update the route and saddr entries for all the transports |
| 782 | * as some of the addresses in the bind address list are |
| 783 | * about to be deleted and cannot be used as source addresses. |
| 784 | */ |
| 785 | list_for_each(pos1, &asoc->peer.transport_addr_list) { |
| 786 | transport = list_entry(pos1, struct sctp_transport, |
| 787 | transports); |
| 788 | dst_release(transport->dst); |
| 789 | sctp_transport_route(transport, NULL, |
| 790 | sctp_sk(asoc->base.sk)); |
| 791 | } |
| 792 | |
| 793 | retval = sctp_send_asconf(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | out: |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() |
| 800 | * |
| 801 | * API 8.1 |
| 802 | * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, |
| 803 | * int flags); |
| 804 | * |
| 805 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 806 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 807 | * or IPv6 addresses. |
| 808 | * |
| 809 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 810 | * Section 3.1.2 for this usage. |
| 811 | * |
| 812 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 813 | * address is contained in its appropriate structure (i.e. struct |
| 814 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
Ville Nuorvala | 23c435f | 2006-10-16 22:08:28 -0700 | [diff] [blame] | 815 | * must be used to distinguish the address length (note that this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | * representation is termed a "packed array" of addresses). The caller |
| 817 | * specifies the number of addresses in the array with addrcnt. |
| 818 | * |
| 819 | * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns |
| 820 | * -1, and sets errno to the appropriate error code. |
| 821 | * |
| 822 | * For SCTP, the port given in each socket address must be the same, or |
| 823 | * sctp_bindx() will fail, setting errno to EINVAL. |
| 824 | * |
| 825 | * The flags parameter is formed from the bitwise OR of zero or more of |
| 826 | * the following currently defined flags: |
| 827 | * |
| 828 | * SCTP_BINDX_ADD_ADDR |
| 829 | * |
| 830 | * SCTP_BINDX_REM_ADDR |
| 831 | * |
| 832 | * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the |
| 833 | * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given |
| 834 | * addresses from the association. The two flags are mutually exclusive; |
| 835 | * if both are given, sctp_bindx() will fail with EINVAL. A caller may |
| 836 | * not remove all addresses from an association; sctp_bindx() will |
| 837 | * reject such an attempt with EINVAL. |
| 838 | * |
| 839 | * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate |
| 840 | * additional addresses with an endpoint after calling bind(). Or use |
| 841 | * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening |
| 842 | * socket is associated with so that no new association accepted will be |
| 843 | * associated with those addresses. If the endpoint supports dynamic |
| 844 | * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a |
| 845 | * endpoint to send the appropriate message to the peer to change the |
| 846 | * peers address lists. |
| 847 | * |
| 848 | * Adding and removing addresses from a connected association is |
| 849 | * optional functionality. Implementations that do not support this |
| 850 | * functionality should return EOPNOTSUPP. |
| 851 | * |
| 852 | * Basically do nothing but copying the addresses from user to kernel |
| 853 | * 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] | 854 | * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() |
| 855 | * from userspace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | * |
| 857 | * We don't use copy_from_user() for optimization: we first do the |
| 858 | * sanity checks (buffer size -fast- and access check-healthy |
| 859 | * pointer); if all of those succeed, then we can alloc the memory |
| 860 | * (expensive operation) needed to copy the data to kernel. Then we do |
| 861 | * the copying without checking the user space area |
| 862 | * (__copy_from_user()). |
| 863 | * |
| 864 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 865 | * it. |
| 866 | * |
| 867 | * sk The sk of the socket |
| 868 | * addrs The pointer to the addresses in user land |
| 869 | * addrssize Size of the addrs buffer |
| 870 | * op Operation to perform (add or remove, see the flags of |
| 871 | * sctp_bindx) |
| 872 | * |
| 873 | * Returns 0 if ok, <0 errno code on error. |
| 874 | */ |
| 875 | SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, |
| 876 | struct sockaddr __user *addrs, |
| 877 | int addrs_size, int op) |
| 878 | { |
| 879 | struct sockaddr *kaddrs; |
| 880 | int err; |
| 881 | int addrcnt = 0; |
| 882 | int walk_size = 0; |
| 883 | struct sockaddr *sa_addr; |
| 884 | void *addr_buf; |
| 885 | struct sctp_af *af; |
| 886 | |
| 887 | SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p" |
| 888 | " addrs_size %d opt %d\n", sk, addrs, addrs_size, op); |
| 889 | |
| 890 | if (unlikely(addrs_size <= 0)) |
| 891 | return -EINVAL; |
| 892 | |
| 893 | /* Check the user passed a healthy pointer. */ |
| 894 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
| 895 | return -EFAULT; |
| 896 | |
| 897 | /* Alloc space for the address array in kernel memory. */ |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 898 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | if (unlikely(!kaddrs)) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
| 903 | kfree(kaddrs); |
| 904 | return -EFAULT; |
| 905 | } |
| 906 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 907 | /* Walk through the addrs buffer and count the number of addresses. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | addr_buf = kaddrs; |
| 909 | while (walk_size < addrs_size) { |
| 910 | sa_addr = (struct sockaddr *)addr_buf; |
| 911 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 912 | |
| 913 | /* If the address family is not supported or if this address |
| 914 | * causes the address buffer to overflow return EINVAL. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 915 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
| 917 | kfree(kaddrs); |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | addrcnt++; |
| 921 | addr_buf += af->sockaddr_len; |
| 922 | walk_size += af->sockaddr_len; |
| 923 | } |
| 924 | |
| 925 | /* Do the work. */ |
| 926 | switch (op) { |
| 927 | case SCTP_BINDX_ADD_ADDR: |
| 928 | err = sctp_bindx_add(sk, kaddrs, addrcnt); |
| 929 | if (err) |
| 930 | goto out; |
| 931 | err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); |
| 932 | break; |
| 933 | |
| 934 | case SCTP_BINDX_REM_ADDR: |
| 935 | err = sctp_bindx_rem(sk, kaddrs, addrcnt); |
| 936 | if (err) |
| 937 | goto out; |
| 938 | err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); |
| 939 | break; |
| 940 | |
| 941 | default: |
| 942 | err = -EINVAL; |
| 943 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | out: |
| 947 | kfree(kaddrs); |
| 948 | |
| 949 | return err; |
| 950 | } |
| 951 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 952 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) |
| 953 | * |
| 954 | * Common routine for handling connect() and sctp_connectx(). |
| 955 | * Connect will come in with just a single address. |
| 956 | */ |
| 957 | static int __sctp_connect(struct sock* sk, |
| 958 | struct sockaddr *kaddrs, |
| 959 | int addrs_size) |
| 960 | { |
| 961 | struct sctp_sock *sp; |
| 962 | struct sctp_endpoint *ep; |
| 963 | struct sctp_association *asoc = NULL; |
| 964 | struct sctp_association *asoc2; |
| 965 | struct sctp_transport *transport; |
| 966 | union sctp_addr to; |
| 967 | struct sctp_af *af; |
| 968 | sctp_scope_t scope; |
| 969 | long timeo; |
| 970 | int err = 0; |
| 971 | int addrcnt = 0; |
| 972 | int walk_size = 0; |
Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 973 | union sctp_addr *sa_addr; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 974 | void *addr_buf; |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame^] | 975 | unsigned short port; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 976 | |
| 977 | sp = sctp_sk(sk); |
| 978 | ep = sp->ep; |
| 979 | |
| 980 | /* connect() cannot be done on a socket that is already in ESTABLISHED |
| 981 | * state - UDP-style peeled off socket or a TCP-style socket that |
| 982 | * is already connected. |
| 983 | * It cannot be done even on a TCP-style listening socket. |
| 984 | */ |
| 985 | if (sctp_sstate(sk, ESTABLISHED) || |
| 986 | (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { |
| 987 | err = -EISCONN; |
| 988 | goto out_free; |
| 989 | } |
| 990 | |
| 991 | /* Walk through the addrs buffer and count the number of addresses. */ |
| 992 | addr_buf = kaddrs; |
| 993 | while (walk_size < addrs_size) { |
Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 994 | sa_addr = (union sctp_addr *)addr_buf; |
| 995 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame^] | 996 | port = ntohs(sa_addr->v4.sin_port); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 997 | |
| 998 | /* If the address family is not supported or if this address |
| 999 | * causes the address buffer to overflow return EINVAL. |
| 1000 | */ |
| 1001 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
| 1002 | err = -EINVAL; |
| 1003 | goto out_free; |
| 1004 | } |
| 1005 | |
Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 1006 | err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1007 | if (err) |
| 1008 | goto out_free; |
| 1009 | |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame^] | 1010 | /* Make sure the destination port is correctly set |
| 1011 | * in all addresses. |
| 1012 | */ |
| 1013 | if (asoc && asoc->peer.port && asoc->peer.port != port) |
| 1014 | goto out_free; |
| 1015 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1016 | memcpy(&to, sa_addr, af->sockaddr_len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1017 | |
| 1018 | /* Check if there already is a matching association on the |
| 1019 | * endpoint (other than the one created here). |
| 1020 | */ |
Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 1021 | asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1022 | if (asoc2 && asoc2 != asoc) { |
| 1023 | if (asoc2->state >= SCTP_STATE_ESTABLISHED) |
| 1024 | err = -EISCONN; |
| 1025 | else |
| 1026 | err = -EALREADY; |
| 1027 | goto out_free; |
| 1028 | } |
| 1029 | |
| 1030 | /* If we could not find a matching association on the endpoint, |
| 1031 | * make sure that there is no peeled-off association matching |
| 1032 | * the peer address even on another socket. |
| 1033 | */ |
Al Viro | 6c7be55 | 2006-11-20 17:11:50 -0800 | [diff] [blame] | 1034 | if (sctp_endpoint_is_peeled_off(ep, sa_addr)) { |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1035 | err = -EADDRNOTAVAIL; |
| 1036 | goto out_free; |
| 1037 | } |
| 1038 | |
| 1039 | if (!asoc) { |
| 1040 | /* If a bind() or sctp_bindx() is not called prior to |
| 1041 | * an sctp_connectx() call, the system picks an |
| 1042 | * ephemeral port and will choose an address set |
| 1043 | * equivalent to binding with a wildcard address. |
| 1044 | */ |
| 1045 | if (!ep->base.bind_addr.port) { |
| 1046 | if (sctp_autobind(sk)) { |
| 1047 | err = -EAGAIN; |
| 1048 | goto out_free; |
| 1049 | } |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1050 | } else { |
| 1051 | /* |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1052 | * If an unprivileged user inherits a 1-many |
| 1053 | * style socket with open associations on a |
| 1054 | * privileged port, it MAY be permitted to |
| 1055 | * accept new associations, but it SHOULD NOT |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1056 | * be permitted to open new associations. |
| 1057 | */ |
| 1058 | if (ep->base.bind_addr.port < PROT_SOCK && |
| 1059 | !capable(CAP_NET_BIND_SERVICE)) { |
| 1060 | err = -EACCES; |
| 1061 | goto out_free; |
| 1062 | } |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1065 | scope = sctp_scope(sa_addr); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1066 | asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
| 1067 | if (!asoc) { |
| 1068 | err = -ENOMEM; |
| 1069 | goto out_free; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /* Prime the peer's transport structures. */ |
Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 1074 | transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL, |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1075 | SCTP_UNKNOWN); |
| 1076 | if (!transport) { |
| 1077 | err = -ENOMEM; |
| 1078 | goto out_free; |
| 1079 | } |
| 1080 | |
| 1081 | addrcnt++; |
| 1082 | addr_buf += af->sockaddr_len; |
| 1083 | walk_size += af->sockaddr_len; |
| 1084 | } |
| 1085 | |
| 1086 | err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL); |
| 1087 | if (err < 0) { |
| 1088 | goto out_free; |
| 1089 | } |
| 1090 | |
| 1091 | err = sctp_primitive_ASSOCIATE(asoc, NULL); |
| 1092 | if (err < 0) { |
| 1093 | goto out_free; |
| 1094 | } |
| 1095 | |
| 1096 | /* Initialize sk's dport and daddr for getpeername() */ |
| 1097 | inet_sk(sk)->dport = htons(asoc->peer.port); |
| 1098 | af = sctp_get_af_specific(to.sa.sa_family); |
| 1099 | af->to_sk_daddr(&to, sk); |
Sridhar Samudrala | 8de8c87 | 2006-05-19 10:58:12 -0700 | [diff] [blame] | 1100 | sk->sk_err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1101 | |
| 1102 | timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK); |
| 1103 | err = sctp_wait_for_connect(asoc, &timeo); |
| 1104 | |
| 1105 | /* Don't free association on exit. */ |
| 1106 | asoc = NULL; |
| 1107 | |
| 1108 | out_free: |
| 1109 | |
| 1110 | SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p" |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1111 | " kaddrs: %p err: %d\n", |
| 1112 | asoc, kaddrs, err); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1113 | if (asoc) |
| 1114 | sctp_association_free(asoc); |
| 1115 | return err; |
| 1116 | } |
| 1117 | |
| 1118 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() |
| 1119 | * |
| 1120 | * API 8.9 |
| 1121 | * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt); |
| 1122 | * |
| 1123 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 1124 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 1125 | * or IPv6 addresses. |
| 1126 | * |
| 1127 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 1128 | * Section 3.1.2 for this usage. |
| 1129 | * |
| 1130 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 1131 | * address is contained in its appropriate structure (i.e. struct |
| 1132 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
| 1133 | * must be used to distengish the address length (note that this |
| 1134 | * representation is termed a "packed array" of addresses). The caller |
| 1135 | * specifies the number of addresses in the array with addrcnt. |
| 1136 | * |
| 1137 | * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns |
| 1138 | * -1, and sets errno to the appropriate error code. |
| 1139 | * |
| 1140 | * For SCTP, the port given in each socket address must be the same, or |
| 1141 | * sctp_connectx() will fail, setting errno to EINVAL. |
| 1142 | * |
| 1143 | * An application can use sctp_connectx to initiate an association with |
| 1144 | * an endpoint that is multi-homed. Much like sctp_bindx() this call |
| 1145 | * allows a caller to specify multiple addresses at which a peer can be |
| 1146 | * reached. The way the SCTP stack uses the list of addresses to set up |
| 1147 | * the association is implementation dependant. This function only |
| 1148 | * specifies that the stack will try to make use of all the addresses in |
| 1149 | * the list when needed. |
| 1150 | * |
| 1151 | * Note that the list of addresses passed in is only used for setting up |
| 1152 | * the association. It does not necessarily equal the set of addresses |
| 1153 | * the peer uses for the resulting association. If the caller wants to |
| 1154 | * find out the set of peer addresses, it must use sctp_getpaddrs() to |
| 1155 | * retrieve them after the association has been set up. |
| 1156 | * |
| 1157 | * Basically do nothing but copying the addresses from user to kernel |
| 1158 | * land and invoking either sctp_connectx(). This is used for tunneling |
| 1159 | * the sctp_connectx() request through sctp_setsockopt() from userspace. |
| 1160 | * |
| 1161 | * We don't use copy_from_user() for optimization: we first do the |
| 1162 | * sanity checks (buffer size -fast- and access check-healthy |
| 1163 | * pointer); if all of those succeed, then we can alloc the memory |
| 1164 | * (expensive operation) needed to copy the data to kernel. Then we do |
| 1165 | * the copying without checking the user space area |
| 1166 | * (__copy_from_user()). |
| 1167 | * |
| 1168 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 1169 | * it. |
| 1170 | * |
| 1171 | * sk The sk of the socket |
| 1172 | * addrs The pointer to the addresses in user land |
| 1173 | * addrssize Size of the addrs buffer |
| 1174 | * |
| 1175 | * Returns 0 if ok, <0 errno code on error. |
| 1176 | */ |
| 1177 | SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk, |
| 1178 | struct sockaddr __user *addrs, |
| 1179 | int addrs_size) |
| 1180 | { |
| 1181 | int err = 0; |
| 1182 | struct sockaddr *kaddrs; |
| 1183 | |
| 1184 | SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n", |
| 1185 | __FUNCTION__, sk, addrs, addrs_size); |
| 1186 | |
| 1187 | if (unlikely(addrs_size <= 0)) |
| 1188 | return -EINVAL; |
| 1189 | |
| 1190 | /* Check the user passed a healthy pointer. */ |
| 1191 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
| 1192 | return -EFAULT; |
| 1193 | |
| 1194 | /* Alloc space for the address array in kernel memory. */ |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 1195 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1196 | if (unlikely(!kaddrs)) |
| 1197 | return -ENOMEM; |
| 1198 | |
| 1199 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
| 1200 | err = -EFAULT; |
| 1201 | } else { |
| 1202 | err = __sctp_connect(sk, kaddrs, addrs_size); |
| 1203 | } |
| 1204 | |
| 1205 | kfree(kaddrs); |
| 1206 | return err; |
| 1207 | } |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | /* API 3.1.4 close() - UDP Style Syntax |
| 1210 | * Applications use close() to perform graceful shutdown (as described in |
| 1211 | * Section 10.1 of [SCTP]) on ALL the associations currently represented |
| 1212 | * by a UDP-style socket. |
| 1213 | * |
| 1214 | * The syntax is |
| 1215 | * |
| 1216 | * ret = close(int sd); |
| 1217 | * |
| 1218 | * sd - the socket descriptor of the associations to be closed. |
| 1219 | * |
| 1220 | * To gracefully shutdown a specific association represented by the |
| 1221 | * UDP-style socket, an application should use the sendmsg() call, |
| 1222 | * passing no user data, but including the appropriate flag in the |
| 1223 | * ancillary data (see Section xxxx). |
| 1224 | * |
| 1225 | * If sd in the close() call is a branched-off socket representing only |
| 1226 | * one association, the shutdown is performed on that association only. |
| 1227 | * |
| 1228 | * 4.1.6 close() - TCP Style Syntax |
| 1229 | * |
| 1230 | * Applications use close() to gracefully close down an association. |
| 1231 | * |
| 1232 | * The syntax is: |
| 1233 | * |
| 1234 | * int close(int sd); |
| 1235 | * |
| 1236 | * sd - the socket descriptor of the association to be closed. |
| 1237 | * |
| 1238 | * After an application calls close() on a socket descriptor, no further |
| 1239 | * socket operations will succeed on that descriptor. |
| 1240 | * |
| 1241 | * API 7.1.4 SO_LINGER |
| 1242 | * |
| 1243 | * An application using the TCP-style socket can use this option to |
| 1244 | * perform the SCTP ABORT primitive. The linger option structure is: |
| 1245 | * |
| 1246 | * struct linger { |
| 1247 | * int l_onoff; // option on/off |
| 1248 | * int l_linger; // linger time |
| 1249 | * }; |
| 1250 | * |
| 1251 | * To enable the option, set l_onoff to 1. If the l_linger value is set |
| 1252 | * to 0, calling close() is the same as the ABORT primitive. If the |
| 1253 | * value is set to a negative value, the setsockopt() call will return |
| 1254 | * an error. If the value is set to a positive value linger_time, the |
| 1255 | * close() can be blocked for at most linger_time ms. If the graceful |
| 1256 | * shutdown phase does not finish during this period, close() will |
| 1257 | * return but the graceful shutdown phase continues in the system. |
| 1258 | */ |
| 1259 | SCTP_STATIC void sctp_close(struct sock *sk, long timeout) |
| 1260 | { |
| 1261 | struct sctp_endpoint *ep; |
| 1262 | struct sctp_association *asoc; |
| 1263 | struct list_head *pos, *temp; |
| 1264 | |
| 1265 | SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout); |
| 1266 | |
| 1267 | sctp_lock_sock(sk); |
| 1268 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 1269 | |
| 1270 | ep = sctp_sk(sk)->ep; |
| 1271 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 1272 | /* Walk all associations on an endpoint. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | list_for_each_safe(pos, temp, &ep->asocs) { |
| 1274 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 1275 | |
| 1276 | if (sctp_style(sk, TCP)) { |
| 1277 | /* A closed association can still be in the list if |
| 1278 | * it belongs to a TCP-style listening socket that is |
| 1279 | * not yet accepted. If so, free it. If not, send an |
| 1280 | * ABORT or SHUTDOWN based on the linger options. |
| 1281 | */ |
| 1282 | if (sctp_state(asoc, CLOSED)) { |
| 1283 | sctp_unhash_established(asoc); |
| 1284 | sctp_association_free(asoc); |
Vladislav Yasevich | b89498a | 2006-05-19 14:32:06 -0700 | [diff] [blame] | 1285 | continue; |
| 1286 | } |
| 1287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1289 | if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { |
| 1290 | struct sctp_chunk *chunk; |
| 1291 | |
| 1292 | chunk = sctp_make_abort_user(asoc, NULL, 0); |
| 1293 | if (chunk) |
| 1294 | sctp_primitive_ABORT(asoc, chunk); |
| 1295 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | sctp_primitive_SHUTDOWN(asoc, NULL); |
| 1297 | } |
| 1298 | |
| 1299 | /* Clean up any skbs sitting on the receive queue. */ |
| 1300 | sctp_queue_purge_ulpevents(&sk->sk_receive_queue); |
| 1301 | sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); |
| 1302 | |
| 1303 | /* On a TCP-style socket, block for at most linger_time if set. */ |
| 1304 | if (sctp_style(sk, TCP) && timeout) |
| 1305 | sctp_wait_for_close(sk, timeout); |
| 1306 | |
| 1307 | /* This will run the backlog queue. */ |
| 1308 | sctp_release_sock(sk); |
| 1309 | |
| 1310 | /* Supposedly, no process has access to the socket, but |
| 1311 | * the net layers still may. |
| 1312 | */ |
| 1313 | sctp_local_bh_disable(); |
| 1314 | sctp_bh_lock_sock(sk); |
| 1315 | |
| 1316 | /* Hold the sock, since sk_common_release() will put sock_put() |
| 1317 | * and we have just a little more cleanup. |
| 1318 | */ |
| 1319 | sock_hold(sk); |
| 1320 | sk_common_release(sk); |
| 1321 | |
| 1322 | sctp_bh_unlock_sock(sk); |
| 1323 | sctp_local_bh_enable(); |
| 1324 | |
| 1325 | sock_put(sk); |
| 1326 | |
| 1327 | SCTP_DBG_OBJCNT_DEC(sock); |
| 1328 | } |
| 1329 | |
| 1330 | /* Handle EPIPE error. */ |
| 1331 | static int sctp_error(struct sock *sk, int flags, int err) |
| 1332 | { |
| 1333 | if (err == -EPIPE) |
| 1334 | err = sock_error(sk) ? : -EPIPE; |
| 1335 | if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) |
| 1336 | send_sig(SIGPIPE, current, 0); |
| 1337 | return err; |
| 1338 | } |
| 1339 | |
| 1340 | /* API 3.1.3 sendmsg() - UDP Style Syntax |
| 1341 | * |
| 1342 | * An application uses sendmsg() and recvmsg() calls to transmit data to |
| 1343 | * and receive data from its peer. |
| 1344 | * |
| 1345 | * ssize_t sendmsg(int socket, const struct msghdr *message, |
| 1346 | * int flags); |
| 1347 | * |
| 1348 | * socket - the socket descriptor of the endpoint. |
| 1349 | * message - pointer to the msghdr structure which contains a single |
| 1350 | * user message and possibly some ancillary data. |
| 1351 | * |
| 1352 | * See Section 5 for complete description of the data |
| 1353 | * structures. |
| 1354 | * |
| 1355 | * flags - flags sent or received with the user message, see Section |
| 1356 | * 5 for complete description of the flags. |
| 1357 | * |
| 1358 | * Note: This function could use a rewrite especially when explicit |
| 1359 | * connect support comes in. |
| 1360 | */ |
| 1361 | /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ |
| 1362 | |
| 1363 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); |
| 1364 | |
| 1365 | SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 1366 | struct msghdr *msg, size_t msg_len) |
| 1367 | { |
| 1368 | struct sctp_sock *sp; |
| 1369 | struct sctp_endpoint *ep; |
| 1370 | struct sctp_association *new_asoc=NULL, *asoc=NULL; |
| 1371 | struct sctp_transport *transport, *chunk_tp; |
| 1372 | struct sctp_chunk *chunk; |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1373 | union sctp_addr to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | struct sockaddr *msg_name = NULL; |
| 1375 | struct sctp_sndrcvinfo default_sinfo = { 0 }; |
| 1376 | struct sctp_sndrcvinfo *sinfo; |
| 1377 | struct sctp_initmsg *sinit; |
| 1378 | sctp_assoc_t associd = 0; |
| 1379 | sctp_cmsgs_t cmsgs = { NULL }; |
| 1380 | int err; |
| 1381 | sctp_scope_t scope; |
| 1382 | long timeo; |
| 1383 | __u16 sinfo_flags = 0; |
| 1384 | struct sctp_datamsg *datamsg; |
| 1385 | struct list_head *pos; |
| 1386 | int msg_flags = msg->msg_flags; |
| 1387 | |
| 1388 | SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n", |
| 1389 | sk, msg, msg_len); |
| 1390 | |
| 1391 | err = 0; |
| 1392 | sp = sctp_sk(sk); |
| 1393 | ep = sp->ep; |
| 1394 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1395 | SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
| 1397 | /* We cannot send a message over a TCP-style listening socket. */ |
| 1398 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { |
| 1399 | err = -EPIPE; |
| 1400 | goto out_nounlock; |
| 1401 | } |
| 1402 | |
| 1403 | /* Parse out the SCTP CMSGs. */ |
| 1404 | err = sctp_msghdr_parse(msg, &cmsgs); |
| 1405 | |
| 1406 | if (err) { |
| 1407 | SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err); |
| 1408 | goto out_nounlock; |
| 1409 | } |
| 1410 | |
| 1411 | /* Fetch the destination address for this packet. This |
| 1412 | * address only selects the association--it is not necessarily |
| 1413 | * the address we will send to. |
| 1414 | * For a peeled-off socket, msg_name is ignored. |
| 1415 | */ |
| 1416 | if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { |
| 1417 | int msg_namelen = msg->msg_namelen; |
| 1418 | |
| 1419 | err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, |
| 1420 | msg_namelen); |
| 1421 | if (err) |
| 1422 | return err; |
| 1423 | |
| 1424 | if (msg_namelen > sizeof(to)) |
| 1425 | msg_namelen = sizeof(to); |
| 1426 | memcpy(&to, msg->msg_name, msg_namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | msg_name = msg->msg_name; |
| 1428 | } |
| 1429 | |
| 1430 | sinfo = cmsgs.info; |
| 1431 | sinit = cmsgs.init; |
| 1432 | |
| 1433 | /* Did the user specify SNDRCVINFO? */ |
| 1434 | if (sinfo) { |
| 1435 | sinfo_flags = sinfo->sinfo_flags; |
| 1436 | associd = sinfo->sinfo_assoc_id; |
| 1437 | } |
| 1438 | |
| 1439 | SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n", |
| 1440 | msg_len, sinfo_flags); |
| 1441 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1442 | /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ |
| 1443 | if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | err = -EINVAL; |
| 1445 | goto out_nounlock; |
| 1446 | } |
| 1447 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1448 | /* If SCTP_EOF is set, no data can be sent. Disallow sending zero |
| 1449 | * length messages when SCTP_EOF|SCTP_ABORT is not set. |
| 1450 | * 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] | 1451 | * the msg_iov set to the user abort reason. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1452 | */ |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1453 | if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || |
| 1454 | (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | err = -EINVAL; |
| 1456 | goto out_nounlock; |
| 1457 | } |
| 1458 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1459 | /* If SCTP_ADDR_OVER is set, there must be an address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | * specified in msg_name. |
| 1461 | */ |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1462 | if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | err = -EINVAL; |
| 1464 | goto out_nounlock; |
| 1465 | } |
| 1466 | |
| 1467 | transport = NULL; |
| 1468 | |
| 1469 | SCTP_DEBUG_PRINTK("About to look up association.\n"); |
| 1470 | |
| 1471 | sctp_lock_sock(sk); |
| 1472 | |
| 1473 | /* If a msg_name has been specified, assume this is to be used. */ |
| 1474 | if (msg_name) { |
| 1475 | /* Look for a matching association on the endpoint. */ |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1476 | asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!asoc) { |
| 1478 | /* If we could not find a matching association on the |
| 1479 | * endpoint, make sure that it is not a TCP-style |
| 1480 | * socket that already has an association or there is |
| 1481 | * no peeled-off association on another socket. |
| 1482 | */ |
| 1483 | if ((sctp_style(sk, TCP) && |
| 1484 | sctp_sstate(sk, ESTABLISHED)) || |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1485 | sctp_endpoint_is_peeled_off(ep, &to)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | err = -EADDRNOTAVAIL; |
| 1487 | goto out_unlock; |
| 1488 | } |
| 1489 | } |
| 1490 | } else { |
| 1491 | asoc = sctp_id2assoc(sk, associd); |
| 1492 | if (!asoc) { |
| 1493 | err = -EPIPE; |
| 1494 | goto out_unlock; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | if (asoc) { |
| 1499 | SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc); |
| 1500 | |
| 1501 | /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED |
| 1502 | * socket that has an association in CLOSED state. This can |
| 1503 | * happen when an accepted socket has an association that is |
| 1504 | * already CLOSED. |
| 1505 | */ |
| 1506 | if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { |
| 1507 | err = -EPIPE; |
| 1508 | goto out_unlock; |
| 1509 | } |
| 1510 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1511 | if (sinfo_flags & SCTP_EOF) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | SCTP_DEBUG_PRINTK("Shutting down association: %p\n", |
| 1513 | asoc); |
| 1514 | sctp_primitive_SHUTDOWN(asoc, NULL); |
| 1515 | err = 0; |
| 1516 | goto out_unlock; |
| 1517 | } |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1518 | if (sinfo_flags & SCTP_ABORT) { |
Sridhar Samudrala | c164a9b | 2006-08-22 11:50:39 -0700 | [diff] [blame] | 1519 | struct sctp_chunk *chunk; |
| 1520 | |
| 1521 | chunk = sctp_make_abort_user(asoc, msg, msg_len); |
| 1522 | if (!chunk) { |
| 1523 | err = -ENOMEM; |
| 1524 | goto out_unlock; |
| 1525 | } |
| 1526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc); |
Sridhar Samudrala | c164a9b | 2006-08-22 11:50:39 -0700 | [diff] [blame] | 1528 | sctp_primitive_ABORT(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | err = 0; |
| 1530 | goto out_unlock; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | /* Do we need to create the association? */ |
| 1535 | if (!asoc) { |
| 1536 | SCTP_DEBUG_PRINTK("There is no association yet.\n"); |
| 1537 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1538 | if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | err = -EINVAL; |
| 1540 | goto out_unlock; |
| 1541 | } |
| 1542 | |
| 1543 | /* Check for invalid stream against the stream counts, |
| 1544 | * either the default or the user specified stream counts. |
| 1545 | */ |
| 1546 | if (sinfo) { |
| 1547 | if (!sinit || (sinit && !sinit->sinit_num_ostreams)) { |
| 1548 | /* Check against the defaults. */ |
| 1549 | if (sinfo->sinfo_stream >= |
| 1550 | sp->initmsg.sinit_num_ostreams) { |
| 1551 | err = -EINVAL; |
| 1552 | goto out_unlock; |
| 1553 | } |
| 1554 | } else { |
| 1555 | /* Check against the requested. */ |
| 1556 | if (sinfo->sinfo_stream >= |
| 1557 | sinit->sinit_num_ostreams) { |
| 1558 | err = -EINVAL; |
| 1559 | goto out_unlock; |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * API 3.1.2 bind() - UDP Style Syntax |
| 1566 | * If a bind() or sctp_bindx() is not called prior to a |
| 1567 | * sendmsg() call that initiates a new association, the |
| 1568 | * system picks an ephemeral port and will choose an address |
| 1569 | * set equivalent to binding with a wildcard address. |
| 1570 | */ |
| 1571 | if (!ep->base.bind_addr.port) { |
| 1572 | if (sctp_autobind(sk)) { |
| 1573 | err = -EAGAIN; |
| 1574 | goto out_unlock; |
| 1575 | } |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1576 | } else { |
| 1577 | /* |
| 1578 | * If an unprivileged user inherits a one-to-many |
| 1579 | * style socket with open associations on a privileged |
| 1580 | * port, it MAY be permitted to accept new associations, |
| 1581 | * but it SHOULD NOT be permitted to open new |
| 1582 | * associations. |
| 1583 | */ |
| 1584 | if (ep->base.bind_addr.port < PROT_SOCK && |
| 1585 | !capable(CAP_NET_BIND_SERVICE)) { |
| 1586 | err = -EACCES; |
| 1587 | goto out_unlock; |
| 1588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | scope = sctp_scope(&to); |
| 1592 | new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
| 1593 | if (!new_asoc) { |
| 1594 | err = -ENOMEM; |
| 1595 | goto out_unlock; |
| 1596 | } |
| 1597 | asoc = new_asoc; |
| 1598 | |
| 1599 | /* If the SCTP_INIT ancillary data is specified, set all |
| 1600 | * the association init values accordingly. |
| 1601 | */ |
| 1602 | if (sinit) { |
| 1603 | if (sinit->sinit_num_ostreams) { |
| 1604 | asoc->c.sinit_num_ostreams = |
| 1605 | sinit->sinit_num_ostreams; |
| 1606 | } |
| 1607 | if (sinit->sinit_max_instreams) { |
| 1608 | asoc->c.sinit_max_instreams = |
| 1609 | sinit->sinit_max_instreams; |
| 1610 | } |
| 1611 | if (sinit->sinit_max_attempts) { |
| 1612 | asoc->max_init_attempts |
| 1613 | = sinit->sinit_max_attempts; |
| 1614 | } |
| 1615 | if (sinit->sinit_max_init_timeo) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1616 | asoc->max_init_timeo = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | msecs_to_jiffies(sinit->sinit_max_init_timeo); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | /* Prime the peer's transport structures. */ |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1622 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | if (!transport) { |
| 1624 | err = -ENOMEM; |
| 1625 | goto out_free; |
| 1626 | } |
| 1627 | err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL); |
| 1628 | if (err < 0) { |
| 1629 | err = -ENOMEM; |
| 1630 | goto out_free; |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | /* ASSERT: we have a valid association at this point. */ |
| 1635 | SCTP_DEBUG_PRINTK("We have a valid association.\n"); |
| 1636 | |
| 1637 | if (!sinfo) { |
| 1638 | /* If the user didn't specify SNDRCVINFO, make up one with |
| 1639 | * some defaults. |
| 1640 | */ |
| 1641 | default_sinfo.sinfo_stream = asoc->default_stream; |
| 1642 | default_sinfo.sinfo_flags = asoc->default_flags; |
| 1643 | default_sinfo.sinfo_ppid = asoc->default_ppid; |
| 1644 | default_sinfo.sinfo_context = asoc->default_context; |
| 1645 | default_sinfo.sinfo_timetolive = asoc->default_timetolive; |
| 1646 | default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); |
| 1647 | sinfo = &default_sinfo; |
| 1648 | } |
| 1649 | |
| 1650 | /* API 7.1.7, the sndbuf size per association bounds the |
| 1651 | * maximum size of data that can be sent in a single send call. |
| 1652 | */ |
| 1653 | if (msg_len > sk->sk_sndbuf) { |
| 1654 | err = -EMSGSIZE; |
| 1655 | goto out_free; |
| 1656 | } |
| 1657 | |
| 1658 | /* If fragmentation is disabled and the message length exceeds the |
| 1659 | * association fragmentation point, return EMSGSIZE. The I-D |
| 1660 | * does not specify what this error is, but this looks like |
| 1661 | * a great fit. |
| 1662 | */ |
| 1663 | if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { |
| 1664 | err = -EMSGSIZE; |
| 1665 | goto out_free; |
| 1666 | } |
| 1667 | |
| 1668 | if (sinfo) { |
| 1669 | /* Check for invalid stream. */ |
| 1670 | if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) { |
| 1671 | err = -EINVAL; |
| 1672 | goto out_free; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1677 | if (!sctp_wspace(asoc)) { |
| 1678 | err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); |
| 1679 | if (err) |
| 1680 | goto out_free; |
| 1681 | } |
| 1682 | |
| 1683 | /* If an address is passed with the sendto/sendmsg call, it is used |
| 1684 | * to override the primary destination address in the TCP model, or |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1685 | * when SCTP_ADDR_OVER flag is set in the UDP model. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | */ |
| 1687 | if ((sctp_style(sk, TCP) && msg_name) || |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1688 | (sinfo_flags & SCTP_ADDR_OVER)) { |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1689 | chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | if (!chunk_tp) { |
| 1691 | err = -EINVAL; |
| 1692 | goto out_free; |
| 1693 | } |
| 1694 | } else |
| 1695 | chunk_tp = NULL; |
| 1696 | |
| 1697 | /* Auto-connect, if we aren't connected already. */ |
| 1698 | if (sctp_state(asoc, CLOSED)) { |
| 1699 | err = sctp_primitive_ASSOCIATE(asoc, NULL); |
| 1700 | if (err < 0) |
| 1701 | goto out_free; |
| 1702 | SCTP_DEBUG_PRINTK("We associated primitively.\n"); |
| 1703 | } |
| 1704 | |
| 1705 | /* Break the message into multiple chunks of maximum size. */ |
| 1706 | datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len); |
| 1707 | if (!datamsg) { |
| 1708 | err = -ENOMEM; |
| 1709 | goto out_free; |
| 1710 | } |
| 1711 | |
| 1712 | /* Now send the (possibly) fragmented message. */ |
| 1713 | list_for_each(pos, &datamsg->chunks) { |
| 1714 | chunk = list_entry(pos, struct sctp_chunk, frag_list); |
| 1715 | sctp_datamsg_track(chunk); |
| 1716 | |
| 1717 | /* Do accounting for the write space. */ |
| 1718 | sctp_set_owner_w(chunk); |
| 1719 | |
| 1720 | chunk->transport = chunk_tp; |
| 1721 | |
| 1722 | /* Send it to the lower layers. Note: all chunks |
| 1723 | * must either fail or succeed. The lower layer |
| 1724 | * works that way today. Keep it that way or this |
| 1725 | * breaks. |
| 1726 | */ |
| 1727 | err = sctp_primitive_SEND(asoc, chunk); |
| 1728 | /* Did the lower layer accept the chunk? */ |
| 1729 | if (err) |
| 1730 | sctp_chunk_free(chunk); |
| 1731 | SCTP_DEBUG_PRINTK("We sent primitively.\n"); |
| 1732 | } |
| 1733 | |
| 1734 | sctp_datamsg_free(datamsg); |
| 1735 | if (err) |
| 1736 | goto out_free; |
| 1737 | else |
| 1738 | err = msg_len; |
| 1739 | |
| 1740 | /* If we are already past ASSOCIATE, the lower |
| 1741 | * layers are responsible for association cleanup. |
| 1742 | */ |
| 1743 | goto out_unlock; |
| 1744 | |
| 1745 | out_free: |
| 1746 | if (new_asoc) |
| 1747 | sctp_association_free(asoc); |
| 1748 | out_unlock: |
| 1749 | sctp_release_sock(sk); |
| 1750 | |
| 1751 | out_nounlock: |
| 1752 | return sctp_error(sk, msg_flags, err); |
| 1753 | |
| 1754 | #if 0 |
| 1755 | do_sock_err: |
| 1756 | if (msg_len) |
| 1757 | err = msg_len; |
| 1758 | else |
| 1759 | err = sock_error(sk); |
| 1760 | goto out; |
| 1761 | |
| 1762 | do_interrupted: |
| 1763 | if (msg_len) |
| 1764 | err = msg_len; |
| 1765 | goto out; |
| 1766 | #endif /* 0 */ |
| 1767 | } |
| 1768 | |
| 1769 | /* This is an extended version of skb_pull() that removes the data from the |
| 1770 | * start of a skb even when data is spread across the list of skb's in the |
| 1771 | * frag_list. len specifies the total amount of data that needs to be removed. |
| 1772 | * when 'len' bytes could be removed from the skb, it returns 0. |
| 1773 | * If 'len' exceeds the total skb length, it returns the no. of bytes that |
| 1774 | * could not be removed. |
| 1775 | */ |
| 1776 | static int sctp_skb_pull(struct sk_buff *skb, int len) |
| 1777 | { |
| 1778 | struct sk_buff *list; |
| 1779 | int skb_len = skb_headlen(skb); |
| 1780 | int rlen; |
| 1781 | |
| 1782 | if (len <= skb_len) { |
| 1783 | __skb_pull(skb, len); |
| 1784 | return 0; |
| 1785 | } |
| 1786 | len -= skb_len; |
| 1787 | __skb_pull(skb, skb_len); |
| 1788 | |
| 1789 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) { |
| 1790 | rlen = sctp_skb_pull(list, len); |
| 1791 | skb->len -= (len-rlen); |
| 1792 | skb->data_len -= (len-rlen); |
| 1793 | |
| 1794 | if (!rlen) |
| 1795 | return 0; |
| 1796 | |
| 1797 | len = rlen; |
| 1798 | } |
| 1799 | |
| 1800 | return len; |
| 1801 | } |
| 1802 | |
| 1803 | /* API 3.1.3 recvmsg() - UDP Style Syntax |
| 1804 | * |
| 1805 | * ssize_t recvmsg(int socket, struct msghdr *message, |
| 1806 | * int flags); |
| 1807 | * |
| 1808 | * socket - the socket descriptor of the endpoint. |
| 1809 | * message - pointer to the msghdr structure which contains a single |
| 1810 | * user message and possibly some ancillary data. |
| 1811 | * |
| 1812 | * See Section 5 for complete description of the data |
| 1813 | * structures. |
| 1814 | * |
| 1815 | * flags - flags sent or received with the user message, see Section |
| 1816 | * 5 for complete description of the flags. |
| 1817 | */ |
| 1818 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *); |
| 1819 | |
| 1820 | SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 1821 | struct msghdr *msg, size_t len, int noblock, |
| 1822 | int flags, int *addr_len) |
| 1823 | { |
| 1824 | struct sctp_ulpevent *event = NULL; |
| 1825 | struct sctp_sock *sp = sctp_sk(sk); |
| 1826 | struct sk_buff *skb; |
| 1827 | int copied; |
| 1828 | int err = 0; |
| 1829 | int skb_len; |
| 1830 | |
| 1831 | SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: " |
| 1832 | "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg, |
| 1833 | "len", len, "knoblauch", noblock, |
| 1834 | "flags", flags, "addr_len", addr_len); |
| 1835 | |
| 1836 | sctp_lock_sock(sk); |
| 1837 | |
| 1838 | if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) { |
| 1839 | err = -ENOTCONN; |
| 1840 | goto out; |
| 1841 | } |
| 1842 | |
| 1843 | skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); |
| 1844 | if (!skb) |
| 1845 | goto out; |
| 1846 | |
| 1847 | /* Get the total length of the skb including any skb's in the |
| 1848 | * frag_list. |
| 1849 | */ |
| 1850 | skb_len = skb->len; |
| 1851 | |
| 1852 | copied = skb_len; |
| 1853 | if (copied > len) |
| 1854 | copied = len; |
| 1855 | |
| 1856 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1857 | |
| 1858 | event = sctp_skb2event(skb); |
| 1859 | |
| 1860 | if (err) |
| 1861 | goto out_free; |
| 1862 | |
| 1863 | sock_recv_timestamp(msg, sk, skb); |
| 1864 | if (sctp_ulpevent_is_notification(event)) { |
| 1865 | msg->msg_flags |= MSG_NOTIFICATION; |
| 1866 | sp->pf->event_msgname(event, msg->msg_name, addr_len); |
| 1867 | } else { |
| 1868 | sp->pf->skb_msgname(skb, msg->msg_name, addr_len); |
| 1869 | } |
| 1870 | |
| 1871 | /* Check if we allow SCTP_SNDRCVINFO. */ |
| 1872 | if (sp->subscribe.sctp_data_io_event) |
| 1873 | sctp_ulpevent_read_sndrcvinfo(event, msg); |
| 1874 | #if 0 |
| 1875 | /* FIXME: we should be calling IP/IPv6 layers. */ |
| 1876 | if (sk->sk_protinfo.af_inet.cmsg_flags) |
| 1877 | ip_cmsg_recv(msg, skb); |
| 1878 | #endif |
| 1879 | |
| 1880 | err = copied; |
| 1881 | |
| 1882 | /* If skb's length exceeds the user's buffer, update the skb and |
| 1883 | * push it back to the receive_queue so that the next call to |
| 1884 | * recvmsg() will return the remaining data. Don't set MSG_EOR. |
| 1885 | */ |
| 1886 | if (skb_len > copied) { |
| 1887 | msg->msg_flags &= ~MSG_EOR; |
| 1888 | if (flags & MSG_PEEK) |
| 1889 | goto out_free; |
| 1890 | sctp_skb_pull(skb, copied); |
| 1891 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1892 | |
| 1893 | /* When only partial message is copied to the user, increase |
| 1894 | * rwnd by that amount. If all the data in the skb is read, |
| 1895 | * rwnd is updated when the event is freed. |
| 1896 | */ |
| 1897 | sctp_assoc_rwnd_increase(event->asoc, copied); |
| 1898 | goto out; |
| 1899 | } else if ((event->msg_flags & MSG_NOTIFICATION) || |
| 1900 | (event->msg_flags & MSG_EOR)) |
| 1901 | msg->msg_flags |= MSG_EOR; |
| 1902 | else |
| 1903 | msg->msg_flags &= ~MSG_EOR; |
| 1904 | |
| 1905 | out_free: |
| 1906 | if (flags & MSG_PEEK) { |
| 1907 | /* Release the skb reference acquired after peeking the skb in |
| 1908 | * sctp_skb_recv_datagram(). |
| 1909 | */ |
| 1910 | kfree_skb(skb); |
| 1911 | } else { |
| 1912 | /* Free the event which includes releasing the reference to |
| 1913 | * the owner of the skb, freeing the skb and updating the |
| 1914 | * rwnd. |
| 1915 | */ |
| 1916 | sctp_ulpevent_free(event); |
| 1917 | } |
| 1918 | out: |
| 1919 | sctp_release_sock(sk); |
| 1920 | return err; |
| 1921 | } |
| 1922 | |
| 1923 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 1924 | * |
| 1925 | * This option is a on/off flag. If enabled no SCTP message |
| 1926 | * fragmentation will be performed. Instead if a message being sent |
| 1927 | * exceeds the current PMTU size, the message will NOT be sent and |
| 1928 | * instead a error will be indicated to the user. |
| 1929 | */ |
| 1930 | static int sctp_setsockopt_disable_fragments(struct sock *sk, |
| 1931 | char __user *optval, int optlen) |
| 1932 | { |
| 1933 | int val; |
| 1934 | |
| 1935 | if (optlen < sizeof(int)) |
| 1936 | return -EINVAL; |
| 1937 | |
| 1938 | if (get_user(val, (int __user *)optval)) |
| 1939 | return -EFAULT; |
| 1940 | |
| 1941 | sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; |
| 1942 | |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, |
| 1947 | int optlen) |
| 1948 | { |
| 1949 | if (optlen != sizeof(struct sctp_event_subscribe)) |
| 1950 | return -EINVAL; |
| 1951 | if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) |
| 1952 | return -EFAULT; |
| 1953 | return 0; |
| 1954 | } |
| 1955 | |
| 1956 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 1957 | * |
| 1958 | * This socket option is applicable to the UDP-style socket only. When |
| 1959 | * set it will cause associations that are idle for more than the |
| 1960 | * specified number of seconds to automatically close. An association |
| 1961 | * being idle is defined an association that has NOT sent or received |
| 1962 | * user data. The special value of '0' indicates that no automatic |
| 1963 | * close of any associations should be performed. The option expects an |
| 1964 | * integer defining the number of seconds of idle time before an |
| 1965 | * association is closed. |
| 1966 | */ |
| 1967 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, |
| 1968 | int optlen) |
| 1969 | { |
| 1970 | struct sctp_sock *sp = sctp_sk(sk); |
| 1971 | |
| 1972 | /* Applicable to UDP-style socket only */ |
| 1973 | if (sctp_style(sk, TCP)) |
| 1974 | return -EOPNOTSUPP; |
| 1975 | if (optlen != sizeof(int)) |
| 1976 | return -EINVAL; |
| 1977 | if (copy_from_user(&sp->autoclose, optval, optlen)) |
| 1978 | return -EFAULT; |
| 1979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 1984 | * |
| 1985 | * Applications can enable or disable heartbeats for any peer address of |
| 1986 | * an association, modify an address's heartbeat interval, force a |
| 1987 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 1988 | * number of retransmissions sent before an address is considered |
| 1989 | * unreachable. The following structure is used to access and modify an |
| 1990 | * address's parameters: |
| 1991 | * |
| 1992 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 1993 | * sctp_assoc_t spp_assoc_id; |
| 1994 | * struct sockaddr_storage spp_address; |
| 1995 | * uint32_t spp_hbinterval; |
| 1996 | * uint16_t spp_pathmaxrxt; |
| 1997 | * uint32_t spp_pathmtu; |
| 1998 | * uint32_t spp_sackdelay; |
| 1999 | * uint32_t spp_flags; |
| 2000 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2002 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 2003 | * application, and identifies the association for |
| 2004 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | * spp_address - This specifies which address is of interest. |
| 2006 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2007 | * in milliseconds. If a value of zero |
| 2008 | * is present in this field then no changes are to |
| 2009 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | * spp_pathmaxrxt - This contains the maximum number of |
| 2011 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2012 | * considered unreachable. If a value of zero |
| 2013 | * is present in this field then no changes are to |
| 2014 | * be made to this parameter. |
| 2015 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 2016 | * specified here will be the "fixed" path mtu. |
| 2017 | * Note that if the spp_address field is empty |
| 2018 | * then all associations on this address will |
| 2019 | * have this fixed path mtu set upon them. |
| 2020 | * |
| 2021 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 2022 | * the number of milliseconds that sacks will be delayed |
| 2023 | * for. This value will apply to all addresses of an |
| 2024 | * association if the spp_address field is empty. Note |
| 2025 | * also, that if delayed sack is enabled and this |
| 2026 | * value is set to 0, no change is made to the last |
| 2027 | * recorded delayed sack timer value. |
| 2028 | * |
| 2029 | * spp_flags - These flags are used to control various features |
| 2030 | * on an association. The flag field may contain |
| 2031 | * zero or more of the following options. |
| 2032 | * |
| 2033 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 2034 | * specified address. Note that if the address |
| 2035 | * field is empty all addresses for the association |
| 2036 | * have heartbeats enabled upon them. |
| 2037 | * |
| 2038 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 2039 | * speicifed address. Note that if the address |
| 2040 | * field is empty all addresses for the association |
| 2041 | * will have their heartbeats disabled. Note also |
| 2042 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 2043 | * mutually exclusive, only one of these two should |
| 2044 | * be specified. Enabling both fields will have |
| 2045 | * undetermined results. |
| 2046 | * |
| 2047 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 2048 | * to be made immediately. |
| 2049 | * |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2050 | * SPP_HB_TIME_IS_ZERO - Specify's that the time for |
| 2051 | * heartbeat delayis to be set to the value of 0 |
| 2052 | * milliseconds. |
| 2053 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2054 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 2055 | * discovery upon the specified address. Note that |
| 2056 | * if the address feild is empty then all addresses |
| 2057 | * on the association are effected. |
| 2058 | * |
| 2059 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 2060 | * discovery upon the specified address. Note that |
| 2061 | * if the address feild is empty then all addresses |
| 2062 | * on the association are effected. Not also that |
| 2063 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 2064 | * exclusive. Enabling both will have undetermined |
| 2065 | * results. |
| 2066 | * |
| 2067 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 2068 | * on delayed sack. The time specified in spp_sackdelay |
| 2069 | * is used to specify the sack delay for this address. Note |
| 2070 | * that if spp_address is empty then all addresses will |
| 2071 | * enable delayed sack and take on the sack delay |
| 2072 | * value specified in spp_sackdelay. |
| 2073 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 2074 | * off delayed sack. If the spp_address field is blank then |
| 2075 | * delayed sack is disabled for the entire association. Note |
| 2076 | * also that this field is mutually exclusive to |
| 2077 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 2078 | * results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | */ |
Adrian Bunk | 1616436 | 2006-09-18 00:40:38 -0700 | [diff] [blame] | 2080 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, |
| 2081 | struct sctp_transport *trans, |
| 2082 | struct sctp_association *asoc, |
| 2083 | struct sctp_sock *sp, |
| 2084 | int hb_change, |
| 2085 | int pmtud_change, |
| 2086 | int sackdelay_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | int error; |
| 2089 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2090 | if (params->spp_flags & SPP_HB_DEMAND && trans) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans); |
| 2092 | if (error) |
| 2093 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2096 | /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of |
| 2097 | * this field is ignored. Note also that a value of zero indicates |
| 2098 | * the current setting should be left unchanged. |
| 2099 | */ |
| 2100 | if (params->spp_flags & SPP_HB_ENABLE) { |
| 2101 | |
| 2102 | /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is |
| 2103 | * set. This lets us use 0 value when this flag |
| 2104 | * is set. |
| 2105 | */ |
| 2106 | if (params->spp_flags & SPP_HB_TIME_IS_ZERO) |
| 2107 | params->spp_hbinterval = 0; |
| 2108 | |
| 2109 | if (params->spp_hbinterval || |
| 2110 | (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { |
| 2111 | if (trans) { |
| 2112 | trans->hbinterval = |
| 2113 | msecs_to_jiffies(params->spp_hbinterval); |
| 2114 | } else if (asoc) { |
| 2115 | asoc->hbinterval = |
| 2116 | msecs_to_jiffies(params->spp_hbinterval); |
| 2117 | } else { |
| 2118 | sp->hbinterval = params->spp_hbinterval; |
| 2119 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (hb_change) { |
| 2124 | if (trans) { |
| 2125 | trans->param_flags = |
| 2126 | (trans->param_flags & ~SPP_HB) | hb_change; |
| 2127 | } else if (asoc) { |
| 2128 | asoc->param_flags = |
| 2129 | (asoc->param_flags & ~SPP_HB) | hb_change; |
| 2130 | } else { |
| 2131 | sp->param_flags = |
| 2132 | (sp->param_flags & ~SPP_HB) | hb_change; |
| 2133 | } |
| 2134 | } |
| 2135 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2136 | /* When Path MTU discovery is disabled the value specified here will |
| 2137 | * be the "fixed" path mtu (i.e. the value of the spp_flags field must |
| 2138 | * include the flag SPP_PMTUD_DISABLE for this field to have any |
| 2139 | * effect). |
| 2140 | */ |
| 2141 | if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2142 | if (trans) { |
| 2143 | trans->pathmtu = params->spp_pathmtu; |
| 2144 | sctp_assoc_sync_pmtu(asoc); |
| 2145 | } else if (asoc) { |
| 2146 | asoc->pathmtu = params->spp_pathmtu; |
| 2147 | sctp_frag_point(sp, params->spp_pathmtu); |
| 2148 | } else { |
| 2149 | sp->pathmtu = params->spp_pathmtu; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | if (pmtud_change) { |
| 2154 | if (trans) { |
| 2155 | int update = (trans->param_flags & SPP_PMTUD_DISABLE) && |
| 2156 | (params->spp_flags & SPP_PMTUD_ENABLE); |
| 2157 | trans->param_flags = |
| 2158 | (trans->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2159 | if (update) { |
| 2160 | sctp_transport_pmtu(trans); |
| 2161 | sctp_assoc_sync_pmtu(asoc); |
| 2162 | } |
| 2163 | } else if (asoc) { |
| 2164 | asoc->param_flags = |
| 2165 | (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2166 | } else { |
| 2167 | sp->param_flags = |
| 2168 | (sp->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2169 | } |
| 2170 | } |
| 2171 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2172 | /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the |
| 2173 | * value of this field is ignored. Note also that a value of zero |
| 2174 | * indicates the current setting should be left unchanged. |
| 2175 | */ |
| 2176 | if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2177 | if (trans) { |
| 2178 | trans->sackdelay = |
| 2179 | msecs_to_jiffies(params->spp_sackdelay); |
| 2180 | } else if (asoc) { |
| 2181 | asoc->sackdelay = |
| 2182 | msecs_to_jiffies(params->spp_sackdelay); |
| 2183 | } else { |
| 2184 | sp->sackdelay = params->spp_sackdelay; |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | if (sackdelay_change) { |
| 2189 | if (trans) { |
| 2190 | trans->param_flags = |
| 2191 | (trans->param_flags & ~SPP_SACKDELAY) | |
| 2192 | sackdelay_change; |
| 2193 | } else if (asoc) { |
| 2194 | asoc->param_flags = |
| 2195 | (asoc->param_flags & ~SPP_SACKDELAY) | |
| 2196 | sackdelay_change; |
| 2197 | } else { |
| 2198 | sp->param_flags = |
| 2199 | (sp->param_flags & ~SPP_SACKDELAY) | |
| 2200 | sackdelay_change; |
| 2201 | } |
| 2202 | } |
| 2203 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2204 | /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value |
| 2205 | * of this field is ignored. Note also that a value of zero |
| 2206 | * indicates the current setting should be left unchanged. |
| 2207 | */ |
| 2208 | if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2209 | if (trans) { |
| 2210 | trans->pathmaxrxt = params->spp_pathmaxrxt; |
| 2211 | } else if (asoc) { |
| 2212 | asoc->pathmaxrxt = params->spp_pathmaxrxt; |
| 2213 | } else { |
| 2214 | sp->pathmaxrxt = params->spp_pathmaxrxt; |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, |
| 2222 | char __user *optval, int optlen) |
| 2223 | { |
| 2224 | struct sctp_paddrparams params; |
| 2225 | struct sctp_transport *trans = NULL; |
| 2226 | struct sctp_association *asoc = NULL; |
| 2227 | struct sctp_sock *sp = sctp_sk(sk); |
| 2228 | int error; |
| 2229 | int hb_change, pmtud_change, sackdelay_change; |
| 2230 | |
| 2231 | if (optlen != sizeof(struct sctp_paddrparams)) |
| 2232 | return - EINVAL; |
| 2233 | |
| 2234 | if (copy_from_user(¶ms, optval, optlen)) |
| 2235 | return -EFAULT; |
| 2236 | |
| 2237 | /* Validate flags and value parameters. */ |
| 2238 | hb_change = params.spp_flags & SPP_HB; |
| 2239 | pmtud_change = params.spp_flags & SPP_PMTUD; |
| 2240 | sackdelay_change = params.spp_flags & SPP_SACKDELAY; |
| 2241 | |
| 2242 | if (hb_change == SPP_HB || |
| 2243 | pmtud_change == SPP_PMTUD || |
| 2244 | sackdelay_change == SPP_SACKDELAY || |
| 2245 | params.spp_sackdelay > 500 || |
| 2246 | (params.spp_pathmtu |
| 2247 | && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) |
| 2248 | return -EINVAL; |
| 2249 | |
| 2250 | /* If an address other than INADDR_ANY is specified, and |
| 2251 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | */ |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2253 | if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) { |
| 2254 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 2255 | params.spp_assoc_id); |
| 2256 | if (!trans) |
| 2257 | return -EINVAL; |
| 2258 | } |
| 2259 | |
| 2260 | /* Get association, if assoc_id != 0 and the socket is a one |
| 2261 | * to many style socket, and an association was not found, then |
| 2262 | * the id was invalid. |
| 2263 | */ |
| 2264 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
| 2265 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) |
| 2266 | return -EINVAL; |
| 2267 | |
| 2268 | /* Heartbeat demand can only be sent on a transport or |
| 2269 | * association, but not a socket. |
| 2270 | */ |
| 2271 | if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) |
| 2272 | return -EINVAL; |
| 2273 | |
| 2274 | /* Process parameters. */ |
| 2275 | error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2276 | hb_change, pmtud_change, |
| 2277 | sackdelay_change); |
| 2278 | |
| 2279 | if (error) |
| 2280 | return error; |
| 2281 | |
| 2282 | /* If changes are for association, also apply parameters to each |
| 2283 | * transport. |
| 2284 | */ |
| 2285 | if (!trans && asoc) { |
| 2286 | struct list_head *pos; |
| 2287 | |
| 2288 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 2289 | trans = list_entry(pos, struct sctp_transport, |
| 2290 | transports); |
| 2291 | sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2292 | hb_change, pmtud_change, |
| 2293 | sackdelay_change); |
| 2294 | } |
| 2295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 2300 | /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2301 | * |
| 2302 | * This options will get or set the delayed ack timer. The time is set |
| 2303 | * in milliseconds. If the assoc_id is 0, then this sets or gets the |
| 2304 | * endpoints default delayed ack timer value. If the assoc_id field is |
| 2305 | * non-zero, then the set or get effects the specified association. |
| 2306 | * |
| 2307 | * struct sctp_assoc_value { |
| 2308 | * sctp_assoc_t assoc_id; |
| 2309 | * uint32_t assoc_value; |
| 2310 | * }; |
| 2311 | * |
| 2312 | * assoc_id - This parameter, indicates which association the |
| 2313 | * user is preforming an action upon. Note that if |
| 2314 | * this field's value is zero then the endpoints |
| 2315 | * default value is changed (effecting future |
| 2316 | * associations only). |
| 2317 | * |
| 2318 | * assoc_value - This parameter contains the number of milliseconds |
| 2319 | * that the user is requesting the delayed ACK timer |
| 2320 | * be set to. Note that this value is defined in |
| 2321 | * the standard to be between 200 and 500 milliseconds. |
| 2322 | * |
| 2323 | * Note: a value of zero will leave the value alone, |
| 2324 | * but disable SACK delay. A non-zero value will also |
| 2325 | * enable SACK delay. |
| 2326 | */ |
| 2327 | |
| 2328 | static int sctp_setsockopt_delayed_ack_time(struct sock *sk, |
| 2329 | char __user *optval, int optlen) |
| 2330 | { |
| 2331 | struct sctp_assoc_value params; |
| 2332 | struct sctp_transport *trans = NULL; |
| 2333 | struct sctp_association *asoc = NULL; |
| 2334 | struct sctp_sock *sp = sctp_sk(sk); |
| 2335 | |
| 2336 | if (optlen != sizeof(struct sctp_assoc_value)) |
| 2337 | return - EINVAL; |
| 2338 | |
| 2339 | if (copy_from_user(¶ms, optval, optlen)) |
| 2340 | return -EFAULT; |
| 2341 | |
| 2342 | /* Validate value parameter. */ |
| 2343 | if (params.assoc_value > 500) |
| 2344 | return -EINVAL; |
| 2345 | |
| 2346 | /* Get association, if assoc_id != 0 and the socket is a one |
| 2347 | * to many style socket, and an association was not found, then |
| 2348 | * the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2349 | */ |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2350 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 2351 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) |
| 2352 | return -EINVAL; |
| 2353 | |
| 2354 | if (params.assoc_value) { |
| 2355 | if (asoc) { |
| 2356 | asoc->sackdelay = |
| 2357 | msecs_to_jiffies(params.assoc_value); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2358 | asoc->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2359 | (asoc->param_flags & ~SPP_SACKDELAY) | |
| 2360 | SPP_SACKDELAY_ENABLE; |
| 2361 | } else { |
| 2362 | sp->sackdelay = params.assoc_value; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2363 | sp->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2364 | (sp->param_flags & ~SPP_SACKDELAY) | |
| 2365 | SPP_SACKDELAY_ENABLE; |
| 2366 | } |
| 2367 | } else { |
| 2368 | if (asoc) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2369 | asoc->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2370 | (asoc->param_flags & ~SPP_SACKDELAY) | |
| 2371 | SPP_SACKDELAY_DISABLE; |
| 2372 | } else { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2373 | sp->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2374 | (sp->param_flags & ~SPP_SACKDELAY) | |
| 2375 | SPP_SACKDELAY_DISABLE; |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | /* If change is for association, also apply to each transport. */ |
| 2380 | if (asoc) { |
| 2381 | struct list_head *pos; |
| 2382 | |
| 2383 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 2384 | trans = list_entry(pos, struct sctp_transport, |
| 2385 | transports); |
| 2386 | if (params.assoc_value) { |
| 2387 | trans->sackdelay = |
| 2388 | msecs_to_jiffies(params.assoc_value); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2389 | trans->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2390 | (trans->param_flags & ~SPP_SACKDELAY) | |
| 2391 | SPP_SACKDELAY_ENABLE; |
| 2392 | } else { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2393 | trans->param_flags = |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2394 | (trans->param_flags & ~SPP_SACKDELAY) | |
| 2395 | SPP_SACKDELAY_DISABLE; |
| 2396 | } |
| 2397 | } |
| 2398 | } |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2399 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2400 | return 0; |
| 2401 | } |
| 2402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 2404 | * |
| 2405 | * Applications can specify protocol parameters for the default association |
| 2406 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 2407 | * is SCTP_INITMSG. |
| 2408 | * |
| 2409 | * Setting initialization parameters is effective only on an unconnected |
| 2410 | * socket (for UDP-style sockets only future associations are effected |
| 2411 | * by the change). With TCP-style sockets, this option is inherited by |
| 2412 | * sockets derived from a listener socket. |
| 2413 | */ |
| 2414 | static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen) |
| 2415 | { |
| 2416 | struct sctp_initmsg sinit; |
| 2417 | struct sctp_sock *sp = sctp_sk(sk); |
| 2418 | |
| 2419 | if (optlen != sizeof(struct sctp_initmsg)) |
| 2420 | return -EINVAL; |
| 2421 | if (copy_from_user(&sinit, optval, optlen)) |
| 2422 | return -EFAULT; |
| 2423 | |
| 2424 | if (sinit.sinit_num_ostreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2425 | sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | if (sinit.sinit_max_instreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2427 | sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | if (sinit.sinit_max_attempts) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2429 | sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | if (sinit.sinit_max_init_timeo) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2431 | sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 2438 | * |
| 2439 | * Applications that wish to use the sendto() system call may wish to |
| 2440 | * specify a default set of parameters that would normally be supplied |
| 2441 | * through the inclusion of ancillary data. This socket option allows |
| 2442 | * such an application to set the default sctp_sndrcvinfo structure. |
| 2443 | * The application that wishes to use this socket option simply passes |
| 2444 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 2445 | * 5.2.2) The input parameters accepted by this call include |
| 2446 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 2447 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 2448 | * to this call if the caller is using the UDP model. |
| 2449 | */ |
| 2450 | static int sctp_setsockopt_default_send_param(struct sock *sk, |
| 2451 | char __user *optval, int optlen) |
| 2452 | { |
| 2453 | struct sctp_sndrcvinfo info; |
| 2454 | struct sctp_association *asoc; |
| 2455 | struct sctp_sock *sp = sctp_sk(sk); |
| 2456 | |
| 2457 | if (optlen != sizeof(struct sctp_sndrcvinfo)) |
| 2458 | return -EINVAL; |
| 2459 | if (copy_from_user(&info, optval, optlen)) |
| 2460 | return -EFAULT; |
| 2461 | |
| 2462 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
| 2463 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
| 2464 | return -EINVAL; |
| 2465 | |
| 2466 | if (asoc) { |
| 2467 | asoc->default_stream = info.sinfo_stream; |
| 2468 | asoc->default_flags = info.sinfo_flags; |
| 2469 | asoc->default_ppid = info.sinfo_ppid; |
| 2470 | asoc->default_context = info.sinfo_context; |
| 2471 | asoc->default_timetolive = info.sinfo_timetolive; |
| 2472 | } else { |
| 2473 | sp->default_stream = info.sinfo_stream; |
| 2474 | sp->default_flags = info.sinfo_flags; |
| 2475 | sp->default_ppid = info.sinfo_ppid; |
| 2476 | sp->default_context = info.sinfo_context; |
| 2477 | sp->default_timetolive = info.sinfo_timetolive; |
| 2478 | } |
| 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 2484 | * |
| 2485 | * Requests that the local SCTP stack use the enclosed peer address as |
| 2486 | * the association primary. The enclosed address must be one of the |
| 2487 | * association peer's addresses. |
| 2488 | */ |
| 2489 | static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, |
| 2490 | int optlen) |
| 2491 | { |
| 2492 | struct sctp_prim prim; |
| 2493 | struct sctp_transport *trans; |
| 2494 | |
| 2495 | if (optlen != sizeof(struct sctp_prim)) |
| 2496 | return -EINVAL; |
| 2497 | |
| 2498 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) |
| 2499 | return -EFAULT; |
| 2500 | |
| 2501 | trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); |
| 2502 | if (!trans) |
| 2503 | return -EINVAL; |
| 2504 | |
| 2505 | sctp_assoc_set_primary(trans->asoc, trans); |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * 7.1.5 SCTP_NODELAY |
| 2512 | * |
| 2513 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 2514 | * generally sent as soon as possible and no unnecessary delays are |
| 2515 | * introduced, at the cost of more packets in the network. Expects an |
| 2516 | * integer boolean flag. |
| 2517 | */ |
| 2518 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, |
| 2519 | int optlen) |
| 2520 | { |
| 2521 | int val; |
| 2522 | |
| 2523 | if (optlen < sizeof(int)) |
| 2524 | return -EINVAL; |
| 2525 | if (get_user(val, (int __user *)optval)) |
| 2526 | return -EFAULT; |
| 2527 | |
| 2528 | sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * |
| 2534 | * 7.1.1 SCTP_RTOINFO |
| 2535 | * |
| 2536 | * The protocol parameters used to initialize and bound retransmission |
| 2537 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 2538 | * and modify these parameters. |
| 2539 | * All parameters are time values, in milliseconds. A value of 0, when |
| 2540 | * modifying the parameters, indicates that the current value should not |
| 2541 | * be changed. |
| 2542 | * |
| 2543 | */ |
| 2544 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) { |
| 2545 | struct sctp_rtoinfo rtoinfo; |
| 2546 | struct sctp_association *asoc; |
| 2547 | |
| 2548 | if (optlen != sizeof (struct sctp_rtoinfo)) |
| 2549 | return -EINVAL; |
| 2550 | |
| 2551 | if (copy_from_user(&rtoinfo, optval, optlen)) |
| 2552 | return -EFAULT; |
| 2553 | |
| 2554 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 2555 | |
| 2556 | /* Set the values to the specific association */ |
| 2557 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
| 2558 | return -EINVAL; |
| 2559 | |
| 2560 | if (asoc) { |
| 2561 | if (rtoinfo.srto_initial != 0) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2562 | asoc->rto_initial = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | msecs_to_jiffies(rtoinfo.srto_initial); |
| 2564 | if (rtoinfo.srto_max != 0) |
| 2565 | asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); |
| 2566 | if (rtoinfo.srto_min != 0) |
| 2567 | asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min); |
| 2568 | } else { |
| 2569 | /* If there is no association or the association-id = 0 |
| 2570 | * set the values to the endpoint. |
| 2571 | */ |
| 2572 | struct sctp_sock *sp = sctp_sk(sk); |
| 2573 | |
| 2574 | if (rtoinfo.srto_initial != 0) |
| 2575 | sp->rtoinfo.srto_initial = rtoinfo.srto_initial; |
| 2576 | if (rtoinfo.srto_max != 0) |
| 2577 | sp->rtoinfo.srto_max = rtoinfo.srto_max; |
| 2578 | if (rtoinfo.srto_min != 0) |
| 2579 | sp->rtoinfo.srto_min = rtoinfo.srto_min; |
| 2580 | } |
| 2581 | |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
| 2585 | /* |
| 2586 | * |
| 2587 | * 7.1.2 SCTP_ASSOCINFO |
| 2588 | * |
| 2589 | * This option is used to tune the the maximum retransmission attempts |
| 2590 | * of the association. |
| 2591 | * Returns an error if the new association retransmission value is |
| 2592 | * greater than the sum of the retransmission value of the peer. |
| 2593 | * See [SCTP] for more information. |
| 2594 | * |
| 2595 | */ |
| 2596 | static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen) |
| 2597 | { |
| 2598 | |
| 2599 | struct sctp_assocparams assocparams; |
| 2600 | struct sctp_association *asoc; |
| 2601 | |
| 2602 | if (optlen != sizeof(struct sctp_assocparams)) |
| 2603 | return -EINVAL; |
| 2604 | if (copy_from_user(&assocparams, optval, optlen)) |
| 2605 | return -EFAULT; |
| 2606 | |
| 2607 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 2608 | |
| 2609 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
| 2610 | return -EINVAL; |
| 2611 | |
| 2612 | /* Set the values to the specific association */ |
| 2613 | if (asoc) { |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2614 | if (assocparams.sasoc_asocmaxrxt != 0) { |
| 2615 | __u32 path_sum = 0; |
| 2616 | int paths = 0; |
| 2617 | struct list_head *pos; |
| 2618 | struct sctp_transport *peer_addr; |
| 2619 | |
| 2620 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 2621 | peer_addr = list_entry(pos, |
| 2622 | struct sctp_transport, |
| 2623 | transports); |
| 2624 | path_sum += peer_addr->pathmaxrxt; |
| 2625 | paths++; |
| 2626 | } |
| 2627 | |
| 2628 | /* Only validate asocmaxrxt if we have more then |
| 2629 | * one path/transport. We do this because path |
| 2630 | * retransmissions are only counted when we have more |
| 2631 | * then one path. |
| 2632 | */ |
| 2633 | if (paths > 1 && |
| 2634 | assocparams.sasoc_asocmaxrxt > path_sum) |
| 2635 | return -EINVAL; |
| 2636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | asoc->max_retrans = assocparams.sasoc_asocmaxrxt; |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | if (assocparams.sasoc_cookie_life != 0) { |
| 2641 | asoc->cookie_life.tv_sec = |
| 2642 | assocparams.sasoc_cookie_life / 1000; |
| 2643 | asoc->cookie_life.tv_usec = |
| 2644 | (assocparams.sasoc_cookie_life % 1000) |
| 2645 | * 1000; |
| 2646 | } |
| 2647 | } else { |
| 2648 | /* Set the values to the endpoint */ |
| 2649 | struct sctp_sock *sp = sctp_sk(sk); |
| 2650 | |
| 2651 | if (assocparams.sasoc_asocmaxrxt != 0) |
| 2652 | sp->assocparams.sasoc_asocmaxrxt = |
| 2653 | assocparams.sasoc_asocmaxrxt; |
| 2654 | if (assocparams.sasoc_cookie_life != 0) |
| 2655 | sp->assocparams.sasoc_cookie_life = |
| 2656 | assocparams.sasoc_cookie_life; |
| 2657 | } |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* |
| 2662 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 2663 | * |
| 2664 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 2665 | * addresses. If this option is turned on and the socket is type |
| 2666 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 2667 | * If this option is turned off, then no mapping will be done of V4 |
| 2668 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 2669 | * addresses on the socket. |
| 2670 | */ |
| 2671 | static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen) |
| 2672 | { |
| 2673 | int val; |
| 2674 | struct sctp_sock *sp = sctp_sk(sk); |
| 2675 | |
| 2676 | if (optlen < sizeof(int)) |
| 2677 | return -EINVAL; |
| 2678 | if (get_user(val, (int __user *)optval)) |
| 2679 | return -EFAULT; |
| 2680 | if (val) |
| 2681 | sp->v4mapped = 1; |
| 2682 | else |
| 2683 | sp->v4mapped = 0; |
| 2684 | |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG) |
| 2690 | * |
| 2691 | * This socket option specifies the maximum size to put in any outgoing |
| 2692 | * SCTP chunk. If a message is larger than this size it will be |
| 2693 | * fragmented by SCTP into the specified size. Note that the underlying |
| 2694 | * SCTP implementation may fragment into smaller sized chunks when the |
| 2695 | * PMTU of the underlying association is smaller than the value set by |
| 2696 | * the user. |
| 2697 | */ |
| 2698 | static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen) |
| 2699 | { |
| 2700 | struct sctp_association *asoc; |
| 2701 | struct list_head *pos; |
| 2702 | struct sctp_sock *sp = sctp_sk(sk); |
| 2703 | int val; |
| 2704 | |
| 2705 | if (optlen < sizeof(int)) |
| 2706 | return -EINVAL; |
| 2707 | if (get_user(val, (int __user *)optval)) |
| 2708 | return -EFAULT; |
Ivan Skytte Jorgensen | 96a3399 | 2005-10-28 15:36:12 -0700 | [diff] [blame] | 2709 | if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | return -EINVAL; |
| 2711 | sp->user_frag = val; |
| 2712 | |
Ivan Skytte Jorgensen | 96a3399 | 2005-10-28 15:36:12 -0700 | [diff] [blame] | 2713 | /* Update the frag_point of the existing associations. */ |
| 2714 | list_for_each(pos, &(sp->ep->asocs)) { |
| 2715 | asoc = list_entry(pos, struct sctp_association, asocs); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2716 | asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | return 0; |
| 2720 | } |
| 2721 | |
| 2722 | |
| 2723 | /* |
| 2724 | * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) |
| 2725 | * |
| 2726 | * Requests that the peer mark the enclosed address as the association |
| 2727 | * primary. The enclosed address must be one of the association's |
| 2728 | * locally bound addresses. The following structure is used to make a |
| 2729 | * set primary request: |
| 2730 | */ |
| 2731 | static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, |
| 2732 | int optlen) |
| 2733 | { |
| 2734 | struct sctp_sock *sp; |
| 2735 | struct sctp_endpoint *ep; |
| 2736 | struct sctp_association *asoc = NULL; |
| 2737 | struct sctp_setpeerprim prim; |
| 2738 | struct sctp_chunk *chunk; |
| 2739 | int err; |
| 2740 | |
| 2741 | sp = sctp_sk(sk); |
| 2742 | ep = sp->ep; |
| 2743 | |
| 2744 | if (!sctp_addip_enable) |
| 2745 | return -EPERM; |
| 2746 | |
| 2747 | if (optlen != sizeof(struct sctp_setpeerprim)) |
| 2748 | return -EINVAL; |
| 2749 | |
| 2750 | if (copy_from_user(&prim, optval, optlen)) |
| 2751 | return -EFAULT; |
| 2752 | |
| 2753 | asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2754 | if (!asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | return -EINVAL; |
| 2756 | |
| 2757 | if (!asoc->peer.asconf_capable) |
| 2758 | return -EPERM; |
| 2759 | |
| 2760 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) |
| 2761 | return -EPERM; |
| 2762 | |
| 2763 | if (!sctp_state(asoc, ESTABLISHED)) |
| 2764 | return -ENOTCONN; |
| 2765 | |
| 2766 | if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) |
| 2767 | return -EADDRNOTAVAIL; |
| 2768 | |
| 2769 | /* Create an ASCONF chunk with SET_PRIMARY parameter */ |
| 2770 | chunk = sctp_make_asconf_set_prim(asoc, |
| 2771 | (union sctp_addr *)&prim.sspp_addr); |
| 2772 | if (!chunk) |
| 2773 | return -ENOMEM; |
| 2774 | |
| 2775 | err = sctp_send_asconf(asoc, chunk); |
| 2776 | |
| 2777 | SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n"); |
| 2778 | |
| 2779 | return err; |
| 2780 | } |
| 2781 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 2782 | static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | int optlen) |
| 2784 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 2785 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 2787 | if (optlen != sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | return -EINVAL; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 2789 | if (copy_from_user(&adaptation, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | return -EFAULT; |
| 2791 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 2792 | sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
| 2794 | return 0; |
| 2795 | } |
| 2796 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 2797 | /* |
| 2798 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 2799 | * |
| 2800 | * The context field in the sctp_sndrcvinfo structure is normally only |
| 2801 | * used when a failed message is retrieved holding the value that was |
| 2802 | * sent down on the actual send call. This option allows the setting of |
| 2803 | * a default context on an association basis that will be received on |
| 2804 | * reading messages from the peer. This is especially helpful in the |
| 2805 | * one-2-many model for an application to keep some reference to an |
| 2806 | * internal state machine that is processing messages on the |
| 2807 | * association. Note that the setting of this value only effects |
| 2808 | * received messages from the peer and does not effect the value that is |
| 2809 | * saved with outbound messages. |
| 2810 | */ |
| 2811 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, |
| 2812 | int optlen) |
| 2813 | { |
| 2814 | struct sctp_assoc_value params; |
| 2815 | struct sctp_sock *sp; |
| 2816 | struct sctp_association *asoc; |
| 2817 | |
| 2818 | if (optlen != sizeof(struct sctp_assoc_value)) |
| 2819 | return -EINVAL; |
| 2820 | if (copy_from_user(¶ms, optval, optlen)) |
| 2821 | return -EFAULT; |
| 2822 | |
| 2823 | sp = sctp_sk(sk); |
| 2824 | |
| 2825 | if (params.assoc_id != 0) { |
| 2826 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 2827 | if (!asoc) |
| 2828 | return -EINVAL; |
| 2829 | asoc->default_rcv_context = params.assoc_value; |
| 2830 | } else { |
| 2831 | sp->default_rcv_context = params.assoc_value; |
| 2832 | } |
| 2833 | |
| 2834 | return 0; |
| 2835 | } |
| 2836 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 2837 | /* |
| 2838 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 2839 | * |
| 2840 | * This options will at a minimum specify if the implementation is doing |
| 2841 | * fragmented interleave. Fragmented interleave, for a one to many |
| 2842 | * socket, is when subsequent calls to receive a message may return |
| 2843 | * parts of messages from different associations. Some implementations |
| 2844 | * may allow you to turn this value on or off. If so, when turned off, |
| 2845 | * no fragment interleave will occur (which will cause a head of line |
| 2846 | * blocking amongst multiple associations sharing the same one to many |
| 2847 | * socket). When this option is turned on, then each receive call may |
| 2848 | * come from a different association (thus the user must receive data |
| 2849 | * with the extended calls (e.g. sctp_recvmsg) to keep track of which |
| 2850 | * association each receive belongs to. |
| 2851 | * |
| 2852 | * This option takes a boolean value. A non-zero value indicates that |
| 2853 | * fragmented interleave is on. A value of zero indicates that |
| 2854 | * fragmented interleave is off. |
| 2855 | * |
| 2856 | * Note that it is important that an implementation that allows this |
| 2857 | * option to be turned on, have it off by default. Otherwise an unaware |
| 2858 | * application using the one to many model may become confused and act |
| 2859 | * incorrectly. |
| 2860 | */ |
| 2861 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, |
| 2862 | char __user *optval, |
| 2863 | int optlen) |
| 2864 | { |
| 2865 | int val; |
| 2866 | |
| 2867 | if (optlen != sizeof(int)) |
| 2868 | return -EINVAL; |
| 2869 | if (get_user(val, (int __user *)optval)) |
| 2870 | return -EFAULT; |
| 2871 | |
| 2872 | sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1; |
| 2873 | |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 2877 | /* |
| 2878 | * 7.1.25. Set or Get the sctp partial delivery point |
| 2879 | * (SCTP_PARTIAL_DELIVERY_POINT) |
| 2880 | * This option will set or get the SCTP partial delivery point. This |
| 2881 | * point is the size of a message where the partial delivery API will be |
| 2882 | * invoked to help free up rwnd space for the peer. Setting this to a |
| 2883 | * lower value will cause partial delivery's to happen more often. The |
| 2884 | * calls argument is an integer that sets or gets the partial delivery |
| 2885 | * point. |
| 2886 | */ |
| 2887 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, |
| 2888 | char __user *optval, |
| 2889 | int optlen) |
| 2890 | { |
| 2891 | u32 val; |
| 2892 | |
| 2893 | if (optlen != sizeof(u32)) |
| 2894 | return -EINVAL; |
| 2895 | if (get_user(val, (int __user *)optval)) |
| 2896 | return -EFAULT; |
| 2897 | |
| 2898 | sctp_sk(sk)->pd_point = val; |
| 2899 | |
| 2900 | return 0; /* is this the right error code? */ |
| 2901 | } |
| 2902 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 2903 | /* |
| 2904 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 2905 | * |
| 2906 | * This option will allow a user to change the maximum burst of packets |
| 2907 | * that can be emitted by this association. Note that the default value |
| 2908 | * is 4, and some implementations may restrict this setting so that it |
| 2909 | * can only be lowered. |
| 2910 | * |
| 2911 | * NOTE: This text doesn't seem right. Do this on a socket basis with |
| 2912 | * future associations inheriting the socket value. |
| 2913 | */ |
| 2914 | static int sctp_setsockopt_maxburst(struct sock *sk, |
| 2915 | char __user *optval, |
| 2916 | int optlen) |
| 2917 | { |
| 2918 | int val; |
| 2919 | |
| 2920 | if (optlen != sizeof(int)) |
| 2921 | return -EINVAL; |
| 2922 | if (get_user(val, (int __user *)optval)) |
| 2923 | return -EFAULT; |
| 2924 | |
| 2925 | if (val < 0) |
| 2926 | return -EINVAL; |
| 2927 | |
| 2928 | sctp_sk(sk)->max_burst = val; |
| 2929 | |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | /* API 6.2 setsockopt(), getsockopt() |
| 2934 | * |
| 2935 | * Applications use setsockopt() and getsockopt() to set or retrieve |
| 2936 | * socket options. Socket options are used to change the default |
| 2937 | * behavior of sockets calls. They are described in Section 7. |
| 2938 | * |
| 2939 | * The syntax is: |
| 2940 | * |
| 2941 | * ret = getsockopt(int sd, int level, int optname, void __user *optval, |
| 2942 | * int __user *optlen); |
| 2943 | * ret = setsockopt(int sd, int level, int optname, const void __user *optval, |
| 2944 | * int optlen); |
| 2945 | * |
| 2946 | * sd - the socket descript. |
| 2947 | * level - set to IPPROTO_SCTP for all SCTP options. |
| 2948 | * optname - the option name. |
| 2949 | * optval - the buffer to store the value of the option. |
| 2950 | * optlen - the size of the buffer. |
| 2951 | */ |
| 2952 | SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname, |
| 2953 | char __user *optval, int optlen) |
| 2954 | { |
| 2955 | int retval = 0; |
| 2956 | |
| 2957 | SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n", |
| 2958 | sk, optname); |
| 2959 | |
| 2960 | /* I can hardly begin to describe how wrong this is. This is |
| 2961 | * so broken as to be worse than useless. The API draft |
| 2962 | * REALLY is NOT helpful here... I am not convinced that the |
| 2963 | * semantics of setsockopt() with a level OTHER THAN SOL_SCTP |
| 2964 | * are at all well-founded. |
| 2965 | */ |
| 2966 | if (level != SOL_SCTP) { |
| 2967 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 2968 | retval = af->setsockopt(sk, level, optname, optval, optlen); |
| 2969 | goto out_nounlock; |
| 2970 | } |
| 2971 | |
| 2972 | sctp_lock_sock(sk); |
| 2973 | |
| 2974 | switch (optname) { |
| 2975 | case SCTP_SOCKOPT_BINDX_ADD: |
| 2976 | /* 'optlen' is the size of the addresses buffer. */ |
| 2977 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 2978 | optlen, SCTP_BINDX_ADD_ADDR); |
| 2979 | break; |
| 2980 | |
| 2981 | case SCTP_SOCKOPT_BINDX_REM: |
| 2982 | /* 'optlen' is the size of the addresses buffer. */ |
| 2983 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 2984 | optlen, SCTP_BINDX_REM_ADDR); |
| 2985 | break; |
| 2986 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 2987 | case SCTP_SOCKOPT_CONNECTX: |
| 2988 | /* 'optlen' is the size of the addresses buffer. */ |
| 2989 | retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval, |
| 2990 | optlen); |
| 2991 | break; |
| 2992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | case SCTP_DISABLE_FRAGMENTS: |
| 2994 | retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); |
| 2995 | break; |
| 2996 | |
| 2997 | case SCTP_EVENTS: |
| 2998 | retval = sctp_setsockopt_events(sk, optval, optlen); |
| 2999 | break; |
| 3000 | |
| 3001 | case SCTP_AUTOCLOSE: |
| 3002 | retval = sctp_setsockopt_autoclose(sk, optval, optlen); |
| 3003 | break; |
| 3004 | |
| 3005 | case SCTP_PEER_ADDR_PARAMS: |
| 3006 | retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); |
| 3007 | break; |
| 3008 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3009 | case SCTP_DELAYED_ACK_TIME: |
| 3010 | retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen); |
| 3011 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3012 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 3013 | retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); |
| 3014 | break; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | case SCTP_INITMSG: |
| 3017 | retval = sctp_setsockopt_initmsg(sk, optval, optlen); |
| 3018 | break; |
| 3019 | case SCTP_DEFAULT_SEND_PARAM: |
| 3020 | retval = sctp_setsockopt_default_send_param(sk, optval, |
| 3021 | optlen); |
| 3022 | break; |
| 3023 | case SCTP_PRIMARY_ADDR: |
| 3024 | retval = sctp_setsockopt_primary_addr(sk, optval, optlen); |
| 3025 | break; |
| 3026 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 3027 | retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); |
| 3028 | break; |
| 3029 | case SCTP_NODELAY: |
| 3030 | retval = sctp_setsockopt_nodelay(sk, optval, optlen); |
| 3031 | break; |
| 3032 | case SCTP_RTOINFO: |
| 3033 | retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); |
| 3034 | break; |
| 3035 | case SCTP_ASSOCINFO: |
| 3036 | retval = sctp_setsockopt_associnfo(sk, optval, optlen); |
| 3037 | break; |
| 3038 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 3039 | retval = sctp_setsockopt_mappedv4(sk, optval, optlen); |
| 3040 | break; |
| 3041 | case SCTP_MAXSEG: |
| 3042 | retval = sctp_setsockopt_maxseg(sk, optval, optlen); |
| 3043 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3044 | case SCTP_ADAPTATION_LAYER: |
| 3045 | retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3047 | case SCTP_CONTEXT: |
| 3048 | retval = sctp_setsockopt_context(sk, optval, optlen); |
| 3049 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3050 | case SCTP_FRAGMENT_INTERLEAVE: |
| 3051 | retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); |
| 3052 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3053 | case SCTP_MAX_BURST: |
| 3054 | retval = sctp_setsockopt_maxburst(sk, optval, optlen); |
| 3055 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | default: |
| 3057 | retval = -ENOPROTOOPT; |
| 3058 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | |
| 3061 | sctp_release_sock(sk); |
| 3062 | |
| 3063 | out_nounlock: |
| 3064 | return retval; |
| 3065 | } |
| 3066 | |
| 3067 | /* API 3.1.6 connect() - UDP Style Syntax |
| 3068 | * |
| 3069 | * An application may use the connect() call in the UDP model to initiate an |
| 3070 | * association without sending data. |
| 3071 | * |
| 3072 | * The syntax is: |
| 3073 | * |
| 3074 | * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); |
| 3075 | * |
| 3076 | * sd: the socket descriptor to have a new association added to. |
| 3077 | * |
| 3078 | * nam: the address structure (either struct sockaddr_in or struct |
| 3079 | * sockaddr_in6 defined in RFC2553 [7]). |
| 3080 | * |
| 3081 | * len: the size of the address. |
| 3082 | */ |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3083 | SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | int addr_len) |
| 3085 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | int err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3087 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | |
| 3089 | sctp_lock_sock(sk); |
| 3090 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3091 | SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n", |
| 3092 | __FUNCTION__, sk, addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3094 | /* Validate addr_len before calling common connect/connectx routine. */ |
| 3095 | af = sctp_get_af_specific(addr->sa_family); |
| 3096 | if (!af || addr_len < af->sockaddr_len) { |
| 3097 | err = -EINVAL; |
| 3098 | } else { |
| 3099 | /* Pass correct addr len to common routine (so it knows there |
| 3100 | * is only one address being passed. |
| 3101 | */ |
| 3102 | err = __sctp_connect(sk, addr, af->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | sctp_release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | return err; |
| 3107 | } |
| 3108 | |
| 3109 | /* FIXME: Write comments. */ |
| 3110 | SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags) |
| 3111 | { |
| 3112 | return -EOPNOTSUPP; /* STUB */ |
| 3113 | } |
| 3114 | |
| 3115 | /* 4.1.4 accept() - TCP Style Syntax |
| 3116 | * |
| 3117 | * Applications use accept() call to remove an established SCTP |
| 3118 | * association from the accept queue of the endpoint. A new socket |
| 3119 | * descriptor will be returned from accept() to represent the newly |
| 3120 | * formed association. |
| 3121 | */ |
| 3122 | SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err) |
| 3123 | { |
| 3124 | struct sctp_sock *sp; |
| 3125 | struct sctp_endpoint *ep; |
| 3126 | struct sock *newsk = NULL; |
| 3127 | struct sctp_association *asoc; |
| 3128 | long timeo; |
| 3129 | int error = 0; |
| 3130 | |
| 3131 | sctp_lock_sock(sk); |
| 3132 | |
| 3133 | sp = sctp_sk(sk); |
| 3134 | ep = sp->ep; |
| 3135 | |
| 3136 | if (!sctp_style(sk, TCP)) { |
| 3137 | error = -EOPNOTSUPP; |
| 3138 | goto out; |
| 3139 | } |
| 3140 | |
| 3141 | if (!sctp_sstate(sk, LISTENING)) { |
| 3142 | error = -EINVAL; |
| 3143 | goto out; |
| 3144 | } |
| 3145 | |
Sridhar Samudrala | 8abfedd | 2006-08-22 00:24:09 -0700 | [diff] [blame] | 3146 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | |
| 3148 | error = sctp_wait_for_accept(sk, timeo); |
| 3149 | if (error) |
| 3150 | goto out; |
| 3151 | |
| 3152 | /* We treat the list of associations on the endpoint as the accept |
| 3153 | * queue and pick the first association on the list. |
| 3154 | */ |
| 3155 | asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); |
| 3156 | |
| 3157 | newsk = sp->pf->create_accept_sk(sk, asoc); |
| 3158 | if (!newsk) { |
| 3159 | error = -ENOMEM; |
| 3160 | goto out; |
| 3161 | } |
| 3162 | |
| 3163 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 3164 | * asoc to the newsk. |
| 3165 | */ |
| 3166 | sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); |
| 3167 | |
| 3168 | out: |
| 3169 | sctp_release_sock(sk); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3170 | *err = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | return newsk; |
| 3172 | } |
| 3173 | |
| 3174 | /* The SCTP ioctl handler. */ |
| 3175 | SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 3176 | { |
| 3177 | return -ENOIOCTLCMD; |
| 3178 | } |
| 3179 | |
| 3180 | /* This is the function which gets called during socket creation to |
| 3181 | * initialized the SCTP-specific portion of the sock. |
| 3182 | * The sock structure should already be zero-filled memory. |
| 3183 | */ |
| 3184 | SCTP_STATIC int sctp_init_sock(struct sock *sk) |
| 3185 | { |
| 3186 | struct sctp_endpoint *ep; |
| 3187 | struct sctp_sock *sp; |
| 3188 | |
| 3189 | SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk); |
| 3190 | |
| 3191 | sp = sctp_sk(sk); |
| 3192 | |
| 3193 | /* Initialize the SCTP per socket area. */ |
| 3194 | switch (sk->sk_type) { |
| 3195 | case SOCK_SEQPACKET: |
| 3196 | sp->type = SCTP_SOCKET_UDP; |
| 3197 | break; |
| 3198 | case SOCK_STREAM: |
| 3199 | sp->type = SCTP_SOCKET_TCP; |
| 3200 | break; |
| 3201 | default: |
| 3202 | return -ESOCKTNOSUPPORT; |
| 3203 | } |
| 3204 | |
| 3205 | /* Initialize default send parameters. These parameters can be |
| 3206 | * modified with the SCTP_DEFAULT_SEND_PARAM socket option. |
| 3207 | */ |
| 3208 | sp->default_stream = 0; |
| 3209 | sp->default_ppid = 0; |
| 3210 | sp->default_flags = 0; |
| 3211 | sp->default_context = 0; |
| 3212 | sp->default_timetolive = 0; |
| 3213 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3214 | sp->default_rcv_context = 0; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3215 | sp->max_burst = sctp_max_burst; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | /* Initialize default setup parameters. These parameters |
| 3218 | * can be modified with the SCTP_INITMSG socket option or |
| 3219 | * overridden by the SCTP_INIT CMSG. |
| 3220 | */ |
| 3221 | sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; |
| 3222 | sp->initmsg.sinit_max_instreams = sctp_max_instreams; |
| 3223 | sp->initmsg.sinit_max_attempts = sctp_max_retrans_init; |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 3224 | sp->initmsg.sinit_max_init_timeo = sctp_rto_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | |
| 3226 | /* Initialize default RTO related parameters. These parameters can |
| 3227 | * be modified for with the SCTP_RTOINFO socket option. |
| 3228 | */ |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 3229 | sp->rtoinfo.srto_initial = sctp_rto_initial; |
| 3230 | sp->rtoinfo.srto_max = sctp_rto_max; |
| 3231 | sp->rtoinfo.srto_min = sctp_rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | |
| 3233 | /* Initialize default association related parameters. These parameters |
| 3234 | * can be modified with the SCTP_ASSOCINFO socket option. |
| 3235 | */ |
| 3236 | sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association; |
| 3237 | sp->assocparams.sasoc_number_peer_destinations = 0; |
| 3238 | sp->assocparams.sasoc_peer_rwnd = 0; |
| 3239 | sp->assocparams.sasoc_local_rwnd = 0; |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 3240 | sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | |
| 3242 | /* Initialize default event subscriptions. By default, all the |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3243 | * options are off. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | */ |
| 3245 | memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); |
| 3246 | |
| 3247 | /* Default Peer Address Parameters. These defaults can |
| 3248 | * be modified via SCTP_PEER_ADDR_PARAMS |
| 3249 | */ |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 3250 | sp->hbinterval = sctp_hb_interval; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3251 | sp->pathmaxrxt = sctp_max_retrans_path; |
| 3252 | sp->pathmtu = 0; // allow default discovery |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 3253 | sp->sackdelay = sctp_sack_timeout; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3254 | sp->param_flags = SPP_HB_ENABLE | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3255 | SPP_PMTUD_ENABLE | |
| 3256 | SPP_SACKDELAY_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | |
| 3258 | /* If enabled no SCTP message fragmentation will be performed. |
| 3259 | * Configure through SCTP_DISABLE_FRAGMENTS socket option. |
| 3260 | */ |
| 3261 | sp->disable_fragments = 0; |
| 3262 | |
Sridhar Samudrala | 208edef | 2006-09-29 17:08:01 -0700 | [diff] [blame] | 3263 | /* Enable Nagle algorithm by default. */ |
| 3264 | sp->nodelay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | |
| 3266 | /* Enable by default. */ |
| 3267 | sp->v4mapped = 1; |
| 3268 | |
| 3269 | /* Auto-close idle associations after the configured |
| 3270 | * number of seconds. A value of 0 disables this |
| 3271 | * feature. Configure through the SCTP_AUTOCLOSE socket option, |
| 3272 | * for UDP-style sockets only. |
| 3273 | */ |
| 3274 | sp->autoclose = 0; |
| 3275 | |
| 3276 | /* User specified fragmentation limit. */ |
| 3277 | sp->user_frag = 0; |
| 3278 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3279 | sp->adaptation_ind = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | |
| 3281 | sp->pf = sctp_get_pf_specific(sk->sk_family); |
| 3282 | |
| 3283 | /* Control variables for partial data delivery. */ |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3284 | atomic_set(&sp->pd_mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | skb_queue_head_init(&sp->pd_lobby); |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3286 | sp->frag_interleave = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
| 3288 | /* Create a per socket endpoint structure. Even if we |
| 3289 | * change the data structure relationships, this may still |
| 3290 | * be useful for storing pre-connect address information. |
| 3291 | */ |
| 3292 | ep = sctp_endpoint_new(sk, GFP_KERNEL); |
| 3293 | if (!ep) |
| 3294 | return -ENOMEM; |
| 3295 | |
| 3296 | sp->ep = ep; |
| 3297 | sp->hmac = NULL; |
| 3298 | |
| 3299 | SCTP_DBG_OBJCNT_INC(sock); |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | /* Cleanup any SCTP per socket resources. */ |
| 3304 | SCTP_STATIC int sctp_destroy_sock(struct sock *sk) |
| 3305 | { |
| 3306 | struct sctp_endpoint *ep; |
| 3307 | |
| 3308 | SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk); |
| 3309 | |
| 3310 | /* Release our hold on the endpoint. */ |
| 3311 | ep = sctp_sk(sk)->ep; |
| 3312 | sctp_endpoint_free(ep); |
| 3313 | |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
| 3317 | /* API 4.1.7 shutdown() - TCP Style Syntax |
| 3318 | * int shutdown(int socket, int how); |
| 3319 | * |
| 3320 | * sd - the socket descriptor of the association to be closed. |
| 3321 | * how - Specifies the type of shutdown. The values are |
| 3322 | * as follows: |
| 3323 | * SHUT_RD |
| 3324 | * Disables further receive operations. No SCTP |
| 3325 | * protocol action is taken. |
| 3326 | * SHUT_WR |
| 3327 | * Disables further send operations, and initiates |
| 3328 | * the SCTP shutdown sequence. |
| 3329 | * SHUT_RDWR |
| 3330 | * Disables further send and receive operations |
| 3331 | * and initiates the SCTP shutdown sequence. |
| 3332 | */ |
| 3333 | SCTP_STATIC void sctp_shutdown(struct sock *sk, int how) |
| 3334 | { |
| 3335 | struct sctp_endpoint *ep; |
| 3336 | struct sctp_association *asoc; |
| 3337 | |
| 3338 | if (!sctp_style(sk, TCP)) |
| 3339 | return; |
| 3340 | |
| 3341 | if (how & SEND_SHUTDOWN) { |
| 3342 | ep = sctp_sk(sk)->ep; |
| 3343 | if (!list_empty(&ep->asocs)) { |
| 3344 | asoc = list_entry(ep->asocs.next, |
| 3345 | struct sctp_association, asocs); |
| 3346 | sctp_primitive_SHUTDOWN(asoc, NULL); |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | /* 7.2.1 Association Status (SCTP_STATUS) |
| 3352 | |
| 3353 | * Applications can retrieve current status information about an |
| 3354 | * association, including association state, peer receiver window size, |
| 3355 | * number of unacked data chunks, and number of data chunks pending |
| 3356 | * receipt. This information is read-only. |
| 3357 | */ |
| 3358 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, |
| 3359 | char __user *optval, |
| 3360 | int __user *optlen) |
| 3361 | { |
| 3362 | struct sctp_status status; |
| 3363 | struct sctp_association *asoc = NULL; |
| 3364 | struct sctp_transport *transport; |
| 3365 | sctp_assoc_t associd; |
| 3366 | int retval = 0; |
| 3367 | |
| 3368 | if (len != sizeof(status)) { |
| 3369 | retval = -EINVAL; |
| 3370 | goto out; |
| 3371 | } |
| 3372 | |
| 3373 | if (copy_from_user(&status, optval, sizeof(status))) { |
| 3374 | retval = -EFAULT; |
| 3375 | goto out; |
| 3376 | } |
| 3377 | |
| 3378 | associd = status.sstat_assoc_id; |
| 3379 | asoc = sctp_id2assoc(sk, associd); |
| 3380 | if (!asoc) { |
| 3381 | retval = -EINVAL; |
| 3382 | goto out; |
| 3383 | } |
| 3384 | |
| 3385 | transport = asoc->peer.primary_path; |
| 3386 | |
| 3387 | status.sstat_assoc_id = sctp_assoc2id(asoc); |
| 3388 | status.sstat_state = asoc->state; |
| 3389 | status.sstat_rwnd = asoc->peer.rwnd; |
| 3390 | status.sstat_unackdata = asoc->unack_data; |
| 3391 | |
| 3392 | status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
| 3393 | status.sstat_instrms = asoc->c.sinit_max_instreams; |
| 3394 | status.sstat_outstrms = asoc->c.sinit_num_ostreams; |
| 3395 | status.sstat_fragmentation_point = asoc->frag_point; |
| 3396 | status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 3397 | memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, |
| 3398 | transport->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | /* Map ipv4 address into v4-mapped-on-v6 address. */ |
| 3400 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
| 3401 | (union sctp_addr *)&status.sstat_primary.spinfo_address); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3402 | status.sstat_primary.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | status.sstat_primary.spinfo_cwnd = transport->cwnd; |
| 3404 | status.sstat_primary.spinfo_srtt = transport->srtt; |
| 3405 | status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3406 | status.sstat_primary.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3408 | if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) |
| 3409 | status.sstat_primary.spinfo_state = SCTP_ACTIVE; |
| 3410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | if (put_user(len, optlen)) { |
| 3412 | retval = -EFAULT; |
| 3413 | goto out; |
| 3414 | } |
| 3415 | |
| 3416 | SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n", |
| 3417 | len, status.sstat_state, status.sstat_rwnd, |
| 3418 | status.sstat_assoc_id); |
| 3419 | |
| 3420 | if (copy_to_user(optval, &status, len)) { |
| 3421 | retval = -EFAULT; |
| 3422 | goto out; |
| 3423 | } |
| 3424 | |
| 3425 | out: |
| 3426 | return (retval); |
| 3427 | } |
| 3428 | |
| 3429 | |
| 3430 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) |
| 3431 | * |
| 3432 | * Applications can retrieve information about a specific peer address |
| 3433 | * of an association, including its reachability state, congestion |
| 3434 | * window, and retransmission timer values. This information is |
| 3435 | * read-only. |
| 3436 | */ |
| 3437 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, |
| 3438 | char __user *optval, |
| 3439 | int __user *optlen) |
| 3440 | { |
| 3441 | struct sctp_paddrinfo pinfo; |
| 3442 | struct sctp_transport *transport; |
| 3443 | int retval = 0; |
| 3444 | |
| 3445 | if (len != sizeof(pinfo)) { |
| 3446 | retval = -EINVAL; |
| 3447 | goto out; |
| 3448 | } |
| 3449 | |
| 3450 | if (copy_from_user(&pinfo, optval, sizeof(pinfo))) { |
| 3451 | retval = -EFAULT; |
| 3452 | goto out; |
| 3453 | } |
| 3454 | |
| 3455 | transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, |
| 3456 | pinfo.spinfo_assoc_id); |
| 3457 | if (!transport) |
| 3458 | return -EINVAL; |
| 3459 | |
| 3460 | pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3461 | pinfo.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | pinfo.spinfo_cwnd = transport->cwnd; |
| 3463 | pinfo.spinfo_srtt = transport->srtt; |
| 3464 | pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3465 | pinfo.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 3467 | if (pinfo.spinfo_state == SCTP_UNKNOWN) |
| 3468 | pinfo.spinfo_state = SCTP_ACTIVE; |
| 3469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | if (put_user(len, optlen)) { |
| 3471 | retval = -EFAULT; |
| 3472 | goto out; |
| 3473 | } |
| 3474 | |
| 3475 | if (copy_to_user(optval, &pinfo, len)) { |
| 3476 | retval = -EFAULT; |
| 3477 | goto out; |
| 3478 | } |
| 3479 | |
| 3480 | out: |
| 3481 | return (retval); |
| 3482 | } |
| 3483 | |
| 3484 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 3485 | * |
| 3486 | * This option is a on/off flag. If enabled no SCTP message |
| 3487 | * fragmentation will be performed. Instead if a message being sent |
| 3488 | * exceeds the current PMTU size, the message will NOT be sent and |
| 3489 | * instead a error will be indicated to the user. |
| 3490 | */ |
| 3491 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, |
| 3492 | char __user *optval, int __user *optlen) |
| 3493 | { |
| 3494 | int val; |
| 3495 | |
| 3496 | if (len < sizeof(int)) |
| 3497 | return -EINVAL; |
| 3498 | |
| 3499 | len = sizeof(int); |
| 3500 | val = (sctp_sk(sk)->disable_fragments == 1); |
| 3501 | if (put_user(len, optlen)) |
| 3502 | return -EFAULT; |
| 3503 | if (copy_to_user(optval, &val, len)) |
| 3504 | return -EFAULT; |
| 3505 | return 0; |
| 3506 | } |
| 3507 | |
| 3508 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) |
| 3509 | * |
| 3510 | * This socket option is used to specify various notifications and |
| 3511 | * ancillary data the user wishes to receive. |
| 3512 | */ |
| 3513 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, |
| 3514 | int __user *optlen) |
| 3515 | { |
| 3516 | if (len != sizeof(struct sctp_event_subscribe)) |
| 3517 | return -EINVAL; |
| 3518 | if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) |
| 3519 | return -EFAULT; |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
| 3523 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 3524 | * |
| 3525 | * This socket option is applicable to the UDP-style socket only. When |
| 3526 | * set it will cause associations that are idle for more than the |
| 3527 | * specified number of seconds to automatically close. An association |
| 3528 | * being idle is defined an association that has NOT sent or received |
| 3529 | * user data. The special value of '0' indicates that no automatic |
| 3530 | * close of any associations should be performed. The option expects an |
| 3531 | * integer defining the number of seconds of idle time before an |
| 3532 | * association is closed. |
| 3533 | */ |
| 3534 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 3535 | { |
| 3536 | /* Applicable to UDP-style socket only */ |
| 3537 | if (sctp_style(sk, TCP)) |
| 3538 | return -EOPNOTSUPP; |
| 3539 | if (len != sizeof(int)) |
| 3540 | return -EINVAL; |
| 3541 | if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len)) |
| 3542 | return -EFAULT; |
| 3543 | return 0; |
| 3544 | } |
| 3545 | |
| 3546 | /* Helper routine to branch off an association to a new socket. */ |
| 3547 | SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc, |
| 3548 | struct socket **sockp) |
| 3549 | { |
| 3550 | struct sock *sk = asoc->base.sk; |
| 3551 | struct socket *sock; |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 3552 | struct inet_sock *inetsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | int err = 0; |
| 3554 | |
| 3555 | /* An association cannot be branched off from an already peeled-off |
| 3556 | * socket, nor is this supported for tcp style sockets. |
| 3557 | */ |
| 3558 | if (!sctp_style(sk, UDP)) |
| 3559 | return -EINVAL; |
| 3560 | |
| 3561 | /* Create a new socket. */ |
| 3562 | err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); |
| 3563 | if (err < 0) |
| 3564 | return err; |
| 3565 | |
| 3566 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 3567 | * asoc to the newsk. |
| 3568 | */ |
| 3569 | sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 3570 | |
| 3571 | /* Make peeled-off sockets more like 1-1 accepted sockets. |
| 3572 | * Set the daddr and initialize id to something more random |
| 3573 | */ |
| 3574 | inetsk = inet_sk(sock->sk); |
| 3575 | inetsk->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; |
| 3576 | inetsk->id = asoc->next_tsn ^ jiffies; |
| 3577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | *sockp = sock; |
| 3579 | |
| 3580 | return err; |
| 3581 | } |
| 3582 | |
| 3583 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 3584 | { |
| 3585 | sctp_peeloff_arg_t peeloff; |
| 3586 | struct socket *newsock; |
| 3587 | int retval = 0; |
| 3588 | struct sctp_association *asoc; |
| 3589 | |
| 3590 | if (len != sizeof(sctp_peeloff_arg_t)) |
| 3591 | return -EINVAL; |
| 3592 | if (copy_from_user(&peeloff, optval, len)) |
| 3593 | return -EFAULT; |
| 3594 | |
| 3595 | asoc = sctp_id2assoc(sk, peeloff.associd); |
| 3596 | if (!asoc) { |
| 3597 | retval = -EINVAL; |
| 3598 | goto out; |
| 3599 | } |
| 3600 | |
| 3601 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc); |
| 3602 | |
| 3603 | retval = sctp_do_peeloff(asoc, &newsock); |
| 3604 | if (retval < 0) |
| 3605 | goto out; |
| 3606 | |
| 3607 | /* Map the socket to an unused fd that can be returned to the user. */ |
| 3608 | retval = sock_map_fd(newsock); |
| 3609 | if (retval < 0) { |
| 3610 | sock_release(newsock); |
| 3611 | goto out; |
| 3612 | } |
| 3613 | |
| 3614 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n", |
| 3615 | __FUNCTION__, sk, asoc, newsock->sk, retval); |
| 3616 | |
| 3617 | /* Return the fd mapped to the new socket. */ |
| 3618 | peeloff.sd = retval; |
| 3619 | if (copy_to_user(optval, &peeloff, len)) |
| 3620 | retval = -EFAULT; |
| 3621 | |
| 3622 | out: |
| 3623 | return retval; |
| 3624 | } |
| 3625 | |
| 3626 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 3627 | * |
| 3628 | * Applications can enable or disable heartbeats for any peer address of |
| 3629 | * an association, modify an address's heartbeat interval, force a |
| 3630 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 3631 | * number of retransmissions sent before an address is considered |
| 3632 | * unreachable. The following structure is used to access and modify an |
| 3633 | * address's parameters: |
| 3634 | * |
| 3635 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3636 | * sctp_assoc_t spp_assoc_id; |
| 3637 | * struct sockaddr_storage spp_address; |
| 3638 | * uint32_t spp_hbinterval; |
| 3639 | * uint16_t spp_pathmaxrxt; |
| 3640 | * uint32_t spp_pathmtu; |
| 3641 | * uint32_t spp_sackdelay; |
| 3642 | * uint32_t spp_flags; |
| 3643 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3645 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 3646 | * application, and identifies the association for |
| 3647 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | * spp_address - This specifies which address is of interest. |
| 3649 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3650 | * in milliseconds. If a value of zero |
| 3651 | * is present in this field then no changes are to |
| 3652 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | * spp_pathmaxrxt - This contains the maximum number of |
| 3654 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3655 | * considered unreachable. If a value of zero |
| 3656 | * is present in this field then no changes are to |
| 3657 | * be made to this parameter. |
| 3658 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 3659 | * specified here will be the "fixed" path mtu. |
| 3660 | * Note that if the spp_address field is empty |
| 3661 | * then all associations on this address will |
| 3662 | * have this fixed path mtu set upon them. |
| 3663 | * |
| 3664 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 3665 | * the number of milliseconds that sacks will be delayed |
| 3666 | * for. This value will apply to all addresses of an |
| 3667 | * association if the spp_address field is empty. Note |
| 3668 | * also, that if delayed sack is enabled and this |
| 3669 | * value is set to 0, no change is made to the last |
| 3670 | * recorded delayed sack timer value. |
| 3671 | * |
| 3672 | * spp_flags - These flags are used to control various features |
| 3673 | * on an association. The flag field may contain |
| 3674 | * zero or more of the following options. |
| 3675 | * |
| 3676 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 3677 | * specified address. Note that if the address |
| 3678 | * field is empty all addresses for the association |
| 3679 | * have heartbeats enabled upon them. |
| 3680 | * |
| 3681 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 3682 | * speicifed address. Note that if the address |
| 3683 | * field is empty all addresses for the association |
| 3684 | * will have their heartbeats disabled. Note also |
| 3685 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 3686 | * mutually exclusive, only one of these two should |
| 3687 | * be specified. Enabling both fields will have |
| 3688 | * undetermined results. |
| 3689 | * |
| 3690 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 3691 | * to be made immediately. |
| 3692 | * |
| 3693 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 3694 | * discovery upon the specified address. Note that |
| 3695 | * if the address feild is empty then all addresses |
| 3696 | * on the association are effected. |
| 3697 | * |
| 3698 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 3699 | * discovery upon the specified address. Note that |
| 3700 | * if the address feild is empty then all addresses |
| 3701 | * on the association are effected. Not also that |
| 3702 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 3703 | * exclusive. Enabling both will have undetermined |
| 3704 | * results. |
| 3705 | * |
| 3706 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 3707 | * on delayed sack. The time specified in spp_sackdelay |
| 3708 | * is used to specify the sack delay for this address. Note |
| 3709 | * that if spp_address is empty then all addresses will |
| 3710 | * enable delayed sack and take on the sack delay |
| 3711 | * value specified in spp_sackdelay. |
| 3712 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 3713 | * off delayed sack. If the spp_address field is blank then |
| 3714 | * delayed sack is disabled for the entire association. Note |
| 3715 | * also that this field is mutually exclusive to |
| 3716 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 3717 | * results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | */ |
| 3719 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3720 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3722 | struct sctp_paddrparams params; |
| 3723 | struct sctp_transport *trans = NULL; |
| 3724 | struct sctp_association *asoc = NULL; |
| 3725 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | |
| 3727 | if (len != sizeof(struct sctp_paddrparams)) |
| 3728 | return -EINVAL; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | if (copy_from_user(¶ms, optval, len)) |
| 3731 | return -EFAULT; |
| 3732 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3733 | /* If an address other than INADDR_ANY is specified, and |
| 3734 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | */ |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3736 | if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) { |
| 3737 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 3738 | params.spp_assoc_id); |
| 3739 | if (!trans) { |
| 3740 | SCTP_DEBUG_PRINTK("Failed no transport\n"); |
| 3741 | return -EINVAL; |
| 3742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3745 | /* Get association, if assoc_id != 0 and the socket is a one |
| 3746 | * to many style socket, and an association was not found, then |
| 3747 | * the id was invalid. |
| 3748 | */ |
| 3749 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
| 3750 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { |
| 3751 | SCTP_DEBUG_PRINTK("Failed no association\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | return -EINVAL; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3755 | if (trans) { |
| 3756 | /* Fetch transport values. */ |
| 3757 | params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); |
| 3758 | params.spp_pathmtu = trans->pathmtu; |
| 3759 | params.spp_pathmaxrxt = trans->pathmaxrxt; |
| 3760 | params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3762 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 3763 | params.spp_flags = trans->param_flags; |
| 3764 | } else if (asoc) { |
| 3765 | /* Fetch association values. */ |
| 3766 | params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); |
| 3767 | params.spp_pathmtu = asoc->pathmtu; |
| 3768 | params.spp_pathmaxrxt = asoc->pathmaxrxt; |
| 3769 | params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 3771 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 3772 | params.spp_flags = asoc->param_flags; |
| 3773 | } else { |
| 3774 | /* Fetch socket values. */ |
| 3775 | params.spp_hbinterval = sp->hbinterval; |
| 3776 | params.spp_pathmtu = sp->pathmtu; |
| 3777 | params.spp_sackdelay = sp->sackdelay; |
| 3778 | params.spp_pathmaxrxt = sp->pathmaxrxt; |
| 3779 | |
| 3780 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 3781 | params.spp_flags = sp->param_flags; |
| 3782 | } |
| 3783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | if (copy_to_user(optval, ¶ms, len)) |
| 3785 | return -EFAULT; |
| 3786 | |
| 3787 | if (put_user(len, optlen)) |
| 3788 | return -EFAULT; |
| 3789 | |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3793 | /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3794 | * |
| 3795 | * This options will get or set the delayed ack timer. The time is set |
| 3796 | * in milliseconds. If the assoc_id is 0, then this sets or gets the |
| 3797 | * endpoints default delayed ack timer value. If the assoc_id field is |
| 3798 | * non-zero, then the set or get effects the specified association. |
| 3799 | * |
| 3800 | * struct sctp_assoc_value { |
| 3801 | * sctp_assoc_t assoc_id; |
| 3802 | * uint32_t assoc_value; |
| 3803 | * }; |
| 3804 | * |
| 3805 | * assoc_id - This parameter, indicates which association the |
| 3806 | * user is preforming an action upon. Note that if |
| 3807 | * this field's value is zero then the endpoints |
| 3808 | * default value is changed (effecting future |
| 3809 | * associations only). |
| 3810 | * |
| 3811 | * assoc_value - This parameter contains the number of milliseconds |
| 3812 | * that the user is requesting the delayed ACK timer |
| 3813 | * be set to. Note that this value is defined in |
| 3814 | * the standard to be between 200 and 500 milliseconds. |
| 3815 | * |
| 3816 | * Note: a value of zero will leave the value alone, |
| 3817 | * but disable SACK delay. A non-zero value will also |
| 3818 | * enable SACK delay. |
| 3819 | */ |
| 3820 | static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len, |
| 3821 | char __user *optval, |
| 3822 | int __user *optlen) |
| 3823 | { |
| 3824 | struct sctp_assoc_value params; |
| 3825 | struct sctp_association *asoc = NULL; |
| 3826 | struct sctp_sock *sp = sctp_sk(sk); |
| 3827 | |
| 3828 | if (len != sizeof(struct sctp_assoc_value)) |
| 3829 | return - EINVAL; |
| 3830 | |
| 3831 | if (copy_from_user(¶ms, optval, len)) |
| 3832 | return -EFAULT; |
| 3833 | |
| 3834 | /* Get association, if assoc_id != 0 and the socket is a one |
| 3835 | * to many style socket, and an association was not found, then |
| 3836 | * the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3837 | */ |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 3838 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3839 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) |
| 3840 | return -EINVAL; |
| 3841 | |
| 3842 | if (asoc) { |
| 3843 | /* Fetch association values. */ |
| 3844 | if (asoc->param_flags & SPP_SACKDELAY_ENABLE) |
| 3845 | params.assoc_value = jiffies_to_msecs( |
| 3846 | asoc->sackdelay); |
| 3847 | else |
| 3848 | params.assoc_value = 0; |
| 3849 | } else { |
| 3850 | /* Fetch socket values. */ |
| 3851 | if (sp->param_flags & SPP_SACKDELAY_ENABLE) |
| 3852 | params.assoc_value = sp->sackdelay; |
| 3853 | else |
| 3854 | params.assoc_value = 0; |
| 3855 | } |
| 3856 | |
| 3857 | if (copy_to_user(optval, ¶ms, len)) |
| 3858 | return -EFAULT; |
| 3859 | |
| 3860 | if (put_user(len, optlen)) |
| 3861 | return -EFAULT; |
| 3862 | |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 3867 | * |
| 3868 | * Applications can specify protocol parameters for the default association |
| 3869 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 3870 | * is SCTP_INITMSG. |
| 3871 | * |
| 3872 | * Setting initialization parameters is effective only on an unconnected |
| 3873 | * socket (for UDP-style sockets only future associations are effected |
| 3874 | * by the change). With TCP-style sockets, this option is inherited by |
| 3875 | * sockets derived from a listener socket. |
| 3876 | */ |
| 3877 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 3878 | { |
| 3879 | if (len != sizeof(struct sctp_initmsg)) |
| 3880 | return -EINVAL; |
| 3881 | if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) |
| 3882 | return -EFAULT; |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3886 | static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len, |
| 3887 | char __user *optval, |
| 3888 | int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | { |
| 3890 | sctp_assoc_t id; |
| 3891 | struct sctp_association *asoc; |
| 3892 | struct list_head *pos; |
| 3893 | int cnt = 0; |
| 3894 | |
| 3895 | if (len != sizeof(sctp_assoc_t)) |
| 3896 | return -EINVAL; |
| 3897 | |
| 3898 | if (copy_from_user(&id, optval, sizeof(sctp_assoc_t))) |
| 3899 | return -EFAULT; |
| 3900 | |
| 3901 | /* For UDP-style sockets, id specifies the association to query. */ |
| 3902 | asoc = sctp_id2assoc(sk, id); |
| 3903 | if (!asoc) |
| 3904 | return -EINVAL; |
| 3905 | |
| 3906 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 3907 | cnt ++; |
| 3908 | } |
| 3909 | |
| 3910 | return cnt; |
| 3911 | } |
| 3912 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3913 | /* |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3914 | * Old API for getting list of peer addresses. Does not work for 32-bit |
| 3915 | * programs running on a 64-bit kernel |
| 3916 | */ |
| 3917 | static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len, |
| 3918 | char __user *optval, |
| 3919 | int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | { |
| 3921 | struct sctp_association *asoc; |
| 3922 | struct list_head *pos; |
| 3923 | int cnt = 0; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3924 | struct sctp_getaddrs_old getaddrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | struct sctp_transport *from; |
| 3926 | void __user *to; |
| 3927 | union sctp_addr temp; |
| 3928 | struct sctp_sock *sp = sctp_sk(sk); |
| 3929 | int addrlen; |
| 3930 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3931 | if (len != sizeof(struct sctp_getaddrs_old)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | return -EINVAL; |
| 3933 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3934 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | return -EFAULT; |
| 3936 | |
| 3937 | if (getaddrs.addr_num <= 0) return -EINVAL; |
| 3938 | |
| 3939 | /* For UDP-style sockets, id specifies the association to query. */ |
| 3940 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 3941 | if (!asoc) |
| 3942 | return -EINVAL; |
| 3943 | |
| 3944 | to = (void __user *)getaddrs.addrs; |
| 3945 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 3946 | from = list_entry(pos, struct sctp_transport, transports); |
Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 3947 | memcpy(&temp, &from->ipaddr, sizeof(temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
| 3949 | addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | if (copy_to_user(to, &temp, addrlen)) |
| 3951 | return -EFAULT; |
| 3952 | to += addrlen ; |
| 3953 | cnt ++; |
| 3954 | if (cnt >= getaddrs.addr_num) break; |
| 3955 | } |
| 3956 | getaddrs.addr_num = cnt; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3957 | if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | return -EFAULT; |
| 3959 | |
| 3960 | return 0; |
| 3961 | } |
| 3962 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3963 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, |
| 3964 | char __user *optval, int __user *optlen) |
| 3965 | { |
| 3966 | struct sctp_association *asoc; |
| 3967 | struct list_head *pos; |
| 3968 | int cnt = 0; |
| 3969 | struct sctp_getaddrs getaddrs; |
| 3970 | struct sctp_transport *from; |
| 3971 | void __user *to; |
| 3972 | union sctp_addr temp; |
| 3973 | struct sctp_sock *sp = sctp_sk(sk); |
| 3974 | int addrlen; |
| 3975 | size_t space_left; |
| 3976 | int bytes_copied; |
| 3977 | |
| 3978 | if (len < sizeof(struct sctp_getaddrs)) |
| 3979 | return -EINVAL; |
| 3980 | |
| 3981 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 3982 | return -EFAULT; |
| 3983 | |
| 3984 | /* For UDP-style sockets, id specifies the association to query. */ |
| 3985 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 3986 | if (!asoc) |
| 3987 | return -EINVAL; |
| 3988 | |
| 3989 | to = optval + offsetof(struct sctp_getaddrs,addrs); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3990 | space_left = len - sizeof(struct sctp_getaddrs) - |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3991 | offsetof(struct sctp_getaddrs,addrs); |
| 3992 | |
| 3993 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 3994 | from = list_entry(pos, struct sctp_transport, transports); |
Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 3995 | memcpy(&temp, &from->ipaddr, sizeof(temp)); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3996 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
| 3997 | addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 3998 | if (space_left < addrlen) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 3999 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4000 | if (copy_to_user(to, &temp, addrlen)) |
| 4001 | return -EFAULT; |
| 4002 | to += addrlen; |
| 4003 | cnt++; |
| 4004 | space_left -= addrlen; |
| 4005 | } |
| 4006 | |
| 4007 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) |
| 4008 | return -EFAULT; |
| 4009 | bytes_copied = ((char __user *)to) - optval; |
| 4010 | if (put_user(bytes_copied, optlen)) |
| 4011 | return -EFAULT; |
| 4012 | |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, |
| 4017 | char __user *optval, |
| 4018 | int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | { |
| 4020 | sctp_assoc_t id; |
| 4021 | struct sctp_bind_addr *bp; |
| 4022 | struct sctp_association *asoc; |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4023 | struct list_head *pos, *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | struct sctp_sockaddr_entry *addr; |
| 4025 | rwlock_t *addr_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | int cnt = 0; |
| 4027 | |
| 4028 | if (len != sizeof(sctp_assoc_t)) |
| 4029 | return -EINVAL; |
| 4030 | |
| 4031 | if (copy_from_user(&id, optval, sizeof(sctp_assoc_t))) |
| 4032 | return -EFAULT; |
| 4033 | |
| 4034 | /* |
| 4035 | * For UDP-style sockets, id specifies the association to query. |
| 4036 | * If the id field is set to the value '0' then the locally bound |
| 4037 | * addresses are returned without regard to any particular |
| 4038 | * association. |
| 4039 | */ |
| 4040 | if (0 == id) { |
| 4041 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
| 4042 | addr_lock = &sctp_sk(sk)->ep->base.addr_lock; |
| 4043 | } else { |
| 4044 | asoc = sctp_id2assoc(sk, id); |
| 4045 | if (!asoc) |
| 4046 | return -EINVAL; |
| 4047 | bp = &asoc->base.bind_addr; |
| 4048 | addr_lock = &asoc->base.addr_lock; |
| 4049 | } |
| 4050 | |
| 4051 | sctp_read_lock(addr_lock); |
| 4052 | |
| 4053 | /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid |
| 4054 | * addresses from the global local address list. |
| 4055 | */ |
| 4056 | if (sctp_list_single_entry(&bp->address_list)) { |
| 4057 | addr = list_entry(bp->address_list.next, |
| 4058 | struct sctp_sockaddr_entry, list); |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4059 | if (sctp_is_any(&addr->a)) { |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4060 | list_for_each_safe(pos, temp, &sctp_local_addr_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | addr = list_entry(pos, |
| 4062 | struct sctp_sockaddr_entry, |
| 4063 | list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4064 | if ((PF_INET == sk->sk_family) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4065 | (AF_INET6 == addr->a.sa.sa_family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | continue; |
| 4067 | cnt++; |
| 4068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | } else { |
| 4070 | cnt = 1; |
| 4071 | } |
| 4072 | goto done; |
| 4073 | } |
| 4074 | |
| 4075 | list_for_each(pos, &bp->address_list) { |
| 4076 | cnt ++; |
| 4077 | } |
| 4078 | |
| 4079 | done: |
| 4080 | sctp_read_unlock(addr_lock); |
| 4081 | return cnt; |
| 4082 | } |
| 4083 | |
| 4084 | /* Helper function that copies local addresses to user and returns the number |
| 4085 | * of addresses copied. |
| 4086 | */ |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4087 | static int sctp_copy_laddrs_old(struct sock *sk, __u16 port, |
| 4088 | int max_addrs, void *to, |
| 4089 | int *bytes_copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | { |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4091 | struct list_head *pos, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | struct sctp_sockaddr_entry *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | union sctp_addr temp; |
| 4094 | int cnt = 0; |
| 4095 | int addrlen; |
| 4096 | |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4097 | list_for_each_safe(pos, next, &sctp_local_addr_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4099 | if ((PF_INET == sk->sk_family) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4100 | (AF_INET6 == addr->a.sa.sa_family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | continue; |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4102 | memcpy(&temp, &addr->a, sizeof(temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
| 4104 | &temp); |
| 4105 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4106 | memcpy(to, &temp, addrlen); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | to += addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4109 | *bytes_copied += addrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | cnt ++; |
| 4111 | if (cnt >= max_addrs) break; |
| 4112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | |
| 4114 | return cnt; |
| 4115 | } |
| 4116 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4117 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, |
| 4118 | size_t space_left, int *bytes_copied) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4119 | { |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4120 | struct list_head *pos, *next; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4121 | struct sctp_sockaddr_entry *addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4122 | union sctp_addr temp; |
| 4123 | int cnt = 0; |
| 4124 | int addrlen; |
| 4125 | |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4126 | list_for_each_safe(pos, next, &sctp_local_addr_list) { |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4127 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4128 | if ((PF_INET == sk->sk_family) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4129 | (AF_INET6 == addr->a.sa.sa_family)) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4130 | continue; |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4131 | memcpy(&temp, &addr->a, sizeof(temp)); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4132 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
| 4133 | &temp); |
| 4134 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4135 | if (space_left < addrlen) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4136 | return -ENOMEM; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4137 | memcpy(to, &temp, addrlen); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 4138 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4139 | to += addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4140 | cnt ++; |
| 4141 | space_left -= addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4142 | bytes_copied += addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4143 | } |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4144 | |
| 4145 | return cnt; |
| 4146 | } |
| 4147 | |
| 4148 | /* Old API for getting list of local addresses. Does not work for 32-bit |
| 4149 | * programs running on a 64-bit kernel |
| 4150 | */ |
| 4151 | static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len, |
| 4152 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | { |
| 4154 | struct sctp_bind_addr *bp; |
| 4155 | struct sctp_association *asoc; |
| 4156 | struct list_head *pos; |
| 4157 | int cnt = 0; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4158 | struct sctp_getaddrs_old getaddrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | struct sctp_sockaddr_entry *addr; |
| 4160 | void __user *to; |
| 4161 | union sctp_addr temp; |
| 4162 | struct sctp_sock *sp = sctp_sk(sk); |
| 4163 | int addrlen; |
| 4164 | rwlock_t *addr_lock; |
| 4165 | int err = 0; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4166 | void *addrs; |
| 4167 | int bytes_copied = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4169 | if (len != sizeof(struct sctp_getaddrs_old)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | return -EINVAL; |
| 4171 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4172 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | return -EFAULT; |
| 4174 | |
| 4175 | if (getaddrs.addr_num <= 0) return -EINVAL; |
| 4176 | /* |
| 4177 | * For UDP-style sockets, id specifies the association to query. |
| 4178 | * If the id field is set to the value '0' then the locally bound |
| 4179 | * addresses are returned without regard to any particular |
| 4180 | * association. |
| 4181 | */ |
| 4182 | if (0 == getaddrs.assoc_id) { |
| 4183 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
| 4184 | addr_lock = &sctp_sk(sk)->ep->base.addr_lock; |
| 4185 | } else { |
| 4186 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 4187 | if (!asoc) |
| 4188 | return -EINVAL; |
| 4189 | bp = &asoc->base.bind_addr; |
| 4190 | addr_lock = &asoc->base.addr_lock; |
| 4191 | } |
| 4192 | |
| 4193 | to = getaddrs.addrs; |
| 4194 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4195 | /* Allocate space for a local instance of packed array to hold all |
| 4196 | * the data. We store addresses here first and then put write them |
| 4197 | * to the user in one shot. |
| 4198 | */ |
| 4199 | addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num, |
| 4200 | GFP_KERNEL); |
| 4201 | if (!addrs) |
| 4202 | return -ENOMEM; |
| 4203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | sctp_read_lock(addr_lock); |
| 4205 | |
| 4206 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid |
| 4207 | * addresses from the global local address list. |
| 4208 | */ |
| 4209 | if (sctp_list_single_entry(&bp->address_list)) { |
| 4210 | addr = list_entry(bp->address_list.next, |
| 4211 | struct sctp_sockaddr_entry, list); |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4212 | if (sctp_is_any(&addr->a)) { |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4213 | cnt = sctp_copy_laddrs_old(sk, bp->port, |
| 4214 | getaddrs.addr_num, |
| 4215 | addrs, &bytes_copied); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4216 | goto copy_getaddrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | list_for_each(pos, &bp->address_list) { |
| 4221 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4222 | memcpy(&temp, &addr->a, sizeof(temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
| 4224 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4225 | memcpy(addrs, &temp, addrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | to += addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4227 | bytes_copied += addrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | cnt ++; |
| 4229 | if (cnt >= getaddrs.addr_num) break; |
| 4230 | } |
| 4231 | |
| 4232 | copy_getaddrs: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4233 | sctp_read_unlock(addr_lock); |
| 4234 | |
| 4235 | /* copy the entire address list into the user provided space */ |
| 4236 | if (copy_to_user(to, addrs, bytes_copied)) { |
| 4237 | err = -EFAULT; |
| 4238 | goto error; |
| 4239 | } |
| 4240 | |
| 4241 | /* copy the leading structure back to user */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | getaddrs.addr_num = cnt; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4243 | if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | err = -EFAULT; |
| 4245 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4246 | error: |
| 4247 | kfree(addrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | return err; |
| 4249 | } |
| 4250 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4251 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, |
| 4252 | char __user *optval, int __user *optlen) |
| 4253 | { |
| 4254 | struct sctp_bind_addr *bp; |
| 4255 | struct sctp_association *asoc; |
| 4256 | struct list_head *pos; |
| 4257 | int cnt = 0; |
| 4258 | struct sctp_getaddrs getaddrs; |
| 4259 | struct sctp_sockaddr_entry *addr; |
| 4260 | void __user *to; |
| 4261 | union sctp_addr temp; |
| 4262 | struct sctp_sock *sp = sctp_sk(sk); |
| 4263 | int addrlen; |
| 4264 | rwlock_t *addr_lock; |
| 4265 | int err = 0; |
| 4266 | size_t space_left; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4267 | int bytes_copied = 0; |
| 4268 | void *addrs; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4269 | |
| 4270 | if (len <= sizeof(struct sctp_getaddrs)) |
| 4271 | return -EINVAL; |
| 4272 | |
| 4273 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 4274 | return -EFAULT; |
| 4275 | |
| 4276 | /* |
| 4277 | * For UDP-style sockets, id specifies the association to query. |
| 4278 | * If the id field is set to the value '0' then the locally bound |
| 4279 | * addresses are returned without regard to any particular |
| 4280 | * association. |
| 4281 | */ |
| 4282 | if (0 == getaddrs.assoc_id) { |
| 4283 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
| 4284 | addr_lock = &sctp_sk(sk)->ep->base.addr_lock; |
| 4285 | } else { |
| 4286 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 4287 | if (!asoc) |
| 4288 | return -EINVAL; |
| 4289 | bp = &asoc->base.bind_addr; |
| 4290 | addr_lock = &asoc->base.addr_lock; |
| 4291 | } |
| 4292 | |
| 4293 | to = optval + offsetof(struct sctp_getaddrs,addrs); |
| 4294 | space_left = len - sizeof(struct sctp_getaddrs) - |
| 4295 | offsetof(struct sctp_getaddrs,addrs); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4296 | addrs = kmalloc(space_left, GFP_KERNEL); |
| 4297 | if (!addrs) |
| 4298 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4299 | |
| 4300 | sctp_read_lock(addr_lock); |
| 4301 | |
| 4302 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid |
| 4303 | * addresses from the global local address list. |
| 4304 | */ |
| 4305 | if (sctp_list_single_entry(&bp->address_list)) { |
| 4306 | addr = list_entry(bp->address_list.next, |
| 4307 | struct sctp_sockaddr_entry, list); |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4308 | if (sctp_is_any(&addr->a)) { |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4309 | cnt = sctp_copy_laddrs(sk, bp->port, addrs, |
| 4310 | space_left, &bytes_copied); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4311 | if (cnt < 0) { |
| 4312 | err = cnt; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4313 | goto error; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4314 | } |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4315 | goto copy_getaddrs; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | list_for_each(pos, &bp->address_list) { |
| 4320 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 4321 | memcpy(&temp, &addr->a, sizeof(temp)); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4322 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
| 4323 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4324 | if (space_left < addrlen) { |
| 4325 | err = -ENOMEM; /*fixme: right error?*/ |
| 4326 | goto error; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4327 | } |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4328 | memcpy(addrs, &temp, addrlen); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4329 | to += addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4330 | bytes_copied += addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4331 | cnt ++; |
| 4332 | space_left -= addrlen; |
| 4333 | } |
| 4334 | |
| 4335 | copy_getaddrs: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4336 | sctp_read_unlock(addr_lock); |
| 4337 | |
| 4338 | if (copy_to_user(to, addrs, bytes_copied)) { |
| 4339 | err = -EFAULT; |
| 4340 | goto error; |
| 4341 | } |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4342 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) |
| 4343 | return -EFAULT; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4344 | if (put_user(bytes_copied, optlen)) |
| 4345 | return -EFAULT; |
| 4346 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 4347 | error: |
| 4348 | kfree(addrs); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4349 | return err; |
| 4350 | } |
| 4351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 4353 | * |
| 4354 | * Requests that the local SCTP stack use the enclosed peer address as |
| 4355 | * the association primary. The enclosed address must be one of the |
| 4356 | * association peer's addresses. |
| 4357 | */ |
| 4358 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, |
| 4359 | char __user *optval, int __user *optlen) |
| 4360 | { |
| 4361 | struct sctp_prim prim; |
| 4362 | struct sctp_association *asoc; |
| 4363 | struct sctp_sock *sp = sctp_sk(sk); |
| 4364 | |
| 4365 | if (len != sizeof(struct sctp_prim)) |
| 4366 | return -EINVAL; |
| 4367 | |
| 4368 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) |
| 4369 | return -EFAULT; |
| 4370 | |
| 4371 | asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); |
| 4372 | if (!asoc) |
| 4373 | return -EINVAL; |
| 4374 | |
| 4375 | if (!asoc->peer.primary_path) |
| 4376 | return -ENOTCONN; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4377 | |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 4378 | memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, |
| 4379 | asoc->peer.primary_path->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | |
| 4381 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, |
| 4382 | (union sctp_addr *)&prim.ssp_addr); |
| 4383 | |
| 4384 | if (copy_to_user(optval, &prim, sizeof(struct sctp_prim))) |
| 4385 | return -EFAULT; |
| 4386 | |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
| 4390 | /* |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4391 | * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | * |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4393 | * Requests that the local endpoint set the specified Adaptation Layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | * Indication parameter for all future INIT and INIT-ACK exchanges. |
| 4395 | */ |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4396 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | char __user *optval, int __user *optlen) |
| 4398 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4399 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4401 | if (len != sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | return -EINVAL; |
| 4403 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4404 | adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; |
| 4405 | if (copy_to_user(optval, &adaptation, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | return -EFAULT; |
Ivan Skytte Jorgensen | a1ab358 | 2005-10-28 15:33:24 -0700 | [diff] [blame] | 4407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | return 0; |
| 4409 | } |
| 4410 | |
| 4411 | /* |
| 4412 | * |
| 4413 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 4414 | * |
| 4415 | * Applications that wish to use the sendto() system call may wish to |
| 4416 | * specify a default set of parameters that would normally be supplied |
| 4417 | * through the inclusion of ancillary data. This socket option allows |
| 4418 | * such an application to set the default sctp_sndrcvinfo structure. |
| 4419 | |
| 4420 | |
| 4421 | * The application that wishes to use this socket option simply passes |
| 4422 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 4423 | * 5.2.2) The input parameters accepted by this call include |
| 4424 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 4425 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 4426 | * to this call if the caller is using the UDP model. |
| 4427 | * |
| 4428 | * For getsockopt, it get the default sctp_sndrcvinfo structure. |
| 4429 | */ |
| 4430 | static int sctp_getsockopt_default_send_param(struct sock *sk, |
| 4431 | int len, char __user *optval, |
| 4432 | int __user *optlen) |
| 4433 | { |
| 4434 | struct sctp_sndrcvinfo info; |
| 4435 | struct sctp_association *asoc; |
| 4436 | struct sctp_sock *sp = sctp_sk(sk); |
| 4437 | |
| 4438 | if (len != sizeof(struct sctp_sndrcvinfo)) |
| 4439 | return -EINVAL; |
| 4440 | if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo))) |
| 4441 | return -EFAULT; |
| 4442 | |
| 4443 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
| 4444 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
| 4445 | return -EINVAL; |
| 4446 | |
| 4447 | if (asoc) { |
| 4448 | info.sinfo_stream = asoc->default_stream; |
| 4449 | info.sinfo_flags = asoc->default_flags; |
| 4450 | info.sinfo_ppid = asoc->default_ppid; |
| 4451 | info.sinfo_context = asoc->default_context; |
| 4452 | info.sinfo_timetolive = asoc->default_timetolive; |
| 4453 | } else { |
| 4454 | info.sinfo_stream = sp->default_stream; |
| 4455 | info.sinfo_flags = sp->default_flags; |
| 4456 | info.sinfo_ppid = sp->default_ppid; |
| 4457 | info.sinfo_context = sp->default_context; |
| 4458 | info.sinfo_timetolive = sp->default_timetolive; |
| 4459 | } |
| 4460 | |
| 4461 | if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo))) |
| 4462 | return -EFAULT; |
| 4463 | |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
| 4467 | /* |
| 4468 | * |
| 4469 | * 7.1.5 SCTP_NODELAY |
| 4470 | * |
| 4471 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 4472 | * generally sent as soon as possible and no unnecessary delays are |
| 4473 | * introduced, at the cost of more packets in the network. Expects an |
| 4474 | * integer boolean flag. |
| 4475 | */ |
| 4476 | |
| 4477 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, |
| 4478 | char __user *optval, int __user *optlen) |
| 4479 | { |
| 4480 | int val; |
| 4481 | |
| 4482 | if (len < sizeof(int)) |
| 4483 | return -EINVAL; |
| 4484 | |
| 4485 | len = sizeof(int); |
| 4486 | val = (sctp_sk(sk)->nodelay == 1); |
| 4487 | if (put_user(len, optlen)) |
| 4488 | return -EFAULT; |
| 4489 | if (copy_to_user(optval, &val, len)) |
| 4490 | return -EFAULT; |
| 4491 | return 0; |
| 4492 | } |
| 4493 | |
| 4494 | /* |
| 4495 | * |
| 4496 | * 7.1.1 SCTP_RTOINFO |
| 4497 | * |
| 4498 | * The protocol parameters used to initialize and bound retransmission |
| 4499 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 4500 | * and modify these parameters. |
| 4501 | * All parameters are time values, in milliseconds. A value of 0, when |
| 4502 | * modifying the parameters, indicates that the current value should not |
| 4503 | * be changed. |
| 4504 | * |
| 4505 | */ |
| 4506 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, |
| 4507 | char __user *optval, |
| 4508 | int __user *optlen) { |
| 4509 | struct sctp_rtoinfo rtoinfo; |
| 4510 | struct sctp_association *asoc; |
| 4511 | |
| 4512 | if (len != sizeof (struct sctp_rtoinfo)) |
| 4513 | return -EINVAL; |
| 4514 | |
| 4515 | if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo))) |
| 4516 | return -EFAULT; |
| 4517 | |
| 4518 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 4519 | |
| 4520 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
| 4521 | return -EINVAL; |
| 4522 | |
| 4523 | /* Values corresponding to the specific association. */ |
| 4524 | if (asoc) { |
| 4525 | rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); |
| 4526 | rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); |
| 4527 | rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); |
| 4528 | } else { |
| 4529 | /* Values corresponding to the endpoint. */ |
| 4530 | struct sctp_sock *sp = sctp_sk(sk); |
| 4531 | |
| 4532 | rtoinfo.srto_initial = sp->rtoinfo.srto_initial; |
| 4533 | rtoinfo.srto_max = sp->rtoinfo.srto_max; |
| 4534 | rtoinfo.srto_min = sp->rtoinfo.srto_min; |
| 4535 | } |
| 4536 | |
| 4537 | if (put_user(len, optlen)) |
| 4538 | return -EFAULT; |
| 4539 | |
| 4540 | if (copy_to_user(optval, &rtoinfo, len)) |
| 4541 | return -EFAULT; |
| 4542 | |
| 4543 | return 0; |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * |
| 4548 | * 7.1.2 SCTP_ASSOCINFO |
| 4549 | * |
| 4550 | * This option is used to tune the the maximum retransmission attempts |
| 4551 | * of the association. |
| 4552 | * Returns an error if the new association retransmission value is |
| 4553 | * greater than the sum of the retransmission value of the peer. |
| 4554 | * See [SCTP] for more information. |
| 4555 | * |
| 4556 | */ |
| 4557 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, |
| 4558 | char __user *optval, |
| 4559 | int __user *optlen) |
| 4560 | { |
| 4561 | |
| 4562 | struct sctp_assocparams assocparams; |
| 4563 | struct sctp_association *asoc; |
| 4564 | struct list_head *pos; |
| 4565 | int cnt = 0; |
| 4566 | |
| 4567 | if (len != sizeof (struct sctp_assocparams)) |
| 4568 | return -EINVAL; |
| 4569 | |
| 4570 | if (copy_from_user(&assocparams, optval, |
| 4571 | sizeof (struct sctp_assocparams))) |
| 4572 | return -EFAULT; |
| 4573 | |
| 4574 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 4575 | |
| 4576 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
| 4577 | return -EINVAL; |
| 4578 | |
| 4579 | /* Values correspoinding to the specific association */ |
Vladislav Yasevich | 1733721 | 2005-04-28 11:57:54 -0700 | [diff] [blame] | 4580 | if (asoc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | assocparams.sasoc_asocmaxrxt = asoc->max_retrans; |
| 4582 | assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; |
| 4583 | assocparams.sasoc_local_rwnd = asoc->a_rwnd; |
| 4584 | assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec |
| 4585 | * 1000) + |
| 4586 | (asoc->cookie_life.tv_usec |
| 4587 | / 1000); |
| 4588 | |
| 4589 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
| 4590 | cnt ++; |
| 4591 | } |
| 4592 | |
| 4593 | assocparams.sasoc_number_peer_destinations = cnt; |
| 4594 | } else { |
| 4595 | /* Values corresponding to the endpoint */ |
| 4596 | struct sctp_sock *sp = sctp_sk(sk); |
| 4597 | |
| 4598 | assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; |
| 4599 | assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; |
| 4600 | assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; |
| 4601 | assocparams.sasoc_cookie_life = |
| 4602 | sp->assocparams.sasoc_cookie_life; |
| 4603 | assocparams.sasoc_number_peer_destinations = |
| 4604 | sp->assocparams. |
| 4605 | sasoc_number_peer_destinations; |
| 4606 | } |
| 4607 | |
| 4608 | if (put_user(len, optlen)) |
| 4609 | return -EFAULT; |
| 4610 | |
| 4611 | if (copy_to_user(optval, &assocparams, len)) |
| 4612 | return -EFAULT; |
| 4613 | |
| 4614 | return 0; |
| 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 4619 | * |
| 4620 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 4621 | * addresses. If this option is turned on and the socket is type |
| 4622 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 4623 | * If this option is turned off, then no mapping will be done of V4 |
| 4624 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 4625 | * addresses on the socket. |
| 4626 | */ |
| 4627 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, |
| 4628 | char __user *optval, int __user *optlen) |
| 4629 | { |
| 4630 | int val; |
| 4631 | struct sctp_sock *sp = sctp_sk(sk); |
| 4632 | |
| 4633 | if (len < sizeof(int)) |
| 4634 | return -EINVAL; |
| 4635 | |
| 4636 | len = sizeof(int); |
| 4637 | val = sp->v4mapped; |
| 4638 | if (put_user(len, optlen)) |
| 4639 | return -EFAULT; |
| 4640 | if (copy_to_user(optval, &val, len)) |
| 4641 | return -EFAULT; |
| 4642 | |
| 4643 | return 0; |
| 4644 | } |
| 4645 | |
| 4646 | /* |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4647 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 4648 | * (chapter and verse is quoted at sctp_setsockopt_context()) |
| 4649 | */ |
| 4650 | static int sctp_getsockopt_context(struct sock *sk, int len, |
| 4651 | char __user *optval, int __user *optlen) |
| 4652 | { |
| 4653 | struct sctp_assoc_value params; |
| 4654 | struct sctp_sock *sp; |
| 4655 | struct sctp_association *asoc; |
| 4656 | |
| 4657 | if (len != sizeof(struct sctp_assoc_value)) |
| 4658 | return -EINVAL; |
| 4659 | |
| 4660 | if (copy_from_user(¶ms, optval, len)) |
| 4661 | return -EFAULT; |
| 4662 | |
| 4663 | sp = sctp_sk(sk); |
| 4664 | |
| 4665 | if (params.assoc_id != 0) { |
| 4666 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4667 | if (!asoc) |
| 4668 | return -EINVAL; |
| 4669 | params.assoc_value = asoc->default_rcv_context; |
| 4670 | } else { |
| 4671 | params.assoc_value = sp->default_rcv_context; |
| 4672 | } |
| 4673 | |
| 4674 | if (put_user(len, optlen)) |
| 4675 | return -EFAULT; |
| 4676 | if (copy_to_user(optval, ¶ms, len)) |
| 4677 | return -EFAULT; |
| 4678 | |
| 4679 | return 0; |
| 4680 | } |
| 4681 | |
| 4682 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG) |
| 4684 | * |
| 4685 | * This socket option specifies the maximum size to put in any outgoing |
| 4686 | * SCTP chunk. If a message is larger than this size it will be |
| 4687 | * fragmented by SCTP into the specified size. Note that the underlying |
| 4688 | * SCTP implementation may fragment into smaller sized chunks when the |
| 4689 | * PMTU of the underlying association is smaller than the value set by |
| 4690 | * the user. |
| 4691 | */ |
| 4692 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, |
| 4693 | char __user *optval, int __user *optlen) |
| 4694 | { |
| 4695 | int val; |
| 4696 | |
| 4697 | if (len < sizeof(int)) |
| 4698 | return -EINVAL; |
| 4699 | |
| 4700 | len = sizeof(int); |
| 4701 | |
| 4702 | val = sctp_sk(sk)->user_frag; |
| 4703 | if (put_user(len, optlen)) |
| 4704 | return -EFAULT; |
| 4705 | if (copy_to_user(optval, &val, len)) |
| 4706 | return -EFAULT; |
| 4707 | |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4711 | /* |
| 4712 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 4713 | * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) |
| 4714 | */ |
| 4715 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, |
| 4716 | char __user *optval, int __user *optlen) |
| 4717 | { |
| 4718 | int val; |
| 4719 | |
| 4720 | if (len < sizeof(int)) |
| 4721 | return -EINVAL; |
| 4722 | |
| 4723 | len = sizeof(int); |
| 4724 | |
| 4725 | val = sctp_sk(sk)->frag_interleave; |
| 4726 | if (put_user(len, optlen)) |
| 4727 | return -EFAULT; |
| 4728 | if (copy_to_user(optval, &val, len)) |
| 4729 | return -EFAULT; |
| 4730 | |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 4734 | /* |
| 4735 | * 7.1.25. Set or Get the sctp partial delivery point |
| 4736 | * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) |
| 4737 | */ |
| 4738 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, |
| 4739 | char __user *optval, |
| 4740 | int __user *optlen) |
| 4741 | { |
| 4742 | u32 val; |
| 4743 | |
| 4744 | if (len < sizeof(u32)) |
| 4745 | return -EINVAL; |
| 4746 | |
| 4747 | len = sizeof(u32); |
| 4748 | |
| 4749 | val = sctp_sk(sk)->pd_point; |
| 4750 | if (put_user(len, optlen)) |
| 4751 | return -EFAULT; |
| 4752 | if (copy_to_user(optval, &val, len)) |
| 4753 | return -EFAULT; |
| 4754 | |
| 4755 | return -ENOTSUPP; |
| 4756 | } |
| 4757 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 4758 | /* |
| 4759 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 4760 | * (chapter and verse is quoted at sctp_setsockopt_maxburst()) |
| 4761 | */ |
| 4762 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, |
| 4763 | char __user *optval, |
| 4764 | int __user *optlen) |
| 4765 | { |
| 4766 | int val; |
| 4767 | |
| 4768 | if (len < sizeof(int)) |
| 4769 | return -EINVAL; |
| 4770 | |
| 4771 | len = sizeof(int); |
| 4772 | |
| 4773 | val = sctp_sk(sk)->max_burst; |
| 4774 | if (put_user(len, optlen)) |
| 4775 | return -EFAULT; |
| 4776 | if (copy_to_user(optval, &val, len)) |
| 4777 | return -EFAULT; |
| 4778 | |
| 4779 | return -ENOTSUPP; |
| 4780 | } |
| 4781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname, |
| 4783 | char __user *optval, int __user *optlen) |
| 4784 | { |
| 4785 | int retval = 0; |
| 4786 | int len; |
| 4787 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4788 | SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n", |
| 4789 | sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | |
| 4791 | /* I can hardly begin to describe how wrong this is. This is |
| 4792 | * so broken as to be worse than useless. The API draft |
| 4793 | * REALLY is NOT helpful here... I am not convinced that the |
| 4794 | * semantics of getsockopt() with a level OTHER THAN SOL_SCTP |
| 4795 | * are at all well-founded. |
| 4796 | */ |
| 4797 | if (level != SOL_SCTP) { |
| 4798 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 4799 | |
| 4800 | retval = af->getsockopt(sk, level, optname, optval, optlen); |
| 4801 | return retval; |
| 4802 | } |
| 4803 | |
| 4804 | if (get_user(len, optlen)) |
| 4805 | return -EFAULT; |
| 4806 | |
| 4807 | sctp_lock_sock(sk); |
| 4808 | |
| 4809 | switch (optname) { |
| 4810 | case SCTP_STATUS: |
| 4811 | retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); |
| 4812 | break; |
| 4813 | case SCTP_DISABLE_FRAGMENTS: |
| 4814 | retval = sctp_getsockopt_disable_fragments(sk, len, optval, |
| 4815 | optlen); |
| 4816 | break; |
| 4817 | case SCTP_EVENTS: |
| 4818 | retval = sctp_getsockopt_events(sk, len, optval, optlen); |
| 4819 | break; |
| 4820 | case SCTP_AUTOCLOSE: |
| 4821 | retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); |
| 4822 | break; |
| 4823 | case SCTP_SOCKOPT_PEELOFF: |
| 4824 | retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); |
| 4825 | break; |
| 4826 | case SCTP_PEER_ADDR_PARAMS: |
| 4827 | retval = sctp_getsockopt_peer_addr_params(sk, len, optval, |
| 4828 | optlen); |
| 4829 | break; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4830 | case SCTP_DELAYED_ACK_TIME: |
| 4831 | retval = sctp_getsockopt_delayed_ack_time(sk, len, optval, |
| 4832 | optlen); |
| 4833 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | case SCTP_INITMSG: |
| 4835 | retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); |
| 4836 | break; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4837 | case SCTP_GET_PEER_ADDRS_NUM_OLD: |
| 4838 | retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval, |
| 4839 | optlen); |
| 4840 | break; |
| 4841 | case SCTP_GET_LOCAL_ADDRS_NUM_OLD: |
| 4842 | retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval, |
| 4843 | optlen); |
| 4844 | break; |
| 4845 | case SCTP_GET_PEER_ADDRS_OLD: |
| 4846 | retval = sctp_getsockopt_peer_addrs_old(sk, len, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | optlen); |
| 4848 | break; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 4849 | case SCTP_GET_LOCAL_ADDRS_OLD: |
| 4850 | retval = sctp_getsockopt_local_addrs_old(sk, len, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | optlen); |
| 4852 | break; |
| 4853 | case SCTP_GET_PEER_ADDRS: |
| 4854 | retval = sctp_getsockopt_peer_addrs(sk, len, optval, |
| 4855 | optlen); |
| 4856 | break; |
| 4857 | case SCTP_GET_LOCAL_ADDRS: |
| 4858 | retval = sctp_getsockopt_local_addrs(sk, len, optval, |
| 4859 | optlen); |
| 4860 | break; |
| 4861 | case SCTP_DEFAULT_SEND_PARAM: |
| 4862 | retval = sctp_getsockopt_default_send_param(sk, len, |
| 4863 | optval, optlen); |
| 4864 | break; |
| 4865 | case SCTP_PRIMARY_ADDR: |
| 4866 | retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); |
| 4867 | break; |
| 4868 | case SCTP_NODELAY: |
| 4869 | retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); |
| 4870 | break; |
| 4871 | case SCTP_RTOINFO: |
| 4872 | retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); |
| 4873 | break; |
| 4874 | case SCTP_ASSOCINFO: |
| 4875 | retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); |
| 4876 | break; |
| 4877 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 4878 | retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); |
| 4879 | break; |
| 4880 | case SCTP_MAXSEG: |
| 4881 | retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); |
| 4882 | break; |
| 4883 | case SCTP_GET_PEER_ADDR_INFO: |
| 4884 | retval = sctp_getsockopt_peer_addr_info(sk, len, optval, |
| 4885 | optlen); |
| 4886 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4887 | case SCTP_ADAPTATION_LAYER: |
| 4888 | retval = sctp_getsockopt_adaptation_layer(sk, len, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | optlen); |
| 4890 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4891 | case SCTP_CONTEXT: |
| 4892 | retval = sctp_getsockopt_context(sk, len, optval, optlen); |
| 4893 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4894 | case SCTP_FRAGMENT_INTERLEAVE: |
| 4895 | retval = sctp_getsockopt_fragment_interleave(sk, len, optval, |
| 4896 | optlen); |
| 4897 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 4898 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 4899 | retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, |
| 4900 | optlen); |
| 4901 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 4902 | case SCTP_MAX_BURST: |
| 4903 | retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); |
| 4904 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | default: |
| 4906 | retval = -ENOPROTOOPT; |
| 4907 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | |
| 4910 | sctp_release_sock(sk); |
| 4911 | return retval; |
| 4912 | } |
| 4913 | |
| 4914 | static void sctp_hash(struct sock *sk) |
| 4915 | { |
| 4916 | /* STUB */ |
| 4917 | } |
| 4918 | |
| 4919 | static void sctp_unhash(struct sock *sk) |
| 4920 | { |
| 4921 | /* STUB */ |
| 4922 | } |
| 4923 | |
| 4924 | /* Check if port is acceptable. Possibly find first available port. |
| 4925 | * |
| 4926 | * The port hash table (contained in the 'global' SCTP protocol storage |
| 4927 | * returned by struct sctp_protocol *sctp_get_protocol()). The hash |
| 4928 | * table is an array of 4096 lists (sctp_bind_hashbucket). Each |
| 4929 | * list (the list number is the port number hashed out, so as you |
| 4930 | * would expect from a hash function, all the ports in a given list have |
| 4931 | * such a number that hashes out to the same list number; you were |
| 4932 | * expecting that, right?); so each list has a set of ports, with a |
| 4933 | * link to the socket (struct sock) that uses it, the port number and |
| 4934 | * a fastreuse flag (FIXME: NPI ipg). |
| 4935 | */ |
| 4936 | static struct sctp_bind_bucket *sctp_bucket_create( |
| 4937 | struct sctp_bind_hashbucket *head, unsigned short snum); |
| 4938 | |
| 4939 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) |
| 4940 | { |
| 4941 | struct sctp_bind_hashbucket *head; /* hash list */ |
| 4942 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
| 4943 | unsigned short snum; |
| 4944 | int ret; |
| 4945 | |
Al Viro | 04afd8b | 2006-11-20 17:02:01 -0800 | [diff] [blame] | 4946 | snum = ntohs(addr->v4.sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | |
| 4948 | SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum); |
| 4949 | sctp_local_bh_disable(); |
| 4950 | |
| 4951 | if (snum == 0) { |
| 4952 | /* Search for an available port. |
| 4953 | * |
| 4954 | * 'sctp_port_rover' was the last port assigned, so |
| 4955 | * we start to search from 'sctp_port_rover + |
| 4956 | * 1'. What we do is first check if port 'rover' is |
| 4957 | * already in the hash table; if not, we use that; if |
| 4958 | * it is, we try next. |
| 4959 | */ |
| 4960 | int low = sysctl_local_port_range[0]; |
| 4961 | int high = sysctl_local_port_range[1]; |
| 4962 | int remaining = (high - low) + 1; |
| 4963 | int rover; |
| 4964 | int index; |
| 4965 | |
| 4966 | sctp_spin_lock(&sctp_port_alloc_lock); |
| 4967 | rover = sctp_port_rover; |
| 4968 | do { |
| 4969 | rover++; |
| 4970 | if ((rover < low) || (rover > high)) |
| 4971 | rover = low; |
| 4972 | index = sctp_phashfn(rover); |
| 4973 | head = &sctp_port_hashtable[index]; |
| 4974 | sctp_spin_lock(&head->lock); |
| 4975 | for (pp = head->chain; pp; pp = pp->next) |
| 4976 | if (pp->port == rover) |
| 4977 | goto next; |
| 4978 | break; |
| 4979 | next: |
| 4980 | sctp_spin_unlock(&head->lock); |
| 4981 | } while (--remaining > 0); |
| 4982 | sctp_port_rover = rover; |
| 4983 | sctp_spin_unlock(&sctp_port_alloc_lock); |
| 4984 | |
| 4985 | /* Exhausted local port range during search? */ |
| 4986 | ret = 1; |
| 4987 | if (remaining <= 0) |
| 4988 | goto fail; |
| 4989 | |
| 4990 | /* OK, here is the one we will use. HEAD (the port |
| 4991 | * hash table list entry) is non-NULL and we hold it's |
| 4992 | * mutex. |
| 4993 | */ |
| 4994 | snum = rover; |
| 4995 | } else { |
| 4996 | /* We are given an specific port number; we verify |
| 4997 | * that it is not being used. If it is used, we will |
| 4998 | * exahust the search in the hash list corresponding |
| 4999 | * to the port number (snum) - we detect that with the |
| 5000 | * port iterator, pp being NULL. |
| 5001 | */ |
| 5002 | head = &sctp_port_hashtable[sctp_phashfn(snum)]; |
| 5003 | sctp_spin_lock(&head->lock); |
| 5004 | for (pp = head->chain; pp; pp = pp->next) { |
| 5005 | if (pp->port == snum) |
| 5006 | goto pp_found; |
| 5007 | } |
| 5008 | } |
| 5009 | pp = NULL; |
| 5010 | goto pp_not_found; |
| 5011 | pp_found: |
| 5012 | if (!hlist_empty(&pp->owner)) { |
| 5013 | /* We had a port hash table hit - there is an |
| 5014 | * available port (pp != NULL) and it is being |
| 5015 | * used by other socket (pp->owner not empty); that other |
| 5016 | * socket is going to be sk2. |
| 5017 | */ |
| 5018 | int reuse = sk->sk_reuse; |
| 5019 | struct sock *sk2; |
| 5020 | struct hlist_node *node; |
| 5021 | |
| 5022 | SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n"); |
| 5023 | if (pp->fastreuse && sk->sk_reuse) |
| 5024 | goto success; |
| 5025 | |
| 5026 | /* Run through the list of sockets bound to the port |
| 5027 | * (pp->port) [via the pointers bind_next and |
| 5028 | * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, |
| 5029 | * we get the endpoint they describe and run through |
| 5030 | * the endpoint's list of IP (v4 or v6) addresses, |
| 5031 | * comparing each of the addresses with the address of |
| 5032 | * the socket sk. If we find a match, then that means |
| 5033 | * that this port/socket (sk) combination are already |
| 5034 | * in an endpoint. |
| 5035 | */ |
| 5036 | sk_for_each_bound(sk2, node, &pp->owner) { |
| 5037 | struct sctp_endpoint *ep2; |
| 5038 | ep2 = sctp_sk(sk2)->ep; |
| 5039 | |
| 5040 | if (reuse && sk2->sk_reuse) |
| 5041 | continue; |
| 5042 | |
Al Viro | 7e1e4a2 | 2006-11-20 17:05:43 -0800 | [diff] [blame] | 5043 | if (sctp_bind_addr_match(&ep2->base.bind_addr, addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | sctp_sk(sk))) { |
| 5045 | ret = (long)sk2; |
| 5046 | goto fail_unlock; |
| 5047 | } |
| 5048 | } |
| 5049 | SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n"); |
| 5050 | } |
| 5051 | pp_not_found: |
| 5052 | /* If there was a hash table miss, create a new port. */ |
| 5053 | ret = 1; |
| 5054 | if (!pp && !(pp = sctp_bucket_create(head, snum))) |
| 5055 | goto fail_unlock; |
| 5056 | |
| 5057 | /* In either case (hit or miss), make sure fastreuse is 1 only |
| 5058 | * if sk->sk_reuse is too (that is, if the caller requested |
| 5059 | * SO_REUSEADDR on this socket -sk-). |
| 5060 | */ |
| 5061 | if (hlist_empty(&pp->owner)) |
| 5062 | pp->fastreuse = sk->sk_reuse ? 1 : 0; |
| 5063 | else if (pp->fastreuse && !sk->sk_reuse) |
| 5064 | pp->fastreuse = 0; |
| 5065 | |
| 5066 | /* We are set, so fill up all the data in the hash table |
| 5067 | * entry, tie the socket list information with the rest of the |
| 5068 | * sockets FIXME: Blurry, NPI (ipg). |
| 5069 | */ |
| 5070 | success: |
| 5071 | inet_sk(sk)->num = snum; |
| 5072 | if (!sctp_sk(sk)->bind_hash) { |
| 5073 | sk_add_bind_node(sk, &pp->owner); |
| 5074 | sctp_sk(sk)->bind_hash = pp; |
| 5075 | } |
| 5076 | ret = 0; |
| 5077 | |
| 5078 | fail_unlock: |
| 5079 | sctp_spin_unlock(&head->lock); |
| 5080 | |
| 5081 | fail: |
| 5082 | sctp_local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | return ret; |
| 5084 | } |
| 5085 | |
| 5086 | /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral |
| 5087 | * port is requested. |
| 5088 | */ |
| 5089 | static int sctp_get_port(struct sock *sk, unsigned short snum) |
| 5090 | { |
| 5091 | long ret; |
| 5092 | union sctp_addr addr; |
| 5093 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 5094 | |
| 5095 | /* Set up a dummy address struct from the sk. */ |
| 5096 | af->from_sk(&addr, sk); |
| 5097 | addr.v4.sin_port = htons(snum); |
| 5098 | |
| 5099 | /* Note: sk->sk_num gets filled in if ephemeral port request. */ |
| 5100 | ret = sctp_get_port_local(sk, &addr); |
| 5101 | |
| 5102 | return (ret ? 1 : 0); |
| 5103 | } |
| 5104 | |
| 5105 | /* |
| 5106 | * 3.1.3 listen() - UDP Style Syntax |
| 5107 | * |
| 5108 | * By default, new associations are not accepted for UDP style sockets. |
| 5109 | * An application uses listen() to mark a socket as being able to |
| 5110 | * accept new associations. |
| 5111 | */ |
| 5112 | SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog) |
| 5113 | { |
| 5114 | struct sctp_sock *sp = sctp_sk(sk); |
| 5115 | struct sctp_endpoint *ep = sp->ep; |
| 5116 | |
| 5117 | /* Only UDP style sockets that are not peeled off are allowed to |
| 5118 | * listen(). |
| 5119 | */ |
| 5120 | if (!sctp_style(sk, UDP)) |
| 5121 | return -EINVAL; |
| 5122 | |
| 5123 | /* If backlog is zero, disable listening. */ |
| 5124 | if (!backlog) { |
| 5125 | if (sctp_sstate(sk, CLOSED)) |
| 5126 | return 0; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | sctp_unhash_endpoint(ep); |
| 5129 | sk->sk_state = SCTP_SS_CLOSED; |
| 5130 | } |
| 5131 | |
| 5132 | /* Return if we are already listening. */ |
| 5133 | if (sctp_sstate(sk, LISTENING)) |
| 5134 | return 0; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | /* |
| 5137 | * If a bind() or sctp_bindx() is not called prior to a listen() |
| 5138 | * call that allows new associations to be accepted, the system |
| 5139 | * picks an ephemeral port and will choose an address set equivalent |
| 5140 | * to binding with a wildcard address. |
| 5141 | * |
| 5142 | * This is not currently spelled out in the SCTP sockets |
| 5143 | * extensions draft, but follows the practice as seen in TCP |
| 5144 | * sockets. |
| 5145 | */ |
| 5146 | if (!ep->base.bind_addr.port) { |
| 5147 | if (sctp_autobind(sk)) |
| 5148 | return -EAGAIN; |
| 5149 | } |
| 5150 | sk->sk_state = SCTP_SS_LISTENING; |
| 5151 | sctp_hash_endpoint(ep); |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
| 5155 | /* |
| 5156 | * 4.1.3 listen() - TCP Style Syntax |
| 5157 | * |
| 5158 | * Applications uses listen() to ready the SCTP endpoint for accepting |
| 5159 | * inbound associations. |
| 5160 | */ |
| 5161 | SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog) |
| 5162 | { |
| 5163 | struct sctp_sock *sp = sctp_sk(sk); |
| 5164 | struct sctp_endpoint *ep = sp->ep; |
| 5165 | |
| 5166 | /* If backlog is zero, disable listening. */ |
| 5167 | if (!backlog) { |
| 5168 | if (sctp_sstate(sk, CLOSED)) |
| 5169 | return 0; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | sctp_unhash_endpoint(ep); |
| 5172 | sk->sk_state = SCTP_SS_CLOSED; |
| 5173 | } |
| 5174 | |
| 5175 | if (sctp_sstate(sk, LISTENING)) |
| 5176 | return 0; |
| 5177 | |
| 5178 | /* |
| 5179 | * If a bind() or sctp_bindx() is not called prior to a listen() |
| 5180 | * call that allows new associations to be accepted, the system |
| 5181 | * picks an ephemeral port and will choose an address set equivalent |
| 5182 | * to binding with a wildcard address. |
| 5183 | * |
| 5184 | * This is not currently spelled out in the SCTP sockets |
| 5185 | * extensions draft, but follows the practice as seen in TCP |
| 5186 | * sockets. |
| 5187 | */ |
| 5188 | if (!ep->base.bind_addr.port) { |
| 5189 | if (sctp_autobind(sk)) |
| 5190 | return -EAGAIN; |
| 5191 | } |
| 5192 | sk->sk_state = SCTP_SS_LISTENING; |
| 5193 | sk->sk_max_ack_backlog = backlog; |
| 5194 | sctp_hash_endpoint(ep); |
| 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | /* |
| 5199 | * Move a socket to LISTENING state. |
| 5200 | */ |
| 5201 | int sctp_inet_listen(struct socket *sock, int backlog) |
| 5202 | { |
| 5203 | struct sock *sk = sock->sk; |
Herbert Xu | 1b489e1 | 2006-08-20 15:07:14 +1000 | [diff] [blame] | 5204 | struct crypto_hash *tfm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | int err = -EINVAL; |
| 5206 | |
| 5207 | if (unlikely(backlog < 0)) |
| 5208 | goto out; |
| 5209 | |
| 5210 | sctp_lock_sock(sk); |
| 5211 | |
| 5212 | if (sock->state != SS_UNCONNECTED) |
| 5213 | goto out; |
| 5214 | |
| 5215 | /* Allocate HMAC for generating cookie. */ |
| 5216 | if (sctp_hmac_alg) { |
Herbert Xu | 1b489e1 | 2006-08-20 15:07:14 +1000 | [diff] [blame] | 5217 | tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | if (!tfm) { |
| 5219 | err = -ENOSYS; |
| 5220 | goto out; |
| 5221 | } |
| 5222 | } |
| 5223 | |
| 5224 | switch (sock->type) { |
| 5225 | case SOCK_SEQPACKET: |
| 5226 | err = sctp_seqpacket_listen(sk, backlog); |
| 5227 | break; |
| 5228 | case SOCK_STREAM: |
| 5229 | err = sctp_stream_listen(sk, backlog); |
| 5230 | break; |
| 5231 | default: |
| 5232 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 5233 | } |
| 5234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | if (err) |
| 5236 | goto cleanup; |
| 5237 | |
| 5238 | /* Store away the transform reference. */ |
| 5239 | sctp_sk(sk)->hmac = tfm; |
| 5240 | out: |
| 5241 | sctp_release_sock(sk); |
| 5242 | return err; |
| 5243 | cleanup: |
Herbert Xu | 1b489e1 | 2006-08-20 15:07:14 +1000 | [diff] [blame] | 5244 | crypto_free_hash(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | goto out; |
| 5246 | } |
| 5247 | |
| 5248 | /* |
| 5249 | * This function is done by modeling the current datagram_poll() and the |
| 5250 | * tcp_poll(). Note that, based on these implementations, we don't |
| 5251 | * lock the socket in this function, even though it seems that, |
| 5252 | * ideally, locking or some other mechanisms can be used to ensure |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 5253 | * the integrity of the counters (sndbuf and wmem_alloc) used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | * in this place. We assume that we don't need locks either until proven |
| 5255 | * otherwise. |
| 5256 | * |
| 5257 | * Another thing to note is that we include the Async I/O support |
| 5258 | * here, again, by modeling the current TCP/UDP code. We don't have |
| 5259 | * a good way to test with it yet. |
| 5260 | */ |
| 5261 | unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 5262 | { |
| 5263 | struct sock *sk = sock->sk; |
| 5264 | struct sctp_sock *sp = sctp_sk(sk); |
| 5265 | unsigned int mask; |
| 5266 | |
| 5267 | poll_wait(file, sk->sk_sleep, wait); |
| 5268 | |
| 5269 | /* A TCP-style listening socket becomes readable when the accept queue |
| 5270 | * is not empty. |
| 5271 | */ |
| 5272 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 5273 | return (!list_empty(&sp->ep->asocs)) ? |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5274 | (POLLIN | POLLRDNORM) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | |
| 5276 | mask = 0; |
| 5277 | |
| 5278 | /* Is there any exceptional events? */ |
| 5279 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 5280 | mask |= POLLERR; |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 5281 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 5282 | mask |= POLLRDHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 5284 | mask |= POLLHUP; |
| 5285 | |
| 5286 | /* Is it readable? Reconsider this code with TCP-style support. */ |
| 5287 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
| 5288 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 5289 | mask |= POLLIN | POLLRDNORM; |
| 5290 | |
| 5291 | /* The association is either gone or not ready. */ |
| 5292 | if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) |
| 5293 | return mask; |
| 5294 | |
| 5295 | /* Is it writable? */ |
| 5296 | if (sctp_writeable(sk)) { |
| 5297 | mask |= POLLOUT | POLLWRNORM; |
| 5298 | } else { |
| 5299 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 5300 | /* |
| 5301 | * Since the socket is not locked, the buffer |
| 5302 | * might be made available after the writeable check and |
| 5303 | * before the bit is set. This could cause a lost I/O |
| 5304 | * signal. tcp_poll() has a race breaker for this race |
| 5305 | * condition. Based on their implementation, we put |
| 5306 | * in the following code to cover it as well. |
| 5307 | */ |
| 5308 | if (sctp_writeable(sk)) |
| 5309 | mask |= POLLOUT | POLLWRNORM; |
| 5310 | } |
| 5311 | return mask; |
| 5312 | } |
| 5313 | |
| 5314 | /******************************************************************** |
| 5315 | * 2nd Level Abstractions |
| 5316 | ********************************************************************/ |
| 5317 | |
| 5318 | static struct sctp_bind_bucket *sctp_bucket_create( |
| 5319 | struct sctp_bind_hashbucket *head, unsigned short snum) |
| 5320 | { |
| 5321 | struct sctp_bind_bucket *pp; |
| 5322 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 5323 | pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | SCTP_DBG_OBJCNT_INC(bind_bucket); |
| 5325 | if (pp) { |
| 5326 | pp->port = snum; |
| 5327 | pp->fastreuse = 0; |
| 5328 | INIT_HLIST_HEAD(&pp->owner); |
| 5329 | if ((pp->next = head->chain) != NULL) |
| 5330 | pp->next->pprev = &pp->next; |
| 5331 | head->chain = pp; |
| 5332 | pp->pprev = &head->chain; |
| 5333 | } |
| 5334 | return pp; |
| 5335 | } |
| 5336 | |
| 5337 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ |
| 5338 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) |
| 5339 | { |
Sridhar Samudrala | 37fa687 | 2006-07-21 14:45:47 -0700 | [diff] [blame] | 5340 | if (pp && hlist_empty(&pp->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | if (pp->next) |
| 5342 | pp->next->pprev = pp->pprev; |
| 5343 | *(pp->pprev) = pp->next; |
| 5344 | kmem_cache_free(sctp_bucket_cachep, pp); |
| 5345 | SCTP_DBG_OBJCNT_DEC(bind_bucket); |
| 5346 | } |
| 5347 | } |
| 5348 | |
| 5349 | /* Release this socket's reference to a local port. */ |
| 5350 | static inline void __sctp_put_port(struct sock *sk) |
| 5351 | { |
| 5352 | struct sctp_bind_hashbucket *head = |
| 5353 | &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)]; |
| 5354 | struct sctp_bind_bucket *pp; |
| 5355 | |
| 5356 | sctp_spin_lock(&head->lock); |
| 5357 | pp = sctp_sk(sk)->bind_hash; |
| 5358 | __sk_del_bind_node(sk); |
| 5359 | sctp_sk(sk)->bind_hash = NULL; |
| 5360 | inet_sk(sk)->num = 0; |
| 5361 | sctp_bucket_destroy(pp); |
| 5362 | sctp_spin_unlock(&head->lock); |
| 5363 | } |
| 5364 | |
| 5365 | void sctp_put_port(struct sock *sk) |
| 5366 | { |
| 5367 | sctp_local_bh_disable(); |
| 5368 | __sctp_put_port(sk); |
| 5369 | sctp_local_bh_enable(); |
| 5370 | } |
| 5371 | |
| 5372 | /* |
| 5373 | * The system picks an ephemeral port and choose an address set equivalent |
| 5374 | * to binding with a wildcard address. |
| 5375 | * One of those addresses will be the primary address for the association. |
| 5376 | * This automatically enables the multihoming capability of SCTP. |
| 5377 | */ |
| 5378 | static int sctp_autobind(struct sock *sk) |
| 5379 | { |
| 5380 | union sctp_addr autoaddr; |
| 5381 | struct sctp_af *af; |
Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 5382 | __be16 port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | |
| 5384 | /* Initialize a local sockaddr structure to INADDR_ANY. */ |
| 5385 | af = sctp_sk(sk)->pf->af; |
| 5386 | |
| 5387 | port = htons(inet_sk(sk)->num); |
| 5388 | af->inaddr_any(&autoaddr, port); |
| 5389 | |
| 5390 | return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); |
| 5391 | } |
| 5392 | |
| 5393 | /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. |
| 5394 | * |
| 5395 | * From RFC 2292 |
| 5396 | * 4.2 The cmsghdr Structure * |
| 5397 | * |
| 5398 | * When ancillary data is sent or received, any number of ancillary data |
| 5399 | * objects can be specified by the msg_control and msg_controllen members of |
| 5400 | * the msghdr structure, because each object is preceded by |
| 5401 | * a cmsghdr structure defining the object's length (the cmsg_len member). |
| 5402 | * Historically Berkeley-derived implementations have passed only one object |
| 5403 | * at a time, but this API allows multiple objects to be |
| 5404 | * passed in a single call to sendmsg() or recvmsg(). The following example |
| 5405 | * shows two ancillary data objects in a control buffer. |
| 5406 | * |
| 5407 | * |<--------------------------- msg_controllen -------------------------->| |
| 5408 | * | | |
| 5409 | * |
| 5410 | * |<----- ancillary data object ----->|<----- ancillary data object ----->| |
| 5411 | * |
| 5412 | * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| |
| 5413 | * | | | |
| 5414 | * |
| 5415 | * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | |
| 5416 | * |
| 5417 | * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | |
| 5418 | * | | | | | |
| 5419 | * |
| 5420 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 5421 | * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| |
| 5422 | * |
| 5423 | * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| |
| 5424 | * |
| 5425 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 5426 | * ^ |
| 5427 | * | |
| 5428 | * |
| 5429 | * msg_control |
| 5430 | * points here |
| 5431 | */ |
| 5432 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg, |
| 5433 | sctp_cmsgs_t *cmsgs) |
| 5434 | { |
| 5435 | struct cmsghdr *cmsg; |
| 5436 | |
| 5437 | for (cmsg = CMSG_FIRSTHDR(msg); |
| 5438 | cmsg != NULL; |
| 5439 | cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) { |
| 5440 | if (!CMSG_OK(msg, cmsg)) |
| 5441 | return -EINVAL; |
| 5442 | |
| 5443 | /* Should we parse this header or ignore? */ |
| 5444 | if (cmsg->cmsg_level != IPPROTO_SCTP) |
| 5445 | continue; |
| 5446 | |
| 5447 | /* Strictly check lengths following example in SCM code. */ |
| 5448 | switch (cmsg->cmsg_type) { |
| 5449 | case SCTP_INIT: |
| 5450 | /* SCTP Socket API Extension |
| 5451 | * 5.2.1 SCTP Initiation Structure (SCTP_INIT) |
| 5452 | * |
| 5453 | * This cmsghdr structure provides information for |
| 5454 | * initializing new SCTP associations with sendmsg(). |
| 5455 | * The SCTP_INITMSG socket option uses this same data |
| 5456 | * structure. This structure is not used for |
| 5457 | * recvmsg(). |
| 5458 | * |
| 5459 | * cmsg_level cmsg_type cmsg_data[] |
| 5460 | * ------------ ------------ ---------------------- |
| 5461 | * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg |
| 5462 | */ |
| 5463 | if (cmsg->cmsg_len != |
| 5464 | CMSG_LEN(sizeof(struct sctp_initmsg))) |
| 5465 | return -EINVAL; |
| 5466 | cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg); |
| 5467 | break; |
| 5468 | |
| 5469 | case SCTP_SNDRCV: |
| 5470 | /* SCTP Socket API Extension |
| 5471 | * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV) |
| 5472 | * |
| 5473 | * This cmsghdr structure specifies SCTP options for |
| 5474 | * sendmsg() and describes SCTP header information |
| 5475 | * about a received message through recvmsg(). |
| 5476 | * |
| 5477 | * cmsg_level cmsg_type cmsg_data[] |
| 5478 | * ------------ ------------ ---------------------- |
| 5479 | * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo |
| 5480 | */ |
| 5481 | if (cmsg->cmsg_len != |
| 5482 | CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) |
| 5483 | return -EINVAL; |
| 5484 | |
| 5485 | cmsgs->info = |
| 5486 | (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 5487 | |
| 5488 | /* Minimally, validate the sinfo_flags. */ |
| 5489 | if (cmsgs->info->sinfo_flags & |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 5490 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
| 5491 | SCTP_ABORT | SCTP_EOF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | return -EINVAL; |
| 5493 | break; |
| 5494 | |
| 5495 | default: |
| 5496 | return -EINVAL; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 5497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | } |
| 5499 | return 0; |
| 5500 | } |
| 5501 | |
| 5502 | /* |
| 5503 | * Wait for a packet.. |
| 5504 | * Note: This function is the same function as in core/datagram.c |
| 5505 | * with a few modifications to make lksctp work. |
| 5506 | */ |
| 5507 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p) |
| 5508 | { |
| 5509 | int error; |
| 5510 | DEFINE_WAIT(wait); |
| 5511 | |
| 5512 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
| 5513 | |
| 5514 | /* Socket errors? */ |
| 5515 | error = sock_error(sk); |
| 5516 | if (error) |
| 5517 | goto out; |
| 5518 | |
| 5519 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 5520 | goto ready; |
| 5521 | |
| 5522 | /* Socket shut down? */ |
| 5523 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 5524 | goto out; |
| 5525 | |
| 5526 | /* Sequenced packets can come disconnected. If so we report the |
| 5527 | * problem. |
| 5528 | */ |
| 5529 | error = -ENOTCONN; |
| 5530 | |
| 5531 | /* Is there a good reason to think that we may receive some data? */ |
| 5532 | if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) |
| 5533 | goto out; |
| 5534 | |
| 5535 | /* Handle signals. */ |
| 5536 | if (signal_pending(current)) |
| 5537 | goto interrupted; |
| 5538 | |
| 5539 | /* Let another process have a go. Since we are going to sleep |
| 5540 | * anyway. Note: This may cause odd behaviors if the message |
| 5541 | * does not fit in the user's buffer, but this seems to be the |
| 5542 | * only way to honor MSG_DONTWAIT realistically. |
| 5543 | */ |
| 5544 | sctp_release_sock(sk); |
| 5545 | *timeo_p = schedule_timeout(*timeo_p); |
| 5546 | sctp_lock_sock(sk); |
| 5547 | |
| 5548 | ready: |
| 5549 | finish_wait(sk->sk_sleep, &wait); |
| 5550 | return 0; |
| 5551 | |
| 5552 | interrupted: |
| 5553 | error = sock_intr_errno(*timeo_p); |
| 5554 | |
| 5555 | out: |
| 5556 | finish_wait(sk->sk_sleep, &wait); |
| 5557 | *err = error; |
| 5558 | return error; |
| 5559 | } |
| 5560 | |
| 5561 | /* Receive a datagram. |
| 5562 | * Note: This is pretty much the same routine as in core/datagram.c |
| 5563 | * with a few changes to make lksctp work. |
| 5564 | */ |
| 5565 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, |
| 5566 | int noblock, int *err) |
| 5567 | { |
| 5568 | int error; |
| 5569 | struct sk_buff *skb; |
| 5570 | long timeo; |
| 5571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | timeo = sock_rcvtimeo(sk, noblock); |
| 5573 | |
| 5574 | SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n", |
| 5575 | timeo, MAX_SCHEDULE_TIMEOUT); |
| 5576 | |
| 5577 | do { |
| 5578 | /* Again only user level code calls this function, |
| 5579 | * so nothing interrupt level |
| 5580 | * will suddenly eat the receive_queue. |
| 5581 | * |
| 5582 | * Look at current nfs client by the way... |
| 5583 | * However, this function was corrent in any case. 8) |
| 5584 | */ |
| 5585 | if (flags & MSG_PEEK) { |
Herbert Xu | 1e061ab | 2005-06-18 22:56:42 -0700 | [diff] [blame] | 5586 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | skb = skb_peek(&sk->sk_receive_queue); |
| 5588 | if (skb) |
| 5589 | atomic_inc(&skb->users); |
Herbert Xu | 1e061ab | 2005-06-18 22:56:42 -0700 | [diff] [blame] | 5590 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } else { |
| 5592 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 5593 | } |
| 5594 | |
| 5595 | if (skb) |
| 5596 | return skb; |
| 5597 | |
Neil Horman | 6736dc3 | 2005-12-02 20:30:06 -0800 | [diff] [blame] | 5598 | /* Caller is allowed not to check sk->sk_err before calling. */ |
| 5599 | error = sock_error(sk); |
| 5600 | if (error) |
| 5601 | goto no_packet; |
| 5602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 5604 | break; |
| 5605 | |
| 5606 | /* User doesn't want to wait. */ |
| 5607 | error = -EAGAIN; |
| 5608 | if (!timeo) |
| 5609 | goto no_packet; |
| 5610 | } while (sctp_wait_for_packet(sk, err, &timeo) == 0); |
| 5611 | |
| 5612 | return NULL; |
| 5613 | |
| 5614 | no_packet: |
| 5615 | *err = error; |
| 5616 | return NULL; |
| 5617 | } |
| 5618 | |
| 5619 | /* If sndbuf has changed, wake up per association sndbuf waiters. */ |
| 5620 | static void __sctp_write_space(struct sctp_association *asoc) |
| 5621 | { |
| 5622 | struct sock *sk = asoc->base.sk; |
| 5623 | struct socket *sock = sk->sk_socket; |
| 5624 | |
| 5625 | if ((sctp_wspace(asoc) > 0) && sock) { |
| 5626 | if (waitqueue_active(&asoc->wait)) |
| 5627 | wake_up_interruptible(&asoc->wait); |
| 5628 | |
| 5629 | if (sctp_writeable(sk)) { |
| 5630 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
| 5631 | wake_up_interruptible(sk->sk_sleep); |
| 5632 | |
| 5633 | /* Note that we try to include the Async I/O support |
| 5634 | * here by modeling from the current TCP/UDP code. |
| 5635 | * We have not tested with it yet. |
| 5636 | */ |
| 5637 | if (sock->fasync_list && |
| 5638 | !(sk->sk_shutdown & SEND_SHUTDOWN)) |
| 5639 | sock_wake_async(sock, 2, POLL_OUT); |
| 5640 | } |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | /* Do accounting for the sndbuf space. |
| 5645 | * Decrement the used sndbuf space of the corresponding association by the |
| 5646 | * data size which was just transmitted(freed). |
| 5647 | */ |
| 5648 | static void sctp_wfree(struct sk_buff *skb) |
| 5649 | { |
| 5650 | struct sctp_association *asoc; |
| 5651 | struct sctp_chunk *chunk; |
| 5652 | struct sock *sk; |
| 5653 | |
| 5654 | /* Get the saved chunk pointer. */ |
| 5655 | chunk = *((struct sctp_chunk **)(skb->cb)); |
| 5656 | asoc = chunk->asoc; |
| 5657 | sk = asoc->base.sk; |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 5658 | asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + |
| 5659 | sizeof(struct sk_buff) + |
| 5660 | sizeof(struct sctp_chunk); |
| 5661 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 5662 | atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
| 5663 | |
| 5664 | sock_wfree(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | __sctp_write_space(asoc); |
| 5666 | |
| 5667 | sctp_association_put(asoc); |
| 5668 | } |
| 5669 | |
Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 5670 | /* Do accounting for the receive space on the socket. |
| 5671 | * Accounting for the association is done in ulpevent.c |
| 5672 | * We set this as a destructor for the cloned data skbs so that |
| 5673 | * accounting is done at the correct time. |
| 5674 | */ |
| 5675 | void sctp_sock_rfree(struct sk_buff *skb) |
| 5676 | { |
| 5677 | struct sock *sk = skb->sk; |
| 5678 | struct sctp_ulpevent *event = sctp_skb2event(skb); |
| 5679 | |
| 5680 | atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); |
| 5681 | } |
| 5682 | |
| 5683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | /* Helper function to wait for space in the sndbuf. */ |
| 5685 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
| 5686 | size_t msg_len) |
| 5687 | { |
| 5688 | struct sock *sk = asoc->base.sk; |
| 5689 | int err = 0; |
| 5690 | long current_timeo = *timeo_p; |
| 5691 | DEFINE_WAIT(wait); |
| 5692 | |
| 5693 | 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] | 5694 | asoc, (long)(*timeo_p), msg_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
| 5696 | /* Increment the association's refcnt. */ |
| 5697 | sctp_association_hold(asoc); |
| 5698 | |
| 5699 | /* Wait on the association specific sndbuf space. */ |
| 5700 | for (;;) { |
| 5701 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 5702 | TASK_INTERRUPTIBLE); |
| 5703 | if (!*timeo_p) |
| 5704 | goto do_nonblock; |
| 5705 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
| 5706 | asoc->base.dead) |
| 5707 | goto do_error; |
| 5708 | if (signal_pending(current)) |
| 5709 | goto do_interrupted; |
| 5710 | if (msg_len <= sctp_wspace(asoc)) |
| 5711 | break; |
| 5712 | |
| 5713 | /* Let another process have a go. Since we are going |
| 5714 | * to sleep anyway. |
| 5715 | */ |
| 5716 | sctp_release_sock(sk); |
| 5717 | current_timeo = schedule_timeout(current_timeo); |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 5718 | BUG_ON(sk != asoc->base.sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | sctp_lock_sock(sk); |
| 5720 | |
| 5721 | *timeo_p = current_timeo; |
| 5722 | } |
| 5723 | |
| 5724 | out: |
| 5725 | finish_wait(&asoc->wait, &wait); |
| 5726 | |
| 5727 | /* Release the association's refcnt. */ |
| 5728 | sctp_association_put(asoc); |
| 5729 | |
| 5730 | return err; |
| 5731 | |
| 5732 | do_error: |
| 5733 | err = -EPIPE; |
| 5734 | goto out; |
| 5735 | |
| 5736 | do_interrupted: |
| 5737 | err = sock_intr_errno(*timeo_p); |
| 5738 | goto out; |
| 5739 | |
| 5740 | do_nonblock: |
| 5741 | err = -EAGAIN; |
| 5742 | goto out; |
| 5743 | } |
| 5744 | |
| 5745 | /* If socket sndbuf has changed, wake up all per association waiters. */ |
| 5746 | void sctp_write_space(struct sock *sk) |
| 5747 | { |
| 5748 | struct sctp_association *asoc; |
| 5749 | struct list_head *pos; |
| 5750 | |
| 5751 | /* Wake up the tasks in each wait queue. */ |
| 5752 | list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) { |
| 5753 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 5754 | __sctp_write_space(asoc); |
| 5755 | } |
| 5756 | } |
| 5757 | |
| 5758 | /* Is there any sndbuf space available on the socket? |
| 5759 | * |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 5760 | * 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] | 5761 | * associations on the same socket. For a UDP-style socket with |
| 5762 | * multiple associations, it is possible for it to be "unwriteable" |
| 5763 | * prematurely. I assume that this is acceptable because |
| 5764 | * a premature "unwriteable" is better than an accidental "writeable" which |
| 5765 | * would cause an unwanted block under certain circumstances. For the 1-1 |
| 5766 | * UDP-style sockets or TCP-style sockets, this code should work. |
| 5767 | * - Daisy |
| 5768 | */ |
| 5769 | static int sctp_writeable(struct sock *sk) |
| 5770 | { |
| 5771 | int amt = 0; |
| 5772 | |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 5773 | amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | if (amt < 0) |
| 5775 | amt = 0; |
| 5776 | return amt; |
| 5777 | } |
| 5778 | |
| 5779 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, |
| 5780 | * returns immediately with EINPROGRESS. |
| 5781 | */ |
| 5782 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) |
| 5783 | { |
| 5784 | struct sock *sk = asoc->base.sk; |
| 5785 | int err = 0; |
| 5786 | long current_timeo = *timeo_p; |
| 5787 | DEFINE_WAIT(wait); |
| 5788 | |
| 5789 | SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc, |
| 5790 | (long)(*timeo_p)); |
| 5791 | |
| 5792 | /* Increment the association's refcnt. */ |
| 5793 | sctp_association_hold(asoc); |
| 5794 | |
| 5795 | for (;;) { |
| 5796 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 5797 | TASK_INTERRUPTIBLE); |
| 5798 | if (!*timeo_p) |
| 5799 | goto do_nonblock; |
| 5800 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 5801 | break; |
| 5802 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
| 5803 | asoc->base.dead) |
| 5804 | goto do_error; |
| 5805 | if (signal_pending(current)) |
| 5806 | goto do_interrupted; |
| 5807 | |
| 5808 | if (sctp_state(asoc, ESTABLISHED)) |
| 5809 | break; |
| 5810 | |
| 5811 | /* Let another process have a go. Since we are going |
| 5812 | * to sleep anyway. |
| 5813 | */ |
| 5814 | sctp_release_sock(sk); |
| 5815 | current_timeo = schedule_timeout(current_timeo); |
| 5816 | sctp_lock_sock(sk); |
| 5817 | |
| 5818 | *timeo_p = current_timeo; |
| 5819 | } |
| 5820 | |
| 5821 | out: |
| 5822 | finish_wait(&asoc->wait, &wait); |
| 5823 | |
| 5824 | /* Release the association's refcnt. */ |
| 5825 | sctp_association_put(asoc); |
| 5826 | |
| 5827 | return err; |
| 5828 | |
| 5829 | do_error: |
Vlad Yasevich | 81845c2 | 2006-01-30 15:59:54 -0800 | [diff] [blame] | 5830 | if (asoc->init_err_counter + 1 > asoc->max_init_attempts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | err = -ETIMEDOUT; |
| 5832 | else |
| 5833 | err = -ECONNREFUSED; |
| 5834 | goto out; |
| 5835 | |
| 5836 | do_interrupted: |
| 5837 | err = sock_intr_errno(*timeo_p); |
| 5838 | goto out; |
| 5839 | |
| 5840 | do_nonblock: |
| 5841 | err = -EINPROGRESS; |
| 5842 | goto out; |
| 5843 | } |
| 5844 | |
| 5845 | static int sctp_wait_for_accept(struct sock *sk, long timeo) |
| 5846 | { |
| 5847 | struct sctp_endpoint *ep; |
| 5848 | int err = 0; |
| 5849 | DEFINE_WAIT(wait); |
| 5850 | |
| 5851 | ep = sctp_sk(sk)->ep; |
| 5852 | |
| 5853 | |
| 5854 | for (;;) { |
| 5855 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, |
| 5856 | TASK_INTERRUPTIBLE); |
| 5857 | |
| 5858 | if (list_empty(&ep->asocs)) { |
| 5859 | sctp_release_sock(sk); |
| 5860 | timeo = schedule_timeout(timeo); |
| 5861 | sctp_lock_sock(sk); |
| 5862 | } |
| 5863 | |
| 5864 | err = -EINVAL; |
| 5865 | if (!sctp_sstate(sk, LISTENING)) |
| 5866 | break; |
| 5867 | |
| 5868 | err = 0; |
| 5869 | if (!list_empty(&ep->asocs)) |
| 5870 | break; |
| 5871 | |
| 5872 | err = sock_intr_errno(timeo); |
| 5873 | if (signal_pending(current)) |
| 5874 | break; |
| 5875 | |
| 5876 | err = -EAGAIN; |
| 5877 | if (!timeo) |
| 5878 | break; |
| 5879 | } |
| 5880 | |
| 5881 | finish_wait(sk->sk_sleep, &wait); |
| 5882 | |
| 5883 | return err; |
| 5884 | } |
| 5885 | |
| 5886 | void sctp_wait_for_close(struct sock *sk, long timeout) |
| 5887 | { |
| 5888 | DEFINE_WAIT(wait); |
| 5889 | |
| 5890 | do { |
| 5891 | prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
| 5892 | if (list_empty(&sctp_sk(sk)->ep->asocs)) |
| 5893 | break; |
| 5894 | sctp_release_sock(sk); |
| 5895 | timeout = schedule_timeout(timeout); |
| 5896 | sctp_lock_sock(sk); |
| 5897 | } while (!signal_pending(current) && timeout); |
| 5898 | |
| 5899 | finish_wait(sk->sk_sleep, &wait); |
| 5900 | } |
| 5901 | |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 5902 | static void sctp_sock_rfree_frag(struct sk_buff *skb) |
| 5903 | { |
| 5904 | struct sk_buff *frag; |
| 5905 | |
| 5906 | if (!skb->data_len) |
| 5907 | goto done; |
| 5908 | |
| 5909 | /* Don't forget the fragments. */ |
| 5910 | for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) |
| 5911 | sctp_sock_rfree_frag(frag); |
| 5912 | |
| 5913 | done: |
| 5914 | sctp_sock_rfree(skb); |
| 5915 | } |
| 5916 | |
| 5917 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) |
| 5918 | { |
| 5919 | struct sk_buff *frag; |
| 5920 | |
| 5921 | if (!skb->data_len) |
| 5922 | goto done; |
| 5923 | |
| 5924 | /* Don't forget the fragments. */ |
| 5925 | for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) |
| 5926 | sctp_skb_set_owner_r_frag(frag, sk); |
| 5927 | |
| 5928 | done: |
| 5929 | sctp_skb_set_owner_r(skb, sk); |
| 5930 | } |
| 5931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | /* Populate the fields of the newsk from the oldsk and migrate the assoc |
| 5933 | * and its messages to the newsk. |
| 5934 | */ |
| 5935 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
| 5936 | struct sctp_association *assoc, |
| 5937 | sctp_socket_type_t type) |
| 5938 | { |
| 5939 | struct sctp_sock *oldsp = sctp_sk(oldsk); |
| 5940 | struct sctp_sock *newsp = sctp_sk(newsk); |
| 5941 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
| 5942 | struct sctp_endpoint *newep = newsp->ep; |
| 5943 | struct sk_buff *skb, *tmp; |
| 5944 | struct sctp_ulpevent *event; |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 5945 | int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | |
| 5947 | /* Migrate socket buffer sizes and all the socket level options to the |
| 5948 | * new socket. |
| 5949 | */ |
| 5950 | newsk->sk_sndbuf = oldsk->sk_sndbuf; |
| 5951 | newsk->sk_rcvbuf = oldsk->sk_rcvbuf; |
| 5952 | /* Brute force copy old sctp opt. */ |
| 5953 | inet_sk_copy_descendant(newsk, oldsk); |
| 5954 | |
| 5955 | /* Restore the ep value that was overwritten with the above structure |
| 5956 | * copy. |
| 5957 | */ |
| 5958 | newsp->ep = newep; |
| 5959 | newsp->hmac = NULL; |
| 5960 | |
| 5961 | /* Hook this new socket in to the bind_hash list. */ |
| 5962 | pp = sctp_sk(oldsk)->bind_hash; |
| 5963 | sk_add_bind_node(newsk, &pp->owner); |
| 5964 | sctp_sk(newsk)->bind_hash = pp; |
| 5965 | inet_sk(newsk)->num = inet_sk(oldsk)->num; |
| 5966 | |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 5967 | /* Copy the bind_addr list from the original endpoint to the new |
| 5968 | * endpoint so that we can handle restarts properly |
| 5969 | */ |
Vladislav Yasevich | eb5fa39 | 2006-08-22 00:23:13 -0700 | [diff] [blame] | 5970 | if (PF_INET6 == assoc->base.sk->sk_family) |
| 5971 | flags = SCTP_ADDR6_ALLOWED; |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 5972 | if (assoc->peer.ipv4_address) |
| 5973 | flags |= SCTP_ADDR4_PEERSUPP; |
| 5974 | if (assoc->peer.ipv6_address) |
| 5975 | flags |= SCTP_ADDR6_PEERSUPP; |
| 5976 | sctp_bind_addr_copy(&newsp->ep->base.bind_addr, |
| 5977 | &oldsp->ep->base.bind_addr, |
| 5978 | SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags); |
| 5979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | /* Move any messages in the old socket's receive queue that are for the |
| 5981 | * peeled off association to the new socket's receive queue. |
| 5982 | */ |
| 5983 | sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { |
| 5984 | event = sctp_skb2event(skb); |
| 5985 | if (event->asoc == assoc) { |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 5986 | sctp_sock_rfree_frag(skb); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 5987 | __skb_unlink(skb, &oldsk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | __skb_queue_tail(&newsk->sk_receive_queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 5989 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | /* Clean up any messages pending delivery due to partial |
| 5994 | * delivery. Three cases: |
| 5995 | * 1) No partial deliver; no work. |
| 5996 | * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. |
| 5997 | * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. |
| 5998 | */ |
| 5999 | skb_queue_head_init(&newsp->pd_lobby); |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6000 | atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6002 | if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | struct sk_buff_head *queue; |
| 6004 | |
| 6005 | /* Decide which queue to move pd_lobby skbs to. */ |
| 6006 | if (assoc->ulpq.pd_mode) { |
| 6007 | queue = &newsp->pd_lobby; |
| 6008 | } else |
| 6009 | queue = &newsk->sk_receive_queue; |
| 6010 | |
| 6011 | /* Walk through the pd_lobby, looking for skbs that |
| 6012 | * need moved to the new socket. |
| 6013 | */ |
| 6014 | sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { |
| 6015 | event = sctp_skb2event(skb); |
| 6016 | if (event->asoc == assoc) { |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6017 | sctp_sock_rfree_frag(skb); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 6018 | __skb_unlink(skb, &oldsp->pd_lobby); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | __skb_queue_tail(queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6020 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | } |
| 6022 | } |
| 6023 | |
| 6024 | /* Clear up any skbs waiting for the partial |
| 6025 | * delivery to finish. |
| 6026 | */ |
| 6027 | if (assoc->ulpq.pd_mode) |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6028 | sctp_clear_pd(oldsk, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | |
| 6030 | } |
| 6031 | |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 6032 | sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) { |
| 6033 | sctp_sock_rfree_frag(skb); |
| 6034 | sctp_skb_set_owner_r_frag(skb, newsk); |
| 6035 | } |
| 6036 | |
| 6037 | sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) { |
| 6038 | sctp_sock_rfree_frag(skb); |
| 6039 | sctp_skb_set_owner_r_frag(skb, newsk); |
| 6040 | } |
| 6041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | /* Set the type of socket to indicate that it is peeled off from the |
| 6043 | * original UDP-style socket or created with the accept() call on a |
| 6044 | * TCP-style socket.. |
| 6045 | */ |
| 6046 | newsp->type = type; |
| 6047 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6048 | /* Mark the new socket "in-use" by the user so that any packets |
| 6049 | * that may arrive on the association after we've moved it are |
| 6050 | * queued to the backlog. This prevents a potential race between |
| 6051 | * backlog processing on the old socket and new-packet processing |
| 6052 | * on the new socket. |
| 6053 | */ |
| 6054 | sctp_lock_sock(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | sctp_assoc_migrate(assoc, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | |
| 6057 | /* If the association on the newsk is already closed before accept() |
| 6058 | * is called, set RCV_SHUTDOWN flag. |
| 6059 | */ |
| 6060 | if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) |
| 6061 | newsk->sk_shutdown |= RCV_SHUTDOWN; |
| 6062 | |
| 6063 | newsk->sk_state = SCTP_SS_ESTABLISHED; |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 6064 | sctp_release_sock(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | } |
| 6066 | |
| 6067 | /* This proto struct describes the ULP interface for SCTP. */ |
| 6068 | struct proto sctp_prot = { |
| 6069 | .name = "SCTP", |
| 6070 | .owner = THIS_MODULE, |
| 6071 | .close = sctp_close, |
| 6072 | .connect = sctp_connect, |
| 6073 | .disconnect = sctp_disconnect, |
| 6074 | .accept = sctp_accept, |
| 6075 | .ioctl = sctp_ioctl, |
| 6076 | .init = sctp_init_sock, |
| 6077 | .destroy = sctp_destroy_sock, |
| 6078 | .shutdown = sctp_shutdown, |
| 6079 | .setsockopt = sctp_setsockopt, |
| 6080 | .getsockopt = sctp_getsockopt, |
| 6081 | .sendmsg = sctp_sendmsg, |
| 6082 | .recvmsg = sctp_recvmsg, |
| 6083 | .bind = sctp_bind, |
| 6084 | .backlog_rcv = sctp_backlog_rcv, |
| 6085 | .hash = sctp_hash, |
| 6086 | .unhash = sctp_unhash, |
| 6087 | .get_port = sctp_get_port, |
| 6088 | .obj_size = sizeof(struct sctp_sock), |
| 6089 | }; |
| 6090 | |
| 6091 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 6092 | struct proto sctpv6_prot = { |
| 6093 | .name = "SCTPv6", |
| 6094 | .owner = THIS_MODULE, |
| 6095 | .close = sctp_close, |
| 6096 | .connect = sctp_connect, |
| 6097 | .disconnect = sctp_disconnect, |
| 6098 | .accept = sctp_accept, |
| 6099 | .ioctl = sctp_ioctl, |
| 6100 | .init = sctp_init_sock, |
| 6101 | .destroy = sctp_destroy_sock, |
| 6102 | .shutdown = sctp_shutdown, |
| 6103 | .setsockopt = sctp_setsockopt, |
| 6104 | .getsockopt = sctp_getsockopt, |
| 6105 | .sendmsg = sctp_sendmsg, |
| 6106 | .recvmsg = sctp_recvmsg, |
| 6107 | .bind = sctp_bind, |
| 6108 | .backlog_rcv = sctp_backlog_rcv, |
| 6109 | .hash = sctp_hash, |
| 6110 | .unhash = sctp_unhash, |
| 6111 | .get_port = sctp_get_port, |
| 6112 | .obj_size = sizeof(struct sctp6_sock), |
| 6113 | }; |
| 6114 | #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */ |