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