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