xref: /linux/net/tipc/node.c (revision cf85f810f911234a06a4ef2439e8694b93b717fc)
1 /*
2  * net/tipc/node.c: TIPC node management routines
3  *
4  * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
5  * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 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.
19  *
20  * 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
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include "core.h"
38 #include "link.h"
39 #include "node.h"
40 #include "name_distr.h"
41 #include "socket.h"
42 #include "bcast.h"
43 #include "monitor.h"
44 #include "discover.h"
45 #include "netlink.h"
46 #include "trace.h"
47 #include "crypto.h"
48 
49 #define INVALID_NODE_SIG	0x10000
50 #define NODE_CLEANUP_AFTER	300000
51 
52 /* 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  */
57 enum {
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 
64 struct 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 
72 struct 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 	u16 named_rcv_nxt;
79 	bool named_open;
80 };
81 
82 /**
83  * struct tipc_node - TIPC node structure
84  * @addr: network address of node
85  * @kref: reference counter to node object
86  * @lock: rwlock governing access to structure
87  * @net: the applicable net namespace
88  * @hash: links to adjacent nodes in unsorted hash chain
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  * @bc_entry: broadcast link entry
92  * @action_flags: bit mask of different types of node actions
93  * @state: connectivity state vs peer node
94  * @preliminary: a preliminary node or not
95  * @failover_sent: failover sent or not
96  * @sync_point: sequence number where synch/failover is finished
97  * @list: links to adjacent nodes in sorted list of cluster's nodes
98  * @working_links: number of working links to node (both active and standby)
99  * @link_cnt: number of links to node
100  * @capabilities: bitmap, indicating peer node's functional capabilities
101  * @signature: node instance identifier
102  * @link_id: local and remote bearer ids of changing link, if any
103  * @peer_id: 128-bit ID of peer
104  * @peer_id_string: ID string of peer
105  * @publ_list: list of publications
106  * @conn_sks: list of connections (FIXME)
107  * @timer: node's keepalive timer
108  * @keepalive_intv: keepalive interval in milliseconds
109  * @rcu: rcu struct for tipc_node
110  * @delete_at: indicates the time for deleting a down node
111  * @peer_net: peer's net namespace
112  * @peer_hash_mix: hash for this peer (FIXME)
113  * @crypto_rx: RX crypto handler
114  */
115 struct tipc_node {
116 	u32 addr;
117 	struct kref kref;
118 	rwlock_t lock;
119 	struct net *net;
120 	struct hlist_node hash;
121 	int active_links[2];
122 	struct tipc_link_entry links[MAX_BEARERS];
123 	struct tipc_bclink_entry bc_entry;
124 	int action_flags;
125 	struct list_head list;
126 	int state;
127 	bool preliminary;
128 	bool failover_sent;
129 	u16 sync_point;
130 	int link_cnt;
131 	u16 working_links;
132 	u16 capabilities;
133 	u32 signature;
134 	u32 link_id;
135 	u8 peer_id[16];
136 	char peer_id_string[NODE_ID_STR_LEN];
137 	struct list_head publ_list;
138 	struct list_head conn_sks;
139 	unsigned long keepalive_intv;
140 	struct timer_list timer;
141 	struct rcu_head rcu;
142 	unsigned long delete_at;
143 	struct net *peer_net;
144 	u32 peer_hash_mix;
145 #ifdef CONFIG_TIPC_CRYPTO
146 	struct tipc_crypto *crypto_rx;
147 #endif
148 };
149 
150 /* Node FSM states and events:
151  */
152 enum {
153 	SELF_DOWN_PEER_DOWN    = 0xdd,
154 	SELF_UP_PEER_UP        = 0xaa,
155 	SELF_DOWN_PEER_LEAVING = 0xd1,
156 	SELF_UP_PEER_COMING    = 0xac,
157 	SELF_COMING_PEER_UP    = 0xca,
158 	SELF_LEAVING_PEER_DOWN = 0x1d,
159 	NODE_FAILINGOVER       = 0xf0,
160 	NODE_SYNCHING          = 0xcc
161 };
162 
163 enum {
164 	SELF_ESTABL_CONTACT_EVT = 0xece,
165 	SELF_LOST_CONTACT_EVT   = 0x1ce,
166 	PEER_ESTABL_CONTACT_EVT = 0x9ece,
167 	PEER_LOST_CONTACT_EVT   = 0x91ce,
168 	NODE_FAILOVER_BEGIN_EVT = 0xfbe,
169 	NODE_FAILOVER_END_EVT   = 0xfee,
170 	NODE_SYNCH_BEGIN_EVT    = 0xcbe,
171 	NODE_SYNCH_END_EVT      = 0xcee
172 };
173 
174 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
175 				  struct sk_buff_head *xmitq,
176 				  struct tipc_media_addr **maddr);
177 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
178 				bool delete);
179 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
180 static void tipc_node_delete(struct tipc_node *node);
181 static void tipc_node_timeout(struct timer_list *t);
182 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
183 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
184 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
185 static bool node_is_up(struct tipc_node *n);
186 static void tipc_node_delete_from_list(struct tipc_node *node);
187 
188 struct tipc_sock_conn {
189 	u32 port;
190 	u32 peer_port;
191 	u32 peer_node;
192 	struct list_head list;
193 };
194 
195 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
196 {
197 	int bearer_id = n->active_links[sel & 1];
198 
199 	if (unlikely(bearer_id == INVALID_BEARER_ID))
200 		return NULL;
201 
202 	return n->links[bearer_id].link;
203 }
204 
205 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
206 {
207 	struct tipc_node *n;
208 	int bearer_id;
209 	unsigned int mtu = MAX_MSG_SIZE;
210 
211 	n = tipc_node_find(net, addr);
212 	if (unlikely(!n))
213 		return mtu;
214 
215 	/* Allow MAX_MSG_SIZE when building connection oriented message
216 	 * if they are in the same core network
217 	 */
218 	if (n->peer_net && connected) {
219 		tipc_node_put(n);
220 		return mtu;
221 	}
222 
223 	bearer_id = n->active_links[sel & 1];
224 	if (likely(bearer_id != INVALID_BEARER_ID))
225 		mtu = n->links[bearer_id].mtu;
226 	tipc_node_put(n);
227 	return mtu;
228 }
229 
230 bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
231 {
232 	u8 *own_id = tipc_own_id(net);
233 	struct tipc_node *n;
234 
235 	if (!own_id)
236 		return true;
237 
238 	if (addr == tipc_own_addr(net)) {
239 		memcpy(id, own_id, TIPC_NODEID_LEN);
240 		return true;
241 	}
242 	n = tipc_node_find(net, addr);
243 	if (!n)
244 		return false;
245 
246 	memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
247 	tipc_node_put(n);
248 	return true;
249 }
250 
251 u16 tipc_node_get_capabilities(struct net *net, u32 addr)
252 {
253 	struct tipc_node *n;
254 	u16 caps;
255 
256 	n = tipc_node_find(net, addr);
257 	if (unlikely(!n))
258 		return TIPC_NODE_CAPABILITIES;
259 	caps = n->capabilities;
260 	tipc_node_put(n);
261 	return caps;
262 }
263 
264 u32 tipc_node_get_addr(struct tipc_node *node)
265 {
266 	return (node) ? node->addr : 0;
267 }
268 
269 char *tipc_node_get_id_str(struct tipc_node *node)
270 {
271 	return node->peer_id_string;
272 }
273 
274 #ifdef CONFIG_TIPC_CRYPTO
275 /**
276  * tipc_node_crypto_rx - Retrieve crypto RX handle from node
277  * @__n: target tipc_node
278  * Note: node ref counter must be held first!
279  */
280 struct tipc_crypto *tipc_node_crypto_rx(struct tipc_node *__n)
281 {
282 	return (__n) ? __n->crypto_rx : NULL;
283 }
284 
285 struct tipc_crypto *tipc_node_crypto_rx_by_list(struct list_head *pos)
286 {
287 	return container_of(pos, struct tipc_node, list)->crypto_rx;
288 }
289 
290 struct tipc_crypto *tipc_node_crypto_rx_by_addr(struct net *net, u32 addr)
291 {
292 	struct tipc_node *n;
293 
294 	n = tipc_node_find(net, addr);
295 	return (n) ? n->crypto_rx : NULL;
296 }
297 #endif
298 
299 static void tipc_node_free(struct rcu_head *rp)
300 {
301 	struct tipc_node *n = container_of(rp, struct tipc_node, rcu);
302 
303 #ifdef CONFIG_TIPC_CRYPTO
304 	tipc_crypto_stop(&n->crypto_rx);
305 #endif
306 	kfree(n);
307 }
308 
309 static void tipc_node_kref_release(struct kref *kref)
310 {
311 	struct tipc_node *n = container_of(kref, struct tipc_node, kref);
312 
313 	kfree(n->bc_entry.link);
314 	call_rcu(&n->rcu, tipc_node_free);
315 }
316 
317 void tipc_node_put(struct tipc_node *node)
318 {
319 	kref_put(&node->kref, tipc_node_kref_release);
320 }
321 
322 void tipc_node_get(struct tipc_node *node)
323 {
324 	kref_get(&node->kref);
325 }
326 
327 /*
328  * tipc_node_find - locate specified node object, if it exists
329  */
330 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
331 {
332 	struct tipc_net *tn = tipc_net(net);
333 	struct tipc_node *node;
334 	unsigned int thash = tipc_hashfn(addr);
335 
336 	rcu_read_lock();
337 	hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
338 		if (node->addr != addr || node->preliminary)
339 			continue;
340 		if (!kref_get_unless_zero(&node->kref))
341 			node = NULL;
342 		break;
343 	}
344 	rcu_read_unlock();
345 	return node;
346 }
347 
348 /* tipc_node_find_by_id - locate specified node object by its 128-bit id
349  * Note: this function is called only when a discovery request failed
350  * to find the node by its 32-bit id, and is not time critical
351  */
352 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
353 {
354 	struct tipc_net *tn = tipc_net(net);
355 	struct tipc_node *n;
356 	bool found = false;
357 
358 	rcu_read_lock();
359 	list_for_each_entry_rcu(n, &tn->node_list, list) {
360 		read_lock_bh(&n->lock);
361 		if (!memcmp(id, n->peer_id, 16) &&
362 		    kref_get_unless_zero(&n->kref))
363 			found = true;
364 		read_unlock_bh(&n->lock);
365 		if (found)
366 			break;
367 	}
368 	rcu_read_unlock();
369 	return found ? n : NULL;
370 }
371 
372 static void tipc_node_read_lock(struct tipc_node *n)
373 	__acquires(n->lock)
374 {
375 	read_lock_bh(&n->lock);
376 }
377 
378 static void tipc_node_read_unlock(struct tipc_node *n)
379 	__releases(n->lock)
380 {
381 	read_unlock_bh(&n->lock);
382 }
383 
384 static void tipc_node_write_lock(struct tipc_node *n)
385 	__acquires(n->lock)
386 {
387 	write_lock_bh(&n->lock);
388 }
389 
390 static void tipc_node_write_unlock_fast(struct tipc_node *n)
391 	__releases(n->lock)
392 {
393 	write_unlock_bh(&n->lock);
394 }
395 
396 static void tipc_node_write_unlock(struct tipc_node *n)
397 	__releases(n->lock)
398 {
399 	struct tipc_socket_addr sk;
400 	struct net *net = n->net;
401 	u32 flags = n->action_flags;
402 	struct list_head *publ_list;
403 	struct tipc_uaddr ua;
404 	u32 bearer_id, node;
405 
406 	if (likely(!flags)) {
407 		write_unlock_bh(&n->lock);
408 		return;
409 	}
410 
411 	tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
412 		   TIPC_LINK_STATE, n->addr, n->addr);
413 	sk.ref = n->link_id;
414 	sk.node = tipc_own_addr(net);
415 	node = n->addr;
416 	bearer_id = n->link_id & 0xffff;
417 	publ_list = &n->publ_list;
418 
419 	n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
420 			     TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
421 
422 	write_unlock_bh(&n->lock);
423 
424 	if (flags & TIPC_NOTIFY_NODE_DOWN)
425 		tipc_publ_notify(net, publ_list, node, n->capabilities);
426 
427 	if (flags & TIPC_NOTIFY_NODE_UP)
428 		tipc_named_node_up(net, node, n->capabilities);
429 
430 	if (flags & TIPC_NOTIFY_LINK_UP) {
431 		tipc_mon_peer_up(net, node, bearer_id);
432 		tipc_nametbl_publish(net, &ua, &sk, sk.ref);
433 	}
434 	if (flags & TIPC_NOTIFY_LINK_DOWN) {
435 		tipc_mon_peer_down(net, node, bearer_id);
436 		tipc_nametbl_withdraw(net, &ua, &sk, sk.ref);
437 	}
438 }
439 
440 static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
441 {
442 	int net_id = tipc_netid(n->net);
443 	struct tipc_net *tn_peer;
444 	struct net *tmp;
445 	u32 hash_chk;
446 
447 	if (n->peer_net)
448 		return;
449 
450 	for_each_net_rcu(tmp) {
451 		tn_peer = tipc_net(tmp);
452 		if (!tn_peer)
453 			continue;
454 		/* Integrity checking whether node exists in namespace or not */
455 		if (tn_peer->net_id != net_id)
456 			continue;
457 		if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
458 			continue;
459 		hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
460 		if (hash_mixes ^ hash_chk)
461 			continue;
462 		n->peer_net = tmp;
463 		n->peer_hash_mix = hash_mixes;
464 		break;
465 	}
466 }
467 
468 struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
469 				   u16 capabilities, u32 hash_mixes,
470 				   bool preliminary)
471 {
472 	struct tipc_net *tn = net_generic(net, tipc_net_id);
473 	struct tipc_link *l, *snd_l = tipc_bc_sndlink(net);
474 	struct tipc_node *n, *temp_node;
475 	unsigned long intv;
476 	int bearer_id;
477 	int i;
478 
479 	spin_lock_bh(&tn->node_list_lock);
480 	n = tipc_node_find(net, addr) ?:
481 		tipc_node_find_by_id(net, peer_id);
482 	if (n) {
483 		if (!n->preliminary)
484 			goto update;
485 		if (preliminary)
486 			goto exit;
487 		/* A preliminary node becomes "real" now, refresh its data */
488 		tipc_node_write_lock(n);
489 		if (!tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
490 					 tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
491 					 n->capabilities, &n->bc_entry.inputq1,
492 					 &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
493 			pr_warn("Broadcast rcv link refresh failed, no memory\n");
494 			tipc_node_write_unlock_fast(n);
495 			tipc_node_put(n);
496 			n = NULL;
497 			goto exit;
498 		}
499 		n->preliminary = false;
500 		n->addr = addr;
501 		hlist_del_rcu(&n->hash);
502 		hlist_add_head_rcu(&n->hash,
503 				   &tn->node_htable[tipc_hashfn(addr)]);
504 		list_del_rcu(&n->list);
505 		list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
506 			if (n->addr < temp_node->addr)
507 				break;
508 		}
509 		list_add_tail_rcu(&n->list, &temp_node->list);
510 		tipc_node_write_unlock_fast(n);
511 
512 update:
513 		if (n->peer_hash_mix ^ hash_mixes)
514 			tipc_node_assign_peer_net(n, hash_mixes);
515 		if (n->capabilities == capabilities)
516 			goto exit;
517 		/* Same node may come back with new capabilities */
518 		tipc_node_write_lock(n);
519 		n->capabilities = capabilities;
520 		for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
521 			l = n->links[bearer_id].link;
522 			if (l)
523 				tipc_link_update_caps(l, capabilities);
524 		}
525 		tipc_node_write_unlock_fast(n);
526 
527 		/* Calculate cluster capabilities */
528 		tn->capabilities = TIPC_NODE_CAPABILITIES;
529 		list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
530 			tn->capabilities &= temp_node->capabilities;
531 		}
532 
533 		tipc_bcast_toggle_rcast(net,
534 					(tn->capabilities & TIPC_BCAST_RCAST));
535 
536 		goto exit;
537 	}
538 	n = kzalloc_obj(*n, GFP_ATOMIC);
539 	if (!n) {
540 		pr_warn("Node creation failed, no memory\n");
541 		goto exit;
542 	}
543 	tipc_nodeid2string(n->peer_id_string, peer_id);
544 #ifdef CONFIG_TIPC_CRYPTO
545 	if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
546 		pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
547 		kfree(n);
548 		n = NULL;
549 		goto exit;
550 	}
551 #endif
552 	n->addr = addr;
553 	n->preliminary = preliminary;
554 	memcpy(&n->peer_id, peer_id, 16);
555 	n->net = net;
556 	n->peer_net = NULL;
557 	n->peer_hash_mix = 0;
558 	/* Assign kernel local namespace if exists */
559 	tipc_node_assign_peer_net(n, hash_mixes);
560 	n->capabilities = capabilities;
561 	kref_init(&n->kref);
562 	rwlock_init(&n->lock);
563 	INIT_HLIST_NODE(&n->hash);
564 	INIT_LIST_HEAD(&n->list);
565 	INIT_LIST_HEAD(&n->publ_list);
566 	INIT_LIST_HEAD(&n->conn_sks);
567 	skb_queue_head_init(&n->bc_entry.namedq);
568 	skb_queue_head_init(&n->bc_entry.inputq1);
569 	__skb_queue_head_init(&n->bc_entry.arrvq);
570 	skb_queue_head_init(&n->bc_entry.inputq2);
571 	for (i = 0; i < MAX_BEARERS; i++)
572 		spin_lock_init(&n->links[i].lock);
573 	n->state = SELF_DOWN_PEER_LEAVING;
574 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
575 	n->signature = INVALID_NODE_SIG;
576 	n->active_links[0] = INVALID_BEARER_ID;
577 	n->active_links[1] = INVALID_BEARER_ID;
578 	if (!preliminary &&
579 	    !tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
580 				 tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
581 				 n->capabilities, &n->bc_entry.inputq1,
582 				 &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
583 		pr_warn("Broadcast rcv link creation failed, no memory\n");
584 		tipc_node_put(n);
585 		n = NULL;
586 		goto exit;
587 	}
588 	tipc_node_get(n);
589 	timer_setup(&n->timer, tipc_node_timeout, 0);
590 	/* Start a slow timer anyway, crypto needs it */
591 	n->keepalive_intv = 10000;
592 	intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
593 	if (!mod_timer(&n->timer, intv))
594 		tipc_node_get(n);
595 	hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
596 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
597 		if (n->addr < temp_node->addr)
598 			break;
599 	}
600 	list_add_tail_rcu(&n->list, &temp_node->list);
601 	/* Calculate cluster capabilities */
602 	tn->capabilities = TIPC_NODE_CAPABILITIES;
603 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
604 		tn->capabilities &= temp_node->capabilities;
605 	}
606 	tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
607 	trace_tipc_node_create(n, true, " ");
608 exit:
609 	spin_unlock_bh(&tn->node_list_lock);
610 	return n;
611 }
612 
613 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
614 {
615 	unsigned long tol = tipc_link_tolerance(l);
616 	unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
617 
618 	/* Link with lowest tolerance determines timer interval */
619 	if (intv < n->keepalive_intv)
620 		n->keepalive_intv = intv;
621 
622 	/* Ensure link's abort limit corresponds to current tolerance */
623 	tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
624 }
625 
626 static void tipc_node_delete_from_list(struct tipc_node *node)
627 {
628 #ifdef CONFIG_TIPC_CRYPTO
629 	tipc_crypto_key_flush(node->crypto_rx);
630 #endif
631 	list_del_rcu(&node->list);
632 	hlist_del_rcu(&node->hash);
633 	tipc_node_put(node);
634 }
635 
636 static void tipc_node_delete(struct tipc_node *node)
637 {
638 	trace_tipc_node_delete(node, true, " ");
639 	tipc_node_delete_from_list(node);
640 
641 	timer_delete_sync(&node->timer);
642 	tipc_node_put(node);
643 }
644 
645 void tipc_node_stop(struct net *net)
646 {
647 	struct tipc_net *tn = tipc_net(net);
648 	struct tipc_node *node, *t_node;
649 
650 	spin_lock_bh(&tn->node_list_lock);
651 	list_for_each_entry_safe(node, t_node, &tn->node_list, list)
652 		tipc_node_delete(node);
653 	spin_unlock_bh(&tn->node_list_lock);
654 }
655 
656 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
657 {
658 	struct tipc_node *n;
659 
660 	if (in_own_node(net, addr))
661 		return;
662 
663 	n = tipc_node_find(net, addr);
664 	if (!n) {
665 		pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
666 		return;
667 	}
668 	tipc_node_write_lock(n);
669 	list_add_tail(subscr, &n->publ_list);
670 	tipc_node_write_unlock_fast(n);
671 	tipc_node_put(n);
672 }
673 
674 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
675 {
676 	struct tipc_node *n;
677 
678 	if (in_own_node(net, addr))
679 		return;
680 
681 	n = tipc_node_find(net, addr);
682 	if (!n) {
683 		pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
684 		return;
685 	}
686 	tipc_node_write_lock(n);
687 	list_del_init(subscr);
688 	tipc_node_write_unlock_fast(n);
689 	tipc_node_put(n);
690 }
691 
692 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
693 {
694 	struct tipc_node *node;
695 	struct tipc_sock_conn *conn;
696 	int err = 0;
697 
698 	if (in_own_node(net, dnode))
699 		return 0;
700 
701 	node = tipc_node_find(net, dnode);
702 	if (!node) {
703 		pr_warn("Connecting sock to node 0x%x failed\n", dnode);
704 		return -EHOSTUNREACH;
705 	}
706 	conn = kmalloc_obj(*conn, GFP_ATOMIC);
707 	if (!conn) {
708 		err = -EHOSTUNREACH;
709 		goto exit;
710 	}
711 	conn->peer_node = dnode;
712 	conn->port = port;
713 	conn->peer_port = peer_port;
714 
715 	tipc_node_write_lock(node);
716 	list_add_tail(&conn->list, &node->conn_sks);
717 	tipc_node_write_unlock(node);
718 exit:
719 	tipc_node_put(node);
720 	return err;
721 }
722 
723 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
724 {
725 	struct tipc_node *node;
726 	struct tipc_sock_conn *conn, *safe;
727 
728 	if (in_own_node(net, dnode))
729 		return;
730 
731 	node = tipc_node_find(net, dnode);
732 	if (!node)
733 		return;
734 
735 	tipc_node_write_lock(node);
736 	list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
737 		if (port != conn->port)
738 			continue;
739 		list_del(&conn->list);
740 		kfree(conn);
741 	}
742 	tipc_node_write_unlock(node);
743 	tipc_node_put(node);
744 }
745 
746 static void  tipc_node_clear_links(struct tipc_node *node)
747 {
748 	int i;
749 
750 	for (i = 0; i < MAX_BEARERS; i++) {
751 		struct tipc_link_entry *le = &node->links[i];
752 
753 		if (le->link) {
754 			kfree(le->link);
755 			le->link = NULL;
756 			node->link_cnt--;
757 		}
758 	}
759 }
760 
761 /* tipc_node_cleanup - delete nodes that does not
762  * have active links for NODE_CLEANUP_AFTER time
763  */
764 static bool tipc_node_cleanup(struct tipc_node *peer)
765 {
766 	struct tipc_node *temp_node;
767 	struct tipc_net *tn = tipc_net(peer->net);
768 	bool deleted = false;
769 
770 	/* If lock held by tipc_node_stop() the node will be deleted anyway */
771 	if (!spin_trylock_bh(&tn->node_list_lock))
772 		return false;
773 
774 	tipc_node_write_lock(peer);
775 
776 	if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
777 		tipc_node_clear_links(peer);
778 		tipc_node_delete_from_list(peer);
779 		deleted = true;
780 	}
781 	tipc_node_write_unlock(peer);
782 
783 	if (!deleted) {
784 		spin_unlock_bh(&tn->node_list_lock);
785 		return deleted;
786 	}
787 
788 	/* Calculate cluster capabilities */
789 	tn->capabilities = TIPC_NODE_CAPABILITIES;
790 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
791 		tn->capabilities &= temp_node->capabilities;
792 	}
793 	tipc_bcast_toggle_rcast(peer->net,
794 				(tn->capabilities & TIPC_BCAST_RCAST));
795 	spin_unlock_bh(&tn->node_list_lock);
796 	return deleted;
797 }
798 
799 /* tipc_node_timeout - handle expiration of node timer
800  */
801 static void tipc_node_timeout(struct timer_list *t)
802 {
803 	struct tipc_node *n = timer_container_of(n, t, timer);
804 	struct tipc_link_entry *le;
805 	struct sk_buff_head xmitq;
806 	int remains = n->link_cnt;
807 	int bearer_id;
808 	int rc = 0;
809 
810 	trace_tipc_node_timeout(n, false, " ");
811 	if (!node_is_up(n) && tipc_node_cleanup(n)) {
812 		/*Removing the reference of Timer*/
813 		tipc_node_put(n);
814 		return;
815 	}
816 
817 #ifdef CONFIG_TIPC_CRYPTO
818 	/* Take any crypto key related actions first */
819 	tipc_crypto_timeout(n->crypto_rx);
820 #endif
821 	__skb_queue_head_init(&xmitq);
822 
823 	/* Initial node interval to value larger (10 seconds), then it will be
824 	 * recalculated with link lowest tolerance
825 	 */
826 	tipc_node_read_lock(n);
827 	n->keepalive_intv = 10000;
828 	tipc_node_read_unlock(n);
829 	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
830 		tipc_node_read_lock(n);
831 		le = &n->links[bearer_id];
832 		if (le->link) {
833 			spin_lock_bh(&le->lock);
834 			/* Link tolerance may change asynchronously: */
835 			tipc_node_calculate_timer(n, le->link);
836 			rc = tipc_link_timeout(le->link, &xmitq);
837 			spin_unlock_bh(&le->lock);
838 			remains--;
839 		}
840 		tipc_node_read_unlock(n);
841 		tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr, n);
842 		if (rc & TIPC_LINK_DOWN_EVT)
843 			tipc_node_link_down(n, bearer_id, false);
844 	}
845 	mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
846 }
847 
848 /**
849  * __tipc_node_link_up - handle addition of link
850  * @n: target tipc_node
851  * @bearer_id: id of the bearer
852  * @xmitq: queue for messages to be xmited on
853  * Node lock must be held by caller
854  * Link becomes active (alone or shared) or standby, depending on its priority.
855  */
856 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
857 				struct sk_buff_head *xmitq)
858 {
859 	int *slot0 = &n->active_links[0];
860 	int *slot1 = &n->active_links[1];
861 	struct tipc_link *ol = node_active_link(n, 0);
862 	struct tipc_link *nl = n->links[bearer_id].link;
863 
864 	if (!nl || tipc_link_is_up(nl))
865 		return;
866 
867 	tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
868 	if (!tipc_link_is_up(nl))
869 		return;
870 
871 	n->working_links++;
872 	n->action_flags |= TIPC_NOTIFY_LINK_UP;
873 	n->link_id = tipc_link_id(nl);
874 
875 	/* Leave room for tunnel header when returning 'mtu' to users: */
876 	n->links[bearer_id].mtu = tipc_link_mss(nl);
877 
878 	tipc_bearer_add_dest(n->net, bearer_id, n->addr);
879 	tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
880 
881 	pr_debug("Established link <%s> on network plane %c\n",
882 		 tipc_link_name(nl), tipc_link_plane(nl));
883 	trace_tipc_node_link_up(n, true, " ");
884 
885 	/* Ensure that a STATE message goes first */
886 	tipc_link_build_state_msg(nl, xmitq);
887 
888 	/* First link? => give it both slots */
889 	if (!ol) {
890 		*slot0 = bearer_id;
891 		*slot1 = bearer_id;
892 		tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
893 		n->action_flags |= TIPC_NOTIFY_NODE_UP;
894 		tipc_link_set_active(nl, true);
895 		tipc_bcast_add_peer(n->net, nl, xmitq);
896 		return;
897 	}
898 
899 	/* Second link => redistribute slots */
900 	if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
901 		pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
902 		*slot0 = bearer_id;
903 		*slot1 = bearer_id;
904 		tipc_link_set_active(nl, true);
905 		tipc_link_set_active(ol, false);
906 	} else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
907 		tipc_link_set_active(nl, true);
908 		*slot1 = bearer_id;
909 	} else {
910 		pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
911 	}
912 
913 	/* Prepare synchronization with first link */
914 	tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
915 }
916 
917 /**
918  * tipc_node_link_up - handle addition of link
919  * @n: target tipc_node
920  * @bearer_id: id of the bearer
921  * @xmitq: queue for messages to be xmited on
922  *
923  * Link becomes active (alone or shared) or standby, depending on its priority.
924  */
925 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
926 			      struct sk_buff_head *xmitq)
927 {
928 	struct tipc_media_addr *maddr;
929 
930 	tipc_node_write_lock(n);
931 	__tipc_node_link_up(n, bearer_id, xmitq);
932 	maddr = &n->links[bearer_id].maddr;
933 	tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr, n);
934 	tipc_node_write_unlock(n);
935 }
936 
937 /**
938  * tipc_node_link_failover() - start failover in case "half-failover"
939  *
940  * This function is only called in a very special situation where link
941  * failover can be already started on peer node but not on this node.
942  * This can happen when e.g.::
943  *
944  *	1. Both links <1A-2A>, <1B-2B> down
945  *	2. Link endpoint 2A up, but 1A still down (e.g. due to network
946  *	disturbance, wrong session, etc.)
947  *	3. Link <1B-2B> up
948  *	4. Link endpoint 2A down (e.g. due to link tolerance timeout)
949  *	5. Node 2 starts failover onto link <1B-2B>
950  *
951  *	==> Node 1 does never start link/node failover!
952  *
953  * @n: tipc node structure
954  * @l: link peer endpoint failingover (- can be NULL)
955  * @tnl: tunnel link
956  * @xmitq: queue for messages to be xmited on tnl link later
957  */
958 static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
959 				    struct tipc_link *tnl,
960 				    struct sk_buff_head *xmitq)
961 {
962 	/* Avoid to be "self-failover" that can never end */
963 	if (!tipc_link_is_up(tnl))
964 		return;
965 
966 	/* Don't rush, failure link may be in the process of resetting */
967 	if (l && !tipc_link_is_reset(l))
968 		return;
969 
970 	tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
971 	tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
972 
973 	n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
974 	tipc_link_failover_prepare(l, tnl, xmitq);
975 
976 	if (l)
977 		tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
978 	tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
979 }
980 
981 /**
982  * __tipc_node_link_down - handle loss of link
983  * @n: target tipc_node
984  * @bearer_id: id of the bearer
985  * @xmitq: queue for messages to be xmited on
986  * @maddr: output media address of the bearer
987  */
988 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
989 				  struct sk_buff_head *xmitq,
990 				  struct tipc_media_addr **maddr)
991 {
992 	struct tipc_link_entry *le = &n->links[*bearer_id];
993 	int *slot0 = &n->active_links[0];
994 	int *slot1 = &n->active_links[1];
995 	int i, highest = 0, prio;
996 	struct tipc_link *l, *_l, *tnl;
997 
998 	l = n->links[*bearer_id].link;
999 	if (!l || tipc_link_is_reset(l))
1000 		return;
1001 
1002 	n->working_links--;
1003 	n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
1004 	n->link_id = tipc_link_id(l);
1005 
1006 	tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
1007 
1008 	pr_debug("Lost link <%s> on network plane %c\n",
1009 		 tipc_link_name(l), tipc_link_plane(l));
1010 
1011 	/* Select new active link if any available */
1012 	*slot0 = INVALID_BEARER_ID;
1013 	*slot1 = INVALID_BEARER_ID;
1014 	for (i = 0; i < MAX_BEARERS; i++) {
1015 		_l = n->links[i].link;
1016 		if (!_l || !tipc_link_is_up(_l))
1017 			continue;
1018 		if (_l == l)
1019 			continue;
1020 		prio = tipc_link_prio(_l);
1021 		if (prio < highest)
1022 			continue;
1023 		if (prio > highest) {
1024 			highest = prio;
1025 			*slot0 = i;
1026 			*slot1 = i;
1027 			continue;
1028 		}
1029 		*slot1 = i;
1030 	}
1031 
1032 	if (!node_is_up(n)) {
1033 		if (tipc_link_peer_is_down(l))
1034 			tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1035 		tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
1036 		trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
1037 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
1038 		tipc_link_reset(l);
1039 		tipc_link_build_reset_msg(l, xmitq);
1040 		*maddr = &n->links[*bearer_id].maddr;
1041 		node_lost_contact(n, &le->inputq);
1042 		tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
1043 		return;
1044 	}
1045 	tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
1046 
1047 	/* There is still a working link => initiate failover */
1048 	*bearer_id = n->active_links[0];
1049 	tnl = n->links[*bearer_id].link;
1050 	tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1051 	tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1052 	n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
1053 	tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
1054 	trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
1055 	tipc_link_reset(l);
1056 	tipc_link_fsm_evt(l, LINK_RESET_EVT);
1057 	tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1058 	tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
1059 	*maddr = &n->links[*bearer_id].maddr;
1060 }
1061 
1062 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
1063 {
1064 	struct tipc_media_addr *maddr = NULL;
1065 	int old_bearer_id = bearer_id;
1066 	struct tipc_link_entry *le;
1067 	struct sk_buff_head xmitq;
1068 	struct tipc_link *l;
1069 
1070 	__skb_queue_head_init(&xmitq);
1071 
1072 	/* Synchronize the link lookup with bearer teardown. */
1073 	tipc_node_write_lock(n);
1074 	le = &n->links[bearer_id];
1075 	l = le->link;
1076 	if (!l) {
1077 		tipc_node_write_unlock_fast(n);
1078 		return;
1079 	}
1080 
1081 	if (!tipc_link_is_establishing(l)) {
1082 		__tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
1083 	} else {
1084 		/* Defuse pending tipc_node_link_up() */
1085 		tipc_link_reset(l);
1086 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
1087 	}
1088 	if (delete) {
1089 		kfree(l);
1090 		le->link = NULL;
1091 		n->link_cnt--;
1092 	}
1093 	trace_tipc_node_link_down(n, true, "node link down or deleted!");
1094 	tipc_node_write_unlock(n);
1095 	if (delete)
1096 		tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
1097 	if (!skb_queue_empty(&xmitq))
1098 		tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr, n);
1099 	tipc_sk_rcv(n->net, &le->inputq);
1100 }
1101 
1102 static bool node_is_up(struct tipc_node *n)
1103 {
1104 	return n->active_links[0] != INVALID_BEARER_ID;
1105 }
1106 
1107 bool tipc_node_is_up(struct net *net, u32 addr)
1108 {
1109 	struct tipc_node *n;
1110 	bool retval = false;
1111 
1112 	if (in_own_node(net, addr))
1113 		return true;
1114 
1115 	n = tipc_node_find(net, addr);
1116 	if (!n)
1117 		return false;
1118 	retval = node_is_up(n);
1119 	tipc_node_put(n);
1120 	return retval;
1121 }
1122 
1123 static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
1124 {
1125 	struct tipc_node *n;
1126 
1127 	addr ^= tipc_net(net)->random;
1128 	while ((n = tipc_node_find(net, addr))) {
1129 		tipc_node_put(n);
1130 		addr++;
1131 	}
1132 	return addr;
1133 }
1134 
1135 /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
1136  * Returns suggested address if any, otherwise 0
1137  */
1138 u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
1139 {
1140 	struct tipc_net *tn = tipc_net(net);
1141 	struct tipc_node *n;
1142 	bool preliminary;
1143 	u32 sugg_addr;
1144 
1145 	/* Suggest new address if some other peer is using this one */
1146 	n = tipc_node_find(net, addr);
1147 	if (n) {
1148 		if (!memcmp(n->peer_id, id, NODE_ID_LEN))
1149 			addr = 0;
1150 		tipc_node_put(n);
1151 		if (!addr)
1152 			return 0;
1153 		return tipc_node_suggest_addr(net, addr);
1154 	}
1155 
1156 	/* Suggest previously used address if peer is known */
1157 	n = tipc_node_find_by_id(net, id);
1158 	if (n) {
1159 		sugg_addr = n->addr;
1160 		preliminary = n->preliminary;
1161 		tipc_node_put(n);
1162 		if (!preliminary)
1163 			return sugg_addr;
1164 	}
1165 
1166 	/* Even this node may be in conflict */
1167 	if (tn->trial_addr == addr)
1168 		return tipc_node_suggest_addr(net, addr);
1169 
1170 	return 0;
1171 }
1172 
1173 void tipc_node_check_dest(struct net *net, u32 addr,
1174 			  u8 *peer_id, struct tipc_bearer *b,
1175 			  u16 capabilities, u32 signature, u32 hash_mixes,
1176 			  struct tipc_media_addr *maddr,
1177 			  bool *respond, bool *dupl_addr)
1178 {
1179 	struct tipc_node *n;
1180 	struct tipc_link *l;
1181 	struct tipc_link_entry *le;
1182 	bool addr_match = false;
1183 	bool sign_match = false;
1184 	bool link_up = false;
1185 	bool link_is_reset = false;
1186 	bool accept_addr = false;
1187 	bool reset = false;
1188 	char *if_name;
1189 	unsigned long intv;
1190 	u16 session;
1191 
1192 	*dupl_addr = false;
1193 	*respond = false;
1194 
1195 	n = tipc_node_create(net, addr, peer_id, capabilities, hash_mixes,
1196 			     false);
1197 	if (!n)
1198 		return;
1199 
1200 	tipc_node_write_lock(n);
1201 
1202 	le = &n->links[b->identity];
1203 
1204 	/* Prepare to validate requesting node's signature and media address */
1205 	l = le->link;
1206 	link_up = l && tipc_link_is_up(l);
1207 	link_is_reset = l && tipc_link_is_reset(l);
1208 	addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
1209 	sign_match = (signature == n->signature);
1210 
1211 	/* These three flags give us eight permutations: */
1212 
1213 	if (sign_match && addr_match && link_up) {
1214 		/* All is fine. Ignore requests. */
1215 		/* Peer node is not a container/local namespace */
1216 		if (!n->peer_hash_mix)
1217 			n->peer_hash_mix = hash_mixes;
1218 	} else if (sign_match && addr_match && !link_up) {
1219 		/* Respond. The link will come up in due time */
1220 		*respond = true;
1221 	} else if (sign_match && !addr_match && link_up) {
1222 		/* Peer has changed i/f address without rebooting.
1223 		 * If so, the link will reset soon, and the next
1224 		 * discovery will be accepted. So we can ignore it.
1225 		 * It may also be a cloned or malicious peer having
1226 		 * chosen the same node address and signature as an
1227 		 * existing one.
1228 		 * Ignore requests until the link goes down, if ever.
1229 		 */
1230 		*dupl_addr = true;
1231 	} else if (sign_match && !addr_match && !link_up) {
1232 		/* Peer link has changed i/f address without rebooting.
1233 		 * It may also be a cloned or malicious peer; we can't
1234 		 * distinguish between the two.
1235 		 * The signature is correct, so we must accept.
1236 		 */
1237 		accept_addr = true;
1238 		*respond = true;
1239 		reset = true;
1240 	} else if (!sign_match && addr_match && link_up) {
1241 		/* Peer node rebooted. Two possibilities:
1242 		 *  - Delayed re-discovery; this link endpoint has already
1243 		 *    reset and re-established contact with the peer, before
1244 		 *    receiving a discovery message from that node.
1245 		 *    (The peer happened to receive one from this node first).
1246 		 *  - The peer came back so fast that our side has not
1247 		 *    discovered it yet. Probing from this side will soon
1248 		 *    reset the link, since there can be no working link
1249 		 *    endpoint at the peer end, and the link will re-establish.
1250 		 *  Accept the signature, since it comes from a known peer.
1251 		 */
1252 		n->signature = signature;
1253 	} else if (!sign_match && addr_match && !link_up) {
1254 		/*  The peer node has rebooted.
1255 		 *  Accept signature, since it is a known peer.
1256 		 */
1257 		n->signature = signature;
1258 		*respond = true;
1259 	} else if (!sign_match && !addr_match && link_up) {
1260 		/* Peer rebooted with new address, or a new/duplicate peer.
1261 		 * Ignore until the link goes down, if ever.
1262 		 */
1263 		*dupl_addr = true;
1264 	} else if (!sign_match && !addr_match && !link_up) {
1265 		/* Peer rebooted with new address, or it is a new peer.
1266 		 * Accept signature and address.
1267 		 */
1268 		n->signature = signature;
1269 		accept_addr = true;
1270 		*respond = true;
1271 		reset = true;
1272 	}
1273 
1274 	if (!accept_addr)
1275 		goto exit;
1276 
1277 	/* Now create new link if not already existing */
1278 	if (!l) {
1279 		if (n->link_cnt == 2)
1280 			goto exit;
1281 
1282 		if_name = strchr(b->name, ':') + 1;
1283 		session = get_random_u16();
1284 		if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
1285 				      b->net_plane, b->mtu, b->priority,
1286 				      b->min_win, b->max_win, session,
1287 				      tipc_own_addr(net), addr, peer_id,
1288 				      n->capabilities,
1289 				      tipc_bc_sndlink(n->net), n->bc_entry.link,
1290 				      &le->inputq,
1291 				      &n->bc_entry.namedq, &l)) {
1292 			*respond = false;
1293 			goto exit;
1294 		}
1295 		trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
1296 		tipc_link_reset(l);
1297 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
1298 		if (n->state == NODE_FAILINGOVER)
1299 			tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1300 		link_is_reset = tipc_link_is_reset(l);
1301 		le->link = l;
1302 		n->link_cnt++;
1303 		tipc_node_calculate_timer(n, l);
1304 		if (n->link_cnt == 1) {
1305 			intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1306 			if (!mod_timer(&n->timer, intv))
1307 				tipc_node_get(n);
1308 		}
1309 	}
1310 	memcpy(&le->maddr, maddr, sizeof(*maddr));
1311 exit:
1312 	tipc_node_write_unlock(n);
1313 	if (reset && !link_is_reset)
1314 		tipc_node_link_down(n, b->identity, false);
1315 	tipc_node_put(n);
1316 }
1317 
1318 void tipc_node_delete_links(struct net *net, int bearer_id)
1319 {
1320 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1321 	struct tipc_node *n;
1322 
1323 	rcu_read_lock();
1324 	list_for_each_entry_rcu(n, &tn->node_list, list) {
1325 		tipc_node_link_down(n, bearer_id, true);
1326 	}
1327 	rcu_read_unlock();
1328 }
1329 
1330 static void tipc_node_reset_links(struct tipc_node *n)
1331 {
1332 	int i;
1333 
1334 	pr_warn("Resetting all links to %x\n", n->addr);
1335 
1336 	trace_tipc_node_reset_links(n, true, " ");
1337 	for (i = 0; i < MAX_BEARERS; i++) {
1338 		tipc_node_link_down(n, i, false);
1339 	}
1340 }
1341 
1342 /* tipc_node_fsm_evt - node finite state machine
1343  * Determines when contact is allowed with peer node
1344  */
1345 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
1346 {
1347 	int state = n->state;
1348 
1349 	switch (state) {
1350 	case SELF_DOWN_PEER_DOWN:
1351 		switch (evt) {
1352 		case SELF_ESTABL_CONTACT_EVT:
1353 			state = SELF_UP_PEER_COMING;
1354 			break;
1355 		case PEER_ESTABL_CONTACT_EVT:
1356 			state = SELF_COMING_PEER_UP;
1357 			break;
1358 		case SELF_LOST_CONTACT_EVT:
1359 		case PEER_LOST_CONTACT_EVT:
1360 			break;
1361 		case NODE_SYNCH_END_EVT:
1362 		case NODE_SYNCH_BEGIN_EVT:
1363 		case NODE_FAILOVER_BEGIN_EVT:
1364 		case NODE_FAILOVER_END_EVT:
1365 		default:
1366 			goto illegal_evt;
1367 		}
1368 		break;
1369 	case SELF_UP_PEER_UP:
1370 		switch (evt) {
1371 		case SELF_LOST_CONTACT_EVT:
1372 			state = SELF_DOWN_PEER_LEAVING;
1373 			break;
1374 		case PEER_LOST_CONTACT_EVT:
1375 			state = SELF_LEAVING_PEER_DOWN;
1376 			break;
1377 		case NODE_SYNCH_BEGIN_EVT:
1378 			state = NODE_SYNCHING;
1379 			break;
1380 		case NODE_FAILOVER_BEGIN_EVT:
1381 			state = NODE_FAILINGOVER;
1382 			break;
1383 		case SELF_ESTABL_CONTACT_EVT:
1384 		case PEER_ESTABL_CONTACT_EVT:
1385 		case NODE_SYNCH_END_EVT:
1386 		case NODE_FAILOVER_END_EVT:
1387 			break;
1388 		default:
1389 			goto illegal_evt;
1390 		}
1391 		break;
1392 	case SELF_DOWN_PEER_LEAVING:
1393 		switch (evt) {
1394 		case PEER_LOST_CONTACT_EVT:
1395 			state = SELF_DOWN_PEER_DOWN;
1396 			break;
1397 		case SELF_ESTABL_CONTACT_EVT:
1398 		case PEER_ESTABL_CONTACT_EVT:
1399 		case SELF_LOST_CONTACT_EVT:
1400 			break;
1401 		case NODE_SYNCH_END_EVT:
1402 		case NODE_SYNCH_BEGIN_EVT:
1403 		case NODE_FAILOVER_BEGIN_EVT:
1404 		case NODE_FAILOVER_END_EVT:
1405 		default:
1406 			goto illegal_evt;
1407 		}
1408 		break;
1409 	case SELF_UP_PEER_COMING:
1410 		switch (evt) {
1411 		case PEER_ESTABL_CONTACT_EVT:
1412 			state = SELF_UP_PEER_UP;
1413 			break;
1414 		case SELF_LOST_CONTACT_EVT:
1415 			state = SELF_DOWN_PEER_DOWN;
1416 			break;
1417 		case SELF_ESTABL_CONTACT_EVT:
1418 		case PEER_LOST_CONTACT_EVT:
1419 		case NODE_SYNCH_END_EVT:
1420 		case NODE_FAILOVER_BEGIN_EVT:
1421 			break;
1422 		case NODE_SYNCH_BEGIN_EVT:
1423 		case NODE_FAILOVER_END_EVT:
1424 		default:
1425 			goto illegal_evt;
1426 		}
1427 		break;
1428 	case SELF_COMING_PEER_UP:
1429 		switch (evt) {
1430 		case SELF_ESTABL_CONTACT_EVT:
1431 			state = SELF_UP_PEER_UP;
1432 			break;
1433 		case PEER_LOST_CONTACT_EVT:
1434 			state = SELF_DOWN_PEER_DOWN;
1435 			break;
1436 		case SELF_LOST_CONTACT_EVT:
1437 		case PEER_ESTABL_CONTACT_EVT:
1438 			break;
1439 		case NODE_SYNCH_END_EVT:
1440 		case NODE_SYNCH_BEGIN_EVT:
1441 		case NODE_FAILOVER_BEGIN_EVT:
1442 		case NODE_FAILOVER_END_EVT:
1443 		default:
1444 			goto illegal_evt;
1445 		}
1446 		break;
1447 	case SELF_LEAVING_PEER_DOWN:
1448 		switch (evt) {
1449 		case SELF_LOST_CONTACT_EVT:
1450 			state = SELF_DOWN_PEER_DOWN;
1451 			break;
1452 		case SELF_ESTABL_CONTACT_EVT:
1453 		case PEER_ESTABL_CONTACT_EVT:
1454 		case PEER_LOST_CONTACT_EVT:
1455 			break;
1456 		case NODE_SYNCH_END_EVT:
1457 		case NODE_SYNCH_BEGIN_EVT:
1458 		case NODE_FAILOVER_BEGIN_EVT:
1459 		case NODE_FAILOVER_END_EVT:
1460 		default:
1461 			goto illegal_evt;
1462 		}
1463 		break;
1464 	case NODE_FAILINGOVER:
1465 		switch (evt) {
1466 		case SELF_LOST_CONTACT_EVT:
1467 			state = SELF_DOWN_PEER_LEAVING;
1468 			break;
1469 		case PEER_LOST_CONTACT_EVT:
1470 			state = SELF_LEAVING_PEER_DOWN;
1471 			break;
1472 		case NODE_FAILOVER_END_EVT:
1473 			state = SELF_UP_PEER_UP;
1474 			break;
1475 		case NODE_FAILOVER_BEGIN_EVT:
1476 		case SELF_ESTABL_CONTACT_EVT:
1477 		case PEER_ESTABL_CONTACT_EVT:
1478 			break;
1479 		case NODE_SYNCH_BEGIN_EVT:
1480 		case NODE_SYNCH_END_EVT:
1481 		default:
1482 			goto illegal_evt;
1483 		}
1484 		break;
1485 	case NODE_SYNCHING:
1486 		switch (evt) {
1487 		case SELF_LOST_CONTACT_EVT:
1488 			state = SELF_DOWN_PEER_LEAVING;
1489 			break;
1490 		case PEER_LOST_CONTACT_EVT:
1491 			state = SELF_LEAVING_PEER_DOWN;
1492 			break;
1493 		case NODE_SYNCH_END_EVT:
1494 			state = SELF_UP_PEER_UP;
1495 			break;
1496 		case NODE_FAILOVER_BEGIN_EVT:
1497 			state = NODE_FAILINGOVER;
1498 			break;
1499 		case NODE_SYNCH_BEGIN_EVT:
1500 		case SELF_ESTABL_CONTACT_EVT:
1501 		case PEER_ESTABL_CONTACT_EVT:
1502 			break;
1503 		case NODE_FAILOVER_END_EVT:
1504 		default:
1505 			goto illegal_evt;
1506 		}
1507 		break;
1508 	default:
1509 		pr_err("Unknown node fsm state %x\n", state);
1510 		break;
1511 	}
1512 	trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
1513 	n->state = state;
1514 	return;
1515 
1516 illegal_evt:
1517 	pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1518 	trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
1519 }
1520 
1521 static void node_lost_contact(struct tipc_node *n,
1522 			      struct sk_buff_head *inputq)
1523 {
1524 	struct tipc_sock_conn *conn, *safe;
1525 	struct tipc_link *l;
1526 	struct list_head *conns = &n->conn_sks;
1527 	struct sk_buff *skb;
1528 	uint i;
1529 
1530 	pr_debug("Lost contact with %x\n", n->addr);
1531 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1532 	trace_tipc_node_lost_contact(n, true, " ");
1533 
1534 	/* Clean up broadcast state */
1535 	tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1536 	skb_queue_purge(&n->bc_entry.namedq);
1537 
1538 	/* Abort any ongoing link failover */
1539 	for (i = 0; i < MAX_BEARERS; i++) {
1540 		l = n->links[i].link;
1541 		if (l)
1542 			tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1543 	}
1544 
1545 	/* Notify publications from this node */
1546 	n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1547 	n->peer_net = NULL;
1548 	n->peer_hash_mix = 0;
1549 	/* Notify sockets connected to node */
1550 	list_for_each_entry_safe(conn, safe, conns, list) {
1551 		skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1552 				      SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1553 				      conn->peer_node, conn->port,
1554 				      conn->peer_port, TIPC_ERR_NO_NODE);
1555 		if (likely(skb))
1556 			skb_queue_tail(inputq, skb);
1557 		list_del(&conn->list);
1558 		kfree(conn);
1559 	}
1560 }
1561 
1562 /**
1563  * tipc_node_get_linkname - get the name of a link
1564  *
1565  * @net: the applicable net namespace
1566  * @bearer_id: id of the bearer
1567  * @addr: peer node address
1568  * @linkname: link name output buffer
1569  * @len: size of @linkname output buffer
1570  *
1571  * Return: 0 on success
1572  */
1573 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1574 			   char *linkname, size_t len)
1575 {
1576 	struct tipc_link *link;
1577 	int err = -EINVAL;
1578 	struct tipc_node *node = tipc_node_find(net, addr);
1579 
1580 	if (!node)
1581 		return err;
1582 
1583 	if (bearer_id >= MAX_BEARERS)
1584 		goto exit;
1585 
1586 	tipc_node_read_lock(node);
1587 	link = node->links[bearer_id].link;
1588 	if (link) {
1589 		strscpy(linkname, tipc_link_name(link), len);
1590 		err = 0;
1591 	}
1592 	tipc_node_read_unlock(node);
1593 exit:
1594 	tipc_node_put(node);
1595 	return err;
1596 }
1597 
1598 /* Caller should hold node lock for the passed node */
1599 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1600 {
1601 	void *hdr;
1602 	struct nlattr *attrs;
1603 
1604 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1605 			  NLM_F_MULTI, TIPC_NL_NODE_GET);
1606 	if (!hdr)
1607 		return -EMSGSIZE;
1608 
1609 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
1610 	if (!attrs)
1611 		goto msg_full;
1612 
1613 	if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1614 		goto attr_msg_full;
1615 	if (node_is_up(node))
1616 		if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1617 			goto attr_msg_full;
1618 
1619 	nla_nest_end(msg->skb, attrs);
1620 	genlmsg_end(msg->skb, hdr);
1621 
1622 	return 0;
1623 
1624 attr_msg_full:
1625 	nla_nest_cancel(msg->skb, attrs);
1626 msg_full:
1627 	genlmsg_cancel(msg->skb, hdr);
1628 
1629 	return -EMSGSIZE;
1630 }
1631 
1632 static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
1633 {
1634 	struct tipc_msg *hdr = buf_msg(skb_peek(list));
1635 	struct sk_buff_head inputq;
1636 
1637 	switch (msg_user(hdr)) {
1638 	case TIPC_LOW_IMPORTANCE:
1639 	case TIPC_MEDIUM_IMPORTANCE:
1640 	case TIPC_HIGH_IMPORTANCE:
1641 	case TIPC_CRITICAL_IMPORTANCE:
1642 		if (msg_connected(hdr) || msg_named(hdr) ||
1643 		    msg_direct(hdr)) {
1644 			tipc_loopback_trace(peer_net, list);
1645 			spin_lock_init(&list->lock);
1646 			tipc_sk_rcv(peer_net, list);
1647 			return;
1648 		}
1649 		if (msg_mcast(hdr)) {
1650 			tipc_loopback_trace(peer_net, list);
1651 			skb_queue_head_init(&inputq);
1652 			tipc_sk_mcast_rcv(peer_net, list, &inputq);
1653 			__skb_queue_purge(list);
1654 			skb_queue_purge(&inputq);
1655 			return;
1656 		}
1657 		return;
1658 	case MSG_FRAGMENTER:
1659 		if (tipc_msg_assemble(list)) {
1660 			tipc_loopback_trace(peer_net, list);
1661 			skb_queue_head_init(&inputq);
1662 			tipc_sk_mcast_rcv(peer_net, list, &inputq);
1663 			__skb_queue_purge(list);
1664 			skb_queue_purge(&inputq);
1665 		}
1666 		return;
1667 	case GROUP_PROTOCOL:
1668 	case CONN_MANAGER:
1669 		tipc_loopback_trace(peer_net, list);
1670 		spin_lock_init(&list->lock);
1671 		tipc_sk_rcv(peer_net, list);
1672 		return;
1673 	case LINK_PROTOCOL:
1674 	case NAME_DISTRIBUTOR:
1675 	case TUNNEL_PROTOCOL:
1676 	case BCAST_PROTOCOL:
1677 		return;
1678 	default:
1679 		return;
1680 	}
1681 }
1682 
1683 /**
1684  * tipc_node_xmit() - general link level function for message sending
1685  * @net: the applicable net namespace
1686  * @list: chain of buffers containing message
1687  * @dnode: address of destination node
1688  * @selector: a number used for deterministic link selection
1689  * Consumes the buffer chain.
1690  * Return: 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1691  */
1692 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1693 		   u32 dnode, int selector)
1694 {
1695 	struct tipc_link_entry *le = NULL;
1696 	struct tipc_node *n;
1697 	struct sk_buff_head xmitq;
1698 	bool node_up = false;
1699 	struct net *peer_net;
1700 	int bearer_id;
1701 	int rc;
1702 
1703 	if (in_own_node(net, dnode)) {
1704 		tipc_loopback_trace(net, list);
1705 		spin_lock_init(&list->lock);
1706 		tipc_sk_rcv(net, list);
1707 		return 0;
1708 	}
1709 
1710 	n = tipc_node_find(net, dnode);
1711 	if (unlikely(!n)) {
1712 		__skb_queue_purge(list);
1713 		return -EHOSTUNREACH;
1714 	}
1715 
1716 	rcu_read_lock();
1717 	tipc_node_read_lock(n);
1718 	node_up = node_is_up(n);
1719 	peer_net = n->peer_net;
1720 	tipc_node_read_unlock(n);
1721 	if (node_up && peer_net && check_net(peer_net)) {
1722 		/* xmit inner linux container */
1723 		tipc_lxc_xmit(peer_net, list);
1724 		if (likely(skb_queue_empty(list))) {
1725 			rcu_read_unlock();
1726 			tipc_node_put(n);
1727 			return 0;
1728 		}
1729 	}
1730 	rcu_read_unlock();
1731 
1732 	tipc_node_read_lock(n);
1733 	bearer_id = n->active_links[selector & 1];
1734 	if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1735 		tipc_node_read_unlock(n);
1736 		tipc_node_put(n);
1737 		__skb_queue_purge(list);
1738 		return -EHOSTUNREACH;
1739 	}
1740 
1741 	__skb_queue_head_init(&xmitq);
1742 	le = &n->links[bearer_id];
1743 	spin_lock_bh(&le->lock);
1744 	rc = tipc_link_xmit(le->link, list, &xmitq);
1745 	spin_unlock_bh(&le->lock);
1746 	tipc_node_read_unlock(n);
1747 
1748 	if (unlikely(rc == -ENOBUFS))
1749 		tipc_node_link_down(n, bearer_id, false);
1750 	else
1751 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
1752 
1753 	tipc_node_put(n);
1754 
1755 	return rc;
1756 }
1757 
1758 /* tipc_node_xmit_skb(): send single buffer to destination
1759  * Buffers sent via this function are generally TIPC_SYSTEM_IMPORTANCE
1760  * messages, which will not be rejected
1761  * The only exception is datagram messages rerouted after secondary
1762  * lookup, which are rare and safe to dispose of anyway.
1763  */
1764 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1765 		       u32 selector)
1766 {
1767 	struct sk_buff_head head;
1768 
1769 	__skb_queue_head_init(&head);
1770 	__skb_queue_tail(&head, skb);
1771 	tipc_node_xmit(net, &head, dnode, selector);
1772 	return 0;
1773 }
1774 
1775 /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1776  * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1777  */
1778 int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1779 {
1780 	struct sk_buff *skb;
1781 	u32 selector, dnode;
1782 
1783 	while ((skb = __skb_dequeue(xmitq))) {
1784 		selector = msg_origport(buf_msg(skb));
1785 		dnode = msg_destnode(buf_msg(skb));
1786 		tipc_node_xmit_skb(net, skb, dnode, selector);
1787 	}
1788 	return 0;
1789 }
1790 
1791 void tipc_node_broadcast(struct net *net, struct sk_buff *skb, int rc_dests)
1792 {
1793 	struct sk_buff_head xmitq;
1794 	struct sk_buff *txskb;
1795 	struct tipc_node *n;
1796 	u16 dummy;
1797 	u32 dst;
1798 
1799 	/* Use broadcast if all nodes support it */
1800 	if (!rc_dests && tipc_bcast_get_mode(net) != BCLINK_MODE_RCAST) {
1801 		__skb_queue_head_init(&xmitq);
1802 		__skb_queue_tail(&xmitq, skb);
1803 		tipc_bcast_xmit(net, &xmitq, &dummy);
1804 		return;
1805 	}
1806 
1807 	/* Otherwise use legacy replicast method */
1808 	rcu_read_lock();
1809 	list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1810 		dst = n->addr;
1811 		if (in_own_node(net, dst))
1812 			continue;
1813 		if (!node_is_up(n))
1814 			continue;
1815 		txskb = pskb_copy(skb, GFP_ATOMIC);
1816 		if (!txskb)
1817 			break;
1818 		msg_set_destnode(buf_msg(txskb), dst);
1819 		tipc_node_xmit_skb(net, txskb, dst, 0);
1820 	}
1821 	rcu_read_unlock();
1822 	kfree_skb(skb);
1823 }
1824 
1825 static void tipc_node_mcast_rcv(struct tipc_node *n)
1826 {
1827 	struct tipc_bclink_entry *be = &n->bc_entry;
1828 
1829 	/* 'arrvq' is under inputq2's lock protection */
1830 	spin_lock_bh(&be->inputq2.lock);
1831 	spin_lock_bh(&be->inputq1.lock);
1832 	skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1833 	spin_unlock_bh(&be->inputq1.lock);
1834 	spin_unlock_bh(&be->inputq2.lock);
1835 	tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1836 }
1837 
1838 static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1839 				  int bearer_id, struct sk_buff_head *xmitq,
1840 				  bool *valid)
1841 {
1842 	struct tipc_link *ucl;
1843 	int rc;
1844 
1845 	rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr, xmitq, valid);
1846 	if (!*valid)
1847 		return;
1848 
1849 	if (rc & TIPC_LINK_DOWN_EVT) {
1850 		tipc_node_reset_links(n);
1851 		return;
1852 	}
1853 
1854 	if (!(rc & TIPC_LINK_SND_STATE))
1855 		return;
1856 
1857 	/* If probe message, a STATE response will be sent anyway */
1858 	if (msg_probe(hdr))
1859 		return;
1860 
1861 	/* Produce a STATE message carrying broadcast NACK */
1862 	tipc_node_read_lock(n);
1863 	ucl = n->links[bearer_id].link;
1864 	if (ucl)
1865 		tipc_link_build_state_msg(ucl, xmitq);
1866 	tipc_node_read_unlock(n);
1867 }
1868 
1869 /**
1870  * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1871  * @net: the applicable net namespace
1872  * @skb: TIPC packet
1873  * @bearer_id: id of bearer message arrived on
1874  *
1875  * Invoked with no locks held.
1876  */
1877 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1878 {
1879 	int rc;
1880 	struct sk_buff_head xmitq;
1881 	struct tipc_bclink_entry *be;
1882 	struct tipc_link_entry *le;
1883 	struct tipc_msg *hdr = buf_msg(skb);
1884 	int usr = msg_user(hdr);
1885 	u32 dnode = msg_destnode(hdr);
1886 	struct tipc_node *n;
1887 
1888 	__skb_queue_head_init(&xmitq);
1889 
1890 	/* If NACK for other node, let rcv link for that node peek into it */
1891 	if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1892 		n = tipc_node_find(net, dnode);
1893 	else
1894 		n = tipc_node_find(net, msg_prevnode(hdr));
1895 	if (!n) {
1896 		kfree_skb(skb);
1897 		return;
1898 	}
1899 	be = &n->bc_entry;
1900 	le = &n->links[bearer_id];
1901 
1902 	rc = tipc_bcast_rcv(net, be->link, skb);
1903 
1904 	/* Broadcast ACKs are sent on a unicast link */
1905 	if (rc & TIPC_LINK_SND_STATE) {
1906 		tipc_node_read_lock(n);
1907 		tipc_link_build_state_msg(le->link, &xmitq);
1908 		tipc_node_read_unlock(n);
1909 	}
1910 
1911 	if (!skb_queue_empty(&xmitq))
1912 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
1913 
1914 	if (!skb_queue_empty(&be->inputq1))
1915 		tipc_node_mcast_rcv(n);
1916 
1917 	/* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
1918 	if (!skb_queue_empty(&n->bc_entry.namedq))
1919 		tipc_named_rcv(net, &n->bc_entry.namedq,
1920 			       &n->bc_entry.named_rcv_nxt,
1921 			       &n->bc_entry.named_open);
1922 
1923 	/* If reassembly or retransmission failure => reset all links to peer */
1924 	if (rc & TIPC_LINK_DOWN_EVT)
1925 		tipc_node_reset_links(n);
1926 
1927 	tipc_node_put(n);
1928 }
1929 
1930 /**
1931  * tipc_node_check_state - check and if necessary update node state
1932  * @n: target tipc_node
1933  * @skb: TIPC packet
1934  * @bearer_id: identity of bearer delivering the packet
1935  * @xmitq: queue for messages to be xmited on
1936  * Return: true if state and msg are ok, otherwise false
1937  */
1938 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1939 				  int bearer_id, struct sk_buff_head *xmitq)
1940 {
1941 	struct tipc_msg *hdr = buf_msg(skb);
1942 	int usr = msg_user(hdr);
1943 	int mtyp = msg_type(hdr);
1944 	u16 oseqno = msg_seqno(hdr);
1945 	u16 exp_pkts = msg_msgcnt(hdr);
1946 	u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1947 	int state = n->state;
1948 	struct tipc_link *l, *tnl, *pl = NULL;
1949 	struct tipc_media_addr *maddr;
1950 	int pb_id;
1951 
1952 	if (trace_tipc_node_check_state_enabled()) {
1953 		trace_tipc_skb_dump(skb, false, "skb for node state check");
1954 		trace_tipc_node_check_state(n, true, " ");
1955 	}
1956 	l = n->links[bearer_id].link;
1957 	if (!l)
1958 		return false;
1959 	rcv_nxt = tipc_link_rcv_nxt(l);
1960 
1961 
1962 	if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1963 		return true;
1964 
1965 	/* Find parallel link, if any */
1966 	for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1967 		if ((pb_id != bearer_id) && n->links[pb_id].link) {
1968 			pl = n->links[pb_id].link;
1969 			break;
1970 		}
1971 	}
1972 
1973 	if (!tipc_link_validate_msg(l, hdr)) {
1974 		trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
1975 		trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
1976 		return false;
1977 	}
1978 
1979 	/* Check and update node accesibility if applicable */
1980 	if (state == SELF_UP_PEER_COMING) {
1981 		if (!tipc_link_is_up(l))
1982 			return true;
1983 		if (!msg_peer_link_is_up(hdr))
1984 			return true;
1985 		tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1986 	}
1987 
1988 	if (state == SELF_DOWN_PEER_LEAVING) {
1989 		if (msg_peer_node_is_up(hdr))
1990 			return false;
1991 		tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1992 		return true;
1993 	}
1994 
1995 	if (state == SELF_LEAVING_PEER_DOWN)
1996 		return false;
1997 
1998 	/* Ignore duplicate packets */
1999 	if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
2000 		return true;
2001 
2002 	/* Initiate or update failover mode if applicable */
2003 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
2004 		syncpt = oseqno + exp_pkts - 1;
2005 		if (pl && !tipc_link_is_reset(pl)) {
2006 			__tipc_node_link_down(n, &pb_id, xmitq, &maddr);
2007 			trace_tipc_node_link_down(n, true,
2008 						  "node link down <- failover!");
2009 			tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
2010 							tipc_link_inputq(l));
2011 		}
2012 
2013 		/* If parallel link was already down, and this happened before
2014 		 * the tunnel link came up, node failover was never started.
2015 		 * Ensure that a FAILOVER_MSG is sent to get peer out of
2016 		 * NODE_FAILINGOVER state, also this node must accept
2017 		 * TUNNEL_MSGs from peer.
2018 		 */
2019 		if (n->state != NODE_FAILINGOVER)
2020 			tipc_node_link_failover(n, pl, l, xmitq);
2021 
2022 		/* If pkts arrive out of order, use lowest calculated syncpt */
2023 		if (less(syncpt, n->sync_point))
2024 			n->sync_point = syncpt;
2025 	}
2026 
2027 	/* Open parallel link when tunnel link reaches synch point */
2028 	if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
2029 		if (!more(rcv_nxt, n->sync_point))
2030 			return true;
2031 		tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
2032 		if (pl)
2033 			tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
2034 		return true;
2035 	}
2036 
2037 	/* No syncing needed if only one link */
2038 	if (!pl || !tipc_link_is_up(pl))
2039 		return true;
2040 
2041 	/* Initiate synch mode if applicable */
2042 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
2043 		if (n->capabilities & TIPC_TUNNEL_ENHANCED)
2044 			syncpt = msg_syncpt(hdr);
2045 		else
2046 			syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
2047 		if (!tipc_link_is_up(l))
2048 			__tipc_node_link_up(n, bearer_id, xmitq);
2049 		if (n->state == SELF_UP_PEER_UP) {
2050 			n->sync_point = syncpt;
2051 			tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
2052 			tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
2053 		}
2054 	}
2055 
2056 	/* Open tunnel link when parallel link reaches synch point */
2057 	if (n->state == NODE_SYNCHING) {
2058 		if (tipc_link_is_synching(l)) {
2059 			tnl = l;
2060 		} else {
2061 			tnl = pl;
2062 			pl = l;
2063 		}
2064 		inputq_len = skb_queue_len(tipc_link_inputq(pl));
2065 		dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
2066 		if (more(dlv_nxt, n->sync_point)) {
2067 			tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
2068 			tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
2069 			return true;
2070 		}
2071 		if (l == pl)
2072 			return true;
2073 		if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
2074 			return true;
2075 		if (usr == LINK_PROTOCOL)
2076 			return true;
2077 		return false;
2078 	}
2079 	return true;
2080 }
2081 
2082 /**
2083  * tipc_rcv - process TIPC packets/messages arriving from off-node
2084  * @net: the applicable net namespace
2085  * @skb: TIPC packet
2086  * @b: pointer to bearer message arrived on
2087  *
2088  * Invoked with no locks held. Bearer pointer must point to a valid bearer
2089  * structure (i.e. cannot be NULL), but bearer can be inactive.
2090  */
2091 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
2092 {
2093 	struct sk_buff_head xmitq;
2094 	struct tipc_link_entry *le;
2095 	struct tipc_msg *hdr;
2096 	struct tipc_node *n;
2097 	int bearer_id = b->identity;
2098 	u32 self = tipc_own_addr(net);
2099 	int usr, rc = 0;
2100 	u16 bc_ack;
2101 #ifdef CONFIG_TIPC_CRYPTO
2102 	struct tipc_ehdr *ehdr;
2103 
2104 	/* Check if message must be decrypted first */
2105 	if (TIPC_SKB_CB(skb)->decrypted || !tipc_ehdr_validate(skb))
2106 		goto rcv;
2107 
2108 	ehdr = (struct tipc_ehdr *)skb->data;
2109 	if (likely(ehdr->user != LINK_CONFIG)) {
2110 		n = tipc_node_find(net, ntohl(ehdr->addr));
2111 		if (unlikely(!n))
2112 			goto discard;
2113 	} else {
2114 		n = tipc_node_find_by_id(net, ehdr->id);
2115 	}
2116 	skb_dst_force(skb);
2117 	tipc_crypto_rcv(net, (n) ? n->crypto_rx : NULL, &skb, b);
2118 	if (!skb)
2119 		return;
2120 
2121 rcv:
2122 #endif
2123 	/* Ensure message is well-formed before touching the header */
2124 	if (unlikely(!tipc_msg_validate(&skb)))
2125 		goto discard;
2126 	__skb_queue_head_init(&xmitq);
2127 	hdr = buf_msg(skb);
2128 	usr = msg_user(hdr);
2129 	bc_ack = msg_bcast_ack(hdr);
2130 
2131 	/* Handle arrival of discovery or broadcast packet */
2132 	if (unlikely(msg_non_seq(hdr))) {
2133 		if (unlikely(usr == LINK_CONFIG))
2134 			return tipc_disc_rcv(net, skb, b);
2135 		else
2136 			return tipc_node_bc_rcv(net, skb, bearer_id);
2137 	}
2138 
2139 	/* Discard unicast link messages destined for another node */
2140 	if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
2141 		goto discard;
2142 
2143 	/* Locate neighboring node that sent packet */
2144 	n = tipc_node_find(net, msg_prevnode(hdr));
2145 	if (unlikely(!n))
2146 		goto discard;
2147 	le = &n->links[bearer_id];
2148 
2149 	/* Ensure broadcast reception is in synch with peer's send state */
2150 	if (unlikely(usr == LINK_PROTOCOL)) {
2151 		bool valid = true;
2152 
2153 		if (unlikely(skb_linearize(skb))) {
2154 			tipc_node_put(n);
2155 			goto discard;
2156 		}
2157 		hdr = buf_msg(skb);
2158 		tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq, &valid);
2159 		if (!valid) {
2160 			tipc_node_put(n);
2161 			goto discard;
2162 		}
2163 	} else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack)) {
2164 		tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
2165 	}
2166 
2167 	/* Receive packet directly if conditions permit */
2168 	tipc_node_read_lock(n);
2169 	if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
2170 		spin_lock_bh(&le->lock);
2171 		if (le->link) {
2172 			rc = tipc_link_rcv(le->link, skb, &xmitq);
2173 			skb = NULL;
2174 		}
2175 		spin_unlock_bh(&le->lock);
2176 	}
2177 	tipc_node_read_unlock(n);
2178 
2179 	/* Check/update node state before receiving */
2180 	if (unlikely(skb)) {
2181 		if (unlikely(skb_linearize(skb)))
2182 			goto out_node_put;
2183 		tipc_node_write_lock(n);
2184 		if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
2185 			if (le->link) {
2186 				rc = tipc_link_rcv(le->link, skb, &xmitq);
2187 				skb = NULL;
2188 			}
2189 		}
2190 		tipc_node_write_unlock(n);
2191 	}
2192 
2193 	if (unlikely(rc & TIPC_LINK_UP_EVT))
2194 		tipc_node_link_up(n, bearer_id, &xmitq);
2195 
2196 	if (unlikely(rc & TIPC_LINK_DOWN_EVT))
2197 		tipc_node_link_down(n, bearer_id, false);
2198 
2199 	if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
2200 		tipc_named_rcv(net, &n->bc_entry.namedq,
2201 			       &n->bc_entry.named_rcv_nxt,
2202 			       &n->bc_entry.named_open);
2203 
2204 	if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
2205 		tipc_node_mcast_rcv(n);
2206 
2207 	if (!skb_queue_empty(&le->inputq))
2208 		tipc_sk_rcv(net, &le->inputq);
2209 
2210 	if (!skb_queue_empty(&xmitq))
2211 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
2212 
2213 out_node_put:
2214 	tipc_node_put(n);
2215 discard:
2216 	kfree_skb(skb);
2217 }
2218 
2219 void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
2220 			      int prop)
2221 {
2222 	struct tipc_net *tn = tipc_net(net);
2223 	int bearer_id = b->identity;
2224 	struct sk_buff_head xmitq;
2225 	struct tipc_link_entry *e;
2226 	struct tipc_node *n;
2227 
2228 	__skb_queue_head_init(&xmitq);
2229 
2230 	rcu_read_lock();
2231 
2232 	list_for_each_entry_rcu(n, &tn->node_list, list) {
2233 		tipc_node_write_lock(n);
2234 		e = &n->links[bearer_id];
2235 		if (e->link) {
2236 			if (prop == TIPC_NLA_PROP_TOL)
2237 				tipc_link_set_tolerance(e->link, b->tolerance,
2238 							&xmitq);
2239 			else if (prop == TIPC_NLA_PROP_MTU)
2240 				tipc_link_set_mtu(e->link, b->mtu);
2241 
2242 			/* Update MTU for node link entry */
2243 			e->mtu = tipc_link_mss(e->link);
2244 		}
2245 
2246 		tipc_node_write_unlock(n);
2247 		tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr, NULL);
2248 	}
2249 
2250 	rcu_read_unlock();
2251 }
2252 
2253 int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
2254 {
2255 	struct net *net = sock_net(skb->sk);
2256 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2257 	struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
2258 	struct tipc_node *peer, *temp_node;
2259 	u8 node_id[NODE_ID_LEN];
2260 	u64 *w0 = (u64 *)&node_id[0];
2261 	u64 *w1 = (u64 *)&node_id[8];
2262 	u32 addr;
2263 	int err;
2264 
2265 	/* We identify the peer by its net */
2266 	if (!info->attrs[TIPC_NLA_NET])
2267 		return -EINVAL;
2268 
2269 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
2270 					  info->attrs[TIPC_NLA_NET],
2271 					  tipc_nl_net_policy, info->extack);
2272 	if (err)
2273 		return err;
2274 
2275 	/* attrs[TIPC_NLA_NET_NODEID] and attrs[TIPC_NLA_NET_ADDR] are
2276 	 * mutually exclusive cases
2277 	 */
2278 	if (attrs[TIPC_NLA_NET_ADDR]) {
2279 		addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
2280 		if (!addr)
2281 			return -EINVAL;
2282 	}
2283 
2284 	if (attrs[TIPC_NLA_NET_NODEID]) {
2285 		if (!attrs[TIPC_NLA_NET_NODEID_W1])
2286 			return -EINVAL;
2287 		*w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
2288 		*w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
2289 		addr = hash128to32(node_id);
2290 	}
2291 
2292 	if (in_own_node(net, addr))
2293 		return -ENOTSUPP;
2294 
2295 	spin_lock_bh(&tn->node_list_lock);
2296 	peer = tipc_node_find(net, addr);
2297 	if (!peer) {
2298 		spin_unlock_bh(&tn->node_list_lock);
2299 		return -ENXIO;
2300 	}
2301 
2302 	tipc_node_write_lock(peer);
2303 	if (peer->state != SELF_DOWN_PEER_DOWN &&
2304 	    peer->state != SELF_DOWN_PEER_LEAVING) {
2305 		tipc_node_write_unlock(peer);
2306 		err = -EBUSY;
2307 		goto err_out;
2308 	}
2309 
2310 	tipc_node_clear_links(peer);
2311 	tipc_node_write_unlock(peer);
2312 	tipc_node_delete(peer);
2313 
2314 	/* Calculate cluster capabilities */
2315 	tn->capabilities = TIPC_NODE_CAPABILITIES;
2316 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
2317 		tn->capabilities &= temp_node->capabilities;
2318 	}
2319 	tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
2320 	err = 0;
2321 err_out:
2322 	tipc_node_put(peer);
2323 	spin_unlock_bh(&tn->node_list_lock);
2324 
2325 	return err;
2326 }
2327 
2328 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
2329 {
2330 	int err;
2331 	struct net *net = sock_net(skb->sk);
2332 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2333 	int done = cb->args[0];
2334 	int last_addr = cb->args[1];
2335 	struct tipc_node *node;
2336 	struct tipc_nl_msg msg;
2337 
2338 	if (done)
2339 		return 0;
2340 
2341 	msg.skb = skb;
2342 	msg.portid = NETLINK_CB(cb->skb).portid;
2343 	msg.seq = cb->nlh->nlmsg_seq;
2344 
2345 	rcu_read_lock();
2346 	if (last_addr) {
2347 		node = tipc_node_find(net, last_addr);
2348 		if (!node) {
2349 			rcu_read_unlock();
2350 			/* We never set seq or call nl_dump_check_consistent()
2351 			 * this means that setting prev_seq here will cause the
2352 			 * consistence check to fail in the netlink callback
2353 			 * handler. Resulting in the NLMSG_DONE message having
2354 			 * the NLM_F_DUMP_INTR flag set if the node state
2355 			 * changed while we released the lock.
2356 			 */
2357 			cb->prev_seq = 1;
2358 			return -EPIPE;
2359 		}
2360 		tipc_node_put(node);
2361 	}
2362 
2363 	list_for_each_entry_rcu(node, &tn->node_list, list) {
2364 		if (node->preliminary)
2365 			continue;
2366 		if (last_addr) {
2367 			if (node->addr == last_addr)
2368 				last_addr = 0;
2369 			else
2370 				continue;
2371 		}
2372 
2373 		tipc_node_read_lock(node);
2374 		err = __tipc_nl_add_node(&msg, node);
2375 		if (err) {
2376 			last_addr = node->addr;
2377 			tipc_node_read_unlock(node);
2378 			goto out;
2379 		}
2380 
2381 		tipc_node_read_unlock(node);
2382 	}
2383 	done = 1;
2384 out:
2385 	cb->args[0] = done;
2386 	cb->args[1] = last_addr;
2387 	rcu_read_unlock();
2388 
2389 	return skb->len;
2390 }
2391 
2392 /* tipc_node_find_by_name - locate owner node of link by link's name
2393  * @net: the applicable net namespace
2394  * @name: pointer to link name string
2395  * @bearer_id: pointer to index in 'node->links' array where the link was found.
2396  *
2397  * Returns pointer to node owning the link, or 0 if no matching link is found.
2398  */
2399 static struct tipc_node *tipc_node_find_by_name(struct net *net,
2400 						const char *link_name,
2401 						unsigned int *bearer_id)
2402 {
2403 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2404 	struct tipc_link *l;
2405 	struct tipc_node *n;
2406 	struct tipc_node *found_node = NULL;
2407 	int i;
2408 
2409 	*bearer_id = 0;
2410 	rcu_read_lock();
2411 	list_for_each_entry_rcu(n, &tn->node_list, list) {
2412 		tipc_node_read_lock(n);
2413 		for (i = 0; i < MAX_BEARERS; i++) {
2414 			l = n->links[i].link;
2415 			if (l && !strcmp(tipc_link_name(l), link_name)) {
2416 				*bearer_id = i;
2417 				found_node = n;
2418 				break;
2419 			}
2420 		}
2421 		tipc_node_read_unlock(n);
2422 		if (found_node)
2423 			break;
2424 	}
2425 	rcu_read_unlock();
2426 
2427 	return found_node;
2428 }
2429 
2430 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
2431 {
2432 	int err;
2433 	int res = 0;
2434 	int bearer_id;
2435 	char *name;
2436 	struct tipc_link *link;
2437 	struct tipc_node *node;
2438 	struct sk_buff_head xmitq;
2439 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2440 	struct net *net = sock_net(skb->sk);
2441 
2442 	__skb_queue_head_init(&xmitq);
2443 
2444 	if (!info->attrs[TIPC_NLA_LINK])
2445 		return -EINVAL;
2446 
2447 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2448 					  info->attrs[TIPC_NLA_LINK],
2449 					  tipc_nl_link_policy, info->extack);
2450 	if (err)
2451 		return err;
2452 
2453 	if (!attrs[TIPC_NLA_LINK_NAME])
2454 		return -EINVAL;
2455 
2456 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2457 
2458 	if (strcmp(name, tipc_bclink_name) == 0)
2459 		return tipc_nl_bc_link_set(net, attrs);
2460 
2461 	node = tipc_node_find_by_name(net, name, &bearer_id);
2462 	if (!node)
2463 		return -EINVAL;
2464 
2465 	tipc_node_read_lock(node);
2466 
2467 	link = node->links[bearer_id].link;
2468 	if (!link) {
2469 		res = -EINVAL;
2470 		goto out;
2471 	}
2472 
2473 	if (attrs[TIPC_NLA_LINK_PROP]) {
2474 		struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2475 
2476 		err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
2477 		if (err) {
2478 			res = err;
2479 			goto out;
2480 		}
2481 
2482 		if (props[TIPC_NLA_PROP_TOL]) {
2483 			u32 tol;
2484 
2485 			tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2486 			tipc_link_set_tolerance(link, tol, &xmitq);
2487 		}
2488 		if (props[TIPC_NLA_PROP_PRIO]) {
2489 			u32 prio;
2490 
2491 			prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2492 			tipc_link_set_prio(link, prio, &xmitq);
2493 		}
2494 		if (props[TIPC_NLA_PROP_WIN]) {
2495 			u32 max_win;
2496 
2497 			max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2498 			tipc_link_set_queue_limits(link,
2499 						   tipc_link_min_win(link),
2500 						   max_win);
2501 		}
2502 	}
2503 
2504 out:
2505 	tipc_node_read_unlock(node);
2506 	tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr,
2507 			 NULL);
2508 	return res;
2509 }
2510 
2511 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2512 {
2513 	struct net *net = genl_info_net(info);
2514 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2515 	struct tipc_nl_msg msg;
2516 	char *name;
2517 	int err;
2518 
2519 	msg.portid = info->snd_portid;
2520 	msg.seq = info->snd_seq;
2521 
2522 	if (!info->attrs[TIPC_NLA_LINK])
2523 		return -EINVAL;
2524 
2525 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2526 					  info->attrs[TIPC_NLA_LINK],
2527 					  tipc_nl_link_policy, info->extack);
2528 	if (err)
2529 		return err;
2530 
2531 	if (!attrs[TIPC_NLA_LINK_NAME])
2532 		return -EINVAL;
2533 
2534 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2535 
2536 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2537 	if (!msg.skb)
2538 		return -ENOMEM;
2539 
2540 	if (strcmp(name, tipc_bclink_name) == 0) {
2541 		err = tipc_nl_add_bc_link(net, &msg, tipc_net(net)->bcl);
2542 		if (err)
2543 			goto err_free;
2544 	} else {
2545 		int bearer_id;
2546 		struct tipc_node *node;
2547 		struct tipc_link *link;
2548 
2549 		node = tipc_node_find_by_name(net, name, &bearer_id);
2550 		if (!node) {
2551 			err = -EINVAL;
2552 			goto err_free;
2553 		}
2554 
2555 		tipc_node_read_lock(node);
2556 		link = node->links[bearer_id].link;
2557 		if (!link) {
2558 			tipc_node_read_unlock(node);
2559 			err = -EINVAL;
2560 			goto err_free;
2561 		}
2562 
2563 		err = __tipc_nl_add_link(net, &msg, link, 0);
2564 		tipc_node_read_unlock(node);
2565 		if (err)
2566 			goto err_free;
2567 	}
2568 
2569 	return genlmsg_reply(msg.skb, info);
2570 
2571 err_free:
2572 	nlmsg_free(msg.skb);
2573 	return err;
2574 }
2575 
2576 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2577 {
2578 	int err;
2579 	char *link_name;
2580 	unsigned int bearer_id;
2581 	struct tipc_link *link;
2582 	struct tipc_node *node;
2583 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2584 	struct net *net = sock_net(skb->sk);
2585 	struct tipc_net *tn = tipc_net(net);
2586 	struct tipc_link_entry *le;
2587 
2588 	if (!info->attrs[TIPC_NLA_LINK])
2589 		return -EINVAL;
2590 
2591 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2592 					  info->attrs[TIPC_NLA_LINK],
2593 					  tipc_nl_link_policy, info->extack);
2594 	if (err)
2595 		return err;
2596 
2597 	if (!attrs[TIPC_NLA_LINK_NAME])
2598 		return -EINVAL;
2599 
2600 	link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2601 
2602 	err = -EINVAL;
2603 	if (!strcmp(link_name, tipc_bclink_name)) {
2604 		err = tipc_bclink_reset_stats(net, tipc_bc_sndlink(net));
2605 		if (err)
2606 			return err;
2607 		return 0;
2608 	} else if (strstr(link_name, tipc_bclink_name)) {
2609 		rcu_read_lock();
2610 		list_for_each_entry_rcu(node, &tn->node_list, list) {
2611 			tipc_node_read_lock(node);
2612 			link = node->bc_entry.link;
2613 			if (link && !strcmp(link_name, tipc_link_name(link))) {
2614 				err = tipc_bclink_reset_stats(net, link);
2615 				tipc_node_read_unlock(node);
2616 				break;
2617 			}
2618 			tipc_node_read_unlock(node);
2619 		}
2620 		rcu_read_unlock();
2621 		return err;
2622 	}
2623 
2624 	node = tipc_node_find_by_name(net, link_name, &bearer_id);
2625 	if (!node)
2626 		return -EINVAL;
2627 
2628 	le = &node->links[bearer_id];
2629 	tipc_node_read_lock(node);
2630 	spin_lock_bh(&le->lock);
2631 	link = node->links[bearer_id].link;
2632 	if (!link) {
2633 		spin_unlock_bh(&le->lock);
2634 		tipc_node_read_unlock(node);
2635 		return -EINVAL;
2636 	}
2637 	tipc_link_reset_stats(link);
2638 	spin_unlock_bh(&le->lock);
2639 	tipc_node_read_unlock(node);
2640 	return 0;
2641 }
2642 
2643 /* Caller should hold node lock  */
2644 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2645 				    struct tipc_node *node, u32 *prev_link,
2646 				    bool bc_link)
2647 {
2648 	u32 i;
2649 	int err;
2650 
2651 	for (i = *prev_link; i < MAX_BEARERS; i++) {
2652 		*prev_link = i;
2653 
2654 		if (!node->links[i].link)
2655 			continue;
2656 
2657 		err = __tipc_nl_add_link(net, msg,
2658 					 node->links[i].link, NLM_F_MULTI);
2659 		if (err)
2660 			return err;
2661 	}
2662 
2663 	if (bc_link) {
2664 		*prev_link = i;
2665 		err = tipc_nl_add_bc_link(net, msg, node->bc_entry.link);
2666 		if (err)
2667 			return err;
2668 	}
2669 
2670 	*prev_link = 0;
2671 
2672 	return 0;
2673 }
2674 
2675 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
2676 {
2677 	struct net *net = sock_net(skb->sk);
2678 	struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
2679 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
2680 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2681 	struct tipc_node *node;
2682 	struct tipc_nl_msg msg;
2683 	u32 prev_node = cb->args[0];
2684 	u32 prev_link = cb->args[1];
2685 	int done = cb->args[2];
2686 	bool bc_link = cb->args[3];
2687 	int err;
2688 
2689 	if (done)
2690 		return 0;
2691 
2692 	if (!prev_node) {
2693 		/* Check if broadcast-receiver links dumping is needed */
2694 		if (attrs && attrs[TIPC_NLA_LINK]) {
2695 			err = nla_parse_nested_deprecated(link,
2696 							  TIPC_NLA_LINK_MAX,
2697 							  attrs[TIPC_NLA_LINK],
2698 							  tipc_nl_link_policy,
2699 							  NULL);
2700 			if (unlikely(err))
2701 				return err;
2702 			if (unlikely(!link[TIPC_NLA_LINK_BROADCAST]))
2703 				return -EINVAL;
2704 			bc_link = true;
2705 		}
2706 	}
2707 
2708 	msg.skb = skb;
2709 	msg.portid = NETLINK_CB(cb->skb).portid;
2710 	msg.seq = cb->nlh->nlmsg_seq;
2711 
2712 	rcu_read_lock();
2713 	if (prev_node) {
2714 		node = tipc_node_find(net, prev_node);
2715 		if (!node) {
2716 			/* We never set seq or call nl_dump_check_consistent()
2717 			 * this means that setting prev_seq here will cause the
2718 			 * consistence check to fail in the netlink callback
2719 			 * handler. Resulting in the last NLMSG_DONE message
2720 			 * having the NLM_F_DUMP_INTR flag set.
2721 			 */
2722 			cb->prev_seq = 1;
2723 			goto out;
2724 		}
2725 		tipc_node_put(node);
2726 
2727 		list_for_each_entry_continue_rcu(node, &tn->node_list,
2728 						 list) {
2729 			tipc_node_read_lock(node);
2730 			err = __tipc_nl_add_node_links(net, &msg, node,
2731 						       &prev_link, bc_link);
2732 			tipc_node_read_unlock(node);
2733 			if (err)
2734 				goto out;
2735 
2736 			prev_node = node->addr;
2737 		}
2738 	} else {
2739 		err = tipc_nl_add_bc_link(net, &msg, tn->bcl);
2740 		if (err)
2741 			goto out;
2742 
2743 		list_for_each_entry_rcu(node, &tn->node_list, list) {
2744 			tipc_node_read_lock(node);
2745 			err = __tipc_nl_add_node_links(net, &msg, node,
2746 						       &prev_link, bc_link);
2747 			tipc_node_read_unlock(node);
2748 			if (err)
2749 				goto out;
2750 
2751 			prev_node = node->addr;
2752 		}
2753 	}
2754 	done = 1;
2755 out:
2756 	rcu_read_unlock();
2757 
2758 	cb->args[0] = prev_node;
2759 	cb->args[1] = prev_link;
2760 	cb->args[2] = done;
2761 	cb->args[3] = bc_link;
2762 
2763 	return skb->len;
2764 }
2765 
2766 int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2767 {
2768 	struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2769 	struct net *net = sock_net(skb->sk);
2770 	int err;
2771 
2772 	if (!info->attrs[TIPC_NLA_MON])
2773 		return -EINVAL;
2774 
2775 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
2776 					  info->attrs[TIPC_NLA_MON],
2777 					  tipc_nl_monitor_policy,
2778 					  info->extack);
2779 	if (err)
2780 		return err;
2781 
2782 	if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2783 		u32 val;
2784 
2785 		val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2786 		err = tipc_nl_monitor_set_threshold(net, val);
2787 		if (err)
2788 			return err;
2789 	}
2790 
2791 	return 0;
2792 }
2793 
2794 static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2795 {
2796 	struct nlattr *attrs;
2797 	void *hdr;
2798 	u32 val;
2799 
2800 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2801 			  0, TIPC_NL_MON_GET);
2802 	if (!hdr)
2803 		return -EMSGSIZE;
2804 
2805 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
2806 	if (!attrs)
2807 		goto msg_full;
2808 
2809 	val = tipc_nl_monitor_get_threshold(net);
2810 
2811 	if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2812 		goto attr_msg_full;
2813 
2814 	nla_nest_end(msg->skb, attrs);
2815 	genlmsg_end(msg->skb, hdr);
2816 
2817 	return 0;
2818 
2819 attr_msg_full:
2820 	nla_nest_cancel(msg->skb, attrs);
2821 msg_full:
2822 	genlmsg_cancel(msg->skb, hdr);
2823 
2824 	return -EMSGSIZE;
2825 }
2826 
2827 int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2828 {
2829 	struct net *net = sock_net(skb->sk);
2830 	struct tipc_nl_msg msg;
2831 	int err;
2832 
2833 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2834 	if (!msg.skb)
2835 		return -ENOMEM;
2836 	msg.portid = info->snd_portid;
2837 	msg.seq = info->snd_seq;
2838 
2839 	err = __tipc_nl_add_monitor_prop(net, &msg);
2840 	if (err) {
2841 		nlmsg_free(msg.skb);
2842 		return err;
2843 	}
2844 
2845 	return genlmsg_reply(msg.skb, info);
2846 }
2847 
2848 int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2849 {
2850 	struct net *net = sock_net(skb->sk);
2851 	u32 prev_bearer = cb->args[0];
2852 	struct tipc_nl_msg msg;
2853 	int bearer_id;
2854 	int err;
2855 
2856 	if (prev_bearer == MAX_BEARERS)
2857 		return 0;
2858 
2859 	msg.skb = skb;
2860 	msg.portid = NETLINK_CB(cb->skb).portid;
2861 	msg.seq = cb->nlh->nlmsg_seq;
2862 
2863 	rtnl_lock();
2864 	for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
2865 		err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2866 		if (err)
2867 			break;
2868 	}
2869 	rtnl_unlock();
2870 	cb->args[0] = bearer_id;
2871 
2872 	return skb->len;
2873 }
2874 
2875 int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2876 				   struct netlink_callback *cb)
2877 {
2878 	struct net *net = sock_net(skb->sk);
2879 	u32 prev_node = cb->args[1];
2880 	u32 bearer_id = cb->args[2];
2881 	int done = cb->args[0];
2882 	struct tipc_nl_msg msg;
2883 	int err;
2884 
2885 	if (!prev_node) {
2886 		struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
2887 		struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2888 
2889 		if (!attrs[TIPC_NLA_MON])
2890 			return -EINVAL;
2891 
2892 		err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
2893 						  attrs[TIPC_NLA_MON],
2894 						  tipc_nl_monitor_policy,
2895 						  NULL);
2896 		if (err)
2897 			return err;
2898 
2899 		if (!mon[TIPC_NLA_MON_REF])
2900 			return -EINVAL;
2901 
2902 		bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2903 
2904 		if (bearer_id >= MAX_BEARERS)
2905 			return -EINVAL;
2906 	}
2907 
2908 	if (done)
2909 		return 0;
2910 
2911 	msg.skb = skb;
2912 	msg.portid = NETLINK_CB(cb->skb).portid;
2913 	msg.seq = cb->nlh->nlmsg_seq;
2914 
2915 	rtnl_lock();
2916 	err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2917 	if (!err)
2918 		done = 1;
2919 
2920 	rtnl_unlock();
2921 	cb->args[0] = done;
2922 	cb->args[1] = prev_node;
2923 	cb->args[2] = bearer_id;
2924 
2925 	return skb->len;
2926 }
2927 
2928 #ifdef CONFIG_TIPC_CRYPTO
2929 static int tipc_nl_retrieve_key(struct nlattr **attrs,
2930 				struct tipc_aead_key **pkey)
2931 {
2932 	struct nlattr *attr = attrs[TIPC_NLA_NODE_KEY];
2933 	struct tipc_aead_key *key;
2934 
2935 	if (!attr)
2936 		return -ENODATA;
2937 
2938 	if (nla_len(attr) < sizeof(*key))
2939 		return -EINVAL;
2940 	key = (struct tipc_aead_key *)nla_data(attr);
2941 	if (key->keylen > TIPC_AEAD_KEYLEN_MAX ||
2942 	    nla_len(attr) < tipc_aead_key_size(key))
2943 		return -EINVAL;
2944 
2945 	*pkey = key;
2946 	return 0;
2947 }
2948 
2949 static int tipc_nl_retrieve_nodeid(struct nlattr **attrs, u8 **node_id)
2950 {
2951 	struct nlattr *attr = attrs[TIPC_NLA_NODE_ID];
2952 
2953 	if (!attr)
2954 		return -ENODATA;
2955 
2956 	if (nla_len(attr) < TIPC_NODEID_LEN)
2957 		return -EINVAL;
2958 
2959 	*node_id = (u8 *)nla_data(attr);
2960 	return 0;
2961 }
2962 
2963 static int tipc_nl_retrieve_rekeying(struct nlattr **attrs, u32 *intv)
2964 {
2965 	struct nlattr *attr = attrs[TIPC_NLA_NODE_REKEYING];
2966 
2967 	if (!attr)
2968 		return -ENODATA;
2969 
2970 	*intv = nla_get_u32(attr);
2971 	return 0;
2972 }
2973 
2974 static int __tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
2975 {
2976 	struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1];
2977 	struct net *net = sock_net(skb->sk);
2978 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx, *c = tx;
2979 	struct tipc_node *n = NULL;
2980 	struct tipc_aead_key *ukey;
2981 	bool rekeying = true, master_key = false;
2982 	u8 *id, *own_id, mode;
2983 	u32 intv = 0;
2984 	int rc = 0;
2985 
2986 	if (!info->attrs[TIPC_NLA_NODE])
2987 		return -EINVAL;
2988 
2989 	rc = nla_parse_nested(attrs, TIPC_NLA_NODE_MAX,
2990 			      info->attrs[TIPC_NLA_NODE],
2991 			      tipc_nl_node_policy, info->extack);
2992 	if (rc)
2993 		return rc;
2994 
2995 	own_id = tipc_own_id(net);
2996 	if (!own_id) {
2997 		GENL_SET_ERR_MSG(info, "not found own node identity (set id?)");
2998 		return -EPERM;
2999 	}
3000 
3001 	rc = tipc_nl_retrieve_rekeying(attrs, &intv);
3002 	if (rc == -ENODATA)
3003 		rekeying = false;
3004 
3005 	rc = tipc_nl_retrieve_key(attrs, &ukey);
3006 	if (rc == -ENODATA && rekeying)
3007 		goto rekeying;
3008 	else if (rc)
3009 		return rc;
3010 
3011 	rc = tipc_aead_key_validate(ukey, info);
3012 	if (rc)
3013 		return rc;
3014 
3015 	rc = tipc_nl_retrieve_nodeid(attrs, &id);
3016 	switch (rc) {
3017 	case -ENODATA:
3018 		mode = CLUSTER_KEY;
3019 		master_key = !!(attrs[TIPC_NLA_NODE_KEY_MASTER]);
3020 		break;
3021 	case 0:
3022 		mode = PER_NODE_KEY;
3023 		if (memcmp(id, own_id, NODE_ID_LEN)) {
3024 			n = tipc_node_find_by_id(net, id) ?:
3025 				tipc_node_create(net, 0, id, 0xffffu, 0, true);
3026 			if (unlikely(!n))
3027 				return -ENOMEM;
3028 			c = n->crypto_rx;
3029 		}
3030 		break;
3031 	default:
3032 		return rc;
3033 	}
3034 
3035 	/* Initiate the TX/RX key */
3036 	rc = tipc_crypto_key_init(c, ukey, mode, master_key);
3037 	if (n)
3038 		tipc_node_put(n);
3039 
3040 	if (unlikely(rc < 0)) {
3041 		GENL_SET_ERR_MSG(info, "unable to initiate or attach new key");
3042 		return rc;
3043 	} else if (c == tx) {
3044 		/* Distribute TX key but not master one */
3045 		if (!master_key && tipc_crypto_key_distr(tx, rc, NULL))
3046 			GENL_SET_ERR_MSG(info, "failed to replicate new key");
3047 rekeying:
3048 		/* Schedule TX rekeying if needed */
3049 		tipc_crypto_rekeying_sched(tx, rekeying, intv);
3050 	}
3051 
3052 	return 0;
3053 }
3054 
3055 int tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
3056 {
3057 	int err;
3058 
3059 	rtnl_lock();
3060 	err = __tipc_nl_node_set_key(skb, info);
3061 	rtnl_unlock();
3062 
3063 	return err;
3064 }
3065 
3066 static int __tipc_nl_node_flush_key(struct sk_buff *skb,
3067 				    struct genl_info *info)
3068 {
3069 	struct net *net = sock_net(skb->sk);
3070 	struct tipc_net *tn = tipc_net(net);
3071 	struct tipc_node *n;
3072 
3073 	tipc_crypto_key_flush(tn->crypto_tx);
3074 	rcu_read_lock();
3075 	list_for_each_entry_rcu(n, &tn->node_list, list)
3076 		tipc_crypto_key_flush(n->crypto_rx);
3077 	rcu_read_unlock();
3078 
3079 	return 0;
3080 }
3081 
3082 int tipc_nl_node_flush_key(struct sk_buff *skb, struct genl_info *info)
3083 {
3084 	int err;
3085 
3086 	rtnl_lock();
3087 	err = __tipc_nl_node_flush_key(skb, info);
3088 	rtnl_unlock();
3089 
3090 	return err;
3091 }
3092 #endif
3093 
3094 /**
3095  * tipc_node_dump - dump TIPC node data
3096  * @n: tipc node to be dumped
3097  * @more: dump more?
3098  *        - false: dump only tipc node data
3099  *        - true: dump node link data as well
3100  * @buf: returned buffer of dump data in format
3101  */
3102 int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
3103 {
3104 	int i = 0;
3105 	size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
3106 
3107 	if (!n) {
3108 		i += scnprintf(buf, sz, "node data: (null)\n");
3109 		return i;
3110 	}
3111 
3112 	i += scnprintf(buf, sz, "node data: %x", n->addr);
3113 	i += scnprintf(buf + i, sz - i, " %x", n->state);
3114 	i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
3115 	i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
3116 	i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
3117 	i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
3118 	i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
3119 	i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
3120 	i += scnprintf(buf + i, sz - i, " %u", n->working_links);
3121 	i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
3122 	i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
3123 
3124 	if (!more)
3125 		return i;
3126 
3127 	i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
3128 	i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
3129 	i += scnprintf(buf + i, sz - i, " media: ");
3130 	i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
3131 	i += scnprintf(buf + i, sz - i, "\n");
3132 	i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
3133 	i += scnprintf(buf + i, sz - i, " inputq: ");
3134 	i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
3135 
3136 	i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
3137 	i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
3138 	i += scnprintf(buf + i, sz - i, " media: ");
3139 	i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
3140 	i += scnprintf(buf + i, sz - i, "\n");
3141 	i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
3142 	i += scnprintf(buf + i, sz - i, " inputq: ");
3143 	i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
3144 
3145 	i += scnprintf(buf + i, sz - i, "bclink:\n ");
3146 	i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
3147 
3148 	return i;
3149 }
3150 
3151 void tipc_node_pre_cleanup_net(struct net *exit_net)
3152 {
3153 	struct tipc_node *n;
3154 	struct tipc_net *tn;
3155 	struct net *tmp;
3156 
3157 	rcu_read_lock();
3158 	for_each_net_rcu(tmp) {
3159 		if (tmp == exit_net)
3160 			continue;
3161 		tn = tipc_net(tmp);
3162 		if (!tn)
3163 			continue;
3164 		spin_lock_bh(&tn->node_list_lock);
3165 		list_for_each_entry_rcu(n, &tn->node_list, list) {
3166 			if (!n->peer_net)
3167 				continue;
3168 			if (n->peer_net != exit_net)
3169 				continue;
3170 			tipc_node_write_lock(n);
3171 			n->peer_net = NULL;
3172 			n->peer_hash_mix = 0;
3173 			tipc_node_write_unlock_fast(n);
3174 			break;
3175 		}
3176 		spin_unlock_bh(&tn->node_list_lock);
3177 	}
3178 	rcu_read_unlock();
3179 }
3180