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