blob: cfdbaf479fd11dbedbcdc6e6223278ecbb161f07 [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 Maloy02be61a2014-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 Paul Maloy5405ff62015-11-19 14:30:44 -0500366 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367
Ying Xuef2f98002015-01-09 15:27:05 +0800368 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500369 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400370 if (n) {
371 /* Same node may come back with new capabilities */
372 n->capabilities = capabilities;
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500373 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400374 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500375 n = kzalloc(sizeof(*n), GFP_ATOMIC);
376 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400377 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500378 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700379 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500380 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100381 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500382 n->net = net;
383 n->capabilities = capabilities;
384 kref_init(&n->kref);
385 rwlock_init(&n->lock);
386 INIT_HLIST_NODE(&n->hash);
387 INIT_LIST_HEAD(&n->list);
388 INIT_LIST_HEAD(&n->publ_list);
389 INIT_LIST_HEAD(&n->conn_sks);
390 skb_queue_head_init(&n->bc_entry.namedq);
391 skb_queue_head_init(&n->bc_entry.inputq1);
392 __skb_queue_head_init(&n->bc_entry.arrvq);
393 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500394 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500395 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500396 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200397 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500398 n->signature = INVALID_NODE_SIG;
399 n->active_links[0] = INVALID_BEARER_ID;
400 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100401 if (!tipc_link_bc_create(net, tipc_own_addr(net),
402 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500403 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500404 n->capabilities,
405 &n->bc_entry.inputq1,
406 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400407 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500408 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400409 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500410 kfree(n);
411 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400412 goto exit;
413 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500414 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700415 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500416 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500417 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
418 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
419 if (n->addr < temp_node->addr)
420 break;
421 }
422 list_add_tail_rcu(&n->list, &temp_node->list);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500423exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800424 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500425 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426}
427
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400428static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
429{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500430 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400431 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400432
433 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400434 if (intv < n->keepalive_intv)
435 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400436
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400437 /* Ensure link's abort limit corresponds to current tolerance */
438 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400439}
440
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200441static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800443 list_del_rcu(&node->list);
444 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500445 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200446}
447
448static void tipc_node_delete(struct tipc_node *node)
449{
450 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500451
452 del_timer_sync(&node->timer);
453 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454}
455
Ying Xuef2f98002015-01-09 15:27:05 +0800456void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800457{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500458 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800459 struct tipc_node *node, *t_node;
460
Ying Xuef2f98002015-01-09 15:27:05 +0800461 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500462 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
463 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800464 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800465}
466
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500467void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
468{
469 struct tipc_node *n;
470
471 if (in_own_node(net, addr))
472 return;
473
474 n = tipc_node_find(net, addr);
475 if (!n) {
476 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
477 return;
478 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500479 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500480 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100481 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500482 tipc_node_put(n);
483}
484
485void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
486{
487 struct tipc_node *n;
488
489 if (in_own_node(net, addr))
490 return;
491
492 n = tipc_node_find(net, addr);
493 if (!n) {
494 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
495 return;
496 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500497 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500498 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100499 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500500 tipc_node_put(n);
501}
502
Ying Xuef2f98002015-01-09 15:27:05 +0800503int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400504{
505 struct tipc_node *node;
506 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800507 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400508
Ying Xue34747532015-01-09 15:27:10 +0800509 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400510 return 0;
511
Ying Xuef2f98002015-01-09 15:27:05 +0800512 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400513 if (!node) {
514 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
515 return -EHOSTUNREACH;
516 }
517 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800518 if (!conn) {
519 err = -EHOSTUNREACH;
520 goto exit;
521 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400522 conn->peer_node = dnode;
523 conn->port = port;
524 conn->peer_port = peer_port;
525
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500526 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400527 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500528 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800529exit:
530 tipc_node_put(node);
531 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400532}
533
Ying Xuef2f98002015-01-09 15:27:05 +0800534void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400535{
536 struct tipc_node *node;
537 struct tipc_sock_conn *conn, *safe;
538
Ying Xue34747532015-01-09 15:27:10 +0800539 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400540 return;
541
Ying Xuef2f98002015-01-09 15:27:05 +0800542 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400543 if (!node)
544 return;
545
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500546 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400547 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
548 if (port != conn->port)
549 continue;
550 list_del(&conn->list);
551 kfree(conn);
552 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500553 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800554 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400555}
556
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200557static void tipc_node_clear_links(struct tipc_node *node)
558{
559 int i;
560
561 for (i = 0; i < MAX_BEARERS; i++) {
562 struct tipc_link_entry *le = &node->links[i];
563
564 if (le->link) {
565 kfree(le->link);
566 le->link = NULL;
567 node->link_cnt--;
568 }
569 }
570}
571
572/* tipc_node_cleanup - delete nodes that does not
573 * have active links for NODE_CLEANUP_AFTER time
574 */
575static int tipc_node_cleanup(struct tipc_node *peer)
576{
577 struct tipc_net *tn = tipc_net(peer->net);
578 bool deleted = false;
579
580 spin_lock_bh(&tn->node_list_lock);
581 tipc_node_write_lock(peer);
582
583 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
584 tipc_node_clear_links(peer);
585 tipc_node_delete_from_list(peer);
586 deleted = true;
587 }
588 tipc_node_write_unlock(peer);
589 spin_unlock_bh(&tn->node_list_lock);
590 return deleted;
591}
592
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400593/* tipc_node_timeout - handle expiration of node timer
594 */
Kees Cook31b102b2017-10-30 14:06:45 -0700595static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400596{
Kees Cook31b102b2017-10-30 14:06:45 -0700597 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400598 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400599 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200600 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400601 int bearer_id;
602 int rc = 0;
603
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200604 if (!node_is_up(n) && tipc_node_cleanup(n)) {
605 /*Removing the reference of Timer*/
606 tipc_node_put(n);
607 return;
608 }
609
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400610 __skb_queue_head_init(&xmitq);
611
Tung Nguyen759f29b2018-06-28 22:39:25 +0200612 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500613 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400614 le = &n->links[bearer_id];
615 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200616 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400617 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400618 tipc_node_calculate_timer(n, le->link);
619 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200620 spin_unlock_bh(&le->lock);
621 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400622 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500623 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400624 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
625 if (rc & TIPC_LINK_DOWN_EVT)
626 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400627 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400628 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400629}
630
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400632 * __tipc_node_link_up - handle addition of link
633 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 * Link becomes active (alone or shared) or standby, depending on its priority.
635 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400636static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
637 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400639 int *slot0 = &n->active_links[0];
640 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400641 struct tipc_link *ol = node_active_link(n, 0);
642 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400644 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400645 return;
646
647 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
648 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400649 return;
650
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400651 n->working_links++;
652 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500653 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400654
655 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500656 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800657
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400658 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400659 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400660
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100661 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500662 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400664 /* Ensure that a STATE message goes first */
665 tipc_link_build_state_msg(nl, xmitq);
666
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400667 /* First link? => give it both slots */
668 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400669 *slot0 = bearer_id;
670 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400671 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
672 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400673 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400674 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400675 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400677
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400678 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500679 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
680 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400681 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400682 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400683 tipc_link_set_active(nl, true);
684 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500685 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400686 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a842015-10-22 08:51:47 -0400687 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400688 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500689 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400692 /* Prepare synchronization with first link */
693 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694}
695
696/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400697 * tipc_node_link_up - handle addition of link
698 *
699 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400701static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
702 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400704 struct tipc_media_addr *maddr;
705
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500706 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400707 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400708 maddr = &n->links[bearer_id].maddr;
709 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500710 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400711}
712
713/**
714 * __tipc_node_link_down - handle loss of link
715 */
716static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
717 struct sk_buff_head *xmitq,
718 struct tipc_media_addr **maddr)
719{
720 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400721 int *slot0 = &n->active_links[0];
722 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500723 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400724 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400726 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400727 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400728 return;
729
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400730 n->working_links--;
731 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500732 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700733
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400734 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400735
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100736 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500737 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400739 /* Select new active link if any available */
740 *slot0 = INVALID_BEARER_ID;
741 *slot1 = INVALID_BEARER_ID;
742 for (i = 0; i < MAX_BEARERS; i++) {
743 _l = n->links[i].link;
744 if (!_l || !tipc_link_is_up(_l))
745 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400746 if (_l == l)
747 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500748 prio = tipc_link_prio(_l);
749 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400750 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500751 if (prio > highest) {
752 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400753 *slot0 = i;
754 *slot1 = i;
755 continue;
756 }
757 *slot1 = i;
758 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400759
Jon Maloy38077b82017-10-13 11:04:19 +0200760 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400761 if (tipc_link_peer_is_down(l))
762 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
763 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
764 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400765 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400766 tipc_link_build_reset_msg(l, xmitq);
767 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400768 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400769 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400770 return;
771 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400772 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400773
774 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500775 *bearer_id = n->active_links[0];
776 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400777 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
778 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500779 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400780 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400781 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400782 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400783 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400784 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500785 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400786}
787
788static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
789{
790 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400791 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400792 struct tipc_media_addr *maddr;
793 struct sk_buff_head xmitq;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400794 int old_bearer_id = bearer_id;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400795
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400796 if (!l)
797 return;
798
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400799 __skb_queue_head_init(&xmitq);
800
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500801 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400802 if (!tipc_link_is_establishing(l)) {
803 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
804 if (delete) {
805 kfree(l);
806 le->link = NULL;
807 n->link_cnt--;
808 }
809 } else {
810 /* Defuse pending tipc_node_link_up() */
811 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400812 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500813 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400814 if (delete)
815 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400816 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
817 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818}
819
Jon Maloy38077b82017-10-13 11:04:19 +0200820static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400822 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823}
824
Jon Maloy38077b82017-10-13 11:04:19 +0200825bool tipc_node_is_up(struct net *net, u32 addr)
826{
827 struct tipc_node *n;
828 bool retval = false;
829
830 if (in_own_node(net, addr))
831 return true;
832
833 n = tipc_node_find(net, addr);
834 if (!n)
835 return false;
836 retval = node_is_up(n);
837 tipc_node_put(n);
838 return retval;
839}
840
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100841static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
842{
843 struct tipc_node *n;
844
845 addr ^= tipc_net(net)->random;
846 while ((n = tipc_node_find(net, addr))) {
847 tipc_node_put(n);
848 addr++;
849 }
850 return addr;
851}
852
853/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
854 */
855u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
856{
857 struct tipc_net *tn = tipc_net(net);
858 struct tipc_node *n;
859
860 /* Suggest new address if some other peer is using this one */
861 n = tipc_node_find(net, addr);
862 if (n) {
863 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
864 addr = 0;
865 tipc_node_put(n);
866 if (!addr)
867 return 0;
868 return tipc_node_suggest_addr(net, addr);
869 }
870
871 /* Suggest previously used address if peer is known */
872 n = tipc_node_find_by_id(net, id);
873 if (n) {
874 addr = n->addr;
875 tipc_node_put(n);
876 }
877 /* Even this node may be in trial phase */
878 if (tn->trial_addr == addr)
879 return tipc_node_suggest_addr(net, addr);
880
881 return addr;
882}
883
884void tipc_node_check_dest(struct net *net, u32 addr,
885 u8 *peer_id, struct tipc_bearer *b,
Jon Paul Maloycf148812015-07-30 18:24:22 -0400886 u16 capabilities, u32 signature,
887 struct tipc_media_addr *maddr,
888 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400889{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400890 struct tipc_node *n;
891 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400892 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400893 bool addr_match = false;
894 bool sign_match = false;
895 bool link_up = false;
896 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400897 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400898 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400899 unsigned long intv;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400900
Jon Paul Maloycf148812015-07-30 18:24:22 -0400901 *dupl_addr = false;
902 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400903
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100904 n = tipc_node_create(net, addr, peer_id, capabilities);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400905 if (!n)
906 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400907
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500908 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400909
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400910 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400911
912 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400913 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400914 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400915 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400916 sign_match = (signature == n->signature);
917
918 /* These three flags give us eight permutations: */
919
920 if (sign_match && addr_match && link_up) {
921 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400922 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400923 } else if (sign_match && addr_match && !link_up) {
924 /* Respond. The link will come up in due time */
925 *respond = true;
926 } else if (sign_match && !addr_match && link_up) {
927 /* Peer has changed i/f address without rebooting.
928 * If so, the link will reset soon, and the next
929 * discovery will be accepted. So we can ignore it.
930 * It may also be an cloned or malicious peer having
931 * chosen the same node address and signature as an
932 * existing one.
933 * Ignore requests until the link goes down, if ever.
934 */
935 *dupl_addr = true;
936 } else if (sign_match && !addr_match && !link_up) {
937 /* Peer link has changed i/f address without rebooting.
938 * It may also be a cloned or malicious peer; we can't
939 * distinguish between the two.
940 * The signature is correct, so we must accept.
941 */
942 accept_addr = true;
943 *respond = true;
944 } else if (!sign_match && addr_match && link_up) {
945 /* Peer node rebooted. Two possibilities:
946 * - Delayed re-discovery; this link endpoint has already
947 * reset and re-established contact with the peer, before
948 * receiving a discovery message from that node.
949 * (The peer happened to receive one from this node first).
950 * - The peer came back so fast that our side has not
951 * discovered it yet. Probing from this side will soon
952 * reset the link, since there can be no working link
953 * endpoint at the peer end, and the link will re-establish.
954 * Accept the signature, since it comes from a known peer.
955 */
956 n->signature = signature;
957 } else if (!sign_match && addr_match && !link_up) {
958 /* The peer node has rebooted.
959 * Accept signature, since it is a known peer.
960 */
961 n->signature = signature;
962 *respond = true;
963 } else if (!sign_match && !addr_match && link_up) {
964 /* Peer rebooted with new address, or a new/duplicate peer.
965 * Ignore until the link goes down, if ever.
966 */
967 *dupl_addr = true;
968 } else if (!sign_match && !addr_match && !link_up) {
969 /* Peer rebooted with new address, or it is a new peer.
970 * Accept signature and address.
971 */
972 n->signature = signature;
973 accept_addr = true;
974 *respond = true;
975 }
976
977 if (!accept_addr)
978 goto exit;
979
980 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400981 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +0100982 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400983 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +0100984
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400985 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400986 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400987 b->net_plane, b->mtu, b->priority,
988 b->window, mod(tipc_net(net)->random),
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100989 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400990 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400991 tipc_bc_sndlink(n->net), n->bc_entry.link,
992 &le->inputq,
993 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -0400994 *respond = false;
995 goto exit;
996 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400997 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400998 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -0400999 if (n->state == NODE_FAILINGOVER)
1000 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001001 le->link = l;
1002 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001003 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001004 if (n->link_cnt == 1) {
1005 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1006 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001007 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001008 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001009 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001010 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001011exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001012 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001013 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001014 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001015 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001016}
1017
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001018void tipc_node_delete_links(struct net *net, int bearer_id)
1019{
1020 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001021 struct tipc_node *n;
1022
1023 rcu_read_lock();
1024 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001025 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001026 }
1027 rcu_read_unlock();
1028}
1029
1030static void tipc_node_reset_links(struct tipc_node *n)
1031{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001032 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001033
Jon Maloyd50ccc22018-03-22 20:42:50 +01001034 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001035
1036 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001037 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001038 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001039}
1040
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001041/* tipc_node_fsm_evt - node finite state machine
1042 * Determines when contact is allowed with peer node
1043 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001044static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001045{
1046 int state = n->state;
1047
1048 switch (state) {
1049 case SELF_DOWN_PEER_DOWN:
1050 switch (evt) {
1051 case SELF_ESTABL_CONTACT_EVT:
1052 state = SELF_UP_PEER_COMING;
1053 break;
1054 case PEER_ESTABL_CONTACT_EVT:
1055 state = SELF_COMING_PEER_UP;
1056 break;
1057 case SELF_LOST_CONTACT_EVT:
1058 case PEER_LOST_CONTACT_EVT:
1059 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001060 case NODE_SYNCH_END_EVT:
1061 case NODE_SYNCH_BEGIN_EVT:
1062 case NODE_FAILOVER_BEGIN_EVT:
1063 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001064 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001065 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001066 }
1067 break;
1068 case SELF_UP_PEER_UP:
1069 switch (evt) {
1070 case SELF_LOST_CONTACT_EVT:
1071 state = SELF_DOWN_PEER_LEAVING;
1072 break;
1073 case PEER_LOST_CONTACT_EVT:
1074 state = SELF_LEAVING_PEER_DOWN;
1075 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001076 case NODE_SYNCH_BEGIN_EVT:
1077 state = NODE_SYNCHING;
1078 break;
1079 case NODE_FAILOVER_BEGIN_EVT:
1080 state = NODE_FAILINGOVER;
1081 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001082 case SELF_ESTABL_CONTACT_EVT:
1083 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001084 case NODE_SYNCH_END_EVT:
1085 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001086 break;
1087 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001088 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001089 }
1090 break;
1091 case SELF_DOWN_PEER_LEAVING:
1092 switch (evt) {
1093 case PEER_LOST_CONTACT_EVT:
1094 state = SELF_DOWN_PEER_DOWN;
1095 break;
1096 case SELF_ESTABL_CONTACT_EVT:
1097 case PEER_ESTABL_CONTACT_EVT:
1098 case SELF_LOST_CONTACT_EVT:
1099 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001100 case NODE_SYNCH_END_EVT:
1101 case NODE_SYNCH_BEGIN_EVT:
1102 case NODE_FAILOVER_BEGIN_EVT:
1103 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001104 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001105 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001106 }
1107 break;
1108 case SELF_UP_PEER_COMING:
1109 switch (evt) {
1110 case PEER_ESTABL_CONTACT_EVT:
1111 state = SELF_UP_PEER_UP;
1112 break;
1113 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001114 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001115 break;
1116 case SELF_ESTABL_CONTACT_EVT:
1117 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001118 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001119 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001120 break;
1121 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001122 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001123 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001124 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001125 }
1126 break;
1127 case SELF_COMING_PEER_UP:
1128 switch (evt) {
1129 case SELF_ESTABL_CONTACT_EVT:
1130 state = SELF_UP_PEER_UP;
1131 break;
1132 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001133 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001134 break;
1135 case SELF_LOST_CONTACT_EVT:
1136 case PEER_ESTABL_CONTACT_EVT:
1137 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001138 case NODE_SYNCH_END_EVT:
1139 case NODE_SYNCH_BEGIN_EVT:
1140 case NODE_FAILOVER_BEGIN_EVT:
1141 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001142 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001143 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001144 }
1145 break;
1146 case SELF_LEAVING_PEER_DOWN:
1147 switch (evt) {
1148 case SELF_LOST_CONTACT_EVT:
1149 state = SELF_DOWN_PEER_DOWN;
1150 break;
1151 case SELF_ESTABL_CONTACT_EVT:
1152 case PEER_ESTABL_CONTACT_EVT:
1153 case PEER_LOST_CONTACT_EVT:
1154 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001155 case NODE_SYNCH_END_EVT:
1156 case NODE_SYNCH_BEGIN_EVT:
1157 case NODE_FAILOVER_BEGIN_EVT:
1158 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001159 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001160 goto illegal_evt;
1161 }
1162 break;
1163 case NODE_FAILINGOVER:
1164 switch (evt) {
1165 case SELF_LOST_CONTACT_EVT:
1166 state = SELF_DOWN_PEER_LEAVING;
1167 break;
1168 case PEER_LOST_CONTACT_EVT:
1169 state = SELF_LEAVING_PEER_DOWN;
1170 break;
1171 case NODE_FAILOVER_END_EVT:
1172 state = SELF_UP_PEER_UP;
1173 break;
1174 case NODE_FAILOVER_BEGIN_EVT:
1175 case SELF_ESTABL_CONTACT_EVT:
1176 case PEER_ESTABL_CONTACT_EVT:
1177 break;
1178 case NODE_SYNCH_BEGIN_EVT:
1179 case NODE_SYNCH_END_EVT:
1180 default:
1181 goto illegal_evt;
1182 }
1183 break;
1184 case NODE_SYNCHING:
1185 switch (evt) {
1186 case SELF_LOST_CONTACT_EVT:
1187 state = SELF_DOWN_PEER_LEAVING;
1188 break;
1189 case PEER_LOST_CONTACT_EVT:
1190 state = SELF_LEAVING_PEER_DOWN;
1191 break;
1192 case NODE_SYNCH_END_EVT:
1193 state = SELF_UP_PEER_UP;
1194 break;
1195 case NODE_FAILOVER_BEGIN_EVT:
1196 state = NODE_FAILINGOVER;
1197 break;
1198 case NODE_SYNCH_BEGIN_EVT:
1199 case SELF_ESTABL_CONTACT_EVT:
1200 case PEER_ESTABL_CONTACT_EVT:
1201 break;
1202 case NODE_FAILOVER_END_EVT:
1203 default:
1204 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001205 }
1206 break;
1207 default:
1208 pr_err("Unknown node fsm state %x\n", state);
1209 break;
1210 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001211 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001212 return;
1213
1214illegal_evt:
1215 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001216}
1217
Jon Paul Maloy52666982015-10-22 08:51:41 -04001218static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001219 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001221 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001222 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001223 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001224 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001225 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226
Jon Maloyd50ccc22018-03-22 20:42:50 +01001227 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001228 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001229
Jon Paul Maloy52666982015-10-22 08:51:41 -04001230 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001231 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001233 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001235 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001236 if (l)
1237 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001239
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001240 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001241 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001242
1243 /* Notify sockets connected to node */
1244 list_for_each_entry_safe(conn, safe, conns, list) {
1245 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001246 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001247 conn->peer_node, conn->port,
1248 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001249 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001250 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001251 list_del(&conn->list);
1252 kfree(conn);
1253 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254}
1255
Erik Hugne78acb1f2014-04-24 16:26:47 +02001256/**
1257 * tipc_node_get_linkname - get the name of a link
1258 *
1259 * @bearer_id: id of the bearer
1260 * @node: peer node address
1261 * @linkname: link name output buffer
1262 *
1263 * Returns 0 on success
1264 */
Ying Xuef2f98002015-01-09 15:27:05 +08001265int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1266 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001267{
1268 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001269 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001270 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001271
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001272 if (!node)
1273 return err;
1274
1275 if (bearer_id >= MAX_BEARERS)
1276 goto exit;
1277
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001278 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001279 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001280 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001281 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001282 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001283 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001284 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001285exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001286 tipc_node_put(node);
1287 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001288}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001289
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001290/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001291static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001292{
1293 void *hdr;
1294 struct nlattr *attrs;
1295
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001296 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001297 NLM_F_MULTI, TIPC_NL_NODE_GET);
1298 if (!hdr)
1299 return -EMSGSIZE;
1300
1301 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1302 if (!attrs)
1303 goto msg_full;
1304
1305 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1306 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001307 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001308 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1309 goto attr_msg_full;
1310
1311 nla_nest_end(msg->skb, attrs);
1312 genlmsg_end(msg->skb, hdr);
1313
1314 return 0;
1315
1316attr_msg_full:
1317 nla_nest_cancel(msg->skb, attrs);
1318msg_full:
1319 genlmsg_cancel(msg->skb, hdr);
1320
1321 return -EMSGSIZE;
1322}
1323
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001324/**
1325 * tipc_node_xmit() is the general link level function for message sending
1326 * @net: the applicable net namespace
1327 * @list: chain of buffers containing message
1328 * @dnode: address of destination node
1329 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001330 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001331 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001332 */
1333int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1334 u32 dnode, int selector)
1335{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001336 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001337 struct tipc_node *n;
1338 struct sk_buff_head xmitq;
Richard Alpe4952cd32016-02-11 10:43:15 +01001339 int bearer_id;
1340 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001341
Richard Alpe4952cd32016-02-11 10:43:15 +01001342 if (in_own_node(net, dnode)) {
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001343 tipc_sk_rcv(net, list);
1344 return 0;
1345 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001346
1347 n = tipc_node_find(net, dnode);
1348 if (unlikely(!n)) {
1349 skb_queue_purge(list);
1350 return -EHOSTUNREACH;
1351 }
1352
1353 tipc_node_read_lock(n);
1354 bearer_id = n->active_links[selector & 1];
1355 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1356 tipc_node_read_unlock(n);
1357 tipc_node_put(n);
1358 skb_queue_purge(list);
1359 return -EHOSTUNREACH;
1360 }
1361
1362 __skb_queue_head_init(&xmitq);
1363 le = &n->links[bearer_id];
1364 spin_lock_bh(&le->lock);
1365 rc = tipc_link_xmit(le->link, list, &xmitq);
1366 spin_unlock_bh(&le->lock);
1367 tipc_node_read_unlock(n);
1368
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001369 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001370 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001371 else
1372 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001373
1374 tipc_node_put(n);
1375
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001376 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001377}
1378
1379/* tipc_node_xmit_skb(): send single buffer to destination
1380 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1381 * messages, which will not be rejected
1382 * The only exception is datagram messages rerouted after secondary
1383 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001384 */
1385int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1386 u32 selector)
1387{
1388 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001389
1390 skb_queue_head_init(&head);
1391 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001392 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001393 return 0;
1394}
1395
Jon Maloyf70d37b2017-10-13 11:04:21 +02001396/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1397 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1398 */
1399int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1400{
1401 struct sk_buff *skb;
1402 u32 selector, dnode;
1403
1404 while ((skb = __skb_dequeue(xmitq))) {
1405 selector = msg_origport(buf_msg(skb));
1406 dnode = msg_destnode(buf_msg(skb));
1407 tipc_node_xmit_skb(net, skb, dnode, selector);
1408 }
1409 return 0;
1410}
1411
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001412void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1413{
1414 struct sk_buff *txskb;
1415 struct tipc_node *n;
1416 u32 dst;
1417
1418 rcu_read_lock();
1419 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1420 dst = n->addr;
1421 if (in_own_node(net, dst))
1422 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001423 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001424 continue;
1425 txskb = pskb_copy(skb, GFP_ATOMIC);
1426 if (!txskb)
1427 break;
1428 msg_set_destnode(buf_msg(txskb), dst);
1429 tipc_node_xmit_skb(net, txskb, dst, 0);
1430 }
1431 rcu_read_unlock();
1432
1433 kfree_skb(skb);
1434}
1435
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001436static void tipc_node_mcast_rcv(struct tipc_node *n)
1437{
1438 struct tipc_bclink_entry *be = &n->bc_entry;
1439
1440 /* 'arrvq' is under inputq2's lock protection */
1441 spin_lock_bh(&be->inputq2.lock);
1442 spin_lock_bh(&be->inputq1.lock);
1443 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1444 spin_unlock_bh(&be->inputq1.lock);
1445 spin_unlock_bh(&be->inputq2.lock);
1446 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1447}
1448
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001449static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1450 int bearer_id, struct sk_buff_head *xmitq)
1451{
1452 struct tipc_link *ucl;
1453 int rc;
1454
1455 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1456
1457 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001458 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001459 return;
1460 }
1461
1462 if (!(rc & TIPC_LINK_SND_STATE))
1463 return;
1464
1465 /* If probe message, a STATE response will be sent anyway */
1466 if (msg_probe(hdr))
1467 return;
1468
1469 /* Produce a STATE message carrying broadcast NACK */
1470 tipc_node_read_lock(n);
1471 ucl = n->links[bearer_id].link;
1472 if (ucl)
1473 tipc_link_build_state_msg(ucl, xmitq);
1474 tipc_node_read_unlock(n);
1475}
1476
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001477/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001478 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1479 * @net: the applicable net namespace
1480 * @skb: TIPC packet
1481 * @bearer_id: id of bearer message arrived on
1482 *
1483 * Invoked with no locks held.
1484 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001485static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001486{
1487 int rc;
1488 struct sk_buff_head xmitq;
1489 struct tipc_bclink_entry *be;
1490 struct tipc_link_entry *le;
1491 struct tipc_msg *hdr = buf_msg(skb);
1492 int usr = msg_user(hdr);
1493 u32 dnode = msg_destnode(hdr);
1494 struct tipc_node *n;
1495
1496 __skb_queue_head_init(&xmitq);
1497
1498 /* If NACK for other node, let rcv link for that node peek into it */
1499 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1500 n = tipc_node_find(net, dnode);
1501 else
1502 n = tipc_node_find(net, msg_prevnode(hdr));
1503 if (!n) {
1504 kfree_skb(skb);
1505 return;
1506 }
1507 be = &n->bc_entry;
1508 le = &n->links[bearer_id];
1509
1510 rc = tipc_bcast_rcv(net, be->link, skb);
1511
Jon Paul Maloy52666982015-10-22 08:51:41 -04001512 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001513 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001514 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001515 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001516 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001517 }
1518
1519 if (!skb_queue_empty(&xmitq))
1520 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1521
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001522 if (!skb_queue_empty(&be->inputq1))
1523 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001524
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001525 /* If reassembly or retransmission failure => reset all links to peer */
1526 if (rc & TIPC_LINK_DOWN_EVT)
1527 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001528
Jon Paul Maloy52666982015-10-22 08:51:41 -04001529 tipc_node_put(n);
1530}
1531
1532/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001533 * tipc_node_check_state - check and if necessary update node state
1534 * @skb: TIPC packet
1535 * @bearer_id: identity of bearer delivering the packet
1536 * Returns true if state is ok, otherwise consumes buffer and returns false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001537 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001538static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001539 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001540{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001541 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001542 int usr = msg_user(hdr);
1543 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001544 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001545 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1546 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001547 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001548 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001549 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001550 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001551 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001552
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001553 l = n->links[bearer_id].link;
1554 if (!l)
1555 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001556 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001557
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001558
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001559 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1560 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001561
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001562 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001563 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1564 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1565 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001566 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001567 }
1568 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001569
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001570 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001571 if (state == SELF_UP_PEER_COMING) {
1572 if (!tipc_link_is_up(l))
1573 return true;
1574 if (!msg_peer_link_is_up(hdr))
1575 return true;
1576 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1577 }
1578
1579 if (state == SELF_DOWN_PEER_LEAVING) {
1580 if (msg_peer_node_is_up(hdr))
1581 return false;
1582 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001583 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001584 }
1585
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001586 if (state == SELF_LEAVING_PEER_DOWN)
1587 return false;
1588
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001589 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001590 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001591 return true;
1592
1593 /* Initiate or update failover mode if applicable */
1594 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1595 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001596 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001597 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001598 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1599 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001600 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001601 /* If pkts arrive out of order, use lowest calculated syncpt */
1602 if (less(syncpt, n->sync_point))
1603 n->sync_point = syncpt;
1604 }
1605
1606 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001607 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001608 if (!more(rcv_nxt, n->sync_point))
1609 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001610 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1611 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001612 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001613 return true;
1614 }
1615
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001616 /* No synching needed if only one link */
1617 if (!pl || !tipc_link_is_up(pl))
1618 return true;
1619
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001620 /* Initiate synch mode if applicable */
1621 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001622 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001623 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001624 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001625 if (n->state == SELF_UP_PEER_UP) {
1626 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001627 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001628 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1629 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001630 }
1631
1632 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001633 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001634 if (tipc_link_is_synching(l)) {
1635 tnl = l;
1636 } else {
1637 tnl = pl;
1638 pl = l;
1639 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001640 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1641 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001642 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001643 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001644 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001645 return true;
1646 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001647 if (l == pl)
1648 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001649 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1650 return true;
1651 if (usr == LINK_PROTOCOL)
1652 return true;
1653 return false;
1654 }
1655 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001656}
1657
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001658/**
1659 * tipc_rcv - process TIPC packets/messages arriving from off-node
1660 * @net: the applicable net namespace
1661 * @skb: TIPC packet
1662 * @bearer: pointer to bearer message arrived on
1663 *
1664 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1665 * structure (i.e. cannot be NULL), but bearer can be inactive.
1666 */
1667void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1668{
1669 struct sk_buff_head xmitq;
1670 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001671 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001672 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001673 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001674 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001675 int usr, rc = 0;
1676 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001677
1678 __skb_queue_head_init(&xmitq);
1679
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001680 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01001681 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001682 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001683 hdr = buf_msg(skb);
1684 usr = msg_user(hdr);
1685 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001686
Jon Paul Maloy52666982015-10-22 08:51:41 -04001687 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001688 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001689 if (unlikely(usr == LINK_CONFIG))
1690 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001691 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001692 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001693 }
1694
Hamish Martinefe79052016-04-29 10:40:24 -04001695 /* Discard unicast link messages destined for another node */
1696 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1697 goto discard;
1698
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001699 /* Locate neighboring node that sent packet */
1700 n = tipc_node_find(net, msg_prevnode(hdr));
1701 if (unlikely(!n))
1702 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001703 le = &n->links[bearer_id];
1704
Jon Paul Maloy52666982015-10-22 08:51:41 -04001705 /* Ensure broadcast reception is in synch with peer's send state */
1706 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001707 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001708 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001709 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001710
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001711 /* Receive packet directly if conditions permit */
1712 tipc_node_read_lock(n);
1713 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001714 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001715 if (le->link) {
1716 rc = tipc_link_rcv(le->link, skb, &xmitq);
1717 skb = NULL;
1718 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001719 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001720 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001721 tipc_node_read_unlock(n);
1722
1723 /* Check/update node state before receiving */
1724 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001725 if (unlikely(skb_linearize(skb)))
1726 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001727 tipc_node_write_lock(n);
1728 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1729 if (le->link) {
1730 rc = tipc_link_rcv(le->link, skb, &xmitq);
1731 skb = NULL;
1732 }
1733 }
1734 tipc_node_write_unlock(n);
1735 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001736
1737 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001738 tipc_node_link_up(n, bearer_id, &xmitq);
1739
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001740 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001741 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001742
Jon Paul Maloy52666982015-10-22 08:51:41 -04001743 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1744 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001745
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001746 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1747 tipc_node_mcast_rcv(n);
1748
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001749 if (!skb_queue_empty(&le->inputq))
1750 tipc_sk_rcv(net, &le->inputq);
1751
1752 if (!skb_queue_empty(&xmitq))
1753 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1754
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001755 tipc_node_put(n);
1756discard:
1757 kfree_skb(skb);
1758}
1759
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001760void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1761 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01001762{
1763 struct tipc_net *tn = tipc_net(net);
1764 int bearer_id = b->identity;
1765 struct sk_buff_head xmitq;
1766 struct tipc_link_entry *e;
1767 struct tipc_node *n;
1768
1769 __skb_queue_head_init(&xmitq);
1770
1771 rcu_read_lock();
1772
1773 list_for_each_entry_rcu(n, &tn->node_list, list) {
1774 tipc_node_write_lock(n);
1775 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001776 if (e->link) {
1777 if (prop == TIPC_NLA_PROP_TOL)
1778 tipc_link_set_tolerance(e->link, b->tolerance,
1779 &xmitq);
1780 else if (prop == TIPC_NLA_PROP_MTU)
1781 tipc_link_set_mtu(e->link, b->mtu);
1782 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01001783 tipc_node_write_unlock(n);
1784 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1785 }
1786
1787 rcu_read_unlock();
1788}
1789
Richard Alpeb3404022016-08-18 10:33:52 +02001790int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1791{
1792 struct net *net = sock_net(skb->sk);
1793 struct tipc_net *tn = net_generic(net, tipc_net_id);
1794 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1795 struct tipc_node *peer;
1796 u32 addr;
1797 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02001798
1799 /* We identify the peer by its net */
1800 if (!info->attrs[TIPC_NLA_NET])
1801 return -EINVAL;
1802
1803 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001804 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
Johannes Bergfe521452017-04-12 14:34:08 +02001805 info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02001806 if (err)
1807 return err;
1808
1809 if (!attrs[TIPC_NLA_NET_ADDR])
1810 return -EINVAL;
1811
1812 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1813
1814 if (in_own_node(net, addr))
1815 return -ENOTSUPP;
1816
1817 spin_lock_bh(&tn->node_list_lock);
1818 peer = tipc_node_find(net, addr);
1819 if (!peer) {
1820 spin_unlock_bh(&tn->node_list_lock);
1821 return -ENXIO;
1822 }
1823
1824 tipc_node_write_lock(peer);
1825 if (peer->state != SELF_DOWN_PEER_DOWN &&
1826 peer->state != SELF_DOWN_PEER_LEAVING) {
1827 tipc_node_write_unlock(peer);
1828 err = -EBUSY;
1829 goto err_out;
1830 }
1831
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001832 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02001833 tipc_node_write_unlock(peer);
1834 tipc_node_delete(peer);
1835
1836 err = 0;
1837err_out:
1838 tipc_node_put(peer);
1839 spin_unlock_bh(&tn->node_list_lock);
1840
1841 return err;
1842}
1843
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001844int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1845{
1846 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001847 struct net *net = sock_net(skb->sk);
1848 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001849 int done = cb->args[0];
1850 int last_addr = cb->args[1];
1851 struct tipc_node *node;
1852 struct tipc_nl_msg msg;
1853
1854 if (done)
1855 return 0;
1856
1857 msg.skb = skb;
1858 msg.portid = NETLINK_CB(cb->skb).portid;
1859 msg.seq = cb->nlh->nlmsg_seq;
1860
1861 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001862 if (last_addr) {
1863 node = tipc_node_find(net, last_addr);
1864 if (!node) {
1865 rcu_read_unlock();
1866 /* We never set seq or call nl_dump_check_consistent()
1867 * this means that setting prev_seq here will cause the
1868 * consistence check to fail in the netlink callback
1869 * handler. Resulting in the NLMSG_DONE message having
1870 * the NLM_F_DUMP_INTR flag set if the node state
1871 * changed while we released the lock.
1872 */
1873 cb->prev_seq = 1;
1874 return -EPIPE;
1875 }
1876 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001877 }
1878
Ying Xuef2f98002015-01-09 15:27:05 +08001879 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001880 if (last_addr) {
1881 if (node->addr == last_addr)
1882 last_addr = 0;
1883 else
1884 continue;
1885 }
1886
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001887 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001888 err = __tipc_nl_add_node(&msg, node);
1889 if (err) {
1890 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001891 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001892 goto out;
1893 }
1894
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001895 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001896 }
1897 done = 1;
1898out:
1899 cb->args[0] = done;
1900 cb->args[1] = last_addr;
1901 rcu_read_unlock();
1902
1903 return skb->len;
1904}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001905
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001906/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001907 * @net: the applicable net namespace
1908 * @name: pointer to link name string
1909 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1910 *
1911 * Returns pointer to node owning the link, or 0 if no matching link is found.
1912 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001913static struct tipc_node *tipc_node_find_by_name(struct net *net,
1914 const char *link_name,
1915 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001916{
1917 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001918 struct tipc_link *l;
1919 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001920 struct tipc_node *found_node = NULL;
1921 int i;
1922
1923 *bearer_id = 0;
1924 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001925 list_for_each_entry_rcu(n, &tn->node_list, list) {
1926 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001927 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001928 l = n->links[i].link;
1929 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001930 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001931 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001932 break;
1933 }
1934 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001935 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001936 if (found_node)
1937 break;
1938 }
1939 rcu_read_unlock();
1940
1941 return found_node;
1942}
1943
1944int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1945{
1946 int err;
1947 int res = 0;
1948 int bearer_id;
1949 char *name;
1950 struct tipc_link *link;
1951 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01001952 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001953 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1954 struct net *net = sock_net(skb->sk);
1955
Richard Alped01332f2016-02-01 08:19:56 +01001956 __skb_queue_head_init(&xmitq);
1957
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001958 if (!info->attrs[TIPC_NLA_LINK])
1959 return -EINVAL;
1960
1961 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1962 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02001963 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001964 if (err)
1965 return err;
1966
1967 if (!attrs[TIPC_NLA_LINK_NAME])
1968 return -EINVAL;
1969
1970 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1971
1972 if (strcmp(name, tipc_bclink_name) == 0)
1973 return tipc_nl_bc_link_set(net, attrs);
1974
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001975 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001976 if (!node)
1977 return -EINVAL;
1978
1979 tipc_node_read_lock(node);
1980
1981 link = node->links[bearer_id].link;
1982 if (!link) {
1983 res = -EINVAL;
1984 goto out;
1985 }
1986
1987 if (attrs[TIPC_NLA_LINK_PROP]) {
1988 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1989
1990 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1991 props);
1992 if (err) {
1993 res = err;
1994 goto out;
1995 }
1996
1997 if (props[TIPC_NLA_PROP_TOL]) {
1998 u32 tol;
1999
2000 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002001 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002002 }
2003 if (props[TIPC_NLA_PROP_PRIO]) {
2004 u32 prio;
2005
2006 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002007 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002008 }
2009 if (props[TIPC_NLA_PROP_WIN]) {
2010 u32 win;
2011
2012 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2013 tipc_link_set_queue_limits(link, win);
2014 }
2015 }
2016
2017out:
2018 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01002019 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002020 return res;
2021}
2022
2023int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2024{
2025 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002026 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002027 struct tipc_nl_msg msg;
2028 char *name;
2029 int err;
2030
2031 msg.portid = info->snd_portid;
2032 msg.seq = info->snd_seq;
2033
Ying Xue94f6a802018-05-08 21:44:06 +08002034 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002035 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002036
2037 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2038 info->attrs[TIPC_NLA_LINK],
2039 tipc_nl_link_policy, info->extack);
2040 if (err)
2041 return err;
2042
2043 if (!attrs[TIPC_NLA_LINK_NAME])
2044 return -EINVAL;
2045
2046 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002047
2048 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2049 if (!msg.skb)
2050 return -ENOMEM;
2051
2052 if (strcmp(name, tipc_bclink_name) == 0) {
2053 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002054 if (err)
2055 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002056 } else {
2057 int bearer_id;
2058 struct tipc_node *node;
2059 struct tipc_link *link;
2060
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002061 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002062 if (!node) {
2063 err = -EINVAL;
2064 goto err_free;
2065 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002066
2067 tipc_node_read_lock(node);
2068 link = node->links[bearer_id].link;
2069 if (!link) {
2070 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002071 err = -EINVAL;
2072 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002073 }
2074
2075 err = __tipc_nl_add_link(net, &msg, link, 0);
2076 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002077 if (err)
2078 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002079 }
2080
2081 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002082
2083err_free:
2084 nlmsg_free(msg.skb);
2085 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002086}
2087
2088int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2089{
2090 int err;
2091 char *link_name;
2092 unsigned int bearer_id;
2093 struct tipc_link *link;
2094 struct tipc_node *node;
2095 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2096 struct net *net = sock_net(skb->sk);
2097 struct tipc_link_entry *le;
2098
2099 if (!info->attrs[TIPC_NLA_LINK])
2100 return -EINVAL;
2101
2102 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2103 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02002104 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002105 if (err)
2106 return err;
2107
2108 if (!attrs[TIPC_NLA_LINK_NAME])
2109 return -EINVAL;
2110
2111 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2112
2113 if (strcmp(link_name, tipc_bclink_name) == 0) {
2114 err = tipc_bclink_reset_stats(net);
2115 if (err)
2116 return err;
2117 return 0;
2118 }
2119
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002120 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002121 if (!node)
2122 return -EINVAL;
2123
2124 le = &node->links[bearer_id];
2125 tipc_node_read_lock(node);
2126 spin_lock_bh(&le->lock);
2127 link = node->links[bearer_id].link;
2128 if (!link) {
2129 spin_unlock_bh(&le->lock);
2130 tipc_node_read_unlock(node);
2131 return -EINVAL;
2132 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002133 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002134 spin_unlock_bh(&le->lock);
2135 tipc_node_read_unlock(node);
2136 return 0;
2137}
2138
2139/* Caller should hold node lock */
2140static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2141 struct tipc_node *node, u32 *prev_link)
2142{
2143 u32 i;
2144 int err;
2145
2146 for (i = *prev_link; i < MAX_BEARERS; i++) {
2147 *prev_link = i;
2148
2149 if (!node->links[i].link)
2150 continue;
2151
2152 err = __tipc_nl_add_link(net, msg,
2153 node->links[i].link, NLM_F_MULTI);
2154 if (err)
2155 return err;
2156 }
2157 *prev_link = 0;
2158
2159 return 0;
2160}
2161
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002162int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002163{
2164 struct net *net = sock_net(skb->sk);
2165 struct tipc_net *tn = net_generic(net, tipc_net_id);
2166 struct tipc_node *node;
2167 struct tipc_nl_msg msg;
2168 u32 prev_node = cb->args[0];
2169 u32 prev_link = cb->args[1];
2170 int done = cb->args[2];
2171 int err;
2172
2173 if (done)
2174 return 0;
2175
2176 msg.skb = skb;
2177 msg.portid = NETLINK_CB(cb->skb).portid;
2178 msg.seq = cb->nlh->nlmsg_seq;
2179
2180 rcu_read_lock();
2181 if (prev_node) {
2182 node = tipc_node_find(net, prev_node);
2183 if (!node) {
2184 /* We never set seq or call nl_dump_check_consistent()
2185 * this means that setting prev_seq here will cause the
2186 * consistence check to fail in the netlink callback
2187 * handler. Resulting in the last NLMSG_DONE message
2188 * having the NLM_F_DUMP_INTR flag set.
2189 */
2190 cb->prev_seq = 1;
2191 goto out;
2192 }
2193 tipc_node_put(node);
2194
2195 list_for_each_entry_continue_rcu(node, &tn->node_list,
2196 list) {
2197 tipc_node_read_lock(node);
2198 err = __tipc_nl_add_node_links(net, &msg, node,
2199 &prev_link);
2200 tipc_node_read_unlock(node);
2201 if (err)
2202 goto out;
2203
2204 prev_node = node->addr;
2205 }
2206 } else {
2207 err = tipc_nl_add_bc_link(net, &msg);
2208 if (err)
2209 goto out;
2210
2211 list_for_each_entry_rcu(node, &tn->node_list, list) {
2212 tipc_node_read_lock(node);
2213 err = __tipc_nl_add_node_links(net, &msg, node,
2214 &prev_link);
2215 tipc_node_read_unlock(node);
2216 if (err)
2217 goto out;
2218
2219 prev_node = node->addr;
2220 }
2221 }
2222 done = 1;
2223out:
2224 rcu_read_unlock();
2225
2226 cb->args[0] = prev_node;
2227 cb->args[1] = prev_link;
2228 cb->args[2] = done;
2229
2230 return skb->len;
2231}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002232
2233int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2234{
2235 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2236 struct net *net = sock_net(skb->sk);
2237 int err;
2238
2239 if (!info->attrs[TIPC_NLA_MON])
2240 return -EINVAL;
2241
2242 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2243 info->attrs[TIPC_NLA_MON],
Johannes Bergfe521452017-04-12 14:34:08 +02002244 tipc_nl_monitor_policy, info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002245 if (err)
2246 return err;
2247
2248 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2249 u32 val;
2250
2251 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2252 err = tipc_nl_monitor_set_threshold(net, val);
2253 if (err)
2254 return err;
2255 }
2256
2257 return 0;
2258}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002259
2260static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2261{
2262 struct nlattr *attrs;
2263 void *hdr;
2264 u32 val;
2265
2266 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2267 0, TIPC_NL_MON_GET);
2268 if (!hdr)
2269 return -EMSGSIZE;
2270
2271 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2272 if (!attrs)
2273 goto msg_full;
2274
2275 val = tipc_nl_monitor_get_threshold(net);
2276
2277 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2278 goto attr_msg_full;
2279
2280 nla_nest_end(msg->skb, attrs);
2281 genlmsg_end(msg->skb, hdr);
2282
2283 return 0;
2284
2285attr_msg_full:
2286 nla_nest_cancel(msg->skb, attrs);
2287msg_full:
2288 genlmsg_cancel(msg->skb, hdr);
2289
2290 return -EMSGSIZE;
2291}
2292
2293int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2294{
2295 struct net *net = sock_net(skb->sk);
2296 struct tipc_nl_msg msg;
2297 int err;
2298
2299 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002300 if (!msg.skb)
2301 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002302 msg.portid = info->snd_portid;
2303 msg.seq = info->snd_seq;
2304
2305 err = __tipc_nl_add_monitor_prop(net, &msg);
2306 if (err) {
2307 nlmsg_free(msg.skb);
2308 return err;
2309 }
2310
2311 return genlmsg_reply(msg.skb, info);
2312}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002313
2314int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2315{
2316 struct net *net = sock_net(skb->sk);
2317 u32 prev_bearer = cb->args[0];
2318 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002319 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002320 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002321
2322 if (prev_bearer == MAX_BEARERS)
2323 return 0;
2324
2325 msg.skb = skb;
2326 msg.portid = NETLINK_CB(cb->skb).portid;
2327 msg.seq = cb->nlh->nlmsg_seq;
2328
2329 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002330 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002331 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002332 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002333 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002334 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002335 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002336 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002337
2338 return skb->len;
2339}
2340
2341int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2342 struct netlink_callback *cb)
2343{
2344 struct net *net = sock_net(skb->sk);
2345 u32 prev_node = cb->args[1];
2346 u32 bearer_id = cb->args[2];
2347 int done = cb->args[0];
2348 struct tipc_nl_msg msg;
2349 int err;
2350
2351 if (!prev_node) {
2352 struct nlattr **attrs;
2353 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2354
2355 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2356 if (err)
2357 return err;
2358
2359 if (!attrs[TIPC_NLA_MON])
2360 return -EINVAL;
2361
2362 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2363 attrs[TIPC_NLA_MON],
Johannes Bergfceb6432017-04-12 14:34:07 +02002364 tipc_nl_monitor_policy, NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002365 if (err)
2366 return err;
2367
2368 if (!mon[TIPC_NLA_MON_REF])
2369 return -EINVAL;
2370
2371 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2372
2373 if (bearer_id >= MAX_BEARERS)
2374 return -EINVAL;
2375 }
2376
2377 if (done)
2378 return 0;
2379
2380 msg.skb = skb;
2381 msg.portid = NETLINK_CB(cb->skb).portid;
2382 msg.seq = cb->nlh->nlmsg_seq;
2383
2384 rtnl_lock();
2385 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2386 if (!err)
2387 done = 1;
2388
2389 rtnl_unlock();
2390 cb->args[0] = done;
2391 cb->args[1] = prev_node;
2392 cb->args[2] = bearer_id;
2393
2394 return skb->len;
2395}