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