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