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"
47 #define INVALID_NODE_SIG 0x10000
48 #define NODE_CLEANUP_AFTER 300000
50 /* Flags used to take different actions according to flag type
51 * TIPC_NOTIFY_NODE_DOWN: notify node is down
52 * TIPC_NOTIFY_NODE_UP: notify node is up
53 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
56 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
57 TIPC_NOTIFY_NODE_UP = (1 << 4),
58 TIPC_NOTIFY_LINK_UP = (1 << 6),
59 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
62 struct tipc_link_entry {
63 struct tipc_link *link;
64 spinlock_t lock; /* per link */
66 struct sk_buff_head inputq;
67 struct tipc_media_addr maddr;
70 struct tipc_bclink_entry {
71 struct tipc_link *link;
72 struct sk_buff_head inputq1;
73 struct sk_buff_head arrvq;
74 struct sk_buff_head inputq2;
75 struct sk_buff_head namedq;
79 * struct tipc_node - TIPC node structure
80 * @addr: network address of node
81 * @ref: reference counter to node object
82 * @lock: rwlock governing access to structure
83 * @net: the applicable net namespace
84 * @hash: links to adjacent nodes in unsorted hash chain
85 * @inputq: pointer to input queue containing messages for msg event
86 * @namedq: pointer to name table input queue with name table messages
87 * @active_links: bearer ids of active links, used as index into links[] array
88 * @links: array containing references to all links to node
89 * @action_flags: bit mask of different types of node actions
90 * @state: connectivity state vs peer node
91 * @sync_point: sequence number where synch/failover is finished
92 * @list: links to adjacent nodes in sorted list of cluster's nodes
93 * @working_links: number of working links to node (both active and standby)
94 * @link_cnt: number of links to node
95 * @capabilities: bitmap, indicating peer node's functional capabilities
96 * @signature: node instance identifier
97 * @link_id: local and remote bearer ids of changing link, if any
98 * @publ_list: list of publications
99 * @rcu: rcu struct for tipc_node
100 * @delete_at: indicates the time for deleting a down node
107 struct hlist_node hash;
109 struct tipc_link_entry links[MAX_BEARERS];
110 struct tipc_bclink_entry bc_entry;
112 struct list_head list;
121 struct list_head publ_list;
122 struct list_head conn_sks;
123 unsigned long keepalive_intv;
124 struct timer_list timer;
126 unsigned long delete_at;
129 /* Node FSM states and events:
132 SELF_DOWN_PEER_DOWN = 0xdd,
133 SELF_UP_PEER_UP = 0xaa,
134 SELF_DOWN_PEER_LEAVING = 0xd1,
135 SELF_UP_PEER_COMING = 0xac,
136 SELF_COMING_PEER_UP = 0xca,
137 SELF_LEAVING_PEER_DOWN = 0x1d,
138 NODE_FAILINGOVER = 0xf0,
143 SELF_ESTABL_CONTACT_EVT = 0xece,
144 SELF_LOST_CONTACT_EVT = 0x1ce,
145 PEER_ESTABL_CONTACT_EVT = 0x9ece,
146 PEER_LOST_CONTACT_EVT = 0x91ce,
147 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
148 NODE_FAILOVER_END_EVT = 0xfee,
149 NODE_SYNCH_BEGIN_EVT = 0xcbe,
150 NODE_SYNCH_END_EVT = 0xcee
153 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
154 struct sk_buff_head *xmitq,
155 struct tipc_media_addr **maddr);
156 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
158 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
159 static void tipc_node_delete(struct tipc_node *node);
160 static void tipc_node_timeout(struct timer_list *t);
161 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
162 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
163 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
164 static void tipc_node_put(struct tipc_node *node);
165 static bool node_is_up(struct tipc_node *n);
166 static void tipc_node_delete_from_list(struct tipc_node *node);
168 struct tipc_sock_conn {
172 struct list_head list;
175 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
177 int bearer_id = n->active_links[sel & 1];
179 if (unlikely(bearer_id == INVALID_BEARER_ID))
182 return n->links[bearer_id].link;
185 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
189 unsigned int mtu = MAX_MSG_SIZE;
191 n = tipc_node_find(net, addr);
195 bearer_id = n->active_links[sel & 1];
196 if (likely(bearer_id != INVALID_BEARER_ID))
197 mtu = n->links[bearer_id].mtu;
202 bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
204 u8 *own_id = tipc_own_id(net);
210 if (addr == tipc_own_addr(net)) {
211 memcpy(id, own_id, TIPC_NODEID_LEN);
214 n = tipc_node_find(net, addr);
218 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
223 u16 tipc_node_get_capabilities(struct net *net, u32 addr)
228 n = tipc_node_find(net, addr);
230 return TIPC_NODE_CAPABILITIES;
231 caps = n->capabilities;
236 static void tipc_node_kref_release(struct kref *kref)
238 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
240 kfree(n->bc_entry.link);
244 static void tipc_node_put(struct tipc_node *node)
246 kref_put(&node->kref, tipc_node_kref_release);
249 static void tipc_node_get(struct tipc_node *node)
251 kref_get(&node->kref);
255 * tipc_node_find - locate specified node object, if it exists
257 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
259 struct tipc_net *tn = tipc_net(net);
260 struct tipc_node *node;
261 unsigned int thash = tipc_hashfn(addr);
264 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
265 if (node->addr != addr)
267 if (!kref_get_unless_zero(&node->kref))
275 /* tipc_node_find_by_id - locate specified node object by its 128-bit id
276 * Note: this function is called only when a discovery request failed
277 * to find the node by its 32-bit id, and is not time critical
279 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
281 struct tipc_net *tn = tipc_net(net);
286 list_for_each_entry_rcu(n, &tn->node_list, list) {
287 read_lock_bh(&n->lock);
288 if (!memcmp(id, n->peer_id, 16) &&
289 kref_get_unless_zero(&n->kref))
291 read_unlock_bh(&n->lock);
296 return found ? n : NULL;
299 static void tipc_node_read_lock(struct tipc_node *n)
301 read_lock_bh(&n->lock);
304 static void tipc_node_read_unlock(struct tipc_node *n)
306 read_unlock_bh(&n->lock);
309 static void tipc_node_write_lock(struct tipc_node *n)
311 write_lock_bh(&n->lock);
314 static void tipc_node_write_unlock_fast(struct tipc_node *n)
316 write_unlock_bh(&n->lock);
319 static void tipc_node_write_unlock(struct tipc_node *n)
321 struct net *net = n->net;
323 u32 flags = n->action_flags;
326 struct list_head *publ_list;
328 if (likely(!flags)) {
329 write_unlock_bh(&n->lock);
334 link_id = n->link_id;
335 bearer_id = link_id & 0xffff;
336 publ_list = &n->publ_list;
338 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
339 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
341 write_unlock_bh(&n->lock);
343 if (flags & TIPC_NOTIFY_NODE_DOWN)
344 tipc_publ_notify(net, publ_list, addr);
346 if (flags & TIPC_NOTIFY_NODE_UP)
347 tipc_named_node_up(net, addr);
349 if (flags & TIPC_NOTIFY_LINK_UP) {
350 tipc_mon_peer_up(net, addr, bearer_id);
351 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
352 TIPC_NODE_SCOPE, link_id, link_id);
354 if (flags & TIPC_NOTIFY_LINK_DOWN) {
355 tipc_mon_peer_down(net, addr, bearer_id);
356 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
361 static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
362 u8 *peer_id, u16 capabilities)
364 struct tipc_net *tn = net_generic(net, tipc_net_id);
365 struct tipc_node *n, *temp_node;
370 spin_lock_bh(&tn->node_list_lock);
371 n = tipc_node_find(net, addr);
373 if (n->capabilities == capabilities)
375 /* Same node may come back with new capabilities */
376 write_lock_bh(&n->lock);
377 n->capabilities = capabilities;
378 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
379 l = n->links[bearer_id].link;
381 tipc_link_update_caps(l, capabilities);
383 write_unlock_bh(&n->lock);
386 n = kzalloc(sizeof(*n), GFP_ATOMIC);
388 pr_warn("Node creation failed, no memory\n");
392 memcpy(&n->peer_id, peer_id, 16);
394 n->capabilities = capabilities;
396 rwlock_init(&n->lock);
397 INIT_HLIST_NODE(&n->hash);
398 INIT_LIST_HEAD(&n->list);
399 INIT_LIST_HEAD(&n->publ_list);
400 INIT_LIST_HEAD(&n->conn_sks);
401 skb_queue_head_init(&n->bc_entry.namedq);
402 skb_queue_head_init(&n->bc_entry.inputq1);
403 __skb_queue_head_init(&n->bc_entry.arrvq);
404 skb_queue_head_init(&n->bc_entry.inputq2);
405 for (i = 0; i < MAX_BEARERS; i++)
406 spin_lock_init(&n->links[i].lock);
407 n->state = SELF_DOWN_PEER_LEAVING;
408 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
409 n->signature = INVALID_NODE_SIG;
410 n->active_links[0] = INVALID_BEARER_ID;
411 n->active_links[1] = INVALID_BEARER_ID;
412 if (!tipc_link_bc_create(net, tipc_own_addr(net),
414 tipc_link_window(tipc_bc_sndlink(net)),
416 &n->bc_entry.inputq1,
418 tipc_bc_sndlink(net),
419 &n->bc_entry.link)) {
420 pr_warn("Broadcast rcv link creation failed, no memory\n");
426 timer_setup(&n->timer, tipc_node_timeout, 0);
427 n->keepalive_intv = U32_MAX;
428 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
429 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
430 if (n->addr < temp_node->addr)
433 list_add_tail_rcu(&n->list, &temp_node->list);
435 spin_unlock_bh(&tn->node_list_lock);
439 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
441 unsigned long tol = tipc_link_tolerance(l);
442 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
444 /* Link with lowest tolerance determines timer interval */
445 if (intv < n->keepalive_intv)
446 n->keepalive_intv = intv;
448 /* Ensure link's abort limit corresponds to current tolerance */
449 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
452 static void tipc_node_delete_from_list(struct tipc_node *node)
454 list_del_rcu(&node->list);
455 hlist_del_rcu(&node->hash);
459 static void tipc_node_delete(struct tipc_node *node)
461 tipc_node_delete_from_list(node);
463 del_timer_sync(&node->timer);
467 void tipc_node_stop(struct net *net)
469 struct tipc_net *tn = tipc_net(net);
470 struct tipc_node *node, *t_node;
472 spin_lock_bh(&tn->node_list_lock);
473 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
474 tipc_node_delete(node);
475 spin_unlock_bh(&tn->node_list_lock);
478 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
482 if (in_own_node(net, addr))
485 n = tipc_node_find(net, addr);
487 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
490 tipc_node_write_lock(n);
491 list_add_tail(subscr, &n->publ_list);
492 tipc_node_write_unlock_fast(n);
496 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
500 if (in_own_node(net, addr))
503 n = tipc_node_find(net, addr);
505 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
508 tipc_node_write_lock(n);
509 list_del_init(subscr);
510 tipc_node_write_unlock_fast(n);
514 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
516 struct tipc_node *node;
517 struct tipc_sock_conn *conn;
520 if (in_own_node(net, dnode))
523 node = tipc_node_find(net, dnode);
525 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
526 return -EHOSTUNREACH;
528 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
533 conn->peer_node = dnode;
535 conn->peer_port = peer_port;
537 tipc_node_write_lock(node);
538 list_add_tail(&conn->list, &node->conn_sks);
539 tipc_node_write_unlock(node);
545 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
547 struct tipc_node *node;
548 struct tipc_sock_conn *conn, *safe;
550 if (in_own_node(net, dnode))
553 node = tipc_node_find(net, dnode);
557 tipc_node_write_lock(node);
558 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
559 if (port != conn->port)
561 list_del(&conn->list);
564 tipc_node_write_unlock(node);
568 static void tipc_node_clear_links(struct tipc_node *node)
572 for (i = 0; i < MAX_BEARERS; i++) {
573 struct tipc_link_entry *le = &node->links[i];
583 /* tipc_node_cleanup - delete nodes that does not
584 * have active links for NODE_CLEANUP_AFTER time
586 static int tipc_node_cleanup(struct tipc_node *peer)
588 struct tipc_net *tn = tipc_net(peer->net);
589 bool deleted = false;
591 spin_lock_bh(&tn->node_list_lock);
592 tipc_node_write_lock(peer);
594 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
595 tipc_node_clear_links(peer);
596 tipc_node_delete_from_list(peer);
599 tipc_node_write_unlock(peer);
600 spin_unlock_bh(&tn->node_list_lock);
604 /* tipc_node_timeout - handle expiration of node timer
606 static void tipc_node_timeout(struct timer_list *t)
608 struct tipc_node *n = from_timer(n, t, timer);
609 struct tipc_link_entry *le;
610 struct sk_buff_head xmitq;
611 int remains = n->link_cnt;
615 if (!node_is_up(n) && tipc_node_cleanup(n)) {
616 /*Removing the reference of Timer*/
621 __skb_queue_head_init(&xmitq);
623 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
624 tipc_node_read_lock(n);
625 le = &n->links[bearer_id];
627 spin_lock_bh(&le->lock);
628 /* Link tolerance may change asynchronously: */
629 tipc_node_calculate_timer(n, le->link);
630 rc = tipc_link_timeout(le->link, &xmitq);
631 spin_unlock_bh(&le->lock);
634 tipc_node_read_unlock(n);
635 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
636 if (rc & TIPC_LINK_DOWN_EVT)
637 tipc_node_link_down(n, bearer_id, false);
639 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
643 * __tipc_node_link_up - handle addition of link
644 * Node lock must be held by caller
645 * Link becomes active (alone or shared) or standby, depending on its priority.
647 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
648 struct sk_buff_head *xmitq)
650 int *slot0 = &n->active_links[0];
651 int *slot1 = &n->active_links[1];
652 struct tipc_link *ol = node_active_link(n, 0);
653 struct tipc_link *nl = n->links[bearer_id].link;
655 if (!nl || tipc_link_is_up(nl))
658 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
659 if (!tipc_link_is_up(nl))
663 n->action_flags |= TIPC_NOTIFY_LINK_UP;
664 n->link_id = tipc_link_id(nl);
666 /* Leave room for tunnel header when returning 'mtu' to users: */
667 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
669 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
670 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
672 pr_debug("Established link <%s> on network plane %c\n",
673 tipc_link_name(nl), tipc_link_plane(nl));
675 /* Ensure that a STATE message goes first */
676 tipc_link_build_state_msg(nl, xmitq);
678 /* First link? => give it both slots */
682 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
683 n->action_flags |= TIPC_NOTIFY_NODE_UP;
684 tipc_link_set_active(nl, true);
685 tipc_bcast_add_peer(n->net, nl, xmitq);
689 /* Second link => redistribute slots */
690 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
691 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
694 tipc_link_set_active(nl, true);
695 tipc_link_set_active(ol, false);
696 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
697 tipc_link_set_active(nl, true);
700 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
703 /* Prepare synchronization with first link */
704 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
708 * tipc_node_link_up - handle addition of link
710 * Link becomes active (alone or shared) or standby, depending on its priority.
712 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
713 struct sk_buff_head *xmitq)
715 struct tipc_media_addr *maddr;
717 tipc_node_write_lock(n);
718 __tipc_node_link_up(n, bearer_id, xmitq);
719 maddr = &n->links[bearer_id].maddr;
720 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
721 tipc_node_write_unlock(n);
725 * __tipc_node_link_down - handle loss of link
727 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
728 struct sk_buff_head *xmitq,
729 struct tipc_media_addr **maddr)
731 struct tipc_link_entry *le = &n->links[*bearer_id];
732 int *slot0 = &n->active_links[0];
733 int *slot1 = &n->active_links[1];
734 int i, highest = 0, prio;
735 struct tipc_link *l, *_l, *tnl;
737 l = n->links[*bearer_id].link;
738 if (!l || tipc_link_is_reset(l))
742 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
743 n->link_id = tipc_link_id(l);
745 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
747 pr_debug("Lost link <%s> on network plane %c\n",
748 tipc_link_name(l), tipc_link_plane(l));
750 /* Select new active link if any available */
751 *slot0 = INVALID_BEARER_ID;
752 *slot1 = INVALID_BEARER_ID;
753 for (i = 0; i < MAX_BEARERS; i++) {
754 _l = n->links[i].link;
755 if (!_l || !tipc_link_is_up(_l))
759 prio = tipc_link_prio(_l);
762 if (prio > highest) {
771 if (!node_is_up(n)) {
772 if (tipc_link_peer_is_down(l))
773 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
774 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
775 tipc_link_fsm_evt(l, LINK_RESET_EVT);
777 tipc_link_build_reset_msg(l, xmitq);
778 *maddr = &n->links[*bearer_id].maddr;
779 node_lost_contact(n, &le->inputq);
780 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
783 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
785 /* There is still a working link => initiate failover */
786 *bearer_id = n->active_links[0];
787 tnl = n->links[*bearer_id].link;
788 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
789 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
790 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
791 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
793 tipc_link_fsm_evt(l, LINK_RESET_EVT);
794 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
795 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
796 *maddr = &n->links[*bearer_id].maddr;
799 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
801 struct tipc_link_entry *le = &n->links[bearer_id];
802 struct tipc_link *l = le->link;
803 struct tipc_media_addr *maddr;
804 struct sk_buff_head xmitq;
805 int old_bearer_id = bearer_id;
810 __skb_queue_head_init(&xmitq);
812 tipc_node_write_lock(n);
813 if (!tipc_link_is_establishing(l)) {
814 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
821 /* Defuse pending tipc_node_link_up() */
822 tipc_link_fsm_evt(l, LINK_RESET_EVT);
824 tipc_node_write_unlock(n);
826 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
827 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
828 tipc_sk_rcv(n->net, &le->inputq);
831 static bool node_is_up(struct tipc_node *n)
833 return n->active_links[0] != INVALID_BEARER_ID;
836 bool tipc_node_is_up(struct net *net, u32 addr)
841 if (in_own_node(net, addr))
844 n = tipc_node_find(net, addr);
847 retval = node_is_up(n);
852 static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
856 addr ^= tipc_net(net)->random;
857 while ((n = tipc_node_find(net, addr))) {
864 /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
865 * Returns suggested address if any, otherwise 0
867 u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
869 struct tipc_net *tn = tipc_net(net);
872 /* Suggest new address if some other peer is using this one */
873 n = tipc_node_find(net, addr);
875 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
880 return tipc_node_suggest_addr(net, addr);
883 /* Suggest previously used address if peer is known */
884 n = tipc_node_find_by_id(net, id);
891 /* Even this node may be in conflict */
892 if (tn->trial_addr == addr)
893 return tipc_node_suggest_addr(net, addr);
898 void tipc_node_check_dest(struct net *net, u32 addr,
899 u8 *peer_id, struct tipc_bearer *b,
900 u16 capabilities, u32 signature,
901 struct tipc_media_addr *maddr,
902 bool *respond, bool *dupl_addr)
906 struct tipc_link_entry *le;
907 bool addr_match = false;
908 bool sign_match = false;
909 bool link_up = false;
910 bool accept_addr = false;
918 n = tipc_node_create(net, addr, peer_id, capabilities);
922 tipc_node_write_lock(n);
924 le = &n->links[b->identity];
926 /* Prepare to validate requesting node's signature and media address */
928 link_up = l && tipc_link_is_up(l);
929 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
930 sign_match = (signature == n->signature);
932 /* These three flags give us eight permutations: */
934 if (sign_match && addr_match && link_up) {
935 /* All is fine. Do nothing. */
937 } else if (sign_match && addr_match && !link_up) {
938 /* Respond. The link will come up in due time */
940 } else if (sign_match && !addr_match && link_up) {
941 /* Peer has changed i/f address without rebooting.
942 * If so, the link will reset soon, and the next
943 * discovery will be accepted. So we can ignore it.
944 * It may also be an cloned or malicious peer having
945 * chosen the same node address and signature as an
947 * Ignore requests until the link goes down, if ever.
950 } else if (sign_match && !addr_match && !link_up) {
951 /* Peer link has changed i/f address without rebooting.
952 * It may also be a cloned or malicious peer; we can't
953 * distinguish between the two.
954 * The signature is correct, so we must accept.
958 } else if (!sign_match && addr_match && link_up) {
959 /* Peer node rebooted. Two possibilities:
960 * - Delayed re-discovery; this link endpoint has already
961 * reset and re-established contact with the peer, before
962 * receiving a discovery message from that node.
963 * (The peer happened to receive one from this node first).
964 * - The peer came back so fast that our side has not
965 * discovered it yet. Probing from this side will soon
966 * reset the link, since there can be no working link
967 * endpoint at the peer end, and the link will re-establish.
968 * Accept the signature, since it comes from a known peer.
970 n->signature = signature;
971 } else if (!sign_match && addr_match && !link_up) {
972 /* The peer node has rebooted.
973 * Accept signature, since it is a known peer.
975 n->signature = signature;
977 } else if (!sign_match && !addr_match && link_up) {
978 /* Peer rebooted with new address, or a new/duplicate peer.
979 * Ignore until the link goes down, if ever.
982 } else if (!sign_match && !addr_match && !link_up) {
983 /* Peer rebooted with new address, or it is a new peer.
984 * Accept signature and address.
986 n->signature = signature;
994 /* Now create new link if not already existing */
996 if (n->link_cnt == 2)
999 if_name = strchr(b->name, ':') + 1;
1000 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
1001 b->net_plane, b->mtu, b->priority,
1002 b->window, mod(tipc_net(net)->random),
1003 tipc_own_addr(net), addr, peer_id,
1005 tipc_bc_sndlink(n->net), n->bc_entry.link,
1007 &n->bc_entry.namedq, &l)) {
1012 tipc_link_fsm_evt(l, LINK_RESET_EVT);
1013 if (n->state == NODE_FAILINGOVER)
1014 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1017 tipc_node_calculate_timer(n, l);
1018 if (n->link_cnt == 1) {
1019 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1020 if (!mod_timer(&n->timer, intv))
1024 memcpy(&le->maddr, maddr, sizeof(*maddr));
1026 tipc_node_write_unlock(n);
1027 if (reset && l && !tipc_link_is_reset(l))
1028 tipc_node_link_down(n, b->identity, false);
1032 void tipc_node_delete_links(struct net *net, int bearer_id)
1034 struct tipc_net *tn = net_generic(net, tipc_net_id);
1035 struct tipc_node *n;
1038 list_for_each_entry_rcu(n, &tn->node_list, list) {
1039 tipc_node_link_down(n, bearer_id, true);
1044 static void tipc_node_reset_links(struct tipc_node *n)
1048 pr_warn("Resetting all links to %x\n", n->addr);
1050 for (i = 0; i < MAX_BEARERS; i++) {
1051 tipc_node_link_down(n, i, false);
1055 /* tipc_node_fsm_evt - node finite state machine
1056 * Determines when contact is allowed with peer node
1058 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
1060 int state = n->state;
1063 case SELF_DOWN_PEER_DOWN:
1065 case SELF_ESTABL_CONTACT_EVT:
1066 state = SELF_UP_PEER_COMING;
1068 case PEER_ESTABL_CONTACT_EVT:
1069 state = SELF_COMING_PEER_UP;
1071 case SELF_LOST_CONTACT_EVT:
1072 case PEER_LOST_CONTACT_EVT:
1074 case NODE_SYNCH_END_EVT:
1075 case NODE_SYNCH_BEGIN_EVT:
1076 case NODE_FAILOVER_BEGIN_EVT:
1077 case NODE_FAILOVER_END_EVT:
1082 case SELF_UP_PEER_UP:
1084 case SELF_LOST_CONTACT_EVT:
1085 state = SELF_DOWN_PEER_LEAVING;
1087 case PEER_LOST_CONTACT_EVT:
1088 state = SELF_LEAVING_PEER_DOWN;
1090 case NODE_SYNCH_BEGIN_EVT:
1091 state = NODE_SYNCHING;
1093 case NODE_FAILOVER_BEGIN_EVT:
1094 state = NODE_FAILINGOVER;
1096 case SELF_ESTABL_CONTACT_EVT:
1097 case PEER_ESTABL_CONTACT_EVT:
1098 case NODE_SYNCH_END_EVT:
1099 case NODE_FAILOVER_END_EVT:
1105 case SELF_DOWN_PEER_LEAVING:
1107 case PEER_LOST_CONTACT_EVT:
1108 state = SELF_DOWN_PEER_DOWN;
1110 case SELF_ESTABL_CONTACT_EVT:
1111 case PEER_ESTABL_CONTACT_EVT:
1112 case SELF_LOST_CONTACT_EVT:
1114 case NODE_SYNCH_END_EVT:
1115 case NODE_SYNCH_BEGIN_EVT:
1116 case NODE_FAILOVER_BEGIN_EVT:
1117 case NODE_FAILOVER_END_EVT:
1122 case SELF_UP_PEER_COMING:
1124 case PEER_ESTABL_CONTACT_EVT:
1125 state = SELF_UP_PEER_UP;
1127 case SELF_LOST_CONTACT_EVT:
1128 state = SELF_DOWN_PEER_DOWN;
1130 case SELF_ESTABL_CONTACT_EVT:
1131 case PEER_LOST_CONTACT_EVT:
1132 case NODE_SYNCH_END_EVT:
1133 case NODE_FAILOVER_BEGIN_EVT:
1135 case NODE_SYNCH_BEGIN_EVT:
1136 case NODE_FAILOVER_END_EVT:
1141 case SELF_COMING_PEER_UP:
1143 case SELF_ESTABL_CONTACT_EVT:
1144 state = SELF_UP_PEER_UP;
1146 case PEER_LOST_CONTACT_EVT:
1147 state = SELF_DOWN_PEER_DOWN;
1149 case SELF_LOST_CONTACT_EVT:
1150 case PEER_ESTABL_CONTACT_EVT:
1152 case NODE_SYNCH_END_EVT:
1153 case NODE_SYNCH_BEGIN_EVT:
1154 case NODE_FAILOVER_BEGIN_EVT:
1155 case NODE_FAILOVER_END_EVT:
1160 case SELF_LEAVING_PEER_DOWN:
1162 case SELF_LOST_CONTACT_EVT:
1163 state = SELF_DOWN_PEER_DOWN;
1165 case SELF_ESTABL_CONTACT_EVT:
1166 case PEER_ESTABL_CONTACT_EVT:
1167 case PEER_LOST_CONTACT_EVT:
1169 case NODE_SYNCH_END_EVT:
1170 case NODE_SYNCH_BEGIN_EVT:
1171 case NODE_FAILOVER_BEGIN_EVT:
1172 case NODE_FAILOVER_END_EVT:
1177 case NODE_FAILINGOVER:
1179 case SELF_LOST_CONTACT_EVT:
1180 state = SELF_DOWN_PEER_LEAVING;
1182 case PEER_LOST_CONTACT_EVT:
1183 state = SELF_LEAVING_PEER_DOWN;
1185 case NODE_FAILOVER_END_EVT:
1186 state = SELF_UP_PEER_UP;
1188 case NODE_FAILOVER_BEGIN_EVT:
1189 case SELF_ESTABL_CONTACT_EVT:
1190 case PEER_ESTABL_CONTACT_EVT:
1192 case NODE_SYNCH_BEGIN_EVT:
1193 case NODE_SYNCH_END_EVT:
1200 case SELF_LOST_CONTACT_EVT:
1201 state = SELF_DOWN_PEER_LEAVING;
1203 case PEER_LOST_CONTACT_EVT:
1204 state = SELF_LEAVING_PEER_DOWN;
1206 case NODE_SYNCH_END_EVT:
1207 state = SELF_UP_PEER_UP;
1209 case NODE_FAILOVER_BEGIN_EVT:
1210 state = NODE_FAILINGOVER;
1212 case NODE_SYNCH_BEGIN_EVT:
1213 case SELF_ESTABL_CONTACT_EVT:
1214 case PEER_ESTABL_CONTACT_EVT:
1216 case NODE_FAILOVER_END_EVT:
1222 pr_err("Unknown node fsm state %x\n", state);
1229 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1232 static void node_lost_contact(struct tipc_node *n,
1233 struct sk_buff_head *inputq)
1235 struct tipc_sock_conn *conn, *safe;
1236 struct tipc_link *l;
1237 struct list_head *conns = &n->conn_sks;
1238 struct sk_buff *skb;
1241 pr_debug("Lost contact with %x\n", n->addr);
1242 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1244 /* Clean up broadcast state */
1245 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1247 /* Abort any ongoing link failover */
1248 for (i = 0; i < MAX_BEARERS; i++) {
1249 l = n->links[i].link;
1251 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1254 /* Notify publications from this node */
1255 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1257 /* Notify sockets connected to node */
1258 list_for_each_entry_safe(conn, safe, conns, list) {
1259 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1260 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1261 conn->peer_node, conn->port,
1262 conn->peer_port, TIPC_ERR_NO_NODE);
1264 skb_queue_tail(inputq, skb);
1265 list_del(&conn->list);
1271 * tipc_node_get_linkname - get the name of a link
1273 * @bearer_id: id of the bearer
1274 * @node: peer node address
1275 * @linkname: link name output buffer
1277 * Returns 0 on success
1279 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1280 char *linkname, size_t len)
1282 struct tipc_link *link;
1284 struct tipc_node *node = tipc_node_find(net, addr);
1289 if (bearer_id >= MAX_BEARERS)
1292 tipc_node_read_lock(node);
1293 link = node->links[bearer_id].link;
1295 strncpy(linkname, tipc_link_name(link), len);
1298 tipc_node_read_unlock(node);
1300 tipc_node_put(node);
1304 /* Caller should hold node lock for the passed node */
1305 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1308 struct nlattr *attrs;
1310 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1311 NLM_F_MULTI, TIPC_NL_NODE_GET);
1315 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1319 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1321 if (node_is_up(node))
1322 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1325 nla_nest_end(msg->skb, attrs);
1326 genlmsg_end(msg->skb, hdr);
1331 nla_nest_cancel(msg->skb, attrs);
1333 genlmsg_cancel(msg->skb, hdr);
1339 * tipc_node_xmit() is the general link level function for message sending
1340 * @net: the applicable net namespace
1341 * @list: chain of buffers containing message
1342 * @dnode: address of destination node
1343 * @selector: a number used for deterministic link selection
1344 * Consumes the buffer chain.
1345 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1347 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1348 u32 dnode, int selector)
1350 struct tipc_link_entry *le = NULL;
1351 struct tipc_node *n;
1352 struct sk_buff_head xmitq;
1356 if (in_own_node(net, dnode)) {
1357 tipc_sk_rcv(net, list);
1361 n = tipc_node_find(net, dnode);
1363 skb_queue_purge(list);
1364 return -EHOSTUNREACH;
1367 tipc_node_read_lock(n);
1368 bearer_id = n->active_links[selector & 1];
1369 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1370 tipc_node_read_unlock(n);
1372 skb_queue_purge(list);
1373 return -EHOSTUNREACH;
1376 __skb_queue_head_init(&xmitq);
1377 le = &n->links[bearer_id];
1378 spin_lock_bh(&le->lock);
1379 rc = tipc_link_xmit(le->link, list, &xmitq);
1380 spin_unlock_bh(&le->lock);
1381 tipc_node_read_unlock(n);
1383 if (unlikely(rc == -ENOBUFS))
1384 tipc_node_link_down(n, bearer_id, false);
1386 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1393 /* tipc_node_xmit_skb(): send single buffer to destination
1394 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1395 * messages, which will not be rejected
1396 * The only exception is datagram messages rerouted after secondary
1397 * lookup, which are rare and safe to dispose of anyway.
1399 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1402 struct sk_buff_head head;
1404 skb_queue_head_init(&head);
1405 __skb_queue_tail(&head, skb);
1406 tipc_node_xmit(net, &head, dnode, selector);
1410 /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1411 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1413 int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1415 struct sk_buff *skb;
1416 u32 selector, dnode;
1418 while ((skb = __skb_dequeue(xmitq))) {
1419 selector = msg_origport(buf_msg(skb));
1420 dnode = msg_destnode(buf_msg(skb));
1421 tipc_node_xmit_skb(net, skb, dnode, selector);
1426 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1428 struct sk_buff *txskb;
1429 struct tipc_node *n;
1433 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1435 if (in_own_node(net, dst))
1439 txskb = pskb_copy(skb, GFP_ATOMIC);
1442 msg_set_destnode(buf_msg(txskb), dst);
1443 tipc_node_xmit_skb(net, txskb, dst, 0);
1450 static void tipc_node_mcast_rcv(struct tipc_node *n)
1452 struct tipc_bclink_entry *be = &n->bc_entry;
1454 /* 'arrvq' is under inputq2's lock protection */
1455 spin_lock_bh(&be->inputq2.lock);
1456 spin_lock_bh(&be->inputq1.lock);
1457 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1458 spin_unlock_bh(&be->inputq1.lock);
1459 spin_unlock_bh(&be->inputq2.lock);
1460 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1463 static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1464 int bearer_id, struct sk_buff_head *xmitq)
1466 struct tipc_link *ucl;
1469 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1471 if (rc & TIPC_LINK_DOWN_EVT) {
1472 tipc_node_reset_links(n);
1476 if (!(rc & TIPC_LINK_SND_STATE))
1479 /* If probe message, a STATE response will be sent anyway */
1483 /* Produce a STATE message carrying broadcast NACK */
1484 tipc_node_read_lock(n);
1485 ucl = n->links[bearer_id].link;
1487 tipc_link_build_state_msg(ucl, xmitq);
1488 tipc_node_read_unlock(n);
1492 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1493 * @net: the applicable net namespace
1495 * @bearer_id: id of bearer message arrived on
1497 * Invoked with no locks held.
1499 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1502 struct sk_buff_head xmitq;
1503 struct tipc_bclink_entry *be;
1504 struct tipc_link_entry *le;
1505 struct tipc_msg *hdr = buf_msg(skb);
1506 int usr = msg_user(hdr);
1507 u32 dnode = msg_destnode(hdr);
1508 struct tipc_node *n;
1510 __skb_queue_head_init(&xmitq);
1512 /* If NACK for other node, let rcv link for that node peek into it */
1513 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1514 n = tipc_node_find(net, dnode);
1516 n = tipc_node_find(net, msg_prevnode(hdr));
1522 le = &n->links[bearer_id];
1524 rc = tipc_bcast_rcv(net, be->link, skb);
1526 /* Broadcast ACKs are sent on a unicast link */
1527 if (rc & TIPC_LINK_SND_STATE) {
1528 tipc_node_read_lock(n);
1529 tipc_link_build_state_msg(le->link, &xmitq);
1530 tipc_node_read_unlock(n);
1533 if (!skb_queue_empty(&xmitq))
1534 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1536 if (!skb_queue_empty(&be->inputq1))
1537 tipc_node_mcast_rcv(n);
1539 /* If reassembly or retransmission failure => reset all links to peer */
1540 if (rc & TIPC_LINK_DOWN_EVT)
1541 tipc_node_reset_links(n);
1547 * tipc_node_check_state - check and if necessary update node state
1549 * @bearer_id: identity of bearer delivering the packet
1550 * Returns true if state and msg are ok, otherwise false
1552 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1553 int bearer_id, struct sk_buff_head *xmitq)
1555 struct tipc_msg *hdr = buf_msg(skb);
1556 int usr = msg_user(hdr);
1557 int mtyp = msg_type(hdr);
1558 u16 oseqno = msg_seqno(hdr);
1559 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1560 u16 exp_pkts = msg_msgcnt(hdr);
1561 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1562 int state = n->state;
1563 struct tipc_link *l, *tnl, *pl = NULL;
1564 struct tipc_media_addr *maddr;
1567 l = n->links[bearer_id].link;
1570 rcv_nxt = tipc_link_rcv_nxt(l);
1573 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1576 /* Find parallel link, if any */
1577 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1578 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1579 pl = n->links[pb_id].link;
1584 if (!tipc_link_validate_msg(l, hdr))
1587 /* Check and update node accesibility if applicable */
1588 if (state == SELF_UP_PEER_COMING) {
1589 if (!tipc_link_is_up(l))
1591 if (!msg_peer_link_is_up(hdr))
1593 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1596 if (state == SELF_DOWN_PEER_LEAVING) {
1597 if (msg_peer_node_is_up(hdr))
1599 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1603 if (state == SELF_LEAVING_PEER_DOWN)
1606 /* Ignore duplicate packets */
1607 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1610 /* Initiate or update failover mode if applicable */
1611 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1612 syncpt = oseqno + exp_pkts - 1;
1613 if (pl && tipc_link_is_up(pl)) {
1614 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1615 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1616 tipc_link_inputq(l));
1618 /* If pkts arrive out of order, use lowest calculated syncpt */
1619 if (less(syncpt, n->sync_point))
1620 n->sync_point = syncpt;
1623 /* Open parallel link when tunnel link reaches synch point */
1624 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1625 if (!more(rcv_nxt, n->sync_point))
1627 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1629 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1633 /* No synching needed if only one link */
1634 if (!pl || !tipc_link_is_up(pl))
1637 /* Initiate synch mode if applicable */
1638 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1639 syncpt = iseqno + exp_pkts - 1;
1640 if (!tipc_link_is_up(l))
1641 __tipc_node_link_up(n, bearer_id, xmitq);
1642 if (n->state == SELF_UP_PEER_UP) {
1643 n->sync_point = syncpt;
1644 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1645 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1649 /* Open tunnel link when parallel link reaches synch point */
1650 if (n->state == NODE_SYNCHING) {
1651 if (tipc_link_is_synching(l)) {
1657 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1658 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1659 if (more(dlv_nxt, n->sync_point)) {
1660 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1661 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1666 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1668 if (usr == LINK_PROTOCOL)
1676 * tipc_rcv - process TIPC packets/messages arriving from off-node
1677 * @net: the applicable net namespace
1679 * @bearer: pointer to bearer message arrived on
1681 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1682 * structure (i.e. cannot be NULL), but bearer can be inactive.
1684 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1686 struct sk_buff_head xmitq;
1687 struct tipc_node *n;
1688 struct tipc_msg *hdr;
1689 int bearer_id = b->identity;
1690 struct tipc_link_entry *le;
1691 u32 self = tipc_own_addr(net);
1695 __skb_queue_head_init(&xmitq);
1697 /* Ensure message is well-formed before touching the header */
1698 if (unlikely(!tipc_msg_validate(&skb)))
1701 usr = msg_user(hdr);
1702 bc_ack = msg_bcast_ack(hdr);
1704 /* Handle arrival of discovery or broadcast packet */
1705 if (unlikely(msg_non_seq(hdr))) {
1706 if (unlikely(usr == LINK_CONFIG))
1707 return tipc_disc_rcv(net, skb, b);
1709 return tipc_node_bc_rcv(net, skb, bearer_id);
1712 /* Discard unicast link messages destined for another node */
1713 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1716 /* Locate neighboring node that sent packet */
1717 n = tipc_node_find(net, msg_prevnode(hdr));
1720 le = &n->links[bearer_id];
1722 /* Ensure broadcast reception is in synch with peer's send state */
1723 if (unlikely(usr == LINK_PROTOCOL))
1724 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
1725 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
1726 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
1728 /* Receive packet directly if conditions permit */
1729 tipc_node_read_lock(n);
1730 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
1731 spin_lock_bh(&le->lock);
1733 rc = tipc_link_rcv(le->link, skb, &xmitq);
1736 spin_unlock_bh(&le->lock);
1738 tipc_node_read_unlock(n);
1740 /* Check/update node state before receiving */
1741 if (unlikely(skb)) {
1742 if (unlikely(skb_linearize(skb)))
1744 tipc_node_write_lock(n);
1745 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1747 rc = tipc_link_rcv(le->link, skb, &xmitq);
1751 tipc_node_write_unlock(n);
1754 if (unlikely(rc & TIPC_LINK_UP_EVT))
1755 tipc_node_link_up(n, bearer_id, &xmitq);
1757 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1758 tipc_node_link_down(n, bearer_id, false);
1760 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1761 tipc_named_rcv(net, &n->bc_entry.namedq);
1763 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1764 tipc_node_mcast_rcv(n);
1766 if (!skb_queue_empty(&le->inputq))
1767 tipc_sk_rcv(net, &le->inputq);
1769 if (!skb_queue_empty(&xmitq))
1770 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1777 void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1780 struct tipc_net *tn = tipc_net(net);
1781 int bearer_id = b->identity;
1782 struct sk_buff_head xmitq;
1783 struct tipc_link_entry *e;
1784 struct tipc_node *n;
1786 __skb_queue_head_init(&xmitq);
1790 list_for_each_entry_rcu(n, &tn->node_list, list) {
1791 tipc_node_write_lock(n);
1792 e = &n->links[bearer_id];
1794 if (prop == TIPC_NLA_PROP_TOL)
1795 tipc_link_set_tolerance(e->link, b->tolerance,
1797 else if (prop == TIPC_NLA_PROP_MTU)
1798 tipc_link_set_mtu(e->link, b->mtu);
1800 tipc_node_write_unlock(n);
1801 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1807 int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1809 struct net *net = sock_net(skb->sk);
1810 struct tipc_net *tn = net_generic(net, tipc_net_id);
1811 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1812 struct tipc_node *peer;
1816 /* We identify the peer by its net */
1817 if (!info->attrs[TIPC_NLA_NET])
1820 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
1821 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
1826 if (!attrs[TIPC_NLA_NET_ADDR])
1829 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1831 if (in_own_node(net, addr))
1834 spin_lock_bh(&tn->node_list_lock);
1835 peer = tipc_node_find(net, addr);
1837 spin_unlock_bh(&tn->node_list_lock);
1841 tipc_node_write_lock(peer);
1842 if (peer->state != SELF_DOWN_PEER_DOWN &&
1843 peer->state != SELF_DOWN_PEER_LEAVING) {
1844 tipc_node_write_unlock(peer);
1849 tipc_node_clear_links(peer);
1850 tipc_node_write_unlock(peer);
1851 tipc_node_delete(peer);
1855 tipc_node_put(peer);
1856 spin_unlock_bh(&tn->node_list_lock);
1861 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1864 struct net *net = sock_net(skb->sk);
1865 struct tipc_net *tn = net_generic(net, tipc_net_id);
1866 int done = cb->args[0];
1867 int last_addr = cb->args[1];
1868 struct tipc_node *node;
1869 struct tipc_nl_msg msg;
1875 msg.portid = NETLINK_CB(cb->skb).portid;
1876 msg.seq = cb->nlh->nlmsg_seq;
1880 node = tipc_node_find(net, last_addr);
1883 /* We never set seq or call nl_dump_check_consistent()
1884 * this means that setting prev_seq here will cause the
1885 * consistence check to fail in the netlink callback
1886 * handler. Resulting in the NLMSG_DONE message having
1887 * the NLM_F_DUMP_INTR flag set if the node state
1888 * changed while we released the lock.
1893 tipc_node_put(node);
1896 list_for_each_entry_rcu(node, &tn->node_list, list) {
1898 if (node->addr == last_addr)
1904 tipc_node_read_lock(node);
1905 err = __tipc_nl_add_node(&msg, node);
1907 last_addr = node->addr;
1908 tipc_node_read_unlock(node);
1912 tipc_node_read_unlock(node);
1917 cb->args[1] = last_addr;
1923 /* tipc_node_find_by_name - locate owner node of link by link's name
1924 * @net: the applicable net namespace
1925 * @name: pointer to link name string
1926 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1928 * Returns pointer to node owning the link, or 0 if no matching link is found.
1930 static struct tipc_node *tipc_node_find_by_name(struct net *net,
1931 const char *link_name,
1932 unsigned int *bearer_id)
1934 struct tipc_net *tn = net_generic(net, tipc_net_id);
1935 struct tipc_link *l;
1936 struct tipc_node *n;
1937 struct tipc_node *found_node = NULL;
1942 list_for_each_entry_rcu(n, &tn->node_list, list) {
1943 tipc_node_read_lock(n);
1944 for (i = 0; i < MAX_BEARERS; i++) {
1945 l = n->links[i].link;
1946 if (l && !strcmp(tipc_link_name(l), link_name)) {
1952 tipc_node_read_unlock(n);
1961 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1967 struct tipc_link *link;
1968 struct tipc_node *node;
1969 struct sk_buff_head xmitq;
1970 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1971 struct net *net = sock_net(skb->sk);
1973 __skb_queue_head_init(&xmitq);
1975 if (!info->attrs[TIPC_NLA_LINK])
1978 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1979 info->attrs[TIPC_NLA_LINK],
1980 tipc_nl_link_policy, info->extack);
1984 if (!attrs[TIPC_NLA_LINK_NAME])
1987 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1989 if (strcmp(name, tipc_bclink_name) == 0)
1990 return tipc_nl_bc_link_set(net, attrs);
1992 node = tipc_node_find_by_name(net, name, &bearer_id);
1996 tipc_node_read_lock(node);
1998 link = node->links[bearer_id].link;
2004 if (attrs[TIPC_NLA_LINK_PROP]) {
2005 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2007 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2014 if (props[TIPC_NLA_PROP_TOL]) {
2017 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2018 tipc_link_set_tolerance(link, tol, &xmitq);
2020 if (props[TIPC_NLA_PROP_PRIO]) {
2023 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2024 tipc_link_set_prio(link, prio, &xmitq);
2026 if (props[TIPC_NLA_PROP_WIN]) {
2029 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2030 tipc_link_set_queue_limits(link, win);
2035 tipc_node_read_unlock(node);
2036 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
2040 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2042 struct net *net = genl_info_net(info);
2043 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2044 struct tipc_nl_msg msg;
2048 msg.portid = info->snd_portid;
2049 msg.seq = info->snd_seq;
2051 if (!info->attrs[TIPC_NLA_LINK])
2054 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2055 info->attrs[TIPC_NLA_LINK],
2056 tipc_nl_link_policy, info->extack);
2060 if (!attrs[TIPC_NLA_LINK_NAME])
2063 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2065 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2069 if (strcmp(name, tipc_bclink_name) == 0) {
2070 err = tipc_nl_add_bc_link(net, &msg);
2075 struct tipc_node *node;
2076 struct tipc_link *link;
2078 node = tipc_node_find_by_name(net, name, &bearer_id);
2084 tipc_node_read_lock(node);
2085 link = node->links[bearer_id].link;
2087 tipc_node_read_unlock(node);
2092 err = __tipc_nl_add_link(net, &msg, link, 0);
2093 tipc_node_read_unlock(node);
2098 return genlmsg_reply(msg.skb, info);
2101 nlmsg_free(msg.skb);
2105 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2109 unsigned int bearer_id;
2110 struct tipc_link *link;
2111 struct tipc_node *node;
2112 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2113 struct net *net = sock_net(skb->sk);
2114 struct tipc_link_entry *le;
2116 if (!info->attrs[TIPC_NLA_LINK])
2119 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2120 info->attrs[TIPC_NLA_LINK],
2121 tipc_nl_link_policy, info->extack);
2125 if (!attrs[TIPC_NLA_LINK_NAME])
2128 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2130 if (strcmp(link_name, tipc_bclink_name) == 0) {
2131 err = tipc_bclink_reset_stats(net);
2137 node = tipc_node_find_by_name(net, link_name, &bearer_id);
2141 le = &node->links[bearer_id];
2142 tipc_node_read_lock(node);
2143 spin_lock_bh(&le->lock);
2144 link = node->links[bearer_id].link;
2146 spin_unlock_bh(&le->lock);
2147 tipc_node_read_unlock(node);
2150 tipc_link_reset_stats(link);
2151 spin_unlock_bh(&le->lock);
2152 tipc_node_read_unlock(node);
2156 /* Caller should hold node lock */
2157 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2158 struct tipc_node *node, u32 *prev_link)
2163 for (i = *prev_link; i < MAX_BEARERS; i++) {
2166 if (!node->links[i].link)
2169 err = __tipc_nl_add_link(net, msg,
2170 node->links[i].link, NLM_F_MULTI);
2179 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
2181 struct net *net = sock_net(skb->sk);
2182 struct tipc_net *tn = net_generic(net, tipc_net_id);
2183 struct tipc_node *node;
2184 struct tipc_nl_msg msg;
2185 u32 prev_node = cb->args[0];
2186 u32 prev_link = cb->args[1];
2187 int done = cb->args[2];
2194 msg.portid = NETLINK_CB(cb->skb).portid;
2195 msg.seq = cb->nlh->nlmsg_seq;
2199 node = tipc_node_find(net, prev_node);
2201 /* We never set seq or call nl_dump_check_consistent()
2202 * this means that setting prev_seq here will cause the
2203 * consistence check to fail in the netlink callback
2204 * handler. Resulting in the last NLMSG_DONE message
2205 * having the NLM_F_DUMP_INTR flag set.
2210 tipc_node_put(node);
2212 list_for_each_entry_continue_rcu(node, &tn->node_list,
2214 tipc_node_read_lock(node);
2215 err = __tipc_nl_add_node_links(net, &msg, node,
2217 tipc_node_read_unlock(node);
2221 prev_node = node->addr;
2224 err = tipc_nl_add_bc_link(net, &msg);
2228 list_for_each_entry_rcu(node, &tn->node_list, list) {
2229 tipc_node_read_lock(node);
2230 err = __tipc_nl_add_node_links(net, &msg, node,
2232 tipc_node_read_unlock(node);
2236 prev_node = node->addr;
2243 cb->args[0] = prev_node;
2244 cb->args[1] = prev_link;
2250 int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2252 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2253 struct net *net = sock_net(skb->sk);
2256 if (!info->attrs[TIPC_NLA_MON])
2259 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2260 info->attrs[TIPC_NLA_MON],
2261 tipc_nl_monitor_policy, info->extack);
2265 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2268 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2269 err = tipc_nl_monitor_set_threshold(net, val);
2277 static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2279 struct nlattr *attrs;
2283 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2284 0, TIPC_NL_MON_GET);
2288 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2292 val = tipc_nl_monitor_get_threshold(net);
2294 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2297 nla_nest_end(msg->skb, attrs);
2298 genlmsg_end(msg->skb, hdr);
2303 nla_nest_cancel(msg->skb, attrs);
2305 genlmsg_cancel(msg->skb, hdr);
2310 int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2312 struct net *net = sock_net(skb->sk);
2313 struct tipc_nl_msg msg;
2316 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2319 msg.portid = info->snd_portid;
2320 msg.seq = info->snd_seq;
2322 err = __tipc_nl_add_monitor_prop(net, &msg);
2324 nlmsg_free(msg.skb);
2328 return genlmsg_reply(msg.skb, info);
2331 int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2333 struct net *net = sock_net(skb->sk);
2334 u32 prev_bearer = cb->args[0];
2335 struct tipc_nl_msg msg;
2339 if (prev_bearer == MAX_BEARERS)
2343 msg.portid = NETLINK_CB(cb->skb).portid;
2344 msg.seq = cb->nlh->nlmsg_seq;
2347 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
2348 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2353 cb->args[0] = bearer_id;
2358 int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2359 struct netlink_callback *cb)
2361 struct net *net = sock_net(skb->sk);
2362 u32 prev_node = cb->args[1];
2363 u32 bearer_id = cb->args[2];
2364 int done = cb->args[0];
2365 struct tipc_nl_msg msg;
2369 struct nlattr **attrs;
2370 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2372 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2376 if (!attrs[TIPC_NLA_MON])
2379 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2380 attrs[TIPC_NLA_MON],
2381 tipc_nl_monitor_policy, NULL);
2385 if (!mon[TIPC_NLA_MON_REF])
2388 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2390 if (bearer_id >= MAX_BEARERS)
2398 msg.portid = NETLINK_CB(cb->skb).portid;
2399 msg.seq = cb->nlh->nlmsg_seq;
2402 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2408 cb->args[1] = prev_node;
2409 cb->args[2] = bearer_id;