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