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