xref: /openbmc/linux/net/tipc/node.c (revision e1e38ea1)
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 
47 #define INVALID_NODE_SIG	0x10000
48 #define NODE_CLEANUP_AFTER	300000
49 
50 /* Flags used to take different actions according to flag type
51  * TIPC_NOTIFY_NODE_DOWN: notify node is down
52  * TIPC_NOTIFY_NODE_UP: notify node is up
53  * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
54  */
55 enum {
56 	TIPC_NOTIFY_NODE_DOWN		= (1 << 3),
57 	TIPC_NOTIFY_NODE_UP		= (1 << 4),
58 	TIPC_NOTIFY_LINK_UP		= (1 << 6),
59 	TIPC_NOTIFY_LINK_DOWN		= (1 << 7)
60 };
61 
62 struct tipc_link_entry {
63 	struct tipc_link *link;
64 	spinlock_t lock; /* per link */
65 	u32 mtu;
66 	struct sk_buff_head inputq;
67 	struct tipc_media_addr maddr;
68 };
69 
70 struct tipc_bclink_entry {
71 	struct tipc_link *link;
72 	struct sk_buff_head inputq1;
73 	struct sk_buff_head arrvq;
74 	struct sk_buff_head inputq2;
75 	struct sk_buff_head namedq;
76 };
77 
78 /**
79  * struct tipc_node - TIPC node structure
80  * @addr: network address of node
81  * @ref: reference counter to node object
82  * @lock: rwlock governing access to structure
83  * @net: the applicable net namespace
84  * @hash: links to adjacent nodes in unsorted hash chain
85  * @inputq: pointer to input queue containing messages for msg event
86  * @namedq: pointer to name table input queue with name table messages
87  * @active_links: bearer ids of active links, used as index into links[] array
88  * @links: array containing references to all links to node
89  * @action_flags: bit mask of different types of node actions
90  * @state: connectivity state vs peer node
91  * @sync_point: sequence number where synch/failover is finished
92  * @list: links to adjacent nodes in sorted list of cluster's nodes
93  * @working_links: number of working links to node (both active and standby)
94  * @link_cnt: number of links to node
95  * @capabilities: bitmap, indicating peer node's functional capabilities
96  * @signature: node instance identifier
97  * @link_id: local and remote bearer ids of changing link, if any
98  * @publ_list: list of publications
99  * @rcu: rcu struct for tipc_node
100  * @delete_at: indicates the time for deleting a down node
101  */
102 struct tipc_node {
103 	u32 addr;
104 	struct kref kref;
105 	rwlock_t lock;
106 	struct net *net;
107 	struct hlist_node hash;
108 	int active_links[2];
109 	struct tipc_link_entry links[MAX_BEARERS];
110 	struct tipc_bclink_entry bc_entry;
111 	int action_flags;
112 	struct list_head list;
113 	int state;
114 	u16 sync_point;
115 	int link_cnt;
116 	u16 working_links;
117 	u16 capabilities;
118 	u32 signature;
119 	u32 link_id;
120 	u8 peer_id[16];
121 	struct list_head publ_list;
122 	struct list_head conn_sks;
123 	unsigned long keepalive_intv;
124 	struct timer_list timer;
125 	struct rcu_head rcu;
126 	unsigned long delete_at;
127 };
128 
129 /* Node FSM states and events:
130  */
131 enum {
132 	SELF_DOWN_PEER_DOWN    = 0xdd,
133 	SELF_UP_PEER_UP        = 0xaa,
134 	SELF_DOWN_PEER_LEAVING = 0xd1,
135 	SELF_UP_PEER_COMING    = 0xac,
136 	SELF_COMING_PEER_UP    = 0xca,
137 	SELF_LEAVING_PEER_DOWN = 0x1d,
138 	NODE_FAILINGOVER       = 0xf0,
139 	NODE_SYNCHING          = 0xcc
140 };
141 
142 enum {
143 	SELF_ESTABL_CONTACT_EVT = 0xece,
144 	SELF_LOST_CONTACT_EVT   = 0x1ce,
145 	PEER_ESTABL_CONTACT_EVT = 0x9ece,
146 	PEER_LOST_CONTACT_EVT   = 0x91ce,
147 	NODE_FAILOVER_BEGIN_EVT = 0xfbe,
148 	NODE_FAILOVER_END_EVT   = 0xfee,
149 	NODE_SYNCH_BEGIN_EVT    = 0xcbe,
150 	NODE_SYNCH_END_EVT      = 0xcee
151 };
152 
153 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
154 				  struct sk_buff_head *xmitq,
155 				  struct tipc_media_addr **maddr);
156 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
157 				bool delete);
158 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
159 static void tipc_node_delete(struct tipc_node *node);
160 static void tipc_node_timeout(struct timer_list *t);
161 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
162 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
163 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
164 static void tipc_node_put(struct tipc_node *node);
165 static bool node_is_up(struct tipc_node *n);
166 static void tipc_node_delete_from_list(struct tipc_node *node);
167 
168 struct tipc_sock_conn {
169 	u32 port;
170 	u32 peer_port;
171 	u32 peer_node;
172 	struct list_head list;
173 };
174 
175 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
176 {
177 	int bearer_id = n->active_links[sel & 1];
178 
179 	if (unlikely(bearer_id == INVALID_BEARER_ID))
180 		return NULL;
181 
182 	return n->links[bearer_id].link;
183 }
184 
185 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
186 {
187 	struct tipc_node *n;
188 	int bearer_id;
189 	unsigned int mtu = MAX_MSG_SIZE;
190 
191 	n = tipc_node_find(net, addr);
192 	if (unlikely(!n))
193 		return mtu;
194 
195 	bearer_id = n->active_links[sel & 1];
196 	if (likely(bearer_id != INVALID_BEARER_ID))
197 		mtu = n->links[bearer_id].mtu;
198 	tipc_node_put(n);
199 	return mtu;
200 }
201 
202 bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
203 {
204 	u8 *own_id = tipc_own_id(net);
205 	struct tipc_node *n;
206 
207 	if (!own_id)
208 		return true;
209 
210 	if (addr == tipc_own_addr(net)) {
211 		memcpy(id, own_id, TIPC_NODEID_LEN);
212 		return true;
213 	}
214 	n = tipc_node_find(net, addr);
215 	if (!n)
216 		return false;
217 
218 	memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
219 	tipc_node_put(n);
220 	return true;
221 }
222 
223 u16 tipc_node_get_capabilities(struct net *net, u32 addr)
224 {
225 	struct tipc_node *n;
226 	u16 caps;
227 
228 	n = tipc_node_find(net, addr);
229 	if (unlikely(!n))
230 		return TIPC_NODE_CAPABILITIES;
231 	caps = n->capabilities;
232 	tipc_node_put(n);
233 	return caps;
234 }
235 
236 static void tipc_node_kref_release(struct kref *kref)
237 {
238 	struct tipc_node *n = container_of(kref, struct tipc_node, kref);
239 
240 	kfree(n->bc_entry.link);
241 	kfree_rcu(n, rcu);
242 }
243 
244 static void tipc_node_put(struct tipc_node *node)
245 {
246 	kref_put(&node->kref, tipc_node_kref_release);
247 }
248 
249 static void tipc_node_get(struct tipc_node *node)
250 {
251 	kref_get(&node->kref);
252 }
253 
254 /*
255  * tipc_node_find - locate specified node object, if it exists
256  */
257 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
258 {
259 	struct tipc_net *tn = tipc_net(net);
260 	struct tipc_node *node;
261 	unsigned int thash = tipc_hashfn(addr);
262 
263 	rcu_read_lock();
264 	hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
265 		if (node->addr != addr)
266 			continue;
267 		if (!kref_get_unless_zero(&node->kref))
268 			node = NULL;
269 		break;
270 	}
271 	rcu_read_unlock();
272 	return node;
273 }
274 
275 /* tipc_node_find_by_id - locate specified node object by its 128-bit id
276  * Note: this function is called only when a discovery request failed
277  * to find the node by its 32-bit id, and is not time critical
278  */
279 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
280 {
281 	struct tipc_net *tn = tipc_net(net);
282 	struct tipc_node *n;
283 	bool found = false;
284 
285 	rcu_read_lock();
286 	list_for_each_entry_rcu(n, &tn->node_list, list) {
287 		read_lock_bh(&n->lock);
288 		if (!memcmp(id, n->peer_id, 16) &&
289 		    kref_get_unless_zero(&n->kref))
290 			found = true;
291 		read_unlock_bh(&n->lock);
292 		if (found)
293 			break;
294 	}
295 	rcu_read_unlock();
296 	return found ? n : NULL;
297 }
298 
299 static void tipc_node_read_lock(struct tipc_node *n)
300 {
301 	read_lock_bh(&n->lock);
302 }
303 
304 static void tipc_node_read_unlock(struct tipc_node *n)
305 {
306 	read_unlock_bh(&n->lock);
307 }
308 
309 static void tipc_node_write_lock(struct tipc_node *n)
310 {
311 	write_lock_bh(&n->lock);
312 }
313 
314 static void tipc_node_write_unlock_fast(struct tipc_node *n)
315 {
316 	write_unlock_bh(&n->lock);
317 }
318 
319 static void tipc_node_write_unlock(struct tipc_node *n)
320 {
321 	struct net *net = n->net;
322 	u32 addr = 0;
323 	u32 flags = n->action_flags;
324 	u32 link_id = 0;
325 	u32 bearer_id;
326 	struct list_head *publ_list;
327 
328 	if (likely(!flags)) {
329 		write_unlock_bh(&n->lock);
330 		return;
331 	}
332 
333 	addr = n->addr;
334 	link_id = n->link_id;
335 	bearer_id = link_id & 0xffff;
336 	publ_list = &n->publ_list;
337 
338 	n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
339 			     TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
340 
341 	write_unlock_bh(&n->lock);
342 
343 	if (flags & TIPC_NOTIFY_NODE_DOWN)
344 		tipc_publ_notify(net, publ_list, addr);
345 
346 	if (flags & TIPC_NOTIFY_NODE_UP)
347 		tipc_named_node_up(net, addr);
348 
349 	if (flags & TIPC_NOTIFY_LINK_UP) {
350 		tipc_mon_peer_up(net, addr, bearer_id);
351 		tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
352 				     TIPC_NODE_SCOPE, link_id, link_id);
353 	}
354 	if (flags & TIPC_NOTIFY_LINK_DOWN) {
355 		tipc_mon_peer_down(net, addr, bearer_id);
356 		tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
357 				      addr, link_id);
358 	}
359 }
360 
361 static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
362 					  u8 *peer_id, u16 capabilities)
363 {
364 	struct tipc_net *tn = net_generic(net, tipc_net_id);
365 	struct tipc_node *n, *temp_node;
366 	struct tipc_link *l;
367 	int bearer_id;
368 	int i;
369 
370 	spin_lock_bh(&tn->node_list_lock);
371 	n = tipc_node_find(net, addr);
372 	if (n) {
373 		if (n->capabilities == capabilities)
374 			goto exit;
375 		/* Same node may come back with new capabilities */
376 		write_lock_bh(&n->lock);
377 		n->capabilities = capabilities;
378 		for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
379 			l = n->links[bearer_id].link;
380 			if (l)
381 				tipc_link_update_caps(l, capabilities);
382 		}
383 		write_unlock_bh(&n->lock);
384 		goto exit;
385 	}
386 	n = kzalloc(sizeof(*n), GFP_ATOMIC);
387 	if (!n) {
388 		pr_warn("Node creation failed, no memory\n");
389 		goto exit;
390 	}
391 	n->addr = addr;
392 	memcpy(&n->peer_id, peer_id, 16);
393 	n->net = net;
394 	n->capabilities = capabilities;
395 	kref_init(&n->kref);
396 	rwlock_init(&n->lock);
397 	INIT_HLIST_NODE(&n->hash);
398 	INIT_LIST_HEAD(&n->list);
399 	INIT_LIST_HEAD(&n->publ_list);
400 	INIT_LIST_HEAD(&n->conn_sks);
401 	skb_queue_head_init(&n->bc_entry.namedq);
402 	skb_queue_head_init(&n->bc_entry.inputq1);
403 	__skb_queue_head_init(&n->bc_entry.arrvq);
404 	skb_queue_head_init(&n->bc_entry.inputq2);
405 	for (i = 0; i < MAX_BEARERS; i++)
406 		spin_lock_init(&n->links[i].lock);
407 	n->state = SELF_DOWN_PEER_LEAVING;
408 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
409 	n->signature = INVALID_NODE_SIG;
410 	n->active_links[0] = INVALID_BEARER_ID;
411 	n->active_links[1] = INVALID_BEARER_ID;
412 	if (!tipc_link_bc_create(net, tipc_own_addr(net),
413 				 addr, U16_MAX,
414 				 tipc_link_window(tipc_bc_sndlink(net)),
415 				 n->capabilities,
416 				 &n->bc_entry.inputq1,
417 				 &n->bc_entry.namedq,
418 				 tipc_bc_sndlink(net),
419 				 &n->bc_entry.link)) {
420 		pr_warn("Broadcast rcv link creation failed, no memory\n");
421 		kfree(n);
422 		n = NULL;
423 		goto exit;
424 	}
425 	tipc_node_get(n);
426 	timer_setup(&n->timer, tipc_node_timeout, 0);
427 	n->keepalive_intv = U32_MAX;
428 	hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
429 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
430 		if (n->addr < temp_node->addr)
431 			break;
432 	}
433 	list_add_tail_rcu(&n->list, &temp_node->list);
434 exit:
435 	spin_unlock_bh(&tn->node_list_lock);
436 	return n;
437 }
438 
439 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
440 {
441 	unsigned long tol = tipc_link_tolerance(l);
442 	unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
443 
444 	/* Link with lowest tolerance determines timer interval */
445 	if (intv < n->keepalive_intv)
446 		n->keepalive_intv = intv;
447 
448 	/* Ensure link's abort limit corresponds to current tolerance */
449 	tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
450 }
451 
452 static void tipc_node_delete_from_list(struct tipc_node *node)
453 {
454 	list_del_rcu(&node->list);
455 	hlist_del_rcu(&node->hash);
456 	tipc_node_put(node);
457 }
458 
459 static void tipc_node_delete(struct tipc_node *node)
460 {
461 	tipc_node_delete_from_list(node);
462 
463 	del_timer_sync(&node->timer);
464 	tipc_node_put(node);
465 }
466 
467 void tipc_node_stop(struct net *net)
468 {
469 	struct tipc_net *tn = tipc_net(net);
470 	struct tipc_node *node, *t_node;
471 
472 	spin_lock_bh(&tn->node_list_lock);
473 	list_for_each_entry_safe(node, t_node, &tn->node_list, list)
474 		tipc_node_delete(node);
475 	spin_unlock_bh(&tn->node_list_lock);
476 }
477 
478 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
479 {
480 	struct tipc_node *n;
481 
482 	if (in_own_node(net, addr))
483 		return;
484 
485 	n = tipc_node_find(net, addr);
486 	if (!n) {
487 		pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
488 		return;
489 	}
490 	tipc_node_write_lock(n);
491 	list_add_tail(subscr, &n->publ_list);
492 	tipc_node_write_unlock_fast(n);
493 	tipc_node_put(n);
494 }
495 
496 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
497 {
498 	struct tipc_node *n;
499 
500 	if (in_own_node(net, addr))
501 		return;
502 
503 	n = tipc_node_find(net, addr);
504 	if (!n) {
505 		pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
506 		return;
507 	}
508 	tipc_node_write_lock(n);
509 	list_del_init(subscr);
510 	tipc_node_write_unlock_fast(n);
511 	tipc_node_put(n);
512 }
513 
514 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
515 {
516 	struct tipc_node *node;
517 	struct tipc_sock_conn *conn;
518 	int err = 0;
519 
520 	if (in_own_node(net, dnode))
521 		return 0;
522 
523 	node = tipc_node_find(net, dnode);
524 	if (!node) {
525 		pr_warn("Connecting sock to node 0x%x failed\n", dnode);
526 		return -EHOSTUNREACH;
527 	}
528 	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
529 	if (!conn) {
530 		err = -EHOSTUNREACH;
531 		goto exit;
532 	}
533 	conn->peer_node = dnode;
534 	conn->port = port;
535 	conn->peer_port = peer_port;
536 
537 	tipc_node_write_lock(node);
538 	list_add_tail(&conn->list, &node->conn_sks);
539 	tipc_node_write_unlock(node);
540 exit:
541 	tipc_node_put(node);
542 	return err;
543 }
544 
545 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
546 {
547 	struct tipc_node *node;
548 	struct tipc_sock_conn *conn, *safe;
549 
550 	if (in_own_node(net, dnode))
551 		return;
552 
553 	node = tipc_node_find(net, dnode);
554 	if (!node)
555 		return;
556 
557 	tipc_node_write_lock(node);
558 	list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
559 		if (port != conn->port)
560 			continue;
561 		list_del(&conn->list);
562 		kfree(conn);
563 	}
564 	tipc_node_write_unlock(node);
565 	tipc_node_put(node);
566 }
567 
568 static void  tipc_node_clear_links(struct tipc_node *node)
569 {
570 	int i;
571 
572 	for (i = 0; i < MAX_BEARERS; i++) {
573 		struct tipc_link_entry *le = &node->links[i];
574 
575 		if (le->link) {
576 			kfree(le->link);
577 			le->link = NULL;
578 			node->link_cnt--;
579 		}
580 	}
581 }
582 
583 /* tipc_node_cleanup - delete nodes that does not
584  * have active links for NODE_CLEANUP_AFTER time
585  */
586 static int tipc_node_cleanup(struct tipc_node *peer)
587 {
588 	struct tipc_net *tn = tipc_net(peer->net);
589 	bool deleted = false;
590 
591 	spin_lock_bh(&tn->node_list_lock);
592 	tipc_node_write_lock(peer);
593 
594 	if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
595 		tipc_node_clear_links(peer);
596 		tipc_node_delete_from_list(peer);
597 		deleted = true;
598 	}
599 	tipc_node_write_unlock(peer);
600 	spin_unlock_bh(&tn->node_list_lock);
601 	return deleted;
602 }
603 
604 /* tipc_node_timeout - handle expiration of node timer
605  */
606 static void tipc_node_timeout(struct timer_list *t)
607 {
608 	struct tipc_node *n = from_timer(n, t, timer);
609 	struct tipc_link_entry *le;
610 	struct sk_buff_head xmitq;
611 	int remains = n->link_cnt;
612 	int bearer_id;
613 	int rc = 0;
614 
615 	if (!node_is_up(n) && tipc_node_cleanup(n)) {
616 		/*Removing the reference of Timer*/
617 		tipc_node_put(n);
618 		return;
619 	}
620 
621 	__skb_queue_head_init(&xmitq);
622 
623 	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
624 		tipc_node_read_lock(n);
625 		le = &n->links[bearer_id];
626 		if (le->link) {
627 			spin_lock_bh(&le->lock);
628 			/* Link tolerance may change asynchronously: */
629 			tipc_node_calculate_timer(n, le->link);
630 			rc = tipc_link_timeout(le->link, &xmitq);
631 			spin_unlock_bh(&le->lock);
632 			remains--;
633 		}
634 		tipc_node_read_unlock(n);
635 		tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
636 		if (rc & TIPC_LINK_DOWN_EVT)
637 			tipc_node_link_down(n, bearer_id, false);
638 	}
639 	mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
640 }
641 
642 /**
643  * __tipc_node_link_up - handle addition of link
644  * Node lock must be held by caller
645  * Link becomes active (alone or shared) or standby, depending on its priority.
646  */
647 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
648 				struct sk_buff_head *xmitq)
649 {
650 	int *slot0 = &n->active_links[0];
651 	int *slot1 = &n->active_links[1];
652 	struct tipc_link *ol = node_active_link(n, 0);
653 	struct tipc_link *nl = n->links[bearer_id].link;
654 
655 	if (!nl || tipc_link_is_up(nl))
656 		return;
657 
658 	tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
659 	if (!tipc_link_is_up(nl))
660 		return;
661 
662 	n->working_links++;
663 	n->action_flags |= TIPC_NOTIFY_LINK_UP;
664 	n->link_id = tipc_link_id(nl);
665 
666 	/* Leave room for tunnel header when returning 'mtu' to users: */
667 	n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
668 
669 	tipc_bearer_add_dest(n->net, bearer_id, n->addr);
670 	tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
671 
672 	pr_debug("Established link <%s> on network plane %c\n",
673 		 tipc_link_name(nl), tipc_link_plane(nl));
674 
675 	/* Ensure that a STATE message goes first */
676 	tipc_link_build_state_msg(nl, xmitq);
677 
678 	/* First link? => give it both slots */
679 	if (!ol) {
680 		*slot0 = bearer_id;
681 		*slot1 = bearer_id;
682 		tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
683 		n->action_flags |= TIPC_NOTIFY_NODE_UP;
684 		tipc_link_set_active(nl, true);
685 		tipc_bcast_add_peer(n->net, nl, xmitq);
686 		return;
687 	}
688 
689 	/* Second link => redistribute slots */
690 	if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
691 		pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
692 		*slot0 = bearer_id;
693 		*slot1 = bearer_id;
694 		tipc_link_set_active(nl, true);
695 		tipc_link_set_active(ol, false);
696 	} else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
697 		tipc_link_set_active(nl, true);
698 		*slot1 = bearer_id;
699 	} else {
700 		pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
701 	}
702 
703 	/* Prepare synchronization with first link */
704 	tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
705 }
706 
707 /**
708  * tipc_node_link_up - handle addition of link
709  *
710  * Link becomes active (alone or shared) or standby, depending on its priority.
711  */
712 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
713 			      struct sk_buff_head *xmitq)
714 {
715 	struct tipc_media_addr *maddr;
716 
717 	tipc_node_write_lock(n);
718 	__tipc_node_link_up(n, bearer_id, xmitq);
719 	maddr = &n->links[bearer_id].maddr;
720 	tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
721 	tipc_node_write_unlock(n);
722 }
723 
724 /**
725  * __tipc_node_link_down - handle loss of link
726  */
727 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
728 				  struct sk_buff_head *xmitq,
729 				  struct tipc_media_addr **maddr)
730 {
731 	struct tipc_link_entry *le = &n->links[*bearer_id];
732 	int *slot0 = &n->active_links[0];
733 	int *slot1 = &n->active_links[1];
734 	int i, highest = 0, prio;
735 	struct tipc_link *l, *_l, *tnl;
736 
737 	l = n->links[*bearer_id].link;
738 	if (!l || tipc_link_is_reset(l))
739 		return;
740 
741 	n->working_links--;
742 	n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
743 	n->link_id = tipc_link_id(l);
744 
745 	tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
746 
747 	pr_debug("Lost link <%s> on network plane %c\n",
748 		 tipc_link_name(l), tipc_link_plane(l));
749 
750 	/* Select new active link if any available */
751 	*slot0 = INVALID_BEARER_ID;
752 	*slot1 = INVALID_BEARER_ID;
753 	for (i = 0; i < MAX_BEARERS; i++) {
754 		_l = n->links[i].link;
755 		if (!_l || !tipc_link_is_up(_l))
756 			continue;
757 		if (_l == l)
758 			continue;
759 		prio = tipc_link_prio(_l);
760 		if (prio < highest)
761 			continue;
762 		if (prio > highest) {
763 			highest = prio;
764 			*slot0 = i;
765 			*slot1 = i;
766 			continue;
767 		}
768 		*slot1 = i;
769 	}
770 
771 	if (!node_is_up(n)) {
772 		if (tipc_link_peer_is_down(l))
773 			tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
774 		tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
775 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
776 		tipc_link_reset(l);
777 		tipc_link_build_reset_msg(l, xmitq);
778 		*maddr = &n->links[*bearer_id].maddr;
779 		node_lost_contact(n, &le->inputq);
780 		tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
781 		return;
782 	}
783 	tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
784 
785 	/* There is still a working link => initiate failover */
786 	*bearer_id = n->active_links[0];
787 	tnl = n->links[*bearer_id].link;
788 	tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
789 	tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
790 	n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
791 	tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
792 	tipc_link_reset(l);
793 	tipc_link_fsm_evt(l, LINK_RESET_EVT);
794 	tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
795 	tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
796 	*maddr = &n->links[*bearer_id].maddr;
797 }
798 
799 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
800 {
801 	struct tipc_link_entry *le = &n->links[bearer_id];
802 	struct tipc_link *l = le->link;
803 	struct tipc_media_addr *maddr;
804 	struct sk_buff_head xmitq;
805 	int old_bearer_id = bearer_id;
806 
807 	if (!l)
808 		return;
809 
810 	__skb_queue_head_init(&xmitq);
811 
812 	tipc_node_write_lock(n);
813 	if (!tipc_link_is_establishing(l)) {
814 		__tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
815 		if (delete) {
816 			kfree(l);
817 			le->link = NULL;
818 			n->link_cnt--;
819 		}
820 	} else {
821 		/* Defuse pending tipc_node_link_up() */
822 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
823 	}
824 	tipc_node_write_unlock(n);
825 	if (delete)
826 		tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
827 	tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
828 	tipc_sk_rcv(n->net, &le->inputq);
829 }
830 
831 static bool node_is_up(struct tipc_node *n)
832 {
833 	return n->active_links[0] != INVALID_BEARER_ID;
834 }
835 
836 bool tipc_node_is_up(struct net *net, u32 addr)
837 {
838 	struct tipc_node *n;
839 	bool retval = false;
840 
841 	if (in_own_node(net, addr))
842 		return true;
843 
844 	n = tipc_node_find(net, addr);
845 	if (!n)
846 		return false;
847 	retval = node_is_up(n);
848 	tipc_node_put(n);
849 	return retval;
850 }
851 
852 static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
853 {
854 	struct tipc_node *n;
855 
856 	addr ^= tipc_net(net)->random;
857 	while ((n = tipc_node_find(net, addr))) {
858 		tipc_node_put(n);
859 		addr++;
860 	}
861 	return addr;
862 }
863 
864 /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
865  * Returns suggested address if any, otherwise 0
866  */
867 u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
868 {
869 	struct tipc_net *tn = tipc_net(net);
870 	struct tipc_node *n;
871 
872 	/* Suggest new address if some other peer is using this one */
873 	n = tipc_node_find(net, addr);
874 	if (n) {
875 		if (!memcmp(n->peer_id, id, NODE_ID_LEN))
876 			addr = 0;
877 		tipc_node_put(n);
878 		if (!addr)
879 			return 0;
880 		return tipc_node_suggest_addr(net, addr);
881 	}
882 
883 	/* Suggest previously used address if peer is known */
884 	n = tipc_node_find_by_id(net, id);
885 	if (n) {
886 		addr = n->addr;
887 		tipc_node_put(n);
888 		return addr;
889 	}
890 
891 	/* Even this node may be in conflict */
892 	if (tn->trial_addr == addr)
893 		return tipc_node_suggest_addr(net, addr);
894 
895 	return 0;
896 }
897 
898 void tipc_node_check_dest(struct net *net, u32 addr,
899 			  u8 *peer_id, struct tipc_bearer *b,
900 			  u16 capabilities, u32 signature,
901 			  struct tipc_media_addr *maddr,
902 			  bool *respond, bool *dupl_addr)
903 {
904 	struct tipc_node *n;
905 	struct tipc_link *l;
906 	struct tipc_link_entry *le;
907 	bool addr_match = false;
908 	bool sign_match = false;
909 	bool link_up = false;
910 	bool accept_addr = false;
911 	bool reset = true;
912 	char *if_name;
913 	unsigned long intv;
914 
915 	*dupl_addr = false;
916 	*respond = false;
917 
918 	n = tipc_node_create(net, addr, peer_id, capabilities);
919 	if (!n)
920 		return;
921 
922 	tipc_node_write_lock(n);
923 
924 	le = &n->links[b->identity];
925 
926 	/* Prepare to validate requesting node's signature and media address */
927 	l = le->link;
928 	link_up = l && tipc_link_is_up(l);
929 	addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
930 	sign_match = (signature == n->signature);
931 
932 	/* These three flags give us eight permutations: */
933 
934 	if (sign_match && addr_match && link_up) {
935 		/* All is fine. Do nothing. */
936 		reset = false;
937 	} else if (sign_match && addr_match && !link_up) {
938 		/* Respond. The link will come up in due time */
939 		*respond = true;
940 	} else if (sign_match && !addr_match && link_up) {
941 		/* Peer has changed i/f address without rebooting.
942 		 * If so, the link will reset soon, and the next
943 		 * discovery will be accepted. So we can ignore it.
944 		 * It may also be an cloned or malicious peer having
945 		 * chosen the same node address and signature as an
946 		 * existing one.
947 		 * Ignore requests until the link goes down, if ever.
948 		 */
949 		*dupl_addr = true;
950 	} else if (sign_match && !addr_match && !link_up) {
951 		/* Peer link has changed i/f address without rebooting.
952 		 * It may also be a cloned or malicious peer; we can't
953 		 * distinguish between the two.
954 		 * The signature is correct, so we must accept.
955 		 */
956 		accept_addr = true;
957 		*respond = true;
958 	} else if (!sign_match && addr_match && link_up) {
959 		/* Peer node rebooted. Two possibilities:
960 		 *  - Delayed re-discovery; this link endpoint has already
961 		 *    reset and re-established contact with the peer, before
962 		 *    receiving a discovery message from that node.
963 		 *    (The peer happened to receive one from this node first).
964 		 *  - The peer came back so fast that our side has not
965 		 *    discovered it yet. Probing from this side will soon
966 		 *    reset the link, since there can be no working link
967 		 *    endpoint at the peer end, and the link will re-establish.
968 		 *  Accept the signature, since it comes from a known peer.
969 		 */
970 		n->signature = signature;
971 	} else if (!sign_match && addr_match && !link_up) {
972 		/*  The peer node has rebooted.
973 		 *  Accept signature, since it is a known peer.
974 		 */
975 		n->signature = signature;
976 		*respond = true;
977 	} else if (!sign_match && !addr_match && link_up) {
978 		/* Peer rebooted with new address, or a new/duplicate peer.
979 		 * Ignore until the link goes down, if ever.
980 		 */
981 		*dupl_addr = true;
982 	} else if (!sign_match && !addr_match && !link_up) {
983 		/* Peer rebooted with new address, or it is a new peer.
984 		 * Accept signature and address.
985 		 */
986 		n->signature = signature;
987 		accept_addr = true;
988 		*respond = true;
989 	}
990 
991 	if (!accept_addr)
992 		goto exit;
993 
994 	/* Now create new link if not already existing */
995 	if (!l) {
996 		if (n->link_cnt == 2)
997 			goto exit;
998 
999 		if_name = strchr(b->name, ':') + 1;
1000 		if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
1001 				      b->net_plane, b->mtu, b->priority,
1002 				      b->window, mod(tipc_net(net)->random),
1003 				      tipc_own_addr(net), addr, peer_id,
1004 				      n->capabilities,
1005 				      tipc_bc_sndlink(n->net), n->bc_entry.link,
1006 				      &le->inputq,
1007 				      &n->bc_entry.namedq, &l)) {
1008 			*respond = false;
1009 			goto exit;
1010 		}
1011 		tipc_link_reset(l);
1012 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
1013 		if (n->state == NODE_FAILINGOVER)
1014 			tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1015 		le->link = l;
1016 		n->link_cnt++;
1017 		tipc_node_calculate_timer(n, l);
1018 		if (n->link_cnt == 1) {
1019 			intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1020 			if (!mod_timer(&n->timer, intv))
1021 				tipc_node_get(n);
1022 		}
1023 	}
1024 	memcpy(&le->maddr, maddr, sizeof(*maddr));
1025 exit:
1026 	tipc_node_write_unlock(n);
1027 	if (reset && l && !tipc_link_is_reset(l))
1028 		tipc_node_link_down(n, b->identity, false);
1029 	tipc_node_put(n);
1030 }
1031 
1032 void tipc_node_delete_links(struct net *net, int bearer_id)
1033 {
1034 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1035 	struct tipc_node *n;
1036 
1037 	rcu_read_lock();
1038 	list_for_each_entry_rcu(n, &tn->node_list, list) {
1039 		tipc_node_link_down(n, bearer_id, true);
1040 	}
1041 	rcu_read_unlock();
1042 }
1043 
1044 static void tipc_node_reset_links(struct tipc_node *n)
1045 {
1046 	int i;
1047 
1048 	pr_warn("Resetting all links to %x\n", n->addr);
1049 
1050 	for (i = 0; i < MAX_BEARERS; i++) {
1051 		tipc_node_link_down(n, i, false);
1052 	}
1053 }
1054 
1055 /* tipc_node_fsm_evt - node finite state machine
1056  * Determines when contact is allowed with peer node
1057  */
1058 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
1059 {
1060 	int state = n->state;
1061 
1062 	switch (state) {
1063 	case SELF_DOWN_PEER_DOWN:
1064 		switch (evt) {
1065 		case SELF_ESTABL_CONTACT_EVT:
1066 			state = SELF_UP_PEER_COMING;
1067 			break;
1068 		case PEER_ESTABL_CONTACT_EVT:
1069 			state = SELF_COMING_PEER_UP;
1070 			break;
1071 		case SELF_LOST_CONTACT_EVT:
1072 		case PEER_LOST_CONTACT_EVT:
1073 			break;
1074 		case NODE_SYNCH_END_EVT:
1075 		case NODE_SYNCH_BEGIN_EVT:
1076 		case NODE_FAILOVER_BEGIN_EVT:
1077 		case NODE_FAILOVER_END_EVT:
1078 		default:
1079 			goto illegal_evt;
1080 		}
1081 		break;
1082 	case SELF_UP_PEER_UP:
1083 		switch (evt) {
1084 		case SELF_LOST_CONTACT_EVT:
1085 			state = SELF_DOWN_PEER_LEAVING;
1086 			break;
1087 		case PEER_LOST_CONTACT_EVT:
1088 			state = SELF_LEAVING_PEER_DOWN;
1089 			break;
1090 		case NODE_SYNCH_BEGIN_EVT:
1091 			state = NODE_SYNCHING;
1092 			break;
1093 		case NODE_FAILOVER_BEGIN_EVT:
1094 			state = NODE_FAILINGOVER;
1095 			break;
1096 		case SELF_ESTABL_CONTACT_EVT:
1097 		case PEER_ESTABL_CONTACT_EVT:
1098 		case NODE_SYNCH_END_EVT:
1099 		case NODE_FAILOVER_END_EVT:
1100 			break;
1101 		default:
1102 			goto illegal_evt;
1103 		}
1104 		break;
1105 	case SELF_DOWN_PEER_LEAVING:
1106 		switch (evt) {
1107 		case PEER_LOST_CONTACT_EVT:
1108 			state = SELF_DOWN_PEER_DOWN;
1109 			break;
1110 		case SELF_ESTABL_CONTACT_EVT:
1111 		case PEER_ESTABL_CONTACT_EVT:
1112 		case SELF_LOST_CONTACT_EVT:
1113 			break;
1114 		case NODE_SYNCH_END_EVT:
1115 		case NODE_SYNCH_BEGIN_EVT:
1116 		case NODE_FAILOVER_BEGIN_EVT:
1117 		case NODE_FAILOVER_END_EVT:
1118 		default:
1119 			goto illegal_evt;
1120 		}
1121 		break;
1122 	case SELF_UP_PEER_COMING:
1123 		switch (evt) {
1124 		case PEER_ESTABL_CONTACT_EVT:
1125 			state = SELF_UP_PEER_UP;
1126 			break;
1127 		case SELF_LOST_CONTACT_EVT:
1128 			state = SELF_DOWN_PEER_DOWN;
1129 			break;
1130 		case SELF_ESTABL_CONTACT_EVT:
1131 		case PEER_LOST_CONTACT_EVT:
1132 		case NODE_SYNCH_END_EVT:
1133 		case NODE_FAILOVER_BEGIN_EVT:
1134 			break;
1135 		case NODE_SYNCH_BEGIN_EVT:
1136 		case NODE_FAILOVER_END_EVT:
1137 		default:
1138 			goto illegal_evt;
1139 		}
1140 		break;
1141 	case SELF_COMING_PEER_UP:
1142 		switch (evt) {
1143 		case SELF_ESTABL_CONTACT_EVT:
1144 			state = SELF_UP_PEER_UP;
1145 			break;
1146 		case PEER_LOST_CONTACT_EVT:
1147 			state = SELF_DOWN_PEER_DOWN;
1148 			break;
1149 		case SELF_LOST_CONTACT_EVT:
1150 		case PEER_ESTABL_CONTACT_EVT:
1151 			break;
1152 		case NODE_SYNCH_END_EVT:
1153 		case NODE_SYNCH_BEGIN_EVT:
1154 		case NODE_FAILOVER_BEGIN_EVT:
1155 		case NODE_FAILOVER_END_EVT:
1156 		default:
1157 			goto illegal_evt;
1158 		}
1159 		break;
1160 	case SELF_LEAVING_PEER_DOWN:
1161 		switch (evt) {
1162 		case SELF_LOST_CONTACT_EVT:
1163 			state = SELF_DOWN_PEER_DOWN;
1164 			break;
1165 		case SELF_ESTABL_CONTACT_EVT:
1166 		case PEER_ESTABL_CONTACT_EVT:
1167 		case PEER_LOST_CONTACT_EVT:
1168 			break;
1169 		case NODE_SYNCH_END_EVT:
1170 		case NODE_SYNCH_BEGIN_EVT:
1171 		case NODE_FAILOVER_BEGIN_EVT:
1172 		case NODE_FAILOVER_END_EVT:
1173 		default:
1174 			goto illegal_evt;
1175 		}
1176 		break;
1177 	case NODE_FAILINGOVER:
1178 		switch (evt) {
1179 		case SELF_LOST_CONTACT_EVT:
1180 			state = SELF_DOWN_PEER_LEAVING;
1181 			break;
1182 		case PEER_LOST_CONTACT_EVT:
1183 			state = SELF_LEAVING_PEER_DOWN;
1184 			break;
1185 		case NODE_FAILOVER_END_EVT:
1186 			state = SELF_UP_PEER_UP;
1187 			break;
1188 		case NODE_FAILOVER_BEGIN_EVT:
1189 		case SELF_ESTABL_CONTACT_EVT:
1190 		case PEER_ESTABL_CONTACT_EVT:
1191 			break;
1192 		case NODE_SYNCH_BEGIN_EVT:
1193 		case NODE_SYNCH_END_EVT:
1194 		default:
1195 			goto illegal_evt;
1196 		}
1197 		break;
1198 	case NODE_SYNCHING:
1199 		switch (evt) {
1200 		case SELF_LOST_CONTACT_EVT:
1201 			state = SELF_DOWN_PEER_LEAVING;
1202 			break;
1203 		case PEER_LOST_CONTACT_EVT:
1204 			state = SELF_LEAVING_PEER_DOWN;
1205 			break;
1206 		case NODE_SYNCH_END_EVT:
1207 			state = SELF_UP_PEER_UP;
1208 			break;
1209 		case NODE_FAILOVER_BEGIN_EVT:
1210 			state = NODE_FAILINGOVER;
1211 			break;
1212 		case NODE_SYNCH_BEGIN_EVT:
1213 		case SELF_ESTABL_CONTACT_EVT:
1214 		case PEER_ESTABL_CONTACT_EVT:
1215 			break;
1216 		case NODE_FAILOVER_END_EVT:
1217 		default:
1218 			goto illegal_evt;
1219 		}
1220 		break;
1221 	default:
1222 		pr_err("Unknown node fsm state %x\n", state);
1223 		break;
1224 	}
1225 	n->state = state;
1226 	return;
1227 
1228 illegal_evt:
1229 	pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1230 }
1231 
1232 static void node_lost_contact(struct tipc_node *n,
1233 			      struct sk_buff_head *inputq)
1234 {
1235 	struct tipc_sock_conn *conn, *safe;
1236 	struct tipc_link *l;
1237 	struct list_head *conns = &n->conn_sks;
1238 	struct sk_buff *skb;
1239 	uint i;
1240 
1241 	pr_debug("Lost contact with %x\n", n->addr);
1242 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1243 
1244 	/* Clean up broadcast state */
1245 	tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1246 
1247 	/* Abort any ongoing link failover */
1248 	for (i = 0; i < MAX_BEARERS; i++) {
1249 		l = n->links[i].link;
1250 		if (l)
1251 			tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1252 	}
1253 
1254 	/* Notify publications from this node */
1255 	n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1256 
1257 	/* Notify sockets connected to node */
1258 	list_for_each_entry_safe(conn, safe, conns, list) {
1259 		skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1260 				      SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1261 				      conn->peer_node, conn->port,
1262 				      conn->peer_port, TIPC_ERR_NO_NODE);
1263 		if (likely(skb))
1264 			skb_queue_tail(inputq, skb);
1265 		list_del(&conn->list);
1266 		kfree(conn);
1267 	}
1268 }
1269 
1270 /**
1271  * tipc_node_get_linkname - get the name of a link
1272  *
1273  * @bearer_id: id of the bearer
1274  * @node: peer node address
1275  * @linkname: link name output buffer
1276  *
1277  * Returns 0 on success
1278  */
1279 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1280 			   char *linkname, size_t len)
1281 {
1282 	struct tipc_link *link;
1283 	int err = -EINVAL;
1284 	struct tipc_node *node = tipc_node_find(net, addr);
1285 
1286 	if (!node)
1287 		return err;
1288 
1289 	if (bearer_id >= MAX_BEARERS)
1290 		goto exit;
1291 
1292 	tipc_node_read_lock(node);
1293 	link = node->links[bearer_id].link;
1294 	if (link) {
1295 		strncpy(linkname, tipc_link_name(link), len);
1296 		err = 0;
1297 	}
1298 	tipc_node_read_unlock(node);
1299 exit:
1300 	tipc_node_put(node);
1301 	return err;
1302 }
1303 
1304 /* Caller should hold node lock for the passed node */
1305 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1306 {
1307 	void *hdr;
1308 	struct nlattr *attrs;
1309 
1310 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1311 			  NLM_F_MULTI, TIPC_NL_NODE_GET);
1312 	if (!hdr)
1313 		return -EMSGSIZE;
1314 
1315 	attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1316 	if (!attrs)
1317 		goto msg_full;
1318 
1319 	if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1320 		goto attr_msg_full;
1321 	if (node_is_up(node))
1322 		if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1323 			goto attr_msg_full;
1324 
1325 	nla_nest_end(msg->skb, attrs);
1326 	genlmsg_end(msg->skb, hdr);
1327 
1328 	return 0;
1329 
1330 attr_msg_full:
1331 	nla_nest_cancel(msg->skb, attrs);
1332 msg_full:
1333 	genlmsg_cancel(msg->skb, hdr);
1334 
1335 	return -EMSGSIZE;
1336 }
1337 
1338 /**
1339  * tipc_node_xmit() is the general link level function for message sending
1340  * @net: the applicable net namespace
1341  * @list: chain of buffers containing message
1342  * @dnode: address of destination node
1343  * @selector: a number used for deterministic link selection
1344  * Consumes the buffer chain.
1345  * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1346  */
1347 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1348 		   u32 dnode, int selector)
1349 {
1350 	struct tipc_link_entry *le = NULL;
1351 	struct tipc_node *n;
1352 	struct sk_buff_head xmitq;
1353 	int bearer_id;
1354 	int rc;
1355 
1356 	if (in_own_node(net, dnode)) {
1357 		tipc_sk_rcv(net, list);
1358 		return 0;
1359 	}
1360 
1361 	n = tipc_node_find(net, dnode);
1362 	if (unlikely(!n)) {
1363 		skb_queue_purge(list);
1364 		return -EHOSTUNREACH;
1365 	}
1366 
1367 	tipc_node_read_lock(n);
1368 	bearer_id = n->active_links[selector & 1];
1369 	if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1370 		tipc_node_read_unlock(n);
1371 		tipc_node_put(n);
1372 		skb_queue_purge(list);
1373 		return -EHOSTUNREACH;
1374 	}
1375 
1376 	__skb_queue_head_init(&xmitq);
1377 	le = &n->links[bearer_id];
1378 	spin_lock_bh(&le->lock);
1379 	rc = tipc_link_xmit(le->link, list, &xmitq);
1380 	spin_unlock_bh(&le->lock);
1381 	tipc_node_read_unlock(n);
1382 
1383 	if (unlikely(rc == -ENOBUFS))
1384 		tipc_node_link_down(n, bearer_id, false);
1385 	else
1386 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1387 
1388 	tipc_node_put(n);
1389 
1390 	return rc;
1391 }
1392 
1393 /* tipc_node_xmit_skb(): send single buffer to destination
1394  * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1395  * messages, which will not be rejected
1396  * The only exception is datagram messages rerouted after secondary
1397  * lookup, which are rare and safe to dispose of anyway.
1398  */
1399 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1400 		       u32 selector)
1401 {
1402 	struct sk_buff_head head;
1403 
1404 	skb_queue_head_init(&head);
1405 	__skb_queue_tail(&head, skb);
1406 	tipc_node_xmit(net, &head, dnode, selector);
1407 	return 0;
1408 }
1409 
1410 /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1411  * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1412  */
1413 int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1414 {
1415 	struct sk_buff *skb;
1416 	u32 selector, dnode;
1417 
1418 	while ((skb = __skb_dequeue(xmitq))) {
1419 		selector = msg_origport(buf_msg(skb));
1420 		dnode = msg_destnode(buf_msg(skb));
1421 		tipc_node_xmit_skb(net, skb, dnode, selector);
1422 	}
1423 	return 0;
1424 }
1425 
1426 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1427 {
1428 	struct sk_buff *txskb;
1429 	struct tipc_node *n;
1430 	u32 dst;
1431 
1432 	rcu_read_lock();
1433 	list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1434 		dst = n->addr;
1435 		if (in_own_node(net, dst))
1436 			continue;
1437 		if (!node_is_up(n))
1438 			continue;
1439 		txskb = pskb_copy(skb, GFP_ATOMIC);
1440 		if (!txskb)
1441 			break;
1442 		msg_set_destnode(buf_msg(txskb), dst);
1443 		tipc_node_xmit_skb(net, txskb, dst, 0);
1444 	}
1445 	rcu_read_unlock();
1446 
1447 	kfree_skb(skb);
1448 }
1449 
1450 static void tipc_node_mcast_rcv(struct tipc_node *n)
1451 {
1452 	struct tipc_bclink_entry *be = &n->bc_entry;
1453 
1454 	/* 'arrvq' is under inputq2's lock protection */
1455 	spin_lock_bh(&be->inputq2.lock);
1456 	spin_lock_bh(&be->inputq1.lock);
1457 	skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1458 	spin_unlock_bh(&be->inputq1.lock);
1459 	spin_unlock_bh(&be->inputq2.lock);
1460 	tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1461 }
1462 
1463 static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1464 				  int bearer_id, struct sk_buff_head *xmitq)
1465 {
1466 	struct tipc_link *ucl;
1467 	int rc;
1468 
1469 	rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1470 
1471 	if (rc & TIPC_LINK_DOWN_EVT) {
1472 		tipc_node_reset_links(n);
1473 		return;
1474 	}
1475 
1476 	if (!(rc & TIPC_LINK_SND_STATE))
1477 		return;
1478 
1479 	/* If probe message, a STATE response will be sent anyway */
1480 	if (msg_probe(hdr))
1481 		return;
1482 
1483 	/* Produce a STATE message carrying broadcast NACK */
1484 	tipc_node_read_lock(n);
1485 	ucl = n->links[bearer_id].link;
1486 	if (ucl)
1487 		tipc_link_build_state_msg(ucl, xmitq);
1488 	tipc_node_read_unlock(n);
1489 }
1490 
1491 /**
1492  * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1493  * @net: the applicable net namespace
1494  * @skb: TIPC packet
1495  * @bearer_id: id of bearer message arrived on
1496  *
1497  * Invoked with no locks held.
1498  */
1499 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1500 {
1501 	int rc;
1502 	struct sk_buff_head xmitq;
1503 	struct tipc_bclink_entry *be;
1504 	struct tipc_link_entry *le;
1505 	struct tipc_msg *hdr = buf_msg(skb);
1506 	int usr = msg_user(hdr);
1507 	u32 dnode = msg_destnode(hdr);
1508 	struct tipc_node *n;
1509 
1510 	__skb_queue_head_init(&xmitq);
1511 
1512 	/* If NACK for other node, let rcv link for that node peek into it */
1513 	if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1514 		n = tipc_node_find(net, dnode);
1515 	else
1516 		n = tipc_node_find(net, msg_prevnode(hdr));
1517 	if (!n) {
1518 		kfree_skb(skb);
1519 		return;
1520 	}
1521 	be = &n->bc_entry;
1522 	le = &n->links[bearer_id];
1523 
1524 	rc = tipc_bcast_rcv(net, be->link, skb);
1525 
1526 	/* Broadcast ACKs are sent on a unicast link */
1527 	if (rc & TIPC_LINK_SND_STATE) {
1528 		tipc_node_read_lock(n);
1529 		tipc_link_build_state_msg(le->link, &xmitq);
1530 		tipc_node_read_unlock(n);
1531 	}
1532 
1533 	if (!skb_queue_empty(&xmitq))
1534 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1535 
1536 	if (!skb_queue_empty(&be->inputq1))
1537 		tipc_node_mcast_rcv(n);
1538 
1539 	/* If reassembly or retransmission failure => reset all links to peer */
1540 	if (rc & TIPC_LINK_DOWN_EVT)
1541 		tipc_node_reset_links(n);
1542 
1543 	tipc_node_put(n);
1544 }
1545 
1546 /**
1547  * tipc_node_check_state - check and if necessary update node state
1548  * @skb: TIPC packet
1549  * @bearer_id: identity of bearer delivering the packet
1550  * Returns true if state and msg are ok, otherwise false
1551  */
1552 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1553 				  int bearer_id, struct sk_buff_head *xmitq)
1554 {
1555 	struct tipc_msg *hdr = buf_msg(skb);
1556 	int usr = msg_user(hdr);
1557 	int mtyp = msg_type(hdr);
1558 	u16 oseqno = msg_seqno(hdr);
1559 	u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1560 	u16 exp_pkts = msg_msgcnt(hdr);
1561 	u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1562 	int state = n->state;
1563 	struct tipc_link *l, *tnl, *pl = NULL;
1564 	struct tipc_media_addr *maddr;
1565 	int pb_id;
1566 
1567 	l = n->links[bearer_id].link;
1568 	if (!l)
1569 		return false;
1570 	rcv_nxt = tipc_link_rcv_nxt(l);
1571 
1572 
1573 	if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1574 		return true;
1575 
1576 	/* Find parallel link, if any */
1577 	for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1578 		if ((pb_id != bearer_id) && n->links[pb_id].link) {
1579 			pl = n->links[pb_id].link;
1580 			break;
1581 		}
1582 	}
1583 
1584 	if (!tipc_link_validate_msg(l, hdr))
1585 		return false;
1586 
1587 	/* Check and update node accesibility if applicable */
1588 	if (state == SELF_UP_PEER_COMING) {
1589 		if (!tipc_link_is_up(l))
1590 			return true;
1591 		if (!msg_peer_link_is_up(hdr))
1592 			return true;
1593 		tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1594 	}
1595 
1596 	if (state == SELF_DOWN_PEER_LEAVING) {
1597 		if (msg_peer_node_is_up(hdr))
1598 			return false;
1599 		tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1600 		return true;
1601 	}
1602 
1603 	if (state == SELF_LEAVING_PEER_DOWN)
1604 		return false;
1605 
1606 	/* Ignore duplicate packets */
1607 	if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1608 		return true;
1609 
1610 	/* Initiate or update failover mode if applicable */
1611 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1612 		syncpt = oseqno + exp_pkts - 1;
1613 		if (pl && tipc_link_is_up(pl)) {
1614 			__tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1615 			tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1616 							tipc_link_inputq(l));
1617 		}
1618 		/* If pkts arrive out of order, use lowest calculated syncpt */
1619 		if (less(syncpt, n->sync_point))
1620 			n->sync_point = syncpt;
1621 	}
1622 
1623 	/* Open parallel link when tunnel link reaches synch point */
1624 	if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1625 		if (!more(rcv_nxt, n->sync_point))
1626 			return true;
1627 		tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1628 		if (pl)
1629 			tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1630 		return true;
1631 	}
1632 
1633 	/* No synching needed if only one link */
1634 	if (!pl || !tipc_link_is_up(pl))
1635 		return true;
1636 
1637 	/* Initiate synch mode if applicable */
1638 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1639 		syncpt = iseqno + exp_pkts - 1;
1640 		if (!tipc_link_is_up(l))
1641 			__tipc_node_link_up(n, bearer_id, xmitq);
1642 		if (n->state == SELF_UP_PEER_UP) {
1643 			n->sync_point = syncpt;
1644 			tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1645 			tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1646 		}
1647 	}
1648 
1649 	/* Open tunnel link when parallel link reaches synch point */
1650 	if (n->state == NODE_SYNCHING) {
1651 		if (tipc_link_is_synching(l)) {
1652 			tnl = l;
1653 		} else {
1654 			tnl = pl;
1655 			pl = l;
1656 		}
1657 		inputq_len = skb_queue_len(tipc_link_inputq(pl));
1658 		dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1659 		if (more(dlv_nxt, n->sync_point)) {
1660 			tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1661 			tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1662 			return true;
1663 		}
1664 		if (l == pl)
1665 			return true;
1666 		if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1667 			return true;
1668 		if (usr == LINK_PROTOCOL)
1669 			return true;
1670 		return false;
1671 	}
1672 	return true;
1673 }
1674 
1675 /**
1676  * tipc_rcv - process TIPC packets/messages arriving from off-node
1677  * @net: the applicable net namespace
1678  * @skb: TIPC packet
1679  * @bearer: pointer to bearer message arrived on
1680  *
1681  * Invoked with no locks held. Bearer pointer must point to a valid bearer
1682  * structure (i.e. cannot be NULL), but bearer can be inactive.
1683  */
1684 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1685 {
1686 	struct sk_buff_head xmitq;
1687 	struct tipc_node *n;
1688 	struct tipc_msg *hdr;
1689 	int bearer_id = b->identity;
1690 	struct tipc_link_entry *le;
1691 	u32 self = tipc_own_addr(net);
1692 	int usr, rc = 0;
1693 	u16 bc_ack;
1694 
1695 	__skb_queue_head_init(&xmitq);
1696 
1697 	/* Ensure message is well-formed before touching the header */
1698 	if (unlikely(!tipc_msg_validate(&skb)))
1699 		goto discard;
1700 	hdr = buf_msg(skb);
1701 	usr = msg_user(hdr);
1702 	bc_ack = msg_bcast_ack(hdr);
1703 
1704 	/* Handle arrival of discovery or broadcast packet */
1705 	if (unlikely(msg_non_seq(hdr))) {
1706 		if (unlikely(usr == LINK_CONFIG))
1707 			return tipc_disc_rcv(net, skb, b);
1708 		else
1709 			return tipc_node_bc_rcv(net, skb, bearer_id);
1710 	}
1711 
1712 	/* Discard unicast link messages destined for another node */
1713 	if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1714 		goto discard;
1715 
1716 	/* Locate neighboring node that sent packet */
1717 	n = tipc_node_find(net, msg_prevnode(hdr));
1718 	if (unlikely(!n))
1719 		goto discard;
1720 	le = &n->links[bearer_id];
1721 
1722 	/* Ensure broadcast reception is in synch with peer's send state */
1723 	if (unlikely(usr == LINK_PROTOCOL))
1724 		tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
1725 	else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
1726 		tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
1727 
1728 	/* Receive packet directly if conditions permit */
1729 	tipc_node_read_lock(n);
1730 	if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
1731 		spin_lock_bh(&le->lock);
1732 		if (le->link) {
1733 			rc = tipc_link_rcv(le->link, skb, &xmitq);
1734 			skb = NULL;
1735 		}
1736 		spin_unlock_bh(&le->lock);
1737 	}
1738 	tipc_node_read_unlock(n);
1739 
1740 	/* Check/update node state before receiving */
1741 	if (unlikely(skb)) {
1742 		if (unlikely(skb_linearize(skb)))
1743 			goto discard;
1744 		tipc_node_write_lock(n);
1745 		if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1746 			if (le->link) {
1747 				rc = tipc_link_rcv(le->link, skb, &xmitq);
1748 				skb = NULL;
1749 			}
1750 		}
1751 		tipc_node_write_unlock(n);
1752 	}
1753 
1754 	if (unlikely(rc & TIPC_LINK_UP_EVT))
1755 		tipc_node_link_up(n, bearer_id, &xmitq);
1756 
1757 	if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1758 		tipc_node_link_down(n, bearer_id, false);
1759 
1760 	if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1761 		tipc_named_rcv(net, &n->bc_entry.namedq);
1762 
1763 	if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1764 		tipc_node_mcast_rcv(n);
1765 
1766 	if (!skb_queue_empty(&le->inputq))
1767 		tipc_sk_rcv(net, &le->inputq);
1768 
1769 	if (!skb_queue_empty(&xmitq))
1770 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1771 
1772 	tipc_node_put(n);
1773 discard:
1774 	kfree_skb(skb);
1775 }
1776 
1777 void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1778 			      int prop)
1779 {
1780 	struct tipc_net *tn = tipc_net(net);
1781 	int bearer_id = b->identity;
1782 	struct sk_buff_head xmitq;
1783 	struct tipc_link_entry *e;
1784 	struct tipc_node *n;
1785 
1786 	__skb_queue_head_init(&xmitq);
1787 
1788 	rcu_read_lock();
1789 
1790 	list_for_each_entry_rcu(n, &tn->node_list, list) {
1791 		tipc_node_write_lock(n);
1792 		e = &n->links[bearer_id];
1793 		if (e->link) {
1794 			if (prop == TIPC_NLA_PROP_TOL)
1795 				tipc_link_set_tolerance(e->link, b->tolerance,
1796 							&xmitq);
1797 			else if (prop == TIPC_NLA_PROP_MTU)
1798 				tipc_link_set_mtu(e->link, b->mtu);
1799 		}
1800 		tipc_node_write_unlock(n);
1801 		tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1802 	}
1803 
1804 	rcu_read_unlock();
1805 }
1806 
1807 int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1808 {
1809 	struct net *net = sock_net(skb->sk);
1810 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1811 	struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1812 	struct tipc_node *peer;
1813 	u32 addr;
1814 	int err;
1815 
1816 	/* We identify the peer by its net */
1817 	if (!info->attrs[TIPC_NLA_NET])
1818 		return -EINVAL;
1819 
1820 	err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
1821 			       info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
1822 			       info->extack);
1823 	if (err)
1824 		return err;
1825 
1826 	if (!attrs[TIPC_NLA_NET_ADDR])
1827 		return -EINVAL;
1828 
1829 	addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1830 
1831 	if (in_own_node(net, addr))
1832 		return -ENOTSUPP;
1833 
1834 	spin_lock_bh(&tn->node_list_lock);
1835 	peer = tipc_node_find(net, addr);
1836 	if (!peer) {
1837 		spin_unlock_bh(&tn->node_list_lock);
1838 		return -ENXIO;
1839 	}
1840 
1841 	tipc_node_write_lock(peer);
1842 	if (peer->state != SELF_DOWN_PEER_DOWN &&
1843 	    peer->state != SELF_DOWN_PEER_LEAVING) {
1844 		tipc_node_write_unlock(peer);
1845 		err = -EBUSY;
1846 		goto err_out;
1847 	}
1848 
1849 	tipc_node_clear_links(peer);
1850 	tipc_node_write_unlock(peer);
1851 	tipc_node_delete(peer);
1852 
1853 	err = 0;
1854 err_out:
1855 	tipc_node_put(peer);
1856 	spin_unlock_bh(&tn->node_list_lock);
1857 
1858 	return err;
1859 }
1860 
1861 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1862 {
1863 	int err;
1864 	struct net *net = sock_net(skb->sk);
1865 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1866 	int done = cb->args[0];
1867 	int last_addr = cb->args[1];
1868 	struct tipc_node *node;
1869 	struct tipc_nl_msg msg;
1870 
1871 	if (done)
1872 		return 0;
1873 
1874 	msg.skb = skb;
1875 	msg.portid = NETLINK_CB(cb->skb).portid;
1876 	msg.seq = cb->nlh->nlmsg_seq;
1877 
1878 	rcu_read_lock();
1879 	if (last_addr) {
1880 		node = tipc_node_find(net, last_addr);
1881 		if (!node) {
1882 			rcu_read_unlock();
1883 			/* We never set seq or call nl_dump_check_consistent()
1884 			 * this means that setting prev_seq here will cause the
1885 			 * consistence check to fail in the netlink callback
1886 			 * handler. Resulting in the NLMSG_DONE message having
1887 			 * the NLM_F_DUMP_INTR flag set if the node state
1888 			 * changed while we released the lock.
1889 			 */
1890 			cb->prev_seq = 1;
1891 			return -EPIPE;
1892 		}
1893 		tipc_node_put(node);
1894 	}
1895 
1896 	list_for_each_entry_rcu(node, &tn->node_list, list) {
1897 		if (last_addr) {
1898 			if (node->addr == last_addr)
1899 				last_addr = 0;
1900 			else
1901 				continue;
1902 		}
1903 
1904 		tipc_node_read_lock(node);
1905 		err = __tipc_nl_add_node(&msg, node);
1906 		if (err) {
1907 			last_addr = node->addr;
1908 			tipc_node_read_unlock(node);
1909 			goto out;
1910 		}
1911 
1912 		tipc_node_read_unlock(node);
1913 	}
1914 	done = 1;
1915 out:
1916 	cb->args[0] = done;
1917 	cb->args[1] = last_addr;
1918 	rcu_read_unlock();
1919 
1920 	return skb->len;
1921 }
1922 
1923 /* tipc_node_find_by_name - locate owner node of link by link's name
1924  * @net: the applicable net namespace
1925  * @name: pointer to link name string
1926  * @bearer_id: pointer to index in 'node->links' array where the link was found.
1927  *
1928  * Returns pointer to node owning the link, or 0 if no matching link is found.
1929  */
1930 static struct tipc_node *tipc_node_find_by_name(struct net *net,
1931 						const char *link_name,
1932 						unsigned int *bearer_id)
1933 {
1934 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1935 	struct tipc_link *l;
1936 	struct tipc_node *n;
1937 	struct tipc_node *found_node = NULL;
1938 	int i;
1939 
1940 	*bearer_id = 0;
1941 	rcu_read_lock();
1942 	list_for_each_entry_rcu(n, &tn->node_list, list) {
1943 		tipc_node_read_lock(n);
1944 		for (i = 0; i < MAX_BEARERS; i++) {
1945 			l = n->links[i].link;
1946 			if (l && !strcmp(tipc_link_name(l), link_name)) {
1947 				*bearer_id = i;
1948 				found_node = n;
1949 				break;
1950 			}
1951 		}
1952 		tipc_node_read_unlock(n);
1953 		if (found_node)
1954 			break;
1955 	}
1956 	rcu_read_unlock();
1957 
1958 	return found_node;
1959 }
1960 
1961 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1962 {
1963 	int err;
1964 	int res = 0;
1965 	int bearer_id;
1966 	char *name;
1967 	struct tipc_link *link;
1968 	struct tipc_node *node;
1969 	struct sk_buff_head xmitq;
1970 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1971 	struct net *net = sock_net(skb->sk);
1972 
1973 	__skb_queue_head_init(&xmitq);
1974 
1975 	if (!info->attrs[TIPC_NLA_LINK])
1976 		return -EINVAL;
1977 
1978 	err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1979 			       info->attrs[TIPC_NLA_LINK],
1980 			       tipc_nl_link_policy, info->extack);
1981 	if (err)
1982 		return err;
1983 
1984 	if (!attrs[TIPC_NLA_LINK_NAME])
1985 		return -EINVAL;
1986 
1987 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1988 
1989 	if (strcmp(name, tipc_bclink_name) == 0)
1990 		return tipc_nl_bc_link_set(net, attrs);
1991 
1992 	node = tipc_node_find_by_name(net, name, &bearer_id);
1993 	if (!node)
1994 		return -EINVAL;
1995 
1996 	tipc_node_read_lock(node);
1997 
1998 	link = node->links[bearer_id].link;
1999 	if (!link) {
2000 		res = -EINVAL;
2001 		goto out;
2002 	}
2003 
2004 	if (attrs[TIPC_NLA_LINK_PROP]) {
2005 		struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2006 
2007 		err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2008 					      props);
2009 		if (err) {
2010 			res = err;
2011 			goto out;
2012 		}
2013 
2014 		if (props[TIPC_NLA_PROP_TOL]) {
2015 			u32 tol;
2016 
2017 			tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2018 			tipc_link_set_tolerance(link, tol, &xmitq);
2019 		}
2020 		if (props[TIPC_NLA_PROP_PRIO]) {
2021 			u32 prio;
2022 
2023 			prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2024 			tipc_link_set_prio(link, prio, &xmitq);
2025 		}
2026 		if (props[TIPC_NLA_PROP_WIN]) {
2027 			u32 win;
2028 
2029 			win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2030 			tipc_link_set_queue_limits(link, win);
2031 		}
2032 	}
2033 
2034 out:
2035 	tipc_node_read_unlock(node);
2036 	tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
2037 	return res;
2038 }
2039 
2040 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2041 {
2042 	struct net *net = genl_info_net(info);
2043 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2044 	struct tipc_nl_msg msg;
2045 	char *name;
2046 	int err;
2047 
2048 	msg.portid = info->snd_portid;
2049 	msg.seq = info->snd_seq;
2050 
2051 	if (!info->attrs[TIPC_NLA_LINK])
2052 		return -EINVAL;
2053 
2054 	err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2055 			       info->attrs[TIPC_NLA_LINK],
2056 			       tipc_nl_link_policy, info->extack);
2057 	if (err)
2058 		return err;
2059 
2060 	if (!attrs[TIPC_NLA_LINK_NAME])
2061 		return -EINVAL;
2062 
2063 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2064 
2065 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2066 	if (!msg.skb)
2067 		return -ENOMEM;
2068 
2069 	if (strcmp(name, tipc_bclink_name) == 0) {
2070 		err = tipc_nl_add_bc_link(net, &msg);
2071 		if (err)
2072 			goto err_free;
2073 	} else {
2074 		int bearer_id;
2075 		struct tipc_node *node;
2076 		struct tipc_link *link;
2077 
2078 		node = tipc_node_find_by_name(net, name, &bearer_id);
2079 		if (!node) {
2080 			err = -EINVAL;
2081 			goto err_free;
2082 		}
2083 
2084 		tipc_node_read_lock(node);
2085 		link = node->links[bearer_id].link;
2086 		if (!link) {
2087 			tipc_node_read_unlock(node);
2088 			err = -EINVAL;
2089 			goto err_free;
2090 		}
2091 
2092 		err = __tipc_nl_add_link(net, &msg, link, 0);
2093 		tipc_node_read_unlock(node);
2094 		if (err)
2095 			goto err_free;
2096 	}
2097 
2098 	return genlmsg_reply(msg.skb, info);
2099 
2100 err_free:
2101 	nlmsg_free(msg.skb);
2102 	return err;
2103 }
2104 
2105 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2106 {
2107 	int err;
2108 	char *link_name;
2109 	unsigned int bearer_id;
2110 	struct tipc_link *link;
2111 	struct tipc_node *node;
2112 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2113 	struct net *net = sock_net(skb->sk);
2114 	struct tipc_link_entry *le;
2115 
2116 	if (!info->attrs[TIPC_NLA_LINK])
2117 		return -EINVAL;
2118 
2119 	err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2120 			       info->attrs[TIPC_NLA_LINK],
2121 			       tipc_nl_link_policy, info->extack);
2122 	if (err)
2123 		return err;
2124 
2125 	if (!attrs[TIPC_NLA_LINK_NAME])
2126 		return -EINVAL;
2127 
2128 	link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2129 
2130 	if (strcmp(link_name, tipc_bclink_name) == 0) {
2131 		err = tipc_bclink_reset_stats(net);
2132 		if (err)
2133 			return err;
2134 		return 0;
2135 	}
2136 
2137 	node = tipc_node_find_by_name(net, link_name, &bearer_id);
2138 	if (!node)
2139 		return -EINVAL;
2140 
2141 	le = &node->links[bearer_id];
2142 	tipc_node_read_lock(node);
2143 	spin_lock_bh(&le->lock);
2144 	link = node->links[bearer_id].link;
2145 	if (!link) {
2146 		spin_unlock_bh(&le->lock);
2147 		tipc_node_read_unlock(node);
2148 		return -EINVAL;
2149 	}
2150 	tipc_link_reset_stats(link);
2151 	spin_unlock_bh(&le->lock);
2152 	tipc_node_read_unlock(node);
2153 	return 0;
2154 }
2155 
2156 /* Caller should hold node lock  */
2157 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2158 				    struct tipc_node *node, u32 *prev_link)
2159 {
2160 	u32 i;
2161 	int err;
2162 
2163 	for (i = *prev_link; i < MAX_BEARERS; i++) {
2164 		*prev_link = i;
2165 
2166 		if (!node->links[i].link)
2167 			continue;
2168 
2169 		err = __tipc_nl_add_link(net, msg,
2170 					 node->links[i].link, NLM_F_MULTI);
2171 		if (err)
2172 			return err;
2173 	}
2174 	*prev_link = 0;
2175 
2176 	return 0;
2177 }
2178 
2179 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
2180 {
2181 	struct net *net = sock_net(skb->sk);
2182 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2183 	struct tipc_node *node;
2184 	struct tipc_nl_msg msg;
2185 	u32 prev_node = cb->args[0];
2186 	u32 prev_link = cb->args[1];
2187 	int done = cb->args[2];
2188 	int err;
2189 
2190 	if (done)
2191 		return 0;
2192 
2193 	msg.skb = skb;
2194 	msg.portid = NETLINK_CB(cb->skb).portid;
2195 	msg.seq = cb->nlh->nlmsg_seq;
2196 
2197 	rcu_read_lock();
2198 	if (prev_node) {
2199 		node = tipc_node_find(net, prev_node);
2200 		if (!node) {
2201 			/* We never set seq or call nl_dump_check_consistent()
2202 			 * this means that setting prev_seq here will cause the
2203 			 * consistence check to fail in the netlink callback
2204 			 * handler. Resulting in the last NLMSG_DONE message
2205 			 * having the NLM_F_DUMP_INTR flag set.
2206 			 */
2207 			cb->prev_seq = 1;
2208 			goto out;
2209 		}
2210 		tipc_node_put(node);
2211 
2212 		list_for_each_entry_continue_rcu(node, &tn->node_list,
2213 						 list) {
2214 			tipc_node_read_lock(node);
2215 			err = __tipc_nl_add_node_links(net, &msg, node,
2216 						       &prev_link);
2217 			tipc_node_read_unlock(node);
2218 			if (err)
2219 				goto out;
2220 
2221 			prev_node = node->addr;
2222 		}
2223 	} else {
2224 		err = tipc_nl_add_bc_link(net, &msg);
2225 		if (err)
2226 			goto out;
2227 
2228 		list_for_each_entry_rcu(node, &tn->node_list, list) {
2229 			tipc_node_read_lock(node);
2230 			err = __tipc_nl_add_node_links(net, &msg, node,
2231 						       &prev_link);
2232 			tipc_node_read_unlock(node);
2233 			if (err)
2234 				goto out;
2235 
2236 			prev_node = node->addr;
2237 		}
2238 	}
2239 	done = 1;
2240 out:
2241 	rcu_read_unlock();
2242 
2243 	cb->args[0] = prev_node;
2244 	cb->args[1] = prev_link;
2245 	cb->args[2] = done;
2246 
2247 	return skb->len;
2248 }
2249 
2250 int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2251 {
2252 	struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2253 	struct net *net = sock_net(skb->sk);
2254 	int err;
2255 
2256 	if (!info->attrs[TIPC_NLA_MON])
2257 		return -EINVAL;
2258 
2259 	err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2260 			       info->attrs[TIPC_NLA_MON],
2261 			       tipc_nl_monitor_policy, info->extack);
2262 	if (err)
2263 		return err;
2264 
2265 	if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2266 		u32 val;
2267 
2268 		val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2269 		err = tipc_nl_monitor_set_threshold(net, val);
2270 		if (err)
2271 			return err;
2272 	}
2273 
2274 	return 0;
2275 }
2276 
2277 static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2278 {
2279 	struct nlattr *attrs;
2280 	void *hdr;
2281 	u32 val;
2282 
2283 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2284 			  0, TIPC_NL_MON_GET);
2285 	if (!hdr)
2286 		return -EMSGSIZE;
2287 
2288 	attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2289 	if (!attrs)
2290 		goto msg_full;
2291 
2292 	val = tipc_nl_monitor_get_threshold(net);
2293 
2294 	if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2295 		goto attr_msg_full;
2296 
2297 	nla_nest_end(msg->skb, attrs);
2298 	genlmsg_end(msg->skb, hdr);
2299 
2300 	return 0;
2301 
2302 attr_msg_full:
2303 	nla_nest_cancel(msg->skb, attrs);
2304 msg_full:
2305 	genlmsg_cancel(msg->skb, hdr);
2306 
2307 	return -EMSGSIZE;
2308 }
2309 
2310 int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2311 {
2312 	struct net *net = sock_net(skb->sk);
2313 	struct tipc_nl_msg msg;
2314 	int err;
2315 
2316 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2317 	if (!msg.skb)
2318 		return -ENOMEM;
2319 	msg.portid = info->snd_portid;
2320 	msg.seq = info->snd_seq;
2321 
2322 	err = __tipc_nl_add_monitor_prop(net, &msg);
2323 	if (err) {
2324 		nlmsg_free(msg.skb);
2325 		return err;
2326 	}
2327 
2328 	return genlmsg_reply(msg.skb, info);
2329 }
2330 
2331 int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2332 {
2333 	struct net *net = sock_net(skb->sk);
2334 	u32 prev_bearer = cb->args[0];
2335 	struct tipc_nl_msg msg;
2336 	int bearer_id;
2337 	int err;
2338 
2339 	if (prev_bearer == MAX_BEARERS)
2340 		return 0;
2341 
2342 	msg.skb = skb;
2343 	msg.portid = NETLINK_CB(cb->skb).portid;
2344 	msg.seq = cb->nlh->nlmsg_seq;
2345 
2346 	rtnl_lock();
2347 	for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
2348 		err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2349 		if (err)
2350 			break;
2351 	}
2352 	rtnl_unlock();
2353 	cb->args[0] = bearer_id;
2354 
2355 	return skb->len;
2356 }
2357 
2358 int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2359 				   struct netlink_callback *cb)
2360 {
2361 	struct net *net = sock_net(skb->sk);
2362 	u32 prev_node = cb->args[1];
2363 	u32 bearer_id = cb->args[2];
2364 	int done = cb->args[0];
2365 	struct tipc_nl_msg msg;
2366 	int err;
2367 
2368 	if (!prev_node) {
2369 		struct nlattr **attrs;
2370 		struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2371 
2372 		err = tipc_nlmsg_parse(cb->nlh, &attrs);
2373 		if (err)
2374 			return err;
2375 
2376 		if (!attrs[TIPC_NLA_MON])
2377 			return -EINVAL;
2378 
2379 		err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2380 				       attrs[TIPC_NLA_MON],
2381 				       tipc_nl_monitor_policy, NULL);
2382 		if (err)
2383 			return err;
2384 
2385 		if (!mon[TIPC_NLA_MON_REF])
2386 			return -EINVAL;
2387 
2388 		bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2389 
2390 		if (bearer_id >= MAX_BEARERS)
2391 			return -EINVAL;
2392 	}
2393 
2394 	if (done)
2395 		return 0;
2396 
2397 	msg.skb = skb;
2398 	msg.portid = NETLINK_CB(cb->skb).portid;
2399 	msg.seq = cb->nlh->nlmsg_seq;
2400 
2401 	rtnl_lock();
2402 	err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2403 	if (!err)
2404 		done = 1;
2405 
2406 	rtnl_unlock();
2407 	cb->args[0] = done;
2408 	cb->args[1] = prev_node;
2409 	cb->args[2] = bearer_id;
2410 
2411 	return skb->len;
2412 }
2413