blob: 0c88778c88b5f142772330123998978fc50284a8 [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"
Tuong Lienfc1b6d62019-11-08 12:05:11 +070047#include "crypto.h"
Jon Paul Maloyc8199302015-10-15 14:52:46 -040048
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050049#define INVALID_NODE_SIG 0x10000
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +020050#define NODE_CLEANUP_AFTER 300000
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050051
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050052/* Flags used to take different actions according to flag type
53 * TIPC_NOTIFY_NODE_DOWN: notify node is down
54 * TIPC_NOTIFY_NODE_UP: notify node is up
55 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
56 */
57enum {
58 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
59 TIPC_NOTIFY_NODE_UP = (1 << 4),
60 TIPC_NOTIFY_LINK_UP = (1 << 6),
61 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
62};
63
64struct tipc_link_entry {
65 struct tipc_link *link;
66 spinlock_t lock; /* per link */
67 u32 mtu;
68 struct sk_buff_head inputq;
69 struct tipc_media_addr maddr;
70};
71
72struct tipc_bclink_entry {
73 struct tipc_link *link;
74 struct sk_buff_head inputq1;
75 struct sk_buff_head arrvq;
76 struct sk_buff_head inputq2;
77 struct sk_buff_head namedq;
78};
79
80/**
81 * struct tipc_node - TIPC node structure
82 * @addr: network address of node
83 * @ref: reference counter to node object
84 * @lock: rwlock governing access to structure
85 * @net: the applicable net namespace
86 * @hash: links to adjacent nodes in unsorted hash chain
87 * @inputq: pointer to input queue containing messages for msg event
88 * @namedq: pointer to name table input queue with name table messages
89 * @active_links: bearer ids of active links, used as index into links[] array
90 * @links: array containing references to all links to node
91 * @action_flags: bit mask of different types of node actions
92 * @state: connectivity state vs peer node
Tuong Lien4cbf8ac2019-11-08 12:05:09 +070093 * @preliminary: a preliminary node or not
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050094 * @sync_point: sequence number where synch/failover is finished
95 * @list: links to adjacent nodes in sorted list of cluster's nodes
96 * @working_links: number of working links to node (both active and standby)
97 * @link_cnt: number of links to node
98 * @capabilities: bitmap, indicating peer node's functional capabilities
99 * @signature: node instance identifier
100 * @link_id: local and remote bearer ids of changing link, if any
101 * @publ_list: list of publications
102 * @rcu: rcu struct for tipc_node
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200103 * @delete_at: indicates the time for deleting a down node
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700104 * @crypto_rx: RX crypto handler
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500105 */
106struct tipc_node {
107 u32 addr;
108 struct kref kref;
109 rwlock_t lock;
110 struct net *net;
111 struct hlist_node hash;
112 int active_links[2];
113 struct tipc_link_entry links[MAX_BEARERS];
114 struct tipc_bclink_entry bc_entry;
115 int action_flags;
116 struct list_head list;
117 int state;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700118 bool preliminary;
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200119 bool failover_sent;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500120 u16 sync_point;
121 int link_cnt;
122 u16 working_links;
123 u16 capabilities;
124 u32 signature;
125 u32 link_id;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100126 u8 peer_id[16];
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700127 char peer_id_string[NODE_ID_STR_LEN];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500128 struct list_head publ_list;
129 struct list_head conn_sks;
130 unsigned long keepalive_intv;
131 struct timer_list timer;
132 struct rcu_head rcu;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200133 unsigned long delete_at;
Hoang Lef73b1282019-10-29 07:51:21 +0700134 struct net *peer_net;
135 u32 peer_hash_mix;
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700136#ifdef CONFIG_TIPC_CRYPTO
137 struct tipc_crypto *crypto_rx;
138#endif
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500139};
140
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400141/* Node FSM states and events:
142 */
143enum {
144 SELF_DOWN_PEER_DOWN = 0xdd,
145 SELF_UP_PEER_UP = 0xaa,
146 SELF_DOWN_PEER_LEAVING = 0xd1,
147 SELF_UP_PEER_COMING = 0xac,
148 SELF_COMING_PEER_UP = 0xca,
149 SELF_LEAVING_PEER_DOWN = 0x1d,
150 NODE_FAILINGOVER = 0xf0,
151 NODE_SYNCHING = 0xcc
152};
153
154enum {
155 SELF_ESTABL_CONTACT_EVT = 0xece,
156 SELF_LOST_CONTACT_EVT = 0x1ce,
157 PEER_ESTABL_CONTACT_EVT = 0x9ece,
158 PEER_LOST_CONTACT_EVT = 0x91ce,
159 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
160 NODE_FAILOVER_END_EVT = 0xfee,
161 NODE_SYNCH_BEGIN_EVT = 0xcbe,
162 NODE_SYNCH_END_EVT = 0xcee
163};
164
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400165static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
166 struct sk_buff_head *xmitq,
167 struct tipc_media_addr **maddr);
168static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
169 bool delete);
170static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800171static void tipc_node_delete(struct tipc_node *node);
Kees Cook31b102b2017-10-30 14:06:45 -0700172static void tipc_node_timeout(struct timer_list *t);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400173static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500174static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100175static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
Jon Maloy38077b82017-10-13 11:04:19 +0200176static bool node_is_up(struct tipc_node *n);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200177static void tipc_node_delete_from_list(struct tipc_node *node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400179struct tipc_sock_conn {
180 u32 port;
181 u32 peer_port;
182 u32 peer_node;
183 struct list_head list;
184};
185
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500186static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
187{
188 int bearer_id = n->active_links[sel & 1];
189
190 if (unlikely(bearer_id == INVALID_BEARER_ID))
191 return NULL;
192
193 return n->links[bearer_id].link;
194}
195
Hoang Lef73b1282019-10-29 07:51:21 +0700196int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500197{
198 struct tipc_node *n;
199 int bearer_id;
200 unsigned int mtu = MAX_MSG_SIZE;
201
202 n = tipc_node_find(net, addr);
203 if (unlikely(!n))
204 return mtu;
205
Hoang Lef73b1282019-10-29 07:51:21 +0700206 /* Allow MAX_MSG_SIZE when building connection oriented message
207 * if they are in the same core network
208 */
209 if (n->peer_net && connected) {
210 tipc_node_put(n);
211 return mtu;
212 }
213
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500214 bearer_id = n->active_links[sel & 1];
215 if (likely(bearer_id != INVALID_BEARER_ID))
216 mtu = n->links[bearer_id].mtu;
217 tipc_node_put(n);
218 return mtu;
219}
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400220
Jon Maloy3e5cf362018-04-25 19:29:36 +0200221bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
222{
223 u8 *own_id = tipc_own_id(net);
224 struct tipc_node *n;
225
226 if (!own_id)
227 return true;
228
229 if (addr == tipc_own_addr(net)) {
230 memcpy(id, own_id, TIPC_NODEID_LEN);
231 return true;
232 }
233 n = tipc_node_find(net, addr);
234 if (!n)
235 return false;
236
237 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
238 tipc_node_put(n);
239 return true;
240}
241
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400242u16 tipc_node_get_capabilities(struct net *net, u32 addr)
243{
244 struct tipc_node *n;
245 u16 caps;
246
247 n = tipc_node_find(net, addr);
248 if (unlikely(!n))
249 return TIPC_NODE_CAPABILITIES;
250 caps = n->capabilities;
251 tipc_node_put(n);
252 return caps;
253}
254
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700255u32 tipc_node_get_addr(struct tipc_node *node)
256{
257 return (node) ? node->addr : 0;
258}
259
260char *tipc_node_get_id_str(struct tipc_node *node)
261{
262 return node->peer_id_string;
263}
264
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700265#ifdef CONFIG_TIPC_CRYPTO
266/**
267 * tipc_node_crypto_rx - Retrieve crypto RX handle from node
268 * Note: node ref counter must be held first!
269 */
270struct tipc_crypto *tipc_node_crypto_rx(struct tipc_node *__n)
271{
272 return (__n) ? __n->crypto_rx : NULL;
273}
274
275struct tipc_crypto *tipc_node_crypto_rx_by_list(struct list_head *pos)
276{
277 return container_of(pos, struct tipc_node, list)->crypto_rx;
278}
279#endif
280
Chen Wandun2437fd72020-02-10 16:11:09 +0800281static void tipc_node_free(struct rcu_head *rp)
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700282{
283 struct tipc_node *n = container_of(rp, struct tipc_node, rcu);
284
285#ifdef CONFIG_TIPC_CRYPTO
286 tipc_crypto_stop(&n->crypto_rx);
287#endif
288 kfree(n);
289}
290
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800291static void tipc_node_kref_release(struct kref *kref)
292{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500293 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800294
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500295 kfree(n->bc_entry.link);
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700296 call_rcu(&n->rcu, tipc_node_free);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800297}
298
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700299void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800300{
301 kref_put(&node->kref, tipc_node_kref_release);
302}
303
304static void tipc_node_get(struct tipc_node *node)
305{
306 kref_get(&node->kref);
307}
308
Allan Stephensa635b462011-11-04 11:54:43 -0400309/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500310 * tipc_node_find - locate specified node object, if it exists
311 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500312static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500313{
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500314 struct tipc_net *tn = tipc_net(net);
Allan Stephens672d99e2011-02-25 18:42:52 -0500315 struct tipc_node *node;
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500316 unsigned int thash = tipc_hashfn(addr);
Allan Stephens672d99e2011-02-25 18:42:52 -0500317
Ying Xue6c7a7622014-03-27 12:54:37 +0800318 rcu_read_lock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500319 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700320 if (node->addr != addr || node->preliminary)
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500321 continue;
322 if (!kref_get_unless_zero(&node->kref))
323 node = NULL;
324 break;
Allan Stephens672d99e2011-02-25 18:42:52 -0500325 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800326 rcu_read_unlock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500327 return node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500328}
329
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100330/* tipc_node_find_by_id - locate specified node object by its 128-bit id
331 * Note: this function is called only when a discovery request failed
332 * to find the node by its 32-bit id, and is not time critical
333 */
334static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
335{
336 struct tipc_net *tn = tipc_net(net);
337 struct tipc_node *n;
338 bool found = false;
339
340 rcu_read_lock();
341 list_for_each_entry_rcu(n, &tn->node_list, list) {
342 read_lock_bh(&n->lock);
343 if (!memcmp(id, n->peer_id, 16) &&
344 kref_get_unless_zero(&n->kref))
345 found = true;
346 read_unlock_bh(&n->lock);
347 if (found)
348 break;
349 }
350 rcu_read_unlock();
351 return found ? n : NULL;
352}
353
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500354static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500355{
356 read_lock_bh(&n->lock);
357}
358
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500359static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500360{
361 read_unlock_bh(&n->lock);
362}
363
364static void tipc_node_write_lock(struct tipc_node *n)
365{
366 write_lock_bh(&n->lock);
367}
368
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100369static void tipc_node_write_unlock_fast(struct tipc_node *n)
370{
371 write_unlock_bh(&n->lock);
372}
373
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500374static void tipc_node_write_unlock(struct tipc_node *n)
375{
376 struct net *net = n->net;
377 u32 addr = 0;
378 u32 flags = n->action_flags;
379 u32 link_id = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400380 u32 bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500381 struct list_head *publ_list;
382
383 if (likely(!flags)) {
384 write_unlock_bh(&n->lock);
385 return;
386 }
387
388 addr = n->addr;
389 link_id = n->link_id;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400390 bearer_id = link_id & 0xffff;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500391 publ_list = &n->publ_list;
392
393 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
394 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
395
396 write_unlock_bh(&n->lock);
397
398 if (flags & TIPC_NOTIFY_NODE_DOWN)
399 tipc_publ_notify(net, publ_list, addr);
400
401 if (flags & TIPC_NOTIFY_NODE_UP)
402 tipc_named_node_up(net, addr);
403
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400404 if (flags & TIPC_NOTIFY_LINK_UP) {
405 tipc_mon_peer_up(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500406 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
Jon Maloy218527f2018-03-29 23:20:41 +0200407 TIPC_NODE_SCOPE, link_id, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400408 }
409 if (flags & TIPC_NOTIFY_LINK_DOWN) {
410 tipc_mon_peer_down(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500411 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
Jon Maloy37922ea2018-03-29 23:20:43 +0200412 addr, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400413 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500414}
415
Hoang Lef73b1282019-10-29 07:51:21 +0700416static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
417{
418 int net_id = tipc_netid(n->net);
419 struct tipc_net *tn_peer;
420 struct net *tmp;
421 u32 hash_chk;
422
423 if (n->peer_net)
424 return;
425
426 for_each_net_rcu(tmp) {
427 tn_peer = tipc_net(tmp);
428 if (!tn_peer)
429 continue;
430 /* Integrity checking whether node exists in namespace or not */
431 if (tn_peer->net_id != net_id)
432 continue;
433 if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
434 continue;
435 hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
436 if (hash_mixes ^ hash_chk)
437 continue;
438 n->peer_net = tmp;
439 n->peer_hash_mix = hash_mixes;
440 break;
441 }
442}
443
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700444struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
445 u16 capabilities, u32 hash_mixes,
446 bool preliminary)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447{
Ying Xuef2f98002015-01-09 15:27:05 +0800448 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500449 struct tipc_node *n, *temp_node;
Jon Maloy9012de52018-07-10 01:07:35 +0200450 struct tipc_link *l;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700451 unsigned long intv;
Jon Maloy9012de52018-07-10 01:07:35 +0200452 int bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500453 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
Ying Xuef2f98002015-01-09 15:27:05 +0800455 spin_lock_bh(&tn->node_list_lock);
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700456 n = tipc_node_find(net, addr) ?:
457 tipc_node_find_by_id(net, peer_id);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400458 if (n) {
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700459 if (!n->preliminary)
460 goto update;
461 if (preliminary)
462 goto exit;
463 /* A preliminary node becomes "real" now, refresh its data */
464 tipc_node_write_lock(n);
465 n->preliminary = false;
466 n->addr = addr;
467 hlist_del_rcu(&n->hash);
468 hlist_add_head_rcu(&n->hash,
469 &tn->node_htable[tipc_hashfn(addr)]);
470 list_del_rcu(&n->list);
471 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
472 if (n->addr < temp_node->addr)
473 break;
474 }
475 list_add_tail_rcu(&n->list, &temp_node->list);
476 tipc_node_write_unlock_fast(n);
477
478update:
Hoang Lef73b1282019-10-29 07:51:21 +0700479 if (n->peer_hash_mix ^ hash_mixes)
480 tipc_node_assign_peer_net(n, hash_mixes);
Jon Maloy40999f12018-07-18 19:50:06 +0200481 if (n->capabilities == capabilities)
482 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400483 /* Same node may come back with new capabilities */
Jon Maloy909620f2019-04-11 21:56:28 +0200484 tipc_node_write_lock(n);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400485 n->capabilities = capabilities;
Jon Maloy9012de52018-07-10 01:07:35 +0200486 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
487 l = n->links[bearer_id].link;
488 if (l)
489 tipc_link_update_caps(l, capabilities);
490 }
Jon Maloy909620f2019-04-11 21:56:28 +0200491 tipc_node_write_unlock_fast(n);
492
Hoang Leff2ebbf2019-03-19 18:49:49 +0700493 /* Calculate cluster capabilities */
494 tn->capabilities = TIPC_NODE_CAPABILITIES;
495 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
496 tn->capabilities &= temp_node->capabilities;
497 }
Hoang Lef73b1282019-10-29 07:51:21 +0700498
Hoang Leba5f6a82019-11-21 10:01:09 +0700499 tipc_bcast_toggle_rcast(net,
500 (tn->capabilities & TIPC_BCAST_RCAST));
501
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500502 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400503 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500504 n = kzalloc(sizeof(*n), GFP_ATOMIC);
505 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400506 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500507 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700508 }
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700509 tipc_nodeid2string(n->peer_id_string, peer_id);
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700510#ifdef CONFIG_TIPC_CRYPTO
511 if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
512 pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
513 kfree(n);
514 n = NULL;
515 goto exit;
516 }
517#endif
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500518 n->addr = addr;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700519 n->preliminary = preliminary;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100520 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500521 n->net = net;
Hoang Lef73b1282019-10-29 07:51:21 +0700522 n->peer_net = NULL;
523 n->peer_hash_mix = 0;
524 /* Assign kernel local namespace if exists */
525 tipc_node_assign_peer_net(n, hash_mixes);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500526 n->capabilities = capabilities;
527 kref_init(&n->kref);
528 rwlock_init(&n->lock);
529 INIT_HLIST_NODE(&n->hash);
530 INIT_LIST_HEAD(&n->list);
531 INIT_LIST_HEAD(&n->publ_list);
532 INIT_LIST_HEAD(&n->conn_sks);
533 skb_queue_head_init(&n->bc_entry.namedq);
534 skb_queue_head_init(&n->bc_entry.inputq1);
535 __skb_queue_head_init(&n->bc_entry.arrvq);
536 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500537 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500538 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500539 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200540 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500541 n->signature = INVALID_NODE_SIG;
542 n->active_links[0] = INVALID_BEARER_ID;
543 n->active_links[1] = INVALID_BEARER_ID;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700544 n->bc_entry.link = NULL;
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500545 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700546 timer_setup(&n->timer, tipc_node_timeout, 0);
Tuong Lien4cbf8ac2019-11-08 12:05:09 +0700547 /* Start a slow timer anyway, crypto needs it */
548 n->keepalive_intv = 10000;
549 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
550 if (!mod_timer(&n->timer, intv))
551 tipc_node_get(n);
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500552 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
553 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
554 if (n->addr < temp_node->addr)
555 break;
556 }
557 list_add_tail_rcu(&n->list, &temp_node->list);
Hoang Leff2ebbf2019-03-19 18:49:49 +0700558 /* Calculate cluster capabilities */
559 tn->capabilities = TIPC_NODE_CAPABILITIES;
560 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
561 tn->capabilities &= temp_node->capabilities;
562 }
Hoang Leba5f6a82019-11-21 10:01:09 +0700563 tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
Tuong Lieneb18a512018-12-19 09:17:59 +0700564 trace_tipc_node_create(n, true, " ");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500565exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800566 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500567 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568}
569
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400570static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
571{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500572 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400573 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400574
575 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400576 if (intv < n->keepalive_intv)
577 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400578
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400579 /* Ensure link's abort limit corresponds to current tolerance */
580 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400581}
582
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200583static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800585 list_del_rcu(&node->list);
586 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500587 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200588}
589
590static void tipc_node_delete(struct tipc_node *node)
591{
Tuong Lieneb18a512018-12-19 09:17:59 +0700592 trace_tipc_node_delete(node, true, " ");
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200593 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500594
595 del_timer_sync(&node->timer);
596 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597}
598
Ying Xuef2f98002015-01-09 15:27:05 +0800599void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800600{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500601 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800602 struct tipc_node *node, *t_node;
603
Ying Xuef2f98002015-01-09 15:27:05 +0800604 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500605 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
606 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800607 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800608}
609
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500610void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
611{
612 struct tipc_node *n;
613
614 if (in_own_node(net, addr))
615 return;
616
617 n = tipc_node_find(net, addr);
618 if (!n) {
619 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
620 return;
621 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500622 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500623 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100624 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500625 tipc_node_put(n);
626}
627
628void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
629{
630 struct tipc_node *n;
631
632 if (in_own_node(net, addr))
633 return;
634
635 n = tipc_node_find(net, addr);
636 if (!n) {
637 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
638 return;
639 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500640 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500641 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100642 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500643 tipc_node_put(n);
644}
645
Ying Xuef2f98002015-01-09 15:27:05 +0800646int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400647{
648 struct tipc_node *node;
649 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800650 int err = 0;
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400651
Ying Xue34747532015-01-09 15:27:10 +0800652 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400653 return 0;
654
Ying Xuef2f98002015-01-09 15:27:05 +0800655 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400656 if (!node) {
657 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
658 return -EHOSTUNREACH;
659 }
660 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800661 if (!conn) {
662 err = -EHOSTUNREACH;
663 goto exit;
664 }
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400665 conn->peer_node = dnode;
666 conn->port = port;
667 conn->peer_port = peer_port;
668
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500669 tipc_node_write_lock(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400670 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500671 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800672exit:
673 tipc_node_put(node);
674 return err;
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400675}
676
Ying Xuef2f98002015-01-09 15:27:05 +0800677void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400678{
679 struct tipc_node *node;
680 struct tipc_sock_conn *conn, *safe;
681
Ying Xue34747532015-01-09 15:27:10 +0800682 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400683 return;
684
Ying Xuef2f98002015-01-09 15:27:05 +0800685 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400686 if (!node)
687 return;
688
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500689 tipc_node_write_lock(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400690 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
691 if (port != conn->port)
692 continue;
693 list_del(&conn->list);
694 kfree(conn);
695 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500696 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800697 tipc_node_put(node);
Jon Paul Maloy02be61a92014-08-22 18:09:08 -0400698}
699
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200700static void tipc_node_clear_links(struct tipc_node *node)
701{
702 int i;
703
704 for (i = 0; i < MAX_BEARERS; i++) {
705 struct tipc_link_entry *le = &node->links[i];
706
707 if (le->link) {
708 kfree(le->link);
709 le->link = NULL;
710 node->link_cnt--;
711 }
712 }
713}
714
715/* tipc_node_cleanup - delete nodes that does not
716 * have active links for NODE_CLEANUP_AFTER time
717 */
Jon Maloyec835f892018-11-26 12:26:14 -0500718static bool tipc_node_cleanup(struct tipc_node *peer)
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200719{
Hoang Leff2ebbf2019-03-19 18:49:49 +0700720 struct tipc_node *temp_node;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200721 struct tipc_net *tn = tipc_net(peer->net);
722 bool deleted = false;
723
Jon Maloyec835f892018-11-26 12:26:14 -0500724 /* If lock held by tipc_node_stop() the node will be deleted anyway */
725 if (!spin_trylock_bh(&tn->node_list_lock))
726 return false;
727
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200728 tipc_node_write_lock(peer);
729
730 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
731 tipc_node_clear_links(peer);
732 tipc_node_delete_from_list(peer);
733 deleted = true;
734 }
735 tipc_node_write_unlock(peer);
Hoang Leff2ebbf2019-03-19 18:49:49 +0700736
Hoang Le6708ef72019-11-06 13:26:09 +0700737 if (!deleted) {
738 spin_unlock_bh(&tn->node_list_lock);
739 return deleted;
740 }
741
Hoang Leff2ebbf2019-03-19 18:49:49 +0700742 /* Calculate cluster capabilities */
743 tn->capabilities = TIPC_NODE_CAPABILITIES;
744 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
745 tn->capabilities &= temp_node->capabilities;
746 }
Hoang Leba5f6a82019-11-21 10:01:09 +0700747 tipc_bcast_toggle_rcast(peer->net,
748 (tn->capabilities & TIPC_BCAST_RCAST));
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200749 spin_unlock_bh(&tn->node_list_lock);
750 return deleted;
751}
752
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400753/* tipc_node_timeout - handle expiration of node timer
754 */
Kees Cook31b102b2017-10-30 14:06:45 -0700755static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400756{
Kees Cook31b102b2017-10-30 14:06:45 -0700757 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400758 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400759 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200760 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400761 int bearer_id;
762 int rc = 0;
763
Tuong Lieneb18a512018-12-19 09:17:59 +0700764 trace_tipc_node_timeout(n, false, " ");
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200765 if (!node_is_up(n) && tipc_node_cleanup(n)) {
766 /*Removing the reference of Timer*/
767 tipc_node_put(n);
768 return;
769 }
770
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700771#ifdef CONFIG_TIPC_CRYPTO
772 /* Take any crypto key related actions first */
773 tipc_crypto_timeout(n->crypto_rx);
774#endif
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400775 __skb_queue_head_init(&xmitq);
776
Hoang Lef5d6c3e2018-12-06 09:00:09 +0700777 /* Initial node interval to value larger (10 seconds), then it will be
778 * recalculated with link lowest tolerance
779 */
780 tipc_node_read_lock(n);
781 n->keepalive_intv = 10000;
782 tipc_node_read_unlock(n);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200783 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500784 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400785 le = &n->links[bearer_id];
786 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200787 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400788 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400789 tipc_node_calculate_timer(n, le->link);
790 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200791 spin_unlock_bh(&le->lock);
792 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400793 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500794 tipc_node_read_unlock(n);
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700795 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr, n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400796 if (rc & TIPC_LINK_DOWN_EVT)
797 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400798 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400799 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400800}
801
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400803 * __tipc_node_link_up - handle addition of link
804 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 * Link becomes active (alone or shared) or standby, depending on its priority.
806 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400807static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
808 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400810 int *slot0 = &n->active_links[0];
811 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400812 struct tipc_link *ol = node_active_link(n, 0);
813 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400815 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400816 return;
817
818 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
819 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400820 return;
821
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400822 n->working_links++;
823 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500824 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400825
826 /* Leave room for tunnel header when returning 'mtu' to users: */
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700827 n->links[bearer_id].mtu = tipc_link_mss(nl);
Ying Xue7b8613e2014-10-20 14:44:25 +0800828
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400829 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400830 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400831
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100832 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500833 tipc_link_name(nl), tipc_link_plane(nl));
Tuong Lieneb18a512018-12-19 09:17:59 +0700834 trace_tipc_node_link_up(n, true, " ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400836 /* Ensure that a STATE message goes first */
837 tipc_link_build_state_msg(nl, xmitq);
838
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400839 /* First link? => give it both slots */
840 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400841 *slot0 = bearer_id;
842 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400843 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
844 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400845 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400846 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400847 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400849
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400850 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500851 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
852 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400853 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400854 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400855 tipc_link_set_active(nl, true);
856 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500857 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400858 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a842015-10-22 08:51:47 -0400859 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400860 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500861 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400864 /* Prepare synchronization with first link */
865 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866}
867
868/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400869 * tipc_node_link_up - handle addition of link
870 *
871 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400873static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
874 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400876 struct tipc_media_addr *maddr;
877
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500878 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400879 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400880 maddr = &n->links[bearer_id].maddr;
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700881 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr, n);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500882 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400883}
884
885/**
Tuong Lienc0b14a02019-05-02 17:23:23 +0700886 * tipc_node_link_failover() - start failover in case "half-failover"
887 *
888 * This function is only called in a very special situation where link
889 * failover can be already started on peer node but not on this node.
890 * This can happen when e.g.
891 * 1. Both links <1A-2A>, <1B-2B> down
892 * 2. Link endpoint 2A up, but 1A still down (e.g. due to network
893 * disturbance, wrong session, etc.)
894 * 3. Link <1B-2B> up
895 * 4. Link endpoint 2A down (e.g. due to link tolerance timeout)
Tuong Liend0f84d02019-06-17 11:56:12 +0700896 * 5. Node 2 starts failover onto link <1B-2B>
Tuong Lienc0b14a02019-05-02 17:23:23 +0700897 *
Tuong Liend0f84d02019-06-17 11:56:12 +0700898 * ==> Node 1 does never start link/node failover!
Tuong Lienc0b14a02019-05-02 17:23:23 +0700899 *
900 * @n: tipc node structure
901 * @l: link peer endpoint failingover (- can be NULL)
902 * @tnl: tunnel link
903 * @xmitq: queue for messages to be xmited on tnl link later
904 */
905static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
906 struct tipc_link *tnl,
907 struct sk_buff_head *xmitq)
908{
909 /* Avoid to be "self-failover" that can never end */
910 if (!tipc_link_is_up(tnl))
911 return;
912
Tuong Liend0f84d02019-06-17 11:56:12 +0700913 /* Don't rush, failure link may be in the process of resetting */
914 if (l && !tipc_link_is_reset(l))
915 return;
916
Tuong Lienc0b14a02019-05-02 17:23:23 +0700917 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
918 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
919
920 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
921 tipc_link_failover_prepare(l, tnl, xmitq);
922
923 if (l)
924 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
925 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
926}
927
928/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400929 * __tipc_node_link_down - handle loss of link
930 */
931static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
932 struct sk_buff_head *xmitq,
933 struct tipc_media_addr **maddr)
934{
935 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400936 int *slot0 = &n->active_links[0];
937 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500938 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400939 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400941 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400942 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400943 return;
944
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400945 n->working_links--;
946 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500947 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700948
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400949 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400950
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100951 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500952 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400954 /* Select new active link if any available */
955 *slot0 = INVALID_BEARER_ID;
956 *slot1 = INVALID_BEARER_ID;
957 for (i = 0; i < MAX_BEARERS; i++) {
958 _l = n->links[i].link;
959 if (!_l || !tipc_link_is_up(_l))
960 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400961 if (_l == l)
962 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500963 prio = tipc_link_prio(_l);
964 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400965 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500966 if (prio > highest) {
967 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400968 *slot0 = i;
969 *slot1 = i;
970 continue;
971 }
972 *slot1 = i;
973 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400974
Jon Maloy38077b82017-10-13 11:04:19 +0200975 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400976 if (tipc_link_peer_is_down(l))
977 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
978 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
Tuong Lien26574db2018-12-19 09:17:57 +0700979 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400980 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400981 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400982 tipc_link_build_reset_msg(l, xmitq);
983 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400984 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400985 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400986 return;
987 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400988 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400989
990 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500991 *bearer_id = n->active_links[0];
992 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400993 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
994 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500995 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400996 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Tuong Lien26574db2018-12-19 09:17:57 +0700997 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400998 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400999 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001000 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001001 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001002 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001003}
1004
1005static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
1006{
1007 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Maloy737889e2019-03-22 15:03:51 +01001008 struct tipc_media_addr *maddr = NULL;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001009 struct tipc_link *l = le->link;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001010 int old_bearer_id = bearer_id;
Jon Maloy737889e2019-03-22 15:03:51 +01001011 struct sk_buff_head xmitq;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001012
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001013 if (!l)
1014 return;
1015
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001016 __skb_queue_head_init(&xmitq);
1017
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001018 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001019 if (!tipc_link_is_establishing(l)) {
1020 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001021 } else {
1022 /* Defuse pending tipc_node_link_up() */
Tuong Lien91986ee2019-02-11 13:29:43 +07001023 tipc_link_reset(l);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001024 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001025 }
Tuong Lien91986ee2019-02-11 13:29:43 +07001026 if (delete) {
1027 kfree(l);
1028 le->link = NULL;
1029 n->link_cnt--;
1030 }
Tuong Lieneb18a512018-12-19 09:17:59 +07001031 trace_tipc_node_link_down(n, true, "node link down or deleted!");
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001032 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001033 if (delete)
1034 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Maloy737889e2019-03-22 15:03:51 +01001035 if (!skb_queue_empty(&xmitq))
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001036 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr, n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001037 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038}
1039
Jon Maloy38077b82017-10-13 11:04:19 +02001040static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -04001042 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043}
1044
Jon Maloy38077b82017-10-13 11:04:19 +02001045bool tipc_node_is_up(struct net *net, u32 addr)
1046{
1047 struct tipc_node *n;
1048 bool retval = false;
1049
1050 if (in_own_node(net, addr))
1051 return true;
1052
1053 n = tipc_node_find(net, addr);
1054 if (!n)
1055 return false;
1056 retval = node_is_up(n);
1057 tipc_node_put(n);
1058 return retval;
1059}
1060
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001061static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
1062{
1063 struct tipc_node *n;
1064
1065 addr ^= tipc_net(net)->random;
1066 while ((n = tipc_node_find(net, addr))) {
1067 tipc_node_put(n);
1068 addr++;
1069 }
1070 return addr;
1071}
1072
1073/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
Jon Maloy2a57f182018-07-06 20:10:03 +02001074 * Returns suggested address if any, otherwise 0
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001075 */
1076u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
1077{
1078 struct tipc_net *tn = tipc_net(net);
1079 struct tipc_node *n;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001080 bool preliminary;
1081 u32 sugg_addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001082
1083 /* Suggest new address if some other peer is using this one */
1084 n = tipc_node_find(net, addr);
1085 if (n) {
1086 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
1087 addr = 0;
1088 tipc_node_put(n);
1089 if (!addr)
1090 return 0;
1091 return tipc_node_suggest_addr(net, addr);
1092 }
1093
1094 /* Suggest previously used address if peer is known */
1095 n = tipc_node_find_by_id(net, id);
1096 if (n) {
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001097 sugg_addr = n->addr;
1098 preliminary = n->preliminary;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001099 tipc_node_put(n);
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001100 if (!preliminary)
1101 return sugg_addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001102 }
Jon Maloy2a57f182018-07-06 20:10:03 +02001103
1104 /* Even this node may be in conflict */
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001105 if (tn->trial_addr == addr)
1106 return tipc_node_suggest_addr(net, addr);
1107
Jon Maloy2a57f182018-07-06 20:10:03 +02001108 return 0;
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001109}
1110
1111void tipc_node_check_dest(struct net *net, u32 addr,
1112 u8 *peer_id, struct tipc_bearer *b,
Hoang Lef73b1282019-10-29 07:51:21 +07001113 u16 capabilities, u32 signature, u32 hash_mixes,
Jon Paul Maloycf148812015-07-30 18:24:22 -04001114 struct tipc_media_addr *maddr,
1115 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001116{
Jon Paul Maloycf148812015-07-30 18:24:22 -04001117 struct tipc_node *n;
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001118 struct tipc_link *l, *snd_l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001119 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001120 bool addr_match = false;
1121 bool sign_match = false;
1122 bool link_up = false;
1123 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001124 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001125 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001126 unsigned long intv;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001127 u16 session;
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001128
Jon Paul Maloycf148812015-07-30 18:24:22 -04001129 *dupl_addr = false;
1130 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001131
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001132 n = tipc_node_create(net, addr, peer_id, capabilities, hash_mixes,
1133 false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001134 if (!n)
1135 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001136
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001137 tipc_node_write_lock(n);
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001138 if (unlikely(!n->bc_entry.link)) {
1139 snd_l = tipc_bc_sndlink(net);
1140 if (!tipc_link_bc_create(net, tipc_own_addr(net),
1141 addr, U16_MAX,
Jon Maloy16ad3f42019-12-10 00:52:46 +01001142 tipc_link_min_win(snd_l),
1143 tipc_link_max_win(snd_l),
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07001144 n->capabilities,
1145 &n->bc_entry.inputq1,
1146 &n->bc_entry.namedq, snd_l,
1147 &n->bc_entry.link)) {
1148 pr_warn("Broadcast rcv link creation failed, no mem\n");
1149 tipc_node_write_unlock_fast(n);
1150 tipc_node_put(n);
1151 return;
1152 }
1153 }
Jon Paul Maloycf148812015-07-30 18:24:22 -04001154
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001155 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -04001156
1157 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001158 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001159 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001160 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001161 sign_match = (signature == n->signature);
1162
1163 /* These three flags give us eight permutations: */
1164
1165 if (sign_match && addr_match && link_up) {
1166 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001167 reset = false;
Hoang Led408bef2019-11-08 10:02:37 +07001168 /* Peer node is not a container/local namespace */
1169 if (!n->peer_hash_mix)
1170 n->peer_hash_mix = hash_mixes;
Jon Paul Maloycf148812015-07-30 18:24:22 -04001171 } else if (sign_match && addr_match && !link_up) {
1172 /* Respond. The link will come up in due time */
1173 *respond = true;
1174 } else if (sign_match && !addr_match && link_up) {
1175 /* Peer has changed i/f address without rebooting.
1176 * If so, the link will reset soon, and the next
1177 * discovery will be accepted. So we can ignore it.
1178 * It may also be an cloned or malicious peer having
1179 * chosen the same node address and signature as an
1180 * existing one.
1181 * Ignore requests until the link goes down, if ever.
1182 */
1183 *dupl_addr = true;
1184 } else if (sign_match && !addr_match && !link_up) {
1185 /* Peer link has changed i/f address without rebooting.
1186 * It may also be a cloned or malicious peer; we can't
1187 * distinguish between the two.
1188 * The signature is correct, so we must accept.
1189 */
1190 accept_addr = true;
1191 *respond = true;
1192 } else if (!sign_match && addr_match && link_up) {
1193 /* Peer node rebooted. Two possibilities:
1194 * - Delayed re-discovery; this link endpoint has already
1195 * reset and re-established contact with the peer, before
1196 * receiving a discovery message from that node.
1197 * (The peer happened to receive one from this node first).
1198 * - The peer came back so fast that our side has not
1199 * discovered it yet. Probing from this side will soon
1200 * reset the link, since there can be no working link
1201 * endpoint at the peer end, and the link will re-establish.
1202 * Accept the signature, since it comes from a known peer.
1203 */
1204 n->signature = signature;
1205 } else if (!sign_match && addr_match && !link_up) {
1206 /* The peer node has rebooted.
1207 * Accept signature, since it is a known peer.
1208 */
1209 n->signature = signature;
1210 *respond = true;
1211 } else if (!sign_match && !addr_match && link_up) {
1212 /* Peer rebooted with new address, or a new/duplicate peer.
1213 * Ignore until the link goes down, if ever.
1214 */
1215 *dupl_addr = true;
1216 } else if (!sign_match && !addr_match && !link_up) {
1217 /* Peer rebooted with new address, or it is a new peer.
1218 * Accept signature and address.
1219 */
1220 n->signature = signature;
1221 accept_addr = true;
1222 *respond = true;
1223 }
1224
1225 if (!accept_addr)
1226 goto exit;
1227
1228 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001229 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +01001230 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001231 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +01001232
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001233 if_name = strchr(b->name, ':') + 1;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001234 get_random_bytes(&session, sizeof(u16));
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001235 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001236 b->net_plane, b->mtu, b->priority,
Jon Maloy16ad3f42019-12-10 00:52:46 +01001237 b->min_win, b->max_win, session,
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001238 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -04001239 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001240 tipc_bc_sndlink(n->net), n->bc_entry.link,
1241 &le->inputq,
1242 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -04001243 *respond = false;
1244 goto exit;
1245 }
Tuong Lien26574db2018-12-19 09:17:57 +07001246 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001247 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -04001248 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001249 if (n->state == NODE_FAILINGOVER)
1250 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001251 le->link = l;
1252 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001253 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001254 if (n->link_cnt == 1) {
1255 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1256 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001257 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001258 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001259 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001260 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001261exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001262 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001263 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001264 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001265 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001266}
1267
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001268void tipc_node_delete_links(struct net *net, int bearer_id)
1269{
1270 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001271 struct tipc_node *n;
1272
1273 rcu_read_lock();
1274 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001275 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001276 }
1277 rcu_read_unlock();
1278}
1279
1280static void tipc_node_reset_links(struct tipc_node *n)
1281{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001282 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001283
Jon Maloyd50ccc22018-03-22 20:42:50 +01001284 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001285
Tuong Lieneb18a512018-12-19 09:17:59 +07001286 trace_tipc_node_reset_links(n, true, " ");
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001287 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001288 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001289 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001290}
1291
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001292/* tipc_node_fsm_evt - node finite state machine
1293 * Determines when contact is allowed with peer node
1294 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001295static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001296{
1297 int state = n->state;
1298
1299 switch (state) {
1300 case SELF_DOWN_PEER_DOWN:
1301 switch (evt) {
1302 case SELF_ESTABL_CONTACT_EVT:
1303 state = SELF_UP_PEER_COMING;
1304 break;
1305 case PEER_ESTABL_CONTACT_EVT:
1306 state = SELF_COMING_PEER_UP;
1307 break;
1308 case SELF_LOST_CONTACT_EVT:
1309 case PEER_LOST_CONTACT_EVT:
1310 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001311 case NODE_SYNCH_END_EVT:
1312 case NODE_SYNCH_BEGIN_EVT:
1313 case NODE_FAILOVER_BEGIN_EVT:
1314 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001315 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001316 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001317 }
1318 break;
1319 case SELF_UP_PEER_UP:
1320 switch (evt) {
1321 case SELF_LOST_CONTACT_EVT:
1322 state = SELF_DOWN_PEER_LEAVING;
1323 break;
1324 case PEER_LOST_CONTACT_EVT:
1325 state = SELF_LEAVING_PEER_DOWN;
1326 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001327 case NODE_SYNCH_BEGIN_EVT:
1328 state = NODE_SYNCHING;
1329 break;
1330 case NODE_FAILOVER_BEGIN_EVT:
1331 state = NODE_FAILINGOVER;
1332 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001333 case SELF_ESTABL_CONTACT_EVT:
1334 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001335 case NODE_SYNCH_END_EVT:
1336 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001337 break;
1338 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001339 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001340 }
1341 break;
1342 case SELF_DOWN_PEER_LEAVING:
1343 switch (evt) {
1344 case PEER_LOST_CONTACT_EVT:
1345 state = SELF_DOWN_PEER_DOWN;
1346 break;
1347 case SELF_ESTABL_CONTACT_EVT:
1348 case PEER_ESTABL_CONTACT_EVT:
1349 case SELF_LOST_CONTACT_EVT:
1350 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001351 case NODE_SYNCH_END_EVT:
1352 case NODE_SYNCH_BEGIN_EVT:
1353 case NODE_FAILOVER_BEGIN_EVT:
1354 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001355 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001356 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001357 }
1358 break;
1359 case SELF_UP_PEER_COMING:
1360 switch (evt) {
1361 case PEER_ESTABL_CONTACT_EVT:
1362 state = SELF_UP_PEER_UP;
1363 break;
1364 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001365 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001366 break;
1367 case SELF_ESTABL_CONTACT_EVT:
1368 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001369 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001370 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001371 break;
1372 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001373 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001374 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001375 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001376 }
1377 break;
1378 case SELF_COMING_PEER_UP:
1379 switch (evt) {
1380 case SELF_ESTABL_CONTACT_EVT:
1381 state = SELF_UP_PEER_UP;
1382 break;
1383 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001384 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001385 break;
1386 case SELF_LOST_CONTACT_EVT:
1387 case PEER_ESTABL_CONTACT_EVT:
1388 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001389 case NODE_SYNCH_END_EVT:
1390 case NODE_SYNCH_BEGIN_EVT:
1391 case NODE_FAILOVER_BEGIN_EVT:
1392 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001393 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001394 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001395 }
1396 break;
1397 case SELF_LEAVING_PEER_DOWN:
1398 switch (evt) {
1399 case SELF_LOST_CONTACT_EVT:
1400 state = SELF_DOWN_PEER_DOWN;
1401 break;
1402 case SELF_ESTABL_CONTACT_EVT:
1403 case PEER_ESTABL_CONTACT_EVT:
1404 case PEER_LOST_CONTACT_EVT:
1405 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001406 case NODE_SYNCH_END_EVT:
1407 case NODE_SYNCH_BEGIN_EVT:
1408 case NODE_FAILOVER_BEGIN_EVT:
1409 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001410 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001411 goto illegal_evt;
1412 }
1413 break;
1414 case NODE_FAILINGOVER:
1415 switch (evt) {
1416 case SELF_LOST_CONTACT_EVT:
1417 state = SELF_DOWN_PEER_LEAVING;
1418 break;
1419 case PEER_LOST_CONTACT_EVT:
1420 state = SELF_LEAVING_PEER_DOWN;
1421 break;
1422 case NODE_FAILOVER_END_EVT:
1423 state = SELF_UP_PEER_UP;
1424 break;
1425 case NODE_FAILOVER_BEGIN_EVT:
1426 case SELF_ESTABL_CONTACT_EVT:
1427 case PEER_ESTABL_CONTACT_EVT:
1428 break;
1429 case NODE_SYNCH_BEGIN_EVT:
1430 case NODE_SYNCH_END_EVT:
1431 default:
1432 goto illegal_evt;
1433 }
1434 break;
1435 case NODE_SYNCHING:
1436 switch (evt) {
1437 case SELF_LOST_CONTACT_EVT:
1438 state = SELF_DOWN_PEER_LEAVING;
1439 break;
1440 case PEER_LOST_CONTACT_EVT:
1441 state = SELF_LEAVING_PEER_DOWN;
1442 break;
1443 case NODE_SYNCH_END_EVT:
1444 state = SELF_UP_PEER_UP;
1445 break;
1446 case NODE_FAILOVER_BEGIN_EVT:
1447 state = NODE_FAILINGOVER;
1448 break;
1449 case NODE_SYNCH_BEGIN_EVT:
1450 case SELF_ESTABL_CONTACT_EVT:
1451 case PEER_ESTABL_CONTACT_EVT:
1452 break;
1453 case NODE_FAILOVER_END_EVT:
1454 default:
1455 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001456 }
1457 break;
1458 default:
1459 pr_err("Unknown node fsm state %x\n", state);
1460 break;
1461 }
Tuong Lieneb18a512018-12-19 09:17:59 +07001462 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001463 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001464 return;
1465
1466illegal_evt:
1467 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Tuong Lieneb18a512018-12-19 09:17:59 +07001468 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001469}
1470
Jon Paul Maloy52666982015-10-22 08:51:41 -04001471static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001472 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001474 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001475 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001476 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001477 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001478 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479
Jon Maloyd50ccc22018-03-22 20:42:50 +01001480 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001481 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Tuong Lieneb18a512018-12-19 09:17:59 +07001482 trace_tipc_node_lost_contact(n, true, " ");
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001483
Jon Paul Maloy52666982015-10-22 08:51:41 -04001484 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001485 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001487 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001489 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001490 if (l)
1491 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001493
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001494 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001495 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Hoang Led408bef2019-11-08 10:02:37 +07001496 n->peer_net = NULL;
1497 n->peer_hash_mix = 0;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001498 /* Notify sockets connected to node */
1499 list_for_each_entry_safe(conn, safe, conns, list) {
1500 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001501 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001502 conn->peer_node, conn->port,
1503 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001504 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001505 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001506 list_del(&conn->list);
1507 kfree(conn);
1508 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509}
1510
Erik Hugne78acb1f2014-04-24 16:26:47 +02001511/**
1512 * tipc_node_get_linkname - get the name of a link
1513 *
1514 * @bearer_id: id of the bearer
1515 * @node: peer node address
1516 * @linkname: link name output buffer
1517 *
1518 * Returns 0 on success
1519 */
Ying Xuef2f98002015-01-09 15:27:05 +08001520int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1521 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001522{
1523 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001524 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001525 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001526
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001527 if (!node)
1528 return err;
1529
1530 if (bearer_id >= MAX_BEARERS)
1531 goto exit;
1532
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001533 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001534 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001535 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001536 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001537 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001538 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001539 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001540exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001541 tipc_node_put(node);
1542 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001543}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001544
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001545/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001546static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001547{
1548 void *hdr;
1549 struct nlattr *attrs;
1550
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001551 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001552 NLM_F_MULTI, TIPC_NL_NODE_GET);
1553 if (!hdr)
1554 return -EMSGSIZE;
1555
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001556 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001557 if (!attrs)
1558 goto msg_full;
1559
1560 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1561 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001562 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001563 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1564 goto attr_msg_full;
1565
1566 nla_nest_end(msg->skb, attrs);
1567 genlmsg_end(msg->skb, hdr);
1568
1569 return 0;
1570
1571attr_msg_full:
1572 nla_nest_cancel(msg->skb, attrs);
1573msg_full:
1574 genlmsg_cancel(msg->skb, hdr);
1575
1576 return -EMSGSIZE;
1577}
1578
Hoang Lef73b1282019-10-29 07:51:21 +07001579static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
1580{
1581 struct tipc_msg *hdr = buf_msg(skb_peek(list));
1582 struct sk_buff_head inputq;
1583
1584 switch (msg_user(hdr)) {
1585 case TIPC_LOW_IMPORTANCE:
1586 case TIPC_MEDIUM_IMPORTANCE:
1587 case TIPC_HIGH_IMPORTANCE:
1588 case TIPC_CRITICAL_IMPORTANCE:
1589 if (msg_connected(hdr) || msg_named(hdr)) {
1590 tipc_loopback_trace(peer_net, list);
1591 spin_lock_init(&list->lock);
1592 tipc_sk_rcv(peer_net, list);
1593 return;
1594 }
1595 if (msg_mcast(hdr)) {
1596 tipc_loopback_trace(peer_net, list);
1597 skb_queue_head_init(&inputq);
1598 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1599 __skb_queue_purge(list);
1600 skb_queue_purge(&inputq);
1601 return;
1602 }
1603 return;
1604 case MSG_FRAGMENTER:
1605 if (tipc_msg_assemble(list)) {
1606 tipc_loopback_trace(peer_net, list);
1607 skb_queue_head_init(&inputq);
1608 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1609 __skb_queue_purge(list);
1610 skb_queue_purge(&inputq);
1611 }
1612 return;
1613 case GROUP_PROTOCOL:
1614 case CONN_MANAGER:
1615 tipc_loopback_trace(peer_net, list);
1616 spin_lock_init(&list->lock);
1617 tipc_sk_rcv(peer_net, list);
1618 return;
1619 case LINK_PROTOCOL:
1620 case NAME_DISTRIBUTOR:
1621 case TUNNEL_PROTOCOL:
1622 case BCAST_PROTOCOL:
1623 return;
1624 default:
1625 return;
1626 };
1627}
1628
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001629/**
1630 * tipc_node_xmit() is the general link level function for message sending
1631 * @net: the applicable net namespace
1632 * @list: chain of buffers containing message
1633 * @dnode: address of destination node
1634 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001635 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001636 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001637 */
1638int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1639 u32 dnode, int selector)
1640{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001641 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001642 struct tipc_node *n;
1643 struct sk_buff_head xmitq;
Hoang Lef73b1282019-10-29 07:51:21 +07001644 bool node_up = false;
Richard Alpe4952cd32016-02-11 10:43:15 +01001645 int bearer_id;
1646 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001647
Richard Alpe4952cd32016-02-11 10:43:15 +01001648 if (in_own_node(net, dnode)) {
John Rutherford6c9081a2019-08-07 12:52:29 +10001649 tipc_loopback_trace(net, list);
Jon Maloye654f9f2019-08-15 16:42:50 +02001650 spin_lock_init(&list->lock);
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001651 tipc_sk_rcv(net, list);
1652 return 0;
1653 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001654
1655 n = tipc_node_find(net, dnode);
1656 if (unlikely(!n)) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001657 __skb_queue_purge(list);
Richard Alpe4952cd32016-02-11 10:43:15 +01001658 return -EHOSTUNREACH;
1659 }
1660
1661 tipc_node_read_lock(n);
Hoang Lef73b1282019-10-29 07:51:21 +07001662 node_up = node_is_up(n);
1663 if (node_up && n->peer_net && check_net(n->peer_net)) {
1664 /* xmit inner linux container */
1665 tipc_lxc_xmit(n->peer_net, list);
1666 if (likely(skb_queue_empty(list))) {
1667 tipc_node_read_unlock(n);
1668 tipc_node_put(n);
1669 return 0;
1670 }
1671 }
1672
Richard Alpe4952cd32016-02-11 10:43:15 +01001673 bearer_id = n->active_links[selector & 1];
1674 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1675 tipc_node_read_unlock(n);
1676 tipc_node_put(n);
Jon Maloye654f9f2019-08-15 16:42:50 +02001677 __skb_queue_purge(list);
Richard Alpe4952cd32016-02-11 10:43:15 +01001678 return -EHOSTUNREACH;
1679 }
1680
1681 __skb_queue_head_init(&xmitq);
1682 le = &n->links[bearer_id];
1683 spin_lock_bh(&le->lock);
1684 rc = tipc_link_xmit(le->link, list, &xmitq);
1685 spin_unlock_bh(&le->lock);
1686 tipc_node_read_unlock(n);
1687
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001688 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001689 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001690 else
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001691 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
Richard Alpe4952cd32016-02-11 10:43:15 +01001692
1693 tipc_node_put(n);
1694
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001695 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001696}
1697
1698/* tipc_node_xmit_skb(): send single buffer to destination
1699 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1700 * messages, which will not be rejected
1701 * The only exception is datagram messages rerouted after secondary
1702 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001703 */
1704int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1705 u32 selector)
1706{
1707 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001708
Jon Maloye654f9f2019-08-15 16:42:50 +02001709 __skb_queue_head_init(&head);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001710 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001711 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001712 return 0;
1713}
1714
Jon Maloyf70d37b2017-10-13 11:04:21 +02001715/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1716 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1717 */
1718int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1719{
1720 struct sk_buff *skb;
1721 u32 selector, dnode;
1722
1723 while ((skb = __skb_dequeue(xmitq))) {
1724 selector = msg_origport(buf_msg(skb));
1725 dnode = msg_destnode(buf_msg(skb));
1726 tipc_node_xmit_skb(net, skb, dnode, selector);
1727 }
1728 return 0;
1729}
1730
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001731void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1732{
1733 struct sk_buff *txskb;
1734 struct tipc_node *n;
1735 u32 dst;
1736
1737 rcu_read_lock();
1738 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1739 dst = n->addr;
1740 if (in_own_node(net, dst))
1741 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001742 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001743 continue;
1744 txskb = pskb_copy(skb, GFP_ATOMIC);
1745 if (!txskb)
1746 break;
1747 msg_set_destnode(buf_msg(txskb), dst);
1748 tipc_node_xmit_skb(net, txskb, dst, 0);
1749 }
1750 rcu_read_unlock();
1751
1752 kfree_skb(skb);
1753}
1754
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001755static void tipc_node_mcast_rcv(struct tipc_node *n)
1756{
1757 struct tipc_bclink_entry *be = &n->bc_entry;
1758
1759 /* 'arrvq' is under inputq2's lock protection */
1760 spin_lock_bh(&be->inputq2.lock);
1761 spin_lock_bh(&be->inputq1.lock);
1762 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1763 spin_unlock_bh(&be->inputq1.lock);
1764 spin_unlock_bh(&be->inputq2.lock);
1765 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1766}
1767
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001768static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1769 int bearer_id, struct sk_buff_head *xmitq)
1770{
1771 struct tipc_link *ucl;
1772 int rc;
1773
1774 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1775
1776 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001777 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001778 return;
1779 }
1780
1781 if (!(rc & TIPC_LINK_SND_STATE))
1782 return;
1783
1784 /* If probe message, a STATE response will be sent anyway */
1785 if (msg_probe(hdr))
1786 return;
1787
1788 /* Produce a STATE message carrying broadcast NACK */
1789 tipc_node_read_lock(n);
1790 ucl = n->links[bearer_id].link;
1791 if (ucl)
1792 tipc_link_build_state_msg(ucl, xmitq);
1793 tipc_node_read_unlock(n);
1794}
1795
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001796/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001797 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1798 * @net: the applicable net namespace
1799 * @skb: TIPC packet
1800 * @bearer_id: id of bearer message arrived on
1801 *
1802 * Invoked with no locks held.
1803 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001804static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001805{
1806 int rc;
1807 struct sk_buff_head xmitq;
1808 struct tipc_bclink_entry *be;
1809 struct tipc_link_entry *le;
1810 struct tipc_msg *hdr = buf_msg(skb);
1811 int usr = msg_user(hdr);
1812 u32 dnode = msg_destnode(hdr);
1813 struct tipc_node *n;
1814
1815 __skb_queue_head_init(&xmitq);
1816
1817 /* If NACK for other node, let rcv link for that node peek into it */
1818 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1819 n = tipc_node_find(net, dnode);
1820 else
1821 n = tipc_node_find(net, msg_prevnode(hdr));
1822 if (!n) {
1823 kfree_skb(skb);
1824 return;
1825 }
1826 be = &n->bc_entry;
1827 le = &n->links[bearer_id];
1828
1829 rc = tipc_bcast_rcv(net, be->link, skb);
1830
Jon Paul Maloy52666982015-10-22 08:51:41 -04001831 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001832 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001833 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001834 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001835 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001836 }
1837
1838 if (!skb_queue_empty(&xmitq))
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001839 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001840
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001841 if (!skb_queue_empty(&be->inputq1))
1842 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f3e2016-07-11 16:08:37 -04001843
Zhenbo Gao5679ee72018-12-18 17:43:52 +08001844 /* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
1845 if (!skb_queue_empty(&n->bc_entry.namedq))
1846 tipc_named_rcv(net, &n->bc_entry.namedq);
1847
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001848 /* If reassembly or retransmission failure => reset all links to peer */
1849 if (rc & TIPC_LINK_DOWN_EVT)
1850 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f3e2016-07-11 16:08:37 -04001851
Jon Paul Maloy52666982015-10-22 08:51:41 -04001852 tipc_node_put(n);
1853}
1854
1855/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001856 * tipc_node_check_state - check and if necessary update node state
1857 * @skb: TIPC packet
1858 * @bearer_id: identity of bearer delivering the packet
Jon Maloy7ea817f2018-07-10 01:07:36 +02001859 * Returns true if state and msg are ok, otherwise false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001860 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001861static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001862 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001863{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001864 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001865 int usr = msg_user(hdr);
1866 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001867 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001868 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001869 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001870 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001871 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001872 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001873 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001874
Tuong Lieneb18a512018-12-19 09:17:59 +07001875 if (trace_tipc_node_check_state_enabled()) {
1876 trace_tipc_skb_dump(skb, false, "skb for node state check");
1877 trace_tipc_node_check_state(n, true, " ");
1878 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001879 l = n->links[bearer_id].link;
1880 if (!l)
1881 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001882 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001883
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001884
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001885 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1886 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001887
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001888 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001889 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1890 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1891 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001892 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001893 }
1894 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001895
Tuong Lien26574db2018-12-19 09:17:57 +07001896 if (!tipc_link_validate_msg(l, hdr)) {
1897 trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
1898 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
Jon Maloy7ea817f2018-07-10 01:07:36 +02001899 return false;
Tuong Lien26574db2018-12-19 09:17:57 +07001900 }
Jon Maloy7ea817f2018-07-10 01:07:36 +02001901
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001902 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001903 if (state == SELF_UP_PEER_COMING) {
1904 if (!tipc_link_is_up(l))
1905 return true;
1906 if (!msg_peer_link_is_up(hdr))
1907 return true;
1908 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1909 }
1910
1911 if (state == SELF_DOWN_PEER_LEAVING) {
1912 if (msg_peer_node_is_up(hdr))
1913 return false;
1914 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001915 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001916 }
1917
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001918 if (state == SELF_LEAVING_PEER_DOWN)
1919 return false;
1920
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001921 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001922 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001923 return true;
1924
1925 /* Initiate or update failover mode if applicable */
1926 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1927 syncpt = oseqno + exp_pkts - 1;
Tuong Liend0f84d02019-06-17 11:56:12 +07001928 if (pl && !tipc_link_is_reset(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001929 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Tuong Lieneb18a512018-12-19 09:17:59 +07001930 trace_tipc_node_link_down(n, true,
1931 "node link down <- failover!");
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001932 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1933 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001934 }
Tuong Lienc0b14a02019-05-02 17:23:23 +07001935
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001936 /* If parallel link was already down, and this happened before
Tuong Lienc0b14a02019-05-02 17:23:23 +07001937 * the tunnel link came up, node failover was never started.
1938 * Ensure that a FAILOVER_MSG is sent to get peer out of
1939 * NODE_FAILINGOVER state, also this node must accept
1940 * TUNNEL_MSGs from peer.
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001941 */
Tuong Lienc0b14a02019-05-02 17:23:23 +07001942 if (n->state != NODE_FAILINGOVER)
1943 tipc_node_link_failover(n, pl, l, xmitq);
1944
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001945 /* If pkts arrive out of order, use lowest calculated syncpt */
1946 if (less(syncpt, n->sync_point))
1947 n->sync_point = syncpt;
1948 }
1949
1950 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001951 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001952 if (!more(rcv_nxt, n->sync_point))
1953 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001954 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1955 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001956 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001957 return true;
1958 }
1959
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001960 /* No synching needed if only one link */
1961 if (!pl || !tipc_link_is_up(pl))
1962 return true;
1963
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001964 /* Initiate synch mode if applicable */
1965 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Tuong Lien4929a932019-07-24 08:56:11 +07001966 if (n->capabilities & TIPC_TUNNEL_ENHANCED)
1967 syncpt = msg_syncpt(hdr);
1968 else
1969 syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001970 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001971 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001972 if (n->state == SELF_UP_PEER_UP) {
1973 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001974 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001975 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1976 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001977 }
1978
1979 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001980 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001981 if (tipc_link_is_synching(l)) {
1982 tnl = l;
1983 } else {
1984 tnl = pl;
1985 pl = l;
1986 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001987 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1988 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001989 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001990 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001991 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001992 return true;
1993 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001994 if (l == pl)
1995 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001996 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1997 return true;
1998 if (usr == LINK_PROTOCOL)
1999 return true;
2000 return false;
2001 }
2002 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04002003}
2004
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002005/**
2006 * tipc_rcv - process TIPC packets/messages arriving from off-node
2007 * @net: the applicable net namespace
2008 * @skb: TIPC packet
2009 * @bearer: pointer to bearer message arrived on
2010 *
2011 * Invoked with no locks held. Bearer pointer must point to a valid bearer
2012 * structure (i.e. cannot be NULL), but bearer can be inactive.
2013 */
2014void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
2015{
2016 struct sk_buff_head xmitq;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002017 struct tipc_link_entry *le;
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002018 struct tipc_msg *hdr;
2019 struct tipc_node *n;
2020 int bearer_id = b->identity;
Hamish Martinefe79052016-04-29 10:40:24 -04002021 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05002022 int usr, rc = 0;
2023 u16 bc_ack;
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002024#ifdef CONFIG_TIPC_CRYPTO
2025 struct tipc_ehdr *ehdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002026
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002027 /* Check if message must be decrypted first */
2028 if (TIPC_SKB_CB(skb)->decrypted || !tipc_ehdr_validate(skb))
2029 goto rcv;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002030
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002031 ehdr = (struct tipc_ehdr *)skb->data;
2032 if (likely(ehdr->user != LINK_CONFIG)) {
2033 n = tipc_node_find(net, ntohl(ehdr->addr));
2034 if (unlikely(!n))
2035 goto discard;
2036 } else {
2037 n = tipc_node_find_by_id(net, ehdr->id);
2038 }
2039 tipc_crypto_rcv(net, (n) ? n->crypto_rx : NULL, &skb, b);
2040 if (!skb)
2041 return;
2042
2043rcv:
2044#endif
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05002045 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01002046 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002047 goto discard;
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002048 __skb_queue_head_init(&xmitq);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05002049 hdr = buf_msg(skb);
2050 usr = msg_user(hdr);
2051 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002052
Jon Paul Maloy52666982015-10-22 08:51:41 -04002053 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002054 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04002055 if (unlikely(usr == LINK_CONFIG))
2056 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002057 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04002058 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002059 }
2060
Hamish Martinefe79052016-04-29 10:40:24 -04002061 /* Discard unicast link messages destined for another node */
2062 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
2063 goto discard;
2064
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002065 /* Locate neighboring node that sent packet */
2066 n = tipc_node_find(net, msg_prevnode(hdr));
2067 if (unlikely(!n))
2068 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002069 le = &n->links[bearer_id];
2070
Jon Paul Maloy52666982015-10-22 08:51:41 -04002071 /* Ensure broadcast reception is in synch with peer's send state */
2072 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002073 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002074 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04002075 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002076
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002077 /* Receive packet directly if conditions permit */
2078 tipc_node_read_lock(n);
2079 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05002080 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002081 if (le->link) {
2082 rc = tipc_link_rcv(le->link, skb, &xmitq);
2083 skb = NULL;
2084 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05002085 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002086 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002087 tipc_node_read_unlock(n);
2088
2089 /* Check/update node state before receiving */
2090 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02002091 if (unlikely(skb_linearize(skb)))
2092 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002093 tipc_node_write_lock(n);
2094 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
2095 if (le->link) {
2096 rc = tipc_link_rcv(le->link, skb, &xmitq);
2097 skb = NULL;
2098 }
2099 }
2100 tipc_node_write_unlock(n);
2101 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002102
2103 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002104 tipc_node_link_up(n, bearer_id, &xmitq);
2105
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002106 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04002107 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002108
Jon Paul Maloy52666982015-10-22 08:51:41 -04002109 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
2110 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04002111
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05002112 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
2113 tipc_node_mcast_rcv(n);
2114
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002115 if (!skb_queue_empty(&le->inputq))
2116 tipc_sk_rcv(net, &le->inputq);
2117
2118 if (!skb_queue_empty(&xmitq))
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002119 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04002120
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002121 tipc_node_put(n);
2122discard:
2123 kfree_skb(skb);
2124}
2125
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02002126void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
2127 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01002128{
2129 struct tipc_net *tn = tipc_net(net);
2130 int bearer_id = b->identity;
2131 struct sk_buff_head xmitq;
2132 struct tipc_link_entry *e;
2133 struct tipc_node *n;
2134
2135 __skb_queue_head_init(&xmitq);
2136
2137 rcu_read_lock();
2138
2139 list_for_each_entry_rcu(n, &tn->node_list, list) {
2140 tipc_node_write_lock(n);
2141 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02002142 if (e->link) {
2143 if (prop == TIPC_NLA_PROP_TOL)
2144 tipc_link_set_tolerance(e->link, b->tolerance,
2145 &xmitq);
2146 else if (prop == TIPC_NLA_PROP_MTU)
2147 tipc_link_set_mtu(e->link, b->mtu);
2148 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01002149 tipc_node_write_unlock(n);
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002150 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr, NULL);
Jon Maloy37c64cf2018-02-14 13:34:39 +01002151 }
2152
2153 rcu_read_unlock();
2154}
2155
Richard Alpeb3404022016-08-18 10:33:52 +02002156int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
2157{
2158 struct net *net = sock_net(skb->sk);
2159 struct tipc_net *tn = net_generic(net, tipc_net_id);
2160 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
Hoang Le6708ef72019-11-06 13:26:09 +07002161 struct tipc_node *peer, *temp_node;
Richard Alpeb3404022016-08-18 10:33:52 +02002162 u32 addr;
2163 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02002164
2165 /* We identify the peer by its net */
2166 if (!info->attrs[TIPC_NLA_NET])
2167 return -EINVAL;
2168
Johannes Berg8cb08172019-04-26 14:07:28 +02002169 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
2170 info->attrs[TIPC_NLA_NET],
2171 tipc_nl_net_policy, info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02002172 if (err)
2173 return err;
2174
2175 if (!attrs[TIPC_NLA_NET_ADDR])
2176 return -EINVAL;
2177
2178 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
2179
2180 if (in_own_node(net, addr))
2181 return -ENOTSUPP;
2182
2183 spin_lock_bh(&tn->node_list_lock);
2184 peer = tipc_node_find(net, addr);
2185 if (!peer) {
2186 spin_unlock_bh(&tn->node_list_lock);
2187 return -ENXIO;
2188 }
2189
2190 tipc_node_write_lock(peer);
2191 if (peer->state != SELF_DOWN_PEER_DOWN &&
2192 peer->state != SELF_DOWN_PEER_LEAVING) {
2193 tipc_node_write_unlock(peer);
2194 err = -EBUSY;
2195 goto err_out;
2196 }
2197
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02002198 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02002199 tipc_node_write_unlock(peer);
2200 tipc_node_delete(peer);
2201
Hoang Le6708ef72019-11-06 13:26:09 +07002202 /* Calculate cluster capabilities */
2203 tn->capabilities = TIPC_NODE_CAPABILITIES;
2204 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
2205 tn->capabilities &= temp_node->capabilities;
2206 }
Hoang Leba5f6a82019-11-21 10:01:09 +07002207 tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
Richard Alpeb3404022016-08-18 10:33:52 +02002208 err = 0;
2209err_out:
2210 tipc_node_put(peer);
2211 spin_unlock_bh(&tn->node_list_lock);
2212
2213 return err;
2214}
2215
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002216int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
2217{
2218 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08002219 struct net *net = sock_net(skb->sk);
2220 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002221 int done = cb->args[0];
2222 int last_addr = cb->args[1];
2223 struct tipc_node *node;
2224 struct tipc_nl_msg msg;
2225
2226 if (done)
2227 return 0;
2228
2229 msg.skb = skb;
2230 msg.portid = NETLINK_CB(cb->skb).portid;
2231 msg.seq = cb->nlh->nlmsg_seq;
2232
2233 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08002234 if (last_addr) {
2235 node = tipc_node_find(net, last_addr);
2236 if (!node) {
2237 rcu_read_unlock();
2238 /* We never set seq or call nl_dump_check_consistent()
2239 * this means that setting prev_seq here will cause the
2240 * consistence check to fail in the netlink callback
2241 * handler. Resulting in the NLMSG_DONE message having
2242 * the NLM_F_DUMP_INTR flag set if the node state
2243 * changed while we released the lock.
2244 */
2245 cb->prev_seq = 1;
2246 return -EPIPE;
2247 }
2248 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002249 }
2250
Ying Xuef2f98002015-01-09 15:27:05 +08002251 list_for_each_entry_rcu(node, &tn->node_list, list) {
Tuong Lien4cbf8ac2019-11-08 12:05:09 +07002252 if (node->preliminary)
2253 continue;
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002254 if (last_addr) {
2255 if (node->addr == last_addr)
2256 last_addr = 0;
2257 else
2258 continue;
2259 }
2260
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002261 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002262 err = __tipc_nl_add_node(&msg, node);
2263 if (err) {
2264 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002265 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002266 goto out;
2267 }
2268
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05002269 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01002270 }
2271 done = 1;
2272out:
2273 cb->args[0] = done;
2274 cb->args[1] = last_addr;
2275 rcu_read_unlock();
2276
2277 return skb->len;
2278}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002279
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002280/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002281 * @net: the applicable net namespace
2282 * @name: pointer to link name string
2283 * @bearer_id: pointer to index in 'node->links' array where the link was found.
2284 *
2285 * Returns pointer to node owning the link, or 0 if no matching link is found.
2286 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002287static struct tipc_node *tipc_node_find_by_name(struct net *net,
2288 const char *link_name,
2289 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002290{
2291 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002292 struct tipc_link *l;
2293 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002294 struct tipc_node *found_node = NULL;
2295 int i;
2296
2297 *bearer_id = 0;
2298 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002299 list_for_each_entry_rcu(n, &tn->node_list, list) {
2300 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002301 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002302 l = n->links[i].link;
2303 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002304 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002305 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002306 break;
2307 }
2308 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002309 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002310 if (found_node)
2311 break;
2312 }
2313 rcu_read_unlock();
2314
2315 return found_node;
2316}
2317
2318int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
2319{
2320 int err;
2321 int res = 0;
2322 int bearer_id;
2323 char *name;
2324 struct tipc_link *link;
2325 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01002326 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002327 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2328 struct net *net = sock_net(skb->sk);
2329
Richard Alped01332f2016-02-01 08:19:56 +01002330 __skb_queue_head_init(&xmitq);
2331
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002332 if (!info->attrs[TIPC_NLA_LINK])
2333 return -EINVAL;
2334
Johannes Berg8cb08172019-04-26 14:07:28 +02002335 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2336 info->attrs[TIPC_NLA_LINK],
2337 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002338 if (err)
2339 return err;
2340
2341 if (!attrs[TIPC_NLA_LINK_NAME])
2342 return -EINVAL;
2343
2344 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2345
2346 if (strcmp(name, tipc_bclink_name) == 0)
2347 return tipc_nl_bc_link_set(net, attrs);
2348
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002349 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002350 if (!node)
2351 return -EINVAL;
2352
2353 tipc_node_read_lock(node);
2354
2355 link = node->links[bearer_id].link;
2356 if (!link) {
2357 res = -EINVAL;
2358 goto out;
2359 }
2360
2361 if (attrs[TIPC_NLA_LINK_PROP]) {
2362 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2363
Jon Maloy16ad3f42019-12-10 00:52:46 +01002364 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002365 if (err) {
2366 res = err;
2367 goto out;
2368 }
2369
2370 if (props[TIPC_NLA_PROP_TOL]) {
2371 u32 tol;
2372
2373 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002374 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002375 }
2376 if (props[TIPC_NLA_PROP_PRIO]) {
2377 u32 prio;
2378
2379 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002380 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002381 }
2382 if (props[TIPC_NLA_PROP_WIN]) {
Jon Maloy16ad3f42019-12-10 00:52:46 +01002383 u32 max_win;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002384
Jon Maloy16ad3f42019-12-10 00:52:46 +01002385 max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2386 tipc_link_set_queue_limits(link,
2387 tipc_link_min_win(link),
2388 max_win);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002389 }
2390 }
2391
2392out:
2393 tipc_node_read_unlock(node);
Tuong Lienfc1b6d62019-11-08 12:05:11 +07002394 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr,
2395 NULL);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002396 return res;
2397}
2398
2399int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2400{
2401 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002402 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002403 struct tipc_nl_msg msg;
2404 char *name;
2405 int err;
2406
2407 msg.portid = info->snd_portid;
2408 msg.seq = info->snd_seq;
2409
Ying Xue94f6a802018-05-08 21:44:06 +08002410 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002411 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002412
Johannes Berg8cb08172019-04-26 14:07:28 +02002413 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2414 info->attrs[TIPC_NLA_LINK],
2415 tipc_nl_link_policy, info->extack);
Ying Xue94f6a802018-05-08 21:44:06 +08002416 if (err)
2417 return err;
2418
2419 if (!attrs[TIPC_NLA_LINK_NAME])
2420 return -EINVAL;
2421
2422 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002423
2424 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2425 if (!msg.skb)
2426 return -ENOMEM;
2427
2428 if (strcmp(name, tipc_bclink_name) == 0) {
2429 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002430 if (err)
2431 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002432 } else {
2433 int bearer_id;
2434 struct tipc_node *node;
2435 struct tipc_link *link;
2436
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002437 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002438 if (!node) {
2439 err = -EINVAL;
2440 goto err_free;
2441 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002442
2443 tipc_node_read_lock(node);
2444 link = node->links[bearer_id].link;
2445 if (!link) {
2446 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002447 err = -EINVAL;
2448 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002449 }
2450
2451 err = __tipc_nl_add_link(net, &msg, link, 0);
2452 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002453 if (err)
2454 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002455 }
2456
2457 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002458
2459err_free:
2460 nlmsg_free(msg.skb);
2461 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002462}
2463
2464int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2465{
2466 int err;
2467 char *link_name;
2468 unsigned int bearer_id;
2469 struct tipc_link *link;
2470 struct tipc_node *node;
2471 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2472 struct net *net = sock_net(skb->sk);
2473 struct tipc_link_entry *le;
2474
2475 if (!info->attrs[TIPC_NLA_LINK])
2476 return -EINVAL;
2477
Johannes Berg8cb08172019-04-26 14:07:28 +02002478 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2479 info->attrs[TIPC_NLA_LINK],
2480 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002481 if (err)
2482 return err;
2483
2484 if (!attrs[TIPC_NLA_LINK_NAME])
2485 return -EINVAL;
2486
2487 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2488
2489 if (strcmp(link_name, tipc_bclink_name) == 0) {
2490 err = tipc_bclink_reset_stats(net);
2491 if (err)
2492 return err;
2493 return 0;
2494 }
2495
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002496 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002497 if (!node)
2498 return -EINVAL;
2499
2500 le = &node->links[bearer_id];
2501 tipc_node_read_lock(node);
2502 spin_lock_bh(&le->lock);
2503 link = node->links[bearer_id].link;
2504 if (!link) {
2505 spin_unlock_bh(&le->lock);
2506 tipc_node_read_unlock(node);
2507 return -EINVAL;
2508 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002509 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002510 spin_unlock_bh(&le->lock);
2511 tipc_node_read_unlock(node);
2512 return 0;
2513}
2514
2515/* Caller should hold node lock */
2516static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2517 struct tipc_node *node, u32 *prev_link)
2518{
2519 u32 i;
2520 int err;
2521
2522 for (i = *prev_link; i < MAX_BEARERS; i++) {
2523 *prev_link = i;
2524
2525 if (!node->links[i].link)
2526 continue;
2527
2528 err = __tipc_nl_add_link(net, msg,
2529 node->links[i].link, NLM_F_MULTI);
2530 if (err)
2531 return err;
2532 }
2533 *prev_link = 0;
2534
2535 return 0;
2536}
2537
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002538int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002539{
2540 struct net *net = sock_net(skb->sk);
2541 struct tipc_net *tn = net_generic(net, tipc_net_id);
2542 struct tipc_node *node;
2543 struct tipc_nl_msg msg;
2544 u32 prev_node = cb->args[0];
2545 u32 prev_link = cb->args[1];
2546 int done = cb->args[2];
2547 int err;
2548
2549 if (done)
2550 return 0;
2551
2552 msg.skb = skb;
2553 msg.portid = NETLINK_CB(cb->skb).portid;
2554 msg.seq = cb->nlh->nlmsg_seq;
2555
2556 rcu_read_lock();
2557 if (prev_node) {
2558 node = tipc_node_find(net, prev_node);
2559 if (!node) {
2560 /* We never set seq or call nl_dump_check_consistent()
2561 * this means that setting prev_seq here will cause the
2562 * consistence check to fail in the netlink callback
2563 * handler. Resulting in the last NLMSG_DONE message
2564 * having the NLM_F_DUMP_INTR flag set.
2565 */
2566 cb->prev_seq = 1;
2567 goto out;
2568 }
2569 tipc_node_put(node);
2570
2571 list_for_each_entry_continue_rcu(node, &tn->node_list,
2572 list) {
2573 tipc_node_read_lock(node);
2574 err = __tipc_nl_add_node_links(net, &msg, node,
2575 &prev_link);
2576 tipc_node_read_unlock(node);
2577 if (err)
2578 goto out;
2579
2580 prev_node = node->addr;
2581 }
2582 } else {
2583 err = tipc_nl_add_bc_link(net, &msg);
2584 if (err)
2585 goto out;
2586
2587 list_for_each_entry_rcu(node, &tn->node_list, list) {
2588 tipc_node_read_lock(node);
2589 err = __tipc_nl_add_node_links(net, &msg, node,
2590 &prev_link);
2591 tipc_node_read_unlock(node);
2592 if (err)
2593 goto out;
2594
2595 prev_node = node->addr;
2596 }
2597 }
2598 done = 1;
2599out:
2600 rcu_read_unlock();
2601
2602 cb->args[0] = prev_node;
2603 cb->args[1] = prev_link;
2604 cb->args[2] = done;
2605
2606 return skb->len;
2607}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002608
2609int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2610{
2611 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2612 struct net *net = sock_net(skb->sk);
2613 int err;
2614
2615 if (!info->attrs[TIPC_NLA_MON])
2616 return -EINVAL;
2617
Johannes Berg8cb08172019-04-26 14:07:28 +02002618 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
2619 info->attrs[TIPC_NLA_MON],
2620 tipc_nl_monitor_policy,
2621 info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002622 if (err)
2623 return err;
2624
2625 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2626 u32 val;
2627
2628 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2629 err = tipc_nl_monitor_set_threshold(net, val);
2630 if (err)
2631 return err;
2632 }
2633
2634 return 0;
2635}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002636
2637static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2638{
2639 struct nlattr *attrs;
2640 void *hdr;
2641 u32 val;
2642
2643 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2644 0, TIPC_NL_MON_GET);
2645 if (!hdr)
2646 return -EMSGSIZE;
2647
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002648 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002649 if (!attrs)
2650 goto msg_full;
2651
2652 val = tipc_nl_monitor_get_threshold(net);
2653
2654 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2655 goto attr_msg_full;
2656
2657 nla_nest_end(msg->skb, attrs);
2658 genlmsg_end(msg->skb, hdr);
2659
2660 return 0;
2661
2662attr_msg_full:
2663 nla_nest_cancel(msg->skb, attrs);
2664msg_full:
2665 genlmsg_cancel(msg->skb, hdr);
2666
2667 return -EMSGSIZE;
2668}
2669
2670int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2671{
2672 struct net *net = sock_net(skb->sk);
2673 struct tipc_nl_msg msg;
2674 int err;
2675
2676 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002677 if (!msg.skb)
2678 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002679 msg.portid = info->snd_portid;
2680 msg.seq = info->snd_seq;
2681
2682 err = __tipc_nl_add_monitor_prop(net, &msg);
2683 if (err) {
2684 nlmsg_free(msg.skb);
2685 return err;
2686 }
2687
2688 return genlmsg_reply(msg.skb, info);
2689}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002690
2691int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2692{
2693 struct net *net = sock_net(skb->sk);
2694 u32 prev_bearer = cb->args[0];
2695 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002696 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002697 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002698
2699 if (prev_bearer == MAX_BEARERS)
2700 return 0;
2701
2702 msg.skb = skb;
2703 msg.portid = NETLINK_CB(cb->skb).portid;
2704 msg.seq = cb->nlh->nlmsg_seq;
2705
2706 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002707 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002708 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002709 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002710 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002711 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002712 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002713 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002714
2715 return skb->len;
2716}
2717
2718int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2719 struct netlink_callback *cb)
2720{
2721 struct net *net = sock_net(skb->sk);
2722 u32 prev_node = cb->args[1];
2723 u32 bearer_id = cb->args[2];
2724 int done = cb->args[0];
2725 struct tipc_nl_msg msg;
2726 int err;
2727
2728 if (!prev_node) {
Jiri Pirko057af702019-10-05 20:04:39 +02002729 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002730 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2731
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002732 if (!attrs[TIPC_NLA_MON])
2733 return -EINVAL;
2734
Johannes Berg8cb08172019-04-26 14:07:28 +02002735 err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
2736 attrs[TIPC_NLA_MON],
2737 tipc_nl_monitor_policy,
2738 NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002739 if (err)
2740 return err;
2741
2742 if (!mon[TIPC_NLA_MON_REF])
2743 return -EINVAL;
2744
2745 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2746
2747 if (bearer_id >= MAX_BEARERS)
2748 return -EINVAL;
2749 }
2750
2751 if (done)
2752 return 0;
2753
2754 msg.skb = skb;
2755 msg.portid = NETLINK_CB(cb->skb).portid;
2756 msg.seq = cb->nlh->nlmsg_seq;
2757
2758 rtnl_lock();
2759 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2760 if (!err)
2761 done = 1;
2762
2763 rtnl_unlock();
2764 cb->args[0] = done;
2765 cb->args[1] = prev_node;
2766 cb->args[2] = bearer_id;
2767
2768 return skb->len;
2769}
Tuong Lienb4b97712018-12-19 09:17:56 +07002770
Tuong Liene1f32192019-11-08 12:05:12 +07002771#ifdef CONFIG_TIPC_CRYPTO
2772static int tipc_nl_retrieve_key(struct nlattr **attrs,
2773 struct tipc_aead_key **key)
2774{
2775 struct nlattr *attr = attrs[TIPC_NLA_NODE_KEY];
2776
2777 if (!attr)
2778 return -ENODATA;
2779
2780 *key = (struct tipc_aead_key *)nla_data(attr);
2781 if (nla_len(attr) < tipc_aead_key_size(*key))
2782 return -EINVAL;
2783
2784 return 0;
2785}
2786
2787static int tipc_nl_retrieve_nodeid(struct nlattr **attrs, u8 **node_id)
2788{
2789 struct nlattr *attr = attrs[TIPC_NLA_NODE_ID];
2790
2791 if (!attr)
2792 return -ENODATA;
2793
2794 if (nla_len(attr) < TIPC_NODEID_LEN)
2795 return -EINVAL;
2796
2797 *node_id = (u8 *)nla_data(attr);
2798 return 0;
2799}
2800
Chen Wandun2437fd72020-02-10 16:11:09 +08002801static int __tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
Tuong Liene1f32192019-11-08 12:05:12 +07002802{
2803 struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1];
2804 struct net *net = sock_net(skb->sk);
2805 struct tipc_net *tn = tipc_net(net);
2806 struct tipc_node *n = NULL;
2807 struct tipc_aead_key *ukey;
2808 struct tipc_crypto *c;
2809 u8 *id, *own_id;
2810 int rc = 0;
2811
2812 if (!info->attrs[TIPC_NLA_NODE])
2813 return -EINVAL;
2814
2815 rc = nla_parse_nested(attrs, TIPC_NLA_NODE_MAX,
2816 info->attrs[TIPC_NLA_NODE],
2817 tipc_nl_node_policy, info->extack);
2818 if (rc)
2819 goto exit;
2820
2821 own_id = tipc_own_id(net);
2822 if (!own_id) {
2823 rc = -EPERM;
2824 goto exit;
2825 }
2826
2827 rc = tipc_nl_retrieve_key(attrs, &ukey);
2828 if (rc)
2829 goto exit;
2830
2831 rc = tipc_aead_key_validate(ukey);
2832 if (rc)
2833 goto exit;
2834
2835 rc = tipc_nl_retrieve_nodeid(attrs, &id);
2836 switch (rc) {
2837 case -ENODATA:
2838 /* Cluster key mode */
2839 rc = tipc_crypto_key_init(tn->crypto_tx, ukey, CLUSTER_KEY);
2840 break;
2841 case 0:
2842 /* Per-node key mode */
2843 if (!memcmp(id, own_id, NODE_ID_LEN)) {
2844 c = tn->crypto_tx;
2845 } else {
2846 n = tipc_node_find_by_id(net, id) ?:
2847 tipc_node_create(net, 0, id, 0xffffu, 0, true);
2848 if (unlikely(!n)) {
2849 rc = -ENOMEM;
2850 break;
2851 }
2852 c = n->crypto_rx;
2853 }
2854
2855 rc = tipc_crypto_key_init(c, ukey, PER_NODE_KEY);
2856 if (n)
2857 tipc_node_put(n);
2858 break;
2859 default:
2860 break;
2861 }
2862
2863exit:
2864 return (rc < 0) ? rc : 0;
2865}
2866
2867int tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
2868{
2869 int err;
2870
2871 rtnl_lock();
2872 err = __tipc_nl_node_set_key(skb, info);
2873 rtnl_unlock();
2874
2875 return err;
2876}
2877
Chen Wandun2437fd72020-02-10 16:11:09 +08002878static int __tipc_nl_node_flush_key(struct sk_buff *skb,
2879 struct genl_info *info)
Tuong Liene1f32192019-11-08 12:05:12 +07002880{
2881 struct net *net = sock_net(skb->sk);
2882 struct tipc_net *tn = tipc_net(net);
2883 struct tipc_node *n;
2884
2885 tipc_crypto_key_flush(tn->crypto_tx);
2886 rcu_read_lock();
2887 list_for_each_entry_rcu(n, &tn->node_list, list)
2888 tipc_crypto_key_flush(n->crypto_rx);
2889 rcu_read_unlock();
2890
2891 pr_info("All keys are flushed!\n");
2892 return 0;
2893}
2894
2895int tipc_nl_node_flush_key(struct sk_buff *skb, struct genl_info *info)
2896{
2897 int err;
2898
2899 rtnl_lock();
2900 err = __tipc_nl_node_flush_key(skb, info);
2901 rtnl_unlock();
2902
2903 return err;
2904}
2905#endif
2906
Tuong Lienb4b97712018-12-19 09:17:56 +07002907/**
2908 * tipc_node_dump - dump TIPC node data
2909 * @n: tipc node to be dumped
2910 * @more: dump more?
2911 * - false: dump only tipc node data
2912 * - true: dump node link data as well
2913 * @buf: returned buffer of dump data in format
2914 */
2915int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
2916{
2917 int i = 0;
2918 size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
2919
2920 if (!n) {
2921 i += scnprintf(buf, sz, "node data: (null)\n");
2922 return i;
2923 }
2924
2925 i += scnprintf(buf, sz, "node data: %x", n->addr);
2926 i += scnprintf(buf + i, sz - i, " %x", n->state);
2927 i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
2928 i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
2929 i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
2930 i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
2931 i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
2932 i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
2933 i += scnprintf(buf + i, sz - i, " %u", n->working_links);
2934 i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
2935 i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
2936
2937 if (!more)
2938 return i;
2939
2940 i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
2941 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
2942 i += scnprintf(buf + i, sz - i, " media: ");
2943 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
2944 i += scnprintf(buf + i, sz - i, "\n");
2945 i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
2946 i += scnprintf(buf + i, sz - i, " inputq: ");
2947 i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
2948
2949 i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
2950 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
2951 i += scnprintf(buf + i, sz - i, " media: ");
2952 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
2953 i += scnprintf(buf + i, sz - i, "\n");
2954 i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
2955 i += scnprintf(buf + i, sz - i, " inputq: ");
2956 i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
2957
2958 i += scnprintf(buf + i, sz - i, "bclink:\n ");
2959 i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
2960
2961 return i;
2962}
Hoang Lef73b1282019-10-29 07:51:21 +07002963
2964void tipc_node_pre_cleanup_net(struct net *exit_net)
2965{
2966 struct tipc_node *n;
2967 struct tipc_net *tn;
2968 struct net *tmp;
2969
2970 rcu_read_lock();
2971 for_each_net_rcu(tmp) {
2972 if (tmp == exit_net)
2973 continue;
2974 tn = tipc_net(tmp);
2975 if (!tn)
2976 continue;
2977 spin_lock_bh(&tn->node_list_lock);
2978 list_for_each_entry_rcu(n, &tn->node_list, list) {
2979 if (!n->peer_net)
2980 continue;
2981 if (n->peer_net != exit_net)
2982 continue;
2983 tipc_node_write_lock(n);
2984 n->peer_net = NULL;
2985 n->peer_hash_mix = 0;
2986 tipc_node_write_unlock_fast(n);
2987 break;
2988 }
2989 spin_unlock_bh(&tn->node_list_lock);
2990 }
2991 rcu_read_unlock();
2992}