2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
5 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
40 #include "name_distr.h"
49 #define INVALID_NODE_SIG 0x10000
50 #define NODE_CLEANUP_AFTER 300000
52 /* Flags used to take different actions according to flag type
53 * TIPC_NOTIFY_NODE_DOWN: notify node is down
54 * TIPC_NOTIFY_NODE_UP: notify node is up
55 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
58 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
59 TIPC_NOTIFY_NODE_UP = (1 << 4),
60 TIPC_NOTIFY_LINK_UP = (1 << 6),
61 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
64 struct tipc_link_entry {
65 struct tipc_link *link;
66 spinlock_t lock; /* per link */
68 struct sk_buff_head inputq;
69 struct tipc_media_addr maddr;
72 struct tipc_bclink_entry {
73 struct tipc_link *link;
74 struct sk_buff_head inputq1;
75 struct sk_buff_head arrvq;
76 struct sk_buff_head inputq2;
77 struct sk_buff_head namedq;
81 * struct tipc_node - TIPC node structure
82 * @addr: network address of node
83 * @ref: reference counter to node object
84 * @lock: rwlock governing access to structure
85 * @net: the applicable net namespace
86 * @hash: links to adjacent nodes in unsorted hash chain
87 * @inputq: pointer to input queue containing messages for msg event
88 * @namedq: pointer to name table input queue with name table messages
89 * @active_links: bearer ids of active links, used as index into links[] array
90 * @links: array containing references to all links to node
91 * @action_flags: bit mask of different types of node actions
92 * @state: connectivity state vs peer node
93 * @preliminary: a preliminary node or not
94 * @sync_point: sequence number where synch/failover is finished
95 * @list: links to adjacent nodes in sorted list of cluster's nodes
96 * @working_links: number of working links to node (both active and standby)
97 * @link_cnt: number of links to node
98 * @capabilities: bitmap, indicating peer node's functional capabilities
99 * @signature: node instance identifier
100 * @link_id: local and remote bearer ids of changing link, if any
101 * @publ_list: list of publications
102 * @rcu: rcu struct for tipc_node
103 * @delete_at: indicates the time for deleting a down node
104 * @crypto_rx: RX crypto handler
111 struct hlist_node hash;
113 struct tipc_link_entry links[MAX_BEARERS];
114 struct tipc_bclink_entry bc_entry;
116 struct list_head list;
127 char peer_id_string[NODE_ID_STR_LEN];
128 struct list_head publ_list;
129 struct list_head conn_sks;
130 unsigned long keepalive_intv;
131 struct timer_list timer;
133 unsigned long delete_at;
134 struct net *peer_net;
136 #ifdef CONFIG_TIPC_CRYPTO
137 struct tipc_crypto *crypto_rx;
141 /* Node FSM states and events:
144 SELF_DOWN_PEER_DOWN = 0xdd,
145 SELF_UP_PEER_UP = 0xaa,
146 SELF_DOWN_PEER_LEAVING = 0xd1,
147 SELF_UP_PEER_COMING = 0xac,
148 SELF_COMING_PEER_UP = 0xca,
149 SELF_LEAVING_PEER_DOWN = 0x1d,
150 NODE_FAILINGOVER = 0xf0,
155 SELF_ESTABL_CONTACT_EVT = 0xece,
156 SELF_LOST_CONTACT_EVT = 0x1ce,
157 PEER_ESTABL_CONTACT_EVT = 0x9ece,
158 PEER_LOST_CONTACT_EVT = 0x91ce,
159 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
160 NODE_FAILOVER_END_EVT = 0xfee,
161 NODE_SYNCH_BEGIN_EVT = 0xcbe,
162 NODE_SYNCH_END_EVT = 0xcee
165 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
166 struct sk_buff_head *xmitq,
167 struct tipc_media_addr **maddr);
168 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
170 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
171 static void tipc_node_delete(struct tipc_node *node);
172 static void tipc_node_timeout(struct timer_list *t);
173 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
174 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
175 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
176 static bool node_is_up(struct tipc_node *n);
177 static void tipc_node_delete_from_list(struct tipc_node *node);
179 struct tipc_sock_conn {
183 struct list_head list;
186 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
188 int bearer_id = n->active_links[sel & 1];
190 if (unlikely(bearer_id == INVALID_BEARER_ID))
193 return n->links[bearer_id].link;
196 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
200 unsigned int mtu = MAX_MSG_SIZE;
202 n = tipc_node_find(net, addr);
206 /* Allow MAX_MSG_SIZE when building connection oriented message
207 * if they are in the same core network
209 if (n->peer_net && connected) {
214 bearer_id = n->active_links[sel & 1];
215 if (likely(bearer_id != INVALID_BEARER_ID))
216 mtu = n->links[bearer_id].mtu;
221 bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
223 u8 *own_id = tipc_own_id(net);
229 if (addr == tipc_own_addr(net)) {
230 memcpy(id, own_id, TIPC_NODEID_LEN);
233 n = tipc_node_find(net, addr);
237 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
242 u16 tipc_node_get_capabilities(struct net *net, u32 addr)
247 n = tipc_node_find(net, addr);
249 return TIPC_NODE_CAPABILITIES;
250 caps = n->capabilities;
255 u32 tipc_node_get_addr(struct tipc_node *node)
257 return (node) ? node->addr : 0;
260 char *tipc_node_get_id_str(struct tipc_node *node)
262 return node->peer_id_string;
265 #ifdef CONFIG_TIPC_CRYPTO
267 * tipc_node_crypto_rx - Retrieve crypto RX handle from node
268 * Note: node ref counter must be held first!
270 struct tipc_crypto *tipc_node_crypto_rx(struct tipc_node *__n)
272 return (__n) ? __n->crypto_rx : NULL;
275 struct tipc_crypto *tipc_node_crypto_rx_by_list(struct list_head *pos)
277 return container_of(pos, struct tipc_node, list)->crypto_rx;
281 static void tipc_node_free(struct rcu_head *rp)
283 struct tipc_node *n = container_of(rp, struct tipc_node, rcu);
285 #ifdef CONFIG_TIPC_CRYPTO
286 tipc_crypto_stop(&n->crypto_rx);
291 static void tipc_node_kref_release(struct kref *kref)
293 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
295 kfree(n->bc_entry.link);
296 call_rcu(&n->rcu, tipc_node_free);
299 void tipc_node_put(struct tipc_node *node)
301 kref_put(&node->kref, tipc_node_kref_release);
304 static void tipc_node_get(struct tipc_node *node)
306 kref_get(&node->kref);
310 * tipc_node_find - locate specified node object, if it exists
312 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
314 struct tipc_net *tn = tipc_net(net);
315 struct tipc_node *node;
316 unsigned int thash = tipc_hashfn(addr);
319 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
320 if (node->addr != addr || node->preliminary)
322 if (!kref_get_unless_zero(&node->kref))
330 /* tipc_node_find_by_id - locate specified node object by its 128-bit id
331 * Note: this function is called only when a discovery request failed
332 * to find the node by its 32-bit id, and is not time critical
334 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
336 struct tipc_net *tn = tipc_net(net);
341 list_for_each_entry_rcu(n, &tn->node_list, list) {
342 read_lock_bh(&n->lock);
343 if (!memcmp(id, n->peer_id, 16) &&
344 kref_get_unless_zero(&n->kref))
346 read_unlock_bh(&n->lock);
351 return found ? n : NULL;
354 static void tipc_node_read_lock(struct tipc_node *n)
356 read_lock_bh(&n->lock);
359 static void tipc_node_read_unlock(struct tipc_node *n)
361 read_unlock_bh(&n->lock);
364 static void tipc_node_write_lock(struct tipc_node *n)
366 write_lock_bh(&n->lock);
369 static void tipc_node_write_unlock_fast(struct tipc_node *n)
371 write_unlock_bh(&n->lock);
374 static void tipc_node_write_unlock(struct tipc_node *n)
376 struct net *net = n->net;
378 u32 flags = n->action_flags;
381 struct list_head *publ_list;
383 if (likely(!flags)) {
384 write_unlock_bh(&n->lock);
389 link_id = n->link_id;
390 bearer_id = link_id & 0xffff;
391 publ_list = &n->publ_list;
393 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
394 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
396 write_unlock_bh(&n->lock);
398 if (flags & TIPC_NOTIFY_NODE_DOWN)
399 tipc_publ_notify(net, publ_list, addr);
401 if (flags & TIPC_NOTIFY_NODE_UP)
402 tipc_named_node_up(net, addr);
404 if (flags & TIPC_NOTIFY_LINK_UP) {
405 tipc_mon_peer_up(net, addr, bearer_id);
406 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
407 TIPC_NODE_SCOPE, link_id, link_id);
409 if (flags & TIPC_NOTIFY_LINK_DOWN) {
410 tipc_mon_peer_down(net, addr, bearer_id);
411 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
416 static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
418 int net_id = tipc_netid(n->net);
419 struct tipc_net *tn_peer;
426 for_each_net_rcu(tmp) {
427 tn_peer = tipc_net(tmp);
430 /* Integrity checking whether node exists in namespace or not */
431 if (tn_peer->net_id != net_id)
433 if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
435 hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
436 if (hash_mixes ^ hash_chk)
439 n->peer_hash_mix = hash_mixes;
444 struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
445 u16 capabilities, u32 hash_mixes,
448 struct tipc_net *tn = net_generic(net, tipc_net_id);
449 struct tipc_node *n, *temp_node;
455 spin_lock_bh(&tn->node_list_lock);
456 n = tipc_node_find(net, addr) ?:
457 tipc_node_find_by_id(net, peer_id);
463 /* A preliminary node becomes "real" now, refresh its data */
464 tipc_node_write_lock(n);
465 n->preliminary = false;
467 hlist_del_rcu(&n->hash);
468 hlist_add_head_rcu(&n->hash,
469 &tn->node_htable[tipc_hashfn(addr)]);
470 list_del_rcu(&n->list);
471 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
472 if (n->addr < temp_node->addr)
475 list_add_tail_rcu(&n->list, &temp_node->list);
476 tipc_node_write_unlock_fast(n);
479 if (n->peer_hash_mix ^ hash_mixes)
480 tipc_node_assign_peer_net(n, hash_mixes);
481 if (n->capabilities == capabilities)
483 /* Same node may come back with new capabilities */
484 tipc_node_write_lock(n);
485 n->capabilities = capabilities;
486 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
487 l = n->links[bearer_id].link;
489 tipc_link_update_caps(l, capabilities);
491 tipc_node_write_unlock_fast(n);
493 /* Calculate cluster capabilities */
494 tn->capabilities = TIPC_NODE_CAPABILITIES;
495 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
496 tn->capabilities &= temp_node->capabilities;
499 tipc_bcast_toggle_rcast(net,
500 (tn->capabilities & TIPC_BCAST_RCAST));
504 n = kzalloc(sizeof(*n), GFP_ATOMIC);
506 pr_warn("Node creation failed, no memory\n");
509 tipc_nodeid2string(n->peer_id_string, peer_id);
510 #ifdef CONFIG_TIPC_CRYPTO
511 if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
512 pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
519 n->preliminary = preliminary;
520 memcpy(&n->peer_id, peer_id, 16);
523 n->peer_hash_mix = 0;
524 /* Assign kernel local namespace if exists */
525 tipc_node_assign_peer_net(n, hash_mixes);
526 n->capabilities = capabilities;
528 rwlock_init(&n->lock);
529 INIT_HLIST_NODE(&n->hash);
530 INIT_LIST_HEAD(&n->list);
531 INIT_LIST_HEAD(&n->publ_list);
532 INIT_LIST_HEAD(&n->conn_sks);
533 skb_queue_head_init(&n->bc_entry.namedq);
534 skb_queue_head_init(&n->bc_entry.inputq1);
535 __skb_queue_head_init(&n->bc_entry.arrvq);
536 skb_queue_head_init(&n->bc_entry.inputq2);
537 for (i = 0; i < MAX_BEARERS; i++)
538 spin_lock_init(&n->links[i].lock);
539 n->state = SELF_DOWN_PEER_LEAVING;
540 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
541 n->signature = INVALID_NODE_SIG;
542 n->active_links[0] = INVALID_BEARER_ID;
543 n->active_links[1] = INVALID_BEARER_ID;
544 n->bc_entry.link = NULL;
546 timer_setup(&n->timer, tipc_node_timeout, 0);
547 /* Start a slow timer anyway, crypto needs it */
548 n->keepalive_intv = 10000;
549 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
550 if (!mod_timer(&n->timer, intv))
552 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
553 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
554 if (n->addr < temp_node->addr)
557 list_add_tail_rcu(&n->list, &temp_node->list);
558 /* Calculate cluster capabilities */
559 tn->capabilities = TIPC_NODE_CAPABILITIES;
560 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
561 tn->capabilities &= temp_node->capabilities;
563 tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
564 trace_tipc_node_create(n, true, " ");
566 spin_unlock_bh(&tn->node_list_lock);
570 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
572 unsigned long tol = tipc_link_tolerance(l);
573 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
575 /* Link with lowest tolerance determines timer interval */
576 if (intv < n->keepalive_intv)
577 n->keepalive_intv = intv;
579 /* Ensure link's abort limit corresponds to current tolerance */
580 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
583 static void tipc_node_delete_from_list(struct tipc_node *node)
585 list_del_rcu(&node->list);
586 hlist_del_rcu(&node->hash);
590 static void tipc_node_delete(struct tipc_node *node)
592 trace_tipc_node_delete(node, true, " ");
593 tipc_node_delete_from_list(node);
595 del_timer_sync(&node->timer);
599 void tipc_node_stop(struct net *net)
601 struct tipc_net *tn = tipc_net(net);
602 struct tipc_node *node, *t_node;
604 spin_lock_bh(&tn->node_list_lock);
605 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
606 tipc_node_delete(node);
607 spin_unlock_bh(&tn->node_list_lock);
610 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
614 if (in_own_node(net, addr))
617 n = tipc_node_find(net, addr);
619 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
622 tipc_node_write_lock(n);
623 list_add_tail(subscr, &n->publ_list);
624 tipc_node_write_unlock_fast(n);
628 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
632 if (in_own_node(net, addr))
635 n = tipc_node_find(net, addr);
637 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
640 tipc_node_write_lock(n);
641 list_del_init(subscr);
642 tipc_node_write_unlock_fast(n);
646 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
648 struct tipc_node *node;
649 struct tipc_sock_conn *conn;
652 if (in_own_node(net, dnode))
655 node = tipc_node_find(net, dnode);
657 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
658 return -EHOSTUNREACH;
660 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
665 conn->peer_node = dnode;
667 conn->peer_port = peer_port;
669 tipc_node_write_lock(node);
670 list_add_tail(&conn->list, &node->conn_sks);
671 tipc_node_write_unlock(node);
677 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
679 struct tipc_node *node;
680 struct tipc_sock_conn *conn, *safe;
682 if (in_own_node(net, dnode))
685 node = tipc_node_find(net, dnode);
689 tipc_node_write_lock(node);
690 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
691 if (port != conn->port)
693 list_del(&conn->list);
696 tipc_node_write_unlock(node);
700 static void tipc_node_clear_links(struct tipc_node *node)
704 for (i = 0; i < MAX_BEARERS; i++) {
705 struct tipc_link_entry *le = &node->links[i];
715 /* tipc_node_cleanup - delete nodes that does not
716 * have active links for NODE_CLEANUP_AFTER time
718 static bool tipc_node_cleanup(struct tipc_node *peer)
720 struct tipc_node *temp_node;
721 struct tipc_net *tn = tipc_net(peer->net);
722 bool deleted = false;
724 /* If lock held by tipc_node_stop() the node will be deleted anyway */
725 if (!spin_trylock_bh(&tn->node_list_lock))
728 tipc_node_write_lock(peer);
730 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
731 tipc_node_clear_links(peer);
732 tipc_node_delete_from_list(peer);
735 tipc_node_write_unlock(peer);
738 spin_unlock_bh(&tn->node_list_lock);
742 /* Calculate cluster capabilities */
743 tn->capabilities = TIPC_NODE_CAPABILITIES;
744 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
745 tn->capabilities &= temp_node->capabilities;
747 tipc_bcast_toggle_rcast(peer->net,
748 (tn->capabilities & TIPC_BCAST_RCAST));
749 spin_unlock_bh(&tn->node_list_lock);
753 /* tipc_node_timeout - handle expiration of node timer
755 static void tipc_node_timeout(struct timer_list *t)
757 struct tipc_node *n = from_timer(n, t, timer);
758 struct tipc_link_entry *le;
759 struct sk_buff_head xmitq;
760 int remains = n->link_cnt;
764 trace_tipc_node_timeout(n, false, " ");
765 if (!node_is_up(n) && tipc_node_cleanup(n)) {
766 /*Removing the reference of Timer*/
771 #ifdef CONFIG_TIPC_CRYPTO
772 /* Take any crypto key related actions first */
773 tipc_crypto_timeout(n->crypto_rx);
775 __skb_queue_head_init(&xmitq);
777 /* Initial node interval to value larger (10 seconds), then it will be
778 * recalculated with link lowest tolerance
780 tipc_node_read_lock(n);
781 n->keepalive_intv = 10000;
782 tipc_node_read_unlock(n);
783 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
784 tipc_node_read_lock(n);
785 le = &n->links[bearer_id];
787 spin_lock_bh(&le->lock);
788 /* Link tolerance may change asynchronously: */
789 tipc_node_calculate_timer(n, le->link);
790 rc = tipc_link_timeout(le->link, &xmitq);
791 spin_unlock_bh(&le->lock);
794 tipc_node_read_unlock(n);
795 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr, n);
796 if (rc & TIPC_LINK_DOWN_EVT)
797 tipc_node_link_down(n, bearer_id, false);
799 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
803 * __tipc_node_link_up - handle addition of link
804 * Node lock must be held by caller
805 * Link becomes active (alone or shared) or standby, depending on its priority.
807 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
808 struct sk_buff_head *xmitq)
810 int *slot0 = &n->active_links[0];
811 int *slot1 = &n->active_links[1];
812 struct tipc_link *ol = node_active_link(n, 0);
813 struct tipc_link *nl = n->links[bearer_id].link;
815 if (!nl || tipc_link_is_up(nl))
818 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
819 if (!tipc_link_is_up(nl))
823 n->action_flags |= TIPC_NOTIFY_LINK_UP;
824 n->link_id = tipc_link_id(nl);
826 /* Leave room for tunnel header when returning 'mtu' to users: */
827 n->links[bearer_id].mtu = tipc_link_mss(nl);
829 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
830 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
832 pr_debug("Established link <%s> on network plane %c\n",
833 tipc_link_name(nl), tipc_link_plane(nl));
834 trace_tipc_node_link_up(n, true, " ");
836 /* Ensure that a STATE message goes first */
837 tipc_link_build_state_msg(nl, xmitq);
839 /* First link? => give it both slots */
843 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
844 n->action_flags |= TIPC_NOTIFY_NODE_UP;
845 tipc_link_set_active(nl, true);
846 tipc_bcast_add_peer(n->net, nl, xmitq);
850 /* Second link => redistribute slots */
851 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
852 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
855 tipc_link_set_active(nl, true);
856 tipc_link_set_active(ol, false);
857 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
858 tipc_link_set_active(nl, true);
861 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
864 /* Prepare synchronization with first link */
865 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
869 * tipc_node_link_up - handle addition of link
871 * Link becomes active (alone or shared) or standby, depending on its priority.
873 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
874 struct sk_buff_head *xmitq)
876 struct tipc_media_addr *maddr;
878 tipc_node_write_lock(n);
879 __tipc_node_link_up(n, bearer_id, xmitq);
880 maddr = &n->links[bearer_id].maddr;
881 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr, n);
882 tipc_node_write_unlock(n);
886 * tipc_node_link_failover() - start failover in case "half-failover"
888 * This function is only called in a very special situation where link
889 * failover can be already started on peer node but not on this node.
890 * This can happen when e.g.
891 * 1. Both links <1A-2A>, <1B-2B> down
892 * 2. Link endpoint 2A up, but 1A still down (e.g. due to network
893 * disturbance, wrong session, etc.)
895 * 4. Link endpoint 2A down (e.g. due to link tolerance timeout)
896 * 5. Node 2 starts failover onto link <1B-2B>
898 * ==> Node 1 does never start link/node failover!
900 * @n: tipc node structure
901 * @l: link peer endpoint failingover (- can be NULL)
903 * @xmitq: queue for messages to be xmited on tnl link later
905 static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
906 struct tipc_link *tnl,
907 struct sk_buff_head *xmitq)
909 /* Avoid to be "self-failover" that can never end */
910 if (!tipc_link_is_up(tnl))
913 /* Don't rush, failure link may be in the process of resetting */
914 if (l && !tipc_link_is_reset(l))
917 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
918 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
920 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
921 tipc_link_failover_prepare(l, tnl, xmitq);
924 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
925 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
929 * __tipc_node_link_down - handle loss of link
931 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
932 struct sk_buff_head *xmitq,
933 struct tipc_media_addr **maddr)
935 struct tipc_link_entry *le = &n->links[*bearer_id];
936 int *slot0 = &n->active_links[0];
937 int *slot1 = &n->active_links[1];
938 int i, highest = 0, prio;
939 struct tipc_link *l, *_l, *tnl;
941 l = n->links[*bearer_id].link;
942 if (!l || tipc_link_is_reset(l))
946 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
947 n->link_id = tipc_link_id(l);
949 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
951 pr_debug("Lost link <%s> on network plane %c\n",
952 tipc_link_name(l), tipc_link_plane(l));
954 /* Select new active link if any available */
955 *slot0 = INVALID_BEARER_ID;
956 *slot1 = INVALID_BEARER_ID;
957 for (i = 0; i < MAX_BEARERS; i++) {
958 _l = n->links[i].link;
959 if (!_l || !tipc_link_is_up(_l))
963 prio = tipc_link_prio(_l);
966 if (prio > highest) {
975 if (!node_is_up(n)) {
976 if (tipc_link_peer_is_down(l))
977 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
978 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
979 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
980 tipc_link_fsm_evt(l, LINK_RESET_EVT);
982 tipc_link_build_reset_msg(l, xmitq);
983 *maddr = &n->links[*bearer_id].maddr;
984 node_lost_contact(n, &le->inputq);
985 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
988 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
990 /* There is still a working link => initiate failover */
991 *bearer_id = n->active_links[0];
992 tnl = n->links[*bearer_id].link;
993 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
994 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
995 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
996 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
997 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
999 tipc_link_fsm_evt(l, LINK_RESET_EVT);
1000 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1001 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
1002 *maddr = &n->links[*bearer_id].maddr;
1005 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
1007 struct tipc_link_entry *le = &n->links[bearer_id];
1008 struct tipc_media_addr *maddr = NULL;
1009 struct tipc_link *l = le->link;
1010 int old_bearer_id = bearer_id;
1011 struct sk_buff_head xmitq;
1016 __skb_queue_head_init(&xmitq);
1018 tipc_node_write_lock(n);
1019 if (!tipc_link_is_establishing(l)) {
1020 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
1022 /* Defuse pending tipc_node_link_up() */
1024 tipc_link_fsm_evt(l, LINK_RESET_EVT);
1031 trace_tipc_node_link_down(n, true, "node link down or deleted!");
1032 tipc_node_write_unlock(n);
1034 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
1035 if (!skb_queue_empty(&xmitq))
1036 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr, n);
1037 tipc_sk_rcv(n->net, &le->inputq);
1040 static bool node_is_up(struct tipc_node *n)
1042 return n->active_links[0] != INVALID_BEARER_ID;
1045 bool tipc_node_is_up(struct net *net, u32 addr)
1047 struct tipc_node *n;
1048 bool retval = false;
1050 if (in_own_node(net, addr))
1053 n = tipc_node_find(net, addr);
1056 retval = node_is_up(n);
1061 static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
1063 struct tipc_node *n;
1065 addr ^= tipc_net(net)->random;
1066 while ((n = tipc_node_find(net, addr))) {
1073 /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
1074 * Returns suggested address if any, otherwise 0
1076 u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
1078 struct tipc_net *tn = tipc_net(net);
1079 struct tipc_node *n;
1083 /* Suggest new address if some other peer is using this one */
1084 n = tipc_node_find(net, addr);
1086 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
1091 return tipc_node_suggest_addr(net, addr);
1094 /* Suggest previously used address if peer is known */
1095 n = tipc_node_find_by_id(net, id);
1097 sugg_addr = n->addr;
1098 preliminary = n->preliminary;
1104 /* Even this node may be in conflict */
1105 if (tn->trial_addr == addr)
1106 return tipc_node_suggest_addr(net, addr);
1111 void tipc_node_check_dest(struct net *net, u32 addr,
1112 u8 *peer_id, struct tipc_bearer *b,
1113 u16 capabilities, u32 signature, u32 hash_mixes,
1114 struct tipc_media_addr *maddr,
1115 bool *respond, bool *dupl_addr)
1117 struct tipc_node *n;
1118 struct tipc_link *l, *snd_l;
1119 struct tipc_link_entry *le;
1120 bool addr_match = false;
1121 bool sign_match = false;
1122 bool link_up = false;
1123 bool accept_addr = false;
1132 n = tipc_node_create(net, addr, peer_id, capabilities, hash_mixes,
1137 tipc_node_write_lock(n);
1138 if (unlikely(!n->bc_entry.link)) {
1139 snd_l = tipc_bc_sndlink(net);
1140 if (!tipc_link_bc_create(net, tipc_own_addr(net),
1141 addr, peer_id, U16_MAX,
1142 tipc_link_min_win(snd_l),
1143 tipc_link_max_win(snd_l),
1145 &n->bc_entry.inputq1,
1146 &n->bc_entry.namedq, snd_l,
1147 &n->bc_entry.link)) {
1148 pr_warn("Broadcast rcv link creation failed, no mem\n");
1149 tipc_node_write_unlock_fast(n);
1155 le = &n->links[b->identity];
1157 /* Prepare to validate requesting node's signature and media address */
1159 link_up = l && tipc_link_is_up(l);
1160 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
1161 sign_match = (signature == n->signature);
1163 /* These three flags give us eight permutations: */
1165 if (sign_match && addr_match && link_up) {
1166 /* All is fine. Do nothing. */
1168 /* Peer node is not a container/local namespace */
1169 if (!n->peer_hash_mix)
1170 n->peer_hash_mix = hash_mixes;
1171 } else if (sign_match && addr_match && !link_up) {
1172 /* Respond. The link will come up in due time */
1174 } else if (sign_match && !addr_match && link_up) {
1175 /* Peer has changed i/f address without rebooting.
1176 * If so, the link will reset soon, and the next
1177 * discovery will be accepted. So we can ignore it.
1178 * It may also be an cloned or malicious peer having
1179 * chosen the same node address and signature as an
1181 * Ignore requests until the link goes down, if ever.
1184 } else if (sign_match && !addr_match && !link_up) {
1185 /* Peer link has changed i/f address without rebooting.
1186 * It may also be a cloned or malicious peer; we can't
1187 * distinguish between the two.
1188 * The signature is correct, so we must accept.
1192 } else if (!sign_match && addr_match && link_up) {
1193 /* Peer node rebooted. Two possibilities:
1194 * - Delayed re-discovery; this link endpoint has already
1195 * reset and re-established contact with the peer, before
1196 * receiving a discovery message from that node.
1197 * (The peer happened to receive one from this node first).
1198 * - The peer came back so fast that our side has not
1199 * discovered it yet. Probing from this side will soon
1200 * reset the link, since there can be no working link
1201 * endpoint at the peer end, and the link will re-establish.
1202 * Accept the signature, since it comes from a known peer.
1204 n->signature = signature;
1205 } else if (!sign_match && addr_match && !link_up) {
1206 /* The peer node has rebooted.
1207 * Accept signature, since it is a known peer.
1209 n->signature = signature;
1211 } else if (!sign_match && !addr_match && link_up) {
1212 /* Peer rebooted with new address, or a new/duplicate peer.
1213 * Ignore until the link goes down, if ever.
1216 } else if (!sign_match && !addr_match && !link_up) {
1217 /* Peer rebooted with new address, or it is a new peer.
1218 * Accept signature and address.
1220 n->signature = signature;
1228 /* Now create new link if not already existing */
1230 if (n->link_cnt == 2)
1233 if_name = strchr(b->name, ':') + 1;
1234 get_random_bytes(&session, sizeof(u16));
1235 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
1236 b->net_plane, b->mtu, b->priority,
1237 b->min_win, b->max_win, session,
1238 tipc_own_addr(net), addr, peer_id,
1240 tipc_bc_sndlink(n->net), n->bc_entry.link,
1242 &n->bc_entry.namedq, &l)) {
1246 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
1248 tipc_link_fsm_evt(l, LINK_RESET_EVT);
1249 if (n->state == NODE_FAILINGOVER)
1250 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1253 tipc_node_calculate_timer(n, l);
1254 if (n->link_cnt == 1) {
1255 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1256 if (!mod_timer(&n->timer, intv))
1260 memcpy(&le->maddr, maddr, sizeof(*maddr));
1262 tipc_node_write_unlock(n);
1263 if (reset && l && !tipc_link_is_reset(l))
1264 tipc_node_link_down(n, b->identity, false);
1268 void tipc_node_delete_links(struct net *net, int bearer_id)
1270 struct tipc_net *tn = net_generic(net, tipc_net_id);
1271 struct tipc_node *n;
1274 list_for_each_entry_rcu(n, &tn->node_list, list) {
1275 tipc_node_link_down(n, bearer_id, true);
1280 static void tipc_node_reset_links(struct tipc_node *n)
1284 pr_warn("Resetting all links to %x\n", n->addr);
1286 trace_tipc_node_reset_links(n, true, " ");
1287 for (i = 0; i < MAX_BEARERS; i++) {
1288 tipc_node_link_down(n, i, false);
1292 /* tipc_node_fsm_evt - node finite state machine
1293 * Determines when contact is allowed with peer node
1295 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
1297 int state = n->state;
1300 case SELF_DOWN_PEER_DOWN:
1302 case SELF_ESTABL_CONTACT_EVT:
1303 state = SELF_UP_PEER_COMING;
1305 case PEER_ESTABL_CONTACT_EVT:
1306 state = SELF_COMING_PEER_UP;
1308 case SELF_LOST_CONTACT_EVT:
1309 case PEER_LOST_CONTACT_EVT:
1311 case NODE_SYNCH_END_EVT:
1312 case NODE_SYNCH_BEGIN_EVT:
1313 case NODE_FAILOVER_BEGIN_EVT:
1314 case NODE_FAILOVER_END_EVT:
1319 case SELF_UP_PEER_UP:
1321 case SELF_LOST_CONTACT_EVT:
1322 state = SELF_DOWN_PEER_LEAVING;
1324 case PEER_LOST_CONTACT_EVT:
1325 state = SELF_LEAVING_PEER_DOWN;
1327 case NODE_SYNCH_BEGIN_EVT:
1328 state = NODE_SYNCHING;
1330 case NODE_FAILOVER_BEGIN_EVT:
1331 state = NODE_FAILINGOVER;
1333 case SELF_ESTABL_CONTACT_EVT:
1334 case PEER_ESTABL_CONTACT_EVT:
1335 case NODE_SYNCH_END_EVT:
1336 case NODE_FAILOVER_END_EVT:
1342 case SELF_DOWN_PEER_LEAVING:
1344 case PEER_LOST_CONTACT_EVT:
1345 state = SELF_DOWN_PEER_DOWN;
1347 case SELF_ESTABL_CONTACT_EVT:
1348 case PEER_ESTABL_CONTACT_EVT:
1349 case SELF_LOST_CONTACT_EVT:
1351 case NODE_SYNCH_END_EVT:
1352 case NODE_SYNCH_BEGIN_EVT:
1353 case NODE_FAILOVER_BEGIN_EVT:
1354 case NODE_FAILOVER_END_EVT:
1359 case SELF_UP_PEER_COMING:
1361 case PEER_ESTABL_CONTACT_EVT:
1362 state = SELF_UP_PEER_UP;
1364 case SELF_LOST_CONTACT_EVT:
1365 state = SELF_DOWN_PEER_DOWN;
1367 case SELF_ESTABL_CONTACT_EVT:
1368 case PEER_LOST_CONTACT_EVT:
1369 case NODE_SYNCH_END_EVT:
1370 case NODE_FAILOVER_BEGIN_EVT:
1372 case NODE_SYNCH_BEGIN_EVT:
1373 case NODE_FAILOVER_END_EVT:
1378 case SELF_COMING_PEER_UP:
1380 case SELF_ESTABL_CONTACT_EVT:
1381 state = SELF_UP_PEER_UP;
1383 case PEER_LOST_CONTACT_EVT:
1384 state = SELF_DOWN_PEER_DOWN;
1386 case SELF_LOST_CONTACT_EVT:
1387 case PEER_ESTABL_CONTACT_EVT:
1389 case NODE_SYNCH_END_EVT:
1390 case NODE_SYNCH_BEGIN_EVT:
1391 case NODE_FAILOVER_BEGIN_EVT:
1392 case NODE_FAILOVER_END_EVT:
1397 case SELF_LEAVING_PEER_DOWN:
1399 case SELF_LOST_CONTACT_EVT:
1400 state = SELF_DOWN_PEER_DOWN;
1402 case SELF_ESTABL_CONTACT_EVT:
1403 case PEER_ESTABL_CONTACT_EVT:
1404 case PEER_LOST_CONTACT_EVT:
1406 case NODE_SYNCH_END_EVT:
1407 case NODE_SYNCH_BEGIN_EVT:
1408 case NODE_FAILOVER_BEGIN_EVT:
1409 case NODE_FAILOVER_END_EVT:
1414 case NODE_FAILINGOVER:
1416 case SELF_LOST_CONTACT_EVT:
1417 state = SELF_DOWN_PEER_LEAVING;
1419 case PEER_LOST_CONTACT_EVT:
1420 state = SELF_LEAVING_PEER_DOWN;
1422 case NODE_FAILOVER_END_EVT:
1423 state = SELF_UP_PEER_UP;
1425 case NODE_FAILOVER_BEGIN_EVT:
1426 case SELF_ESTABL_CONTACT_EVT:
1427 case PEER_ESTABL_CONTACT_EVT:
1429 case NODE_SYNCH_BEGIN_EVT:
1430 case NODE_SYNCH_END_EVT:
1437 case SELF_LOST_CONTACT_EVT:
1438 state = SELF_DOWN_PEER_LEAVING;
1440 case PEER_LOST_CONTACT_EVT:
1441 state = SELF_LEAVING_PEER_DOWN;
1443 case NODE_SYNCH_END_EVT:
1444 state = SELF_UP_PEER_UP;
1446 case NODE_FAILOVER_BEGIN_EVT:
1447 state = NODE_FAILINGOVER;
1449 case NODE_SYNCH_BEGIN_EVT:
1450 case SELF_ESTABL_CONTACT_EVT:
1451 case PEER_ESTABL_CONTACT_EVT:
1453 case NODE_FAILOVER_END_EVT:
1459 pr_err("Unknown node fsm state %x\n", state);
1462 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
1467 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1468 trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
1471 static void node_lost_contact(struct tipc_node *n,
1472 struct sk_buff_head *inputq)
1474 struct tipc_sock_conn *conn, *safe;
1475 struct tipc_link *l;
1476 struct list_head *conns = &n->conn_sks;
1477 struct sk_buff *skb;
1480 pr_debug("Lost contact with %x\n", n->addr);
1481 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1482 trace_tipc_node_lost_contact(n, true, " ");
1484 /* Clean up broadcast state */
1485 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1487 /* Abort any ongoing link failover */
1488 for (i = 0; i < MAX_BEARERS; i++) {
1489 l = n->links[i].link;
1491 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1494 /* Notify publications from this node */
1495 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1497 n->peer_hash_mix = 0;
1498 /* Notify sockets connected to node */
1499 list_for_each_entry_safe(conn, safe, conns, list) {
1500 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1501 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1502 conn->peer_node, conn->port,
1503 conn->peer_port, TIPC_ERR_NO_NODE);
1505 skb_queue_tail(inputq, skb);
1506 list_del(&conn->list);
1512 * tipc_node_get_linkname - get the name of a link
1514 * @bearer_id: id of the bearer
1515 * @node: peer node address
1516 * @linkname: link name output buffer
1518 * Returns 0 on success
1520 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1521 char *linkname, size_t len)
1523 struct tipc_link *link;
1525 struct tipc_node *node = tipc_node_find(net, addr);
1530 if (bearer_id >= MAX_BEARERS)
1533 tipc_node_read_lock(node);
1534 link = node->links[bearer_id].link;
1536 strncpy(linkname, tipc_link_name(link), len);
1539 tipc_node_read_unlock(node);
1541 tipc_node_put(node);
1545 /* Caller should hold node lock for the passed node */
1546 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1549 struct nlattr *attrs;
1551 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1552 NLM_F_MULTI, TIPC_NL_NODE_GET);
1556 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
1560 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1562 if (node_is_up(node))
1563 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1566 nla_nest_end(msg->skb, attrs);
1567 genlmsg_end(msg->skb, hdr);
1572 nla_nest_cancel(msg->skb, attrs);
1574 genlmsg_cancel(msg->skb, hdr);
1579 static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
1581 struct tipc_msg *hdr = buf_msg(skb_peek(list));
1582 struct sk_buff_head inputq;
1584 switch (msg_user(hdr)) {
1585 case TIPC_LOW_IMPORTANCE:
1586 case TIPC_MEDIUM_IMPORTANCE:
1587 case TIPC_HIGH_IMPORTANCE:
1588 case TIPC_CRITICAL_IMPORTANCE:
1589 if (msg_connected(hdr) || msg_named(hdr) ||
1591 tipc_loopback_trace(peer_net, list);
1592 spin_lock_init(&list->lock);
1593 tipc_sk_rcv(peer_net, list);
1596 if (msg_mcast(hdr)) {
1597 tipc_loopback_trace(peer_net, list);
1598 skb_queue_head_init(&inputq);
1599 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1600 __skb_queue_purge(list);
1601 skb_queue_purge(&inputq);
1605 case MSG_FRAGMENTER:
1606 if (tipc_msg_assemble(list)) {
1607 tipc_loopback_trace(peer_net, list);
1608 skb_queue_head_init(&inputq);
1609 tipc_sk_mcast_rcv(peer_net, list, &inputq);
1610 __skb_queue_purge(list);
1611 skb_queue_purge(&inputq);
1614 case GROUP_PROTOCOL:
1616 tipc_loopback_trace(peer_net, list);
1617 spin_lock_init(&list->lock);
1618 tipc_sk_rcv(peer_net, list);
1621 case NAME_DISTRIBUTOR:
1622 case TUNNEL_PROTOCOL:
1623 case BCAST_PROTOCOL:
1631 * tipc_node_xmit() is the general link level function for message sending
1632 * @net: the applicable net namespace
1633 * @list: chain of buffers containing message
1634 * @dnode: address of destination node
1635 * @selector: a number used for deterministic link selection
1636 * Consumes the buffer chain.
1637 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1639 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1640 u32 dnode, int selector)
1642 struct tipc_link_entry *le = NULL;
1643 struct tipc_node *n;
1644 struct sk_buff_head xmitq;
1645 bool node_up = false;
1649 if (in_own_node(net, dnode)) {
1650 tipc_loopback_trace(net, list);
1651 spin_lock_init(&list->lock);
1652 tipc_sk_rcv(net, list);
1656 n = tipc_node_find(net, dnode);
1658 __skb_queue_purge(list);
1659 return -EHOSTUNREACH;
1662 tipc_node_read_lock(n);
1663 node_up = node_is_up(n);
1664 if (node_up && n->peer_net && check_net(n->peer_net)) {
1665 /* xmit inner linux container */
1666 tipc_lxc_xmit(n->peer_net, list);
1667 if (likely(skb_queue_empty(list))) {
1668 tipc_node_read_unlock(n);
1674 bearer_id = n->active_links[selector & 1];
1675 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1676 tipc_node_read_unlock(n);
1678 __skb_queue_purge(list);
1679 return -EHOSTUNREACH;
1682 __skb_queue_head_init(&xmitq);
1683 le = &n->links[bearer_id];
1684 spin_lock_bh(&le->lock);
1685 rc = tipc_link_xmit(le->link, list, &xmitq);
1686 spin_unlock_bh(&le->lock);
1687 tipc_node_read_unlock(n);
1689 if (unlikely(rc == -ENOBUFS))
1690 tipc_node_link_down(n, bearer_id, false);
1692 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
1699 /* tipc_node_xmit_skb(): send single buffer to destination
1700 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1701 * messages, which will not be rejected
1702 * The only exception is datagram messages rerouted after secondary
1703 * lookup, which are rare and safe to dispose of anyway.
1705 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1708 struct sk_buff_head head;
1710 __skb_queue_head_init(&head);
1711 __skb_queue_tail(&head, skb);
1712 tipc_node_xmit(net, &head, dnode, selector);
1716 /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1717 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1719 int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1721 struct sk_buff *skb;
1722 u32 selector, dnode;
1724 while ((skb = __skb_dequeue(xmitq))) {
1725 selector = msg_origport(buf_msg(skb));
1726 dnode = msg_destnode(buf_msg(skb));
1727 tipc_node_xmit_skb(net, skb, dnode, selector);
1732 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1734 struct sk_buff *txskb;
1735 struct tipc_node *n;
1739 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1741 if (in_own_node(net, dst))
1745 txskb = pskb_copy(skb, GFP_ATOMIC);
1748 msg_set_destnode(buf_msg(txskb), dst);
1749 tipc_node_xmit_skb(net, txskb, dst, 0);
1756 static void tipc_node_mcast_rcv(struct tipc_node *n)
1758 struct tipc_bclink_entry *be = &n->bc_entry;
1760 /* 'arrvq' is under inputq2's lock protection */
1761 spin_lock_bh(&be->inputq2.lock);
1762 spin_lock_bh(&be->inputq1.lock);
1763 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1764 spin_unlock_bh(&be->inputq1.lock);
1765 spin_unlock_bh(&be->inputq2.lock);
1766 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1769 static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1770 int bearer_id, struct sk_buff_head *xmitq)
1772 struct tipc_link *ucl;
1775 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr, xmitq);
1777 if (rc & TIPC_LINK_DOWN_EVT) {
1778 tipc_node_reset_links(n);
1782 if (!(rc & TIPC_LINK_SND_STATE))
1785 /* If probe message, a STATE response will be sent anyway */
1789 /* Produce a STATE message carrying broadcast NACK */
1790 tipc_node_read_lock(n);
1791 ucl = n->links[bearer_id].link;
1793 tipc_link_build_state_msg(ucl, xmitq);
1794 tipc_node_read_unlock(n);
1798 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1799 * @net: the applicable net namespace
1801 * @bearer_id: id of bearer message arrived on
1803 * Invoked with no locks held.
1805 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1808 struct sk_buff_head xmitq;
1809 struct tipc_bclink_entry *be;
1810 struct tipc_link_entry *le;
1811 struct tipc_msg *hdr = buf_msg(skb);
1812 int usr = msg_user(hdr);
1813 u32 dnode = msg_destnode(hdr);
1814 struct tipc_node *n;
1816 __skb_queue_head_init(&xmitq);
1818 /* If NACK for other node, let rcv link for that node peek into it */
1819 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1820 n = tipc_node_find(net, dnode);
1822 n = tipc_node_find(net, msg_prevnode(hdr));
1828 le = &n->links[bearer_id];
1830 rc = tipc_bcast_rcv(net, be->link, skb);
1832 /* Broadcast ACKs are sent on a unicast link */
1833 if (rc & TIPC_LINK_SND_STATE) {
1834 tipc_node_read_lock(n);
1835 tipc_link_build_state_msg(le->link, &xmitq);
1836 tipc_node_read_unlock(n);
1839 if (!skb_queue_empty(&xmitq))
1840 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
1842 if (!skb_queue_empty(&be->inputq1))
1843 tipc_node_mcast_rcv(n);
1845 /* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
1846 if (!skb_queue_empty(&n->bc_entry.namedq))
1847 tipc_named_rcv(net, &n->bc_entry.namedq);
1849 /* If reassembly or retransmission failure => reset all links to peer */
1850 if (rc & TIPC_LINK_DOWN_EVT)
1851 tipc_node_reset_links(n);
1857 * tipc_node_check_state - check and if necessary update node state
1859 * @bearer_id: identity of bearer delivering the packet
1860 * Returns true if state and msg are ok, otherwise false
1862 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1863 int bearer_id, struct sk_buff_head *xmitq)
1865 struct tipc_msg *hdr = buf_msg(skb);
1866 int usr = msg_user(hdr);
1867 int mtyp = msg_type(hdr);
1868 u16 oseqno = msg_seqno(hdr);
1869 u16 exp_pkts = msg_msgcnt(hdr);
1870 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1871 int state = n->state;
1872 struct tipc_link *l, *tnl, *pl = NULL;
1873 struct tipc_media_addr *maddr;
1876 if (trace_tipc_node_check_state_enabled()) {
1877 trace_tipc_skb_dump(skb, false, "skb for node state check");
1878 trace_tipc_node_check_state(n, true, " ");
1880 l = n->links[bearer_id].link;
1883 rcv_nxt = tipc_link_rcv_nxt(l);
1886 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1889 /* Find parallel link, if any */
1890 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1891 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1892 pl = n->links[pb_id].link;
1897 if (!tipc_link_validate_msg(l, hdr)) {
1898 trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
1899 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
1903 /* Check and update node accesibility if applicable */
1904 if (state == SELF_UP_PEER_COMING) {
1905 if (!tipc_link_is_up(l))
1907 if (!msg_peer_link_is_up(hdr))
1909 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1912 if (state == SELF_DOWN_PEER_LEAVING) {
1913 if (msg_peer_node_is_up(hdr))
1915 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1919 if (state == SELF_LEAVING_PEER_DOWN)
1922 /* Ignore duplicate packets */
1923 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1926 /* Initiate or update failover mode if applicable */
1927 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1928 syncpt = oseqno + exp_pkts - 1;
1929 if (pl && !tipc_link_is_reset(pl)) {
1930 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1931 trace_tipc_node_link_down(n, true,
1932 "node link down <- failover!");
1933 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1934 tipc_link_inputq(l));
1937 /* If parallel link was already down, and this happened before
1938 * the tunnel link came up, node failover was never started.
1939 * Ensure that a FAILOVER_MSG is sent to get peer out of
1940 * NODE_FAILINGOVER state, also this node must accept
1941 * TUNNEL_MSGs from peer.
1943 if (n->state != NODE_FAILINGOVER)
1944 tipc_node_link_failover(n, pl, l, xmitq);
1946 /* If pkts arrive out of order, use lowest calculated syncpt */
1947 if (less(syncpt, n->sync_point))
1948 n->sync_point = syncpt;
1951 /* Open parallel link when tunnel link reaches synch point */
1952 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1953 if (!more(rcv_nxt, n->sync_point))
1955 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1957 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1961 /* No synching needed if only one link */
1962 if (!pl || !tipc_link_is_up(pl))
1965 /* Initiate synch mode if applicable */
1966 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1967 if (n->capabilities & TIPC_TUNNEL_ENHANCED)
1968 syncpt = msg_syncpt(hdr);
1970 syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
1971 if (!tipc_link_is_up(l))
1972 __tipc_node_link_up(n, bearer_id, xmitq);
1973 if (n->state == SELF_UP_PEER_UP) {
1974 n->sync_point = syncpt;
1975 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1976 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1980 /* Open tunnel link when parallel link reaches synch point */
1981 if (n->state == NODE_SYNCHING) {
1982 if (tipc_link_is_synching(l)) {
1988 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1989 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1990 if (more(dlv_nxt, n->sync_point)) {
1991 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1992 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1997 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1999 if (usr == LINK_PROTOCOL)
2007 * tipc_rcv - process TIPC packets/messages arriving from off-node
2008 * @net: the applicable net namespace
2010 * @bearer: pointer to bearer message arrived on
2012 * Invoked with no locks held. Bearer pointer must point to a valid bearer
2013 * structure (i.e. cannot be NULL), but bearer can be inactive.
2015 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
2017 struct sk_buff_head xmitq;
2018 struct tipc_link_entry *le;
2019 struct tipc_msg *hdr;
2020 struct tipc_node *n;
2021 int bearer_id = b->identity;
2022 u32 self = tipc_own_addr(net);
2025 #ifdef CONFIG_TIPC_CRYPTO
2026 struct tipc_ehdr *ehdr;
2028 /* Check if message must be decrypted first */
2029 if (TIPC_SKB_CB(skb)->decrypted || !tipc_ehdr_validate(skb))
2032 ehdr = (struct tipc_ehdr *)skb->data;
2033 if (likely(ehdr->user != LINK_CONFIG)) {
2034 n = tipc_node_find(net, ntohl(ehdr->addr));
2038 n = tipc_node_find_by_id(net, ehdr->id);
2040 tipc_crypto_rcv(net, (n) ? n->crypto_rx : NULL, &skb, b);
2046 /* Ensure message is well-formed before touching the header */
2047 if (unlikely(!tipc_msg_validate(&skb)))
2049 __skb_queue_head_init(&xmitq);
2051 usr = msg_user(hdr);
2052 bc_ack = msg_bcast_ack(hdr);
2054 /* Handle arrival of discovery or broadcast packet */
2055 if (unlikely(msg_non_seq(hdr))) {
2056 if (unlikely(usr == LINK_CONFIG))
2057 return tipc_disc_rcv(net, skb, b);
2059 return tipc_node_bc_rcv(net, skb, bearer_id);
2062 /* Discard unicast link messages destined for another node */
2063 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
2066 /* Locate neighboring node that sent packet */
2067 n = tipc_node_find(net, msg_prevnode(hdr));
2070 le = &n->links[bearer_id];
2072 /* Ensure broadcast reception is in synch with peer's send state */
2073 if (unlikely(usr == LINK_PROTOCOL)) {
2074 if (unlikely(skb_linearize(skb))) {
2079 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
2080 } else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack)) {
2081 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
2084 /* Receive packet directly if conditions permit */
2085 tipc_node_read_lock(n);
2086 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
2087 spin_lock_bh(&le->lock);
2089 rc = tipc_link_rcv(le->link, skb, &xmitq);
2092 spin_unlock_bh(&le->lock);
2094 tipc_node_read_unlock(n);
2096 /* Check/update node state before receiving */
2097 if (unlikely(skb)) {
2098 if (unlikely(skb_linearize(skb)))
2100 tipc_node_write_lock(n);
2101 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
2103 rc = tipc_link_rcv(le->link, skb, &xmitq);
2107 tipc_node_write_unlock(n);
2110 if (unlikely(rc & TIPC_LINK_UP_EVT))
2111 tipc_node_link_up(n, bearer_id, &xmitq);
2113 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
2114 tipc_node_link_down(n, bearer_id, false);
2116 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
2117 tipc_named_rcv(net, &n->bc_entry.namedq);
2119 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
2120 tipc_node_mcast_rcv(n);
2122 if (!skb_queue_empty(&le->inputq))
2123 tipc_sk_rcv(net, &le->inputq);
2125 if (!skb_queue_empty(&xmitq))
2126 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
2134 void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
2137 struct tipc_net *tn = tipc_net(net);
2138 int bearer_id = b->identity;
2139 struct sk_buff_head xmitq;
2140 struct tipc_link_entry *e;
2141 struct tipc_node *n;
2143 __skb_queue_head_init(&xmitq);
2147 list_for_each_entry_rcu(n, &tn->node_list, list) {
2148 tipc_node_write_lock(n);
2149 e = &n->links[bearer_id];
2151 if (prop == TIPC_NLA_PROP_TOL)
2152 tipc_link_set_tolerance(e->link, b->tolerance,
2154 else if (prop == TIPC_NLA_PROP_MTU)
2155 tipc_link_set_mtu(e->link, b->mtu);
2157 tipc_node_write_unlock(n);
2158 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr, NULL);
2164 int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
2166 struct net *net = sock_net(skb->sk);
2167 struct tipc_net *tn = net_generic(net, tipc_net_id);
2168 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
2169 struct tipc_node *peer, *temp_node;
2173 /* We identify the peer by its net */
2174 if (!info->attrs[TIPC_NLA_NET])
2177 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
2178 info->attrs[TIPC_NLA_NET],
2179 tipc_nl_net_policy, info->extack);
2183 if (!attrs[TIPC_NLA_NET_ADDR])
2186 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
2188 if (in_own_node(net, addr))
2191 spin_lock_bh(&tn->node_list_lock);
2192 peer = tipc_node_find(net, addr);
2194 spin_unlock_bh(&tn->node_list_lock);
2198 tipc_node_write_lock(peer);
2199 if (peer->state != SELF_DOWN_PEER_DOWN &&
2200 peer->state != SELF_DOWN_PEER_LEAVING) {
2201 tipc_node_write_unlock(peer);
2206 tipc_node_clear_links(peer);
2207 tipc_node_write_unlock(peer);
2208 tipc_node_delete(peer);
2210 /* Calculate cluster capabilities */
2211 tn->capabilities = TIPC_NODE_CAPABILITIES;
2212 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
2213 tn->capabilities &= temp_node->capabilities;
2215 tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
2218 tipc_node_put(peer);
2219 spin_unlock_bh(&tn->node_list_lock);
2224 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
2227 struct net *net = sock_net(skb->sk);
2228 struct tipc_net *tn = net_generic(net, tipc_net_id);
2229 int done = cb->args[0];
2230 int last_addr = cb->args[1];
2231 struct tipc_node *node;
2232 struct tipc_nl_msg msg;
2238 msg.portid = NETLINK_CB(cb->skb).portid;
2239 msg.seq = cb->nlh->nlmsg_seq;
2243 node = tipc_node_find(net, last_addr);
2246 /* We never set seq or call nl_dump_check_consistent()
2247 * this means that setting prev_seq here will cause the
2248 * consistence check to fail in the netlink callback
2249 * handler. Resulting in the NLMSG_DONE message having
2250 * the NLM_F_DUMP_INTR flag set if the node state
2251 * changed while we released the lock.
2256 tipc_node_put(node);
2259 list_for_each_entry_rcu(node, &tn->node_list, list) {
2260 if (node->preliminary)
2263 if (node->addr == last_addr)
2269 tipc_node_read_lock(node);
2270 err = __tipc_nl_add_node(&msg, node);
2272 last_addr = node->addr;
2273 tipc_node_read_unlock(node);
2277 tipc_node_read_unlock(node);
2282 cb->args[1] = last_addr;
2288 /* tipc_node_find_by_name - locate owner node of link by link's name
2289 * @net: the applicable net namespace
2290 * @name: pointer to link name string
2291 * @bearer_id: pointer to index in 'node->links' array where the link was found.
2293 * Returns pointer to node owning the link, or 0 if no matching link is found.
2295 static struct tipc_node *tipc_node_find_by_name(struct net *net,
2296 const char *link_name,
2297 unsigned int *bearer_id)
2299 struct tipc_net *tn = net_generic(net, tipc_net_id);
2300 struct tipc_link *l;
2301 struct tipc_node *n;
2302 struct tipc_node *found_node = NULL;
2307 list_for_each_entry_rcu(n, &tn->node_list, list) {
2308 tipc_node_read_lock(n);
2309 for (i = 0; i < MAX_BEARERS; i++) {
2310 l = n->links[i].link;
2311 if (l && !strcmp(tipc_link_name(l), link_name)) {
2317 tipc_node_read_unlock(n);
2326 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
2332 struct tipc_link *link;
2333 struct tipc_node *node;
2334 struct sk_buff_head xmitq;
2335 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2336 struct net *net = sock_net(skb->sk);
2338 __skb_queue_head_init(&xmitq);
2340 if (!info->attrs[TIPC_NLA_LINK])
2343 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2344 info->attrs[TIPC_NLA_LINK],
2345 tipc_nl_link_policy, info->extack);
2349 if (!attrs[TIPC_NLA_LINK_NAME])
2352 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2354 if (strcmp(name, tipc_bclink_name) == 0)
2355 return tipc_nl_bc_link_set(net, attrs);
2357 node = tipc_node_find_by_name(net, name, &bearer_id);
2361 tipc_node_read_lock(node);
2363 link = node->links[bearer_id].link;
2369 if (attrs[TIPC_NLA_LINK_PROP]) {
2370 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2372 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
2378 if (props[TIPC_NLA_PROP_TOL]) {
2381 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2382 tipc_link_set_tolerance(link, tol, &xmitq);
2384 if (props[TIPC_NLA_PROP_PRIO]) {
2387 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2388 tipc_link_set_prio(link, prio, &xmitq);
2390 if (props[TIPC_NLA_PROP_WIN]) {
2393 max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2394 tipc_link_set_queue_limits(link,
2395 tipc_link_min_win(link),
2401 tipc_node_read_unlock(node);
2402 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr,
2407 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2409 struct net *net = genl_info_net(info);
2410 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2411 struct tipc_nl_msg msg;
2415 msg.portid = info->snd_portid;
2416 msg.seq = info->snd_seq;
2418 if (!info->attrs[TIPC_NLA_LINK])
2421 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2422 info->attrs[TIPC_NLA_LINK],
2423 tipc_nl_link_policy, info->extack);
2427 if (!attrs[TIPC_NLA_LINK_NAME])
2430 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2432 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2436 if (strcmp(name, tipc_bclink_name) == 0) {
2437 err = tipc_nl_add_bc_link(net, &msg, tipc_net(net)->bcl);
2442 struct tipc_node *node;
2443 struct tipc_link *link;
2445 node = tipc_node_find_by_name(net, name, &bearer_id);
2451 tipc_node_read_lock(node);
2452 link = node->links[bearer_id].link;
2454 tipc_node_read_unlock(node);
2459 err = __tipc_nl_add_link(net, &msg, link, 0);
2460 tipc_node_read_unlock(node);
2465 return genlmsg_reply(msg.skb, info);
2468 nlmsg_free(msg.skb);
2472 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2476 unsigned int bearer_id;
2477 struct tipc_link *link;
2478 struct tipc_node *node;
2479 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2480 struct net *net = sock_net(skb->sk);
2481 struct tipc_net *tn = tipc_net(net);
2482 struct tipc_link_entry *le;
2484 if (!info->attrs[TIPC_NLA_LINK])
2487 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
2488 info->attrs[TIPC_NLA_LINK],
2489 tipc_nl_link_policy, info->extack);
2493 if (!attrs[TIPC_NLA_LINK_NAME])
2496 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2499 if (!strcmp(link_name, tipc_bclink_name)) {
2500 err = tipc_bclink_reset_stats(net, tipc_bc_sndlink(net));
2504 } else if (strstr(link_name, tipc_bclink_name)) {
2506 list_for_each_entry_rcu(node, &tn->node_list, list) {
2507 tipc_node_read_lock(node);
2508 link = node->bc_entry.link;
2509 if (link && !strcmp(link_name, tipc_link_name(link))) {
2510 err = tipc_bclink_reset_stats(net, link);
2511 tipc_node_read_unlock(node);
2514 tipc_node_read_unlock(node);
2520 node = tipc_node_find_by_name(net, link_name, &bearer_id);
2524 le = &node->links[bearer_id];
2525 tipc_node_read_lock(node);
2526 spin_lock_bh(&le->lock);
2527 link = node->links[bearer_id].link;
2529 spin_unlock_bh(&le->lock);
2530 tipc_node_read_unlock(node);
2533 tipc_link_reset_stats(link);
2534 spin_unlock_bh(&le->lock);
2535 tipc_node_read_unlock(node);
2539 /* Caller should hold node lock */
2540 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2541 struct tipc_node *node, u32 *prev_link,
2547 for (i = *prev_link; i < MAX_BEARERS; i++) {
2550 if (!node->links[i].link)
2553 err = __tipc_nl_add_link(net, msg,
2554 node->links[i].link, NLM_F_MULTI);
2561 err = tipc_nl_add_bc_link(net, msg, node->bc_entry.link);
2571 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
2573 struct net *net = sock_net(skb->sk);
2574 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
2575 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
2576 struct tipc_net *tn = net_generic(net, tipc_net_id);
2577 struct tipc_node *node;
2578 struct tipc_nl_msg msg;
2579 u32 prev_node = cb->args[0];
2580 u32 prev_link = cb->args[1];
2581 int done = cb->args[2];
2582 bool bc_link = cb->args[3];
2589 /* Check if broadcast-receiver links dumping is needed */
2590 if (attrs && attrs[TIPC_NLA_LINK]) {
2591 err = nla_parse_nested_deprecated(link,
2593 attrs[TIPC_NLA_LINK],
2594 tipc_nl_link_policy,
2598 if (unlikely(!link[TIPC_NLA_LINK_BROADCAST]))
2605 msg.portid = NETLINK_CB(cb->skb).portid;
2606 msg.seq = cb->nlh->nlmsg_seq;
2610 node = tipc_node_find(net, prev_node);
2612 /* We never set seq or call nl_dump_check_consistent()
2613 * this means that setting prev_seq here will cause the
2614 * consistence check to fail in the netlink callback
2615 * handler. Resulting in the last NLMSG_DONE message
2616 * having the NLM_F_DUMP_INTR flag set.
2621 tipc_node_put(node);
2623 list_for_each_entry_continue_rcu(node, &tn->node_list,
2625 tipc_node_read_lock(node);
2626 err = __tipc_nl_add_node_links(net, &msg, node,
2627 &prev_link, bc_link);
2628 tipc_node_read_unlock(node);
2632 prev_node = node->addr;
2635 err = tipc_nl_add_bc_link(net, &msg, tn->bcl);
2639 list_for_each_entry_rcu(node, &tn->node_list, list) {
2640 tipc_node_read_lock(node);
2641 err = __tipc_nl_add_node_links(net, &msg, node,
2642 &prev_link, bc_link);
2643 tipc_node_read_unlock(node);
2647 prev_node = node->addr;
2654 cb->args[0] = prev_node;
2655 cb->args[1] = prev_link;
2657 cb->args[3] = bc_link;
2662 int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2664 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2665 struct net *net = sock_net(skb->sk);
2668 if (!info->attrs[TIPC_NLA_MON])
2671 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
2672 info->attrs[TIPC_NLA_MON],
2673 tipc_nl_monitor_policy,
2678 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2681 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2682 err = tipc_nl_monitor_set_threshold(net, val);
2690 static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2692 struct nlattr *attrs;
2696 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2697 0, TIPC_NL_MON_GET);
2701 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
2705 val = tipc_nl_monitor_get_threshold(net);
2707 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2710 nla_nest_end(msg->skb, attrs);
2711 genlmsg_end(msg->skb, hdr);
2716 nla_nest_cancel(msg->skb, attrs);
2718 genlmsg_cancel(msg->skb, hdr);
2723 int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2725 struct net *net = sock_net(skb->sk);
2726 struct tipc_nl_msg msg;
2729 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2732 msg.portid = info->snd_portid;
2733 msg.seq = info->snd_seq;
2735 err = __tipc_nl_add_monitor_prop(net, &msg);
2737 nlmsg_free(msg.skb);
2741 return genlmsg_reply(msg.skb, info);
2744 int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2746 struct net *net = sock_net(skb->sk);
2747 u32 prev_bearer = cb->args[0];
2748 struct tipc_nl_msg msg;
2752 if (prev_bearer == MAX_BEARERS)
2756 msg.portid = NETLINK_CB(cb->skb).portid;
2757 msg.seq = cb->nlh->nlmsg_seq;
2760 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
2761 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2766 cb->args[0] = bearer_id;
2771 int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2772 struct netlink_callback *cb)
2774 struct net *net = sock_net(skb->sk);
2775 u32 prev_node = cb->args[1];
2776 u32 bearer_id = cb->args[2];
2777 int done = cb->args[0];
2778 struct tipc_nl_msg msg;
2782 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
2783 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2785 if (!attrs[TIPC_NLA_MON])
2788 err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
2789 attrs[TIPC_NLA_MON],
2790 tipc_nl_monitor_policy,
2795 if (!mon[TIPC_NLA_MON_REF])
2798 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2800 if (bearer_id >= MAX_BEARERS)
2808 msg.portid = NETLINK_CB(cb->skb).portid;
2809 msg.seq = cb->nlh->nlmsg_seq;
2812 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2818 cb->args[1] = prev_node;
2819 cb->args[2] = bearer_id;
2824 #ifdef CONFIG_TIPC_CRYPTO
2825 static int tipc_nl_retrieve_key(struct nlattr **attrs,
2826 struct tipc_aead_key **key)
2828 struct nlattr *attr = attrs[TIPC_NLA_NODE_KEY];
2833 *key = (struct tipc_aead_key *)nla_data(attr);
2834 if (nla_len(attr) < tipc_aead_key_size(*key))
2840 static int tipc_nl_retrieve_nodeid(struct nlattr **attrs, u8 **node_id)
2842 struct nlattr *attr = attrs[TIPC_NLA_NODE_ID];
2847 if (nla_len(attr) < TIPC_NODEID_LEN)
2850 *node_id = (u8 *)nla_data(attr);
2854 static int __tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
2856 struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1];
2857 struct net *net = sock_net(skb->sk);
2858 struct tipc_net *tn = tipc_net(net);
2859 struct tipc_node *n = NULL;
2860 struct tipc_aead_key *ukey;
2861 struct tipc_crypto *c;
2865 if (!info->attrs[TIPC_NLA_NODE])
2868 rc = nla_parse_nested(attrs, TIPC_NLA_NODE_MAX,
2869 info->attrs[TIPC_NLA_NODE],
2870 tipc_nl_node_policy, info->extack);
2874 own_id = tipc_own_id(net);
2880 rc = tipc_nl_retrieve_key(attrs, &ukey);
2884 rc = tipc_aead_key_validate(ukey);
2888 rc = tipc_nl_retrieve_nodeid(attrs, &id);
2891 /* Cluster key mode */
2892 rc = tipc_crypto_key_init(tn->crypto_tx, ukey, CLUSTER_KEY);
2895 /* Per-node key mode */
2896 if (!memcmp(id, own_id, NODE_ID_LEN)) {
2899 n = tipc_node_find_by_id(net, id) ?:
2900 tipc_node_create(net, 0, id, 0xffffu, 0, true);
2908 rc = tipc_crypto_key_init(c, ukey, PER_NODE_KEY);
2917 return (rc < 0) ? rc : 0;
2920 int tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
2925 err = __tipc_nl_node_set_key(skb, info);
2931 static int __tipc_nl_node_flush_key(struct sk_buff *skb,
2932 struct genl_info *info)
2934 struct net *net = sock_net(skb->sk);
2935 struct tipc_net *tn = tipc_net(net);
2936 struct tipc_node *n;
2938 tipc_crypto_key_flush(tn->crypto_tx);
2940 list_for_each_entry_rcu(n, &tn->node_list, list)
2941 tipc_crypto_key_flush(n->crypto_rx);
2944 pr_info("All keys are flushed!\n");
2948 int tipc_nl_node_flush_key(struct sk_buff *skb, struct genl_info *info)
2953 err = __tipc_nl_node_flush_key(skb, info);
2961 * tipc_node_dump - dump TIPC node data
2962 * @n: tipc node to be dumped
2964 * - false: dump only tipc node data
2965 * - true: dump node link data as well
2966 * @buf: returned buffer of dump data in format
2968 int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
2971 size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
2974 i += scnprintf(buf, sz, "node data: (null)\n");
2978 i += scnprintf(buf, sz, "node data: %x", n->addr);
2979 i += scnprintf(buf + i, sz - i, " %x", n->state);
2980 i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
2981 i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
2982 i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
2983 i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
2984 i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
2985 i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
2986 i += scnprintf(buf + i, sz - i, " %u", n->working_links);
2987 i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
2988 i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
2993 i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
2994 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
2995 i += scnprintf(buf + i, sz - i, " media: ");
2996 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
2997 i += scnprintf(buf + i, sz - i, "\n");
2998 i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
2999 i += scnprintf(buf + i, sz - i, " inputq: ");
3000 i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
3002 i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
3003 i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
3004 i += scnprintf(buf + i, sz - i, " media: ");
3005 i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
3006 i += scnprintf(buf + i, sz - i, "\n");
3007 i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
3008 i += scnprintf(buf + i, sz - i, " inputq: ");
3009 i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
3011 i += scnprintf(buf + i, sz - i, "bclink:\n ");
3012 i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
3017 void tipc_node_pre_cleanup_net(struct net *exit_net)
3019 struct tipc_node *n;
3020 struct tipc_net *tn;
3024 for_each_net_rcu(tmp) {
3025 if (tmp == exit_net)
3030 spin_lock_bh(&tn->node_list_lock);
3031 list_for_each_entry_rcu(n, &tn->node_list, list) {
3034 if (n->peer_net != exit_net)
3036 tipc_node_write_lock(n);
3038 n->peer_hash_mix = 0;
3039 tipc_node_write_unlock_fast(n);
3042 spin_unlock_bh(&tn->node_list_lock);