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