blob: 52fd80b0e7287568778deea89626c8bc3850cfc9 [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 Maloy9012de52018-07-10 01:07:35 +0200366 struct tipc_link *l;
367 int bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500368 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369
Ying Xuef2f98002015-01-09 15:27:05 +0800370 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500371 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400372 if (n) {
373 /* Same node may come back with new capabilities */
374 n->capabilities = capabilities;
Jon Maloy9012de52018-07-10 01:07:35 +0200375 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
376 l = n->links[bearer_id].link;
377 if (l)
378 tipc_link_update_caps(l, capabilities);
379 }
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500380 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400381 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500382 n = kzalloc(sizeof(*n), GFP_ATOMIC);
383 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400384 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500385 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700386 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500387 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100388 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500389 n->net = net;
390 n->capabilities = capabilities;
391 kref_init(&n->kref);
392 rwlock_init(&n->lock);
393 INIT_HLIST_NODE(&n->hash);
394 INIT_LIST_HEAD(&n->list);
395 INIT_LIST_HEAD(&n->publ_list);
396 INIT_LIST_HEAD(&n->conn_sks);
397 skb_queue_head_init(&n->bc_entry.namedq);
398 skb_queue_head_init(&n->bc_entry.inputq1);
399 __skb_queue_head_init(&n->bc_entry.arrvq);
400 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500401 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500402 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500403 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200404 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500405 n->signature = INVALID_NODE_SIG;
406 n->active_links[0] = INVALID_BEARER_ID;
407 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100408 if (!tipc_link_bc_create(net, tipc_own_addr(net),
409 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500410 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500411 n->capabilities,
412 &n->bc_entry.inputq1,
413 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400414 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500415 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400416 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500417 kfree(n);
418 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400419 goto exit;
420 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500421 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700422 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500423 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500424 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
425 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
426 if (n->addr < temp_node->addr)
427 break;
428 }
429 list_add_tail_rcu(&n->list, &temp_node->list);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500430exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800431 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500432 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433}
434
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400435static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
436{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500437 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400438 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400439
440 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400441 if (intv < n->keepalive_intv)
442 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400443
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400444 /* Ensure link's abort limit corresponds to current tolerance */
445 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400446}
447
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200448static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800450 list_del_rcu(&node->list);
451 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500452 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200453}
454
455static void tipc_node_delete(struct tipc_node *node)
456{
457 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500458
459 del_timer_sync(&node->timer);
460 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
Ying Xuef2f98002015-01-09 15:27:05 +0800463void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800464{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500465 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800466 struct tipc_node *node, *t_node;
467
Ying Xuef2f98002015-01-09 15:27:05 +0800468 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500469 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
470 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800471 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800472}
473
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500474void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
475{
476 struct tipc_node *n;
477
478 if (in_own_node(net, addr))
479 return;
480
481 n = tipc_node_find(net, addr);
482 if (!n) {
483 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
484 return;
485 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500486 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500487 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100488 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500489 tipc_node_put(n);
490}
491
492void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
493{
494 struct tipc_node *n;
495
496 if (in_own_node(net, addr))
497 return;
498
499 n = tipc_node_find(net, addr);
500 if (!n) {
501 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
502 return;
503 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500504 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500505 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100506 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500507 tipc_node_put(n);
508}
509
Ying Xuef2f98002015-01-09 15:27:05 +0800510int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400511{
512 struct tipc_node *node;
513 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800514 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400515
Ying Xue34747532015-01-09 15:27:10 +0800516 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400517 return 0;
518
Ying Xuef2f98002015-01-09 15:27:05 +0800519 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400520 if (!node) {
521 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
522 return -EHOSTUNREACH;
523 }
524 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800525 if (!conn) {
526 err = -EHOSTUNREACH;
527 goto exit;
528 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400529 conn->peer_node = dnode;
530 conn->port = port;
531 conn->peer_port = peer_port;
532
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500533 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400534 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500535 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800536exit:
537 tipc_node_put(node);
538 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400539}
540
Ying Xuef2f98002015-01-09 15:27:05 +0800541void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400542{
543 struct tipc_node *node;
544 struct tipc_sock_conn *conn, *safe;
545
Ying Xue34747532015-01-09 15:27:10 +0800546 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400547 return;
548
Ying Xuef2f98002015-01-09 15:27:05 +0800549 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400550 if (!node)
551 return;
552
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500553 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400554 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
555 if (port != conn->port)
556 continue;
557 list_del(&conn->list);
558 kfree(conn);
559 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500560 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800561 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400562}
563
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200564static void tipc_node_clear_links(struct tipc_node *node)
565{
566 int i;
567
568 for (i = 0; i < MAX_BEARERS; i++) {
569 struct tipc_link_entry *le = &node->links[i];
570
571 if (le->link) {
572 kfree(le->link);
573 le->link = NULL;
574 node->link_cnt--;
575 }
576 }
577}
578
579/* tipc_node_cleanup - delete nodes that does not
580 * have active links for NODE_CLEANUP_AFTER time
581 */
582static int tipc_node_cleanup(struct tipc_node *peer)
583{
584 struct tipc_net *tn = tipc_net(peer->net);
585 bool deleted = false;
586
587 spin_lock_bh(&tn->node_list_lock);
588 tipc_node_write_lock(peer);
589
590 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
591 tipc_node_clear_links(peer);
592 tipc_node_delete_from_list(peer);
593 deleted = true;
594 }
595 tipc_node_write_unlock(peer);
596 spin_unlock_bh(&tn->node_list_lock);
597 return deleted;
598}
599
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400600/* tipc_node_timeout - handle expiration of node timer
601 */
Kees Cook31b102b2017-10-30 14:06:45 -0700602static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400603{
Kees Cook31b102b2017-10-30 14:06:45 -0700604 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400605 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400606 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200607 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400608 int bearer_id;
609 int rc = 0;
610
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200611 if (!node_is_up(n) && tipc_node_cleanup(n)) {
612 /*Removing the reference of Timer*/
613 tipc_node_put(n);
614 return;
615 }
616
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400617 __skb_queue_head_init(&xmitq);
618
Tung Nguyen759f29b2018-06-28 22:39:25 +0200619 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500620 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400621 le = &n->links[bearer_id];
622 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200623 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400624 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400625 tipc_node_calculate_timer(n, le->link);
626 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200627 spin_unlock_bh(&le->lock);
628 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400629 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500630 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400631 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
632 if (rc & TIPC_LINK_DOWN_EVT)
633 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400634 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400635 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400636}
637
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400639 * __tipc_node_link_up - handle addition of link
640 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 * Link becomes active (alone or shared) or standby, depending on its priority.
642 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400643static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
644 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400646 int *slot0 = &n->active_links[0];
647 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400648 struct tipc_link *ol = node_active_link(n, 0);
649 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400651 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400652 return;
653
654 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
655 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400656 return;
657
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400658 n->working_links++;
659 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500660 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400661
662 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500663 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800664
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400665 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400666 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400667
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100668 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500669 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900670
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400671 /* Ensure that a STATE message goes first */
672 tipc_link_build_state_msg(nl, xmitq);
673
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400674 /* First link? => give it both slots */
675 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400676 *slot0 = bearer_id;
677 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400678 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
679 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400680 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400681 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400682 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400684
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400685 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500686 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
687 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400688 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400689 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400690 tipc_link_set_active(nl, true);
691 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500692 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400693 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a82015-10-22 08:51:47 -0400694 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400695 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500696 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400699 /* Prepare synchronization with first link */
700 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701}
702
703/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400704 * tipc_node_link_up - handle addition of link
705 *
706 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400708static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
709 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400711 struct tipc_media_addr *maddr;
712
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500713 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400714 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400715 maddr = &n->links[bearer_id].maddr;
716 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500717 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400718}
719
720/**
721 * __tipc_node_link_down - handle loss of link
722 */
723static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
724 struct sk_buff_head *xmitq,
725 struct tipc_media_addr **maddr)
726{
727 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400728 int *slot0 = &n->active_links[0];
729 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500730 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400731 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400733 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400734 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400735 return;
736
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400737 n->working_links--;
738 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500739 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700740
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400741 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400742
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100743 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500744 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400746 /* Select new active link if any available */
747 *slot0 = INVALID_BEARER_ID;
748 *slot1 = INVALID_BEARER_ID;
749 for (i = 0; i < MAX_BEARERS; i++) {
750 _l = n->links[i].link;
751 if (!_l || !tipc_link_is_up(_l))
752 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400753 if (_l == l)
754 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500755 prio = tipc_link_prio(_l);
756 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400757 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500758 if (prio > highest) {
759 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400760 *slot0 = i;
761 *slot1 = i;
762 continue;
763 }
764 *slot1 = i;
765 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400766
Jon Maloy38077b82017-10-13 11:04:19 +0200767 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400768 if (tipc_link_peer_is_down(l))
769 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
770 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
771 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400772 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400773 tipc_link_build_reset_msg(l, xmitq);
774 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400775 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400776 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400777 return;
778 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400779 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400780
781 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500782 *bearer_id = n->active_links[0];
783 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400784 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
785 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500786 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400787 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400788 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400789 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400790 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400791 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500792 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400793}
794
795static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
796{
797 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400798 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400799 struct tipc_media_addr *maddr;
800 struct sk_buff_head xmitq;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400801 int old_bearer_id = bearer_id;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400802
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400803 if (!l)
804 return;
805
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400806 __skb_queue_head_init(&xmitq);
807
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500808 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400809 if (!tipc_link_is_establishing(l)) {
810 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
811 if (delete) {
812 kfree(l);
813 le->link = NULL;
814 n->link_cnt--;
815 }
816 } else {
817 /* Defuse pending tipc_node_link_up() */
818 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400819 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500820 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400821 if (delete)
822 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400823 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
824 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825}
826
Jon Maloy38077b82017-10-13 11:04:19 +0200827static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400829 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830}
831
Jon Maloy38077b82017-10-13 11:04:19 +0200832bool tipc_node_is_up(struct net *net, u32 addr)
833{
834 struct tipc_node *n;
835 bool retval = false;
836
837 if (in_own_node(net, addr))
838 return true;
839
840 n = tipc_node_find(net, addr);
841 if (!n)
842 return false;
843 retval = node_is_up(n);
844 tipc_node_put(n);
845 return retval;
846}
847
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100848static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
849{
850 struct tipc_node *n;
851
852 addr ^= tipc_net(net)->random;
853 while ((n = tipc_node_find(net, addr))) {
854 tipc_node_put(n);
855 addr++;
856 }
857 return addr;
858}
859
860/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
861 */
862u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
863{
864 struct tipc_net *tn = tipc_net(net);
865 struct tipc_node *n;
866
867 /* Suggest new address if some other peer is using this one */
868 n = tipc_node_find(net, addr);
869 if (n) {
870 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
871 addr = 0;
872 tipc_node_put(n);
873 if (!addr)
874 return 0;
875 return tipc_node_suggest_addr(net, addr);
876 }
877
878 /* Suggest previously used address if peer is known */
879 n = tipc_node_find_by_id(net, id);
880 if (n) {
881 addr = n->addr;
882 tipc_node_put(n);
883 }
884 /* Even this node may be in trial phase */
885 if (tn->trial_addr == addr)
886 return tipc_node_suggest_addr(net, addr);
887
888 return addr;
889}
890
891void tipc_node_check_dest(struct net *net, u32 addr,
892 u8 *peer_id, struct tipc_bearer *b,
Jon Paul Maloycf148812015-07-30 18:24:22 -0400893 u16 capabilities, u32 signature,
894 struct tipc_media_addr *maddr,
895 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400896{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400897 struct tipc_node *n;
898 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400899 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400900 bool addr_match = false;
901 bool sign_match = false;
902 bool link_up = false;
903 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400904 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400905 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400906 unsigned long intv;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400907
Jon Paul Maloycf148812015-07-30 18:24:22 -0400908 *dupl_addr = false;
909 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400910
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100911 n = tipc_node_create(net, addr, peer_id, capabilities);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400912 if (!n)
913 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400914
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500915 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400916
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400917 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400918
919 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400920 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400921 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400922 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400923 sign_match = (signature == n->signature);
924
925 /* These three flags give us eight permutations: */
926
927 if (sign_match && addr_match && link_up) {
928 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400929 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400930 } else if (sign_match && addr_match && !link_up) {
931 /* Respond. The link will come up in due time */
932 *respond = true;
933 } else if (sign_match && !addr_match && link_up) {
934 /* Peer has changed i/f address without rebooting.
935 * If so, the link will reset soon, and the next
936 * discovery will be accepted. So we can ignore it.
937 * It may also be an cloned or malicious peer having
938 * chosen the same node address and signature as an
939 * existing one.
940 * Ignore requests until the link goes down, if ever.
941 */
942 *dupl_addr = true;
943 } else if (sign_match && !addr_match && !link_up) {
944 /* Peer link has changed i/f address without rebooting.
945 * It may also be a cloned or malicious peer; we can't
946 * distinguish between the two.
947 * The signature is correct, so we must accept.
948 */
949 accept_addr = true;
950 *respond = true;
951 } else if (!sign_match && addr_match && link_up) {
952 /* Peer node rebooted. Two possibilities:
953 * - Delayed re-discovery; this link endpoint has already
954 * reset and re-established contact with the peer, before
955 * receiving a discovery message from that node.
956 * (The peer happened to receive one from this node first).
957 * - The peer came back so fast that our side has not
958 * discovered it yet. Probing from this side will soon
959 * reset the link, since there can be no working link
960 * endpoint at the peer end, and the link will re-establish.
961 * Accept the signature, since it comes from a known peer.
962 */
963 n->signature = signature;
964 } else if (!sign_match && addr_match && !link_up) {
965 /* The peer node has rebooted.
966 * Accept signature, since it is a known peer.
967 */
968 n->signature = signature;
969 *respond = true;
970 } else if (!sign_match && !addr_match && link_up) {
971 /* Peer rebooted with new address, or a new/duplicate peer.
972 * Ignore until the link goes down, if ever.
973 */
974 *dupl_addr = true;
975 } else if (!sign_match && !addr_match && !link_up) {
976 /* Peer rebooted with new address, or it is a new peer.
977 * Accept signature and address.
978 */
979 n->signature = signature;
980 accept_addr = true;
981 *respond = true;
982 }
983
984 if (!accept_addr)
985 goto exit;
986
987 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400988 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +0100989 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400990 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +0100991
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400992 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400993 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400994 b->net_plane, b->mtu, b->priority,
995 b->window, mod(tipc_net(net)->random),
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100996 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400997 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400998 tipc_bc_sndlink(n->net), n->bc_entry.link,
999 &le->inputq,
1000 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -04001001 *respond = false;
1002 goto exit;
1003 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001004 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -04001005 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001006 if (n->state == NODE_FAILINGOVER)
1007 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001008 le->link = l;
1009 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001010 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001011 if (n->link_cnt == 1) {
1012 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1013 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001014 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001015 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001016 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001017 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001018exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001019 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001020 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001021 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001022 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001023}
1024
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001025void tipc_node_delete_links(struct net *net, int bearer_id)
1026{
1027 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001028 struct tipc_node *n;
1029
1030 rcu_read_lock();
1031 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001032 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001033 }
1034 rcu_read_unlock();
1035}
1036
1037static void tipc_node_reset_links(struct tipc_node *n)
1038{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001039 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001040
Jon Maloyd50ccc22018-03-22 20:42:50 +01001041 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001042
1043 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001044 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001045 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001046}
1047
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001048/* tipc_node_fsm_evt - node finite state machine
1049 * Determines when contact is allowed with peer node
1050 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001051static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001052{
1053 int state = n->state;
1054
1055 switch (state) {
1056 case SELF_DOWN_PEER_DOWN:
1057 switch (evt) {
1058 case SELF_ESTABL_CONTACT_EVT:
1059 state = SELF_UP_PEER_COMING;
1060 break;
1061 case PEER_ESTABL_CONTACT_EVT:
1062 state = SELF_COMING_PEER_UP;
1063 break;
1064 case SELF_LOST_CONTACT_EVT:
1065 case PEER_LOST_CONTACT_EVT:
1066 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001067 case NODE_SYNCH_END_EVT:
1068 case NODE_SYNCH_BEGIN_EVT:
1069 case NODE_FAILOVER_BEGIN_EVT:
1070 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001071 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001072 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001073 }
1074 break;
1075 case SELF_UP_PEER_UP:
1076 switch (evt) {
1077 case SELF_LOST_CONTACT_EVT:
1078 state = SELF_DOWN_PEER_LEAVING;
1079 break;
1080 case PEER_LOST_CONTACT_EVT:
1081 state = SELF_LEAVING_PEER_DOWN;
1082 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001083 case NODE_SYNCH_BEGIN_EVT:
1084 state = NODE_SYNCHING;
1085 break;
1086 case NODE_FAILOVER_BEGIN_EVT:
1087 state = NODE_FAILINGOVER;
1088 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001089 case SELF_ESTABL_CONTACT_EVT:
1090 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001091 case NODE_SYNCH_END_EVT:
1092 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001093 break;
1094 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001095 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001096 }
1097 break;
1098 case SELF_DOWN_PEER_LEAVING:
1099 switch (evt) {
1100 case PEER_LOST_CONTACT_EVT:
1101 state = SELF_DOWN_PEER_DOWN;
1102 break;
1103 case SELF_ESTABL_CONTACT_EVT:
1104 case PEER_ESTABL_CONTACT_EVT:
1105 case SELF_LOST_CONTACT_EVT:
1106 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001107 case NODE_SYNCH_END_EVT:
1108 case NODE_SYNCH_BEGIN_EVT:
1109 case NODE_FAILOVER_BEGIN_EVT:
1110 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001111 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001112 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001113 }
1114 break;
1115 case SELF_UP_PEER_COMING:
1116 switch (evt) {
1117 case PEER_ESTABL_CONTACT_EVT:
1118 state = SELF_UP_PEER_UP;
1119 break;
1120 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001121 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001122 break;
1123 case SELF_ESTABL_CONTACT_EVT:
1124 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001125 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001126 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001127 break;
1128 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001129 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001130 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001131 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001132 }
1133 break;
1134 case SELF_COMING_PEER_UP:
1135 switch (evt) {
1136 case SELF_ESTABL_CONTACT_EVT:
1137 state = SELF_UP_PEER_UP;
1138 break;
1139 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001140 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001141 break;
1142 case SELF_LOST_CONTACT_EVT:
1143 case PEER_ESTABL_CONTACT_EVT:
1144 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001145 case NODE_SYNCH_END_EVT:
1146 case NODE_SYNCH_BEGIN_EVT:
1147 case NODE_FAILOVER_BEGIN_EVT:
1148 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001149 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001150 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001151 }
1152 break;
1153 case SELF_LEAVING_PEER_DOWN:
1154 switch (evt) {
1155 case SELF_LOST_CONTACT_EVT:
1156 state = SELF_DOWN_PEER_DOWN;
1157 break;
1158 case SELF_ESTABL_CONTACT_EVT:
1159 case PEER_ESTABL_CONTACT_EVT:
1160 case PEER_LOST_CONTACT_EVT:
1161 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001162 case NODE_SYNCH_END_EVT:
1163 case NODE_SYNCH_BEGIN_EVT:
1164 case NODE_FAILOVER_BEGIN_EVT:
1165 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001166 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001167 goto illegal_evt;
1168 }
1169 break;
1170 case NODE_FAILINGOVER:
1171 switch (evt) {
1172 case SELF_LOST_CONTACT_EVT:
1173 state = SELF_DOWN_PEER_LEAVING;
1174 break;
1175 case PEER_LOST_CONTACT_EVT:
1176 state = SELF_LEAVING_PEER_DOWN;
1177 break;
1178 case NODE_FAILOVER_END_EVT:
1179 state = SELF_UP_PEER_UP;
1180 break;
1181 case NODE_FAILOVER_BEGIN_EVT:
1182 case SELF_ESTABL_CONTACT_EVT:
1183 case PEER_ESTABL_CONTACT_EVT:
1184 break;
1185 case NODE_SYNCH_BEGIN_EVT:
1186 case NODE_SYNCH_END_EVT:
1187 default:
1188 goto illegal_evt;
1189 }
1190 break;
1191 case NODE_SYNCHING:
1192 switch (evt) {
1193 case SELF_LOST_CONTACT_EVT:
1194 state = SELF_DOWN_PEER_LEAVING;
1195 break;
1196 case PEER_LOST_CONTACT_EVT:
1197 state = SELF_LEAVING_PEER_DOWN;
1198 break;
1199 case NODE_SYNCH_END_EVT:
1200 state = SELF_UP_PEER_UP;
1201 break;
1202 case NODE_FAILOVER_BEGIN_EVT:
1203 state = NODE_FAILINGOVER;
1204 break;
1205 case NODE_SYNCH_BEGIN_EVT:
1206 case SELF_ESTABL_CONTACT_EVT:
1207 case PEER_ESTABL_CONTACT_EVT:
1208 break;
1209 case NODE_FAILOVER_END_EVT:
1210 default:
1211 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001212 }
1213 break;
1214 default:
1215 pr_err("Unknown node fsm state %x\n", state);
1216 break;
1217 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001218 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001219 return;
1220
1221illegal_evt:
1222 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001223}
1224
Jon Paul Maloy52666982015-10-22 08:51:41 -04001225static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001226 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001228 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001229 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001230 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001231 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001232 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233
Jon Maloyd50ccc22018-03-22 20:42:50 +01001234 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001235 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001236
Jon Paul Maloy52666982015-10-22 08:51:41 -04001237 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001238 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001240 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001242 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001243 if (l)
1244 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001246
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001247 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001248 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001249
1250 /* Notify sockets connected to node */
1251 list_for_each_entry_safe(conn, safe, conns, list) {
1252 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001253 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001254 conn->peer_node, conn->port,
1255 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001256 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001257 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001258 list_del(&conn->list);
1259 kfree(conn);
1260 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261}
1262
Erik Hugne78acb1f2014-04-24 16:26:47 +02001263/**
1264 * tipc_node_get_linkname - get the name of a link
1265 *
1266 * @bearer_id: id of the bearer
1267 * @node: peer node address
1268 * @linkname: link name output buffer
1269 *
1270 * Returns 0 on success
1271 */
Ying Xuef2f98002015-01-09 15:27:05 +08001272int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1273 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001274{
1275 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001276 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001277 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001278
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001279 if (!node)
1280 return err;
1281
1282 if (bearer_id >= MAX_BEARERS)
1283 goto exit;
1284
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001285 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001286 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001287 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001288 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001289 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001290 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001291 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001292exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001293 tipc_node_put(node);
1294 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001295}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001296
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001297/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001298static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001299{
1300 void *hdr;
1301 struct nlattr *attrs;
1302
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001303 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001304 NLM_F_MULTI, TIPC_NL_NODE_GET);
1305 if (!hdr)
1306 return -EMSGSIZE;
1307
1308 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1309 if (!attrs)
1310 goto msg_full;
1311
1312 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1313 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001314 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001315 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1316 goto attr_msg_full;
1317
1318 nla_nest_end(msg->skb, attrs);
1319 genlmsg_end(msg->skb, hdr);
1320
1321 return 0;
1322
1323attr_msg_full:
1324 nla_nest_cancel(msg->skb, attrs);
1325msg_full:
1326 genlmsg_cancel(msg->skb, hdr);
1327
1328 return -EMSGSIZE;
1329}
1330
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001331/**
1332 * tipc_node_xmit() is the general link level function for message sending
1333 * @net: the applicable net namespace
1334 * @list: chain of buffers containing message
1335 * @dnode: address of destination node
1336 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001337 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001338 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001339 */
1340int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1341 u32 dnode, int selector)
1342{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001343 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001344 struct tipc_node *n;
1345 struct sk_buff_head xmitq;
Richard Alpe4952cd32016-02-11 10:43:15 +01001346 int bearer_id;
1347 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001348
Richard Alpe4952cd32016-02-11 10:43:15 +01001349 if (in_own_node(net, dnode)) {
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001350 tipc_sk_rcv(net, list);
1351 return 0;
1352 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001353
1354 n = tipc_node_find(net, dnode);
1355 if (unlikely(!n)) {
1356 skb_queue_purge(list);
1357 return -EHOSTUNREACH;
1358 }
1359
1360 tipc_node_read_lock(n);
1361 bearer_id = n->active_links[selector & 1];
1362 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1363 tipc_node_read_unlock(n);
1364 tipc_node_put(n);
1365 skb_queue_purge(list);
1366 return -EHOSTUNREACH;
1367 }
1368
1369 __skb_queue_head_init(&xmitq);
1370 le = &n->links[bearer_id];
1371 spin_lock_bh(&le->lock);
1372 rc = tipc_link_xmit(le->link, list, &xmitq);
1373 spin_unlock_bh(&le->lock);
1374 tipc_node_read_unlock(n);
1375
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001376 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001377 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001378 else
1379 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001380
1381 tipc_node_put(n);
1382
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001383 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001384}
1385
1386/* tipc_node_xmit_skb(): send single buffer to destination
1387 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1388 * messages, which will not be rejected
1389 * The only exception is datagram messages rerouted after secondary
1390 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001391 */
1392int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1393 u32 selector)
1394{
1395 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001396
1397 skb_queue_head_init(&head);
1398 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001399 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001400 return 0;
1401}
1402
Jon Maloyf70d37b2017-10-13 11:04:21 +02001403/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1404 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1405 */
1406int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1407{
1408 struct sk_buff *skb;
1409 u32 selector, dnode;
1410
1411 while ((skb = __skb_dequeue(xmitq))) {
1412 selector = msg_origport(buf_msg(skb));
1413 dnode = msg_destnode(buf_msg(skb));
1414 tipc_node_xmit_skb(net, skb, dnode, selector);
1415 }
1416 return 0;
1417}
1418
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001419void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1420{
1421 struct sk_buff *txskb;
1422 struct tipc_node *n;
1423 u32 dst;
1424
1425 rcu_read_lock();
1426 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1427 dst = n->addr;
1428 if (in_own_node(net, dst))
1429 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001430 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001431 continue;
1432 txskb = pskb_copy(skb, GFP_ATOMIC);
1433 if (!txskb)
1434 break;
1435 msg_set_destnode(buf_msg(txskb), dst);
1436 tipc_node_xmit_skb(net, txskb, dst, 0);
1437 }
1438 rcu_read_unlock();
1439
1440 kfree_skb(skb);
1441}
1442
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001443static void tipc_node_mcast_rcv(struct tipc_node *n)
1444{
1445 struct tipc_bclink_entry *be = &n->bc_entry;
1446
1447 /* 'arrvq' is under inputq2's lock protection */
1448 spin_lock_bh(&be->inputq2.lock);
1449 spin_lock_bh(&be->inputq1.lock);
1450 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1451 spin_unlock_bh(&be->inputq1.lock);
1452 spin_unlock_bh(&be->inputq2.lock);
1453 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1454}
1455
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001456static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1457 int bearer_id, struct sk_buff_head *xmitq)
1458{
1459 struct tipc_link *ucl;
1460 int rc;
1461
1462 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1463
1464 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001465 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001466 return;
1467 }
1468
1469 if (!(rc & TIPC_LINK_SND_STATE))
1470 return;
1471
1472 /* If probe message, a STATE response will be sent anyway */
1473 if (msg_probe(hdr))
1474 return;
1475
1476 /* Produce a STATE message carrying broadcast NACK */
1477 tipc_node_read_lock(n);
1478 ucl = n->links[bearer_id].link;
1479 if (ucl)
1480 tipc_link_build_state_msg(ucl, xmitq);
1481 tipc_node_read_unlock(n);
1482}
1483
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001484/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001485 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1486 * @net: the applicable net namespace
1487 * @skb: TIPC packet
1488 * @bearer_id: id of bearer message arrived on
1489 *
1490 * Invoked with no locks held.
1491 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001492static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001493{
1494 int rc;
1495 struct sk_buff_head xmitq;
1496 struct tipc_bclink_entry *be;
1497 struct tipc_link_entry *le;
1498 struct tipc_msg *hdr = buf_msg(skb);
1499 int usr = msg_user(hdr);
1500 u32 dnode = msg_destnode(hdr);
1501 struct tipc_node *n;
1502
1503 __skb_queue_head_init(&xmitq);
1504
1505 /* If NACK for other node, let rcv link for that node peek into it */
1506 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1507 n = tipc_node_find(net, dnode);
1508 else
1509 n = tipc_node_find(net, msg_prevnode(hdr));
1510 if (!n) {
1511 kfree_skb(skb);
1512 return;
1513 }
1514 be = &n->bc_entry;
1515 le = &n->links[bearer_id];
1516
1517 rc = tipc_bcast_rcv(net, be->link, skb);
1518
Jon Paul Maloy52666982015-10-22 08:51:41 -04001519 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001520 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001521 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001522 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001523 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001524 }
1525
1526 if (!skb_queue_empty(&xmitq))
1527 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1528
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001529 if (!skb_queue_empty(&be->inputq1))
1530 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001531
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001532 /* If reassembly or retransmission failure => reset all links to peer */
1533 if (rc & TIPC_LINK_DOWN_EVT)
1534 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001535
Jon Paul Maloy52666982015-10-22 08:51:41 -04001536 tipc_node_put(n);
1537}
1538
1539/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001540 * tipc_node_check_state - check and if necessary update node state
1541 * @skb: TIPC packet
1542 * @bearer_id: identity of bearer delivering the packet
Jon Maloy7ea817f2018-07-10 01:07:36 +02001543 * Returns true if state and msg are ok, otherwise false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001544 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001545static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001546 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001547{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001548 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001549 int usr = msg_user(hdr);
1550 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001551 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001552 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1553 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001554 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001555 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001556 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001557 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001558 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001559
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001560 l = n->links[bearer_id].link;
1561 if (!l)
1562 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001563 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001564
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001565
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001566 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1567 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001568
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001569 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001570 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1571 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1572 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001573 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001574 }
1575 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001576
Jon Maloy7ea817f2018-07-10 01:07:36 +02001577 if (!tipc_link_validate_msg(l, hdr))
1578 return false;
1579
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001580 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001581 if (state == SELF_UP_PEER_COMING) {
1582 if (!tipc_link_is_up(l))
1583 return true;
1584 if (!msg_peer_link_is_up(hdr))
1585 return true;
1586 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1587 }
1588
1589 if (state == SELF_DOWN_PEER_LEAVING) {
1590 if (msg_peer_node_is_up(hdr))
1591 return false;
1592 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001593 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001594 }
1595
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001596 if (state == SELF_LEAVING_PEER_DOWN)
1597 return false;
1598
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001599 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001600 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001601 return true;
1602
1603 /* Initiate or update failover mode if applicable */
1604 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1605 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001606 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001607 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001608 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1609 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001610 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001611 /* If pkts arrive out of order, use lowest calculated syncpt */
1612 if (less(syncpt, n->sync_point))
1613 n->sync_point = syncpt;
1614 }
1615
1616 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001617 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001618 if (!more(rcv_nxt, n->sync_point))
1619 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001620 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1621 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001622 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001623 return true;
1624 }
1625
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001626 /* No synching needed if only one link */
1627 if (!pl || !tipc_link_is_up(pl))
1628 return true;
1629
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001630 /* Initiate synch mode if applicable */
1631 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001632 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001633 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001634 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001635 if (n->state == SELF_UP_PEER_UP) {
1636 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001637 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001638 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1639 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001640 }
1641
1642 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001643 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001644 if (tipc_link_is_synching(l)) {
1645 tnl = l;
1646 } else {
1647 tnl = pl;
1648 pl = l;
1649 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001650 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1651 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001652 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001653 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001654 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001655 return true;
1656 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001657 if (l == pl)
1658 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001659 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1660 return true;
1661 if (usr == LINK_PROTOCOL)
1662 return true;
1663 return false;
1664 }
1665 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001666}
1667
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001668/**
1669 * tipc_rcv - process TIPC packets/messages arriving from off-node
1670 * @net: the applicable net namespace
1671 * @skb: TIPC packet
1672 * @bearer: pointer to bearer message arrived on
1673 *
1674 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1675 * structure (i.e. cannot be NULL), but bearer can be inactive.
1676 */
1677void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1678{
1679 struct sk_buff_head xmitq;
1680 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001681 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001682 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001683 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001684 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001685 int usr, rc = 0;
1686 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001687
1688 __skb_queue_head_init(&xmitq);
1689
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001690 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01001691 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001692 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001693 hdr = buf_msg(skb);
1694 usr = msg_user(hdr);
1695 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001696
Jon Paul Maloy52666982015-10-22 08:51:41 -04001697 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001698 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001699 if (unlikely(usr == LINK_CONFIG))
1700 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001701 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001702 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001703 }
1704
Hamish Martinefe79052016-04-29 10:40:24 -04001705 /* Discard unicast link messages destined for another node */
1706 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1707 goto discard;
1708
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001709 /* Locate neighboring node that sent packet */
1710 n = tipc_node_find(net, msg_prevnode(hdr));
1711 if (unlikely(!n))
1712 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001713 le = &n->links[bearer_id];
1714
Jon Paul Maloy52666982015-10-22 08:51:41 -04001715 /* Ensure broadcast reception is in synch with peer's send state */
1716 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001717 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001718 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001719 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001720
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001721 /* Receive packet directly if conditions permit */
1722 tipc_node_read_lock(n);
1723 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001724 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001725 if (le->link) {
1726 rc = tipc_link_rcv(le->link, skb, &xmitq);
1727 skb = NULL;
1728 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001729 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001730 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001731 tipc_node_read_unlock(n);
1732
1733 /* Check/update node state before receiving */
1734 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001735 if (unlikely(skb_linearize(skb)))
1736 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001737 tipc_node_write_lock(n);
1738 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1739 if (le->link) {
1740 rc = tipc_link_rcv(le->link, skb, &xmitq);
1741 skb = NULL;
1742 }
1743 }
1744 tipc_node_write_unlock(n);
1745 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001746
1747 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001748 tipc_node_link_up(n, bearer_id, &xmitq);
1749
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001750 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001751 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001752
Jon Paul Maloy52666982015-10-22 08:51:41 -04001753 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1754 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001755
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001756 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1757 tipc_node_mcast_rcv(n);
1758
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001759 if (!skb_queue_empty(&le->inputq))
1760 tipc_sk_rcv(net, &le->inputq);
1761
1762 if (!skb_queue_empty(&xmitq))
1763 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1764
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001765 tipc_node_put(n);
1766discard:
1767 kfree_skb(skb);
1768}
1769
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001770void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1771 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01001772{
1773 struct tipc_net *tn = tipc_net(net);
1774 int bearer_id = b->identity;
1775 struct sk_buff_head xmitq;
1776 struct tipc_link_entry *e;
1777 struct tipc_node *n;
1778
1779 __skb_queue_head_init(&xmitq);
1780
1781 rcu_read_lock();
1782
1783 list_for_each_entry_rcu(n, &tn->node_list, list) {
1784 tipc_node_write_lock(n);
1785 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001786 if (e->link) {
1787 if (prop == TIPC_NLA_PROP_TOL)
1788 tipc_link_set_tolerance(e->link, b->tolerance,
1789 &xmitq);
1790 else if (prop == TIPC_NLA_PROP_MTU)
1791 tipc_link_set_mtu(e->link, b->mtu);
1792 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01001793 tipc_node_write_unlock(n);
1794 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1795 }
1796
1797 rcu_read_unlock();
1798}
1799
Richard Alpeb3404022016-08-18 10:33:52 +02001800int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1801{
1802 struct net *net = sock_net(skb->sk);
1803 struct tipc_net *tn = net_generic(net, tipc_net_id);
1804 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1805 struct tipc_node *peer;
1806 u32 addr;
1807 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02001808
1809 /* We identify the peer by its net */
1810 if (!info->attrs[TIPC_NLA_NET])
1811 return -EINVAL;
1812
1813 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001814 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
Johannes Bergfe521452017-04-12 14:34:08 +02001815 info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02001816 if (err)
1817 return err;
1818
1819 if (!attrs[TIPC_NLA_NET_ADDR])
1820 return -EINVAL;
1821
1822 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1823
1824 if (in_own_node(net, addr))
1825 return -ENOTSUPP;
1826
1827 spin_lock_bh(&tn->node_list_lock);
1828 peer = tipc_node_find(net, addr);
1829 if (!peer) {
1830 spin_unlock_bh(&tn->node_list_lock);
1831 return -ENXIO;
1832 }
1833
1834 tipc_node_write_lock(peer);
1835 if (peer->state != SELF_DOWN_PEER_DOWN &&
1836 peer->state != SELF_DOWN_PEER_LEAVING) {
1837 tipc_node_write_unlock(peer);
1838 err = -EBUSY;
1839 goto err_out;
1840 }
1841
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001842 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02001843 tipc_node_write_unlock(peer);
1844 tipc_node_delete(peer);
1845
1846 err = 0;
1847err_out:
1848 tipc_node_put(peer);
1849 spin_unlock_bh(&tn->node_list_lock);
1850
1851 return err;
1852}
1853
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001854int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1855{
1856 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001857 struct net *net = sock_net(skb->sk);
1858 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001859 int done = cb->args[0];
1860 int last_addr = cb->args[1];
1861 struct tipc_node *node;
1862 struct tipc_nl_msg msg;
1863
1864 if (done)
1865 return 0;
1866
1867 msg.skb = skb;
1868 msg.portid = NETLINK_CB(cb->skb).portid;
1869 msg.seq = cb->nlh->nlmsg_seq;
1870
1871 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001872 if (last_addr) {
1873 node = tipc_node_find(net, last_addr);
1874 if (!node) {
1875 rcu_read_unlock();
1876 /* We never set seq or call nl_dump_check_consistent()
1877 * this means that setting prev_seq here will cause the
1878 * consistence check to fail in the netlink callback
1879 * handler. Resulting in the NLMSG_DONE message having
1880 * the NLM_F_DUMP_INTR flag set if the node state
1881 * changed while we released the lock.
1882 */
1883 cb->prev_seq = 1;
1884 return -EPIPE;
1885 }
1886 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001887 }
1888
Ying Xuef2f98002015-01-09 15:27:05 +08001889 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001890 if (last_addr) {
1891 if (node->addr == last_addr)
1892 last_addr = 0;
1893 else
1894 continue;
1895 }
1896
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001897 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001898 err = __tipc_nl_add_node(&msg, node);
1899 if (err) {
1900 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001901 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001902 goto out;
1903 }
1904
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001905 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001906 }
1907 done = 1;
1908out:
1909 cb->args[0] = done;
1910 cb->args[1] = last_addr;
1911 rcu_read_unlock();
1912
1913 return skb->len;
1914}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001915
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001916/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001917 * @net: the applicable net namespace
1918 * @name: pointer to link name string
1919 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1920 *
1921 * Returns pointer to node owning the link, or 0 if no matching link is found.
1922 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001923static struct tipc_node *tipc_node_find_by_name(struct net *net,
1924 const char *link_name,
1925 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001926{
1927 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001928 struct tipc_link *l;
1929 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001930 struct tipc_node *found_node = NULL;
1931 int i;
1932
1933 *bearer_id = 0;
1934 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001935 list_for_each_entry_rcu(n, &tn->node_list, list) {
1936 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001937 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001938 l = n->links[i].link;
1939 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001940 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001941 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001942 break;
1943 }
1944 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001945 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001946 if (found_node)
1947 break;
1948 }
1949 rcu_read_unlock();
1950
1951 return found_node;
1952}
1953
1954int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1955{
1956 int err;
1957 int res = 0;
1958 int bearer_id;
1959 char *name;
1960 struct tipc_link *link;
1961 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01001962 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001963 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1964 struct net *net = sock_net(skb->sk);
1965
Richard Alped01332f2016-02-01 08:19:56 +01001966 __skb_queue_head_init(&xmitq);
1967
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001968 if (!info->attrs[TIPC_NLA_LINK])
1969 return -EINVAL;
1970
1971 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1972 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02001973 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001974 if (err)
1975 return err;
1976
1977 if (!attrs[TIPC_NLA_LINK_NAME])
1978 return -EINVAL;
1979
1980 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1981
1982 if (strcmp(name, tipc_bclink_name) == 0)
1983 return tipc_nl_bc_link_set(net, attrs);
1984
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001985 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001986 if (!node)
1987 return -EINVAL;
1988
1989 tipc_node_read_lock(node);
1990
1991 link = node->links[bearer_id].link;
1992 if (!link) {
1993 res = -EINVAL;
1994 goto out;
1995 }
1996
1997 if (attrs[TIPC_NLA_LINK_PROP]) {
1998 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1999
2000 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2001 props);
2002 if (err) {
2003 res = err;
2004 goto out;
2005 }
2006
2007 if (props[TIPC_NLA_PROP_TOL]) {
2008 u32 tol;
2009
2010 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002011 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002012 }
2013 if (props[TIPC_NLA_PROP_PRIO]) {
2014 u32 prio;
2015
2016 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002017 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002018 }
2019 if (props[TIPC_NLA_PROP_WIN]) {
2020 u32 win;
2021
2022 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2023 tipc_link_set_queue_limits(link, win);
2024 }
2025 }
2026
2027out:
2028 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01002029 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002030 return res;
2031}
2032
2033int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2034{
2035 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002036 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002037 struct tipc_nl_msg msg;
2038 char *name;
2039 int err;
2040
2041 msg.portid = info->snd_portid;
2042 msg.seq = info->snd_seq;
2043
Ying Xue94f6a802018-05-08 21:44:06 +08002044 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002045 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002046
2047 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2048 info->attrs[TIPC_NLA_LINK],
2049 tipc_nl_link_policy, info->extack);
2050 if (err)
2051 return err;
2052
2053 if (!attrs[TIPC_NLA_LINK_NAME])
2054 return -EINVAL;
2055
2056 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002057
2058 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2059 if (!msg.skb)
2060 return -ENOMEM;
2061
2062 if (strcmp(name, tipc_bclink_name) == 0) {
2063 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002064 if (err)
2065 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002066 } else {
2067 int bearer_id;
2068 struct tipc_node *node;
2069 struct tipc_link *link;
2070
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002071 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002072 if (!node) {
2073 err = -EINVAL;
2074 goto err_free;
2075 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002076
2077 tipc_node_read_lock(node);
2078 link = node->links[bearer_id].link;
2079 if (!link) {
2080 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002081 err = -EINVAL;
2082 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002083 }
2084
2085 err = __tipc_nl_add_link(net, &msg, link, 0);
2086 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002087 if (err)
2088 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002089 }
2090
2091 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002092
2093err_free:
2094 nlmsg_free(msg.skb);
2095 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002096}
2097
2098int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2099{
2100 int err;
2101 char *link_name;
2102 unsigned int bearer_id;
2103 struct tipc_link *link;
2104 struct tipc_node *node;
2105 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2106 struct net *net = sock_net(skb->sk);
2107 struct tipc_link_entry *le;
2108
2109 if (!info->attrs[TIPC_NLA_LINK])
2110 return -EINVAL;
2111
2112 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2113 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02002114 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002115 if (err)
2116 return err;
2117
2118 if (!attrs[TIPC_NLA_LINK_NAME])
2119 return -EINVAL;
2120
2121 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2122
2123 if (strcmp(link_name, tipc_bclink_name) == 0) {
2124 err = tipc_bclink_reset_stats(net);
2125 if (err)
2126 return err;
2127 return 0;
2128 }
2129
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002130 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002131 if (!node)
2132 return -EINVAL;
2133
2134 le = &node->links[bearer_id];
2135 tipc_node_read_lock(node);
2136 spin_lock_bh(&le->lock);
2137 link = node->links[bearer_id].link;
2138 if (!link) {
2139 spin_unlock_bh(&le->lock);
2140 tipc_node_read_unlock(node);
2141 return -EINVAL;
2142 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002143 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002144 spin_unlock_bh(&le->lock);
2145 tipc_node_read_unlock(node);
2146 return 0;
2147}
2148
2149/* Caller should hold node lock */
2150static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2151 struct tipc_node *node, u32 *prev_link)
2152{
2153 u32 i;
2154 int err;
2155
2156 for (i = *prev_link; i < MAX_BEARERS; i++) {
2157 *prev_link = i;
2158
2159 if (!node->links[i].link)
2160 continue;
2161
2162 err = __tipc_nl_add_link(net, msg,
2163 node->links[i].link, NLM_F_MULTI);
2164 if (err)
2165 return err;
2166 }
2167 *prev_link = 0;
2168
2169 return 0;
2170}
2171
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002172int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002173{
2174 struct net *net = sock_net(skb->sk);
2175 struct tipc_net *tn = net_generic(net, tipc_net_id);
2176 struct tipc_node *node;
2177 struct tipc_nl_msg msg;
2178 u32 prev_node = cb->args[0];
2179 u32 prev_link = cb->args[1];
2180 int done = cb->args[2];
2181 int err;
2182
2183 if (done)
2184 return 0;
2185
2186 msg.skb = skb;
2187 msg.portid = NETLINK_CB(cb->skb).portid;
2188 msg.seq = cb->nlh->nlmsg_seq;
2189
2190 rcu_read_lock();
2191 if (prev_node) {
2192 node = tipc_node_find(net, prev_node);
2193 if (!node) {
2194 /* We never set seq or call nl_dump_check_consistent()
2195 * this means that setting prev_seq here will cause the
2196 * consistence check to fail in the netlink callback
2197 * handler. Resulting in the last NLMSG_DONE message
2198 * having the NLM_F_DUMP_INTR flag set.
2199 */
2200 cb->prev_seq = 1;
2201 goto out;
2202 }
2203 tipc_node_put(node);
2204
2205 list_for_each_entry_continue_rcu(node, &tn->node_list,
2206 list) {
2207 tipc_node_read_lock(node);
2208 err = __tipc_nl_add_node_links(net, &msg, node,
2209 &prev_link);
2210 tipc_node_read_unlock(node);
2211 if (err)
2212 goto out;
2213
2214 prev_node = node->addr;
2215 }
2216 } else {
2217 err = tipc_nl_add_bc_link(net, &msg);
2218 if (err)
2219 goto out;
2220
2221 list_for_each_entry_rcu(node, &tn->node_list, list) {
2222 tipc_node_read_lock(node);
2223 err = __tipc_nl_add_node_links(net, &msg, node,
2224 &prev_link);
2225 tipc_node_read_unlock(node);
2226 if (err)
2227 goto out;
2228
2229 prev_node = node->addr;
2230 }
2231 }
2232 done = 1;
2233out:
2234 rcu_read_unlock();
2235
2236 cb->args[0] = prev_node;
2237 cb->args[1] = prev_link;
2238 cb->args[2] = done;
2239
2240 return skb->len;
2241}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002242
2243int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2244{
2245 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2246 struct net *net = sock_net(skb->sk);
2247 int err;
2248
2249 if (!info->attrs[TIPC_NLA_MON])
2250 return -EINVAL;
2251
2252 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2253 info->attrs[TIPC_NLA_MON],
Johannes Bergfe521452017-04-12 14:34:08 +02002254 tipc_nl_monitor_policy, info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002255 if (err)
2256 return err;
2257
2258 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2259 u32 val;
2260
2261 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2262 err = tipc_nl_monitor_set_threshold(net, val);
2263 if (err)
2264 return err;
2265 }
2266
2267 return 0;
2268}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002269
2270static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2271{
2272 struct nlattr *attrs;
2273 void *hdr;
2274 u32 val;
2275
2276 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2277 0, TIPC_NL_MON_GET);
2278 if (!hdr)
2279 return -EMSGSIZE;
2280
2281 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2282 if (!attrs)
2283 goto msg_full;
2284
2285 val = tipc_nl_monitor_get_threshold(net);
2286
2287 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2288 goto attr_msg_full;
2289
2290 nla_nest_end(msg->skb, attrs);
2291 genlmsg_end(msg->skb, hdr);
2292
2293 return 0;
2294
2295attr_msg_full:
2296 nla_nest_cancel(msg->skb, attrs);
2297msg_full:
2298 genlmsg_cancel(msg->skb, hdr);
2299
2300 return -EMSGSIZE;
2301}
2302
2303int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2304{
2305 struct net *net = sock_net(skb->sk);
2306 struct tipc_nl_msg msg;
2307 int err;
2308
2309 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002310 if (!msg.skb)
2311 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002312 msg.portid = info->snd_portid;
2313 msg.seq = info->snd_seq;
2314
2315 err = __tipc_nl_add_monitor_prop(net, &msg);
2316 if (err) {
2317 nlmsg_free(msg.skb);
2318 return err;
2319 }
2320
2321 return genlmsg_reply(msg.skb, info);
2322}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002323
2324int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2325{
2326 struct net *net = sock_net(skb->sk);
2327 u32 prev_bearer = cb->args[0];
2328 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002329 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002330 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002331
2332 if (prev_bearer == MAX_BEARERS)
2333 return 0;
2334
2335 msg.skb = skb;
2336 msg.portid = NETLINK_CB(cb->skb).portid;
2337 msg.seq = cb->nlh->nlmsg_seq;
2338
2339 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002340 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002341 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002342 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002343 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002344 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002345 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002346 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002347
2348 return skb->len;
2349}
2350
2351int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2352 struct netlink_callback *cb)
2353{
2354 struct net *net = sock_net(skb->sk);
2355 u32 prev_node = cb->args[1];
2356 u32 bearer_id = cb->args[2];
2357 int done = cb->args[0];
2358 struct tipc_nl_msg msg;
2359 int err;
2360
2361 if (!prev_node) {
2362 struct nlattr **attrs;
2363 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2364
2365 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2366 if (err)
2367 return err;
2368
2369 if (!attrs[TIPC_NLA_MON])
2370 return -EINVAL;
2371
2372 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2373 attrs[TIPC_NLA_MON],
Johannes Bergfceb6432017-04-12 14:34:07 +02002374 tipc_nl_monitor_policy, NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002375 if (err)
2376 return err;
2377
2378 if (!mon[TIPC_NLA_MON_REF])
2379 return -EINVAL;
2380
2381 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2382
2383 if (bearer_id >= MAX_BEARERS)
2384 return -EINVAL;
2385 }
2386
2387 if (done)
2388 return 0;
2389
2390 msg.skb = skb;
2391 msg.portid = NETLINK_CB(cb->skb).portid;
2392 msg.seq = cb->nlh->nlmsg_seq;
2393
2394 rtnl_lock();
2395 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2396 if (!err)
2397 done = 1;
2398
2399 rtnl_unlock();
2400 cb->args[0] = done;
2401 cb->args[1] = prev_node;
2402 cb->args[2] = bearer_id;
2403
2404 return skb->len;
2405}