blob: 3f7a4ed719900bc2464c23c021853f1cf4734070 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/node.c: TIPC node management routines
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -04004 * Copyright (c) 2000-2006, 2012-2015, Ericsson AB
Ying Xue46651c52014-03-27 12:54:36 +08005 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Richard Alpe22ae7cf2015-02-09 09:50:18 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Jon Paul Maloy50100a52014-08-22 18:09:07 -040041#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040042#include "bcast.h"
Jon Paul Maloyd9992972015-07-16 16:54:31 -040043#include "discover.h"
Jon Paul Maloyc8199302015-10-15 14:52:46 -040044
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050045#define INVALID_NODE_SIG 0x10000
46
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050047/* 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
51 */
52enum {
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)
57};
58
59struct tipc_link_entry {
60 struct tipc_link *link;
61 spinlock_t lock; /* per link */
62 u32 mtu;
63 struct sk_buff_head inputq;
64 struct tipc_media_addr maddr;
65};
66
67struct 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;
73};
74
75/**
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
97 */
98struct tipc_node {
99 u32 addr;
100 struct kref kref;
101 rwlock_t lock;
102 struct net *net;
103 struct hlist_node hash;
104 int active_links[2];
105 struct tipc_link_entry links[MAX_BEARERS];
106 struct tipc_bclink_entry bc_entry;
107 int action_flags;
108 struct list_head list;
109 int state;
110 u16 sync_point;
111 int link_cnt;
112 u16 working_links;
113 u16 capabilities;
114 u32 signature;
115 u32 link_id;
116 struct list_head publ_list;
117 struct list_head conn_sks;
118 unsigned long keepalive_intv;
119 struct timer_list timer;
120 struct rcu_head rcu;
121};
122
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400123/* Node FSM states and events:
124 */
125enum {
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,
133 NODE_SYNCHING = 0xcc
134};
135
136enum {
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
145};
146
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400147static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
148 struct sk_buff_head *xmitq,
149 struct tipc_media_addr **maddr);
150static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
151 bool delete);
152static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800153static void tipc_node_delete(struct tipc_node *node);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400154static void tipc_node_timeout(unsigned long data);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400155static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500156static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
157static void tipc_node_put(struct tipc_node *node);
158static bool tipc_node_is_up(struct tipc_node *n);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400160struct tipc_sock_conn {
161 u32 port;
162 u32 peer_port;
163 u32 peer_node;
164 struct list_head list;
165};
166
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500167static 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] = {
170 .type = NLA_STRING,
171 .len = TIPC_MAX_LINK_NAME
172 },
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 }
181};
182
Richard Alpe3e4b6ab2014-11-20 10:29:17 +0100183static 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 }
187};
188
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500189static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
190{
191 int bearer_id = n->active_links[sel & 1];
192
193 if (unlikely(bearer_id == INVALID_BEARER_ID))
194 return NULL;
195
196 return n->links[bearer_id].link;
197}
198
199int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
200{
201 struct tipc_node *n;
202 int bearer_id;
203 unsigned int mtu = MAX_MSG_SIZE;
204
205 n = tipc_node_find(net, addr);
206 if (unlikely(!n))
207 return mtu;
208
209 bearer_id = n->active_links[sel & 1];
210 if (likely(bearer_id != INVALID_BEARER_ID))
211 mtu = n->links[bearer_id].mtu;
212 tipc_node_put(n);
213 return mtu;
214}
Allan Stephens1ec2bb02011-10-27 15:03:24 -0400215/*
Allan Stephensa635b462011-11-04 11:54:43 -0400216 * 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).
220 */
Paul Gortmaker872f24d2012-04-23 04:49:13 +0000221static unsigned int tipc_hashfn(u32 addr)
Allan Stephensa635b462011-11-04 11:54:43 -0400222{
223 return addr & (NODE_HTABLE_SIZE - 1);
224}
225
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800226static void tipc_node_kref_release(struct kref *kref)
227{
228 struct tipc_node *node = container_of(kref, struct tipc_node, kref);
229
230 tipc_node_delete(node);
231}
232
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500233static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800234{
235 kref_put(&node->kref, tipc_node_kref_release);
236}
237
238static void tipc_node_get(struct tipc_node *node)
239{
240 kref_get(&node->kref);
241}
242
Allan Stephensa635b462011-11-04 11:54:43 -0400243/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500244 * tipc_node_find - locate specified node object, if it exists
245 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500246static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500247{
Ying Xuef2f98002015-01-09 15:27:05 +0800248 struct tipc_net *tn = net_generic(net, tipc_net_id);
Allan Stephens672d99e2011-02-25 18:42:52 -0500249 struct tipc_node *node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500250
Ying Xue34747532015-01-09 15:27:10 +0800251 if (unlikely(!in_own_cluster_exact(net, addr)))
Allan Stephens672d99e2011-02-25 18:42:52 -0500252 return NULL;
253
Ying Xue6c7a7622014-03-27 12:54:37 +0800254 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800255 hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
256 hash) {
Ying Xue46651c52014-03-27 12:54:36 +0800257 if (node->addr == addr) {
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800258 tipc_node_get(node);
Ying Xue6c7a7622014-03-27 12:54:37 +0800259 rcu_read_unlock();
Allan Stephens672d99e2011-02-25 18:42:52 -0500260 return node;
Ying Xue46651c52014-03-27 12:54:36 +0800261 }
Allan Stephens672d99e2011-02-25 18:42:52 -0500262 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800263 rcu_read_unlock();
Allan Stephens672d99e2011-02-25 18:42:52 -0500264 return NULL;
265}
266
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500267static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500268{
269 read_lock_bh(&n->lock);
270}
271
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500272static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500273{
274 read_unlock_bh(&n->lock);
275}
276
277static void tipc_node_write_lock(struct tipc_node *n)
278{
279 write_lock_bh(&n->lock);
280}
281
282static void tipc_node_write_unlock(struct tipc_node *n)
283{
284 struct net *net = n->net;
285 u32 addr = 0;
286 u32 flags = n->action_flags;
287 u32 link_id = 0;
288 struct list_head *publ_list;
289
290 if (likely(!flags)) {
291 write_unlock_bh(&n->lock);
292 return;
293 }
294
295 addr = n->addr;
296 link_id = n->link_id;
297 publ_list = &n->publ_list;
298
299 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
300 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
301
302 write_unlock_bh(&n->lock);
303
304 if (flags & TIPC_NOTIFY_NODE_DOWN)
305 tipc_publ_notify(net, publ_list, addr);
306
307 if (flags & TIPC_NOTIFY_NODE_UP)
308 tipc_named_node_up(net, addr);
309
310 if (flags & TIPC_NOTIFY_LINK_UP)
311 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
312 TIPC_NODE_SCOPE, link_id, addr);
313
314 if (flags & TIPC_NOTIFY_LINK_DOWN)
315 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
316 link_id, addr);
317}
318
Jon Paul Maloycf148812015-07-30 18:24:22 -0400319struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320{
Ying Xuef2f98002015-01-09 15:27:05 +0800321 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500322 struct tipc_node *n, *temp_node;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500323 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Ying Xuef2f98002015-01-09 15:27:05 +0800325 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500326 n = tipc_node_find(net, addr);
327 if (n)
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500328 goto exit;
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500329 n = kzalloc(sizeof(*n), GFP_ATOMIC);
330 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400331 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500332 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700333 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500334 n->addr = addr;
335 n->net = net;
336 n->capabilities = capabilities;
337 kref_init(&n->kref);
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);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500347 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500348 spin_lock_init(&n->links[i].lock);
349 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
Ying Xuef2f98002015-01-09 15:27:05 +0800350 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500351 if (n->addr < temp_node->addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500352 break;
353 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500354 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,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500360 U16_MAX,
361 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500362 n->capabilities,
363 &n->bc_entry.inputq1,
364 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400365 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500366 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400367 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500368 kfree(n);
369 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400370 goto exit;
371 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500372 tipc_node_get(n);
373 setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
374 n->keepalive_intv = U32_MAX;
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500375exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800376 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500377 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378}
379
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400380static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
381{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500382 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400383 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
384 unsigned long keepalive_intv = msecs_to_jiffies(intv);
385
386 /* Link with lowest tolerance determines timer interval */
387 if (keepalive_intv < n->keepalive_intv)
388 n->keepalive_intv = keepalive_intv;
389
390 /* Ensure link's abort limit corresponds to current interval */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500391 tipc_link_set_abort_limit(l, tol / jiffies_to_msecs(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400392}
393
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800394static void tipc_node_delete(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800396 list_del_rcu(&node->list);
397 hlist_del_rcu(&node->hash);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400398 kfree(node->bc_entry.link);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800399 kfree_rcu(node, rcu);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400}
401
Ying Xuef2f98002015-01-09 15:27:05 +0800402void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800403{
Ying Xuef2f98002015-01-09 15:27:05 +0800404 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue46651c52014-03-27 12:54:36 +0800405 struct tipc_node *node, *t_node;
406
Ying Xuef2f98002015-01-09 15:27:05 +0800407 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400408 list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
409 if (del_timer(&node->timer))
410 tipc_node_put(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800411 tipc_node_put(node);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400412 }
Ying Xuef2f98002015-01-09 15:27:05 +0800413 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800414}
415
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500416void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
417{
418 struct tipc_node *n;
419
420 if (in_own_node(net, addr))
421 return;
422
423 n = tipc_node_find(net, addr);
424 if (!n) {
425 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
426 return;
427 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500428 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500429 list_add_tail(subscr, &n->publ_list);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500430 tipc_node_write_unlock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500431 tipc_node_put(n);
432}
433
434void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
435{
436 struct tipc_node *n;
437
438 if (in_own_node(net, addr))
439 return;
440
441 n = tipc_node_find(net, addr);
442 if (!n) {
443 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
444 return;
445 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500446 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500447 list_del_init(subscr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500448 tipc_node_write_unlock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500449 tipc_node_put(n);
450}
451
Ying Xuef2f98002015-01-09 15:27:05 +0800452int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400453{
454 struct tipc_node *node;
455 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800456 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400457
Ying Xue34747532015-01-09 15:27:10 +0800458 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400459 return 0;
460
Ying Xuef2f98002015-01-09 15:27:05 +0800461 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400462 if (!node) {
463 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
464 return -EHOSTUNREACH;
465 }
466 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800467 if (!conn) {
468 err = -EHOSTUNREACH;
469 goto exit;
470 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400471 conn->peer_node = dnode;
472 conn->port = port;
473 conn->peer_port = peer_port;
474
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500475 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400476 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500477 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800478exit:
479 tipc_node_put(node);
480 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400481}
482
Ying Xuef2f98002015-01-09 15:27:05 +0800483void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400484{
485 struct tipc_node *node;
486 struct tipc_sock_conn *conn, *safe;
487
Ying Xue34747532015-01-09 15:27:10 +0800488 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400489 return;
490
Ying Xuef2f98002015-01-09 15:27:05 +0800491 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400492 if (!node)
493 return;
494
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500495 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400496 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
497 if (port != conn->port)
498 continue;
499 list_del(&conn->list);
500 kfree(conn);
501 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500502 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800503 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400504}
505
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400506/* tipc_node_timeout - handle expiration of node timer
507 */
508static void tipc_node_timeout(unsigned long data)
509{
510 struct tipc_node *n = (struct tipc_node *)data;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400511 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400512 struct sk_buff_head xmitq;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400513 int bearer_id;
514 int rc = 0;
515
516 __skb_queue_head_init(&xmitq);
517
518 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500519 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400520 le = &n->links[bearer_id];
Jon Paul Maloy2312bf62015-11-19 14:30:43 -0500521 spin_lock_bh(&le->lock);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400522 if (le->link) {
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400523 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400524 tipc_node_calculate_timer(n, le->link);
525 rc = tipc_link_timeout(le->link, &xmitq);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400526 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -0500527 spin_unlock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500528 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400529 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);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400532 }
533 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
534 tipc_node_get(n);
535 tipc_node_put(n);
536}
537
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400539 * __tipc_node_link_up - handle addition of link
540 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 * Link becomes active (alone or shared) or standby, depending on its priority.
542 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400543static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
544 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400546 int *slot0 = &n->active_links[0];
547 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400548 struct tipc_link *ol = node_active_link(n, 0);
549 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400551 if (!nl)
552 return;
553
554 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
555 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400556 return;
557
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400558 n->working_links++;
559 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500560 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400561
562 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500563 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800564
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400565 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400566 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400567
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100568 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500569 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400571 /* First link? => give it both slots */
572 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400573 *slot0 = bearer_id;
574 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400575 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
576 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400577 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400578 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400580
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400581 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500582 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
583 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400584 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400585 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400586 tipc_link_set_active(nl, true);
587 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500588 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400589 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a82015-10-22 08:51:47 -0400590 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400591 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500592 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400595 /* Prepare synchronization with first link */
596 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597}
598
599/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400600 * tipc_node_link_up - handle addition of link
601 *
602 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400604static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
605 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500607 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400608 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500609 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400610}
611
612/**
613 * __tipc_node_link_down - handle loss of link
614 */
615static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
616 struct sk_buff_head *xmitq,
617 struct tipc_media_addr **maddr)
618{
619 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400620 int *slot0 = &n->active_links[0];
621 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500622 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400623 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400625 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400626 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400627 return;
628
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400629 n->working_links--;
630 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500631 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700632
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400633 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400634
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100635 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500636 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400638 /* 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))
644 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400645 if (_l == l)
646 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500647 prio = tipc_link_prio(_l);
648 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400649 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500650 if (prio > highest) {
651 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400652 *slot0 = i;
653 *slot1 = i;
654 continue;
655 }
656 *slot1 = i;
657 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400658
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400659 if (!tipc_node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400660 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);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400664 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400665 tipc_link_build_reset_msg(l, xmitq);
666 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400667 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400668 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400669 return;
670 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400671 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400672
673 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500674 *bearer_id = n->active_links[0];
675 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400676 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
677 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500678 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400679 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400680 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400681 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400682 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400683 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500684 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400685}
686
687static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
688{
689 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400690 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400691 struct tipc_media_addr *maddr;
692 struct sk_buff_head xmitq;
693
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400694 if (!l)
695 return;
696
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400697 __skb_queue_head_init(&xmitq);
698
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500699 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400700 if (!tipc_link_is_establishing(l)) {
701 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
702 if (delete) {
703 kfree(l);
704 le->link = NULL;
705 n->link_cnt--;
706 }
707 } else {
708 /* Defuse pending tipc_node_link_up() */
709 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400710 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500711 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400712 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
713 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714}
715
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500716static bool tipc_node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400718 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719}
720
Jon Paul Maloycf148812015-07-30 18:24:22 -0400721void 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)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400726{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400727 struct tipc_node *n;
728 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400729 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400730 bool addr_match = false;
731 bool sign_match = false;
732 bool link_up = false;
733 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400734 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400735 char *if_name;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400736
Jon Paul Maloycf148812015-07-30 18:24:22 -0400737 *dupl_addr = false;
738 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400739
Jon Paul Maloycf148812015-07-30 18:24:22 -0400740 n = tipc_node_create(net, onode, capabilities);
741 if (!n)
742 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400743
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500744 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400745
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400746 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400747
748 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400749 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400750 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400751 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400752 sign_match = (signature == n->signature);
753
754 /* These three flags give us eight permutations: */
755
756 if (sign_match && addr_match && link_up) {
757 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400758 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400759 } else if (sign_match && addr_match && !link_up) {
760 /* Respond. The link will come up in due time */
761 *respond = true;
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
768 * existing one.
769 * Ignore requests until the link goes down, if ever.
770 */
771 *dupl_addr = true;
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.
777 */
778 accept_addr = true;
779 *respond = true;
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.
791 */
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.
796 */
797 n->signature = signature;
798 *respond = true;
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.
802 */
803 *dupl_addr = true;
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.
807 */
808 n->signature = signature;
809 accept_addr = true;
810 *respond = true;
811 }
812
813 if (!accept_addr)
814 goto exit;
815
816 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400817 if (!l) {
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400818 if (n->link_cnt == 2) {
819 pr_warn("Cannot establish 3rd link to %x\n", n->addr);
820 goto exit;
821 }
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400822 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400823 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400824 b->net_plane, b->mtu, b->priority,
825 b->window, mod(tipc_net(net)->random),
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400826 tipc_own_addr(net), onode,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400827 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400828 tipc_bc_sndlink(n->net), n->bc_entry.link,
829 &le->inputq,
830 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -0400831 *respond = false;
832 goto exit;
833 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400834 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400835 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -0400836 if (n->state == NODE_FAILINGOVER)
837 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400838 le->link = l;
839 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400840 tipc_node_calculate_timer(n, l);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400841 if (n->link_cnt == 1)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400842 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
843 tipc_node_get(n);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400844 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400845 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400846exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500847 tipc_node_write_unlock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400848 if (reset && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400849 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400850 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400851}
852
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400853void tipc_node_delete_links(struct net *net, int bearer_id)
854{
855 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400856 struct tipc_node *n;
857
858 rcu_read_lock();
859 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400860 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400861 }
862 rcu_read_unlock();
863}
864
865static void tipc_node_reset_links(struct tipc_node *n)
866{
867 char addr_string[16];
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400868 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400869
870 pr_warn("Resetting all links to %s\n",
871 tipc_addr_string_fill(addr_string, n->addr));
872
873 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400874 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400875 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400876}
877
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400878/* tipc_node_fsm_evt - node finite state machine
879 * Determines when contact is allowed with peer node
880 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400881static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400882{
883 int state = n->state;
884
885 switch (state) {
886 case SELF_DOWN_PEER_DOWN:
887 switch (evt) {
888 case SELF_ESTABL_CONTACT_EVT:
889 state = SELF_UP_PEER_COMING;
890 break;
891 case PEER_ESTABL_CONTACT_EVT:
892 state = SELF_COMING_PEER_UP;
893 break;
894 case SELF_LOST_CONTACT_EVT:
895 case PEER_LOST_CONTACT_EVT:
896 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400897 case NODE_SYNCH_END_EVT:
898 case NODE_SYNCH_BEGIN_EVT:
899 case NODE_FAILOVER_BEGIN_EVT:
900 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400901 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400902 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400903 }
904 break;
905 case SELF_UP_PEER_UP:
906 switch (evt) {
907 case SELF_LOST_CONTACT_EVT:
908 state = SELF_DOWN_PEER_LEAVING;
909 break;
910 case PEER_LOST_CONTACT_EVT:
911 state = SELF_LEAVING_PEER_DOWN;
912 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400913 case NODE_SYNCH_BEGIN_EVT:
914 state = NODE_SYNCHING;
915 break;
916 case NODE_FAILOVER_BEGIN_EVT:
917 state = NODE_FAILINGOVER;
918 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400919 case SELF_ESTABL_CONTACT_EVT:
920 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400921 case NODE_SYNCH_END_EVT:
922 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400923 break;
924 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400925 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400926 }
927 break;
928 case SELF_DOWN_PEER_LEAVING:
929 switch (evt) {
930 case PEER_LOST_CONTACT_EVT:
931 state = SELF_DOWN_PEER_DOWN;
932 break;
933 case SELF_ESTABL_CONTACT_EVT:
934 case PEER_ESTABL_CONTACT_EVT:
935 case SELF_LOST_CONTACT_EVT:
936 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400937 case NODE_SYNCH_END_EVT:
938 case NODE_SYNCH_BEGIN_EVT:
939 case NODE_FAILOVER_BEGIN_EVT:
940 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400941 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400942 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400943 }
944 break;
945 case SELF_UP_PEER_COMING:
946 switch (evt) {
947 case PEER_ESTABL_CONTACT_EVT:
948 state = SELF_UP_PEER_UP;
949 break;
950 case SELF_LOST_CONTACT_EVT:
951 state = SELF_DOWN_PEER_LEAVING;
952 break;
953 case SELF_ESTABL_CONTACT_EVT:
954 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400955 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400956 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400957 break;
958 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400959 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400960 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400961 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400962 }
963 break;
964 case SELF_COMING_PEER_UP:
965 switch (evt) {
966 case SELF_ESTABL_CONTACT_EVT:
967 state = SELF_UP_PEER_UP;
968 break;
969 case PEER_LOST_CONTACT_EVT:
970 state = SELF_LEAVING_PEER_DOWN;
971 break;
972 case SELF_LOST_CONTACT_EVT:
973 case PEER_ESTABL_CONTACT_EVT:
974 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400975 case NODE_SYNCH_END_EVT:
976 case NODE_SYNCH_BEGIN_EVT:
977 case NODE_FAILOVER_BEGIN_EVT:
978 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400979 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400980 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400981 }
982 break;
983 case SELF_LEAVING_PEER_DOWN:
984 switch (evt) {
985 case SELF_LOST_CONTACT_EVT:
986 state = SELF_DOWN_PEER_DOWN;
987 break;
988 case SELF_ESTABL_CONTACT_EVT:
989 case PEER_ESTABL_CONTACT_EVT:
990 case PEER_LOST_CONTACT_EVT:
991 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400992 case NODE_SYNCH_END_EVT:
993 case NODE_SYNCH_BEGIN_EVT:
994 case NODE_FAILOVER_BEGIN_EVT:
995 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400996 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400997 goto illegal_evt;
998 }
999 break;
1000 case NODE_FAILINGOVER:
1001 switch (evt) {
1002 case SELF_LOST_CONTACT_EVT:
1003 state = SELF_DOWN_PEER_LEAVING;
1004 break;
1005 case PEER_LOST_CONTACT_EVT:
1006 state = SELF_LEAVING_PEER_DOWN;
1007 break;
1008 case NODE_FAILOVER_END_EVT:
1009 state = SELF_UP_PEER_UP;
1010 break;
1011 case NODE_FAILOVER_BEGIN_EVT:
1012 case SELF_ESTABL_CONTACT_EVT:
1013 case PEER_ESTABL_CONTACT_EVT:
1014 break;
1015 case NODE_SYNCH_BEGIN_EVT:
1016 case NODE_SYNCH_END_EVT:
1017 default:
1018 goto illegal_evt;
1019 }
1020 break;
1021 case NODE_SYNCHING:
1022 switch (evt) {
1023 case SELF_LOST_CONTACT_EVT:
1024 state = SELF_DOWN_PEER_LEAVING;
1025 break;
1026 case PEER_LOST_CONTACT_EVT:
1027 state = SELF_LEAVING_PEER_DOWN;
1028 break;
1029 case NODE_SYNCH_END_EVT:
1030 state = SELF_UP_PEER_UP;
1031 break;
1032 case NODE_FAILOVER_BEGIN_EVT:
1033 state = NODE_FAILINGOVER;
1034 break;
1035 case NODE_SYNCH_BEGIN_EVT:
1036 case SELF_ESTABL_CONTACT_EVT:
1037 case PEER_ESTABL_CONTACT_EVT:
1038 break;
1039 case NODE_FAILOVER_END_EVT:
1040 default:
1041 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001042 }
1043 break;
1044 default:
1045 pr_err("Unknown node fsm state %x\n", state);
1046 break;
1047 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001048 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001049 return;
1050
1051illegal_evt:
1052 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001053}
1054
Jon Paul Maloy52666982015-10-22 08:51:41 -04001055static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001056 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 char addr_string[16];
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001059 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001060 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001061 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001062 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001063 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064
Erik Hugne3fa9cac2015-01-22 17:10:31 +01001065 pr_debug("Lost contact with %s\n",
Jon Paul Maloy52666982015-10-22 08:51:41 -04001066 tipc_addr_string_fill(addr_string, n->addr));
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001067
Jon Paul Maloy52666982015-10-22 08:51:41 -04001068 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001069 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001071 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001073 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001074 if (l)
1075 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001077
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001078 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001079 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001080
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,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001084 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001085 conn->peer_node, conn->port,
1086 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001087 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001088 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001089 list_del(&conn->list);
1090 kfree(conn);
1091 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092}
1093
Erik Hugne78acb1f2014-04-24 16:26:47 +02001094/**
1095 * tipc_node_get_linkname - get the name of a link
1096 *
1097 * @bearer_id: id of the bearer
1098 * @node: peer node address
1099 * @linkname: link name output buffer
1100 *
1101 * Returns 0 on success
1102 */
Ying Xuef2f98002015-01-09 15:27:05 +08001103int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1104 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001105{
1106 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001107 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001108 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001109
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001110 if (!node)
1111 return err;
1112
1113 if (bearer_id >= MAX_BEARERS)
1114 goto exit;
1115
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001116 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001117 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001118 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001119 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001120 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001121 }
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001122exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001123 tipc_node_read_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001124 tipc_node_put(node);
1125 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001126}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001127
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001128/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001129static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001130{
1131 void *hdr;
1132 struct nlattr *attrs;
1133
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001134 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001135 NLM_F_MULTI, TIPC_NL_NODE_GET);
1136 if (!hdr)
1137 return -EMSGSIZE;
1138
1139 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1140 if (!attrs)
1141 goto msg_full;
1142
1143 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1144 goto attr_msg_full;
1145 if (tipc_node_is_up(node))
1146 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1147 goto attr_msg_full;
1148
1149 nla_nest_end(msg->skb, attrs);
1150 genlmsg_end(msg->skb, hdr);
1151
1152 return 0;
1153
1154attr_msg_full:
1155 nla_nest_cancel(msg->skb, attrs);
1156msg_full:
1157 genlmsg_cancel(msg->skb, hdr);
1158
1159 return -EMSGSIZE;
1160}
1161
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001162/**
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
1170 */
1171int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1172 u32 dnode, int selector)
1173{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001174 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001175 struct tipc_node *n;
1176 struct sk_buff_head xmitq;
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001177 int bearer_id = -1;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001178 int rc = -EHOSTUNREACH;
1179
1180 __skb_queue_head_init(&xmitq);
1181 n = tipc_node_find(net, dnode);
1182 if (likely(n)) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001183 tipc_node_read_lock(n);
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001184 bearer_id = n->active_links[selector & 1];
1185 if (bearer_id >= 0) {
1186 le = &n->links[bearer_id];
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001187 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001188 rc = tipc_link_xmit(le->link, list, &xmitq);
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001189 spin_unlock_bh(&le->lock);
1190 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001191 tipc_node_read_unlock(n);
1192 if (likely(!skb_queue_empty(&xmitq))) {
1193 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1194 return 0;
1195 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001196 if (unlikely(rc == -ENOBUFS))
1197 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001198 tipc_node_put(n);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001199 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001200 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001201
1202 if (unlikely(!in_own_node(net, dnode)))
1203 return rc;
1204 tipc_sk_rcv(net, list);
1205 return 0;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001206}
1207
1208/* tipc_node_xmit_skb(): send single buffer to destination
1209 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1210 * messages, which will not be rejected
1211 * The only exception is datagram messages rerouted after secondary
1212 * lookup, which are rare and safe to dispose of anyway.
1213 * TODO: Return real return value, and let callers use
1214 * tipc_wait_for_sendpkt() where applicable
1215 */
1216int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1217 u32 selector)
1218{
1219 struct sk_buff_head head;
1220 int rc;
1221
1222 skb_queue_head_init(&head);
1223 __skb_queue_tail(&head, skb);
1224 rc = tipc_node_xmit(net, &head, dnode, selector);
1225 if (rc == -ELINKCONG)
1226 kfree_skb(skb);
1227 return 0;
1228}
1229
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001230void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1231{
1232 struct sk_buff *txskb;
1233 struct tipc_node *n;
1234 u32 dst;
1235
1236 rcu_read_lock();
1237 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1238 dst = n->addr;
1239 if (in_own_node(net, dst))
1240 continue;
1241 if (!tipc_node_is_up(n))
1242 continue;
1243 txskb = pskb_copy(skb, GFP_ATOMIC);
1244 if (!txskb)
1245 break;
1246 msg_set_destnode(buf_msg(txskb), dst);
1247 tipc_node_xmit_skb(net, txskb, dst, 0);
1248 }
1249 rcu_read_unlock();
1250
1251 kfree_skb(skb);
1252}
1253
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001254/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001255 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1256 * @net: the applicable net namespace
1257 * @skb: TIPC packet
1258 * @bearer_id: id of bearer message arrived on
1259 *
1260 * Invoked with no locks held.
1261 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001262static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001263{
1264 int rc;
1265 struct sk_buff_head xmitq;
1266 struct tipc_bclink_entry *be;
1267 struct tipc_link_entry *le;
1268 struct tipc_msg *hdr = buf_msg(skb);
1269 int usr = msg_user(hdr);
1270 u32 dnode = msg_destnode(hdr);
1271 struct tipc_node *n;
1272
1273 __skb_queue_head_init(&xmitq);
1274
1275 /* If NACK for other node, let rcv link for that node peek into it */
1276 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1277 n = tipc_node_find(net, dnode);
1278 else
1279 n = tipc_node_find(net, msg_prevnode(hdr));
1280 if (!n) {
1281 kfree_skb(skb);
1282 return;
1283 }
1284 be = &n->bc_entry;
1285 le = &n->links[bearer_id];
1286
1287 rc = tipc_bcast_rcv(net, be->link, skb);
1288
1289 /* Broadcast link reset may happen at reassembly failure */
1290 if (rc & TIPC_LINK_DOWN_EVT)
1291 tipc_node_reset_links(n);
1292
1293 /* Broadcast ACKs are sent on a unicast link */
1294 if (rc & TIPC_LINK_SND_BC_ACK) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001295 tipc_node_read_lock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001296 tipc_link_build_ack_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001297 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001298 }
1299
1300 if (!skb_queue_empty(&xmitq))
1301 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1302
1303 /* Deliver. 'arrvq' is under inputq2's lock protection */
1304 if (!skb_queue_empty(&be->inputq1)) {
1305 spin_lock_bh(&be->inputq2.lock);
1306 spin_lock_bh(&be->inputq1.lock);
1307 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1308 spin_unlock_bh(&be->inputq1.lock);
1309 spin_unlock_bh(&be->inputq2.lock);
1310 tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
1311 }
1312 tipc_node_put(n);
1313}
1314
1315/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001316 * tipc_node_check_state - check and if necessary update node state
1317 * @skb: TIPC packet
1318 * @bearer_id: identity of bearer delivering the packet
1319 * Returns true if state is ok, otherwise consumes buffer and returns false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001320 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001321static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001322 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001323{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001324 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001325 int usr = msg_user(hdr);
1326 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001327 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001328 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1329 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001330 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001331 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001332 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001333 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001334 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001335
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001336 l = n->links[bearer_id].link;
1337 if (!l)
1338 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001339 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001340
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001341
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001342 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1343 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001344
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001345 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001346 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1347 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1348 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001349 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001350 }
1351 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001352
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001353 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001354 if (state == SELF_UP_PEER_COMING) {
1355 if (!tipc_link_is_up(l))
1356 return true;
1357 if (!msg_peer_link_is_up(hdr))
1358 return true;
1359 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1360 }
1361
1362 if (state == SELF_DOWN_PEER_LEAVING) {
1363 if (msg_peer_node_is_up(hdr))
1364 return false;
1365 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001366 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001367 }
1368
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001369 if (state == SELF_LEAVING_PEER_DOWN)
1370 return false;
1371
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001372 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001373 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001374 return true;
1375
1376 /* Initiate or update failover mode if applicable */
1377 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1378 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001379 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001380 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001381 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1382 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001383 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001384 /* If pkts arrive out of order, use lowest calculated syncpt */
1385 if (less(syncpt, n->sync_point))
1386 n->sync_point = syncpt;
1387 }
1388
1389 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001390 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001391 if (!more(rcv_nxt, n->sync_point))
1392 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001393 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1394 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001395 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001396 return true;
1397 }
1398
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001399 /* No synching needed if only one link */
1400 if (!pl || !tipc_link_is_up(pl))
1401 return true;
1402
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001403 /* Initiate synch mode if applicable */
1404 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001405 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001406 if (!tipc_link_is_up(l)) {
1407 tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001408 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001409 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001410 if (n->state == SELF_UP_PEER_UP) {
1411 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001412 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001413 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1414 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001415 }
1416
1417 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001418 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001419 if (tipc_link_is_synching(l)) {
1420 tnl = l;
1421 } else {
1422 tnl = pl;
1423 pl = l;
1424 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001425 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1426 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001427 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001428 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001429 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001430 return true;
1431 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001432 if (l == pl)
1433 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001434 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1435 return true;
1436 if (usr == LINK_PROTOCOL)
1437 return true;
1438 return false;
1439 }
1440 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001441}
1442
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001443/**
1444 * tipc_rcv - process TIPC packets/messages arriving from off-node
1445 * @net: the applicable net namespace
1446 * @skb: TIPC packet
1447 * @bearer: pointer to bearer message arrived on
1448 *
1449 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1450 * structure (i.e. cannot be NULL), but bearer can be inactive.
1451 */
1452void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1453{
1454 struct sk_buff_head xmitq;
1455 struct tipc_node *n;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001456 struct tipc_msg *hdr = buf_msg(skb);
1457 int usr = msg_user(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001458 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001459 struct tipc_link_entry *le;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001460 u16 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001461 int rc = 0;
1462
1463 __skb_queue_head_init(&xmitq);
1464
1465 /* Ensure message is well-formed */
1466 if (unlikely(!tipc_msg_validate(skb)))
1467 goto discard;
1468
Jon Paul Maloy52666982015-10-22 08:51:41 -04001469 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001470 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001471 if (unlikely(usr == LINK_CONFIG))
1472 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001473 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001474 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001475 }
1476
1477 /* Locate neighboring node that sent packet */
1478 n = tipc_node_find(net, msg_prevnode(hdr));
1479 if (unlikely(!n))
1480 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001481 le = &n->links[bearer_id];
1482
Jon Paul Maloy52666982015-10-22 08:51:41 -04001483 /* Ensure broadcast reception is in synch with peer's send state */
1484 if (unlikely(usr == LINK_PROTOCOL))
1485 tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001486 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy52666982015-10-22 08:51:41 -04001487 tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
1488
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001489 /* Receive packet directly if conditions permit */
1490 tipc_node_read_lock(n);
1491 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001492 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001493 if (le->link) {
1494 rc = tipc_link_rcv(le->link, skb, &xmitq);
1495 skb = NULL;
1496 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001497 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001498 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001499 tipc_node_read_unlock(n);
1500
1501 /* Check/update node state before receiving */
1502 if (unlikely(skb)) {
1503 tipc_node_write_lock(n);
1504 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1505 if (le->link) {
1506 rc = tipc_link_rcv(le->link, skb, &xmitq);
1507 skb = NULL;
1508 }
1509 }
1510 tipc_node_write_unlock(n);
1511 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001512
1513 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001514 tipc_node_link_up(n, bearer_id, &xmitq);
1515
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001516 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001517 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001518
Jon Paul Maloy52666982015-10-22 08:51:41 -04001519 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1520 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001521
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001522 if (!skb_queue_empty(&le->inputq))
1523 tipc_sk_rcv(net, &le->inputq);
1524
1525 if (!skb_queue_empty(&xmitq))
1526 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1527
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001528 tipc_node_put(n);
1529discard:
1530 kfree_skb(skb);
1531}
1532
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001533int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1534{
1535 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001536 struct net *net = sock_net(skb->sk);
1537 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001538 int done = cb->args[0];
1539 int last_addr = cb->args[1];
1540 struct tipc_node *node;
1541 struct tipc_nl_msg msg;
1542
1543 if (done)
1544 return 0;
1545
1546 msg.skb = skb;
1547 msg.portid = NETLINK_CB(cb->skb).portid;
1548 msg.seq = cb->nlh->nlmsg_seq;
1549
1550 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001551 if (last_addr) {
1552 node = tipc_node_find(net, last_addr);
1553 if (!node) {
1554 rcu_read_unlock();
1555 /* We never set seq or call nl_dump_check_consistent()
1556 * this means that setting prev_seq here will cause the
1557 * consistence check to fail in the netlink callback
1558 * handler. Resulting in the NLMSG_DONE message having
1559 * the NLM_F_DUMP_INTR flag set if the node state
1560 * changed while we released the lock.
1561 */
1562 cb->prev_seq = 1;
1563 return -EPIPE;
1564 }
1565 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001566 }
1567
Ying Xuef2f98002015-01-09 15:27:05 +08001568 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001569 if (last_addr) {
1570 if (node->addr == last_addr)
1571 last_addr = 0;
1572 else
1573 continue;
1574 }
1575
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001576 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001577 err = __tipc_nl_add_node(&msg, node);
1578 if (err) {
1579 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001580 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001581 goto out;
1582 }
1583
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001584 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001585 }
1586 done = 1;
1587out:
1588 cb->args[0] = done;
1589 cb->args[1] = last_addr;
1590 rcu_read_unlock();
1591
1592 return skb->len;
1593}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001594
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001595/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001596 * @net: the applicable net namespace
1597 * @name: pointer to link name string
1598 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1599 *
1600 * Returns pointer to node owning the link, or 0 if no matching link is found.
1601 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001602static struct tipc_node *tipc_node_find_by_name(struct net *net,
1603 const char *link_name,
1604 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001605{
1606 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001607 struct tipc_link *l;
1608 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001609 struct tipc_node *found_node = NULL;
1610 int i;
1611
1612 *bearer_id = 0;
1613 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001614 list_for_each_entry_rcu(n, &tn->node_list, list) {
1615 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001616 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001617 l = n->links[i].link;
1618 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001619 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001620 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001621 break;
1622 }
1623 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001624 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001625 if (found_node)
1626 break;
1627 }
1628 rcu_read_unlock();
1629
1630 return found_node;
1631}
1632
1633int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1634{
1635 int err;
1636 int res = 0;
1637 int bearer_id;
1638 char *name;
1639 struct tipc_link *link;
1640 struct tipc_node *node;
1641 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1642 struct net *net = sock_net(skb->sk);
1643
1644 if (!info->attrs[TIPC_NLA_LINK])
1645 return -EINVAL;
1646
1647 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1648 info->attrs[TIPC_NLA_LINK],
1649 tipc_nl_link_policy);
1650 if (err)
1651 return err;
1652
1653 if (!attrs[TIPC_NLA_LINK_NAME])
1654 return -EINVAL;
1655
1656 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1657
1658 if (strcmp(name, tipc_bclink_name) == 0)
1659 return tipc_nl_bc_link_set(net, attrs);
1660
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001661 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001662 if (!node)
1663 return -EINVAL;
1664
1665 tipc_node_read_lock(node);
1666
1667 link = node->links[bearer_id].link;
1668 if (!link) {
1669 res = -EINVAL;
1670 goto out;
1671 }
1672
1673 if (attrs[TIPC_NLA_LINK_PROP]) {
1674 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1675
1676 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1677 props);
1678 if (err) {
1679 res = err;
1680 goto out;
1681 }
1682
1683 if (props[TIPC_NLA_PROP_TOL]) {
1684 u32 tol;
1685
1686 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001687 tipc_link_set_tolerance(link, tol);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001688 }
1689 if (props[TIPC_NLA_PROP_PRIO]) {
1690 u32 prio;
1691
1692 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001693 tipc_link_set_prio(link, prio);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001694 }
1695 if (props[TIPC_NLA_PROP_WIN]) {
1696 u32 win;
1697
1698 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1699 tipc_link_set_queue_limits(link, win);
1700 }
1701 }
1702
1703out:
1704 tipc_node_read_unlock(node);
1705
1706 return res;
1707}
1708
1709int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
1710{
1711 struct net *net = genl_info_net(info);
1712 struct tipc_nl_msg msg;
1713 char *name;
1714 int err;
1715
1716 msg.portid = info->snd_portid;
1717 msg.seq = info->snd_seq;
1718
1719 if (!info->attrs[TIPC_NLA_LINK_NAME])
1720 return -EINVAL;
1721 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
1722
1723 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1724 if (!msg.skb)
1725 return -ENOMEM;
1726
1727 if (strcmp(name, tipc_bclink_name) == 0) {
1728 err = tipc_nl_add_bc_link(net, &msg);
1729 if (err) {
1730 nlmsg_free(msg.skb);
1731 return err;
1732 }
1733 } else {
1734 int bearer_id;
1735 struct tipc_node *node;
1736 struct tipc_link *link;
1737
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001738 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001739 if (!node)
1740 return -EINVAL;
1741
1742 tipc_node_read_lock(node);
1743 link = node->links[bearer_id].link;
1744 if (!link) {
1745 tipc_node_read_unlock(node);
1746 nlmsg_free(msg.skb);
1747 return -EINVAL;
1748 }
1749
1750 err = __tipc_nl_add_link(net, &msg, link, 0);
1751 tipc_node_read_unlock(node);
1752 if (err) {
1753 nlmsg_free(msg.skb);
1754 return err;
1755 }
1756 }
1757
1758 return genlmsg_reply(msg.skb, info);
1759}
1760
1761int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
1762{
1763 int err;
1764 char *link_name;
1765 unsigned int bearer_id;
1766 struct tipc_link *link;
1767 struct tipc_node *node;
1768 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1769 struct net *net = sock_net(skb->sk);
1770 struct tipc_link_entry *le;
1771
1772 if (!info->attrs[TIPC_NLA_LINK])
1773 return -EINVAL;
1774
1775 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1776 info->attrs[TIPC_NLA_LINK],
1777 tipc_nl_link_policy);
1778 if (err)
1779 return err;
1780
1781 if (!attrs[TIPC_NLA_LINK_NAME])
1782 return -EINVAL;
1783
1784 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1785
1786 if (strcmp(link_name, tipc_bclink_name) == 0) {
1787 err = tipc_bclink_reset_stats(net);
1788 if (err)
1789 return err;
1790 return 0;
1791 }
1792
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001793 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001794 if (!node)
1795 return -EINVAL;
1796
1797 le = &node->links[bearer_id];
1798 tipc_node_read_lock(node);
1799 spin_lock_bh(&le->lock);
1800 link = node->links[bearer_id].link;
1801 if (!link) {
1802 spin_unlock_bh(&le->lock);
1803 tipc_node_read_unlock(node);
1804 return -EINVAL;
1805 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001806 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001807 spin_unlock_bh(&le->lock);
1808 tipc_node_read_unlock(node);
1809 return 0;
1810}
1811
1812/* Caller should hold node lock */
1813static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
1814 struct tipc_node *node, u32 *prev_link)
1815{
1816 u32 i;
1817 int err;
1818
1819 for (i = *prev_link; i < MAX_BEARERS; i++) {
1820 *prev_link = i;
1821
1822 if (!node->links[i].link)
1823 continue;
1824
1825 err = __tipc_nl_add_link(net, msg,
1826 node->links[i].link, NLM_F_MULTI);
1827 if (err)
1828 return err;
1829 }
1830 *prev_link = 0;
1831
1832 return 0;
1833}
1834
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001835int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001836{
1837 struct net *net = sock_net(skb->sk);
1838 struct tipc_net *tn = net_generic(net, tipc_net_id);
1839 struct tipc_node *node;
1840 struct tipc_nl_msg msg;
1841 u32 prev_node = cb->args[0];
1842 u32 prev_link = cb->args[1];
1843 int done = cb->args[2];
1844 int err;
1845
1846 if (done)
1847 return 0;
1848
1849 msg.skb = skb;
1850 msg.portid = NETLINK_CB(cb->skb).portid;
1851 msg.seq = cb->nlh->nlmsg_seq;
1852
1853 rcu_read_lock();
1854 if (prev_node) {
1855 node = tipc_node_find(net, prev_node);
1856 if (!node) {
1857 /* We never set seq or call nl_dump_check_consistent()
1858 * this means that setting prev_seq here will cause the
1859 * consistence check to fail in the netlink callback
1860 * handler. Resulting in the last NLMSG_DONE message
1861 * having the NLM_F_DUMP_INTR flag set.
1862 */
1863 cb->prev_seq = 1;
1864 goto out;
1865 }
1866 tipc_node_put(node);
1867
1868 list_for_each_entry_continue_rcu(node, &tn->node_list,
1869 list) {
1870 tipc_node_read_lock(node);
1871 err = __tipc_nl_add_node_links(net, &msg, node,
1872 &prev_link);
1873 tipc_node_read_unlock(node);
1874 if (err)
1875 goto out;
1876
1877 prev_node = node->addr;
1878 }
1879 } else {
1880 err = tipc_nl_add_bc_link(net, &msg);
1881 if (err)
1882 goto out;
1883
1884 list_for_each_entry_rcu(node, &tn->node_list, list) {
1885 tipc_node_read_lock(node);
1886 err = __tipc_nl_add_node_links(net, &msg, node,
1887 &prev_link);
1888 tipc_node_read_unlock(node);
1889 if (err)
1890 goto out;
1891
1892 prev_node = node->addr;
1893 }
1894 }
1895 done = 1;
1896out:
1897 rcu_read_unlock();
1898
1899 cb->args[0] = prev_node;
1900 cb->args[1] = prev_link;
1901 cb->args[2] = done;
1902
1903 return skb->len;
1904}