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