blob: b66d2f67b1dd274495ef2dc88a5471097726472c [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"
Tuong Lienb4b97712018-12-19 09:17:56 +070046#include "trace.h"
Jon Paul Maloyc8199302015-10-15 14:52:46 -040047
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050048#define INVALID_NODE_SIG 0x10000
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +020049#define NODE_CLEANUP_AFTER 300000
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050050
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050051/* Flags used to take different actions according to flag type
52 * TIPC_NOTIFY_NODE_DOWN: notify node is down
53 * TIPC_NOTIFY_NODE_UP: notify node is up
54 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
55 */
56enum {
57 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
58 TIPC_NOTIFY_NODE_UP = (1 << 4),
59 TIPC_NOTIFY_LINK_UP = (1 << 6),
60 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
61};
62
63struct tipc_link_entry {
64 struct tipc_link *link;
65 spinlock_t lock; /* per link */
66 u32 mtu;
67 struct sk_buff_head inputq;
68 struct tipc_media_addr maddr;
69};
70
71struct tipc_bclink_entry {
72 struct tipc_link *link;
73 struct sk_buff_head inputq1;
74 struct sk_buff_head arrvq;
75 struct sk_buff_head inputq2;
76 struct sk_buff_head namedq;
77};
78
79/**
80 * struct tipc_node - TIPC node structure
81 * @addr: network address of node
82 * @ref: reference counter to node object
83 * @lock: rwlock governing access to structure
84 * @net: the applicable net namespace
85 * @hash: links to adjacent nodes in unsorted hash chain
86 * @inputq: pointer to input queue containing messages for msg event
87 * @namedq: pointer to name table input queue with name table messages
88 * @active_links: bearer ids of active links, used as index into links[] array
89 * @links: array containing references to all links to node
90 * @action_flags: bit mask of different types of node actions
91 * @state: connectivity state vs peer node
92 * @sync_point: sequence number where synch/failover is finished
93 * @list: links to adjacent nodes in sorted list of cluster's nodes
94 * @working_links: number of working links to node (both active and standby)
95 * @link_cnt: number of links to node
96 * @capabilities: bitmap, indicating peer node's functional capabilities
97 * @signature: node instance identifier
98 * @link_id: local and remote bearer ids of changing link, if any
99 * @publ_list: list of publications
100 * @rcu: rcu struct for tipc_node
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200101 * @delete_at: indicates the time for deleting a down node
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500102 */
103struct tipc_node {
104 u32 addr;
105 struct kref kref;
106 rwlock_t lock;
107 struct net *net;
108 struct hlist_node hash;
109 int active_links[2];
110 struct tipc_link_entry links[MAX_BEARERS];
111 struct tipc_bclink_entry bc_entry;
112 int action_flags;
113 struct list_head list;
114 int state;
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200115 bool failover_sent;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500116 u16 sync_point;
117 int link_cnt;
118 u16 working_links;
119 u16 capabilities;
120 u32 signature;
121 u32 link_id;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100122 u8 peer_id[16];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500123 struct list_head publ_list;
124 struct list_head conn_sks;
125 unsigned long keepalive_intv;
126 struct timer_list timer;
127 struct rcu_head rcu;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200128 unsigned long delete_at;
Hoang Lef73b1282019-10-29 07:51:21 +0700129 struct net *peer_net;
130 u32 peer_hash_mix;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500131};
132
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400133/* Node FSM states and events:
134 */
135enum {
136 SELF_DOWN_PEER_DOWN = 0xdd,
137 SELF_UP_PEER_UP = 0xaa,
138 SELF_DOWN_PEER_LEAVING = 0xd1,
139 SELF_UP_PEER_COMING = 0xac,
140 SELF_COMING_PEER_UP = 0xca,
141 SELF_LEAVING_PEER_DOWN = 0x1d,
142 NODE_FAILINGOVER = 0xf0,
143 NODE_SYNCHING = 0xcc
144};
145
146enum {
147 SELF_ESTABL_CONTACT_EVT = 0xece,
148 SELF_LOST_CONTACT_EVT = 0x1ce,
149 PEER_ESTABL_CONTACT_EVT = 0x9ece,
150 PEER_LOST_CONTACT_EVT = 0x91ce,
151 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
152 NODE_FAILOVER_END_EVT = 0xfee,
153 NODE_SYNCH_BEGIN_EVT = 0xcbe,
154 NODE_SYNCH_END_EVT = 0xcee
155};
156
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400157static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
158 struct sk_buff_head *xmitq,
159 struct tipc_media_addr **maddr);
160static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
161 bool delete);
162static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800163static void tipc_node_delete(struct tipc_node *node);
Kees Cook31b102b2017-10-30 14:06:45 -0700164static void tipc_node_timeout(struct timer_list *t);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400165static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500166static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100167static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500168static void tipc_node_put(struct tipc_node *node);
Jon Maloy38077b82017-10-13 11:04:19 +0200169static bool node_is_up(struct tipc_node *n);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200170static void tipc_node_delete_from_list(struct tipc_node *node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400172struct tipc_sock_conn {
173 u32 port;
174 u32 peer_port;
175 u32 peer_node;
176 struct list_head list;
177};
178
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500179static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
180{
181 int bearer_id = n->active_links[sel & 1];
182
183 if (unlikely(bearer_id == INVALID_BEARER_ID))
184 return NULL;
185
186 return n->links[bearer_id].link;
187}
188
Hoang Lef73b1282019-10-29 07:51:21 +0700189int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500190{
191 struct tipc_node *n;
192 int bearer_id;
193 unsigned int mtu = MAX_MSG_SIZE;
194
195 n = tipc_node_find(net, addr);
196 if (unlikely(!n))
197 return mtu;
198
Hoang Lef73b1282019-10-29 07:51:21 +0700199 /* Allow MAX_MSG_SIZE when building connection oriented message
200 * if they are in the same core network
201 */
202 if (n->peer_net && connected) {
203 tipc_node_put(n);
204 return mtu;
205 }
206
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500207 bearer_id = n->active_links[sel & 1];
208 if (likely(bearer_id != INVALID_BEARER_ID))
209 mtu = n->links[bearer_id].mtu;
210 tipc_node_put(n);
211 return mtu;
212}
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400213
Jon Maloy3e5cf362018-04-25 19:29:36 +0200214bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
215{
216 u8 *own_id = tipc_own_id(net);
217 struct tipc_node *n;
218
219 if (!own_id)
220 return true;
221
222 if (addr == tipc_own_addr(net)) {
223 memcpy(id, own_id, TIPC_NODEID_LEN);
224 return true;
225 }
226 n = tipc_node_find(net, addr);
227 if (!n)
228 return false;
229
230 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
231 tipc_node_put(n);
232 return true;
233}
234
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400235u16 tipc_node_get_capabilities(struct net *net, u32 addr)
236{
237 struct tipc_node *n;
238 u16 caps;
239
240 n = tipc_node_find(net, addr);
241 if (unlikely(!n))
242 return TIPC_NODE_CAPABILITIES;
243 caps = n->capabilities;
244 tipc_node_put(n);
245 return caps;
246}
247
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800248static void tipc_node_kref_release(struct kref *kref)
249{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500250 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800251
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500252 kfree(n->bc_entry.link);
253 kfree_rcu(n, rcu);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800254}
255
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500256static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800257{
258 kref_put(&node->kref, tipc_node_kref_release);
259}
260
261static void tipc_node_get(struct tipc_node *node)
262{
263 kref_get(&node->kref);
264}
265
Allan Stephensa635b462011-11-04 11:54:43 -0400266/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500267 * tipc_node_find - locate specified node object, if it exists
268 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500269static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500270{
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500271 struct tipc_net *tn = tipc_net(net);
Allan Stephens672d99e2011-02-25 18:42:52 -0500272 struct tipc_node *node;
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500273 unsigned int thash = tipc_hashfn(addr);
Allan Stephens672d99e2011-02-25 18:42:52 -0500274
Ying Xue6c7a7622014-03-27 12:54:37 +0800275 rcu_read_lock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500276 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
277 if (node->addr != addr)
278 continue;
279 if (!kref_get_unless_zero(&node->kref))
280 node = NULL;
281 break;
Allan Stephens672d99e2011-02-25 18:42:52 -0500282 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800283 rcu_read_unlock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500284 return node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500285}
286
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100287/* tipc_node_find_by_id - locate specified node object by its 128-bit id
288 * Note: this function is called only when a discovery request failed
289 * to find the node by its 32-bit id, and is not time critical
290 */
291static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
292{
293 struct tipc_net *tn = tipc_net(net);
294 struct tipc_node *n;
295 bool found = false;
296
297 rcu_read_lock();
298 list_for_each_entry_rcu(n, &tn->node_list, list) {
299 read_lock_bh(&n->lock);
300 if (!memcmp(id, n->peer_id, 16) &&
301 kref_get_unless_zero(&n->kref))
302 found = true;
303 read_unlock_bh(&n->lock);
304 if (found)
305 break;
306 }
307 rcu_read_unlock();
308 return found ? n : NULL;
309}
310
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500311static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500312{
313 read_lock_bh(&n->lock);
314}
315
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500316static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500317{
318 read_unlock_bh(&n->lock);
319}
320
321static void tipc_node_write_lock(struct tipc_node *n)
322{
323 write_lock_bh(&n->lock);
324}
325
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100326static void tipc_node_write_unlock_fast(struct tipc_node *n)
327{
328 write_unlock_bh(&n->lock);
329}
330
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500331static void tipc_node_write_unlock(struct tipc_node *n)
332{
333 struct net *net = n->net;
334 u32 addr = 0;
335 u32 flags = n->action_flags;
336 u32 link_id = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400337 u32 bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500338 struct list_head *publ_list;
339
340 if (likely(!flags)) {
341 write_unlock_bh(&n->lock);
342 return;
343 }
344
345 addr = n->addr;
346 link_id = n->link_id;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400347 bearer_id = link_id & 0xffff;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500348 publ_list = &n->publ_list;
349
350 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
351 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
352
353 write_unlock_bh(&n->lock);
354
355 if (flags & TIPC_NOTIFY_NODE_DOWN)
356 tipc_publ_notify(net, publ_list, addr);
357
358 if (flags & TIPC_NOTIFY_NODE_UP)
359 tipc_named_node_up(net, addr);
360
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400361 if (flags & TIPC_NOTIFY_LINK_UP) {
362 tipc_mon_peer_up(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500363 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
Jon Maloy218527f2018-03-29 23:20:41 +0200364 TIPC_NODE_SCOPE, link_id, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400365 }
366 if (flags & TIPC_NOTIFY_LINK_DOWN) {
367 tipc_mon_peer_down(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500368 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
Jon Maloy37922ea2018-03-29 23:20:43 +0200369 addr, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400370 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500371}
372
Hoang Lef73b1282019-10-29 07:51:21 +0700373static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
374{
375 int net_id = tipc_netid(n->net);
376 struct tipc_net *tn_peer;
377 struct net *tmp;
378 u32 hash_chk;
379
380 if (n->peer_net)
381 return;
382
383 for_each_net_rcu(tmp) {
384 tn_peer = tipc_net(tmp);
385 if (!tn_peer)
386 continue;
387 /* Integrity checking whether node exists in namespace or not */
388 if (tn_peer->net_id != net_id)
389 continue;
390 if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
391 continue;
392 hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
393 if (hash_mixes ^ hash_chk)
394 continue;
395 n->peer_net = tmp;
396 n->peer_hash_mix = hash_mixes;
397 break;
398 }
399}
400
Wei Yongjune1a22d12018-03-26 14:33:13 +0000401static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
Hoang Lef73b1282019-10-29 07:51:21 +0700402 u8 *peer_id, u16 capabilities,
403 u32 signature, u32 hash_mixes)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404{
Ying Xuef2f98002015-01-09 15:27:05 +0800405 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500406 struct tipc_node *n, *temp_node;
Jon Maloy9012de52018-07-10 01:07:35 +0200407 struct tipc_link *l;
408 int bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500409 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410
Ying Xuef2f98002015-01-09 15:27:05 +0800411 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500412 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400413 if (n) {
Hoang Lef73b1282019-10-29 07:51:21 +0700414 if (n->peer_hash_mix ^ hash_mixes)
415 tipc_node_assign_peer_net(n, hash_mixes);
Jon Maloy40999f12018-07-18 19:50:06 +0200416 if (n->capabilities == capabilities)
417 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400418 /* Same node may come back with new capabilities */
Jon Maloy909620f2019-04-11 21:56:28 +0200419 tipc_node_write_lock(n);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400420 n->capabilities = capabilities;
Jon Maloy9012de52018-07-10 01:07:35 +0200421 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
422 l = n->links[bearer_id].link;
423 if (l)
424 tipc_link_update_caps(l, capabilities);
425 }
Jon Maloy909620f2019-04-11 21:56:28 +0200426 tipc_node_write_unlock_fast(n);
427
Hoang Leff2ebbf2019-03-19 18:49:49 +0700428 /* Calculate cluster capabilities */
429 tn->capabilities = TIPC_NODE_CAPABILITIES;
430 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
431 tn->capabilities &= temp_node->capabilities;
432 }
Hoang Lef73b1282019-10-29 07:51:21 +0700433
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500434 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400435 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500436 n = kzalloc(sizeof(*n), GFP_ATOMIC);
437 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400438 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500439 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700440 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500441 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100442 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500443 n->net = net;
Hoang Lef73b1282019-10-29 07:51:21 +0700444 n->peer_net = NULL;
445 n->peer_hash_mix = 0;
446 /* Assign kernel local namespace if exists */
447 tipc_node_assign_peer_net(n, hash_mixes);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500448 n->capabilities = capabilities;
449 kref_init(&n->kref);
450 rwlock_init(&n->lock);
451 INIT_HLIST_NODE(&n->hash);
452 INIT_LIST_HEAD(&n->list);
453 INIT_LIST_HEAD(&n->publ_list);
454 INIT_LIST_HEAD(&n->conn_sks);
455 skb_queue_head_init(&n->bc_entry.namedq);
456 skb_queue_head_init(&n->bc_entry.inputq1);
457 __skb_queue_head_init(&n->bc_entry.arrvq);
458 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500459 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500460 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500461 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200462 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500463 n->signature = INVALID_NODE_SIG;
464 n->active_links[0] = INVALID_BEARER_ID;
465 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100466 if (!tipc_link_bc_create(net, tipc_own_addr(net),
467 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500468 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500469 n->capabilities,
470 &n->bc_entry.inputq1,
471 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400472 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500473 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400474 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500475 kfree(n);
476 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400477 goto exit;
478 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500479 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700480 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500481 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500482 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
483 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
484 if (n->addr < temp_node->addr)
485 break;
486 }
487 list_add_tail_rcu(&n->list, &temp_node->list);
Hoang Leff2ebbf2019-03-19 18:49:49 +0700488 /* Calculate cluster capabilities */
489 tn->capabilities = TIPC_NODE_CAPABILITIES;
490 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
491 tn->capabilities &= temp_node->capabilities;
492 }
Tuong Lieneb18a512018-12-19 09:17:59 +0700493 trace_tipc_node_create(n, true, " ");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500494exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800495 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500496 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497}
498
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400499static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
500{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500501 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400502 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400503
504 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400505 if (intv < n->keepalive_intv)
506 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400507
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400508 /* Ensure link's abort limit corresponds to current tolerance */
509 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400510}
511
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200512static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800514 list_del_rcu(&node->list);
515 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500516 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200517}
518
519static void tipc_node_delete(struct tipc_node *node)
520{
Tuong Lieneb18a512018-12-19 09:17:59 +0700521 trace_tipc_node_delete(node, true, " ");
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200522 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500523
524 del_timer_sync(&node->timer);
525 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526}
527
Ying Xuef2f98002015-01-09 15:27:05 +0800528void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800529{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500530 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800531 struct tipc_node *node, *t_node;
532
Ying Xuef2f98002015-01-09 15:27:05 +0800533 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500534 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
535 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800536 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800537}
538
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500539void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
540{
541 struct tipc_node *n;
542
543 if (in_own_node(net, addr))
544 return;
545
546 n = tipc_node_find(net, addr);
547 if (!n) {
548 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
549 return;
550 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500551 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500552 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100553 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500554 tipc_node_put(n);
555}
556
557void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
558{
559 struct tipc_node *n;
560
561 if (in_own_node(net, addr))
562 return;
563
564 n = tipc_node_find(net, addr);
565 if (!n) {
566 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
567 return;
568 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500569 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500570 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100571 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500572 tipc_node_put(n);
573}
574
Ying Xuef2f98002015-01-09 15:27:05 +0800575int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400576{
577 struct tipc_node *node;
578 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800579 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400580
Ying Xue34747532015-01-09 15:27:10 +0800581 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400582 return 0;
583
Ying Xuef2f98002015-01-09 15:27:05 +0800584 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400585 if (!node) {
586 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
587 return -EHOSTUNREACH;
588 }
589 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800590 if (!conn) {
591 err = -EHOSTUNREACH;
592 goto exit;
593 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400594 conn->peer_node = dnode;
595 conn->port = port;
596 conn->peer_port = peer_port;
597
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500598 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400599 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500600 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800601exit:
602 tipc_node_put(node);
603 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400604}
605
Ying Xuef2f98002015-01-09 15:27:05 +0800606void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400607{
608 struct tipc_node *node;
609 struct tipc_sock_conn *conn, *safe;
610
Ying Xue34747532015-01-09 15:27:10 +0800611 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400612 return;
613
Ying Xuef2f98002015-01-09 15:27:05 +0800614 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400615 if (!node)
616 return;
617
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500618 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400619 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
620 if (port != conn->port)
621 continue;
622 list_del(&conn->list);
623 kfree(conn);
624 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500625 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800626 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400627}
628
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200629static void tipc_node_clear_links(struct tipc_node *node)
630{
631 int i;
632
633 for (i = 0; i < MAX_BEARERS; i++) {
634 struct tipc_link_entry *le = &node->links[i];
635
636 if (le->link) {
637 kfree(le->link);
638 le->link = NULL;
639 node->link_cnt--;
640 }
641 }
642}
643
644/* tipc_node_cleanup - delete nodes that does not
645 * have active links for NODE_CLEANUP_AFTER time
646 */
Jon Maloyec835f892018-11-26 12:26:14 -0500647static bool tipc_node_cleanup(struct tipc_node *peer)
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200648{
Hoang Leff2ebbf2019-03-19 18:49:49 +0700649 struct tipc_node *temp_node;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200650 struct tipc_net *tn = tipc_net(peer->net);
651 bool deleted = false;
652
Jon Maloyec835f892018-11-26 12:26:14 -0500653 /* If lock held by tipc_node_stop() the node will be deleted anyway */
654 if (!spin_trylock_bh(&tn->node_list_lock))
655 return false;
656
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200657 tipc_node_write_lock(peer);
658
659 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
660 tipc_node_clear_links(peer);
661 tipc_node_delete_from_list(peer);
662 deleted = true;
663 }
664 tipc_node_write_unlock(peer);
Hoang Leff2ebbf2019-03-19 18:49:49 +0700665
Hoang Le6708ef72019-11-06 13:26:09 +0700666 if (!deleted) {
667 spin_unlock_bh(&tn->node_list_lock);
668 return deleted;
669 }
670
Hoang Leff2ebbf2019-03-19 18:49:49 +0700671 /* Calculate cluster capabilities */
672 tn->capabilities = TIPC_NODE_CAPABILITIES;
673 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
674 tn->capabilities &= temp_node->capabilities;
675 }
676
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200677 spin_unlock_bh(&tn->node_list_lock);
678 return deleted;
679}
680
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400681/* tipc_node_timeout - handle expiration of node timer
682 */
Kees Cook31b102b2017-10-30 14:06:45 -0700683static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400684{
Kees Cook31b102b2017-10-30 14:06:45 -0700685 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400686 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400687 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200688 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400689 int bearer_id;
690 int rc = 0;
691
Tuong Lieneb18a512018-12-19 09:17:59 +0700692 trace_tipc_node_timeout(n, false, " ");
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200693 if (!node_is_up(n) && tipc_node_cleanup(n)) {
694 /*Removing the reference of Timer*/
695 tipc_node_put(n);
696 return;
697 }
698
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400699 __skb_queue_head_init(&xmitq);
700
Hoang Lef5d6c3e2018-12-06 09:00:09 +0700701 /* Initial node interval to value larger (10 seconds), then it will be
702 * recalculated with link lowest tolerance
703 */
704 tipc_node_read_lock(n);
705 n->keepalive_intv = 10000;
706 tipc_node_read_unlock(n);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200707 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500708 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400709 le = &n->links[bearer_id];
710 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200711 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400712 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400713 tipc_node_calculate_timer(n, le->link);
714 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200715 spin_unlock_bh(&le->lock);
716 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400717 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500718 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400719 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
720 if (rc & TIPC_LINK_DOWN_EVT)
721 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400722 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400723 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400724}
725
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400727 * __tipc_node_link_up - handle addition of link
728 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Link becomes active (alone or shared) or standby, depending on its priority.
730 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400731static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
732 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400734 int *slot0 = &n->active_links[0];
735 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400736 struct tipc_link *ol = node_active_link(n, 0);
737 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400739 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400740 return;
741
742 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
743 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400744 return;
745
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400746 n->working_links++;
747 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500748 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400749
750 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500751 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800752
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400753 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400754 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400755
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100756 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500757 tipc_link_name(nl), tipc_link_plane(nl));
Tuong Lieneb18a512018-12-19 09:17:59 +0700758 trace_tipc_node_link_up(n, true, " ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400760 /* Ensure that a STATE message goes first */
761 tipc_link_build_state_msg(nl, xmitq);
762
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400763 /* First link? => give it both slots */
764 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400765 *slot0 = bearer_id;
766 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400767 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
768 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400769 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400770 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400771 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400773
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400774 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500775 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
776 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400777 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400778 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400779 tipc_link_set_active(nl, true);
780 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500781 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400782 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a842015-10-22 08:51:47 -0400783 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400784 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500785 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400788 /* Prepare synchronization with first link */
789 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790}
791
792/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400793 * tipc_node_link_up - handle addition of link
794 *
795 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400797static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
798 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400800 struct tipc_media_addr *maddr;
801
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500802 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400803 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400804 maddr = &n->links[bearer_id].maddr;
805 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500806 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400807}
808
809/**
Tuong Lienc0b14a082019-05-02 17:23:23 +0700810 * tipc_node_link_failover() - start failover in case "half-failover"
811 *
812 * This function is only called in a very special situation where link
813 * failover can be already started on peer node but not on this node.
814 * This can happen when e.g.
815 * 1. Both links <1A-2A>, <1B-2B> down
816 * 2. Link endpoint 2A up, but 1A still down (e.g. due to network
817 * disturbance, wrong session, etc.)
818 * 3. Link <1B-2B> up
819 * 4. Link endpoint 2A down (e.g. due to link tolerance timeout)
Tuong Liend0f84d02019-06-17 11:56:12 +0700820 * 5. Node 2 starts failover onto link <1B-2B>
Tuong Lienc0b14a082019-05-02 17:23:23 +0700821 *
Tuong Liend0f84d02019-06-17 11:56:12 +0700822 * ==> Node 1 does never start link/node failover!
Tuong Lienc0b14a082019-05-02 17:23:23 +0700823 *
824 * @n: tipc node structure
825 * @l: link peer endpoint failingover (- can be NULL)
826 * @tnl: tunnel link
827 * @xmitq: queue for messages to be xmited on tnl link later
828 */
829static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
830 struct tipc_link *tnl,
831 struct sk_buff_head *xmitq)
832{
833 /* Avoid to be "self-failover" that can never end */
834 if (!tipc_link_is_up(tnl))
835 return;
836
Tuong Liend0f84d02019-06-17 11:56:12 +0700837 /* Don't rush, failure link may be in the process of resetting */
838 if (l && !tipc_link_is_reset(l))
839 return;
840
Tuong Lienc0b14a082019-05-02 17:23:23 +0700841 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
842 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
843
844 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
845 tipc_link_failover_prepare(l, tnl, xmitq);
846
847 if (l)
848 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
849 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
850}
851
852/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400853 * __tipc_node_link_down - handle loss of link
854 */
855static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
856 struct sk_buff_head *xmitq,
857 struct tipc_media_addr **maddr)
858{
859 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400860 int *slot0 = &n->active_links[0];
861 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500862 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400863 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400865 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400866 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400867 return;
868
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400869 n->working_links--;
870 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500871 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700872
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400873 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400874
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100875 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500876 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400878 /* Select new active link if any available */
879 *slot0 = INVALID_BEARER_ID;
880 *slot1 = INVALID_BEARER_ID;
881 for (i = 0; i < MAX_BEARERS; i++) {
882 _l = n->links[i].link;
883 if (!_l || !tipc_link_is_up(_l))
884 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400885 if (_l == l)
886 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500887 prio = tipc_link_prio(_l);
888 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400889 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500890 if (prio > highest) {
891 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400892 *slot0 = i;
893 *slot1 = i;
894 continue;
895 }
896 *slot1 = i;
897 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400898
Jon Maloy38077b82017-10-13 11:04:19 +0200899 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400900 if (tipc_link_peer_is_down(l))
901 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
902 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
Tuong Lien26574db2018-12-19 09:17:57 +0700903 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400904 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400905 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400906 tipc_link_build_reset_msg(l, xmitq);
907 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400908 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400909 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400910 return;
911 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400912 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400913
914 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500915 *bearer_id = n->active_links[0];
916 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400917 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
918 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500919 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400920 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Tuong Lien26574db2018-12-19 09:17:57 +0700921 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400922 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400923 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400924 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400925 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500926 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400927}
928
929static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
930{
931 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Maloy737889e2019-03-22 15:03:51 +0100932 struct tipc_media_addr *maddr = NULL;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400933 struct tipc_link *l = le->link;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400934 int old_bearer_id = bearer_id;
Jon Maloy737889e2019-03-22 15:03:51 +0100935 struct sk_buff_head xmitq;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400936
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400937 if (!l)
938 return;
939
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400940 __skb_queue_head_init(&xmitq);
941
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500942 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400943 if (!tipc_link_is_establishing(l)) {
944 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400945 } else {
946 /* Defuse pending tipc_node_link_up() */
Tuong Lien91986ee2019-02-11 13:29:43 +0700947 tipc_link_reset(l);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400948 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400949 }
Tuong Lien91986ee2019-02-11 13:29:43 +0700950 if (delete) {
951 kfree(l);
952 le->link = NULL;
953 n->link_cnt--;
954 }
Tuong Lieneb18a512018-12-19 09:17:59 +0700955 trace_tipc_node_link_down(n, true, "node link down or deleted!");
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500956 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400957 if (delete)
958 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Maloy737889e2019-03-22 15:03:51 +0100959 if (!skb_queue_empty(&xmitq))
960 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400961 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962}
963
Jon Maloy38077b82017-10-13 11:04:19 +0200964static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400966 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967}
968
Jon Maloy38077b82017-10-13 11:04:19 +0200969bool tipc_node_is_up(struct net *net, u32 addr)
970{
971 struct tipc_node *n;
972 bool retval = false;
973
974 if (in_own_node(net, addr))
975 return true;
976
977 n = tipc_node_find(net, addr);
978 if (!n)
979 return false;
980 retval = node_is_up(n);
981 tipc_node_put(n);
982 return retval;
983}
984
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100985static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
986{
987 struct tipc_node *n;
988
989 addr ^= tipc_net(net)->random;
990 while ((n = tipc_node_find(net, addr))) {
991 tipc_node_put(n);
992 addr++;
993 }
994 return addr;
995}
996
997/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
Jon Maloy2a57f182018-07-06 20:10:03 +0200998 * Returns suggested address if any, otherwise 0
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100999 */
1000u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
1001{
1002 struct tipc_net *tn = tipc_net(net);
1003 struct tipc_node *n;
1004
1005 /* Suggest new address if some other peer is using this one */
1006 n = tipc_node_find(net, addr);
1007 if (n) {
1008 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
1009 addr = 0;
1010 tipc_node_put(n);
1011 if (!addr)
1012 return 0;
1013 return tipc_node_suggest_addr(net, addr);
1014 }
1015
1016 /* Suggest previously used address if peer is known */
1017 n = tipc_node_find_by_id(net, id);
1018 if (n) {
1019 addr = n->addr;
1020 tipc_node_put(n);
Jon Maloy2a57f182018-07-06 20:10:03 +02001021 return addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001022 }
Jon Maloy2a57f182018-07-06 20:10:03 +02001023
1024 /* Even this node may be in conflict */
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001025 if (tn->trial_addr == addr)
1026 return tipc_node_suggest_addr(net, addr);
1027
Jon Maloy2a57f182018-07-06 20:10:03 +02001028 return 0;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001029}
1030
1031void tipc_node_check_dest(struct net *net, u32 addr,
1032 u8 *peer_id, struct tipc_bearer *b,
Hoang Lef73b1282019-10-29 07:51:21 +07001033 u16 capabilities, u32 signature, u32 hash_mixes,
Jon Paul Maloycf148812015-07-30 18:24:22 -04001034 struct tipc_media_addr *maddr,
1035 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001036{
Jon Paul Maloycf148812015-07-30 18:24:22 -04001037 struct tipc_node *n;
1038 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001039 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001040 bool addr_match = false;
1041 bool sign_match = false;
1042 bool link_up = false;
1043 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001044 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001045 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001046 unsigned long intv;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001047 u16 session;
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001048
Jon Paul Maloycf148812015-07-30 18:24:22 -04001049 *dupl_addr = false;
1050 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001051
Hoang Lef73b1282019-10-29 07:51:21 +07001052 n = tipc_node_create(net, addr, peer_id, capabilities, signature,
1053 hash_mixes);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001054 if (!n)
1055 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001056
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001057 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001058
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001059 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -04001060
1061 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001062 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001063 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001064 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001065 sign_match = (signature == n->signature);
1066
1067 /* These three flags give us eight permutations: */
1068
1069 if (sign_match && addr_match && link_up) {
1070 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001071 reset = false;
Hoang Led408bef2019-11-08 10:02:37 +07001072 /* Peer node is not a container/local namespace */
1073 if (!n->peer_hash_mix)
1074 n->peer_hash_mix = hash_mixes;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001075 } else if (sign_match && addr_match && !link_up) {
1076 /* Respond. The link will come up in due time */
1077 *respond = true;
1078 } else if (sign_match && !addr_match && link_up) {
1079 /* Peer has changed i/f address without rebooting.
1080 * If so, the link will reset soon, and the next
1081 * discovery will be accepted. So we can ignore it.
1082 * It may also be an cloned or malicious peer having
1083 * chosen the same node address and signature as an
1084 * existing one.
1085 * Ignore requests until the link goes down, if ever.
1086 */
1087 *dupl_addr = true;
1088 } else if (sign_match && !addr_match && !link_up) {
1089 /* Peer link has changed i/f address without rebooting.
1090 * It may also be a cloned or malicious peer; we can't
1091 * distinguish between the two.
1092 * The signature is correct, so we must accept.
1093 */
1094 accept_addr = true;
1095 *respond = true;
1096 } else if (!sign_match && addr_match && link_up) {
1097 /* Peer node rebooted. Two possibilities:
1098 * - Delayed re-discovery; this link endpoint has already
1099 * reset and re-established contact with the peer, before
1100 * receiving a discovery message from that node.
1101 * (The peer happened to receive one from this node first).
1102 * - The peer came back so fast that our side has not
1103 * discovered it yet. Probing from this side will soon
1104 * reset the link, since there can be no working link
1105 * endpoint at the peer end, and the link will re-establish.
1106 * Accept the signature, since it comes from a known peer.
1107 */
1108 n->signature = signature;
1109 } else if (!sign_match && addr_match && !link_up) {
1110 /* The peer node has rebooted.
1111 * Accept signature, since it is a known peer.
1112 */
1113 n->signature = signature;
1114 *respond = true;
1115 } else if (!sign_match && !addr_match && link_up) {
1116 /* Peer rebooted with new address, or a new/duplicate peer.
1117 * Ignore until the link goes down, if ever.
1118 */
1119 *dupl_addr = true;
1120 } else if (!sign_match && !addr_match && !link_up) {
1121 /* Peer rebooted with new address, or it is a new peer.
1122 * Accept signature and address.
1123 */
1124 n->signature = signature;
1125 accept_addr = true;
1126 *respond = true;
1127 }
1128
1129 if (!accept_addr)
1130 goto exit;
1131
1132 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001133 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +01001134 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001135 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +01001136
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001137 if_name = strchr(b->name, ':') + 1;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001138 get_random_bytes(&session, sizeof(u16));
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001139 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001140 b->net_plane, b->mtu, b->priority,
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001141 b->window, session,
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001142 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -04001143 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001144 tipc_bc_sndlink(n->net), n->bc_entry.link,
1145 &le->inputq,
1146 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -04001147 *respond = false;
1148 goto exit;
1149 }
Tuong Lien26574db2018-12-19 09:17:57 +07001150 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001151 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -04001152 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001153 if (n->state == NODE_FAILINGOVER)
1154 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001155 le->link = l;
1156 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001157 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001158 if (n->link_cnt == 1) {
1159 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1160 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001161 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001162 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001163 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001164 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001165exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001166 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001167 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001168 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001169 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001170}
1171
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001172void tipc_node_delete_links(struct net *net, int bearer_id)
1173{
1174 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001175 struct tipc_node *n;
1176
1177 rcu_read_lock();
1178 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001179 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001180 }
1181 rcu_read_unlock();
1182}
1183
1184static void tipc_node_reset_links(struct tipc_node *n)
1185{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001186 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001187
Jon Maloyd50ccc22018-03-22 20:42:50 +01001188 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001189
Tuong Lieneb18a512018-12-19 09:17:59 +07001190 trace_tipc_node_reset_links(n, true, " ");
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001191 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001192 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001193 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001194}
1195
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001196/* tipc_node_fsm_evt - node finite state machine
1197 * Determines when contact is allowed with peer node
1198 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001199static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001200{
1201 int state = n->state;
1202
1203 switch (state) {
1204 case SELF_DOWN_PEER_DOWN:
1205 switch (evt) {
1206 case SELF_ESTABL_CONTACT_EVT:
1207 state = SELF_UP_PEER_COMING;
1208 break;
1209 case PEER_ESTABL_CONTACT_EVT:
1210 state = SELF_COMING_PEER_UP;
1211 break;
1212 case SELF_LOST_CONTACT_EVT:
1213 case PEER_LOST_CONTACT_EVT:
1214 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001215 case NODE_SYNCH_END_EVT:
1216 case NODE_SYNCH_BEGIN_EVT:
1217 case NODE_FAILOVER_BEGIN_EVT:
1218 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001219 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001220 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001221 }
1222 break;
1223 case SELF_UP_PEER_UP:
1224 switch (evt) {
1225 case SELF_LOST_CONTACT_EVT:
1226 state = SELF_DOWN_PEER_LEAVING;
1227 break;
1228 case PEER_LOST_CONTACT_EVT:
1229 state = SELF_LEAVING_PEER_DOWN;
1230 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001231 case NODE_SYNCH_BEGIN_EVT:
1232 state = NODE_SYNCHING;
1233 break;
1234 case NODE_FAILOVER_BEGIN_EVT:
1235 state = NODE_FAILINGOVER;
1236 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001237 case SELF_ESTABL_CONTACT_EVT:
1238 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001239 case NODE_SYNCH_END_EVT:
1240 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001241 break;
1242 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001243 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001244 }
1245 break;
1246 case SELF_DOWN_PEER_LEAVING:
1247 switch (evt) {
1248 case PEER_LOST_CONTACT_EVT:
1249 state = SELF_DOWN_PEER_DOWN;
1250 break;
1251 case SELF_ESTABL_CONTACT_EVT:
1252 case PEER_ESTABL_CONTACT_EVT:
1253 case SELF_LOST_CONTACT_EVT:
1254 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001255 case NODE_SYNCH_END_EVT:
1256 case NODE_SYNCH_BEGIN_EVT:
1257 case NODE_FAILOVER_BEGIN_EVT:
1258 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001259 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001260 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001261 }
1262 break;
1263 case SELF_UP_PEER_COMING:
1264 switch (evt) {
1265 case PEER_ESTABL_CONTACT_EVT:
1266 state = SELF_UP_PEER_UP;
1267 break;
1268 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001269 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001270 break;
1271 case SELF_ESTABL_CONTACT_EVT:
1272 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001273 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001274 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001275 break;
1276 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001277 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001278 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001279 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001280 }
1281 break;
1282 case SELF_COMING_PEER_UP:
1283 switch (evt) {
1284 case SELF_ESTABL_CONTACT_EVT:
1285 state = SELF_UP_PEER_UP;
1286 break;
1287 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001288 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001289 break;
1290 case SELF_LOST_CONTACT_EVT:
1291 case PEER_ESTABL_CONTACT_EVT:
1292 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001293 case NODE_SYNCH_END_EVT:
1294 case NODE_SYNCH_BEGIN_EVT:
1295 case NODE_FAILOVER_BEGIN_EVT:
1296 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001297 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001298 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001299 }
1300 break;
1301 case SELF_LEAVING_PEER_DOWN:
1302 switch (evt) {
1303 case SELF_LOST_CONTACT_EVT:
1304 state = SELF_DOWN_PEER_DOWN;
1305 break;
1306 case SELF_ESTABL_CONTACT_EVT:
1307 case PEER_ESTABL_CONTACT_EVT:
1308 case PEER_LOST_CONTACT_EVT:
1309 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001310 case NODE_SYNCH_END_EVT:
1311 case NODE_SYNCH_BEGIN_EVT:
1312 case NODE_FAILOVER_BEGIN_EVT:
1313 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001314 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001315 goto illegal_evt;
1316 }
1317 break;
1318 case NODE_FAILINGOVER:
1319 switch (evt) {
1320 case SELF_LOST_CONTACT_EVT:
1321 state = SELF_DOWN_PEER_LEAVING;
1322 break;
1323 case PEER_LOST_CONTACT_EVT:
1324 state = SELF_LEAVING_PEER_DOWN;
1325 break;
1326 case NODE_FAILOVER_END_EVT:
1327 state = SELF_UP_PEER_UP;
1328 break;
1329 case NODE_FAILOVER_BEGIN_EVT:
1330 case SELF_ESTABL_CONTACT_EVT:
1331 case PEER_ESTABL_CONTACT_EVT:
1332 break;
1333 case NODE_SYNCH_BEGIN_EVT:
1334 case NODE_SYNCH_END_EVT:
1335 default:
1336 goto illegal_evt;
1337 }
1338 break;
1339 case NODE_SYNCHING:
1340 switch (evt) {
1341 case SELF_LOST_CONTACT_EVT:
1342 state = SELF_DOWN_PEER_LEAVING;
1343 break;
1344 case PEER_LOST_CONTACT_EVT:
1345 state = SELF_LEAVING_PEER_DOWN;
1346 break;
1347 case NODE_SYNCH_END_EVT:
1348 state = SELF_UP_PEER_UP;
1349 break;
1350 case NODE_FAILOVER_BEGIN_EVT:
1351 state = NODE_FAILINGOVER;
1352 break;
1353 case NODE_SYNCH_BEGIN_EVT:
1354 case SELF_ESTABL_CONTACT_EVT:
1355 case PEER_ESTABL_CONTACT_EVT:
1356 break;
1357 case NODE_FAILOVER_END_EVT:
1358 default:
1359 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001360 }
1361 break;
1362 default:
1363 pr_err("Unknown node fsm state %x\n", state);
1364 break;
1365 }
Tuong Lieneb18a512018-12-19 09:17:59 +07001366 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001367 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001368 return;
1369
1370illegal_evt:
1371 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Tuong Lieneb18a512018-12-19 09:17:59 +07001372 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001373}
1374
Jon Paul Maloy52666982015-10-22 08:51:41 -04001375static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001376 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001378 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001379 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001380 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001381 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001382 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383
Jon Maloyd50ccc22018-03-22 20:42:50 +01001384 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001385 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Tuong Lieneb18a512018-12-19 09:17:59 +07001386 trace_tipc_node_lost_contact(n, true, " ");
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001387
Jon Paul Maloy52666982015-10-22 08:51:41 -04001388 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001389 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001391 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001393 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001394 if (l)
1395 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001397
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001398 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001399 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Hoang Led408bef2019-11-08 10:02:37 +07001400 n->peer_net = NULL;
1401 n->peer_hash_mix = 0;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001402 /* Notify sockets connected to node */
1403 list_for_each_entry_safe(conn, safe, conns, list) {
1404 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001405 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001406 conn->peer_node, conn->port,
1407 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001408 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001409 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001410 list_del(&conn->list);
1411 kfree(conn);
1412 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413}
1414
Erik Hugne78acb1f2014-04-24 16:26:47 +02001415/**
1416 * tipc_node_get_linkname - get the name of a link
1417 *
1418 * @bearer_id: id of the bearer
1419 * @node: peer node address
1420 * @linkname: link name output buffer
1421 *
1422 * Returns 0 on success
1423 */
Ying Xuef2f98002015-01-09 15:27:05 +08001424int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1425 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001426{
1427 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001428 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001429 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001430
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001431 if (!node)
1432 return err;
1433
1434 if (bearer_id >= MAX_BEARERS)
1435 goto exit;
1436
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001437 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001438 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001439 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001440 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001441 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001442 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001443 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001444exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001445 tipc_node_put(node);
1446 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001447}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001448
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001449/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001450static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001451{
1452 void *hdr;
1453 struct nlattr *attrs;
1454
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001455 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001456 NLM_F_MULTI, TIPC_NL_NODE_GET);
1457 if (!hdr)
1458 return -EMSGSIZE;
1459
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001460 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001461 if (!attrs)
1462 goto msg_full;
1463
1464 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1465 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001466 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001467 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1468 goto attr_msg_full;
1469
1470 nla_nest_end(msg->skb, attrs);
1471 genlmsg_end(msg->skb, hdr);
1472
1473 return 0;
1474
1475attr_msg_full:
1476 nla_nest_cancel(msg->skb, attrs);
1477msg_full:
1478 genlmsg_cancel(msg->skb, hdr);
1479
1480 return -EMSGSIZE;
1481}
1482
Hoang Lef73b1282019-10-29 07:51:21 +07001483static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
1484{
1485 struct tipc_msg *hdr = buf_msg(skb_peek(list));
1486 struct sk_buff_head inputq;
1487
1488 switch (msg_user(hdr)) {
1489 case TIPC_LOW_IMPORTANCE:
1490 case TIPC_MEDIUM_IMPORTANCE:
1491 case TIPC_HIGH_IMPORTANCE:
1492 case TIPC_CRITICAL_IMPORTANCE:
1493 if (msg_connected(hdr) || msg_named(hdr)) {
1494 tipc_loopback_trace(peer_net, list);
1495 spin_lock_init(&list->lock);
1496 tipc_sk_rcv(peer_net, list);
1497 return;
1498 }
1499 if (msg_mcast(hdr)) {
1500 tipc_loopback_trace(peer_net, list);
1501 skb_queue_head_init(&inputq);
1502 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1503 __skb_queue_purge(list);
1504 skb_queue_purge(&inputq);
1505 return;
1506 }
1507 return;
1508 case MSG_FRAGMENTER:
1509 if (tipc_msg_assemble(list)) {
1510 tipc_loopback_trace(peer_net, list);
1511 skb_queue_head_init(&inputq);
1512 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1513 __skb_queue_purge(list);
1514 skb_queue_purge(&inputq);
1515 }
1516 return;
1517 case GROUP_PROTOCOL:
1518 case CONN_MANAGER:
1519 tipc_loopback_trace(peer_net, list);
1520 spin_lock_init(&list->lock);
1521 tipc_sk_rcv(peer_net, list);
1522 return;
1523 case LINK_PROTOCOL:
1524 case NAME_DISTRIBUTOR:
1525 case TUNNEL_PROTOCOL:
1526 case BCAST_PROTOCOL:
1527 return;
1528 default:
1529 return;
1530 };
1531}
1532
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001533/**
1534 * tipc_node_xmit() is the general link level function for message sending
1535 * @net: the applicable net namespace
1536 * @list: chain of buffers containing message
1537 * @dnode: address of destination node
1538 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001539 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001540 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001541 */
1542int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1543 u32 dnode, int selector)
1544{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001545 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001546 struct tipc_node *n;
1547 struct sk_buff_head xmitq;
Hoang Lef73b1282019-10-29 07:51:21 +07001548 bool node_up = false;
Richard Alpe4952cd32016-02-11 10:43:15 +01001549 int bearer_id;
1550 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001551
Richard Alpe4952cd32016-02-11 10:43:15 +01001552 if (in_own_node(net, dnode)) {
John Rutherford6c9081a2019-08-07 12:52:29 +10001553 tipc_loopback_trace(net, list);
Jon Maloye654f9f2019-08-15 16:42:50 +02001554 spin_lock_init(&list->lock);
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001555 tipc_sk_rcv(net, list);
1556 return 0;
1557 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001558
1559 n = tipc_node_find(net, dnode);
1560 if (unlikely(!n)) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001561 __skb_queue_purge(list);
Richard Alpe4952cd32016-02-11 10:43:15 +01001562 return -EHOSTUNREACH;
1563 }
1564
1565 tipc_node_read_lock(n);
Hoang Lef73b1282019-10-29 07:51:21 +07001566 node_up = node_is_up(n);
1567 if (node_up && n->peer_net && check_net(n->peer_net)) {
1568 /* xmit inner linux container */
1569 tipc_lxc_xmit(n->peer_net, list);
1570 if (likely(skb_queue_empty(list))) {
1571 tipc_node_read_unlock(n);
1572 tipc_node_put(n);
1573 return 0;
1574 }
1575 }
1576
Richard Alpe4952cd32016-02-11 10:43:15 +01001577 bearer_id = n->active_links[selector & 1];
1578 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1579 tipc_node_read_unlock(n);
1580 tipc_node_put(n);
Jon Maloye654f9f2019-08-15 16:42:50 +02001581 __skb_queue_purge(list);
Richard Alpe4952cd32016-02-11 10:43:15 +01001582 return -EHOSTUNREACH;
1583 }
1584
1585 __skb_queue_head_init(&xmitq);
1586 le = &n->links[bearer_id];
1587 spin_lock_bh(&le->lock);
1588 rc = tipc_link_xmit(le->link, list, &xmitq);
1589 spin_unlock_bh(&le->lock);
1590 tipc_node_read_unlock(n);
1591
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001592 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001593 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001594 else
1595 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001596
1597 tipc_node_put(n);
1598
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001599 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001600}
1601
1602/* tipc_node_xmit_skb(): send single buffer to destination
1603 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1604 * messages, which will not be rejected
1605 * The only exception is datagram messages rerouted after secondary
1606 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001607 */
1608int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1609 u32 selector)
1610{
1611 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001612
Jon Maloye654f9f2019-08-15 16:42:50 +02001613 __skb_queue_head_init(&head);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001614 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001615 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001616 return 0;
1617}
1618
Jon Maloyf70d37b2017-10-13 11:04:21 +02001619/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1620 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1621 */
1622int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1623{
1624 struct sk_buff *skb;
1625 u32 selector, dnode;
1626
1627 while ((skb = __skb_dequeue(xmitq))) {
1628 selector = msg_origport(buf_msg(skb));
1629 dnode = msg_destnode(buf_msg(skb));
1630 tipc_node_xmit_skb(net, skb, dnode, selector);
1631 }
1632 return 0;
1633}
1634
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001635void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1636{
1637 struct sk_buff *txskb;
1638 struct tipc_node *n;
1639 u32 dst;
1640
1641 rcu_read_lock();
1642 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1643 dst = n->addr;
1644 if (in_own_node(net, dst))
1645 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001646 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001647 continue;
1648 txskb = pskb_copy(skb, GFP_ATOMIC);
1649 if (!txskb)
1650 break;
1651 msg_set_destnode(buf_msg(txskb), dst);
1652 tipc_node_xmit_skb(net, txskb, dst, 0);
1653 }
1654 rcu_read_unlock();
1655
1656 kfree_skb(skb);
1657}
1658
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001659static void tipc_node_mcast_rcv(struct tipc_node *n)
1660{
1661 struct tipc_bclink_entry *be = &n->bc_entry;
1662
1663 /* 'arrvq' is under inputq2's lock protection */
1664 spin_lock_bh(&be->inputq2.lock);
1665 spin_lock_bh(&be->inputq1.lock);
1666 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1667 spin_unlock_bh(&be->inputq1.lock);
1668 spin_unlock_bh(&be->inputq2.lock);
1669 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1670}
1671
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001672static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1673 int bearer_id, struct sk_buff_head *xmitq)
1674{
1675 struct tipc_link *ucl;
1676 int rc;
1677
1678 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1679
1680 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001681 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001682 return;
1683 }
1684
1685 if (!(rc & TIPC_LINK_SND_STATE))
1686 return;
1687
1688 /* If probe message, a STATE response will be sent anyway */
1689 if (msg_probe(hdr))
1690 return;
1691
1692 /* Produce a STATE message carrying broadcast NACK */
1693 tipc_node_read_lock(n);
1694 ucl = n->links[bearer_id].link;
1695 if (ucl)
1696 tipc_link_build_state_msg(ucl, xmitq);
1697 tipc_node_read_unlock(n);
1698}
1699
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001700/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001701 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1702 * @net: the applicable net namespace
1703 * @skb: TIPC packet
1704 * @bearer_id: id of bearer message arrived on
1705 *
1706 * Invoked with no locks held.
1707 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001708static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001709{
1710 int rc;
1711 struct sk_buff_head xmitq;
1712 struct tipc_bclink_entry *be;
1713 struct tipc_link_entry *le;
1714 struct tipc_msg *hdr = buf_msg(skb);
1715 int usr = msg_user(hdr);
1716 u32 dnode = msg_destnode(hdr);
1717 struct tipc_node *n;
1718
1719 __skb_queue_head_init(&xmitq);
1720
1721 /* If NACK for other node, let rcv link for that node peek into it */
1722 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1723 n = tipc_node_find(net, dnode);
1724 else
1725 n = tipc_node_find(net, msg_prevnode(hdr));
1726 if (!n) {
1727 kfree_skb(skb);
1728 return;
1729 }
1730 be = &n->bc_entry;
1731 le = &n->links[bearer_id];
1732
1733 rc = tipc_bcast_rcv(net, be->link, skb);
1734
Jon Paul Maloy52666982015-10-22 08:51:41 -04001735 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001736 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001737 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001738 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001739 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001740 }
1741
1742 if (!skb_queue_empty(&xmitq))
1743 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1744
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001745 if (!skb_queue_empty(&be->inputq1))
1746 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001747
Zhenbo Gao5679ee72018-12-18 17:43:52 +08001748 /* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
1749 if (!skb_queue_empty(&n->bc_entry.namedq))
1750 tipc_named_rcv(net, &n->bc_entry.namedq);
1751
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001752 /* If reassembly or retransmission failure => reset all links to peer */
1753 if (rc & TIPC_LINK_DOWN_EVT)
1754 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001755
Jon Paul Maloy52666982015-10-22 08:51:41 -04001756 tipc_node_put(n);
1757}
1758
1759/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001760 * tipc_node_check_state - check and if necessary update node state
1761 * @skb: TIPC packet
1762 * @bearer_id: identity of bearer delivering the packet
Jon Maloy7ea817f2018-07-10 01:07:36 +02001763 * Returns true if state and msg are ok, otherwise false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001764 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001765static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001766 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001767{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001768 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001769 int usr = msg_user(hdr);
1770 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001771 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001772 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001773 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001774 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001775 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001776 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001777 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001778
Tuong Lieneb18a512018-12-19 09:17:59 +07001779 if (trace_tipc_node_check_state_enabled()) {
1780 trace_tipc_skb_dump(skb, false, "skb for node state check");
1781 trace_tipc_node_check_state(n, true, " ");
1782 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001783 l = n->links[bearer_id].link;
1784 if (!l)
1785 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001786 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001787
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001788
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001789 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1790 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001791
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001792 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001793 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1794 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1795 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001796 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001797 }
1798 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001799
Tuong Lien26574db2018-12-19 09:17:57 +07001800 if (!tipc_link_validate_msg(l, hdr)) {
1801 trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
1802 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
Jon Maloy7ea817f2018-07-10 01:07:36 +02001803 return false;
Tuong Lien26574db2018-12-19 09:17:57 +07001804 }
Jon Maloy7ea817f2018-07-10 01:07:36 +02001805
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001806 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001807 if (state == SELF_UP_PEER_COMING) {
1808 if (!tipc_link_is_up(l))
1809 return true;
1810 if (!msg_peer_link_is_up(hdr))
1811 return true;
1812 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1813 }
1814
1815 if (state == SELF_DOWN_PEER_LEAVING) {
1816 if (msg_peer_node_is_up(hdr))
1817 return false;
1818 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001819 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001820 }
1821
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001822 if (state == SELF_LEAVING_PEER_DOWN)
1823 return false;
1824
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001825 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001826 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001827 return true;
1828
1829 /* Initiate or update failover mode if applicable */
1830 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1831 syncpt = oseqno + exp_pkts - 1;
Tuong Liend0f84d02019-06-17 11:56:12 +07001832 if (pl && !tipc_link_is_reset(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001833 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Tuong Lieneb18a512018-12-19 09:17:59 +07001834 trace_tipc_node_link_down(n, true,
1835 "node link down <- failover!");
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001836 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1837 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001838 }
Tuong Lienc0b14a082019-05-02 17:23:23 +07001839
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001840 /* If parallel link was already down, and this happened before
Tuong Lienc0b14a082019-05-02 17:23:23 +07001841 * the tunnel link came up, node failover was never started.
1842 * Ensure that a FAILOVER_MSG is sent to get peer out of
1843 * NODE_FAILINGOVER state, also this node must accept
1844 * TUNNEL_MSGs from peer.
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001845 */
Tuong Lienc0b14a082019-05-02 17:23:23 +07001846 if (n->state != NODE_FAILINGOVER)
1847 tipc_node_link_failover(n, pl, l, xmitq);
1848
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001849 /* If pkts arrive out of order, use lowest calculated syncpt */
1850 if (less(syncpt, n->sync_point))
1851 n->sync_point = syncpt;
1852 }
1853
1854 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001855 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001856 if (!more(rcv_nxt, n->sync_point))
1857 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001858 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1859 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001860 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001861 return true;
1862 }
1863
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001864 /* No synching needed if only one link */
1865 if (!pl || !tipc_link_is_up(pl))
1866 return true;
1867
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001868 /* Initiate synch mode if applicable */
1869 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Tuong Lien4929a932019-07-24 08:56:11 +07001870 if (n->capabilities & TIPC_TUNNEL_ENHANCED)
1871 syncpt = msg_syncpt(hdr);
1872 else
1873 syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001874 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001875 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001876 if (n->state == SELF_UP_PEER_UP) {
1877 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001878 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001879 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1880 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001881 }
1882
1883 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001884 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001885 if (tipc_link_is_synching(l)) {
1886 tnl = l;
1887 } else {
1888 tnl = pl;
1889 pl = l;
1890 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001891 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1892 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001893 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001894 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001895 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001896 return true;
1897 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001898 if (l == pl)
1899 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001900 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1901 return true;
1902 if (usr == LINK_PROTOCOL)
1903 return true;
1904 return false;
1905 }
1906 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001907}
1908
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001909/**
1910 * tipc_rcv - process TIPC packets/messages arriving from off-node
1911 * @net: the applicable net namespace
1912 * @skb: TIPC packet
1913 * @bearer: pointer to bearer message arrived on
1914 *
1915 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1916 * structure (i.e. cannot be NULL), but bearer can be inactive.
1917 */
1918void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1919{
1920 struct sk_buff_head xmitq;
1921 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001922 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001923 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001924 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001925 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001926 int usr, rc = 0;
1927 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001928
1929 __skb_queue_head_init(&xmitq);
1930
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001931 /* Ensure message is well-formed before touching the header */
Jon Maloy866e5fd2019-07-17 23:43:44 +02001932 TIPC_SKB_CB(skb)->validated = false;
Jon Maloyd618d092017-11-15 21:23:56 +01001933 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001934 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001935 hdr = buf_msg(skb);
1936 usr = msg_user(hdr);
1937 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001938
Jon Paul Maloy52666982015-10-22 08:51:41 -04001939 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001940 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001941 if (unlikely(usr == LINK_CONFIG))
1942 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001943 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001944 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001945 }
1946
Hamish Martinefe79052016-04-29 10:40:24 -04001947 /* Discard unicast link messages destined for another node */
1948 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1949 goto discard;
1950
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001951 /* Locate neighboring node that sent packet */
1952 n = tipc_node_find(net, msg_prevnode(hdr));
1953 if (unlikely(!n))
1954 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001955 le = &n->links[bearer_id];
1956
Jon Paul Maloy52666982015-10-22 08:51:41 -04001957 /* Ensure broadcast reception is in synch with peer's send state */
1958 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001959 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001960 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001961 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001962
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001963 /* Receive packet directly if conditions permit */
1964 tipc_node_read_lock(n);
1965 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001966 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001967 if (le->link) {
1968 rc = tipc_link_rcv(le->link, skb, &xmitq);
1969 skb = NULL;
1970 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001971 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001972 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001973 tipc_node_read_unlock(n);
1974
1975 /* Check/update node state before receiving */
1976 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001977 if (unlikely(skb_linearize(skb)))
1978 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001979 tipc_node_write_lock(n);
1980 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1981 if (le->link) {
1982 rc = tipc_link_rcv(le->link, skb, &xmitq);
1983 skb = NULL;
1984 }
1985 }
1986 tipc_node_write_unlock(n);
1987 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001988
1989 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001990 tipc_node_link_up(n, bearer_id, &xmitq);
1991
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001992 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001993 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001994
Jon Paul Maloy52666982015-10-22 08:51:41 -04001995 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1996 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001997
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001998 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1999 tipc_node_mcast_rcv(n);
2000
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002001 if (!skb_queue_empty(&le->inputq))
2002 tipc_sk_rcv(net, &le->inputq);
2003
2004 if (!skb_queue_empty(&xmitq))
2005 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
2006
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002007 tipc_node_put(n);
2008discard:
2009 kfree_skb(skb);
2010}
2011
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02002012void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
2013 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01002014{
2015 struct tipc_net *tn = tipc_net(net);
2016 int bearer_id = b->identity;
2017 struct sk_buff_head xmitq;
2018 struct tipc_link_entry *e;
2019 struct tipc_node *n;
2020
2021 __skb_queue_head_init(&xmitq);
2022
2023 rcu_read_lock();
2024
2025 list_for_each_entry_rcu(n, &tn->node_list, list) {
2026 tipc_node_write_lock(n);
2027 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02002028 if (e->link) {
2029 if (prop == TIPC_NLA_PROP_TOL)
2030 tipc_link_set_tolerance(e->link, b->tolerance,
2031 &xmitq);
2032 else if (prop == TIPC_NLA_PROP_MTU)
2033 tipc_link_set_mtu(e->link, b->mtu);
2034 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01002035 tipc_node_write_unlock(n);
2036 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
2037 }
2038
2039 rcu_read_unlock();
2040}
2041
Richard Alpeb3404022016-08-18 10:33:52 +02002042int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
2043{
2044 struct net *net = sock_net(skb->sk);
2045 struct tipc_net *tn = net_generic(net, tipc_net_id);
2046 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
Hoang Le6708ef72019-11-06 13:26:09 +07002047 struct tipc_node *peer, *temp_node;
Richard Alpeb3404022016-08-18 10:33:52 +02002048 u32 addr;
2049 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02002050
2051 /* We identify the peer by its net */
2052 if (!info->attrs[TIPC_NLA_NET])
2053 return -EINVAL;
2054
Johannes Berg8cb08172019-04-26 14:07:28 +02002055 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
2056 info->attrs[TIPC_NLA_NET],
2057 tipc_nl_net_policy, info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02002058 if (err)
2059 return err;
2060
2061 if (!attrs[TIPC_NLA_NET_ADDR])
2062 return -EINVAL;
2063
2064 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
2065
2066 if (in_own_node(net, addr))
2067 return -ENOTSUPP;
2068
2069 spin_lock_bh(&tn->node_list_lock);
2070 peer = tipc_node_find(net, addr);
2071 if (!peer) {
2072 spin_unlock_bh(&tn->node_list_lock);
2073 return -ENXIO;
2074 }
2075
2076 tipc_node_write_lock(peer);
2077 if (peer->state != SELF_DOWN_PEER_DOWN &&
2078 peer->state != SELF_DOWN_PEER_LEAVING) {
2079 tipc_node_write_unlock(peer);
2080 err = -EBUSY;
2081 goto err_out;
2082 }
2083
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02002084 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02002085 tipc_node_write_unlock(peer);
2086 tipc_node_delete(peer);
2087
Hoang Le6708ef72019-11-06 13:26:09 +07002088 /* Calculate cluster capabilities */
2089 tn->capabilities = TIPC_NODE_CAPABILITIES;
2090 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
2091 tn->capabilities &= temp_node->capabilities;
2092 }
Richard Alpeb3404022016-08-18 10:33:52 +02002093 err = 0;
2094err_out:
2095 tipc_node_put(peer);
2096 spin_unlock_bh(&tn->node_list_lock);
2097
2098 return err;
2099}
2100
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002101int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
2102{
2103 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08002104 struct net *net = sock_net(skb->sk);
2105 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002106 int done = cb->args[0];
2107 int last_addr = cb->args[1];
2108 struct tipc_node *node;
2109 struct tipc_nl_msg msg;
2110
2111 if (done)
2112 return 0;
2113
2114 msg.skb = skb;
2115 msg.portid = NETLINK_CB(cb->skb).portid;
2116 msg.seq = cb->nlh->nlmsg_seq;
2117
2118 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08002119 if (last_addr) {
2120 node = tipc_node_find(net, last_addr);
2121 if (!node) {
2122 rcu_read_unlock();
2123 /* We never set seq or call nl_dump_check_consistent()
2124 * this means that setting prev_seq here will cause the
2125 * consistence check to fail in the netlink callback
2126 * handler. Resulting in the NLMSG_DONE message having
2127 * the NLM_F_DUMP_INTR flag set if the node state
2128 * changed while we released the lock.
2129 */
2130 cb->prev_seq = 1;
2131 return -EPIPE;
2132 }
2133 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002134 }
2135
Ying Xuef2f98002015-01-09 15:27:05 +08002136 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002137 if (last_addr) {
2138 if (node->addr == last_addr)
2139 last_addr = 0;
2140 else
2141 continue;
2142 }
2143
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002144 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002145 err = __tipc_nl_add_node(&msg, node);
2146 if (err) {
2147 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002148 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002149 goto out;
2150 }
2151
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002152 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002153 }
2154 done = 1;
2155out:
2156 cb->args[0] = done;
2157 cb->args[1] = last_addr;
2158 rcu_read_unlock();
2159
2160 return skb->len;
2161}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002162
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002163/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002164 * @net: the applicable net namespace
2165 * @name: pointer to link name string
2166 * @bearer_id: pointer to index in 'node->links' array where the link was found.
2167 *
2168 * Returns pointer to node owning the link, or 0 if no matching link is found.
2169 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002170static struct tipc_node *tipc_node_find_by_name(struct net *net,
2171 const char *link_name,
2172 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002173{
2174 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002175 struct tipc_link *l;
2176 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002177 struct tipc_node *found_node = NULL;
2178 int i;
2179
2180 *bearer_id = 0;
2181 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002182 list_for_each_entry_rcu(n, &tn->node_list, list) {
2183 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002184 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002185 l = n->links[i].link;
2186 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002187 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002188 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002189 break;
2190 }
2191 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002192 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002193 if (found_node)
2194 break;
2195 }
2196 rcu_read_unlock();
2197
2198 return found_node;
2199}
2200
2201int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
2202{
2203 int err;
2204 int res = 0;
2205 int bearer_id;
2206 char *name;
2207 struct tipc_link *link;
2208 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01002209 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002210 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2211 struct net *net = sock_net(skb->sk);
2212
Richard Alped01332f2016-02-01 08:19:56 +01002213 __skb_queue_head_init(&xmitq);
2214
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002215 if (!info->attrs[TIPC_NLA_LINK])
2216 return -EINVAL;
2217
Johannes Berg8cb08172019-04-26 14:07:28 +02002218 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2219 info->attrs[TIPC_NLA_LINK],
2220 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002221 if (err)
2222 return err;
2223
2224 if (!attrs[TIPC_NLA_LINK_NAME])
2225 return -EINVAL;
2226
2227 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2228
2229 if (strcmp(name, tipc_bclink_name) == 0)
2230 return tipc_nl_bc_link_set(net, attrs);
2231
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002232 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002233 if (!node)
2234 return -EINVAL;
2235
2236 tipc_node_read_lock(node);
2237
2238 link = node->links[bearer_id].link;
2239 if (!link) {
2240 res = -EINVAL;
2241 goto out;
2242 }
2243
2244 if (attrs[TIPC_NLA_LINK_PROP]) {
2245 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2246
2247 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2248 props);
2249 if (err) {
2250 res = err;
2251 goto out;
2252 }
2253
2254 if (props[TIPC_NLA_PROP_TOL]) {
2255 u32 tol;
2256
2257 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002258 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002259 }
2260 if (props[TIPC_NLA_PROP_PRIO]) {
2261 u32 prio;
2262
2263 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002264 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002265 }
2266 if (props[TIPC_NLA_PROP_WIN]) {
2267 u32 win;
2268
2269 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2270 tipc_link_set_queue_limits(link, win);
2271 }
2272 }
2273
2274out:
2275 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01002276 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002277 return res;
2278}
2279
2280int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2281{
2282 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002283 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002284 struct tipc_nl_msg msg;
2285 char *name;
2286 int err;
2287
2288 msg.portid = info->snd_portid;
2289 msg.seq = info->snd_seq;
2290
Ying Xue94f6a802018-05-08 21:44:06 +08002291 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002292 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002293
Johannes Berg8cb08172019-04-26 14:07:28 +02002294 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2295 info->attrs[TIPC_NLA_LINK],
2296 tipc_nl_link_policy, info->extack);
Ying Xue94f6a802018-05-08 21:44:06 +08002297 if (err)
2298 return err;
2299
2300 if (!attrs[TIPC_NLA_LINK_NAME])
2301 return -EINVAL;
2302
2303 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002304
2305 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2306 if (!msg.skb)
2307 return -ENOMEM;
2308
2309 if (strcmp(name, tipc_bclink_name) == 0) {
2310 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002311 if (err)
2312 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002313 } else {
2314 int bearer_id;
2315 struct tipc_node *node;
2316 struct tipc_link *link;
2317
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002318 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002319 if (!node) {
2320 err = -EINVAL;
2321 goto err_free;
2322 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002323
2324 tipc_node_read_lock(node);
2325 link = node->links[bearer_id].link;
2326 if (!link) {
2327 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002328 err = -EINVAL;
2329 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002330 }
2331
2332 err = __tipc_nl_add_link(net, &msg, link, 0);
2333 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002334 if (err)
2335 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002336 }
2337
2338 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002339
2340err_free:
2341 nlmsg_free(msg.skb);
2342 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002343}
2344
2345int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2346{
2347 int err;
2348 char *link_name;
2349 unsigned int bearer_id;
2350 struct tipc_link *link;
2351 struct tipc_node *node;
2352 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2353 struct net *net = sock_net(skb->sk);
2354 struct tipc_link_entry *le;
2355
2356 if (!info->attrs[TIPC_NLA_LINK])
2357 return -EINVAL;
2358
Johannes Berg8cb08172019-04-26 14:07:28 +02002359 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2360 info->attrs[TIPC_NLA_LINK],
2361 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002362 if (err)
2363 return err;
2364
2365 if (!attrs[TIPC_NLA_LINK_NAME])
2366 return -EINVAL;
2367
2368 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2369
2370 if (strcmp(link_name, tipc_bclink_name) == 0) {
2371 err = tipc_bclink_reset_stats(net);
2372 if (err)
2373 return err;
2374 return 0;
2375 }
2376
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002377 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002378 if (!node)
2379 return -EINVAL;
2380
2381 le = &node->links[bearer_id];
2382 tipc_node_read_lock(node);
2383 spin_lock_bh(&le->lock);
2384 link = node->links[bearer_id].link;
2385 if (!link) {
2386 spin_unlock_bh(&le->lock);
2387 tipc_node_read_unlock(node);
2388 return -EINVAL;
2389 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002390 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002391 spin_unlock_bh(&le->lock);
2392 tipc_node_read_unlock(node);
2393 return 0;
2394}
2395
2396/* Caller should hold node lock */
2397static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2398 struct tipc_node *node, u32 *prev_link)
2399{
2400 u32 i;
2401 int err;
2402
2403 for (i = *prev_link; i < MAX_BEARERS; i++) {
2404 *prev_link = i;
2405
2406 if (!node->links[i].link)
2407 continue;
2408
2409 err = __tipc_nl_add_link(net, msg,
2410 node->links[i].link, NLM_F_MULTI);
2411 if (err)
2412 return err;
2413 }
2414 *prev_link = 0;
2415
2416 return 0;
2417}
2418
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002419int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002420{
2421 struct net *net = sock_net(skb->sk);
2422 struct tipc_net *tn = net_generic(net, tipc_net_id);
2423 struct tipc_node *node;
2424 struct tipc_nl_msg msg;
2425 u32 prev_node = cb->args[0];
2426 u32 prev_link = cb->args[1];
2427 int done = cb->args[2];
2428 int err;
2429
2430 if (done)
2431 return 0;
2432
2433 msg.skb = skb;
2434 msg.portid = NETLINK_CB(cb->skb).portid;
2435 msg.seq = cb->nlh->nlmsg_seq;
2436
2437 rcu_read_lock();
2438 if (prev_node) {
2439 node = tipc_node_find(net, prev_node);
2440 if (!node) {
2441 /* We never set seq or call nl_dump_check_consistent()
2442 * this means that setting prev_seq here will cause the
2443 * consistence check to fail in the netlink callback
2444 * handler. Resulting in the last NLMSG_DONE message
2445 * having the NLM_F_DUMP_INTR flag set.
2446 */
2447 cb->prev_seq = 1;
2448 goto out;
2449 }
2450 tipc_node_put(node);
2451
2452 list_for_each_entry_continue_rcu(node, &tn->node_list,
2453 list) {
2454 tipc_node_read_lock(node);
2455 err = __tipc_nl_add_node_links(net, &msg, node,
2456 &prev_link);
2457 tipc_node_read_unlock(node);
2458 if (err)
2459 goto out;
2460
2461 prev_node = node->addr;
2462 }
2463 } else {
2464 err = tipc_nl_add_bc_link(net, &msg);
2465 if (err)
2466 goto out;
2467
2468 list_for_each_entry_rcu(node, &tn->node_list, list) {
2469 tipc_node_read_lock(node);
2470 err = __tipc_nl_add_node_links(net, &msg, node,
2471 &prev_link);
2472 tipc_node_read_unlock(node);
2473 if (err)
2474 goto out;
2475
2476 prev_node = node->addr;
2477 }
2478 }
2479 done = 1;
2480out:
2481 rcu_read_unlock();
2482
2483 cb->args[0] = prev_node;
2484 cb->args[1] = prev_link;
2485 cb->args[2] = done;
2486
2487 return skb->len;
2488}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002489
2490int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2491{
2492 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2493 struct net *net = sock_net(skb->sk);
2494 int err;
2495
2496 if (!info->attrs[TIPC_NLA_MON])
2497 return -EINVAL;
2498
Johannes Berg8cb08172019-04-26 14:07:28 +02002499 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
2500 info->attrs[TIPC_NLA_MON],
2501 tipc_nl_monitor_policy,
2502 info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002503 if (err)
2504 return err;
2505
2506 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2507 u32 val;
2508
2509 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2510 err = tipc_nl_monitor_set_threshold(net, val);
2511 if (err)
2512 return err;
2513 }
2514
2515 return 0;
2516}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002517
2518static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2519{
2520 struct nlattr *attrs;
2521 void *hdr;
2522 u32 val;
2523
2524 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2525 0, TIPC_NL_MON_GET);
2526 if (!hdr)
2527 return -EMSGSIZE;
2528
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002529 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002530 if (!attrs)
2531 goto msg_full;
2532
2533 val = tipc_nl_monitor_get_threshold(net);
2534
2535 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2536 goto attr_msg_full;
2537
2538 nla_nest_end(msg->skb, attrs);
2539 genlmsg_end(msg->skb, hdr);
2540
2541 return 0;
2542
2543attr_msg_full:
2544 nla_nest_cancel(msg->skb, attrs);
2545msg_full:
2546 genlmsg_cancel(msg->skb, hdr);
2547
2548 return -EMSGSIZE;
2549}
2550
2551int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2552{
2553 struct net *net = sock_net(skb->sk);
2554 struct tipc_nl_msg msg;
2555 int err;
2556
2557 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002558 if (!msg.skb)
2559 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002560 msg.portid = info->snd_portid;
2561 msg.seq = info->snd_seq;
2562
2563 err = __tipc_nl_add_monitor_prop(net, &msg);
2564 if (err) {
2565 nlmsg_free(msg.skb);
2566 return err;
2567 }
2568
2569 return genlmsg_reply(msg.skb, info);
2570}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002571
2572int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2573{
2574 struct net *net = sock_net(skb->sk);
2575 u32 prev_bearer = cb->args[0];
2576 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002577 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002578 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002579
2580 if (prev_bearer == MAX_BEARERS)
2581 return 0;
2582
2583 msg.skb = skb;
2584 msg.portid = NETLINK_CB(cb->skb).portid;
2585 msg.seq = cb->nlh->nlmsg_seq;
2586
2587 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002588 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002589 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002590 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002591 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002592 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002593 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002594 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002595
2596 return skb->len;
2597}
2598
2599int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2600 struct netlink_callback *cb)
2601{
2602 struct net *net = sock_net(skb->sk);
2603 u32 prev_node = cb->args[1];
2604 u32 bearer_id = cb->args[2];
2605 int done = cb->args[0];
2606 struct tipc_nl_msg msg;
2607 int err;
2608
2609 if (!prev_node) {
Jiri Pirko057af702019-10-05 20:04:39 +02002610 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002611 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2612
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002613 if (!attrs[TIPC_NLA_MON])
2614 return -EINVAL;
2615
Johannes Berg8cb08172019-04-26 14:07:28 +02002616 err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
2617 attrs[TIPC_NLA_MON],
2618 tipc_nl_monitor_policy,
2619 NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002620 if (err)
2621 return err;
2622
2623 if (!mon[TIPC_NLA_MON_REF])
2624 return -EINVAL;
2625
2626 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2627
2628 if (bearer_id >= MAX_BEARERS)
2629 return -EINVAL;
2630 }
2631
2632 if (done)
2633 return 0;
2634
2635 msg.skb = skb;
2636 msg.portid = NETLINK_CB(cb->skb).portid;
2637 msg.seq = cb->nlh->nlmsg_seq;
2638
2639 rtnl_lock();
2640 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2641 if (!err)
2642 done = 1;
2643
2644 rtnl_unlock();
2645 cb->args[0] = done;
2646 cb->args[1] = prev_node;
2647 cb->args[2] = bearer_id;
2648
2649 return skb->len;
2650}
Tuong Lienb4b97712018-12-19 09:17:56 +07002651
2652u32 tipc_node_get_addr(struct tipc_node *node)
2653{
2654 return (node) ? node->addr : 0;
2655}
2656
2657/**
2658 * tipc_node_dump - dump TIPC node data
2659 * @n: tipc node to be dumped
2660 * @more: dump more?
2661 * - false: dump only tipc node data
2662 * - true: dump node link data as well
2663 * @buf: returned buffer of dump data in format
2664 */
2665int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
2666{
2667 int i = 0;
2668 size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
2669
2670 if (!n) {
2671 i += scnprintf(buf, sz, "node data: (null)\n");
2672 return i;
2673 }
2674
2675 i += scnprintf(buf, sz, "node data: %x", n->addr);
2676 i += scnprintf(buf + i, sz - i, " %x", n->state);
2677 i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
2678 i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
2679 i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
2680 i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
2681 i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
2682 i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
2683 i += scnprintf(buf + i, sz - i, " %u", n->working_links);
2684 i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
2685 i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
2686
2687 if (!more)
2688 return i;
2689
2690 i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
2691 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
2692 i += scnprintf(buf + i, sz - i, " media: ");
2693 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
2694 i += scnprintf(buf + i, sz - i, "\n");
2695 i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
2696 i += scnprintf(buf + i, sz - i, " inputq: ");
2697 i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
2698
2699 i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
2700 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
2701 i += scnprintf(buf + i, sz - i, " media: ");
2702 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
2703 i += scnprintf(buf + i, sz - i, "\n");
2704 i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
2705 i += scnprintf(buf + i, sz - i, " inputq: ");
2706 i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
2707
2708 i += scnprintf(buf + i, sz - i, "bclink:\n ");
2709 i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
2710
2711 return i;
2712}
Hoang Lef73b1282019-10-29 07:51:21 +07002713
2714void tipc_node_pre_cleanup_net(struct net *exit_net)
2715{
2716 struct tipc_node *n;
2717 struct tipc_net *tn;
2718 struct net *tmp;
2719
2720 rcu_read_lock();
2721 for_each_net_rcu(tmp) {
2722 if (tmp == exit_net)
2723 continue;
2724 tn = tipc_net(tmp);
2725 if (!tn)
2726 continue;
2727 spin_lock_bh(&tn->node_list_lock);
2728 list_for_each_entry_rcu(n, &tn->node_list, list) {
2729 if (!n->peer_net)
2730 continue;
2731 if (n->peer_net != exit_net)
2732 continue;
2733 tipc_node_write_lock(n);
2734 n->peer_net = NULL;
2735 n->peer_hash_mix = 0;
2736 tipc_node_write_unlock_fast(n);
2737 break;
2738 }
2739 spin_unlock_bh(&tn->node_list_lock);
2740 }
2741 rcu_read_unlock();
2742}