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