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