blob: 68014f1b69765269236ac0a6d839ca754d1235da [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/node.c: TIPC node management routines
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy60020e12016-05-02 11:58:46 -04004 * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
Ying Xue46651c52014-03-27 12:54:36 +08005 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Jon Paul Maloy50100a52014-08-22 18:09:07 -040041#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040042#include "bcast.h"
Jon Paul Maloy35c55c92016-06-13 20:46:22 -040043#include "monitor.h"
Jon Paul Maloyd9992972015-07-16 16:54:31 -040044#include "discover.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Jon Paul Maloyc8199302015-10-15 14:52:46 -040046
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050047#define INVALID_NODE_SIG 0x10000
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +020048#define NODE_CLEANUP_AFTER 300000
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050049
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050050/* Flags used to take different actions according to flag type
51 * TIPC_NOTIFY_NODE_DOWN: notify node is down
52 * TIPC_NOTIFY_NODE_UP: notify node is up
53 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
54 */
55enum {
56 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
57 TIPC_NOTIFY_NODE_UP = (1 << 4),
58 TIPC_NOTIFY_LINK_UP = (1 << 6),
59 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
60};
61
62struct tipc_link_entry {
63 struct tipc_link *link;
64 spinlock_t lock; /* per link */
65 u32 mtu;
66 struct sk_buff_head inputq;
67 struct tipc_media_addr maddr;
68};
69
70struct tipc_bclink_entry {
71 struct tipc_link *link;
72 struct sk_buff_head inputq1;
73 struct sk_buff_head arrvq;
74 struct sk_buff_head inputq2;
75 struct sk_buff_head namedq;
76};
77
78/**
79 * struct tipc_node - TIPC node structure
80 * @addr: network address of node
81 * @ref: reference counter to node object
82 * @lock: rwlock governing access to structure
83 * @net: the applicable net namespace
84 * @hash: links to adjacent nodes in unsorted hash chain
85 * @inputq: pointer to input queue containing messages for msg event
86 * @namedq: pointer to name table input queue with name table messages
87 * @active_links: bearer ids of active links, used as index into links[] array
88 * @links: array containing references to all links to node
89 * @action_flags: bit mask of different types of node actions
90 * @state: connectivity state vs peer node
91 * @sync_point: sequence number where synch/failover is finished
92 * @list: links to adjacent nodes in sorted list of cluster's nodes
93 * @working_links: number of working links to node (both active and standby)
94 * @link_cnt: number of links to node
95 * @capabilities: bitmap, indicating peer node's functional capabilities
96 * @signature: node instance identifier
97 * @link_id: local and remote bearer ids of changing link, if any
98 * @publ_list: list of publications
99 * @rcu: rcu struct for tipc_node
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200100 * @delete_at: indicates the time for deleting a down node
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500101 */
102struct tipc_node {
103 u32 addr;
104 struct kref kref;
105 rwlock_t lock;
106 struct net *net;
107 struct hlist_node hash;
108 int active_links[2];
109 struct tipc_link_entry links[MAX_BEARERS];
110 struct tipc_bclink_entry bc_entry;
111 int action_flags;
112 struct list_head list;
113 int state;
114 u16 sync_point;
115 int link_cnt;
116 u16 working_links;
117 u16 capabilities;
118 u32 signature;
119 u32 link_id;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100120 u8 peer_id[16];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500121 struct list_head publ_list;
122 struct list_head conn_sks;
123 unsigned long keepalive_intv;
124 struct timer_list timer;
125 struct rcu_head rcu;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200126 unsigned long delete_at;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500127};
128
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400129/* Node FSM states and events:
130 */
131enum {
132 SELF_DOWN_PEER_DOWN = 0xdd,
133 SELF_UP_PEER_UP = 0xaa,
134 SELF_DOWN_PEER_LEAVING = 0xd1,
135 SELF_UP_PEER_COMING = 0xac,
136 SELF_COMING_PEER_UP = 0xca,
137 SELF_LEAVING_PEER_DOWN = 0x1d,
138 NODE_FAILINGOVER = 0xf0,
139 NODE_SYNCHING = 0xcc
140};
141
142enum {
143 SELF_ESTABL_CONTACT_EVT = 0xece,
144 SELF_LOST_CONTACT_EVT = 0x1ce,
145 PEER_ESTABL_CONTACT_EVT = 0x9ece,
146 PEER_LOST_CONTACT_EVT = 0x91ce,
147 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
148 NODE_FAILOVER_END_EVT = 0xfee,
149 NODE_SYNCH_BEGIN_EVT = 0xcbe,
150 NODE_SYNCH_END_EVT = 0xcee
151};
152
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400153static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
154 struct sk_buff_head *xmitq,
155 struct tipc_media_addr **maddr);
156static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
157 bool delete);
158static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800159static void tipc_node_delete(struct tipc_node *node);
Kees Cook31b102b2017-10-30 14:06:45 -0700160static void tipc_node_timeout(struct timer_list *t);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400161static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500162static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100163static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500164static void tipc_node_put(struct tipc_node *node);
Jon Maloy38077b82017-10-13 11:04:19 +0200165static bool node_is_up(struct tipc_node *n);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200166static void tipc_node_delete_from_list(struct tipc_node *node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400168struct tipc_sock_conn {
169 u32 port;
170 u32 peer_port;
171 u32 peer_node;
172 struct list_head list;
173};
174
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500175static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
176{
177 int bearer_id = n->active_links[sel & 1];
178
179 if (unlikely(bearer_id == INVALID_BEARER_ID))
180 return NULL;
181
182 return n->links[bearer_id].link;
183}
184
185int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
186{
187 struct tipc_node *n;
188 int bearer_id;
189 unsigned int mtu = MAX_MSG_SIZE;
190
191 n = tipc_node_find(net, addr);
192 if (unlikely(!n))
193 return mtu;
194
195 bearer_id = n->active_links[sel & 1];
196 if (likely(bearer_id != INVALID_BEARER_ID))
197 mtu = n->links[bearer_id].mtu;
198 tipc_node_put(n);
199 return mtu;
200}
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400201
Jon Maloy3e5cf362018-04-25 19:29:36 +0200202bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
203{
204 u8 *own_id = tipc_own_id(net);
205 struct tipc_node *n;
206
207 if (!own_id)
208 return true;
209
210 if (addr == tipc_own_addr(net)) {
211 memcpy(id, own_id, TIPC_NODEID_LEN);
212 return true;
213 }
214 n = tipc_node_find(net, addr);
215 if (!n)
216 return false;
217
218 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
219 tipc_node_put(n);
220 return true;
221}
222
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400223u16 tipc_node_get_capabilities(struct net *net, u32 addr)
224{
225 struct tipc_node *n;
226 u16 caps;
227
228 n = tipc_node_find(net, addr);
229 if (unlikely(!n))
230 return TIPC_NODE_CAPABILITIES;
231 caps = n->capabilities;
232 tipc_node_put(n);
233 return caps;
234}
235
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800236static void tipc_node_kref_release(struct kref *kref)
237{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500238 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800239
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500240 kfree(n->bc_entry.link);
241 kfree_rcu(n, rcu);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800242}
243
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500244static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800245{
246 kref_put(&node->kref, tipc_node_kref_release);
247}
248
249static void tipc_node_get(struct tipc_node *node)
250{
251 kref_get(&node->kref);
252}
253
Allan Stephensa635b462011-11-04 11:54:43 -0400254/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500255 * tipc_node_find - locate specified node object, if it exists
256 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500257static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500258{
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500259 struct tipc_net *tn = tipc_net(net);
Allan Stephens672d99e2011-02-25 18:42:52 -0500260 struct tipc_node *node;
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500261 unsigned int thash = tipc_hashfn(addr);
Allan Stephens672d99e2011-02-25 18:42:52 -0500262
Ying Xue6c7a7622014-03-27 12:54:37 +0800263 rcu_read_lock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500264 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
265 if (node->addr != addr)
266 continue;
267 if (!kref_get_unless_zero(&node->kref))
268 node = NULL;
269 break;
Allan Stephens672d99e2011-02-25 18:42:52 -0500270 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800271 rcu_read_unlock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500272 return node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500273}
274
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100275/* tipc_node_find_by_id - locate specified node object by its 128-bit id
276 * Note: this function is called only when a discovery request failed
277 * to find the node by its 32-bit id, and is not time critical
278 */
279static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
280{
281 struct tipc_net *tn = tipc_net(net);
282 struct tipc_node *n;
283 bool found = false;
284
285 rcu_read_lock();
286 list_for_each_entry_rcu(n, &tn->node_list, list) {
287 read_lock_bh(&n->lock);
288 if (!memcmp(id, n->peer_id, 16) &&
289 kref_get_unless_zero(&n->kref))
290 found = true;
291 read_unlock_bh(&n->lock);
292 if (found)
293 break;
294 }
295 rcu_read_unlock();
296 return found ? n : NULL;
297}
298
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500299static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500300{
301 read_lock_bh(&n->lock);
302}
303
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500304static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500305{
306 read_unlock_bh(&n->lock);
307}
308
309static void tipc_node_write_lock(struct tipc_node *n)
310{
311 write_lock_bh(&n->lock);
312}
313
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100314static void tipc_node_write_unlock_fast(struct tipc_node *n)
315{
316 write_unlock_bh(&n->lock);
317}
318
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500319static void tipc_node_write_unlock(struct tipc_node *n)
320{
321 struct net *net = n->net;
322 u32 addr = 0;
323 u32 flags = n->action_flags;
324 u32 link_id = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400325 u32 bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500326 struct list_head *publ_list;
327
328 if (likely(!flags)) {
329 write_unlock_bh(&n->lock);
330 return;
331 }
332
333 addr = n->addr;
334 link_id = n->link_id;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400335 bearer_id = link_id & 0xffff;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500336 publ_list = &n->publ_list;
337
338 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
339 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
340
341 write_unlock_bh(&n->lock);
342
343 if (flags & TIPC_NOTIFY_NODE_DOWN)
344 tipc_publ_notify(net, publ_list, addr);
345
346 if (flags & TIPC_NOTIFY_NODE_UP)
347 tipc_named_node_up(net, addr);
348
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400349 if (flags & TIPC_NOTIFY_LINK_UP) {
350 tipc_mon_peer_up(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500351 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
Jon Maloy218527f2018-03-29 23:20:41 +0200352 TIPC_NODE_SCOPE, link_id, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400353 }
354 if (flags & TIPC_NOTIFY_LINK_DOWN) {
355 tipc_mon_peer_down(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500356 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
Jon Maloy37922ea2018-03-29 23:20:43 +0200357 addr, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400358 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500359}
360
Wei Yongjune1a22d12018-03-26 14:33:13 +0000361static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
362 u8 *peer_id, u16 capabilities)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363{
Ying Xuef2f98002015-01-09 15:27:05 +0800364 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500365 struct tipc_node *n, *temp_node;
Jon Maloy9012de52018-07-10 01:07:35 +0200366 struct tipc_link *l;
367 int bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500368 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369
Ying Xuef2f98002015-01-09 15:27:05 +0800370 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500371 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400372 if (n) {
Jon Maloy40999f12018-07-18 19:50:06 +0200373 if (n->capabilities == capabilities)
374 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400375 /* Same node may come back with new capabilities */
Jon Maloy40999f12018-07-18 19:50:06 +0200376 write_lock_bh(&n->lock);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400377 n->capabilities = capabilities;
Jon Maloy9012de52018-07-10 01:07:35 +0200378 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
379 l = n->links[bearer_id].link;
380 if (l)
381 tipc_link_update_caps(l, capabilities);
382 }
Jon Maloy40999f12018-07-18 19:50:06 +0200383 write_unlock_bh(&n->lock);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500384 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400385 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500386 n = kzalloc(sizeof(*n), GFP_ATOMIC);
387 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400388 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500389 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700390 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500391 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100392 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500393 n->net = net;
394 n->capabilities = capabilities;
395 kref_init(&n->kref);
396 rwlock_init(&n->lock);
397 INIT_HLIST_NODE(&n->hash);
398 INIT_LIST_HEAD(&n->list);
399 INIT_LIST_HEAD(&n->publ_list);
400 INIT_LIST_HEAD(&n->conn_sks);
401 skb_queue_head_init(&n->bc_entry.namedq);
402 skb_queue_head_init(&n->bc_entry.inputq1);
403 __skb_queue_head_init(&n->bc_entry.arrvq);
404 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500405 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500406 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500407 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200408 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500409 n->signature = INVALID_NODE_SIG;
410 n->active_links[0] = INVALID_BEARER_ID;
411 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100412 if (!tipc_link_bc_create(net, tipc_own_addr(net),
413 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500414 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500415 n->capabilities,
416 &n->bc_entry.inputq1,
417 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400418 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500419 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400420 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500421 kfree(n);
422 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400423 goto exit;
424 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500425 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700426 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500427 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500428 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
429 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
430 if (n->addr < temp_node->addr)
431 break;
432 }
433 list_add_tail_rcu(&n->list, &temp_node->list);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500434exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800435 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500436 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437}
438
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400439static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
440{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500441 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400442 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400443
444 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400445 if (intv < n->keepalive_intv)
446 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400447
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400448 /* Ensure link's abort limit corresponds to current tolerance */
449 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400450}
451
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200452static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800454 list_del_rcu(&node->list);
455 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500456 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200457}
458
459static void tipc_node_delete(struct tipc_node *node)
460{
461 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500462
463 del_timer_sync(&node->timer);
464 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
Ying Xuef2f98002015-01-09 15:27:05 +0800467void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800468{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500469 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800470 struct tipc_node *node, *t_node;
471
Ying Xuef2f98002015-01-09 15:27:05 +0800472 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500473 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
474 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800475 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800476}
477
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500478void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
479{
480 struct tipc_node *n;
481
482 if (in_own_node(net, addr))
483 return;
484
485 n = tipc_node_find(net, addr);
486 if (!n) {
487 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
488 return;
489 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500490 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500491 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100492 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500493 tipc_node_put(n);
494}
495
496void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
497{
498 struct tipc_node *n;
499
500 if (in_own_node(net, addr))
501 return;
502
503 n = tipc_node_find(net, addr);
504 if (!n) {
505 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
506 return;
507 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500508 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500509 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100510 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500511 tipc_node_put(n);
512}
513
Ying Xuef2f98002015-01-09 15:27:05 +0800514int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400515{
516 struct tipc_node *node;
517 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800518 int err = 0;
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400519
Ying Xue34747532015-01-09 15:27:10 +0800520 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400521 return 0;
522
Ying Xuef2f98002015-01-09 15:27:05 +0800523 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400524 if (!node) {
525 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
526 return -EHOSTUNREACH;
527 }
528 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800529 if (!conn) {
530 err = -EHOSTUNREACH;
531 goto exit;
532 }
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400533 conn->peer_node = dnode;
534 conn->port = port;
535 conn->peer_port = peer_port;
536
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500537 tipc_node_write_lock(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400538 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500539 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800540exit:
541 tipc_node_put(node);
542 return err;
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400543}
544
Ying Xuef2f98002015-01-09 15:27:05 +0800545void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400546{
547 struct tipc_node *node;
548 struct tipc_sock_conn *conn, *safe;
549
Ying Xue34747532015-01-09 15:27:10 +0800550 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400551 return;
552
Ying Xuef2f98002015-01-09 15:27:05 +0800553 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400554 if (!node)
555 return;
556
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500557 tipc_node_write_lock(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400558 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
559 if (port != conn->port)
560 continue;
561 list_del(&conn->list);
562 kfree(conn);
563 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500564 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800565 tipc_node_put(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400566}
567
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200568static void tipc_node_clear_links(struct tipc_node *node)
569{
570 int i;
571
572 for (i = 0; i < MAX_BEARERS; i++) {
573 struct tipc_link_entry *le = &node->links[i];
574
575 if (le->link) {
576 kfree(le->link);
577 le->link = NULL;
578 node->link_cnt--;
579 }
580 }
581}
582
583/* tipc_node_cleanup - delete nodes that does not
584 * have active links for NODE_CLEANUP_AFTER time
585 */
586static int tipc_node_cleanup(struct tipc_node *peer)
587{
588 struct tipc_net *tn = tipc_net(peer->net);
589 bool deleted = false;
590
591 spin_lock_bh(&tn->node_list_lock);
592 tipc_node_write_lock(peer);
593
594 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
595 tipc_node_clear_links(peer);
596 tipc_node_delete_from_list(peer);
597 deleted = true;
598 }
599 tipc_node_write_unlock(peer);
600 spin_unlock_bh(&tn->node_list_lock);
601 return deleted;
602}
603
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400604/* tipc_node_timeout - handle expiration of node timer
605 */
Kees Cook31b102b2017-10-30 14:06:45 -0700606static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400607{
Kees Cook31b102b2017-10-30 14:06:45 -0700608 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400609 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400610 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200611 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400612 int bearer_id;
613 int rc = 0;
614
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200615 if (!node_is_up(n) && tipc_node_cleanup(n)) {
616 /*Removing the reference of Timer*/
617 tipc_node_put(n);
618 return;
619 }
620
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400621 __skb_queue_head_init(&xmitq);
622
Tung Nguyen759f29b2018-06-28 22:39:25 +0200623 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500624 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400625 le = &n->links[bearer_id];
626 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200627 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400628 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400629 tipc_node_calculate_timer(n, le->link);
630 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200631 spin_unlock_bh(&le->lock);
632 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400633 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500634 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400635 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
636 if (rc & TIPC_LINK_DOWN_EVT)
637 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400638 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400639 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400640}
641
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400643 * __tipc_node_link_up - handle addition of link
644 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 * Link becomes active (alone or shared) or standby, depending on its priority.
646 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400647static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
648 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400650 int *slot0 = &n->active_links[0];
651 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400652 struct tipc_link *ol = node_active_link(n, 0);
653 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400655 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400656 return;
657
658 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
659 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400660 return;
661
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400662 n->working_links++;
663 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500664 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400665
666 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500667 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800668
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400669 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400670 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400671
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100672 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500673 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400675 /* Ensure that a STATE message goes first */
676 tipc_link_build_state_msg(nl, xmitq);
677
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400678 /* First link? => give it both slots */
679 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400680 *slot0 = bearer_id;
681 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400682 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
683 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400684 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400685 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400686 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400688
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400689 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500690 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
691 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400692 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400693 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400694 tipc_link_set_active(nl, true);
695 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500696 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400697 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a842015-10-22 08:51:47 -0400698 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400699 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500700 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400703 /* Prepare synchronization with first link */
704 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705}
706
707/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400708 * tipc_node_link_up - handle addition of link
709 *
710 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400712static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
713 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400715 struct tipc_media_addr *maddr;
716
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500717 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400718 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400719 maddr = &n->links[bearer_id].maddr;
720 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500721 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400722}
723
724/**
725 * __tipc_node_link_down - handle loss of link
726 */
727static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
728 struct sk_buff_head *xmitq,
729 struct tipc_media_addr **maddr)
730{
731 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400732 int *slot0 = &n->active_links[0];
733 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500734 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400735 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400737 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400738 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400739 return;
740
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400741 n->working_links--;
742 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500743 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700744
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400745 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400746
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100747 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500748 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400750 /* Select new active link if any available */
751 *slot0 = INVALID_BEARER_ID;
752 *slot1 = INVALID_BEARER_ID;
753 for (i = 0; i < MAX_BEARERS; i++) {
754 _l = n->links[i].link;
755 if (!_l || !tipc_link_is_up(_l))
756 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400757 if (_l == l)
758 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500759 prio = tipc_link_prio(_l);
760 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400761 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500762 if (prio > highest) {
763 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400764 *slot0 = i;
765 *slot1 = i;
766 continue;
767 }
768 *slot1 = i;
769 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400770
Jon Maloy38077b82017-10-13 11:04:19 +0200771 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400772 if (tipc_link_peer_is_down(l))
773 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
774 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
775 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400776 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400777 tipc_link_build_reset_msg(l, xmitq);
778 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400779 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400780 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400781 return;
782 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400783 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400784
785 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500786 *bearer_id = n->active_links[0];
787 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400788 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
789 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500790 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400791 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400792 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400793 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400794 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400795 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500796 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400797}
798
799static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
800{
801 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400802 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400803 struct tipc_media_addr *maddr;
804 struct sk_buff_head xmitq;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400805 int old_bearer_id = bearer_id;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400806
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400807 if (!l)
808 return;
809
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400810 __skb_queue_head_init(&xmitq);
811
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500812 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400813 if (!tipc_link_is_establishing(l)) {
814 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
815 if (delete) {
816 kfree(l);
817 le->link = NULL;
818 n->link_cnt--;
819 }
820 } else {
821 /* Defuse pending tipc_node_link_up() */
822 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400823 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500824 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400825 if (delete)
826 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400827 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
828 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829}
830
Jon Maloy38077b82017-10-13 11:04:19 +0200831static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400833 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834}
835
Jon Maloy38077b82017-10-13 11:04:19 +0200836bool tipc_node_is_up(struct net *net, u32 addr)
837{
838 struct tipc_node *n;
839 bool retval = false;
840
841 if (in_own_node(net, addr))
842 return true;
843
844 n = tipc_node_find(net, addr);
845 if (!n)
846 return false;
847 retval = node_is_up(n);
848 tipc_node_put(n);
849 return retval;
850}
851
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100852static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
853{
854 struct tipc_node *n;
855
856 addr ^= tipc_net(net)->random;
857 while ((n = tipc_node_find(net, addr))) {
858 tipc_node_put(n);
859 addr++;
860 }
861 return addr;
862}
863
864/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
Jon Maloy2a57f182018-07-06 20:10:03 +0200865 * Returns suggested address if any, otherwise 0
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100866 */
867u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
868{
869 struct tipc_net *tn = tipc_net(net);
870 struct tipc_node *n;
871
872 /* Suggest new address if some other peer is using this one */
873 n = tipc_node_find(net, addr);
874 if (n) {
875 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
876 addr = 0;
877 tipc_node_put(n);
878 if (!addr)
879 return 0;
880 return tipc_node_suggest_addr(net, addr);
881 }
882
883 /* Suggest previously used address if peer is known */
884 n = tipc_node_find_by_id(net, id);
885 if (n) {
886 addr = n->addr;
887 tipc_node_put(n);
Jon Maloy2a57f182018-07-06 20:10:03 +0200888 return addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100889 }
Jon Maloy2a57f182018-07-06 20:10:03 +0200890
891 /* Even this node may be in conflict */
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100892 if (tn->trial_addr == addr)
893 return tipc_node_suggest_addr(net, addr);
894
Jon Maloy2a57f182018-07-06 20:10:03 +0200895 return 0;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100896}
897
898void tipc_node_check_dest(struct net *net, u32 addr,
899 u8 *peer_id, struct tipc_bearer *b,
Jon Paul Maloycf148812015-07-30 18:24:22 -0400900 u16 capabilities, u32 signature,
901 struct tipc_media_addr *maddr,
902 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400903{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400904 struct tipc_node *n;
905 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400906 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400907 bool addr_match = false;
908 bool sign_match = false;
909 bool link_up = false;
910 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400911 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400912 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400913 unsigned long intv;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400914
Jon Paul Maloycf148812015-07-30 18:24:22 -0400915 *dupl_addr = false;
916 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400917
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100918 n = tipc_node_create(net, addr, peer_id, capabilities);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400919 if (!n)
920 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400921
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500922 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400923
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400924 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400925
926 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400927 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400928 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400929 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400930 sign_match = (signature == n->signature);
931
932 /* These three flags give us eight permutations: */
933
934 if (sign_match && addr_match && link_up) {
935 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400936 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400937 } else if (sign_match && addr_match && !link_up) {
938 /* Respond. The link will come up in due time */
939 *respond = true;
940 } else if (sign_match && !addr_match && link_up) {
941 /* Peer has changed i/f address without rebooting.
942 * If so, the link will reset soon, and the next
943 * discovery will be accepted. So we can ignore it.
944 * It may also be an cloned or malicious peer having
945 * chosen the same node address and signature as an
946 * existing one.
947 * Ignore requests until the link goes down, if ever.
948 */
949 *dupl_addr = true;
950 } else if (sign_match && !addr_match && !link_up) {
951 /* Peer link has changed i/f address without rebooting.
952 * It may also be a cloned or malicious peer; we can't
953 * distinguish between the two.
954 * The signature is correct, so we must accept.
955 */
956 accept_addr = true;
957 *respond = true;
958 } else if (!sign_match && addr_match && link_up) {
959 /* Peer node rebooted. Two possibilities:
960 * - Delayed re-discovery; this link endpoint has already
961 * reset and re-established contact with the peer, before
962 * receiving a discovery message from that node.
963 * (The peer happened to receive one from this node first).
964 * - The peer came back so fast that our side has not
965 * discovered it yet. Probing from this side will soon
966 * reset the link, since there can be no working link
967 * endpoint at the peer end, and the link will re-establish.
968 * Accept the signature, since it comes from a known peer.
969 */
970 n->signature = signature;
971 } else if (!sign_match && addr_match && !link_up) {
972 /* The peer node has rebooted.
973 * Accept signature, since it is a known peer.
974 */
975 n->signature = signature;
976 *respond = true;
977 } else if (!sign_match && !addr_match && link_up) {
978 /* Peer rebooted with new address, or a new/duplicate peer.
979 * Ignore until the link goes down, if ever.
980 */
981 *dupl_addr = true;
982 } else if (!sign_match && !addr_match && !link_up) {
983 /* Peer rebooted with new address, or it is a new peer.
984 * Accept signature and address.
985 */
986 n->signature = signature;
987 accept_addr = true;
988 *respond = true;
989 }
990
991 if (!accept_addr)
992 goto exit;
993
994 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400995 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +0100996 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400997 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +0100998
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400999 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001000 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001001 b->net_plane, b->mtu, b->priority,
1002 b->window, mod(tipc_net(net)->random),
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001003 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -04001004 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001005 tipc_bc_sndlink(n->net), n->bc_entry.link,
1006 &le->inputq,
1007 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -04001008 *respond = false;
1009 goto exit;
1010 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001011 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -04001012 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001013 if (n->state == NODE_FAILINGOVER)
1014 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001015 le->link = l;
1016 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001017 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001018 if (n->link_cnt == 1) {
1019 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1020 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001021 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001022 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001023 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001024 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001025exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001026 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001027 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001028 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001029 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001030}
1031
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001032void tipc_node_delete_links(struct net *net, int bearer_id)
1033{
1034 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001035 struct tipc_node *n;
1036
1037 rcu_read_lock();
1038 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001039 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001040 }
1041 rcu_read_unlock();
1042}
1043
1044static void tipc_node_reset_links(struct tipc_node *n)
1045{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001046 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001047
Jon Maloyd50ccc22018-03-22 20:42:50 +01001048 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001049
1050 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001051 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001052 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001053}
1054
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001055/* tipc_node_fsm_evt - node finite state machine
1056 * Determines when contact is allowed with peer node
1057 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001058static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001059{
1060 int state = n->state;
1061
1062 switch (state) {
1063 case SELF_DOWN_PEER_DOWN:
1064 switch (evt) {
1065 case SELF_ESTABL_CONTACT_EVT:
1066 state = SELF_UP_PEER_COMING;
1067 break;
1068 case PEER_ESTABL_CONTACT_EVT:
1069 state = SELF_COMING_PEER_UP;
1070 break;
1071 case SELF_LOST_CONTACT_EVT:
1072 case PEER_LOST_CONTACT_EVT:
1073 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001074 case NODE_SYNCH_END_EVT:
1075 case NODE_SYNCH_BEGIN_EVT:
1076 case NODE_FAILOVER_BEGIN_EVT:
1077 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001078 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001079 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001080 }
1081 break;
1082 case SELF_UP_PEER_UP:
1083 switch (evt) {
1084 case SELF_LOST_CONTACT_EVT:
1085 state = SELF_DOWN_PEER_LEAVING;
1086 break;
1087 case PEER_LOST_CONTACT_EVT:
1088 state = SELF_LEAVING_PEER_DOWN;
1089 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001090 case NODE_SYNCH_BEGIN_EVT:
1091 state = NODE_SYNCHING;
1092 break;
1093 case NODE_FAILOVER_BEGIN_EVT:
1094 state = NODE_FAILINGOVER;
1095 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001096 case SELF_ESTABL_CONTACT_EVT:
1097 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001098 case NODE_SYNCH_END_EVT:
1099 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001100 break;
1101 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001102 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001103 }
1104 break;
1105 case SELF_DOWN_PEER_LEAVING:
1106 switch (evt) {
1107 case PEER_LOST_CONTACT_EVT:
1108 state = SELF_DOWN_PEER_DOWN;
1109 break;
1110 case SELF_ESTABL_CONTACT_EVT:
1111 case PEER_ESTABL_CONTACT_EVT:
1112 case SELF_LOST_CONTACT_EVT:
1113 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001114 case NODE_SYNCH_END_EVT:
1115 case NODE_SYNCH_BEGIN_EVT:
1116 case NODE_FAILOVER_BEGIN_EVT:
1117 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001118 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001119 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001120 }
1121 break;
1122 case SELF_UP_PEER_COMING:
1123 switch (evt) {
1124 case PEER_ESTABL_CONTACT_EVT:
1125 state = SELF_UP_PEER_UP;
1126 break;
1127 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001128 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001129 break;
1130 case SELF_ESTABL_CONTACT_EVT:
1131 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001132 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001133 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001134 break;
1135 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001136 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001137 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001138 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001139 }
1140 break;
1141 case SELF_COMING_PEER_UP:
1142 switch (evt) {
1143 case SELF_ESTABL_CONTACT_EVT:
1144 state = SELF_UP_PEER_UP;
1145 break;
1146 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001147 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001148 break;
1149 case SELF_LOST_CONTACT_EVT:
1150 case PEER_ESTABL_CONTACT_EVT:
1151 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001152 case NODE_SYNCH_END_EVT:
1153 case NODE_SYNCH_BEGIN_EVT:
1154 case NODE_FAILOVER_BEGIN_EVT:
1155 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001156 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001157 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001158 }
1159 break;
1160 case SELF_LEAVING_PEER_DOWN:
1161 switch (evt) {
1162 case SELF_LOST_CONTACT_EVT:
1163 state = SELF_DOWN_PEER_DOWN;
1164 break;
1165 case SELF_ESTABL_CONTACT_EVT:
1166 case PEER_ESTABL_CONTACT_EVT:
1167 case PEER_LOST_CONTACT_EVT:
1168 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001169 case NODE_SYNCH_END_EVT:
1170 case NODE_SYNCH_BEGIN_EVT:
1171 case NODE_FAILOVER_BEGIN_EVT:
1172 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001173 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001174 goto illegal_evt;
1175 }
1176 break;
1177 case NODE_FAILINGOVER:
1178 switch (evt) {
1179 case SELF_LOST_CONTACT_EVT:
1180 state = SELF_DOWN_PEER_LEAVING;
1181 break;
1182 case PEER_LOST_CONTACT_EVT:
1183 state = SELF_LEAVING_PEER_DOWN;
1184 break;
1185 case NODE_FAILOVER_END_EVT:
1186 state = SELF_UP_PEER_UP;
1187 break;
1188 case NODE_FAILOVER_BEGIN_EVT:
1189 case SELF_ESTABL_CONTACT_EVT:
1190 case PEER_ESTABL_CONTACT_EVT:
1191 break;
1192 case NODE_SYNCH_BEGIN_EVT:
1193 case NODE_SYNCH_END_EVT:
1194 default:
1195 goto illegal_evt;
1196 }
1197 break;
1198 case NODE_SYNCHING:
1199 switch (evt) {
1200 case SELF_LOST_CONTACT_EVT:
1201 state = SELF_DOWN_PEER_LEAVING;
1202 break;
1203 case PEER_LOST_CONTACT_EVT:
1204 state = SELF_LEAVING_PEER_DOWN;
1205 break;
1206 case NODE_SYNCH_END_EVT:
1207 state = SELF_UP_PEER_UP;
1208 break;
1209 case NODE_FAILOVER_BEGIN_EVT:
1210 state = NODE_FAILINGOVER;
1211 break;
1212 case NODE_SYNCH_BEGIN_EVT:
1213 case SELF_ESTABL_CONTACT_EVT:
1214 case PEER_ESTABL_CONTACT_EVT:
1215 break;
1216 case NODE_FAILOVER_END_EVT:
1217 default:
1218 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001219 }
1220 break;
1221 default:
1222 pr_err("Unknown node fsm state %x\n", state);
1223 break;
1224 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001225 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001226 return;
1227
1228illegal_evt:
1229 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001230}
1231
Jon Paul Maloy52666982015-10-22 08:51:41 -04001232static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001233 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001235 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001236 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001237 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001238 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001239 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240
Jon Maloyd50ccc22018-03-22 20:42:50 +01001241 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001242 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001243
Jon Paul Maloy52666982015-10-22 08:51:41 -04001244 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001245 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001247 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001249 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001250 if (l)
1251 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001253
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001254 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001255 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001256
1257 /* Notify sockets connected to node */
1258 list_for_each_entry_safe(conn, safe, conns, list) {
1259 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001260 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001261 conn->peer_node, conn->port,
1262 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001263 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001264 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001265 list_del(&conn->list);
1266 kfree(conn);
1267 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268}
1269
Erik Hugne78acb1f2014-04-24 16:26:47 +02001270/**
1271 * tipc_node_get_linkname - get the name of a link
1272 *
1273 * @bearer_id: id of the bearer
1274 * @node: peer node address
1275 * @linkname: link name output buffer
1276 *
1277 * Returns 0 on success
1278 */
Ying Xuef2f98002015-01-09 15:27:05 +08001279int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1280 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001281{
1282 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001283 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001284 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001285
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001286 if (!node)
1287 return err;
1288
1289 if (bearer_id >= MAX_BEARERS)
1290 goto exit;
1291
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001292 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001293 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001294 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001295 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001296 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001297 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001298 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001299exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001300 tipc_node_put(node);
1301 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001302}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001303
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001304/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001305static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001306{
1307 void *hdr;
1308 struct nlattr *attrs;
1309
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001310 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001311 NLM_F_MULTI, TIPC_NL_NODE_GET);
1312 if (!hdr)
1313 return -EMSGSIZE;
1314
1315 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1316 if (!attrs)
1317 goto msg_full;
1318
1319 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1320 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001321 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001322 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1323 goto attr_msg_full;
1324
1325 nla_nest_end(msg->skb, attrs);
1326 genlmsg_end(msg->skb, hdr);
1327
1328 return 0;
1329
1330attr_msg_full:
1331 nla_nest_cancel(msg->skb, attrs);
1332msg_full:
1333 genlmsg_cancel(msg->skb, hdr);
1334
1335 return -EMSGSIZE;
1336}
1337
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001338/**
1339 * tipc_node_xmit() is the general link level function for message sending
1340 * @net: the applicable net namespace
1341 * @list: chain of buffers containing message
1342 * @dnode: address of destination node
1343 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001344 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001345 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001346 */
1347int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1348 u32 dnode, int selector)
1349{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001350 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001351 struct tipc_node *n;
1352 struct sk_buff_head xmitq;
Richard Alpe4952cd32016-02-11 10:43:15 +01001353 int bearer_id;
1354 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001355
Richard Alpe4952cd32016-02-11 10:43:15 +01001356 if (in_own_node(net, dnode)) {
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001357 tipc_sk_rcv(net, list);
1358 return 0;
1359 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001360
1361 n = tipc_node_find(net, dnode);
1362 if (unlikely(!n)) {
1363 skb_queue_purge(list);
1364 return -EHOSTUNREACH;
1365 }
1366
1367 tipc_node_read_lock(n);
1368 bearer_id = n->active_links[selector & 1];
1369 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1370 tipc_node_read_unlock(n);
1371 tipc_node_put(n);
1372 skb_queue_purge(list);
1373 return -EHOSTUNREACH;
1374 }
1375
1376 __skb_queue_head_init(&xmitq);
1377 le = &n->links[bearer_id];
1378 spin_lock_bh(&le->lock);
1379 rc = tipc_link_xmit(le->link, list, &xmitq);
1380 spin_unlock_bh(&le->lock);
1381 tipc_node_read_unlock(n);
1382
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001383 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001384 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001385 else
1386 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001387
1388 tipc_node_put(n);
1389
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001390 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001391}
1392
1393/* tipc_node_xmit_skb(): send single buffer to destination
1394 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1395 * messages, which will not be rejected
1396 * The only exception is datagram messages rerouted after secondary
1397 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001398 */
1399int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1400 u32 selector)
1401{
1402 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001403
1404 skb_queue_head_init(&head);
1405 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001406 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001407 return 0;
1408}
1409
Jon Maloyf70d37b2017-10-13 11:04:21 +02001410/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1411 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1412 */
1413int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1414{
1415 struct sk_buff *skb;
1416 u32 selector, dnode;
1417
1418 while ((skb = __skb_dequeue(xmitq))) {
1419 selector = msg_origport(buf_msg(skb));
1420 dnode = msg_destnode(buf_msg(skb));
1421 tipc_node_xmit_skb(net, skb, dnode, selector);
1422 }
1423 return 0;
1424}
1425
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001426void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1427{
1428 struct sk_buff *txskb;
1429 struct tipc_node *n;
1430 u32 dst;
1431
1432 rcu_read_lock();
1433 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1434 dst = n->addr;
1435 if (in_own_node(net, dst))
1436 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001437 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001438 continue;
1439 txskb = pskb_copy(skb, GFP_ATOMIC);
1440 if (!txskb)
1441 break;
1442 msg_set_destnode(buf_msg(txskb), dst);
1443 tipc_node_xmit_skb(net, txskb, dst, 0);
1444 }
1445 rcu_read_unlock();
1446
1447 kfree_skb(skb);
1448}
1449
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001450static void tipc_node_mcast_rcv(struct tipc_node *n)
1451{
1452 struct tipc_bclink_entry *be = &n->bc_entry;
1453
1454 /* 'arrvq' is under inputq2's lock protection */
1455 spin_lock_bh(&be->inputq2.lock);
1456 spin_lock_bh(&be->inputq1.lock);
1457 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1458 spin_unlock_bh(&be->inputq1.lock);
1459 spin_unlock_bh(&be->inputq2.lock);
1460 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1461}
1462
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001463static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1464 int bearer_id, struct sk_buff_head *xmitq)
1465{
1466 struct tipc_link *ucl;
1467 int rc;
1468
1469 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1470
1471 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001472 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001473 return;
1474 }
1475
1476 if (!(rc & TIPC_LINK_SND_STATE))
1477 return;
1478
1479 /* If probe message, a STATE response will be sent anyway */
1480 if (msg_probe(hdr))
1481 return;
1482
1483 /* Produce a STATE message carrying broadcast NACK */
1484 tipc_node_read_lock(n);
1485 ucl = n->links[bearer_id].link;
1486 if (ucl)
1487 tipc_link_build_state_msg(ucl, xmitq);
1488 tipc_node_read_unlock(n);
1489}
1490
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001491/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001492 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1493 * @net: the applicable net namespace
1494 * @skb: TIPC packet
1495 * @bearer_id: id of bearer message arrived on
1496 *
1497 * Invoked with no locks held.
1498 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001499static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001500{
1501 int rc;
1502 struct sk_buff_head xmitq;
1503 struct tipc_bclink_entry *be;
1504 struct tipc_link_entry *le;
1505 struct tipc_msg *hdr = buf_msg(skb);
1506 int usr = msg_user(hdr);
1507 u32 dnode = msg_destnode(hdr);
1508 struct tipc_node *n;
1509
1510 __skb_queue_head_init(&xmitq);
1511
1512 /* If NACK for other node, let rcv link for that node peek into it */
1513 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1514 n = tipc_node_find(net, dnode);
1515 else
1516 n = tipc_node_find(net, msg_prevnode(hdr));
1517 if (!n) {
1518 kfree_skb(skb);
1519 return;
1520 }
1521 be = &n->bc_entry;
1522 le = &n->links[bearer_id];
1523
1524 rc = tipc_bcast_rcv(net, be->link, skb);
1525
Jon Paul Maloy52666982015-10-22 08:51:41 -04001526 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001527 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001528 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001529 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001530 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001531 }
1532
1533 if (!skb_queue_empty(&xmitq))
1534 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1535
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001536 if (!skb_queue_empty(&be->inputq1))
1537 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f3e2016-07-11 16:08:37 -04001538
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001539 /* If reassembly or retransmission failure => reset all links to peer */
1540 if (rc & TIPC_LINK_DOWN_EVT)
1541 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f3e2016-07-11 16:08:37 -04001542
Jon Paul Maloy52666982015-10-22 08:51:41 -04001543 tipc_node_put(n);
1544}
1545
1546/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001547 * tipc_node_check_state - check and if necessary update node state
1548 * @skb: TIPC packet
1549 * @bearer_id: identity of bearer delivering the packet
Jon Maloy7ea817f2018-07-10 01:07:36 +02001550 * Returns true if state and msg are ok, otherwise false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001551 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001552static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001553 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001554{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001555 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001556 int usr = msg_user(hdr);
1557 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001558 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001559 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1560 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001561 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001562 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001563 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001564 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001565 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001566
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001567 l = n->links[bearer_id].link;
1568 if (!l)
1569 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001570 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001571
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001572
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001573 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1574 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001575
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001576 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001577 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1578 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1579 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001580 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001581 }
1582 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001583
Jon Maloy7ea817f2018-07-10 01:07:36 +02001584 if (!tipc_link_validate_msg(l, hdr))
1585 return false;
1586
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001587 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001588 if (state == SELF_UP_PEER_COMING) {
1589 if (!tipc_link_is_up(l))
1590 return true;
1591 if (!msg_peer_link_is_up(hdr))
1592 return true;
1593 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1594 }
1595
1596 if (state == SELF_DOWN_PEER_LEAVING) {
1597 if (msg_peer_node_is_up(hdr))
1598 return false;
1599 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001600 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001601 }
1602
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001603 if (state == SELF_LEAVING_PEER_DOWN)
1604 return false;
1605
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001606 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001607 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001608 return true;
1609
1610 /* Initiate or update failover mode if applicable */
1611 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1612 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001613 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001614 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001615 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1616 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001617 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001618 /* If pkts arrive out of order, use lowest calculated syncpt */
1619 if (less(syncpt, n->sync_point))
1620 n->sync_point = syncpt;
1621 }
1622
1623 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001624 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001625 if (!more(rcv_nxt, n->sync_point))
1626 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001627 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1628 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001629 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001630 return true;
1631 }
1632
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001633 /* No synching needed if only one link */
1634 if (!pl || !tipc_link_is_up(pl))
1635 return true;
1636
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001637 /* Initiate synch mode if applicable */
1638 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001639 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001640 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001641 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001642 if (n->state == SELF_UP_PEER_UP) {
1643 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001644 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001645 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1646 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001647 }
1648
1649 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001650 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001651 if (tipc_link_is_synching(l)) {
1652 tnl = l;
1653 } else {
1654 tnl = pl;
1655 pl = l;
1656 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001657 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1658 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001659 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001660 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001661 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001662 return true;
1663 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001664 if (l == pl)
1665 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001666 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1667 return true;
1668 if (usr == LINK_PROTOCOL)
1669 return true;
1670 return false;
1671 }
1672 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001673}
1674
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001675/**
1676 * tipc_rcv - process TIPC packets/messages arriving from off-node
1677 * @net: the applicable net namespace
1678 * @skb: TIPC packet
1679 * @bearer: pointer to bearer message arrived on
1680 *
1681 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1682 * structure (i.e. cannot be NULL), but bearer can be inactive.
1683 */
1684void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1685{
1686 struct sk_buff_head xmitq;
1687 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001688 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001689 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001690 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001691 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001692 int usr, rc = 0;
1693 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001694
1695 __skb_queue_head_init(&xmitq);
1696
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001697 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01001698 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001699 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001700 hdr = buf_msg(skb);
1701 usr = msg_user(hdr);
1702 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001703
Jon Paul Maloy52666982015-10-22 08:51:41 -04001704 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001705 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001706 if (unlikely(usr == LINK_CONFIG))
1707 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001708 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001709 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001710 }
1711
Hamish Martinefe79052016-04-29 10:40:24 -04001712 /* Discard unicast link messages destined for another node */
1713 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1714 goto discard;
1715
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001716 /* Locate neighboring node that sent packet */
1717 n = tipc_node_find(net, msg_prevnode(hdr));
1718 if (unlikely(!n))
1719 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001720 le = &n->links[bearer_id];
1721
Jon Paul Maloy52666982015-10-22 08:51:41 -04001722 /* Ensure broadcast reception is in synch with peer's send state */
1723 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001724 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001725 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001726 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001727
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001728 /* Receive packet directly if conditions permit */
1729 tipc_node_read_lock(n);
1730 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001731 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001732 if (le->link) {
1733 rc = tipc_link_rcv(le->link, skb, &xmitq);
1734 skb = NULL;
1735 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001736 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001737 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001738 tipc_node_read_unlock(n);
1739
1740 /* Check/update node state before receiving */
1741 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001742 if (unlikely(skb_linearize(skb)))
1743 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001744 tipc_node_write_lock(n);
1745 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1746 if (le->link) {
1747 rc = tipc_link_rcv(le->link, skb, &xmitq);
1748 skb = NULL;
1749 }
1750 }
1751 tipc_node_write_unlock(n);
1752 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001753
1754 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001755 tipc_node_link_up(n, bearer_id, &xmitq);
1756
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001757 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001758 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001759
Jon Paul Maloy52666982015-10-22 08:51:41 -04001760 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1761 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001762
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001763 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1764 tipc_node_mcast_rcv(n);
1765
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001766 if (!skb_queue_empty(&le->inputq))
1767 tipc_sk_rcv(net, &le->inputq);
1768
1769 if (!skb_queue_empty(&xmitq))
1770 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1771
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001772 tipc_node_put(n);
1773discard:
1774 kfree_skb(skb);
1775}
1776
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001777void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1778 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01001779{
1780 struct tipc_net *tn = tipc_net(net);
1781 int bearer_id = b->identity;
1782 struct sk_buff_head xmitq;
1783 struct tipc_link_entry *e;
1784 struct tipc_node *n;
1785
1786 __skb_queue_head_init(&xmitq);
1787
1788 rcu_read_lock();
1789
1790 list_for_each_entry_rcu(n, &tn->node_list, list) {
1791 tipc_node_write_lock(n);
1792 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001793 if (e->link) {
1794 if (prop == TIPC_NLA_PROP_TOL)
1795 tipc_link_set_tolerance(e->link, b->tolerance,
1796 &xmitq);
1797 else if (prop == TIPC_NLA_PROP_MTU)
1798 tipc_link_set_mtu(e->link, b->mtu);
1799 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01001800 tipc_node_write_unlock(n);
1801 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1802 }
1803
1804 rcu_read_unlock();
1805}
1806
Richard Alpeb3404022016-08-18 10:33:52 +02001807int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1808{
1809 struct net *net = sock_net(skb->sk);
1810 struct tipc_net *tn = net_generic(net, tipc_net_id);
1811 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1812 struct tipc_node *peer;
1813 u32 addr;
1814 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02001815
1816 /* We identify the peer by its net */
1817 if (!info->attrs[TIPC_NLA_NET])
1818 return -EINVAL;
1819
1820 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001821 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
Johannes Bergfe521452017-04-12 14:34:08 +02001822 info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02001823 if (err)
1824 return err;
1825
1826 if (!attrs[TIPC_NLA_NET_ADDR])
1827 return -EINVAL;
1828
1829 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1830
1831 if (in_own_node(net, addr))
1832 return -ENOTSUPP;
1833
1834 spin_lock_bh(&tn->node_list_lock);
1835 peer = tipc_node_find(net, addr);
1836 if (!peer) {
1837 spin_unlock_bh(&tn->node_list_lock);
1838 return -ENXIO;
1839 }
1840
1841 tipc_node_write_lock(peer);
1842 if (peer->state != SELF_DOWN_PEER_DOWN &&
1843 peer->state != SELF_DOWN_PEER_LEAVING) {
1844 tipc_node_write_unlock(peer);
1845 err = -EBUSY;
1846 goto err_out;
1847 }
1848
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001849 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02001850 tipc_node_write_unlock(peer);
1851 tipc_node_delete(peer);
1852
1853 err = 0;
1854err_out:
1855 tipc_node_put(peer);
1856 spin_unlock_bh(&tn->node_list_lock);
1857
1858 return err;
1859}
1860
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001861int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1862{
1863 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001864 struct net *net = sock_net(skb->sk);
1865 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001866 int done = cb->args[0];
1867 int last_addr = cb->args[1];
1868 struct tipc_node *node;
1869 struct tipc_nl_msg msg;
1870
1871 if (done)
1872 return 0;
1873
1874 msg.skb = skb;
1875 msg.portid = NETLINK_CB(cb->skb).portid;
1876 msg.seq = cb->nlh->nlmsg_seq;
1877
1878 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001879 if (last_addr) {
1880 node = tipc_node_find(net, last_addr);
1881 if (!node) {
1882 rcu_read_unlock();
1883 /* We never set seq or call nl_dump_check_consistent()
1884 * this means that setting prev_seq here will cause the
1885 * consistence check to fail in the netlink callback
1886 * handler. Resulting in the NLMSG_DONE message having
1887 * the NLM_F_DUMP_INTR flag set if the node state
1888 * changed while we released the lock.
1889 */
1890 cb->prev_seq = 1;
1891 return -EPIPE;
1892 }
1893 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001894 }
1895
Ying Xuef2f98002015-01-09 15:27:05 +08001896 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001897 if (last_addr) {
1898 if (node->addr == last_addr)
1899 last_addr = 0;
1900 else
1901 continue;
1902 }
1903
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001904 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001905 err = __tipc_nl_add_node(&msg, node);
1906 if (err) {
1907 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001908 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001909 goto out;
1910 }
1911
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001912 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001913 }
1914 done = 1;
1915out:
1916 cb->args[0] = done;
1917 cb->args[1] = last_addr;
1918 rcu_read_unlock();
1919
1920 return skb->len;
1921}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001922
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001923/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001924 * @net: the applicable net namespace
1925 * @name: pointer to link name string
1926 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1927 *
1928 * Returns pointer to node owning the link, or 0 if no matching link is found.
1929 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001930static struct tipc_node *tipc_node_find_by_name(struct net *net,
1931 const char *link_name,
1932 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001933{
1934 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001935 struct tipc_link *l;
1936 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001937 struct tipc_node *found_node = NULL;
1938 int i;
1939
1940 *bearer_id = 0;
1941 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001942 list_for_each_entry_rcu(n, &tn->node_list, list) {
1943 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001944 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001945 l = n->links[i].link;
1946 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001947 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001948 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001949 break;
1950 }
1951 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001952 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001953 if (found_node)
1954 break;
1955 }
1956 rcu_read_unlock();
1957
1958 return found_node;
1959}
1960
1961int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1962{
1963 int err;
1964 int res = 0;
1965 int bearer_id;
1966 char *name;
1967 struct tipc_link *link;
1968 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01001969 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001970 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1971 struct net *net = sock_net(skb->sk);
1972
Richard Alped01332f2016-02-01 08:19:56 +01001973 __skb_queue_head_init(&xmitq);
1974
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001975 if (!info->attrs[TIPC_NLA_LINK])
1976 return -EINVAL;
1977
1978 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1979 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02001980 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001981 if (err)
1982 return err;
1983
1984 if (!attrs[TIPC_NLA_LINK_NAME])
1985 return -EINVAL;
1986
1987 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1988
1989 if (strcmp(name, tipc_bclink_name) == 0)
1990 return tipc_nl_bc_link_set(net, attrs);
1991
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001992 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001993 if (!node)
1994 return -EINVAL;
1995
1996 tipc_node_read_lock(node);
1997
1998 link = node->links[bearer_id].link;
1999 if (!link) {
2000 res = -EINVAL;
2001 goto out;
2002 }
2003
2004 if (attrs[TIPC_NLA_LINK_PROP]) {
2005 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2006
2007 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2008 props);
2009 if (err) {
2010 res = err;
2011 goto out;
2012 }
2013
2014 if (props[TIPC_NLA_PROP_TOL]) {
2015 u32 tol;
2016
2017 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002018 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002019 }
2020 if (props[TIPC_NLA_PROP_PRIO]) {
2021 u32 prio;
2022
2023 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002024 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002025 }
2026 if (props[TIPC_NLA_PROP_WIN]) {
2027 u32 win;
2028
2029 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2030 tipc_link_set_queue_limits(link, win);
2031 }
2032 }
2033
2034out:
2035 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01002036 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002037 return res;
2038}
2039
2040int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2041{
2042 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002043 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002044 struct tipc_nl_msg msg;
2045 char *name;
2046 int err;
2047
2048 msg.portid = info->snd_portid;
2049 msg.seq = info->snd_seq;
2050
Ying Xue94f6a802018-05-08 21:44:06 +08002051 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002052 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002053
2054 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2055 info->attrs[TIPC_NLA_LINK],
2056 tipc_nl_link_policy, info->extack);
2057 if (err)
2058 return err;
2059
2060 if (!attrs[TIPC_NLA_LINK_NAME])
2061 return -EINVAL;
2062
2063 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002064
2065 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2066 if (!msg.skb)
2067 return -ENOMEM;
2068
2069 if (strcmp(name, tipc_bclink_name) == 0) {
2070 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002071 if (err)
2072 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002073 } else {
2074 int bearer_id;
2075 struct tipc_node *node;
2076 struct tipc_link *link;
2077
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002078 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002079 if (!node) {
2080 err = -EINVAL;
2081 goto err_free;
2082 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002083
2084 tipc_node_read_lock(node);
2085 link = node->links[bearer_id].link;
2086 if (!link) {
2087 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002088 err = -EINVAL;
2089 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002090 }
2091
2092 err = __tipc_nl_add_link(net, &msg, link, 0);
2093 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002094 if (err)
2095 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002096 }
2097
2098 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002099
2100err_free:
2101 nlmsg_free(msg.skb);
2102 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002103}
2104
2105int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2106{
2107 int err;
2108 char *link_name;
2109 unsigned int bearer_id;
2110 struct tipc_link *link;
2111 struct tipc_node *node;
2112 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2113 struct net *net = sock_net(skb->sk);
2114 struct tipc_link_entry *le;
2115
2116 if (!info->attrs[TIPC_NLA_LINK])
2117 return -EINVAL;
2118
2119 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2120 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02002121 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002122 if (err)
2123 return err;
2124
2125 if (!attrs[TIPC_NLA_LINK_NAME])
2126 return -EINVAL;
2127
2128 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2129
2130 if (strcmp(link_name, tipc_bclink_name) == 0) {
2131 err = tipc_bclink_reset_stats(net);
2132 if (err)
2133 return err;
2134 return 0;
2135 }
2136
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002137 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002138 if (!node)
2139 return -EINVAL;
2140
2141 le = &node->links[bearer_id];
2142 tipc_node_read_lock(node);
2143 spin_lock_bh(&le->lock);
2144 link = node->links[bearer_id].link;
2145 if (!link) {
2146 spin_unlock_bh(&le->lock);
2147 tipc_node_read_unlock(node);
2148 return -EINVAL;
2149 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002150 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002151 spin_unlock_bh(&le->lock);
2152 tipc_node_read_unlock(node);
2153 return 0;
2154}
2155
2156/* Caller should hold node lock */
2157static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2158 struct tipc_node *node, u32 *prev_link)
2159{
2160 u32 i;
2161 int err;
2162
2163 for (i = *prev_link; i < MAX_BEARERS; i++) {
2164 *prev_link = i;
2165
2166 if (!node->links[i].link)
2167 continue;
2168
2169 err = __tipc_nl_add_link(net, msg,
2170 node->links[i].link, NLM_F_MULTI);
2171 if (err)
2172 return err;
2173 }
2174 *prev_link = 0;
2175
2176 return 0;
2177}
2178
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002179int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002180{
2181 struct net *net = sock_net(skb->sk);
2182 struct tipc_net *tn = net_generic(net, tipc_net_id);
2183 struct tipc_node *node;
2184 struct tipc_nl_msg msg;
2185 u32 prev_node = cb->args[0];
2186 u32 prev_link = cb->args[1];
2187 int done = cb->args[2];
2188 int err;
2189
2190 if (done)
2191 return 0;
2192
2193 msg.skb = skb;
2194 msg.portid = NETLINK_CB(cb->skb).portid;
2195 msg.seq = cb->nlh->nlmsg_seq;
2196
2197 rcu_read_lock();
2198 if (prev_node) {
2199 node = tipc_node_find(net, prev_node);
2200 if (!node) {
2201 /* We never set seq or call nl_dump_check_consistent()
2202 * this means that setting prev_seq here will cause the
2203 * consistence check to fail in the netlink callback
2204 * handler. Resulting in the last NLMSG_DONE message
2205 * having the NLM_F_DUMP_INTR flag set.
2206 */
2207 cb->prev_seq = 1;
2208 goto out;
2209 }
2210 tipc_node_put(node);
2211
2212 list_for_each_entry_continue_rcu(node, &tn->node_list,
2213 list) {
2214 tipc_node_read_lock(node);
2215 err = __tipc_nl_add_node_links(net, &msg, node,
2216 &prev_link);
2217 tipc_node_read_unlock(node);
2218 if (err)
2219 goto out;
2220
2221 prev_node = node->addr;
2222 }
2223 } else {
2224 err = tipc_nl_add_bc_link(net, &msg);
2225 if (err)
2226 goto out;
2227
2228 list_for_each_entry_rcu(node, &tn->node_list, list) {
2229 tipc_node_read_lock(node);
2230 err = __tipc_nl_add_node_links(net, &msg, node,
2231 &prev_link);
2232 tipc_node_read_unlock(node);
2233 if (err)
2234 goto out;
2235
2236 prev_node = node->addr;
2237 }
2238 }
2239 done = 1;
2240out:
2241 rcu_read_unlock();
2242
2243 cb->args[0] = prev_node;
2244 cb->args[1] = prev_link;
2245 cb->args[2] = done;
2246
2247 return skb->len;
2248}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002249
2250int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2251{
2252 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2253 struct net *net = sock_net(skb->sk);
2254 int err;
2255
2256 if (!info->attrs[TIPC_NLA_MON])
2257 return -EINVAL;
2258
2259 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2260 info->attrs[TIPC_NLA_MON],
Johannes Bergfe521452017-04-12 14:34:08 +02002261 tipc_nl_monitor_policy, info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002262 if (err)
2263 return err;
2264
2265 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2266 u32 val;
2267
2268 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2269 err = tipc_nl_monitor_set_threshold(net, val);
2270 if (err)
2271 return err;
2272 }
2273
2274 return 0;
2275}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002276
2277static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2278{
2279 struct nlattr *attrs;
2280 void *hdr;
2281 u32 val;
2282
2283 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2284 0, TIPC_NL_MON_GET);
2285 if (!hdr)
2286 return -EMSGSIZE;
2287
2288 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2289 if (!attrs)
2290 goto msg_full;
2291
2292 val = tipc_nl_monitor_get_threshold(net);
2293
2294 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2295 goto attr_msg_full;
2296
2297 nla_nest_end(msg->skb, attrs);
2298 genlmsg_end(msg->skb, hdr);
2299
2300 return 0;
2301
2302attr_msg_full:
2303 nla_nest_cancel(msg->skb, attrs);
2304msg_full:
2305 genlmsg_cancel(msg->skb, hdr);
2306
2307 return -EMSGSIZE;
2308}
2309
2310int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2311{
2312 struct net *net = sock_net(skb->sk);
2313 struct tipc_nl_msg msg;
2314 int err;
2315
2316 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002317 if (!msg.skb)
2318 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002319 msg.portid = info->snd_portid;
2320 msg.seq = info->snd_seq;
2321
2322 err = __tipc_nl_add_monitor_prop(net, &msg);
2323 if (err) {
2324 nlmsg_free(msg.skb);
2325 return err;
2326 }
2327
2328 return genlmsg_reply(msg.skb, info);
2329}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002330
2331int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2332{
2333 struct net *net = sock_net(skb->sk);
2334 u32 prev_bearer = cb->args[0];
2335 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002336 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002337 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002338
2339 if (prev_bearer == MAX_BEARERS)
2340 return 0;
2341
2342 msg.skb = skb;
2343 msg.portid = NETLINK_CB(cb->skb).portid;
2344 msg.seq = cb->nlh->nlmsg_seq;
2345
2346 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002347 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002348 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002349 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002350 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002351 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002352 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002353 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002354
2355 return skb->len;
2356}
2357
2358int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2359 struct netlink_callback *cb)
2360{
2361 struct net *net = sock_net(skb->sk);
2362 u32 prev_node = cb->args[1];
2363 u32 bearer_id = cb->args[2];
2364 int done = cb->args[0];
2365 struct tipc_nl_msg msg;
2366 int err;
2367
2368 if (!prev_node) {
2369 struct nlattr **attrs;
2370 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2371
2372 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2373 if (err)
2374 return err;
2375
2376 if (!attrs[TIPC_NLA_MON])
2377 return -EINVAL;
2378
2379 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2380 attrs[TIPC_NLA_MON],
Johannes Bergfceb6432017-04-12 14:34:07 +02002381 tipc_nl_monitor_policy, NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002382 if (err)
2383 return err;
2384
2385 if (!mon[TIPC_NLA_MON_REF])
2386 return -EINVAL;
2387
2388 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2389
2390 if (bearer_id >= MAX_BEARERS)
2391 return -EINVAL;
2392 }
2393
2394 if (done)
2395 return 0;
2396
2397 msg.skb = skb;
2398 msg.portid = NETLINK_CB(cb->skb).portid;
2399 msg.seq = cb->nlh->nlmsg_seq;
2400
2401 rtnl_lock();
2402 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2403 if (!err)
2404 done = 1;
2405
2406 rtnl_unlock();
2407 cb->args[0] = done;
2408 cb->args[1] = prev_node;
2409 cb->args[2] = bearer_id;
2410
2411 return skb->len;
2412}