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