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