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 struct net *net, int *pos, void *p) { 5373 int err, hash = 0, idx = 0, start; 5374 struct sctp_hashbucket *head; 5375 struct sctp_endpoint *ep; 5376 5377 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; 5378 hash++, head++) { 5379 start = idx; 5380 again: 5381 read_lock_bh(&head->lock); 5382 sctp_for_each_hentry(ep, &head->chain) { 5383 if (sock_net(ep->base.sk) != net) 5384 continue; 5385 if (idx++ >= *pos) { 5386 sctp_endpoint_hold(ep); 5387 break; 5388 } 5389 } 5390 read_unlock_bh(&head->lock); 5391 5392 if (ep) { 5393 err = cb(ep, p); 5394 sctp_endpoint_put(ep); 5395 if (err) 5396 return err; 5397 (*pos)++; 5398 5399 idx = start; 5400 goto again; 5401 } 5402 } 5403 5404 return 0; 5405 } 5406 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); 5407 5408 int sctp_transport_lookup_process(sctp_callback_t cb, struct net *net, 5409 const union sctp_addr *laddr, 5410 const union sctp_addr *paddr, void *p, int dif) 5411 { 5412 struct sctp_transport *transport; 5413 struct sctp_endpoint *ep; 5414 int err = -ENOENT; 5415 5416 rcu_read_lock(); 5417 transport = sctp_addrs_lookup_transport(net, laddr, paddr, dif, dif); 5418 if (!transport) { 5419 rcu_read_unlock(); 5420 return err; 5421 } 5422 ep = transport->asoc->ep; 5423 if (!sctp_endpoint_hold(ep)) { /* asoc can be peeled off */ 5424 sctp_transport_put(transport); 5425 rcu_read_unlock(); 5426 return err; 5427 } 5428 rcu_read_unlock(); 5429 5430 err = cb(ep, transport, p); 5431 sctp_endpoint_put(ep); 5432 sctp_transport_put(transport); 5433 return err; 5434 } 5435 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); 5436 5437 int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done, 5438 struct net *net, int *pos, void *p) 5439 { 5440 struct rhashtable_iter hti; 5441 struct sctp_transport *tsp; 5442 struct sctp_endpoint *ep; 5443 int ret; 5444 5445 again: 5446 ret = 0; 5447 sctp_transport_walk_start(&hti); 5448 5449 tsp = sctp_transport_get_idx(net, &hti, *pos + 1); 5450 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) { 5451 ep = tsp->asoc->ep; 5452 if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */ 5453 ret = cb(ep, tsp, p); 5454 if (ret) 5455 break; 5456 sctp_endpoint_put(ep); 5457 } 5458 (*pos)++; 5459 sctp_transport_put(tsp); 5460 } 5461 sctp_transport_walk_stop(&hti); 5462 5463 if (ret) { 5464 if (cb_done && !cb_done(ep, tsp, p)) { 5465 (*pos)++; 5466 sctp_endpoint_put(ep); 5467 sctp_transport_put(tsp); 5468 goto again; 5469 } 5470 sctp_endpoint_put(ep); 5471 sctp_transport_put(tsp); 5472 } 5473 5474 return ret; 5475 } 5476 EXPORT_SYMBOL_GPL(sctp_transport_traverse_process); 5477 5478 /* 7.2.1 Association Status (SCTP_STATUS) 5479 5480 * Applications can retrieve current status information about an 5481 * association, including association state, peer receiver window size, 5482 * number of unacked data chunks, and number of data chunks pending 5483 * receipt. This information is read-only. 5484 */ 5485 static int sctp_getsockopt_sctp_status(struct sock *sk, int len, 5486 char __user *optval, 5487 int __user *optlen) 5488 { 5489 struct sctp_status status; 5490 struct sctp_association *asoc = NULL; 5491 struct sctp_transport *transport; 5492 sctp_assoc_t associd; 5493 int retval = 0; 5494 5495 if (len < sizeof(status)) { 5496 retval = -EINVAL; 5497 goto out; 5498 } 5499 5500 len = sizeof(status); 5501 if (copy_from_user(&status, optval, len)) { 5502 retval = -EFAULT; 5503 goto out; 5504 } 5505 5506 associd = status.sstat_assoc_id; 5507 asoc = sctp_id2assoc(sk, associd); 5508 if (!asoc) { 5509 retval = -EINVAL; 5510 goto out; 5511 } 5512 5513 transport = asoc->peer.primary_path; 5514 5515 status.sstat_assoc_id = sctp_assoc2id(asoc); 5516 status.sstat_state = sctp_assoc_to_state(asoc); 5517 status.sstat_rwnd = asoc->peer.rwnd; 5518 status.sstat_unackdata = asoc->unack_data; 5519 5520 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 5521 status.sstat_instrms = asoc->stream.incnt; 5522 status.sstat_outstrms = asoc->stream.outcnt; 5523 status.sstat_fragmentation_point = asoc->frag_point; 5524 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 5525 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, 5526 transport->af_specific->sockaddr_len); 5527 /* Map ipv4 address into v4-mapped-on-v6 address. */ 5528 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), 5529 (union sctp_addr *)&status.sstat_primary.spinfo_address); 5530 status.sstat_primary.spinfo_state = transport->state; 5531 status.sstat_primary.spinfo_cwnd = transport->cwnd; 5532 status.sstat_primary.spinfo_srtt = transport->srtt; 5533 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); 5534 status.sstat_primary.spinfo_mtu = transport->pathmtu; 5535 5536 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) 5537 status.sstat_primary.spinfo_state = SCTP_ACTIVE; 5538 5539 if (put_user(len, optlen)) { 5540 retval = -EFAULT; 5541 goto out; 5542 } 5543 5544 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", 5545 __func__, len, status.sstat_state, status.sstat_rwnd, 5546 status.sstat_assoc_id); 5547 5548 if (copy_to_user(optval, &status, len)) { 5549 retval = -EFAULT; 5550 goto out; 5551 } 5552 5553 out: 5554 return retval; 5555 } 5556 5557 5558 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 5559 * 5560 * Applications can retrieve information about a specific peer address 5561 * of an association, including its reachability state, congestion 5562 * window, and retransmission timer values. This information is 5563 * read-only. 5564 */ 5565 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, 5566 char __user *optval, 5567 int __user *optlen) 5568 { 5569 struct sctp_paddrinfo pinfo; 5570 struct sctp_transport *transport; 5571 int retval = 0; 5572 5573 if (len < sizeof(pinfo)) { 5574 retval = -EINVAL; 5575 goto out; 5576 } 5577 5578 len = sizeof(pinfo); 5579 if (copy_from_user(&pinfo, optval, len)) { 5580 retval = -EFAULT; 5581 goto out; 5582 } 5583 5584 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, 5585 pinfo.spinfo_assoc_id); 5586 if (!transport) { 5587 retval = -EINVAL; 5588 goto out; 5589 } 5590 5591 if (transport->state == SCTP_PF && 5592 transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) { 5593 retval = -EACCES; 5594 goto out; 5595 } 5596 5597 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 5598 pinfo.spinfo_state = transport->state; 5599 pinfo.spinfo_cwnd = transport->cwnd; 5600 pinfo.spinfo_srtt = transport->srtt; 5601 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); 5602 pinfo.spinfo_mtu = transport->pathmtu; 5603 5604 if (pinfo.spinfo_state == SCTP_UNKNOWN) 5605 pinfo.spinfo_state = SCTP_ACTIVE; 5606 5607 if (put_user(len, optlen)) { 5608 retval = -EFAULT; 5609 goto out; 5610 } 5611 5612 if (copy_to_user(optval, &pinfo, len)) { 5613 retval = -EFAULT; 5614 goto out; 5615 } 5616 5617 out: 5618 return retval; 5619 } 5620 5621 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 5622 * 5623 * This option is a on/off flag. If enabled no SCTP message 5624 * fragmentation will be performed. Instead if a message being sent 5625 * exceeds the current PMTU size, the message will NOT be sent and 5626 * instead a error will be indicated to the user. 5627 */ 5628 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, 5629 char __user *optval, int __user *optlen) 5630 { 5631 int val; 5632 5633 if (len < sizeof(int)) 5634 return -EINVAL; 5635 5636 len = sizeof(int); 5637 val = (sctp_sk(sk)->disable_fragments == 1); 5638 if (put_user(len, optlen)) 5639 return -EFAULT; 5640 if (copy_to_user(optval, &val, len)) 5641 return -EFAULT; 5642 return 0; 5643 } 5644 5645 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) 5646 * 5647 * This socket option is used to specify various notifications and 5648 * ancillary data the user wishes to receive. 5649 */ 5650 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, 5651 int __user *optlen) 5652 { 5653 struct sctp_event_subscribe subscribe; 5654 __u8 *sn_type = (__u8 *)&subscribe; 5655 int i; 5656 5657 if (len == 0) 5658 return -EINVAL; 5659 if (len > sizeof(struct sctp_event_subscribe)) 5660 len = sizeof(struct sctp_event_subscribe); 5661 if (put_user(len, optlen)) 5662 return -EFAULT; 5663 5664 for (i = 0; i < len; i++) 5665 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe, 5666 SCTP_SN_TYPE_BASE + i); 5667 5668 if (copy_to_user(optval, &subscribe, len)) 5669 return -EFAULT; 5670 5671 return 0; 5672 } 5673 5674 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 5675 * 5676 * This socket option is applicable to the UDP-style socket only. When 5677 * set it will cause associations that are idle for more than the 5678 * specified number of seconds to automatically close. An association 5679 * being idle is defined an association that has NOT sent or received 5680 * user data. The special value of '0' indicates that no automatic 5681 * close of any associations should be performed. The option expects an 5682 * integer defining the number of seconds of idle time before an 5683 * association is closed. 5684 */ 5685 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) 5686 { 5687 /* Applicable to UDP-style socket only */ 5688 if (sctp_style(sk, TCP)) 5689 return -EOPNOTSUPP; 5690 if (len < sizeof(int)) 5691 return -EINVAL; 5692 len = sizeof(int); 5693 if (put_user(len, optlen)) 5694 return -EFAULT; 5695 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval)) 5696 return -EFAULT; 5697 return 0; 5698 } 5699 5700 /* Helper routine to branch off an association to a new socket. */ 5701 static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, 5702 struct socket **sockp) 5703 { 5704 struct sctp_association *asoc = sctp_id2assoc(sk, id); 5705 struct socket *sock; 5706 struct sock *newsk; 5707 int err = 0; 5708 5709 /* Do not peel off from one netns to another one. */ 5710 if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) 5711 return -EINVAL; 5712 5713 if (!asoc) 5714 return -EINVAL; 5715 5716 /* An association cannot be branched off from an already peeled-off 5717 * socket, nor is this supported for tcp style sockets. 5718 */ 5719 if (!sctp_style(sk, UDP)) 5720 return -EINVAL; 5721 5722 err = sock_create_lite(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); 5723 if (err) 5724 return err; 5725 5726 newsk = sctp_clone_sock(sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); 5727 if (IS_ERR(newsk)) { 5728 sock_release(sock); 5729 *sockp = NULL; 5730 return PTR_ERR(newsk); 5731 } 5732 5733 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 5734 __inet_accept(sk->sk_socket, sock, newsk); 5735 release_sock(newsk); 5736 5737 sock->ops = sk->sk_socket->ops; 5738 __module_get(sock->ops->owner); 5739 5740 *sockp = sock; 5741 5742 return err; 5743 } 5744 5745 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, 5746 struct file **newfile, unsigned flags) 5747 { 5748 struct socket *newsock; 5749 int retval; 5750 5751 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); 5752 if (retval < 0) 5753 goto out; 5754 5755 /* Map the socket to an unused fd that can be returned to the user. */ 5756 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); 5757 if (retval < 0) { 5758 sock_release(newsock); 5759 goto out; 5760 } 5761 5762 *newfile = sock_alloc_file(newsock, 0, NULL); 5763 if (IS_ERR(*newfile)) { 5764 put_unused_fd(retval); 5765 retval = PTR_ERR(*newfile); 5766 *newfile = NULL; 5767 return retval; 5768 } 5769 5770 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, 5771 retval); 5772 5773 peeloff->sd = retval; 5774 5775 if (flags & SOCK_NONBLOCK) 5776 (*newfile)->f_flags |= O_NONBLOCK; 5777 out: 5778 return retval; 5779 } 5780 5781 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) 5782 { 5783 sctp_peeloff_arg_t peeloff; 5784 struct file *newfile = NULL; 5785 int retval = 0; 5786 5787 if (len < sizeof(sctp_peeloff_arg_t)) 5788 return -EINVAL; 5789 len = sizeof(sctp_peeloff_arg_t); 5790 if (copy_from_user(&peeloff, optval, len)) 5791 return -EFAULT; 5792 5793 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); 5794 if (retval < 0) 5795 goto out; 5796 5797 /* Return the fd mapped to the new socket. */ 5798 if (put_user(len, optlen)) { 5799 fput(newfile); 5800 put_unused_fd(retval); 5801 return -EFAULT; 5802 } 5803 5804 if (copy_to_user(optval, &peeloff, len)) { 5805 fput(newfile); 5806 put_unused_fd(retval); 5807 return -EFAULT; 5808 } 5809 fd_install(retval, newfile); 5810 out: 5811 return retval; 5812 } 5813 5814 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, 5815 char __user *optval, int __user *optlen) 5816 { 5817 sctp_peeloff_flags_arg_t peeloff; 5818 struct file *newfile = NULL; 5819 int retval = 0; 5820 5821 if (len < sizeof(sctp_peeloff_flags_arg_t)) 5822 return -EINVAL; 5823 len = sizeof(sctp_peeloff_flags_arg_t); 5824 if (copy_from_user(&peeloff, optval, len)) 5825 return -EFAULT; 5826 5827 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, 5828 &newfile, peeloff.flags); 5829 if (retval < 0) 5830 goto out; 5831 5832 /* Return the fd mapped to the new socket. */ 5833 if (put_user(len, optlen)) { 5834 fput(newfile); 5835 put_unused_fd(retval); 5836 return -EFAULT; 5837 } 5838 5839 if (copy_to_user(optval, &peeloff, len)) { 5840 fput(newfile); 5841 put_unused_fd(retval); 5842 return -EFAULT; 5843 } 5844 fd_install(retval, newfile); 5845 out: 5846 return retval; 5847 } 5848 5849 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 5850 * 5851 * Applications can enable or disable heartbeats for any peer address of 5852 * an association, modify an address's heartbeat interval, force a 5853 * heartbeat to be sent immediately, and adjust the address's maximum 5854 * number of retransmissions sent before an address is considered 5855 * unreachable. The following structure is used to access and modify an 5856 * address's parameters: 5857 * 5858 * struct sctp_paddrparams { 5859 * sctp_assoc_t spp_assoc_id; 5860 * struct sockaddr_storage spp_address; 5861 * uint32_t spp_hbinterval; 5862 * uint16_t spp_pathmaxrxt; 5863 * uint32_t spp_pathmtu; 5864 * uint32_t spp_sackdelay; 5865 * uint32_t spp_flags; 5866 * }; 5867 * 5868 * spp_assoc_id - (one-to-many style socket) This is filled in the 5869 * application, and identifies the association for 5870 * this query. 5871 * spp_address - This specifies which address is of interest. 5872 * spp_hbinterval - This contains the value of the heartbeat interval, 5873 * in milliseconds. If a value of zero 5874 * is present in this field then no changes are to 5875 * be made to this parameter. 5876 * spp_pathmaxrxt - This contains the maximum number of 5877 * retransmissions before this address shall be 5878 * considered unreachable. If a value of zero 5879 * is present in this field then no changes are to 5880 * be made to this parameter. 5881 * spp_pathmtu - When Path MTU discovery is disabled the value 5882 * specified here will be the "fixed" path mtu. 5883 * Note that if the spp_address field is empty 5884 * then all associations on this address will 5885 * have this fixed path mtu set upon them. 5886 * 5887 * spp_sackdelay - When delayed sack is enabled, this value specifies 5888 * the number of milliseconds that sacks will be delayed 5889 * for. This value will apply to all addresses of an 5890 * association if the spp_address field is empty. Note 5891 * also, that if delayed sack is enabled and this 5892 * value is set to 0, no change is made to the last 5893 * recorded delayed sack timer value. 5894 * 5895 * spp_flags - These flags are used to control various features 5896 * on an association. The flag field may contain 5897 * zero or more of the following options. 5898 * 5899 * SPP_HB_ENABLE - Enable heartbeats on the 5900 * specified address. Note that if the address 5901 * field is empty all addresses for the association 5902 * have heartbeats enabled upon them. 5903 * 5904 * SPP_HB_DISABLE - Disable heartbeats on the 5905 * speicifed address. Note that if the address 5906 * field is empty all addresses for the association 5907 * will have their heartbeats disabled. Note also 5908 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 5909 * mutually exclusive, only one of these two should 5910 * be specified. Enabling both fields will have 5911 * undetermined results. 5912 * 5913 * SPP_HB_DEMAND - Request a user initiated heartbeat 5914 * to be made immediately. 5915 * 5916 * SPP_PMTUD_ENABLE - This field will enable PMTU 5917 * discovery upon the specified address. Note that 5918 * if the address feild is empty then all addresses 5919 * on the association are effected. 5920 * 5921 * SPP_PMTUD_DISABLE - This field will disable PMTU 5922 * discovery upon the specified address. Note that 5923 * if the address feild is empty then all addresses 5924 * on the association are effected. Not also that 5925 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 5926 * exclusive. Enabling both will have undetermined 5927 * results. 5928 * 5929 * SPP_SACKDELAY_ENABLE - Setting this flag turns 5930 * on delayed sack. The time specified in spp_sackdelay 5931 * is used to specify the sack delay for this address. Note 5932 * that if spp_address is empty then all addresses will 5933 * enable delayed sack and take on the sack delay 5934 * value specified in spp_sackdelay. 5935 * SPP_SACKDELAY_DISABLE - Setting this flag turns 5936 * off delayed sack. If the spp_address field is blank then 5937 * delayed sack is disabled for the entire association. Note 5938 * also that this field is mutually exclusive to 5939 * SPP_SACKDELAY_ENABLE, setting both will have undefined 5940 * results. 5941 * 5942 * SPP_IPV6_FLOWLABEL: Setting this flag enables the 5943 * setting of the IPV6 flow label value. The value is 5944 * contained in the spp_ipv6_flowlabel field. 5945 * Upon retrieval, this flag will be set to indicate that 5946 * the spp_ipv6_flowlabel field has a valid value returned. 5947 * If a specific destination address is set (in the 5948 * spp_address field), then the value returned is that of 5949 * the address. If just an association is specified (and 5950 * no address), then the association's default flow label 5951 * is returned. If neither an association nor a destination 5952 * is specified, then the socket's default flow label is 5953 * returned. For non-IPv6 sockets, this flag will be left 5954 * cleared. 5955 * 5956 * SPP_DSCP: Setting this flag enables the setting of the 5957 * Differentiated Services Code Point (DSCP) value 5958 * associated with either the association or a specific 5959 * address. The value is obtained in the spp_dscp field. 5960 * Upon retrieval, this flag will be set to indicate that 5961 * the spp_dscp field has a valid value returned. If a 5962 * specific destination address is set when called (in the 5963 * spp_address field), then that specific destination 5964 * address's DSCP value is returned. If just an association 5965 * is specified, then the association's default DSCP is 5966 * returned. If neither an association nor a destination is 5967 * specified, then the socket's default DSCP is returned. 5968 * 5969 * spp_ipv6_flowlabel 5970 * - This field is used in conjunction with the 5971 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. 5972 * The 20 least significant bits are used for the flow 5973 * label. This setting has precedence over any IPv6-layer 5974 * setting. 5975 * 5976 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag 5977 * and contains the DSCP. The 6 most significant bits are 5978 * used for the DSCP. This setting has precedence over any 5979 * IPv4- or IPv6- layer setting. 5980 */ 5981 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, 5982 char __user *optval, int __user *optlen) 5983 { 5984 struct sctp_paddrparams params; 5985 struct sctp_transport *trans = NULL; 5986 struct sctp_association *asoc = NULL; 5987 struct sctp_sock *sp = sctp_sk(sk); 5988 5989 if (len >= sizeof(params)) 5990 len = sizeof(params); 5991 else if (len >= ALIGN(offsetof(struct sctp_paddrparams, 5992 spp_ipv6_flowlabel), 4)) 5993 len = ALIGN(offsetof(struct sctp_paddrparams, 5994 spp_ipv6_flowlabel), 4); 5995 else 5996 return -EINVAL; 5997 5998 if (copy_from_user(¶ms, optval, len)) 5999 return -EFAULT; 6000 6001 /* If an address other than INADDR_ANY is specified, and 6002 * no transport is found, then the request is invalid. 6003 */ 6004 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { 6005 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 6006 params.spp_assoc_id); 6007 if (!trans) { 6008 pr_debug("%s: failed no transport\n", __func__); 6009 return -EINVAL; 6010 } 6011 } 6012 6013 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 6014 * socket is a one to many style socket, and an association 6015 * was not found, then the id was invalid. 6016 */ 6017 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 6018 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC && 6019 sctp_style(sk, UDP)) { 6020 pr_debug("%s: failed no association\n", __func__); 6021 return -EINVAL; 6022 } 6023 6024 if (trans) { 6025 /* Fetch transport values. */ 6026 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); 6027 params.spp_pathmtu = trans->pathmtu; 6028 params.spp_pathmaxrxt = trans->pathmaxrxt; 6029 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); 6030 6031 /*draft-11 doesn't say what to return in spp_flags*/ 6032 params.spp_flags = trans->param_flags; 6033 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6034 params.spp_ipv6_flowlabel = trans->flowlabel & 6035 SCTP_FLOWLABEL_VAL_MASK; 6036 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6037 } 6038 if (trans->dscp & SCTP_DSCP_SET_MASK) { 6039 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK; 6040 params.spp_flags |= SPP_DSCP; 6041 } 6042 } else if (asoc) { 6043 /* Fetch association values. */ 6044 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); 6045 params.spp_pathmtu = asoc->pathmtu; 6046 params.spp_pathmaxrxt = asoc->pathmaxrxt; 6047 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); 6048 6049 /*draft-11 doesn't say what to return in spp_flags*/ 6050 params.spp_flags = asoc->param_flags; 6051 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6052 params.spp_ipv6_flowlabel = asoc->flowlabel & 6053 SCTP_FLOWLABEL_VAL_MASK; 6054 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6055 } 6056 if (asoc->dscp & SCTP_DSCP_SET_MASK) { 6057 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK; 6058 params.spp_flags |= SPP_DSCP; 6059 } 6060 } else { 6061 /* Fetch socket values. */ 6062 params.spp_hbinterval = sp->hbinterval; 6063 params.spp_pathmtu = sp->pathmtu; 6064 params.spp_sackdelay = sp->sackdelay; 6065 params.spp_pathmaxrxt = sp->pathmaxrxt; 6066 6067 /*draft-11 doesn't say what to return in spp_flags*/ 6068 params.spp_flags = sp->param_flags; 6069 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6070 params.spp_ipv6_flowlabel = sp->flowlabel & 6071 SCTP_FLOWLABEL_VAL_MASK; 6072 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6073 } 6074 if (sp->dscp & SCTP_DSCP_SET_MASK) { 6075 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK; 6076 params.spp_flags |= SPP_DSCP; 6077 } 6078 } 6079 6080 if (copy_to_user(optval, ¶ms, len)) 6081 return -EFAULT; 6082 6083 if (put_user(len, optlen)) 6084 return -EFAULT; 6085 6086 return 0; 6087 } 6088 6089 /* 6090 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) 6091 * 6092 * This option will effect the way delayed acks are performed. This 6093 * option allows you to get or set the delayed ack time, in 6094 * milliseconds. It also allows changing the delayed ack frequency. 6095 * Changing the frequency to 1 disables the delayed sack algorithm. If 6096 * the assoc_id is 0, then this sets or gets the endpoints default 6097 * values. If the assoc_id field is non-zero, then the set or get 6098 * effects the specified association for the one to many model (the 6099 * assoc_id field is ignored by the one to one model). Note that if 6100 * sack_delay or sack_freq are 0 when setting this option, then the 6101 * current values will remain unchanged. 6102 * 6103 * struct sctp_sack_info { 6104 * sctp_assoc_t sack_assoc_id; 6105 * uint32_t sack_delay; 6106 * uint32_t sack_freq; 6107 * }; 6108 * 6109 * sack_assoc_id - This parameter, indicates which association the user 6110 * is performing an action upon. Note that if this field's value is 6111 * zero then the endpoints default value is changed (effecting future 6112 * associations only). 6113 * 6114 * sack_delay - This parameter contains the number of milliseconds that 6115 * the user is requesting the delayed ACK timer be set to. Note that 6116 * this value is defined in the standard to be between 200 and 500 6117 * milliseconds. 6118 * 6119 * sack_freq - This parameter contains the number of packets that must 6120 * be received before a sack is sent without waiting for the delay 6121 * timer to expire. The default value for this is 2, setting this 6122 * value to 1 will disable the delayed sack algorithm. 6123 */ 6124 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, 6125 char __user *optval, 6126 int __user *optlen) 6127 { 6128 struct sctp_sack_info params; 6129 struct sctp_association *asoc = NULL; 6130 struct sctp_sock *sp = sctp_sk(sk); 6131 6132 if (len >= sizeof(struct sctp_sack_info)) { 6133 len = sizeof(struct sctp_sack_info); 6134 6135 if (copy_from_user(¶ms, optval, len)) 6136 return -EFAULT; 6137 } else if (len == sizeof(struct sctp_assoc_value)) { 6138 pr_warn_ratelimited(DEPRECATED 6139 "%s (pid %d) " 6140 "Use of struct sctp_assoc_value in delayed_ack socket option.\n" 6141 "Use struct sctp_sack_info instead\n", 6142 current->comm, task_pid_nr(current)); 6143 if (copy_from_user(¶ms, optval, len)) 6144 return -EFAULT; 6145 } else 6146 return -EINVAL; 6147 6148 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the 6149 * socket is a one to many style socket, and an association 6150 * was not found, then the id was invalid. 6151 */ 6152 asoc = sctp_id2assoc(sk, params.sack_assoc_id); 6153 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC && 6154 sctp_style(sk, UDP)) 6155 return -EINVAL; 6156 6157 if (asoc) { 6158 /* Fetch association values. */ 6159 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { 6160 params.sack_delay = jiffies_to_msecs(asoc->sackdelay); 6161 params.sack_freq = asoc->sackfreq; 6162 6163 } else { 6164 params.sack_delay = 0; 6165 params.sack_freq = 1; 6166 } 6167 } else { 6168 /* Fetch socket values. */ 6169 if (sp->param_flags & SPP_SACKDELAY_ENABLE) { 6170 params.sack_delay = sp->sackdelay; 6171 params.sack_freq = sp->sackfreq; 6172 } else { 6173 params.sack_delay = 0; 6174 params.sack_freq = 1; 6175 } 6176 } 6177 6178 if (copy_to_user(optval, ¶ms, len)) 6179 return -EFAULT; 6180 6181 if (put_user(len, optlen)) 6182 return -EFAULT; 6183 6184 return 0; 6185 } 6186 6187 /* 7.1.3 Initialization Parameters (SCTP_INITMSG) 6188 * 6189 * Applications can specify protocol parameters for the default association 6190 * initialization. The option name argument to setsockopt() and getsockopt() 6191 * is SCTP_INITMSG. 6192 * 6193 * Setting initialization parameters is effective only on an unconnected 6194 * socket (for UDP-style sockets only future associations are effected 6195 * by the change). With TCP-style sockets, this option is inherited by 6196 * sockets derived from a listener socket. 6197 */ 6198 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) 6199 { 6200 if (len < sizeof(struct sctp_initmsg)) 6201 return -EINVAL; 6202 len = sizeof(struct sctp_initmsg); 6203 if (put_user(len, optlen)) 6204 return -EFAULT; 6205 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) 6206 return -EFAULT; 6207 return 0; 6208 } 6209 6210 6211 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, 6212 char __user *optval, int __user *optlen) 6213 { 6214 struct sctp_association *asoc; 6215 int cnt = 0; 6216 struct sctp_getaddrs getaddrs; 6217 struct sctp_transport *from; 6218 void __user *to; 6219 union sctp_addr temp; 6220 struct sctp_sock *sp = sctp_sk(sk); 6221 int addrlen; 6222 size_t space_left; 6223 int bytes_copied; 6224 6225 if (len < sizeof(struct sctp_getaddrs)) 6226 return -EINVAL; 6227 6228 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 6229 return -EFAULT; 6230 6231 /* For UDP-style sockets, id specifies the association to query. */ 6232 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 6233 if (!asoc) 6234 return -EINVAL; 6235 6236 to = optval + offsetof(struct sctp_getaddrs, addrs); 6237 space_left = len - offsetof(struct sctp_getaddrs, addrs); 6238 6239 list_for_each_entry(from, &asoc->peer.transport_addr_list, 6240 transports) { 6241 memcpy(&temp, &from->ipaddr, sizeof(temp)); 6242 addrlen = sctp_get_pf_specific(sk->sk_family) 6243 ->addr_to_user(sp, &temp); 6244 if (space_left < addrlen) 6245 return -ENOMEM; 6246 if (copy_to_user(to, &temp, addrlen)) 6247 return -EFAULT; 6248 to += addrlen; 6249 cnt++; 6250 space_left -= addrlen; 6251 } 6252 6253 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) 6254 return -EFAULT; 6255 bytes_copied = ((char __user *)to) - optval; 6256 if (put_user(bytes_copied, optlen)) 6257 return -EFAULT; 6258 6259 return 0; 6260 } 6261 6262 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, 6263 size_t space_left, int *bytes_copied) 6264 { 6265 struct sctp_sockaddr_entry *addr; 6266 union sctp_addr temp; 6267 int cnt = 0; 6268 int addrlen; 6269 struct net *net = sock_net(sk); 6270 6271 rcu_read_lock(); 6272 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { 6273 if (!addr->valid) 6274 continue; 6275 6276 if ((PF_INET == sk->sk_family) && 6277 (AF_INET6 == addr->a.sa.sa_family)) 6278 continue; 6279 if ((PF_INET6 == sk->sk_family) && 6280 inet_v6_ipv6only(sk) && 6281 (AF_INET == addr->a.sa.sa_family)) 6282 continue; 6283 memcpy(&temp, &addr->a, sizeof(temp)); 6284 if (!temp.v4.sin_port) 6285 temp.v4.sin_port = htons(port); 6286 6287 addrlen = sctp_get_pf_specific(sk->sk_family) 6288 ->addr_to_user(sctp_sk(sk), &temp); 6289 6290 if (space_left < addrlen) { 6291 cnt = -ENOMEM; 6292 break; 6293 } 6294 memcpy(to, &temp, addrlen); 6295 6296 to += addrlen; 6297 cnt++; 6298 space_left -= addrlen; 6299 *bytes_copied += addrlen; 6300 } 6301 rcu_read_unlock(); 6302 6303 return cnt; 6304 } 6305 6306 6307 static int sctp_getsockopt_local_addrs(struct sock *sk, int len, 6308 char __user *optval, int __user *optlen) 6309 { 6310 struct sctp_bind_addr *bp; 6311 struct sctp_association *asoc; 6312 int cnt = 0; 6313 struct sctp_getaddrs getaddrs; 6314 struct sctp_sockaddr_entry *addr; 6315 void __user *to; 6316 union sctp_addr temp; 6317 struct sctp_sock *sp = sctp_sk(sk); 6318 int addrlen; 6319 int err = 0; 6320 size_t space_left; 6321 int bytes_copied = 0; 6322 void *addrs; 6323 void *buf; 6324 6325 if (len < sizeof(struct sctp_getaddrs)) 6326 return -EINVAL; 6327 6328 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 6329 return -EFAULT; 6330 6331 /* 6332 * For UDP-style sockets, id specifies the association to query. 6333 * If the id field is set to the value '0' then the locally bound 6334 * addresses are returned without regard to any particular 6335 * association. 6336 */ 6337 if (0 == getaddrs.assoc_id) { 6338 bp = &sctp_sk(sk)->ep->base.bind_addr; 6339 } else { 6340 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 6341 if (!asoc) 6342 return -EINVAL; 6343 bp = &asoc->base.bind_addr; 6344 } 6345 6346 to = optval + offsetof(struct sctp_getaddrs, addrs); 6347 space_left = len - offsetof(struct sctp_getaddrs, addrs); 6348 6349 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); 6350 if (!addrs) 6351 return -ENOMEM; 6352 6353 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid 6354 * addresses from the global local address list. 6355 */ 6356 if (sctp_list_single_entry(&bp->address_list)) { 6357 addr = list_entry(bp->address_list.next, 6358 struct sctp_sockaddr_entry, list); 6359 if (sctp_is_any(sk, &addr->a)) { 6360 cnt = sctp_copy_laddrs(sk, bp->port, addrs, 6361 space_left, &bytes_copied); 6362 if (cnt < 0) { 6363 err = cnt; 6364 goto out; 6365 } 6366 goto copy_getaddrs; 6367 } 6368 } 6369 6370 buf = addrs; 6371 /* Protection on the bound address list is not needed since 6372 * in the socket option context we hold a socket lock and 6373 * thus the bound address list can't change. 6374 */ 6375 list_for_each_entry(addr, &bp->address_list, list) { 6376 memcpy(&temp, &addr->a, sizeof(temp)); 6377 addrlen = sctp_get_pf_specific(sk->sk_family) 6378 ->addr_to_user(sp, &temp); 6379 if (space_left < addrlen) { 6380 err = -ENOMEM; /*fixme: right error?*/ 6381 goto out; 6382 } 6383 memcpy(buf, &temp, addrlen); 6384 buf += addrlen; 6385 bytes_copied += addrlen; 6386 cnt++; 6387 space_left -= addrlen; 6388 } 6389 6390 copy_getaddrs: 6391 if (copy_to_user(to, addrs, bytes_copied)) { 6392 err = -EFAULT; 6393 goto out; 6394 } 6395 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { 6396 err = -EFAULT; 6397 goto out; 6398 } 6399 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too, 6400 * but we can't change it anymore. 6401 */ 6402 if (put_user(bytes_copied, optlen)) 6403 err = -EFAULT; 6404 out: 6405 kfree(addrs); 6406 return err; 6407 } 6408 6409 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 6410 * 6411 * Requests that the local SCTP stack use the enclosed peer address as 6412 * the association primary. The enclosed address must be one of the 6413 * association peer's addresses. 6414 */ 6415 static int sctp_getsockopt_primary_addr(struct sock *sk, int len, 6416 char __user *optval, int __user *optlen) 6417 { 6418 struct sctp_prim prim; 6419 struct sctp_association *asoc; 6420 struct sctp_sock *sp = sctp_sk(sk); 6421 6422 if (len < sizeof(struct sctp_prim)) 6423 return -EINVAL; 6424 6425 len = sizeof(struct sctp_prim); 6426 6427 if (copy_from_user(&prim, optval, len)) 6428 return -EFAULT; 6429 6430 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); 6431 if (!asoc) 6432 return -EINVAL; 6433 6434 if (!asoc->peer.primary_path) 6435 return -ENOTCONN; 6436 6437 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, 6438 asoc->peer.primary_path->af_specific->sockaddr_len); 6439 6440 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, 6441 (union sctp_addr *)&prim.ssp_addr); 6442 6443 if (put_user(len, optlen)) 6444 return -EFAULT; 6445 if (copy_to_user(optval, &prim, len)) 6446 return -EFAULT; 6447 6448 return 0; 6449 } 6450 6451 /* 6452 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) 6453 * 6454 * Requests that the local endpoint set the specified Adaptation Layer 6455 * Indication parameter for all future INIT and INIT-ACK exchanges. 6456 */ 6457 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, 6458 char __user *optval, int __user *optlen) 6459 { 6460 struct sctp_setadaptation adaptation; 6461 6462 if (len < sizeof(struct sctp_setadaptation)) 6463 return -EINVAL; 6464 6465 len = sizeof(struct sctp_setadaptation); 6466 6467 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; 6468 6469 if (put_user(len, optlen)) 6470 return -EFAULT; 6471 if (copy_to_user(optval, &adaptation, len)) 6472 return -EFAULT; 6473 6474 return 0; 6475 } 6476 6477 /* 6478 * 6479 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 6480 * 6481 * Applications that wish to use the sendto() system call may wish to 6482 * specify a default set of parameters that would normally be supplied 6483 * through the inclusion of ancillary data. This socket option allows 6484 * such an application to set the default sctp_sndrcvinfo structure. 6485 6486 6487 * The application that wishes to use this socket option simply passes 6488 * in to this call the sctp_sndrcvinfo structure defined in Section 6489 * 5.2.2) The input parameters accepted by this call include 6490 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 6491 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 6492 * to this call if the caller is using the UDP model. 6493 * 6494 * For getsockopt, it get the default sctp_sndrcvinfo structure. 6495 */ 6496 static int sctp_getsockopt_default_send_param(struct sock *sk, 6497 int len, char __user *optval, 6498 int __user *optlen) 6499 { 6500 struct sctp_sock *sp = sctp_sk(sk); 6501 struct sctp_association *asoc; 6502 struct sctp_sndrcvinfo info; 6503 6504 if (len < sizeof(info)) 6505 return -EINVAL; 6506 6507 len = sizeof(info); 6508 6509 if (copy_from_user(&info, optval, len)) 6510 return -EFAULT; 6511 6512 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 6513 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC && 6514 sctp_style(sk, UDP)) 6515 return -EINVAL; 6516 6517 if (asoc) { 6518 info.sinfo_stream = asoc->default_stream; 6519 info.sinfo_flags = asoc->default_flags; 6520 info.sinfo_ppid = asoc->default_ppid; 6521 info.sinfo_context = asoc->default_context; 6522 info.sinfo_timetolive = asoc->default_timetolive; 6523 } else { 6524 info.sinfo_stream = sp->default_stream; 6525 info.sinfo_flags = sp->default_flags; 6526 info.sinfo_ppid = sp->default_ppid; 6527 info.sinfo_context = sp->default_context; 6528 info.sinfo_timetolive = sp->default_timetolive; 6529 } 6530 6531 if (put_user(len, optlen)) 6532 return -EFAULT; 6533 if (copy_to_user(optval, &info, len)) 6534 return -EFAULT; 6535 6536 return 0; 6537 } 6538 6539 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters 6540 * (SCTP_DEFAULT_SNDINFO) 6541 */ 6542 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, 6543 char __user *optval, 6544 int __user *optlen) 6545 { 6546 struct sctp_sock *sp = sctp_sk(sk); 6547 struct sctp_association *asoc; 6548 struct sctp_sndinfo info; 6549 6550 if (len < sizeof(info)) 6551 return -EINVAL; 6552 6553 len = sizeof(info); 6554 6555 if (copy_from_user(&info, optval, len)) 6556 return -EFAULT; 6557 6558 asoc = sctp_id2assoc(sk, info.snd_assoc_id); 6559 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC && 6560 sctp_style(sk, UDP)) 6561 return -EINVAL; 6562 6563 if (asoc) { 6564 info.snd_sid = asoc->default_stream; 6565 info.snd_flags = asoc->default_flags; 6566 info.snd_ppid = asoc->default_ppid; 6567 info.snd_context = asoc->default_context; 6568 } else { 6569 info.snd_sid = sp->default_stream; 6570 info.snd_flags = sp->default_flags; 6571 info.snd_ppid = sp->default_ppid; 6572 info.snd_context = sp->default_context; 6573 } 6574 6575 if (put_user(len, optlen)) 6576 return -EFAULT; 6577 if (copy_to_user(optval, &info, len)) 6578 return -EFAULT; 6579 6580 return 0; 6581 } 6582 6583 /* 6584 * 6585 * 7.1.5 SCTP_NODELAY 6586 * 6587 * Turn on/off any Nagle-like algorithm. This means that packets are 6588 * generally sent as soon as possible and no unnecessary delays are 6589 * introduced, at the cost of more packets in the network. Expects an 6590 * integer boolean flag. 6591 */ 6592 6593 static int sctp_getsockopt_nodelay(struct sock *sk, int len, 6594 char __user *optval, int __user *optlen) 6595 { 6596 int val; 6597 6598 if (len < sizeof(int)) 6599 return -EINVAL; 6600 6601 len = sizeof(int); 6602 val = (sctp_sk(sk)->nodelay == 1); 6603 if (put_user(len, optlen)) 6604 return -EFAULT; 6605 if (copy_to_user(optval, &val, len)) 6606 return -EFAULT; 6607 return 0; 6608 } 6609 6610 /* 6611 * 6612 * 7.1.1 SCTP_RTOINFO 6613 * 6614 * The protocol parameters used to initialize and bound retransmission 6615 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 6616 * and modify these parameters. 6617 * All parameters are time values, in milliseconds. A value of 0, when 6618 * modifying the parameters, indicates that the current value should not 6619 * be changed. 6620 * 6621 */ 6622 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, 6623 char __user *optval, 6624 int __user *optlen) { 6625 struct sctp_rtoinfo rtoinfo; 6626 struct sctp_association *asoc; 6627 6628 if (len < sizeof (struct sctp_rtoinfo)) 6629 return -EINVAL; 6630 6631 len = sizeof(struct sctp_rtoinfo); 6632 6633 if (copy_from_user(&rtoinfo, optval, len)) 6634 return -EFAULT; 6635 6636 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 6637 6638 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC && 6639 sctp_style(sk, UDP)) 6640 return -EINVAL; 6641 6642 /* Values corresponding to the specific association. */ 6643 if (asoc) { 6644 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); 6645 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); 6646 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); 6647 } else { 6648 /* Values corresponding to the endpoint. */ 6649 struct sctp_sock *sp = sctp_sk(sk); 6650 6651 rtoinfo.srto_initial = sp->rtoinfo.srto_initial; 6652 rtoinfo.srto_max = sp->rtoinfo.srto_max; 6653 rtoinfo.srto_min = sp->rtoinfo.srto_min; 6654 } 6655 6656 if (put_user(len, optlen)) 6657 return -EFAULT; 6658 6659 if (copy_to_user(optval, &rtoinfo, len)) 6660 return -EFAULT; 6661 6662 return 0; 6663 } 6664 6665 /* 6666 * 6667 * 7.1.2 SCTP_ASSOCINFO 6668 * 6669 * This option is used to tune the maximum retransmission attempts 6670 * of the association. 6671 * Returns an error if the new association retransmission value is 6672 * greater than the sum of the retransmission value of the peer. 6673 * See [SCTP] for more information. 6674 * 6675 */ 6676 static int sctp_getsockopt_associnfo(struct sock *sk, int len, 6677 char __user *optval, 6678 int __user *optlen) 6679 { 6680 6681 struct sctp_assocparams assocparams; 6682 struct sctp_association *asoc; 6683 struct list_head *pos; 6684 int cnt = 0; 6685 6686 if (len < sizeof (struct sctp_assocparams)) 6687 return -EINVAL; 6688 6689 len = sizeof(struct sctp_assocparams); 6690 6691 if (copy_from_user(&assocparams, optval, len)) 6692 return -EFAULT; 6693 6694 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 6695 6696 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC && 6697 sctp_style(sk, UDP)) 6698 return -EINVAL; 6699 6700 /* Values correspoinding to the specific association */ 6701 if (asoc) { 6702 assocparams.sasoc_asocmaxrxt = asoc->max_retrans; 6703 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; 6704 assocparams.sasoc_local_rwnd = asoc->a_rwnd; 6705 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); 6706 6707 list_for_each(pos, &asoc->peer.transport_addr_list) { 6708 cnt++; 6709 } 6710 6711 assocparams.sasoc_number_peer_destinations = cnt; 6712 } else { 6713 /* Values corresponding to the endpoint */ 6714 struct sctp_sock *sp = sctp_sk(sk); 6715 6716 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; 6717 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; 6718 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; 6719 assocparams.sasoc_cookie_life = 6720 sp->assocparams.sasoc_cookie_life; 6721 assocparams.sasoc_number_peer_destinations = 6722 sp->assocparams. 6723 sasoc_number_peer_destinations; 6724 } 6725 6726 if (put_user(len, optlen)) 6727 return -EFAULT; 6728 6729 if (copy_to_user(optval, &assocparams, len)) 6730 return -EFAULT; 6731 6732 return 0; 6733 } 6734 6735 /* 6736 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 6737 * 6738 * This socket option is a boolean flag which turns on or off mapped V4 6739 * addresses. If this option is turned on and the socket is type 6740 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 6741 * If this option is turned off, then no mapping will be done of V4 6742 * addresses and a user will receive both PF_INET6 and PF_INET type 6743 * addresses on the socket. 6744 */ 6745 static int sctp_getsockopt_mappedv4(struct sock *sk, int len, 6746 char __user *optval, int __user *optlen) 6747 { 6748 int val; 6749 struct sctp_sock *sp = sctp_sk(sk); 6750 6751 if (len < sizeof(int)) 6752 return -EINVAL; 6753 6754 len = sizeof(int); 6755 val = sp->v4mapped; 6756 if (put_user(len, optlen)) 6757 return -EFAULT; 6758 if (copy_to_user(optval, &val, len)) 6759 return -EFAULT; 6760 6761 return 0; 6762 } 6763 6764 /* 6765 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 6766 * (chapter and verse is quoted at sctp_setsockopt_context()) 6767 */ 6768 static int sctp_getsockopt_context(struct sock *sk, int len, 6769 char __user *optval, int __user *optlen) 6770 { 6771 struct sctp_assoc_value params; 6772 struct sctp_association *asoc; 6773 6774 if (len < sizeof(struct sctp_assoc_value)) 6775 return -EINVAL; 6776 6777 len = sizeof(struct sctp_assoc_value); 6778 6779 if (copy_from_user(¶ms, optval, len)) 6780 return -EFAULT; 6781 6782 asoc = sctp_id2assoc(sk, params.assoc_id); 6783 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6784 sctp_style(sk, UDP)) 6785 return -EINVAL; 6786 6787 params.assoc_value = asoc ? asoc->default_rcv_context 6788 : sctp_sk(sk)->default_rcv_context; 6789 6790 if (put_user(len, optlen)) 6791 return -EFAULT; 6792 if (copy_to_user(optval, ¶ms, len)) 6793 return -EFAULT; 6794 6795 return 0; 6796 } 6797 6798 /* 6799 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 6800 * This option will get or set the maximum size to put in any outgoing 6801 * SCTP DATA chunk. If a message is larger than this size it will be 6802 * fragmented by SCTP into the specified size. Note that the underlying 6803 * SCTP implementation may fragment into smaller sized chunks when the 6804 * PMTU of the underlying association is smaller than the value set by 6805 * the user. The default value for this option is '0' which indicates 6806 * the user is NOT limiting fragmentation and only the PMTU will effect 6807 * SCTP's choice of DATA chunk size. Note also that values set larger 6808 * than the maximum size of an IP datagram will effectively let SCTP 6809 * control fragmentation (i.e. the same as setting this option to 0). 6810 * 6811 * The following structure is used to access and modify this parameter: 6812 * 6813 * struct sctp_assoc_value { 6814 * sctp_assoc_t assoc_id; 6815 * uint32_t assoc_value; 6816 * }; 6817 * 6818 * assoc_id: This parameter is ignored for one-to-one style sockets. 6819 * For one-to-many style sockets this parameter indicates which 6820 * association the user is performing an action upon. Note that if 6821 * this field's value is zero then the endpoints default value is 6822 * changed (effecting future associations only). 6823 * assoc_value: This parameter specifies the maximum size in bytes. 6824 */ 6825 static int sctp_getsockopt_maxseg(struct sock *sk, int len, 6826 char __user *optval, int __user *optlen) 6827 { 6828 struct sctp_assoc_value params; 6829 struct sctp_association *asoc; 6830 6831 if (len == sizeof(int)) { 6832 pr_warn_ratelimited(DEPRECATED 6833 "%s (pid %d) " 6834 "Use of int in maxseg socket option.\n" 6835 "Use struct sctp_assoc_value instead\n", 6836 current->comm, task_pid_nr(current)); 6837 params.assoc_id = SCTP_FUTURE_ASSOC; 6838 } else if (len >= sizeof(struct sctp_assoc_value)) { 6839 len = sizeof(struct sctp_assoc_value); 6840 if (copy_from_user(¶ms, optval, len)) 6841 return -EFAULT; 6842 } else 6843 return -EINVAL; 6844 6845 asoc = sctp_id2assoc(sk, params.assoc_id); 6846 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6847 sctp_style(sk, UDP)) 6848 return -EINVAL; 6849 6850 if (asoc) 6851 params.assoc_value = asoc->frag_point; 6852 else 6853 params.assoc_value = sctp_sk(sk)->user_frag; 6854 6855 if (put_user(len, optlen)) 6856 return -EFAULT; 6857 if (len == sizeof(int)) { 6858 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6859 return -EFAULT; 6860 } else { 6861 if (copy_to_user(optval, ¶ms, len)) 6862 return -EFAULT; 6863 } 6864 6865 return 0; 6866 } 6867 6868 /* 6869 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 6870 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) 6871 */ 6872 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, 6873 char __user *optval, int __user *optlen) 6874 { 6875 int val; 6876 6877 if (len < sizeof(int)) 6878 return -EINVAL; 6879 6880 len = sizeof(int); 6881 6882 val = sctp_sk(sk)->frag_interleave; 6883 if (put_user(len, optlen)) 6884 return -EFAULT; 6885 if (copy_to_user(optval, &val, len)) 6886 return -EFAULT; 6887 6888 return 0; 6889 } 6890 6891 /* 6892 * 7.1.25. Set or Get the sctp partial delivery point 6893 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) 6894 */ 6895 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, 6896 char __user *optval, 6897 int __user *optlen) 6898 { 6899 u32 val; 6900 6901 if (len < sizeof(u32)) 6902 return -EINVAL; 6903 6904 len = sizeof(u32); 6905 6906 val = sctp_sk(sk)->pd_point; 6907 if (put_user(len, optlen)) 6908 return -EFAULT; 6909 if (copy_to_user(optval, &val, len)) 6910 return -EFAULT; 6911 6912 return 0; 6913 } 6914 6915 /* 6916 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 6917 * (chapter and verse is quoted at sctp_setsockopt_maxburst()) 6918 */ 6919 static int sctp_getsockopt_maxburst(struct sock *sk, int len, 6920 char __user *optval, 6921 int __user *optlen) 6922 { 6923 struct sctp_assoc_value params; 6924 struct sctp_association *asoc; 6925 6926 if (len == sizeof(int)) { 6927 pr_warn_ratelimited(DEPRECATED 6928 "%s (pid %d) " 6929 "Use of int in max_burst socket option.\n" 6930 "Use struct sctp_assoc_value instead\n", 6931 current->comm, task_pid_nr(current)); 6932 params.assoc_id = SCTP_FUTURE_ASSOC; 6933 } else if (len >= sizeof(struct sctp_assoc_value)) { 6934 len = sizeof(struct sctp_assoc_value); 6935 if (copy_from_user(¶ms, optval, len)) 6936 return -EFAULT; 6937 } else 6938 return -EINVAL; 6939 6940 asoc = sctp_id2assoc(sk, params.assoc_id); 6941 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6942 sctp_style(sk, UDP)) 6943 return -EINVAL; 6944 6945 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst; 6946 6947 if (len == sizeof(int)) { 6948 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6949 return -EFAULT; 6950 } else { 6951 if (copy_to_user(optval, ¶ms, len)) 6952 return -EFAULT; 6953 } 6954 6955 return 0; 6956 6957 } 6958 6959 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, 6960 char __user *optval, int __user *optlen) 6961 { 6962 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6963 struct sctp_hmacalgo __user *p = (void __user *)optval; 6964 struct sctp_hmac_algo_param *hmacs; 6965 __u16 data_len = 0; 6966 u32 num_idents; 6967 int i; 6968 6969 if (!ep->auth_enable) 6970 return -EACCES; 6971 6972 hmacs = ep->auth_hmacs_list; 6973 data_len = ntohs(hmacs->param_hdr.length) - 6974 sizeof(struct sctp_paramhdr); 6975 6976 if (len < sizeof(struct sctp_hmacalgo) + data_len) 6977 return -EINVAL; 6978 6979 len = sizeof(struct sctp_hmacalgo) + data_len; 6980 num_idents = data_len / sizeof(u16); 6981 6982 if (put_user(len, optlen)) 6983 return -EFAULT; 6984 if (put_user(num_idents, &p->shmac_num_idents)) 6985 return -EFAULT; 6986 for (i = 0; i < num_idents; i++) { 6987 __u16 hmacid = ntohs(hmacs->hmac_ids[i]); 6988 6989 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) 6990 return -EFAULT; 6991 } 6992 return 0; 6993 } 6994 6995 static int sctp_getsockopt_active_key(struct sock *sk, int len, 6996 char __user *optval, int __user *optlen) 6997 { 6998 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6999 struct sctp_authkeyid val; 7000 struct sctp_association *asoc; 7001 7002 if (len < sizeof(struct sctp_authkeyid)) 7003 return -EINVAL; 7004 7005 len = sizeof(struct sctp_authkeyid); 7006 if (copy_from_user(&val, optval, len)) 7007 return -EFAULT; 7008 7009 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 7010 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 7011 return -EINVAL; 7012 7013 if (asoc) { 7014 if (!asoc->peer.auth_capable) 7015 return -EACCES; 7016 val.scact_keynumber = asoc->active_key_id; 7017 } else { 7018 if (!ep->auth_enable) 7019 return -EACCES; 7020 val.scact_keynumber = ep->active_key_id; 7021 } 7022 7023 if (put_user(len, optlen)) 7024 return -EFAULT; 7025 if (copy_to_user(optval, &val, len)) 7026 return -EFAULT; 7027 7028 return 0; 7029 } 7030 7031 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, 7032 char __user *optval, int __user *optlen) 7033 { 7034 struct sctp_authchunks __user *p = (void __user *)optval; 7035 struct sctp_authchunks val; 7036 struct sctp_association *asoc; 7037 struct sctp_chunks_param *ch; 7038 u32 num_chunks = 0; 7039 char __user *to; 7040 7041 if (len < sizeof(struct sctp_authchunks)) 7042 return -EINVAL; 7043 7044 if (copy_from_user(&val, optval, sizeof(val))) 7045 return -EFAULT; 7046 7047 to = p->gauth_chunks; 7048 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 7049 if (!asoc) 7050 return -EINVAL; 7051 7052 if (!asoc->peer.auth_capable) 7053 return -EACCES; 7054 7055 ch = asoc->peer.peer_chunks; 7056 if (!ch) 7057 goto num; 7058 7059 /* See if the user provided enough room for all the data */ 7060 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 7061 if (len < sizeof(struct sctp_authchunks) + num_chunks) 7062 return -EINVAL; 7063 7064 if (copy_to_user(to, ch->chunks, num_chunks)) 7065 return -EFAULT; 7066 num: 7067 len = sizeof(struct sctp_authchunks) + num_chunks; 7068 if (put_user(len, optlen)) 7069 return -EFAULT; 7070 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 7071 return -EFAULT; 7072 return 0; 7073 } 7074 7075 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, 7076 char __user *optval, int __user *optlen) 7077 { 7078 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 7079 struct sctp_authchunks __user *p = (void __user *)optval; 7080 struct sctp_authchunks val; 7081 struct sctp_association *asoc; 7082 struct sctp_chunks_param *ch; 7083 u32 num_chunks = 0; 7084 char __user *to; 7085 7086 if (len < sizeof(struct sctp_authchunks)) 7087 return -EINVAL; 7088 7089 if (copy_from_user(&val, optval, sizeof(val))) 7090 return -EFAULT; 7091 7092 to = p->gauth_chunks; 7093 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 7094 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC && 7095 sctp_style(sk, UDP)) 7096 return -EINVAL; 7097 7098 if (asoc) { 7099 if (!asoc->peer.auth_capable) 7100 return -EACCES; 7101 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; 7102 } else { 7103 if (!ep->auth_enable) 7104 return -EACCES; 7105 ch = ep->auth_chunk_list; 7106 } 7107 if (!ch) 7108 goto num; 7109 7110 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 7111 if (len < sizeof(struct sctp_authchunks) + num_chunks) 7112 return -EINVAL; 7113 7114 if (copy_to_user(to, ch->chunks, num_chunks)) 7115 return -EFAULT; 7116 num: 7117 len = sizeof(struct sctp_authchunks) + num_chunks; 7118 if (put_user(len, optlen)) 7119 return -EFAULT; 7120 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 7121 return -EFAULT; 7122 7123 return 0; 7124 } 7125 7126 /* 7127 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) 7128 * This option gets the current number of associations that are attached 7129 * to a one-to-many style socket. The option value is an uint32_t. 7130 */ 7131 static int sctp_getsockopt_assoc_number(struct sock *sk, int len, 7132 char __user *optval, int __user *optlen) 7133 { 7134 struct sctp_sock *sp = sctp_sk(sk); 7135 struct sctp_association *asoc; 7136 u32 val = 0; 7137 7138 if (sctp_style(sk, TCP)) 7139 return -EOPNOTSUPP; 7140 7141 if (len < sizeof(u32)) 7142 return -EINVAL; 7143 7144 len = sizeof(u32); 7145 7146 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7147 val++; 7148 } 7149 7150 if (put_user(len, optlen)) 7151 return -EFAULT; 7152 if (copy_to_user(optval, &val, len)) 7153 return -EFAULT; 7154 7155 return 0; 7156 } 7157 7158 /* 7159 * 8.1.23 SCTP_AUTO_ASCONF 7160 * See the corresponding setsockopt entry as description 7161 */ 7162 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, 7163 char __user *optval, int __user *optlen) 7164 { 7165 int val = 0; 7166 7167 if (len < sizeof(int)) 7168 return -EINVAL; 7169 7170 len = sizeof(int); 7171 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) 7172 val = 1; 7173 if (put_user(len, optlen)) 7174 return -EFAULT; 7175 if (copy_to_user(optval, &val, len)) 7176 return -EFAULT; 7177 return 0; 7178 } 7179 7180 /* 7181 * 8.2.6. Get the Current Identifiers of Associations 7182 * (SCTP_GET_ASSOC_ID_LIST) 7183 * 7184 * This option gets the current list of SCTP association identifiers of 7185 * the SCTP associations handled by a one-to-many style socket. 7186 */ 7187 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, 7188 char __user *optval, int __user *optlen) 7189 { 7190 struct sctp_sock *sp = sctp_sk(sk); 7191 struct sctp_association *asoc; 7192 struct sctp_assoc_ids *ids; 7193 size_t ids_size; 7194 u32 num = 0; 7195 7196 if (sctp_style(sk, TCP)) 7197 return -EOPNOTSUPP; 7198 7199 if (len < sizeof(struct sctp_assoc_ids)) 7200 return -EINVAL; 7201 7202 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7203 num++; 7204 } 7205 7206 ids_size = struct_size(ids, gaids_assoc_id, num); 7207 if (len < ids_size) 7208 return -EINVAL; 7209 7210 len = ids_size; 7211 ids = kmalloc(len, GFP_USER | __GFP_NOWARN); 7212 if (unlikely(!ids)) 7213 return -ENOMEM; 7214 7215 ids->gaids_number_of_ids = num; 7216 num = 0; 7217 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7218 ids->gaids_assoc_id[num++] = asoc->assoc_id; 7219 } 7220 7221 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { 7222 kfree(ids); 7223 return -EFAULT; 7224 } 7225 7226 kfree(ids); 7227 return 0; 7228 } 7229 7230 /* 7231 * SCTP_PEER_ADDR_THLDS 7232 * 7233 * This option allows us to fetch the partially failed threshold for one or all 7234 * transports in an association. See Section 6.1 of: 7235 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt 7236 */ 7237 static int sctp_getsockopt_paddr_thresholds(struct sock *sk, 7238 char __user *optval, int len, 7239 int __user *optlen, bool v2) 7240 { 7241 struct sctp_paddrthlds_v2 val; 7242 struct sctp_transport *trans; 7243 struct sctp_association *asoc; 7244 int min; 7245 7246 min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds); 7247 if (len < min) 7248 return -EINVAL; 7249 len = min; 7250 if (copy_from_user(&val, optval, len)) 7251 return -EFAULT; 7252 7253 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { 7254 trans = sctp_addr_id2transport(sk, &val.spt_address, 7255 val.spt_assoc_id); 7256 if (!trans) 7257 return -ENOENT; 7258 7259 val.spt_pathmaxrxt = trans->pathmaxrxt; 7260 val.spt_pathpfthld = trans->pf_retrans; 7261 val.spt_pathcpthld = trans->ps_retrans; 7262 7263 goto out; 7264 } 7265 7266 asoc = sctp_id2assoc(sk, val.spt_assoc_id); 7267 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC && 7268 sctp_style(sk, UDP)) 7269 return -EINVAL; 7270 7271 if (asoc) { 7272 val.spt_pathpfthld = asoc->pf_retrans; 7273 val.spt_pathmaxrxt = asoc->pathmaxrxt; 7274 val.spt_pathcpthld = asoc->ps_retrans; 7275 } else { 7276 struct sctp_sock *sp = sctp_sk(sk); 7277 7278 val.spt_pathpfthld = sp->pf_retrans; 7279 val.spt_pathmaxrxt = sp->pathmaxrxt; 7280 val.spt_pathcpthld = sp->ps_retrans; 7281 } 7282 7283 out: 7284 if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 7285 return -EFAULT; 7286 7287 return 0; 7288 } 7289 7290 /* 7291 * SCTP_GET_ASSOC_STATS 7292 * 7293 * This option retrieves local per endpoint statistics. It is modeled 7294 * after OpenSolaris' implementation 7295 */ 7296 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, 7297 char __user *optval, 7298 int __user *optlen) 7299 { 7300 struct sctp_assoc_stats sas; 7301 struct sctp_association *asoc = NULL; 7302 7303 /* User must provide at least the assoc id */ 7304 if (len < sizeof(sctp_assoc_t)) 7305 return -EINVAL; 7306 7307 /* Allow the struct to grow and fill in as much as possible */ 7308 len = min_t(size_t, len, sizeof(sas)); 7309 7310 if (copy_from_user(&sas, optval, len)) 7311 return -EFAULT; 7312 7313 asoc = sctp_id2assoc(sk, sas.sas_assoc_id); 7314 if (!asoc) 7315 return -EINVAL; 7316 7317 sas.sas_rtxchunks = asoc->stats.rtxchunks; 7318 sas.sas_gapcnt = asoc->stats.gapcnt; 7319 sas.sas_outofseqtsns = asoc->stats.outofseqtsns; 7320 sas.sas_osacks = asoc->stats.osacks; 7321 sas.sas_isacks = asoc->stats.isacks; 7322 sas.sas_octrlchunks = asoc->stats.octrlchunks; 7323 sas.sas_ictrlchunks = asoc->stats.ictrlchunks; 7324 sas.sas_oodchunks = asoc->stats.oodchunks; 7325 sas.sas_iodchunks = asoc->stats.iodchunks; 7326 sas.sas_ouodchunks = asoc->stats.ouodchunks; 7327 sas.sas_iuodchunks = asoc->stats.iuodchunks; 7328 sas.sas_idupchunks = asoc->stats.idupchunks; 7329 sas.sas_opackets = asoc->stats.opackets; 7330 sas.sas_ipackets = asoc->stats.ipackets; 7331 7332 /* New high max rto observed, will return 0 if not a single 7333 * RTO update took place. obs_rto_ipaddr will be bogus 7334 * in such a case 7335 */ 7336 sas.sas_maxrto = asoc->stats.max_obs_rto; 7337 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, 7338 sizeof(struct sockaddr_storage)); 7339 7340 /* Mark beginning of a new observation period */ 7341 asoc->stats.max_obs_rto = asoc->rto_min; 7342 7343 if (put_user(len, optlen)) 7344 return -EFAULT; 7345 7346 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id); 7347 7348 if (copy_to_user(optval, &sas, len)) 7349 return -EFAULT; 7350 7351 return 0; 7352 } 7353 7354 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len, 7355 char __user *optval, 7356 int __user *optlen) 7357 { 7358 int val = 0; 7359 7360 if (len < sizeof(int)) 7361 return -EINVAL; 7362 7363 len = sizeof(int); 7364 if (sctp_sk(sk)->recvrcvinfo) 7365 val = 1; 7366 if (put_user(len, optlen)) 7367 return -EFAULT; 7368 if (copy_to_user(optval, &val, len)) 7369 return -EFAULT; 7370 7371 return 0; 7372 } 7373 7374 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len, 7375 char __user *optval, 7376 int __user *optlen) 7377 { 7378 int val = 0; 7379 7380 if (len < sizeof(int)) 7381 return -EINVAL; 7382 7383 len = sizeof(int); 7384 if (sctp_sk(sk)->recvnxtinfo) 7385 val = 1; 7386 if (put_user(len, optlen)) 7387 return -EFAULT; 7388 if (copy_to_user(optval, &val, len)) 7389 return -EFAULT; 7390 7391 return 0; 7392 } 7393 7394 static int sctp_getsockopt_pr_supported(struct sock *sk, int len, 7395 char __user *optval, 7396 int __user *optlen) 7397 { 7398 struct sctp_assoc_value params; 7399 struct sctp_association *asoc; 7400 int retval = -EFAULT; 7401 7402 if (len < sizeof(params)) { 7403 retval = -EINVAL; 7404 goto out; 7405 } 7406 7407 len = sizeof(params); 7408 if (copy_from_user(¶ms, optval, len)) 7409 goto out; 7410 7411 asoc = sctp_id2assoc(sk, params.assoc_id); 7412 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7413 sctp_style(sk, UDP)) { 7414 retval = -EINVAL; 7415 goto out; 7416 } 7417 7418 params.assoc_value = asoc ? asoc->peer.prsctp_capable 7419 : sctp_sk(sk)->ep->prsctp_enable; 7420 7421 if (put_user(len, optlen)) 7422 goto out; 7423 7424 if (copy_to_user(optval, ¶ms, len)) 7425 goto out; 7426 7427 retval = 0; 7428 7429 out: 7430 return retval; 7431 } 7432 7433 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, 7434 char __user *optval, 7435 int __user *optlen) 7436 { 7437 struct sctp_default_prinfo info; 7438 struct sctp_association *asoc; 7439 int retval = -EFAULT; 7440 7441 if (len < sizeof(info)) { 7442 retval = -EINVAL; 7443 goto out; 7444 } 7445 7446 len = sizeof(info); 7447 if (copy_from_user(&info, optval, len)) 7448 goto out; 7449 7450 asoc = sctp_id2assoc(sk, info.pr_assoc_id); 7451 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC && 7452 sctp_style(sk, UDP)) { 7453 retval = -EINVAL; 7454 goto out; 7455 } 7456 7457 if (asoc) { 7458 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); 7459 info.pr_value = asoc->default_timetolive; 7460 } else { 7461 struct sctp_sock *sp = sctp_sk(sk); 7462 7463 info.pr_policy = SCTP_PR_POLICY(sp->default_flags); 7464 info.pr_value = sp->default_timetolive; 7465 } 7466 7467 if (put_user(len, optlen)) 7468 goto out; 7469 7470 if (copy_to_user(optval, &info, len)) 7471 goto out; 7472 7473 retval = 0; 7474 7475 out: 7476 return retval; 7477 } 7478 7479 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, 7480 char __user *optval, 7481 int __user *optlen) 7482 { 7483 struct sctp_prstatus params; 7484 struct sctp_association *asoc; 7485 int policy; 7486 int retval = -EINVAL; 7487 7488 if (len < sizeof(params)) 7489 goto out; 7490 7491 len = sizeof(params); 7492 if (copy_from_user(¶ms, optval, len)) { 7493 retval = -EFAULT; 7494 goto out; 7495 } 7496 7497 policy = params.sprstat_policy; 7498 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || 7499 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) 7500 goto out; 7501 7502 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 7503 if (!asoc) 7504 goto out; 7505 7506 if (policy == SCTP_PR_SCTP_ALL) { 7507 params.sprstat_abandoned_unsent = 0; 7508 params.sprstat_abandoned_sent = 0; 7509 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 7510 params.sprstat_abandoned_unsent += 7511 asoc->abandoned_unsent[policy]; 7512 params.sprstat_abandoned_sent += 7513 asoc->abandoned_sent[policy]; 7514 } 7515 } else { 7516 params.sprstat_abandoned_unsent = 7517 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 7518 params.sprstat_abandoned_sent = 7519 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; 7520 } 7521 7522 if (put_user(len, optlen)) { 7523 retval = -EFAULT; 7524 goto out; 7525 } 7526 7527 if (copy_to_user(optval, ¶ms, len)) { 7528 retval = -EFAULT; 7529 goto out; 7530 } 7531 7532 retval = 0; 7533 7534 out: 7535 return retval; 7536 } 7537 7538 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, 7539 char __user *optval, 7540 int __user *optlen) 7541 { 7542 struct sctp_stream_out_ext *streamoute; 7543 struct sctp_association *asoc; 7544 struct sctp_prstatus params; 7545 int retval = -EINVAL; 7546 int policy; 7547 7548 if (len < sizeof(params)) 7549 goto out; 7550 7551 len = sizeof(params); 7552 if (copy_from_user(¶ms, optval, len)) { 7553 retval = -EFAULT; 7554 goto out; 7555 } 7556 7557 policy = params.sprstat_policy; 7558 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || 7559 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) 7560 goto out; 7561 7562 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 7563 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) 7564 goto out; 7565 7566 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext; 7567 if (!streamoute) { 7568 /* Not allocated yet, means all stats are 0 */ 7569 params.sprstat_abandoned_unsent = 0; 7570 params.sprstat_abandoned_sent = 0; 7571 retval = 0; 7572 goto out; 7573 } 7574 7575 if (policy == SCTP_PR_SCTP_ALL) { 7576 params.sprstat_abandoned_unsent = 0; 7577 params.sprstat_abandoned_sent = 0; 7578 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 7579 params.sprstat_abandoned_unsent += 7580 streamoute->abandoned_unsent[policy]; 7581 params.sprstat_abandoned_sent += 7582 streamoute->abandoned_sent[policy]; 7583 } 7584 } else { 7585 params.sprstat_abandoned_unsent = 7586 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 7587 params.sprstat_abandoned_sent = 7588 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; 7589 } 7590 7591 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { 7592 retval = -EFAULT; 7593 goto out; 7594 } 7595 7596 retval = 0; 7597 7598 out: 7599 return retval; 7600 } 7601 7602 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, 7603 char __user *optval, 7604 int __user *optlen) 7605 { 7606 struct sctp_assoc_value params; 7607 struct sctp_association *asoc; 7608 int retval = -EFAULT; 7609 7610 if (len < sizeof(params)) { 7611 retval = -EINVAL; 7612 goto out; 7613 } 7614 7615 len = sizeof(params); 7616 if (copy_from_user(¶ms, optval, len)) 7617 goto out; 7618 7619 asoc = sctp_id2assoc(sk, params.assoc_id); 7620 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7621 sctp_style(sk, UDP)) { 7622 retval = -EINVAL; 7623 goto out; 7624 } 7625 7626 params.assoc_value = asoc ? asoc->peer.reconf_capable 7627 : sctp_sk(sk)->ep->reconf_enable; 7628 7629 if (put_user(len, optlen)) 7630 goto out; 7631 7632 if (copy_to_user(optval, ¶ms, len)) 7633 goto out; 7634 7635 retval = 0; 7636 7637 out: 7638 return retval; 7639 } 7640 7641 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, 7642 char __user *optval, 7643 int __user *optlen) 7644 { 7645 struct sctp_assoc_value params; 7646 struct sctp_association *asoc; 7647 int retval = -EFAULT; 7648 7649 if (len < sizeof(params)) { 7650 retval = -EINVAL; 7651 goto out; 7652 } 7653 7654 len = sizeof(params); 7655 if (copy_from_user(¶ms, optval, len)) 7656 goto out; 7657 7658 asoc = sctp_id2assoc(sk, params.assoc_id); 7659 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7660 sctp_style(sk, UDP)) { 7661 retval = -EINVAL; 7662 goto out; 7663 } 7664 7665 params.assoc_value = asoc ? asoc->strreset_enable 7666 : sctp_sk(sk)->ep->strreset_enable; 7667 7668 if (put_user(len, optlen)) 7669 goto out; 7670 7671 if (copy_to_user(optval, ¶ms, len)) 7672 goto out; 7673 7674 retval = 0; 7675 7676 out: 7677 return retval; 7678 } 7679 7680 static int sctp_getsockopt_scheduler(struct sock *sk, int len, 7681 char __user *optval, 7682 int __user *optlen) 7683 { 7684 struct sctp_assoc_value params; 7685 struct sctp_association *asoc; 7686 int retval = -EFAULT; 7687 7688 if (len < sizeof(params)) { 7689 retval = -EINVAL; 7690 goto out; 7691 } 7692 7693 len = sizeof(params); 7694 if (copy_from_user(¶ms, optval, len)) 7695 goto out; 7696 7697 asoc = sctp_id2assoc(sk, params.assoc_id); 7698 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7699 sctp_style(sk, UDP)) { 7700 retval = -EINVAL; 7701 goto out; 7702 } 7703 7704 params.assoc_value = asoc ? sctp_sched_get_sched(asoc) 7705 : sctp_sk(sk)->default_ss; 7706 7707 if (put_user(len, optlen)) 7708 goto out; 7709 7710 if (copy_to_user(optval, ¶ms, len)) 7711 goto out; 7712 7713 retval = 0; 7714 7715 out: 7716 return retval; 7717 } 7718 7719 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, 7720 char __user *optval, 7721 int __user *optlen) 7722 { 7723 struct sctp_stream_value params; 7724 struct sctp_association *asoc; 7725 int retval = -EFAULT; 7726 7727 if (len < sizeof(params)) { 7728 retval = -EINVAL; 7729 goto out; 7730 } 7731 7732 len = sizeof(params); 7733 if (copy_from_user(¶ms, optval, len)) 7734 goto out; 7735 7736 asoc = sctp_id2assoc(sk, params.assoc_id); 7737 if (!asoc) { 7738 retval = -EINVAL; 7739 goto out; 7740 } 7741 7742 retval = sctp_sched_get_value(asoc, params.stream_id, 7743 ¶ms.stream_value); 7744 if (retval) 7745 goto out; 7746 7747 if (put_user(len, optlen)) { 7748 retval = -EFAULT; 7749 goto out; 7750 } 7751 7752 if (copy_to_user(optval, ¶ms, len)) { 7753 retval = -EFAULT; 7754 goto out; 7755 } 7756 7757 out: 7758 return retval; 7759 } 7760 7761 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, 7762 char __user *optval, 7763 int __user *optlen) 7764 { 7765 struct sctp_assoc_value params; 7766 struct sctp_association *asoc; 7767 int retval = -EFAULT; 7768 7769 if (len < sizeof(params)) { 7770 retval = -EINVAL; 7771 goto out; 7772 } 7773 7774 len = sizeof(params); 7775 if (copy_from_user(¶ms, optval, len)) 7776 goto out; 7777 7778 asoc = sctp_id2assoc(sk, params.assoc_id); 7779 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7780 sctp_style(sk, UDP)) { 7781 retval = -EINVAL; 7782 goto out; 7783 } 7784 7785 params.assoc_value = asoc ? asoc->peer.intl_capable 7786 : sctp_sk(sk)->ep->intl_enable; 7787 7788 if (put_user(len, optlen)) 7789 goto out; 7790 7791 if (copy_to_user(optval, ¶ms, len)) 7792 goto out; 7793 7794 retval = 0; 7795 7796 out: 7797 return retval; 7798 } 7799 7800 static int sctp_getsockopt_reuse_port(struct sock *sk, int len, 7801 char __user *optval, 7802 int __user *optlen) 7803 { 7804 int val; 7805 7806 if (len < sizeof(int)) 7807 return -EINVAL; 7808 7809 len = sizeof(int); 7810 val = sctp_sk(sk)->reuse; 7811 if (put_user(len, optlen)) 7812 return -EFAULT; 7813 7814 if (copy_to_user(optval, &val, len)) 7815 return -EFAULT; 7816 7817 return 0; 7818 } 7819 7820 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval, 7821 int __user *optlen) 7822 { 7823 struct sctp_association *asoc; 7824 struct sctp_event param; 7825 __u16 subscribe; 7826 7827 if (len < sizeof(param)) 7828 return -EINVAL; 7829 7830 len = sizeof(param); 7831 if (copy_from_user(¶m, optval, len)) 7832 return -EFAULT; 7833 7834 if (param.se_type < SCTP_SN_TYPE_BASE || 7835 param.se_type > SCTP_SN_TYPE_MAX) 7836 return -EINVAL; 7837 7838 asoc = sctp_id2assoc(sk, param.se_assoc_id); 7839 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC && 7840 sctp_style(sk, UDP)) 7841 return -EINVAL; 7842 7843 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe; 7844 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type); 7845 7846 if (put_user(len, optlen)) 7847 return -EFAULT; 7848 7849 if (copy_to_user(optval, ¶m, len)) 7850 return -EFAULT; 7851 7852 return 0; 7853 } 7854 7855 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len, 7856 char __user *optval, 7857 int __user *optlen) 7858 { 7859 struct sctp_assoc_value params; 7860 struct sctp_association *asoc; 7861 int retval = -EFAULT; 7862 7863 if (len < sizeof(params)) { 7864 retval = -EINVAL; 7865 goto out; 7866 } 7867 7868 len = sizeof(params); 7869 if (copy_from_user(¶ms, optval, len)) 7870 goto out; 7871 7872 asoc = sctp_id2assoc(sk, params.assoc_id); 7873 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7874 sctp_style(sk, UDP)) { 7875 retval = -EINVAL; 7876 goto out; 7877 } 7878 7879 params.assoc_value = asoc ? asoc->peer.asconf_capable 7880 : sctp_sk(sk)->ep->asconf_enable; 7881 7882 if (put_user(len, optlen)) 7883 goto out; 7884 7885 if (copy_to_user(optval, ¶ms, len)) 7886 goto out; 7887 7888 retval = 0; 7889 7890 out: 7891 return retval; 7892 } 7893 7894 static int sctp_getsockopt_auth_supported(struct sock *sk, int len, 7895 char __user *optval, 7896 int __user *optlen) 7897 { 7898 struct sctp_assoc_value params; 7899 struct sctp_association *asoc; 7900 int retval = -EFAULT; 7901 7902 if (len < sizeof(params)) { 7903 retval = -EINVAL; 7904 goto out; 7905 } 7906 7907 len = sizeof(params); 7908 if (copy_from_user(¶ms, optval, len)) 7909 goto out; 7910 7911 asoc = sctp_id2assoc(sk, params.assoc_id); 7912 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7913 sctp_style(sk, UDP)) { 7914 retval = -EINVAL; 7915 goto out; 7916 } 7917 7918 params.assoc_value = asoc ? asoc->peer.auth_capable 7919 : sctp_sk(sk)->ep->auth_enable; 7920 7921 if (put_user(len, optlen)) 7922 goto out; 7923 7924 if (copy_to_user(optval, ¶ms, len)) 7925 goto out; 7926 7927 retval = 0; 7928 7929 out: 7930 return retval; 7931 } 7932 7933 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len, 7934 char __user *optval, 7935 int __user *optlen) 7936 { 7937 struct sctp_assoc_value params; 7938 struct sctp_association *asoc; 7939 int retval = -EFAULT; 7940 7941 if (len < sizeof(params)) { 7942 retval = -EINVAL; 7943 goto out; 7944 } 7945 7946 len = sizeof(params); 7947 if (copy_from_user(¶ms, optval, len)) 7948 goto out; 7949 7950 asoc = sctp_id2assoc(sk, params.assoc_id); 7951 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7952 sctp_style(sk, UDP)) { 7953 retval = -EINVAL; 7954 goto out; 7955 } 7956 7957 params.assoc_value = asoc ? asoc->peer.ecn_capable 7958 : sctp_sk(sk)->ep->ecn_enable; 7959 7960 if (put_user(len, optlen)) 7961 goto out; 7962 7963 if (copy_to_user(optval, ¶ms, len)) 7964 goto out; 7965 7966 retval = 0; 7967 7968 out: 7969 return retval; 7970 } 7971 7972 static int sctp_getsockopt_pf_expose(struct sock *sk, int len, 7973 char __user *optval, 7974 int __user *optlen) 7975 { 7976 struct sctp_assoc_value params; 7977 struct sctp_association *asoc; 7978 int retval = -EFAULT; 7979 7980 if (len < sizeof(params)) { 7981 retval = -EINVAL; 7982 goto out; 7983 } 7984 7985 len = sizeof(params); 7986 if (copy_from_user(¶ms, optval, len)) 7987 goto out; 7988 7989 asoc = sctp_id2assoc(sk, params.assoc_id); 7990 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7991 sctp_style(sk, UDP)) { 7992 retval = -EINVAL; 7993 goto out; 7994 } 7995 7996 params.assoc_value = asoc ? asoc->pf_expose 7997 : sctp_sk(sk)->pf_expose; 7998 7999 if (put_user(len, optlen)) 8000 goto out; 8001 8002 if (copy_to_user(optval, ¶ms, len)) 8003 goto out; 8004 8005 retval = 0; 8006 8007 out: 8008 return retval; 8009 } 8010 8011 static int sctp_getsockopt_encap_port(struct sock *sk, int len, 8012 char __user *optval, int __user *optlen) 8013 { 8014 struct sctp_association *asoc; 8015 struct sctp_udpencaps encap; 8016 struct sctp_transport *t; 8017 __be16 encap_port; 8018 8019 if (len < sizeof(encap)) 8020 return -EINVAL; 8021 8022 len = sizeof(encap); 8023 if (copy_from_user(&encap, optval, len)) 8024 return -EFAULT; 8025 8026 /* If an address other than INADDR_ANY is specified, and 8027 * no transport is found, then the request is invalid. 8028 */ 8029 if (!sctp_is_any(sk, (union sctp_addr *)&encap.sue_address)) { 8030 t = sctp_addr_id2transport(sk, &encap.sue_address, 8031 encap.sue_assoc_id); 8032 if (!t) { 8033 pr_debug("%s: failed no transport\n", __func__); 8034 return -EINVAL; 8035 } 8036 8037 encap_port = t->encap_port; 8038 goto out; 8039 } 8040 8041 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 8042 * socket is a one to many style socket, and an association 8043 * was not found, then the id was invalid. 8044 */ 8045 asoc = sctp_id2assoc(sk, encap.sue_assoc_id); 8046 if (!asoc && encap.sue_assoc_id != SCTP_FUTURE_ASSOC && 8047 sctp_style(sk, UDP)) { 8048 pr_debug("%s: failed no association\n", __func__); 8049 return -EINVAL; 8050 } 8051 8052 if (asoc) { 8053 encap_port = asoc->encap_port; 8054 goto out; 8055 } 8056 8057 encap_port = sctp_sk(sk)->encap_port; 8058 8059 out: 8060 encap.sue_port = (__force uint16_t)encap_port; 8061 if (copy_to_user(optval, &encap, len)) 8062 return -EFAULT; 8063 8064 if (put_user(len, optlen)) 8065 return -EFAULT; 8066 8067 return 0; 8068 } 8069 8070 static int sctp_getsockopt_probe_interval(struct sock *sk, int len, 8071 char __user *optval, 8072 int __user *optlen) 8073 { 8074 struct sctp_probeinterval params; 8075 struct sctp_association *asoc; 8076 struct sctp_transport *t; 8077 __u32 probe_interval; 8078 8079 if (len < sizeof(params)) 8080 return -EINVAL; 8081 8082 len = sizeof(params); 8083 if (copy_from_user(¶ms, optval, len)) 8084 return -EFAULT; 8085 8086 /* If an address other than INADDR_ANY is specified, and 8087 * no transport is found, then the request is invalid. 8088 */ 8089 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spi_address)) { 8090 t = sctp_addr_id2transport(sk, ¶ms.spi_address, 8091 params.spi_assoc_id); 8092 if (!t) { 8093 pr_debug("%s: failed no transport\n", __func__); 8094 return -EINVAL; 8095 } 8096 8097 probe_interval = jiffies_to_msecs(t->probe_interval); 8098 goto out; 8099 } 8100 8101 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 8102 * socket is a one to many style socket, and an association 8103 * was not found, then the id was invalid. 8104 */ 8105 asoc = sctp_id2assoc(sk, params.spi_assoc_id); 8106 if (!asoc && params.spi_assoc_id != SCTP_FUTURE_ASSOC && 8107 sctp_style(sk, UDP)) { 8108 pr_debug("%s: failed no association\n", __func__); 8109 return -EINVAL; 8110 } 8111 8112 if (asoc) { 8113 probe_interval = jiffies_to_msecs(asoc->probe_interval); 8114 goto out; 8115 } 8116 8117 probe_interval = sctp_sk(sk)->probe_interval; 8118 8119 out: 8120 params.spi_interval = probe_interval; 8121 if (copy_to_user(optval, ¶ms, len)) 8122 return -EFAULT; 8123 8124 if (put_user(len, optlen)) 8125 return -EFAULT; 8126 8127 return 0; 8128 } 8129 8130 static int sctp_getsockopt(struct sock *sk, int level, int optname, 8131 char __user *optval, int __user *optlen) 8132 { 8133 int retval = 0; 8134 int len; 8135 8136 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); 8137 8138 /* I can hardly begin to describe how wrong this is. This is 8139 * so broken as to be worse than useless. The API draft 8140 * REALLY is NOT helpful here... I am not convinced that the 8141 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP 8142 * are at all well-founded. 8143 */ 8144 if (level != SOL_SCTP) { 8145 struct sctp_af *af = sctp_sk(sk)->pf->af; 8146 8147 retval = af->getsockopt(sk, level, optname, optval, optlen); 8148 return retval; 8149 } 8150 8151 if (get_user(len, optlen)) 8152 return -EFAULT; 8153 8154 if (len < 0) 8155 return -EINVAL; 8156 8157 lock_sock(sk); 8158 8159 switch (optname) { 8160 case SCTP_STATUS: 8161 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); 8162 break; 8163 case SCTP_DISABLE_FRAGMENTS: 8164 retval = sctp_getsockopt_disable_fragments(sk, len, optval, 8165 optlen); 8166 break; 8167 case SCTP_EVENTS: 8168 retval = sctp_getsockopt_events(sk, len, optval, optlen); 8169 break; 8170 case SCTP_AUTOCLOSE: 8171 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); 8172 break; 8173 case SCTP_SOCKOPT_PEELOFF: 8174 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); 8175 break; 8176 case SCTP_SOCKOPT_PEELOFF_FLAGS: 8177 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); 8178 break; 8179 case SCTP_PEER_ADDR_PARAMS: 8180 retval = sctp_getsockopt_peer_addr_params(sk, len, optval, 8181 optlen); 8182 break; 8183 case SCTP_DELAYED_SACK: 8184 retval = sctp_getsockopt_delayed_ack(sk, len, optval, 8185 optlen); 8186 break; 8187 case SCTP_INITMSG: 8188 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); 8189 break; 8190 case SCTP_GET_PEER_ADDRS: 8191 retval = sctp_getsockopt_peer_addrs(sk, len, optval, 8192 optlen); 8193 break; 8194 case SCTP_GET_LOCAL_ADDRS: 8195 retval = sctp_getsockopt_local_addrs(sk, len, optval, 8196 optlen); 8197 break; 8198 case SCTP_SOCKOPT_CONNECTX3: 8199 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); 8200 break; 8201 case SCTP_DEFAULT_SEND_PARAM: 8202 retval = sctp_getsockopt_default_send_param(sk, len, 8203 optval, optlen); 8204 break; 8205 case SCTP_DEFAULT_SNDINFO: 8206 retval = sctp_getsockopt_default_sndinfo(sk, len, 8207 optval, optlen); 8208 break; 8209 case SCTP_PRIMARY_ADDR: 8210 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); 8211 break; 8212 case SCTP_NODELAY: 8213 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); 8214 break; 8215 case SCTP_RTOINFO: 8216 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); 8217 break; 8218 case SCTP_ASSOCINFO: 8219 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); 8220 break; 8221 case SCTP_I_WANT_MAPPED_V4_ADDR: 8222 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); 8223 break; 8224 case SCTP_MAXSEG: 8225 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); 8226 break; 8227 case SCTP_GET_PEER_ADDR_INFO: 8228 retval = sctp_getsockopt_peer_addr_info(sk, len, optval, 8229 optlen); 8230 break; 8231 case SCTP_ADAPTATION_LAYER: 8232 retval = sctp_getsockopt_adaptation_layer(sk, len, optval, 8233 optlen); 8234 break; 8235 case SCTP_CONTEXT: 8236 retval = sctp_getsockopt_context(sk, len, optval, optlen); 8237 break; 8238 case SCTP_FRAGMENT_INTERLEAVE: 8239 retval = sctp_getsockopt_fragment_interleave(sk, len, optval, 8240 optlen); 8241 break; 8242 case SCTP_PARTIAL_DELIVERY_POINT: 8243 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, 8244 optlen); 8245 break; 8246 case SCTP_MAX_BURST: 8247 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); 8248 break; 8249 case SCTP_AUTH_KEY: 8250 case SCTP_AUTH_CHUNK: 8251 case SCTP_AUTH_DELETE_KEY: 8252 case SCTP_AUTH_DEACTIVATE_KEY: 8253 retval = -EOPNOTSUPP; 8254 break; 8255 case SCTP_HMAC_IDENT: 8256 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); 8257 break; 8258 case SCTP_AUTH_ACTIVE_KEY: 8259 retval = sctp_getsockopt_active_key(sk, len, optval, optlen); 8260 break; 8261 case SCTP_PEER_AUTH_CHUNKS: 8262 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, 8263 optlen); 8264 break; 8265 case SCTP_LOCAL_AUTH_CHUNKS: 8266 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, 8267 optlen); 8268 break; 8269 case SCTP_GET_ASSOC_NUMBER: 8270 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); 8271 break; 8272 case SCTP_GET_ASSOC_ID_LIST: 8273 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); 8274 break; 8275 case SCTP_AUTO_ASCONF: 8276 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); 8277 break; 8278 case SCTP_PEER_ADDR_THLDS: 8279 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, 8280 optlen, false); 8281 break; 8282 case SCTP_PEER_ADDR_THLDS_V2: 8283 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, 8284 optlen, true); 8285 break; 8286 case SCTP_GET_ASSOC_STATS: 8287 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); 8288 break; 8289 case SCTP_RECVRCVINFO: 8290 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); 8291 break; 8292 case SCTP_RECVNXTINFO: 8293 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); 8294 break; 8295 case SCTP_PR_SUPPORTED: 8296 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); 8297 break; 8298 case SCTP_DEFAULT_PRINFO: 8299 retval = sctp_getsockopt_default_prinfo(sk, len, optval, 8300 optlen); 8301 break; 8302 case SCTP_PR_ASSOC_STATUS: 8303 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, 8304 optlen); 8305 break; 8306 case SCTP_PR_STREAM_STATUS: 8307 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, 8308 optlen); 8309 break; 8310 case SCTP_RECONFIG_SUPPORTED: 8311 retval = sctp_getsockopt_reconfig_supported(sk, len, optval, 8312 optlen); 8313 break; 8314 case SCTP_ENABLE_STREAM_RESET: 8315 retval = sctp_getsockopt_enable_strreset(sk, len, optval, 8316 optlen); 8317 break; 8318 case SCTP_STREAM_SCHEDULER: 8319 retval = sctp_getsockopt_scheduler(sk, len, optval, 8320 optlen); 8321 break; 8322 case SCTP_STREAM_SCHEDULER_VALUE: 8323 retval = sctp_getsockopt_scheduler_value(sk, len, optval, 8324 optlen); 8325 break; 8326 case SCTP_INTERLEAVING_SUPPORTED: 8327 retval = sctp_getsockopt_interleaving_supported(sk, len, optval, 8328 optlen); 8329 break; 8330 case SCTP_REUSE_PORT: 8331 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen); 8332 break; 8333 case SCTP_EVENT: 8334 retval = sctp_getsockopt_event(sk, len, optval, optlen); 8335 break; 8336 case SCTP_ASCONF_SUPPORTED: 8337 retval = sctp_getsockopt_asconf_supported(sk, len, optval, 8338 optlen); 8339 break; 8340 case SCTP_AUTH_SUPPORTED: 8341 retval = sctp_getsockopt_auth_supported(sk, len, optval, 8342 optlen); 8343 break; 8344 case SCTP_ECN_SUPPORTED: 8345 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen); 8346 break; 8347 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE: 8348 retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen); 8349 break; 8350 case SCTP_REMOTE_UDP_ENCAPS_PORT: 8351 retval = sctp_getsockopt_encap_port(sk, len, optval, optlen); 8352 break; 8353 case SCTP_PLPMTUD_PROBE_INTERVAL: 8354 retval = sctp_getsockopt_probe_interval(sk, len, optval, optlen); 8355 break; 8356 default: 8357 retval = -ENOPROTOOPT; 8358 break; 8359 } 8360 8361 release_sock(sk); 8362 return retval; 8363 } 8364 8365 static bool sctp_bpf_bypass_getsockopt(int level, int optname) 8366 { 8367 if (level == SOL_SCTP) { 8368 switch (optname) { 8369 case SCTP_SOCKOPT_PEELOFF: 8370 case SCTP_SOCKOPT_PEELOFF_FLAGS: 8371 case SCTP_SOCKOPT_CONNECTX3: 8372 return true; 8373 default: 8374 return false; 8375 } 8376 } 8377 8378 return false; 8379 } 8380 8381 static int sctp_hash(struct sock *sk) 8382 { 8383 /* STUB */ 8384 return 0; 8385 } 8386 8387 static void sctp_unhash(struct sock *sk) 8388 { 8389 sock_rps_delete_flow(sk); 8390 } 8391 8392 /* Check if port is acceptable. Possibly find first available port. 8393 * 8394 * The port hash table (contained in the 'global' SCTP protocol storage 8395 * returned by struct sctp_protocol *sctp_get_protocol()). The hash 8396 * table is an array of 4096 lists (sctp_bind_hashbucket). Each 8397 * list (the list number is the port number hashed out, so as you 8398 * would expect from a hash function, all the ports in a given list have 8399 * such a number that hashes out to the same list number; you were 8400 * expecting that, right?); so each list has a set of ports, with a 8401 * link to the socket (struct sock) that uses it, the port number and 8402 * a fastreuse flag (FIXME: NPI ipg). 8403 */ 8404 static struct sctp_bind_bucket *sctp_bucket_create( 8405 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); 8406 8407 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr) 8408 { 8409 struct sctp_sock *sp = sctp_sk(sk); 8410 bool reuse = (sk->sk_reuse || sp->reuse); 8411 struct sctp_bind_hashbucket *head; /* hash list */ 8412 struct net *net = sock_net(sk); 8413 struct sctp_bind_bucket *pp; 8414 kuid_t uid = sk_uid(sk); 8415 unsigned short snum; 8416 int ret; 8417 8418 snum = ntohs(addr->v4.sin_port); 8419 8420 pr_debug("%s: begins, snum:%d\n", __func__, snum); 8421 8422 if (snum == 0) { 8423 /* Search for an available port. */ 8424 int low, high, remaining, index; 8425 unsigned int rover; 8426 8427 inet_sk_get_local_port_range(sk, &low, &high); 8428 remaining = (high - low) + 1; 8429 rover = get_random_u32_below(remaining) + low; 8430 8431 do { 8432 rover++; 8433 if ((rover < low) || (rover > high)) 8434 rover = low; 8435 if (inet_is_local_reserved_port(net, rover)) 8436 continue; 8437 index = sctp_phashfn(net, rover); 8438 head = &sctp_port_hashtable[index]; 8439 spin_lock_bh(&head->lock); 8440 sctp_for_each_hentry(pp, &head->chain) 8441 if ((pp->port == rover) && 8442 net_eq(net, pp->net)) 8443 goto next; 8444 break; 8445 next: 8446 spin_unlock_bh(&head->lock); 8447 cond_resched(); 8448 } while (--remaining > 0); 8449 8450 /* Exhausted local port range during search? */ 8451 ret = 1; 8452 if (remaining <= 0) 8453 return ret; 8454 8455 /* OK, here is the one we will use. HEAD (the port 8456 * hash table list entry) is non-NULL and we hold it's 8457 * mutex. 8458 */ 8459 snum = rover; 8460 } else { 8461 /* We are given an specific port number; we verify 8462 * that it is not being used. If it is used, we will 8463 * exahust the search in the hash list corresponding 8464 * to the port number (snum) - we detect that with the 8465 * port iterator, pp being NULL. 8466 */ 8467 head = &sctp_port_hashtable[sctp_phashfn(net, snum)]; 8468 spin_lock_bh(&head->lock); 8469 sctp_for_each_hentry(pp, &head->chain) { 8470 if ((pp->port == snum) && net_eq(pp->net, net)) 8471 goto pp_found; 8472 } 8473 } 8474 pp = NULL; 8475 goto pp_not_found; 8476 pp_found: 8477 if (!hlist_empty(&pp->owner)) { 8478 /* We had a port hash table hit - there is an 8479 * available port (pp != NULL) and it is being 8480 * used by other socket (pp->owner not empty); that other 8481 * socket is going to be sk2. 8482 */ 8483 struct sock *sk2; 8484 8485 pr_debug("%s: found a possible match\n", __func__); 8486 8487 if ((pp->fastreuse && reuse && 8488 sk->sk_state != SCTP_SS_LISTENING) || 8489 (pp->fastreuseport && sk->sk_reuseport && 8490 uid_eq(pp->fastuid, uid))) 8491 goto success; 8492 8493 /* Run through the list of sockets bound to the port 8494 * (pp->port) [via the pointers bind_next and 8495 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, 8496 * we get the endpoint they describe and run through 8497 * the endpoint's list of IP (v4 or v6) addresses, 8498 * comparing each of the addresses with the address of 8499 * the socket sk. If we find a match, then that means 8500 * that this port/socket (sk) combination are already 8501 * in an endpoint. 8502 */ 8503 sk_for_each_bound(sk2, &pp->owner) { 8504 int bound_dev_if2 = READ_ONCE(sk2->sk_bound_dev_if); 8505 struct sctp_sock *sp2 = sctp_sk(sk2); 8506 struct sctp_endpoint *ep2 = sp2->ep; 8507 8508 if (sk == sk2 || 8509 (reuse && (sk2->sk_reuse || sp2->reuse) && 8510 sk2->sk_state != SCTP_SS_LISTENING) || 8511 (sk->sk_reuseport && sk2->sk_reuseport && 8512 uid_eq(uid, sk_uid(sk2)))) 8513 continue; 8514 8515 if ((!sk->sk_bound_dev_if || !bound_dev_if2 || 8516 sk->sk_bound_dev_if == bound_dev_if2) && 8517 sctp_bind_addr_conflict(&ep2->base.bind_addr, 8518 addr, sp2, sp)) { 8519 ret = 1; 8520 goto fail_unlock; 8521 } 8522 } 8523 8524 pr_debug("%s: found a match\n", __func__); 8525 } 8526 pp_not_found: 8527 /* If there was a hash table miss, create a new port. */ 8528 ret = 1; 8529 if (!pp && !(pp = sctp_bucket_create(head, net, snum))) 8530 goto fail_unlock; 8531 8532 /* In either case (hit or miss), make sure fastreuse is 1 only 8533 * if sk->sk_reuse is too (that is, if the caller requested 8534 * SO_REUSEADDR on this socket -sk-). 8535 */ 8536 if (hlist_empty(&pp->owner)) { 8537 if (reuse && sk->sk_state != SCTP_SS_LISTENING) 8538 pp->fastreuse = 1; 8539 else 8540 pp->fastreuse = 0; 8541 8542 if (sk->sk_reuseport) { 8543 pp->fastreuseport = 1; 8544 pp->fastuid = uid; 8545 } else { 8546 pp->fastreuseport = 0; 8547 } 8548 } else { 8549 if (pp->fastreuse && 8550 (!reuse || sk->sk_state == SCTP_SS_LISTENING)) 8551 pp->fastreuse = 0; 8552 8553 if (pp->fastreuseport && 8554 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid))) 8555 pp->fastreuseport = 0; 8556 } 8557 8558 /* We are set, so fill up all the data in the hash table 8559 * entry, tie the socket list information with the rest of the 8560 * sockets FIXME: Blurry, NPI (ipg). 8561 */ 8562 success: 8563 if (!sp->bind_hash) { 8564 inet_sk(sk)->inet_num = snum; 8565 sk_add_bind_node(sk, &pp->owner); 8566 sp->bind_hash = pp; 8567 } 8568 ret = 0; 8569 8570 fail_unlock: 8571 spin_unlock_bh(&head->lock); 8572 return ret; 8573 } 8574 8575 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral 8576 * port is requested. 8577 */ 8578 static int sctp_get_port(struct sock *sk, unsigned short snum) 8579 { 8580 union sctp_addr addr; 8581 struct sctp_af *af = sctp_sk(sk)->pf->af; 8582 8583 /* Set up a dummy address struct from the sk. */ 8584 af->from_sk(&addr, sk); 8585 addr.v4.sin_port = htons(snum); 8586 8587 /* Note: sk->sk_num gets filled in if ephemeral port request. */ 8588 return sctp_get_port_local(sk, &addr); 8589 } 8590 8591 /* 8592 * Move a socket to LISTENING state. 8593 */ 8594 static int sctp_listen_start(struct sock *sk, int backlog) 8595 { 8596 struct sctp_sock *sp = sctp_sk(sk); 8597 struct sctp_endpoint *ep = sp->ep; 8598 int err; 8599 8600 /* 8601 * If a bind() or sctp_bindx() is not called prior to a listen() 8602 * call that allows new associations to be accepted, the system 8603 * picks an ephemeral port and will choose an address set equivalent 8604 * to binding with a wildcard address. 8605 * 8606 * This is not currently spelled out in the SCTP sockets 8607 * extensions draft, but follows the practice as seen in TCP 8608 * sockets. 8609 * 8610 */ 8611 inet_sk_set_state(sk, SCTP_SS_LISTENING); 8612 if (!ep->base.bind_addr.port) { 8613 if (sctp_autobind(sk)) { 8614 err = -EAGAIN; 8615 goto err; 8616 } 8617 } else { 8618 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { 8619 err = -EADDRINUSE; 8620 goto err; 8621 } 8622 } 8623 8624 WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 8625 err = sctp_hash_endpoint(ep); 8626 if (err) 8627 goto err; 8628 8629 return 0; 8630 err: 8631 inet_sk_set_state(sk, SCTP_SS_CLOSED); 8632 return err; 8633 } 8634 8635 /* 8636 * 4.1.3 / 5.1.3 listen() 8637 * 8638 * By default, new associations are not accepted for UDP style sockets. 8639 * An application uses listen() to mark a socket as being able to 8640 * accept new associations. 8641 * 8642 * On TCP style sockets, applications use listen() to ready the SCTP 8643 * endpoint for accepting inbound associations. 8644 * 8645 * On both types of endpoints a backlog of '0' disables listening. 8646 * 8647 * Move a socket to LISTENING state. 8648 */ 8649 int sctp_inet_listen(struct socket *sock, int backlog) 8650 { 8651 struct sock *sk = sock->sk; 8652 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 8653 int err = -EINVAL; 8654 8655 if (unlikely(backlog < 0)) 8656 return err; 8657 8658 lock_sock(sk); 8659 8660 /* Peeled-off sockets are not allowed to listen(). */ 8661 if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) 8662 goto out; 8663 8664 if (sock->state != SS_UNCONNECTED) 8665 goto out; 8666 8667 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) 8668 goto out; 8669 8670 /* If backlog is zero, disable listening. */ 8671 if (!backlog) { 8672 if (sctp_sstate(sk, CLOSED)) 8673 goto out; 8674 8675 err = 0; 8676 sctp_unhash_endpoint(ep); 8677 sk->sk_state = SCTP_SS_CLOSED; 8678 if (sk->sk_reuse || sctp_sk(sk)->reuse) 8679 sctp_sk(sk)->bind_hash->fastreuse = 1; 8680 goto out; 8681 } 8682 8683 /* If we are already listening, just update the backlog */ 8684 if (sctp_sstate(sk, LISTENING)) 8685 WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 8686 else { 8687 err = sctp_listen_start(sk, backlog); 8688 if (err) 8689 goto out; 8690 } 8691 8692 err = 0; 8693 out: 8694 release_sock(sk); 8695 return err; 8696 } 8697 8698 /* 8699 * This function is done by modeling the current datagram_poll() and the 8700 * tcp_poll(). Note that, based on these implementations, we don't 8701 * lock the socket in this function, even though it seems that, 8702 * ideally, locking or some other mechanisms can be used to ensure 8703 * the integrity of the counters (sndbuf and wmem_alloc) used 8704 * in this place. We assume that we don't need locks either until proven 8705 * otherwise. 8706 * 8707 * Another thing to note is that we include the Async I/O support 8708 * here, again, by modeling the current TCP/UDP code. We don't have 8709 * a good way to test with it yet. 8710 */ 8711 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait) 8712 { 8713 struct sock *sk = sock->sk; 8714 struct sctp_sock *sp = sctp_sk(sk); 8715 __poll_t mask; 8716 8717 poll_wait(file, sk_sleep(sk), wait); 8718 8719 sock_rps_record_flow(sk); 8720 8721 /* A TCP-style listening socket becomes readable when the accept queue 8722 * is not empty. 8723 */ 8724 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 8725 return (!list_empty(&sp->ep->asocs)) ? 8726 (EPOLLIN | EPOLLRDNORM) : 0; 8727 8728 mask = 0; 8729 8730 /* Is there any exceptional events? */ 8731 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 8732 mask |= EPOLLERR | 8733 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 8734 if (sk->sk_shutdown & RCV_SHUTDOWN) 8735 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; 8736 if (sk->sk_shutdown == SHUTDOWN_MASK) 8737 mask |= EPOLLHUP; 8738 8739 /* Is it readable? Reconsider this code with TCP-style support. */ 8740 if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) 8741 mask |= EPOLLIN | EPOLLRDNORM; 8742 8743 /* The association is either gone or not ready. */ 8744 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) 8745 return mask; 8746 8747 /* Is it writable? */ 8748 if (sctp_writeable(sk)) { 8749 mask |= EPOLLOUT | EPOLLWRNORM; 8750 } else { 8751 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 8752 /* 8753 * Since the socket is not locked, the buffer 8754 * might be made available after the writeable check and 8755 * before the bit is set. This could cause a lost I/O 8756 * signal. tcp_poll() has a race breaker for this race 8757 * condition. Based on their implementation, we put 8758 * in the following code to cover it as well. 8759 */ 8760 if (sctp_writeable(sk)) 8761 mask |= EPOLLOUT | EPOLLWRNORM; 8762 } 8763 return mask; 8764 } 8765 8766 /******************************************************************** 8767 * 2nd Level Abstractions 8768 ********************************************************************/ 8769 8770 static struct sctp_bind_bucket *sctp_bucket_create( 8771 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) 8772 { 8773 struct sctp_bind_bucket *pp; 8774 8775 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); 8776 if (pp) { 8777 SCTP_DBG_OBJCNT_INC(bind_bucket); 8778 pp->port = snum; 8779 pp->fastreuse = 0; 8780 INIT_HLIST_HEAD(&pp->owner); 8781 pp->net = net; 8782 hlist_add_head(&pp->node, &head->chain); 8783 } 8784 return pp; 8785 } 8786 8787 /* Caller must hold hashbucket lock for this tb with local BH disabled */ 8788 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) 8789 { 8790 if (pp && hlist_empty(&pp->owner)) { 8791 __hlist_del(&pp->node); 8792 kmem_cache_free(sctp_bucket_cachep, pp); 8793 SCTP_DBG_OBJCNT_DEC(bind_bucket); 8794 } 8795 } 8796 8797 /* Release this socket's reference to a local port. */ 8798 static inline void __sctp_put_port(struct sock *sk) 8799 { 8800 struct sctp_bind_hashbucket *head = 8801 &sctp_port_hashtable[sctp_phashfn(sock_net(sk), 8802 inet_sk(sk)->inet_num)]; 8803 struct sctp_bind_bucket *pp; 8804 8805 spin_lock(&head->lock); 8806 pp = sctp_sk(sk)->bind_hash; 8807 __sk_del_bind_node(sk); 8808 sctp_sk(sk)->bind_hash = NULL; 8809 inet_sk(sk)->inet_num = 0; 8810 sctp_bucket_destroy(pp); 8811 spin_unlock(&head->lock); 8812 } 8813 8814 void sctp_put_port(struct sock *sk) 8815 { 8816 local_bh_disable(); 8817 __sctp_put_port(sk); 8818 local_bh_enable(); 8819 } 8820 8821 /* 8822 * The system picks an ephemeral port and choose an address set equivalent 8823 * to binding with a wildcard address. 8824 * One of those addresses will be the primary address for the association. 8825 * This automatically enables the multihoming capability of SCTP. 8826 */ 8827 static int sctp_autobind(struct sock *sk) 8828 { 8829 union sctp_addr autoaddr; 8830 struct sctp_af *af; 8831 __be16 port; 8832 8833 /* Initialize a local sockaddr structure to INADDR_ANY. */ 8834 af = sctp_sk(sk)->pf->af; 8835 8836 port = htons(inet_sk(sk)->inet_num); 8837 af->inaddr_any(&autoaddr, port); 8838 8839 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); 8840 } 8841 8842 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. 8843 * 8844 * From RFC 2292 8845 * 4.2 The cmsghdr Structure * 8846 * 8847 * When ancillary data is sent or received, any number of ancillary data 8848 * objects can be specified by the msg_control and msg_controllen members of 8849 * the msghdr structure, because each object is preceded by 8850 * a cmsghdr structure defining the object's length (the cmsg_len member). 8851 * Historically Berkeley-derived implementations have passed only one object 8852 * at a time, but this API allows multiple objects to be 8853 * passed in a single call to sendmsg() or recvmsg(). The following example 8854 * shows two ancillary data objects in a control buffer. 8855 * 8856 * |<--------------------------- msg_controllen -------------------------->| 8857 * | | 8858 * 8859 * |<----- ancillary data object ----->|<----- ancillary data object ----->| 8860 * 8861 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| 8862 * | | | 8863 * 8864 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | 8865 * 8866 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | 8867 * | | | | | 8868 * 8869 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 8870 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| 8871 * 8872 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| 8873 * 8874 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 8875 * ^ 8876 * | 8877 * 8878 * msg_control 8879 * points here 8880 */ 8881 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs) 8882 { 8883 struct msghdr *my_msg = (struct msghdr *)msg; 8884 struct cmsghdr *cmsg; 8885 8886 for_each_cmsghdr(cmsg, my_msg) { 8887 if (!CMSG_OK(my_msg, cmsg)) 8888 return -EINVAL; 8889 8890 /* Should we parse this header or ignore? */ 8891 if (cmsg->cmsg_level != IPPROTO_SCTP) 8892 continue; 8893 8894 /* Strictly check lengths following example in SCM code. */ 8895 switch (cmsg->cmsg_type) { 8896 case SCTP_INIT: 8897 /* SCTP Socket API Extension 8898 * 5.3.1 SCTP Initiation Structure (SCTP_INIT) 8899 * 8900 * This cmsghdr structure provides information for 8901 * initializing new SCTP associations with sendmsg(). 8902 * The SCTP_INITMSG socket option uses this same data 8903 * structure. This structure is not used for 8904 * recvmsg(). 8905 * 8906 * cmsg_level cmsg_type cmsg_data[] 8907 * ------------ ------------ ---------------------- 8908 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg 8909 */ 8910 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) 8911 return -EINVAL; 8912 8913 cmsgs->init = CMSG_DATA(cmsg); 8914 break; 8915 8916 case SCTP_SNDRCV: 8917 /* SCTP Socket API Extension 8918 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) 8919 * 8920 * This cmsghdr structure specifies SCTP options for 8921 * sendmsg() and describes SCTP header information 8922 * about a received message through recvmsg(). 8923 * 8924 * cmsg_level cmsg_type cmsg_data[] 8925 * ------------ ------------ ---------------------- 8926 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo 8927 */ 8928 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) 8929 return -EINVAL; 8930 8931 cmsgs->srinfo = CMSG_DATA(cmsg); 8932 8933 if (cmsgs->srinfo->sinfo_flags & 8934 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 8935 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 8936 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 8937 return -EINVAL; 8938 break; 8939 8940 case SCTP_SNDINFO: 8941 /* SCTP Socket API Extension 8942 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) 8943 * 8944 * This cmsghdr structure specifies SCTP options for 8945 * sendmsg(). This structure and SCTP_RCVINFO replaces 8946 * SCTP_SNDRCV which has been deprecated. 8947 * 8948 * cmsg_level cmsg_type cmsg_data[] 8949 * ------------ ------------ --------------------- 8950 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo 8951 */ 8952 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) 8953 return -EINVAL; 8954 8955 cmsgs->sinfo = CMSG_DATA(cmsg); 8956 8957 if (cmsgs->sinfo->snd_flags & 8958 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 8959 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 8960 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 8961 return -EINVAL; 8962 break; 8963 case SCTP_PRINFO: 8964 /* SCTP Socket API Extension 8965 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO) 8966 * 8967 * This cmsghdr structure specifies SCTP options for sendmsg(). 8968 * 8969 * cmsg_level cmsg_type cmsg_data[] 8970 * ------------ ------------ --------------------- 8971 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo 8972 */ 8973 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo))) 8974 return -EINVAL; 8975 8976 cmsgs->prinfo = CMSG_DATA(cmsg); 8977 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK) 8978 return -EINVAL; 8979 8980 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE) 8981 cmsgs->prinfo->pr_value = 0; 8982 break; 8983 case SCTP_AUTHINFO: 8984 /* SCTP Socket API Extension 8985 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO) 8986 * 8987 * This cmsghdr structure specifies SCTP options for sendmsg(). 8988 * 8989 * cmsg_level cmsg_type cmsg_data[] 8990 * ------------ ------------ --------------------- 8991 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo 8992 */ 8993 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo))) 8994 return -EINVAL; 8995 8996 cmsgs->authinfo = CMSG_DATA(cmsg); 8997 break; 8998 case SCTP_DSTADDRV4: 8999 case SCTP_DSTADDRV6: 9000 /* SCTP Socket API Extension 9001 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6) 9002 * 9003 * This cmsghdr structure specifies SCTP options for sendmsg(). 9004 * 9005 * cmsg_level cmsg_type cmsg_data[] 9006 * ------------ ------------ --------------------- 9007 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr 9008 * ------------ ------------ --------------------- 9009 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr 9010 */ 9011 cmsgs->addrs_msg = my_msg; 9012 break; 9013 default: 9014 return -EINVAL; 9015 } 9016 } 9017 9018 return 0; 9019 } 9020 9021 /* 9022 * Wait for a packet.. 9023 * Note: This function is the same function as in core/datagram.c 9024 * with a few modifications to make lksctp work. 9025 */ 9026 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p) 9027 { 9028 int error; 9029 DEFINE_WAIT(wait); 9030 9031 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 9032 9033 /* Socket errors? */ 9034 error = sock_error(sk); 9035 if (error) 9036 goto out; 9037 9038 if (!skb_queue_empty(&sk->sk_receive_queue)) 9039 goto ready; 9040 9041 /* Socket shut down? */ 9042 if (sk->sk_shutdown & RCV_SHUTDOWN) 9043 goto out; 9044 9045 /* Sequenced packets can come disconnected. If so we report the 9046 * problem. 9047 */ 9048 error = -ENOTCONN; 9049 9050 /* Is there a good reason to think that we may receive some data? */ 9051 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) 9052 goto out; 9053 9054 /* Handle signals. */ 9055 if (signal_pending(current)) 9056 goto interrupted; 9057 9058 /* Let another process have a go. Since we are going to sleep 9059 * anyway. Note: This may cause odd behaviors if the message 9060 * does not fit in the user's buffer, but this seems to be the 9061 * only way to honor MSG_DONTWAIT realistically. 9062 */ 9063 release_sock(sk); 9064 *timeo_p = schedule_timeout(*timeo_p); 9065 lock_sock(sk); 9066 9067 ready: 9068 finish_wait(sk_sleep(sk), &wait); 9069 return 0; 9070 9071 interrupted: 9072 error = sock_intr_errno(*timeo_p); 9073 9074 out: 9075 finish_wait(sk_sleep(sk), &wait); 9076 *err = error; 9077 return error; 9078 } 9079 9080 /* Receive a datagram. 9081 * Note: This is pretty much the same routine as in core/datagram.c 9082 * with a few changes to make lksctp work. 9083 */ 9084 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err) 9085 { 9086 int error; 9087 struct sk_buff *skb; 9088 long timeo; 9089 9090 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 9091 9092 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, 9093 MAX_SCHEDULE_TIMEOUT); 9094 9095 do { 9096 /* Again only user level code calls this function, 9097 * so nothing interrupt level 9098 * will suddenly eat the receive_queue. 9099 * 9100 * Look at current nfs client by the way... 9101 * However, this function was correct in any case. 8) 9102 */ 9103 if (flags & MSG_PEEK) { 9104 skb = skb_peek(&sk->sk_receive_queue); 9105 if (skb) 9106 refcount_inc(&skb->users); 9107 } else { 9108 skb = __skb_dequeue(&sk->sk_receive_queue); 9109 } 9110 9111 if (skb) 9112 return skb; 9113 9114 /* Caller is allowed not to check sk->sk_err before calling. */ 9115 error = sock_error(sk); 9116 if (error) 9117 goto no_packet; 9118 9119 if (sk->sk_shutdown & RCV_SHUTDOWN) 9120 break; 9121 9122 9123 /* User doesn't want to wait. */ 9124 error = -EAGAIN; 9125 if (!timeo) 9126 goto no_packet; 9127 } while (sctp_wait_for_packet(sk, err, &timeo) == 0); 9128 9129 return NULL; 9130 9131 no_packet: 9132 *err = error; 9133 return NULL; 9134 } 9135 9136 /* If sndbuf has changed, wake up per association sndbuf waiters. */ 9137 static void __sctp_write_space(struct sctp_association *asoc) 9138 { 9139 struct sock *sk = asoc->base.sk; 9140 9141 if (sctp_wspace(asoc) <= 0) 9142 return; 9143 9144 if (waitqueue_active(&asoc->wait)) 9145 wake_up_interruptible(&asoc->wait); 9146 9147 if (sctp_writeable(sk)) { 9148 struct socket_wq *wq; 9149 9150 rcu_read_lock(); 9151 wq = rcu_dereference(sk->sk_wq); 9152 if (wq) { 9153 if (waitqueue_active(&wq->wait)) 9154 wake_up_interruptible_poll(&wq->wait, EPOLLOUT | 9155 EPOLLWRNORM | EPOLLWRBAND); 9156 9157 /* Note that we try to include the Async I/O support 9158 * here by modeling from the current TCP/UDP code. 9159 * We have not tested with it yet. 9160 */ 9161 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 9162 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); 9163 } 9164 rcu_read_unlock(); 9165 } 9166 } 9167 9168 static void sctp_wake_up_waiters(struct sock *sk, 9169 struct sctp_association *asoc) 9170 { 9171 struct sctp_association *tmp = asoc; 9172 9173 /* We do accounting for the sndbuf space per association, 9174 * so we only need to wake our own association. 9175 */ 9176 if (asoc->ep->sndbuf_policy) 9177 return __sctp_write_space(asoc); 9178 9179 /* If association goes down and is just flushing its 9180 * outq, then just normally notify others. 9181 */ 9182 if (asoc->base.dead) 9183 return sctp_write_space(sk); 9184 9185 /* Accounting for the sndbuf space is per socket, so we 9186 * need to wake up others, try to be fair and in case of 9187 * other associations, let them have a go first instead 9188 * of just doing a sctp_write_space() call. 9189 * 9190 * Note that we reach sctp_wake_up_waiters() only when 9191 * associations free up queued chunks, thus we are under 9192 * lock and the list of associations on a socket is 9193 * guaranteed not to change. 9194 */ 9195 for (tmp = list_next_entry(tmp, asocs); 1; 9196 tmp = list_next_entry(tmp, asocs)) { 9197 /* Manually skip the head element. */ 9198 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) 9199 continue; 9200 /* Wake up association. */ 9201 __sctp_write_space(tmp); 9202 /* We've reached the end. */ 9203 if (tmp == asoc) 9204 break; 9205 } 9206 } 9207 9208 /* Do accounting for the sndbuf space. 9209 * Decrement the used sndbuf space of the corresponding association by the 9210 * data size which was just transmitted(freed). 9211 */ 9212 static void sctp_wfree(struct sk_buff *skb) 9213 { 9214 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; 9215 struct sctp_association *asoc = chunk->asoc; 9216 struct sock *sk = asoc->base.sk; 9217 9218 sk_mem_uncharge(sk, skb->truesize); 9219 sk_wmem_queued_add(sk, -(skb->truesize + sizeof(struct sctp_chunk))); 9220 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk); 9221 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), 9222 &sk->sk_wmem_alloc)); 9223 9224 if (chunk->shkey) { 9225 struct sctp_shared_key *shkey = chunk->shkey; 9226 9227 /* refcnt == 2 and !list_empty mean after this release, it's 9228 * not being used anywhere, and it's time to notify userland 9229 * that this shkey can be freed if it's been deactivated. 9230 */ 9231 if (shkey->deactivated && !list_empty(&shkey->key_list) && 9232 refcount_read(&shkey->refcnt) == 2) { 9233 struct sctp_ulpevent *ev; 9234 9235 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id, 9236 SCTP_AUTH_FREE_KEY, 9237 GFP_KERNEL); 9238 if (ev) 9239 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 9240 } 9241 sctp_auth_shkey_release(chunk->shkey); 9242 } 9243 9244 sock_wfree(skb); 9245 sctp_wake_up_waiters(sk, asoc); 9246 9247 sctp_association_put(asoc); 9248 } 9249 9250 /* Do accounting for the receive space on the socket. 9251 * Accounting for the association is done in ulpevent.c 9252 * We set this as a destructor for the cloned data skbs so that 9253 * accounting is done at the correct time. 9254 */ 9255 void sctp_sock_rfree(struct sk_buff *skb) 9256 { 9257 struct sock *sk = skb->sk; 9258 struct sctp_ulpevent *event = sctp_skb2event(skb); 9259 9260 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); 9261 9262 /* 9263 * Mimic the behavior of sock_rfree 9264 */ 9265 sk_mem_uncharge(sk, event->rmem_len); 9266 } 9267 9268 9269 /* Helper function to wait for space in the sndbuf. */ 9270 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, 9271 struct sctp_transport *transport, 9272 long *timeo_p, size_t msg_len) 9273 { 9274 struct sock *sk = asoc->base.sk; 9275 long current_timeo = *timeo_p; 9276 DEFINE_WAIT(wait); 9277 int err = 0; 9278 9279 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, 9280 *timeo_p, msg_len); 9281 9282 /* Increment the transport and association's refcnt. */ 9283 if (transport) 9284 sctp_transport_hold(transport); 9285 sctp_association_hold(asoc); 9286 9287 /* Wait on the association specific sndbuf space. */ 9288 for (;;) { 9289 prepare_to_wait_exclusive(&asoc->wait, &wait, 9290 TASK_INTERRUPTIBLE); 9291 if (asoc->base.dead) 9292 goto do_dead; 9293 if ((!*timeo_p) || (transport && transport->dead)) 9294 goto do_nonblock; 9295 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) 9296 goto do_error; 9297 if (signal_pending(current)) 9298 goto do_interrupted; 9299 if ((int)msg_len <= sctp_wspace(asoc) && 9300 sk_wmem_schedule(sk, msg_len)) 9301 break; 9302 9303 /* Let another process have a go. Since we are going 9304 * to sleep anyway. 9305 */ 9306 release_sock(sk); 9307 current_timeo = schedule_timeout(current_timeo); 9308 lock_sock(sk); 9309 if (sk != asoc->base.sk) 9310 goto do_error; 9311 9312 *timeo_p = current_timeo; 9313 } 9314 9315 out: 9316 finish_wait(&asoc->wait, &wait); 9317 9318 /* Release the transport and association's refcnt. */ 9319 if (transport) 9320 sctp_transport_put(transport); 9321 sctp_association_put(asoc); 9322 9323 return err; 9324 9325 do_dead: 9326 err = -ESRCH; 9327 goto out; 9328 9329 do_error: 9330 err = -EPIPE; 9331 goto out; 9332 9333 do_interrupted: 9334 err = sock_intr_errno(*timeo_p); 9335 goto out; 9336 9337 do_nonblock: 9338 err = -EAGAIN; 9339 goto out; 9340 } 9341 9342 void sctp_data_ready(struct sock *sk) 9343 { 9344 struct socket_wq *wq; 9345 9346 trace_sk_data_ready(sk); 9347 9348 rcu_read_lock(); 9349 wq = rcu_dereference(sk->sk_wq); 9350 if (skwq_has_sleeper(wq)) 9351 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | 9352 EPOLLRDNORM | EPOLLRDBAND); 9353 sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); 9354 rcu_read_unlock(); 9355 } 9356 9357 /* If socket sndbuf has changed, wake up all per association waiters. */ 9358 void sctp_write_space(struct sock *sk) 9359 { 9360 struct sctp_association *asoc; 9361 9362 /* Wake up the tasks in each wait queue. */ 9363 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { 9364 __sctp_write_space(asoc); 9365 } 9366 } 9367 9368 /* Is there any sndbuf space available on the socket? 9369 * 9370 * Note that sk_wmem_alloc is the sum of the send buffers on all of the 9371 * associations on the same socket. For a UDP-style socket with 9372 * multiple associations, it is possible for it to be "unwriteable" 9373 * prematurely. I assume that this is acceptable because 9374 * a premature "unwriteable" is better than an accidental "writeable" which 9375 * would cause an unwanted block under certain circumstances. For the 1-1 9376 * UDP-style sockets or TCP-style sockets, this code should work. 9377 * - Daisy 9378 */ 9379 static bool sctp_writeable(const struct sock *sk) 9380 { 9381 return READ_ONCE(sk->sk_sndbuf) > READ_ONCE(sk->sk_wmem_queued); 9382 } 9383 9384 /* Wait for an association to go into ESTABLISHED state. If timeout is 0, 9385 * returns immediately with EINPROGRESS. 9386 */ 9387 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) 9388 { 9389 struct sock *sk = asoc->base.sk; 9390 int err = 0; 9391 long current_timeo = *timeo_p; 9392 DEFINE_WAIT(wait); 9393 9394 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); 9395 9396 /* Increment the association's refcnt. */ 9397 sctp_association_hold(asoc); 9398 9399 for (;;) { 9400 prepare_to_wait_exclusive(&asoc->wait, &wait, 9401 TASK_INTERRUPTIBLE); 9402 if (!*timeo_p) 9403 goto do_nonblock; 9404 if (sk->sk_shutdown & RCV_SHUTDOWN) 9405 break; 9406 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || 9407 asoc->base.dead) 9408 goto do_error; 9409 if (signal_pending(current)) 9410 goto do_interrupted; 9411 9412 if (sctp_state(asoc, ESTABLISHED)) 9413 break; 9414 9415 /* Let another process have a go. Since we are going 9416 * to sleep anyway. 9417 */ 9418 release_sock(sk); 9419 current_timeo = schedule_timeout(current_timeo); 9420 lock_sock(sk); 9421 if (sk != asoc->base.sk) 9422 goto do_error; 9423 9424 *timeo_p = current_timeo; 9425 } 9426 9427 out: 9428 finish_wait(&asoc->wait, &wait); 9429 9430 /* Release the association's refcnt. */ 9431 sctp_association_put(asoc); 9432 9433 return err; 9434 9435 do_error: 9436 if (asoc->init_err_counter + 1 > asoc->max_init_attempts) 9437 err = -ETIMEDOUT; 9438 else 9439 err = -ECONNREFUSED; 9440 goto out; 9441 9442 do_interrupted: 9443 err = sock_intr_errno(*timeo_p); 9444 goto out; 9445 9446 do_nonblock: 9447 err = -EINPROGRESS; 9448 goto out; 9449 } 9450 9451 static int sctp_wait_for_accept(struct sock *sk, long timeo) 9452 { 9453 struct sctp_endpoint *ep; 9454 int err = 0; 9455 DEFINE_WAIT(wait); 9456 9457 ep = sctp_sk(sk)->ep; 9458 9459 9460 for (;;) { 9461 prepare_to_wait_exclusive(sk_sleep(sk), &wait, 9462 TASK_INTERRUPTIBLE); 9463 9464 if (list_empty(&ep->asocs)) { 9465 release_sock(sk); 9466 timeo = schedule_timeout(timeo); 9467 lock_sock(sk); 9468 } 9469 9470 err = -EINVAL; 9471 if (!sctp_sstate(sk, LISTENING) || 9472 (sk->sk_shutdown & RCV_SHUTDOWN)) 9473 break; 9474 9475 err = 0; 9476 if (!list_empty(&ep->asocs)) 9477 break; 9478 9479 err = sock_intr_errno(timeo); 9480 if (signal_pending(current)) 9481 break; 9482 9483 err = -EAGAIN; 9484 if (!timeo) 9485 break; 9486 } 9487 9488 finish_wait(sk_sleep(sk), &wait); 9489 9490 return err; 9491 } 9492 9493 static void sctp_wait_for_close(struct sock *sk, long timeout) 9494 { 9495 DEFINE_WAIT(wait); 9496 9497 do { 9498 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 9499 if (list_empty(&sctp_sk(sk)->ep->asocs)) 9500 break; 9501 release_sock(sk); 9502 timeout = schedule_timeout(timeout); 9503 lock_sock(sk); 9504 } while (!signal_pending(current) && timeout); 9505 9506 finish_wait(sk_sleep(sk), &wait); 9507 } 9508 9509 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) 9510 { 9511 struct sk_buff *frag; 9512 9513 if (!skb->data_len) 9514 goto done; 9515 9516 /* Don't forget the fragments. */ 9517 skb_walk_frags(skb, frag) 9518 sctp_skb_set_owner_r_frag(frag, sk); 9519 9520 done: 9521 sctp_skb_set_owner_r(skb, sk); 9522 } 9523 9524 /* Populate the fields of the newsk from the oldsk and migrate the assoc 9525 * and its messages to the newsk. 9526 */ 9527 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 9528 struct sctp_association *assoc, 9529 enum sctp_socket_type type) 9530 { 9531 struct sctp_sock *oldsp = sctp_sk(oldsk); 9532 struct sctp_sock *newsp = sctp_sk(newsk); 9533 struct sctp_bind_bucket *pp; /* hash list port iterator */ 9534 struct sctp_endpoint *newep = newsp->ep; 9535 struct sk_buff *skb, *tmp; 9536 struct sctp_ulpevent *event; 9537 struct sctp_bind_hashbucket *head; 9538 int err; 9539 9540 /* Restore the ep value that was overwritten with the above structure 9541 * copy. 9542 */ 9543 newsp->ep = newep; 9544 9545 /* Hook this new socket in to the bind_hash list. */ 9546 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), 9547 inet_sk(oldsk)->inet_num)]; 9548 spin_lock_bh(&head->lock); 9549 pp = sctp_sk(oldsk)->bind_hash; 9550 sk_add_bind_node(newsk, &pp->owner); 9551 sctp_sk(newsk)->bind_hash = pp; 9552 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; 9553 spin_unlock_bh(&head->lock); 9554 9555 /* Copy the bind_addr list from the original endpoint to the new 9556 * endpoint so that we can handle restarts properly 9557 */ 9558 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr, 9559 &oldsp->ep->base.bind_addr, GFP_KERNEL); 9560 if (err) 9561 return err; 9562 9563 sctp_auto_asconf_init(newsp); 9564 9565 /* Move any messages in the old socket's receive queue that are for the 9566 * peeled off association to the new socket's receive queue. 9567 */ 9568 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { 9569 event = sctp_skb2event(skb); 9570 if (event->asoc == assoc) { 9571 __skb_unlink(skb, &oldsk->sk_receive_queue); 9572 __skb_queue_tail(&newsk->sk_receive_queue, skb); 9573 sctp_skb_set_owner_r_frag(skb, newsk); 9574 } 9575 } 9576 9577 /* Clean up any messages pending delivery due to partial 9578 * delivery. Three cases: 9579 * 1) No partial deliver; no work. 9580 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. 9581 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. 9582 */ 9583 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); 9584 9585 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { 9586 struct sk_buff_head *queue; 9587 9588 /* Decide which queue to move pd_lobby skbs to. */ 9589 if (assoc->ulpq.pd_mode) { 9590 queue = &newsp->pd_lobby; 9591 } else 9592 queue = &newsk->sk_receive_queue; 9593 9594 /* Walk through the pd_lobby, looking for skbs that 9595 * need moved to the new socket. 9596 */ 9597 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { 9598 event = sctp_skb2event(skb); 9599 if (event->asoc == assoc) { 9600 __skb_unlink(skb, &oldsp->pd_lobby); 9601 __skb_queue_tail(queue, skb); 9602 sctp_skb_set_owner_r_frag(skb, newsk); 9603 } 9604 } 9605 9606 /* Clear up any skbs waiting for the partial 9607 * delivery to finish. 9608 */ 9609 if (assoc->ulpq.pd_mode) 9610 sctp_clear_pd(oldsk, NULL); 9611 9612 } 9613 9614 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag); 9615 9616 /* Set the type of socket to indicate that it is peeled off from the 9617 * original UDP-style socket or created with the accept() call on a 9618 * TCP-style socket.. 9619 */ 9620 newsp->type = type; 9621 9622 /* Mark the new socket "in-use" by the user so that any packets 9623 * that may arrive on the association after we've moved it are 9624 * queued to the backlog. This prevents a potential race between 9625 * backlog processing on the old socket and new-packet processing 9626 * on the new socket. 9627 * 9628 * The caller has just allocated newsk so we can guarantee that other 9629 * paths won't try to lock it and then oldsk. 9630 */ 9631 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 9632 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w); 9633 sctp_assoc_migrate(assoc, newsk); 9634 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w); 9635 9636 /* If the association on the newsk is already closed before accept() 9637 * is called, set RCV_SHUTDOWN flag. 9638 */ 9639 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { 9640 inet_sk_set_state(newsk, SCTP_SS_CLOSED); 9641 newsk->sk_shutdown |= RCV_SHUTDOWN; 9642 } else { 9643 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED); 9644 } 9645 9646 release_sock(newsk); 9647 9648 return 0; 9649 } 9650 9651 9652 /* This proto struct describes the ULP interface for SCTP. */ 9653 struct proto sctp_prot = { 9654 .name = "SCTP", 9655 .owner = THIS_MODULE, 9656 .close = sctp_close, 9657 .disconnect = sctp_disconnect, 9658 .accept = sctp_accept, 9659 .ioctl = sctp_ioctl, 9660 .init = sctp_init_sock, 9661 .destroy = sctp_destroy_sock, 9662 .shutdown = sctp_shutdown, 9663 .setsockopt = sctp_setsockopt, 9664 .getsockopt = sctp_getsockopt, 9665 .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt, 9666 .sendmsg = sctp_sendmsg, 9667 .recvmsg = sctp_recvmsg, 9668 .bind = sctp_bind, 9669 .bind_add = sctp_bind_add, 9670 .backlog_rcv = sctp_backlog_rcv, 9671 .hash = sctp_hash, 9672 .unhash = sctp_unhash, 9673 .no_autobind = true, 9674 .obj_size = sizeof(struct sctp_sock), 9675 .useroffset = offsetof(struct sctp_sock, subscribe), 9676 .usersize = offsetof(struct sctp_sock, initmsg) - 9677 offsetof(struct sctp_sock, subscribe) + 9678 sizeof_field(struct sctp_sock, initmsg), 9679 .sysctl_mem = sysctl_sctp_mem, 9680 .sysctl_rmem = sysctl_sctp_rmem, 9681 .sysctl_wmem = sysctl_sctp_wmem, 9682 .memory_pressure = &sctp_memory_pressure, 9683 .enter_memory_pressure = sctp_enter_memory_pressure, 9684 9685 .memory_allocated = &sctp_memory_allocated, 9686 .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc, 9687 9688 .sockets_allocated = &sctp_sockets_allocated, 9689 }; 9690 9691 #if IS_ENABLED(CONFIG_IPV6) 9692 9693 static void sctp_v6_destruct_sock(struct sock *sk) 9694 { 9695 inet6_sock_destruct(sk); 9696 } 9697 9698 static int sctp_v6_init_sock(struct sock *sk) 9699 { 9700 int ret = sctp_init_sock(sk); 9701 9702 if (!ret) 9703 sk->sk_destruct = sctp_v6_destruct_sock; 9704 9705 return ret; 9706 } 9707 9708 struct proto sctpv6_prot = { 9709 .name = "SCTPv6", 9710 .owner = THIS_MODULE, 9711 .close = sctp_close, 9712 .disconnect = sctp_disconnect, 9713 .accept = sctp_accept, 9714 .ioctl = sctp_ioctl, 9715 .init = sctp_v6_init_sock, 9716 .destroy = sctp_destroy_sock, 9717 .shutdown = sctp_shutdown, 9718 .setsockopt = sctp_setsockopt, 9719 .getsockopt = sctp_getsockopt, 9720 .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt, 9721 .sendmsg = sctp_sendmsg, 9722 .recvmsg = sctp_recvmsg, 9723 .bind = sctp_bind, 9724 .bind_add = sctp_bind_add, 9725 .backlog_rcv = sctp_backlog_rcv, 9726 .hash = sctp_hash, 9727 .unhash = sctp_unhash, 9728 .no_autobind = true, 9729 .obj_size = sizeof(struct sctp6_sock), 9730 .ipv6_pinfo_offset = offsetof(struct sctp6_sock, inet6), 9731 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), 9732 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - 9733 offsetof(struct sctp6_sock, sctp.subscribe) + 9734 sizeof_field(struct sctp6_sock, sctp.initmsg), 9735 .sysctl_mem = sysctl_sctp_mem, 9736 .sysctl_rmem = sysctl_sctp_rmem, 9737 .sysctl_wmem = sysctl_sctp_wmem, 9738 .memory_pressure = &sctp_memory_pressure, 9739 .enter_memory_pressure = sctp_enter_memory_pressure, 9740 9741 .memory_allocated = &sctp_memory_allocated, 9742 .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc, 9743 9744 .sockets_allocated = &sctp_sockets_allocated, 9745 }; 9746 #endif /* IS_ENABLED(CONFIG_IPV6) */ 9747