blob: e110ba67422e277745a1ab3cdfa1f3e214afd99a [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/* Out-of-range value for node signature */
46#define INVALID_NODE_SIG 0x10000
47
48#define INVALID_BEARER_ID -1
49
50/* Flags used to take different actions according to flag type
51 * TIPC_NOTIFY_NODE_DOWN: notify node is down
52 * TIPC_NOTIFY_NODE_UP: notify node is up
53 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
54 */
55enum {
56 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
57 TIPC_NOTIFY_NODE_UP = (1 << 4),
58 TIPC_NOTIFY_LINK_UP = (1 << 6),
59 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
60};
61
62struct tipc_link_entry {
63 struct tipc_link *link;
64 spinlock_t lock; /* per link */
65 u32 mtu;
66 struct sk_buff_head inputq;
67 struct tipc_media_addr maddr;
68};
69
70struct tipc_bclink_entry {
71 struct tipc_link *link;
72 struct sk_buff_head inputq1;
73 struct sk_buff_head arrvq;
74 struct sk_buff_head inputq2;
75 struct sk_buff_head namedq;
76};
77
78/**
79 * struct tipc_node - TIPC node structure
80 * @addr: network address of node
81 * @ref: reference counter to node object
82 * @lock: rwlock governing access to structure
83 * @net: the applicable net namespace
84 * @hash: links to adjacent nodes in unsorted hash chain
85 * @inputq: pointer to input queue containing messages for msg event
86 * @namedq: pointer to name table input queue with name table messages
87 * @active_links: bearer ids of active links, used as index into links[] array
88 * @links: array containing references to all links to node
89 * @action_flags: bit mask of different types of node actions
90 * @state: connectivity state vs peer node
91 * @sync_point: sequence number where synch/failover is finished
92 * @list: links to adjacent nodes in sorted list of cluster's nodes
93 * @working_links: number of working links to node (both active and standby)
94 * @link_cnt: number of links to node
95 * @capabilities: bitmap, indicating peer node's functional capabilities
96 * @signature: node instance identifier
97 * @link_id: local and remote bearer ids of changing link, if any
98 * @publ_list: list of publications
99 * @rcu: rcu struct for tipc_node
100 */
101struct tipc_node {
102 u32 addr;
103 struct kref kref;
104 rwlock_t lock;
105 struct net *net;
106 struct hlist_node hash;
107 int active_links[2];
108 struct tipc_link_entry links[MAX_BEARERS];
109 struct tipc_bclink_entry bc_entry;
110 int action_flags;
111 struct list_head list;
112 int state;
113 u16 sync_point;
114 int link_cnt;
115 u16 working_links;
116 u16 capabilities;
117 u32 signature;
118 u32 link_id;
119 struct list_head publ_list;
120 struct list_head conn_sks;
121 unsigned long keepalive_intv;
122 struct timer_list timer;
123 struct rcu_head rcu;
124};
125
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400126/* Node FSM states and events:
127 */
128enum {
129 SELF_DOWN_PEER_DOWN = 0xdd,
130 SELF_UP_PEER_UP = 0xaa,
131 SELF_DOWN_PEER_LEAVING = 0xd1,
132 SELF_UP_PEER_COMING = 0xac,
133 SELF_COMING_PEER_UP = 0xca,
134 SELF_LEAVING_PEER_DOWN = 0x1d,
135 NODE_FAILINGOVER = 0xf0,
136 NODE_SYNCHING = 0xcc
137};
138
139enum {
140 SELF_ESTABL_CONTACT_EVT = 0xece,
141 SELF_LOST_CONTACT_EVT = 0x1ce,
142 PEER_ESTABL_CONTACT_EVT = 0x9ece,
143 PEER_LOST_CONTACT_EVT = 0x91ce,
144 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
145 NODE_FAILOVER_END_EVT = 0xfee,
146 NODE_SYNCH_BEGIN_EVT = 0xcbe,
147 NODE_SYNCH_END_EVT = 0xcee
148};
149
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400150static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
151 struct sk_buff_head *xmitq,
152 struct tipc_media_addr **maddr);
153static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
154 bool delete);
155static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800156static void tipc_node_delete(struct tipc_node *node);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400157static void tipc_node_timeout(unsigned long data);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400158static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500159static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
160static void tipc_node_put(struct tipc_node *node);
161static bool tipc_node_is_up(struct tipc_node *n);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400163struct tipc_sock_conn {
164 u32 port;
165 u32 peer_port;
166 u32 peer_node;
167 struct list_head list;
168};
169
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500170static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
171 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
172 [TIPC_NLA_LINK_NAME] = {
173 .type = NLA_STRING,
174 .len = TIPC_MAX_LINK_NAME
175 },
176 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
177 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
178 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
179 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
180 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
181 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
182 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
183 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
184};
185
Richard Alpe3e4b6ab2014-11-20 10:29:17 +0100186static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
187 [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC },
188 [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 },
189 [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG }
190};
191
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500192static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
193{
194 int bearer_id = n->active_links[sel & 1];
195
196 if (unlikely(bearer_id == INVALID_BEARER_ID))
197 return NULL;
198
199 return n->links[bearer_id].link;
200}
201
202int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
203{
204 struct tipc_node *n;
205 int bearer_id;
206 unsigned int mtu = MAX_MSG_SIZE;
207
208 n = tipc_node_find(net, addr);
209 if (unlikely(!n))
210 return mtu;
211
212 bearer_id = n->active_links[sel & 1];
213 if (likely(bearer_id != INVALID_BEARER_ID))
214 mtu = n->links[bearer_id].mtu;
215 tipc_node_put(n);
216 return mtu;
217}
Allan Stephens1ec2bb02011-10-27 15:03:24 -0400218/*
Allan Stephensa635b462011-11-04 11:54:43 -0400219 * A trivial power-of-two bitmask technique is used for speed, since this
220 * operation is done for every incoming TIPC packet. The number of hash table
221 * entries has been chosen so that no hash chain exceeds 8 nodes and will
222 * usually be much smaller (typically only a single node).
223 */
Paul Gortmaker872f24d2012-04-23 04:49:13 +0000224static unsigned int tipc_hashfn(u32 addr)
Allan Stephensa635b462011-11-04 11:54:43 -0400225{
226 return addr & (NODE_HTABLE_SIZE - 1);
227}
228
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800229static void tipc_node_kref_release(struct kref *kref)
230{
231 struct tipc_node *node = container_of(kref, struct tipc_node, kref);
232
233 tipc_node_delete(node);
234}
235
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500236static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800237{
238 kref_put(&node->kref, tipc_node_kref_release);
239}
240
241static void tipc_node_get(struct tipc_node *node)
242{
243 kref_get(&node->kref);
244}
245
Allan Stephensa635b462011-11-04 11:54:43 -0400246/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500247 * tipc_node_find - locate specified node object, if it exists
248 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500249static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500250{
Ying Xuef2f98002015-01-09 15:27:05 +0800251 struct tipc_net *tn = net_generic(net, tipc_net_id);
Allan Stephens672d99e2011-02-25 18:42:52 -0500252 struct tipc_node *node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500253
Ying Xue34747532015-01-09 15:27:10 +0800254 if (unlikely(!in_own_cluster_exact(net, addr)))
Allan Stephens672d99e2011-02-25 18:42:52 -0500255 return NULL;
256
Ying Xue6c7a7622014-03-27 12:54:37 +0800257 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800258 hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
259 hash) {
Ying Xue46651c52014-03-27 12:54:36 +0800260 if (node->addr == addr) {
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800261 tipc_node_get(node);
Ying Xue6c7a7622014-03-27 12:54:37 +0800262 rcu_read_unlock();
Allan Stephens672d99e2011-02-25 18:42:52 -0500263 return node;
Ying Xue46651c52014-03-27 12:54:36 +0800264 }
Allan Stephens672d99e2011-02-25 18:42:52 -0500265 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800266 rcu_read_unlock();
Allan Stephens672d99e2011-02-25 18:42:52 -0500267 return NULL;
268}
269
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500270static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500271{
272 read_lock_bh(&n->lock);
273}
274
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500275static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500276{
277 read_unlock_bh(&n->lock);
278}
279
280static void tipc_node_write_lock(struct tipc_node *n)
281{
282 write_lock_bh(&n->lock);
283}
284
285static void tipc_node_write_unlock(struct tipc_node *n)
286{
287 struct net *net = n->net;
288 u32 addr = 0;
289 u32 flags = n->action_flags;
290 u32 link_id = 0;
291 struct list_head *publ_list;
292
293 if (likely(!flags)) {
294 write_unlock_bh(&n->lock);
295 return;
296 }
297
298 addr = n->addr;
299 link_id = n->link_id;
300 publ_list = &n->publ_list;
301
302 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
303 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
304
305 write_unlock_bh(&n->lock);
306
307 if (flags & TIPC_NOTIFY_NODE_DOWN)
308 tipc_publ_notify(net, publ_list, addr);
309
310 if (flags & TIPC_NOTIFY_NODE_UP)
311 tipc_named_node_up(net, addr);
312
313 if (flags & TIPC_NOTIFY_LINK_UP)
314 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
315 TIPC_NODE_SCOPE, link_id, addr);
316
317 if (flags & TIPC_NOTIFY_LINK_DOWN)
318 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
319 link_id, addr);
320}
321
Jon Paul Maloycf148812015-07-30 18:24:22 -0400322struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323{
Ying Xuef2f98002015-01-09 15:27:05 +0800324 struct tipc_net *tn = net_generic(net, tipc_net_id);
Allan Stephens672d99e2011-02-25 18:42:52 -0500325 struct tipc_node *n_ptr, *temp_node;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500326 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327
Ying Xuef2f98002015-01-09 15:27:05 +0800328 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500329 n_ptr = tipc_node_find(net, addr);
330 if (n_ptr)
331 goto exit;
Allan Stephens5af54792010-12-31 18:59:23 +0000332 n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700333 if (!n_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400334 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500335 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700336 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700337 n_ptr->addr = addr;
Ying Xuef2f98002015-01-09 15:27:05 +0800338 n_ptr->net = net;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400339 n_ptr->capabilities = capabilities;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800340 kref_init(&n_ptr->kref);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500341 rwlock_init(&n_ptr->lock);
Allan Stephens672d99e2011-02-25 18:42:52 -0500342 INIT_HLIST_NODE(&n_ptr->hash);
343 INIT_LIST_HEAD(&n_ptr->list);
Ying Xuea8f48af2014-11-26 11:41:45 +0800344 INIT_LIST_HEAD(&n_ptr->publ_list);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400345 INIT_LIST_HEAD(&n_ptr->conn_sks);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400346 skb_queue_head_init(&n_ptr->bc_entry.namedq);
347 skb_queue_head_init(&n_ptr->bc_entry.inputq1);
348 __skb_queue_head_init(&n_ptr->bc_entry.arrvq);
349 skb_queue_head_init(&n_ptr->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500350 for (i = 0; i < MAX_BEARERS; i++)
351 spin_lock_init(&n_ptr->links[i].lock);
Ying Xuef2f98002015-01-09 15:27:05 +0800352 hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
Ying Xuef2f98002015-01-09 15:27:05 +0800353 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
Allan Stephens672d99e2011-02-25 18:42:52 -0500354 if (n_ptr->addr < temp_node->addr)
355 break;
356 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800357 list_add_tail_rcu(&n_ptr->list, &temp_node->list);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400358 n_ptr->state = SELF_DOWN_PEER_LEAVING;
Allan Stephensfc0eea62011-10-28 16:26:41 -0400359 n_ptr->signature = INVALID_NODE_SIG;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400360 n_ptr->active_links[0] = INVALID_BEARER_ID;
361 n_ptr->active_links[1] = INVALID_BEARER_ID;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400362 if (!tipc_link_bc_create(net, tipc_own_addr(net), n_ptr->addr,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400363 U16_MAX, tipc_bc_sndlink(net)->window,
364 n_ptr->capabilities,
365 &n_ptr->bc_entry.inputq1,
366 &n_ptr->bc_entry.namedq,
367 tipc_bc_sndlink(net),
368 &n_ptr->bc_entry.link)) {
369 pr_warn("Broadcast rcv link creation failed, no memory\n");
370 kfree(n_ptr);
371 n_ptr = NULL;
372 goto exit;
373 }
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800374 tipc_node_get(n_ptr);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400375 setup_timer(&n_ptr->timer, tipc_node_timeout, (unsigned long)n_ptr);
376 n_ptr->keepalive_intv = U32_MAX;
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500377exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800378 spin_unlock_bh(&tn->node_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 return n_ptr;
380}
381
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400382static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
383{
384 unsigned long tol = l->tolerance;
385 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
386 unsigned long keepalive_intv = msecs_to_jiffies(intv);
387
388 /* Link with lowest tolerance determines timer interval */
389 if (keepalive_intv < n->keepalive_intv)
390 n->keepalive_intv = keepalive_intv;
391
392 /* Ensure link's abort limit corresponds to current interval */
393 l->abort_limit = l->tolerance / jiffies_to_msecs(n->keepalive_intv);
394}
395
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800396static void tipc_node_delete(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800398 list_del_rcu(&node->list);
399 hlist_del_rcu(&node->hash);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400400 kfree(node->bc_entry.link);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800401 kfree_rcu(node, rcu);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402}
403
Ying Xuef2f98002015-01-09 15:27:05 +0800404void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800405{
Ying Xuef2f98002015-01-09 15:27:05 +0800406 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue46651c52014-03-27 12:54:36 +0800407 struct tipc_node *node, *t_node;
408
Ying Xuef2f98002015-01-09 15:27:05 +0800409 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400410 list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
411 if (del_timer(&node->timer))
412 tipc_node_put(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800413 tipc_node_put(node);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400414 }
Ying Xuef2f98002015-01-09 15:27:05 +0800415 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800416}
417
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500418void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
419{
420 struct tipc_node *n;
421
422 if (in_own_node(net, addr))
423 return;
424
425 n = tipc_node_find(net, addr);
426 if (!n) {
427 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
428 return;
429 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500430 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500431 list_add_tail(subscr, &n->publ_list);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500432 tipc_node_write_unlock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500433 tipc_node_put(n);
434}
435
436void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
437{
438 struct tipc_node *n;
439
440 if (in_own_node(net, addr))
441 return;
442
443 n = tipc_node_find(net, addr);
444 if (!n) {
445 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
446 return;
447 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500448 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500449 list_del_init(subscr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500450 tipc_node_write_unlock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500451 tipc_node_put(n);
452}
453
Ying Xuef2f98002015-01-09 15:27:05 +0800454int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400455{
456 struct tipc_node *node;
457 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800458 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400459
Ying Xue34747532015-01-09 15:27:10 +0800460 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400461 return 0;
462
Ying Xuef2f98002015-01-09 15:27:05 +0800463 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400464 if (!node) {
465 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
466 return -EHOSTUNREACH;
467 }
468 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800469 if (!conn) {
470 err = -EHOSTUNREACH;
471 goto exit;
472 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400473 conn->peer_node = dnode;
474 conn->port = port;
475 conn->peer_port = peer_port;
476
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500477 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400478 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500479 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800480exit:
481 tipc_node_put(node);
482 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400483}
484
Ying Xuef2f98002015-01-09 15:27:05 +0800485void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400486{
487 struct tipc_node *node;
488 struct tipc_sock_conn *conn, *safe;
489
Ying Xue34747532015-01-09 15:27:10 +0800490 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400491 return;
492
Ying Xuef2f98002015-01-09 15:27:05 +0800493 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400494 if (!node)
495 return;
496
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500497 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400498 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
499 if (port != conn->port)
500 continue;
501 list_del(&conn->list);
502 kfree(conn);
503 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500504 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800505 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400506}
507
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400508/* tipc_node_timeout - handle expiration of node timer
509 */
510static void tipc_node_timeout(unsigned long data)
511{
512 struct tipc_node *n = (struct tipc_node *)data;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400513 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400514 struct sk_buff_head xmitq;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400515 int bearer_id;
516 int rc = 0;
517
518 __skb_queue_head_init(&xmitq);
519
520 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500521 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400522 le = &n->links[bearer_id];
Jon Paul Maloy2312bf62015-11-19 14:30:43 -0500523 spin_lock_bh(&le->lock);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400524 if (le->link) {
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400525 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400526 tipc_node_calculate_timer(n, le->link);
527 rc = tipc_link_timeout(le->link, &xmitq);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400528 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -0500529 spin_unlock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500530 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400531 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
532 if (rc & TIPC_LINK_DOWN_EVT)
533 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400534 }
535 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
536 tipc_node_get(n);
537 tipc_node_put(n);
538}
539
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400541 * __tipc_node_link_up - handle addition of link
542 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * Link becomes active (alone or shared) or standby, depending on its priority.
544 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400545static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
546 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400548 int *slot0 = &n->active_links[0];
549 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400550 struct tipc_link *ol = node_active_link(n, 0);
551 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400553 if (!nl)
554 return;
555
556 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
557 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400558 return;
559
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400560 n->working_links++;
561 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400562 n->link_id = nl->peer_bearer_id << 16 | bearer_id;
563
564 /* Leave room for tunnel header when returning 'mtu' to users: */
565 n->links[bearer_id].mtu = nl->mtu - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800566
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400567 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400568 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400569
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100570 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400571 nl->name, nl->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400573 /* First link? => give it both slots */
574 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400575 *slot0 = bearer_id;
576 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400577 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
578 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400579 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400580 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400582
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400583 /* Second link => redistribute slots */
584 if (nl->priority > ol->priority) {
585 pr_debug("Old link <%s> becomes standby\n", ol->name);
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400586 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400587 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400588 tipc_link_set_active(nl, true);
589 tipc_link_set_active(ol, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400590 } else if (nl->priority == ol->priority) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400591 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a82015-10-22 08:51:47 -0400592 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400593 } else {
594 pr_debug("New link <%s> is standby\n", nl->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400597 /* Prepare synchronization with first link */
598 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599}
600
601/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400602 * tipc_node_link_up - handle addition of link
603 *
604 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400606static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
607 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500609 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400610 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500611 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400612}
613
614/**
615 * __tipc_node_link_down - handle loss of link
616 */
617static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
618 struct sk_buff_head *xmitq,
619 struct tipc_media_addr **maddr)
620{
621 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400622 int *slot0 = &n->active_links[0];
623 int *slot1 = &n->active_links[1];
624 int i, highest = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400625 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400627 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400628 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400629 return;
630
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400631 n->working_links--;
632 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400633 n->link_id = l->peer_bearer_id << 16 | *bearer_id;
Allan Stephens5392d642006-06-25 23:52:50 -0700634
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400635 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400636
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100637 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400638 l->name, l->net_plane);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400640 /* Select new active link if any available */
641 *slot0 = INVALID_BEARER_ID;
642 *slot1 = INVALID_BEARER_ID;
643 for (i = 0; i < MAX_BEARERS; i++) {
644 _l = n->links[i].link;
645 if (!_l || !tipc_link_is_up(_l))
646 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400647 if (_l == l)
648 continue;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400649 if (_l->priority < highest)
650 continue;
651 if (_l->priority > highest) {
652 highest = _l->priority;
653 *slot0 = i;
654 *slot1 = i;
655 continue;
656 }
657 *slot1 = i;
658 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400659
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400660 if (!tipc_node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400661 if (tipc_link_peer_is_down(l))
662 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
663 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
664 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400665 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400666 tipc_link_build_reset_msg(l, xmitq);
667 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400668 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400669 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400670 return;
671 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400672 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400673
674 /* There is still a working link => initiate failover */
675 tnl = node_active_link(n, 0);
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 Maloy6e498152015-07-30 18:24:19 -0400678 n->sync_point = tnl->rcv_nxt + (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);
684 *maddr = &n->links[tnl->bearer_id].maddr;
685 *bearer_id = tnl->bearer_id;
686}
687
688static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
689{
690 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400691 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400692 struct tipc_media_addr *maddr;
693 struct sk_buff_head xmitq;
694
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400695 if (!l)
696 return;
697
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400698 __skb_queue_head_init(&xmitq);
699
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500700 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400701 if (!tipc_link_is_establishing(l)) {
702 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
703 if (delete) {
704 kfree(l);
705 le->link = NULL;
706 n->link_cnt--;
707 }
708 } else {
709 /* Defuse pending tipc_node_link_up() */
710 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400711 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500712 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400713 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
714 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715}
716
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500717static bool tipc_node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400719 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720}
721
Jon Paul Maloycf148812015-07-30 18:24:22 -0400722void tipc_node_check_dest(struct net *net, u32 onode,
723 struct tipc_bearer *b,
724 u16 capabilities, u32 signature,
725 struct tipc_media_addr *maddr,
726 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400727{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400728 struct tipc_node *n;
729 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400730 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400731 bool addr_match = false;
732 bool sign_match = false;
733 bool link_up = false;
734 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400735 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400736 char *if_name;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400737
Jon Paul Maloycf148812015-07-30 18:24:22 -0400738 *dupl_addr = false;
739 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400740
Jon Paul Maloycf148812015-07-30 18:24:22 -0400741 n = tipc_node_create(net, onode, capabilities);
742 if (!n)
743 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400744
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500745 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400746
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400747 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400748
749 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400750 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400751 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400752 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400753 sign_match = (signature == n->signature);
754
755 /* These three flags give us eight permutations: */
756
757 if (sign_match && addr_match && link_up) {
758 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400759 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400760 } else if (sign_match && addr_match && !link_up) {
761 /* Respond. The link will come up in due time */
762 *respond = true;
763 } else if (sign_match && !addr_match && link_up) {
764 /* Peer has changed i/f address without rebooting.
765 * If so, the link will reset soon, and the next
766 * discovery will be accepted. So we can ignore it.
767 * It may also be an cloned or malicious peer having
768 * chosen the same node address and signature as an
769 * existing one.
770 * Ignore requests until the link goes down, if ever.
771 */
772 *dupl_addr = true;
773 } else if (sign_match && !addr_match && !link_up) {
774 /* Peer link has changed i/f address without rebooting.
775 * It may also be a cloned or malicious peer; we can't
776 * distinguish between the two.
777 * The signature is correct, so we must accept.
778 */
779 accept_addr = true;
780 *respond = true;
781 } else if (!sign_match && addr_match && link_up) {
782 /* Peer node rebooted. Two possibilities:
783 * - Delayed re-discovery; this link endpoint has already
784 * reset and re-established contact with the peer, before
785 * receiving a discovery message from that node.
786 * (The peer happened to receive one from this node first).
787 * - The peer came back so fast that our side has not
788 * discovered it yet. Probing from this side will soon
789 * reset the link, since there can be no working link
790 * endpoint at the peer end, and the link will re-establish.
791 * Accept the signature, since it comes from a known peer.
792 */
793 n->signature = signature;
794 } else if (!sign_match && addr_match && !link_up) {
795 /* The peer node has rebooted.
796 * Accept signature, since it is a known peer.
797 */
798 n->signature = signature;
799 *respond = true;
800 } else if (!sign_match && !addr_match && link_up) {
801 /* Peer rebooted with new address, or a new/duplicate peer.
802 * Ignore until the link goes down, if ever.
803 */
804 *dupl_addr = true;
805 } else if (!sign_match && !addr_match && !link_up) {
806 /* Peer rebooted with new address, or it is a new peer.
807 * Accept signature and address.
808 */
809 n->signature = signature;
810 accept_addr = true;
811 *respond = true;
812 }
813
814 if (!accept_addr)
815 goto exit;
816
817 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400818 if (!l) {
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400819 if (n->link_cnt == 2) {
820 pr_warn("Cannot establish 3rd link to %x\n", n->addr);
821 goto exit;
822 }
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400823 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400824 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400825 b->net_plane, b->mtu, b->priority,
826 b->window, mod(tipc_net(net)->random),
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400827 tipc_own_addr(net), onode,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400828 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400829 tipc_bc_sndlink(n->net), n->bc_entry.link,
830 &le->inputq,
831 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -0400832 *respond = false;
833 goto exit;
834 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400835 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400836 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -0400837 if (n->state == NODE_FAILINGOVER)
838 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400839 le->link = l;
840 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400841 tipc_node_calculate_timer(n, l);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400842 if (n->link_cnt == 1)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400843 if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
844 tipc_node_get(n);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400845 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400846 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400847exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500848 tipc_node_write_unlock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400849 if (reset && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400850 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400851 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400852}
853
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400854void tipc_node_delete_links(struct net *net, int bearer_id)
855{
856 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400857 struct tipc_node *n;
858
859 rcu_read_lock();
860 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400861 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400862 }
863 rcu_read_unlock();
864}
865
866static void tipc_node_reset_links(struct tipc_node *n)
867{
868 char addr_string[16];
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400869 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400870
871 pr_warn("Resetting all links to %s\n",
872 tipc_addr_string_fill(addr_string, n->addr));
873
874 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400875 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400876 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400877}
878
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400879/* tipc_node_fsm_evt - node finite state machine
880 * Determines when contact is allowed with peer node
881 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400882static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400883{
884 int state = n->state;
885
886 switch (state) {
887 case SELF_DOWN_PEER_DOWN:
888 switch (evt) {
889 case SELF_ESTABL_CONTACT_EVT:
890 state = SELF_UP_PEER_COMING;
891 break;
892 case PEER_ESTABL_CONTACT_EVT:
893 state = SELF_COMING_PEER_UP;
894 break;
895 case SELF_LOST_CONTACT_EVT:
896 case PEER_LOST_CONTACT_EVT:
897 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400898 case NODE_SYNCH_END_EVT:
899 case NODE_SYNCH_BEGIN_EVT:
900 case NODE_FAILOVER_BEGIN_EVT:
901 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400902 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400903 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400904 }
905 break;
906 case SELF_UP_PEER_UP:
907 switch (evt) {
908 case SELF_LOST_CONTACT_EVT:
909 state = SELF_DOWN_PEER_LEAVING;
910 break;
911 case PEER_LOST_CONTACT_EVT:
912 state = SELF_LEAVING_PEER_DOWN;
913 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400914 case NODE_SYNCH_BEGIN_EVT:
915 state = NODE_SYNCHING;
916 break;
917 case NODE_FAILOVER_BEGIN_EVT:
918 state = NODE_FAILINGOVER;
919 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400920 case SELF_ESTABL_CONTACT_EVT:
921 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400922 case NODE_SYNCH_END_EVT:
923 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400924 break;
925 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400926 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400927 }
928 break;
929 case SELF_DOWN_PEER_LEAVING:
930 switch (evt) {
931 case PEER_LOST_CONTACT_EVT:
932 state = SELF_DOWN_PEER_DOWN;
933 break;
934 case SELF_ESTABL_CONTACT_EVT:
935 case PEER_ESTABL_CONTACT_EVT:
936 case SELF_LOST_CONTACT_EVT:
937 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400938 case NODE_SYNCH_END_EVT:
939 case NODE_SYNCH_BEGIN_EVT:
940 case NODE_FAILOVER_BEGIN_EVT:
941 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400942 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400943 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400944 }
945 break;
946 case SELF_UP_PEER_COMING:
947 switch (evt) {
948 case PEER_ESTABL_CONTACT_EVT:
949 state = SELF_UP_PEER_UP;
950 break;
951 case SELF_LOST_CONTACT_EVT:
952 state = SELF_DOWN_PEER_LEAVING;
953 break;
954 case SELF_ESTABL_CONTACT_EVT:
955 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400956 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400957 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400958 break;
959 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400960 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400961 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400962 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400963 }
964 break;
965 case SELF_COMING_PEER_UP:
966 switch (evt) {
967 case SELF_ESTABL_CONTACT_EVT:
968 state = SELF_UP_PEER_UP;
969 break;
970 case PEER_LOST_CONTACT_EVT:
971 state = SELF_LEAVING_PEER_DOWN;
972 break;
973 case SELF_LOST_CONTACT_EVT:
974 case PEER_ESTABL_CONTACT_EVT:
975 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400976 case NODE_SYNCH_END_EVT:
977 case NODE_SYNCH_BEGIN_EVT:
978 case NODE_FAILOVER_BEGIN_EVT:
979 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400980 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400981 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400982 }
983 break;
984 case SELF_LEAVING_PEER_DOWN:
985 switch (evt) {
986 case SELF_LOST_CONTACT_EVT:
987 state = SELF_DOWN_PEER_DOWN;
988 break;
989 case SELF_ESTABL_CONTACT_EVT:
990 case PEER_ESTABL_CONTACT_EVT:
991 case PEER_LOST_CONTACT_EVT:
992 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400993 case NODE_SYNCH_END_EVT:
994 case NODE_SYNCH_BEGIN_EVT:
995 case NODE_FAILOVER_BEGIN_EVT:
996 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400997 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -0400998 goto illegal_evt;
999 }
1000 break;
1001 case NODE_FAILINGOVER:
1002 switch (evt) {
1003 case SELF_LOST_CONTACT_EVT:
1004 state = SELF_DOWN_PEER_LEAVING;
1005 break;
1006 case PEER_LOST_CONTACT_EVT:
1007 state = SELF_LEAVING_PEER_DOWN;
1008 break;
1009 case NODE_FAILOVER_END_EVT:
1010 state = SELF_UP_PEER_UP;
1011 break;
1012 case NODE_FAILOVER_BEGIN_EVT:
1013 case SELF_ESTABL_CONTACT_EVT:
1014 case PEER_ESTABL_CONTACT_EVT:
1015 break;
1016 case NODE_SYNCH_BEGIN_EVT:
1017 case NODE_SYNCH_END_EVT:
1018 default:
1019 goto illegal_evt;
1020 }
1021 break;
1022 case NODE_SYNCHING:
1023 switch (evt) {
1024 case SELF_LOST_CONTACT_EVT:
1025 state = SELF_DOWN_PEER_LEAVING;
1026 break;
1027 case PEER_LOST_CONTACT_EVT:
1028 state = SELF_LEAVING_PEER_DOWN;
1029 break;
1030 case NODE_SYNCH_END_EVT:
1031 state = SELF_UP_PEER_UP;
1032 break;
1033 case NODE_FAILOVER_BEGIN_EVT:
1034 state = NODE_FAILINGOVER;
1035 break;
1036 case NODE_SYNCH_BEGIN_EVT:
1037 case SELF_ESTABL_CONTACT_EVT:
1038 case PEER_ESTABL_CONTACT_EVT:
1039 break;
1040 case NODE_FAILOVER_END_EVT:
1041 default:
1042 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001043 }
1044 break;
1045 default:
1046 pr_err("Unknown node fsm state %x\n", state);
1047 break;
1048 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001049 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001050 return;
1051
1052illegal_evt:
1053 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001054}
1055
Jon Paul Maloy52666982015-10-22 08:51:41 -04001056static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001057 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 char addr_string[16];
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001060 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001061 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001062 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001063 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001064 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065
Erik Hugne3fa9cac2015-01-22 17:10:31 +01001066 pr_debug("Lost contact with %s\n",
Jon Paul Maloy52666982015-10-22 08:51:41 -04001067 tipc_addr_string_fill(addr_string, n->addr));
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001068
Jon Paul Maloy52666982015-10-22 08:51:41 -04001069 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001070 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001072 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001074 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001075 if (l)
1076 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001078
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001079 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001080 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001081
1082 /* Notify sockets connected to node */
1083 list_for_each_entry_safe(conn, safe, conns, list) {
1084 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001085 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001086 conn->peer_node, conn->port,
1087 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001088 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001089 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001090 list_del(&conn->list);
1091 kfree(conn);
1092 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093}
1094
Erik Hugne78acb1f2014-04-24 16:26:47 +02001095/**
1096 * tipc_node_get_linkname - get the name of a link
1097 *
1098 * @bearer_id: id of the bearer
1099 * @node: peer node address
1100 * @linkname: link name output buffer
1101 *
1102 * Returns 0 on success
1103 */
Ying Xuef2f98002015-01-09 15:27:05 +08001104int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1105 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001106{
1107 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001108 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001109 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001110
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001111 if (!node)
1112 return err;
1113
1114 if (bearer_id >= MAX_BEARERS)
1115 goto exit;
1116
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001117 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001118 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001119 if (link) {
1120 strncpy(linkname, link->name, len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001121 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001122 }
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001123exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001124 tipc_node_read_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001125 tipc_node_put(node);
1126 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001127}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001128
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001129/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001130static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001131{
1132 void *hdr;
1133 struct nlattr *attrs;
1134
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001135 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001136 NLM_F_MULTI, TIPC_NL_NODE_GET);
1137 if (!hdr)
1138 return -EMSGSIZE;
1139
1140 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1141 if (!attrs)
1142 goto msg_full;
1143
1144 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1145 goto attr_msg_full;
1146 if (tipc_node_is_up(node))
1147 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1148 goto attr_msg_full;
1149
1150 nla_nest_end(msg->skb, attrs);
1151 genlmsg_end(msg->skb, hdr);
1152
1153 return 0;
1154
1155attr_msg_full:
1156 nla_nest_cancel(msg->skb, attrs);
1157msg_full:
1158 genlmsg_cancel(msg->skb, hdr);
1159
1160 return -EMSGSIZE;
1161}
1162
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001163/**
1164 * tipc_node_xmit() is the general link level function for message sending
1165 * @net: the applicable net namespace
1166 * @list: chain of buffers containing message
1167 * @dnode: address of destination node
1168 * @selector: a number used for deterministic link selection
1169 * Consumes the buffer chain, except when returning -ELINKCONG
1170 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
1171 */
1172int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1173 u32 dnode, int selector)
1174{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001175 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001176 struct tipc_node *n;
1177 struct sk_buff_head xmitq;
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001178 int bearer_id = -1;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001179 int rc = -EHOSTUNREACH;
1180
1181 __skb_queue_head_init(&xmitq);
1182 n = tipc_node_find(net, dnode);
1183 if (likely(n)) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001184 tipc_node_read_lock(n);
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001185 bearer_id = n->active_links[selector & 1];
1186 if (bearer_id >= 0) {
1187 le = &n->links[bearer_id];
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001188 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001189 rc = tipc_link_xmit(le->link, list, &xmitq);
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001190 spin_unlock_bh(&le->lock);
1191 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001192 tipc_node_read_unlock(n);
1193 if (likely(!skb_queue_empty(&xmitq))) {
1194 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1195 return 0;
1196 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001197 if (unlikely(rc == -ENOBUFS))
1198 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001199 tipc_node_put(n);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001200 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001201 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001202
1203 if (unlikely(!in_own_node(net, dnode)))
1204 return rc;
1205 tipc_sk_rcv(net, list);
1206 return 0;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001207}
1208
1209/* tipc_node_xmit_skb(): send single buffer to destination
1210 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1211 * messages, which will not be rejected
1212 * The only exception is datagram messages rerouted after secondary
1213 * lookup, which are rare and safe to dispose of anyway.
1214 * TODO: Return real return value, and let callers use
1215 * tipc_wait_for_sendpkt() where applicable
1216 */
1217int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1218 u32 selector)
1219{
1220 struct sk_buff_head head;
1221 int rc;
1222
1223 skb_queue_head_init(&head);
1224 __skb_queue_tail(&head, skb);
1225 rc = tipc_node_xmit(net, &head, dnode, selector);
1226 if (rc == -ELINKCONG)
1227 kfree_skb(skb);
1228 return 0;
1229}
1230
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001231void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1232{
1233 struct sk_buff *txskb;
1234 struct tipc_node *n;
1235 u32 dst;
1236
1237 rcu_read_lock();
1238 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1239 dst = n->addr;
1240 if (in_own_node(net, dst))
1241 continue;
1242 if (!tipc_node_is_up(n))
1243 continue;
1244 txskb = pskb_copy(skb, GFP_ATOMIC);
1245 if (!txskb)
1246 break;
1247 msg_set_destnode(buf_msg(txskb), dst);
1248 tipc_node_xmit_skb(net, txskb, dst, 0);
1249 }
1250 rcu_read_unlock();
1251
1252 kfree_skb(skb);
1253}
1254
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001255/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001256 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1257 * @net: the applicable net namespace
1258 * @skb: TIPC packet
1259 * @bearer_id: id of bearer message arrived on
1260 *
1261 * Invoked with no locks held.
1262 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001263static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001264{
1265 int rc;
1266 struct sk_buff_head xmitq;
1267 struct tipc_bclink_entry *be;
1268 struct tipc_link_entry *le;
1269 struct tipc_msg *hdr = buf_msg(skb);
1270 int usr = msg_user(hdr);
1271 u32 dnode = msg_destnode(hdr);
1272 struct tipc_node *n;
1273
1274 __skb_queue_head_init(&xmitq);
1275
1276 /* If NACK for other node, let rcv link for that node peek into it */
1277 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1278 n = tipc_node_find(net, dnode);
1279 else
1280 n = tipc_node_find(net, msg_prevnode(hdr));
1281 if (!n) {
1282 kfree_skb(skb);
1283 return;
1284 }
1285 be = &n->bc_entry;
1286 le = &n->links[bearer_id];
1287
1288 rc = tipc_bcast_rcv(net, be->link, skb);
1289
1290 /* Broadcast link reset may happen at reassembly failure */
1291 if (rc & TIPC_LINK_DOWN_EVT)
1292 tipc_node_reset_links(n);
1293
1294 /* Broadcast ACKs are sent on a unicast link */
1295 if (rc & TIPC_LINK_SND_BC_ACK) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001296 tipc_node_read_lock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001297 tipc_link_build_ack_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001298 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001299 }
1300
1301 if (!skb_queue_empty(&xmitq))
1302 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1303
1304 /* Deliver. 'arrvq' is under inputq2's lock protection */
1305 if (!skb_queue_empty(&be->inputq1)) {
1306 spin_lock_bh(&be->inputq2.lock);
1307 spin_lock_bh(&be->inputq1.lock);
1308 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1309 spin_unlock_bh(&be->inputq1.lock);
1310 spin_unlock_bh(&be->inputq2.lock);
1311 tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
1312 }
1313 tipc_node_put(n);
1314}
1315
1316/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001317 * tipc_node_check_state - check and if necessary update node state
1318 * @skb: TIPC packet
1319 * @bearer_id: identity of bearer delivering the packet
1320 * Returns true if state is ok, otherwise consumes buffer and returns false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001321 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001322static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001323 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001324{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001325 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001326 int usr = msg_user(hdr);
1327 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001328 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001329 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1330 u16 exp_pkts = msg_msgcnt(hdr);
1331 u16 rcv_nxt, syncpt, dlv_nxt;
1332 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001333 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001334 struct tipc_media_addr *maddr;
1335 int i, pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001336
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001337 l = n->links[bearer_id].link;
1338 if (!l)
1339 return false;
1340 rcv_nxt = l->rcv_nxt;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001341
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001342
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001343 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1344 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001345
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001346 /* Find parallel link, if any */
1347 for (i = 0; i < MAX_BEARERS; i++) {
1348 if ((i != bearer_id) && n->links[i].link) {
1349 pl = n->links[i].link;
1350 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001351 }
1352 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001353
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001354 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001355 if (state == SELF_UP_PEER_COMING) {
1356 if (!tipc_link_is_up(l))
1357 return true;
1358 if (!msg_peer_link_is_up(hdr))
1359 return true;
1360 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1361 }
1362
1363 if (state == SELF_DOWN_PEER_LEAVING) {
1364 if (msg_peer_node_is_up(hdr))
1365 return false;
1366 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001367 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001368 }
1369
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001370 if (state == SELF_LEAVING_PEER_DOWN)
1371 return false;
1372
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001373 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001374 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001375 return true;
1376
1377 /* Initiate or update failover mode if applicable */
1378 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1379 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001380 if (pl && tipc_link_is_up(pl)) {
1381 pb_id = pl->bearer_id;
1382 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1383 tipc_skb_queue_splice_tail_init(pl->inputq, l->inputq);
1384 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001385 /* If pkts arrive out of order, use lowest calculated syncpt */
1386 if (less(syncpt, n->sync_point))
1387 n->sync_point = syncpt;
1388 }
1389
1390 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001391 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001392 if (!more(rcv_nxt, n->sync_point))
1393 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001394 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1395 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001396 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001397 return true;
1398 }
1399
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001400 /* No synching needed if only one link */
1401 if (!pl || !tipc_link_is_up(pl))
1402 return true;
1403
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001404 /* Initiate synch mode if applicable */
1405 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001406 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001407 if (!tipc_link_is_up(l)) {
1408 tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001409 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001410 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001411 if (n->state == SELF_UP_PEER_UP) {
1412 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001413 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001414 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1415 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001416 }
1417
1418 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001419 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001420 if (tipc_link_is_synching(l)) {
1421 tnl = l;
1422 } else {
1423 tnl = pl;
1424 pl = l;
1425 }
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001426 dlv_nxt = pl->rcv_nxt - mod(skb_queue_len(pl->inputq));
1427 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);
1486 else if (unlikely(n->bc_entry.link->acked != bc_ack))
1487 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
1595/* tipc_link_find_owner - locate owner node of link by link's name
1596 * @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 */
1602static struct tipc_node *tipc_link_find_owner(struct net *net,
1603 const char *link_name,
1604 unsigned int *bearer_id)
1605{
1606 struct tipc_net *tn = net_generic(net, tipc_net_id);
1607 struct tipc_link *l_ptr;
1608 struct tipc_node *n_ptr;
1609 struct tipc_node *found_node = NULL;
1610 int i;
1611
1612 *bearer_id = 0;
1613 rcu_read_lock();
1614 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
1615 tipc_node_read_lock(n_ptr);
1616 for (i = 0; i < MAX_BEARERS; i++) {
1617 l_ptr = n_ptr->links[i].link;
1618 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1619 *bearer_id = i;
1620 found_node = n_ptr;
1621 break;
1622 }
1623 }
1624 tipc_node_read_unlock(n_ptr);
1625 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
1661 node = tipc_link_find_owner(net, name, &bearer_id);
1662 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]);
1687 link->tolerance = tol;
1688 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
1689 }
1690 if (props[TIPC_NLA_PROP_PRIO]) {
1691 u32 prio;
1692
1693 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1694 link->priority = prio;
1695 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
1696 }
1697 if (props[TIPC_NLA_PROP_WIN]) {
1698 u32 win;
1699
1700 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1701 tipc_link_set_queue_limits(link, win);
1702 }
1703 }
1704
1705out:
1706 tipc_node_read_unlock(node);
1707
1708 return res;
1709}
1710
1711int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
1712{
1713 struct net *net = genl_info_net(info);
1714 struct tipc_nl_msg msg;
1715 char *name;
1716 int err;
1717
1718 msg.portid = info->snd_portid;
1719 msg.seq = info->snd_seq;
1720
1721 if (!info->attrs[TIPC_NLA_LINK_NAME])
1722 return -EINVAL;
1723 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
1724
1725 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1726 if (!msg.skb)
1727 return -ENOMEM;
1728
1729 if (strcmp(name, tipc_bclink_name) == 0) {
1730 err = tipc_nl_add_bc_link(net, &msg);
1731 if (err) {
1732 nlmsg_free(msg.skb);
1733 return err;
1734 }
1735 } else {
1736 int bearer_id;
1737 struct tipc_node *node;
1738 struct tipc_link *link;
1739
1740 node = tipc_link_find_owner(net, name, &bearer_id);
1741 if (!node)
1742 return -EINVAL;
1743
1744 tipc_node_read_lock(node);
1745 link = node->links[bearer_id].link;
1746 if (!link) {
1747 tipc_node_read_unlock(node);
1748 nlmsg_free(msg.skb);
1749 return -EINVAL;
1750 }
1751
1752 err = __tipc_nl_add_link(net, &msg, link, 0);
1753 tipc_node_read_unlock(node);
1754 if (err) {
1755 nlmsg_free(msg.skb);
1756 return err;
1757 }
1758 }
1759
1760 return genlmsg_reply(msg.skb, info);
1761}
1762
1763int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
1764{
1765 int err;
1766 char *link_name;
1767 unsigned int bearer_id;
1768 struct tipc_link *link;
1769 struct tipc_node *node;
1770 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1771 struct net *net = sock_net(skb->sk);
1772 struct tipc_link_entry *le;
1773
1774 if (!info->attrs[TIPC_NLA_LINK])
1775 return -EINVAL;
1776
1777 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1778 info->attrs[TIPC_NLA_LINK],
1779 tipc_nl_link_policy);
1780 if (err)
1781 return err;
1782
1783 if (!attrs[TIPC_NLA_LINK_NAME])
1784 return -EINVAL;
1785
1786 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1787
1788 if (strcmp(link_name, tipc_bclink_name) == 0) {
1789 err = tipc_bclink_reset_stats(net);
1790 if (err)
1791 return err;
1792 return 0;
1793 }
1794
1795 node = tipc_link_find_owner(net, link_name, &bearer_id);
1796 if (!node)
1797 return -EINVAL;
1798
1799 le = &node->links[bearer_id];
1800 tipc_node_read_lock(node);
1801 spin_lock_bh(&le->lock);
1802 link = node->links[bearer_id].link;
1803 if (!link) {
1804 spin_unlock_bh(&le->lock);
1805 tipc_node_read_unlock(node);
1806 return -EINVAL;
1807 }
1808 link_reset_statistics(link);
1809 spin_unlock_bh(&le->lock);
1810 tipc_node_read_unlock(node);
1811 return 0;
1812}
1813
1814/* Caller should hold node lock */
1815static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
1816 struct tipc_node *node, u32 *prev_link)
1817{
1818 u32 i;
1819 int err;
1820
1821 for (i = *prev_link; i < MAX_BEARERS; i++) {
1822 *prev_link = i;
1823
1824 if (!node->links[i].link)
1825 continue;
1826
1827 err = __tipc_nl_add_link(net, msg,
1828 node->links[i].link, NLM_F_MULTI);
1829 if (err)
1830 return err;
1831 }
1832 *prev_link = 0;
1833
1834 return 0;
1835}
1836
1837int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
1838{
1839 struct net *net = sock_net(skb->sk);
1840 struct tipc_net *tn = net_generic(net, tipc_net_id);
1841 struct tipc_node *node;
1842 struct tipc_nl_msg msg;
1843 u32 prev_node = cb->args[0];
1844 u32 prev_link = cb->args[1];
1845 int done = cb->args[2];
1846 int err;
1847
1848 if (done)
1849 return 0;
1850
1851 msg.skb = skb;
1852 msg.portid = NETLINK_CB(cb->skb).portid;
1853 msg.seq = cb->nlh->nlmsg_seq;
1854
1855 rcu_read_lock();
1856 if (prev_node) {
1857 node = tipc_node_find(net, prev_node);
1858 if (!node) {
1859 /* We never set seq or call nl_dump_check_consistent()
1860 * this means that setting prev_seq here will cause the
1861 * consistence check to fail in the netlink callback
1862 * handler. Resulting in the last NLMSG_DONE message
1863 * having the NLM_F_DUMP_INTR flag set.
1864 */
1865 cb->prev_seq = 1;
1866 goto out;
1867 }
1868 tipc_node_put(node);
1869
1870 list_for_each_entry_continue_rcu(node, &tn->node_list,
1871 list) {
1872 tipc_node_read_lock(node);
1873 err = __tipc_nl_add_node_links(net, &msg, node,
1874 &prev_link);
1875 tipc_node_read_unlock(node);
1876 if (err)
1877 goto out;
1878
1879 prev_node = node->addr;
1880 }
1881 } else {
1882 err = tipc_nl_add_bc_link(net, &msg);
1883 if (err)
1884 goto out;
1885
1886 list_for_each_entry_rcu(node, &tn->node_list, list) {
1887 tipc_node_read_lock(node);
1888 err = __tipc_nl_add_node_links(net, &msg, node,
1889 &prev_link);
1890 tipc_node_read_unlock(node);
1891 if (err)
1892 goto out;
1893
1894 prev_node = node->addr;
1895 }
1896 }
1897 done = 1;
1898out:
1899 rcu_read_unlock();
1900
1901 cb->args[0] = prev_node;
1902 cb->args[1] = prev_link;
1903 cb->args[2] = done;
1904
1905 return skb->len;
1906}