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