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