Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/node.c: TIPC node management routines |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 4 | * Copyright (c) 2000-2006, 2012-2016, Ericsson AB |
Ying Xue | 46651c5 | 2014-03-27 12:54:36 +0800 | [diff] [blame] | 5 | * Copyright (c) 2005-2006, 2010-2014, 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 | |
| 37 | #include "core.h" |
Richard Alpe | 22ae7cf | 2015-02-09 09:50:18 +0100 | [diff] [blame] | 38 | #include "link.h" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 39 | #include "node.h" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 40 | #include "name_distr.h" |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 41 | #include "socket.h" |
Jon Paul Maloy | a6bf70f | 2015-05-14 10:46:13 -0400 | [diff] [blame] | 42 | #include "bcast.h" |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 43 | #include "monitor.h" |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 44 | #include "discover.h" |
Richard Alpe | 49cc66e | 2016-03-04 17:04:42 +0100 | [diff] [blame] | 45 | #include "netlink.h" |
Jon Paul Maloy | c819930 | 2015-10-15 14:52:46 -0400 | [diff] [blame] | 46 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 47 | #define INVALID_NODE_SIG 0x10000 |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 48 | #define NODE_CLEANUP_AFTER 300000 |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 49 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 50 | /* Flags used to take different actions according to flag type |
| 51 | * TIPC_NOTIFY_NODE_DOWN: notify node is down |
| 52 | * TIPC_NOTIFY_NODE_UP: notify node is up |
| 53 | * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type |
| 54 | */ |
| 55 | enum { |
| 56 | TIPC_NOTIFY_NODE_DOWN = (1 << 3), |
| 57 | TIPC_NOTIFY_NODE_UP = (1 << 4), |
| 58 | TIPC_NOTIFY_LINK_UP = (1 << 6), |
| 59 | TIPC_NOTIFY_LINK_DOWN = (1 << 7) |
| 60 | }; |
| 61 | |
| 62 | struct tipc_link_entry { |
| 63 | struct tipc_link *link; |
| 64 | spinlock_t lock; /* per link */ |
| 65 | u32 mtu; |
| 66 | struct sk_buff_head inputq; |
| 67 | struct tipc_media_addr maddr; |
| 68 | }; |
| 69 | |
| 70 | struct tipc_bclink_entry { |
| 71 | struct tipc_link *link; |
| 72 | struct sk_buff_head inputq1; |
| 73 | struct sk_buff_head arrvq; |
| 74 | struct sk_buff_head inputq2; |
| 75 | struct sk_buff_head namedq; |
| 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * struct tipc_node - TIPC node structure |
| 80 | * @addr: network address of node |
| 81 | * @ref: reference counter to node object |
| 82 | * @lock: rwlock governing access to structure |
| 83 | * @net: the applicable net namespace |
| 84 | * @hash: links to adjacent nodes in unsorted hash chain |
| 85 | * @inputq: pointer to input queue containing messages for msg event |
| 86 | * @namedq: pointer to name table input queue with name table messages |
| 87 | * @active_links: bearer ids of active links, used as index into links[] array |
| 88 | * @links: array containing references to all links to node |
| 89 | * @action_flags: bit mask of different types of node actions |
| 90 | * @state: connectivity state vs peer node |
| 91 | * @sync_point: sequence number where synch/failover is finished |
| 92 | * @list: links to adjacent nodes in sorted list of cluster's nodes |
| 93 | * @working_links: number of working links to node (both active and standby) |
| 94 | * @link_cnt: number of links to node |
| 95 | * @capabilities: bitmap, indicating peer node's functional capabilities |
| 96 | * @signature: node instance identifier |
| 97 | * @link_id: local and remote bearer ids of changing link, if any |
| 98 | * @publ_list: list of publications |
| 99 | * @rcu: rcu struct for tipc_node |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 100 | * @delete_at: indicates the time for deleting a down node |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 101 | */ |
| 102 | struct tipc_node { |
| 103 | u32 addr; |
| 104 | struct kref kref; |
| 105 | rwlock_t lock; |
| 106 | struct net *net; |
| 107 | struct hlist_node hash; |
| 108 | int active_links[2]; |
| 109 | struct tipc_link_entry links[MAX_BEARERS]; |
| 110 | struct tipc_bclink_entry bc_entry; |
| 111 | int action_flags; |
| 112 | struct list_head list; |
| 113 | int state; |
| 114 | u16 sync_point; |
| 115 | int link_cnt; |
| 116 | u16 working_links; |
| 117 | u16 capabilities; |
| 118 | u32 signature; |
| 119 | u32 link_id; |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 120 | u8 peer_id[16]; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 121 | struct list_head publ_list; |
| 122 | struct list_head conn_sks; |
| 123 | unsigned long keepalive_intv; |
| 124 | struct timer_list timer; |
| 125 | struct rcu_head rcu; |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 126 | unsigned long delete_at; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 129 | /* Node FSM states and events: |
| 130 | */ |
| 131 | enum { |
| 132 | SELF_DOWN_PEER_DOWN = 0xdd, |
| 133 | SELF_UP_PEER_UP = 0xaa, |
| 134 | SELF_DOWN_PEER_LEAVING = 0xd1, |
| 135 | SELF_UP_PEER_COMING = 0xac, |
| 136 | SELF_COMING_PEER_UP = 0xca, |
| 137 | SELF_LEAVING_PEER_DOWN = 0x1d, |
| 138 | NODE_FAILINGOVER = 0xf0, |
| 139 | NODE_SYNCHING = 0xcc |
| 140 | }; |
| 141 | |
| 142 | enum { |
| 143 | SELF_ESTABL_CONTACT_EVT = 0xece, |
| 144 | SELF_LOST_CONTACT_EVT = 0x1ce, |
| 145 | PEER_ESTABL_CONTACT_EVT = 0x9ece, |
| 146 | PEER_LOST_CONTACT_EVT = 0x91ce, |
| 147 | NODE_FAILOVER_BEGIN_EVT = 0xfbe, |
| 148 | NODE_FAILOVER_END_EVT = 0xfee, |
| 149 | NODE_SYNCH_BEGIN_EVT = 0xcbe, |
| 150 | NODE_SYNCH_END_EVT = 0xcee |
| 151 | }; |
| 152 | |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 153 | static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id, |
| 154 | struct sk_buff_head *xmitq, |
| 155 | struct tipc_media_addr **maddr); |
| 156 | static void tipc_node_link_down(struct tipc_node *n, int bearer_id, |
| 157 | bool delete); |
| 158 | static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 159 | static void tipc_node_delete(struct tipc_node *node); |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 160 | static void tipc_node_timeout(struct timer_list *t); |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 161 | static void tipc_node_fsm_evt(struct tipc_node *n, int evt); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 162 | static struct tipc_node *tipc_node_find(struct net *net, u32 addr); |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 163 | static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 164 | static void tipc_node_put(struct tipc_node *node); |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 165 | static bool node_is_up(struct tipc_node *n); |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 166 | static void tipc_node_delete_from_list(struct tipc_node *node); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 167 | |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 168 | struct tipc_sock_conn { |
| 169 | u32 port; |
| 170 | u32 peer_port; |
| 171 | u32 peer_node; |
| 172 | struct list_head list; |
| 173 | }; |
| 174 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 175 | static struct tipc_link *node_active_link(struct tipc_node *n, int sel) |
| 176 | { |
| 177 | int bearer_id = n->active_links[sel & 1]; |
| 178 | |
| 179 | if (unlikely(bearer_id == INVALID_BEARER_ID)) |
| 180 | return NULL; |
| 181 | |
| 182 | return n->links[bearer_id].link; |
| 183 | } |
| 184 | |
| 185 | int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel) |
| 186 | { |
| 187 | struct tipc_node *n; |
| 188 | int bearer_id; |
| 189 | unsigned int mtu = MAX_MSG_SIZE; |
| 190 | |
| 191 | n = tipc_node_find(net, addr); |
| 192 | if (unlikely(!n)) |
| 193 | return mtu; |
| 194 | |
| 195 | bearer_id = n->active_links[sel & 1]; |
| 196 | if (likely(bearer_id != INVALID_BEARER_ID)) |
| 197 | mtu = n->links[bearer_id].mtu; |
| 198 | tipc_node_put(n); |
| 199 | return mtu; |
| 200 | } |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 201 | |
Jon Maloy | 3e5cf36 | 2018-04-25 19:29:36 +0200 | [diff] [blame] | 202 | bool tipc_node_get_id(struct net *net, u32 addr, u8 *id) |
| 203 | { |
| 204 | u8 *own_id = tipc_own_id(net); |
| 205 | struct tipc_node *n; |
| 206 | |
| 207 | if (!own_id) |
| 208 | return true; |
| 209 | |
| 210 | if (addr == tipc_own_addr(net)) { |
| 211 | memcpy(id, own_id, TIPC_NODEID_LEN); |
| 212 | return true; |
| 213 | } |
| 214 | n = tipc_node_find(net, addr); |
| 215 | if (!n) |
| 216 | return false; |
| 217 | |
| 218 | memcpy(id, &n->peer_id, TIPC_NODEID_LEN); |
| 219 | tipc_node_put(n); |
| 220 | return true; |
| 221 | } |
| 222 | |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 223 | u16 tipc_node_get_capabilities(struct net *net, u32 addr) |
| 224 | { |
| 225 | struct tipc_node *n; |
| 226 | u16 caps; |
| 227 | |
| 228 | n = tipc_node_find(net, addr); |
| 229 | if (unlikely(!n)) |
| 230 | return TIPC_NODE_CAPABILITIES; |
| 231 | caps = n->capabilities; |
| 232 | tipc_node_put(n); |
| 233 | return caps; |
| 234 | } |
| 235 | |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 236 | static void tipc_node_kref_release(struct kref *kref) |
| 237 | { |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 238 | struct tipc_node *n = container_of(kref, struct tipc_node, kref); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 239 | |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 240 | kfree(n->bc_entry.link); |
| 241 | kfree_rcu(n, rcu); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 244 | static void tipc_node_put(struct tipc_node *node) |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 245 | { |
| 246 | kref_put(&node->kref, tipc_node_kref_release); |
| 247 | } |
| 248 | |
| 249 | static void tipc_node_get(struct tipc_node *node) |
| 250 | { |
| 251 | kref_get(&node->kref); |
| 252 | } |
| 253 | |
Allan Stephens | a635b46 | 2011-11-04 11:54:43 -0400 | [diff] [blame] | 254 | /* |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 255 | * tipc_node_find - locate specified node object, if it exists |
| 256 | */ |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 257 | static struct tipc_node *tipc_node_find(struct net *net, u32 addr) |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 258 | { |
Jon Paul Maloy | b170997 | 2016-02-24 11:00:19 -0500 | [diff] [blame] | 259 | struct tipc_net *tn = tipc_net(net); |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 260 | struct tipc_node *node; |
Jon Paul Maloy | b170997 | 2016-02-24 11:00:19 -0500 | [diff] [blame] | 261 | unsigned int thash = tipc_hashfn(addr); |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 262 | |
Ying Xue | 6c7a762 | 2014-03-27 12:54:37 +0800 | [diff] [blame] | 263 | rcu_read_lock(); |
Jon Paul Maloy | b170997 | 2016-02-24 11:00:19 -0500 | [diff] [blame] | 264 | hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) { |
| 265 | if (node->addr != addr) |
| 266 | continue; |
| 267 | if (!kref_get_unless_zero(&node->kref)) |
| 268 | node = NULL; |
| 269 | break; |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 270 | } |
Ying Xue | 6c7a762 | 2014-03-27 12:54:37 +0800 | [diff] [blame] | 271 | rcu_read_unlock(); |
Jon Paul Maloy | b170997 | 2016-02-24 11:00:19 -0500 | [diff] [blame] | 272 | return node; |
Allan Stephens | 672d99e | 2011-02-25 18:42:52 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 275 | /* tipc_node_find_by_id - locate specified node object by its 128-bit id |
| 276 | * Note: this function is called only when a discovery request failed |
| 277 | * to find the node by its 32-bit id, and is not time critical |
| 278 | */ |
| 279 | static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id) |
| 280 | { |
| 281 | struct tipc_net *tn = tipc_net(net); |
| 282 | struct tipc_node *n; |
| 283 | bool found = false; |
| 284 | |
| 285 | rcu_read_lock(); |
| 286 | list_for_each_entry_rcu(n, &tn->node_list, list) { |
| 287 | read_lock_bh(&n->lock); |
| 288 | if (!memcmp(id, n->peer_id, 16) && |
| 289 | kref_get_unless_zero(&n->kref)) |
| 290 | found = true; |
| 291 | read_unlock_bh(&n->lock); |
| 292 | if (found) |
| 293 | break; |
| 294 | } |
| 295 | rcu_read_unlock(); |
| 296 | return found ? n : NULL; |
| 297 | } |
| 298 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 299 | static void tipc_node_read_lock(struct tipc_node *n) |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 300 | { |
| 301 | read_lock_bh(&n->lock); |
| 302 | } |
| 303 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 304 | static void tipc_node_read_unlock(struct tipc_node *n) |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 305 | { |
| 306 | read_unlock_bh(&n->lock); |
| 307 | } |
| 308 | |
| 309 | static void tipc_node_write_lock(struct tipc_node *n) |
| 310 | { |
| 311 | write_lock_bh(&n->lock); |
| 312 | } |
| 313 | |
Parthasarathy Bhuvaragan | 93f955a | 2017-01-24 13:00:43 +0100 | [diff] [blame] | 314 | static void tipc_node_write_unlock_fast(struct tipc_node *n) |
| 315 | { |
| 316 | write_unlock_bh(&n->lock); |
| 317 | } |
| 318 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 319 | static void tipc_node_write_unlock(struct tipc_node *n) |
| 320 | { |
| 321 | struct net *net = n->net; |
| 322 | u32 addr = 0; |
| 323 | u32 flags = n->action_flags; |
| 324 | u32 link_id = 0; |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 325 | u32 bearer_id; |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 326 | struct list_head *publ_list; |
| 327 | |
| 328 | if (likely(!flags)) { |
| 329 | write_unlock_bh(&n->lock); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | addr = n->addr; |
| 334 | link_id = n->link_id; |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 335 | bearer_id = link_id & 0xffff; |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 336 | publ_list = &n->publ_list; |
| 337 | |
| 338 | n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP | |
| 339 | TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP); |
| 340 | |
| 341 | write_unlock_bh(&n->lock); |
| 342 | |
| 343 | if (flags & TIPC_NOTIFY_NODE_DOWN) |
| 344 | tipc_publ_notify(net, publ_list, addr); |
| 345 | |
| 346 | if (flags & TIPC_NOTIFY_NODE_UP) |
| 347 | tipc_named_node_up(net, addr); |
| 348 | |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 349 | if (flags & TIPC_NOTIFY_LINK_UP) { |
| 350 | tipc_mon_peer_up(net, addr, bearer_id); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 351 | tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr, |
Jon Maloy | 218527f | 2018-03-29 23:20:41 +0200 | [diff] [blame] | 352 | TIPC_NODE_SCOPE, link_id, link_id); |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 353 | } |
| 354 | if (flags & TIPC_NOTIFY_LINK_DOWN) { |
| 355 | tipc_mon_peer_down(net, addr, bearer_id); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 356 | tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr, |
Jon Maloy | 37922ea | 2018-03-29 23:20:43 +0200 | [diff] [blame] | 357 | addr, link_id); |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 358 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Wei Yongjun | e1a22d1 | 2018-03-26 14:33:13 +0000 | [diff] [blame] | 361 | static struct tipc_node *tipc_node_create(struct net *net, u32 addr, |
| 362 | u8 *peer_id, u16 capabilities) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 363 | { |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 364 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 365 | struct tipc_node *n, *temp_node; |
Jon Maloy | 9012de5 | 2018-07-10 01:07:35 +0200 | [diff] [blame] | 366 | struct tipc_link *l; |
| 367 | int bearer_id; |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 368 | int i; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 369 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 370 | spin_lock_bh(&tn->node_list_lock); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 371 | n = tipc_node_find(net, addr); |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 372 | if (n) { |
Jon Maloy | 40999f1 | 2018-07-18 19:50:06 +0200 | [diff] [blame] | 373 | if (n->capabilities == capabilities) |
| 374 | goto exit; |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 375 | /* Same node may come back with new capabilities */ |
Jon Maloy | 40999f1 | 2018-07-18 19:50:06 +0200 | [diff] [blame] | 376 | write_lock_bh(&n->lock); |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 377 | n->capabilities = capabilities; |
Jon Maloy | 9012de5 | 2018-07-10 01:07:35 +0200 | [diff] [blame] | 378 | for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) { |
| 379 | l = n->links[bearer_id].link; |
| 380 | if (l) |
| 381 | tipc_link_update_caps(l, capabilities); |
| 382 | } |
Jon Maloy | 40999f1 | 2018-07-18 19:50:06 +0200 | [diff] [blame] | 383 | write_unlock_bh(&n->lock); |
Jon Paul Maloy | b45db71 | 2015-02-03 08:59:19 -0500 | [diff] [blame] | 384 | goto exit; |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 385 | } |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 386 | n = kzalloc(sizeof(*n), GFP_ATOMIC); |
| 387 | if (!n) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 388 | pr_warn("Node creation failed, no memory\n"); |
Jon Paul Maloy | b45db71 | 2015-02-03 08:59:19 -0500 | [diff] [blame] | 389 | goto exit; |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 390 | } |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 391 | n->addr = addr; |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 392 | memcpy(&n->peer_id, peer_id, 16); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 393 | n->net = net; |
| 394 | n->capabilities = capabilities; |
| 395 | kref_init(&n->kref); |
| 396 | rwlock_init(&n->lock); |
| 397 | INIT_HLIST_NODE(&n->hash); |
| 398 | INIT_LIST_HEAD(&n->list); |
| 399 | INIT_LIST_HEAD(&n->publ_list); |
| 400 | INIT_LIST_HEAD(&n->conn_sks); |
| 401 | skb_queue_head_init(&n->bc_entry.namedq); |
| 402 | skb_queue_head_init(&n->bc_entry.inputq1); |
| 403 | __skb_queue_head_init(&n->bc_entry.arrvq); |
| 404 | skb_queue_head_init(&n->bc_entry.inputq2); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 405 | for (i = 0; i < MAX_BEARERS; i++) |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 406 | spin_lock_init(&n->links[i].lock); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 407 | n->state = SELF_DOWN_PEER_LEAVING; |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 408 | n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 409 | n->signature = INVALID_NODE_SIG; |
| 410 | n->active_links[0] = INVALID_BEARER_ID; |
| 411 | n->active_links[1] = INVALID_BEARER_ID; |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 412 | if (!tipc_link_bc_create(net, tipc_own_addr(net), |
| 413 | addr, U16_MAX, |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 414 | tipc_link_window(tipc_bc_sndlink(net)), |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 415 | n->capabilities, |
| 416 | &n->bc_entry.inputq1, |
| 417 | &n->bc_entry.namedq, |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 418 | tipc_bc_sndlink(net), |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 419 | &n->bc_entry.link)) { |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 420 | pr_warn("Broadcast rcv link creation failed, no memory\n"); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 421 | kfree(n); |
| 422 | n = NULL; |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 423 | goto exit; |
| 424 | } |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 425 | tipc_node_get(n); |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 426 | timer_setup(&n->timer, tipc_node_timeout, 0); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 427 | n->keepalive_intv = U32_MAX; |
Jon Paul Maloy | d5c91fb | 2016-02-10 16:14:57 -0500 | [diff] [blame] | 428 | hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]); |
| 429 | list_for_each_entry_rcu(temp_node, &tn->node_list, list) { |
| 430 | if (n->addr < temp_node->addr) |
| 431 | break; |
| 432 | } |
| 433 | list_add_tail_rcu(&n->list, &temp_node->list); |
Jon Paul Maloy | b45db71 | 2015-02-03 08:59:19 -0500 | [diff] [blame] | 434 | exit: |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 435 | spin_unlock_bh(&tn->node_list_lock); |
Jon Paul Maloy | 1a90632 | 2015-11-19 14:30:47 -0500 | [diff] [blame] | 436 | return n; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 439 | static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l) |
| 440 | { |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 441 | unsigned long tol = tipc_link_tolerance(l); |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 442 | unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 443 | |
| 444 | /* Link with lowest tolerance determines timer interval */ |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 445 | if (intv < n->keepalive_intv) |
| 446 | n->keepalive_intv = intv; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 447 | |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 448 | /* Ensure link's abort limit corresponds to current tolerance */ |
| 449 | tipc_link_set_abort_limit(l, tol / n->keepalive_intv); |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 450 | } |
| 451 | |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 452 | static void tipc_node_delete_from_list(struct tipc_node *node) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 453 | { |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 454 | list_del_rcu(&node->list); |
| 455 | hlist_del_rcu(&node->hash); |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 456 | tipc_node_put(node); |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void tipc_node_delete(struct tipc_node *node) |
| 460 | { |
| 461 | tipc_node_delete_from_list(node); |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 462 | |
| 463 | del_timer_sync(&node->timer); |
| 464 | tipc_node_put(node); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 467 | void tipc_node_stop(struct net *net) |
Ying Xue | 46651c5 | 2014-03-27 12:54:36 +0800 | [diff] [blame] | 468 | { |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 469 | struct tipc_net *tn = tipc_net(net); |
Ying Xue | 46651c5 | 2014-03-27 12:54:36 +0800 | [diff] [blame] | 470 | struct tipc_node *node, *t_node; |
| 471 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 472 | spin_lock_bh(&tn->node_list_lock); |
Jon Paul Maloy | d25a0125 | 2016-02-24 11:10:48 -0500 | [diff] [blame] | 473 | list_for_each_entry_safe(node, t_node, &tn->node_list, list) |
| 474 | tipc_node_delete(node); |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 475 | spin_unlock_bh(&tn->node_list_lock); |
Ying Xue | 46651c5 | 2014-03-27 12:54:36 +0800 | [diff] [blame] | 476 | } |
| 477 | |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 478 | void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr) |
| 479 | { |
| 480 | struct tipc_node *n; |
| 481 | |
| 482 | if (in_own_node(net, addr)) |
| 483 | return; |
| 484 | |
| 485 | n = tipc_node_find(net, addr); |
| 486 | if (!n) { |
| 487 | pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr); |
| 488 | return; |
| 489 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 490 | tipc_node_write_lock(n); |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 491 | list_add_tail(subscr, &n->publ_list); |
Parthasarathy Bhuvaragan | 93f955a | 2017-01-24 13:00:43 +0100 | [diff] [blame] | 492 | tipc_node_write_unlock_fast(n); |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 493 | tipc_node_put(n); |
| 494 | } |
| 495 | |
| 496 | void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr) |
| 497 | { |
| 498 | struct tipc_node *n; |
| 499 | |
| 500 | if (in_own_node(net, addr)) |
| 501 | return; |
| 502 | |
| 503 | n = tipc_node_find(net, addr); |
| 504 | if (!n) { |
| 505 | pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr); |
| 506 | return; |
| 507 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 508 | tipc_node_write_lock(n); |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 509 | list_del_init(subscr); |
Parthasarathy Bhuvaragan | 93f955a | 2017-01-24 13:00:43 +0100 | [diff] [blame] | 510 | tipc_node_write_unlock_fast(n); |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 511 | tipc_node_put(n); |
| 512 | } |
| 513 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 514 | int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port) |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 515 | { |
| 516 | struct tipc_node *node; |
| 517 | struct tipc_sock_conn *conn; |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 518 | int err = 0; |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 519 | |
Ying Xue | 3474753 | 2015-01-09 15:27:10 +0800 | [diff] [blame] | 520 | if (in_own_node(net, dnode)) |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 521 | return 0; |
| 522 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 523 | node = tipc_node_find(net, dnode); |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 524 | if (!node) { |
| 525 | pr_warn("Connecting sock to node 0x%x failed\n", dnode); |
| 526 | return -EHOSTUNREACH; |
| 527 | } |
| 528 | conn = kmalloc(sizeof(*conn), GFP_ATOMIC); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 529 | if (!conn) { |
| 530 | err = -EHOSTUNREACH; |
| 531 | goto exit; |
| 532 | } |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 533 | conn->peer_node = dnode; |
| 534 | conn->port = port; |
| 535 | conn->peer_port = peer_port; |
| 536 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 537 | tipc_node_write_lock(node); |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 538 | list_add_tail(&conn->list, &node->conn_sks); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 539 | tipc_node_write_unlock(node); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 540 | exit: |
| 541 | tipc_node_put(node); |
| 542 | return err; |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 543 | } |
| 544 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 545 | void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port) |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 546 | { |
| 547 | struct tipc_node *node; |
| 548 | struct tipc_sock_conn *conn, *safe; |
| 549 | |
Ying Xue | 3474753 | 2015-01-09 15:27:10 +0800 | [diff] [blame] | 550 | if (in_own_node(net, dnode)) |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 551 | return; |
| 552 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 553 | node = tipc_node_find(net, dnode); |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 554 | if (!node) |
| 555 | return; |
| 556 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 557 | tipc_node_write_lock(node); |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 558 | list_for_each_entry_safe(conn, safe, &node->conn_sks, list) { |
| 559 | if (port != conn->port) |
| 560 | continue; |
| 561 | list_del(&conn->list); |
| 562 | kfree(conn); |
| 563 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 564 | tipc_node_write_unlock(node); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 565 | tipc_node_put(node); |
Jon Paul Maloy | 02be61a9 | 2014-08-22 18:09:08 -0400 | [diff] [blame] | 566 | } |
| 567 | |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 568 | static void tipc_node_clear_links(struct tipc_node *node) |
| 569 | { |
| 570 | int i; |
| 571 | |
| 572 | for (i = 0; i < MAX_BEARERS; i++) { |
| 573 | struct tipc_link_entry *le = &node->links[i]; |
| 574 | |
| 575 | if (le->link) { |
| 576 | kfree(le->link); |
| 577 | le->link = NULL; |
| 578 | node->link_cnt--; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* tipc_node_cleanup - delete nodes that does not |
| 584 | * have active links for NODE_CLEANUP_AFTER time |
| 585 | */ |
| 586 | static int tipc_node_cleanup(struct tipc_node *peer) |
| 587 | { |
| 588 | struct tipc_net *tn = tipc_net(peer->net); |
| 589 | bool deleted = false; |
| 590 | |
| 591 | spin_lock_bh(&tn->node_list_lock); |
| 592 | tipc_node_write_lock(peer); |
| 593 | |
| 594 | if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) { |
| 595 | tipc_node_clear_links(peer); |
| 596 | tipc_node_delete_from_list(peer); |
| 597 | deleted = true; |
| 598 | } |
| 599 | tipc_node_write_unlock(peer); |
| 600 | spin_unlock_bh(&tn->node_list_lock); |
| 601 | return deleted; |
| 602 | } |
| 603 | |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 604 | /* tipc_node_timeout - handle expiration of node timer |
| 605 | */ |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 606 | static void tipc_node_timeout(struct timer_list *t) |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 607 | { |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 608 | struct tipc_node *n = from_timer(n, t, timer); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 609 | struct tipc_link_entry *le; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 610 | struct sk_buff_head xmitq; |
Tung Nguyen | 759f29b | 2018-06-28 22:39:25 +0200 | [diff] [blame] | 611 | int remains = n->link_cnt; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 612 | int bearer_id; |
| 613 | int rc = 0; |
| 614 | |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 615 | if (!node_is_up(n) && tipc_node_cleanup(n)) { |
| 616 | /*Removing the reference of Timer*/ |
| 617 | tipc_node_put(n); |
| 618 | return; |
| 619 | } |
| 620 | |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 621 | __skb_queue_head_init(&xmitq); |
| 622 | |
Tung Nguyen | 759f29b | 2018-06-28 22:39:25 +0200 | [diff] [blame] | 623 | for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) { |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 624 | tipc_node_read_lock(n); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 625 | le = &n->links[bearer_id]; |
| 626 | if (le->link) { |
Tung Nguyen | 759f29b | 2018-06-28 22:39:25 +0200 | [diff] [blame] | 627 | spin_lock_bh(&le->lock); |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 628 | /* Link tolerance may change asynchronously: */ |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 629 | tipc_node_calculate_timer(n, le->link); |
| 630 | rc = tipc_link_timeout(le->link, &xmitq); |
Tung Nguyen | 759f29b | 2018-06-28 22:39:25 +0200 | [diff] [blame] | 631 | spin_unlock_bh(&le->lock); |
| 632 | remains--; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 633 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 634 | tipc_node_read_unlock(n); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 635 | tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr); |
| 636 | if (rc & TIPC_LINK_DOWN_EVT) |
| 637 | tipc_node_link_down(n, bearer_id, false); |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 638 | } |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 639 | mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv)); |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 642 | /** |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 643 | * __tipc_node_link_up - handle addition of link |
| 644 | * Node lock must be held by caller |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 645 | * Link becomes active (alone or shared) or standby, depending on its priority. |
| 646 | */ |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 647 | static void __tipc_node_link_up(struct tipc_node *n, int bearer_id, |
| 648 | struct sk_buff_head *xmitq) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 649 | { |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 650 | int *slot0 = &n->active_links[0]; |
| 651 | int *slot1 = &n->active_links[1]; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 652 | struct tipc_link *ol = node_active_link(n, 0); |
| 653 | struct tipc_link *nl = n->links[bearer_id].link; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 654 | |
Jon Paul Maloy | e7142c3 | 2016-05-11 19:15:45 -0400 | [diff] [blame] | 655 | if (!nl || tipc_link_is_up(nl)) |
Jon Paul Maloy | 73f646c | 2015-10-15 14:52:44 -0400 | [diff] [blame] | 656 | return; |
| 657 | |
| 658 | tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT); |
| 659 | if (!tipc_link_is_up(nl)) |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 660 | return; |
| 661 | |
Jon Paul Maloy | 9d13ec6 | 2015-07-16 16:54:19 -0400 | [diff] [blame] | 662 | n->working_links++; |
| 663 | n->action_flags |= TIPC_NOTIFY_LINK_UP; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 664 | n->link_id = tipc_link_id(nl); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 665 | |
| 666 | /* Leave room for tunnel header when returning 'mtu' to users: */ |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 667 | n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE; |
Ying Xue | 7b8613e | 2014-10-20 14:44:25 +0800 | [diff] [blame] | 668 | |
Jon Paul Maloy | cbeb83c | 2015-07-30 18:24:15 -0400 | [diff] [blame] | 669 | tipc_bearer_add_dest(n->net, bearer_id, n->addr); |
Jon Paul Maloy | b06b281 | 2015-10-22 08:51:42 -0400 | [diff] [blame] | 670 | tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id); |
Jon Paul Maloy | cbeb83c | 2015-07-30 18:24:15 -0400 | [diff] [blame] | 671 | |
Erik Hugne | 3fa9cac | 2015-01-22 17:10:31 +0100 | [diff] [blame] | 672 | pr_debug("Established link <%s> on network plane %c\n", |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 673 | tipc_link_name(nl), tipc_link_plane(nl)); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 674 | |
Jon Paul Maloy | 34b9cd6 | 2016-04-15 13:33:07 -0400 | [diff] [blame] | 675 | /* Ensure that a STATE message goes first */ |
| 676 | tipc_link_build_state_msg(nl, xmitq); |
| 677 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 678 | /* First link? => give it both slots */ |
| 679 | if (!ol) { |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 680 | *slot0 = bearer_id; |
| 681 | *slot1 = bearer_id; |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 682 | tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT); |
| 683 | n->action_flags |= TIPC_NOTIFY_NODE_UP; |
Jon Paul Maloy | def22c4 | 2016-04-28 20:16:08 -0400 | [diff] [blame] | 684 | tipc_link_set_active(nl, true); |
Jon Paul Maloy | b06b281 | 2015-10-22 08:51:42 -0400 | [diff] [blame] | 685 | tipc_bcast_add_peer(n->net, nl, xmitq); |
Jon Paul Maloy | 9d13ec6 | 2015-07-16 16:54:19 -0400 | [diff] [blame] | 686 | return; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 687 | } |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 688 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 689 | /* Second link => redistribute slots */ |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 690 | if (tipc_link_prio(nl) > tipc_link_prio(ol)) { |
| 691 | pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol)); |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 692 | *slot0 = bearer_id; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 693 | *slot1 = bearer_id; |
Jon Paul Maloy | c72fa87 | 2015-10-22 08:51:46 -0400 | [diff] [blame] | 694 | tipc_link_set_active(nl, true); |
| 695 | tipc_link_set_active(ol, false); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 696 | } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) { |
Jon Paul Maloy | c72fa87 | 2015-10-22 08:51:46 -0400 | [diff] [blame] | 697 | tipc_link_set_active(nl, true); |
Jon Paul Maloy | c49a0a84 | 2015-10-22 08:51:47 -0400 | [diff] [blame] | 698 | *slot1 = bearer_id; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 699 | } else { |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 700 | pr_debug("New link <%s> is standby\n", tipc_link_name(nl)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 701 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 702 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 703 | /* Prepare synchronization with first link */ |
| 704 | tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 708 | * tipc_node_link_up - handle addition of link |
| 709 | * |
| 710 | * Link becomes active (alone or shared) or standby, depending on its priority. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 711 | */ |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 712 | static void tipc_node_link_up(struct tipc_node *n, int bearer_id, |
| 713 | struct sk_buff_head *xmitq) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 714 | { |
Jon Paul Maloy | de7e07f | 2016-04-15 13:33:06 -0400 | [diff] [blame] | 715 | struct tipc_media_addr *maddr; |
| 716 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 717 | tipc_node_write_lock(n); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 718 | __tipc_node_link_up(n, bearer_id, xmitq); |
Jon Paul Maloy | de7e07f | 2016-04-15 13:33:06 -0400 | [diff] [blame] | 719 | maddr = &n->links[bearer_id].maddr; |
| 720 | tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 721 | tipc_node_write_unlock(n); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
| 725 | * __tipc_node_link_down - handle loss of link |
| 726 | */ |
| 727 | static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id, |
| 728 | struct sk_buff_head *xmitq, |
| 729 | struct tipc_media_addr **maddr) |
| 730 | { |
| 731 | struct tipc_link_entry *le = &n->links[*bearer_id]; |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 732 | int *slot0 = &n->active_links[0]; |
| 733 | int *slot1 = &n->active_links[1]; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 734 | int i, highest = 0, prio; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 735 | struct tipc_link *l, *_l, *tnl; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 736 | |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 737 | l = n->links[*bearer_id].link; |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 738 | if (!l || tipc_link_is_reset(l)) |
Jon Paul Maloy | 655fb24 | 2015-07-30 18:24:17 -0400 | [diff] [blame] | 739 | return; |
| 740 | |
Jon Paul Maloy | 9d13ec6 | 2015-07-16 16:54:19 -0400 | [diff] [blame] | 741 | n->working_links--; |
| 742 | n->action_flags |= TIPC_NOTIFY_LINK_DOWN; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 743 | n->link_id = tipc_link_id(l); |
Allan Stephens | 5392d64 | 2006-06-25 23:52:50 -0700 | [diff] [blame] | 744 | |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 745 | tipc_bearer_remove_dest(n->net, *bearer_id, n->addr); |
Jon Paul Maloy | 655fb24 | 2015-07-30 18:24:17 -0400 | [diff] [blame] | 746 | |
Erik Hugne | 3fa9cac | 2015-01-22 17:10:31 +0100 | [diff] [blame] | 747 | pr_debug("Lost link <%s> on network plane %c\n", |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 748 | tipc_link_name(l), tipc_link_plane(l)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 749 | |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 750 | /* Select new active link if any available */ |
| 751 | *slot0 = INVALID_BEARER_ID; |
| 752 | *slot1 = INVALID_BEARER_ID; |
| 753 | for (i = 0; i < MAX_BEARERS; i++) { |
| 754 | _l = n->links[i].link; |
| 755 | if (!_l || !tipc_link_is_up(_l)) |
| 756 | continue; |
Jon Paul Maloy | 655fb24 | 2015-07-30 18:24:17 -0400 | [diff] [blame] | 757 | if (_l == l) |
| 758 | continue; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 759 | prio = tipc_link_prio(_l); |
| 760 | if (prio < highest) |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 761 | continue; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 762 | if (prio > highest) { |
| 763 | highest = prio; |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 764 | *slot0 = i; |
| 765 | *slot1 = i; |
| 766 | continue; |
| 767 | } |
| 768 | *slot1 = i; |
| 769 | } |
Jon Paul Maloy | 655fb24 | 2015-07-30 18:24:17 -0400 | [diff] [blame] | 770 | |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 771 | if (!node_is_up(n)) { |
Jon Paul Maloy | c819930 | 2015-10-15 14:52:46 -0400 | [diff] [blame] | 772 | if (tipc_link_peer_is_down(l)) |
| 773 | tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT); |
| 774 | tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT); |
| 775 | tipc_link_fsm_evt(l, LINK_RESET_EVT); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 776 | tipc_link_reset(l); |
Jon Paul Maloy | 282b3a0 | 2015-10-15 14:52:45 -0400 | [diff] [blame] | 777 | tipc_link_build_reset_msg(l, xmitq); |
| 778 | *maddr = &n->links[*bearer_id].maddr; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 779 | node_lost_contact(n, &le->inputq); |
Jon Paul Maloy | b06b281 | 2015-10-22 08:51:42 -0400 | [diff] [blame] | 780 | tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 781 | return; |
| 782 | } |
Jon Paul Maloy | b06b281 | 2015-10-22 08:51:42 -0400 | [diff] [blame] | 783 | tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 784 | |
| 785 | /* There is still a working link => initiate failover */ |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 786 | *bearer_id = n->active_links[0]; |
| 787 | tnl = n->links[*bearer_id].link; |
Jon Paul Maloy | 5ae2f8e | 2015-08-20 02:12:55 -0400 | [diff] [blame] | 788 | tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT); |
| 789 | tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 790 | n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 791 | tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 792 | tipc_link_reset(l); |
Jon Paul Maloy | c819930 | 2015-10-15 14:52:46 -0400 | [diff] [blame] | 793 | tipc_link_fsm_evt(l, LINK_RESET_EVT); |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 794 | tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 795 | tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 796 | *maddr = &n->links[*bearer_id].maddr; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete) |
| 800 | { |
| 801 | struct tipc_link_entry *le = &n->links[bearer_id]; |
Jon Paul Maloy | 73f646c | 2015-10-15 14:52:44 -0400 | [diff] [blame] | 802 | struct tipc_link *l = le->link; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 803 | struct tipc_media_addr *maddr; |
| 804 | struct sk_buff_head xmitq; |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 805 | int old_bearer_id = bearer_id; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 806 | |
Jon Paul Maloy | 73f646c | 2015-10-15 14:52:44 -0400 | [diff] [blame] | 807 | if (!l) |
| 808 | return; |
| 809 | |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 810 | __skb_queue_head_init(&xmitq); |
| 811 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 812 | tipc_node_write_lock(n); |
Jon Paul Maloy | 73f646c | 2015-10-15 14:52:44 -0400 | [diff] [blame] | 813 | if (!tipc_link_is_establishing(l)) { |
| 814 | __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr); |
| 815 | if (delete) { |
| 816 | kfree(l); |
| 817 | le->link = NULL; |
| 818 | n->link_cnt--; |
| 819 | } |
| 820 | } else { |
| 821 | /* Defuse pending tipc_node_link_up() */ |
| 822 | tipc_link_fsm_evt(l, LINK_RESET_EVT); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 823 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 824 | tipc_node_write_unlock(n); |
Jon Paul Maloy | 35c55c9 | 2016-06-13 20:46:22 -0400 | [diff] [blame] | 825 | if (delete) |
| 826 | tipc_mon_remove_peer(n->net, n->addr, old_bearer_id); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 827 | tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr); |
| 828 | tipc_sk_rcv(n->net, &le->inputq); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 831 | static bool node_is_up(struct tipc_node *n) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 832 | { |
Jon Paul Maloy | 36e78a4 | 2015-07-16 16:54:22 -0400 | [diff] [blame] | 833 | return n->active_links[0] != INVALID_BEARER_ID; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 836 | bool tipc_node_is_up(struct net *net, u32 addr) |
| 837 | { |
| 838 | struct tipc_node *n; |
| 839 | bool retval = false; |
| 840 | |
| 841 | if (in_own_node(net, addr)) |
| 842 | return true; |
| 843 | |
| 844 | n = tipc_node_find(net, addr); |
| 845 | if (!n) |
| 846 | return false; |
| 847 | retval = node_is_up(n); |
| 848 | tipc_node_put(n); |
| 849 | return retval; |
| 850 | } |
| 851 | |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 852 | static u32 tipc_node_suggest_addr(struct net *net, u32 addr) |
| 853 | { |
| 854 | struct tipc_node *n; |
| 855 | |
| 856 | addr ^= tipc_net(net)->random; |
| 857 | while ((n = tipc_node_find(net, addr))) { |
| 858 | tipc_node_put(n); |
| 859 | addr++; |
| 860 | } |
| 861 | return addr; |
| 862 | } |
| 863 | |
| 864 | /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not |
Jon Maloy | 2a57f18 | 2018-07-06 20:10:03 +0200 | [diff] [blame] | 865 | * Returns suggested address if any, otherwise 0 |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 866 | */ |
| 867 | u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr) |
| 868 | { |
| 869 | struct tipc_net *tn = tipc_net(net); |
| 870 | struct tipc_node *n; |
| 871 | |
| 872 | /* Suggest new address if some other peer is using this one */ |
| 873 | n = tipc_node_find(net, addr); |
| 874 | if (n) { |
| 875 | if (!memcmp(n->peer_id, id, NODE_ID_LEN)) |
| 876 | addr = 0; |
| 877 | tipc_node_put(n); |
| 878 | if (!addr) |
| 879 | return 0; |
| 880 | return tipc_node_suggest_addr(net, addr); |
| 881 | } |
| 882 | |
| 883 | /* Suggest previously used address if peer is known */ |
| 884 | n = tipc_node_find_by_id(net, id); |
| 885 | if (n) { |
| 886 | addr = n->addr; |
| 887 | tipc_node_put(n); |
Jon Maloy | 2a57f18 | 2018-07-06 20:10:03 +0200 | [diff] [blame] | 888 | return addr; |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 889 | } |
Jon Maloy | 2a57f18 | 2018-07-06 20:10:03 +0200 | [diff] [blame] | 890 | |
| 891 | /* Even this node may be in conflict */ |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 892 | if (tn->trial_addr == addr) |
| 893 | return tipc_node_suggest_addr(net, addr); |
| 894 | |
Jon Maloy | 2a57f18 | 2018-07-06 20:10:03 +0200 | [diff] [blame] | 895 | return 0; |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | void tipc_node_check_dest(struct net *net, u32 addr, |
| 899 | u8 *peer_id, struct tipc_bearer *b, |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 900 | u16 capabilities, u32 signature, |
| 901 | struct tipc_media_addr *maddr, |
| 902 | bool *respond, bool *dupl_addr) |
Jon Paul Maloy | d3a43b9 | 2015-07-16 16:54:20 -0400 | [diff] [blame] | 903 | { |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 904 | struct tipc_node *n; |
| 905 | struct tipc_link *l; |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 906 | struct tipc_link_entry *le; |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 907 | bool addr_match = false; |
| 908 | bool sign_match = false; |
| 909 | bool link_up = false; |
| 910 | bool accept_addr = false; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 911 | bool reset = true; |
Jon Paul Maloy | 0e05498 | 2015-10-22 08:51:36 -0400 | [diff] [blame] | 912 | char *if_name; |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 913 | unsigned long intv; |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 914 | |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 915 | *dupl_addr = false; |
| 916 | *respond = false; |
Jon Paul Maloy | d3a43b9 | 2015-07-16 16:54:20 -0400 | [diff] [blame] | 917 | |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 918 | n = tipc_node_create(net, addr, peer_id, capabilities); |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 919 | if (!n) |
| 920 | return; |
Jon Paul Maloy | d3a43b9 | 2015-07-16 16:54:20 -0400 | [diff] [blame] | 921 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 922 | tipc_node_write_lock(n); |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 923 | |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 924 | le = &n->links[b->identity]; |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 925 | |
| 926 | /* Prepare to validate requesting node's signature and media address */ |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 927 | l = le->link; |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 928 | link_up = l && tipc_link_is_up(l); |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 929 | addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr)); |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 930 | sign_match = (signature == n->signature); |
| 931 | |
| 932 | /* These three flags give us eight permutations: */ |
| 933 | |
| 934 | if (sign_match && addr_match && link_up) { |
| 935 | /* All is fine. Do nothing. */ |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 936 | reset = false; |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 937 | } else if (sign_match && addr_match && !link_up) { |
| 938 | /* Respond. The link will come up in due time */ |
| 939 | *respond = true; |
| 940 | } else if (sign_match && !addr_match && link_up) { |
| 941 | /* Peer has changed i/f address without rebooting. |
| 942 | * If so, the link will reset soon, and the next |
| 943 | * discovery will be accepted. So we can ignore it. |
| 944 | * It may also be an cloned or malicious peer having |
| 945 | * chosen the same node address and signature as an |
| 946 | * existing one. |
| 947 | * Ignore requests until the link goes down, if ever. |
| 948 | */ |
| 949 | *dupl_addr = true; |
| 950 | } else if (sign_match && !addr_match && !link_up) { |
| 951 | /* Peer link has changed i/f address without rebooting. |
| 952 | * It may also be a cloned or malicious peer; we can't |
| 953 | * distinguish between the two. |
| 954 | * The signature is correct, so we must accept. |
| 955 | */ |
| 956 | accept_addr = true; |
| 957 | *respond = true; |
| 958 | } else if (!sign_match && addr_match && link_up) { |
| 959 | /* Peer node rebooted. Two possibilities: |
| 960 | * - Delayed re-discovery; this link endpoint has already |
| 961 | * reset and re-established contact with the peer, before |
| 962 | * receiving a discovery message from that node. |
| 963 | * (The peer happened to receive one from this node first). |
| 964 | * - The peer came back so fast that our side has not |
| 965 | * discovered it yet. Probing from this side will soon |
| 966 | * reset the link, since there can be no working link |
| 967 | * endpoint at the peer end, and the link will re-establish. |
| 968 | * Accept the signature, since it comes from a known peer. |
| 969 | */ |
| 970 | n->signature = signature; |
| 971 | } else if (!sign_match && addr_match && !link_up) { |
| 972 | /* The peer node has rebooted. |
| 973 | * Accept signature, since it is a known peer. |
| 974 | */ |
| 975 | n->signature = signature; |
| 976 | *respond = true; |
| 977 | } else if (!sign_match && !addr_match && link_up) { |
| 978 | /* Peer rebooted with new address, or a new/duplicate peer. |
| 979 | * Ignore until the link goes down, if ever. |
| 980 | */ |
| 981 | *dupl_addr = true; |
| 982 | } else if (!sign_match && !addr_match && !link_up) { |
| 983 | /* Peer rebooted with new address, or it is a new peer. |
| 984 | * Accept signature and address. |
| 985 | */ |
| 986 | n->signature = signature; |
| 987 | accept_addr = true; |
| 988 | *respond = true; |
| 989 | } |
| 990 | |
| 991 | if (!accept_addr) |
| 992 | goto exit; |
| 993 | |
| 994 | /* Now create new link if not already existing */ |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 995 | if (!l) { |
Jon Maloy | 2026364 | 2018-03-22 20:42:47 +0100 | [diff] [blame] | 996 | if (n->link_cnt == 2) |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 997 | goto exit; |
Jon Maloy | 2026364 | 2018-03-22 20:42:47 +0100 | [diff] [blame] | 998 | |
Jon Paul Maloy | 0e05498 | 2015-10-22 08:51:36 -0400 | [diff] [blame] | 999 | if_name = strchr(b->name, ':') + 1; |
Jon Paul Maloy | c72fa87 | 2015-10-22 08:51:46 -0400 | [diff] [blame] | 1000 | if (!tipc_link_create(net, if_name, b->identity, b->tolerance, |
Jon Paul Maloy | 0e05498 | 2015-10-22 08:51:36 -0400 | [diff] [blame] | 1001 | b->net_plane, b->mtu, b->priority, |
| 1002 | b->window, mod(tipc_net(net)->random), |
Jon Maloy | 25b0b9c | 2018-03-22 20:42:51 +0100 | [diff] [blame] | 1003 | tipc_own_addr(net), addr, peer_id, |
Jon Paul Maloy | 2af5ae3 | 2015-10-22 08:51:48 -0400 | [diff] [blame] | 1004 | n->capabilities, |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1005 | tipc_bc_sndlink(n->net), n->bc_entry.link, |
| 1006 | &le->inputq, |
| 1007 | &n->bc_entry.namedq, &l)) { |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 1008 | *respond = false; |
| 1009 | goto exit; |
| 1010 | } |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 1011 | tipc_link_reset(l); |
Jon Paul Maloy | c819930 | 2015-10-15 14:52:46 -0400 | [diff] [blame] | 1012 | tipc_link_fsm_evt(l, LINK_RESET_EVT); |
Jon Paul Maloy | 17b2063 | 2015-08-20 02:12:54 -0400 | [diff] [blame] | 1013 | if (n->state == NODE_FAILINGOVER) |
| 1014 | tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT); |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 1015 | le->link = l; |
| 1016 | n->link_cnt++; |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 1017 | tipc_node_calculate_timer(n, l); |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 1018 | if (n->link_cnt == 1) { |
| 1019 | intv = jiffies + msecs_to_jiffies(n->keepalive_intv); |
| 1020 | if (!mod_timer(&n->timer, intv)) |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 1021 | tipc_node_get(n); |
Jon Paul Maloy | 5ca509f | 2016-06-08 12:00:05 -0400 | [diff] [blame] | 1022 | } |
Jon Paul Maloy | 8a1577c | 2015-07-16 16:54:29 -0400 | [diff] [blame] | 1023 | } |
Jon Paul Maloy | 440d896 | 2015-07-30 18:24:26 -0400 | [diff] [blame] | 1024 | memcpy(&le->maddr, maddr, sizeof(*maddr)); |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 1025 | exit: |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1026 | tipc_node_write_unlock(n); |
Richard Alpe | 2837f39 | 2016-03-03 14:20:41 +0100 | [diff] [blame] | 1027 | if (reset && l && !tipc_link_is_reset(l)) |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1028 | tipc_node_link_down(n, b->identity, false); |
Jon Paul Maloy | cf14881 | 2015-07-30 18:24:22 -0400 | [diff] [blame] | 1029 | tipc_node_put(n); |
Jon Paul Maloy | d3a43b9 | 2015-07-16 16:54:20 -0400 | [diff] [blame] | 1030 | } |
| 1031 | |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1032 | void tipc_node_delete_links(struct net *net, int bearer_id) |
| 1033 | { |
| 1034 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1035 | struct tipc_node *n; |
| 1036 | |
| 1037 | rcu_read_lock(); |
| 1038 | list_for_each_entry_rcu(n, &tn->node_list, list) { |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1039 | tipc_node_link_down(n, bearer_id, true); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1040 | } |
| 1041 | rcu_read_unlock(); |
| 1042 | } |
| 1043 | |
| 1044 | static void tipc_node_reset_links(struct tipc_node *n) |
| 1045 | { |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1046 | int i; |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1047 | |
Jon Maloy | d50ccc2 | 2018-03-22 20:42:50 +0100 | [diff] [blame] | 1048 | pr_warn("Resetting all links to %x\n", n->addr); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1049 | |
| 1050 | for (i = 0; i < MAX_BEARERS; i++) { |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1051 | tipc_node_link_down(n, i, false); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1052 | } |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1055 | /* tipc_node_fsm_evt - node finite state machine |
| 1056 | * Determines when contact is allowed with peer node |
| 1057 | */ |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1058 | static void tipc_node_fsm_evt(struct tipc_node *n, int evt) |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1059 | { |
| 1060 | int state = n->state; |
| 1061 | |
| 1062 | switch (state) { |
| 1063 | case SELF_DOWN_PEER_DOWN: |
| 1064 | switch (evt) { |
| 1065 | case SELF_ESTABL_CONTACT_EVT: |
| 1066 | state = SELF_UP_PEER_COMING; |
| 1067 | break; |
| 1068 | case PEER_ESTABL_CONTACT_EVT: |
| 1069 | state = SELF_COMING_PEER_UP; |
| 1070 | break; |
| 1071 | case SELF_LOST_CONTACT_EVT: |
| 1072 | case PEER_LOST_CONTACT_EVT: |
| 1073 | break; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1074 | case NODE_SYNCH_END_EVT: |
| 1075 | case NODE_SYNCH_BEGIN_EVT: |
| 1076 | case NODE_FAILOVER_BEGIN_EVT: |
| 1077 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1078 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1079 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1080 | } |
| 1081 | break; |
| 1082 | case SELF_UP_PEER_UP: |
| 1083 | switch (evt) { |
| 1084 | case SELF_LOST_CONTACT_EVT: |
| 1085 | state = SELF_DOWN_PEER_LEAVING; |
| 1086 | break; |
| 1087 | case PEER_LOST_CONTACT_EVT: |
| 1088 | state = SELF_LEAVING_PEER_DOWN; |
| 1089 | break; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1090 | case NODE_SYNCH_BEGIN_EVT: |
| 1091 | state = NODE_SYNCHING; |
| 1092 | break; |
| 1093 | case NODE_FAILOVER_BEGIN_EVT: |
| 1094 | state = NODE_FAILINGOVER; |
| 1095 | break; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1096 | case SELF_ESTABL_CONTACT_EVT: |
| 1097 | case PEER_ESTABL_CONTACT_EVT: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1098 | case NODE_SYNCH_END_EVT: |
| 1099 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1100 | break; |
| 1101 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1102 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1103 | } |
| 1104 | break; |
| 1105 | case SELF_DOWN_PEER_LEAVING: |
| 1106 | switch (evt) { |
| 1107 | case PEER_LOST_CONTACT_EVT: |
| 1108 | state = SELF_DOWN_PEER_DOWN; |
| 1109 | break; |
| 1110 | case SELF_ESTABL_CONTACT_EVT: |
| 1111 | case PEER_ESTABL_CONTACT_EVT: |
| 1112 | case SELF_LOST_CONTACT_EVT: |
| 1113 | break; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1114 | case NODE_SYNCH_END_EVT: |
| 1115 | case NODE_SYNCH_BEGIN_EVT: |
| 1116 | case NODE_FAILOVER_BEGIN_EVT: |
| 1117 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1118 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1119 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1120 | } |
| 1121 | break; |
| 1122 | case SELF_UP_PEER_COMING: |
| 1123 | switch (evt) { |
| 1124 | case PEER_ESTABL_CONTACT_EVT: |
| 1125 | state = SELF_UP_PEER_UP; |
| 1126 | break; |
| 1127 | case SELF_LOST_CONTACT_EVT: |
Jon Paul Maloy | c4282ca | 2016-06-08 12:00:04 -0400 | [diff] [blame] | 1128 | state = SELF_DOWN_PEER_DOWN; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1129 | break; |
| 1130 | case SELF_ESTABL_CONTACT_EVT: |
| 1131 | case PEER_LOST_CONTACT_EVT: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1132 | case NODE_SYNCH_END_EVT: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1133 | case NODE_FAILOVER_BEGIN_EVT: |
Jon Paul Maloy | 73f646c | 2015-10-15 14:52:44 -0400 | [diff] [blame] | 1134 | break; |
| 1135 | case NODE_SYNCH_BEGIN_EVT: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1136 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1137 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1138 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1139 | } |
| 1140 | break; |
| 1141 | case SELF_COMING_PEER_UP: |
| 1142 | switch (evt) { |
| 1143 | case SELF_ESTABL_CONTACT_EVT: |
| 1144 | state = SELF_UP_PEER_UP; |
| 1145 | break; |
| 1146 | case PEER_LOST_CONTACT_EVT: |
Jon Paul Maloy | c4282ca | 2016-06-08 12:00:04 -0400 | [diff] [blame] | 1147 | state = SELF_DOWN_PEER_DOWN; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1148 | break; |
| 1149 | case SELF_LOST_CONTACT_EVT: |
| 1150 | case PEER_ESTABL_CONTACT_EVT: |
| 1151 | break; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1152 | case NODE_SYNCH_END_EVT: |
| 1153 | case NODE_SYNCH_BEGIN_EVT: |
| 1154 | case NODE_FAILOVER_BEGIN_EVT: |
| 1155 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1156 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1157 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1158 | } |
| 1159 | break; |
| 1160 | case SELF_LEAVING_PEER_DOWN: |
| 1161 | switch (evt) { |
| 1162 | case SELF_LOST_CONTACT_EVT: |
| 1163 | state = SELF_DOWN_PEER_DOWN; |
| 1164 | break; |
| 1165 | case SELF_ESTABL_CONTACT_EVT: |
| 1166 | case PEER_ESTABL_CONTACT_EVT: |
| 1167 | case PEER_LOST_CONTACT_EVT: |
| 1168 | break; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1169 | case NODE_SYNCH_END_EVT: |
| 1170 | case NODE_SYNCH_BEGIN_EVT: |
| 1171 | case NODE_FAILOVER_BEGIN_EVT: |
| 1172 | case NODE_FAILOVER_END_EVT: |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1173 | default: |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1174 | goto illegal_evt; |
| 1175 | } |
| 1176 | break; |
| 1177 | case NODE_FAILINGOVER: |
| 1178 | switch (evt) { |
| 1179 | case SELF_LOST_CONTACT_EVT: |
| 1180 | state = SELF_DOWN_PEER_LEAVING; |
| 1181 | break; |
| 1182 | case PEER_LOST_CONTACT_EVT: |
| 1183 | state = SELF_LEAVING_PEER_DOWN; |
| 1184 | break; |
| 1185 | case NODE_FAILOVER_END_EVT: |
| 1186 | state = SELF_UP_PEER_UP; |
| 1187 | break; |
| 1188 | case NODE_FAILOVER_BEGIN_EVT: |
| 1189 | case SELF_ESTABL_CONTACT_EVT: |
| 1190 | case PEER_ESTABL_CONTACT_EVT: |
| 1191 | break; |
| 1192 | case NODE_SYNCH_BEGIN_EVT: |
| 1193 | case NODE_SYNCH_END_EVT: |
| 1194 | default: |
| 1195 | goto illegal_evt; |
| 1196 | } |
| 1197 | break; |
| 1198 | case NODE_SYNCHING: |
| 1199 | switch (evt) { |
| 1200 | case SELF_LOST_CONTACT_EVT: |
| 1201 | state = SELF_DOWN_PEER_LEAVING; |
| 1202 | break; |
| 1203 | case PEER_LOST_CONTACT_EVT: |
| 1204 | state = SELF_LEAVING_PEER_DOWN; |
| 1205 | break; |
| 1206 | case NODE_SYNCH_END_EVT: |
| 1207 | state = SELF_UP_PEER_UP; |
| 1208 | break; |
| 1209 | case NODE_FAILOVER_BEGIN_EVT: |
| 1210 | state = NODE_FAILINGOVER; |
| 1211 | break; |
| 1212 | case NODE_SYNCH_BEGIN_EVT: |
| 1213 | case SELF_ESTABL_CONTACT_EVT: |
| 1214 | case PEER_ESTABL_CONTACT_EVT: |
| 1215 | break; |
| 1216 | case NODE_FAILOVER_END_EVT: |
| 1217 | default: |
| 1218 | goto illegal_evt; |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1219 | } |
| 1220 | break; |
| 1221 | default: |
| 1222 | pr_err("Unknown node fsm state %x\n", state); |
| 1223 | break; |
| 1224 | } |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1225 | n->state = state; |
Jon Paul Maloy | 66996b6 | 2015-07-30 18:24:18 -0400 | [diff] [blame] | 1226 | return; |
| 1227 | |
| 1228 | illegal_evt: |
| 1229 | pr_err("Illegal node fsm evt %x in state %x\n", evt, state); |
Jon Paul Maloy | 1a20cc2 | 2015-07-16 16:54:30 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1232 | static void node_lost_contact(struct tipc_node *n, |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1233 | struct sk_buff_head *inputq) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1234 | { |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1235 | struct tipc_sock_conn *conn, *safe; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1236 | struct tipc_link *l; |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1237 | struct list_head *conns = &n->conn_sks; |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1238 | struct sk_buff *skb; |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1239 | uint i; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1240 | |
Jon Maloy | d50ccc2 | 2018-03-22 20:42:50 +0100 | [diff] [blame] | 1241 | pr_debug("Lost contact with %x\n", n->addr); |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 1242 | n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER); |
Allan Stephens | c5bd4d8 | 2011-04-07 11:58:08 -0400 | [diff] [blame] | 1243 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1244 | /* Clean up broadcast state */ |
Jon Paul Maloy | b06b281 | 2015-10-22 08:51:42 -0400 | [diff] [blame] | 1245 | tipc_bcast_remove_peer(n->net, n->bc_entry.link); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1246 | |
Jon Paul Maloy | dff29b1 | 2015-04-02 09:33:01 -0400 | [diff] [blame] | 1247 | /* Abort any ongoing link failover */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1248 | for (i = 0; i < MAX_BEARERS; i++) { |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1249 | l = n->links[i].link; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1250 | if (l) |
| 1251 | tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1252 | } |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1253 | |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1254 | /* Notify publications from this node */ |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1255 | n->action_flags |= TIPC_NOTIFY_NODE_DOWN; |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1256 | |
| 1257 | /* Notify sockets connected to node */ |
| 1258 | list_for_each_entry_safe(conn, safe, conns, list) { |
| 1259 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1260 | SHORT_H_SIZE, 0, tipc_own_addr(n->net), |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1261 | conn->peer_node, conn->port, |
| 1262 | conn->peer_port, TIPC_ERR_NO_NODE); |
Jon Paul Maloy | 23d8335 | 2015-07-30 18:24:24 -0400 | [diff] [blame] | 1263 | if (likely(skb)) |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1264 | skb_queue_tail(inputq, skb); |
Jon Paul Maloy | 708ac32 | 2015-02-05 08:36:42 -0500 | [diff] [blame] | 1265 | list_del(&conn->list); |
| 1266 | kfree(conn); |
| 1267 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1270 | /** |
| 1271 | * tipc_node_get_linkname - get the name of a link |
| 1272 | * |
| 1273 | * @bearer_id: id of the bearer |
| 1274 | * @node: peer node address |
| 1275 | * @linkname: link name output buffer |
| 1276 | * |
| 1277 | * Returns 0 on success |
| 1278 | */ |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1279 | int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr, |
| 1280 | char *linkname, size_t len) |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1281 | { |
| 1282 | struct tipc_link *link; |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 1283 | int err = -EINVAL; |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1284 | struct tipc_node *node = tipc_node_find(net, addr); |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1285 | |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 1286 | if (!node) |
| 1287 | return err; |
| 1288 | |
| 1289 | if (bearer_id >= MAX_BEARERS) |
| 1290 | goto exit; |
| 1291 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1292 | tipc_node_read_lock(node); |
Jon Paul Maloy | 9d13ec6 | 2015-07-16 16:54:19 -0400 | [diff] [blame] | 1293 | link = node->links[bearer_id].link; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1294 | if (link) { |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1295 | strncpy(linkname, tipc_link_name(link), len); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 1296 | err = 0; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1297 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1298 | tipc_node_read_unlock(node); |
Parthasarathy Bhuvaragan | 991ca84 | 2017-08-24 16:31:24 +0200 | [diff] [blame] | 1299 | exit: |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 1300 | tipc_node_put(node); |
| 1301 | return err; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 1302 | } |
Ying Xue | 9db9fdd | 2014-05-05 08:56:12 +0800 | [diff] [blame] | 1303 | |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1304 | /* Caller should hold node lock for the passed node */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 1305 | static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node) |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1306 | { |
| 1307 | void *hdr; |
| 1308 | struct nlattr *attrs; |
| 1309 | |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1310 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1311 | NLM_F_MULTI, TIPC_NL_NODE_GET); |
| 1312 | if (!hdr) |
| 1313 | return -EMSGSIZE; |
| 1314 | |
| 1315 | attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE); |
| 1316 | if (!attrs) |
| 1317 | goto msg_full; |
| 1318 | |
| 1319 | if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr)) |
| 1320 | goto attr_msg_full; |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 1321 | if (node_is_up(node)) |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1322 | if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP)) |
| 1323 | goto attr_msg_full; |
| 1324 | |
| 1325 | nla_nest_end(msg->skb, attrs); |
| 1326 | genlmsg_end(msg->skb, hdr); |
| 1327 | |
| 1328 | return 0; |
| 1329 | |
| 1330 | attr_msg_full: |
| 1331 | nla_nest_cancel(msg->skb, attrs); |
| 1332 | msg_full: |
| 1333 | genlmsg_cancel(msg->skb, hdr); |
| 1334 | |
| 1335 | return -EMSGSIZE; |
| 1336 | } |
| 1337 | |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1338 | /** |
| 1339 | * tipc_node_xmit() is the general link level function for message sending |
| 1340 | * @net: the applicable net namespace |
| 1341 | * @list: chain of buffers containing message |
| 1342 | * @dnode: address of destination node |
| 1343 | * @selector: a number used for deterministic link selection |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1344 | * Consumes the buffer chain. |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1345 | * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1346 | */ |
| 1347 | int tipc_node_xmit(struct net *net, struct sk_buff_head *list, |
| 1348 | u32 dnode, int selector) |
| 1349 | { |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1350 | struct tipc_link_entry *le = NULL; |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1351 | struct tipc_node *n; |
| 1352 | struct sk_buff_head xmitq; |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1353 | int bearer_id; |
| 1354 | int rc; |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1355 | |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1356 | if (in_own_node(net, dnode)) { |
Jon Paul Maloy | dc8d1eb | 2015-12-02 15:19:37 -0500 | [diff] [blame] | 1357 | tipc_sk_rcv(net, list); |
| 1358 | return 0; |
| 1359 | } |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1360 | |
| 1361 | n = tipc_node_find(net, dnode); |
| 1362 | if (unlikely(!n)) { |
| 1363 | skb_queue_purge(list); |
| 1364 | return -EHOSTUNREACH; |
| 1365 | } |
| 1366 | |
| 1367 | tipc_node_read_lock(n); |
| 1368 | bearer_id = n->active_links[selector & 1]; |
| 1369 | if (unlikely(bearer_id == INVALID_BEARER_ID)) { |
| 1370 | tipc_node_read_unlock(n); |
| 1371 | tipc_node_put(n); |
| 1372 | skb_queue_purge(list); |
| 1373 | return -EHOSTUNREACH; |
| 1374 | } |
| 1375 | |
| 1376 | __skb_queue_head_init(&xmitq); |
| 1377 | le = &n->links[bearer_id]; |
| 1378 | spin_lock_bh(&le->lock); |
| 1379 | rc = tipc_link_xmit(le->link, list, &xmitq); |
| 1380 | spin_unlock_bh(&le->lock); |
| 1381 | tipc_node_read_unlock(n); |
| 1382 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1383 | if (unlikely(rc == -ENOBUFS)) |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1384 | tipc_node_link_down(n, bearer_id, false); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1385 | else |
| 1386 | tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr); |
Richard Alpe | 4952cd3 | 2016-02-11 10:43:15 +0100 | [diff] [blame] | 1387 | |
| 1388 | tipc_node_put(n); |
| 1389 | |
Jon Paul Maloy | dc8d1eb | 2015-12-02 15:19:37 -0500 | [diff] [blame] | 1390 | return rc; |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /* tipc_node_xmit_skb(): send single buffer to destination |
| 1394 | * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE |
| 1395 | * messages, which will not be rejected |
| 1396 | * The only exception is datagram messages rerouted after secondary |
| 1397 | * lookup, which are rare and safe to dispose of anyway. |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1398 | */ |
| 1399 | int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode, |
| 1400 | u32 selector) |
| 1401 | { |
| 1402 | struct sk_buff_head head; |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1403 | |
| 1404 | skb_queue_head_init(&head); |
| 1405 | __skb_queue_tail(&head, skb); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1406 | tipc_node_xmit(net, &head, dnode, selector); |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | |
Jon Maloy | f70d37b | 2017-10-13 11:04:21 +0200 | [diff] [blame] | 1410 | /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations |
| 1411 | * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected |
| 1412 | */ |
| 1413 | int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq) |
| 1414 | { |
| 1415 | struct sk_buff *skb; |
| 1416 | u32 selector, dnode; |
| 1417 | |
| 1418 | while ((skb = __skb_dequeue(xmitq))) { |
| 1419 | selector = msg_origport(buf_msg(skb)); |
| 1420 | dnode = msg_destnode(buf_msg(skb)); |
| 1421 | tipc_node_xmit_skb(net, skb, dnode, selector); |
| 1422 | } |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 1426 | void tipc_node_broadcast(struct net *net, struct sk_buff *skb) |
| 1427 | { |
| 1428 | struct sk_buff *txskb; |
| 1429 | struct tipc_node *n; |
| 1430 | u32 dst; |
| 1431 | |
| 1432 | rcu_read_lock(); |
| 1433 | list_for_each_entry_rcu(n, tipc_nodes(net), list) { |
| 1434 | dst = n->addr; |
| 1435 | if (in_own_node(net, dst)) |
| 1436 | continue; |
Jon Maloy | 38077b8 | 2017-10-13 11:04:19 +0200 | [diff] [blame] | 1437 | if (!node_is_up(n)) |
Jon Paul Maloy | 1d7e1c2 | 2015-11-19 14:30:42 -0500 | [diff] [blame] | 1438 | continue; |
| 1439 | txskb = pskb_copy(skb, GFP_ATOMIC); |
| 1440 | if (!txskb) |
| 1441 | break; |
| 1442 | msg_set_destnode(buf_msg(txskb), dst); |
| 1443 | tipc_node_xmit_skb(net, txskb, dst, 0); |
| 1444 | } |
| 1445 | rcu_read_unlock(); |
| 1446 | |
| 1447 | kfree_skb(skb); |
| 1448 | } |
| 1449 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 1450 | static void tipc_node_mcast_rcv(struct tipc_node *n) |
| 1451 | { |
| 1452 | struct tipc_bclink_entry *be = &n->bc_entry; |
| 1453 | |
| 1454 | /* 'arrvq' is under inputq2's lock protection */ |
| 1455 | spin_lock_bh(&be->inputq2.lock); |
| 1456 | spin_lock_bh(&be->inputq1.lock); |
| 1457 | skb_queue_splice_tail_init(&be->inputq1, &be->arrvq); |
| 1458 | spin_unlock_bh(&be->inputq1.lock); |
| 1459 | spin_unlock_bh(&be->inputq2.lock); |
| 1460 | tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2); |
| 1461 | } |
| 1462 | |
Jon Paul Maloy | 02d11ca | 2016-09-01 13:52:49 -0400 | [diff] [blame] | 1463 | static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr, |
| 1464 | int bearer_id, struct sk_buff_head *xmitq) |
| 1465 | { |
| 1466 | struct tipc_link *ucl; |
| 1467 | int rc; |
| 1468 | |
| 1469 | rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr); |
| 1470 | |
| 1471 | if (rc & TIPC_LINK_DOWN_EVT) { |
Jon Paul Maloy | 40501f90 | 2017-08-21 17:59:30 +0200 | [diff] [blame] | 1472 | tipc_node_reset_links(n); |
Jon Paul Maloy | 02d11ca | 2016-09-01 13:52:49 -0400 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | |
| 1476 | if (!(rc & TIPC_LINK_SND_STATE)) |
| 1477 | return; |
| 1478 | |
| 1479 | /* If probe message, a STATE response will be sent anyway */ |
| 1480 | if (msg_probe(hdr)) |
| 1481 | return; |
| 1482 | |
| 1483 | /* Produce a STATE message carrying broadcast NACK */ |
| 1484 | tipc_node_read_lock(n); |
| 1485 | ucl = n->links[bearer_id].link; |
| 1486 | if (ucl) |
| 1487 | tipc_link_build_state_msg(ucl, xmitq); |
| 1488 | tipc_node_read_unlock(n); |
| 1489 | } |
| 1490 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1491 | /** |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1492 | * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node |
| 1493 | * @net: the applicable net namespace |
| 1494 | * @skb: TIPC packet |
| 1495 | * @bearer_id: id of bearer message arrived on |
| 1496 | * |
| 1497 | * Invoked with no locks held. |
| 1498 | */ |
Wu Fengguang | 742e038 | 2015-10-24 22:56:01 +0800 | [diff] [blame] | 1499 | static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id) |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1500 | { |
| 1501 | int rc; |
| 1502 | struct sk_buff_head xmitq; |
| 1503 | struct tipc_bclink_entry *be; |
| 1504 | struct tipc_link_entry *le; |
| 1505 | struct tipc_msg *hdr = buf_msg(skb); |
| 1506 | int usr = msg_user(hdr); |
| 1507 | u32 dnode = msg_destnode(hdr); |
| 1508 | struct tipc_node *n; |
| 1509 | |
| 1510 | __skb_queue_head_init(&xmitq); |
| 1511 | |
| 1512 | /* If NACK for other node, let rcv link for that node peek into it */ |
| 1513 | if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net))) |
| 1514 | n = tipc_node_find(net, dnode); |
| 1515 | else |
| 1516 | n = tipc_node_find(net, msg_prevnode(hdr)); |
| 1517 | if (!n) { |
| 1518 | kfree_skb(skb); |
| 1519 | return; |
| 1520 | } |
| 1521 | be = &n->bc_entry; |
| 1522 | le = &n->links[bearer_id]; |
| 1523 | |
| 1524 | rc = tipc_bcast_rcv(net, be->link, skb); |
| 1525 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1526 | /* Broadcast ACKs are sent on a unicast link */ |
Jon Paul Maloy | 02d11ca | 2016-09-01 13:52:49 -0400 | [diff] [blame] | 1527 | if (rc & TIPC_LINK_SND_STATE) { |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1528 | tipc_node_read_lock(n); |
Jon Paul Maloy | 34b9cd6 | 2016-04-15 13:33:07 -0400 | [diff] [blame] | 1529 | tipc_link_build_state_msg(le->link, &xmitq); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1530 | tipc_node_read_unlock(n); |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | if (!skb_queue_empty(&xmitq)) |
| 1534 | tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr); |
| 1535 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 1536 | if (!skb_queue_empty(&be->inputq1)) |
| 1537 | tipc_node_mcast_rcv(n); |
Jon Paul Maloy | 1fc07f3e | 2016-07-11 16:08:37 -0400 | [diff] [blame] | 1538 | |
Jon Paul Maloy | 40501f90 | 2017-08-21 17:59:30 +0200 | [diff] [blame] | 1539 | /* If reassembly or retransmission failure => reset all links to peer */ |
| 1540 | if (rc & TIPC_LINK_DOWN_EVT) |
| 1541 | tipc_node_reset_links(n); |
Jon Paul Maloy | 1fc07f3e | 2016-07-11 16:08:37 -0400 | [diff] [blame] | 1542 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1543 | tipc_node_put(n); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1547 | * tipc_node_check_state - check and if necessary update node state |
| 1548 | * @skb: TIPC packet |
| 1549 | * @bearer_id: identity of bearer delivering the packet |
Jon Maloy | 7ea817f | 2018-07-10 01:07:36 +0200 | [diff] [blame] | 1550 | * Returns true if state and msg are ok, otherwise false |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1551 | */ |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1552 | static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb, |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 1553 | int bearer_id, struct sk_buff_head *xmitq) |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1554 | { |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1555 | struct tipc_msg *hdr = buf_msg(skb); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1556 | int usr = msg_user(hdr); |
| 1557 | int mtyp = msg_type(hdr); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1558 | u16 oseqno = msg_seqno(hdr); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1559 | u16 iseqno = msg_seqno(msg_get_wrapped(hdr)); |
| 1560 | u16 exp_pkts = msg_msgcnt(hdr); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1561 | u16 rcv_nxt, syncpt, dlv_nxt, inputq_len; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1562 | int state = n->state; |
Jon Paul Maloy | 2be80c2 | 2015-08-20 02:12:56 -0400 | [diff] [blame] | 1563 | struct tipc_link *l, *tnl, *pl = NULL; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1564 | struct tipc_media_addr *maddr; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1565 | int pb_id; |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1566 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1567 | l = n->links[bearer_id].link; |
| 1568 | if (!l) |
| 1569 | return false; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1570 | rcv_nxt = tipc_link_rcv_nxt(l); |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1571 | |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1572 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1573 | if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) |
| 1574 | return true; |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1575 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1576 | /* Find parallel link, if any */ |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1577 | for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) { |
| 1578 | if ((pb_id != bearer_id) && n->links[pb_id].link) { |
| 1579 | pl = n->links[pb_id].link; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1580 | break; |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1581 | } |
| 1582 | } |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1583 | |
Jon Maloy | 7ea817f | 2018-07-10 01:07:36 +0200 | [diff] [blame] | 1584 | if (!tipc_link_validate_msg(l, hdr)) |
| 1585 | return false; |
| 1586 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1587 | /* Check and update node accesibility if applicable */ |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1588 | if (state == SELF_UP_PEER_COMING) { |
| 1589 | if (!tipc_link_is_up(l)) |
| 1590 | return true; |
| 1591 | if (!msg_peer_link_is_up(hdr)) |
| 1592 | return true; |
| 1593 | tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT); |
| 1594 | } |
| 1595 | |
| 1596 | if (state == SELF_DOWN_PEER_LEAVING) { |
| 1597 | if (msg_peer_node_is_up(hdr)) |
| 1598 | return false; |
| 1599 | tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT); |
Jon Paul Maloy | 5c10e97 | 2015-11-19 14:30:41 -0500 | [diff] [blame] | 1600 | return true; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1601 | } |
| 1602 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1603 | if (state == SELF_LEAVING_PEER_DOWN) |
| 1604 | return false; |
| 1605 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1606 | /* Ignore duplicate packets */ |
Jon Paul Maloy | 0f8b8e2 | 2015-10-13 12:41:51 -0400 | [diff] [blame] | 1607 | if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt)) |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1608 | return true; |
| 1609 | |
| 1610 | /* Initiate or update failover mode if applicable */ |
| 1611 | if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) { |
| 1612 | syncpt = oseqno + exp_pkts - 1; |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1613 | if (pl && tipc_link_is_up(pl)) { |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1614 | __tipc_node_link_down(n, &pb_id, xmitq, &maddr); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1615 | tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl), |
| 1616 | tipc_link_inputq(l)); |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1617 | } |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1618 | /* If pkts arrive out of order, use lowest calculated syncpt */ |
| 1619 | if (less(syncpt, n->sync_point)) |
| 1620 | n->sync_point = syncpt; |
| 1621 | } |
| 1622 | |
| 1623 | /* Open parallel link when tunnel link reaches synch point */ |
Jon Paul Maloy | 17b2063 | 2015-08-20 02:12:54 -0400 | [diff] [blame] | 1624 | if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) { |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 1625 | if (!more(rcv_nxt, n->sync_point)) |
| 1626 | return true; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1627 | tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT); |
| 1628 | if (pl) |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 1629 | tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | |
Jon Paul Maloy | 5ae2f8e | 2015-08-20 02:12:55 -0400 | [diff] [blame] | 1633 | /* No synching needed if only one link */ |
| 1634 | if (!pl || !tipc_link_is_up(pl)) |
| 1635 | return true; |
| 1636 | |
Jon Paul Maloy | 0f8b8e2 | 2015-10-13 12:41:51 -0400 | [diff] [blame] | 1637 | /* Initiate synch mode if applicable */ |
| 1638 | if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) { |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1639 | syncpt = iseqno + exp_pkts - 1; |
Jon Paul Maloy | ed43594 | 2017-08-08 22:23:56 +0200 | [diff] [blame] | 1640 | if (!tipc_link_is_up(l)) |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1641 | __tipc_node_link_up(n, bearer_id, xmitq); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1642 | if (n->state == SELF_UP_PEER_UP) { |
| 1643 | n->sync_point = syncpt; |
Jon Paul Maloy | 662921c | 2015-07-30 18:24:21 -0400 | [diff] [blame] | 1644 | tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1645 | tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT); |
| 1646 | } |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* Open tunnel link when parallel link reaches synch point */ |
Jon Paul Maloy | 5c10e97 | 2015-11-19 14:30:41 -0500 | [diff] [blame] | 1650 | if (n->state == NODE_SYNCHING) { |
Jon Paul Maloy | 2be80c2 | 2015-08-20 02:12:56 -0400 | [diff] [blame] | 1651 | if (tipc_link_is_synching(l)) { |
| 1652 | tnl = l; |
| 1653 | } else { |
| 1654 | tnl = pl; |
| 1655 | pl = l; |
| 1656 | } |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1657 | inputq_len = skb_queue_len(tipc_link_inputq(pl)); |
| 1658 | dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len; |
Jon Paul Maloy | 5ae2f8e | 2015-08-20 02:12:55 -0400 | [diff] [blame] | 1659 | if (more(dlv_nxt, n->sync_point)) { |
Jon Paul Maloy | 2be80c2 | 2015-08-20 02:12:56 -0400 | [diff] [blame] | 1660 | tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1661 | tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1662 | return true; |
| 1663 | } |
Jon Paul Maloy | 2be80c2 | 2015-08-20 02:12:56 -0400 | [diff] [blame] | 1664 | if (l == pl) |
| 1665 | return true; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1666 | if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG)) |
| 1667 | return true; |
| 1668 | if (usr == LINK_PROTOCOL) |
| 1669 | return true; |
| 1670 | return false; |
| 1671 | } |
| 1672 | return true; |
Jon Paul Maloy | 6144a99 | 2015-07-30 18:24:16 -0400 | [diff] [blame] | 1673 | } |
| 1674 | |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1675 | /** |
| 1676 | * tipc_rcv - process TIPC packets/messages arriving from off-node |
| 1677 | * @net: the applicable net namespace |
| 1678 | * @skb: TIPC packet |
| 1679 | * @bearer: pointer to bearer message arrived on |
| 1680 | * |
| 1681 | * Invoked with no locks held. Bearer pointer must point to a valid bearer |
| 1682 | * structure (i.e. cannot be NULL), but bearer can be inactive. |
| 1683 | */ |
| 1684 | void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b) |
| 1685 | { |
| 1686 | struct sk_buff_head xmitq; |
| 1687 | struct tipc_node *n; |
Jon Paul Maloy | 681a55d | 2017-02-23 11:10:31 -0500 | [diff] [blame] | 1688 | struct tipc_msg *hdr; |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1689 | int bearer_id = b->identity; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1690 | struct tipc_link_entry *le; |
Hamish Martin | efe7905 | 2016-04-29 10:40:24 -0400 | [diff] [blame] | 1691 | u32 self = tipc_own_addr(net); |
Jon Paul Maloy | 681a55d | 2017-02-23 11:10:31 -0500 | [diff] [blame] | 1692 | int usr, rc = 0; |
| 1693 | u16 bc_ack; |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1694 | |
| 1695 | __skb_queue_head_init(&xmitq); |
| 1696 | |
Jon Paul Maloy | 681a55d | 2017-02-23 11:10:31 -0500 | [diff] [blame] | 1697 | /* Ensure message is well-formed before touching the header */ |
Jon Maloy | d618d09 | 2017-11-15 21:23:56 +0100 | [diff] [blame] | 1698 | if (unlikely(!tipc_msg_validate(&skb))) |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1699 | goto discard; |
Jon Paul Maloy | 681a55d | 2017-02-23 11:10:31 -0500 | [diff] [blame] | 1700 | hdr = buf_msg(skb); |
| 1701 | usr = msg_user(hdr); |
| 1702 | bc_ack = msg_bcast_ack(hdr); |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1703 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1704 | /* Handle arrival of discovery or broadcast packet */ |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1705 | if (unlikely(msg_non_seq(hdr))) { |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1706 | if (unlikely(usr == LINK_CONFIG)) |
| 1707 | return tipc_disc_rcv(net, skb, b); |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1708 | else |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1709 | return tipc_node_bc_rcv(net, skb, bearer_id); |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1710 | } |
| 1711 | |
Hamish Martin | efe7905 | 2016-04-29 10:40:24 -0400 | [diff] [blame] | 1712 | /* Discard unicast link messages destined for another node */ |
| 1713 | if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self))) |
| 1714 | goto discard; |
| 1715 | |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1716 | /* Locate neighboring node that sent packet */ |
| 1717 | n = tipc_node_find(net, msg_prevnode(hdr)); |
| 1718 | if (unlikely(!n)) |
| 1719 | goto discard; |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1720 | le = &n->links[bearer_id]; |
| 1721 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1722 | /* Ensure broadcast reception is in synch with peer's send state */ |
| 1723 | if (unlikely(usr == LINK_PROTOCOL)) |
Jon Paul Maloy | 02d11ca | 2016-09-01 13:52:49 -0400 | [diff] [blame] | 1724 | tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1725 | else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack)) |
Jon Paul Maloy | 06bd2b1 | 2016-10-27 18:51:55 -0400 | [diff] [blame] | 1726 | tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr); |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1727 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1728 | /* Receive packet directly if conditions permit */ |
| 1729 | tipc_node_read_lock(n); |
| 1730 | if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) { |
Jon Paul Maloy | 2312bf6 | 2015-11-19 14:30:43 -0500 | [diff] [blame] | 1731 | spin_lock_bh(&le->lock); |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1732 | if (le->link) { |
| 1733 | rc = tipc_link_rcv(le->link, skb, &xmitq); |
| 1734 | skb = NULL; |
| 1735 | } |
Jon Paul Maloy | 2312bf6 | 2015-11-19 14:30:43 -0500 | [diff] [blame] | 1736 | spin_unlock_bh(&le->lock); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1737 | } |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1738 | tipc_node_read_unlock(n); |
| 1739 | |
| 1740 | /* Check/update node state before receiving */ |
| 1741 | if (unlikely(skb)) { |
Parthasarathy Bhuvaragan | 2716313 | 2017-08-24 16:31:22 +0200 | [diff] [blame] | 1742 | if (unlikely(skb_linearize(skb))) |
| 1743 | goto discard; |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1744 | tipc_node_write_lock(n); |
| 1745 | if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) { |
| 1746 | if (le->link) { |
| 1747 | rc = tipc_link_rcv(le->link, skb, &xmitq); |
| 1748 | skb = NULL; |
| 1749 | } |
| 1750 | } |
| 1751 | tipc_node_write_unlock(n); |
| 1752 | } |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1753 | |
| 1754 | if (unlikely(rc & TIPC_LINK_UP_EVT)) |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1755 | tipc_node_link_up(n, bearer_id, &xmitq); |
| 1756 | |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1757 | if (unlikely(rc & TIPC_LINK_DOWN_EVT)) |
Jon Paul Maloy | 598411d | 2015-07-30 18:24:23 -0400 | [diff] [blame] | 1758 | tipc_node_link_down(n, bearer_id, false); |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1759 | |
Jon Paul Maloy | 5266698 | 2015-10-22 08:51:41 -0400 | [diff] [blame] | 1760 | if (unlikely(!skb_queue_empty(&n->bc_entry.namedq))) |
| 1761 | tipc_named_rcv(net, &n->bc_entry.namedq); |
Jon Paul Maloy | 23d8335 | 2015-07-30 18:24:24 -0400 | [diff] [blame] | 1762 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 1763 | if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1))) |
| 1764 | tipc_node_mcast_rcv(n); |
| 1765 | |
Jon Paul Maloy | 6e49815 | 2015-07-30 18:24:19 -0400 | [diff] [blame] | 1766 | if (!skb_queue_empty(&le->inputq)) |
| 1767 | tipc_sk_rcv(net, &le->inputq); |
| 1768 | |
| 1769 | if (!skb_queue_empty(&xmitq)) |
| 1770 | tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr); |
| 1771 | |
Jon Paul Maloy | d999297 | 2015-07-16 16:54:31 -0400 | [diff] [blame] | 1772 | tipc_node_put(n); |
| 1773 | discard: |
| 1774 | kfree_skb(skb); |
| 1775 | } |
| 1776 | |
GhantaKrishnamurthy MohanKrishna | 682cd3c | 2018-04-19 11:06:20 +0200 | [diff] [blame] | 1777 | void tipc_node_apply_property(struct net *net, struct tipc_bearer *b, |
| 1778 | int prop) |
Jon Maloy | 37c64cf | 2018-02-14 13:34:39 +0100 | [diff] [blame] | 1779 | { |
| 1780 | struct tipc_net *tn = tipc_net(net); |
| 1781 | int bearer_id = b->identity; |
| 1782 | struct sk_buff_head xmitq; |
| 1783 | struct tipc_link_entry *e; |
| 1784 | struct tipc_node *n; |
| 1785 | |
| 1786 | __skb_queue_head_init(&xmitq); |
| 1787 | |
| 1788 | rcu_read_lock(); |
| 1789 | |
| 1790 | list_for_each_entry_rcu(n, &tn->node_list, list) { |
| 1791 | tipc_node_write_lock(n); |
| 1792 | e = &n->links[bearer_id]; |
GhantaKrishnamurthy MohanKrishna | 682cd3c | 2018-04-19 11:06:20 +0200 | [diff] [blame] | 1793 | if (e->link) { |
| 1794 | if (prop == TIPC_NLA_PROP_TOL) |
| 1795 | tipc_link_set_tolerance(e->link, b->tolerance, |
| 1796 | &xmitq); |
| 1797 | else if (prop == TIPC_NLA_PROP_MTU) |
| 1798 | tipc_link_set_mtu(e->link, b->mtu); |
| 1799 | } |
Jon Maloy | 37c64cf | 2018-02-14 13:34:39 +0100 | [diff] [blame] | 1800 | tipc_node_write_unlock(n); |
| 1801 | tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr); |
| 1802 | } |
| 1803 | |
| 1804 | rcu_read_unlock(); |
| 1805 | } |
| 1806 | |
Richard Alpe | b340402 | 2016-08-18 10:33:52 +0200 | [diff] [blame] | 1807 | int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info) |
| 1808 | { |
| 1809 | struct net *net = sock_net(skb->sk); |
| 1810 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 1811 | struct nlattr *attrs[TIPC_NLA_NET_MAX + 1]; |
| 1812 | struct tipc_node *peer; |
| 1813 | u32 addr; |
| 1814 | int err; |
Richard Alpe | b340402 | 2016-08-18 10:33:52 +0200 | [diff] [blame] | 1815 | |
| 1816 | /* We identify the peer by its net */ |
| 1817 | if (!info->attrs[TIPC_NLA_NET]) |
| 1818 | return -EINVAL; |
| 1819 | |
| 1820 | err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1821 | info->attrs[TIPC_NLA_NET], tipc_nl_net_policy, |
Johannes Berg | fe52145 | 2017-04-12 14:34:08 +0200 | [diff] [blame] | 1822 | info->extack); |
Richard Alpe | b340402 | 2016-08-18 10:33:52 +0200 | [diff] [blame] | 1823 | if (err) |
| 1824 | return err; |
| 1825 | |
| 1826 | if (!attrs[TIPC_NLA_NET_ADDR]) |
| 1827 | return -EINVAL; |
| 1828 | |
| 1829 | addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]); |
| 1830 | |
| 1831 | if (in_own_node(net, addr)) |
| 1832 | return -ENOTSUPP; |
| 1833 | |
| 1834 | spin_lock_bh(&tn->node_list_lock); |
| 1835 | peer = tipc_node_find(net, addr); |
| 1836 | if (!peer) { |
| 1837 | spin_unlock_bh(&tn->node_list_lock); |
| 1838 | return -ENXIO; |
| 1839 | } |
| 1840 | |
| 1841 | tipc_node_write_lock(peer); |
| 1842 | if (peer->state != SELF_DOWN_PEER_DOWN && |
| 1843 | peer->state != SELF_DOWN_PEER_LEAVING) { |
| 1844 | tipc_node_write_unlock(peer); |
| 1845 | err = -EBUSY; |
| 1846 | goto err_out; |
| 1847 | } |
| 1848 | |
GhantaKrishnamurthy MohanKrishna | 6a939f3 | 2018-06-29 13:23:41 +0200 | [diff] [blame] | 1849 | tipc_node_clear_links(peer); |
Richard Alpe | b340402 | 2016-08-18 10:33:52 +0200 | [diff] [blame] | 1850 | tipc_node_write_unlock(peer); |
| 1851 | tipc_node_delete(peer); |
| 1852 | |
| 1853 | err = 0; |
| 1854 | err_out: |
| 1855 | tipc_node_put(peer); |
| 1856 | spin_unlock_bh(&tn->node_list_lock); |
| 1857 | |
| 1858 | return err; |
| 1859 | } |
| 1860 | |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1861 | int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1862 | { |
| 1863 | int err; |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1864 | struct net *net = sock_net(skb->sk); |
| 1865 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1866 | int done = cb->args[0]; |
| 1867 | int last_addr = cb->args[1]; |
| 1868 | struct tipc_node *node; |
| 1869 | struct tipc_nl_msg msg; |
| 1870 | |
| 1871 | if (done) |
| 1872 | return 0; |
| 1873 | |
| 1874 | msg.skb = skb; |
| 1875 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 1876 | msg.seq = cb->nlh->nlmsg_seq; |
| 1877 | |
| 1878 | rcu_read_lock(); |
Ying Xue | 8a0f6eb | 2015-03-26 18:10:24 +0800 | [diff] [blame] | 1879 | if (last_addr) { |
| 1880 | node = tipc_node_find(net, last_addr); |
| 1881 | if (!node) { |
| 1882 | rcu_read_unlock(); |
| 1883 | /* We never set seq or call nl_dump_check_consistent() |
| 1884 | * this means that setting prev_seq here will cause the |
| 1885 | * consistence check to fail in the netlink callback |
| 1886 | * handler. Resulting in the NLMSG_DONE message having |
| 1887 | * the NLM_F_DUMP_INTR flag set if the node state |
| 1888 | * changed while we released the lock. |
| 1889 | */ |
| 1890 | cb->prev_seq = 1; |
| 1891 | return -EPIPE; |
| 1892 | } |
| 1893 | tipc_node_put(node); |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1896 | list_for_each_entry_rcu(node, &tn->node_list, list) { |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1897 | if (last_addr) { |
| 1898 | if (node->addr == last_addr) |
| 1899 | last_addr = 0; |
| 1900 | else |
| 1901 | continue; |
| 1902 | } |
| 1903 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1904 | tipc_node_read_lock(node); |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1905 | err = __tipc_nl_add_node(&msg, node); |
| 1906 | if (err) { |
| 1907 | last_addr = node->addr; |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1908 | tipc_node_read_unlock(node); |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1909 | goto out; |
| 1910 | } |
| 1911 | |
Jon Paul Maloy | 5405ff6 | 2015-11-19 14:30:44 -0500 | [diff] [blame] | 1912 | tipc_node_read_unlock(node); |
Richard Alpe | 3e4b6ab | 2014-11-20 10:29:17 +0100 | [diff] [blame] | 1913 | } |
| 1914 | done = 1; |
| 1915 | out: |
| 1916 | cb->args[0] = done; |
| 1917 | cb->args[1] = last_addr; |
| 1918 | rcu_read_unlock(); |
| 1919 | |
| 1920 | return skb->len; |
| 1921 | } |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1922 | |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1923 | /* tipc_node_find_by_name - locate owner node of link by link's name |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1924 | * @net: the applicable net namespace |
| 1925 | * @name: pointer to link name string |
| 1926 | * @bearer_id: pointer to index in 'node->links' array where the link was found. |
| 1927 | * |
| 1928 | * Returns pointer to node owning the link, or 0 if no matching link is found. |
| 1929 | */ |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1930 | static struct tipc_node *tipc_node_find_by_name(struct net *net, |
| 1931 | const char *link_name, |
| 1932 | unsigned int *bearer_id) |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1933 | { |
| 1934 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1935 | struct tipc_link *l; |
| 1936 | struct tipc_node *n; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1937 | struct tipc_node *found_node = NULL; |
| 1938 | int i; |
| 1939 | |
| 1940 | *bearer_id = 0; |
| 1941 | rcu_read_lock(); |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1942 | list_for_each_entry_rcu(n, &tn->node_list, list) { |
| 1943 | tipc_node_read_lock(n); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1944 | for (i = 0; i < MAX_BEARERS; i++) { |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1945 | l = n->links[i].link; |
| 1946 | if (l && !strcmp(tipc_link_name(l), link_name)) { |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1947 | *bearer_id = i; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1948 | found_node = n; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1949 | break; |
| 1950 | } |
| 1951 | } |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1952 | tipc_node_read_unlock(n); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1953 | if (found_node) |
| 1954 | break; |
| 1955 | } |
| 1956 | rcu_read_unlock(); |
| 1957 | |
| 1958 | return found_node; |
| 1959 | } |
| 1960 | |
| 1961 | int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info) |
| 1962 | { |
| 1963 | int err; |
| 1964 | int res = 0; |
| 1965 | int bearer_id; |
| 1966 | char *name; |
| 1967 | struct tipc_link *link; |
| 1968 | struct tipc_node *node; |
Richard Alpe | d01332f | 2016-02-01 08:19:56 +0100 | [diff] [blame] | 1969 | struct sk_buff_head xmitq; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1970 | struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1]; |
| 1971 | struct net *net = sock_net(skb->sk); |
| 1972 | |
Richard Alpe | d01332f | 2016-02-01 08:19:56 +0100 | [diff] [blame] | 1973 | __skb_queue_head_init(&xmitq); |
| 1974 | |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1975 | if (!info->attrs[TIPC_NLA_LINK]) |
| 1976 | return -EINVAL; |
| 1977 | |
| 1978 | err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX, |
| 1979 | info->attrs[TIPC_NLA_LINK], |
Johannes Berg | fe52145 | 2017-04-12 14:34:08 +0200 | [diff] [blame] | 1980 | tipc_nl_link_policy, info->extack); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1981 | if (err) |
| 1982 | return err; |
| 1983 | |
| 1984 | if (!attrs[TIPC_NLA_LINK_NAME]) |
| 1985 | return -EINVAL; |
| 1986 | |
| 1987 | name = nla_data(attrs[TIPC_NLA_LINK_NAME]); |
| 1988 | |
| 1989 | if (strcmp(name, tipc_bclink_name) == 0) |
| 1990 | return tipc_nl_bc_link_set(net, attrs); |
| 1991 | |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1992 | node = tipc_node_find_by_name(net, name, &bearer_id); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1993 | if (!node) |
| 1994 | return -EINVAL; |
| 1995 | |
| 1996 | tipc_node_read_lock(node); |
| 1997 | |
| 1998 | link = node->links[bearer_id].link; |
| 1999 | if (!link) { |
| 2000 | res = -EINVAL; |
| 2001 | goto out; |
| 2002 | } |
| 2003 | |
| 2004 | if (attrs[TIPC_NLA_LINK_PROP]) { |
| 2005 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; |
| 2006 | |
| 2007 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], |
| 2008 | props); |
| 2009 | if (err) { |
| 2010 | res = err; |
| 2011 | goto out; |
| 2012 | } |
| 2013 | |
| 2014 | if (props[TIPC_NLA_PROP_TOL]) { |
| 2015 | u32 tol; |
| 2016 | |
| 2017 | tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]); |
Richard Alpe | d01332f | 2016-02-01 08:19:56 +0100 | [diff] [blame] | 2018 | tipc_link_set_tolerance(link, tol, &xmitq); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2019 | } |
| 2020 | if (props[TIPC_NLA_PROP_PRIO]) { |
| 2021 | u32 prio; |
| 2022 | |
| 2023 | prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); |
Richard Alpe | d01332f | 2016-02-01 08:19:56 +0100 | [diff] [blame] | 2024 | tipc_link_set_prio(link, prio, &xmitq); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2025 | } |
| 2026 | if (props[TIPC_NLA_PROP_WIN]) { |
| 2027 | u32 win; |
| 2028 | |
| 2029 | win = nla_get_u32(props[TIPC_NLA_PROP_WIN]); |
| 2030 | tipc_link_set_queue_limits(link, win); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | out: |
| 2035 | tipc_node_read_unlock(node); |
Richard Alpe | d01332f | 2016-02-01 08:19:56 +0100 | [diff] [blame] | 2036 | tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2037 | return res; |
| 2038 | } |
| 2039 | |
| 2040 | int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info) |
| 2041 | { |
| 2042 | struct net *net = genl_info_net(info); |
Ying Xue | 94f6a80 | 2018-05-08 21:44:06 +0800 | [diff] [blame] | 2043 | struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1]; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2044 | struct tipc_nl_msg msg; |
| 2045 | char *name; |
| 2046 | int err; |
| 2047 | |
| 2048 | msg.portid = info->snd_portid; |
| 2049 | msg.seq = info->snd_seq; |
| 2050 | |
Ying Xue | 94f6a80 | 2018-05-08 21:44:06 +0800 | [diff] [blame] | 2051 | if (!info->attrs[TIPC_NLA_LINK]) |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2052 | return -EINVAL; |
Ying Xue | 94f6a80 | 2018-05-08 21:44:06 +0800 | [diff] [blame] | 2053 | |
| 2054 | err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX, |
| 2055 | info->attrs[TIPC_NLA_LINK], |
| 2056 | tipc_nl_link_policy, info->extack); |
| 2057 | if (err) |
| 2058 | return err; |
| 2059 | |
| 2060 | if (!attrs[TIPC_NLA_LINK_NAME]) |
| 2061 | return -EINVAL; |
| 2062 | |
| 2063 | name = nla_data(attrs[TIPC_NLA_LINK_NAME]); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2064 | |
| 2065 | msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 2066 | if (!msg.skb) |
| 2067 | return -ENOMEM; |
| 2068 | |
| 2069 | if (strcmp(name, tipc_bclink_name) == 0) { |
| 2070 | err = tipc_nl_add_bc_link(net, &msg); |
Cong Wang | 59b3661 | 2018-01-10 12:50:25 -0800 | [diff] [blame] | 2071 | if (err) |
| 2072 | goto err_free; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2073 | } else { |
| 2074 | int bearer_id; |
| 2075 | struct tipc_node *node; |
| 2076 | struct tipc_link *link; |
| 2077 | |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 2078 | node = tipc_node_find_by_name(net, name, &bearer_id); |
Cong Wang | 59b3661 | 2018-01-10 12:50:25 -0800 | [diff] [blame] | 2079 | if (!node) { |
| 2080 | err = -EINVAL; |
| 2081 | goto err_free; |
| 2082 | } |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2083 | |
| 2084 | tipc_node_read_lock(node); |
| 2085 | link = node->links[bearer_id].link; |
| 2086 | if (!link) { |
| 2087 | tipc_node_read_unlock(node); |
Cong Wang | 59b3661 | 2018-01-10 12:50:25 -0800 | [diff] [blame] | 2088 | err = -EINVAL; |
| 2089 | goto err_free; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | err = __tipc_nl_add_link(net, &msg, link, 0); |
| 2093 | tipc_node_read_unlock(node); |
Cong Wang | 59b3661 | 2018-01-10 12:50:25 -0800 | [diff] [blame] | 2094 | if (err) |
| 2095 | goto err_free; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | return genlmsg_reply(msg.skb, info); |
Cong Wang | 59b3661 | 2018-01-10 12:50:25 -0800 | [diff] [blame] | 2099 | |
| 2100 | err_free: |
| 2101 | nlmsg_free(msg.skb); |
| 2102 | return err; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info) |
| 2106 | { |
| 2107 | int err; |
| 2108 | char *link_name; |
| 2109 | unsigned int bearer_id; |
| 2110 | struct tipc_link *link; |
| 2111 | struct tipc_node *node; |
| 2112 | struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1]; |
| 2113 | struct net *net = sock_net(skb->sk); |
| 2114 | struct tipc_link_entry *le; |
| 2115 | |
| 2116 | if (!info->attrs[TIPC_NLA_LINK]) |
| 2117 | return -EINVAL; |
| 2118 | |
| 2119 | err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX, |
| 2120 | info->attrs[TIPC_NLA_LINK], |
Johannes Berg | fe52145 | 2017-04-12 14:34:08 +0200 | [diff] [blame] | 2121 | tipc_nl_link_policy, info->extack); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2122 | if (err) |
| 2123 | return err; |
| 2124 | |
| 2125 | if (!attrs[TIPC_NLA_LINK_NAME]) |
| 2126 | return -EINVAL; |
| 2127 | |
| 2128 | link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]); |
| 2129 | |
| 2130 | if (strcmp(link_name, tipc_bclink_name) == 0) { |
| 2131 | err = tipc_bclink_reset_stats(net); |
| 2132 | if (err) |
| 2133 | return err; |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 2137 | node = tipc_node_find_by_name(net, link_name, &bearer_id); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2138 | if (!node) |
| 2139 | return -EINVAL; |
| 2140 | |
| 2141 | le = &node->links[bearer_id]; |
| 2142 | tipc_node_read_lock(node); |
| 2143 | spin_lock_bh(&le->lock); |
| 2144 | link = node->links[bearer_id].link; |
| 2145 | if (!link) { |
| 2146 | spin_unlock_bh(&le->lock); |
| 2147 | tipc_node_read_unlock(node); |
| 2148 | return -EINVAL; |
| 2149 | } |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 2150 | tipc_link_reset_stats(link); |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2151 | spin_unlock_bh(&le->lock); |
| 2152 | tipc_node_read_unlock(node); |
| 2153 | return 0; |
| 2154 | } |
| 2155 | |
| 2156 | /* Caller should hold node lock */ |
| 2157 | static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg, |
| 2158 | struct tipc_node *node, u32 *prev_link) |
| 2159 | { |
| 2160 | u32 i; |
| 2161 | int err; |
| 2162 | |
| 2163 | for (i = *prev_link; i < MAX_BEARERS; i++) { |
| 2164 | *prev_link = i; |
| 2165 | |
| 2166 | if (!node->links[i].link) |
| 2167 | continue; |
| 2168 | |
| 2169 | err = __tipc_nl_add_link(net, msg, |
| 2170 | node->links[i].link, NLM_F_MULTI); |
| 2171 | if (err) |
| 2172 | return err; |
| 2173 | } |
| 2174 | *prev_link = 0; |
| 2175 | |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 2179 | int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb) |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 2180 | { |
| 2181 | struct net *net = sock_net(skb->sk); |
| 2182 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 2183 | struct tipc_node *node; |
| 2184 | struct tipc_nl_msg msg; |
| 2185 | u32 prev_node = cb->args[0]; |
| 2186 | u32 prev_link = cb->args[1]; |
| 2187 | int done = cb->args[2]; |
| 2188 | int err; |
| 2189 | |
| 2190 | if (done) |
| 2191 | return 0; |
| 2192 | |
| 2193 | msg.skb = skb; |
| 2194 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 2195 | msg.seq = cb->nlh->nlmsg_seq; |
| 2196 | |
| 2197 | rcu_read_lock(); |
| 2198 | if (prev_node) { |
| 2199 | node = tipc_node_find(net, prev_node); |
| 2200 | if (!node) { |
| 2201 | /* We never set seq or call nl_dump_check_consistent() |
| 2202 | * this means that setting prev_seq here will cause the |
| 2203 | * consistence check to fail in the netlink callback |
| 2204 | * handler. Resulting in the last NLMSG_DONE message |
| 2205 | * having the NLM_F_DUMP_INTR flag set. |
| 2206 | */ |
| 2207 | cb->prev_seq = 1; |
| 2208 | goto out; |
| 2209 | } |
| 2210 | tipc_node_put(node); |
| 2211 | |
| 2212 | list_for_each_entry_continue_rcu(node, &tn->node_list, |
| 2213 | list) { |
| 2214 | tipc_node_read_lock(node); |
| 2215 | err = __tipc_nl_add_node_links(net, &msg, node, |
| 2216 | &prev_link); |
| 2217 | tipc_node_read_unlock(node); |
| 2218 | if (err) |
| 2219 | goto out; |
| 2220 | |
| 2221 | prev_node = node->addr; |
| 2222 | } |
| 2223 | } else { |
| 2224 | err = tipc_nl_add_bc_link(net, &msg); |
| 2225 | if (err) |
| 2226 | goto out; |
| 2227 | |
| 2228 | list_for_each_entry_rcu(node, &tn->node_list, list) { |
| 2229 | tipc_node_read_lock(node); |
| 2230 | err = __tipc_nl_add_node_links(net, &msg, node, |
| 2231 | &prev_link); |
| 2232 | tipc_node_read_unlock(node); |
| 2233 | if (err) |
| 2234 | goto out; |
| 2235 | |
| 2236 | prev_node = node->addr; |
| 2237 | } |
| 2238 | } |
| 2239 | done = 1; |
| 2240 | out: |
| 2241 | rcu_read_unlock(); |
| 2242 | |
| 2243 | cb->args[0] = prev_node; |
| 2244 | cb->args[1] = prev_link; |
| 2245 | cb->args[2] = done; |
| 2246 | |
| 2247 | return skb->len; |
| 2248 | } |
Parthasarathy Bhuvaragan | 7b3f522 | 2016-07-26 08:47:19 +0200 | [diff] [blame] | 2249 | |
| 2250 | int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info) |
| 2251 | { |
| 2252 | struct nlattr *attrs[TIPC_NLA_MON_MAX + 1]; |
| 2253 | struct net *net = sock_net(skb->sk); |
| 2254 | int err; |
| 2255 | |
| 2256 | if (!info->attrs[TIPC_NLA_MON]) |
| 2257 | return -EINVAL; |
| 2258 | |
| 2259 | err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX, |
| 2260 | info->attrs[TIPC_NLA_MON], |
Johannes Berg | fe52145 | 2017-04-12 14:34:08 +0200 | [diff] [blame] | 2261 | tipc_nl_monitor_policy, info->extack); |
Parthasarathy Bhuvaragan | 7b3f522 | 2016-07-26 08:47:19 +0200 | [diff] [blame] | 2262 | if (err) |
| 2263 | return err; |
| 2264 | |
| 2265 | if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) { |
| 2266 | u32 val; |
| 2267 | |
| 2268 | val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]); |
| 2269 | err = tipc_nl_monitor_set_threshold(net, val); |
| 2270 | if (err) |
| 2271 | return err; |
| 2272 | } |
| 2273 | |
| 2274 | return 0; |
| 2275 | } |
Parthasarathy Bhuvaragan | bf1035b | 2016-07-26 08:47:20 +0200 | [diff] [blame] | 2276 | |
| 2277 | static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg) |
| 2278 | { |
| 2279 | struct nlattr *attrs; |
| 2280 | void *hdr; |
| 2281 | u32 val; |
| 2282 | |
| 2283 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
| 2284 | 0, TIPC_NL_MON_GET); |
| 2285 | if (!hdr) |
| 2286 | return -EMSGSIZE; |
| 2287 | |
| 2288 | attrs = nla_nest_start(msg->skb, TIPC_NLA_MON); |
| 2289 | if (!attrs) |
| 2290 | goto msg_full; |
| 2291 | |
| 2292 | val = tipc_nl_monitor_get_threshold(net); |
| 2293 | |
| 2294 | if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val)) |
| 2295 | goto attr_msg_full; |
| 2296 | |
| 2297 | nla_nest_end(msg->skb, attrs); |
| 2298 | genlmsg_end(msg->skb, hdr); |
| 2299 | |
| 2300 | return 0; |
| 2301 | |
| 2302 | attr_msg_full: |
| 2303 | nla_nest_cancel(msg->skb, attrs); |
| 2304 | msg_full: |
| 2305 | genlmsg_cancel(msg->skb, hdr); |
| 2306 | |
| 2307 | return -EMSGSIZE; |
| 2308 | } |
| 2309 | |
| 2310 | int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info) |
| 2311 | { |
| 2312 | struct net *net = sock_net(skb->sk); |
| 2313 | struct tipc_nl_msg msg; |
| 2314 | int err; |
| 2315 | |
| 2316 | msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
Pan Bian | 78302fd | 2017-04-23 15:09:19 +0800 | [diff] [blame] | 2317 | if (!msg.skb) |
| 2318 | return -ENOMEM; |
Parthasarathy Bhuvaragan | bf1035b | 2016-07-26 08:47:20 +0200 | [diff] [blame] | 2319 | msg.portid = info->snd_portid; |
| 2320 | msg.seq = info->snd_seq; |
| 2321 | |
| 2322 | err = __tipc_nl_add_monitor_prop(net, &msg); |
| 2323 | if (err) { |
| 2324 | nlmsg_free(msg.skb); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
| 2328 | return genlmsg_reply(msg.skb, info); |
| 2329 | } |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2330 | |
| 2331 | int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb) |
| 2332 | { |
| 2333 | struct net *net = sock_net(skb->sk); |
| 2334 | u32 prev_bearer = cb->args[0]; |
| 2335 | struct tipc_nl_msg msg; |
Tung Nguyen | 36a50a9 | 2018-04-17 21:58:27 +0200 | [diff] [blame] | 2336 | int bearer_id; |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2337 | int err; |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2338 | |
| 2339 | if (prev_bearer == MAX_BEARERS) |
| 2340 | return 0; |
| 2341 | |
| 2342 | msg.skb = skb; |
| 2343 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 2344 | msg.seq = cb->nlh->nlmsg_seq; |
| 2345 | |
| 2346 | rtnl_lock(); |
Tung Nguyen | 36a50a9 | 2018-04-17 21:58:27 +0200 | [diff] [blame] | 2347 | for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) { |
Jon Maloy | 7dbc73e6 | 2018-04-25 18:29:25 +0200 | [diff] [blame] | 2348 | err = __tipc_nl_add_monitor(net, &msg, bearer_id); |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2349 | if (err) |
Tung Nguyen | 36a50a9 | 2018-04-17 21:58:27 +0200 | [diff] [blame] | 2350 | break; |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2351 | } |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2352 | rtnl_unlock(); |
Tung Nguyen | 36a50a9 | 2018-04-17 21:58:27 +0200 | [diff] [blame] | 2353 | cb->args[0] = bearer_id; |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2354 | |
| 2355 | return skb->len; |
| 2356 | } |
| 2357 | |
| 2358 | int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb, |
| 2359 | struct netlink_callback *cb) |
| 2360 | { |
| 2361 | struct net *net = sock_net(skb->sk); |
| 2362 | u32 prev_node = cb->args[1]; |
| 2363 | u32 bearer_id = cb->args[2]; |
| 2364 | int done = cb->args[0]; |
| 2365 | struct tipc_nl_msg msg; |
| 2366 | int err; |
| 2367 | |
| 2368 | if (!prev_node) { |
| 2369 | struct nlattr **attrs; |
| 2370 | struct nlattr *mon[TIPC_NLA_MON_MAX + 1]; |
| 2371 | |
| 2372 | err = tipc_nlmsg_parse(cb->nlh, &attrs); |
| 2373 | if (err) |
| 2374 | return err; |
| 2375 | |
| 2376 | if (!attrs[TIPC_NLA_MON]) |
| 2377 | return -EINVAL; |
| 2378 | |
| 2379 | err = nla_parse_nested(mon, TIPC_NLA_MON_MAX, |
| 2380 | attrs[TIPC_NLA_MON], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 2381 | tipc_nl_monitor_policy, NULL); |
Parthasarathy Bhuvaragan | cf6f7e1 | 2016-07-26 08:47:22 +0200 | [diff] [blame] | 2382 | if (err) |
| 2383 | return err; |
| 2384 | |
| 2385 | if (!mon[TIPC_NLA_MON_REF]) |
| 2386 | return -EINVAL; |
| 2387 | |
| 2388 | bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]); |
| 2389 | |
| 2390 | if (bearer_id >= MAX_BEARERS) |
| 2391 | return -EINVAL; |
| 2392 | } |
| 2393 | |
| 2394 | if (done) |
| 2395 | return 0; |
| 2396 | |
| 2397 | msg.skb = skb; |
| 2398 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 2399 | msg.seq = cb->nlh->nlmsg_seq; |
| 2400 | |
| 2401 | rtnl_lock(); |
| 2402 | err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node); |
| 2403 | if (!err) |
| 2404 | done = 1; |
| 2405 | |
| 2406 | rtnl_unlock(); |
| 2407 | cb->args[0] = done; |
| 2408 | cb->args[1] = prev_node; |
| 2409 | cb->args[2] = bearer_id; |
| 2410 | |
| 2411 | return skb->len; |
| 2412 | } |