1 /* 2 * net/tipc/socket.c: TIPC socket API 3 * 4 * Copyright (c) 2001-2007, 2012-2016, Ericsson AB 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the names of the copyright holders nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <linux/rhashtable.h> 38 #include "core.h" 39 #include "name_table.h" 40 #include "node.h" 41 #include "link.h" 42 #include "name_distr.h" 43 #include "socket.h" 44 #include "bcast.h" 45 #include "netlink.h" 46 47 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ 48 #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */ 49 #define TIPC_FWD_MSG 1 50 #define TIPC_MAX_PORT 0xffffffff 51 #define TIPC_MIN_PORT 1 52 53 enum { 54 TIPC_LISTEN = TCP_LISTEN, 55 TIPC_ESTABLISHED = TCP_ESTABLISHED, 56 TIPC_OPEN = TCP_CLOSE, 57 TIPC_DISCONNECTING = TCP_CLOSE_WAIT, 58 TIPC_CONNECTING = TCP_SYN_SENT, 59 }; 60 61 /** 62 * struct tipc_sock - TIPC socket structure 63 * @sk: socket - interacts with 'port' and with user via the socket API 64 * @conn_type: TIPC type used when connection was established 65 * @conn_instance: TIPC instance used when connection was established 66 * @published: non-zero if port has one or more associated names 67 * @max_pkt: maximum packet size "hint" used when building messages sent by port 68 * @portid: unique port identity in TIPC socket hash table 69 * @phdr: preformatted message header used when sending messages 70 * #cong_links: list of congested links 71 * @publications: list of publications for port 72 * @blocking_link: address of the congested link we are currently sleeping on 73 * @pub_count: total # of publications port has made during its lifetime 74 * @probing_state: 75 * @conn_timeout: the time we can wait for an unresponded setup request 76 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue 77 * @cong_link_cnt: number of congested links 78 * @sent_unacked: # messages sent by socket, and not yet acked by peer 79 * @rcv_unacked: # messages read by user, but not yet acked back to peer 80 * @peer: 'connected' peer for dgram/rdm 81 * @node: hash table node 82 * @mc_method: cookie for use between socket and broadcast layer 83 * @rcu: rcu struct for tipc_sock 84 */ 85 struct tipc_sock { 86 struct sock sk; 87 u32 conn_type; 88 u32 conn_instance; 89 int published; 90 u32 max_pkt; 91 u32 portid; 92 struct tipc_msg phdr; 93 struct list_head cong_links; 94 struct list_head publications; 95 u32 pub_count; 96 uint conn_timeout; 97 atomic_t dupl_rcvcnt; 98 bool probe_unacked; 99 u16 cong_link_cnt; 100 u16 snt_unacked; 101 u16 snd_win; 102 u16 peer_caps; 103 u16 rcv_unacked; 104 u16 rcv_win; 105 struct sockaddr_tipc peer; 106 struct rhash_head node; 107 struct tipc_mc_method mc_method; 108 struct rcu_head rcu; 109 }; 110 111 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); 112 static void tipc_data_ready(struct sock *sk); 113 static void tipc_write_space(struct sock *sk); 114 static void tipc_sock_destruct(struct sock *sk); 115 static int tipc_release(struct socket *sock); 116 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); 117 static void tipc_sk_timeout(unsigned long data); 118 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, 119 struct tipc_name_seq const *seq); 120 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, 121 struct tipc_name_seq const *seq); 122 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); 123 static int tipc_sk_insert(struct tipc_sock *tsk); 124 static void tipc_sk_remove(struct tipc_sock *tsk); 125 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz); 126 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz); 127 128 static const struct proto_ops packet_ops; 129 static const struct proto_ops stream_ops; 130 static const struct proto_ops msg_ops; 131 static struct proto tipc_proto; 132 static const struct rhashtable_params tsk_rht_params; 133 134 static u32 tsk_own_node(struct tipc_sock *tsk) 135 { 136 return msg_prevnode(&tsk->phdr); 137 } 138 139 static u32 tsk_peer_node(struct tipc_sock *tsk) 140 { 141 return msg_destnode(&tsk->phdr); 142 } 143 144 static u32 tsk_peer_port(struct tipc_sock *tsk) 145 { 146 return msg_destport(&tsk->phdr); 147 } 148 149 static bool tsk_unreliable(struct tipc_sock *tsk) 150 { 151 return msg_src_droppable(&tsk->phdr) != 0; 152 } 153 154 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable) 155 { 156 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0); 157 } 158 159 static bool tsk_unreturnable(struct tipc_sock *tsk) 160 { 161 return msg_dest_droppable(&tsk->phdr) != 0; 162 } 163 164 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable) 165 { 166 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0); 167 } 168 169 static int tsk_importance(struct tipc_sock *tsk) 170 { 171 return msg_importance(&tsk->phdr); 172 } 173 174 static int tsk_set_importance(struct tipc_sock *tsk, int imp) 175 { 176 if (imp > TIPC_CRITICAL_IMPORTANCE) 177 return -EINVAL; 178 msg_set_importance(&tsk->phdr, (u32)imp); 179 return 0; 180 } 181 182 static struct tipc_sock *tipc_sk(const struct sock *sk) 183 { 184 return container_of(sk, struct tipc_sock, sk); 185 } 186 187 static bool tsk_conn_cong(struct tipc_sock *tsk) 188 { 189 return tsk->snt_unacked > tsk->snd_win; 190 } 191 192 /* tsk_blocks(): translate a buffer size in bytes to number of 193 * advertisable blocks, taking into account the ratio truesize(len)/len 194 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ 195 */ 196 static u16 tsk_adv_blocks(int len) 197 { 198 return len / FLOWCTL_BLK_SZ / 4; 199 } 200 201 /* tsk_inc(): increment counter for sent or received data 202 * - If block based flow control is not supported by peer we 203 * fall back to message based ditto, incrementing the counter 204 */ 205 static u16 tsk_inc(struct tipc_sock *tsk, int msglen) 206 { 207 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL)) 208 return ((msglen / FLOWCTL_BLK_SZ) + 1); 209 return 1; 210 } 211 212 /** 213 * tsk_advance_rx_queue - discard first buffer in socket receive queue 214 * 215 * Caller must hold socket lock 216 */ 217 static void tsk_advance_rx_queue(struct sock *sk) 218 { 219 kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); 220 } 221 222 /* tipc_sk_respond() : send response message back to sender 223 */ 224 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err) 225 { 226 u32 selector; 227 u32 dnode; 228 u32 onode = tipc_own_addr(sock_net(sk)); 229 230 if (!tipc_msg_reverse(onode, &skb, err)) 231 return; 232 233 dnode = msg_destnode(buf_msg(skb)); 234 selector = msg_origport(buf_msg(skb)); 235 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector); 236 } 237 238 /** 239 * tsk_rej_rx_queue - reject all buffers in socket receive queue 240 * 241 * Caller must hold socket lock 242 */ 243 static void tsk_rej_rx_queue(struct sock *sk) 244 { 245 struct sk_buff *skb; 246 247 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) 248 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT); 249 } 250 251 static bool tipc_sk_connected(struct sock *sk) 252 { 253 return sk->sk_state == TIPC_ESTABLISHED; 254 } 255 256 /* tipc_sk_type_connectionless - check if the socket is datagram socket 257 * @sk: socket 258 * 259 * Returns true if connection less, false otherwise 260 */ 261 static bool tipc_sk_type_connectionless(struct sock *sk) 262 { 263 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM; 264 } 265 266 /* tsk_peer_msg - verify if message was sent by connected port's peer 267 * 268 * Handles cases where the node's network address has changed from 269 * the default of <0.0.0> to its configured setting. 270 */ 271 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) 272 { 273 struct sock *sk = &tsk->sk; 274 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id); 275 u32 peer_port = tsk_peer_port(tsk); 276 u32 orig_node; 277 u32 peer_node; 278 279 if (unlikely(!tipc_sk_connected(sk))) 280 return false; 281 282 if (unlikely(msg_origport(msg) != peer_port)) 283 return false; 284 285 orig_node = msg_orignode(msg); 286 peer_node = tsk_peer_node(tsk); 287 288 if (likely(orig_node == peer_node)) 289 return true; 290 291 if (!orig_node && (peer_node == tn->own_addr)) 292 return true; 293 294 if (!peer_node && (orig_node == tn->own_addr)) 295 return true; 296 297 return false; 298 } 299 300 /* tipc_set_sk_state - set the sk_state of the socket 301 * @sk: socket 302 * 303 * Caller must hold socket lock 304 * 305 * Returns 0 on success, errno otherwise 306 */ 307 static int tipc_set_sk_state(struct sock *sk, int state) 308 { 309 int oldsk_state = sk->sk_state; 310 int res = -EINVAL; 311 312 switch (state) { 313 case TIPC_OPEN: 314 res = 0; 315 break; 316 case TIPC_LISTEN: 317 case TIPC_CONNECTING: 318 if (oldsk_state == TIPC_OPEN) 319 res = 0; 320 break; 321 case TIPC_ESTABLISHED: 322 if (oldsk_state == TIPC_CONNECTING || 323 oldsk_state == TIPC_OPEN) 324 res = 0; 325 break; 326 case TIPC_DISCONNECTING: 327 if (oldsk_state == TIPC_CONNECTING || 328 oldsk_state == TIPC_ESTABLISHED) 329 res = 0; 330 break; 331 } 332 333 if (!res) 334 sk->sk_state = state; 335 336 return res; 337 } 338 339 static int tipc_sk_sock_err(struct socket *sock, long *timeout) 340 { 341 struct sock *sk = sock->sk; 342 int err = sock_error(sk); 343 int typ = sock->type; 344 345 if (err) 346 return err; 347 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) { 348 if (sk->sk_state == TIPC_DISCONNECTING) 349 return -EPIPE; 350 else if (!tipc_sk_connected(sk)) 351 return -ENOTCONN; 352 } 353 if (!*timeout) 354 return -EAGAIN; 355 if (signal_pending(current)) 356 return sock_intr_errno(*timeout); 357 358 return 0; 359 } 360 361 #define tipc_wait_for_cond(sock_, timeout_, condition_) \ 362 ({ \ 363 int rc_ = 0; \ 364 int done_ = 0; \ 365 \ 366 while (!(condition_) && !done_) { \ 367 struct sock *sk_ = sock->sk; \ 368 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \ 369 \ 370 rc_ = tipc_sk_sock_err(sock_, timeout_); \ 371 if (rc_) \ 372 break; \ 373 prepare_to_wait(sk_sleep(sk_), &wait_, \ 374 TASK_INTERRUPTIBLE); \ 375 done_ = sk_wait_event(sk_, timeout_, \ 376 (condition_), &wait_); \ 377 remove_wait_queue(sk_sleep(sk_), &wait_); \ 378 } \ 379 rc_; \ 380 }) 381 382 /** 383 * tipc_sk_create - create a TIPC socket 384 * @net: network namespace (must be default network) 385 * @sock: pre-allocated socket structure 386 * @protocol: protocol indicator (must be 0) 387 * @kern: caused by kernel or by userspace? 388 * 389 * This routine creates additional data structures used by the TIPC socket, 390 * initializes them, and links them together. 391 * 392 * Returns 0 on success, errno otherwise 393 */ 394 static int tipc_sk_create(struct net *net, struct socket *sock, 395 int protocol, int kern) 396 { 397 struct tipc_net *tn; 398 const struct proto_ops *ops; 399 struct sock *sk; 400 struct tipc_sock *tsk; 401 struct tipc_msg *msg; 402 403 /* Validate arguments */ 404 if (unlikely(protocol != 0)) 405 return -EPROTONOSUPPORT; 406 407 switch (sock->type) { 408 case SOCK_STREAM: 409 ops = &stream_ops; 410 break; 411 case SOCK_SEQPACKET: 412 ops = &packet_ops; 413 break; 414 case SOCK_DGRAM: 415 case SOCK_RDM: 416 ops = &msg_ops; 417 break; 418 default: 419 return -EPROTOTYPE; 420 } 421 422 /* Allocate socket's protocol area */ 423 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern); 424 if (sk == NULL) 425 return -ENOMEM; 426 427 tsk = tipc_sk(sk); 428 tsk->max_pkt = MAX_PKT_DEFAULT; 429 INIT_LIST_HEAD(&tsk->publications); 430 INIT_LIST_HEAD(&tsk->cong_links); 431 msg = &tsk->phdr; 432 tn = net_generic(sock_net(sk), tipc_net_id); 433 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, 434 NAMED_H_SIZE, 0); 435 436 /* Finish initializing socket data structures */ 437 sock->ops = ops; 438 sock_init_data(sock, sk); 439 tipc_set_sk_state(sk, TIPC_OPEN); 440 if (tipc_sk_insert(tsk)) { 441 pr_warn("Socket create failed; port number exhausted\n"); 442 return -EINVAL; 443 } 444 msg_set_origport(msg, tsk->portid); 445 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk); 446 sk->sk_shutdown = 0; 447 sk->sk_backlog_rcv = tipc_backlog_rcv; 448 sk->sk_rcvbuf = sysctl_tipc_rmem[1]; 449 sk->sk_data_ready = tipc_data_ready; 450 sk->sk_write_space = tipc_write_space; 451 sk->sk_destruct = tipc_sock_destruct; 452 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; 453 atomic_set(&tsk->dupl_rcvcnt, 0); 454 455 /* Start out with safe limits until we receive an advertised window */ 456 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN); 457 tsk->rcv_win = tsk->snd_win; 458 459 if (tipc_sk_type_connectionless(sk)) { 460 tsk_set_unreturnable(tsk, true); 461 if (sock->type == SOCK_DGRAM) 462 tsk_set_unreliable(tsk, true); 463 } 464 465 return 0; 466 } 467 468 static void tipc_sk_callback(struct rcu_head *head) 469 { 470 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu); 471 472 sock_put(&tsk->sk); 473 } 474 475 /* Caller should hold socket lock for the socket. */ 476 static void __tipc_shutdown(struct socket *sock, int error) 477 { 478 struct sock *sk = sock->sk; 479 struct tipc_sock *tsk = tipc_sk(sk); 480 struct net *net = sock_net(sk); 481 long timeout = CONN_TIMEOUT_DEFAULT; 482 u32 dnode = tsk_peer_node(tsk); 483 struct sk_buff *skb; 484 485 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */ 486 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt && 487 !tsk_conn_cong(tsk))); 488 489 /* Reject all unreceived messages, except on an active connection 490 * (which disconnects locally & sends a 'FIN+' to peer). 491 */ 492 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 493 if (TIPC_SKB_CB(skb)->bytes_read) { 494 kfree_skb(skb); 495 continue; 496 } 497 if (!tipc_sk_type_connectionless(sk) && 498 sk->sk_state != TIPC_DISCONNECTING) { 499 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 500 tipc_node_remove_conn(net, dnode, tsk->portid); 501 } 502 tipc_sk_respond(sk, skb, error); 503 } 504 505 if (tipc_sk_type_connectionless(sk)) 506 return; 507 508 if (sk->sk_state != TIPC_DISCONNECTING) { 509 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, 510 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode, 511 tsk_own_node(tsk), tsk_peer_port(tsk), 512 tsk->portid, error); 513 if (skb) 514 tipc_node_xmit_skb(net, skb, dnode, tsk->portid); 515 tipc_node_remove_conn(net, dnode, tsk->portid); 516 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 517 } 518 } 519 520 /** 521 * tipc_release - destroy a TIPC socket 522 * @sock: socket to destroy 523 * 524 * This routine cleans up any messages that are still queued on the socket. 525 * For DGRAM and RDM socket types, all queued messages are rejected. 526 * For SEQPACKET and STREAM socket types, the first message is rejected 527 * and any others are discarded. (If the first message on a STREAM socket 528 * is partially-read, it is discarded and the next one is rejected instead.) 529 * 530 * NOTE: Rejected messages are not necessarily returned to the sender! They 531 * are returned or discarded according to the "destination droppable" setting 532 * specified for the message by the sender. 533 * 534 * Returns 0 on success, errno otherwise 535 */ 536 static int tipc_release(struct socket *sock) 537 { 538 struct sock *sk = sock->sk; 539 struct tipc_sock *tsk; 540 541 /* 542 * Exit if socket isn't fully initialized (occurs when a failed accept() 543 * releases a pre-allocated child socket that was never used) 544 */ 545 if (sk == NULL) 546 return 0; 547 548 tsk = tipc_sk(sk); 549 lock_sock(sk); 550 551 __tipc_shutdown(sock, TIPC_ERR_NO_PORT); 552 sk->sk_shutdown = SHUTDOWN_MASK; 553 tipc_sk_withdraw(tsk, 0, NULL); 554 sk_stop_timer(sk, &sk->sk_timer); 555 tipc_sk_remove(tsk); 556 557 /* Reject any messages that accumulated in backlog queue */ 558 release_sock(sk); 559 u32_list_purge(&tsk->cong_links); 560 tsk->cong_link_cnt = 0; 561 call_rcu(&tsk->rcu, tipc_sk_callback); 562 sock->sk = NULL; 563 564 return 0; 565 } 566 567 /** 568 * tipc_bind - associate or disassocate TIPC name(s) with a socket 569 * @sock: socket structure 570 * @uaddr: socket address describing name(s) and desired operation 571 * @uaddr_len: size of socket address data structure 572 * 573 * Name and name sequence binding is indicated using a positive scope value; 574 * a negative scope value unbinds the specified name. Specifying no name 575 * (i.e. a socket address length of 0) unbinds all names from the socket. 576 * 577 * Returns 0 on success, errno otherwise 578 * 579 * NOTE: This routine doesn't need to take the socket lock since it doesn't 580 * access any non-constant socket information. 581 */ 582 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, 583 int uaddr_len) 584 { 585 struct sock *sk = sock->sk; 586 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 587 struct tipc_sock *tsk = tipc_sk(sk); 588 int res = -EINVAL; 589 590 lock_sock(sk); 591 if (unlikely(!uaddr_len)) { 592 res = tipc_sk_withdraw(tsk, 0, NULL); 593 goto exit; 594 } 595 596 if (uaddr_len < sizeof(struct sockaddr_tipc)) { 597 res = -EINVAL; 598 goto exit; 599 } 600 if (addr->family != AF_TIPC) { 601 res = -EAFNOSUPPORT; 602 goto exit; 603 } 604 605 if (addr->addrtype == TIPC_ADDR_NAME) 606 addr->addr.nameseq.upper = addr->addr.nameseq.lower; 607 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { 608 res = -EAFNOSUPPORT; 609 goto exit; 610 } 611 612 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && 613 (addr->addr.nameseq.type != TIPC_TOP_SRV) && 614 (addr->addr.nameseq.type != TIPC_CFG_SRV)) { 615 res = -EACCES; 616 goto exit; 617 } 618 619 res = (addr->scope > 0) ? 620 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) : 621 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq); 622 exit: 623 release_sock(sk); 624 return res; 625 } 626 627 /** 628 * tipc_getname - get port ID of socket or peer socket 629 * @sock: socket structure 630 * @uaddr: area for returned socket address 631 * @uaddr_len: area for returned length of socket address 632 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID 633 * 634 * Returns 0 on success, errno otherwise 635 * 636 * NOTE: This routine doesn't need to take the socket lock since it only 637 * accesses socket information that is unchanging (or which changes in 638 * a completely predictable manner). 639 */ 640 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, 641 int *uaddr_len, int peer) 642 { 643 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 644 struct sock *sk = sock->sk; 645 struct tipc_sock *tsk = tipc_sk(sk); 646 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id); 647 648 memset(addr, 0, sizeof(*addr)); 649 if (peer) { 650 if ((!tipc_sk_connected(sk)) && 651 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING))) 652 return -ENOTCONN; 653 addr->addr.id.ref = tsk_peer_port(tsk); 654 addr->addr.id.node = tsk_peer_node(tsk); 655 } else { 656 addr->addr.id.ref = tsk->portid; 657 addr->addr.id.node = tn->own_addr; 658 } 659 660 *uaddr_len = sizeof(*addr); 661 addr->addrtype = TIPC_ADDR_ID; 662 addr->family = AF_TIPC; 663 addr->scope = 0; 664 addr->addr.name.domain = 0; 665 666 return 0; 667 } 668 669 /** 670 * tipc_poll - read and possibly block on pollmask 671 * @file: file structure associated with the socket 672 * @sock: socket for which to calculate the poll bits 673 * @wait: ??? 674 * 675 * Returns pollmask value 676 * 677 * COMMENTARY: 678 * It appears that the usual socket locking mechanisms are not useful here 679 * since the pollmask info is potentially out-of-date the moment this routine 680 * exits. TCP and other protocols seem to rely on higher level poll routines 681 * to handle any preventable race conditions, so TIPC will do the same ... 682 * 683 * IMPORTANT: The fact that a read or write operation is indicated does NOT 684 * imply that the operation will succeed, merely that it should be performed 685 * and will not block. 686 */ 687 static unsigned int tipc_poll(struct file *file, struct socket *sock, 688 poll_table *wait) 689 { 690 struct sock *sk = sock->sk; 691 struct tipc_sock *tsk = tipc_sk(sk); 692 u32 mask = 0; 693 694 sock_poll_wait(file, sk_sleep(sk), wait); 695 696 if (sk->sk_shutdown & RCV_SHUTDOWN) 697 mask |= POLLRDHUP | POLLIN | POLLRDNORM; 698 if (sk->sk_shutdown == SHUTDOWN_MASK) 699 mask |= POLLHUP; 700 701 switch (sk->sk_state) { 702 case TIPC_ESTABLISHED: 703 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk)) 704 mask |= POLLOUT; 705 /* fall thru' */ 706 case TIPC_LISTEN: 707 case TIPC_CONNECTING: 708 if (!skb_queue_empty(&sk->sk_receive_queue)) 709 mask |= (POLLIN | POLLRDNORM); 710 break; 711 case TIPC_OPEN: 712 if (!tsk->cong_link_cnt) 713 mask |= POLLOUT; 714 if (tipc_sk_type_connectionless(sk) && 715 (!skb_queue_empty(&sk->sk_receive_queue))) 716 mask |= (POLLIN | POLLRDNORM); 717 break; 718 case TIPC_DISCONNECTING: 719 mask = (POLLIN | POLLRDNORM | POLLHUP); 720 break; 721 } 722 723 return mask; 724 } 725 726 /** 727 * tipc_sendmcast - send multicast message 728 * @sock: socket structure 729 * @seq: destination address 730 * @msg: message to send 731 * @dlen: length of data to send 732 * @timeout: timeout to wait for wakeup 733 * 734 * Called from function tipc_sendmsg(), which has done all sanity checks 735 * Returns the number of bytes sent on success, or errno 736 */ 737 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, 738 struct msghdr *msg, size_t dlen, long timeout) 739 { 740 struct sock *sk = sock->sk; 741 struct tipc_sock *tsk = tipc_sk(sk); 742 struct tipc_msg *hdr = &tsk->phdr; 743 struct net *net = sock_net(sk); 744 int mtu = tipc_bcast_get_mtu(net); 745 struct tipc_mc_method *method = &tsk->mc_method; 746 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE); 747 struct sk_buff_head pkts; 748 struct tipc_nlist dsts; 749 int rc; 750 751 /* Block or return if any destination link is congested */ 752 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt); 753 if (unlikely(rc)) 754 return rc; 755 756 /* Lookup destination nodes */ 757 tipc_nlist_init(&dsts, tipc_own_addr(net)); 758 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower, 759 seq->upper, domain, &dsts); 760 if (!dsts.local && !dsts.remote) 761 return -EHOSTUNREACH; 762 763 /* Build message header */ 764 msg_set_type(hdr, TIPC_MCAST_MSG); 765 msg_set_hdr_sz(hdr, MCAST_H_SIZE); 766 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE); 767 msg_set_destport(hdr, 0); 768 msg_set_destnode(hdr, 0); 769 msg_set_nametype(hdr, seq->type); 770 msg_set_namelower(hdr, seq->lower); 771 msg_set_nameupper(hdr, seq->upper); 772 773 /* Build message as chain of buffers */ 774 skb_queue_head_init(&pkts); 775 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts); 776 777 /* Send message if build was successful */ 778 if (unlikely(rc == dlen)) 779 rc = tipc_mcast_xmit(net, &pkts, method, &dsts, 780 &tsk->cong_link_cnt); 781 782 tipc_nlist_purge(&dsts); 783 784 return rc ? rc : dlen; 785 } 786 787 /** 788 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets 789 * @arrvq: queue with arriving messages, to be cloned after destination lookup 790 * @inputq: queue with cloned messages, delivered to socket after dest lookup 791 * 792 * Multi-threaded: parallel calls with reference to same queues may occur 793 */ 794 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, 795 struct sk_buff_head *inputq) 796 { 797 struct tipc_msg *msg; 798 struct list_head dports; 799 u32 portid; 800 u32 scope = TIPC_CLUSTER_SCOPE; 801 struct sk_buff_head tmpq; 802 uint hsz; 803 struct sk_buff *skb, *_skb; 804 805 __skb_queue_head_init(&tmpq); 806 INIT_LIST_HEAD(&dports); 807 808 skb = tipc_skb_peek(arrvq, &inputq->lock); 809 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) { 810 msg = buf_msg(skb); 811 hsz = skb_headroom(skb) + msg_hdr_sz(msg); 812 813 if (in_own_node(net, msg_orignode(msg))) 814 scope = TIPC_NODE_SCOPE; 815 816 /* Create destination port list and message clones: */ 817 tipc_nametbl_mc_translate(net, 818 msg_nametype(msg), msg_namelower(msg), 819 msg_nameupper(msg), scope, &dports); 820 portid = u32_pop(&dports); 821 for (; portid; portid = u32_pop(&dports)) { 822 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC); 823 if (_skb) { 824 msg_set_destport(buf_msg(_skb), portid); 825 __skb_queue_tail(&tmpq, _skb); 826 continue; 827 } 828 pr_warn("Failed to clone mcast rcv buffer\n"); 829 } 830 /* Append to inputq if not already done by other thread */ 831 spin_lock_bh(&inputq->lock); 832 if (skb_peek(arrvq) == skb) { 833 skb_queue_splice_tail_init(&tmpq, inputq); 834 kfree_skb(__skb_dequeue(arrvq)); 835 } 836 spin_unlock_bh(&inputq->lock); 837 __skb_queue_purge(&tmpq); 838 kfree_skb(skb); 839 } 840 tipc_sk_rcv(net, inputq); 841 } 842 843 /** 844 * tipc_sk_proto_rcv - receive a connection mng protocol message 845 * @tsk: receiving socket 846 * @skb: pointer to message buffer. 847 */ 848 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb, 849 struct sk_buff_head *xmitq) 850 { 851 struct sock *sk = &tsk->sk; 852 u32 onode = tsk_own_node(tsk); 853 struct tipc_msg *hdr = buf_msg(skb); 854 int mtyp = msg_type(hdr); 855 bool conn_cong; 856 857 /* Ignore if connection cannot be validated: */ 858 if (!tsk_peer_msg(tsk, hdr)) 859 goto exit; 860 861 tsk->probe_unacked = false; 862 863 if (mtyp == CONN_PROBE) { 864 msg_set_type(hdr, CONN_PROBE_REPLY); 865 if (tipc_msg_reverse(onode, &skb, TIPC_OK)) 866 __skb_queue_tail(xmitq, skb); 867 return; 868 } else if (mtyp == CONN_ACK) { 869 conn_cong = tsk_conn_cong(tsk); 870 tsk->snt_unacked -= msg_conn_ack(hdr); 871 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) 872 tsk->snd_win = msg_adv_win(hdr); 873 if (conn_cong) 874 sk->sk_write_space(sk); 875 } else if (mtyp != CONN_PROBE_REPLY) { 876 pr_warn("Received unknown CONN_PROTO msg\n"); 877 } 878 exit: 879 kfree_skb(skb); 880 } 881 882 /** 883 * tipc_sendmsg - send message in connectionless manner 884 * @sock: socket structure 885 * @m: message to send 886 * @dsz: amount of user data to be sent 887 * 888 * Message must have an destination specified explicitly. 889 * Used for SOCK_RDM and SOCK_DGRAM messages, 890 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. 891 * (Note: 'SYN+' is prohibited on SOCK_STREAM.) 892 * 893 * Returns the number of bytes sent on success, or errno otherwise 894 */ 895 static int tipc_sendmsg(struct socket *sock, 896 struct msghdr *m, size_t dsz) 897 { 898 struct sock *sk = sock->sk; 899 int ret; 900 901 lock_sock(sk); 902 ret = __tipc_sendmsg(sock, m, dsz); 903 release_sock(sk); 904 905 return ret; 906 } 907 908 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen) 909 { 910 struct sock *sk = sock->sk; 911 struct net *net = sock_net(sk); 912 struct tipc_sock *tsk = tipc_sk(sk); 913 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 914 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 915 struct list_head *clinks = &tsk->cong_links; 916 bool syn = !tipc_sk_type_connectionless(sk); 917 struct tipc_msg *hdr = &tsk->phdr; 918 struct tipc_name_seq *seq; 919 struct sk_buff_head pkts; 920 u32 type, inst, domain; 921 u32 dnode, dport; 922 int mtu, rc; 923 924 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE)) 925 return -EMSGSIZE; 926 927 if (unlikely(!dest)) { 928 dest = &tsk->peer; 929 if (!syn || dest->family != AF_TIPC) 930 return -EDESTADDRREQ; 931 } 932 933 if (unlikely(m->msg_namelen < sizeof(*dest))) 934 return -EINVAL; 935 936 if (unlikely(dest->family != AF_TIPC)) 937 return -EINVAL; 938 939 if (unlikely(syn)) { 940 if (sk->sk_state == TIPC_LISTEN) 941 return -EPIPE; 942 if (sk->sk_state != TIPC_OPEN) 943 return -EISCONN; 944 if (tsk->published) 945 return -EOPNOTSUPP; 946 if (dest->addrtype == TIPC_ADDR_NAME) { 947 tsk->conn_type = dest->addr.name.name.type; 948 tsk->conn_instance = dest->addr.name.name.instance; 949 } 950 } 951 952 seq = &dest->addr.nameseq; 953 if (dest->addrtype == TIPC_ADDR_MCAST) 954 return tipc_sendmcast(sock, seq, m, dlen, timeout); 955 956 if (dest->addrtype == TIPC_ADDR_NAME) { 957 type = dest->addr.name.name.type; 958 inst = dest->addr.name.name.instance; 959 domain = dest->addr.name.domain; 960 dnode = domain; 961 msg_set_type(hdr, TIPC_NAMED_MSG); 962 msg_set_hdr_sz(hdr, NAMED_H_SIZE); 963 msg_set_nametype(hdr, type); 964 msg_set_nameinst(hdr, inst); 965 msg_set_lookup_scope(hdr, tipc_addr_scope(domain)); 966 dport = tipc_nametbl_translate(net, type, inst, &dnode); 967 msg_set_destnode(hdr, dnode); 968 msg_set_destport(hdr, dport); 969 if (unlikely(!dport && !dnode)) 970 return -EHOSTUNREACH; 971 972 } else if (dest->addrtype == TIPC_ADDR_ID) { 973 dnode = dest->addr.id.node; 974 msg_set_type(hdr, TIPC_DIRECT_MSG); 975 msg_set_lookup_scope(hdr, 0); 976 msg_set_destnode(hdr, dnode); 977 msg_set_destport(hdr, dest->addr.id.ref); 978 msg_set_hdr_sz(hdr, BASIC_H_SIZE); 979 } 980 981 /* Block or return if destination link is congested */ 982 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode)); 983 if (unlikely(rc)) 984 return rc; 985 986 skb_queue_head_init(&pkts); 987 mtu = tipc_node_get_mtu(net, dnode, tsk->portid); 988 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts); 989 if (unlikely(rc != dlen)) 990 return rc; 991 992 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid); 993 if (unlikely(rc == -ELINKCONG)) { 994 u32_push(clinks, dnode); 995 tsk->cong_link_cnt++; 996 rc = 0; 997 } 998 999 if (unlikely(syn && !rc)) 1000 tipc_set_sk_state(sk, TIPC_CONNECTING); 1001 1002 return rc ? rc : dlen; 1003 } 1004 1005 /** 1006 * tipc_sendstream - send stream-oriented data 1007 * @sock: socket structure 1008 * @m: data to send 1009 * @dsz: total length of data to be transmitted 1010 * 1011 * Used for SOCK_STREAM data. 1012 * 1013 * Returns the number of bytes sent on success (or partial success), 1014 * or errno if no data sent 1015 */ 1016 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz) 1017 { 1018 struct sock *sk = sock->sk; 1019 int ret; 1020 1021 lock_sock(sk); 1022 ret = __tipc_sendstream(sock, m, dsz); 1023 release_sock(sk); 1024 1025 return ret; 1026 } 1027 1028 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen) 1029 { 1030 struct sock *sk = sock->sk; 1031 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 1032 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 1033 struct tipc_sock *tsk = tipc_sk(sk); 1034 struct tipc_msg *hdr = &tsk->phdr; 1035 struct net *net = sock_net(sk); 1036 struct sk_buff_head pkts; 1037 u32 dnode = tsk_peer_node(tsk); 1038 int send, sent = 0; 1039 int rc = 0; 1040 1041 skb_queue_head_init(&pkts); 1042 1043 if (unlikely(dlen > INT_MAX)) 1044 return -EMSGSIZE; 1045 1046 /* Handle implicit connection setup */ 1047 if (unlikely(dest)) { 1048 rc = __tipc_sendmsg(sock, m, dlen); 1049 if (dlen && (dlen == rc)) 1050 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr)); 1051 return rc; 1052 } 1053 1054 do { 1055 rc = tipc_wait_for_cond(sock, &timeout, 1056 (!tsk->cong_link_cnt && 1057 !tsk_conn_cong(tsk) && 1058 tipc_sk_connected(sk))); 1059 if (unlikely(rc)) 1060 break; 1061 1062 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE); 1063 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts); 1064 if (unlikely(rc != send)) 1065 break; 1066 1067 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid); 1068 if (unlikely(rc == -ELINKCONG)) { 1069 tsk->cong_link_cnt = 1; 1070 rc = 0; 1071 } 1072 if (likely(!rc)) { 1073 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE); 1074 sent += send; 1075 } 1076 } while (sent < dlen && !rc); 1077 1078 return rc ? rc : sent; 1079 } 1080 1081 /** 1082 * tipc_send_packet - send a connection-oriented message 1083 * @sock: socket structure 1084 * @m: message to send 1085 * @dsz: length of data to be transmitted 1086 * 1087 * Used for SOCK_SEQPACKET messages. 1088 * 1089 * Returns the number of bytes sent on success, or errno otherwise 1090 */ 1091 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz) 1092 { 1093 if (dsz > TIPC_MAX_USER_MSG_SIZE) 1094 return -EMSGSIZE; 1095 1096 return tipc_sendstream(sock, m, dsz); 1097 } 1098 1099 /* tipc_sk_finish_conn - complete the setup of a connection 1100 */ 1101 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, 1102 u32 peer_node) 1103 { 1104 struct sock *sk = &tsk->sk; 1105 struct net *net = sock_net(sk); 1106 struct tipc_msg *msg = &tsk->phdr; 1107 1108 msg_set_destnode(msg, peer_node); 1109 msg_set_destport(msg, peer_port); 1110 msg_set_type(msg, TIPC_CONN_MSG); 1111 msg_set_lookup_scope(msg, 0); 1112 msg_set_hdr_sz(msg, SHORT_H_SIZE); 1113 1114 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL); 1115 tipc_set_sk_state(sk, TIPC_ESTABLISHED); 1116 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port); 1117 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid); 1118 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node); 1119 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) 1120 return; 1121 1122 /* Fall back to message based flow control */ 1123 tsk->rcv_win = FLOWCTL_MSG_WIN; 1124 tsk->snd_win = FLOWCTL_MSG_WIN; 1125 } 1126 1127 /** 1128 * set_orig_addr - capture sender's address for received message 1129 * @m: descriptor for message info 1130 * @msg: received message header 1131 * 1132 * Note: Address is not captured if not requested by receiver. 1133 */ 1134 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) 1135 { 1136 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); 1137 1138 if (addr) { 1139 addr->family = AF_TIPC; 1140 addr->addrtype = TIPC_ADDR_ID; 1141 memset(&addr->addr, 0, sizeof(addr->addr)); 1142 addr->addr.id.ref = msg_origport(msg); 1143 addr->addr.id.node = msg_orignode(msg); 1144 addr->addr.name.domain = 0; /* could leave uninitialized */ 1145 addr->scope = 0; /* could leave uninitialized */ 1146 m->msg_namelen = sizeof(struct sockaddr_tipc); 1147 } 1148 } 1149 1150 /** 1151 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message 1152 * @m: descriptor for message info 1153 * @msg: received message header 1154 * @tsk: TIPC port associated with message 1155 * 1156 * Note: Ancillary data is not captured if not requested by receiver. 1157 * 1158 * Returns 0 if successful, otherwise errno 1159 */ 1160 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg, 1161 struct tipc_sock *tsk) 1162 { 1163 u32 anc_data[3]; 1164 u32 err; 1165 u32 dest_type; 1166 int has_name; 1167 int res; 1168 1169 if (likely(m->msg_controllen == 0)) 1170 return 0; 1171 1172 /* Optionally capture errored message object(s) */ 1173 err = msg ? msg_errcode(msg) : 0; 1174 if (unlikely(err)) { 1175 anc_data[0] = err; 1176 anc_data[1] = msg_data_sz(msg); 1177 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); 1178 if (res) 1179 return res; 1180 if (anc_data[1]) { 1181 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], 1182 msg_data(msg)); 1183 if (res) 1184 return res; 1185 } 1186 } 1187 1188 /* Optionally capture message destination object */ 1189 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; 1190 switch (dest_type) { 1191 case TIPC_NAMED_MSG: 1192 has_name = 1; 1193 anc_data[0] = msg_nametype(msg); 1194 anc_data[1] = msg_namelower(msg); 1195 anc_data[2] = msg_namelower(msg); 1196 break; 1197 case TIPC_MCAST_MSG: 1198 has_name = 1; 1199 anc_data[0] = msg_nametype(msg); 1200 anc_data[1] = msg_namelower(msg); 1201 anc_data[2] = msg_nameupper(msg); 1202 break; 1203 case TIPC_CONN_MSG: 1204 has_name = (tsk->conn_type != 0); 1205 anc_data[0] = tsk->conn_type; 1206 anc_data[1] = tsk->conn_instance; 1207 anc_data[2] = tsk->conn_instance; 1208 break; 1209 default: 1210 has_name = 0; 1211 } 1212 if (has_name) { 1213 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); 1214 if (res) 1215 return res; 1216 } 1217 1218 return 0; 1219 } 1220 1221 static void tipc_sk_send_ack(struct tipc_sock *tsk) 1222 { 1223 struct sock *sk = &tsk->sk; 1224 struct net *net = sock_net(sk); 1225 struct sk_buff *skb = NULL; 1226 struct tipc_msg *msg; 1227 u32 peer_port = tsk_peer_port(tsk); 1228 u32 dnode = tsk_peer_node(tsk); 1229 1230 if (!tipc_sk_connected(sk)) 1231 return; 1232 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, 1233 dnode, tsk_own_node(tsk), peer_port, 1234 tsk->portid, TIPC_OK); 1235 if (!skb) 1236 return; 1237 msg = buf_msg(skb); 1238 msg_set_conn_ack(msg, tsk->rcv_unacked); 1239 tsk->rcv_unacked = 0; 1240 1241 /* Adjust to and advertize the correct window limit */ 1242 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) { 1243 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf); 1244 msg_set_adv_win(msg, tsk->rcv_win); 1245 } 1246 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg)); 1247 } 1248 1249 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) 1250 { 1251 struct sock *sk = sock->sk; 1252 DEFINE_WAIT(wait); 1253 long timeo = *timeop; 1254 int err; 1255 1256 for (;;) { 1257 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1258 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { 1259 if (sk->sk_shutdown & RCV_SHUTDOWN) { 1260 err = -ENOTCONN; 1261 break; 1262 } 1263 release_sock(sk); 1264 timeo = schedule_timeout(timeo); 1265 lock_sock(sk); 1266 } 1267 err = 0; 1268 if (!skb_queue_empty(&sk->sk_receive_queue)) 1269 break; 1270 err = -EAGAIN; 1271 if (!timeo) 1272 break; 1273 err = sock_intr_errno(timeo); 1274 if (signal_pending(current)) 1275 break; 1276 } 1277 finish_wait(sk_sleep(sk), &wait); 1278 *timeop = timeo; 1279 return err; 1280 } 1281 1282 /** 1283 * tipc_recvmsg - receive packet-oriented message 1284 * @m: descriptor for message info 1285 * @buf_len: total size of user buffer area 1286 * @flags: receive flags 1287 * 1288 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. 1289 * If the complete message doesn't fit in user area, truncate it. 1290 * 1291 * Returns size of returned message data, errno otherwise 1292 */ 1293 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len, 1294 int flags) 1295 { 1296 struct sock *sk = sock->sk; 1297 struct tipc_sock *tsk = tipc_sk(sk); 1298 struct sk_buff *buf; 1299 struct tipc_msg *msg; 1300 bool is_connectionless = tipc_sk_type_connectionless(sk); 1301 long timeo; 1302 unsigned int sz; 1303 u32 err; 1304 int res, hlen; 1305 1306 /* Catch invalid receive requests */ 1307 if (unlikely(!buf_len)) 1308 return -EINVAL; 1309 1310 lock_sock(sk); 1311 1312 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) { 1313 res = -ENOTCONN; 1314 goto exit; 1315 } 1316 1317 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1318 restart: 1319 1320 /* Look for a message in receive queue; wait if necessary */ 1321 res = tipc_wait_for_rcvmsg(sock, &timeo); 1322 if (res) 1323 goto exit; 1324 1325 /* Look at first message in receive queue */ 1326 buf = skb_peek(&sk->sk_receive_queue); 1327 msg = buf_msg(buf); 1328 sz = msg_data_sz(msg); 1329 hlen = msg_hdr_sz(msg); 1330 err = msg_errcode(msg); 1331 1332 /* Discard an empty non-errored message & try again */ 1333 if ((!sz) && (!err)) { 1334 tsk_advance_rx_queue(sk); 1335 goto restart; 1336 } 1337 1338 /* Capture sender's address (optional) */ 1339 set_orig_addr(m, msg); 1340 1341 /* Capture ancillary data (optional) */ 1342 res = tipc_sk_anc_data_recv(m, msg, tsk); 1343 if (res) 1344 goto exit; 1345 1346 /* Capture message data (if valid) & compute return value (always) */ 1347 if (!err) { 1348 if (unlikely(buf_len < sz)) { 1349 sz = buf_len; 1350 m->msg_flags |= MSG_TRUNC; 1351 } 1352 res = skb_copy_datagram_msg(buf, hlen, m, sz); 1353 if (res) 1354 goto exit; 1355 res = sz; 1356 } else { 1357 if (is_connectionless || err == TIPC_CONN_SHUTDOWN || 1358 m->msg_control) 1359 res = 0; 1360 else 1361 res = -ECONNRESET; 1362 } 1363 1364 if (unlikely(flags & MSG_PEEK)) 1365 goto exit; 1366 1367 if (likely(!is_connectionless)) { 1368 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz); 1369 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4))) 1370 tipc_sk_send_ack(tsk); 1371 } 1372 tsk_advance_rx_queue(sk); 1373 exit: 1374 release_sock(sk); 1375 return res; 1376 } 1377 1378 /** 1379 * tipc_recv_stream - receive stream-oriented data 1380 * @m: descriptor for message info 1381 * @buf_len: total size of user buffer area 1382 * @flags: receive flags 1383 * 1384 * Used for SOCK_STREAM messages only. If not enough data is available 1385 * will optionally wait for more; never truncates data. 1386 * 1387 * Returns size of returned message data, errno otherwise 1388 */ 1389 static int tipc_recv_stream(struct socket *sock, struct msghdr *m, 1390 size_t buf_len, int flags) 1391 { 1392 struct sock *sk = sock->sk; 1393 struct tipc_sock *tsk = tipc_sk(sk); 1394 struct sk_buff *buf; 1395 struct tipc_msg *msg; 1396 long timeo; 1397 unsigned int sz; 1398 int target; 1399 int sz_copied = 0; 1400 u32 err; 1401 int res = 0, hlen; 1402 1403 /* Catch invalid receive attempts */ 1404 if (unlikely(!buf_len)) 1405 return -EINVAL; 1406 1407 lock_sock(sk); 1408 1409 if (unlikely(sk->sk_state == TIPC_OPEN)) { 1410 res = -ENOTCONN; 1411 goto exit; 1412 } 1413 1414 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); 1415 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1416 1417 restart: 1418 /* Look for a message in receive queue; wait if necessary */ 1419 res = tipc_wait_for_rcvmsg(sock, &timeo); 1420 if (res) 1421 goto exit; 1422 1423 /* Look at first message in receive queue */ 1424 buf = skb_peek(&sk->sk_receive_queue); 1425 msg = buf_msg(buf); 1426 sz = msg_data_sz(msg); 1427 hlen = msg_hdr_sz(msg); 1428 err = msg_errcode(msg); 1429 1430 /* Discard an empty non-errored message & try again */ 1431 if ((!sz) && (!err)) { 1432 tsk_advance_rx_queue(sk); 1433 goto restart; 1434 } 1435 1436 /* Optionally capture sender's address & ancillary data of first msg */ 1437 if (sz_copied == 0) { 1438 set_orig_addr(m, msg); 1439 res = tipc_sk_anc_data_recv(m, msg, tsk); 1440 if (res) 1441 goto exit; 1442 } 1443 1444 /* Capture message data (if valid) & compute return value (always) */ 1445 if (!err) { 1446 u32 offset = TIPC_SKB_CB(buf)->bytes_read; 1447 u32 needed; 1448 int sz_to_copy; 1449 1450 sz -= offset; 1451 needed = (buf_len - sz_copied); 1452 sz_to_copy = min(sz, needed); 1453 1454 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy); 1455 if (res) 1456 goto exit; 1457 1458 sz_copied += sz_to_copy; 1459 1460 if (sz_to_copy < sz) { 1461 if (!(flags & MSG_PEEK)) 1462 TIPC_SKB_CB(buf)->bytes_read = 1463 offset + sz_to_copy; 1464 goto exit; 1465 } 1466 } else { 1467 if (sz_copied != 0) 1468 goto exit; /* can't add error msg to valid data */ 1469 1470 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) 1471 res = 0; 1472 else 1473 res = -ECONNRESET; 1474 } 1475 1476 if (unlikely(flags & MSG_PEEK)) 1477 goto exit; 1478 1479 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz); 1480 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4))) 1481 tipc_sk_send_ack(tsk); 1482 tsk_advance_rx_queue(sk); 1483 1484 /* Loop around if more data is required */ 1485 if ((sz_copied < buf_len) && /* didn't get all requested data */ 1486 (!skb_queue_empty(&sk->sk_receive_queue) || 1487 (sz_copied < target)) && /* and more is ready or required */ 1488 (!err)) /* and haven't reached a FIN */ 1489 goto restart; 1490 1491 exit: 1492 release_sock(sk); 1493 return sz_copied ? sz_copied : res; 1494 } 1495 1496 /** 1497 * tipc_write_space - wake up thread if port congestion is released 1498 * @sk: socket 1499 */ 1500 static void tipc_write_space(struct sock *sk) 1501 { 1502 struct socket_wq *wq; 1503 1504 rcu_read_lock(); 1505 wq = rcu_dereference(sk->sk_wq); 1506 if (skwq_has_sleeper(wq)) 1507 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | 1508 POLLWRNORM | POLLWRBAND); 1509 rcu_read_unlock(); 1510 } 1511 1512 /** 1513 * tipc_data_ready - wake up threads to indicate messages have been received 1514 * @sk: socket 1515 * @len: the length of messages 1516 */ 1517 static void tipc_data_ready(struct sock *sk) 1518 { 1519 struct socket_wq *wq; 1520 1521 rcu_read_lock(); 1522 wq = rcu_dereference(sk->sk_wq); 1523 if (skwq_has_sleeper(wq)) 1524 wake_up_interruptible_sync_poll(&wq->wait, POLLIN | 1525 POLLRDNORM | POLLRDBAND); 1526 rcu_read_unlock(); 1527 } 1528 1529 static void tipc_sock_destruct(struct sock *sk) 1530 { 1531 __skb_queue_purge(&sk->sk_receive_queue); 1532 } 1533 1534 /** 1535 * filter_connect - Handle all incoming messages for a connection-based socket 1536 * @tsk: TIPC socket 1537 * @skb: pointer to message buffer. Set to NULL if buffer is consumed 1538 * 1539 * Returns true if everything ok, false otherwise 1540 */ 1541 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb) 1542 { 1543 struct sock *sk = &tsk->sk; 1544 struct net *net = sock_net(sk); 1545 struct tipc_msg *hdr = buf_msg(skb); 1546 1547 if (unlikely(msg_mcast(hdr))) 1548 return false; 1549 1550 switch (sk->sk_state) { 1551 case TIPC_CONNECTING: 1552 /* Accept only ACK or NACK message */ 1553 if (unlikely(!msg_connected(hdr))) 1554 return false; 1555 1556 if (unlikely(msg_errcode(hdr))) { 1557 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 1558 sk->sk_err = ECONNREFUSED; 1559 return true; 1560 } 1561 1562 if (unlikely(!msg_isdata(hdr))) { 1563 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 1564 sk->sk_err = EINVAL; 1565 return true; 1566 } 1567 1568 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr)); 1569 msg_set_importance(&tsk->phdr, msg_importance(hdr)); 1570 1571 /* If 'ACK+' message, add to socket receive queue */ 1572 if (msg_data_sz(hdr)) 1573 return true; 1574 1575 /* If empty 'ACK-' message, wake up sleeping connect() */ 1576 if (waitqueue_active(sk_sleep(sk))) 1577 wake_up_interruptible(sk_sleep(sk)); 1578 1579 /* 'ACK-' message is neither accepted nor rejected: */ 1580 msg_set_dest_droppable(hdr, 1); 1581 return false; 1582 1583 case TIPC_OPEN: 1584 case TIPC_DISCONNECTING: 1585 break; 1586 case TIPC_LISTEN: 1587 /* Accept only SYN message */ 1588 if (!msg_connected(hdr) && !(msg_errcode(hdr))) 1589 return true; 1590 break; 1591 case TIPC_ESTABLISHED: 1592 /* Accept only connection-based messages sent by peer */ 1593 if (unlikely(!tsk_peer_msg(tsk, hdr))) 1594 return false; 1595 1596 if (unlikely(msg_errcode(hdr))) { 1597 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 1598 /* Let timer expire on it's own */ 1599 tipc_node_remove_conn(net, tsk_peer_node(tsk), 1600 tsk->portid); 1601 sk->sk_state_change(sk); 1602 } 1603 return true; 1604 default: 1605 pr_err("Unknown sk_state %u\n", sk->sk_state); 1606 } 1607 1608 return false; 1609 } 1610 1611 /** 1612 * rcvbuf_limit - get proper overload limit of socket receive queue 1613 * @sk: socket 1614 * @skb: message 1615 * 1616 * For connection oriented messages, irrespective of importance, 1617 * default queue limit is 2 MB. 1618 * 1619 * For connectionless messages, queue limits are based on message 1620 * importance as follows: 1621 * 1622 * TIPC_LOW_IMPORTANCE (2 MB) 1623 * TIPC_MEDIUM_IMPORTANCE (4 MB) 1624 * TIPC_HIGH_IMPORTANCE (8 MB) 1625 * TIPC_CRITICAL_IMPORTANCE (16 MB) 1626 * 1627 * Returns overload limit according to corresponding message importance 1628 */ 1629 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb) 1630 { 1631 struct tipc_sock *tsk = tipc_sk(sk); 1632 struct tipc_msg *hdr = buf_msg(skb); 1633 1634 if (unlikely(!msg_connected(hdr))) 1635 return sk->sk_rcvbuf << msg_importance(hdr); 1636 1637 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL)) 1638 return sk->sk_rcvbuf; 1639 1640 return FLOWCTL_MSG_LIM; 1641 } 1642 1643 /** 1644 * filter_rcv - validate incoming message 1645 * @sk: socket 1646 * @skb: pointer to message. 1647 * 1648 * Enqueues message on receive queue if acceptable; optionally handles 1649 * disconnect indication for a connected socket. 1650 * 1651 * Called with socket lock already taken 1652 * 1653 * Returns true if message was added to socket receive queue, otherwise false 1654 */ 1655 static bool filter_rcv(struct sock *sk, struct sk_buff *skb, 1656 struct sk_buff_head *xmitq) 1657 { 1658 struct tipc_sock *tsk = tipc_sk(sk); 1659 struct tipc_msg *hdr = buf_msg(skb); 1660 unsigned int limit = rcvbuf_limit(sk, skb); 1661 int err = TIPC_OK; 1662 int usr = msg_user(hdr); 1663 u32 onode; 1664 1665 if (unlikely(msg_user(hdr) == CONN_MANAGER)) { 1666 tipc_sk_proto_rcv(tsk, skb, xmitq); 1667 return false; 1668 } 1669 1670 if (unlikely(usr == SOCK_WAKEUP)) { 1671 onode = msg_orignode(hdr); 1672 kfree_skb(skb); 1673 u32_del(&tsk->cong_links, onode); 1674 tsk->cong_link_cnt--; 1675 sk->sk_write_space(sk); 1676 return false; 1677 } 1678 1679 /* Drop if illegal message type */ 1680 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) { 1681 kfree_skb(skb); 1682 return false; 1683 } 1684 1685 /* Reject if wrong message type for current socket state */ 1686 if (tipc_sk_type_connectionless(sk)) { 1687 if (msg_connected(hdr)) { 1688 err = TIPC_ERR_NO_PORT; 1689 goto reject; 1690 } 1691 } else if (unlikely(!filter_connect(tsk, skb))) { 1692 err = TIPC_ERR_NO_PORT; 1693 goto reject; 1694 } 1695 1696 /* Reject message if there isn't room to queue it */ 1697 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) { 1698 err = TIPC_ERR_OVERLOAD; 1699 goto reject; 1700 } 1701 1702 /* Enqueue message */ 1703 TIPC_SKB_CB(skb)->bytes_read = 0; 1704 __skb_queue_tail(&sk->sk_receive_queue, skb); 1705 skb_set_owner_r(skb, sk); 1706 1707 sk->sk_data_ready(sk); 1708 return true; 1709 1710 reject: 1711 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err)) 1712 __skb_queue_tail(xmitq, skb); 1713 return false; 1714 } 1715 1716 /** 1717 * tipc_backlog_rcv - handle incoming message from backlog queue 1718 * @sk: socket 1719 * @skb: message 1720 * 1721 * Caller must hold socket lock 1722 * 1723 * Returns 0 1724 */ 1725 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb) 1726 { 1727 unsigned int truesize = skb->truesize; 1728 struct sk_buff_head xmitq; 1729 u32 dnode, selector; 1730 1731 __skb_queue_head_init(&xmitq); 1732 1733 if (likely(filter_rcv(sk, skb, &xmitq))) { 1734 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt); 1735 return 0; 1736 } 1737 1738 if (skb_queue_empty(&xmitq)) 1739 return 0; 1740 1741 /* Send response/rejected message */ 1742 skb = __skb_dequeue(&xmitq); 1743 dnode = msg_destnode(buf_msg(skb)); 1744 selector = msg_origport(buf_msg(skb)); 1745 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector); 1746 return 0; 1747 } 1748 1749 /** 1750 * tipc_sk_enqueue - extract all buffers with destination 'dport' from 1751 * inputq and try adding them to socket or backlog queue 1752 * @inputq: list of incoming buffers with potentially different destinations 1753 * @sk: socket where the buffers should be enqueued 1754 * @dport: port number for the socket 1755 * 1756 * Caller must hold socket lock 1757 */ 1758 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk, 1759 u32 dport, struct sk_buff_head *xmitq) 1760 { 1761 unsigned long time_limit = jiffies + 2; 1762 struct sk_buff *skb; 1763 unsigned int lim; 1764 atomic_t *dcnt; 1765 u32 onode; 1766 1767 while (skb_queue_len(inputq)) { 1768 if (unlikely(time_after_eq(jiffies, time_limit))) 1769 return; 1770 1771 skb = tipc_skb_dequeue(inputq, dport); 1772 if (unlikely(!skb)) 1773 return; 1774 1775 /* Add message directly to receive queue if possible */ 1776 if (!sock_owned_by_user(sk)) { 1777 filter_rcv(sk, skb, xmitq); 1778 continue; 1779 } 1780 1781 /* Try backlog, compensating for double-counted bytes */ 1782 dcnt = &tipc_sk(sk)->dupl_rcvcnt; 1783 if (!sk->sk_backlog.len) 1784 atomic_set(dcnt, 0); 1785 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt); 1786 if (likely(!sk_add_backlog(sk, skb, lim))) 1787 continue; 1788 1789 /* Overload => reject message back to sender */ 1790 onode = tipc_own_addr(sock_net(sk)); 1791 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) 1792 __skb_queue_tail(xmitq, skb); 1793 break; 1794 } 1795 } 1796 1797 /** 1798 * tipc_sk_rcv - handle a chain of incoming buffers 1799 * @inputq: buffer list containing the buffers 1800 * Consumes all buffers in list until inputq is empty 1801 * Note: may be called in multiple threads referring to the same queue 1802 */ 1803 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq) 1804 { 1805 struct sk_buff_head xmitq; 1806 u32 dnode, dport = 0; 1807 int err; 1808 struct tipc_sock *tsk; 1809 struct sock *sk; 1810 struct sk_buff *skb; 1811 1812 __skb_queue_head_init(&xmitq); 1813 while (skb_queue_len(inputq)) { 1814 dport = tipc_skb_peek_port(inputq, dport); 1815 tsk = tipc_sk_lookup(net, dport); 1816 1817 if (likely(tsk)) { 1818 sk = &tsk->sk; 1819 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) { 1820 tipc_sk_enqueue(inputq, sk, dport, &xmitq); 1821 spin_unlock_bh(&sk->sk_lock.slock); 1822 } 1823 /* Send pending response/rejected messages, if any */ 1824 while ((skb = __skb_dequeue(&xmitq))) { 1825 dnode = msg_destnode(buf_msg(skb)); 1826 tipc_node_xmit_skb(net, skb, dnode, dport); 1827 } 1828 sock_put(sk); 1829 continue; 1830 } 1831 1832 /* No destination socket => dequeue skb if still there */ 1833 skb = tipc_skb_dequeue(inputq, dport); 1834 if (!skb) 1835 return; 1836 1837 /* Try secondary lookup if unresolved named message */ 1838 err = TIPC_ERR_NO_PORT; 1839 if (tipc_msg_lookup_dest(net, skb, &err)) 1840 goto xmit; 1841 1842 /* Prepare for message rejection */ 1843 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err)) 1844 continue; 1845 xmit: 1846 dnode = msg_destnode(buf_msg(skb)); 1847 tipc_node_xmit_skb(net, skb, dnode, dport); 1848 } 1849 } 1850 1851 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) 1852 { 1853 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1854 struct sock *sk = sock->sk; 1855 int done; 1856 1857 do { 1858 int err = sock_error(sk); 1859 if (err) 1860 return err; 1861 if (!*timeo_p) 1862 return -ETIMEDOUT; 1863 if (signal_pending(current)) 1864 return sock_intr_errno(*timeo_p); 1865 1866 add_wait_queue(sk_sleep(sk), &wait); 1867 done = sk_wait_event(sk, timeo_p, 1868 sk->sk_state != TIPC_CONNECTING, &wait); 1869 remove_wait_queue(sk_sleep(sk), &wait); 1870 } while (!done); 1871 return 0; 1872 } 1873 1874 /** 1875 * tipc_connect - establish a connection to another TIPC port 1876 * @sock: socket structure 1877 * @dest: socket address for destination port 1878 * @destlen: size of socket address data structure 1879 * @flags: file-related flags associated with socket 1880 * 1881 * Returns 0 on success, errno otherwise 1882 */ 1883 static int tipc_connect(struct socket *sock, struct sockaddr *dest, 1884 int destlen, int flags) 1885 { 1886 struct sock *sk = sock->sk; 1887 struct tipc_sock *tsk = tipc_sk(sk); 1888 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; 1889 struct msghdr m = {NULL,}; 1890 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout; 1891 int previous; 1892 int res = 0; 1893 1894 lock_sock(sk); 1895 1896 /* DGRAM/RDM connect(), just save the destaddr */ 1897 if (tipc_sk_type_connectionless(sk)) { 1898 if (dst->family == AF_UNSPEC) { 1899 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc)); 1900 } else if (destlen != sizeof(struct sockaddr_tipc)) { 1901 res = -EINVAL; 1902 } else { 1903 memcpy(&tsk->peer, dest, destlen); 1904 } 1905 goto exit; 1906 } 1907 1908 /* 1909 * Reject connection attempt using multicast address 1910 * 1911 * Note: send_msg() validates the rest of the address fields, 1912 * so there's no need to do it here 1913 */ 1914 if (dst->addrtype == TIPC_ADDR_MCAST) { 1915 res = -EINVAL; 1916 goto exit; 1917 } 1918 1919 previous = sk->sk_state; 1920 1921 switch (sk->sk_state) { 1922 case TIPC_OPEN: 1923 /* Send a 'SYN-' to destination */ 1924 m.msg_name = dest; 1925 m.msg_namelen = destlen; 1926 1927 /* If connect is in non-blocking case, set MSG_DONTWAIT to 1928 * indicate send_msg() is never blocked. 1929 */ 1930 if (!timeout) 1931 m.msg_flags = MSG_DONTWAIT; 1932 1933 res = __tipc_sendmsg(sock, &m, 0); 1934 if ((res < 0) && (res != -EWOULDBLOCK)) 1935 goto exit; 1936 1937 /* Just entered TIPC_CONNECTING state; the only 1938 * difference is that return value in non-blocking 1939 * case is EINPROGRESS, rather than EALREADY. 1940 */ 1941 res = -EINPROGRESS; 1942 /* fall thru' */ 1943 case TIPC_CONNECTING: 1944 if (!timeout) { 1945 if (previous == TIPC_CONNECTING) 1946 res = -EALREADY; 1947 goto exit; 1948 } 1949 timeout = msecs_to_jiffies(timeout); 1950 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ 1951 res = tipc_wait_for_connect(sock, &timeout); 1952 break; 1953 case TIPC_ESTABLISHED: 1954 res = -EISCONN; 1955 break; 1956 default: 1957 res = -EINVAL; 1958 } 1959 1960 exit: 1961 release_sock(sk); 1962 return res; 1963 } 1964 1965 /** 1966 * tipc_listen - allow socket to listen for incoming connections 1967 * @sock: socket structure 1968 * @len: (unused) 1969 * 1970 * Returns 0 on success, errno otherwise 1971 */ 1972 static int tipc_listen(struct socket *sock, int len) 1973 { 1974 struct sock *sk = sock->sk; 1975 int res; 1976 1977 lock_sock(sk); 1978 res = tipc_set_sk_state(sk, TIPC_LISTEN); 1979 release_sock(sk); 1980 1981 return res; 1982 } 1983 1984 static int tipc_wait_for_accept(struct socket *sock, long timeo) 1985 { 1986 struct sock *sk = sock->sk; 1987 DEFINE_WAIT(wait); 1988 int err; 1989 1990 /* True wake-one mechanism for incoming connections: only 1991 * one process gets woken up, not the 'whole herd'. 1992 * Since we do not 'race & poll' for established sockets 1993 * anymore, the common case will execute the loop only once. 1994 */ 1995 for (;;) { 1996 prepare_to_wait_exclusive(sk_sleep(sk), &wait, 1997 TASK_INTERRUPTIBLE); 1998 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { 1999 release_sock(sk); 2000 timeo = schedule_timeout(timeo); 2001 lock_sock(sk); 2002 } 2003 err = 0; 2004 if (!skb_queue_empty(&sk->sk_receive_queue)) 2005 break; 2006 err = -EAGAIN; 2007 if (!timeo) 2008 break; 2009 err = sock_intr_errno(timeo); 2010 if (signal_pending(current)) 2011 break; 2012 } 2013 finish_wait(sk_sleep(sk), &wait); 2014 return err; 2015 } 2016 2017 /** 2018 * tipc_accept - wait for connection request 2019 * @sock: listening socket 2020 * @newsock: new socket that is to be connected 2021 * @flags: file-related flags associated with socket 2022 * 2023 * Returns 0 on success, errno otherwise 2024 */ 2025 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) 2026 { 2027 struct sock *new_sk, *sk = sock->sk; 2028 struct sk_buff *buf; 2029 struct tipc_sock *new_tsock; 2030 struct tipc_msg *msg; 2031 long timeo; 2032 int res; 2033 2034 lock_sock(sk); 2035 2036 if (sk->sk_state != TIPC_LISTEN) { 2037 res = -EINVAL; 2038 goto exit; 2039 } 2040 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 2041 res = tipc_wait_for_accept(sock, timeo); 2042 if (res) 2043 goto exit; 2044 2045 buf = skb_peek(&sk->sk_receive_queue); 2046 2047 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0); 2048 if (res) 2049 goto exit; 2050 security_sk_clone(sock->sk, new_sock->sk); 2051 2052 new_sk = new_sock->sk; 2053 new_tsock = tipc_sk(new_sk); 2054 msg = buf_msg(buf); 2055 2056 /* we lock on new_sk; but lockdep sees the lock on sk */ 2057 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); 2058 2059 /* 2060 * Reject any stray messages received by new socket 2061 * before the socket lock was taken (very, very unlikely) 2062 */ 2063 tsk_rej_rx_queue(new_sk); 2064 2065 /* Connect new socket to it's peer */ 2066 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); 2067 2068 tsk_set_importance(new_tsock, msg_importance(msg)); 2069 if (msg_named(msg)) { 2070 new_tsock->conn_type = msg_nametype(msg); 2071 new_tsock->conn_instance = msg_nameinst(msg); 2072 } 2073 2074 /* 2075 * Respond to 'SYN-' by discarding it & returning 'ACK'-. 2076 * Respond to 'SYN+' by queuing it on new socket. 2077 */ 2078 if (!msg_data_sz(msg)) { 2079 struct msghdr m = {NULL,}; 2080 2081 tsk_advance_rx_queue(sk); 2082 __tipc_sendstream(new_sock, &m, 0); 2083 } else { 2084 __skb_dequeue(&sk->sk_receive_queue); 2085 __skb_queue_head(&new_sk->sk_receive_queue, buf); 2086 skb_set_owner_r(buf, new_sk); 2087 } 2088 release_sock(new_sk); 2089 exit: 2090 release_sock(sk); 2091 return res; 2092 } 2093 2094 /** 2095 * tipc_shutdown - shutdown socket connection 2096 * @sock: socket structure 2097 * @how: direction to close (must be SHUT_RDWR) 2098 * 2099 * Terminates connection (if necessary), then purges socket's receive queue. 2100 * 2101 * Returns 0 on success, errno otherwise 2102 */ 2103 static int tipc_shutdown(struct socket *sock, int how) 2104 { 2105 struct sock *sk = sock->sk; 2106 int res; 2107 2108 if (how != SHUT_RDWR) 2109 return -EINVAL; 2110 2111 lock_sock(sk); 2112 2113 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN); 2114 sk->sk_shutdown = SEND_SHUTDOWN; 2115 2116 if (sk->sk_state == TIPC_DISCONNECTING) { 2117 /* Discard any unreceived messages */ 2118 __skb_queue_purge(&sk->sk_receive_queue); 2119 2120 /* Wake up anyone sleeping in poll */ 2121 sk->sk_state_change(sk); 2122 res = 0; 2123 } else { 2124 res = -ENOTCONN; 2125 } 2126 2127 release_sock(sk); 2128 return res; 2129 } 2130 2131 static void tipc_sk_timeout(unsigned long data) 2132 { 2133 struct tipc_sock *tsk = (struct tipc_sock *)data; 2134 struct sock *sk = &tsk->sk; 2135 struct sk_buff *skb = NULL; 2136 u32 peer_port, peer_node; 2137 u32 own_node = tsk_own_node(tsk); 2138 2139 bh_lock_sock(sk); 2140 if (!tipc_sk_connected(sk)) { 2141 bh_unlock_sock(sk); 2142 goto exit; 2143 } 2144 peer_port = tsk_peer_port(tsk); 2145 peer_node = tsk_peer_node(tsk); 2146 2147 if (tsk->probe_unacked) { 2148 if (!sock_owned_by_user(sk)) { 2149 tipc_set_sk_state(sk, TIPC_DISCONNECTING); 2150 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk), 2151 tsk_peer_port(tsk)); 2152 sk->sk_state_change(sk); 2153 } else { 2154 /* Try again later */ 2155 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20)); 2156 } 2157 2158 bh_unlock_sock(sk); 2159 goto exit; 2160 } 2161 2162 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, 2163 INT_H_SIZE, 0, peer_node, own_node, 2164 peer_port, tsk->portid, TIPC_OK); 2165 tsk->probe_unacked = true; 2166 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL); 2167 bh_unlock_sock(sk); 2168 if (skb) 2169 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid); 2170 exit: 2171 sock_put(sk); 2172 } 2173 2174 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, 2175 struct tipc_name_seq const *seq) 2176 { 2177 struct sock *sk = &tsk->sk; 2178 struct net *net = sock_net(sk); 2179 struct publication *publ; 2180 u32 key; 2181 2182 if (tipc_sk_connected(sk)) 2183 return -EINVAL; 2184 key = tsk->portid + tsk->pub_count + 1; 2185 if (key == tsk->portid) 2186 return -EADDRINUSE; 2187 2188 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper, 2189 scope, tsk->portid, key); 2190 if (unlikely(!publ)) 2191 return -EINVAL; 2192 2193 list_add(&publ->pport_list, &tsk->publications); 2194 tsk->pub_count++; 2195 tsk->published = 1; 2196 return 0; 2197 } 2198 2199 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, 2200 struct tipc_name_seq const *seq) 2201 { 2202 struct net *net = sock_net(&tsk->sk); 2203 struct publication *publ; 2204 struct publication *safe; 2205 int rc = -EINVAL; 2206 2207 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) { 2208 if (seq) { 2209 if (publ->scope != scope) 2210 continue; 2211 if (publ->type != seq->type) 2212 continue; 2213 if (publ->lower != seq->lower) 2214 continue; 2215 if (publ->upper != seq->upper) 2216 break; 2217 tipc_nametbl_withdraw(net, publ->type, publ->lower, 2218 publ->ref, publ->key); 2219 rc = 0; 2220 break; 2221 } 2222 tipc_nametbl_withdraw(net, publ->type, publ->lower, 2223 publ->ref, publ->key); 2224 rc = 0; 2225 } 2226 if (list_empty(&tsk->publications)) 2227 tsk->published = 0; 2228 return rc; 2229 } 2230 2231 /* tipc_sk_reinit: set non-zero address in all existing sockets 2232 * when we go from standalone to network mode. 2233 */ 2234 void tipc_sk_reinit(struct net *net) 2235 { 2236 struct tipc_net *tn = net_generic(net, tipc_net_id); 2237 const struct bucket_table *tbl; 2238 struct rhash_head *pos; 2239 struct tipc_sock *tsk; 2240 struct tipc_msg *msg; 2241 int i; 2242 2243 rcu_read_lock(); 2244 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); 2245 for (i = 0; i < tbl->size; i++) { 2246 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { 2247 spin_lock_bh(&tsk->sk.sk_lock.slock); 2248 msg = &tsk->phdr; 2249 msg_set_prevnode(msg, tn->own_addr); 2250 msg_set_orignode(msg, tn->own_addr); 2251 spin_unlock_bh(&tsk->sk.sk_lock.slock); 2252 } 2253 } 2254 rcu_read_unlock(); 2255 } 2256 2257 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid) 2258 { 2259 struct tipc_net *tn = net_generic(net, tipc_net_id); 2260 struct tipc_sock *tsk; 2261 2262 rcu_read_lock(); 2263 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params); 2264 if (tsk) 2265 sock_hold(&tsk->sk); 2266 rcu_read_unlock(); 2267 2268 return tsk; 2269 } 2270 2271 static int tipc_sk_insert(struct tipc_sock *tsk) 2272 { 2273 struct sock *sk = &tsk->sk; 2274 struct net *net = sock_net(sk); 2275 struct tipc_net *tn = net_generic(net, tipc_net_id); 2276 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1; 2277 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT; 2278 2279 while (remaining--) { 2280 portid++; 2281 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT)) 2282 portid = TIPC_MIN_PORT; 2283 tsk->portid = portid; 2284 sock_hold(&tsk->sk); 2285 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node, 2286 tsk_rht_params)) 2287 return 0; 2288 sock_put(&tsk->sk); 2289 } 2290 2291 return -1; 2292 } 2293 2294 static void tipc_sk_remove(struct tipc_sock *tsk) 2295 { 2296 struct sock *sk = &tsk->sk; 2297 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id); 2298 2299 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) { 2300 WARN_ON(atomic_read(&sk->sk_refcnt) == 1); 2301 __sock_put(sk); 2302 } 2303 } 2304 2305 static const struct rhashtable_params tsk_rht_params = { 2306 .nelem_hint = 192, 2307 .head_offset = offsetof(struct tipc_sock, node), 2308 .key_offset = offsetof(struct tipc_sock, portid), 2309 .key_len = sizeof(u32), /* portid */ 2310 .max_size = 1048576, 2311 .min_size = 256, 2312 .automatic_shrinking = true, 2313 }; 2314 2315 int tipc_sk_rht_init(struct net *net) 2316 { 2317 struct tipc_net *tn = net_generic(net, tipc_net_id); 2318 2319 return rhashtable_init(&tn->sk_rht, &tsk_rht_params); 2320 } 2321 2322 void tipc_sk_rht_destroy(struct net *net) 2323 { 2324 struct tipc_net *tn = net_generic(net, tipc_net_id); 2325 2326 /* Wait for socket readers to complete */ 2327 synchronize_net(); 2328 2329 rhashtable_destroy(&tn->sk_rht); 2330 } 2331 2332 /** 2333 * tipc_setsockopt - set socket option 2334 * @sock: socket structure 2335 * @lvl: option level 2336 * @opt: option identifier 2337 * @ov: pointer to new option value 2338 * @ol: length of option value 2339 * 2340 * For stream sockets only, accepts and ignores all IPPROTO_TCP options 2341 * (to ease compatibility). 2342 * 2343 * Returns 0 on success, errno otherwise 2344 */ 2345 static int tipc_setsockopt(struct socket *sock, int lvl, int opt, 2346 char __user *ov, unsigned int ol) 2347 { 2348 struct sock *sk = sock->sk; 2349 struct tipc_sock *tsk = tipc_sk(sk); 2350 u32 value = 0; 2351 int res = 0; 2352 2353 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 2354 return 0; 2355 if (lvl != SOL_TIPC) 2356 return -ENOPROTOOPT; 2357 2358 switch (opt) { 2359 case TIPC_IMPORTANCE: 2360 case TIPC_SRC_DROPPABLE: 2361 case TIPC_DEST_DROPPABLE: 2362 case TIPC_CONN_TIMEOUT: 2363 if (ol < sizeof(value)) 2364 return -EINVAL; 2365 res = get_user(value, (u32 __user *)ov); 2366 if (res) 2367 return res; 2368 break; 2369 default: 2370 if (ov || ol) 2371 return -EINVAL; 2372 } 2373 2374 lock_sock(sk); 2375 2376 switch (opt) { 2377 case TIPC_IMPORTANCE: 2378 res = tsk_set_importance(tsk, value); 2379 break; 2380 case TIPC_SRC_DROPPABLE: 2381 if (sock->type != SOCK_STREAM) 2382 tsk_set_unreliable(tsk, value); 2383 else 2384 res = -ENOPROTOOPT; 2385 break; 2386 case TIPC_DEST_DROPPABLE: 2387 tsk_set_unreturnable(tsk, value); 2388 break; 2389 case TIPC_CONN_TIMEOUT: 2390 tipc_sk(sk)->conn_timeout = value; 2391 break; 2392 case TIPC_MCAST_BROADCAST: 2393 tsk->mc_method.rcast = false; 2394 tsk->mc_method.mandatory = true; 2395 break; 2396 case TIPC_MCAST_REPLICAST: 2397 tsk->mc_method.rcast = true; 2398 tsk->mc_method.mandatory = true; 2399 break; 2400 default: 2401 res = -EINVAL; 2402 } 2403 2404 release_sock(sk); 2405 2406 return res; 2407 } 2408 2409 /** 2410 * tipc_getsockopt - get socket option 2411 * @sock: socket structure 2412 * @lvl: option level 2413 * @opt: option identifier 2414 * @ov: receptacle for option value 2415 * @ol: receptacle for length of option value 2416 * 2417 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options 2418 * (to ease compatibility). 2419 * 2420 * Returns 0 on success, errno otherwise 2421 */ 2422 static int tipc_getsockopt(struct socket *sock, int lvl, int opt, 2423 char __user *ov, int __user *ol) 2424 { 2425 struct sock *sk = sock->sk; 2426 struct tipc_sock *tsk = tipc_sk(sk); 2427 int len; 2428 u32 value; 2429 int res; 2430 2431 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 2432 return put_user(0, ol); 2433 if (lvl != SOL_TIPC) 2434 return -ENOPROTOOPT; 2435 res = get_user(len, ol); 2436 if (res) 2437 return res; 2438 2439 lock_sock(sk); 2440 2441 switch (opt) { 2442 case TIPC_IMPORTANCE: 2443 value = tsk_importance(tsk); 2444 break; 2445 case TIPC_SRC_DROPPABLE: 2446 value = tsk_unreliable(tsk); 2447 break; 2448 case TIPC_DEST_DROPPABLE: 2449 value = tsk_unreturnable(tsk); 2450 break; 2451 case TIPC_CONN_TIMEOUT: 2452 value = tsk->conn_timeout; 2453 /* no need to set "res", since already 0 at this point */ 2454 break; 2455 case TIPC_NODE_RECVQ_DEPTH: 2456 value = 0; /* was tipc_queue_size, now obsolete */ 2457 break; 2458 case TIPC_SOCK_RECVQ_DEPTH: 2459 value = skb_queue_len(&sk->sk_receive_queue); 2460 break; 2461 default: 2462 res = -EINVAL; 2463 } 2464 2465 release_sock(sk); 2466 2467 if (res) 2468 return res; /* "get" failed */ 2469 2470 if (len < sizeof(value)) 2471 return -EINVAL; 2472 2473 if (copy_to_user(ov, &value, sizeof(value))) 2474 return -EFAULT; 2475 2476 return put_user(sizeof(value), ol); 2477 } 2478 2479 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 2480 { 2481 struct sock *sk = sock->sk; 2482 struct tipc_sioc_ln_req lnr; 2483 void __user *argp = (void __user *)arg; 2484 2485 switch (cmd) { 2486 case SIOCGETLINKNAME: 2487 if (copy_from_user(&lnr, argp, sizeof(lnr))) 2488 return -EFAULT; 2489 if (!tipc_node_get_linkname(sock_net(sk), 2490 lnr.bearer_id & 0xffff, lnr.peer, 2491 lnr.linkname, TIPC_MAX_LINK_NAME)) { 2492 if (copy_to_user(argp, &lnr, sizeof(lnr))) 2493 return -EFAULT; 2494 return 0; 2495 } 2496 return -EADDRNOTAVAIL; 2497 default: 2498 return -ENOIOCTLCMD; 2499 } 2500 } 2501 2502 /* Protocol switches for the various types of TIPC sockets */ 2503 2504 static const struct proto_ops msg_ops = { 2505 .owner = THIS_MODULE, 2506 .family = AF_TIPC, 2507 .release = tipc_release, 2508 .bind = tipc_bind, 2509 .connect = tipc_connect, 2510 .socketpair = sock_no_socketpair, 2511 .accept = sock_no_accept, 2512 .getname = tipc_getname, 2513 .poll = tipc_poll, 2514 .ioctl = tipc_ioctl, 2515 .listen = sock_no_listen, 2516 .shutdown = tipc_shutdown, 2517 .setsockopt = tipc_setsockopt, 2518 .getsockopt = tipc_getsockopt, 2519 .sendmsg = tipc_sendmsg, 2520 .recvmsg = tipc_recvmsg, 2521 .mmap = sock_no_mmap, 2522 .sendpage = sock_no_sendpage 2523 }; 2524 2525 static const struct proto_ops packet_ops = { 2526 .owner = THIS_MODULE, 2527 .family = AF_TIPC, 2528 .release = tipc_release, 2529 .bind = tipc_bind, 2530 .connect = tipc_connect, 2531 .socketpair = sock_no_socketpair, 2532 .accept = tipc_accept, 2533 .getname = tipc_getname, 2534 .poll = tipc_poll, 2535 .ioctl = tipc_ioctl, 2536 .listen = tipc_listen, 2537 .shutdown = tipc_shutdown, 2538 .setsockopt = tipc_setsockopt, 2539 .getsockopt = tipc_getsockopt, 2540 .sendmsg = tipc_send_packet, 2541 .recvmsg = tipc_recvmsg, 2542 .mmap = sock_no_mmap, 2543 .sendpage = sock_no_sendpage 2544 }; 2545 2546 static const struct proto_ops stream_ops = { 2547 .owner = THIS_MODULE, 2548 .family = AF_TIPC, 2549 .release = tipc_release, 2550 .bind = tipc_bind, 2551 .connect = tipc_connect, 2552 .socketpair = sock_no_socketpair, 2553 .accept = tipc_accept, 2554 .getname = tipc_getname, 2555 .poll = tipc_poll, 2556 .ioctl = tipc_ioctl, 2557 .listen = tipc_listen, 2558 .shutdown = tipc_shutdown, 2559 .setsockopt = tipc_setsockopt, 2560 .getsockopt = tipc_getsockopt, 2561 .sendmsg = tipc_sendstream, 2562 .recvmsg = tipc_recv_stream, 2563 .mmap = sock_no_mmap, 2564 .sendpage = sock_no_sendpage 2565 }; 2566 2567 static const struct net_proto_family tipc_family_ops = { 2568 .owner = THIS_MODULE, 2569 .family = AF_TIPC, 2570 .create = tipc_sk_create 2571 }; 2572 2573 static struct proto tipc_proto = { 2574 .name = "TIPC", 2575 .owner = THIS_MODULE, 2576 .obj_size = sizeof(struct tipc_sock), 2577 .sysctl_rmem = sysctl_tipc_rmem 2578 }; 2579 2580 /** 2581 * tipc_socket_init - initialize TIPC socket interface 2582 * 2583 * Returns 0 on success, errno otherwise 2584 */ 2585 int tipc_socket_init(void) 2586 { 2587 int res; 2588 2589 res = proto_register(&tipc_proto, 1); 2590 if (res) { 2591 pr_err("Failed to register TIPC protocol type\n"); 2592 goto out; 2593 } 2594 2595 res = sock_register(&tipc_family_ops); 2596 if (res) { 2597 pr_err("Failed to register TIPC socket type\n"); 2598 proto_unregister(&tipc_proto); 2599 goto out; 2600 } 2601 out: 2602 return res; 2603 } 2604 2605 /** 2606 * tipc_socket_stop - stop TIPC socket interface 2607 */ 2608 void tipc_socket_stop(void) 2609 { 2610 sock_unregister(tipc_family_ops.family); 2611 proto_unregister(&tipc_proto); 2612 } 2613 2614 /* Caller should hold socket lock for the passed tipc socket. */ 2615 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk) 2616 { 2617 u32 peer_node; 2618 u32 peer_port; 2619 struct nlattr *nest; 2620 2621 peer_node = tsk_peer_node(tsk); 2622 peer_port = tsk_peer_port(tsk); 2623 2624 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON); 2625 2626 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node)) 2627 goto msg_full; 2628 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port)) 2629 goto msg_full; 2630 2631 if (tsk->conn_type != 0) { 2632 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG)) 2633 goto msg_full; 2634 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type)) 2635 goto msg_full; 2636 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance)) 2637 goto msg_full; 2638 } 2639 nla_nest_end(skb, nest); 2640 2641 return 0; 2642 2643 msg_full: 2644 nla_nest_cancel(skb, nest); 2645 2646 return -EMSGSIZE; 2647 } 2648 2649 /* Caller should hold socket lock for the passed tipc socket. */ 2650 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb, 2651 struct tipc_sock *tsk) 2652 { 2653 int err; 2654 void *hdr; 2655 struct nlattr *attrs; 2656 struct net *net = sock_net(skb->sk); 2657 struct tipc_net *tn = net_generic(net, tipc_net_id); 2658 struct sock *sk = &tsk->sk; 2659 2660 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 2661 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); 2662 if (!hdr) 2663 goto msg_cancel; 2664 2665 attrs = nla_nest_start(skb, TIPC_NLA_SOCK); 2666 if (!attrs) 2667 goto genlmsg_cancel; 2668 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid)) 2669 goto attr_msg_cancel; 2670 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr)) 2671 goto attr_msg_cancel; 2672 2673 if (tipc_sk_connected(sk)) { 2674 err = __tipc_nl_add_sk_con(skb, tsk); 2675 if (err) 2676 goto attr_msg_cancel; 2677 } else if (!list_empty(&tsk->publications)) { 2678 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL)) 2679 goto attr_msg_cancel; 2680 } 2681 nla_nest_end(skb, attrs); 2682 genlmsg_end(skb, hdr); 2683 2684 return 0; 2685 2686 attr_msg_cancel: 2687 nla_nest_cancel(skb, attrs); 2688 genlmsg_cancel: 2689 genlmsg_cancel(skb, hdr); 2690 msg_cancel: 2691 return -EMSGSIZE; 2692 } 2693 2694 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb) 2695 { 2696 int err; 2697 struct tipc_sock *tsk; 2698 const struct bucket_table *tbl; 2699 struct rhash_head *pos; 2700 struct net *net = sock_net(skb->sk); 2701 struct tipc_net *tn = net_generic(net, tipc_net_id); 2702 u32 tbl_id = cb->args[0]; 2703 u32 prev_portid = cb->args[1]; 2704 2705 rcu_read_lock(); 2706 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); 2707 for (; tbl_id < tbl->size; tbl_id++) { 2708 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) { 2709 spin_lock_bh(&tsk->sk.sk_lock.slock); 2710 if (prev_portid && prev_portid != tsk->portid) { 2711 spin_unlock_bh(&tsk->sk.sk_lock.slock); 2712 continue; 2713 } 2714 2715 err = __tipc_nl_add_sk(skb, cb, tsk); 2716 if (err) { 2717 prev_portid = tsk->portid; 2718 spin_unlock_bh(&tsk->sk.sk_lock.slock); 2719 goto out; 2720 } 2721 prev_portid = 0; 2722 spin_unlock_bh(&tsk->sk.sk_lock.slock); 2723 } 2724 } 2725 out: 2726 rcu_read_unlock(); 2727 cb->args[0] = tbl_id; 2728 cb->args[1] = prev_portid; 2729 2730 return skb->len; 2731 } 2732 2733 /* Caller should hold socket lock for the passed tipc socket. */ 2734 static int __tipc_nl_add_sk_publ(struct sk_buff *skb, 2735 struct netlink_callback *cb, 2736 struct publication *publ) 2737 { 2738 void *hdr; 2739 struct nlattr *attrs; 2740 2741 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 2742 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET); 2743 if (!hdr) 2744 goto msg_cancel; 2745 2746 attrs = nla_nest_start(skb, TIPC_NLA_PUBL); 2747 if (!attrs) 2748 goto genlmsg_cancel; 2749 2750 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) 2751 goto attr_msg_cancel; 2752 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type)) 2753 goto attr_msg_cancel; 2754 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower)) 2755 goto attr_msg_cancel; 2756 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper)) 2757 goto attr_msg_cancel; 2758 2759 nla_nest_end(skb, attrs); 2760 genlmsg_end(skb, hdr); 2761 2762 return 0; 2763 2764 attr_msg_cancel: 2765 nla_nest_cancel(skb, attrs); 2766 genlmsg_cancel: 2767 genlmsg_cancel(skb, hdr); 2768 msg_cancel: 2769 return -EMSGSIZE; 2770 } 2771 2772 /* Caller should hold socket lock for the passed tipc socket. */ 2773 static int __tipc_nl_list_sk_publ(struct sk_buff *skb, 2774 struct netlink_callback *cb, 2775 struct tipc_sock *tsk, u32 *last_publ) 2776 { 2777 int err; 2778 struct publication *p; 2779 2780 if (*last_publ) { 2781 list_for_each_entry(p, &tsk->publications, pport_list) { 2782 if (p->key == *last_publ) 2783 break; 2784 } 2785 if (p->key != *last_publ) { 2786 /* We never set seq or call nl_dump_check_consistent() 2787 * this means that setting prev_seq here will cause the 2788 * consistence check to fail in the netlink callback 2789 * handler. Resulting in the last NLMSG_DONE message 2790 * having the NLM_F_DUMP_INTR flag set. 2791 */ 2792 cb->prev_seq = 1; 2793 *last_publ = 0; 2794 return -EPIPE; 2795 } 2796 } else { 2797 p = list_first_entry(&tsk->publications, struct publication, 2798 pport_list); 2799 } 2800 2801 list_for_each_entry_from(p, &tsk->publications, pport_list) { 2802 err = __tipc_nl_add_sk_publ(skb, cb, p); 2803 if (err) { 2804 *last_publ = p->key; 2805 return err; 2806 } 2807 } 2808 *last_publ = 0; 2809 2810 return 0; 2811 } 2812 2813 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb) 2814 { 2815 int err; 2816 u32 tsk_portid = cb->args[0]; 2817 u32 last_publ = cb->args[1]; 2818 u32 done = cb->args[2]; 2819 struct net *net = sock_net(skb->sk); 2820 struct tipc_sock *tsk; 2821 2822 if (!tsk_portid) { 2823 struct nlattr **attrs; 2824 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; 2825 2826 err = tipc_nlmsg_parse(cb->nlh, &attrs); 2827 if (err) 2828 return err; 2829 2830 if (!attrs[TIPC_NLA_SOCK]) 2831 return -EINVAL; 2832 2833 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, 2834 attrs[TIPC_NLA_SOCK], 2835 tipc_nl_sock_policy); 2836 if (err) 2837 return err; 2838 2839 if (!sock[TIPC_NLA_SOCK_REF]) 2840 return -EINVAL; 2841 2842 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); 2843 } 2844 2845 if (done) 2846 return 0; 2847 2848 tsk = tipc_sk_lookup(net, tsk_portid); 2849 if (!tsk) 2850 return -EINVAL; 2851 2852 lock_sock(&tsk->sk); 2853 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ); 2854 if (!err) 2855 done = 1; 2856 release_sock(&tsk->sk); 2857 sock_put(&tsk->sk); 2858 2859 cb->args[0] = tsk_portid; 2860 cb->args[1] = last_publ; 2861 cb->args[2] = done; 2862 2863 return skb->len; 2864 } 2865