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