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