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