blob: 8972ca1c654cbb99160387a1d6d97a96ff81f786 [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 Maloy60020e12016-05-02 11:58:46 -04004 * Copyright (c) 2000-2006, 2012-2016, 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 Maloy35c55c92016-06-13 20:46:22 -040043#include "monitor.h"
Jon Paul Maloyd9992972015-07-16 16:54:31 -040044#include "discover.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Jon Paul Maloyc8199302015-10-15 14:52:46 -040046
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050047#define INVALID_NODE_SIG 0x10000
48
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050049/* Flags used to take different actions according to flag type
50 * TIPC_NOTIFY_NODE_DOWN: notify node is down
51 * TIPC_NOTIFY_NODE_UP: notify node is up
52 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
53 */
54enum {
55 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
56 TIPC_NOTIFY_NODE_UP = (1 << 4),
57 TIPC_NOTIFY_LINK_UP = (1 << 6),
58 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
59};
60
61struct tipc_link_entry {
62 struct tipc_link *link;
63 spinlock_t lock; /* per link */
64 u32 mtu;
65 struct sk_buff_head inputq;
66 struct tipc_media_addr maddr;
67};
68
69struct tipc_bclink_entry {
70 struct tipc_link *link;
71 struct sk_buff_head inputq1;
72 struct sk_buff_head arrvq;
73 struct sk_buff_head inputq2;
74 struct sk_buff_head namedq;
75};
76
77/**
78 * struct tipc_node - TIPC node structure
79 * @addr: network address of node
80 * @ref: reference counter to node object
81 * @lock: rwlock governing access to structure
82 * @net: the applicable net namespace
83 * @hash: links to adjacent nodes in unsorted hash chain
84 * @inputq: pointer to input queue containing messages for msg event
85 * @namedq: pointer to name table input queue with name table messages
86 * @active_links: bearer ids of active links, used as index into links[] array
87 * @links: array containing references to all links to node
88 * @action_flags: bit mask of different types of node actions
89 * @state: connectivity state vs peer node
90 * @sync_point: sequence number where synch/failover is finished
91 * @list: links to adjacent nodes in sorted list of cluster's nodes
92 * @working_links: number of working links to node (both active and standby)
93 * @link_cnt: number of links to node
94 * @capabilities: bitmap, indicating peer node's functional capabilities
95 * @signature: node instance identifier
96 * @link_id: local and remote bearer ids of changing link, if any
97 * @publ_list: list of publications
98 * @rcu: rcu struct for tipc_node
99 */
100struct tipc_node {
101 u32 addr;
102 struct kref kref;
103 rwlock_t lock;
104 struct net *net;
105 struct hlist_node hash;
106 int active_links[2];
107 struct tipc_link_entry links[MAX_BEARERS];
108 struct tipc_bclink_entry bc_entry;
109 int action_flags;
110 struct list_head list;
111 int state;
112 u16 sync_point;
113 int link_cnt;
114 u16 working_links;
115 u16 capabilities;
116 u32 signature;
117 u32 link_id;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100118 u8 peer_id[16];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500119 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);
Kees Cook31b102b2017-10-30 14:06:45 -0700157static void tipc_node_timeout(struct timer_list *t);
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);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100160static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500161static void tipc_node_put(struct tipc_node *node);
Jon Maloy38077b82017-10-13 11:04:19 +0200162static bool node_is_up(struct tipc_node *n);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400164struct tipc_sock_conn {
165 u32 port;
166 u32 peer_port;
167 u32 peer_node;
168 struct list_head list;
169};
170
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500171static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
172{
173 int bearer_id = n->active_links[sel & 1];
174
175 if (unlikely(bearer_id == INVALID_BEARER_ID))
176 return NULL;
177
178 return n->links[bearer_id].link;
179}
180
181int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
182{
183 struct tipc_node *n;
184 int bearer_id;
185 unsigned int mtu = MAX_MSG_SIZE;
186
187 n = tipc_node_find(net, addr);
188 if (unlikely(!n))
189 return mtu;
190
191 bearer_id = n->active_links[sel & 1];
192 if (likely(bearer_id != INVALID_BEARER_ID))
193 mtu = n->links[bearer_id].mtu;
194 tipc_node_put(n);
195 return mtu;
196}
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400197
Jon Maloy3e5cf362018-04-25 19:29:36 +0200198bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
199{
200 u8 *own_id = tipc_own_id(net);
201 struct tipc_node *n;
202
203 if (!own_id)
204 return true;
205
206 if (addr == tipc_own_addr(net)) {
207 memcpy(id, own_id, TIPC_NODEID_LEN);
208 return true;
209 }
210 n = tipc_node_find(net, addr);
211 if (!n)
212 return false;
213
214 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
215 tipc_node_put(n);
216 return true;
217}
218
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400219u16 tipc_node_get_capabilities(struct net *net, u32 addr)
220{
221 struct tipc_node *n;
222 u16 caps;
223
224 n = tipc_node_find(net, addr);
225 if (unlikely(!n))
226 return TIPC_NODE_CAPABILITIES;
227 caps = n->capabilities;
228 tipc_node_put(n);
229 return caps;
230}
231
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800232static void tipc_node_kref_release(struct kref *kref)
233{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500234 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800235
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500236 kfree(n->bc_entry.link);
237 kfree_rcu(n, rcu);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800238}
239
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500240static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800241{
242 kref_put(&node->kref, tipc_node_kref_release);
243}
244
245static void tipc_node_get(struct tipc_node *node)
246{
247 kref_get(&node->kref);
248}
249
Allan Stephensa635b462011-11-04 11:54:43 -0400250/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500251 * tipc_node_find - locate specified node object, if it exists
252 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500253static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500254{
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500255 struct tipc_net *tn = tipc_net(net);
Allan Stephens672d99e2011-02-25 18:42:52 -0500256 struct tipc_node *node;
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500257 unsigned int thash = tipc_hashfn(addr);
Allan Stephens672d99e2011-02-25 18:42:52 -0500258
Ying Xue6c7a7622014-03-27 12:54:37 +0800259 rcu_read_lock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500260 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
261 if (node->addr != addr)
262 continue;
263 if (!kref_get_unless_zero(&node->kref))
264 node = NULL;
265 break;
Allan Stephens672d99e2011-02-25 18:42:52 -0500266 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800267 rcu_read_unlock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500268 return node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500269}
270
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100271/* tipc_node_find_by_id - locate specified node object by its 128-bit id
272 * Note: this function is called only when a discovery request failed
273 * to find the node by its 32-bit id, and is not time critical
274 */
275static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
276{
277 struct tipc_net *tn = tipc_net(net);
278 struct tipc_node *n;
279 bool found = false;
280
281 rcu_read_lock();
282 list_for_each_entry_rcu(n, &tn->node_list, list) {
283 read_lock_bh(&n->lock);
284 if (!memcmp(id, n->peer_id, 16) &&
285 kref_get_unless_zero(&n->kref))
286 found = true;
287 read_unlock_bh(&n->lock);
288 if (found)
289 break;
290 }
291 rcu_read_unlock();
292 return found ? n : NULL;
293}
294
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500295static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500296{
297 read_lock_bh(&n->lock);
298}
299
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500300static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500301{
302 read_unlock_bh(&n->lock);
303}
304
305static void tipc_node_write_lock(struct tipc_node *n)
306{
307 write_lock_bh(&n->lock);
308}
309
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100310static void tipc_node_write_unlock_fast(struct tipc_node *n)
311{
312 write_unlock_bh(&n->lock);
313}
314
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500315static void tipc_node_write_unlock(struct tipc_node *n)
316{
317 struct net *net = n->net;
318 u32 addr = 0;
319 u32 flags = n->action_flags;
320 u32 link_id = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400321 u32 bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500322 struct list_head *publ_list;
323
324 if (likely(!flags)) {
325 write_unlock_bh(&n->lock);
326 return;
327 }
328
329 addr = n->addr;
330 link_id = n->link_id;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400331 bearer_id = link_id & 0xffff;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500332 publ_list = &n->publ_list;
333
334 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
335 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
336
337 write_unlock_bh(&n->lock);
338
339 if (flags & TIPC_NOTIFY_NODE_DOWN)
340 tipc_publ_notify(net, publ_list, addr);
341
342 if (flags & TIPC_NOTIFY_NODE_UP)
343 tipc_named_node_up(net, addr);
344
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400345 if (flags & TIPC_NOTIFY_LINK_UP) {
346 tipc_mon_peer_up(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500347 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
Jon Maloy218527f2018-03-29 23:20:41 +0200348 TIPC_NODE_SCOPE, link_id, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400349 }
350 if (flags & TIPC_NOTIFY_LINK_DOWN) {
351 tipc_mon_peer_down(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500352 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
Jon Maloy37922ea2018-03-29 23:20:43 +0200353 addr, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400354 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500355}
356
Wei Yongjune1a22d12018-03-26 14:33:13 +0000357static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
358 u8 *peer_id, u16 capabilities)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359{
Ying Xuef2f98002015-01-09 15:27:05 +0800360 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500361 struct tipc_node *n, *temp_node;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500362 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
Ying Xuef2f98002015-01-09 15:27:05 +0800364 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500365 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400366 if (n) {
367 /* Same node may come back with new capabilities */
368 n->capabilities = capabilities;
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500369 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400370 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500371 n = kzalloc(sizeof(*n), GFP_ATOMIC);
372 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400373 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500374 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700375 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500376 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100377 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500378 n->net = net;
379 n->capabilities = capabilities;
380 kref_init(&n->kref);
381 rwlock_init(&n->lock);
382 INIT_HLIST_NODE(&n->hash);
383 INIT_LIST_HEAD(&n->list);
384 INIT_LIST_HEAD(&n->publ_list);
385 INIT_LIST_HEAD(&n->conn_sks);
386 skb_queue_head_init(&n->bc_entry.namedq);
387 skb_queue_head_init(&n->bc_entry.inputq1);
388 __skb_queue_head_init(&n->bc_entry.arrvq);
389 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500390 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500391 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500392 n->state = SELF_DOWN_PEER_LEAVING;
393 n->signature = INVALID_NODE_SIG;
394 n->active_links[0] = INVALID_BEARER_ID;
395 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100396 if (!tipc_link_bc_create(net, tipc_own_addr(net),
397 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500398 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500399 n->capabilities,
400 &n->bc_entry.inputq1,
401 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400402 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500403 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400404 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500405 kfree(n);
406 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400407 goto exit;
408 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500409 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700410 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500411 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500412 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
413 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
414 if (n->addr < temp_node->addr)
415 break;
416 }
417 list_add_tail_rcu(&n->list, &temp_node->list);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500418exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800419 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500420 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421}
422
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400423static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
424{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500425 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400426 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400427
428 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400429 if (intv < n->keepalive_intv)
430 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400431
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400432 /* Ensure link's abort limit corresponds to current tolerance */
433 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400434}
435
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800436static void tipc_node_delete(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800438 list_del_rcu(&node->list);
439 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500440 tipc_node_put(node);
441
442 del_timer_sync(&node->timer);
443 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444}
445
Ying Xuef2f98002015-01-09 15:27:05 +0800446void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800447{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500448 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800449 struct tipc_node *node, *t_node;
450
Ying Xuef2f98002015-01-09 15:27:05 +0800451 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500452 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
453 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800454 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800455}
456
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500457void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
458{
459 struct tipc_node *n;
460
461 if (in_own_node(net, addr))
462 return;
463
464 n = tipc_node_find(net, addr);
465 if (!n) {
466 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
467 return;
468 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500469 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500470 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100471 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500472 tipc_node_put(n);
473}
474
475void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
476{
477 struct tipc_node *n;
478
479 if (in_own_node(net, addr))
480 return;
481
482 n = tipc_node_find(net, addr);
483 if (!n) {
484 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
485 return;
486 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500487 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500488 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100489 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500490 tipc_node_put(n);
491}
492
Ying Xuef2f98002015-01-09 15:27:05 +0800493int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400494{
495 struct tipc_node *node;
496 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800497 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400498
Ying Xue34747532015-01-09 15:27:10 +0800499 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400500 return 0;
501
Ying Xuef2f98002015-01-09 15:27:05 +0800502 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400503 if (!node) {
504 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
505 return -EHOSTUNREACH;
506 }
507 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800508 if (!conn) {
509 err = -EHOSTUNREACH;
510 goto exit;
511 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400512 conn->peer_node = dnode;
513 conn->port = port;
514 conn->peer_port = peer_port;
515
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500516 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400517 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500518 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800519exit:
520 tipc_node_put(node);
521 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400522}
523
Ying Xuef2f98002015-01-09 15:27:05 +0800524void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400525{
526 struct tipc_node *node;
527 struct tipc_sock_conn *conn, *safe;
528
Ying Xue34747532015-01-09 15:27:10 +0800529 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400530 return;
531
Ying Xuef2f98002015-01-09 15:27:05 +0800532 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400533 if (!node)
534 return;
535
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500536 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400537 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
538 if (port != conn->port)
539 continue;
540 list_del(&conn->list);
541 kfree(conn);
542 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500543 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800544 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400545}
546
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400547/* tipc_node_timeout - handle expiration of node timer
548 */
Kees Cook31b102b2017-10-30 14:06:45 -0700549static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400550{
Kees Cook31b102b2017-10-30 14:06:45 -0700551 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400552 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400553 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200554 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400555 int bearer_id;
556 int rc = 0;
557
558 __skb_queue_head_init(&xmitq);
559
Tung Nguyen759f29b2018-06-28 22:39:25 +0200560 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500561 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400562 le = &n->links[bearer_id];
563 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200564 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400565 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400566 tipc_node_calculate_timer(n, le->link);
567 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200568 spin_unlock_bh(&le->lock);
569 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400570 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500571 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400572 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
573 if (rc & TIPC_LINK_DOWN_EVT)
574 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400575 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400576 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400577}
578
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400580 * __tipc_node_link_up - handle addition of link
581 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Link becomes active (alone or shared) or standby, depending on its priority.
583 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400584static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
585 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400587 int *slot0 = &n->active_links[0];
588 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400589 struct tipc_link *ol = node_active_link(n, 0);
590 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400592 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400593 return;
594
595 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
596 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400597 return;
598
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400599 n->working_links++;
600 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500601 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400602
603 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500604 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800605
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400606 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400607 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400608
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100609 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500610 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400612 /* Ensure that a STATE message goes first */
613 tipc_link_build_state_msg(nl, xmitq);
614
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400615 /* First link? => give it both slots */
616 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400617 *slot0 = bearer_id;
618 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400619 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
620 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400621 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400622 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400623 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400625
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400626 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500627 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
628 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400629 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400630 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400631 tipc_link_set_active(nl, true);
632 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500633 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400634 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a82015-10-22 08:51:47 -0400635 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400636 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500637 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400640 /* Prepare synchronization with first link */
641 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642}
643
644/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400645 * tipc_node_link_up - handle addition of link
646 *
647 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400649static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
650 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400652 struct tipc_media_addr *maddr;
653
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500654 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400655 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400656 maddr = &n->links[bearer_id].maddr;
657 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500658 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400659}
660
661/**
662 * __tipc_node_link_down - handle loss of link
663 */
664static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
665 struct sk_buff_head *xmitq,
666 struct tipc_media_addr **maddr)
667{
668 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400669 int *slot0 = &n->active_links[0];
670 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500671 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400672 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400674 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400675 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400676 return;
677
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400678 n->working_links--;
679 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500680 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700681
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400682 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400683
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100684 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500685 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400687 /* Select new active link if any available */
688 *slot0 = INVALID_BEARER_ID;
689 *slot1 = INVALID_BEARER_ID;
690 for (i = 0; i < MAX_BEARERS; i++) {
691 _l = n->links[i].link;
692 if (!_l || !tipc_link_is_up(_l))
693 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400694 if (_l == l)
695 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500696 prio = tipc_link_prio(_l);
697 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400698 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500699 if (prio > highest) {
700 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400701 *slot0 = i;
702 *slot1 = i;
703 continue;
704 }
705 *slot1 = i;
706 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400707
Jon Maloy38077b82017-10-13 11:04:19 +0200708 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400709 if (tipc_link_peer_is_down(l))
710 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
711 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
712 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400713 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400714 tipc_link_build_reset_msg(l, xmitq);
715 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400716 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400717 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400718 return;
719 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400720 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400721
722 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500723 *bearer_id = n->active_links[0];
724 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400725 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
726 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500727 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400728 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400729 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400730 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400731 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400732 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500733 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400734}
735
736static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
737{
738 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400739 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400740 struct tipc_media_addr *maddr;
741 struct sk_buff_head xmitq;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400742 int old_bearer_id = bearer_id;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400743
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400744 if (!l)
745 return;
746
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400747 __skb_queue_head_init(&xmitq);
748
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500749 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400750 if (!tipc_link_is_establishing(l)) {
751 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
752 if (delete) {
753 kfree(l);
754 le->link = NULL;
755 n->link_cnt--;
756 }
757 } else {
758 /* Defuse pending tipc_node_link_up() */
759 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400760 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500761 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400762 if (delete)
763 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400764 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
765 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766}
767
Jon Maloy38077b82017-10-13 11:04:19 +0200768static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400770 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771}
772
Jon Maloy38077b82017-10-13 11:04:19 +0200773bool tipc_node_is_up(struct net *net, u32 addr)
774{
775 struct tipc_node *n;
776 bool retval = false;
777
778 if (in_own_node(net, addr))
779 return true;
780
781 n = tipc_node_find(net, addr);
782 if (!n)
783 return false;
784 retval = node_is_up(n);
785 tipc_node_put(n);
786 return retval;
787}
788
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100789static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
790{
791 struct tipc_node *n;
792
793 addr ^= tipc_net(net)->random;
794 while ((n = tipc_node_find(net, addr))) {
795 tipc_node_put(n);
796 addr++;
797 }
798 return addr;
799}
800
801/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
802 */
803u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
804{
805 struct tipc_net *tn = tipc_net(net);
806 struct tipc_node *n;
807
808 /* Suggest new address if some other peer is using this one */
809 n = tipc_node_find(net, addr);
810 if (n) {
811 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
812 addr = 0;
813 tipc_node_put(n);
814 if (!addr)
815 return 0;
816 return tipc_node_suggest_addr(net, addr);
817 }
818
819 /* Suggest previously used address if peer is known */
820 n = tipc_node_find_by_id(net, id);
821 if (n) {
822 addr = n->addr;
823 tipc_node_put(n);
824 }
825 /* Even this node may be in trial phase */
826 if (tn->trial_addr == addr)
827 return tipc_node_suggest_addr(net, addr);
828
829 return addr;
830}
831
832void tipc_node_check_dest(struct net *net, u32 addr,
833 u8 *peer_id, struct tipc_bearer *b,
Jon Paul Maloycf148812015-07-30 18:24:22 -0400834 u16 capabilities, u32 signature,
835 struct tipc_media_addr *maddr,
836 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400837{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400838 struct tipc_node *n;
839 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400840 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400841 bool addr_match = false;
842 bool sign_match = false;
843 bool link_up = false;
844 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400845 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400846 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400847 unsigned long intv;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400848
Jon Paul Maloycf148812015-07-30 18:24:22 -0400849 *dupl_addr = false;
850 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400851
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100852 n = tipc_node_create(net, addr, peer_id, capabilities);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400853 if (!n)
854 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400855
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500856 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400857
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400858 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400859
860 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400861 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400862 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400863 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400864 sign_match = (signature == n->signature);
865
866 /* These three flags give us eight permutations: */
867
868 if (sign_match && addr_match && link_up) {
869 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400870 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400871 } else if (sign_match && addr_match && !link_up) {
872 /* Respond. The link will come up in due time */
873 *respond = true;
874 } else if (sign_match && !addr_match && link_up) {
875 /* Peer has changed i/f address without rebooting.
876 * If so, the link will reset soon, and the next
877 * discovery will be accepted. So we can ignore it.
878 * It may also be an cloned or malicious peer having
879 * chosen the same node address and signature as an
880 * existing one.
881 * Ignore requests until the link goes down, if ever.
882 */
883 *dupl_addr = true;
884 } else if (sign_match && !addr_match && !link_up) {
885 /* Peer link has changed i/f address without rebooting.
886 * It may also be a cloned or malicious peer; we can't
887 * distinguish between the two.
888 * The signature is correct, so we must accept.
889 */
890 accept_addr = true;
891 *respond = true;
892 } else if (!sign_match && addr_match && link_up) {
893 /* Peer node rebooted. Two possibilities:
894 * - Delayed re-discovery; this link endpoint has already
895 * reset and re-established contact with the peer, before
896 * receiving a discovery message from that node.
897 * (The peer happened to receive one from this node first).
898 * - The peer came back so fast that our side has not
899 * discovered it yet. Probing from this side will soon
900 * reset the link, since there can be no working link
901 * endpoint at the peer end, and the link will re-establish.
902 * Accept the signature, since it comes from a known peer.
903 */
904 n->signature = signature;
905 } else if (!sign_match && addr_match && !link_up) {
906 /* The peer node has rebooted.
907 * Accept signature, since it is a known peer.
908 */
909 n->signature = signature;
910 *respond = true;
911 } else if (!sign_match && !addr_match && link_up) {
912 /* Peer rebooted with new address, or a new/duplicate peer.
913 * Ignore until the link goes down, if ever.
914 */
915 *dupl_addr = true;
916 } else if (!sign_match && !addr_match && !link_up) {
917 /* Peer rebooted with new address, or it is a new peer.
918 * Accept signature and address.
919 */
920 n->signature = signature;
921 accept_addr = true;
922 *respond = true;
923 }
924
925 if (!accept_addr)
926 goto exit;
927
928 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400929 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +0100930 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400931 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +0100932
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400933 if_name = strchr(b->name, ':') + 1;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400934 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400935 b->net_plane, b->mtu, b->priority,
936 b->window, mod(tipc_net(net)->random),
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100937 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400938 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400939 tipc_bc_sndlink(n->net), n->bc_entry.link,
940 &le->inputq,
941 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -0400942 *respond = false;
943 goto exit;
944 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400945 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400946 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -0400947 if (n->state == NODE_FAILINGOVER)
948 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400949 le->link = l;
950 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400951 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400952 if (n->link_cnt == 1) {
953 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
954 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400955 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400956 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400957 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400958 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400959exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500960 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +0100961 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400962 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400963 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400964}
965
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400966void tipc_node_delete_links(struct net *net, int bearer_id)
967{
968 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400969 struct tipc_node *n;
970
971 rcu_read_lock();
972 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400973 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400974 }
975 rcu_read_unlock();
976}
977
978static void tipc_node_reset_links(struct tipc_node *n)
979{
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400980 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400981
Jon Maloyd50ccc22018-03-22 20:42:50 +0100982 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400983
984 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400985 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400986 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400987}
988
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400989/* tipc_node_fsm_evt - node finite state machine
990 * Determines when contact is allowed with peer node
991 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400992static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400993{
994 int state = n->state;
995
996 switch (state) {
997 case SELF_DOWN_PEER_DOWN:
998 switch (evt) {
999 case SELF_ESTABL_CONTACT_EVT:
1000 state = SELF_UP_PEER_COMING;
1001 break;
1002 case PEER_ESTABL_CONTACT_EVT:
1003 state = SELF_COMING_PEER_UP;
1004 break;
1005 case SELF_LOST_CONTACT_EVT:
1006 case PEER_LOST_CONTACT_EVT:
1007 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001008 case NODE_SYNCH_END_EVT:
1009 case NODE_SYNCH_BEGIN_EVT:
1010 case NODE_FAILOVER_BEGIN_EVT:
1011 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001012 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001013 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001014 }
1015 break;
1016 case SELF_UP_PEER_UP:
1017 switch (evt) {
1018 case SELF_LOST_CONTACT_EVT:
1019 state = SELF_DOWN_PEER_LEAVING;
1020 break;
1021 case PEER_LOST_CONTACT_EVT:
1022 state = SELF_LEAVING_PEER_DOWN;
1023 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001024 case NODE_SYNCH_BEGIN_EVT:
1025 state = NODE_SYNCHING;
1026 break;
1027 case NODE_FAILOVER_BEGIN_EVT:
1028 state = NODE_FAILINGOVER;
1029 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001030 case SELF_ESTABL_CONTACT_EVT:
1031 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001032 case NODE_SYNCH_END_EVT:
1033 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001034 break;
1035 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001036 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001037 }
1038 break;
1039 case SELF_DOWN_PEER_LEAVING:
1040 switch (evt) {
1041 case PEER_LOST_CONTACT_EVT:
1042 state = SELF_DOWN_PEER_DOWN;
1043 break;
1044 case SELF_ESTABL_CONTACT_EVT:
1045 case PEER_ESTABL_CONTACT_EVT:
1046 case SELF_LOST_CONTACT_EVT:
1047 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001048 case NODE_SYNCH_END_EVT:
1049 case NODE_SYNCH_BEGIN_EVT:
1050 case NODE_FAILOVER_BEGIN_EVT:
1051 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001052 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001053 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001054 }
1055 break;
1056 case SELF_UP_PEER_COMING:
1057 switch (evt) {
1058 case PEER_ESTABL_CONTACT_EVT:
1059 state = SELF_UP_PEER_UP;
1060 break;
1061 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001062 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001063 break;
1064 case SELF_ESTABL_CONTACT_EVT:
1065 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001066 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001067 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001068 break;
1069 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001070 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001071 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001072 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001073 }
1074 break;
1075 case SELF_COMING_PEER_UP:
1076 switch (evt) {
1077 case SELF_ESTABL_CONTACT_EVT:
1078 state = SELF_UP_PEER_UP;
1079 break;
1080 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001081 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001082 break;
1083 case SELF_LOST_CONTACT_EVT:
1084 case PEER_ESTABL_CONTACT_EVT:
1085 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001086 case NODE_SYNCH_END_EVT:
1087 case NODE_SYNCH_BEGIN_EVT:
1088 case NODE_FAILOVER_BEGIN_EVT:
1089 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001090 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001091 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001092 }
1093 break;
1094 case SELF_LEAVING_PEER_DOWN:
1095 switch (evt) {
1096 case SELF_LOST_CONTACT_EVT:
1097 state = SELF_DOWN_PEER_DOWN;
1098 break;
1099 case SELF_ESTABL_CONTACT_EVT:
1100 case PEER_ESTABL_CONTACT_EVT:
1101 case PEER_LOST_CONTACT_EVT:
1102 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001103 case NODE_SYNCH_END_EVT:
1104 case NODE_SYNCH_BEGIN_EVT:
1105 case NODE_FAILOVER_BEGIN_EVT:
1106 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001107 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001108 goto illegal_evt;
1109 }
1110 break;
1111 case NODE_FAILINGOVER:
1112 switch (evt) {
1113 case SELF_LOST_CONTACT_EVT:
1114 state = SELF_DOWN_PEER_LEAVING;
1115 break;
1116 case PEER_LOST_CONTACT_EVT:
1117 state = SELF_LEAVING_PEER_DOWN;
1118 break;
1119 case NODE_FAILOVER_END_EVT:
1120 state = SELF_UP_PEER_UP;
1121 break;
1122 case NODE_FAILOVER_BEGIN_EVT:
1123 case SELF_ESTABL_CONTACT_EVT:
1124 case PEER_ESTABL_CONTACT_EVT:
1125 break;
1126 case NODE_SYNCH_BEGIN_EVT:
1127 case NODE_SYNCH_END_EVT:
1128 default:
1129 goto illegal_evt;
1130 }
1131 break;
1132 case NODE_SYNCHING:
1133 switch (evt) {
1134 case SELF_LOST_CONTACT_EVT:
1135 state = SELF_DOWN_PEER_LEAVING;
1136 break;
1137 case PEER_LOST_CONTACT_EVT:
1138 state = SELF_LEAVING_PEER_DOWN;
1139 break;
1140 case NODE_SYNCH_END_EVT:
1141 state = SELF_UP_PEER_UP;
1142 break;
1143 case NODE_FAILOVER_BEGIN_EVT:
1144 state = NODE_FAILINGOVER;
1145 break;
1146 case NODE_SYNCH_BEGIN_EVT:
1147 case SELF_ESTABL_CONTACT_EVT:
1148 case PEER_ESTABL_CONTACT_EVT:
1149 break;
1150 case NODE_FAILOVER_END_EVT:
1151 default:
1152 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001153 }
1154 break;
1155 default:
1156 pr_err("Unknown node fsm state %x\n", state);
1157 break;
1158 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001159 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001160 return;
1161
1162illegal_evt:
1163 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001164}
1165
Jon Paul Maloy52666982015-10-22 08:51:41 -04001166static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001167 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001169 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001170 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001171 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001172 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001173 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174
Jon Maloyd50ccc22018-03-22 20:42:50 +01001175 pr_debug("Lost contact with %x\n", n->addr);
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001176
Jon Paul Maloy52666982015-10-22 08:51:41 -04001177 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001178 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001180 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001182 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001183 if (l)
1184 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001186
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001187 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001188 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001189
1190 /* Notify sockets connected to node */
1191 list_for_each_entry_safe(conn, safe, conns, list) {
1192 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001193 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001194 conn->peer_node, conn->port,
1195 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001196 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001197 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001198 list_del(&conn->list);
1199 kfree(conn);
1200 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201}
1202
Erik Hugne78acb1f2014-04-24 16:26:47 +02001203/**
1204 * tipc_node_get_linkname - get the name of a link
1205 *
1206 * @bearer_id: id of the bearer
1207 * @node: peer node address
1208 * @linkname: link name output buffer
1209 *
1210 * Returns 0 on success
1211 */
Ying Xuef2f98002015-01-09 15:27:05 +08001212int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1213 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001214{
1215 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001216 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001217 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001218
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001219 if (!node)
1220 return err;
1221
1222 if (bearer_id >= MAX_BEARERS)
1223 goto exit;
1224
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001225 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001226 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001227 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001228 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001229 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001230 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001231 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001232exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001233 tipc_node_put(node);
1234 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001235}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001236
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001237/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001238static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001239{
1240 void *hdr;
1241 struct nlattr *attrs;
1242
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001243 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001244 NLM_F_MULTI, TIPC_NL_NODE_GET);
1245 if (!hdr)
1246 return -EMSGSIZE;
1247
1248 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1249 if (!attrs)
1250 goto msg_full;
1251
1252 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1253 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001254 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001255 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1256 goto attr_msg_full;
1257
1258 nla_nest_end(msg->skb, attrs);
1259 genlmsg_end(msg->skb, hdr);
1260
1261 return 0;
1262
1263attr_msg_full:
1264 nla_nest_cancel(msg->skb, attrs);
1265msg_full:
1266 genlmsg_cancel(msg->skb, hdr);
1267
1268 return -EMSGSIZE;
1269}
1270
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001271/**
1272 * tipc_node_xmit() is the general link level function for message sending
1273 * @net: the applicable net namespace
1274 * @list: chain of buffers containing message
1275 * @dnode: address of destination node
1276 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001277 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001278 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001279 */
1280int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1281 u32 dnode, int selector)
1282{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001283 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001284 struct tipc_node *n;
1285 struct sk_buff_head xmitq;
Richard Alpe4952cd32016-02-11 10:43:15 +01001286 int bearer_id;
1287 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001288
Richard Alpe4952cd32016-02-11 10:43:15 +01001289 if (in_own_node(net, dnode)) {
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001290 tipc_sk_rcv(net, list);
1291 return 0;
1292 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001293
1294 n = tipc_node_find(net, dnode);
1295 if (unlikely(!n)) {
1296 skb_queue_purge(list);
1297 return -EHOSTUNREACH;
1298 }
1299
1300 tipc_node_read_lock(n);
1301 bearer_id = n->active_links[selector & 1];
1302 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1303 tipc_node_read_unlock(n);
1304 tipc_node_put(n);
1305 skb_queue_purge(list);
1306 return -EHOSTUNREACH;
1307 }
1308
1309 __skb_queue_head_init(&xmitq);
1310 le = &n->links[bearer_id];
1311 spin_lock_bh(&le->lock);
1312 rc = tipc_link_xmit(le->link, list, &xmitq);
1313 spin_unlock_bh(&le->lock);
1314 tipc_node_read_unlock(n);
1315
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001316 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001317 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001318 else
1319 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001320
1321 tipc_node_put(n);
1322
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001323 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001324}
1325
1326/* tipc_node_xmit_skb(): send single buffer to destination
1327 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1328 * messages, which will not be rejected
1329 * The only exception is datagram messages rerouted after secondary
1330 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001331 */
1332int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1333 u32 selector)
1334{
1335 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001336
1337 skb_queue_head_init(&head);
1338 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001339 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001340 return 0;
1341}
1342
Jon Maloyf70d37b2017-10-13 11:04:21 +02001343/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1344 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1345 */
1346int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1347{
1348 struct sk_buff *skb;
1349 u32 selector, dnode;
1350
1351 while ((skb = __skb_dequeue(xmitq))) {
1352 selector = msg_origport(buf_msg(skb));
1353 dnode = msg_destnode(buf_msg(skb));
1354 tipc_node_xmit_skb(net, skb, dnode, selector);
1355 }
1356 return 0;
1357}
1358
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001359void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1360{
1361 struct sk_buff *txskb;
1362 struct tipc_node *n;
1363 u32 dst;
1364
1365 rcu_read_lock();
1366 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1367 dst = n->addr;
1368 if (in_own_node(net, dst))
1369 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001370 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001371 continue;
1372 txskb = pskb_copy(skb, GFP_ATOMIC);
1373 if (!txskb)
1374 break;
1375 msg_set_destnode(buf_msg(txskb), dst);
1376 tipc_node_xmit_skb(net, txskb, dst, 0);
1377 }
1378 rcu_read_unlock();
1379
1380 kfree_skb(skb);
1381}
1382
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001383static void tipc_node_mcast_rcv(struct tipc_node *n)
1384{
1385 struct tipc_bclink_entry *be = &n->bc_entry;
1386
1387 /* 'arrvq' is under inputq2's lock protection */
1388 spin_lock_bh(&be->inputq2.lock);
1389 spin_lock_bh(&be->inputq1.lock);
1390 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1391 spin_unlock_bh(&be->inputq1.lock);
1392 spin_unlock_bh(&be->inputq2.lock);
1393 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1394}
1395
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001396static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1397 int bearer_id, struct sk_buff_head *xmitq)
1398{
1399 struct tipc_link *ucl;
1400 int rc;
1401
1402 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1403
1404 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001405 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001406 return;
1407 }
1408
1409 if (!(rc & TIPC_LINK_SND_STATE))
1410 return;
1411
1412 /* If probe message, a STATE response will be sent anyway */
1413 if (msg_probe(hdr))
1414 return;
1415
1416 /* Produce a STATE message carrying broadcast NACK */
1417 tipc_node_read_lock(n);
1418 ucl = n->links[bearer_id].link;
1419 if (ucl)
1420 tipc_link_build_state_msg(ucl, xmitq);
1421 tipc_node_read_unlock(n);
1422}
1423
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001424/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001425 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1426 * @net: the applicable net namespace
1427 * @skb: TIPC packet
1428 * @bearer_id: id of bearer message arrived on
1429 *
1430 * Invoked with no locks held.
1431 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001432static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001433{
1434 int rc;
1435 struct sk_buff_head xmitq;
1436 struct tipc_bclink_entry *be;
1437 struct tipc_link_entry *le;
1438 struct tipc_msg *hdr = buf_msg(skb);
1439 int usr = msg_user(hdr);
1440 u32 dnode = msg_destnode(hdr);
1441 struct tipc_node *n;
1442
1443 __skb_queue_head_init(&xmitq);
1444
1445 /* If NACK for other node, let rcv link for that node peek into it */
1446 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1447 n = tipc_node_find(net, dnode);
1448 else
1449 n = tipc_node_find(net, msg_prevnode(hdr));
1450 if (!n) {
1451 kfree_skb(skb);
1452 return;
1453 }
1454 be = &n->bc_entry;
1455 le = &n->links[bearer_id];
1456
1457 rc = tipc_bcast_rcv(net, be->link, skb);
1458
Jon Paul Maloy52666982015-10-22 08:51:41 -04001459 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001460 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001461 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001462 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001463 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001464 }
1465
1466 if (!skb_queue_empty(&xmitq))
1467 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1468
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001469 if (!skb_queue_empty(&be->inputq1))
1470 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001471
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001472 /* If reassembly or retransmission failure => reset all links to peer */
1473 if (rc & TIPC_LINK_DOWN_EVT)
1474 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001475
Jon Paul Maloy52666982015-10-22 08:51:41 -04001476 tipc_node_put(n);
1477}
1478
1479/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001480 * tipc_node_check_state - check and if necessary update node state
1481 * @skb: TIPC packet
1482 * @bearer_id: identity of bearer delivering the packet
1483 * Returns true if state is ok, otherwise consumes buffer and returns false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001484 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001485static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001486 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001487{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001488 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001489 int usr = msg_user(hdr);
1490 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001491 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001492 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1493 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001494 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001495 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001496 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001497 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001498 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001499
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001500 l = n->links[bearer_id].link;
1501 if (!l)
1502 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001503 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001504
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001505
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001506 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1507 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001508
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001509 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001510 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1511 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1512 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001513 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001514 }
1515 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001516
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001517 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001518 if (state == SELF_UP_PEER_COMING) {
1519 if (!tipc_link_is_up(l))
1520 return true;
1521 if (!msg_peer_link_is_up(hdr))
1522 return true;
1523 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1524 }
1525
1526 if (state == SELF_DOWN_PEER_LEAVING) {
1527 if (msg_peer_node_is_up(hdr))
1528 return false;
1529 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001530 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001531 }
1532
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001533 if (state == SELF_LEAVING_PEER_DOWN)
1534 return false;
1535
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001536 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001537 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001538 return true;
1539
1540 /* Initiate or update failover mode if applicable */
1541 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1542 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001543 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001544 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001545 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1546 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001547 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001548 /* If pkts arrive out of order, use lowest calculated syncpt */
1549 if (less(syncpt, n->sync_point))
1550 n->sync_point = syncpt;
1551 }
1552
1553 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001554 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001555 if (!more(rcv_nxt, n->sync_point))
1556 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001557 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1558 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001559 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001560 return true;
1561 }
1562
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001563 /* No synching needed if only one link */
1564 if (!pl || !tipc_link_is_up(pl))
1565 return true;
1566
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001567 /* Initiate synch mode if applicable */
1568 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001569 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001570 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001571 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001572 if (n->state == SELF_UP_PEER_UP) {
1573 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001574 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001575 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1576 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001577 }
1578
1579 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001580 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001581 if (tipc_link_is_synching(l)) {
1582 tnl = l;
1583 } else {
1584 tnl = pl;
1585 pl = l;
1586 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001587 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1588 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001589 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001590 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001591 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001592 return true;
1593 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001594 if (l == pl)
1595 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001596 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1597 return true;
1598 if (usr == LINK_PROTOCOL)
1599 return true;
1600 return false;
1601 }
1602 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001603}
1604
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001605/**
1606 * tipc_rcv - process TIPC packets/messages arriving from off-node
1607 * @net: the applicable net namespace
1608 * @skb: TIPC packet
1609 * @bearer: pointer to bearer message arrived on
1610 *
1611 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1612 * structure (i.e. cannot be NULL), but bearer can be inactive.
1613 */
1614void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1615{
1616 struct sk_buff_head xmitq;
1617 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001618 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001619 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001620 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001621 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001622 int usr, rc = 0;
1623 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001624
1625 __skb_queue_head_init(&xmitq);
1626
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001627 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01001628 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001629 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001630 hdr = buf_msg(skb);
1631 usr = msg_user(hdr);
1632 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001633
Jon Paul Maloy52666982015-10-22 08:51:41 -04001634 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001635 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001636 if (unlikely(usr == LINK_CONFIG))
1637 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001638 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001639 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001640 }
1641
Hamish Martinefe79052016-04-29 10:40:24 -04001642 /* Discard unicast link messages destined for another node */
1643 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1644 goto discard;
1645
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001646 /* Locate neighboring node that sent packet */
1647 n = tipc_node_find(net, msg_prevnode(hdr));
1648 if (unlikely(!n))
1649 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001650 le = &n->links[bearer_id];
1651
Jon Paul Maloy52666982015-10-22 08:51:41 -04001652 /* Ensure broadcast reception is in synch with peer's send state */
1653 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001654 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001655 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001656 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001657
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001658 /* Receive packet directly if conditions permit */
1659 tipc_node_read_lock(n);
1660 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001661 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001662 if (le->link) {
1663 rc = tipc_link_rcv(le->link, skb, &xmitq);
1664 skb = NULL;
1665 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001666 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001667 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001668 tipc_node_read_unlock(n);
1669
1670 /* Check/update node state before receiving */
1671 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001672 if (unlikely(skb_linearize(skb)))
1673 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001674 tipc_node_write_lock(n);
1675 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1676 if (le->link) {
1677 rc = tipc_link_rcv(le->link, skb, &xmitq);
1678 skb = NULL;
1679 }
1680 }
1681 tipc_node_write_unlock(n);
1682 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001683
1684 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001685 tipc_node_link_up(n, bearer_id, &xmitq);
1686
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001687 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001688 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001689
Jon Paul Maloy52666982015-10-22 08:51:41 -04001690 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1691 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001692
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001693 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1694 tipc_node_mcast_rcv(n);
1695
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001696 if (!skb_queue_empty(&le->inputq))
1697 tipc_sk_rcv(net, &le->inputq);
1698
1699 if (!skb_queue_empty(&xmitq))
1700 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1701
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001702 tipc_node_put(n);
1703discard:
1704 kfree_skb(skb);
1705}
1706
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001707void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1708 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01001709{
1710 struct tipc_net *tn = tipc_net(net);
1711 int bearer_id = b->identity;
1712 struct sk_buff_head xmitq;
1713 struct tipc_link_entry *e;
1714 struct tipc_node *n;
1715
1716 __skb_queue_head_init(&xmitq);
1717
1718 rcu_read_lock();
1719
1720 list_for_each_entry_rcu(n, &tn->node_list, list) {
1721 tipc_node_write_lock(n);
1722 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001723 if (e->link) {
1724 if (prop == TIPC_NLA_PROP_TOL)
1725 tipc_link_set_tolerance(e->link, b->tolerance,
1726 &xmitq);
1727 else if (prop == TIPC_NLA_PROP_MTU)
1728 tipc_link_set_mtu(e->link, b->mtu);
1729 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01001730 tipc_node_write_unlock(n);
1731 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1732 }
1733
1734 rcu_read_unlock();
1735}
1736
Richard Alpeb3404022016-08-18 10:33:52 +02001737int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1738{
1739 struct net *net = sock_net(skb->sk);
1740 struct tipc_net *tn = net_generic(net, tipc_net_id);
1741 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1742 struct tipc_node *peer;
1743 u32 addr;
1744 int err;
1745 int i;
1746
1747 /* We identify the peer by its net */
1748 if (!info->attrs[TIPC_NLA_NET])
1749 return -EINVAL;
1750
1751 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001752 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
Johannes Bergfe521452017-04-12 14:34:08 +02001753 info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02001754 if (err)
1755 return err;
1756
1757 if (!attrs[TIPC_NLA_NET_ADDR])
1758 return -EINVAL;
1759
1760 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1761
1762 if (in_own_node(net, addr))
1763 return -ENOTSUPP;
1764
1765 spin_lock_bh(&tn->node_list_lock);
1766 peer = tipc_node_find(net, addr);
1767 if (!peer) {
1768 spin_unlock_bh(&tn->node_list_lock);
1769 return -ENXIO;
1770 }
1771
1772 tipc_node_write_lock(peer);
1773 if (peer->state != SELF_DOWN_PEER_DOWN &&
1774 peer->state != SELF_DOWN_PEER_LEAVING) {
1775 tipc_node_write_unlock(peer);
1776 err = -EBUSY;
1777 goto err_out;
1778 }
1779
1780 for (i = 0; i < MAX_BEARERS; i++) {
1781 struct tipc_link_entry *le = &peer->links[i];
1782
1783 if (le->link) {
1784 kfree(le->link);
1785 le->link = NULL;
1786 peer->link_cnt--;
1787 }
1788 }
1789 tipc_node_write_unlock(peer);
1790 tipc_node_delete(peer);
1791
1792 err = 0;
1793err_out:
1794 tipc_node_put(peer);
1795 spin_unlock_bh(&tn->node_list_lock);
1796
1797 return err;
1798}
1799
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001800int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1801{
1802 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001803 struct net *net = sock_net(skb->sk);
1804 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001805 int done = cb->args[0];
1806 int last_addr = cb->args[1];
1807 struct tipc_node *node;
1808 struct tipc_nl_msg msg;
1809
1810 if (done)
1811 return 0;
1812
1813 msg.skb = skb;
1814 msg.portid = NETLINK_CB(cb->skb).portid;
1815 msg.seq = cb->nlh->nlmsg_seq;
1816
1817 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001818 if (last_addr) {
1819 node = tipc_node_find(net, last_addr);
1820 if (!node) {
1821 rcu_read_unlock();
1822 /* We never set seq or call nl_dump_check_consistent()
1823 * this means that setting prev_seq here will cause the
1824 * consistence check to fail in the netlink callback
1825 * handler. Resulting in the NLMSG_DONE message having
1826 * the NLM_F_DUMP_INTR flag set if the node state
1827 * changed while we released the lock.
1828 */
1829 cb->prev_seq = 1;
1830 return -EPIPE;
1831 }
1832 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001833 }
1834
Ying Xuef2f98002015-01-09 15:27:05 +08001835 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001836 if (last_addr) {
1837 if (node->addr == last_addr)
1838 last_addr = 0;
1839 else
1840 continue;
1841 }
1842
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001843 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001844 err = __tipc_nl_add_node(&msg, node);
1845 if (err) {
1846 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001847 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001848 goto out;
1849 }
1850
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001851 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001852 }
1853 done = 1;
1854out:
1855 cb->args[0] = done;
1856 cb->args[1] = last_addr;
1857 rcu_read_unlock();
1858
1859 return skb->len;
1860}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001861
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001862/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001863 * @net: the applicable net namespace
1864 * @name: pointer to link name string
1865 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1866 *
1867 * Returns pointer to node owning the link, or 0 if no matching link is found.
1868 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001869static struct tipc_node *tipc_node_find_by_name(struct net *net,
1870 const char *link_name,
1871 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001872{
1873 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001874 struct tipc_link *l;
1875 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001876 struct tipc_node *found_node = NULL;
1877 int i;
1878
1879 *bearer_id = 0;
1880 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001881 list_for_each_entry_rcu(n, &tn->node_list, list) {
1882 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001883 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001884 l = n->links[i].link;
1885 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001886 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001887 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001888 break;
1889 }
1890 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001891 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001892 if (found_node)
1893 break;
1894 }
1895 rcu_read_unlock();
1896
1897 return found_node;
1898}
1899
1900int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1901{
1902 int err;
1903 int res = 0;
1904 int bearer_id;
1905 char *name;
1906 struct tipc_link *link;
1907 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01001908 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001909 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1910 struct net *net = sock_net(skb->sk);
1911
Richard Alped01332f2016-02-01 08:19:56 +01001912 __skb_queue_head_init(&xmitq);
1913
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001914 if (!info->attrs[TIPC_NLA_LINK])
1915 return -EINVAL;
1916
1917 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1918 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02001919 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001920 if (err)
1921 return err;
1922
1923 if (!attrs[TIPC_NLA_LINK_NAME])
1924 return -EINVAL;
1925
1926 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1927
1928 if (strcmp(name, tipc_bclink_name) == 0)
1929 return tipc_nl_bc_link_set(net, attrs);
1930
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001931 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001932 if (!node)
1933 return -EINVAL;
1934
1935 tipc_node_read_lock(node);
1936
1937 link = node->links[bearer_id].link;
1938 if (!link) {
1939 res = -EINVAL;
1940 goto out;
1941 }
1942
1943 if (attrs[TIPC_NLA_LINK_PROP]) {
1944 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1945
1946 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1947 props);
1948 if (err) {
1949 res = err;
1950 goto out;
1951 }
1952
1953 if (props[TIPC_NLA_PROP_TOL]) {
1954 u32 tol;
1955
1956 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01001957 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001958 }
1959 if (props[TIPC_NLA_PROP_PRIO]) {
1960 u32 prio;
1961
1962 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01001963 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001964 }
1965 if (props[TIPC_NLA_PROP_WIN]) {
1966 u32 win;
1967
1968 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1969 tipc_link_set_queue_limits(link, win);
1970 }
1971 }
1972
1973out:
1974 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01001975 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001976 return res;
1977}
1978
1979int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
1980{
1981 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08001982 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001983 struct tipc_nl_msg msg;
1984 char *name;
1985 int err;
1986
1987 msg.portid = info->snd_portid;
1988 msg.seq = info->snd_seq;
1989
Ying Xue94f6a802018-05-08 21:44:06 +08001990 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001991 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08001992
1993 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1994 info->attrs[TIPC_NLA_LINK],
1995 tipc_nl_link_policy, info->extack);
1996 if (err)
1997 return err;
1998
1999 if (!attrs[TIPC_NLA_LINK_NAME])
2000 return -EINVAL;
2001
2002 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002003
2004 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2005 if (!msg.skb)
2006 return -ENOMEM;
2007
2008 if (strcmp(name, tipc_bclink_name) == 0) {
2009 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002010 if (err)
2011 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002012 } else {
2013 int bearer_id;
2014 struct tipc_node *node;
2015 struct tipc_link *link;
2016
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002017 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002018 if (!node) {
2019 err = -EINVAL;
2020 goto err_free;
2021 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002022
2023 tipc_node_read_lock(node);
2024 link = node->links[bearer_id].link;
2025 if (!link) {
2026 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002027 err = -EINVAL;
2028 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002029 }
2030
2031 err = __tipc_nl_add_link(net, &msg, link, 0);
2032 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002033 if (err)
2034 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002035 }
2036
2037 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002038
2039err_free:
2040 nlmsg_free(msg.skb);
2041 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002042}
2043
2044int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2045{
2046 int err;
2047 char *link_name;
2048 unsigned int bearer_id;
2049 struct tipc_link *link;
2050 struct tipc_node *node;
2051 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2052 struct net *net = sock_net(skb->sk);
2053 struct tipc_link_entry *le;
2054
2055 if (!info->attrs[TIPC_NLA_LINK])
2056 return -EINVAL;
2057
2058 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2059 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02002060 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002061 if (err)
2062 return err;
2063
2064 if (!attrs[TIPC_NLA_LINK_NAME])
2065 return -EINVAL;
2066
2067 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2068
2069 if (strcmp(link_name, tipc_bclink_name) == 0) {
2070 err = tipc_bclink_reset_stats(net);
2071 if (err)
2072 return err;
2073 return 0;
2074 }
2075
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002076 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002077 if (!node)
2078 return -EINVAL;
2079
2080 le = &node->links[bearer_id];
2081 tipc_node_read_lock(node);
2082 spin_lock_bh(&le->lock);
2083 link = node->links[bearer_id].link;
2084 if (!link) {
2085 spin_unlock_bh(&le->lock);
2086 tipc_node_read_unlock(node);
2087 return -EINVAL;
2088 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002089 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002090 spin_unlock_bh(&le->lock);
2091 tipc_node_read_unlock(node);
2092 return 0;
2093}
2094
2095/* Caller should hold node lock */
2096static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2097 struct tipc_node *node, u32 *prev_link)
2098{
2099 u32 i;
2100 int err;
2101
2102 for (i = *prev_link; i < MAX_BEARERS; i++) {
2103 *prev_link = i;
2104
2105 if (!node->links[i].link)
2106 continue;
2107
2108 err = __tipc_nl_add_link(net, msg,
2109 node->links[i].link, NLM_F_MULTI);
2110 if (err)
2111 return err;
2112 }
2113 *prev_link = 0;
2114
2115 return 0;
2116}
2117
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002118int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002119{
2120 struct net *net = sock_net(skb->sk);
2121 struct tipc_net *tn = net_generic(net, tipc_net_id);
2122 struct tipc_node *node;
2123 struct tipc_nl_msg msg;
2124 u32 prev_node = cb->args[0];
2125 u32 prev_link = cb->args[1];
2126 int done = cb->args[2];
2127 int err;
2128
2129 if (done)
2130 return 0;
2131
2132 msg.skb = skb;
2133 msg.portid = NETLINK_CB(cb->skb).portid;
2134 msg.seq = cb->nlh->nlmsg_seq;
2135
2136 rcu_read_lock();
2137 if (prev_node) {
2138 node = tipc_node_find(net, prev_node);
2139 if (!node) {
2140 /* We never set seq or call nl_dump_check_consistent()
2141 * this means that setting prev_seq here will cause the
2142 * consistence check to fail in the netlink callback
2143 * handler. Resulting in the last NLMSG_DONE message
2144 * having the NLM_F_DUMP_INTR flag set.
2145 */
2146 cb->prev_seq = 1;
2147 goto out;
2148 }
2149 tipc_node_put(node);
2150
2151 list_for_each_entry_continue_rcu(node, &tn->node_list,
2152 list) {
2153 tipc_node_read_lock(node);
2154 err = __tipc_nl_add_node_links(net, &msg, node,
2155 &prev_link);
2156 tipc_node_read_unlock(node);
2157 if (err)
2158 goto out;
2159
2160 prev_node = node->addr;
2161 }
2162 } else {
2163 err = tipc_nl_add_bc_link(net, &msg);
2164 if (err)
2165 goto out;
2166
2167 list_for_each_entry_rcu(node, &tn->node_list, list) {
2168 tipc_node_read_lock(node);
2169 err = __tipc_nl_add_node_links(net, &msg, node,
2170 &prev_link);
2171 tipc_node_read_unlock(node);
2172 if (err)
2173 goto out;
2174
2175 prev_node = node->addr;
2176 }
2177 }
2178 done = 1;
2179out:
2180 rcu_read_unlock();
2181
2182 cb->args[0] = prev_node;
2183 cb->args[1] = prev_link;
2184 cb->args[2] = done;
2185
2186 return skb->len;
2187}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002188
2189int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2190{
2191 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2192 struct net *net = sock_net(skb->sk);
2193 int err;
2194
2195 if (!info->attrs[TIPC_NLA_MON])
2196 return -EINVAL;
2197
2198 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2199 info->attrs[TIPC_NLA_MON],
Johannes Bergfe521452017-04-12 14:34:08 +02002200 tipc_nl_monitor_policy, info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002201 if (err)
2202 return err;
2203
2204 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2205 u32 val;
2206
2207 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2208 err = tipc_nl_monitor_set_threshold(net, val);
2209 if (err)
2210 return err;
2211 }
2212
2213 return 0;
2214}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002215
2216static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2217{
2218 struct nlattr *attrs;
2219 void *hdr;
2220 u32 val;
2221
2222 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2223 0, TIPC_NL_MON_GET);
2224 if (!hdr)
2225 return -EMSGSIZE;
2226
2227 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2228 if (!attrs)
2229 goto msg_full;
2230
2231 val = tipc_nl_monitor_get_threshold(net);
2232
2233 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2234 goto attr_msg_full;
2235
2236 nla_nest_end(msg->skb, attrs);
2237 genlmsg_end(msg->skb, hdr);
2238
2239 return 0;
2240
2241attr_msg_full:
2242 nla_nest_cancel(msg->skb, attrs);
2243msg_full:
2244 genlmsg_cancel(msg->skb, hdr);
2245
2246 return -EMSGSIZE;
2247}
2248
2249int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2250{
2251 struct net *net = sock_net(skb->sk);
2252 struct tipc_nl_msg msg;
2253 int err;
2254
2255 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002256 if (!msg.skb)
2257 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002258 msg.portid = info->snd_portid;
2259 msg.seq = info->snd_seq;
2260
2261 err = __tipc_nl_add_monitor_prop(net, &msg);
2262 if (err) {
2263 nlmsg_free(msg.skb);
2264 return err;
2265 }
2266
2267 return genlmsg_reply(msg.skb, info);
2268}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002269
2270int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2271{
2272 struct net *net = sock_net(skb->sk);
2273 u32 prev_bearer = cb->args[0];
2274 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002275 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002276 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002277
2278 if (prev_bearer == MAX_BEARERS)
2279 return 0;
2280
2281 msg.skb = skb;
2282 msg.portid = NETLINK_CB(cb->skb).portid;
2283 msg.seq = cb->nlh->nlmsg_seq;
2284
2285 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002286 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002287 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002288 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002289 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002290 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002291 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002292 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002293
2294 return skb->len;
2295}
2296
2297int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2298 struct netlink_callback *cb)
2299{
2300 struct net *net = sock_net(skb->sk);
2301 u32 prev_node = cb->args[1];
2302 u32 bearer_id = cb->args[2];
2303 int done = cb->args[0];
2304 struct tipc_nl_msg msg;
2305 int err;
2306
2307 if (!prev_node) {
2308 struct nlattr **attrs;
2309 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2310
2311 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2312 if (err)
2313 return err;
2314
2315 if (!attrs[TIPC_NLA_MON])
2316 return -EINVAL;
2317
2318 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2319 attrs[TIPC_NLA_MON],
Johannes Bergfceb6432017-04-12 14:34:07 +02002320 tipc_nl_monitor_policy, NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002321 if (err)
2322 return err;
2323
2324 if (!mon[TIPC_NLA_MON_REF])
2325 return -EINVAL;
2326
2327 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2328
2329 if (bearer_id >= MAX_BEARERS)
2330 return -EINVAL;
2331 }
2332
2333 if (done)
2334 return 0;
2335
2336 msg.skb = skb;
2337 msg.portid = NETLINK_CB(cb->skb).portid;
2338 msg.seq = cb->nlh->nlmsg_seq;
2339
2340 rtnl_lock();
2341 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2342 if (!err)
2343 done = 1;
2344
2345 rtnl_unlock();
2346 cb->args[0] = done;
2347 cb->args[1] = prev_node;
2348 cb->args[2] = bearer_id;
2349
2350 return skb->len;
2351}