2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2000-2006, 2012-2015, 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"
45 #define INVALID_NODE_SIG 0x10000
47 /* Flags used to take different actions according to flag type
48 * TIPC_NOTIFY_NODE_DOWN: notify node is down
49 * TIPC_NOTIFY_NODE_UP: notify node is up
50 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
53 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
54 TIPC_NOTIFY_NODE_UP = (1 << 4),
55 TIPC_NOTIFY_LINK_UP = (1 << 6),
56 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
59 struct tipc_link_entry {
60 struct tipc_link *link;
61 spinlock_t lock; /* per link */
63 struct sk_buff_head inputq;
64 struct tipc_media_addr maddr;
67 struct tipc_bclink_entry {
68 struct tipc_link *link;
69 struct sk_buff_head inputq1;
70 struct sk_buff_head arrvq;
71 struct sk_buff_head inputq2;
72 struct sk_buff_head namedq;
76 * struct tipc_node - TIPC node structure
77 * @addr: network address of node
78 * @ref: reference counter to node object
79 * @lock: rwlock governing access to structure
80 * @net: the applicable net namespace
81 * @hash: links to adjacent nodes in unsorted hash chain
82 * @inputq: pointer to input queue containing messages for msg event
83 * @namedq: pointer to name table input queue with name table messages
84 * @active_links: bearer ids of active links, used as index into links[] array
85 * @links: array containing references to all links to node
86 * @action_flags: bit mask of different types of node actions
87 * @state: connectivity state vs peer node
88 * @sync_point: sequence number where synch/failover is finished
89 * @list: links to adjacent nodes in sorted list of cluster's nodes
90 * @working_links: number of working links to node (both active and standby)
91 * @link_cnt: number of links to node
92 * @capabilities: bitmap, indicating peer node's functional capabilities
93 * @signature: node instance identifier
94 * @link_id: local and remote bearer ids of changing link, if any
95 * @publ_list: list of publications
96 * @rcu: rcu struct for tipc_node
103 struct hlist_node hash;
105 struct tipc_link_entry links[MAX_BEARERS];
106 struct tipc_bclink_entry bc_entry;
108 struct list_head list;
116 struct list_head publ_list;
117 struct list_head conn_sks;
118 unsigned long keepalive_intv;
119 struct timer_list timer;
123 /* Node FSM states and events:
126 SELF_DOWN_PEER_DOWN = 0xdd,
127 SELF_UP_PEER_UP = 0xaa,
128 SELF_DOWN_PEER_LEAVING = 0xd1,
129 SELF_UP_PEER_COMING = 0xac,
130 SELF_COMING_PEER_UP = 0xca,
131 SELF_LEAVING_PEER_DOWN = 0x1d,
132 NODE_FAILINGOVER = 0xf0,
137 SELF_ESTABL_CONTACT_EVT = 0xece,
138 SELF_LOST_CONTACT_EVT = 0x1ce,
139 PEER_ESTABL_CONTACT_EVT = 0x9ece,
140 PEER_LOST_CONTACT_EVT = 0x91ce,
141 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
142 NODE_FAILOVER_END_EVT = 0xfee,
143 NODE_SYNCH_BEGIN_EVT = 0xcbe,
144 NODE_SYNCH_END_EVT = 0xcee
147 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
148 struct sk_buff_head *xmitq,
149 struct tipc_media_addr **maddr);
150 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
152 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
153 static void tipc_node_delete(struct tipc_node *node);
154 static void tipc_node_timeout(unsigned long data);
155 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
156 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
157 static void tipc_node_put(struct tipc_node *node);
158 static bool tipc_node_is_up(struct tipc_node *n);
160 struct tipc_sock_conn {
164 struct list_head list;
167 static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
168 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
169 [TIPC_NLA_LINK_NAME] = {
171 .len = TIPC_MAX_LINK_NAME
173 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
174 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
175 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
176 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
177 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
178 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
179 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
180 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
183 static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
184 [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC },
185 [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 },
186 [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG }
189 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
191 int bearer_id = n->active_links[sel & 1];
193 if (unlikely(bearer_id == INVALID_BEARER_ID))
196 return n->links[bearer_id].link;
199 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
203 unsigned int mtu = MAX_MSG_SIZE;
205 n = tipc_node_find(net, addr);
209 bearer_id = n->active_links[sel & 1];
210 if (likely(bearer_id != INVALID_BEARER_ID))
211 mtu = n->links[bearer_id].mtu;
216 * A trivial power-of-two bitmask technique is used for speed, since this
217 * operation is done for every incoming TIPC packet. The number of hash table
218 * entries has been chosen so that no hash chain exceeds 8 nodes and will
219 * usually be much smaller (typically only a single node).
221 static unsigned int tipc_hashfn(u32 addr)
223 return addr & (NODE_HTABLE_SIZE - 1);
226 static void tipc_node_kref_release(struct kref *kref)
228 struct tipc_node *node = container_of(kref, struct tipc_node, kref);
230 tipc_node_delete(node);
233 static void tipc_node_put(struct tipc_node *node)
235 kref_put(&node->kref, tipc_node_kref_release);
238 static void tipc_node_get(struct tipc_node *node)
240 kref_get(&node->kref);
244 * tipc_node_find - locate specified node object, if it exists
246 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
248 struct tipc_net *tn = net_generic(net, tipc_net_id);
249 struct tipc_node *node;
251 if (unlikely(!in_own_cluster_exact(net, addr)))
255 hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
257 if (node->addr == addr) {
267 static void tipc_node_read_lock(struct tipc_node *n)
269 read_lock_bh(&n->lock);
272 static void tipc_node_read_unlock(struct tipc_node *n)
274 read_unlock_bh(&n->lock);
277 static void tipc_node_write_lock(struct tipc_node *n)
279 write_lock_bh(&n->lock);
282 static void tipc_node_write_unlock(struct tipc_node *n)
284 struct net *net = n->net;
286 u32 flags = n->action_flags;
288 struct list_head *publ_list;
290 if (likely(!flags)) {
291 write_unlock_bh(&n->lock);
296 link_id = n->link_id;
297 publ_list = &n->publ_list;
299 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
300 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
302 write_unlock_bh(&n->lock);
304 if (flags & TIPC_NOTIFY_NODE_DOWN)
305 tipc_publ_notify(net, publ_list, addr);
307 if (flags & TIPC_NOTIFY_NODE_UP)
308 tipc_named_node_up(net, addr);
310 if (flags & TIPC_NOTIFY_LINK_UP)
311 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
312 TIPC_NODE_SCOPE, link_id, addr);
314 if (flags & TIPC_NOTIFY_LINK_DOWN)
315 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
319 struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
321 struct tipc_net *tn = net_generic(net, tipc_net_id);
322 struct tipc_node *n, *temp_node;
325 spin_lock_bh(&tn->node_list_lock);
326 n = tipc_node_find(net, addr);
329 n = kzalloc(sizeof(*n), GFP_ATOMIC);
331 pr_warn("Node creation failed, no memory\n");
336 n->capabilities = capabilities;
338 rwlock_init(&n->lock);
339 INIT_HLIST_NODE(&n->hash);
340 INIT_LIST_HEAD(&n->list);
341 INIT_LIST_HEAD(&n->publ_list);
342 INIT_LIST_HEAD(&n->conn_sks);
343 skb_queue_head_init(&n->bc_entry.namedq);
344 skb_queue_head_init(&n->bc_entry.inputq1);
345 __skb_queue_head_init(&n->bc_entry.arrvq);
346 skb_queue_head_init(&n->bc_entry.inputq2);
347 for (i = 0; i < MAX_BEARERS; i++)
348 spin_lock_init(&n->links[i].lock);
349 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
350 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
351 if (n->addr < temp_node->addr)
354 list_add_tail_rcu(&n->list, &temp_node->list);
355 n->state = SELF_DOWN_PEER_LEAVING;
356 n->signature = INVALID_NODE_SIG;
357 n->active_links[0] = INVALID_BEARER_ID;
358 n->active_links[1] = INVALID_BEARER_ID;
359 if (!tipc_link_bc_create(net, tipc_own_addr(net), n->addr,
361 tipc_link_window(tipc_bc_sndlink(net)),
363 &n->bc_entry.inputq1,
365 tipc_bc_sndlink(net),
366 &n->bc_entry.link)) {
367 pr_warn("Broadcast rcv link creation failed, no memory\n");
373 setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
374 n->keepalive_intv = U32_MAX;
376 spin_unlock_bh(&tn->node_list_lock);
380 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
382 unsigned long tol = tipc_link_tolerance(l);
383 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
384 unsigned long keepalive_intv = msecs_to_jiffies(intv);
386 /* Link with lowest tolerance determines timer interval */
387 if (keepalive_intv < n->keepalive_intv)
388 n->keepalive_intv = keepalive_intv;
390 /* Ensure link's abort limit corresponds to current interval */
391 tipc_link_set_abort_limit(l, tol / jiffies_to_msecs(n->keepalive_intv));
394 static void tipc_node_delete(struct tipc_node *node)
396 list_del_rcu(&node->list);
397 hlist_del_rcu(&node->hash);
398 kfree(node->bc_entry.link);
399 kfree_rcu(node, rcu);
402 void tipc_node_stop(struct net *net)
404 struct tipc_net *tn = net_generic(net, tipc_net_id);
405 struct tipc_node *node, *t_node;
407 spin_lock_bh(&tn->node_list_lock);
408 list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
409 if (del_timer(&node->timer))
413 spin_unlock_bh(&tn->node_list_lock);
416 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
420 if (in_own_node(net, addr))
423 n = tipc_node_find(net, addr);
425 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
428 tipc_node_write_lock(n);
429 list_add_tail(subscr, &n->publ_list);
430 tipc_node_write_unlock(n);
434 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
438 if (in_own_node(net, addr))
441 n = tipc_node_find(net, addr);
443 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
446 tipc_node_write_lock(n);
447 list_del_init(subscr);
448 tipc_node_write_unlock(n);
452 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
454 struct tipc_node *node;
455 struct tipc_sock_conn *conn;
458 if (in_own_node(net, dnode))
461 node = tipc_node_find(net, dnode);
463 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
464 return -EHOSTUNREACH;
466 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
471 conn->peer_node = dnode;
473 conn->peer_port = peer_port;
475 tipc_node_write_lock(node);
476 list_add_tail(&conn->list, &node->conn_sks);
477 tipc_node_write_unlock(node);
483 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
485 struct tipc_node *node;
486 struct tipc_sock_conn *conn, *safe;
488 if (in_own_node(net, dnode))
491 node = tipc_node_find(net, dnode);
495 tipc_node_write_lock(node);
496 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
497 if (port != conn->port)
499 list_del(&conn->list);
502 tipc_node_write_unlock(node);
506 /* tipc_node_timeout - handle expiration of node timer
508 static void tipc_node_timeout(unsigned long data)
510 struct tipc_node *n = (struct tipc_node *)data;
511 struct tipc_link_entry *le;
512 struct sk_buff_head xmitq;
516 __skb_queue_head_init(&xmitq);
518 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
519 tipc_node_read_lock(n);
520 le = &n->links[bearer_id];
521 spin_lock_bh(&le->lock);
523 /* Link tolerance may change asynchronously: */
524 tipc_node_calculate_timer(n, le->link);
525 rc = tipc_link_timeout(le->link, &xmitq);
527 spin_unlock_bh(&le->lock);
528 tipc_node_read_unlock(n);
529 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
530 if (rc & TIPC_LINK_DOWN_EVT)
531 tipc_node_link_down(n, bearer_id, false);
533 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
539 * __tipc_node_link_up - handle addition of link
540 * Node lock must be held by caller
541 * Link becomes active (alone or shared) or standby, depending on its priority.
543 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
544 struct sk_buff_head *xmitq)
546 int *slot0 = &n->active_links[0];
547 int *slot1 = &n->active_links[1];
548 struct tipc_link *ol = node_active_link(n, 0);
549 struct tipc_link *nl = n->links[bearer_id].link;
554 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
555 if (!tipc_link_is_up(nl))
559 n->action_flags |= TIPC_NOTIFY_LINK_UP;
560 n->link_id = tipc_link_id(nl);
562 /* Leave room for tunnel header when returning 'mtu' to users: */
563 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
565 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
566 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
568 pr_debug("Established link <%s> on network plane %c\n",
569 tipc_link_name(nl), tipc_link_plane(nl));
571 /* First link? => give it both slots */
575 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
576 n->action_flags |= TIPC_NOTIFY_NODE_UP;
577 tipc_bcast_add_peer(n->net, nl, xmitq);
581 /* Second link => redistribute slots */
582 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
583 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
586 tipc_link_set_active(nl, true);
587 tipc_link_set_active(ol, false);
588 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
589 tipc_link_set_active(nl, true);
592 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
595 /* Prepare synchronization with first link */
596 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
600 * tipc_node_link_up - handle addition of link
602 * Link becomes active (alone or shared) or standby, depending on its priority.
604 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
605 struct sk_buff_head *xmitq)
607 tipc_node_write_lock(n);
608 __tipc_node_link_up(n, bearer_id, xmitq);
609 tipc_node_write_unlock(n);
613 * __tipc_node_link_down - handle loss of link
615 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
616 struct sk_buff_head *xmitq,
617 struct tipc_media_addr **maddr)
619 struct tipc_link_entry *le = &n->links[*bearer_id];
620 int *slot0 = &n->active_links[0];
621 int *slot1 = &n->active_links[1];
622 int i, highest = 0, prio;
623 struct tipc_link *l, *_l, *tnl;
625 l = n->links[*bearer_id].link;
626 if (!l || tipc_link_is_reset(l))
630 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
631 n->link_id = tipc_link_id(l);
633 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
635 pr_debug("Lost link <%s> on network plane %c\n",
636 tipc_link_name(l), tipc_link_plane(l));
638 /* Select new active link if any available */
639 *slot0 = INVALID_BEARER_ID;
640 *slot1 = INVALID_BEARER_ID;
641 for (i = 0; i < MAX_BEARERS; i++) {
642 _l = n->links[i].link;
643 if (!_l || !tipc_link_is_up(_l))
647 prio = tipc_link_prio(_l);
650 if (prio > highest) {
659 if (!tipc_node_is_up(n)) {
660 if (tipc_link_peer_is_down(l))
661 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
662 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
663 tipc_link_fsm_evt(l, LINK_RESET_EVT);
665 tipc_link_build_reset_msg(l, xmitq);
666 *maddr = &n->links[*bearer_id].maddr;
667 node_lost_contact(n, &le->inputq);
668 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
671 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
673 /* There is still a working link => initiate failover */
674 *bearer_id = n->active_links[0];
675 tnl = n->links[*bearer_id].link;
676 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
677 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
678 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
679 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
681 tipc_link_fsm_evt(l, LINK_RESET_EVT);
682 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
683 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
684 *maddr = &n->links[*bearer_id].maddr;
687 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
689 struct tipc_link_entry *le = &n->links[bearer_id];
690 struct tipc_link *l = le->link;
691 struct tipc_media_addr *maddr;
692 struct sk_buff_head xmitq;
697 __skb_queue_head_init(&xmitq);
699 tipc_node_write_lock(n);
700 if (!tipc_link_is_establishing(l)) {
701 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
708 /* Defuse pending tipc_node_link_up() */
709 tipc_link_fsm_evt(l, LINK_RESET_EVT);
711 tipc_node_write_unlock(n);
712 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
713 tipc_sk_rcv(n->net, &le->inputq);
716 static bool tipc_node_is_up(struct tipc_node *n)
718 return n->active_links[0] != INVALID_BEARER_ID;
721 void tipc_node_check_dest(struct net *net, u32 onode,
722 struct tipc_bearer *b,
723 u16 capabilities, u32 signature,
724 struct tipc_media_addr *maddr,
725 bool *respond, bool *dupl_addr)
729 struct tipc_link_entry *le;
730 bool addr_match = false;
731 bool sign_match = false;
732 bool link_up = false;
733 bool accept_addr = false;
740 n = tipc_node_create(net, onode, capabilities);
744 tipc_node_write_lock(n);
746 le = &n->links[b->identity];
748 /* Prepare to validate requesting node's signature and media address */
750 link_up = l && tipc_link_is_up(l);
751 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
752 sign_match = (signature == n->signature);
754 /* These three flags give us eight permutations: */
756 if (sign_match && addr_match && link_up) {
757 /* All is fine. Do nothing. */
759 } else if (sign_match && addr_match && !link_up) {
760 /* Respond. The link will come up in due time */
762 } else if (sign_match && !addr_match && link_up) {
763 /* Peer has changed i/f address without rebooting.
764 * If so, the link will reset soon, and the next
765 * discovery will be accepted. So we can ignore it.
766 * It may also be an cloned or malicious peer having
767 * chosen the same node address and signature as an
769 * Ignore requests until the link goes down, if ever.
772 } else if (sign_match && !addr_match && !link_up) {
773 /* Peer link has changed i/f address without rebooting.
774 * It may also be a cloned or malicious peer; we can't
775 * distinguish between the two.
776 * The signature is correct, so we must accept.
780 } else if (!sign_match && addr_match && link_up) {
781 /* Peer node rebooted. Two possibilities:
782 * - Delayed re-discovery; this link endpoint has already
783 * reset and re-established contact with the peer, before
784 * receiving a discovery message from that node.
785 * (The peer happened to receive one from this node first).
786 * - The peer came back so fast that our side has not
787 * discovered it yet. Probing from this side will soon
788 * reset the link, since there can be no working link
789 * endpoint at the peer end, and the link will re-establish.
790 * Accept the signature, since it comes from a known peer.
792 n->signature = signature;
793 } else if (!sign_match && addr_match && !link_up) {
794 /* The peer node has rebooted.
795 * Accept signature, since it is a known peer.
797 n->signature = signature;
799 } else if (!sign_match && !addr_match && link_up) {
800 /* Peer rebooted with new address, or a new/duplicate peer.
801 * Ignore until the link goes down, if ever.
804 } else if (!sign_match && !addr_match && !link_up) {
805 /* Peer rebooted with new address, or it is a new peer.
806 * Accept signature and address.
808 n->signature = signature;
816 /* Now create new link if not already existing */
818 if (n->link_cnt == 2) {
819 pr_warn("Cannot establish 3rd link to %x\n", n->addr);
822 if_name = strchr(b->name, ':') + 1;
823 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
824 b->net_plane, b->mtu, b->priority,
825 b->window, mod(tipc_net(net)->random),
826 tipc_own_addr(net), onode,
828 tipc_bc_sndlink(n->net), n->bc_entry.link,
830 &n->bc_entry.namedq, &l)) {
835 tipc_link_fsm_evt(l, LINK_RESET_EVT);
836 if (n->state == NODE_FAILINGOVER)
837 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
840 tipc_node_calculate_timer(n, l);
841 if (n->link_cnt == 1)
842 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
845 memcpy(&le->maddr, maddr, sizeof(*maddr));
847 tipc_node_write_unlock(n);
848 if (reset && !tipc_link_is_reset(l))
849 tipc_node_link_down(n, b->identity, false);
853 void tipc_node_delete_links(struct net *net, int bearer_id)
855 struct tipc_net *tn = net_generic(net, tipc_net_id);
859 list_for_each_entry_rcu(n, &tn->node_list, list) {
860 tipc_node_link_down(n, bearer_id, true);
865 static void tipc_node_reset_links(struct tipc_node *n)
867 char addr_string[16];
870 pr_warn("Resetting all links to %s\n",
871 tipc_addr_string_fill(addr_string, n->addr));
873 for (i = 0; i < MAX_BEARERS; i++) {
874 tipc_node_link_down(n, i, false);
878 /* tipc_node_fsm_evt - node finite state machine
879 * Determines when contact is allowed with peer node
881 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
883 int state = n->state;
886 case SELF_DOWN_PEER_DOWN:
888 case SELF_ESTABL_CONTACT_EVT:
889 state = SELF_UP_PEER_COMING;
891 case PEER_ESTABL_CONTACT_EVT:
892 state = SELF_COMING_PEER_UP;
894 case SELF_LOST_CONTACT_EVT:
895 case PEER_LOST_CONTACT_EVT:
897 case NODE_SYNCH_END_EVT:
898 case NODE_SYNCH_BEGIN_EVT:
899 case NODE_FAILOVER_BEGIN_EVT:
900 case NODE_FAILOVER_END_EVT:
905 case SELF_UP_PEER_UP:
907 case SELF_LOST_CONTACT_EVT:
908 state = SELF_DOWN_PEER_LEAVING;
910 case PEER_LOST_CONTACT_EVT:
911 state = SELF_LEAVING_PEER_DOWN;
913 case NODE_SYNCH_BEGIN_EVT:
914 state = NODE_SYNCHING;
916 case NODE_FAILOVER_BEGIN_EVT:
917 state = NODE_FAILINGOVER;
919 case SELF_ESTABL_CONTACT_EVT:
920 case PEER_ESTABL_CONTACT_EVT:
921 case NODE_SYNCH_END_EVT:
922 case NODE_FAILOVER_END_EVT:
928 case SELF_DOWN_PEER_LEAVING:
930 case PEER_LOST_CONTACT_EVT:
931 state = SELF_DOWN_PEER_DOWN;
933 case SELF_ESTABL_CONTACT_EVT:
934 case PEER_ESTABL_CONTACT_EVT:
935 case SELF_LOST_CONTACT_EVT:
937 case NODE_SYNCH_END_EVT:
938 case NODE_SYNCH_BEGIN_EVT:
939 case NODE_FAILOVER_BEGIN_EVT:
940 case NODE_FAILOVER_END_EVT:
945 case SELF_UP_PEER_COMING:
947 case PEER_ESTABL_CONTACT_EVT:
948 state = SELF_UP_PEER_UP;
950 case SELF_LOST_CONTACT_EVT:
951 state = SELF_DOWN_PEER_LEAVING;
953 case SELF_ESTABL_CONTACT_EVT:
954 case PEER_LOST_CONTACT_EVT:
955 case NODE_SYNCH_END_EVT:
956 case NODE_FAILOVER_BEGIN_EVT:
958 case NODE_SYNCH_BEGIN_EVT:
959 case NODE_FAILOVER_END_EVT:
964 case SELF_COMING_PEER_UP:
966 case SELF_ESTABL_CONTACT_EVT:
967 state = SELF_UP_PEER_UP;
969 case PEER_LOST_CONTACT_EVT:
970 state = SELF_LEAVING_PEER_DOWN;
972 case SELF_LOST_CONTACT_EVT:
973 case PEER_ESTABL_CONTACT_EVT:
975 case NODE_SYNCH_END_EVT:
976 case NODE_SYNCH_BEGIN_EVT:
977 case NODE_FAILOVER_BEGIN_EVT:
978 case NODE_FAILOVER_END_EVT:
983 case SELF_LEAVING_PEER_DOWN:
985 case SELF_LOST_CONTACT_EVT:
986 state = SELF_DOWN_PEER_DOWN;
988 case SELF_ESTABL_CONTACT_EVT:
989 case PEER_ESTABL_CONTACT_EVT:
990 case PEER_LOST_CONTACT_EVT:
992 case NODE_SYNCH_END_EVT:
993 case NODE_SYNCH_BEGIN_EVT:
994 case NODE_FAILOVER_BEGIN_EVT:
995 case NODE_FAILOVER_END_EVT:
1000 case NODE_FAILINGOVER:
1002 case SELF_LOST_CONTACT_EVT:
1003 state = SELF_DOWN_PEER_LEAVING;
1005 case PEER_LOST_CONTACT_EVT:
1006 state = SELF_LEAVING_PEER_DOWN;
1008 case NODE_FAILOVER_END_EVT:
1009 state = SELF_UP_PEER_UP;
1011 case NODE_FAILOVER_BEGIN_EVT:
1012 case SELF_ESTABL_CONTACT_EVT:
1013 case PEER_ESTABL_CONTACT_EVT:
1015 case NODE_SYNCH_BEGIN_EVT:
1016 case NODE_SYNCH_END_EVT:
1023 case SELF_LOST_CONTACT_EVT:
1024 state = SELF_DOWN_PEER_LEAVING;
1026 case PEER_LOST_CONTACT_EVT:
1027 state = SELF_LEAVING_PEER_DOWN;
1029 case NODE_SYNCH_END_EVT:
1030 state = SELF_UP_PEER_UP;
1032 case NODE_FAILOVER_BEGIN_EVT:
1033 state = NODE_FAILINGOVER;
1035 case NODE_SYNCH_BEGIN_EVT:
1036 case SELF_ESTABL_CONTACT_EVT:
1037 case PEER_ESTABL_CONTACT_EVT:
1039 case NODE_FAILOVER_END_EVT:
1045 pr_err("Unknown node fsm state %x\n", state);
1052 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1055 static void node_lost_contact(struct tipc_node *n,
1056 struct sk_buff_head *inputq)
1058 char addr_string[16];
1059 struct tipc_sock_conn *conn, *safe;
1060 struct tipc_link *l;
1061 struct list_head *conns = &n->conn_sks;
1062 struct sk_buff *skb;
1065 pr_debug("Lost contact with %s\n",
1066 tipc_addr_string_fill(addr_string, n->addr));
1068 /* Clean up broadcast state */
1069 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1071 /* Abort any ongoing link failover */
1072 for (i = 0; i < MAX_BEARERS; i++) {
1073 l = n->links[i].link;
1075 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1078 /* Notify publications from this node */
1079 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1081 /* Notify sockets connected to node */
1082 list_for_each_entry_safe(conn, safe, conns, list) {
1083 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1084 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1085 conn->peer_node, conn->port,
1086 conn->peer_port, TIPC_ERR_NO_NODE);
1088 skb_queue_tail(inputq, skb);
1089 list_del(&conn->list);
1095 * tipc_node_get_linkname - get the name of a link
1097 * @bearer_id: id of the bearer
1098 * @node: peer node address
1099 * @linkname: link name output buffer
1101 * Returns 0 on success
1103 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1104 char *linkname, size_t len)
1106 struct tipc_link *link;
1108 struct tipc_node *node = tipc_node_find(net, addr);
1113 if (bearer_id >= MAX_BEARERS)
1116 tipc_node_read_lock(node);
1117 link = node->links[bearer_id].link;
1119 strncpy(linkname, tipc_link_name(link), len);
1123 tipc_node_read_unlock(node);
1124 tipc_node_put(node);
1128 /* Caller should hold node lock for the passed node */
1129 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1132 struct nlattr *attrs;
1134 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1135 NLM_F_MULTI, TIPC_NL_NODE_GET);
1139 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1143 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1145 if (tipc_node_is_up(node))
1146 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1149 nla_nest_end(msg->skb, attrs);
1150 genlmsg_end(msg->skb, hdr);
1155 nla_nest_cancel(msg->skb, attrs);
1157 genlmsg_cancel(msg->skb, hdr);
1163 * tipc_node_xmit() is the general link level function for message sending
1164 * @net: the applicable net namespace
1165 * @list: chain of buffers containing message
1166 * @dnode: address of destination node
1167 * @selector: a number used for deterministic link selection
1168 * Consumes the buffer chain, except when returning -ELINKCONG
1169 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
1171 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1172 u32 dnode, int selector)
1174 struct tipc_link_entry *le = NULL;
1175 struct tipc_node *n;
1176 struct sk_buff_head xmitq;
1178 int rc = -EHOSTUNREACH;
1180 __skb_queue_head_init(&xmitq);
1181 n = tipc_node_find(net, dnode);
1183 tipc_node_read_lock(n);
1184 bearer_id = n->active_links[selector & 1];
1185 if (bearer_id >= 0) {
1186 le = &n->links[bearer_id];
1187 spin_lock_bh(&le->lock);
1188 rc = tipc_link_xmit(le->link, list, &xmitq);
1189 spin_unlock_bh(&le->lock);
1191 tipc_node_read_unlock(n);
1193 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1194 else if (rc == -ENOBUFS)
1195 tipc_node_link_down(n, bearer_id, false);
1200 if (likely(in_own_node(net, dnode))) {
1201 tipc_sk_rcv(net, list);
1207 /* tipc_node_xmit_skb(): send single buffer to destination
1208 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1209 * messages, which will not be rejected
1210 * The only exception is datagram messages rerouted after secondary
1211 * lookup, which are rare and safe to dispose of anyway.
1212 * TODO: Return real return value, and let callers use
1213 * tipc_wait_for_sendpkt() where applicable
1215 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1218 struct sk_buff_head head;
1221 skb_queue_head_init(&head);
1222 __skb_queue_tail(&head, skb);
1223 rc = tipc_node_xmit(net, &head, dnode, selector);
1224 if (rc == -ELINKCONG)
1229 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1231 struct sk_buff *txskb;
1232 struct tipc_node *n;
1236 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1238 if (in_own_node(net, dst))
1240 if (!tipc_node_is_up(n))
1242 txskb = pskb_copy(skb, GFP_ATOMIC);
1245 msg_set_destnode(buf_msg(txskb), dst);
1246 tipc_node_xmit_skb(net, txskb, dst, 0);
1254 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1255 * @net: the applicable net namespace
1257 * @bearer_id: id of bearer message arrived on
1259 * Invoked with no locks held.
1261 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1264 struct sk_buff_head xmitq;
1265 struct tipc_bclink_entry *be;
1266 struct tipc_link_entry *le;
1267 struct tipc_msg *hdr = buf_msg(skb);
1268 int usr = msg_user(hdr);
1269 u32 dnode = msg_destnode(hdr);
1270 struct tipc_node *n;
1272 __skb_queue_head_init(&xmitq);
1274 /* If NACK for other node, let rcv link for that node peek into it */
1275 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1276 n = tipc_node_find(net, dnode);
1278 n = tipc_node_find(net, msg_prevnode(hdr));
1284 le = &n->links[bearer_id];
1286 rc = tipc_bcast_rcv(net, be->link, skb);
1288 /* Broadcast link reset may happen at reassembly failure */
1289 if (rc & TIPC_LINK_DOWN_EVT)
1290 tipc_node_reset_links(n);
1292 /* Broadcast ACKs are sent on a unicast link */
1293 if (rc & TIPC_LINK_SND_BC_ACK) {
1294 tipc_node_read_lock(n);
1295 tipc_link_build_ack_msg(le->link, &xmitq);
1296 tipc_node_read_unlock(n);
1299 if (!skb_queue_empty(&xmitq))
1300 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1302 /* Deliver. 'arrvq' is under inputq2's lock protection */
1303 if (!skb_queue_empty(&be->inputq1)) {
1304 spin_lock_bh(&be->inputq2.lock);
1305 spin_lock_bh(&be->inputq1.lock);
1306 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1307 spin_unlock_bh(&be->inputq1.lock);
1308 spin_unlock_bh(&be->inputq2.lock);
1309 tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
1315 * tipc_node_check_state - check and if necessary update node state
1317 * @bearer_id: identity of bearer delivering the packet
1318 * Returns true if state is ok, otherwise consumes buffer and returns false
1320 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1321 int bearer_id, struct sk_buff_head *xmitq)
1323 struct tipc_msg *hdr = buf_msg(skb);
1324 int usr = msg_user(hdr);
1325 int mtyp = msg_type(hdr);
1326 u16 oseqno = msg_seqno(hdr);
1327 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1328 u16 exp_pkts = msg_msgcnt(hdr);
1329 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1330 int state = n->state;
1331 struct tipc_link *l, *tnl, *pl = NULL;
1332 struct tipc_media_addr *maddr;
1335 l = n->links[bearer_id].link;
1338 rcv_nxt = tipc_link_rcv_nxt(l);
1341 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1344 /* Find parallel link, if any */
1345 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1346 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1347 pl = n->links[pb_id].link;
1352 /* Check and update node accesibility if applicable */
1353 if (state == SELF_UP_PEER_COMING) {
1354 if (!tipc_link_is_up(l))
1356 if (!msg_peer_link_is_up(hdr))
1358 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1361 if (state == SELF_DOWN_PEER_LEAVING) {
1362 if (msg_peer_node_is_up(hdr))
1364 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1368 if (state == SELF_LEAVING_PEER_DOWN)
1371 /* Ignore duplicate packets */
1372 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1375 /* Initiate or update failover mode if applicable */
1376 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1377 syncpt = oseqno + exp_pkts - 1;
1378 if (pl && tipc_link_is_up(pl)) {
1379 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1380 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1381 tipc_link_inputq(l));
1383 /* If pkts arrive out of order, use lowest calculated syncpt */
1384 if (less(syncpt, n->sync_point))
1385 n->sync_point = syncpt;
1388 /* Open parallel link when tunnel link reaches synch point */
1389 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1390 if (!more(rcv_nxt, n->sync_point))
1392 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1394 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1398 /* No synching needed if only one link */
1399 if (!pl || !tipc_link_is_up(pl))
1402 /* Initiate synch mode if applicable */
1403 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1404 syncpt = iseqno + exp_pkts - 1;
1405 if (!tipc_link_is_up(l)) {
1406 tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
1407 __tipc_node_link_up(n, bearer_id, xmitq);
1409 if (n->state == SELF_UP_PEER_UP) {
1410 n->sync_point = syncpt;
1411 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1412 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1416 /* Open tunnel link when parallel link reaches synch point */
1417 if (n->state == NODE_SYNCHING) {
1418 if (tipc_link_is_synching(l)) {
1424 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1425 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1426 if (more(dlv_nxt, n->sync_point)) {
1427 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1428 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1433 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1435 if (usr == LINK_PROTOCOL)
1443 * tipc_rcv - process TIPC packets/messages arriving from off-node
1444 * @net: the applicable net namespace
1446 * @bearer: pointer to bearer message arrived on
1448 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1449 * structure (i.e. cannot be NULL), but bearer can be inactive.
1451 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1453 struct sk_buff_head xmitq;
1454 struct tipc_node *n;
1455 struct tipc_msg *hdr = buf_msg(skb);
1456 int usr = msg_user(hdr);
1457 int bearer_id = b->identity;
1458 struct tipc_link_entry *le;
1459 u16 bc_ack = msg_bcast_ack(hdr);
1462 __skb_queue_head_init(&xmitq);
1464 /* Ensure message is well-formed */
1465 if (unlikely(!tipc_msg_validate(skb)))
1468 /* Handle arrival of discovery or broadcast packet */
1469 if (unlikely(msg_non_seq(hdr))) {
1470 if (unlikely(usr == LINK_CONFIG))
1471 return tipc_disc_rcv(net, skb, b);
1473 return tipc_node_bc_rcv(net, skb, bearer_id);
1476 /* Locate neighboring node that sent packet */
1477 n = tipc_node_find(net, msg_prevnode(hdr));
1480 le = &n->links[bearer_id];
1482 /* Ensure broadcast reception is in synch with peer's send state */
1483 if (unlikely(usr == LINK_PROTOCOL))
1484 tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
1485 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
1486 tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
1488 /* Receive packet directly if conditions permit */
1489 tipc_node_read_lock(n);
1490 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
1491 spin_lock_bh(&le->lock);
1493 rc = tipc_link_rcv(le->link, skb, &xmitq);
1496 spin_unlock_bh(&le->lock);
1498 tipc_node_read_unlock(n);
1500 /* Check/update node state before receiving */
1501 if (unlikely(skb)) {
1502 tipc_node_write_lock(n);
1503 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1505 rc = tipc_link_rcv(le->link, skb, &xmitq);
1509 tipc_node_write_unlock(n);
1512 if (unlikely(rc & TIPC_LINK_UP_EVT))
1513 tipc_node_link_up(n, bearer_id, &xmitq);
1515 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1516 tipc_node_link_down(n, bearer_id, false);
1518 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1519 tipc_named_rcv(net, &n->bc_entry.namedq);
1521 if (!skb_queue_empty(&le->inputq))
1522 tipc_sk_rcv(net, &le->inputq);
1524 if (!skb_queue_empty(&xmitq))
1525 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1532 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1535 struct net *net = sock_net(skb->sk);
1536 struct tipc_net *tn = net_generic(net, tipc_net_id);
1537 int done = cb->args[0];
1538 int last_addr = cb->args[1];
1539 struct tipc_node *node;
1540 struct tipc_nl_msg msg;
1546 msg.portid = NETLINK_CB(cb->skb).portid;
1547 msg.seq = cb->nlh->nlmsg_seq;
1551 node = tipc_node_find(net, last_addr);
1554 /* We never set seq or call nl_dump_check_consistent()
1555 * this means that setting prev_seq here will cause the
1556 * consistence check to fail in the netlink callback
1557 * handler. Resulting in the NLMSG_DONE message having
1558 * the NLM_F_DUMP_INTR flag set if the node state
1559 * changed while we released the lock.
1564 tipc_node_put(node);
1567 list_for_each_entry_rcu(node, &tn->node_list, list) {
1569 if (node->addr == last_addr)
1575 tipc_node_read_lock(node);
1576 err = __tipc_nl_add_node(&msg, node);
1578 last_addr = node->addr;
1579 tipc_node_read_unlock(node);
1583 tipc_node_read_unlock(node);
1588 cb->args[1] = last_addr;
1594 /* tipc_node_find_by_name - locate owner node of link by link's name
1595 * @net: the applicable net namespace
1596 * @name: pointer to link name string
1597 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1599 * Returns pointer to node owning the link, or 0 if no matching link is found.
1601 static struct tipc_node *tipc_node_find_by_name(struct net *net,
1602 const char *link_name,
1603 unsigned int *bearer_id)
1605 struct tipc_net *tn = net_generic(net, tipc_net_id);
1606 struct tipc_link *l;
1607 struct tipc_node *n;
1608 struct tipc_node *found_node = NULL;
1613 list_for_each_entry_rcu(n, &tn->node_list, list) {
1614 tipc_node_read_lock(n);
1615 for (i = 0; i < MAX_BEARERS; i++) {
1616 l = n->links[i].link;
1617 if (l && !strcmp(tipc_link_name(l), link_name)) {
1623 tipc_node_read_unlock(n);
1632 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1638 struct tipc_link *link;
1639 struct tipc_node *node;
1640 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1641 struct net *net = sock_net(skb->sk);
1643 if (!info->attrs[TIPC_NLA_LINK])
1646 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1647 info->attrs[TIPC_NLA_LINK],
1648 tipc_nl_link_policy);
1652 if (!attrs[TIPC_NLA_LINK_NAME])
1655 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1657 if (strcmp(name, tipc_bclink_name) == 0)
1658 return tipc_nl_bc_link_set(net, attrs);
1660 node = tipc_node_find_by_name(net, name, &bearer_id);
1664 tipc_node_read_lock(node);
1666 link = node->links[bearer_id].link;
1672 if (attrs[TIPC_NLA_LINK_PROP]) {
1673 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1675 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1682 if (props[TIPC_NLA_PROP_TOL]) {
1685 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1686 tipc_link_set_tolerance(link, tol);
1688 if (props[TIPC_NLA_PROP_PRIO]) {
1691 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1692 tipc_link_set_prio(link, prio);
1694 if (props[TIPC_NLA_PROP_WIN]) {
1697 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1698 tipc_link_set_queue_limits(link, win);
1703 tipc_node_read_unlock(node);
1708 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
1710 struct net *net = genl_info_net(info);
1711 struct tipc_nl_msg msg;
1715 msg.portid = info->snd_portid;
1716 msg.seq = info->snd_seq;
1718 if (!info->attrs[TIPC_NLA_LINK_NAME])
1720 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
1722 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1726 if (strcmp(name, tipc_bclink_name) == 0) {
1727 err = tipc_nl_add_bc_link(net, &msg);
1729 nlmsg_free(msg.skb);
1734 struct tipc_node *node;
1735 struct tipc_link *link;
1737 node = tipc_node_find_by_name(net, name, &bearer_id);
1741 tipc_node_read_lock(node);
1742 link = node->links[bearer_id].link;
1744 tipc_node_read_unlock(node);
1745 nlmsg_free(msg.skb);
1749 err = __tipc_nl_add_link(net, &msg, link, 0);
1750 tipc_node_read_unlock(node);
1752 nlmsg_free(msg.skb);
1757 return genlmsg_reply(msg.skb, info);
1760 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
1764 unsigned int bearer_id;
1765 struct tipc_link *link;
1766 struct tipc_node *node;
1767 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1768 struct net *net = sock_net(skb->sk);
1769 struct tipc_link_entry *le;
1771 if (!info->attrs[TIPC_NLA_LINK])
1774 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1775 info->attrs[TIPC_NLA_LINK],
1776 tipc_nl_link_policy);
1780 if (!attrs[TIPC_NLA_LINK_NAME])
1783 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1785 if (strcmp(link_name, tipc_bclink_name) == 0) {
1786 err = tipc_bclink_reset_stats(net);
1792 node = tipc_node_find_by_name(net, link_name, &bearer_id);
1796 le = &node->links[bearer_id];
1797 tipc_node_read_lock(node);
1798 spin_lock_bh(&le->lock);
1799 link = node->links[bearer_id].link;
1801 spin_unlock_bh(&le->lock);
1802 tipc_node_read_unlock(node);
1805 tipc_link_reset_stats(link);
1806 spin_unlock_bh(&le->lock);
1807 tipc_node_read_unlock(node);
1811 /* Caller should hold node lock */
1812 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
1813 struct tipc_node *node, u32 *prev_link)
1818 for (i = *prev_link; i < MAX_BEARERS; i++) {
1821 if (!node->links[i].link)
1824 err = __tipc_nl_add_link(net, msg,
1825 node->links[i].link, NLM_F_MULTI);
1834 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
1836 struct net *net = sock_net(skb->sk);
1837 struct tipc_net *tn = net_generic(net, tipc_net_id);
1838 struct tipc_node *node;
1839 struct tipc_nl_msg msg;
1840 u32 prev_node = cb->args[0];
1841 u32 prev_link = cb->args[1];
1842 int done = cb->args[2];
1849 msg.portid = NETLINK_CB(cb->skb).portid;
1850 msg.seq = cb->nlh->nlmsg_seq;
1854 node = tipc_node_find(net, prev_node);
1856 /* We never set seq or call nl_dump_check_consistent()
1857 * this means that setting prev_seq here will cause the
1858 * consistence check to fail in the netlink callback
1859 * handler. Resulting in the last NLMSG_DONE message
1860 * having the NLM_F_DUMP_INTR flag set.
1865 tipc_node_put(node);
1867 list_for_each_entry_continue_rcu(node, &tn->node_list,
1869 tipc_node_read_lock(node);
1870 err = __tipc_nl_add_node_links(net, &msg, node,
1872 tipc_node_read_unlock(node);
1876 prev_node = node->addr;
1879 err = tipc_nl_add_bc_link(net, &msg);
1883 list_for_each_entry_rcu(node, &tn->node_list, list) {
1884 tipc_node_read_lock(node);
1885 err = __tipc_nl_add_node_links(net, &msg, node,
1887 tipc_node_read_unlock(node);
1891 prev_node = node->addr;
1898 cb->args[0] = prev_node;
1899 cb->args[1] = prev_link;