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