1 /* SCTP kernel 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 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 * This SCTP 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 * This SCTP 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, see 32 * <http://www.gnu.org/licenses/>. 33 * 34 * Please send any bug reports or fixes you make to the 35 * email address(es): 36 * lksctp developers <linux-sctp@vger.kernel.org> 37 * 38 * Written or modified by: 39 * La Monte H.P. Yarroll <piggy@acm.org> 40 * Narasimha Budihal <narsi@refcode.org> 41 * Karl Knutson <karl@athena.chicago.il.us> 42 * Jon Grimm <jgrimm@us.ibm.com> 43 * Xingang Guo <xingang.guo@intel.com> 44 * Daisy Chang <daisyc@us.ibm.com> 45 * Sridhar Samudrala <samudrala@us.ibm.com> 46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> 47 * Ardelle Fan <ardelle.fan@intel.com> 48 * Ryan Layer <rmlayer@us.ibm.com> 49 * Anup Pemmaiah <pemmaiah@cc.usu.edu> 50 * Kevin Gao <kevin.gao@intel.com> 51 */ 52 53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 54 55 #include <crypto/hash.h> 56 #include <linux/types.h> 57 #include <linux/kernel.h> 58 #include <linux/wait.h> 59 #include <linux/time.h> 60 #include <linux/sched/signal.h> 61 #include <linux/ip.h> 62 #include <linux/capability.h> 63 #include <linux/fcntl.h> 64 #include <linux/poll.h> 65 #include <linux/init.h> 66 #include <linux/slab.h> 67 #include <linux/file.h> 68 #include <linux/compat.h> 69 70 #include <net/ip.h> 71 #include <net/icmp.h> 72 #include <net/route.h> 73 #include <net/ipv6.h> 74 #include <net/inet_common.h> 75 #include <net/busy_poll.h> 76 77 #include <linux/socket.h> /* for sa_family_t */ 78 #include <linux/export.h> 79 #include <net/sock.h> 80 #include <net/sctp/sctp.h> 81 #include <net/sctp/sm.h> 82 #include <net/sctp/stream_sched.h> 83 84 /* Forward declarations for internal helper functions. */ 85 static int sctp_writeable(struct sock *sk); 86 static void sctp_wfree(struct sk_buff *skb); 87 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, 88 size_t msg_len); 89 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p); 90 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); 91 static int sctp_wait_for_accept(struct sock *sk, long timeo); 92 static void sctp_wait_for_close(struct sock *sk, long timeo); 93 static void sctp_destruct_sock(struct sock *sk); 94 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 95 union sctp_addr *addr, int len); 96 static int sctp_bindx_add(struct sock *, struct sockaddr *, int); 97 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); 98 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); 99 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); 100 static int sctp_send_asconf(struct sctp_association *asoc, 101 struct sctp_chunk *chunk); 102 static int sctp_do_bind(struct sock *, union sctp_addr *, int); 103 static int sctp_autobind(struct sock *sk); 104 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 105 struct sctp_association *assoc, 106 enum sctp_socket_type type); 107 108 static unsigned long sctp_memory_pressure; 109 static atomic_long_t sctp_memory_allocated; 110 struct percpu_counter sctp_sockets_allocated; 111 112 static void sctp_enter_memory_pressure(struct sock *sk) 113 { 114 sctp_memory_pressure = 1; 115 } 116 117 118 /* Get the sndbuf space available at the time on the association. */ 119 static inline int sctp_wspace(struct sctp_association *asoc) 120 { 121 int amt; 122 123 if (asoc->ep->sndbuf_policy) 124 amt = asoc->sndbuf_used; 125 else 126 amt = sk_wmem_alloc_get(asoc->base.sk); 127 128 if (amt >= asoc->base.sk->sk_sndbuf) { 129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK) 130 amt = 0; 131 else { 132 amt = sk_stream_wspace(asoc->base.sk); 133 if (amt < 0) 134 amt = 0; 135 } 136 } else { 137 amt = asoc->base.sk->sk_sndbuf - amt; 138 } 139 return amt; 140 } 141 142 /* Increment the used sndbuf space count of the corresponding association by 143 * the size of the outgoing data chunk. 144 * Also, set the skb destructor for sndbuf accounting later. 145 * 146 * Since it is always 1-1 between chunk and skb, and also a new skb is always 147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the 148 * destructor in the data chunk skb for the purpose of the sndbuf space 149 * tracking. 150 */ 151 static inline void sctp_set_owner_w(struct sctp_chunk *chunk) 152 { 153 struct sctp_association *asoc = chunk->asoc; 154 struct sock *sk = asoc->base.sk; 155 156 /* The sndbuf space is tracked per association. */ 157 sctp_association_hold(asoc); 158 159 if (chunk->shkey) 160 sctp_auth_shkey_hold(chunk->shkey); 161 162 skb_set_owner_w(chunk->skb, sk); 163 164 chunk->skb->destructor = sctp_wfree; 165 /* Save the chunk pointer in skb for sctp_wfree to use later. */ 166 skb_shinfo(chunk->skb)->destructor_arg = chunk; 167 168 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + 169 sizeof(struct sk_buff) + 170 sizeof(struct sctp_chunk); 171 172 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); 173 sk->sk_wmem_queued += chunk->skb->truesize; 174 sk_mem_charge(sk, chunk->skb->truesize); 175 } 176 177 static void sctp_clear_owner_w(struct sctp_chunk *chunk) 178 { 179 skb_orphan(chunk->skb); 180 } 181 182 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, 183 void (*cb)(struct sctp_chunk *)) 184 185 { 186 struct sctp_outq *q = &asoc->outqueue; 187 struct sctp_transport *t; 188 struct sctp_chunk *chunk; 189 190 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) 191 list_for_each_entry(chunk, &t->transmitted, transmitted_list) 192 cb(chunk); 193 194 list_for_each_entry(chunk, &q->retransmit, transmitted_list) 195 cb(chunk); 196 197 list_for_each_entry(chunk, &q->sacked, transmitted_list) 198 cb(chunk); 199 200 list_for_each_entry(chunk, &q->abandoned, transmitted_list) 201 cb(chunk); 202 203 list_for_each_entry(chunk, &q->out_chunk_list, list) 204 cb(chunk); 205 } 206 207 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk, 208 void (*cb)(struct sk_buff *, struct sock *)) 209 210 { 211 struct sk_buff *skb, *tmp; 212 213 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp) 214 cb(skb, sk); 215 216 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp) 217 cb(skb, sk); 218 219 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp) 220 cb(skb, sk); 221 } 222 223 /* Verify that this is a valid address. */ 224 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, 225 int len) 226 { 227 struct sctp_af *af; 228 229 /* Verify basic sockaddr. */ 230 af = sctp_sockaddr_af(sctp_sk(sk), addr, len); 231 if (!af) 232 return -EINVAL; 233 234 /* Is this a valid SCTP address? */ 235 if (!af->addr_valid(addr, sctp_sk(sk), NULL)) 236 return -EINVAL; 237 238 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) 239 return -EINVAL; 240 241 return 0; 242 } 243 244 /* Look up the association by its id. If this is not a UDP-style 245 * socket, the ID field is always ignored. 246 */ 247 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) 248 { 249 struct sctp_association *asoc = NULL; 250 251 /* If this is not a UDP-style socket, assoc id should be ignored. */ 252 if (!sctp_style(sk, UDP)) { 253 /* Return NULL if the socket state is not ESTABLISHED. It 254 * could be a TCP-style listening socket or a socket which 255 * hasn't yet called connect() to establish an association. 256 */ 257 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING)) 258 return NULL; 259 260 /* Get the first and the only association from the list. */ 261 if (!list_empty(&sctp_sk(sk)->ep->asocs)) 262 asoc = list_entry(sctp_sk(sk)->ep->asocs.next, 263 struct sctp_association, asocs); 264 return asoc; 265 } 266 267 /* Otherwise this is a UDP-style socket. */ 268 if (!id || (id == (sctp_assoc_t)-1)) 269 return NULL; 270 271 spin_lock_bh(&sctp_assocs_id_lock); 272 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); 273 spin_unlock_bh(&sctp_assocs_id_lock); 274 275 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) 276 return NULL; 277 278 return asoc; 279 } 280 281 /* Look up the transport from an address and an assoc id. If both address and 282 * id are specified, the associations matching the address and the id should be 283 * the same. 284 */ 285 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, 286 struct sockaddr_storage *addr, 287 sctp_assoc_t id) 288 { 289 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; 290 struct sctp_af *af = sctp_get_af_specific(addr->ss_family); 291 union sctp_addr *laddr = (union sctp_addr *)addr; 292 struct sctp_transport *transport; 293 294 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len)) 295 return NULL; 296 297 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, 298 laddr, 299 &transport); 300 301 if (!addr_asoc) 302 return NULL; 303 304 id_asoc = sctp_id2assoc(sk, id); 305 if (id_asoc && (id_asoc != addr_asoc)) 306 return NULL; 307 308 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), 309 (union sctp_addr *)addr); 310 311 return transport; 312 } 313 314 /* API 3.1.2 bind() - UDP Style Syntax 315 * The syntax of bind() is, 316 * 317 * ret = bind(int sd, struct sockaddr *addr, int addrlen); 318 * 319 * sd - the socket descriptor returned by socket(). 320 * addr - the address structure (struct sockaddr_in or struct 321 * sockaddr_in6 [RFC 2553]), 322 * addr_len - the size of the address structure. 323 */ 324 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len) 325 { 326 int retval = 0; 327 328 lock_sock(sk); 329 330 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk, 331 addr, addr_len); 332 333 /* Disallow binding twice. */ 334 if (!sctp_sk(sk)->ep->base.bind_addr.port) 335 retval = sctp_do_bind(sk, (union sctp_addr *)addr, 336 addr_len); 337 else 338 retval = -EINVAL; 339 340 release_sock(sk); 341 342 return retval; 343 } 344 345 static long sctp_get_port_local(struct sock *, union sctp_addr *); 346 347 /* Verify this is a valid sockaddr. */ 348 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 349 union sctp_addr *addr, int len) 350 { 351 struct sctp_af *af; 352 353 /* Check minimum size. */ 354 if (len < sizeof (struct sockaddr)) 355 return NULL; 356 357 if (!opt->pf->af_supported(addr->sa.sa_family, opt)) 358 return NULL; 359 360 /* V4 mapped address are really of AF_INET family */ 361 if (addr->sa.sa_family == AF_INET6 && 362 ipv6_addr_v4mapped(&addr->v6.sin6_addr) && 363 !opt->pf->af_supported(AF_INET, opt)) 364 return NULL; 365 366 /* If we get this far, af is valid. */ 367 af = sctp_get_af_specific(addr->sa.sa_family); 368 369 if (len < af->sockaddr_len) 370 return NULL; 371 372 return af; 373 } 374 375 /* Bind a local address either to an endpoint or to an association. */ 376 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) 377 { 378 struct net *net = sock_net(sk); 379 struct sctp_sock *sp = sctp_sk(sk); 380 struct sctp_endpoint *ep = sp->ep; 381 struct sctp_bind_addr *bp = &ep->base.bind_addr; 382 struct sctp_af *af; 383 unsigned short snum; 384 int ret = 0; 385 386 /* Common sockaddr verification. */ 387 af = sctp_sockaddr_af(sp, addr, len); 388 if (!af) { 389 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n", 390 __func__, sk, addr, len); 391 return -EINVAL; 392 } 393 394 snum = ntohs(addr->v4.sin_port); 395 396 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n", 397 __func__, sk, &addr->sa, bp->port, snum, len); 398 399 /* PF specific bind() address verification. */ 400 if (!sp->pf->bind_verify(sp, addr)) 401 return -EADDRNOTAVAIL; 402 403 /* We must either be unbound, or bind to the same port. 404 * It's OK to allow 0 ports if we are already bound. 405 * We'll just inhert an already bound port in this case 406 */ 407 if (bp->port) { 408 if (!snum) 409 snum = bp->port; 410 else if (snum != bp->port) { 411 pr_debug("%s: new port %d doesn't match existing port " 412 "%d\n", __func__, snum, bp->port); 413 return -EINVAL; 414 } 415 } 416 417 if (snum && snum < inet_prot_sock(net) && 418 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 419 return -EACCES; 420 421 /* See if the address matches any of the addresses we may have 422 * already bound before checking against other endpoints. 423 */ 424 if (sctp_bind_addr_match(bp, addr, sp)) 425 return -EINVAL; 426 427 /* Make sure we are allowed to bind here. 428 * The function sctp_get_port_local() does duplicate address 429 * detection. 430 */ 431 addr->v4.sin_port = htons(snum); 432 if ((ret = sctp_get_port_local(sk, addr))) { 433 return -EADDRINUSE; 434 } 435 436 /* Refresh ephemeral port. */ 437 if (!bp->port) 438 bp->port = inet_sk(sk)->inet_num; 439 440 /* Add the address to the bind address list. 441 * Use GFP_ATOMIC since BHs will be disabled. 442 */ 443 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len, 444 SCTP_ADDR_SRC, GFP_ATOMIC); 445 446 /* Copy back into socket for getsockname() use. */ 447 if (!ret) { 448 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); 449 sp->pf->to_sk_saddr(addr, sk); 450 } 451 452 return ret; 453 } 454 455 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks 456 * 457 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged 458 * at any one time. If a sender, after sending an ASCONF chunk, decides 459 * it needs to transfer another ASCONF Chunk, it MUST wait until the 460 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a 461 * subsequent ASCONF. Note this restriction binds each side, so at any 462 * time two ASCONF may be in-transit on any given association (one sent 463 * from each endpoint). 464 */ 465 static int sctp_send_asconf(struct sctp_association *asoc, 466 struct sctp_chunk *chunk) 467 { 468 struct net *net = sock_net(asoc->base.sk); 469 int retval = 0; 470 471 /* If there is an outstanding ASCONF chunk, queue it for later 472 * transmission. 473 */ 474 if (asoc->addip_last_asconf) { 475 list_add_tail(&chunk->list, &asoc->addip_chunk_list); 476 goto out; 477 } 478 479 /* Hold the chunk until an ASCONF_ACK is received. */ 480 sctp_chunk_hold(chunk); 481 retval = sctp_primitive_ASCONF(net, asoc, chunk); 482 if (retval) 483 sctp_chunk_free(chunk); 484 else 485 asoc->addip_last_asconf = chunk; 486 487 out: 488 return retval; 489 } 490 491 /* Add a list of addresses as bind addresses to local endpoint or 492 * association. 493 * 494 * Basically run through each address specified in the addrs/addrcnt 495 * array/length pair, determine if it is IPv6 or IPv4 and call 496 * sctp_do_bind() on it. 497 * 498 * If any of them fails, then the operation will be reversed and the 499 * ones that were added will be removed. 500 * 501 * Only sctp_setsockopt_bindx() is supposed to call this function. 502 */ 503 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) 504 { 505 int cnt; 506 int retval = 0; 507 void *addr_buf; 508 struct sockaddr *sa_addr; 509 struct sctp_af *af; 510 511 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk, 512 addrs, addrcnt); 513 514 addr_buf = addrs; 515 for (cnt = 0; cnt < addrcnt; cnt++) { 516 /* The list may contain either IPv4 or IPv6 address; 517 * determine the address length for walking thru the list. 518 */ 519 sa_addr = addr_buf; 520 af = sctp_get_af_specific(sa_addr->sa_family); 521 if (!af) { 522 retval = -EINVAL; 523 goto err_bindx_add; 524 } 525 526 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, 527 af->sockaddr_len); 528 529 addr_buf += af->sockaddr_len; 530 531 err_bindx_add: 532 if (retval < 0) { 533 /* Failed. Cleanup the ones that have been added */ 534 if (cnt > 0) 535 sctp_bindx_rem(sk, addrs, cnt); 536 return retval; 537 } 538 } 539 540 return retval; 541 } 542 543 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the 544 * associations that are part of the endpoint indicating that a list of local 545 * addresses are added to the endpoint. 546 * 547 * If any of the addresses is already in the bind address list of the 548 * association, we do not send the chunk for that association. But it will not 549 * affect other associations. 550 * 551 * Only sctp_setsockopt_bindx() is supposed to call this function. 552 */ 553 static int sctp_send_asconf_add_ip(struct sock *sk, 554 struct sockaddr *addrs, 555 int addrcnt) 556 { 557 struct net *net = sock_net(sk); 558 struct sctp_sock *sp; 559 struct sctp_endpoint *ep; 560 struct sctp_association *asoc; 561 struct sctp_bind_addr *bp; 562 struct sctp_chunk *chunk; 563 struct sctp_sockaddr_entry *laddr; 564 union sctp_addr *addr; 565 union sctp_addr saveaddr; 566 void *addr_buf; 567 struct sctp_af *af; 568 struct list_head *p; 569 int i; 570 int retval = 0; 571 572 if (!net->sctp.addip_enable) 573 return retval; 574 575 sp = sctp_sk(sk); 576 ep = sp->ep; 577 578 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 579 __func__, sk, addrs, addrcnt); 580 581 list_for_each_entry(asoc, &ep->asocs, asocs) { 582 if (!asoc->peer.asconf_capable) 583 continue; 584 585 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) 586 continue; 587 588 if (!sctp_state(asoc, ESTABLISHED)) 589 continue; 590 591 /* Check if any address in the packed array of addresses is 592 * in the bind address list of the association. If so, 593 * do not send the asconf chunk to its peer, but continue with 594 * other associations. 595 */ 596 addr_buf = addrs; 597 for (i = 0; i < addrcnt; i++) { 598 addr = addr_buf; 599 af = sctp_get_af_specific(addr->v4.sin_family); 600 if (!af) { 601 retval = -EINVAL; 602 goto out; 603 } 604 605 if (sctp_assoc_lookup_laddr(asoc, addr)) 606 break; 607 608 addr_buf += af->sockaddr_len; 609 } 610 if (i < addrcnt) 611 continue; 612 613 /* Use the first valid address in bind addr list of 614 * association as Address Parameter of ASCONF CHUNK. 615 */ 616 bp = &asoc->base.bind_addr; 617 p = bp->address_list.next; 618 laddr = list_entry(p, struct sctp_sockaddr_entry, list); 619 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, 620 addrcnt, SCTP_PARAM_ADD_IP); 621 if (!chunk) { 622 retval = -ENOMEM; 623 goto out; 624 } 625 626 /* Add the new addresses to the bind address list with 627 * use_as_src set to 0. 628 */ 629 addr_buf = addrs; 630 for (i = 0; i < addrcnt; i++) { 631 addr = addr_buf; 632 af = sctp_get_af_specific(addr->v4.sin_family); 633 memcpy(&saveaddr, addr, af->sockaddr_len); 634 retval = sctp_add_bind_addr(bp, &saveaddr, 635 sizeof(saveaddr), 636 SCTP_ADDR_NEW, GFP_ATOMIC); 637 addr_buf += af->sockaddr_len; 638 } 639 if (asoc->src_out_of_asoc_ok) { 640 struct sctp_transport *trans; 641 642 list_for_each_entry(trans, 643 &asoc->peer.transport_addr_list, transports) { 644 /* Clear the source and route cache */ 645 sctp_transport_dst_release(trans); 646 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, 647 2*asoc->pathmtu, 4380)); 648 trans->ssthresh = asoc->peer.i.a_rwnd; 649 trans->rto = asoc->rto_initial; 650 sctp_max_rto(asoc, trans); 651 trans->rtt = trans->srtt = trans->rttvar = 0; 652 sctp_transport_route(trans, NULL, 653 sctp_sk(asoc->base.sk)); 654 } 655 } 656 retval = sctp_send_asconf(asoc, chunk); 657 } 658 659 out: 660 return retval; 661 } 662 663 /* Remove a list of addresses from bind addresses list. Do not remove the 664 * last address. 665 * 666 * Basically run through each address specified in the addrs/addrcnt 667 * array/length pair, determine if it is IPv6 or IPv4 and call 668 * sctp_del_bind() on it. 669 * 670 * If any of them fails, then the operation will be reversed and the 671 * ones that were removed will be added back. 672 * 673 * At least one address has to be left; if only one address is 674 * available, the operation will return -EBUSY. 675 * 676 * Only sctp_setsockopt_bindx() is supposed to call this function. 677 */ 678 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) 679 { 680 struct sctp_sock *sp = sctp_sk(sk); 681 struct sctp_endpoint *ep = sp->ep; 682 int cnt; 683 struct sctp_bind_addr *bp = &ep->base.bind_addr; 684 int retval = 0; 685 void *addr_buf; 686 union sctp_addr *sa_addr; 687 struct sctp_af *af; 688 689 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 690 __func__, sk, addrs, addrcnt); 691 692 addr_buf = addrs; 693 for (cnt = 0; cnt < addrcnt; cnt++) { 694 /* If the bind address list is empty or if there is only one 695 * bind address, there is nothing more to be removed (we need 696 * at least one address here). 697 */ 698 if (list_empty(&bp->address_list) || 699 (sctp_list_single_entry(&bp->address_list))) { 700 retval = -EBUSY; 701 goto err_bindx_rem; 702 } 703 704 sa_addr = addr_buf; 705 af = sctp_get_af_specific(sa_addr->sa.sa_family); 706 if (!af) { 707 retval = -EINVAL; 708 goto err_bindx_rem; 709 } 710 711 if (!af->addr_valid(sa_addr, sp, NULL)) { 712 retval = -EADDRNOTAVAIL; 713 goto err_bindx_rem; 714 } 715 716 if (sa_addr->v4.sin_port && 717 sa_addr->v4.sin_port != htons(bp->port)) { 718 retval = -EINVAL; 719 goto err_bindx_rem; 720 } 721 722 if (!sa_addr->v4.sin_port) 723 sa_addr->v4.sin_port = htons(bp->port); 724 725 /* FIXME - There is probably a need to check if sk->sk_saddr and 726 * sk->sk_rcv_addr are currently set to one of the addresses to 727 * be removed. This is something which needs to be looked into 728 * when we are fixing the outstanding issues with multi-homing 729 * socket routing and failover schemes. Refer to comments in 730 * sctp_do_bind(). -daisy 731 */ 732 retval = sctp_del_bind_addr(bp, sa_addr); 733 734 addr_buf += af->sockaddr_len; 735 err_bindx_rem: 736 if (retval < 0) { 737 /* Failed. Add the ones that has been removed back */ 738 if (cnt > 0) 739 sctp_bindx_add(sk, addrs, cnt); 740 return retval; 741 } 742 } 743 744 return retval; 745 } 746 747 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of 748 * the associations that are part of the endpoint indicating that a list of 749 * local addresses are removed from the endpoint. 750 * 751 * If any of the addresses is already in the bind address list of the 752 * association, we do not send the chunk for that association. But it will not 753 * affect other associations. 754 * 755 * Only sctp_setsockopt_bindx() is supposed to call this function. 756 */ 757 static int sctp_send_asconf_del_ip(struct sock *sk, 758 struct sockaddr *addrs, 759 int addrcnt) 760 { 761 struct net *net = sock_net(sk); 762 struct sctp_sock *sp; 763 struct sctp_endpoint *ep; 764 struct sctp_association *asoc; 765 struct sctp_transport *transport; 766 struct sctp_bind_addr *bp; 767 struct sctp_chunk *chunk; 768 union sctp_addr *laddr; 769 void *addr_buf; 770 struct sctp_af *af; 771 struct sctp_sockaddr_entry *saddr; 772 int i; 773 int retval = 0; 774 int stored = 0; 775 776 chunk = NULL; 777 if (!net->sctp.addip_enable) 778 return retval; 779 780 sp = sctp_sk(sk); 781 ep = sp->ep; 782 783 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 784 __func__, sk, addrs, addrcnt); 785 786 list_for_each_entry(asoc, &ep->asocs, asocs) { 787 788 if (!asoc->peer.asconf_capable) 789 continue; 790 791 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) 792 continue; 793 794 if (!sctp_state(asoc, ESTABLISHED)) 795 continue; 796 797 /* Check if any address in the packed array of addresses is 798 * not present in the bind address list of the association. 799 * If so, do not send the asconf chunk to its peer, but 800 * continue with other associations. 801 */ 802 addr_buf = addrs; 803 for (i = 0; i < addrcnt; i++) { 804 laddr = addr_buf; 805 af = sctp_get_af_specific(laddr->v4.sin_family); 806 if (!af) { 807 retval = -EINVAL; 808 goto out; 809 } 810 811 if (!sctp_assoc_lookup_laddr(asoc, laddr)) 812 break; 813 814 addr_buf += af->sockaddr_len; 815 } 816 if (i < addrcnt) 817 continue; 818 819 /* Find one address in the association's bind address list 820 * that is not in the packed array of addresses. This is to 821 * make sure that we do not delete all the addresses in the 822 * association. 823 */ 824 bp = &asoc->base.bind_addr; 825 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, 826 addrcnt, sp); 827 if ((laddr == NULL) && (addrcnt == 1)) { 828 if (asoc->asconf_addr_del_pending) 829 continue; 830 asoc->asconf_addr_del_pending = 831 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC); 832 if (asoc->asconf_addr_del_pending == NULL) { 833 retval = -ENOMEM; 834 goto out; 835 } 836 asoc->asconf_addr_del_pending->sa.sa_family = 837 addrs->sa_family; 838 asoc->asconf_addr_del_pending->v4.sin_port = 839 htons(bp->port); 840 if (addrs->sa_family == AF_INET) { 841 struct sockaddr_in *sin; 842 843 sin = (struct sockaddr_in *)addrs; 844 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; 845 } else if (addrs->sa_family == AF_INET6) { 846 struct sockaddr_in6 *sin6; 847 848 sin6 = (struct sockaddr_in6 *)addrs; 849 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; 850 } 851 852 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n", 853 __func__, asoc, &asoc->asconf_addr_del_pending->sa, 854 asoc->asconf_addr_del_pending); 855 856 asoc->src_out_of_asoc_ok = 1; 857 stored = 1; 858 goto skip_mkasconf; 859 } 860 861 if (laddr == NULL) 862 return -EINVAL; 863 864 /* We do not need RCU protection throughout this loop 865 * because this is done under a socket lock from the 866 * setsockopt call. 867 */ 868 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, 869 SCTP_PARAM_DEL_IP); 870 if (!chunk) { 871 retval = -ENOMEM; 872 goto out; 873 } 874 875 skip_mkasconf: 876 /* Reset use_as_src flag for the addresses in the bind address 877 * list that are to be deleted. 878 */ 879 addr_buf = addrs; 880 for (i = 0; i < addrcnt; i++) { 881 laddr = addr_buf; 882 af = sctp_get_af_specific(laddr->v4.sin_family); 883 list_for_each_entry(saddr, &bp->address_list, list) { 884 if (sctp_cmp_addr_exact(&saddr->a, laddr)) 885 saddr->state = SCTP_ADDR_DEL; 886 } 887 addr_buf += af->sockaddr_len; 888 } 889 890 /* Update the route and saddr entries for all the transports 891 * as some of the addresses in the bind address list are 892 * about to be deleted and cannot be used as source addresses. 893 */ 894 list_for_each_entry(transport, &asoc->peer.transport_addr_list, 895 transports) { 896 sctp_transport_dst_release(transport); 897 sctp_transport_route(transport, NULL, 898 sctp_sk(asoc->base.sk)); 899 } 900 901 if (stored) 902 /* We don't need to transmit ASCONF */ 903 continue; 904 retval = sctp_send_asconf(asoc, chunk); 905 } 906 out: 907 return retval; 908 } 909 910 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */ 911 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) 912 { 913 struct sock *sk = sctp_opt2sk(sp); 914 union sctp_addr *addr; 915 struct sctp_af *af; 916 917 /* It is safe to write port space in caller. */ 918 addr = &addrw->a; 919 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); 920 af = sctp_get_af_specific(addr->sa.sa_family); 921 if (!af) 922 return -EINVAL; 923 if (sctp_verify_addr(sk, addr, af->sockaddr_len)) 924 return -EINVAL; 925 926 if (addrw->state == SCTP_ADDR_NEW) 927 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); 928 else 929 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); 930 } 931 932 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() 933 * 934 * API 8.1 935 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, 936 * int flags); 937 * 938 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 939 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 940 * or IPv6 addresses. 941 * 942 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 943 * Section 3.1.2 for this usage. 944 * 945 * addrs is a pointer to an array of one or more socket addresses. Each 946 * address is contained in its appropriate structure (i.e. struct 947 * sockaddr_in or struct sockaddr_in6) the family of the address type 948 * must be used to distinguish the address length (note that this 949 * representation is termed a "packed array" of addresses). The caller 950 * specifies the number of addresses in the array with addrcnt. 951 * 952 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns 953 * -1, and sets errno to the appropriate error code. 954 * 955 * For SCTP, the port given in each socket address must be the same, or 956 * sctp_bindx() will fail, setting errno to EINVAL. 957 * 958 * The flags parameter is formed from the bitwise OR of zero or more of 959 * the following currently defined flags: 960 * 961 * SCTP_BINDX_ADD_ADDR 962 * 963 * SCTP_BINDX_REM_ADDR 964 * 965 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the 966 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given 967 * addresses from the association. The two flags are mutually exclusive; 968 * if both are given, sctp_bindx() will fail with EINVAL. A caller may 969 * not remove all addresses from an association; sctp_bindx() will 970 * reject such an attempt with EINVAL. 971 * 972 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate 973 * additional addresses with an endpoint after calling bind(). Or use 974 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening 975 * socket is associated with so that no new association accepted will be 976 * associated with those addresses. If the endpoint supports dynamic 977 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a 978 * endpoint to send the appropriate message to the peer to change the 979 * peers address lists. 980 * 981 * Adding and removing addresses from a connected association is 982 * optional functionality. Implementations that do not support this 983 * functionality should return EOPNOTSUPP. 984 * 985 * Basically do nothing but copying the addresses from user to kernel 986 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. 987 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() 988 * from userspace. 989 * 990 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 991 * it. 992 * 993 * sk The sk of the socket 994 * addrs The pointer to the addresses in user land 995 * addrssize Size of the addrs buffer 996 * op Operation to perform (add or remove, see the flags of 997 * sctp_bindx) 998 * 999 * Returns 0 if ok, <0 errno code on error. 1000 */ 1001 static int sctp_setsockopt_bindx(struct sock *sk, 1002 struct sockaddr __user *addrs, 1003 int addrs_size, int op) 1004 { 1005 struct sockaddr *kaddrs; 1006 int err; 1007 int addrcnt = 0; 1008 int walk_size = 0; 1009 struct sockaddr *sa_addr; 1010 void *addr_buf; 1011 struct sctp_af *af; 1012 1013 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n", 1014 __func__, sk, addrs, addrs_size, op); 1015 1016 if (unlikely(addrs_size <= 0)) 1017 return -EINVAL; 1018 1019 kaddrs = vmemdup_user(addrs, addrs_size); 1020 if (unlikely(IS_ERR(kaddrs))) 1021 return PTR_ERR(kaddrs); 1022 1023 /* Walk through the addrs buffer and count the number of addresses. */ 1024 addr_buf = kaddrs; 1025 while (walk_size < addrs_size) { 1026 if (walk_size + sizeof(sa_family_t) > addrs_size) { 1027 kvfree(kaddrs); 1028 return -EINVAL; 1029 } 1030 1031 sa_addr = addr_buf; 1032 af = sctp_get_af_specific(sa_addr->sa_family); 1033 1034 /* If the address family is not supported or if this address 1035 * causes the address buffer to overflow return EINVAL. 1036 */ 1037 if (!af || (walk_size + af->sockaddr_len) > addrs_size) { 1038 kvfree(kaddrs); 1039 return -EINVAL; 1040 } 1041 addrcnt++; 1042 addr_buf += af->sockaddr_len; 1043 walk_size += af->sockaddr_len; 1044 } 1045 1046 /* Do the work. */ 1047 switch (op) { 1048 case SCTP_BINDX_ADD_ADDR: 1049 err = sctp_bindx_add(sk, kaddrs, addrcnt); 1050 if (err) 1051 goto out; 1052 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); 1053 break; 1054 1055 case SCTP_BINDX_REM_ADDR: 1056 err = sctp_bindx_rem(sk, kaddrs, addrcnt); 1057 if (err) 1058 goto out; 1059 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); 1060 break; 1061 1062 default: 1063 err = -EINVAL; 1064 break; 1065 } 1066 1067 out: 1068 kvfree(kaddrs); 1069 1070 return err; 1071 } 1072 1073 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) 1074 * 1075 * Common routine for handling connect() and sctp_connectx(). 1076 * Connect will come in with just a single address. 1077 */ 1078 static int __sctp_connect(struct sock *sk, 1079 struct sockaddr *kaddrs, 1080 int addrs_size, 1081 sctp_assoc_t *assoc_id) 1082 { 1083 struct net *net = sock_net(sk); 1084 struct sctp_sock *sp; 1085 struct sctp_endpoint *ep; 1086 struct sctp_association *asoc = NULL; 1087 struct sctp_association *asoc2; 1088 struct sctp_transport *transport; 1089 union sctp_addr to; 1090 enum sctp_scope scope; 1091 long timeo; 1092 int err = 0; 1093 int addrcnt = 0; 1094 int walk_size = 0; 1095 union sctp_addr *sa_addr = NULL; 1096 void *addr_buf; 1097 unsigned short port; 1098 unsigned int f_flags = 0; 1099 1100 sp = sctp_sk(sk); 1101 ep = sp->ep; 1102 1103 /* connect() cannot be done on a socket that is already in ESTABLISHED 1104 * state - UDP-style peeled off socket or a TCP-style socket that 1105 * is already connected. 1106 * It cannot be done even on a TCP-style listening socket. 1107 */ 1108 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) || 1109 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { 1110 err = -EISCONN; 1111 goto out_free; 1112 } 1113 1114 /* Walk through the addrs buffer and count the number of addresses. */ 1115 addr_buf = kaddrs; 1116 while (walk_size < addrs_size) { 1117 struct sctp_af *af; 1118 1119 if (walk_size + sizeof(sa_family_t) > addrs_size) { 1120 err = -EINVAL; 1121 goto out_free; 1122 } 1123 1124 sa_addr = addr_buf; 1125 af = sctp_get_af_specific(sa_addr->sa.sa_family); 1126 1127 /* If the address family is not supported or if this address 1128 * causes the address buffer to overflow return EINVAL. 1129 */ 1130 if (!af || (walk_size + af->sockaddr_len) > addrs_size) { 1131 err = -EINVAL; 1132 goto out_free; 1133 } 1134 1135 port = ntohs(sa_addr->v4.sin_port); 1136 1137 /* Save current address so we can work with it */ 1138 memcpy(&to, sa_addr, af->sockaddr_len); 1139 1140 err = sctp_verify_addr(sk, &to, af->sockaddr_len); 1141 if (err) 1142 goto out_free; 1143 1144 /* Make sure the destination port is correctly set 1145 * in all addresses. 1146 */ 1147 if (asoc && asoc->peer.port && asoc->peer.port != port) { 1148 err = -EINVAL; 1149 goto out_free; 1150 } 1151 1152 /* Check if there already is a matching association on the 1153 * endpoint (other than the one created here). 1154 */ 1155 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); 1156 if (asoc2 && asoc2 != asoc) { 1157 if (asoc2->state >= SCTP_STATE_ESTABLISHED) 1158 err = -EISCONN; 1159 else 1160 err = -EALREADY; 1161 goto out_free; 1162 } 1163 1164 /* If we could not find a matching association on the endpoint, 1165 * make sure that there is no peeled-off association matching 1166 * the peer address even on another socket. 1167 */ 1168 if (sctp_endpoint_is_peeled_off(ep, &to)) { 1169 err = -EADDRNOTAVAIL; 1170 goto out_free; 1171 } 1172 1173 if (!asoc) { 1174 /* If a bind() or sctp_bindx() is not called prior to 1175 * an sctp_connectx() call, the system picks an 1176 * ephemeral port and will choose an address set 1177 * equivalent to binding with a wildcard address. 1178 */ 1179 if (!ep->base.bind_addr.port) { 1180 if (sctp_autobind(sk)) { 1181 err = -EAGAIN; 1182 goto out_free; 1183 } 1184 } else { 1185 /* 1186 * If an unprivileged user inherits a 1-many 1187 * style socket with open associations on a 1188 * privileged port, it MAY be permitted to 1189 * accept new associations, but it SHOULD NOT 1190 * be permitted to open new associations. 1191 */ 1192 if (ep->base.bind_addr.port < 1193 inet_prot_sock(net) && 1194 !ns_capable(net->user_ns, 1195 CAP_NET_BIND_SERVICE)) { 1196 err = -EACCES; 1197 goto out_free; 1198 } 1199 } 1200 1201 scope = sctp_scope(&to); 1202 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); 1203 if (!asoc) { 1204 err = -ENOMEM; 1205 goto out_free; 1206 } 1207 1208 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, 1209 GFP_KERNEL); 1210 if (err < 0) { 1211 goto out_free; 1212 } 1213 1214 } 1215 1216 /* Prime the peer's transport structures. */ 1217 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, 1218 SCTP_UNKNOWN); 1219 if (!transport) { 1220 err = -ENOMEM; 1221 goto out_free; 1222 } 1223 1224 addrcnt++; 1225 addr_buf += af->sockaddr_len; 1226 walk_size += af->sockaddr_len; 1227 } 1228 1229 /* In case the user of sctp_connectx() wants an association 1230 * id back, assign one now. 1231 */ 1232 if (assoc_id) { 1233 err = sctp_assoc_set_id(asoc, GFP_KERNEL); 1234 if (err < 0) 1235 goto out_free; 1236 } 1237 1238 err = sctp_primitive_ASSOCIATE(net, asoc, NULL); 1239 if (err < 0) { 1240 goto out_free; 1241 } 1242 1243 /* Initialize sk's dport and daddr for getpeername() */ 1244 inet_sk(sk)->inet_dport = htons(asoc->peer.port); 1245 sp->pf->to_sk_daddr(sa_addr, sk); 1246 sk->sk_err = 0; 1247 1248 /* in-kernel sockets don't generally have a file allocated to them 1249 * if all they do is call sock_create_kern(). 1250 */ 1251 if (sk->sk_socket->file) 1252 f_flags = sk->sk_socket->file->f_flags; 1253 1254 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); 1255 1256 if (assoc_id) 1257 *assoc_id = asoc->assoc_id; 1258 err = sctp_wait_for_connect(asoc, &timeo); 1259 /* Note: the asoc may be freed after the return of 1260 * sctp_wait_for_connect. 1261 */ 1262 1263 /* Don't free association on exit. */ 1264 asoc = NULL; 1265 1266 out_free: 1267 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n", 1268 __func__, asoc, kaddrs, err); 1269 1270 if (asoc) { 1271 /* sctp_primitive_ASSOCIATE may have added this association 1272 * To the hash table, try to unhash it, just in case, its a noop 1273 * if it wasn't hashed so we're safe 1274 */ 1275 sctp_association_free(asoc); 1276 } 1277 return err; 1278 } 1279 1280 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() 1281 * 1282 * API 8.9 1283 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, 1284 * sctp_assoc_t *asoc); 1285 * 1286 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 1287 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 1288 * or IPv6 addresses. 1289 * 1290 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 1291 * Section 3.1.2 for this usage. 1292 * 1293 * addrs is a pointer to an array of one or more socket addresses. Each 1294 * address is contained in its appropriate structure (i.e. struct 1295 * sockaddr_in or struct sockaddr_in6) the family of the address type 1296 * must be used to distengish the address length (note that this 1297 * representation is termed a "packed array" of addresses). The caller 1298 * specifies the number of addresses in the array with addrcnt. 1299 * 1300 * On success, sctp_connectx() returns 0. It also sets the assoc_id to 1301 * the association id of the new association. On failure, sctp_connectx() 1302 * returns -1, and sets errno to the appropriate error code. The assoc_id 1303 * is not touched by the kernel. 1304 * 1305 * For SCTP, the port given in each socket address must be the same, or 1306 * sctp_connectx() will fail, setting errno to EINVAL. 1307 * 1308 * An application can use sctp_connectx to initiate an association with 1309 * an endpoint that is multi-homed. Much like sctp_bindx() this call 1310 * allows a caller to specify multiple addresses at which a peer can be 1311 * reached. The way the SCTP stack uses the list of addresses to set up 1312 * the association is implementation dependent. This function only 1313 * specifies that the stack will try to make use of all the addresses in 1314 * the list when needed. 1315 * 1316 * Note that the list of addresses passed in is only used for setting up 1317 * the association. It does not necessarily equal the set of addresses 1318 * the peer uses for the resulting association. If the caller wants to 1319 * find out the set of peer addresses, it must use sctp_getpaddrs() to 1320 * retrieve them after the association has been set up. 1321 * 1322 * Basically do nothing but copying the addresses from user to kernel 1323 * land and invoking either sctp_connectx(). This is used for tunneling 1324 * the sctp_connectx() request through sctp_setsockopt() from userspace. 1325 * 1326 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 1327 * it. 1328 * 1329 * sk The sk of the socket 1330 * addrs The pointer to the addresses in user land 1331 * addrssize Size of the addrs buffer 1332 * 1333 * Returns >=0 if ok, <0 errno code on error. 1334 */ 1335 static int __sctp_setsockopt_connectx(struct sock *sk, 1336 struct sockaddr __user *addrs, 1337 int addrs_size, 1338 sctp_assoc_t *assoc_id) 1339 { 1340 struct sockaddr *kaddrs; 1341 int err = 0; 1342 1343 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n", 1344 __func__, sk, addrs, addrs_size); 1345 1346 if (unlikely(addrs_size <= 0)) 1347 return -EINVAL; 1348 1349 kaddrs = vmemdup_user(addrs, addrs_size); 1350 if (unlikely(IS_ERR(kaddrs))) 1351 return PTR_ERR(kaddrs); 1352 1353 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id); 1354 kvfree(kaddrs); 1355 1356 return err; 1357 } 1358 1359 /* 1360 * This is an older interface. It's kept for backward compatibility 1361 * to the option that doesn't provide association id. 1362 */ 1363 static int sctp_setsockopt_connectx_old(struct sock *sk, 1364 struct sockaddr __user *addrs, 1365 int addrs_size) 1366 { 1367 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); 1368 } 1369 1370 /* 1371 * New interface for the API. The since the API is done with a socket 1372 * option, to make it simple we feed back the association id is as a return 1373 * indication to the call. Error is always negative and association id is 1374 * always positive. 1375 */ 1376 static int sctp_setsockopt_connectx(struct sock *sk, 1377 struct sockaddr __user *addrs, 1378 int addrs_size) 1379 { 1380 sctp_assoc_t assoc_id = 0; 1381 int err = 0; 1382 1383 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); 1384 1385 if (err) 1386 return err; 1387 else 1388 return assoc_id; 1389 } 1390 1391 /* 1392 * New (hopefully final) interface for the API. 1393 * We use the sctp_getaddrs_old structure so that use-space library 1394 * can avoid any unnecessary allocations. The only different part 1395 * is that we store the actual length of the address buffer into the 1396 * addrs_num structure member. That way we can re-use the existing 1397 * code. 1398 */ 1399 #ifdef CONFIG_COMPAT 1400 struct compat_sctp_getaddrs_old { 1401 sctp_assoc_t assoc_id; 1402 s32 addr_num; 1403 compat_uptr_t addrs; /* struct sockaddr * */ 1404 }; 1405 #endif 1406 1407 static int sctp_getsockopt_connectx3(struct sock *sk, int len, 1408 char __user *optval, 1409 int __user *optlen) 1410 { 1411 struct sctp_getaddrs_old param; 1412 sctp_assoc_t assoc_id = 0; 1413 int err = 0; 1414 1415 #ifdef CONFIG_COMPAT 1416 if (in_compat_syscall()) { 1417 struct compat_sctp_getaddrs_old param32; 1418 1419 if (len < sizeof(param32)) 1420 return -EINVAL; 1421 if (copy_from_user(¶m32, optval, sizeof(param32))) 1422 return -EFAULT; 1423 1424 param.assoc_id = param32.assoc_id; 1425 param.addr_num = param32.addr_num; 1426 param.addrs = compat_ptr(param32.addrs); 1427 } else 1428 #endif 1429 { 1430 if (len < sizeof(param)) 1431 return -EINVAL; 1432 if (copy_from_user(¶m, optval, sizeof(param))) 1433 return -EFAULT; 1434 } 1435 1436 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *) 1437 param.addrs, param.addr_num, 1438 &assoc_id); 1439 if (err == 0 || err == -EINPROGRESS) { 1440 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) 1441 return -EFAULT; 1442 if (put_user(sizeof(assoc_id), optlen)) 1443 return -EFAULT; 1444 } 1445 1446 return err; 1447 } 1448 1449 /* API 3.1.4 close() - UDP Style Syntax 1450 * Applications use close() to perform graceful shutdown (as described in 1451 * Section 10.1 of [SCTP]) on ALL the associations currently represented 1452 * by a UDP-style socket. 1453 * 1454 * The syntax is 1455 * 1456 * ret = close(int sd); 1457 * 1458 * sd - the socket descriptor of the associations to be closed. 1459 * 1460 * To gracefully shutdown a specific association represented by the 1461 * UDP-style socket, an application should use the sendmsg() call, 1462 * passing no user data, but including the appropriate flag in the 1463 * ancillary data (see Section xxxx). 1464 * 1465 * If sd in the close() call is a branched-off socket representing only 1466 * one association, the shutdown is performed on that association only. 1467 * 1468 * 4.1.6 close() - TCP Style Syntax 1469 * 1470 * Applications use close() to gracefully close down an association. 1471 * 1472 * The syntax is: 1473 * 1474 * int close(int sd); 1475 * 1476 * sd - the socket descriptor of the association to be closed. 1477 * 1478 * After an application calls close() on a socket descriptor, no further 1479 * socket operations will succeed on that descriptor. 1480 * 1481 * API 7.1.4 SO_LINGER 1482 * 1483 * An application using the TCP-style socket can use this option to 1484 * perform the SCTP ABORT primitive. The linger option structure is: 1485 * 1486 * struct linger { 1487 * int l_onoff; // option on/off 1488 * int l_linger; // linger time 1489 * }; 1490 * 1491 * To enable the option, set l_onoff to 1. If the l_linger value is set 1492 * to 0, calling close() is the same as the ABORT primitive. If the 1493 * value is set to a negative value, the setsockopt() call will return 1494 * an error. If the value is set to a positive value linger_time, the 1495 * close() can be blocked for at most linger_time ms. If the graceful 1496 * shutdown phase does not finish during this period, close() will 1497 * return but the graceful shutdown phase continues in the system. 1498 */ 1499 static void sctp_close(struct sock *sk, long timeout) 1500 { 1501 struct net *net = sock_net(sk); 1502 struct sctp_endpoint *ep; 1503 struct sctp_association *asoc; 1504 struct list_head *pos, *temp; 1505 unsigned int data_was_unread; 1506 1507 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout); 1508 1509 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 1510 sk->sk_shutdown = SHUTDOWN_MASK; 1511 inet_sk_set_state(sk, SCTP_SS_CLOSING); 1512 1513 ep = sctp_sk(sk)->ep; 1514 1515 /* Clean up any skbs sitting on the receive queue. */ 1516 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); 1517 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); 1518 1519 /* Walk all associations on an endpoint. */ 1520 list_for_each_safe(pos, temp, &ep->asocs) { 1521 asoc = list_entry(pos, struct sctp_association, asocs); 1522 1523 if (sctp_style(sk, TCP)) { 1524 /* A closed association can still be in the list if 1525 * it belongs to a TCP-style listening socket that is 1526 * not yet accepted. If so, free it. If not, send an 1527 * ABORT or SHUTDOWN based on the linger options. 1528 */ 1529 if (sctp_state(asoc, CLOSED)) { 1530 sctp_association_free(asoc); 1531 continue; 1532 } 1533 } 1534 1535 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || 1536 !skb_queue_empty(&asoc->ulpq.reasm) || 1537 !skb_queue_empty(&asoc->ulpq.reasm_uo) || 1538 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { 1539 struct sctp_chunk *chunk; 1540 1541 chunk = sctp_make_abort_user(asoc, NULL, 0); 1542 sctp_primitive_ABORT(net, asoc, chunk); 1543 } else 1544 sctp_primitive_SHUTDOWN(net, asoc, NULL); 1545 } 1546 1547 /* On a TCP-style socket, block for at most linger_time if set. */ 1548 if (sctp_style(sk, TCP) && timeout) 1549 sctp_wait_for_close(sk, timeout); 1550 1551 /* This will run the backlog queue. */ 1552 release_sock(sk); 1553 1554 /* Supposedly, no process has access to the socket, but 1555 * the net layers still may. 1556 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock 1557 * held and that should be grabbed before socket lock. 1558 */ 1559 spin_lock_bh(&net->sctp.addr_wq_lock); 1560 bh_lock_sock_nested(sk); 1561 1562 /* Hold the sock, since sk_common_release() will put sock_put() 1563 * and we have just a little more cleanup. 1564 */ 1565 sock_hold(sk); 1566 sk_common_release(sk); 1567 1568 bh_unlock_sock(sk); 1569 spin_unlock_bh(&net->sctp.addr_wq_lock); 1570 1571 sock_put(sk); 1572 1573 SCTP_DBG_OBJCNT_DEC(sock); 1574 } 1575 1576 /* Handle EPIPE error. */ 1577 static int sctp_error(struct sock *sk, int flags, int err) 1578 { 1579 if (err == -EPIPE) 1580 err = sock_error(sk) ? : -EPIPE; 1581 if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) 1582 send_sig(SIGPIPE, current, 0); 1583 return err; 1584 } 1585 1586 /* API 3.1.3 sendmsg() - UDP Style Syntax 1587 * 1588 * An application uses sendmsg() and recvmsg() calls to transmit data to 1589 * and receive data from its peer. 1590 * 1591 * ssize_t sendmsg(int socket, const struct msghdr *message, 1592 * int flags); 1593 * 1594 * socket - the socket descriptor of the endpoint. 1595 * message - pointer to the msghdr structure which contains a single 1596 * user message and possibly some ancillary data. 1597 * 1598 * See Section 5 for complete description of the data 1599 * structures. 1600 * 1601 * flags - flags sent or received with the user message, see Section 1602 * 5 for complete description of the flags. 1603 * 1604 * Note: This function could use a rewrite especially when explicit 1605 * connect support comes in. 1606 */ 1607 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ 1608 1609 static int sctp_msghdr_parse(const struct msghdr *msg, 1610 struct sctp_cmsgs *cmsgs); 1611 1612 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs, 1613 struct sctp_sndrcvinfo *srinfo, 1614 const struct msghdr *msg, size_t msg_len) 1615 { 1616 __u16 sflags; 1617 int err; 1618 1619 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP)) 1620 return -EPIPE; 1621 1622 if (msg_len > sk->sk_sndbuf) 1623 return -EMSGSIZE; 1624 1625 memset(cmsgs, 0, sizeof(*cmsgs)); 1626 err = sctp_msghdr_parse(msg, cmsgs); 1627 if (err) { 1628 pr_debug("%s: msghdr parse err:%x\n", __func__, err); 1629 return err; 1630 } 1631 1632 memset(srinfo, 0, sizeof(*srinfo)); 1633 if (cmsgs->srinfo) { 1634 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream; 1635 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags; 1636 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid; 1637 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context; 1638 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id; 1639 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive; 1640 } 1641 1642 if (cmsgs->sinfo) { 1643 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid; 1644 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags; 1645 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid; 1646 srinfo->sinfo_context = cmsgs->sinfo->snd_context; 1647 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id; 1648 } 1649 1650 if (cmsgs->prinfo) { 1651 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value; 1652 SCTP_PR_SET_POLICY(srinfo->sinfo_flags, 1653 cmsgs->prinfo->pr_policy); 1654 } 1655 1656 sflags = srinfo->sinfo_flags; 1657 if (!sflags && msg_len) 1658 return 0; 1659 1660 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT))) 1661 return -EINVAL; 1662 1663 if (((sflags & SCTP_EOF) && msg_len > 0) || 1664 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0)) 1665 return -EINVAL; 1666 1667 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name) 1668 return -EINVAL; 1669 1670 return 0; 1671 } 1672 1673 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, 1674 struct sctp_cmsgs *cmsgs, 1675 union sctp_addr *daddr, 1676 struct sctp_transport **tp) 1677 { 1678 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 1679 struct net *net = sock_net(sk); 1680 struct sctp_association *asoc; 1681 enum sctp_scope scope; 1682 struct cmsghdr *cmsg; 1683 int err; 1684 1685 *tp = NULL; 1686 1687 if (sflags & (SCTP_EOF | SCTP_ABORT)) 1688 return -EINVAL; 1689 1690 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) || 1691 sctp_sstate(sk, CLOSING))) 1692 return -EADDRNOTAVAIL; 1693 1694 if (sctp_endpoint_is_peeled_off(ep, daddr)) 1695 return -EADDRNOTAVAIL; 1696 1697 if (!ep->base.bind_addr.port) { 1698 if (sctp_autobind(sk)) 1699 return -EAGAIN; 1700 } else { 1701 if (ep->base.bind_addr.port < inet_prot_sock(net) && 1702 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 1703 return -EACCES; 1704 } 1705 1706 scope = sctp_scope(daddr); 1707 1708 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); 1709 if (!asoc) 1710 return -ENOMEM; 1711 1712 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) { 1713 err = -ENOMEM; 1714 goto free; 1715 } 1716 1717 if (cmsgs->init) { 1718 struct sctp_initmsg *init = cmsgs->init; 1719 1720 if (init->sinit_num_ostreams) { 1721 __u16 outcnt = init->sinit_num_ostreams; 1722 1723 asoc->c.sinit_num_ostreams = outcnt; 1724 /* outcnt has been changed, need to re-init stream */ 1725 err = sctp_stream_init(&asoc->stream, outcnt, 0, 1726 GFP_KERNEL); 1727 if (err) 1728 goto free; 1729 } 1730 1731 if (init->sinit_max_instreams) 1732 asoc->c.sinit_max_instreams = init->sinit_max_instreams; 1733 1734 if (init->sinit_max_attempts) 1735 asoc->max_init_attempts = init->sinit_max_attempts; 1736 1737 if (init->sinit_max_init_timeo) 1738 asoc->max_init_timeo = 1739 msecs_to_jiffies(init->sinit_max_init_timeo); 1740 } 1741 1742 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); 1743 if (!*tp) { 1744 err = -ENOMEM; 1745 goto free; 1746 } 1747 1748 if (!cmsgs->addrs_msg) 1749 return 0; 1750 1751 /* sendv addr list parse */ 1752 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) { 1753 struct sctp_transport *transport; 1754 struct sctp_association *old; 1755 union sctp_addr _daddr; 1756 int dlen; 1757 1758 if (cmsg->cmsg_level != IPPROTO_SCTP || 1759 (cmsg->cmsg_type != SCTP_DSTADDRV4 && 1760 cmsg->cmsg_type != SCTP_DSTADDRV6)) 1761 continue; 1762 1763 daddr = &_daddr; 1764 memset(daddr, 0, sizeof(*daddr)); 1765 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr); 1766 if (cmsg->cmsg_type == SCTP_DSTADDRV4) { 1767 if (dlen < sizeof(struct in_addr)) { 1768 err = -EINVAL; 1769 goto free; 1770 } 1771 1772 dlen = sizeof(struct in_addr); 1773 daddr->v4.sin_family = AF_INET; 1774 daddr->v4.sin_port = htons(asoc->peer.port); 1775 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen); 1776 } else { 1777 if (dlen < sizeof(struct in6_addr)) { 1778 err = -EINVAL; 1779 goto free; 1780 } 1781 1782 dlen = sizeof(struct in6_addr); 1783 daddr->v6.sin6_family = AF_INET6; 1784 daddr->v6.sin6_port = htons(asoc->peer.port); 1785 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen); 1786 } 1787 err = sctp_verify_addr(sk, daddr, sizeof(*daddr)); 1788 if (err) 1789 goto free; 1790 1791 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport); 1792 if (old && old != asoc) { 1793 if (old->state >= SCTP_STATE_ESTABLISHED) 1794 err = -EISCONN; 1795 else 1796 err = -EALREADY; 1797 goto free; 1798 } 1799 1800 if (sctp_endpoint_is_peeled_off(ep, daddr)) { 1801 err = -EADDRNOTAVAIL; 1802 goto free; 1803 } 1804 1805 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, 1806 SCTP_UNKNOWN); 1807 if (!transport) { 1808 err = -ENOMEM; 1809 goto free; 1810 } 1811 } 1812 1813 return 0; 1814 1815 free: 1816 sctp_association_free(asoc); 1817 return err; 1818 } 1819 1820 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc, 1821 __u16 sflags, struct msghdr *msg, 1822 size_t msg_len) 1823 { 1824 struct sock *sk = asoc->base.sk; 1825 struct net *net = sock_net(sk); 1826 1827 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) 1828 return -EPIPE; 1829 1830 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) && 1831 !sctp_state(asoc, ESTABLISHED)) 1832 return 0; 1833 1834 if (sflags & SCTP_EOF) { 1835 pr_debug("%s: shutting down association:%p\n", __func__, asoc); 1836 sctp_primitive_SHUTDOWN(net, asoc, NULL); 1837 1838 return 0; 1839 } 1840 1841 if (sflags & SCTP_ABORT) { 1842 struct sctp_chunk *chunk; 1843 1844 chunk = sctp_make_abort_user(asoc, msg, msg_len); 1845 if (!chunk) 1846 return -ENOMEM; 1847 1848 pr_debug("%s: aborting association:%p\n", __func__, asoc); 1849 sctp_primitive_ABORT(net, asoc, chunk); 1850 1851 return 0; 1852 } 1853 1854 return 1; 1855 } 1856 1857 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, 1858 struct msghdr *msg, size_t msg_len, 1859 struct sctp_transport *transport, 1860 struct sctp_sndrcvinfo *sinfo) 1861 { 1862 struct sock *sk = asoc->base.sk; 1863 struct net *net = sock_net(sk); 1864 struct sctp_datamsg *datamsg; 1865 bool wait_connect = false; 1866 struct sctp_chunk *chunk; 1867 long timeo; 1868 int err; 1869 1870 if (sinfo->sinfo_stream >= asoc->stream.outcnt) { 1871 err = -EINVAL; 1872 goto err; 1873 } 1874 1875 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) { 1876 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); 1877 if (err) 1878 goto err; 1879 } 1880 1881 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) { 1882 err = -EMSGSIZE; 1883 goto err; 1884 } 1885 1886 if (asoc->pmtu_pending) 1887 sctp_assoc_pending_pmtu(asoc); 1888 1889 if (sctp_wspace(asoc) < msg_len) 1890 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); 1891 1892 if (!sctp_wspace(asoc)) { 1893 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1894 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); 1895 if (err) 1896 goto err; 1897 } 1898 1899 if (sctp_state(asoc, CLOSED)) { 1900 err = sctp_primitive_ASSOCIATE(net, asoc, NULL); 1901 if (err) 1902 goto err; 1903 1904 if (sctp_sk(sk)->strm_interleave) { 1905 timeo = sock_sndtimeo(sk, 0); 1906 err = sctp_wait_for_connect(asoc, &timeo); 1907 if (err) 1908 goto err; 1909 } else { 1910 wait_connect = true; 1911 } 1912 1913 pr_debug("%s: we associated primitively\n", __func__); 1914 } 1915 1916 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); 1917 if (IS_ERR(datamsg)) { 1918 err = PTR_ERR(datamsg); 1919 goto err; 1920 } 1921 1922 asoc->force_delay = !!(msg->msg_flags & MSG_MORE); 1923 1924 list_for_each_entry(chunk, &datamsg->chunks, frag_list) { 1925 sctp_chunk_hold(chunk); 1926 sctp_set_owner_w(chunk); 1927 chunk->transport = transport; 1928 } 1929 1930 err = sctp_primitive_SEND(net, asoc, datamsg); 1931 if (err) { 1932 sctp_datamsg_free(datamsg); 1933 goto err; 1934 } 1935 1936 pr_debug("%s: we sent primitively\n", __func__); 1937 1938 sctp_datamsg_put(datamsg); 1939 1940 if (unlikely(wait_connect)) { 1941 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1942 sctp_wait_for_connect(asoc, &timeo); 1943 } 1944 1945 err = msg_len; 1946 1947 err: 1948 return err; 1949 } 1950 1951 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk, 1952 const struct msghdr *msg, 1953 struct sctp_cmsgs *cmsgs) 1954 { 1955 union sctp_addr *daddr = NULL; 1956 int err; 1957 1958 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { 1959 int len = msg->msg_namelen; 1960 1961 if (len > sizeof(*daddr)) 1962 len = sizeof(*daddr); 1963 1964 daddr = (union sctp_addr *)msg->msg_name; 1965 1966 err = sctp_verify_addr(sk, daddr, len); 1967 if (err) 1968 return ERR_PTR(err); 1969 } 1970 1971 return daddr; 1972 } 1973 1974 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc, 1975 struct sctp_sndrcvinfo *sinfo, 1976 struct sctp_cmsgs *cmsgs) 1977 { 1978 if (!cmsgs->srinfo && !cmsgs->sinfo) { 1979 sinfo->sinfo_stream = asoc->default_stream; 1980 sinfo->sinfo_ppid = asoc->default_ppid; 1981 sinfo->sinfo_context = asoc->default_context; 1982 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc); 1983 1984 if (!cmsgs->prinfo) 1985 sinfo->sinfo_flags = asoc->default_flags; 1986 } 1987 1988 if (!cmsgs->srinfo && !cmsgs->prinfo) 1989 sinfo->sinfo_timetolive = asoc->default_timetolive; 1990 1991 if (cmsgs->authinfo) { 1992 /* Reuse sinfo_tsn to indicate that authinfo was set and 1993 * sinfo_ssn to save the keyid on tx path. 1994 */ 1995 sinfo->sinfo_tsn = 1; 1996 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber; 1997 } 1998 } 1999 2000 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) 2001 { 2002 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 2003 struct sctp_transport *transport = NULL; 2004 struct sctp_sndrcvinfo _sinfo, *sinfo; 2005 struct sctp_association *asoc; 2006 struct sctp_cmsgs cmsgs; 2007 union sctp_addr *daddr; 2008 bool new = false; 2009 __u16 sflags; 2010 int err; 2011 2012 /* Parse and get snd_info */ 2013 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len); 2014 if (err) 2015 goto out; 2016 2017 sinfo = &_sinfo; 2018 sflags = sinfo->sinfo_flags; 2019 2020 /* Get daddr from msg */ 2021 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs); 2022 if (IS_ERR(daddr)) { 2023 err = PTR_ERR(daddr); 2024 goto out; 2025 } 2026 2027 lock_sock(sk); 2028 2029 /* SCTP_SENDALL process */ 2030 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) { 2031 list_for_each_entry(asoc, &ep->asocs, asocs) { 2032 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, 2033 msg_len); 2034 if (err == 0) 2035 continue; 2036 if (err < 0) 2037 goto out_unlock; 2038 2039 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); 2040 2041 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, 2042 NULL, sinfo); 2043 if (err < 0) 2044 goto out_unlock; 2045 2046 iov_iter_revert(&msg->msg_iter, err); 2047 } 2048 2049 goto out_unlock; 2050 } 2051 2052 /* Get and check or create asoc */ 2053 if (daddr) { 2054 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); 2055 if (asoc) { 2056 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, 2057 msg_len); 2058 if (err <= 0) 2059 goto out_unlock; 2060 } else { 2061 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr, 2062 &transport); 2063 if (err) 2064 goto out_unlock; 2065 2066 asoc = transport->asoc; 2067 new = true; 2068 } 2069 2070 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER)) 2071 transport = NULL; 2072 } else { 2073 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id); 2074 if (!asoc) { 2075 err = -EPIPE; 2076 goto out_unlock; 2077 } 2078 2079 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len); 2080 if (err <= 0) 2081 goto out_unlock; 2082 } 2083 2084 /* Update snd_info with the asoc */ 2085 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); 2086 2087 /* Send msg to the asoc */ 2088 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo); 2089 if (err < 0 && err != -ESRCH && new) 2090 sctp_association_free(asoc); 2091 2092 out_unlock: 2093 release_sock(sk); 2094 out: 2095 return sctp_error(sk, msg->msg_flags, err); 2096 } 2097 2098 /* This is an extended version of skb_pull() that removes the data from the 2099 * start of a skb even when data is spread across the list of skb's in the 2100 * frag_list. len specifies the total amount of data that needs to be removed. 2101 * when 'len' bytes could be removed from the skb, it returns 0. 2102 * If 'len' exceeds the total skb length, it returns the no. of bytes that 2103 * could not be removed. 2104 */ 2105 static int sctp_skb_pull(struct sk_buff *skb, int len) 2106 { 2107 struct sk_buff *list; 2108 int skb_len = skb_headlen(skb); 2109 int rlen; 2110 2111 if (len <= skb_len) { 2112 __skb_pull(skb, len); 2113 return 0; 2114 } 2115 len -= skb_len; 2116 __skb_pull(skb, skb_len); 2117 2118 skb_walk_frags(skb, list) { 2119 rlen = sctp_skb_pull(list, len); 2120 skb->len -= (len-rlen); 2121 skb->data_len -= (len-rlen); 2122 2123 if (!rlen) 2124 return 0; 2125 2126 len = rlen; 2127 } 2128 2129 return len; 2130 } 2131 2132 /* API 3.1.3 recvmsg() - UDP Style Syntax 2133 * 2134 * ssize_t recvmsg(int socket, struct msghdr *message, 2135 * int flags); 2136 * 2137 * socket - the socket descriptor of the endpoint. 2138 * message - pointer to the msghdr structure which contains a single 2139 * user message and possibly some ancillary data. 2140 * 2141 * See Section 5 for complete description of the data 2142 * structures. 2143 * 2144 * flags - flags sent or received with the user message, see Section 2145 * 5 for complete description of the flags. 2146 */ 2147 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2148 int noblock, int flags, int *addr_len) 2149 { 2150 struct sctp_ulpevent *event = NULL; 2151 struct sctp_sock *sp = sctp_sk(sk); 2152 struct sk_buff *skb, *head_skb; 2153 int copied; 2154 int err = 0; 2155 int skb_len; 2156 2157 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, " 2158 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags, 2159 addr_len); 2160 2161 lock_sock(sk); 2162 2163 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) && 2164 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) { 2165 err = -ENOTCONN; 2166 goto out; 2167 } 2168 2169 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); 2170 if (!skb) 2171 goto out; 2172 2173 /* Get the total length of the skb including any skb's in the 2174 * frag_list. 2175 */ 2176 skb_len = skb->len; 2177 2178 copied = skb_len; 2179 if (copied > len) 2180 copied = len; 2181 2182 err = skb_copy_datagram_msg(skb, 0, msg, copied); 2183 2184 event = sctp_skb2event(skb); 2185 2186 if (err) 2187 goto out_free; 2188 2189 if (event->chunk && event->chunk->head_skb) 2190 head_skb = event->chunk->head_skb; 2191 else 2192 head_skb = skb; 2193 sock_recv_ts_and_drops(msg, sk, head_skb); 2194 if (sctp_ulpevent_is_notification(event)) { 2195 msg->msg_flags |= MSG_NOTIFICATION; 2196 sp->pf->event_msgname(event, msg->msg_name, addr_len); 2197 } else { 2198 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len); 2199 } 2200 2201 /* Check if we allow SCTP_NXTINFO. */ 2202 if (sp->recvnxtinfo) 2203 sctp_ulpevent_read_nxtinfo(event, msg, sk); 2204 /* Check if we allow SCTP_RCVINFO. */ 2205 if (sp->recvrcvinfo) 2206 sctp_ulpevent_read_rcvinfo(event, msg); 2207 /* Check if we allow SCTP_SNDRCVINFO. */ 2208 if (sp->subscribe.sctp_data_io_event) 2209 sctp_ulpevent_read_sndrcvinfo(event, msg); 2210 2211 err = copied; 2212 2213 /* If skb's length exceeds the user's buffer, update the skb and 2214 * push it back to the receive_queue so that the next call to 2215 * recvmsg() will return the remaining data. Don't set MSG_EOR. 2216 */ 2217 if (skb_len > copied) { 2218 msg->msg_flags &= ~MSG_EOR; 2219 if (flags & MSG_PEEK) 2220 goto out_free; 2221 sctp_skb_pull(skb, copied); 2222 skb_queue_head(&sk->sk_receive_queue, skb); 2223 2224 /* When only partial message is copied to the user, increase 2225 * rwnd by that amount. If all the data in the skb is read, 2226 * rwnd is updated when the event is freed. 2227 */ 2228 if (!sctp_ulpevent_is_notification(event)) 2229 sctp_assoc_rwnd_increase(event->asoc, copied); 2230 goto out; 2231 } else if ((event->msg_flags & MSG_NOTIFICATION) || 2232 (event->msg_flags & MSG_EOR)) 2233 msg->msg_flags |= MSG_EOR; 2234 else 2235 msg->msg_flags &= ~MSG_EOR; 2236 2237 out_free: 2238 if (flags & MSG_PEEK) { 2239 /* Release the skb reference acquired after peeking the skb in 2240 * sctp_skb_recv_datagram(). 2241 */ 2242 kfree_skb(skb); 2243 } else { 2244 /* Free the event which includes releasing the reference to 2245 * the owner of the skb, freeing the skb and updating the 2246 * rwnd. 2247 */ 2248 sctp_ulpevent_free(event); 2249 } 2250 out: 2251 release_sock(sk); 2252 return err; 2253 } 2254 2255 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 2256 * 2257 * This option is a on/off flag. If enabled no SCTP message 2258 * fragmentation will be performed. Instead if a message being sent 2259 * exceeds the current PMTU size, the message will NOT be sent and 2260 * instead a error will be indicated to the user. 2261 */ 2262 static int sctp_setsockopt_disable_fragments(struct sock *sk, 2263 char __user *optval, 2264 unsigned int optlen) 2265 { 2266 int val; 2267 2268 if (optlen < sizeof(int)) 2269 return -EINVAL; 2270 2271 if (get_user(val, (int __user *)optval)) 2272 return -EFAULT; 2273 2274 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; 2275 2276 return 0; 2277 } 2278 2279 static int sctp_setsockopt_events(struct sock *sk, char __user *optval, 2280 unsigned int optlen) 2281 { 2282 struct sctp_association *asoc; 2283 struct sctp_ulpevent *event; 2284 2285 if (optlen > sizeof(struct sctp_event_subscribe)) 2286 return -EINVAL; 2287 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) 2288 return -EFAULT; 2289 2290 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT, 2291 * if there is no data to be sent or retransmit, the stack will 2292 * immediately send up this notification. 2293 */ 2294 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT, 2295 &sctp_sk(sk)->subscribe)) { 2296 asoc = sctp_id2assoc(sk, 0); 2297 2298 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { 2299 event = sctp_ulpevent_make_sender_dry_event(asoc, 2300 GFP_USER | __GFP_NOWARN); 2301 if (!event) 2302 return -ENOMEM; 2303 2304 asoc->stream.si->enqueue_event(&asoc->ulpq, event); 2305 } 2306 } 2307 2308 return 0; 2309 } 2310 2311 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 2312 * 2313 * This socket option is applicable to the UDP-style socket only. When 2314 * set it will cause associations that are idle for more than the 2315 * specified number of seconds to automatically close. An association 2316 * being idle is defined an association that has NOT sent or received 2317 * user data. The special value of '0' indicates that no automatic 2318 * close of any associations should be performed. The option expects an 2319 * integer defining the number of seconds of idle time before an 2320 * association is closed. 2321 */ 2322 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, 2323 unsigned int optlen) 2324 { 2325 struct sctp_sock *sp = sctp_sk(sk); 2326 struct net *net = sock_net(sk); 2327 2328 /* Applicable to UDP-style socket only */ 2329 if (sctp_style(sk, TCP)) 2330 return -EOPNOTSUPP; 2331 if (optlen != sizeof(int)) 2332 return -EINVAL; 2333 if (copy_from_user(&sp->autoclose, optval, optlen)) 2334 return -EFAULT; 2335 2336 if (sp->autoclose > net->sctp.max_autoclose) 2337 sp->autoclose = net->sctp.max_autoclose; 2338 2339 return 0; 2340 } 2341 2342 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 2343 * 2344 * Applications can enable or disable heartbeats for any peer address of 2345 * an association, modify an address's heartbeat interval, force a 2346 * heartbeat to be sent immediately, and adjust the address's maximum 2347 * number of retransmissions sent before an address is considered 2348 * unreachable. The following structure is used to access and modify an 2349 * address's parameters: 2350 * 2351 * struct sctp_paddrparams { 2352 * sctp_assoc_t spp_assoc_id; 2353 * struct sockaddr_storage spp_address; 2354 * uint32_t spp_hbinterval; 2355 * uint16_t spp_pathmaxrxt; 2356 * uint32_t spp_pathmtu; 2357 * uint32_t spp_sackdelay; 2358 * uint32_t spp_flags; 2359 * }; 2360 * 2361 * spp_assoc_id - (one-to-many style socket) This is filled in the 2362 * application, and identifies the association for 2363 * this query. 2364 * spp_address - This specifies which address is of interest. 2365 * spp_hbinterval - This contains the value of the heartbeat interval, 2366 * in milliseconds. If a value of zero 2367 * is present in this field then no changes are to 2368 * be made to this parameter. 2369 * spp_pathmaxrxt - This contains the maximum number of 2370 * retransmissions before this address shall be 2371 * considered unreachable. If a value of zero 2372 * is present in this field then no changes are to 2373 * be made to this parameter. 2374 * spp_pathmtu - When Path MTU discovery is disabled the value 2375 * specified here will be the "fixed" path mtu. 2376 * Note that if the spp_address field is empty 2377 * then all associations on this address will 2378 * have this fixed path mtu set upon them. 2379 * 2380 * spp_sackdelay - When delayed sack is enabled, this value specifies 2381 * the number of milliseconds that sacks will be delayed 2382 * for. This value will apply to all addresses of an 2383 * association if the spp_address field is empty. Note 2384 * also, that if delayed sack is enabled and this 2385 * value is set to 0, no change is made to the last 2386 * recorded delayed sack timer value. 2387 * 2388 * spp_flags - These flags are used to control various features 2389 * on an association. The flag field may contain 2390 * zero or more of the following options. 2391 * 2392 * SPP_HB_ENABLE - Enable heartbeats on the 2393 * specified address. Note that if the address 2394 * field is empty all addresses for the association 2395 * have heartbeats enabled upon them. 2396 * 2397 * SPP_HB_DISABLE - Disable heartbeats on the 2398 * speicifed address. Note that if the address 2399 * field is empty all addresses for the association 2400 * will have their heartbeats disabled. Note also 2401 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 2402 * mutually exclusive, only one of these two should 2403 * be specified. Enabling both fields will have 2404 * undetermined results. 2405 * 2406 * SPP_HB_DEMAND - Request a user initiated heartbeat 2407 * to be made immediately. 2408 * 2409 * SPP_HB_TIME_IS_ZERO - Specify's that the time for 2410 * heartbeat delayis to be set to the value of 0 2411 * milliseconds. 2412 * 2413 * SPP_PMTUD_ENABLE - This field will enable PMTU 2414 * discovery upon the specified address. Note that 2415 * if the address feild is empty then all addresses 2416 * on the association are effected. 2417 * 2418 * SPP_PMTUD_DISABLE - This field will disable PMTU 2419 * discovery upon the specified address. Note that 2420 * if the address feild is empty then all addresses 2421 * on the association are effected. Not also that 2422 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 2423 * exclusive. Enabling both will have undetermined 2424 * results. 2425 * 2426 * SPP_SACKDELAY_ENABLE - Setting this flag turns 2427 * on delayed sack. The time specified in spp_sackdelay 2428 * is used to specify the sack delay for this address. Note 2429 * that if spp_address is empty then all addresses will 2430 * enable delayed sack and take on the sack delay 2431 * value specified in spp_sackdelay. 2432 * SPP_SACKDELAY_DISABLE - Setting this flag turns 2433 * off delayed sack. If the spp_address field is blank then 2434 * delayed sack is disabled for the entire association. Note 2435 * also that this field is mutually exclusive to 2436 * SPP_SACKDELAY_ENABLE, setting both will have undefined 2437 * results. 2438 */ 2439 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, 2440 struct sctp_transport *trans, 2441 struct sctp_association *asoc, 2442 struct sctp_sock *sp, 2443 int hb_change, 2444 int pmtud_change, 2445 int sackdelay_change) 2446 { 2447 int error; 2448 2449 if (params->spp_flags & SPP_HB_DEMAND && trans) { 2450 struct net *net = sock_net(trans->asoc->base.sk); 2451 2452 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans); 2453 if (error) 2454 return error; 2455 } 2456 2457 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of 2458 * this field is ignored. Note also that a value of zero indicates 2459 * the current setting should be left unchanged. 2460 */ 2461 if (params->spp_flags & SPP_HB_ENABLE) { 2462 2463 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is 2464 * set. This lets us use 0 value when this flag 2465 * is set. 2466 */ 2467 if (params->spp_flags & SPP_HB_TIME_IS_ZERO) 2468 params->spp_hbinterval = 0; 2469 2470 if (params->spp_hbinterval || 2471 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { 2472 if (trans) { 2473 trans->hbinterval = 2474 msecs_to_jiffies(params->spp_hbinterval); 2475 } else if (asoc) { 2476 asoc->hbinterval = 2477 msecs_to_jiffies(params->spp_hbinterval); 2478 } else { 2479 sp->hbinterval = params->spp_hbinterval; 2480 } 2481 } 2482 } 2483 2484 if (hb_change) { 2485 if (trans) { 2486 trans->param_flags = 2487 (trans->param_flags & ~SPP_HB) | hb_change; 2488 } else if (asoc) { 2489 asoc->param_flags = 2490 (asoc->param_flags & ~SPP_HB) | hb_change; 2491 } else { 2492 sp->param_flags = 2493 (sp->param_flags & ~SPP_HB) | hb_change; 2494 } 2495 } 2496 2497 /* When Path MTU discovery is disabled the value specified here will 2498 * be the "fixed" path mtu (i.e. the value of the spp_flags field must 2499 * include the flag SPP_PMTUD_DISABLE for this field to have any 2500 * effect). 2501 */ 2502 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { 2503 if (trans) { 2504 trans->pathmtu = params->spp_pathmtu; 2505 sctp_assoc_sync_pmtu(asoc); 2506 } else if (asoc) { 2507 asoc->pathmtu = params->spp_pathmtu; 2508 } else { 2509 sp->pathmtu = params->spp_pathmtu; 2510 } 2511 } 2512 2513 if (pmtud_change) { 2514 if (trans) { 2515 int update = (trans->param_flags & SPP_PMTUD_DISABLE) && 2516 (params->spp_flags & SPP_PMTUD_ENABLE); 2517 trans->param_flags = 2518 (trans->param_flags & ~SPP_PMTUD) | pmtud_change; 2519 if (update) { 2520 sctp_transport_pmtu(trans, sctp_opt2sk(sp)); 2521 sctp_assoc_sync_pmtu(asoc); 2522 } 2523 } else if (asoc) { 2524 asoc->param_flags = 2525 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; 2526 } else { 2527 sp->param_flags = 2528 (sp->param_flags & ~SPP_PMTUD) | pmtud_change; 2529 } 2530 } 2531 2532 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the 2533 * value of this field is ignored. Note also that a value of zero 2534 * indicates the current setting should be left unchanged. 2535 */ 2536 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { 2537 if (trans) { 2538 trans->sackdelay = 2539 msecs_to_jiffies(params->spp_sackdelay); 2540 } else if (asoc) { 2541 asoc->sackdelay = 2542 msecs_to_jiffies(params->spp_sackdelay); 2543 } else { 2544 sp->sackdelay = params->spp_sackdelay; 2545 } 2546 } 2547 2548 if (sackdelay_change) { 2549 if (trans) { 2550 trans->param_flags = 2551 (trans->param_flags & ~SPP_SACKDELAY) | 2552 sackdelay_change; 2553 } else if (asoc) { 2554 asoc->param_flags = 2555 (asoc->param_flags & ~SPP_SACKDELAY) | 2556 sackdelay_change; 2557 } else { 2558 sp->param_flags = 2559 (sp->param_flags & ~SPP_SACKDELAY) | 2560 sackdelay_change; 2561 } 2562 } 2563 2564 /* Note that a value of zero indicates the current setting should be 2565 left unchanged. 2566 */ 2567 if (params->spp_pathmaxrxt) { 2568 if (trans) { 2569 trans->pathmaxrxt = params->spp_pathmaxrxt; 2570 } else if (asoc) { 2571 asoc->pathmaxrxt = params->spp_pathmaxrxt; 2572 } else { 2573 sp->pathmaxrxt = params->spp_pathmaxrxt; 2574 } 2575 } 2576 2577 return 0; 2578 } 2579 2580 static int sctp_setsockopt_peer_addr_params(struct sock *sk, 2581 char __user *optval, 2582 unsigned int optlen) 2583 { 2584 struct sctp_paddrparams params; 2585 struct sctp_transport *trans = NULL; 2586 struct sctp_association *asoc = NULL; 2587 struct sctp_sock *sp = sctp_sk(sk); 2588 int error; 2589 int hb_change, pmtud_change, sackdelay_change; 2590 2591 if (optlen != sizeof(struct sctp_paddrparams)) 2592 return -EINVAL; 2593 2594 if (copy_from_user(¶ms, optval, optlen)) 2595 return -EFAULT; 2596 2597 /* Validate flags and value parameters. */ 2598 hb_change = params.spp_flags & SPP_HB; 2599 pmtud_change = params.spp_flags & SPP_PMTUD; 2600 sackdelay_change = params.spp_flags & SPP_SACKDELAY; 2601 2602 if (hb_change == SPP_HB || 2603 pmtud_change == SPP_PMTUD || 2604 sackdelay_change == SPP_SACKDELAY || 2605 params.spp_sackdelay > 500 || 2606 (params.spp_pathmtu && 2607 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) 2608 return -EINVAL; 2609 2610 /* If an address other than INADDR_ANY is specified, and 2611 * no transport is found, then the request is invalid. 2612 */ 2613 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { 2614 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 2615 params.spp_assoc_id); 2616 if (!trans) 2617 return -EINVAL; 2618 } 2619 2620 /* Get association, if assoc_id != 0 and the socket is a one 2621 * to many style socket, and an association was not found, then 2622 * the id was invalid. 2623 */ 2624 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 2625 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) 2626 return -EINVAL; 2627 2628 /* Heartbeat demand can only be sent on a transport or 2629 * association, but not a socket. 2630 */ 2631 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) 2632 return -EINVAL; 2633 2634 /* Process parameters. */ 2635 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, 2636 hb_change, pmtud_change, 2637 sackdelay_change); 2638 2639 if (error) 2640 return error; 2641 2642 /* If changes are for association, also apply parameters to each 2643 * transport. 2644 */ 2645 if (!trans && asoc) { 2646 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 2647 transports) { 2648 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, 2649 hb_change, pmtud_change, 2650 sackdelay_change); 2651 } 2652 } 2653 2654 return 0; 2655 } 2656 2657 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags) 2658 { 2659 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE; 2660 } 2661 2662 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags) 2663 { 2664 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE; 2665 } 2666 2667 /* 2668 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) 2669 * 2670 * This option will effect the way delayed acks are performed. This 2671 * option allows you to get or set the delayed ack time, in 2672 * milliseconds. It also allows changing the delayed ack frequency. 2673 * Changing the frequency to 1 disables the delayed sack algorithm. If 2674 * the assoc_id is 0, then this sets or gets the endpoints default 2675 * values. If the assoc_id field is non-zero, then the set or get 2676 * effects the specified association for the one to many model (the 2677 * assoc_id field is ignored by the one to one model). Note that if 2678 * sack_delay or sack_freq are 0 when setting this option, then the 2679 * current values will remain unchanged. 2680 * 2681 * struct sctp_sack_info { 2682 * sctp_assoc_t sack_assoc_id; 2683 * uint32_t sack_delay; 2684 * uint32_t sack_freq; 2685 * }; 2686 * 2687 * sack_assoc_id - This parameter, indicates which association the user 2688 * is performing an action upon. Note that if this field's value is 2689 * zero then the endpoints default value is changed (effecting future 2690 * associations only). 2691 * 2692 * sack_delay - This parameter contains the number of milliseconds that 2693 * the user is requesting the delayed ACK timer be set to. Note that 2694 * this value is defined in the standard to be between 200 and 500 2695 * milliseconds. 2696 * 2697 * sack_freq - This parameter contains the number of packets that must 2698 * be received before a sack is sent without waiting for the delay 2699 * timer to expire. The default value for this is 2, setting this 2700 * value to 1 will disable the delayed sack algorithm. 2701 */ 2702 2703 static int sctp_setsockopt_delayed_ack(struct sock *sk, 2704 char __user *optval, unsigned int optlen) 2705 { 2706 struct sctp_sack_info params; 2707 struct sctp_transport *trans = NULL; 2708 struct sctp_association *asoc = NULL; 2709 struct sctp_sock *sp = sctp_sk(sk); 2710 2711 if (optlen == sizeof(struct sctp_sack_info)) { 2712 if (copy_from_user(¶ms, optval, optlen)) 2713 return -EFAULT; 2714 2715 if (params.sack_delay == 0 && params.sack_freq == 0) 2716 return 0; 2717 } else if (optlen == sizeof(struct sctp_assoc_value)) { 2718 pr_warn_ratelimited(DEPRECATED 2719 "%s (pid %d) " 2720 "Use of struct sctp_assoc_value in delayed_ack socket option.\n" 2721 "Use struct sctp_sack_info instead\n", 2722 current->comm, task_pid_nr(current)); 2723 if (copy_from_user(¶ms, optval, optlen)) 2724 return -EFAULT; 2725 2726 if (params.sack_delay == 0) 2727 params.sack_freq = 1; 2728 else 2729 params.sack_freq = 0; 2730 } else 2731 return -EINVAL; 2732 2733 /* Validate value parameter. */ 2734 if (params.sack_delay > 500) 2735 return -EINVAL; 2736 2737 /* Get association, if sack_assoc_id != 0 and the socket is a one 2738 * to many style socket, and an association was not found, then 2739 * the id was invalid. 2740 */ 2741 asoc = sctp_id2assoc(sk, params.sack_assoc_id); 2742 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) 2743 return -EINVAL; 2744 2745 if (params.sack_delay) { 2746 if (asoc) { 2747 asoc->sackdelay = 2748 msecs_to_jiffies(params.sack_delay); 2749 asoc->param_flags = 2750 sctp_spp_sackdelay_enable(asoc->param_flags); 2751 } else { 2752 sp->sackdelay = params.sack_delay; 2753 sp->param_flags = 2754 sctp_spp_sackdelay_enable(sp->param_flags); 2755 } 2756 } 2757 2758 if (params.sack_freq == 1) { 2759 if (asoc) { 2760 asoc->param_flags = 2761 sctp_spp_sackdelay_disable(asoc->param_flags); 2762 } else { 2763 sp->param_flags = 2764 sctp_spp_sackdelay_disable(sp->param_flags); 2765 } 2766 } else if (params.sack_freq > 1) { 2767 if (asoc) { 2768 asoc->sackfreq = params.sack_freq; 2769 asoc->param_flags = 2770 sctp_spp_sackdelay_enable(asoc->param_flags); 2771 } else { 2772 sp->sackfreq = params.sack_freq; 2773 sp->param_flags = 2774 sctp_spp_sackdelay_enable(sp->param_flags); 2775 } 2776 } 2777 2778 /* If change is for association, also apply to each transport. */ 2779 if (asoc) { 2780 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 2781 transports) { 2782 if (params.sack_delay) { 2783 trans->sackdelay = 2784 msecs_to_jiffies(params.sack_delay); 2785 trans->param_flags = 2786 sctp_spp_sackdelay_enable(trans->param_flags); 2787 } 2788 if (params.sack_freq == 1) { 2789 trans->param_flags = 2790 sctp_spp_sackdelay_disable(trans->param_flags); 2791 } else if (params.sack_freq > 1) { 2792 trans->sackfreq = params.sack_freq; 2793 trans->param_flags = 2794 sctp_spp_sackdelay_enable(trans->param_flags); 2795 } 2796 } 2797 } 2798 2799 return 0; 2800 } 2801 2802 /* 7.1.3 Initialization Parameters (SCTP_INITMSG) 2803 * 2804 * Applications can specify protocol parameters for the default association 2805 * initialization. The option name argument to setsockopt() and getsockopt() 2806 * is SCTP_INITMSG. 2807 * 2808 * Setting initialization parameters is effective only on an unconnected 2809 * socket (for UDP-style sockets only future associations are effected 2810 * by the change). With TCP-style sockets, this option is inherited by 2811 * sockets derived from a listener socket. 2812 */ 2813 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen) 2814 { 2815 struct sctp_initmsg sinit; 2816 struct sctp_sock *sp = sctp_sk(sk); 2817 2818 if (optlen != sizeof(struct sctp_initmsg)) 2819 return -EINVAL; 2820 if (copy_from_user(&sinit, optval, optlen)) 2821 return -EFAULT; 2822 2823 if (sinit.sinit_num_ostreams) 2824 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; 2825 if (sinit.sinit_max_instreams) 2826 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; 2827 if (sinit.sinit_max_attempts) 2828 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; 2829 if (sinit.sinit_max_init_timeo) 2830 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; 2831 2832 return 0; 2833 } 2834 2835 /* 2836 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 2837 * 2838 * Applications that wish to use the sendto() system call may wish to 2839 * specify a default set of parameters that would normally be supplied 2840 * through the inclusion of ancillary data. This socket option allows 2841 * such an application to set the default sctp_sndrcvinfo structure. 2842 * The application that wishes to use this socket option simply passes 2843 * in to this call the sctp_sndrcvinfo structure defined in Section 2844 * 5.2.2) The input parameters accepted by this call include 2845 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 2846 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 2847 * to this call if the caller is using the UDP model. 2848 */ 2849 static int sctp_setsockopt_default_send_param(struct sock *sk, 2850 char __user *optval, 2851 unsigned int optlen) 2852 { 2853 struct sctp_sock *sp = sctp_sk(sk); 2854 struct sctp_association *asoc; 2855 struct sctp_sndrcvinfo info; 2856 2857 if (optlen != sizeof(info)) 2858 return -EINVAL; 2859 if (copy_from_user(&info, optval, optlen)) 2860 return -EFAULT; 2861 if (info.sinfo_flags & 2862 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 2863 SCTP_ABORT | SCTP_EOF)) 2864 return -EINVAL; 2865 2866 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 2867 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) 2868 return -EINVAL; 2869 if (asoc) { 2870 asoc->default_stream = info.sinfo_stream; 2871 asoc->default_flags = info.sinfo_flags; 2872 asoc->default_ppid = info.sinfo_ppid; 2873 asoc->default_context = info.sinfo_context; 2874 asoc->default_timetolive = info.sinfo_timetolive; 2875 } else { 2876 sp->default_stream = info.sinfo_stream; 2877 sp->default_flags = info.sinfo_flags; 2878 sp->default_ppid = info.sinfo_ppid; 2879 sp->default_context = info.sinfo_context; 2880 sp->default_timetolive = info.sinfo_timetolive; 2881 } 2882 2883 return 0; 2884 } 2885 2886 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters 2887 * (SCTP_DEFAULT_SNDINFO) 2888 */ 2889 static int sctp_setsockopt_default_sndinfo(struct sock *sk, 2890 char __user *optval, 2891 unsigned int optlen) 2892 { 2893 struct sctp_sock *sp = sctp_sk(sk); 2894 struct sctp_association *asoc; 2895 struct sctp_sndinfo info; 2896 2897 if (optlen != sizeof(info)) 2898 return -EINVAL; 2899 if (copy_from_user(&info, optval, optlen)) 2900 return -EFAULT; 2901 if (info.snd_flags & 2902 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 2903 SCTP_ABORT | SCTP_EOF)) 2904 return -EINVAL; 2905 2906 asoc = sctp_id2assoc(sk, info.snd_assoc_id); 2907 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) 2908 return -EINVAL; 2909 if (asoc) { 2910 asoc->default_stream = info.snd_sid; 2911 asoc->default_flags = info.snd_flags; 2912 asoc->default_ppid = info.snd_ppid; 2913 asoc->default_context = info.snd_context; 2914 } else { 2915 sp->default_stream = info.snd_sid; 2916 sp->default_flags = info.snd_flags; 2917 sp->default_ppid = info.snd_ppid; 2918 sp->default_context = info.snd_context; 2919 } 2920 2921 return 0; 2922 } 2923 2924 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 2925 * 2926 * Requests that the local SCTP stack use the enclosed peer address as 2927 * the association primary. The enclosed address must be one of the 2928 * association peer's addresses. 2929 */ 2930 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, 2931 unsigned int optlen) 2932 { 2933 struct sctp_prim prim; 2934 struct sctp_transport *trans; 2935 2936 if (optlen != sizeof(struct sctp_prim)) 2937 return -EINVAL; 2938 2939 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) 2940 return -EFAULT; 2941 2942 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); 2943 if (!trans) 2944 return -EINVAL; 2945 2946 sctp_assoc_set_primary(trans->asoc, trans); 2947 2948 return 0; 2949 } 2950 2951 /* 2952 * 7.1.5 SCTP_NODELAY 2953 * 2954 * Turn on/off any Nagle-like algorithm. This means that packets are 2955 * generally sent as soon as possible and no unnecessary delays are 2956 * introduced, at the cost of more packets in the network. Expects an 2957 * integer boolean flag. 2958 */ 2959 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, 2960 unsigned int optlen) 2961 { 2962 int val; 2963 2964 if (optlen < sizeof(int)) 2965 return -EINVAL; 2966 if (get_user(val, (int __user *)optval)) 2967 return -EFAULT; 2968 2969 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; 2970 return 0; 2971 } 2972 2973 /* 2974 * 2975 * 7.1.1 SCTP_RTOINFO 2976 * 2977 * The protocol parameters used to initialize and bound retransmission 2978 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 2979 * and modify these parameters. 2980 * All parameters are time values, in milliseconds. A value of 0, when 2981 * modifying the parameters, indicates that the current value should not 2982 * be changed. 2983 * 2984 */ 2985 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) 2986 { 2987 struct sctp_rtoinfo rtoinfo; 2988 struct sctp_association *asoc; 2989 unsigned long rto_min, rto_max; 2990 struct sctp_sock *sp = sctp_sk(sk); 2991 2992 if (optlen != sizeof (struct sctp_rtoinfo)) 2993 return -EINVAL; 2994 2995 if (copy_from_user(&rtoinfo, optval, optlen)) 2996 return -EFAULT; 2997 2998 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 2999 3000 /* Set the values to the specific association */ 3001 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) 3002 return -EINVAL; 3003 3004 rto_max = rtoinfo.srto_max; 3005 rto_min = rtoinfo.srto_min; 3006 3007 if (rto_max) 3008 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max; 3009 else 3010 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max; 3011 3012 if (rto_min) 3013 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min; 3014 else 3015 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min; 3016 3017 if (rto_min > rto_max) 3018 return -EINVAL; 3019 3020 if (asoc) { 3021 if (rtoinfo.srto_initial != 0) 3022 asoc->rto_initial = 3023 msecs_to_jiffies(rtoinfo.srto_initial); 3024 asoc->rto_max = rto_max; 3025 asoc->rto_min = rto_min; 3026 } else { 3027 /* If there is no association or the association-id = 0 3028 * set the values to the endpoint. 3029 */ 3030 if (rtoinfo.srto_initial != 0) 3031 sp->rtoinfo.srto_initial = rtoinfo.srto_initial; 3032 sp->rtoinfo.srto_max = rto_max; 3033 sp->rtoinfo.srto_min = rto_min; 3034 } 3035 3036 return 0; 3037 } 3038 3039 /* 3040 * 3041 * 7.1.2 SCTP_ASSOCINFO 3042 * 3043 * This option is used to tune the maximum retransmission attempts 3044 * of the association. 3045 * Returns an error if the new association retransmission value is 3046 * greater than the sum of the retransmission value of the peer. 3047 * See [SCTP] for more information. 3048 * 3049 */ 3050 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen) 3051 { 3052 3053 struct sctp_assocparams assocparams; 3054 struct sctp_association *asoc; 3055 3056 if (optlen != sizeof(struct sctp_assocparams)) 3057 return -EINVAL; 3058 if (copy_from_user(&assocparams, optval, optlen)) 3059 return -EFAULT; 3060 3061 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 3062 3063 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) 3064 return -EINVAL; 3065 3066 /* Set the values to the specific association */ 3067 if (asoc) { 3068 if (assocparams.sasoc_asocmaxrxt != 0) { 3069 __u32 path_sum = 0; 3070 int paths = 0; 3071 struct sctp_transport *peer_addr; 3072 3073 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, 3074 transports) { 3075 path_sum += peer_addr->pathmaxrxt; 3076 paths++; 3077 } 3078 3079 /* Only validate asocmaxrxt if we have more than 3080 * one path/transport. We do this because path 3081 * retransmissions are only counted when we have more 3082 * then one path. 3083 */ 3084 if (paths > 1 && 3085 assocparams.sasoc_asocmaxrxt > path_sum) 3086 return -EINVAL; 3087 3088 asoc->max_retrans = assocparams.sasoc_asocmaxrxt; 3089 } 3090 3091 if (assocparams.sasoc_cookie_life != 0) 3092 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life); 3093 } else { 3094 /* Set the values to the endpoint */ 3095 struct sctp_sock *sp = sctp_sk(sk); 3096 3097 if (assocparams.sasoc_asocmaxrxt != 0) 3098 sp->assocparams.sasoc_asocmaxrxt = 3099 assocparams.sasoc_asocmaxrxt; 3100 if (assocparams.sasoc_cookie_life != 0) 3101 sp->assocparams.sasoc_cookie_life = 3102 assocparams.sasoc_cookie_life; 3103 } 3104 return 0; 3105 } 3106 3107 /* 3108 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 3109 * 3110 * This socket option is a boolean flag which turns on or off mapped V4 3111 * addresses. If this option is turned on and the socket is type 3112 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 3113 * If this option is turned off, then no mapping will be done of V4 3114 * addresses and a user will receive both PF_INET6 and PF_INET type 3115 * addresses on the socket. 3116 */ 3117 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen) 3118 { 3119 int val; 3120 struct sctp_sock *sp = sctp_sk(sk); 3121 3122 if (optlen < sizeof(int)) 3123 return -EINVAL; 3124 if (get_user(val, (int __user *)optval)) 3125 return -EFAULT; 3126 if (val) 3127 sp->v4mapped = 1; 3128 else 3129 sp->v4mapped = 0; 3130 3131 return 0; 3132 } 3133 3134 /* 3135 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 3136 * This option will get or set the maximum size to put in any outgoing 3137 * SCTP DATA chunk. If a message is larger than this size it will be 3138 * fragmented by SCTP into the specified size. Note that the underlying 3139 * SCTP implementation may fragment into smaller sized chunks when the 3140 * PMTU of the underlying association is smaller than the value set by 3141 * the user. The default value for this option is '0' which indicates 3142 * the user is NOT limiting fragmentation and only the PMTU will effect 3143 * SCTP's choice of DATA chunk size. Note also that values set larger 3144 * than the maximum size of an IP datagram will effectively let SCTP 3145 * control fragmentation (i.e. the same as setting this option to 0). 3146 * 3147 * The following structure is used to access and modify this parameter: 3148 * 3149 * struct sctp_assoc_value { 3150 * sctp_assoc_t assoc_id; 3151 * uint32_t assoc_value; 3152 * }; 3153 * 3154 * assoc_id: This parameter is ignored for one-to-one style sockets. 3155 * For one-to-many style sockets this parameter indicates which 3156 * association the user is performing an action upon. Note that if 3157 * this field's value is zero then the endpoints default value is 3158 * changed (effecting future associations only). 3159 * assoc_value: This parameter specifies the maximum size in bytes. 3160 */ 3161 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen) 3162 { 3163 struct sctp_sock *sp = sctp_sk(sk); 3164 struct sctp_assoc_value params; 3165 struct sctp_association *asoc; 3166 int val; 3167 3168 if (optlen == sizeof(int)) { 3169 pr_warn_ratelimited(DEPRECATED 3170 "%s (pid %d) " 3171 "Use of int in maxseg socket option.\n" 3172 "Use struct sctp_assoc_value instead\n", 3173 current->comm, task_pid_nr(current)); 3174 if (copy_from_user(&val, optval, optlen)) 3175 return -EFAULT; 3176 params.assoc_id = 0; 3177 } else if (optlen == sizeof(struct sctp_assoc_value)) { 3178 if (copy_from_user(¶ms, optval, optlen)) 3179 return -EFAULT; 3180 val = params.assoc_value; 3181 } else { 3182 return -EINVAL; 3183 } 3184 3185 if (val) { 3186 int min_len, max_len; 3187 3188 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len; 3189 min_len -= sizeof(struct sctphdr) + 3190 sizeof(struct sctp_data_chunk); 3191 3192 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk); 3193 3194 if (val < min_len || val > max_len) 3195 return -EINVAL; 3196 } 3197 3198 asoc = sctp_id2assoc(sk, params.assoc_id); 3199 if (asoc) { 3200 if (val == 0) { 3201 val = asoc->pathmtu - sp->pf->af->net_header_len; 3202 val -= sizeof(struct sctphdr) + 3203 sctp_datachk_len(&asoc->stream); 3204 } 3205 asoc->user_frag = val; 3206 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu); 3207 } else { 3208 if (params.assoc_id && sctp_style(sk, UDP)) 3209 return -EINVAL; 3210 sp->user_frag = val; 3211 } 3212 3213 return 0; 3214 } 3215 3216 3217 /* 3218 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) 3219 * 3220 * Requests that the peer mark the enclosed address as the association 3221 * primary. The enclosed address must be one of the association's 3222 * locally bound addresses. The following structure is used to make a 3223 * set primary request: 3224 */ 3225 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, 3226 unsigned int optlen) 3227 { 3228 struct net *net = sock_net(sk); 3229 struct sctp_sock *sp; 3230 struct sctp_association *asoc = NULL; 3231 struct sctp_setpeerprim prim; 3232 struct sctp_chunk *chunk; 3233 struct sctp_af *af; 3234 int err; 3235 3236 sp = sctp_sk(sk); 3237 3238 if (!net->sctp.addip_enable) 3239 return -EPERM; 3240 3241 if (optlen != sizeof(struct sctp_setpeerprim)) 3242 return -EINVAL; 3243 3244 if (copy_from_user(&prim, optval, optlen)) 3245 return -EFAULT; 3246 3247 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); 3248 if (!asoc) 3249 return -EINVAL; 3250 3251 if (!asoc->peer.asconf_capable) 3252 return -EPERM; 3253 3254 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) 3255 return -EPERM; 3256 3257 if (!sctp_state(asoc, ESTABLISHED)) 3258 return -ENOTCONN; 3259 3260 af = sctp_get_af_specific(prim.sspp_addr.ss_family); 3261 if (!af) 3262 return -EINVAL; 3263 3264 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL)) 3265 return -EADDRNOTAVAIL; 3266 3267 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) 3268 return -EADDRNOTAVAIL; 3269 3270 /* Create an ASCONF chunk with SET_PRIMARY parameter */ 3271 chunk = sctp_make_asconf_set_prim(asoc, 3272 (union sctp_addr *)&prim.sspp_addr); 3273 if (!chunk) 3274 return -ENOMEM; 3275 3276 err = sctp_send_asconf(asoc, chunk); 3277 3278 pr_debug("%s: we set peer primary addr primitively\n", __func__); 3279 3280 return err; 3281 } 3282 3283 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, 3284 unsigned int optlen) 3285 { 3286 struct sctp_setadaptation adaptation; 3287 3288 if (optlen != sizeof(struct sctp_setadaptation)) 3289 return -EINVAL; 3290 if (copy_from_user(&adaptation, optval, optlen)) 3291 return -EFAULT; 3292 3293 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; 3294 3295 return 0; 3296 } 3297 3298 /* 3299 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 3300 * 3301 * The context field in the sctp_sndrcvinfo structure is normally only 3302 * used when a failed message is retrieved holding the value that was 3303 * sent down on the actual send call. This option allows the setting of 3304 * a default context on an association basis that will be received on 3305 * reading messages from the peer. This is especially helpful in the 3306 * one-2-many model for an application to keep some reference to an 3307 * internal state machine that is processing messages on the 3308 * association. Note that the setting of this value only effects 3309 * received messages from the peer and does not effect the value that is 3310 * saved with outbound messages. 3311 */ 3312 static int sctp_setsockopt_context(struct sock *sk, char __user *optval, 3313 unsigned int optlen) 3314 { 3315 struct sctp_assoc_value params; 3316 struct sctp_sock *sp; 3317 struct sctp_association *asoc; 3318 3319 if (optlen != sizeof(struct sctp_assoc_value)) 3320 return -EINVAL; 3321 if (copy_from_user(¶ms, optval, optlen)) 3322 return -EFAULT; 3323 3324 sp = sctp_sk(sk); 3325 3326 if (params.assoc_id != 0) { 3327 asoc = sctp_id2assoc(sk, params.assoc_id); 3328 if (!asoc) 3329 return -EINVAL; 3330 asoc->default_rcv_context = params.assoc_value; 3331 } else { 3332 sp->default_rcv_context = params.assoc_value; 3333 } 3334 3335 return 0; 3336 } 3337 3338 /* 3339 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 3340 * 3341 * This options will at a minimum specify if the implementation is doing 3342 * fragmented interleave. Fragmented interleave, for a one to many 3343 * socket, is when subsequent calls to receive a message may return 3344 * parts of messages from different associations. Some implementations 3345 * may allow you to turn this value on or off. If so, when turned off, 3346 * no fragment interleave will occur (which will cause a head of line 3347 * blocking amongst multiple associations sharing the same one to many 3348 * socket). When this option is turned on, then each receive call may 3349 * come from a different association (thus the user must receive data 3350 * with the extended calls (e.g. sctp_recvmsg) to keep track of which 3351 * association each receive belongs to. 3352 * 3353 * This option takes a boolean value. A non-zero value indicates that 3354 * fragmented interleave is on. A value of zero indicates that 3355 * fragmented interleave is off. 3356 * 3357 * Note that it is important that an implementation that allows this 3358 * option to be turned on, have it off by default. Otherwise an unaware 3359 * application using the one to many model may become confused and act 3360 * incorrectly. 3361 */ 3362 static int sctp_setsockopt_fragment_interleave(struct sock *sk, 3363 char __user *optval, 3364 unsigned int optlen) 3365 { 3366 int val; 3367 3368 if (optlen != sizeof(int)) 3369 return -EINVAL; 3370 if (get_user(val, (int __user *)optval)) 3371 return -EFAULT; 3372 3373 sctp_sk(sk)->frag_interleave = !!val; 3374 3375 if (!sctp_sk(sk)->frag_interleave) 3376 sctp_sk(sk)->strm_interleave = 0; 3377 3378 return 0; 3379 } 3380 3381 /* 3382 * 8.1.21. Set or Get the SCTP Partial Delivery Point 3383 * (SCTP_PARTIAL_DELIVERY_POINT) 3384 * 3385 * This option will set or get the SCTP partial delivery point. This 3386 * point is the size of a message where the partial delivery API will be 3387 * invoked to help free up rwnd space for the peer. Setting this to a 3388 * lower value will cause partial deliveries to happen more often. The 3389 * calls argument is an integer that sets or gets the partial delivery 3390 * point. Note also that the call will fail if the user attempts to set 3391 * this value larger than the socket receive buffer size. 3392 * 3393 * Note that any single message having a length smaller than or equal to 3394 * the SCTP partial delivery point will be delivered in one single read 3395 * call as long as the user provided buffer is large enough to hold the 3396 * message. 3397 */ 3398 static int sctp_setsockopt_partial_delivery_point(struct sock *sk, 3399 char __user *optval, 3400 unsigned int optlen) 3401 { 3402 u32 val; 3403 3404 if (optlen != sizeof(u32)) 3405 return -EINVAL; 3406 if (get_user(val, (int __user *)optval)) 3407 return -EFAULT; 3408 3409 /* Note: We double the receive buffer from what the user sets 3410 * it to be, also initial rwnd is based on rcvbuf/2. 3411 */ 3412 if (val > (sk->sk_rcvbuf >> 1)) 3413 return -EINVAL; 3414 3415 sctp_sk(sk)->pd_point = val; 3416 3417 return 0; /* is this the right error code? */ 3418 } 3419 3420 /* 3421 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 3422 * 3423 * This option will allow a user to change the maximum burst of packets 3424 * that can be emitted by this association. Note that the default value 3425 * is 4, and some implementations may restrict this setting so that it 3426 * can only be lowered. 3427 * 3428 * NOTE: This text doesn't seem right. Do this on a socket basis with 3429 * future associations inheriting the socket value. 3430 */ 3431 static int sctp_setsockopt_maxburst(struct sock *sk, 3432 char __user *optval, 3433 unsigned int optlen) 3434 { 3435 struct sctp_assoc_value params; 3436 struct sctp_sock *sp; 3437 struct sctp_association *asoc; 3438 int val; 3439 int assoc_id = 0; 3440 3441 if (optlen == sizeof(int)) { 3442 pr_warn_ratelimited(DEPRECATED 3443 "%s (pid %d) " 3444 "Use of int in max_burst socket option deprecated.\n" 3445 "Use struct sctp_assoc_value instead\n", 3446 current->comm, task_pid_nr(current)); 3447 if (copy_from_user(&val, optval, optlen)) 3448 return -EFAULT; 3449 } else if (optlen == sizeof(struct sctp_assoc_value)) { 3450 if (copy_from_user(¶ms, optval, optlen)) 3451 return -EFAULT; 3452 val = params.assoc_value; 3453 assoc_id = params.assoc_id; 3454 } else 3455 return -EINVAL; 3456 3457 sp = sctp_sk(sk); 3458 3459 if (assoc_id != 0) { 3460 asoc = sctp_id2assoc(sk, assoc_id); 3461 if (!asoc) 3462 return -EINVAL; 3463 asoc->max_burst = val; 3464 } else 3465 sp->max_burst = val; 3466 3467 return 0; 3468 } 3469 3470 /* 3471 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) 3472 * 3473 * This set option adds a chunk type that the user is requesting to be 3474 * received only in an authenticated way. Changes to the list of chunks 3475 * will only effect future associations on the socket. 3476 */ 3477 static int sctp_setsockopt_auth_chunk(struct sock *sk, 3478 char __user *optval, 3479 unsigned int optlen) 3480 { 3481 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3482 struct sctp_authchunk val; 3483 3484 if (!ep->auth_enable) 3485 return -EACCES; 3486 3487 if (optlen != sizeof(struct sctp_authchunk)) 3488 return -EINVAL; 3489 if (copy_from_user(&val, optval, optlen)) 3490 return -EFAULT; 3491 3492 switch (val.sauth_chunk) { 3493 case SCTP_CID_INIT: 3494 case SCTP_CID_INIT_ACK: 3495 case SCTP_CID_SHUTDOWN_COMPLETE: 3496 case SCTP_CID_AUTH: 3497 return -EINVAL; 3498 } 3499 3500 /* add this chunk id to the endpoint */ 3501 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk); 3502 } 3503 3504 /* 3505 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) 3506 * 3507 * This option gets or sets the list of HMAC algorithms that the local 3508 * endpoint requires the peer to use. 3509 */ 3510 static int sctp_setsockopt_hmac_ident(struct sock *sk, 3511 char __user *optval, 3512 unsigned int optlen) 3513 { 3514 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3515 struct sctp_hmacalgo *hmacs; 3516 u32 idents; 3517 int err; 3518 3519 if (!ep->auth_enable) 3520 return -EACCES; 3521 3522 if (optlen < sizeof(struct sctp_hmacalgo)) 3523 return -EINVAL; 3524 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) + 3525 SCTP_AUTH_NUM_HMACS * sizeof(u16)); 3526 3527 hmacs = memdup_user(optval, optlen); 3528 if (IS_ERR(hmacs)) 3529 return PTR_ERR(hmacs); 3530 3531 idents = hmacs->shmac_num_idents; 3532 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || 3533 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { 3534 err = -EINVAL; 3535 goto out; 3536 } 3537 3538 err = sctp_auth_ep_set_hmacs(ep, hmacs); 3539 out: 3540 kfree(hmacs); 3541 return err; 3542 } 3543 3544 /* 3545 * 7.1.20. Set a shared key (SCTP_AUTH_KEY) 3546 * 3547 * This option will set a shared secret key which is used to build an 3548 * association shared key. 3549 */ 3550 static int sctp_setsockopt_auth_key(struct sock *sk, 3551 char __user *optval, 3552 unsigned int optlen) 3553 { 3554 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3555 struct sctp_authkey *authkey; 3556 struct sctp_association *asoc; 3557 int ret; 3558 3559 if (!ep->auth_enable) 3560 return -EACCES; 3561 3562 if (optlen <= sizeof(struct sctp_authkey)) 3563 return -EINVAL; 3564 /* authkey->sca_keylength is u16, so optlen can't be bigger than 3565 * this. 3566 */ 3567 optlen = min_t(unsigned int, optlen, USHRT_MAX + 3568 sizeof(struct sctp_authkey)); 3569 3570 authkey = memdup_user(optval, optlen); 3571 if (IS_ERR(authkey)) 3572 return PTR_ERR(authkey); 3573 3574 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { 3575 ret = -EINVAL; 3576 goto out; 3577 } 3578 3579 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); 3580 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { 3581 ret = -EINVAL; 3582 goto out; 3583 } 3584 3585 ret = sctp_auth_set_key(ep, asoc, authkey); 3586 out: 3587 kzfree(authkey); 3588 return ret; 3589 } 3590 3591 /* 3592 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) 3593 * 3594 * This option will get or set the active shared key to be used to build 3595 * the association shared key. 3596 */ 3597 static int sctp_setsockopt_active_key(struct sock *sk, 3598 char __user *optval, 3599 unsigned int optlen) 3600 { 3601 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3602 struct sctp_authkeyid val; 3603 struct sctp_association *asoc; 3604 3605 if (!ep->auth_enable) 3606 return -EACCES; 3607 3608 if (optlen != sizeof(struct sctp_authkeyid)) 3609 return -EINVAL; 3610 if (copy_from_user(&val, optval, optlen)) 3611 return -EFAULT; 3612 3613 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 3614 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 3615 return -EINVAL; 3616 3617 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber); 3618 } 3619 3620 /* 3621 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) 3622 * 3623 * This set option will delete a shared secret key from use. 3624 */ 3625 static int sctp_setsockopt_del_key(struct sock *sk, 3626 char __user *optval, 3627 unsigned int optlen) 3628 { 3629 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3630 struct sctp_authkeyid val; 3631 struct sctp_association *asoc; 3632 3633 if (!ep->auth_enable) 3634 return -EACCES; 3635 3636 if (optlen != sizeof(struct sctp_authkeyid)) 3637 return -EINVAL; 3638 if (copy_from_user(&val, optval, optlen)) 3639 return -EFAULT; 3640 3641 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 3642 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 3643 return -EINVAL; 3644 3645 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber); 3646 3647 } 3648 3649 /* 3650 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) 3651 * 3652 * This set option will deactivate a shared secret key. 3653 */ 3654 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval, 3655 unsigned int optlen) 3656 { 3657 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3658 struct sctp_authkeyid val; 3659 struct sctp_association *asoc; 3660 3661 if (!ep->auth_enable) 3662 return -EACCES; 3663 3664 if (optlen != sizeof(struct sctp_authkeyid)) 3665 return -EINVAL; 3666 if (copy_from_user(&val, optval, optlen)) 3667 return -EFAULT; 3668 3669 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 3670 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 3671 return -EINVAL; 3672 3673 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber); 3674 } 3675 3676 /* 3677 * 8.1.23 SCTP_AUTO_ASCONF 3678 * 3679 * This option will enable or disable the use of the automatic generation of 3680 * ASCONF chunks to add and delete addresses to an existing association. Note 3681 * that this option has two caveats namely: a) it only affects sockets that 3682 * are bound to all addresses available to the SCTP stack, and b) the system 3683 * administrator may have an overriding control that turns the ASCONF feature 3684 * off no matter what setting the socket option may have. 3685 * This option expects an integer boolean flag, where a non-zero value turns on 3686 * the option, and a zero value turns off the option. 3687 * Note. In this implementation, socket operation overrides default parameter 3688 * being set by sysctl as well as FreeBSD implementation 3689 */ 3690 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval, 3691 unsigned int optlen) 3692 { 3693 int val; 3694 struct sctp_sock *sp = sctp_sk(sk); 3695 3696 if (optlen < sizeof(int)) 3697 return -EINVAL; 3698 if (get_user(val, (int __user *)optval)) 3699 return -EFAULT; 3700 if (!sctp_is_ep_boundall(sk) && val) 3701 return -EINVAL; 3702 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf)) 3703 return 0; 3704 3705 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); 3706 if (val == 0 && sp->do_auto_asconf) { 3707 list_del(&sp->auto_asconf_list); 3708 sp->do_auto_asconf = 0; 3709 } else if (val && !sp->do_auto_asconf) { 3710 list_add_tail(&sp->auto_asconf_list, 3711 &sock_net(sk)->sctp.auto_asconf_splist); 3712 sp->do_auto_asconf = 1; 3713 } 3714 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); 3715 return 0; 3716 } 3717 3718 /* 3719 * SCTP_PEER_ADDR_THLDS 3720 * 3721 * This option allows us to alter the partially failed threshold for one or all 3722 * transports in an association. See Section 6.1 of: 3723 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt 3724 */ 3725 static int sctp_setsockopt_paddr_thresholds(struct sock *sk, 3726 char __user *optval, 3727 unsigned int optlen) 3728 { 3729 struct sctp_paddrthlds val; 3730 struct sctp_transport *trans; 3731 struct sctp_association *asoc; 3732 3733 if (optlen < sizeof(struct sctp_paddrthlds)) 3734 return -EINVAL; 3735 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, 3736 sizeof(struct sctp_paddrthlds))) 3737 return -EFAULT; 3738 3739 3740 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { 3741 asoc = sctp_id2assoc(sk, val.spt_assoc_id); 3742 if (!asoc) 3743 return -ENOENT; 3744 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 3745 transports) { 3746 if (val.spt_pathmaxrxt) 3747 trans->pathmaxrxt = val.spt_pathmaxrxt; 3748 trans->pf_retrans = val.spt_pathpfthld; 3749 } 3750 3751 if (val.spt_pathmaxrxt) 3752 asoc->pathmaxrxt = val.spt_pathmaxrxt; 3753 asoc->pf_retrans = val.spt_pathpfthld; 3754 } else { 3755 trans = sctp_addr_id2transport(sk, &val.spt_address, 3756 val.spt_assoc_id); 3757 if (!trans) 3758 return -ENOENT; 3759 3760 if (val.spt_pathmaxrxt) 3761 trans->pathmaxrxt = val.spt_pathmaxrxt; 3762 trans->pf_retrans = val.spt_pathpfthld; 3763 } 3764 3765 return 0; 3766 } 3767 3768 static int sctp_setsockopt_recvrcvinfo(struct sock *sk, 3769 char __user *optval, 3770 unsigned int optlen) 3771 { 3772 int val; 3773 3774 if (optlen < sizeof(int)) 3775 return -EINVAL; 3776 if (get_user(val, (int __user *) optval)) 3777 return -EFAULT; 3778 3779 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1; 3780 3781 return 0; 3782 } 3783 3784 static int sctp_setsockopt_recvnxtinfo(struct sock *sk, 3785 char __user *optval, 3786 unsigned int optlen) 3787 { 3788 int val; 3789 3790 if (optlen < sizeof(int)) 3791 return -EINVAL; 3792 if (get_user(val, (int __user *) optval)) 3793 return -EFAULT; 3794 3795 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1; 3796 3797 return 0; 3798 } 3799 3800 static int sctp_setsockopt_pr_supported(struct sock *sk, 3801 char __user *optval, 3802 unsigned int optlen) 3803 { 3804 struct sctp_assoc_value params; 3805 struct sctp_association *asoc; 3806 int retval = -EINVAL; 3807 3808 if (optlen != sizeof(params)) 3809 goto out; 3810 3811 if (copy_from_user(¶ms, optval, optlen)) { 3812 retval = -EFAULT; 3813 goto out; 3814 } 3815 3816 asoc = sctp_id2assoc(sk, params.assoc_id); 3817 if (asoc) { 3818 asoc->prsctp_enable = !!params.assoc_value; 3819 } else if (!params.assoc_id) { 3820 struct sctp_sock *sp = sctp_sk(sk); 3821 3822 sp->ep->prsctp_enable = !!params.assoc_value; 3823 } else { 3824 goto out; 3825 } 3826 3827 retval = 0; 3828 3829 out: 3830 return retval; 3831 } 3832 3833 static int sctp_setsockopt_default_prinfo(struct sock *sk, 3834 char __user *optval, 3835 unsigned int optlen) 3836 { 3837 struct sctp_default_prinfo info; 3838 struct sctp_association *asoc; 3839 int retval = -EINVAL; 3840 3841 if (optlen != sizeof(info)) 3842 goto out; 3843 3844 if (copy_from_user(&info, optval, sizeof(info))) { 3845 retval = -EFAULT; 3846 goto out; 3847 } 3848 3849 if (info.pr_policy & ~SCTP_PR_SCTP_MASK) 3850 goto out; 3851 3852 if (info.pr_policy == SCTP_PR_SCTP_NONE) 3853 info.pr_value = 0; 3854 3855 asoc = sctp_id2assoc(sk, info.pr_assoc_id); 3856 if (asoc) { 3857 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy); 3858 asoc->default_timetolive = info.pr_value; 3859 } else if (!info.pr_assoc_id) { 3860 struct sctp_sock *sp = sctp_sk(sk); 3861 3862 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy); 3863 sp->default_timetolive = info.pr_value; 3864 } else { 3865 goto out; 3866 } 3867 3868 retval = 0; 3869 3870 out: 3871 return retval; 3872 } 3873 3874 static int sctp_setsockopt_reconfig_supported(struct sock *sk, 3875 char __user *optval, 3876 unsigned int optlen) 3877 { 3878 struct sctp_assoc_value params; 3879 struct sctp_association *asoc; 3880 int retval = -EINVAL; 3881 3882 if (optlen != sizeof(params)) 3883 goto out; 3884 3885 if (copy_from_user(¶ms, optval, optlen)) { 3886 retval = -EFAULT; 3887 goto out; 3888 } 3889 3890 asoc = sctp_id2assoc(sk, params.assoc_id); 3891 if (asoc) { 3892 asoc->reconf_enable = !!params.assoc_value; 3893 } else if (!params.assoc_id) { 3894 struct sctp_sock *sp = sctp_sk(sk); 3895 3896 sp->ep->reconf_enable = !!params.assoc_value; 3897 } else { 3898 goto out; 3899 } 3900 3901 retval = 0; 3902 3903 out: 3904 return retval; 3905 } 3906 3907 static int sctp_setsockopt_enable_strreset(struct sock *sk, 3908 char __user *optval, 3909 unsigned int optlen) 3910 { 3911 struct sctp_assoc_value params; 3912 struct sctp_association *asoc; 3913 int retval = -EINVAL; 3914 3915 if (optlen != sizeof(params)) 3916 goto out; 3917 3918 if (copy_from_user(¶ms, optval, optlen)) { 3919 retval = -EFAULT; 3920 goto out; 3921 } 3922 3923 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK)) 3924 goto out; 3925 3926 asoc = sctp_id2assoc(sk, params.assoc_id); 3927 if (asoc) { 3928 asoc->strreset_enable = params.assoc_value; 3929 } else if (!params.assoc_id) { 3930 struct sctp_sock *sp = sctp_sk(sk); 3931 3932 sp->ep->strreset_enable = params.assoc_value; 3933 } else { 3934 goto out; 3935 } 3936 3937 retval = 0; 3938 3939 out: 3940 return retval; 3941 } 3942 3943 static int sctp_setsockopt_reset_streams(struct sock *sk, 3944 char __user *optval, 3945 unsigned int optlen) 3946 { 3947 struct sctp_reset_streams *params; 3948 struct sctp_association *asoc; 3949 int retval = -EINVAL; 3950 3951 if (optlen < sizeof(*params)) 3952 return -EINVAL; 3953 /* srs_number_streams is u16, so optlen can't be bigger than this. */ 3954 optlen = min_t(unsigned int, optlen, USHRT_MAX + 3955 sizeof(__u16) * sizeof(*params)); 3956 3957 params = memdup_user(optval, optlen); 3958 if (IS_ERR(params)) 3959 return PTR_ERR(params); 3960 3961 if (params->srs_number_streams * sizeof(__u16) > 3962 optlen - sizeof(*params)) 3963 goto out; 3964 3965 asoc = sctp_id2assoc(sk, params->srs_assoc_id); 3966 if (!asoc) 3967 goto out; 3968 3969 retval = sctp_send_reset_streams(asoc, params); 3970 3971 out: 3972 kfree(params); 3973 return retval; 3974 } 3975 3976 static int sctp_setsockopt_reset_assoc(struct sock *sk, 3977 char __user *optval, 3978 unsigned int optlen) 3979 { 3980 struct sctp_association *asoc; 3981 sctp_assoc_t associd; 3982 int retval = -EINVAL; 3983 3984 if (optlen != sizeof(associd)) 3985 goto out; 3986 3987 if (copy_from_user(&associd, optval, optlen)) { 3988 retval = -EFAULT; 3989 goto out; 3990 } 3991 3992 asoc = sctp_id2assoc(sk, associd); 3993 if (!asoc) 3994 goto out; 3995 3996 retval = sctp_send_reset_assoc(asoc); 3997 3998 out: 3999 return retval; 4000 } 4001 4002 static int sctp_setsockopt_add_streams(struct sock *sk, 4003 char __user *optval, 4004 unsigned int optlen) 4005 { 4006 struct sctp_association *asoc; 4007 struct sctp_add_streams params; 4008 int retval = -EINVAL; 4009 4010 if (optlen != sizeof(params)) 4011 goto out; 4012 4013 if (copy_from_user(¶ms, optval, optlen)) { 4014 retval = -EFAULT; 4015 goto out; 4016 } 4017 4018 asoc = sctp_id2assoc(sk, params.sas_assoc_id); 4019 if (!asoc) 4020 goto out; 4021 4022 retval = sctp_send_add_streams(asoc, ¶ms); 4023 4024 out: 4025 return retval; 4026 } 4027 4028 static int sctp_setsockopt_scheduler(struct sock *sk, 4029 char __user *optval, 4030 unsigned int optlen) 4031 { 4032 struct sctp_association *asoc; 4033 struct sctp_assoc_value params; 4034 int retval = -EINVAL; 4035 4036 if (optlen < sizeof(params)) 4037 goto out; 4038 4039 optlen = sizeof(params); 4040 if (copy_from_user(¶ms, optval, optlen)) { 4041 retval = -EFAULT; 4042 goto out; 4043 } 4044 4045 if (params.assoc_value > SCTP_SS_MAX) 4046 goto out; 4047 4048 asoc = sctp_id2assoc(sk, params.assoc_id); 4049 if (!asoc) 4050 goto out; 4051 4052 retval = sctp_sched_set_sched(asoc, params.assoc_value); 4053 4054 out: 4055 return retval; 4056 } 4057 4058 static int sctp_setsockopt_scheduler_value(struct sock *sk, 4059 char __user *optval, 4060 unsigned int optlen) 4061 { 4062 struct sctp_association *asoc; 4063 struct sctp_stream_value params; 4064 int retval = -EINVAL; 4065 4066 if (optlen < sizeof(params)) 4067 goto out; 4068 4069 optlen = sizeof(params); 4070 if (copy_from_user(¶ms, optval, optlen)) { 4071 retval = -EFAULT; 4072 goto out; 4073 } 4074 4075 asoc = sctp_id2assoc(sk, params.assoc_id); 4076 if (!asoc) 4077 goto out; 4078 4079 retval = sctp_sched_set_value(asoc, params.stream_id, 4080 params.stream_value, GFP_KERNEL); 4081 4082 out: 4083 return retval; 4084 } 4085 4086 static int sctp_setsockopt_interleaving_supported(struct sock *sk, 4087 char __user *optval, 4088 unsigned int optlen) 4089 { 4090 struct sctp_sock *sp = sctp_sk(sk); 4091 struct net *net = sock_net(sk); 4092 struct sctp_assoc_value params; 4093 int retval = -EINVAL; 4094 4095 if (optlen < sizeof(params)) 4096 goto out; 4097 4098 optlen = sizeof(params); 4099 if (copy_from_user(¶ms, optval, optlen)) { 4100 retval = -EFAULT; 4101 goto out; 4102 } 4103 4104 if (params.assoc_id) 4105 goto out; 4106 4107 if (!net->sctp.intl_enable || !sp->frag_interleave) { 4108 retval = -EPERM; 4109 goto out; 4110 } 4111 4112 sp->strm_interleave = !!params.assoc_value; 4113 4114 retval = 0; 4115 4116 out: 4117 return retval; 4118 } 4119 4120 /* API 6.2 setsockopt(), getsockopt() 4121 * 4122 * Applications use setsockopt() and getsockopt() to set or retrieve 4123 * socket options. Socket options are used to change the default 4124 * behavior of sockets calls. They are described in Section 7. 4125 * 4126 * The syntax is: 4127 * 4128 * ret = getsockopt(int sd, int level, int optname, void __user *optval, 4129 * int __user *optlen); 4130 * ret = setsockopt(int sd, int level, int optname, const void __user *optval, 4131 * int optlen); 4132 * 4133 * sd - the socket descript. 4134 * level - set to IPPROTO_SCTP for all SCTP options. 4135 * optname - the option name. 4136 * optval - the buffer to store the value of the option. 4137 * optlen - the size of the buffer. 4138 */ 4139 static int sctp_setsockopt(struct sock *sk, int level, int optname, 4140 char __user *optval, unsigned int optlen) 4141 { 4142 int retval = 0; 4143 4144 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); 4145 4146 /* I can hardly begin to describe how wrong this is. This is 4147 * so broken as to be worse than useless. The API draft 4148 * REALLY is NOT helpful here... I am not convinced that the 4149 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP 4150 * are at all well-founded. 4151 */ 4152 if (level != SOL_SCTP) { 4153 struct sctp_af *af = sctp_sk(sk)->pf->af; 4154 retval = af->setsockopt(sk, level, optname, optval, optlen); 4155 goto out_nounlock; 4156 } 4157 4158 lock_sock(sk); 4159 4160 switch (optname) { 4161 case SCTP_SOCKOPT_BINDX_ADD: 4162 /* 'optlen' is the size of the addresses buffer. */ 4163 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, 4164 optlen, SCTP_BINDX_ADD_ADDR); 4165 break; 4166 4167 case SCTP_SOCKOPT_BINDX_REM: 4168 /* 'optlen' is the size of the addresses buffer. */ 4169 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, 4170 optlen, SCTP_BINDX_REM_ADDR); 4171 break; 4172 4173 case SCTP_SOCKOPT_CONNECTX_OLD: 4174 /* 'optlen' is the size of the addresses buffer. */ 4175 retval = sctp_setsockopt_connectx_old(sk, 4176 (struct sockaddr __user *)optval, 4177 optlen); 4178 break; 4179 4180 case SCTP_SOCKOPT_CONNECTX: 4181 /* 'optlen' is the size of the addresses buffer. */ 4182 retval = sctp_setsockopt_connectx(sk, 4183 (struct sockaddr __user *)optval, 4184 optlen); 4185 break; 4186 4187 case SCTP_DISABLE_FRAGMENTS: 4188 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); 4189 break; 4190 4191 case SCTP_EVENTS: 4192 retval = sctp_setsockopt_events(sk, optval, optlen); 4193 break; 4194 4195 case SCTP_AUTOCLOSE: 4196 retval = sctp_setsockopt_autoclose(sk, optval, optlen); 4197 break; 4198 4199 case SCTP_PEER_ADDR_PARAMS: 4200 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); 4201 break; 4202 4203 case SCTP_DELAYED_SACK: 4204 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); 4205 break; 4206 case SCTP_PARTIAL_DELIVERY_POINT: 4207 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); 4208 break; 4209 4210 case SCTP_INITMSG: 4211 retval = sctp_setsockopt_initmsg(sk, optval, optlen); 4212 break; 4213 case SCTP_DEFAULT_SEND_PARAM: 4214 retval = sctp_setsockopt_default_send_param(sk, optval, 4215 optlen); 4216 break; 4217 case SCTP_DEFAULT_SNDINFO: 4218 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen); 4219 break; 4220 case SCTP_PRIMARY_ADDR: 4221 retval = sctp_setsockopt_primary_addr(sk, optval, optlen); 4222 break; 4223 case SCTP_SET_PEER_PRIMARY_ADDR: 4224 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); 4225 break; 4226 case SCTP_NODELAY: 4227 retval = sctp_setsockopt_nodelay(sk, optval, optlen); 4228 break; 4229 case SCTP_RTOINFO: 4230 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); 4231 break; 4232 case SCTP_ASSOCINFO: 4233 retval = sctp_setsockopt_associnfo(sk, optval, optlen); 4234 break; 4235 case SCTP_I_WANT_MAPPED_V4_ADDR: 4236 retval = sctp_setsockopt_mappedv4(sk, optval, optlen); 4237 break; 4238 case SCTP_MAXSEG: 4239 retval = sctp_setsockopt_maxseg(sk, optval, optlen); 4240 break; 4241 case SCTP_ADAPTATION_LAYER: 4242 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); 4243 break; 4244 case SCTP_CONTEXT: 4245 retval = sctp_setsockopt_context(sk, optval, optlen); 4246 break; 4247 case SCTP_FRAGMENT_INTERLEAVE: 4248 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); 4249 break; 4250 case SCTP_MAX_BURST: 4251 retval = sctp_setsockopt_maxburst(sk, optval, optlen); 4252 break; 4253 case SCTP_AUTH_CHUNK: 4254 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); 4255 break; 4256 case SCTP_HMAC_IDENT: 4257 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); 4258 break; 4259 case SCTP_AUTH_KEY: 4260 retval = sctp_setsockopt_auth_key(sk, optval, optlen); 4261 break; 4262 case SCTP_AUTH_ACTIVE_KEY: 4263 retval = sctp_setsockopt_active_key(sk, optval, optlen); 4264 break; 4265 case SCTP_AUTH_DELETE_KEY: 4266 retval = sctp_setsockopt_del_key(sk, optval, optlen); 4267 break; 4268 case SCTP_AUTH_DEACTIVATE_KEY: 4269 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen); 4270 break; 4271 case SCTP_AUTO_ASCONF: 4272 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen); 4273 break; 4274 case SCTP_PEER_ADDR_THLDS: 4275 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen); 4276 break; 4277 case SCTP_RECVRCVINFO: 4278 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen); 4279 break; 4280 case SCTP_RECVNXTINFO: 4281 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen); 4282 break; 4283 case SCTP_PR_SUPPORTED: 4284 retval = sctp_setsockopt_pr_supported(sk, optval, optlen); 4285 break; 4286 case SCTP_DEFAULT_PRINFO: 4287 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen); 4288 break; 4289 case SCTP_RECONFIG_SUPPORTED: 4290 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen); 4291 break; 4292 case SCTP_ENABLE_STREAM_RESET: 4293 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen); 4294 break; 4295 case SCTP_RESET_STREAMS: 4296 retval = sctp_setsockopt_reset_streams(sk, optval, optlen); 4297 break; 4298 case SCTP_RESET_ASSOC: 4299 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen); 4300 break; 4301 case SCTP_ADD_STREAMS: 4302 retval = sctp_setsockopt_add_streams(sk, optval, optlen); 4303 break; 4304 case SCTP_STREAM_SCHEDULER: 4305 retval = sctp_setsockopt_scheduler(sk, optval, optlen); 4306 break; 4307 case SCTP_STREAM_SCHEDULER_VALUE: 4308 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen); 4309 break; 4310 case SCTP_INTERLEAVING_SUPPORTED: 4311 retval = sctp_setsockopt_interleaving_supported(sk, optval, 4312 optlen); 4313 break; 4314 default: 4315 retval = -ENOPROTOOPT; 4316 break; 4317 } 4318 4319 release_sock(sk); 4320 4321 out_nounlock: 4322 return retval; 4323 } 4324 4325 /* API 3.1.6 connect() - UDP Style Syntax 4326 * 4327 * An application may use the connect() call in the UDP model to initiate an 4328 * association without sending data. 4329 * 4330 * The syntax is: 4331 * 4332 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); 4333 * 4334 * sd: the socket descriptor to have a new association added to. 4335 * 4336 * nam: the address structure (either struct sockaddr_in or struct 4337 * sockaddr_in6 defined in RFC2553 [7]). 4338 * 4339 * len: the size of the address. 4340 */ 4341 static int sctp_connect(struct sock *sk, struct sockaddr *addr, 4342 int addr_len) 4343 { 4344 int err = 0; 4345 struct sctp_af *af; 4346 4347 lock_sock(sk); 4348 4349 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk, 4350 addr, addr_len); 4351 4352 /* Validate addr_len before calling common connect/connectx routine. */ 4353 af = sctp_get_af_specific(addr->sa_family); 4354 if (!af || addr_len < af->sockaddr_len) { 4355 err = -EINVAL; 4356 } else { 4357 /* Pass correct addr len to common routine (so it knows there 4358 * is only one address being passed. 4359 */ 4360 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL); 4361 } 4362 4363 release_sock(sk); 4364 return err; 4365 } 4366 4367 /* FIXME: Write comments. */ 4368 static int sctp_disconnect(struct sock *sk, int flags) 4369 { 4370 return -EOPNOTSUPP; /* STUB */ 4371 } 4372 4373 /* 4.1.4 accept() - TCP Style Syntax 4374 * 4375 * Applications use accept() call to remove an established SCTP 4376 * association from the accept queue of the endpoint. A new socket 4377 * descriptor will be returned from accept() to represent the newly 4378 * formed association. 4379 */ 4380 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern) 4381 { 4382 struct sctp_sock *sp; 4383 struct sctp_endpoint *ep; 4384 struct sock *newsk = NULL; 4385 struct sctp_association *asoc; 4386 long timeo; 4387 int error = 0; 4388 4389 lock_sock(sk); 4390 4391 sp = sctp_sk(sk); 4392 ep = sp->ep; 4393 4394 if (!sctp_style(sk, TCP)) { 4395 error = -EOPNOTSUPP; 4396 goto out; 4397 } 4398 4399 if (!sctp_sstate(sk, LISTENING)) { 4400 error = -EINVAL; 4401 goto out; 4402 } 4403 4404 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 4405 4406 error = sctp_wait_for_accept(sk, timeo); 4407 if (error) 4408 goto out; 4409 4410 /* We treat the list of associations on the endpoint as the accept 4411 * queue and pick the first association on the list. 4412 */ 4413 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); 4414 4415 newsk = sp->pf->create_accept_sk(sk, asoc, kern); 4416 if (!newsk) { 4417 error = -ENOMEM; 4418 goto out; 4419 } 4420 4421 /* Populate the fields of the newsk from the oldsk and migrate the 4422 * asoc to the newsk. 4423 */ 4424 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); 4425 4426 out: 4427 release_sock(sk); 4428 *err = error; 4429 return newsk; 4430 } 4431 4432 /* The SCTP ioctl handler. */ 4433 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) 4434 { 4435 int rc = -ENOTCONN; 4436 4437 lock_sock(sk); 4438 4439 /* 4440 * SEQPACKET-style sockets in LISTENING state are valid, for 4441 * SCTP, so only discard TCP-style sockets in LISTENING state. 4442 */ 4443 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 4444 goto out; 4445 4446 switch (cmd) { 4447 case SIOCINQ: { 4448 struct sk_buff *skb; 4449 unsigned int amount = 0; 4450 4451 skb = skb_peek(&sk->sk_receive_queue); 4452 if (skb != NULL) { 4453 /* 4454 * We will only return the amount of this packet since 4455 * that is all that will be read. 4456 */ 4457 amount = skb->len; 4458 } 4459 rc = put_user(amount, (int __user *)arg); 4460 break; 4461 } 4462 default: 4463 rc = -ENOIOCTLCMD; 4464 break; 4465 } 4466 out: 4467 release_sock(sk); 4468 return rc; 4469 } 4470 4471 /* This is the function which gets called during socket creation to 4472 * initialized the SCTP-specific portion of the sock. 4473 * The sock structure should already be zero-filled memory. 4474 */ 4475 static int sctp_init_sock(struct sock *sk) 4476 { 4477 struct net *net = sock_net(sk); 4478 struct sctp_sock *sp; 4479 4480 pr_debug("%s: sk:%p\n", __func__, sk); 4481 4482 sp = sctp_sk(sk); 4483 4484 /* Initialize the SCTP per socket area. */ 4485 switch (sk->sk_type) { 4486 case SOCK_SEQPACKET: 4487 sp->type = SCTP_SOCKET_UDP; 4488 break; 4489 case SOCK_STREAM: 4490 sp->type = SCTP_SOCKET_TCP; 4491 break; 4492 default: 4493 return -ESOCKTNOSUPPORT; 4494 } 4495 4496 sk->sk_gso_type = SKB_GSO_SCTP; 4497 4498 /* Initialize default send parameters. These parameters can be 4499 * modified with the SCTP_DEFAULT_SEND_PARAM socket option. 4500 */ 4501 sp->default_stream = 0; 4502 sp->default_ppid = 0; 4503 sp->default_flags = 0; 4504 sp->default_context = 0; 4505 sp->default_timetolive = 0; 4506 4507 sp->default_rcv_context = 0; 4508 sp->max_burst = net->sctp.max_burst; 4509 4510 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg; 4511 4512 /* Initialize default setup parameters. These parameters 4513 * can be modified with the SCTP_INITMSG socket option or 4514 * overridden by the SCTP_INIT CMSG. 4515 */ 4516 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; 4517 sp->initmsg.sinit_max_instreams = sctp_max_instreams; 4518 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init; 4519 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; 4520 4521 /* Initialize default RTO related parameters. These parameters can 4522 * be modified for with the SCTP_RTOINFO socket option. 4523 */ 4524 sp->rtoinfo.srto_initial = net->sctp.rto_initial; 4525 sp->rtoinfo.srto_max = net->sctp.rto_max; 4526 sp->rtoinfo.srto_min = net->sctp.rto_min; 4527 4528 /* Initialize default association related parameters. These parameters 4529 * can be modified with the SCTP_ASSOCINFO socket option. 4530 */ 4531 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; 4532 sp->assocparams.sasoc_number_peer_destinations = 0; 4533 sp->assocparams.sasoc_peer_rwnd = 0; 4534 sp->assocparams.sasoc_local_rwnd = 0; 4535 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; 4536 4537 /* Initialize default event subscriptions. By default, all the 4538 * options are off. 4539 */ 4540 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); 4541 4542 /* Default Peer Address Parameters. These defaults can 4543 * be modified via SCTP_PEER_ADDR_PARAMS 4544 */ 4545 sp->hbinterval = net->sctp.hb_interval; 4546 sp->pathmaxrxt = net->sctp.max_retrans_path; 4547 sp->pathmtu = 0; /* allow default discovery */ 4548 sp->sackdelay = net->sctp.sack_timeout; 4549 sp->sackfreq = 2; 4550 sp->param_flags = SPP_HB_ENABLE | 4551 SPP_PMTUD_ENABLE | 4552 SPP_SACKDELAY_ENABLE; 4553 4554 /* If enabled no SCTP message fragmentation will be performed. 4555 * Configure through SCTP_DISABLE_FRAGMENTS socket option. 4556 */ 4557 sp->disable_fragments = 0; 4558 4559 /* Enable Nagle algorithm by default. */ 4560 sp->nodelay = 0; 4561 4562 sp->recvrcvinfo = 0; 4563 sp->recvnxtinfo = 0; 4564 4565 /* Enable by default. */ 4566 sp->v4mapped = 1; 4567 4568 /* Auto-close idle associations after the configured 4569 * number of seconds. A value of 0 disables this 4570 * feature. Configure through the SCTP_AUTOCLOSE socket option, 4571 * for UDP-style sockets only. 4572 */ 4573 sp->autoclose = 0; 4574 4575 /* User specified fragmentation limit. */ 4576 sp->user_frag = 0; 4577 4578 sp->adaptation_ind = 0; 4579 4580 sp->pf = sctp_get_pf_specific(sk->sk_family); 4581 4582 /* Control variables for partial data delivery. */ 4583 atomic_set(&sp->pd_mode, 0); 4584 skb_queue_head_init(&sp->pd_lobby); 4585 sp->frag_interleave = 0; 4586 4587 /* Create a per socket endpoint structure. Even if we 4588 * change the data structure relationships, this may still 4589 * be useful for storing pre-connect address information. 4590 */ 4591 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL); 4592 if (!sp->ep) 4593 return -ENOMEM; 4594 4595 sp->hmac = NULL; 4596 4597 sk->sk_destruct = sctp_destruct_sock; 4598 4599 SCTP_DBG_OBJCNT_INC(sock); 4600 4601 local_bh_disable(); 4602 sk_sockets_allocated_inc(sk); 4603 sock_prot_inuse_add(net, sk->sk_prot, 1); 4604 4605 /* Nothing can fail after this block, otherwise 4606 * sctp_destroy_sock() will be called without addr_wq_lock held 4607 */ 4608 if (net->sctp.default_auto_asconf) { 4609 spin_lock(&sock_net(sk)->sctp.addr_wq_lock); 4610 list_add_tail(&sp->auto_asconf_list, 4611 &net->sctp.auto_asconf_splist); 4612 sp->do_auto_asconf = 1; 4613 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); 4614 } else { 4615 sp->do_auto_asconf = 0; 4616 } 4617 4618 local_bh_enable(); 4619 4620 return 0; 4621 } 4622 4623 /* Cleanup any SCTP per socket resources. Must be called with 4624 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true 4625 */ 4626 static void sctp_destroy_sock(struct sock *sk) 4627 { 4628 struct sctp_sock *sp; 4629 4630 pr_debug("%s: sk:%p\n", __func__, sk); 4631 4632 /* Release our hold on the endpoint. */ 4633 sp = sctp_sk(sk); 4634 /* This could happen during socket init, thus we bail out 4635 * early, since the rest of the below is not setup either. 4636 */ 4637 if (sp->ep == NULL) 4638 return; 4639 4640 if (sp->do_auto_asconf) { 4641 sp->do_auto_asconf = 0; 4642 list_del(&sp->auto_asconf_list); 4643 } 4644 sctp_endpoint_free(sp->ep); 4645 local_bh_disable(); 4646 sk_sockets_allocated_dec(sk); 4647 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 4648 local_bh_enable(); 4649 } 4650 4651 /* Triggered when there are no references on the socket anymore */ 4652 static void sctp_destruct_sock(struct sock *sk) 4653 { 4654 struct sctp_sock *sp = sctp_sk(sk); 4655 4656 /* Free up the HMAC transform. */ 4657 crypto_free_shash(sp->hmac); 4658 4659 inet_sock_destruct(sk); 4660 } 4661 4662 /* API 4.1.7 shutdown() - TCP Style Syntax 4663 * int shutdown(int socket, int how); 4664 * 4665 * sd - the socket descriptor of the association to be closed. 4666 * how - Specifies the type of shutdown. The values are 4667 * as follows: 4668 * SHUT_RD 4669 * Disables further receive operations. No SCTP 4670 * protocol action is taken. 4671 * SHUT_WR 4672 * Disables further send operations, and initiates 4673 * the SCTP shutdown sequence. 4674 * SHUT_RDWR 4675 * Disables further send and receive operations 4676 * and initiates the SCTP shutdown sequence. 4677 */ 4678 static void sctp_shutdown(struct sock *sk, int how) 4679 { 4680 struct net *net = sock_net(sk); 4681 struct sctp_endpoint *ep; 4682 4683 if (!sctp_style(sk, TCP)) 4684 return; 4685 4686 ep = sctp_sk(sk)->ep; 4687 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) { 4688 struct sctp_association *asoc; 4689 4690 inet_sk_set_state(sk, SCTP_SS_CLOSING); 4691 asoc = list_entry(ep->asocs.next, 4692 struct sctp_association, asocs); 4693 sctp_primitive_SHUTDOWN(net, asoc, NULL); 4694 } 4695 } 4696 4697 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc, 4698 struct sctp_info *info) 4699 { 4700 struct sctp_transport *prim; 4701 struct list_head *pos; 4702 int mask; 4703 4704 memset(info, 0, sizeof(*info)); 4705 if (!asoc) { 4706 struct sctp_sock *sp = sctp_sk(sk); 4707 4708 info->sctpi_s_autoclose = sp->autoclose; 4709 info->sctpi_s_adaptation_ind = sp->adaptation_ind; 4710 info->sctpi_s_pd_point = sp->pd_point; 4711 info->sctpi_s_nodelay = sp->nodelay; 4712 info->sctpi_s_disable_fragments = sp->disable_fragments; 4713 info->sctpi_s_v4mapped = sp->v4mapped; 4714 info->sctpi_s_frag_interleave = sp->frag_interleave; 4715 info->sctpi_s_type = sp->type; 4716 4717 return 0; 4718 } 4719 4720 info->sctpi_tag = asoc->c.my_vtag; 4721 info->sctpi_state = asoc->state; 4722 info->sctpi_rwnd = asoc->a_rwnd; 4723 info->sctpi_unackdata = asoc->unack_data; 4724 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 4725 info->sctpi_instrms = asoc->stream.incnt; 4726 info->sctpi_outstrms = asoc->stream.outcnt; 4727 list_for_each(pos, &asoc->base.inqueue.in_chunk_list) 4728 info->sctpi_inqueue++; 4729 list_for_each(pos, &asoc->outqueue.out_chunk_list) 4730 info->sctpi_outqueue++; 4731 info->sctpi_overall_error = asoc->overall_error_count; 4732 info->sctpi_max_burst = asoc->max_burst; 4733 info->sctpi_maxseg = asoc->frag_point; 4734 info->sctpi_peer_rwnd = asoc->peer.rwnd; 4735 info->sctpi_peer_tag = asoc->c.peer_vtag; 4736 4737 mask = asoc->peer.ecn_capable << 1; 4738 mask = (mask | asoc->peer.ipv4_address) << 1; 4739 mask = (mask | asoc->peer.ipv6_address) << 1; 4740 mask = (mask | asoc->peer.hostname_address) << 1; 4741 mask = (mask | asoc->peer.asconf_capable) << 1; 4742 mask = (mask | asoc->peer.prsctp_capable) << 1; 4743 mask = (mask | asoc->peer.auth_capable); 4744 info->sctpi_peer_capable = mask; 4745 mask = asoc->peer.sack_needed << 1; 4746 mask = (mask | asoc->peer.sack_generation) << 1; 4747 mask = (mask | asoc->peer.zero_window_announced); 4748 info->sctpi_peer_sack = mask; 4749 4750 info->sctpi_isacks = asoc->stats.isacks; 4751 info->sctpi_osacks = asoc->stats.osacks; 4752 info->sctpi_opackets = asoc->stats.opackets; 4753 info->sctpi_ipackets = asoc->stats.ipackets; 4754 info->sctpi_rtxchunks = asoc->stats.rtxchunks; 4755 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns; 4756 info->sctpi_idupchunks = asoc->stats.idupchunks; 4757 info->sctpi_gapcnt = asoc->stats.gapcnt; 4758 info->sctpi_ouodchunks = asoc->stats.ouodchunks; 4759 info->sctpi_iuodchunks = asoc->stats.iuodchunks; 4760 info->sctpi_oodchunks = asoc->stats.oodchunks; 4761 info->sctpi_iodchunks = asoc->stats.iodchunks; 4762 info->sctpi_octrlchunks = asoc->stats.octrlchunks; 4763 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks; 4764 4765 prim = asoc->peer.primary_path; 4766 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr)); 4767 info->sctpi_p_state = prim->state; 4768 info->sctpi_p_cwnd = prim->cwnd; 4769 info->sctpi_p_srtt = prim->srtt; 4770 info->sctpi_p_rto = jiffies_to_msecs(prim->rto); 4771 info->sctpi_p_hbinterval = prim->hbinterval; 4772 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt; 4773 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay); 4774 info->sctpi_p_ssthresh = prim->ssthresh; 4775 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked; 4776 info->sctpi_p_flight_size = prim->flight_size; 4777 info->sctpi_p_error = prim->error_count; 4778 4779 return 0; 4780 } 4781 EXPORT_SYMBOL_GPL(sctp_get_sctp_info); 4782 4783 /* use callback to avoid exporting the core structure */ 4784 void sctp_transport_walk_start(struct rhashtable_iter *iter) 4785 { 4786 rhltable_walk_enter(&sctp_transport_hashtable, iter); 4787 4788 rhashtable_walk_start(iter); 4789 } 4790 4791 void sctp_transport_walk_stop(struct rhashtable_iter *iter) 4792 { 4793 rhashtable_walk_stop(iter); 4794 rhashtable_walk_exit(iter); 4795 } 4796 4797 struct sctp_transport *sctp_transport_get_next(struct net *net, 4798 struct rhashtable_iter *iter) 4799 { 4800 struct sctp_transport *t; 4801 4802 t = rhashtable_walk_next(iter); 4803 for (; t; t = rhashtable_walk_next(iter)) { 4804 if (IS_ERR(t)) { 4805 if (PTR_ERR(t) == -EAGAIN) 4806 continue; 4807 break; 4808 } 4809 4810 if (net_eq(sock_net(t->asoc->base.sk), net) && 4811 t->asoc->peer.primary_path == t) 4812 break; 4813 } 4814 4815 return t; 4816 } 4817 4818 struct sctp_transport *sctp_transport_get_idx(struct net *net, 4819 struct rhashtable_iter *iter, 4820 int pos) 4821 { 4822 void *obj = SEQ_START_TOKEN; 4823 4824 while (pos && (obj = sctp_transport_get_next(net, iter)) && 4825 !IS_ERR(obj)) 4826 pos--; 4827 4828 return obj; 4829 } 4830 4831 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), 4832 void *p) { 4833 int err = 0; 4834 int hash = 0; 4835 struct sctp_ep_common *epb; 4836 struct sctp_hashbucket *head; 4837 4838 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; 4839 hash++, head++) { 4840 read_lock_bh(&head->lock); 4841 sctp_for_each_hentry(epb, &head->chain) { 4842 err = cb(sctp_ep(epb), p); 4843 if (err) 4844 break; 4845 } 4846 read_unlock_bh(&head->lock); 4847 } 4848 4849 return err; 4850 } 4851 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); 4852 4853 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *), 4854 struct net *net, 4855 const union sctp_addr *laddr, 4856 const union sctp_addr *paddr, void *p) 4857 { 4858 struct sctp_transport *transport; 4859 int err; 4860 4861 rcu_read_lock(); 4862 transport = sctp_addrs_lookup_transport(net, laddr, paddr); 4863 rcu_read_unlock(); 4864 if (!transport) 4865 return -ENOENT; 4866 4867 err = cb(transport, p); 4868 sctp_transport_put(transport); 4869 4870 return err; 4871 } 4872 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); 4873 4874 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *), 4875 int (*cb_done)(struct sctp_transport *, void *), 4876 struct net *net, int *pos, void *p) { 4877 struct rhashtable_iter hti; 4878 struct sctp_transport *tsp; 4879 int ret; 4880 4881 again: 4882 ret = 0; 4883 sctp_transport_walk_start(&hti); 4884 4885 tsp = sctp_transport_get_idx(net, &hti, *pos + 1); 4886 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) { 4887 if (!sctp_transport_hold(tsp)) 4888 continue; 4889 ret = cb(tsp, p); 4890 if (ret) 4891 break; 4892 (*pos)++; 4893 sctp_transport_put(tsp); 4894 } 4895 sctp_transport_walk_stop(&hti); 4896 4897 if (ret) { 4898 if (cb_done && !cb_done(tsp, p)) { 4899 (*pos)++; 4900 sctp_transport_put(tsp); 4901 goto again; 4902 } 4903 sctp_transport_put(tsp); 4904 } 4905 4906 return ret; 4907 } 4908 EXPORT_SYMBOL_GPL(sctp_for_each_transport); 4909 4910 /* 7.2.1 Association Status (SCTP_STATUS) 4911 4912 * Applications can retrieve current status information about an 4913 * association, including association state, peer receiver window size, 4914 * number of unacked data chunks, and number of data chunks pending 4915 * receipt. This information is read-only. 4916 */ 4917 static int sctp_getsockopt_sctp_status(struct sock *sk, int len, 4918 char __user *optval, 4919 int __user *optlen) 4920 { 4921 struct sctp_status status; 4922 struct sctp_association *asoc = NULL; 4923 struct sctp_transport *transport; 4924 sctp_assoc_t associd; 4925 int retval = 0; 4926 4927 if (len < sizeof(status)) { 4928 retval = -EINVAL; 4929 goto out; 4930 } 4931 4932 len = sizeof(status); 4933 if (copy_from_user(&status, optval, len)) { 4934 retval = -EFAULT; 4935 goto out; 4936 } 4937 4938 associd = status.sstat_assoc_id; 4939 asoc = sctp_id2assoc(sk, associd); 4940 if (!asoc) { 4941 retval = -EINVAL; 4942 goto out; 4943 } 4944 4945 transport = asoc->peer.primary_path; 4946 4947 status.sstat_assoc_id = sctp_assoc2id(asoc); 4948 status.sstat_state = sctp_assoc_to_state(asoc); 4949 status.sstat_rwnd = asoc->peer.rwnd; 4950 status.sstat_unackdata = asoc->unack_data; 4951 4952 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 4953 status.sstat_instrms = asoc->stream.incnt; 4954 status.sstat_outstrms = asoc->stream.outcnt; 4955 status.sstat_fragmentation_point = asoc->frag_point; 4956 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 4957 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, 4958 transport->af_specific->sockaddr_len); 4959 /* Map ipv4 address into v4-mapped-on-v6 address. */ 4960 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), 4961 (union sctp_addr *)&status.sstat_primary.spinfo_address); 4962 status.sstat_primary.spinfo_state = transport->state; 4963 status.sstat_primary.spinfo_cwnd = transport->cwnd; 4964 status.sstat_primary.spinfo_srtt = transport->srtt; 4965 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); 4966 status.sstat_primary.spinfo_mtu = transport->pathmtu; 4967 4968 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) 4969 status.sstat_primary.spinfo_state = SCTP_ACTIVE; 4970 4971 if (put_user(len, optlen)) { 4972 retval = -EFAULT; 4973 goto out; 4974 } 4975 4976 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", 4977 __func__, len, status.sstat_state, status.sstat_rwnd, 4978 status.sstat_assoc_id); 4979 4980 if (copy_to_user(optval, &status, len)) { 4981 retval = -EFAULT; 4982 goto out; 4983 } 4984 4985 out: 4986 return retval; 4987 } 4988 4989 4990 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 4991 * 4992 * Applications can retrieve information about a specific peer address 4993 * of an association, including its reachability state, congestion 4994 * window, and retransmission timer values. This information is 4995 * read-only. 4996 */ 4997 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, 4998 char __user *optval, 4999 int __user *optlen) 5000 { 5001 struct sctp_paddrinfo pinfo; 5002 struct sctp_transport *transport; 5003 int retval = 0; 5004 5005 if (len < sizeof(pinfo)) { 5006 retval = -EINVAL; 5007 goto out; 5008 } 5009 5010 len = sizeof(pinfo); 5011 if (copy_from_user(&pinfo, optval, len)) { 5012 retval = -EFAULT; 5013 goto out; 5014 } 5015 5016 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, 5017 pinfo.spinfo_assoc_id); 5018 if (!transport) 5019 return -EINVAL; 5020 5021 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 5022 pinfo.spinfo_state = transport->state; 5023 pinfo.spinfo_cwnd = transport->cwnd; 5024 pinfo.spinfo_srtt = transport->srtt; 5025 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); 5026 pinfo.spinfo_mtu = transport->pathmtu; 5027 5028 if (pinfo.spinfo_state == SCTP_UNKNOWN) 5029 pinfo.spinfo_state = SCTP_ACTIVE; 5030 5031 if (put_user(len, optlen)) { 5032 retval = -EFAULT; 5033 goto out; 5034 } 5035 5036 if (copy_to_user(optval, &pinfo, len)) { 5037 retval = -EFAULT; 5038 goto out; 5039 } 5040 5041 out: 5042 return retval; 5043 } 5044 5045 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 5046 * 5047 * This option is a on/off flag. If enabled no SCTP message 5048 * fragmentation will be performed. Instead if a message being sent 5049 * exceeds the current PMTU size, the message will NOT be sent and 5050 * instead a error will be indicated to the user. 5051 */ 5052 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, 5053 char __user *optval, int __user *optlen) 5054 { 5055 int val; 5056 5057 if (len < sizeof(int)) 5058 return -EINVAL; 5059 5060 len = sizeof(int); 5061 val = (sctp_sk(sk)->disable_fragments == 1); 5062 if (put_user(len, optlen)) 5063 return -EFAULT; 5064 if (copy_to_user(optval, &val, len)) 5065 return -EFAULT; 5066 return 0; 5067 } 5068 5069 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) 5070 * 5071 * This socket option is used to specify various notifications and 5072 * ancillary data the user wishes to receive. 5073 */ 5074 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, 5075 int __user *optlen) 5076 { 5077 if (len == 0) 5078 return -EINVAL; 5079 if (len > sizeof(struct sctp_event_subscribe)) 5080 len = sizeof(struct sctp_event_subscribe); 5081 if (put_user(len, optlen)) 5082 return -EFAULT; 5083 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) 5084 return -EFAULT; 5085 return 0; 5086 } 5087 5088 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 5089 * 5090 * This socket option is applicable to the UDP-style socket only. When 5091 * set it will cause associations that are idle for more than the 5092 * specified number of seconds to automatically close. An association 5093 * being idle is defined an association that has NOT sent or received 5094 * user data. The special value of '0' indicates that no automatic 5095 * close of any associations should be performed. The option expects an 5096 * integer defining the number of seconds of idle time before an 5097 * association is closed. 5098 */ 5099 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) 5100 { 5101 /* Applicable to UDP-style socket only */ 5102 if (sctp_style(sk, TCP)) 5103 return -EOPNOTSUPP; 5104 if (len < sizeof(int)) 5105 return -EINVAL; 5106 len = sizeof(int); 5107 if (put_user(len, optlen)) 5108 return -EFAULT; 5109 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval)) 5110 return -EFAULT; 5111 return 0; 5112 } 5113 5114 /* Helper routine to branch off an association to a new socket. */ 5115 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp) 5116 { 5117 struct sctp_association *asoc = sctp_id2assoc(sk, id); 5118 struct sctp_sock *sp = sctp_sk(sk); 5119 struct socket *sock; 5120 int err = 0; 5121 5122 /* Do not peel off from one netns to another one. */ 5123 if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) 5124 return -EINVAL; 5125 5126 if (!asoc) 5127 return -EINVAL; 5128 5129 /* An association cannot be branched off from an already peeled-off 5130 * socket, nor is this supported for tcp style sockets. 5131 */ 5132 if (!sctp_style(sk, UDP)) 5133 return -EINVAL; 5134 5135 /* Create a new socket. */ 5136 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); 5137 if (err < 0) 5138 return err; 5139 5140 sctp_copy_sock(sock->sk, sk, asoc); 5141 5142 /* Make peeled-off sockets more like 1-1 accepted sockets. 5143 * Set the daddr and initialize id to something more random 5144 */ 5145 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk); 5146 5147 /* Populate the fields of the newsk from the oldsk and migrate the 5148 * asoc to the newsk. 5149 */ 5150 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); 5151 5152 *sockp = sock; 5153 5154 return err; 5155 } 5156 EXPORT_SYMBOL(sctp_do_peeloff); 5157 5158 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, 5159 struct file **newfile, unsigned flags) 5160 { 5161 struct socket *newsock; 5162 int retval; 5163 5164 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); 5165 if (retval < 0) 5166 goto out; 5167 5168 /* Map the socket to an unused fd that can be returned to the user. */ 5169 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); 5170 if (retval < 0) { 5171 sock_release(newsock); 5172 goto out; 5173 } 5174 5175 *newfile = sock_alloc_file(newsock, 0, NULL); 5176 if (IS_ERR(*newfile)) { 5177 put_unused_fd(retval); 5178 retval = PTR_ERR(*newfile); 5179 *newfile = NULL; 5180 return retval; 5181 } 5182 5183 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, 5184 retval); 5185 5186 peeloff->sd = retval; 5187 5188 if (flags & SOCK_NONBLOCK) 5189 (*newfile)->f_flags |= O_NONBLOCK; 5190 out: 5191 return retval; 5192 } 5193 5194 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) 5195 { 5196 sctp_peeloff_arg_t peeloff; 5197 struct file *newfile = NULL; 5198 int retval = 0; 5199 5200 if (len < sizeof(sctp_peeloff_arg_t)) 5201 return -EINVAL; 5202 len = sizeof(sctp_peeloff_arg_t); 5203 if (copy_from_user(&peeloff, optval, len)) 5204 return -EFAULT; 5205 5206 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); 5207 if (retval < 0) 5208 goto out; 5209 5210 /* Return the fd mapped to the new socket. */ 5211 if (put_user(len, optlen)) { 5212 fput(newfile); 5213 put_unused_fd(retval); 5214 return -EFAULT; 5215 } 5216 5217 if (copy_to_user(optval, &peeloff, len)) { 5218 fput(newfile); 5219 put_unused_fd(retval); 5220 return -EFAULT; 5221 } 5222 fd_install(retval, newfile); 5223 out: 5224 return retval; 5225 } 5226 5227 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, 5228 char __user *optval, int __user *optlen) 5229 { 5230 sctp_peeloff_flags_arg_t peeloff; 5231 struct file *newfile = NULL; 5232 int retval = 0; 5233 5234 if (len < sizeof(sctp_peeloff_flags_arg_t)) 5235 return -EINVAL; 5236 len = sizeof(sctp_peeloff_flags_arg_t); 5237 if (copy_from_user(&peeloff, optval, len)) 5238 return -EFAULT; 5239 5240 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, 5241 &newfile, peeloff.flags); 5242 if (retval < 0) 5243 goto out; 5244 5245 /* Return the fd mapped to the new socket. */ 5246 if (put_user(len, optlen)) { 5247 fput(newfile); 5248 put_unused_fd(retval); 5249 return -EFAULT; 5250 } 5251 5252 if (copy_to_user(optval, &peeloff, len)) { 5253 fput(newfile); 5254 put_unused_fd(retval); 5255 return -EFAULT; 5256 } 5257 fd_install(retval, newfile); 5258 out: 5259 return retval; 5260 } 5261 5262 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 5263 * 5264 * Applications can enable or disable heartbeats for any peer address of 5265 * an association, modify an address's heartbeat interval, force a 5266 * heartbeat to be sent immediately, and adjust the address's maximum 5267 * number of retransmissions sent before an address is considered 5268 * unreachable. The following structure is used to access and modify an 5269 * address's parameters: 5270 * 5271 * struct sctp_paddrparams { 5272 * sctp_assoc_t spp_assoc_id; 5273 * struct sockaddr_storage spp_address; 5274 * uint32_t spp_hbinterval; 5275 * uint16_t spp_pathmaxrxt; 5276 * uint32_t spp_pathmtu; 5277 * uint32_t spp_sackdelay; 5278 * uint32_t spp_flags; 5279 * }; 5280 * 5281 * spp_assoc_id - (one-to-many style socket) This is filled in the 5282 * application, and identifies the association for 5283 * this query. 5284 * spp_address - This specifies which address is of interest. 5285 * spp_hbinterval - This contains the value of the heartbeat interval, 5286 * in milliseconds. If a value of zero 5287 * is present in this field then no changes are to 5288 * be made to this parameter. 5289 * spp_pathmaxrxt - This contains the maximum number of 5290 * retransmissions before this address shall be 5291 * considered unreachable. If a value of zero 5292 * is present in this field then no changes are to 5293 * be made to this parameter. 5294 * spp_pathmtu - When Path MTU discovery is disabled the value 5295 * specified here will be the "fixed" path mtu. 5296 * Note that if the spp_address field is empty 5297 * then all associations on this address will 5298 * have this fixed path mtu set upon them. 5299 * 5300 * spp_sackdelay - When delayed sack is enabled, this value specifies 5301 * the number of milliseconds that sacks will be delayed 5302 * for. This value will apply to all addresses of an 5303 * association if the spp_address field is empty. Note 5304 * also, that if delayed sack is enabled and this 5305 * value is set to 0, no change is made to the last 5306 * recorded delayed sack timer value. 5307 * 5308 * spp_flags - These flags are used to control various features 5309 * on an association. The flag field may contain 5310 * zero or more of the following options. 5311 * 5312 * SPP_HB_ENABLE - Enable heartbeats on the 5313 * specified address. Note that if the address 5314 * field is empty all addresses for the association 5315 * have heartbeats enabled upon them. 5316 * 5317 * SPP_HB_DISABLE - Disable heartbeats on the 5318 * speicifed address. Note that if the address 5319 * field is empty all addresses for the association 5320 * will have their heartbeats disabled. Note also 5321 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 5322 * mutually exclusive, only one of these two should 5323 * be specified. Enabling both fields will have 5324 * undetermined results. 5325 * 5326 * SPP_HB_DEMAND - Request a user initiated heartbeat 5327 * to be made immediately. 5328 * 5329 * SPP_PMTUD_ENABLE - This field will enable PMTU 5330 * discovery upon the specified address. Note that 5331 * if the address feild is empty then all addresses 5332 * on the association are effected. 5333 * 5334 * SPP_PMTUD_DISABLE - This field will disable PMTU 5335 * discovery upon the specified address. Note that 5336 * if the address feild is empty then all addresses 5337 * on the association are effected. Not also that 5338 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 5339 * exclusive. Enabling both will have undetermined 5340 * results. 5341 * 5342 * SPP_SACKDELAY_ENABLE - Setting this flag turns 5343 * on delayed sack. The time specified in spp_sackdelay 5344 * is used to specify the sack delay for this address. Note 5345 * that if spp_address is empty then all addresses will 5346 * enable delayed sack and take on the sack delay 5347 * value specified in spp_sackdelay. 5348 * SPP_SACKDELAY_DISABLE - Setting this flag turns 5349 * off delayed sack. If the spp_address field is blank then 5350 * delayed sack is disabled for the entire association. Note 5351 * also that this field is mutually exclusive to 5352 * SPP_SACKDELAY_ENABLE, setting both will have undefined 5353 * results. 5354 */ 5355 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, 5356 char __user *optval, int __user *optlen) 5357 { 5358 struct sctp_paddrparams params; 5359 struct sctp_transport *trans = NULL; 5360 struct sctp_association *asoc = NULL; 5361 struct sctp_sock *sp = sctp_sk(sk); 5362 5363 if (len < sizeof(struct sctp_paddrparams)) 5364 return -EINVAL; 5365 len = sizeof(struct sctp_paddrparams); 5366 if (copy_from_user(¶ms, optval, len)) 5367 return -EFAULT; 5368 5369 /* If an address other than INADDR_ANY is specified, and 5370 * no transport is found, then the request is invalid. 5371 */ 5372 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { 5373 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 5374 params.spp_assoc_id); 5375 if (!trans) { 5376 pr_debug("%s: failed no transport\n", __func__); 5377 return -EINVAL; 5378 } 5379 } 5380 5381 /* Get association, if assoc_id != 0 and the socket is a one 5382 * to many style socket, and an association was not found, then 5383 * the id was invalid. 5384 */ 5385 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 5386 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { 5387 pr_debug("%s: failed no association\n", __func__); 5388 return -EINVAL; 5389 } 5390 5391 if (trans) { 5392 /* Fetch transport values. */ 5393 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); 5394 params.spp_pathmtu = trans->pathmtu; 5395 params.spp_pathmaxrxt = trans->pathmaxrxt; 5396 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); 5397 5398 /*draft-11 doesn't say what to return in spp_flags*/ 5399 params.spp_flags = trans->param_flags; 5400 } else if (asoc) { 5401 /* Fetch association values. */ 5402 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); 5403 params.spp_pathmtu = asoc->pathmtu; 5404 params.spp_pathmaxrxt = asoc->pathmaxrxt; 5405 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); 5406 5407 /*draft-11 doesn't say what to return in spp_flags*/ 5408 params.spp_flags = asoc->param_flags; 5409 } else { 5410 /* Fetch socket values. */ 5411 params.spp_hbinterval = sp->hbinterval; 5412 params.spp_pathmtu = sp->pathmtu; 5413 params.spp_sackdelay = sp->sackdelay; 5414 params.spp_pathmaxrxt = sp->pathmaxrxt; 5415 5416 /*draft-11 doesn't say what to return in spp_flags*/ 5417 params.spp_flags = sp->param_flags; 5418 } 5419 5420 if (copy_to_user(optval, ¶ms, len)) 5421 return -EFAULT; 5422 5423 if (put_user(len, optlen)) 5424 return -EFAULT; 5425 5426 return 0; 5427 } 5428 5429 /* 5430 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) 5431 * 5432 * This option will effect the way delayed acks are performed. This 5433 * option allows you to get or set the delayed ack time, in 5434 * milliseconds. It also allows changing the delayed ack frequency. 5435 * Changing the frequency to 1 disables the delayed sack algorithm. If 5436 * the assoc_id is 0, then this sets or gets the endpoints default 5437 * values. If the assoc_id field is non-zero, then the set or get 5438 * effects the specified association for the one to many model (the 5439 * assoc_id field is ignored by the one to one model). Note that if 5440 * sack_delay or sack_freq are 0 when setting this option, then the 5441 * current values will remain unchanged. 5442 * 5443 * struct sctp_sack_info { 5444 * sctp_assoc_t sack_assoc_id; 5445 * uint32_t sack_delay; 5446 * uint32_t sack_freq; 5447 * }; 5448 * 5449 * sack_assoc_id - This parameter, indicates which association the user 5450 * is performing an action upon. Note that if this field's value is 5451 * zero then the endpoints default value is changed (effecting future 5452 * associations only). 5453 * 5454 * sack_delay - This parameter contains the number of milliseconds that 5455 * the user is requesting the delayed ACK timer be set to. Note that 5456 * this value is defined in the standard to be between 200 and 500 5457 * milliseconds. 5458 * 5459 * sack_freq - This parameter contains the number of packets that must 5460 * be received before a sack is sent without waiting for the delay 5461 * timer to expire. The default value for this is 2, setting this 5462 * value to 1 will disable the delayed sack algorithm. 5463 */ 5464 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, 5465 char __user *optval, 5466 int __user *optlen) 5467 { 5468 struct sctp_sack_info params; 5469 struct sctp_association *asoc = NULL; 5470 struct sctp_sock *sp = sctp_sk(sk); 5471 5472 if (len >= sizeof(struct sctp_sack_info)) { 5473 len = sizeof(struct sctp_sack_info); 5474 5475 if (copy_from_user(¶ms, optval, len)) 5476 return -EFAULT; 5477 } else if (len == sizeof(struct sctp_assoc_value)) { 5478 pr_warn_ratelimited(DEPRECATED 5479 "%s (pid %d) " 5480 "Use of struct sctp_assoc_value in delayed_ack socket option.\n" 5481 "Use struct sctp_sack_info instead\n", 5482 current->comm, task_pid_nr(current)); 5483 if (copy_from_user(¶ms, optval, len)) 5484 return -EFAULT; 5485 } else 5486 return -EINVAL; 5487 5488 /* Get association, if sack_assoc_id != 0 and the socket is a one 5489 * to many style socket, and an association was not found, then 5490 * the id was invalid. 5491 */ 5492 asoc = sctp_id2assoc(sk, params.sack_assoc_id); 5493 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) 5494 return -EINVAL; 5495 5496 if (asoc) { 5497 /* Fetch association values. */ 5498 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { 5499 params.sack_delay = jiffies_to_msecs( 5500 asoc->sackdelay); 5501 params.sack_freq = asoc->sackfreq; 5502 5503 } else { 5504 params.sack_delay = 0; 5505 params.sack_freq = 1; 5506 } 5507 } else { 5508 /* Fetch socket values. */ 5509 if (sp->param_flags & SPP_SACKDELAY_ENABLE) { 5510 params.sack_delay = sp->sackdelay; 5511 params.sack_freq = sp->sackfreq; 5512 } else { 5513 params.sack_delay = 0; 5514 params.sack_freq = 1; 5515 } 5516 } 5517 5518 if (copy_to_user(optval, ¶ms, len)) 5519 return -EFAULT; 5520 5521 if (put_user(len, optlen)) 5522 return -EFAULT; 5523 5524 return 0; 5525 } 5526 5527 /* 7.1.3 Initialization Parameters (SCTP_INITMSG) 5528 * 5529 * Applications can specify protocol parameters for the default association 5530 * initialization. The option name argument to setsockopt() and getsockopt() 5531 * is SCTP_INITMSG. 5532 * 5533 * Setting initialization parameters is effective only on an unconnected 5534 * socket (for UDP-style sockets only future associations are effected 5535 * by the change). With TCP-style sockets, this option is inherited by 5536 * sockets derived from a listener socket. 5537 */ 5538 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) 5539 { 5540 if (len < sizeof(struct sctp_initmsg)) 5541 return -EINVAL; 5542 len = sizeof(struct sctp_initmsg); 5543 if (put_user(len, optlen)) 5544 return -EFAULT; 5545 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) 5546 return -EFAULT; 5547 return 0; 5548 } 5549 5550 5551 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, 5552 char __user *optval, int __user *optlen) 5553 { 5554 struct sctp_association *asoc; 5555 int cnt = 0; 5556 struct sctp_getaddrs getaddrs; 5557 struct sctp_transport *from; 5558 void __user *to; 5559 union sctp_addr temp; 5560 struct sctp_sock *sp = sctp_sk(sk); 5561 int addrlen; 5562 size_t space_left; 5563 int bytes_copied; 5564 5565 if (len < sizeof(struct sctp_getaddrs)) 5566 return -EINVAL; 5567 5568 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 5569 return -EFAULT; 5570 5571 /* For UDP-style sockets, id specifies the association to query. */ 5572 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 5573 if (!asoc) 5574 return -EINVAL; 5575 5576 to = optval + offsetof(struct sctp_getaddrs, addrs); 5577 space_left = len - offsetof(struct sctp_getaddrs, addrs); 5578 5579 list_for_each_entry(from, &asoc->peer.transport_addr_list, 5580 transports) { 5581 memcpy(&temp, &from->ipaddr, sizeof(temp)); 5582 addrlen = sctp_get_pf_specific(sk->sk_family) 5583 ->addr_to_user(sp, &temp); 5584 if (space_left < addrlen) 5585 return -ENOMEM; 5586 if (copy_to_user(to, &temp, addrlen)) 5587 return -EFAULT; 5588 to += addrlen; 5589 cnt++; 5590 space_left -= addrlen; 5591 } 5592 5593 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) 5594 return -EFAULT; 5595 bytes_copied = ((char __user *)to) - optval; 5596 if (put_user(bytes_copied, optlen)) 5597 return -EFAULT; 5598 5599 return 0; 5600 } 5601 5602 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, 5603 size_t space_left, int *bytes_copied) 5604 { 5605 struct sctp_sockaddr_entry *addr; 5606 union sctp_addr temp; 5607 int cnt = 0; 5608 int addrlen; 5609 struct net *net = sock_net(sk); 5610 5611 rcu_read_lock(); 5612 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { 5613 if (!addr->valid) 5614 continue; 5615 5616 if ((PF_INET == sk->sk_family) && 5617 (AF_INET6 == addr->a.sa.sa_family)) 5618 continue; 5619 if ((PF_INET6 == sk->sk_family) && 5620 inet_v6_ipv6only(sk) && 5621 (AF_INET == addr->a.sa.sa_family)) 5622 continue; 5623 memcpy(&temp, &addr->a, sizeof(temp)); 5624 if (!temp.v4.sin_port) 5625 temp.v4.sin_port = htons(port); 5626 5627 addrlen = sctp_get_pf_specific(sk->sk_family) 5628 ->addr_to_user(sctp_sk(sk), &temp); 5629 5630 if (space_left < addrlen) { 5631 cnt = -ENOMEM; 5632 break; 5633 } 5634 memcpy(to, &temp, addrlen); 5635 5636 to += addrlen; 5637 cnt++; 5638 space_left -= addrlen; 5639 *bytes_copied += addrlen; 5640 } 5641 rcu_read_unlock(); 5642 5643 return cnt; 5644 } 5645 5646 5647 static int sctp_getsockopt_local_addrs(struct sock *sk, int len, 5648 char __user *optval, int __user *optlen) 5649 { 5650 struct sctp_bind_addr *bp; 5651 struct sctp_association *asoc; 5652 int cnt = 0; 5653 struct sctp_getaddrs getaddrs; 5654 struct sctp_sockaddr_entry *addr; 5655 void __user *to; 5656 union sctp_addr temp; 5657 struct sctp_sock *sp = sctp_sk(sk); 5658 int addrlen; 5659 int err = 0; 5660 size_t space_left; 5661 int bytes_copied = 0; 5662 void *addrs; 5663 void *buf; 5664 5665 if (len < sizeof(struct sctp_getaddrs)) 5666 return -EINVAL; 5667 5668 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 5669 return -EFAULT; 5670 5671 /* 5672 * For UDP-style sockets, id specifies the association to query. 5673 * If the id field is set to the value '0' then the locally bound 5674 * addresses are returned without regard to any particular 5675 * association. 5676 */ 5677 if (0 == getaddrs.assoc_id) { 5678 bp = &sctp_sk(sk)->ep->base.bind_addr; 5679 } else { 5680 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 5681 if (!asoc) 5682 return -EINVAL; 5683 bp = &asoc->base.bind_addr; 5684 } 5685 5686 to = optval + offsetof(struct sctp_getaddrs, addrs); 5687 space_left = len - offsetof(struct sctp_getaddrs, addrs); 5688 5689 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); 5690 if (!addrs) 5691 return -ENOMEM; 5692 5693 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid 5694 * addresses from the global local address list. 5695 */ 5696 if (sctp_list_single_entry(&bp->address_list)) { 5697 addr = list_entry(bp->address_list.next, 5698 struct sctp_sockaddr_entry, list); 5699 if (sctp_is_any(sk, &addr->a)) { 5700 cnt = sctp_copy_laddrs(sk, bp->port, addrs, 5701 space_left, &bytes_copied); 5702 if (cnt < 0) { 5703 err = cnt; 5704 goto out; 5705 } 5706 goto copy_getaddrs; 5707 } 5708 } 5709 5710 buf = addrs; 5711 /* Protection on the bound address list is not needed since 5712 * in the socket option context we hold a socket lock and 5713 * thus the bound address list can't change. 5714 */ 5715 list_for_each_entry(addr, &bp->address_list, list) { 5716 memcpy(&temp, &addr->a, sizeof(temp)); 5717 addrlen = sctp_get_pf_specific(sk->sk_family) 5718 ->addr_to_user(sp, &temp); 5719 if (space_left < addrlen) { 5720 err = -ENOMEM; /*fixme: right error?*/ 5721 goto out; 5722 } 5723 memcpy(buf, &temp, addrlen); 5724 buf += addrlen; 5725 bytes_copied += addrlen; 5726 cnt++; 5727 space_left -= addrlen; 5728 } 5729 5730 copy_getaddrs: 5731 if (copy_to_user(to, addrs, bytes_copied)) { 5732 err = -EFAULT; 5733 goto out; 5734 } 5735 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { 5736 err = -EFAULT; 5737 goto out; 5738 } 5739 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too, 5740 * but we can't change it anymore. 5741 */ 5742 if (put_user(bytes_copied, optlen)) 5743 err = -EFAULT; 5744 out: 5745 kfree(addrs); 5746 return err; 5747 } 5748 5749 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 5750 * 5751 * Requests that the local SCTP stack use the enclosed peer address as 5752 * the association primary. The enclosed address must be one of the 5753 * association peer's addresses. 5754 */ 5755 static int sctp_getsockopt_primary_addr(struct sock *sk, int len, 5756 char __user *optval, int __user *optlen) 5757 { 5758 struct sctp_prim prim; 5759 struct sctp_association *asoc; 5760 struct sctp_sock *sp = sctp_sk(sk); 5761 5762 if (len < sizeof(struct sctp_prim)) 5763 return -EINVAL; 5764 5765 len = sizeof(struct sctp_prim); 5766 5767 if (copy_from_user(&prim, optval, len)) 5768 return -EFAULT; 5769 5770 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); 5771 if (!asoc) 5772 return -EINVAL; 5773 5774 if (!asoc->peer.primary_path) 5775 return -ENOTCONN; 5776 5777 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, 5778 asoc->peer.primary_path->af_specific->sockaddr_len); 5779 5780 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, 5781 (union sctp_addr *)&prim.ssp_addr); 5782 5783 if (put_user(len, optlen)) 5784 return -EFAULT; 5785 if (copy_to_user(optval, &prim, len)) 5786 return -EFAULT; 5787 5788 return 0; 5789 } 5790 5791 /* 5792 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) 5793 * 5794 * Requests that the local endpoint set the specified Adaptation Layer 5795 * Indication parameter for all future INIT and INIT-ACK exchanges. 5796 */ 5797 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, 5798 char __user *optval, int __user *optlen) 5799 { 5800 struct sctp_setadaptation adaptation; 5801 5802 if (len < sizeof(struct sctp_setadaptation)) 5803 return -EINVAL; 5804 5805 len = sizeof(struct sctp_setadaptation); 5806 5807 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; 5808 5809 if (put_user(len, optlen)) 5810 return -EFAULT; 5811 if (copy_to_user(optval, &adaptation, len)) 5812 return -EFAULT; 5813 5814 return 0; 5815 } 5816 5817 /* 5818 * 5819 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 5820 * 5821 * Applications that wish to use the sendto() system call may wish to 5822 * specify a default set of parameters that would normally be supplied 5823 * through the inclusion of ancillary data. This socket option allows 5824 * such an application to set the default sctp_sndrcvinfo structure. 5825 5826 5827 * The application that wishes to use this socket option simply passes 5828 * in to this call the sctp_sndrcvinfo structure defined in Section 5829 * 5.2.2) The input parameters accepted by this call include 5830 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 5831 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 5832 * to this call if the caller is using the UDP model. 5833 * 5834 * For getsockopt, it get the default sctp_sndrcvinfo structure. 5835 */ 5836 static int sctp_getsockopt_default_send_param(struct sock *sk, 5837 int len, char __user *optval, 5838 int __user *optlen) 5839 { 5840 struct sctp_sock *sp = sctp_sk(sk); 5841 struct sctp_association *asoc; 5842 struct sctp_sndrcvinfo info; 5843 5844 if (len < sizeof(info)) 5845 return -EINVAL; 5846 5847 len = sizeof(info); 5848 5849 if (copy_from_user(&info, optval, len)) 5850 return -EFAULT; 5851 5852 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 5853 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) 5854 return -EINVAL; 5855 if (asoc) { 5856 info.sinfo_stream = asoc->default_stream; 5857 info.sinfo_flags = asoc->default_flags; 5858 info.sinfo_ppid = asoc->default_ppid; 5859 info.sinfo_context = asoc->default_context; 5860 info.sinfo_timetolive = asoc->default_timetolive; 5861 } else { 5862 info.sinfo_stream = sp->default_stream; 5863 info.sinfo_flags = sp->default_flags; 5864 info.sinfo_ppid = sp->default_ppid; 5865 info.sinfo_context = sp->default_context; 5866 info.sinfo_timetolive = sp->default_timetolive; 5867 } 5868 5869 if (put_user(len, optlen)) 5870 return -EFAULT; 5871 if (copy_to_user(optval, &info, len)) 5872 return -EFAULT; 5873 5874 return 0; 5875 } 5876 5877 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters 5878 * (SCTP_DEFAULT_SNDINFO) 5879 */ 5880 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, 5881 char __user *optval, 5882 int __user *optlen) 5883 { 5884 struct sctp_sock *sp = sctp_sk(sk); 5885 struct sctp_association *asoc; 5886 struct sctp_sndinfo info; 5887 5888 if (len < sizeof(info)) 5889 return -EINVAL; 5890 5891 len = sizeof(info); 5892 5893 if (copy_from_user(&info, optval, len)) 5894 return -EFAULT; 5895 5896 asoc = sctp_id2assoc(sk, info.snd_assoc_id); 5897 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) 5898 return -EINVAL; 5899 if (asoc) { 5900 info.snd_sid = asoc->default_stream; 5901 info.snd_flags = asoc->default_flags; 5902 info.snd_ppid = asoc->default_ppid; 5903 info.snd_context = asoc->default_context; 5904 } else { 5905 info.snd_sid = sp->default_stream; 5906 info.snd_flags = sp->default_flags; 5907 info.snd_ppid = sp->default_ppid; 5908 info.snd_context = sp->default_context; 5909 } 5910 5911 if (put_user(len, optlen)) 5912 return -EFAULT; 5913 if (copy_to_user(optval, &info, len)) 5914 return -EFAULT; 5915 5916 return 0; 5917 } 5918 5919 /* 5920 * 5921 * 7.1.5 SCTP_NODELAY 5922 * 5923 * Turn on/off any Nagle-like algorithm. This means that packets are 5924 * generally sent as soon as possible and no unnecessary delays are 5925 * introduced, at the cost of more packets in the network. Expects an 5926 * integer boolean flag. 5927 */ 5928 5929 static int sctp_getsockopt_nodelay(struct sock *sk, int len, 5930 char __user *optval, int __user *optlen) 5931 { 5932 int val; 5933 5934 if (len < sizeof(int)) 5935 return -EINVAL; 5936 5937 len = sizeof(int); 5938 val = (sctp_sk(sk)->nodelay == 1); 5939 if (put_user(len, optlen)) 5940 return -EFAULT; 5941 if (copy_to_user(optval, &val, len)) 5942 return -EFAULT; 5943 return 0; 5944 } 5945 5946 /* 5947 * 5948 * 7.1.1 SCTP_RTOINFO 5949 * 5950 * The protocol parameters used to initialize and bound retransmission 5951 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 5952 * and modify these parameters. 5953 * All parameters are time values, in milliseconds. A value of 0, when 5954 * modifying the parameters, indicates that the current value should not 5955 * be changed. 5956 * 5957 */ 5958 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, 5959 char __user *optval, 5960 int __user *optlen) { 5961 struct sctp_rtoinfo rtoinfo; 5962 struct sctp_association *asoc; 5963 5964 if (len < sizeof (struct sctp_rtoinfo)) 5965 return -EINVAL; 5966 5967 len = sizeof(struct sctp_rtoinfo); 5968 5969 if (copy_from_user(&rtoinfo, optval, len)) 5970 return -EFAULT; 5971 5972 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 5973 5974 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) 5975 return -EINVAL; 5976 5977 /* Values corresponding to the specific association. */ 5978 if (asoc) { 5979 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); 5980 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); 5981 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); 5982 } else { 5983 /* Values corresponding to the endpoint. */ 5984 struct sctp_sock *sp = sctp_sk(sk); 5985 5986 rtoinfo.srto_initial = sp->rtoinfo.srto_initial; 5987 rtoinfo.srto_max = sp->rtoinfo.srto_max; 5988 rtoinfo.srto_min = sp->rtoinfo.srto_min; 5989 } 5990 5991 if (put_user(len, optlen)) 5992 return -EFAULT; 5993 5994 if (copy_to_user(optval, &rtoinfo, len)) 5995 return -EFAULT; 5996 5997 return 0; 5998 } 5999 6000 /* 6001 * 6002 * 7.1.2 SCTP_ASSOCINFO 6003 * 6004 * This option is used to tune the maximum retransmission attempts 6005 * of the association. 6006 * Returns an error if the new association retransmission value is 6007 * greater than the sum of the retransmission value of the peer. 6008 * See [SCTP] for more information. 6009 * 6010 */ 6011 static int sctp_getsockopt_associnfo(struct sock *sk, int len, 6012 char __user *optval, 6013 int __user *optlen) 6014 { 6015 6016 struct sctp_assocparams assocparams; 6017 struct sctp_association *asoc; 6018 struct list_head *pos; 6019 int cnt = 0; 6020 6021 if (len < sizeof (struct sctp_assocparams)) 6022 return -EINVAL; 6023 6024 len = sizeof(struct sctp_assocparams); 6025 6026 if (copy_from_user(&assocparams, optval, len)) 6027 return -EFAULT; 6028 6029 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 6030 6031 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) 6032 return -EINVAL; 6033 6034 /* Values correspoinding to the specific association */ 6035 if (asoc) { 6036 assocparams.sasoc_asocmaxrxt = asoc->max_retrans; 6037 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; 6038 assocparams.sasoc_local_rwnd = asoc->a_rwnd; 6039 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); 6040 6041 list_for_each(pos, &asoc->peer.transport_addr_list) { 6042 cnt++; 6043 } 6044 6045 assocparams.sasoc_number_peer_destinations = cnt; 6046 } else { 6047 /* Values corresponding to the endpoint */ 6048 struct sctp_sock *sp = sctp_sk(sk); 6049 6050 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; 6051 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; 6052 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; 6053 assocparams.sasoc_cookie_life = 6054 sp->assocparams.sasoc_cookie_life; 6055 assocparams.sasoc_number_peer_destinations = 6056 sp->assocparams. 6057 sasoc_number_peer_destinations; 6058 } 6059 6060 if (put_user(len, optlen)) 6061 return -EFAULT; 6062 6063 if (copy_to_user(optval, &assocparams, len)) 6064 return -EFAULT; 6065 6066 return 0; 6067 } 6068 6069 /* 6070 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 6071 * 6072 * This socket option is a boolean flag which turns on or off mapped V4 6073 * addresses. If this option is turned on and the socket is type 6074 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 6075 * If this option is turned off, then no mapping will be done of V4 6076 * addresses and a user will receive both PF_INET6 and PF_INET type 6077 * addresses on the socket. 6078 */ 6079 static int sctp_getsockopt_mappedv4(struct sock *sk, int len, 6080 char __user *optval, int __user *optlen) 6081 { 6082 int val; 6083 struct sctp_sock *sp = sctp_sk(sk); 6084 6085 if (len < sizeof(int)) 6086 return -EINVAL; 6087 6088 len = sizeof(int); 6089 val = sp->v4mapped; 6090 if (put_user(len, optlen)) 6091 return -EFAULT; 6092 if (copy_to_user(optval, &val, len)) 6093 return -EFAULT; 6094 6095 return 0; 6096 } 6097 6098 /* 6099 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 6100 * (chapter and verse is quoted at sctp_setsockopt_context()) 6101 */ 6102 static int sctp_getsockopt_context(struct sock *sk, int len, 6103 char __user *optval, int __user *optlen) 6104 { 6105 struct sctp_assoc_value params; 6106 struct sctp_sock *sp; 6107 struct sctp_association *asoc; 6108 6109 if (len < sizeof(struct sctp_assoc_value)) 6110 return -EINVAL; 6111 6112 len = sizeof(struct sctp_assoc_value); 6113 6114 if (copy_from_user(¶ms, optval, len)) 6115 return -EFAULT; 6116 6117 sp = sctp_sk(sk); 6118 6119 if (params.assoc_id != 0) { 6120 asoc = sctp_id2assoc(sk, params.assoc_id); 6121 if (!asoc) 6122 return -EINVAL; 6123 params.assoc_value = asoc->default_rcv_context; 6124 } else { 6125 params.assoc_value = sp->default_rcv_context; 6126 } 6127 6128 if (put_user(len, optlen)) 6129 return -EFAULT; 6130 if (copy_to_user(optval, ¶ms, len)) 6131 return -EFAULT; 6132 6133 return 0; 6134 } 6135 6136 /* 6137 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 6138 * This option will get or set the maximum size to put in any outgoing 6139 * SCTP DATA chunk. If a message is larger than this size it will be 6140 * fragmented by SCTP into the specified size. Note that the underlying 6141 * SCTP implementation may fragment into smaller sized chunks when the 6142 * PMTU of the underlying association is smaller than the value set by 6143 * the user. The default value for this option is '0' which indicates 6144 * the user is NOT limiting fragmentation and only the PMTU will effect 6145 * SCTP's choice of DATA chunk size. Note also that values set larger 6146 * than the maximum size of an IP datagram will effectively let SCTP 6147 * control fragmentation (i.e. the same as setting this option to 0). 6148 * 6149 * The following structure is used to access and modify this parameter: 6150 * 6151 * struct sctp_assoc_value { 6152 * sctp_assoc_t assoc_id; 6153 * uint32_t assoc_value; 6154 * }; 6155 * 6156 * assoc_id: This parameter is ignored for one-to-one style sockets. 6157 * For one-to-many style sockets this parameter indicates which 6158 * association the user is performing an action upon. Note that if 6159 * this field's value is zero then the endpoints default value is 6160 * changed (effecting future associations only). 6161 * assoc_value: This parameter specifies the maximum size in bytes. 6162 */ 6163 static int sctp_getsockopt_maxseg(struct sock *sk, int len, 6164 char __user *optval, int __user *optlen) 6165 { 6166 struct sctp_assoc_value params; 6167 struct sctp_association *asoc; 6168 6169 if (len == sizeof(int)) { 6170 pr_warn_ratelimited(DEPRECATED 6171 "%s (pid %d) " 6172 "Use of int in maxseg socket option.\n" 6173 "Use struct sctp_assoc_value instead\n", 6174 current->comm, task_pid_nr(current)); 6175 params.assoc_id = 0; 6176 } else if (len >= sizeof(struct sctp_assoc_value)) { 6177 len = sizeof(struct sctp_assoc_value); 6178 if (copy_from_user(¶ms, optval, len)) 6179 return -EFAULT; 6180 } else 6181 return -EINVAL; 6182 6183 asoc = sctp_id2assoc(sk, params.assoc_id); 6184 if (!asoc && params.assoc_id && sctp_style(sk, UDP)) 6185 return -EINVAL; 6186 6187 if (asoc) 6188 params.assoc_value = asoc->frag_point; 6189 else 6190 params.assoc_value = sctp_sk(sk)->user_frag; 6191 6192 if (put_user(len, optlen)) 6193 return -EFAULT; 6194 if (len == sizeof(int)) { 6195 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6196 return -EFAULT; 6197 } else { 6198 if (copy_to_user(optval, ¶ms, len)) 6199 return -EFAULT; 6200 } 6201 6202 return 0; 6203 } 6204 6205 /* 6206 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 6207 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) 6208 */ 6209 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, 6210 char __user *optval, int __user *optlen) 6211 { 6212 int val; 6213 6214 if (len < sizeof(int)) 6215 return -EINVAL; 6216 6217 len = sizeof(int); 6218 6219 val = sctp_sk(sk)->frag_interleave; 6220 if (put_user(len, optlen)) 6221 return -EFAULT; 6222 if (copy_to_user(optval, &val, len)) 6223 return -EFAULT; 6224 6225 return 0; 6226 } 6227 6228 /* 6229 * 7.1.25. Set or Get the sctp partial delivery point 6230 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) 6231 */ 6232 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, 6233 char __user *optval, 6234 int __user *optlen) 6235 { 6236 u32 val; 6237 6238 if (len < sizeof(u32)) 6239 return -EINVAL; 6240 6241 len = sizeof(u32); 6242 6243 val = sctp_sk(sk)->pd_point; 6244 if (put_user(len, optlen)) 6245 return -EFAULT; 6246 if (copy_to_user(optval, &val, len)) 6247 return -EFAULT; 6248 6249 return 0; 6250 } 6251 6252 /* 6253 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 6254 * (chapter and verse is quoted at sctp_setsockopt_maxburst()) 6255 */ 6256 static int sctp_getsockopt_maxburst(struct sock *sk, int len, 6257 char __user *optval, 6258 int __user *optlen) 6259 { 6260 struct sctp_assoc_value params; 6261 struct sctp_sock *sp; 6262 struct sctp_association *asoc; 6263 6264 if (len == sizeof(int)) { 6265 pr_warn_ratelimited(DEPRECATED 6266 "%s (pid %d) " 6267 "Use of int in max_burst socket option.\n" 6268 "Use struct sctp_assoc_value instead\n", 6269 current->comm, task_pid_nr(current)); 6270 params.assoc_id = 0; 6271 } else if (len >= sizeof(struct sctp_assoc_value)) { 6272 len = sizeof(struct sctp_assoc_value); 6273 if (copy_from_user(¶ms, optval, len)) 6274 return -EFAULT; 6275 } else 6276 return -EINVAL; 6277 6278 sp = sctp_sk(sk); 6279 6280 if (params.assoc_id != 0) { 6281 asoc = sctp_id2assoc(sk, params.assoc_id); 6282 if (!asoc) 6283 return -EINVAL; 6284 params.assoc_value = asoc->max_burst; 6285 } else 6286 params.assoc_value = sp->max_burst; 6287 6288 if (len == sizeof(int)) { 6289 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6290 return -EFAULT; 6291 } else { 6292 if (copy_to_user(optval, ¶ms, len)) 6293 return -EFAULT; 6294 } 6295 6296 return 0; 6297 6298 } 6299 6300 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, 6301 char __user *optval, int __user *optlen) 6302 { 6303 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6304 struct sctp_hmacalgo __user *p = (void __user *)optval; 6305 struct sctp_hmac_algo_param *hmacs; 6306 __u16 data_len = 0; 6307 u32 num_idents; 6308 int i; 6309 6310 if (!ep->auth_enable) 6311 return -EACCES; 6312 6313 hmacs = ep->auth_hmacs_list; 6314 data_len = ntohs(hmacs->param_hdr.length) - 6315 sizeof(struct sctp_paramhdr); 6316 6317 if (len < sizeof(struct sctp_hmacalgo) + data_len) 6318 return -EINVAL; 6319 6320 len = sizeof(struct sctp_hmacalgo) + data_len; 6321 num_idents = data_len / sizeof(u16); 6322 6323 if (put_user(len, optlen)) 6324 return -EFAULT; 6325 if (put_user(num_idents, &p->shmac_num_idents)) 6326 return -EFAULT; 6327 for (i = 0; i < num_idents; i++) { 6328 __u16 hmacid = ntohs(hmacs->hmac_ids[i]); 6329 6330 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) 6331 return -EFAULT; 6332 } 6333 return 0; 6334 } 6335 6336 static int sctp_getsockopt_active_key(struct sock *sk, int len, 6337 char __user *optval, int __user *optlen) 6338 { 6339 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6340 struct sctp_authkeyid val; 6341 struct sctp_association *asoc; 6342 6343 if (!ep->auth_enable) 6344 return -EACCES; 6345 6346 if (len < sizeof(struct sctp_authkeyid)) 6347 return -EINVAL; 6348 6349 len = sizeof(struct sctp_authkeyid); 6350 if (copy_from_user(&val, optval, len)) 6351 return -EFAULT; 6352 6353 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 6354 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 6355 return -EINVAL; 6356 6357 if (asoc) 6358 val.scact_keynumber = asoc->active_key_id; 6359 else 6360 val.scact_keynumber = ep->active_key_id; 6361 6362 if (put_user(len, optlen)) 6363 return -EFAULT; 6364 if (copy_to_user(optval, &val, len)) 6365 return -EFAULT; 6366 6367 return 0; 6368 } 6369 6370 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, 6371 char __user *optval, int __user *optlen) 6372 { 6373 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6374 struct sctp_authchunks __user *p = (void __user *)optval; 6375 struct sctp_authchunks val; 6376 struct sctp_association *asoc; 6377 struct sctp_chunks_param *ch; 6378 u32 num_chunks = 0; 6379 char __user *to; 6380 6381 if (!ep->auth_enable) 6382 return -EACCES; 6383 6384 if (len < sizeof(struct sctp_authchunks)) 6385 return -EINVAL; 6386 6387 if (copy_from_user(&val, optval, sizeof(val))) 6388 return -EFAULT; 6389 6390 to = p->gauth_chunks; 6391 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 6392 if (!asoc) 6393 return -EINVAL; 6394 6395 ch = asoc->peer.peer_chunks; 6396 if (!ch) 6397 goto num; 6398 6399 /* See if the user provided enough room for all the data */ 6400 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 6401 if (len < num_chunks) 6402 return -EINVAL; 6403 6404 if (copy_to_user(to, ch->chunks, num_chunks)) 6405 return -EFAULT; 6406 num: 6407 len = sizeof(struct sctp_authchunks) + num_chunks; 6408 if (put_user(len, optlen)) 6409 return -EFAULT; 6410 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 6411 return -EFAULT; 6412 return 0; 6413 } 6414 6415 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, 6416 char __user *optval, int __user *optlen) 6417 { 6418 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6419 struct sctp_authchunks __user *p = (void __user *)optval; 6420 struct sctp_authchunks val; 6421 struct sctp_association *asoc; 6422 struct sctp_chunks_param *ch; 6423 u32 num_chunks = 0; 6424 char __user *to; 6425 6426 if (!ep->auth_enable) 6427 return -EACCES; 6428 6429 if (len < sizeof(struct sctp_authchunks)) 6430 return -EINVAL; 6431 6432 if (copy_from_user(&val, optval, sizeof(val))) 6433 return -EFAULT; 6434 6435 to = p->gauth_chunks; 6436 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 6437 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) 6438 return -EINVAL; 6439 6440 if (asoc) 6441 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; 6442 else 6443 ch = ep->auth_chunk_list; 6444 6445 if (!ch) 6446 goto num; 6447 6448 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 6449 if (len < sizeof(struct sctp_authchunks) + num_chunks) 6450 return -EINVAL; 6451 6452 if (copy_to_user(to, ch->chunks, num_chunks)) 6453 return -EFAULT; 6454 num: 6455 len = sizeof(struct sctp_authchunks) + num_chunks; 6456 if (put_user(len, optlen)) 6457 return -EFAULT; 6458 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 6459 return -EFAULT; 6460 6461 return 0; 6462 } 6463 6464 /* 6465 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) 6466 * This option gets the current number of associations that are attached 6467 * to a one-to-many style socket. The option value is an uint32_t. 6468 */ 6469 static int sctp_getsockopt_assoc_number(struct sock *sk, int len, 6470 char __user *optval, int __user *optlen) 6471 { 6472 struct sctp_sock *sp = sctp_sk(sk); 6473 struct sctp_association *asoc; 6474 u32 val = 0; 6475 6476 if (sctp_style(sk, TCP)) 6477 return -EOPNOTSUPP; 6478 6479 if (len < sizeof(u32)) 6480 return -EINVAL; 6481 6482 len = sizeof(u32); 6483 6484 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 6485 val++; 6486 } 6487 6488 if (put_user(len, optlen)) 6489 return -EFAULT; 6490 if (copy_to_user(optval, &val, len)) 6491 return -EFAULT; 6492 6493 return 0; 6494 } 6495 6496 /* 6497 * 8.1.23 SCTP_AUTO_ASCONF 6498 * See the corresponding setsockopt entry as description 6499 */ 6500 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, 6501 char __user *optval, int __user *optlen) 6502 { 6503 int val = 0; 6504 6505 if (len < sizeof(int)) 6506 return -EINVAL; 6507 6508 len = sizeof(int); 6509 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) 6510 val = 1; 6511 if (put_user(len, optlen)) 6512 return -EFAULT; 6513 if (copy_to_user(optval, &val, len)) 6514 return -EFAULT; 6515 return 0; 6516 } 6517 6518 /* 6519 * 8.2.6. Get the Current Identifiers of Associations 6520 * (SCTP_GET_ASSOC_ID_LIST) 6521 * 6522 * This option gets the current list of SCTP association identifiers of 6523 * the SCTP associations handled by a one-to-many style socket. 6524 */ 6525 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, 6526 char __user *optval, int __user *optlen) 6527 { 6528 struct sctp_sock *sp = sctp_sk(sk); 6529 struct sctp_association *asoc; 6530 struct sctp_assoc_ids *ids; 6531 u32 num = 0; 6532 6533 if (sctp_style(sk, TCP)) 6534 return -EOPNOTSUPP; 6535 6536 if (len < sizeof(struct sctp_assoc_ids)) 6537 return -EINVAL; 6538 6539 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 6540 num++; 6541 } 6542 6543 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num) 6544 return -EINVAL; 6545 6546 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num; 6547 6548 ids = kmalloc(len, GFP_USER | __GFP_NOWARN); 6549 if (unlikely(!ids)) 6550 return -ENOMEM; 6551 6552 ids->gaids_number_of_ids = num; 6553 num = 0; 6554 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 6555 ids->gaids_assoc_id[num++] = asoc->assoc_id; 6556 } 6557 6558 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { 6559 kfree(ids); 6560 return -EFAULT; 6561 } 6562 6563 kfree(ids); 6564 return 0; 6565 } 6566 6567 /* 6568 * SCTP_PEER_ADDR_THLDS 6569 * 6570 * This option allows us to fetch the partially failed threshold for one or all 6571 * transports in an association. See Section 6.1 of: 6572 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt 6573 */ 6574 static int sctp_getsockopt_paddr_thresholds(struct sock *sk, 6575 char __user *optval, 6576 int len, 6577 int __user *optlen) 6578 { 6579 struct sctp_paddrthlds val; 6580 struct sctp_transport *trans; 6581 struct sctp_association *asoc; 6582 6583 if (len < sizeof(struct sctp_paddrthlds)) 6584 return -EINVAL; 6585 len = sizeof(struct sctp_paddrthlds); 6586 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len)) 6587 return -EFAULT; 6588 6589 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { 6590 asoc = sctp_id2assoc(sk, val.spt_assoc_id); 6591 if (!asoc) 6592 return -ENOENT; 6593 6594 val.spt_pathpfthld = asoc->pf_retrans; 6595 val.spt_pathmaxrxt = asoc->pathmaxrxt; 6596 } else { 6597 trans = sctp_addr_id2transport(sk, &val.spt_address, 6598 val.spt_assoc_id); 6599 if (!trans) 6600 return -ENOENT; 6601 6602 val.spt_pathmaxrxt = trans->pathmaxrxt; 6603 val.spt_pathpfthld = trans->pf_retrans; 6604 } 6605 6606 if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 6607 return -EFAULT; 6608 6609 return 0; 6610 } 6611 6612 /* 6613 * SCTP_GET_ASSOC_STATS 6614 * 6615 * This option retrieves local per endpoint statistics. It is modeled 6616 * after OpenSolaris' implementation 6617 */ 6618 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, 6619 char __user *optval, 6620 int __user *optlen) 6621 { 6622 struct sctp_assoc_stats sas; 6623 struct sctp_association *asoc = NULL; 6624 6625 /* User must provide at least the assoc id */ 6626 if (len < sizeof(sctp_assoc_t)) 6627 return -EINVAL; 6628 6629 /* Allow the struct to grow and fill in as much as possible */ 6630 len = min_t(size_t, len, sizeof(sas)); 6631 6632 if (copy_from_user(&sas, optval, len)) 6633 return -EFAULT; 6634 6635 asoc = sctp_id2assoc(sk, sas.sas_assoc_id); 6636 if (!asoc) 6637 return -EINVAL; 6638 6639 sas.sas_rtxchunks = asoc->stats.rtxchunks; 6640 sas.sas_gapcnt = asoc->stats.gapcnt; 6641 sas.sas_outofseqtsns = asoc->stats.outofseqtsns; 6642 sas.sas_osacks = asoc->stats.osacks; 6643 sas.sas_isacks = asoc->stats.isacks; 6644 sas.sas_octrlchunks = asoc->stats.octrlchunks; 6645 sas.sas_ictrlchunks = asoc->stats.ictrlchunks; 6646 sas.sas_oodchunks = asoc->stats.oodchunks; 6647 sas.sas_iodchunks = asoc->stats.iodchunks; 6648 sas.sas_ouodchunks = asoc->stats.ouodchunks; 6649 sas.sas_iuodchunks = asoc->stats.iuodchunks; 6650 sas.sas_idupchunks = asoc->stats.idupchunks; 6651 sas.sas_opackets = asoc->stats.opackets; 6652 sas.sas_ipackets = asoc->stats.ipackets; 6653 6654 /* New high max rto observed, will return 0 if not a single 6655 * RTO update took place. obs_rto_ipaddr will be bogus 6656 * in such a case 6657 */ 6658 sas.sas_maxrto = asoc->stats.max_obs_rto; 6659 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, 6660 sizeof(struct sockaddr_storage)); 6661 6662 /* Mark beginning of a new observation period */ 6663 asoc->stats.max_obs_rto = asoc->rto_min; 6664 6665 if (put_user(len, optlen)) 6666 return -EFAULT; 6667 6668 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id); 6669 6670 if (copy_to_user(optval, &sas, len)) 6671 return -EFAULT; 6672 6673 return 0; 6674 } 6675 6676 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len, 6677 char __user *optval, 6678 int __user *optlen) 6679 { 6680 int val = 0; 6681 6682 if (len < sizeof(int)) 6683 return -EINVAL; 6684 6685 len = sizeof(int); 6686 if (sctp_sk(sk)->recvrcvinfo) 6687 val = 1; 6688 if (put_user(len, optlen)) 6689 return -EFAULT; 6690 if (copy_to_user(optval, &val, len)) 6691 return -EFAULT; 6692 6693 return 0; 6694 } 6695 6696 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len, 6697 char __user *optval, 6698 int __user *optlen) 6699 { 6700 int val = 0; 6701 6702 if (len < sizeof(int)) 6703 return -EINVAL; 6704 6705 len = sizeof(int); 6706 if (sctp_sk(sk)->recvnxtinfo) 6707 val = 1; 6708 if (put_user(len, optlen)) 6709 return -EFAULT; 6710 if (copy_to_user(optval, &val, len)) 6711 return -EFAULT; 6712 6713 return 0; 6714 } 6715 6716 static int sctp_getsockopt_pr_supported(struct sock *sk, int len, 6717 char __user *optval, 6718 int __user *optlen) 6719 { 6720 struct sctp_assoc_value params; 6721 struct sctp_association *asoc; 6722 int retval = -EFAULT; 6723 6724 if (len < sizeof(params)) { 6725 retval = -EINVAL; 6726 goto out; 6727 } 6728 6729 len = sizeof(params); 6730 if (copy_from_user(¶ms, optval, len)) 6731 goto out; 6732 6733 asoc = sctp_id2assoc(sk, params.assoc_id); 6734 if (asoc) { 6735 params.assoc_value = asoc->prsctp_enable; 6736 } else if (!params.assoc_id) { 6737 struct sctp_sock *sp = sctp_sk(sk); 6738 6739 params.assoc_value = sp->ep->prsctp_enable; 6740 } else { 6741 retval = -EINVAL; 6742 goto out; 6743 } 6744 6745 if (put_user(len, optlen)) 6746 goto out; 6747 6748 if (copy_to_user(optval, ¶ms, len)) 6749 goto out; 6750 6751 retval = 0; 6752 6753 out: 6754 return retval; 6755 } 6756 6757 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, 6758 char __user *optval, 6759 int __user *optlen) 6760 { 6761 struct sctp_default_prinfo info; 6762 struct sctp_association *asoc; 6763 int retval = -EFAULT; 6764 6765 if (len < sizeof(info)) { 6766 retval = -EINVAL; 6767 goto out; 6768 } 6769 6770 len = sizeof(info); 6771 if (copy_from_user(&info, optval, len)) 6772 goto out; 6773 6774 asoc = sctp_id2assoc(sk, info.pr_assoc_id); 6775 if (asoc) { 6776 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); 6777 info.pr_value = asoc->default_timetolive; 6778 } else if (!info.pr_assoc_id) { 6779 struct sctp_sock *sp = sctp_sk(sk); 6780 6781 info.pr_policy = SCTP_PR_POLICY(sp->default_flags); 6782 info.pr_value = sp->default_timetolive; 6783 } else { 6784 retval = -EINVAL; 6785 goto out; 6786 } 6787 6788 if (put_user(len, optlen)) 6789 goto out; 6790 6791 if (copy_to_user(optval, &info, len)) 6792 goto out; 6793 6794 retval = 0; 6795 6796 out: 6797 return retval; 6798 } 6799 6800 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, 6801 char __user *optval, 6802 int __user *optlen) 6803 { 6804 struct sctp_prstatus params; 6805 struct sctp_association *asoc; 6806 int policy; 6807 int retval = -EINVAL; 6808 6809 if (len < sizeof(params)) 6810 goto out; 6811 6812 len = sizeof(params); 6813 if (copy_from_user(¶ms, optval, len)) { 6814 retval = -EFAULT; 6815 goto out; 6816 } 6817 6818 policy = params.sprstat_policy; 6819 if (policy & ~SCTP_PR_SCTP_MASK) 6820 goto out; 6821 6822 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 6823 if (!asoc) 6824 goto out; 6825 6826 if (policy == SCTP_PR_SCTP_NONE) { 6827 params.sprstat_abandoned_unsent = 0; 6828 params.sprstat_abandoned_sent = 0; 6829 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 6830 params.sprstat_abandoned_unsent += 6831 asoc->abandoned_unsent[policy]; 6832 params.sprstat_abandoned_sent += 6833 asoc->abandoned_sent[policy]; 6834 } 6835 } else { 6836 params.sprstat_abandoned_unsent = 6837 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 6838 params.sprstat_abandoned_sent = 6839 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; 6840 } 6841 6842 if (put_user(len, optlen)) { 6843 retval = -EFAULT; 6844 goto out; 6845 } 6846 6847 if (copy_to_user(optval, ¶ms, len)) { 6848 retval = -EFAULT; 6849 goto out; 6850 } 6851 6852 retval = 0; 6853 6854 out: 6855 return retval; 6856 } 6857 6858 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, 6859 char __user *optval, 6860 int __user *optlen) 6861 { 6862 struct sctp_stream_out_ext *streamoute; 6863 struct sctp_association *asoc; 6864 struct sctp_prstatus params; 6865 int retval = -EINVAL; 6866 int policy; 6867 6868 if (len < sizeof(params)) 6869 goto out; 6870 6871 len = sizeof(params); 6872 if (copy_from_user(¶ms, optval, len)) { 6873 retval = -EFAULT; 6874 goto out; 6875 } 6876 6877 policy = params.sprstat_policy; 6878 if (policy & ~SCTP_PR_SCTP_MASK) 6879 goto out; 6880 6881 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 6882 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) 6883 goto out; 6884 6885 streamoute = asoc->stream.out[params.sprstat_sid].ext; 6886 if (!streamoute) { 6887 /* Not allocated yet, means all stats are 0 */ 6888 params.sprstat_abandoned_unsent = 0; 6889 params.sprstat_abandoned_sent = 0; 6890 retval = 0; 6891 goto out; 6892 } 6893 6894 if (policy == SCTP_PR_SCTP_NONE) { 6895 params.sprstat_abandoned_unsent = 0; 6896 params.sprstat_abandoned_sent = 0; 6897 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 6898 params.sprstat_abandoned_unsent += 6899 streamoute->abandoned_unsent[policy]; 6900 params.sprstat_abandoned_sent += 6901 streamoute->abandoned_sent[policy]; 6902 } 6903 } else { 6904 params.sprstat_abandoned_unsent = 6905 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 6906 params.sprstat_abandoned_sent = 6907 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; 6908 } 6909 6910 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { 6911 retval = -EFAULT; 6912 goto out; 6913 } 6914 6915 retval = 0; 6916 6917 out: 6918 return retval; 6919 } 6920 6921 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, 6922 char __user *optval, 6923 int __user *optlen) 6924 { 6925 struct sctp_assoc_value params; 6926 struct sctp_association *asoc; 6927 int retval = -EFAULT; 6928 6929 if (len < sizeof(params)) { 6930 retval = -EINVAL; 6931 goto out; 6932 } 6933 6934 len = sizeof(params); 6935 if (copy_from_user(¶ms, optval, len)) 6936 goto out; 6937 6938 asoc = sctp_id2assoc(sk, params.assoc_id); 6939 if (asoc) { 6940 params.assoc_value = asoc->reconf_enable; 6941 } else if (!params.assoc_id) { 6942 struct sctp_sock *sp = sctp_sk(sk); 6943 6944 params.assoc_value = sp->ep->reconf_enable; 6945 } else { 6946 retval = -EINVAL; 6947 goto out; 6948 } 6949 6950 if (put_user(len, optlen)) 6951 goto out; 6952 6953 if (copy_to_user(optval, ¶ms, len)) 6954 goto out; 6955 6956 retval = 0; 6957 6958 out: 6959 return retval; 6960 } 6961 6962 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, 6963 char __user *optval, 6964 int __user *optlen) 6965 { 6966 struct sctp_assoc_value params; 6967 struct sctp_association *asoc; 6968 int retval = -EFAULT; 6969 6970 if (len < sizeof(params)) { 6971 retval = -EINVAL; 6972 goto out; 6973 } 6974 6975 len = sizeof(params); 6976 if (copy_from_user(¶ms, optval, len)) 6977 goto out; 6978 6979 asoc = sctp_id2assoc(sk, params.assoc_id); 6980 if (asoc) { 6981 params.assoc_value = asoc->strreset_enable; 6982 } else if (!params.assoc_id) { 6983 struct sctp_sock *sp = sctp_sk(sk); 6984 6985 params.assoc_value = sp->ep->strreset_enable; 6986 } else { 6987 retval = -EINVAL; 6988 goto out; 6989 } 6990 6991 if (put_user(len, optlen)) 6992 goto out; 6993 6994 if (copy_to_user(optval, ¶ms, len)) 6995 goto out; 6996 6997 retval = 0; 6998 6999 out: 7000 return retval; 7001 } 7002 7003 static int sctp_getsockopt_scheduler(struct sock *sk, int len, 7004 char __user *optval, 7005 int __user *optlen) 7006 { 7007 struct sctp_assoc_value params; 7008 struct sctp_association *asoc; 7009 int retval = -EFAULT; 7010 7011 if (len < sizeof(params)) { 7012 retval = -EINVAL; 7013 goto out; 7014 } 7015 7016 len = sizeof(params); 7017 if (copy_from_user(¶ms, optval, len)) 7018 goto out; 7019 7020 asoc = sctp_id2assoc(sk, params.assoc_id); 7021 if (!asoc) { 7022 retval = -EINVAL; 7023 goto out; 7024 } 7025 7026 params.assoc_value = sctp_sched_get_sched(asoc); 7027 7028 if (put_user(len, optlen)) 7029 goto out; 7030 7031 if (copy_to_user(optval, ¶ms, len)) 7032 goto out; 7033 7034 retval = 0; 7035 7036 out: 7037 return retval; 7038 } 7039 7040 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, 7041 char __user *optval, 7042 int __user *optlen) 7043 { 7044 struct sctp_stream_value params; 7045 struct sctp_association *asoc; 7046 int retval = -EFAULT; 7047 7048 if (len < sizeof(params)) { 7049 retval = -EINVAL; 7050 goto out; 7051 } 7052 7053 len = sizeof(params); 7054 if (copy_from_user(¶ms, optval, len)) 7055 goto out; 7056 7057 asoc = sctp_id2assoc(sk, params.assoc_id); 7058 if (!asoc) { 7059 retval = -EINVAL; 7060 goto out; 7061 } 7062 7063 retval = sctp_sched_get_value(asoc, params.stream_id, 7064 ¶ms.stream_value); 7065 if (retval) 7066 goto out; 7067 7068 if (put_user(len, optlen)) { 7069 retval = -EFAULT; 7070 goto out; 7071 } 7072 7073 if (copy_to_user(optval, ¶ms, len)) { 7074 retval = -EFAULT; 7075 goto out; 7076 } 7077 7078 out: 7079 return retval; 7080 } 7081 7082 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, 7083 char __user *optval, 7084 int __user *optlen) 7085 { 7086 struct sctp_assoc_value params; 7087 struct sctp_association *asoc; 7088 int retval = -EFAULT; 7089 7090 if (len < sizeof(params)) { 7091 retval = -EINVAL; 7092 goto out; 7093 } 7094 7095 len = sizeof(params); 7096 if (copy_from_user(¶ms, optval, len)) 7097 goto out; 7098 7099 asoc = sctp_id2assoc(sk, params.assoc_id); 7100 if (asoc) { 7101 params.assoc_value = asoc->intl_enable; 7102 } else if (!params.assoc_id) { 7103 struct sctp_sock *sp = sctp_sk(sk); 7104 7105 params.assoc_value = sp->strm_interleave; 7106 } else { 7107 retval = -EINVAL; 7108 goto out; 7109 } 7110 7111 if (put_user(len, optlen)) 7112 goto out; 7113 7114 if (copy_to_user(optval, ¶ms, len)) 7115 goto out; 7116 7117 retval = 0; 7118 7119 out: 7120 return retval; 7121 } 7122 7123 static int sctp_getsockopt(struct sock *sk, int level, int optname, 7124 char __user *optval, int __user *optlen) 7125 { 7126 int retval = 0; 7127 int len; 7128 7129 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); 7130 7131 /* I can hardly begin to describe how wrong this is. This is 7132 * so broken as to be worse than useless. The API draft 7133 * REALLY is NOT helpful here... I am not convinced that the 7134 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP 7135 * are at all well-founded. 7136 */ 7137 if (level != SOL_SCTP) { 7138 struct sctp_af *af = sctp_sk(sk)->pf->af; 7139 7140 retval = af->getsockopt(sk, level, optname, optval, optlen); 7141 return retval; 7142 } 7143 7144 if (get_user(len, optlen)) 7145 return -EFAULT; 7146 7147 if (len < 0) 7148 return -EINVAL; 7149 7150 lock_sock(sk); 7151 7152 switch (optname) { 7153 case SCTP_STATUS: 7154 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); 7155 break; 7156 case SCTP_DISABLE_FRAGMENTS: 7157 retval = sctp_getsockopt_disable_fragments(sk, len, optval, 7158 optlen); 7159 break; 7160 case SCTP_EVENTS: 7161 retval = sctp_getsockopt_events(sk, len, optval, optlen); 7162 break; 7163 case SCTP_AUTOCLOSE: 7164 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); 7165 break; 7166 case SCTP_SOCKOPT_PEELOFF: 7167 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); 7168 break; 7169 case SCTP_SOCKOPT_PEELOFF_FLAGS: 7170 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); 7171 break; 7172 case SCTP_PEER_ADDR_PARAMS: 7173 retval = sctp_getsockopt_peer_addr_params(sk, len, optval, 7174 optlen); 7175 break; 7176 case SCTP_DELAYED_SACK: 7177 retval = sctp_getsockopt_delayed_ack(sk, len, optval, 7178 optlen); 7179 break; 7180 case SCTP_INITMSG: 7181 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); 7182 break; 7183 case SCTP_GET_PEER_ADDRS: 7184 retval = sctp_getsockopt_peer_addrs(sk, len, optval, 7185 optlen); 7186 break; 7187 case SCTP_GET_LOCAL_ADDRS: 7188 retval = sctp_getsockopt_local_addrs(sk, len, optval, 7189 optlen); 7190 break; 7191 case SCTP_SOCKOPT_CONNECTX3: 7192 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); 7193 break; 7194 case SCTP_DEFAULT_SEND_PARAM: 7195 retval = sctp_getsockopt_default_send_param(sk, len, 7196 optval, optlen); 7197 break; 7198 case SCTP_DEFAULT_SNDINFO: 7199 retval = sctp_getsockopt_default_sndinfo(sk, len, 7200 optval, optlen); 7201 break; 7202 case SCTP_PRIMARY_ADDR: 7203 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); 7204 break; 7205 case SCTP_NODELAY: 7206 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); 7207 break; 7208 case SCTP_RTOINFO: 7209 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); 7210 break; 7211 case SCTP_ASSOCINFO: 7212 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); 7213 break; 7214 case SCTP_I_WANT_MAPPED_V4_ADDR: 7215 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); 7216 break; 7217 case SCTP_MAXSEG: 7218 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); 7219 break; 7220 case SCTP_GET_PEER_ADDR_INFO: 7221 retval = sctp_getsockopt_peer_addr_info(sk, len, optval, 7222 optlen); 7223 break; 7224 case SCTP_ADAPTATION_LAYER: 7225 retval = sctp_getsockopt_adaptation_layer(sk, len, optval, 7226 optlen); 7227 break; 7228 case SCTP_CONTEXT: 7229 retval = sctp_getsockopt_context(sk, len, optval, optlen); 7230 break; 7231 case SCTP_FRAGMENT_INTERLEAVE: 7232 retval = sctp_getsockopt_fragment_interleave(sk, len, optval, 7233 optlen); 7234 break; 7235 case SCTP_PARTIAL_DELIVERY_POINT: 7236 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, 7237 optlen); 7238 break; 7239 case SCTP_MAX_BURST: 7240 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); 7241 break; 7242 case SCTP_AUTH_KEY: 7243 case SCTP_AUTH_CHUNK: 7244 case SCTP_AUTH_DELETE_KEY: 7245 case SCTP_AUTH_DEACTIVATE_KEY: 7246 retval = -EOPNOTSUPP; 7247 break; 7248 case SCTP_HMAC_IDENT: 7249 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); 7250 break; 7251 case SCTP_AUTH_ACTIVE_KEY: 7252 retval = sctp_getsockopt_active_key(sk, len, optval, optlen); 7253 break; 7254 case SCTP_PEER_AUTH_CHUNKS: 7255 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, 7256 optlen); 7257 break; 7258 case SCTP_LOCAL_AUTH_CHUNKS: 7259 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, 7260 optlen); 7261 break; 7262 case SCTP_GET_ASSOC_NUMBER: 7263 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); 7264 break; 7265 case SCTP_GET_ASSOC_ID_LIST: 7266 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); 7267 break; 7268 case SCTP_AUTO_ASCONF: 7269 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); 7270 break; 7271 case SCTP_PEER_ADDR_THLDS: 7272 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen); 7273 break; 7274 case SCTP_GET_ASSOC_STATS: 7275 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); 7276 break; 7277 case SCTP_RECVRCVINFO: 7278 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); 7279 break; 7280 case SCTP_RECVNXTINFO: 7281 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); 7282 break; 7283 case SCTP_PR_SUPPORTED: 7284 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); 7285 break; 7286 case SCTP_DEFAULT_PRINFO: 7287 retval = sctp_getsockopt_default_prinfo(sk, len, optval, 7288 optlen); 7289 break; 7290 case SCTP_PR_ASSOC_STATUS: 7291 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, 7292 optlen); 7293 break; 7294 case SCTP_PR_STREAM_STATUS: 7295 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, 7296 optlen); 7297 break; 7298 case SCTP_RECONFIG_SUPPORTED: 7299 retval = sctp_getsockopt_reconfig_supported(sk, len, optval, 7300 optlen); 7301 break; 7302 case SCTP_ENABLE_STREAM_RESET: 7303 retval = sctp_getsockopt_enable_strreset(sk, len, optval, 7304 optlen); 7305 break; 7306 case SCTP_STREAM_SCHEDULER: 7307 retval = sctp_getsockopt_scheduler(sk, len, optval, 7308 optlen); 7309 break; 7310 case SCTP_STREAM_SCHEDULER_VALUE: 7311 retval = sctp_getsockopt_scheduler_value(sk, len, optval, 7312 optlen); 7313 break; 7314 case SCTP_INTERLEAVING_SUPPORTED: 7315 retval = sctp_getsockopt_interleaving_supported(sk, len, optval, 7316 optlen); 7317 break; 7318 default: 7319 retval = -ENOPROTOOPT; 7320 break; 7321 } 7322 7323 release_sock(sk); 7324 return retval; 7325 } 7326 7327 static int sctp_hash(struct sock *sk) 7328 { 7329 /* STUB */ 7330 return 0; 7331 } 7332 7333 static void sctp_unhash(struct sock *sk) 7334 { 7335 /* STUB */ 7336 } 7337 7338 /* Check if port is acceptable. Possibly find first available port. 7339 * 7340 * The port hash table (contained in the 'global' SCTP protocol storage 7341 * returned by struct sctp_protocol *sctp_get_protocol()). The hash 7342 * table is an array of 4096 lists (sctp_bind_hashbucket). Each 7343 * list (the list number is the port number hashed out, so as you 7344 * would expect from a hash function, all the ports in a given list have 7345 * such a number that hashes out to the same list number; you were 7346 * expecting that, right?); so each list has a set of ports, with a 7347 * link to the socket (struct sock) that uses it, the port number and 7348 * a fastreuse flag (FIXME: NPI ipg). 7349 */ 7350 static struct sctp_bind_bucket *sctp_bucket_create( 7351 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); 7352 7353 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) 7354 { 7355 struct sctp_bind_hashbucket *head; /* hash list */ 7356 struct sctp_bind_bucket *pp; 7357 unsigned short snum; 7358 int ret; 7359 7360 snum = ntohs(addr->v4.sin_port); 7361 7362 pr_debug("%s: begins, snum:%d\n", __func__, snum); 7363 7364 local_bh_disable(); 7365 7366 if (snum == 0) { 7367 /* Search for an available port. */ 7368 int low, high, remaining, index; 7369 unsigned int rover; 7370 struct net *net = sock_net(sk); 7371 7372 inet_get_local_port_range(net, &low, &high); 7373 remaining = (high - low) + 1; 7374 rover = prandom_u32() % remaining + low; 7375 7376 do { 7377 rover++; 7378 if ((rover < low) || (rover > high)) 7379 rover = low; 7380 if (inet_is_local_reserved_port(net, rover)) 7381 continue; 7382 index = sctp_phashfn(sock_net(sk), rover); 7383 head = &sctp_port_hashtable[index]; 7384 spin_lock(&head->lock); 7385 sctp_for_each_hentry(pp, &head->chain) 7386 if ((pp->port == rover) && 7387 net_eq(sock_net(sk), pp->net)) 7388 goto next; 7389 break; 7390 next: 7391 spin_unlock(&head->lock); 7392 } while (--remaining > 0); 7393 7394 /* Exhausted local port range during search? */ 7395 ret = 1; 7396 if (remaining <= 0) 7397 goto fail; 7398 7399 /* OK, here is the one we will use. HEAD (the port 7400 * hash table list entry) is non-NULL and we hold it's 7401 * mutex. 7402 */ 7403 snum = rover; 7404 } else { 7405 /* We are given an specific port number; we verify 7406 * that it is not being used. If it is used, we will 7407 * exahust the search in the hash list corresponding 7408 * to the port number (snum) - we detect that with the 7409 * port iterator, pp being NULL. 7410 */ 7411 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)]; 7412 spin_lock(&head->lock); 7413 sctp_for_each_hentry(pp, &head->chain) { 7414 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk))) 7415 goto pp_found; 7416 } 7417 } 7418 pp = NULL; 7419 goto pp_not_found; 7420 pp_found: 7421 if (!hlist_empty(&pp->owner)) { 7422 /* We had a port hash table hit - there is an 7423 * available port (pp != NULL) and it is being 7424 * used by other socket (pp->owner not empty); that other 7425 * socket is going to be sk2. 7426 */ 7427 int reuse = sk->sk_reuse; 7428 struct sock *sk2; 7429 7430 pr_debug("%s: found a possible match\n", __func__); 7431 7432 if (pp->fastreuse && sk->sk_reuse && 7433 sk->sk_state != SCTP_SS_LISTENING) 7434 goto success; 7435 7436 /* Run through the list of sockets bound to the port 7437 * (pp->port) [via the pointers bind_next and 7438 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, 7439 * we get the endpoint they describe and run through 7440 * the endpoint's list of IP (v4 or v6) addresses, 7441 * comparing each of the addresses with the address of 7442 * the socket sk. If we find a match, then that means 7443 * that this port/socket (sk) combination are already 7444 * in an endpoint. 7445 */ 7446 sk_for_each_bound(sk2, &pp->owner) { 7447 struct sctp_endpoint *ep2; 7448 ep2 = sctp_sk(sk2)->ep; 7449 7450 if (sk == sk2 || 7451 (reuse && sk2->sk_reuse && 7452 sk2->sk_state != SCTP_SS_LISTENING)) 7453 continue; 7454 7455 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, 7456 sctp_sk(sk2), sctp_sk(sk))) { 7457 ret = (long)sk2; 7458 goto fail_unlock; 7459 } 7460 } 7461 7462 pr_debug("%s: found a match\n", __func__); 7463 } 7464 pp_not_found: 7465 /* If there was a hash table miss, create a new port. */ 7466 ret = 1; 7467 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum))) 7468 goto fail_unlock; 7469 7470 /* In either case (hit or miss), make sure fastreuse is 1 only 7471 * if sk->sk_reuse is too (that is, if the caller requested 7472 * SO_REUSEADDR on this socket -sk-). 7473 */ 7474 if (hlist_empty(&pp->owner)) { 7475 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) 7476 pp->fastreuse = 1; 7477 else 7478 pp->fastreuse = 0; 7479 } else if (pp->fastreuse && 7480 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) 7481 pp->fastreuse = 0; 7482 7483 /* We are set, so fill up all the data in the hash table 7484 * entry, tie the socket list information with the rest of the 7485 * sockets FIXME: Blurry, NPI (ipg). 7486 */ 7487 success: 7488 if (!sctp_sk(sk)->bind_hash) { 7489 inet_sk(sk)->inet_num = snum; 7490 sk_add_bind_node(sk, &pp->owner); 7491 sctp_sk(sk)->bind_hash = pp; 7492 } 7493 ret = 0; 7494 7495 fail_unlock: 7496 spin_unlock(&head->lock); 7497 7498 fail: 7499 local_bh_enable(); 7500 return ret; 7501 } 7502 7503 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral 7504 * port is requested. 7505 */ 7506 static int sctp_get_port(struct sock *sk, unsigned short snum) 7507 { 7508 union sctp_addr addr; 7509 struct sctp_af *af = sctp_sk(sk)->pf->af; 7510 7511 /* Set up a dummy address struct from the sk. */ 7512 af->from_sk(&addr, sk); 7513 addr.v4.sin_port = htons(snum); 7514 7515 /* Note: sk->sk_num gets filled in if ephemeral port request. */ 7516 return !!sctp_get_port_local(sk, &addr); 7517 } 7518 7519 /* 7520 * Move a socket to LISTENING state. 7521 */ 7522 static int sctp_listen_start(struct sock *sk, int backlog) 7523 { 7524 struct sctp_sock *sp = sctp_sk(sk); 7525 struct sctp_endpoint *ep = sp->ep; 7526 struct crypto_shash *tfm = NULL; 7527 char alg[32]; 7528 7529 /* Allocate HMAC for generating cookie. */ 7530 if (!sp->hmac && sp->sctp_hmac_alg) { 7531 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg); 7532 tfm = crypto_alloc_shash(alg, 0, 0); 7533 if (IS_ERR(tfm)) { 7534 net_info_ratelimited("failed to load transform for %s: %ld\n", 7535 sp->sctp_hmac_alg, PTR_ERR(tfm)); 7536 return -ENOSYS; 7537 } 7538 sctp_sk(sk)->hmac = tfm; 7539 } 7540 7541 /* 7542 * If a bind() or sctp_bindx() is not called prior to a listen() 7543 * call that allows new associations to be accepted, the system 7544 * picks an ephemeral port and will choose an address set equivalent 7545 * to binding with a wildcard address. 7546 * 7547 * This is not currently spelled out in the SCTP sockets 7548 * extensions draft, but follows the practice as seen in TCP 7549 * sockets. 7550 * 7551 */ 7552 inet_sk_set_state(sk, SCTP_SS_LISTENING); 7553 if (!ep->base.bind_addr.port) { 7554 if (sctp_autobind(sk)) 7555 return -EAGAIN; 7556 } else { 7557 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { 7558 inet_sk_set_state(sk, SCTP_SS_CLOSED); 7559 return -EADDRINUSE; 7560 } 7561 } 7562 7563 sk->sk_max_ack_backlog = backlog; 7564 sctp_hash_endpoint(ep); 7565 return 0; 7566 } 7567 7568 /* 7569 * 4.1.3 / 5.1.3 listen() 7570 * 7571 * By default, new associations are not accepted for UDP style sockets. 7572 * An application uses listen() to mark a socket as being able to 7573 * accept new associations. 7574 * 7575 * On TCP style sockets, applications use listen() to ready the SCTP 7576 * endpoint for accepting inbound associations. 7577 * 7578 * On both types of endpoints a backlog of '0' disables listening. 7579 * 7580 * Move a socket to LISTENING state. 7581 */ 7582 int sctp_inet_listen(struct socket *sock, int backlog) 7583 { 7584 struct sock *sk = sock->sk; 7585 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 7586 int err = -EINVAL; 7587 7588 if (unlikely(backlog < 0)) 7589 return err; 7590 7591 lock_sock(sk); 7592 7593 /* Peeled-off sockets are not allowed to listen(). */ 7594 if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) 7595 goto out; 7596 7597 if (sock->state != SS_UNCONNECTED) 7598 goto out; 7599 7600 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) 7601 goto out; 7602 7603 /* If backlog is zero, disable listening. */ 7604 if (!backlog) { 7605 if (sctp_sstate(sk, CLOSED)) 7606 goto out; 7607 7608 err = 0; 7609 sctp_unhash_endpoint(ep); 7610 sk->sk_state = SCTP_SS_CLOSED; 7611 if (sk->sk_reuse) 7612 sctp_sk(sk)->bind_hash->fastreuse = 1; 7613 goto out; 7614 } 7615 7616 /* If we are already listening, just update the backlog */ 7617 if (sctp_sstate(sk, LISTENING)) 7618 sk->sk_max_ack_backlog = backlog; 7619 else { 7620 err = sctp_listen_start(sk, backlog); 7621 if (err) 7622 goto out; 7623 } 7624 7625 err = 0; 7626 out: 7627 release_sock(sk); 7628 return err; 7629 } 7630 7631 /* 7632 * This function is done by modeling the current datagram_poll() and the 7633 * tcp_poll(). Note that, based on these implementations, we don't 7634 * lock the socket in this function, even though it seems that, 7635 * ideally, locking or some other mechanisms can be used to ensure 7636 * the integrity of the counters (sndbuf and wmem_alloc) used 7637 * in this place. We assume that we don't need locks either until proven 7638 * otherwise. 7639 * 7640 * Another thing to note is that we include the Async I/O support 7641 * here, again, by modeling the current TCP/UDP code. We don't have 7642 * a good way to test with it yet. 7643 */ 7644 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait) 7645 { 7646 struct sock *sk = sock->sk; 7647 struct sctp_sock *sp = sctp_sk(sk); 7648 __poll_t mask; 7649 7650 poll_wait(file, sk_sleep(sk), wait); 7651 7652 sock_rps_record_flow(sk); 7653 7654 /* A TCP-style listening socket becomes readable when the accept queue 7655 * is not empty. 7656 */ 7657 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 7658 return (!list_empty(&sp->ep->asocs)) ? 7659 (EPOLLIN | EPOLLRDNORM) : 0; 7660 7661 mask = 0; 7662 7663 /* Is there any exceptional events? */ 7664 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) 7665 mask |= EPOLLERR | 7666 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 7667 if (sk->sk_shutdown & RCV_SHUTDOWN) 7668 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; 7669 if (sk->sk_shutdown == SHUTDOWN_MASK) 7670 mask |= EPOLLHUP; 7671 7672 /* Is it readable? Reconsider this code with TCP-style support. */ 7673 if (!skb_queue_empty(&sk->sk_receive_queue)) 7674 mask |= EPOLLIN | EPOLLRDNORM; 7675 7676 /* The association is either gone or not ready. */ 7677 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) 7678 return mask; 7679 7680 /* Is it writable? */ 7681 if (sctp_writeable(sk)) { 7682 mask |= EPOLLOUT | EPOLLWRNORM; 7683 } else { 7684 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 7685 /* 7686 * Since the socket is not locked, the buffer 7687 * might be made available after the writeable check and 7688 * before the bit is set. This could cause a lost I/O 7689 * signal. tcp_poll() has a race breaker for this race 7690 * condition. Based on their implementation, we put 7691 * in the following code to cover it as well. 7692 */ 7693 if (sctp_writeable(sk)) 7694 mask |= EPOLLOUT | EPOLLWRNORM; 7695 } 7696 return mask; 7697 } 7698 7699 /******************************************************************** 7700 * 2nd Level Abstractions 7701 ********************************************************************/ 7702 7703 static struct sctp_bind_bucket *sctp_bucket_create( 7704 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) 7705 { 7706 struct sctp_bind_bucket *pp; 7707 7708 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); 7709 if (pp) { 7710 SCTP_DBG_OBJCNT_INC(bind_bucket); 7711 pp->port = snum; 7712 pp->fastreuse = 0; 7713 INIT_HLIST_HEAD(&pp->owner); 7714 pp->net = net; 7715 hlist_add_head(&pp->node, &head->chain); 7716 } 7717 return pp; 7718 } 7719 7720 /* Caller must hold hashbucket lock for this tb with local BH disabled */ 7721 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) 7722 { 7723 if (pp && hlist_empty(&pp->owner)) { 7724 __hlist_del(&pp->node); 7725 kmem_cache_free(sctp_bucket_cachep, pp); 7726 SCTP_DBG_OBJCNT_DEC(bind_bucket); 7727 } 7728 } 7729 7730 /* Release this socket's reference to a local port. */ 7731 static inline void __sctp_put_port(struct sock *sk) 7732 { 7733 struct sctp_bind_hashbucket *head = 7734 &sctp_port_hashtable[sctp_phashfn(sock_net(sk), 7735 inet_sk(sk)->inet_num)]; 7736 struct sctp_bind_bucket *pp; 7737 7738 spin_lock(&head->lock); 7739 pp = sctp_sk(sk)->bind_hash; 7740 __sk_del_bind_node(sk); 7741 sctp_sk(sk)->bind_hash = NULL; 7742 inet_sk(sk)->inet_num = 0; 7743 sctp_bucket_destroy(pp); 7744 spin_unlock(&head->lock); 7745 } 7746 7747 void sctp_put_port(struct sock *sk) 7748 { 7749 local_bh_disable(); 7750 __sctp_put_port(sk); 7751 local_bh_enable(); 7752 } 7753 7754 /* 7755 * The system picks an ephemeral port and choose an address set equivalent 7756 * to binding with a wildcard address. 7757 * One of those addresses will be the primary address for the association. 7758 * This automatically enables the multihoming capability of SCTP. 7759 */ 7760 static int sctp_autobind(struct sock *sk) 7761 { 7762 union sctp_addr autoaddr; 7763 struct sctp_af *af; 7764 __be16 port; 7765 7766 /* Initialize a local sockaddr structure to INADDR_ANY. */ 7767 af = sctp_sk(sk)->pf->af; 7768 7769 port = htons(inet_sk(sk)->inet_num); 7770 af->inaddr_any(&autoaddr, port); 7771 7772 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); 7773 } 7774 7775 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. 7776 * 7777 * From RFC 2292 7778 * 4.2 The cmsghdr Structure * 7779 * 7780 * When ancillary data is sent or received, any number of ancillary data 7781 * objects can be specified by the msg_control and msg_controllen members of 7782 * the msghdr structure, because each object is preceded by 7783 * a cmsghdr structure defining the object's length (the cmsg_len member). 7784 * Historically Berkeley-derived implementations have passed only one object 7785 * at a time, but this API allows multiple objects to be 7786 * passed in a single call to sendmsg() or recvmsg(). The following example 7787 * shows two ancillary data objects in a control buffer. 7788 * 7789 * |<--------------------------- msg_controllen -------------------------->| 7790 * | | 7791 * 7792 * |<----- ancillary data object ----->|<----- ancillary data object ----->| 7793 * 7794 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| 7795 * | | | 7796 * 7797 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | 7798 * 7799 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | 7800 * | | | | | 7801 * 7802 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 7803 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| 7804 * 7805 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| 7806 * 7807 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 7808 * ^ 7809 * | 7810 * 7811 * msg_control 7812 * points here 7813 */ 7814 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs) 7815 { 7816 struct msghdr *my_msg = (struct msghdr *)msg; 7817 struct cmsghdr *cmsg; 7818 7819 for_each_cmsghdr(cmsg, my_msg) { 7820 if (!CMSG_OK(my_msg, cmsg)) 7821 return -EINVAL; 7822 7823 /* Should we parse this header or ignore? */ 7824 if (cmsg->cmsg_level != IPPROTO_SCTP) 7825 continue; 7826 7827 /* Strictly check lengths following example in SCM code. */ 7828 switch (cmsg->cmsg_type) { 7829 case SCTP_INIT: 7830 /* SCTP Socket API Extension 7831 * 5.3.1 SCTP Initiation Structure (SCTP_INIT) 7832 * 7833 * This cmsghdr structure provides information for 7834 * initializing new SCTP associations with sendmsg(). 7835 * The SCTP_INITMSG socket option uses this same data 7836 * structure. This structure is not used for 7837 * recvmsg(). 7838 * 7839 * cmsg_level cmsg_type cmsg_data[] 7840 * ------------ ------------ ---------------------- 7841 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg 7842 */ 7843 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) 7844 return -EINVAL; 7845 7846 cmsgs->init = CMSG_DATA(cmsg); 7847 break; 7848 7849 case SCTP_SNDRCV: 7850 /* SCTP Socket API Extension 7851 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) 7852 * 7853 * This cmsghdr structure specifies SCTP options for 7854 * sendmsg() and describes SCTP header information 7855 * about a received message through recvmsg(). 7856 * 7857 * cmsg_level cmsg_type cmsg_data[] 7858 * ------------ ------------ ---------------------- 7859 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo 7860 */ 7861 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) 7862 return -EINVAL; 7863 7864 cmsgs->srinfo = CMSG_DATA(cmsg); 7865 7866 if (cmsgs->srinfo->sinfo_flags & 7867 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 7868 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 7869 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 7870 return -EINVAL; 7871 break; 7872 7873 case SCTP_SNDINFO: 7874 /* SCTP Socket API Extension 7875 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) 7876 * 7877 * This cmsghdr structure specifies SCTP options for 7878 * sendmsg(). This structure and SCTP_RCVINFO replaces 7879 * SCTP_SNDRCV which has been deprecated. 7880 * 7881 * cmsg_level cmsg_type cmsg_data[] 7882 * ------------ ------------ --------------------- 7883 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo 7884 */ 7885 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) 7886 return -EINVAL; 7887 7888 cmsgs->sinfo = CMSG_DATA(cmsg); 7889 7890 if (cmsgs->sinfo->snd_flags & 7891 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 7892 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 7893 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 7894 return -EINVAL; 7895 break; 7896 case SCTP_PRINFO: 7897 /* SCTP Socket API Extension 7898 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO) 7899 * 7900 * This cmsghdr structure specifies SCTP options for sendmsg(). 7901 * 7902 * cmsg_level cmsg_type cmsg_data[] 7903 * ------------ ------------ --------------------- 7904 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo 7905 */ 7906 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo))) 7907 return -EINVAL; 7908 7909 cmsgs->prinfo = CMSG_DATA(cmsg); 7910 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK) 7911 return -EINVAL; 7912 7913 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE) 7914 cmsgs->prinfo->pr_value = 0; 7915 break; 7916 case SCTP_AUTHINFO: 7917 /* SCTP Socket API Extension 7918 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO) 7919 * 7920 * This cmsghdr structure specifies SCTP options for sendmsg(). 7921 * 7922 * cmsg_level cmsg_type cmsg_data[] 7923 * ------------ ------------ --------------------- 7924 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo 7925 */ 7926 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo))) 7927 return -EINVAL; 7928 7929 cmsgs->authinfo = CMSG_DATA(cmsg); 7930 break; 7931 case SCTP_DSTADDRV4: 7932 case SCTP_DSTADDRV6: 7933 /* SCTP Socket API Extension 7934 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6) 7935 * 7936 * This cmsghdr structure specifies SCTP options for sendmsg(). 7937 * 7938 * cmsg_level cmsg_type cmsg_data[] 7939 * ------------ ------------ --------------------- 7940 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr 7941 * ------------ ------------ --------------------- 7942 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr 7943 */ 7944 cmsgs->addrs_msg = my_msg; 7945 break; 7946 default: 7947 return -EINVAL; 7948 } 7949 } 7950 7951 return 0; 7952 } 7953 7954 /* 7955 * Wait for a packet.. 7956 * Note: This function is the same function as in core/datagram.c 7957 * with a few modifications to make lksctp work. 7958 */ 7959 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p) 7960 { 7961 int error; 7962 DEFINE_WAIT(wait); 7963 7964 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 7965 7966 /* Socket errors? */ 7967 error = sock_error(sk); 7968 if (error) 7969 goto out; 7970 7971 if (!skb_queue_empty(&sk->sk_receive_queue)) 7972 goto ready; 7973 7974 /* Socket shut down? */ 7975 if (sk->sk_shutdown & RCV_SHUTDOWN) 7976 goto out; 7977 7978 /* Sequenced packets can come disconnected. If so we report the 7979 * problem. 7980 */ 7981 error = -ENOTCONN; 7982 7983 /* Is there a good reason to think that we may receive some data? */ 7984 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) 7985 goto out; 7986 7987 /* Handle signals. */ 7988 if (signal_pending(current)) 7989 goto interrupted; 7990 7991 /* Let another process have a go. Since we are going to sleep 7992 * anyway. Note: This may cause odd behaviors if the message 7993 * does not fit in the user's buffer, but this seems to be the 7994 * only way to honor MSG_DONTWAIT realistically. 7995 */ 7996 release_sock(sk); 7997 *timeo_p = schedule_timeout(*timeo_p); 7998 lock_sock(sk); 7999 8000 ready: 8001 finish_wait(sk_sleep(sk), &wait); 8002 return 0; 8003 8004 interrupted: 8005 error = sock_intr_errno(*timeo_p); 8006 8007 out: 8008 finish_wait(sk_sleep(sk), &wait); 8009 *err = error; 8010 return error; 8011 } 8012 8013 /* Receive a datagram. 8014 * Note: This is pretty much the same routine as in core/datagram.c 8015 * with a few changes to make lksctp work. 8016 */ 8017 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, 8018 int noblock, int *err) 8019 { 8020 int error; 8021 struct sk_buff *skb; 8022 long timeo; 8023 8024 timeo = sock_rcvtimeo(sk, noblock); 8025 8026 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, 8027 MAX_SCHEDULE_TIMEOUT); 8028 8029 do { 8030 /* Again only user level code calls this function, 8031 * so nothing interrupt level 8032 * will suddenly eat the receive_queue. 8033 * 8034 * Look at current nfs client by the way... 8035 * However, this function was correct in any case. 8) 8036 */ 8037 if (flags & MSG_PEEK) { 8038 skb = skb_peek(&sk->sk_receive_queue); 8039 if (skb) 8040 refcount_inc(&skb->users); 8041 } else { 8042 skb = __skb_dequeue(&sk->sk_receive_queue); 8043 } 8044 8045 if (skb) 8046 return skb; 8047 8048 /* Caller is allowed not to check sk->sk_err before calling. */ 8049 error = sock_error(sk); 8050 if (error) 8051 goto no_packet; 8052 8053 if (sk->sk_shutdown & RCV_SHUTDOWN) 8054 break; 8055 8056 if (sk_can_busy_loop(sk)) { 8057 sk_busy_loop(sk, noblock); 8058 8059 if (!skb_queue_empty(&sk->sk_receive_queue)) 8060 continue; 8061 } 8062 8063 /* User doesn't want to wait. */ 8064 error = -EAGAIN; 8065 if (!timeo) 8066 goto no_packet; 8067 } while (sctp_wait_for_packet(sk, err, &timeo) == 0); 8068 8069 return NULL; 8070 8071 no_packet: 8072 *err = error; 8073 return NULL; 8074 } 8075 8076 /* If sndbuf has changed, wake up per association sndbuf waiters. */ 8077 static void __sctp_write_space(struct sctp_association *asoc) 8078 { 8079 struct sock *sk = asoc->base.sk; 8080 8081 if (sctp_wspace(asoc) <= 0) 8082 return; 8083 8084 if (waitqueue_active(&asoc->wait)) 8085 wake_up_interruptible(&asoc->wait); 8086 8087 if (sctp_writeable(sk)) { 8088 struct socket_wq *wq; 8089 8090 rcu_read_lock(); 8091 wq = rcu_dereference(sk->sk_wq); 8092 if (wq) { 8093 if (waitqueue_active(&wq->wait)) 8094 wake_up_interruptible(&wq->wait); 8095 8096 /* Note that we try to include the Async I/O support 8097 * here by modeling from the current TCP/UDP code. 8098 * We have not tested with it yet. 8099 */ 8100 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 8101 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); 8102 } 8103 rcu_read_unlock(); 8104 } 8105 } 8106 8107 static void sctp_wake_up_waiters(struct sock *sk, 8108 struct sctp_association *asoc) 8109 { 8110 struct sctp_association *tmp = asoc; 8111 8112 /* We do accounting for the sndbuf space per association, 8113 * so we only need to wake our own association. 8114 */ 8115 if (asoc->ep->sndbuf_policy) 8116 return __sctp_write_space(asoc); 8117 8118 /* If association goes down and is just flushing its 8119 * outq, then just normally notify others. 8120 */ 8121 if (asoc->base.dead) 8122 return sctp_write_space(sk); 8123 8124 /* Accounting for the sndbuf space is per socket, so we 8125 * need to wake up others, try to be fair and in case of 8126 * other associations, let them have a go first instead 8127 * of just doing a sctp_write_space() call. 8128 * 8129 * Note that we reach sctp_wake_up_waiters() only when 8130 * associations free up queued chunks, thus we are under 8131 * lock and the list of associations on a socket is 8132 * guaranteed not to change. 8133 */ 8134 for (tmp = list_next_entry(tmp, asocs); 1; 8135 tmp = list_next_entry(tmp, asocs)) { 8136 /* Manually skip the head element. */ 8137 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) 8138 continue; 8139 /* Wake up association. */ 8140 __sctp_write_space(tmp); 8141 /* We've reached the end. */ 8142 if (tmp == asoc) 8143 break; 8144 } 8145 } 8146 8147 /* Do accounting for the sndbuf space. 8148 * Decrement the used sndbuf space of the corresponding association by the 8149 * data size which was just transmitted(freed). 8150 */ 8151 static void sctp_wfree(struct sk_buff *skb) 8152 { 8153 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; 8154 struct sctp_association *asoc = chunk->asoc; 8155 struct sock *sk = asoc->base.sk; 8156 8157 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + 8158 sizeof(struct sk_buff) + 8159 sizeof(struct sctp_chunk); 8160 8161 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc)); 8162 8163 /* 8164 * This undoes what is done via sctp_set_owner_w and sk_mem_charge 8165 */ 8166 sk->sk_wmem_queued -= skb->truesize; 8167 sk_mem_uncharge(sk, skb->truesize); 8168 8169 if (chunk->shkey) { 8170 struct sctp_shared_key *shkey = chunk->shkey; 8171 8172 /* refcnt == 2 and !list_empty mean after this release, it's 8173 * not being used anywhere, and it's time to notify userland 8174 * that this shkey can be freed if it's been deactivated. 8175 */ 8176 if (shkey->deactivated && !list_empty(&shkey->key_list) && 8177 refcount_read(&shkey->refcnt) == 2) { 8178 struct sctp_ulpevent *ev; 8179 8180 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id, 8181 SCTP_AUTH_FREE_KEY, 8182 GFP_KERNEL); 8183 if (ev) 8184 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 8185 } 8186 sctp_auth_shkey_release(chunk->shkey); 8187 } 8188 8189 sock_wfree(skb); 8190 sctp_wake_up_waiters(sk, asoc); 8191 8192 sctp_association_put(asoc); 8193 } 8194 8195 /* Do accounting for the receive space on the socket. 8196 * Accounting for the association is done in ulpevent.c 8197 * We set this as a destructor for the cloned data skbs so that 8198 * accounting is done at the correct time. 8199 */ 8200 void sctp_sock_rfree(struct sk_buff *skb) 8201 { 8202 struct sock *sk = skb->sk; 8203 struct sctp_ulpevent *event = sctp_skb2event(skb); 8204 8205 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); 8206 8207 /* 8208 * Mimic the behavior of sock_rfree 8209 */ 8210 sk_mem_uncharge(sk, event->rmem_len); 8211 } 8212 8213 8214 /* Helper function to wait for space in the sndbuf. */ 8215 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, 8216 size_t msg_len) 8217 { 8218 struct sock *sk = asoc->base.sk; 8219 long current_timeo = *timeo_p; 8220 DEFINE_WAIT(wait); 8221 int err = 0; 8222 8223 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, 8224 *timeo_p, msg_len); 8225 8226 /* Increment the association's refcnt. */ 8227 sctp_association_hold(asoc); 8228 8229 /* Wait on the association specific sndbuf space. */ 8230 for (;;) { 8231 prepare_to_wait_exclusive(&asoc->wait, &wait, 8232 TASK_INTERRUPTIBLE); 8233 if (asoc->base.dead) 8234 goto do_dead; 8235 if (!*timeo_p) 8236 goto do_nonblock; 8237 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) 8238 goto do_error; 8239 if (signal_pending(current)) 8240 goto do_interrupted; 8241 if (msg_len <= sctp_wspace(asoc)) 8242 break; 8243 8244 /* Let another process have a go. Since we are going 8245 * to sleep anyway. 8246 */ 8247 release_sock(sk); 8248 current_timeo = schedule_timeout(current_timeo); 8249 lock_sock(sk); 8250 if (sk != asoc->base.sk) 8251 goto do_error; 8252 8253 *timeo_p = current_timeo; 8254 } 8255 8256 out: 8257 finish_wait(&asoc->wait, &wait); 8258 8259 /* Release the association's refcnt. */ 8260 sctp_association_put(asoc); 8261 8262 return err; 8263 8264 do_dead: 8265 err = -ESRCH; 8266 goto out; 8267 8268 do_error: 8269 err = -EPIPE; 8270 goto out; 8271 8272 do_interrupted: 8273 err = sock_intr_errno(*timeo_p); 8274 goto out; 8275 8276 do_nonblock: 8277 err = -EAGAIN; 8278 goto out; 8279 } 8280 8281 void sctp_data_ready(struct sock *sk) 8282 { 8283 struct socket_wq *wq; 8284 8285 rcu_read_lock(); 8286 wq = rcu_dereference(sk->sk_wq); 8287 if (skwq_has_sleeper(wq)) 8288 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | 8289 EPOLLRDNORM | EPOLLRDBAND); 8290 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 8291 rcu_read_unlock(); 8292 } 8293 8294 /* If socket sndbuf has changed, wake up all per association waiters. */ 8295 void sctp_write_space(struct sock *sk) 8296 { 8297 struct sctp_association *asoc; 8298 8299 /* Wake up the tasks in each wait queue. */ 8300 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { 8301 __sctp_write_space(asoc); 8302 } 8303 } 8304 8305 /* Is there any sndbuf space available on the socket? 8306 * 8307 * Note that sk_wmem_alloc is the sum of the send buffers on all of the 8308 * associations on the same socket. For a UDP-style socket with 8309 * multiple associations, it is possible for it to be "unwriteable" 8310 * prematurely. I assume that this is acceptable because 8311 * a premature "unwriteable" is better than an accidental "writeable" which 8312 * would cause an unwanted block under certain circumstances. For the 1-1 8313 * UDP-style sockets or TCP-style sockets, this code should work. 8314 * - Daisy 8315 */ 8316 static int sctp_writeable(struct sock *sk) 8317 { 8318 int amt = 0; 8319 8320 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 8321 if (amt < 0) 8322 amt = 0; 8323 return amt; 8324 } 8325 8326 /* Wait for an association to go into ESTABLISHED state. If timeout is 0, 8327 * returns immediately with EINPROGRESS. 8328 */ 8329 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) 8330 { 8331 struct sock *sk = asoc->base.sk; 8332 int err = 0; 8333 long current_timeo = *timeo_p; 8334 DEFINE_WAIT(wait); 8335 8336 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); 8337 8338 /* Increment the association's refcnt. */ 8339 sctp_association_hold(asoc); 8340 8341 for (;;) { 8342 prepare_to_wait_exclusive(&asoc->wait, &wait, 8343 TASK_INTERRUPTIBLE); 8344 if (!*timeo_p) 8345 goto do_nonblock; 8346 if (sk->sk_shutdown & RCV_SHUTDOWN) 8347 break; 8348 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || 8349 asoc->base.dead) 8350 goto do_error; 8351 if (signal_pending(current)) 8352 goto do_interrupted; 8353 8354 if (sctp_state(asoc, ESTABLISHED)) 8355 break; 8356 8357 /* Let another process have a go. Since we are going 8358 * to sleep anyway. 8359 */ 8360 release_sock(sk); 8361 current_timeo = schedule_timeout(current_timeo); 8362 lock_sock(sk); 8363 8364 *timeo_p = current_timeo; 8365 } 8366 8367 out: 8368 finish_wait(&asoc->wait, &wait); 8369 8370 /* Release the association's refcnt. */ 8371 sctp_association_put(asoc); 8372 8373 return err; 8374 8375 do_error: 8376 if (asoc->init_err_counter + 1 > asoc->max_init_attempts) 8377 err = -ETIMEDOUT; 8378 else 8379 err = -ECONNREFUSED; 8380 goto out; 8381 8382 do_interrupted: 8383 err = sock_intr_errno(*timeo_p); 8384 goto out; 8385 8386 do_nonblock: 8387 err = -EINPROGRESS; 8388 goto out; 8389 } 8390 8391 static int sctp_wait_for_accept(struct sock *sk, long timeo) 8392 { 8393 struct sctp_endpoint *ep; 8394 int err = 0; 8395 DEFINE_WAIT(wait); 8396 8397 ep = sctp_sk(sk)->ep; 8398 8399 8400 for (;;) { 8401 prepare_to_wait_exclusive(sk_sleep(sk), &wait, 8402 TASK_INTERRUPTIBLE); 8403 8404 if (list_empty(&ep->asocs)) { 8405 release_sock(sk); 8406 timeo = schedule_timeout(timeo); 8407 lock_sock(sk); 8408 } 8409 8410 err = -EINVAL; 8411 if (!sctp_sstate(sk, LISTENING)) 8412 break; 8413 8414 err = 0; 8415 if (!list_empty(&ep->asocs)) 8416 break; 8417 8418 err = sock_intr_errno(timeo); 8419 if (signal_pending(current)) 8420 break; 8421 8422 err = -EAGAIN; 8423 if (!timeo) 8424 break; 8425 } 8426 8427 finish_wait(sk_sleep(sk), &wait); 8428 8429 return err; 8430 } 8431 8432 static void sctp_wait_for_close(struct sock *sk, long timeout) 8433 { 8434 DEFINE_WAIT(wait); 8435 8436 do { 8437 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 8438 if (list_empty(&sctp_sk(sk)->ep->asocs)) 8439 break; 8440 release_sock(sk); 8441 timeout = schedule_timeout(timeout); 8442 lock_sock(sk); 8443 } while (!signal_pending(current) && timeout); 8444 8445 finish_wait(sk_sleep(sk), &wait); 8446 } 8447 8448 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) 8449 { 8450 struct sk_buff *frag; 8451 8452 if (!skb->data_len) 8453 goto done; 8454 8455 /* Don't forget the fragments. */ 8456 skb_walk_frags(skb, frag) 8457 sctp_skb_set_owner_r_frag(frag, sk); 8458 8459 done: 8460 sctp_skb_set_owner_r(skb, sk); 8461 } 8462 8463 void sctp_copy_sock(struct sock *newsk, struct sock *sk, 8464 struct sctp_association *asoc) 8465 { 8466 struct inet_sock *inet = inet_sk(sk); 8467 struct inet_sock *newinet; 8468 8469 newsk->sk_type = sk->sk_type; 8470 newsk->sk_bound_dev_if = sk->sk_bound_dev_if; 8471 newsk->sk_flags = sk->sk_flags; 8472 newsk->sk_tsflags = sk->sk_tsflags; 8473 newsk->sk_no_check_tx = sk->sk_no_check_tx; 8474 newsk->sk_no_check_rx = sk->sk_no_check_rx; 8475 newsk->sk_reuse = sk->sk_reuse; 8476 8477 newsk->sk_shutdown = sk->sk_shutdown; 8478 newsk->sk_destruct = sctp_destruct_sock; 8479 newsk->sk_family = sk->sk_family; 8480 newsk->sk_protocol = IPPROTO_SCTP; 8481 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 8482 newsk->sk_sndbuf = sk->sk_sndbuf; 8483 newsk->sk_rcvbuf = sk->sk_rcvbuf; 8484 newsk->sk_lingertime = sk->sk_lingertime; 8485 newsk->sk_rcvtimeo = sk->sk_rcvtimeo; 8486 newsk->sk_sndtimeo = sk->sk_sndtimeo; 8487 newsk->sk_rxhash = sk->sk_rxhash; 8488 8489 newinet = inet_sk(newsk); 8490 8491 /* Initialize sk's sport, dport, rcv_saddr and daddr for 8492 * getsockname() and getpeername() 8493 */ 8494 newinet->inet_sport = inet->inet_sport; 8495 newinet->inet_saddr = inet->inet_saddr; 8496 newinet->inet_rcv_saddr = inet->inet_rcv_saddr; 8497 newinet->inet_dport = htons(asoc->peer.port); 8498 newinet->pmtudisc = inet->pmtudisc; 8499 newinet->inet_id = asoc->next_tsn ^ jiffies; 8500 8501 newinet->uc_ttl = inet->uc_ttl; 8502 newinet->mc_loop = 1; 8503 newinet->mc_ttl = 1; 8504 newinet->mc_index = 0; 8505 newinet->mc_list = NULL; 8506 8507 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP) 8508 net_enable_timestamp(); 8509 8510 security_sk_clone(sk, newsk); 8511 } 8512 8513 static inline void sctp_copy_descendant(struct sock *sk_to, 8514 const struct sock *sk_from) 8515 { 8516 int ancestor_size = sizeof(struct inet_sock) + 8517 sizeof(struct sctp_sock) - 8518 offsetof(struct sctp_sock, auto_asconf_list); 8519 8520 if (sk_from->sk_family == PF_INET6) 8521 ancestor_size += sizeof(struct ipv6_pinfo); 8522 8523 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size); 8524 } 8525 8526 /* Populate the fields of the newsk from the oldsk and migrate the assoc 8527 * and its messages to the newsk. 8528 */ 8529 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 8530 struct sctp_association *assoc, 8531 enum sctp_socket_type type) 8532 { 8533 struct sctp_sock *oldsp = sctp_sk(oldsk); 8534 struct sctp_sock *newsp = sctp_sk(newsk); 8535 struct sctp_bind_bucket *pp; /* hash list port iterator */ 8536 struct sctp_endpoint *newep = newsp->ep; 8537 struct sk_buff *skb, *tmp; 8538 struct sctp_ulpevent *event; 8539 struct sctp_bind_hashbucket *head; 8540 8541 /* Migrate socket buffer sizes and all the socket level options to the 8542 * new socket. 8543 */ 8544 newsk->sk_sndbuf = oldsk->sk_sndbuf; 8545 newsk->sk_rcvbuf = oldsk->sk_rcvbuf; 8546 /* Brute force copy old sctp opt. */ 8547 sctp_copy_descendant(newsk, oldsk); 8548 8549 /* Restore the ep value that was overwritten with the above structure 8550 * copy. 8551 */ 8552 newsp->ep = newep; 8553 newsp->hmac = NULL; 8554 8555 /* Hook this new socket in to the bind_hash list. */ 8556 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), 8557 inet_sk(oldsk)->inet_num)]; 8558 spin_lock_bh(&head->lock); 8559 pp = sctp_sk(oldsk)->bind_hash; 8560 sk_add_bind_node(newsk, &pp->owner); 8561 sctp_sk(newsk)->bind_hash = pp; 8562 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; 8563 spin_unlock_bh(&head->lock); 8564 8565 /* Copy the bind_addr list from the original endpoint to the new 8566 * endpoint so that we can handle restarts properly 8567 */ 8568 sctp_bind_addr_dup(&newsp->ep->base.bind_addr, 8569 &oldsp->ep->base.bind_addr, GFP_KERNEL); 8570 8571 /* Move any messages in the old socket's receive queue that are for the 8572 * peeled off association to the new socket's receive queue. 8573 */ 8574 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { 8575 event = sctp_skb2event(skb); 8576 if (event->asoc == assoc) { 8577 __skb_unlink(skb, &oldsk->sk_receive_queue); 8578 __skb_queue_tail(&newsk->sk_receive_queue, skb); 8579 sctp_skb_set_owner_r_frag(skb, newsk); 8580 } 8581 } 8582 8583 /* Clean up any messages pending delivery due to partial 8584 * delivery. Three cases: 8585 * 1) No partial deliver; no work. 8586 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. 8587 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. 8588 */ 8589 skb_queue_head_init(&newsp->pd_lobby); 8590 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); 8591 8592 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { 8593 struct sk_buff_head *queue; 8594 8595 /* Decide which queue to move pd_lobby skbs to. */ 8596 if (assoc->ulpq.pd_mode) { 8597 queue = &newsp->pd_lobby; 8598 } else 8599 queue = &newsk->sk_receive_queue; 8600 8601 /* Walk through the pd_lobby, looking for skbs that 8602 * need moved to the new socket. 8603 */ 8604 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { 8605 event = sctp_skb2event(skb); 8606 if (event->asoc == assoc) { 8607 __skb_unlink(skb, &oldsp->pd_lobby); 8608 __skb_queue_tail(queue, skb); 8609 sctp_skb_set_owner_r_frag(skb, newsk); 8610 } 8611 } 8612 8613 /* Clear up any skbs waiting for the partial 8614 * delivery to finish. 8615 */ 8616 if (assoc->ulpq.pd_mode) 8617 sctp_clear_pd(oldsk, NULL); 8618 8619 } 8620 8621 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag); 8622 8623 /* Set the type of socket to indicate that it is peeled off from the 8624 * original UDP-style socket or created with the accept() call on a 8625 * TCP-style socket.. 8626 */ 8627 newsp->type = type; 8628 8629 /* Mark the new socket "in-use" by the user so that any packets 8630 * that may arrive on the association after we've moved it are 8631 * queued to the backlog. This prevents a potential race between 8632 * backlog processing on the old socket and new-packet processing 8633 * on the new socket. 8634 * 8635 * The caller has just allocated newsk so we can guarantee that other 8636 * paths won't try to lock it and then oldsk. 8637 */ 8638 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 8639 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w); 8640 sctp_assoc_migrate(assoc, newsk); 8641 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w); 8642 8643 /* If the association on the newsk is already closed before accept() 8644 * is called, set RCV_SHUTDOWN flag. 8645 */ 8646 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { 8647 inet_sk_set_state(newsk, SCTP_SS_CLOSED); 8648 newsk->sk_shutdown |= RCV_SHUTDOWN; 8649 } else { 8650 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED); 8651 } 8652 8653 release_sock(newsk); 8654 } 8655 8656 8657 /* This proto struct describes the ULP interface for SCTP. */ 8658 struct proto sctp_prot = { 8659 .name = "SCTP", 8660 .owner = THIS_MODULE, 8661 .close = sctp_close, 8662 .connect = sctp_connect, 8663 .disconnect = sctp_disconnect, 8664 .accept = sctp_accept, 8665 .ioctl = sctp_ioctl, 8666 .init = sctp_init_sock, 8667 .destroy = sctp_destroy_sock, 8668 .shutdown = sctp_shutdown, 8669 .setsockopt = sctp_setsockopt, 8670 .getsockopt = sctp_getsockopt, 8671 .sendmsg = sctp_sendmsg, 8672 .recvmsg = sctp_recvmsg, 8673 .bind = sctp_bind, 8674 .backlog_rcv = sctp_backlog_rcv, 8675 .hash = sctp_hash, 8676 .unhash = sctp_unhash, 8677 .get_port = sctp_get_port, 8678 .obj_size = sizeof(struct sctp_sock), 8679 .useroffset = offsetof(struct sctp_sock, subscribe), 8680 .usersize = offsetof(struct sctp_sock, initmsg) - 8681 offsetof(struct sctp_sock, subscribe) + 8682 sizeof_field(struct sctp_sock, initmsg), 8683 .sysctl_mem = sysctl_sctp_mem, 8684 .sysctl_rmem = sysctl_sctp_rmem, 8685 .sysctl_wmem = sysctl_sctp_wmem, 8686 .memory_pressure = &sctp_memory_pressure, 8687 .enter_memory_pressure = sctp_enter_memory_pressure, 8688 .memory_allocated = &sctp_memory_allocated, 8689 .sockets_allocated = &sctp_sockets_allocated, 8690 }; 8691 8692 #if IS_ENABLED(CONFIG_IPV6) 8693 8694 #include <net/transp_v6.h> 8695 static void sctp_v6_destroy_sock(struct sock *sk) 8696 { 8697 sctp_destroy_sock(sk); 8698 inet6_destroy_sock(sk); 8699 } 8700 8701 struct proto sctpv6_prot = { 8702 .name = "SCTPv6", 8703 .owner = THIS_MODULE, 8704 .close = sctp_close, 8705 .connect = sctp_connect, 8706 .disconnect = sctp_disconnect, 8707 .accept = sctp_accept, 8708 .ioctl = sctp_ioctl, 8709 .init = sctp_init_sock, 8710 .destroy = sctp_v6_destroy_sock, 8711 .shutdown = sctp_shutdown, 8712 .setsockopt = sctp_setsockopt, 8713 .getsockopt = sctp_getsockopt, 8714 .sendmsg = sctp_sendmsg, 8715 .recvmsg = sctp_recvmsg, 8716 .bind = sctp_bind, 8717 .backlog_rcv = sctp_backlog_rcv, 8718 .hash = sctp_hash, 8719 .unhash = sctp_unhash, 8720 .get_port = sctp_get_port, 8721 .obj_size = sizeof(struct sctp6_sock), 8722 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), 8723 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - 8724 offsetof(struct sctp6_sock, sctp.subscribe) + 8725 sizeof_field(struct sctp6_sock, sctp.initmsg), 8726 .sysctl_mem = sysctl_sctp_mem, 8727 .sysctl_rmem = sysctl_sctp_rmem, 8728 .sysctl_wmem = sysctl_sctp_wmem, 8729 .memory_pressure = &sctp_memory_pressure, 8730 .enter_memory_pressure = sctp_enter_memory_pressure, 8731 .memory_allocated = &sctp_memory_allocated, 8732 .sockets_allocated = &sctp_sockets_allocated, 8733 }; 8734 #endif /* IS_ENABLED(CONFIG_IPV6) */ 8735