blob: 2afc4f8c37a74db4896508283f434909a0151732 [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
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +020048#define NODE_CLEANUP_AFTER 300000
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050049
Jon Paul Maloy5be9c082015-11-19 14:30:45 -050050/* Flags used to take different actions according to flag type
51 * TIPC_NOTIFY_NODE_DOWN: notify node is down
52 * TIPC_NOTIFY_NODE_UP: notify node is up
53 * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
54 */
55enum {
56 TIPC_NOTIFY_NODE_DOWN = (1 << 3),
57 TIPC_NOTIFY_NODE_UP = (1 << 4),
58 TIPC_NOTIFY_LINK_UP = (1 << 6),
59 TIPC_NOTIFY_LINK_DOWN = (1 << 7)
60};
61
62struct tipc_link_entry {
63 struct tipc_link *link;
64 spinlock_t lock; /* per link */
65 u32 mtu;
66 struct sk_buff_head inputq;
67 struct tipc_media_addr maddr;
68};
69
70struct tipc_bclink_entry {
71 struct tipc_link *link;
72 struct sk_buff_head inputq1;
73 struct sk_buff_head arrvq;
74 struct sk_buff_head inputq2;
75 struct sk_buff_head namedq;
76};
77
78/**
79 * struct tipc_node - TIPC node structure
80 * @addr: network address of node
81 * @ref: reference counter to node object
82 * @lock: rwlock governing access to structure
83 * @net: the applicable net namespace
84 * @hash: links to adjacent nodes in unsorted hash chain
85 * @inputq: pointer to input queue containing messages for msg event
86 * @namedq: pointer to name table input queue with name table messages
87 * @active_links: bearer ids of active links, used as index into links[] array
88 * @links: array containing references to all links to node
89 * @action_flags: bit mask of different types of node actions
90 * @state: connectivity state vs peer node
91 * @sync_point: sequence number where synch/failover is finished
92 * @list: links to adjacent nodes in sorted list of cluster's nodes
93 * @working_links: number of working links to node (both active and standby)
94 * @link_cnt: number of links to node
95 * @capabilities: bitmap, indicating peer node's functional capabilities
96 * @signature: node instance identifier
97 * @link_id: local and remote bearer ids of changing link, if any
98 * @publ_list: list of publications
99 * @rcu: rcu struct for tipc_node
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200100 * @delete_at: indicates the time for deleting a down node
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500101 */
102struct tipc_node {
103 u32 addr;
104 struct kref kref;
105 rwlock_t lock;
106 struct net *net;
107 struct hlist_node hash;
108 int active_links[2];
109 struct tipc_link_entry links[MAX_BEARERS];
110 struct tipc_bclink_entry bc_entry;
111 int action_flags;
112 struct list_head list;
113 int state;
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200114 bool failover_sent;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500115 u16 sync_point;
116 int link_cnt;
117 u16 working_links;
118 u16 capabilities;
119 u32 signature;
120 u32 link_id;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100121 u8 peer_id[16];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500122 struct list_head publ_list;
123 struct list_head conn_sks;
124 unsigned long keepalive_intv;
125 struct timer_list timer;
126 struct rcu_head rcu;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200127 unsigned long delete_at;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500128};
129
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400130/* Node FSM states and events:
131 */
132enum {
133 SELF_DOWN_PEER_DOWN = 0xdd,
134 SELF_UP_PEER_UP = 0xaa,
135 SELF_DOWN_PEER_LEAVING = 0xd1,
136 SELF_UP_PEER_COMING = 0xac,
137 SELF_COMING_PEER_UP = 0xca,
138 SELF_LEAVING_PEER_DOWN = 0x1d,
139 NODE_FAILINGOVER = 0xf0,
140 NODE_SYNCHING = 0xcc
141};
142
143enum {
144 SELF_ESTABL_CONTACT_EVT = 0xece,
145 SELF_LOST_CONTACT_EVT = 0x1ce,
146 PEER_ESTABL_CONTACT_EVT = 0x9ece,
147 PEER_LOST_CONTACT_EVT = 0x91ce,
148 NODE_FAILOVER_BEGIN_EVT = 0xfbe,
149 NODE_FAILOVER_END_EVT = 0xfee,
150 NODE_SYNCH_BEGIN_EVT = 0xcbe,
151 NODE_SYNCH_END_EVT = 0xcee
152};
153
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400154static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
155 struct sk_buff_head *xmitq,
156 struct tipc_media_addr **maddr);
157static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
158 bool delete);
159static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800160static void tipc_node_delete(struct tipc_node *node);
Kees Cook31b102b2017-10-30 14:06:45 -0700161static void tipc_node_timeout(struct timer_list *t);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400162static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500163static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100164static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500165static void tipc_node_put(struct tipc_node *node);
Jon Maloy38077b82017-10-13 11:04:19 +0200166static bool node_is_up(struct tipc_node *n);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200167static void tipc_node_delete_from_list(struct tipc_node *node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400169struct tipc_sock_conn {
170 u32 port;
171 u32 peer_port;
172 u32 peer_node;
173 struct list_head list;
174};
175
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500176static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
177{
178 int bearer_id = n->active_links[sel & 1];
179
180 if (unlikely(bearer_id == INVALID_BEARER_ID))
181 return NULL;
182
183 return n->links[bearer_id].link;
184}
185
186int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
187{
188 struct tipc_node *n;
189 int bearer_id;
190 unsigned int mtu = MAX_MSG_SIZE;
191
192 n = tipc_node_find(net, addr);
193 if (unlikely(!n))
194 return mtu;
195
196 bearer_id = n->active_links[sel & 1];
197 if (likely(bearer_id != INVALID_BEARER_ID))
198 mtu = n->links[bearer_id].mtu;
199 tipc_node_put(n);
200 return mtu;
201}
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400202
Jon Maloy3e5cf362018-04-25 19:29:36 +0200203bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
204{
205 u8 *own_id = tipc_own_id(net);
206 struct tipc_node *n;
207
208 if (!own_id)
209 return true;
210
211 if (addr == tipc_own_addr(net)) {
212 memcpy(id, own_id, TIPC_NODEID_LEN);
213 return true;
214 }
215 n = tipc_node_find(net, addr);
216 if (!n)
217 return false;
218
219 memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
220 tipc_node_put(n);
221 return true;
222}
223
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400224u16 tipc_node_get_capabilities(struct net *net, u32 addr)
225{
226 struct tipc_node *n;
227 u16 caps;
228
229 n = tipc_node_find(net, addr);
230 if (unlikely(!n))
231 return TIPC_NODE_CAPABILITIES;
232 caps = n->capabilities;
233 tipc_node_put(n);
234 return caps;
235}
236
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800237static void tipc_node_kref_release(struct kref *kref)
238{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500239 struct tipc_node *n = container_of(kref, struct tipc_node, kref);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800240
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500241 kfree(n->bc_entry.link);
242 kfree_rcu(n, rcu);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800243}
244
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500245static void tipc_node_put(struct tipc_node *node)
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800246{
247 kref_put(&node->kref, tipc_node_kref_release);
248}
249
250static void tipc_node_get(struct tipc_node *node)
251{
252 kref_get(&node->kref);
253}
254
Allan Stephensa635b462011-11-04 11:54:43 -0400255/*
Allan Stephens672d99e2011-02-25 18:42:52 -0500256 * tipc_node_find - locate specified node object, if it exists
257 */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500258static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
Allan Stephens672d99e2011-02-25 18:42:52 -0500259{
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500260 struct tipc_net *tn = tipc_net(net);
Allan Stephens672d99e2011-02-25 18:42:52 -0500261 struct tipc_node *node;
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500262 unsigned int thash = tipc_hashfn(addr);
Allan Stephens672d99e2011-02-25 18:42:52 -0500263
Ying Xue6c7a7622014-03-27 12:54:37 +0800264 rcu_read_lock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500265 hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
266 if (node->addr != addr)
267 continue;
268 if (!kref_get_unless_zero(&node->kref))
269 node = NULL;
270 break;
Allan Stephens672d99e2011-02-25 18:42:52 -0500271 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800272 rcu_read_unlock();
Jon Paul Maloyb1709972016-02-24 11:00:19 -0500273 return node;
Allan Stephens672d99e2011-02-25 18:42:52 -0500274}
275
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100276/* tipc_node_find_by_id - locate specified node object by its 128-bit id
277 * Note: this function is called only when a discovery request failed
278 * to find the node by its 32-bit id, and is not time critical
279 */
280static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
281{
282 struct tipc_net *tn = tipc_net(net);
283 struct tipc_node *n;
284 bool found = false;
285
286 rcu_read_lock();
287 list_for_each_entry_rcu(n, &tn->node_list, list) {
288 read_lock_bh(&n->lock);
289 if (!memcmp(id, n->peer_id, 16) &&
290 kref_get_unless_zero(&n->kref))
291 found = true;
292 read_unlock_bh(&n->lock);
293 if (found)
294 break;
295 }
296 rcu_read_unlock();
297 return found ? n : NULL;
298}
299
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500300static void tipc_node_read_lock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500301{
302 read_lock_bh(&n->lock);
303}
304
Jon Paul Maloy5be9c082015-11-19 14:30:45 -0500305static void tipc_node_read_unlock(struct tipc_node *n)
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500306{
307 read_unlock_bh(&n->lock);
308}
309
310static void tipc_node_write_lock(struct tipc_node *n)
311{
312 write_lock_bh(&n->lock);
313}
314
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100315static void tipc_node_write_unlock_fast(struct tipc_node *n)
316{
317 write_unlock_bh(&n->lock);
318}
319
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500320static void tipc_node_write_unlock(struct tipc_node *n)
321{
322 struct net *net = n->net;
323 u32 addr = 0;
324 u32 flags = n->action_flags;
325 u32 link_id = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400326 u32 bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500327 struct list_head *publ_list;
328
329 if (likely(!flags)) {
330 write_unlock_bh(&n->lock);
331 return;
332 }
333
334 addr = n->addr;
335 link_id = n->link_id;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400336 bearer_id = link_id & 0xffff;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500337 publ_list = &n->publ_list;
338
339 n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
340 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
341
342 write_unlock_bh(&n->lock);
343
344 if (flags & TIPC_NOTIFY_NODE_DOWN)
345 tipc_publ_notify(net, publ_list, addr);
346
347 if (flags & TIPC_NOTIFY_NODE_UP)
348 tipc_named_node_up(net, addr);
349
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400350 if (flags & TIPC_NOTIFY_LINK_UP) {
351 tipc_mon_peer_up(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500352 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
Jon Maloy218527f2018-03-29 23:20:41 +0200353 TIPC_NODE_SCOPE, link_id, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400354 }
355 if (flags & TIPC_NOTIFY_LINK_DOWN) {
356 tipc_mon_peer_down(net, addr, bearer_id);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500357 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
Jon Maloy37922ea2018-03-29 23:20:43 +0200358 addr, link_id);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400359 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500360}
361
Wei Yongjune1a22d12018-03-26 14:33:13 +0000362static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
363 u8 *peer_id, u16 capabilities)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364{
Ying Xuef2f98002015-01-09 15:27:05 +0800365 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500366 struct tipc_node *n, *temp_node;
Jon Maloy9012de52018-07-10 01:07:35 +0200367 struct tipc_link *l;
368 int bearer_id;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500369 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370
Ying Xuef2f98002015-01-09 15:27:05 +0800371 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500372 n = tipc_node_find(net, addr);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400373 if (n) {
Jon Maloy40999f12018-07-18 19:50:06 +0200374 if (n->capabilities == capabilities)
375 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400376 /* Same node may come back with new capabilities */
Jon Maloy40999f12018-07-18 19:50:06 +0200377 write_lock_bh(&n->lock);
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400378 n->capabilities = capabilities;
Jon Maloy9012de52018-07-10 01:07:35 +0200379 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
380 l = n->links[bearer_id].link;
381 if (l)
382 tipc_link_update_caps(l, capabilities);
383 }
Jon Maloy40999f12018-07-18 19:50:06 +0200384 write_unlock_bh(&n->lock);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500385 goto exit;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400386 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500387 n = kzalloc(sizeof(*n), GFP_ATOMIC);
388 if (!n) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400389 pr_warn("Node creation failed, no memory\n");
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500390 goto exit;
Allan Stephensa10bd922006-06-25 23:52:17 -0700391 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500392 n->addr = addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100393 memcpy(&n->peer_id, peer_id, 16);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500394 n->net = net;
395 n->capabilities = capabilities;
396 kref_init(&n->kref);
397 rwlock_init(&n->lock);
398 INIT_HLIST_NODE(&n->hash);
399 INIT_LIST_HEAD(&n->list);
400 INIT_LIST_HEAD(&n->publ_list);
401 INIT_LIST_HEAD(&n->conn_sks);
402 skb_queue_head_init(&n->bc_entry.namedq);
403 skb_queue_head_init(&n->bc_entry.inputq1);
404 __skb_queue_head_init(&n->bc_entry.arrvq);
405 skb_queue_head_init(&n->bc_entry.inputq2);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500406 for (i = 0; i < MAX_BEARERS; i++)
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500407 spin_lock_init(&n->links[i].lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500408 n->state = SELF_DOWN_PEER_LEAVING;
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200409 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500410 n->signature = INVALID_NODE_SIG;
411 n->active_links[0] = INVALID_BEARER_ID;
412 n->active_links[1] = INVALID_BEARER_ID;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100413 if (!tipc_link_bc_create(net, tipc_own_addr(net),
414 addr, U16_MAX,
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500415 tipc_link_window(tipc_bc_sndlink(net)),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500416 n->capabilities,
417 &n->bc_entry.inputq1,
418 &n->bc_entry.namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400419 tipc_bc_sndlink(net),
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500420 &n->bc_entry.link)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -0400421 pr_warn("Broadcast rcv link creation failed, no memory\n");
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500422 kfree(n);
423 n = NULL;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400424 goto exit;
425 }
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500426 tipc_node_get(n);
Kees Cook31b102b2017-10-30 14:06:45 -0700427 timer_setup(&n->timer, tipc_node_timeout, 0);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500428 n->keepalive_intv = U32_MAX;
Jon Paul Maloyd5c91fb2016-02-10 16:14:57 -0500429 hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
430 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
431 if (n->addr < temp_node->addr)
432 break;
433 }
434 list_add_tail_rcu(&n->list, &temp_node->list);
Jon Paul Maloyb45db712015-02-03 08:59:19 -0500435exit:
Ying Xuef2f98002015-01-09 15:27:05 +0800436 spin_unlock_bh(&tn->node_list_lock);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500437 return n;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438}
439
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400440static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
441{
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500442 unsigned long tol = tipc_link_tolerance(l);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400443 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400444
445 /* Link with lowest tolerance determines timer interval */
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400446 if (intv < n->keepalive_intv)
447 n->keepalive_intv = intv;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400448
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400449 /* Ensure link's abort limit corresponds to current tolerance */
450 tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400451}
452
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200453static void tipc_node_delete_from_list(struct tipc_node *node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454{
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800455 list_del_rcu(&node->list);
456 hlist_del_rcu(&node->hash);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500457 tipc_node_put(node);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200458}
459
460static void tipc_node_delete(struct tipc_node *node)
461{
462 tipc_node_delete_from_list(node);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500463
464 del_timer_sync(&node->timer);
465 tipc_node_put(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466}
467
Ying Xuef2f98002015-01-09 15:27:05 +0800468void tipc_node_stop(struct net *net)
Ying Xue46651c52014-03-27 12:54:36 +0800469{
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500470 struct tipc_net *tn = tipc_net(net);
Ying Xue46651c52014-03-27 12:54:36 +0800471 struct tipc_node *node, *t_node;
472
Ying Xuef2f98002015-01-09 15:27:05 +0800473 spin_lock_bh(&tn->node_list_lock);
Jon Paul Maloyd25a01252016-02-24 11:10:48 -0500474 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
475 tipc_node_delete(node);
Ying Xuef2f98002015-01-09 15:27:05 +0800476 spin_unlock_bh(&tn->node_list_lock);
Ying Xue46651c52014-03-27 12:54:36 +0800477}
478
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500479void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
480{
481 struct tipc_node *n;
482
483 if (in_own_node(net, addr))
484 return;
485
486 n = tipc_node_find(net, addr);
487 if (!n) {
488 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
489 return;
490 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500491 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500492 list_add_tail(subscr, &n->publ_list);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100493 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500494 tipc_node_put(n);
495}
496
497void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
498{
499 struct tipc_node *n;
500
501 if (in_own_node(net, addr))
502 return;
503
504 n = tipc_node_find(net, addr);
505 if (!n) {
506 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
507 return;
508 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500509 tipc_node_write_lock(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500510 list_del_init(subscr);
Parthasarathy Bhuvaragan93f955a2017-01-24 13:00:43 +0100511 tipc_node_write_unlock_fast(n);
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -0500512 tipc_node_put(n);
513}
514
Ying Xuef2f98002015-01-09 15:27:05 +0800515int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400516{
517 struct tipc_node *node;
518 struct tipc_sock_conn *conn;
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800519 int err = 0;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400520
Ying Xue34747532015-01-09 15:27:10 +0800521 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400522 return 0;
523
Ying Xuef2f98002015-01-09 15:27:05 +0800524 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400525 if (!node) {
526 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
527 return -EHOSTUNREACH;
528 }
529 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800530 if (!conn) {
531 err = -EHOSTUNREACH;
532 goto exit;
533 }
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400534 conn->peer_node = dnode;
535 conn->port = port;
536 conn->peer_port = peer_port;
537
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500538 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400539 list_add_tail(&conn->list, &node->conn_sks);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500540 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800541exit:
542 tipc_node_put(node);
543 return err;
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400544}
545
Ying Xuef2f98002015-01-09 15:27:05 +0800546void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400547{
548 struct tipc_node *node;
549 struct tipc_sock_conn *conn, *safe;
550
Ying Xue34747532015-01-09 15:27:10 +0800551 if (in_own_node(net, dnode))
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400552 return;
553
Ying Xuef2f98002015-01-09 15:27:05 +0800554 node = tipc_node_find(net, dnode);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400555 if (!node)
556 return;
557
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500558 tipc_node_write_lock(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400559 list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
560 if (port != conn->port)
561 continue;
562 list_del(&conn->list);
563 kfree(conn);
564 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500565 tipc_node_write_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800566 tipc_node_put(node);
Jon Paul Maloy02be61a2014-08-22 18:09:08 -0400567}
568
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200569static void tipc_node_clear_links(struct tipc_node *node)
570{
571 int i;
572
573 for (i = 0; i < MAX_BEARERS; i++) {
574 struct tipc_link_entry *le = &node->links[i];
575
576 if (le->link) {
577 kfree(le->link);
578 le->link = NULL;
579 node->link_cnt--;
580 }
581 }
582}
583
584/* tipc_node_cleanup - delete nodes that does not
585 * have active links for NODE_CLEANUP_AFTER time
586 */
587static int tipc_node_cleanup(struct tipc_node *peer)
588{
589 struct tipc_net *tn = tipc_net(peer->net);
590 bool deleted = false;
591
592 spin_lock_bh(&tn->node_list_lock);
593 tipc_node_write_lock(peer);
594
595 if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
596 tipc_node_clear_links(peer);
597 tipc_node_delete_from_list(peer);
598 deleted = true;
599 }
600 tipc_node_write_unlock(peer);
601 spin_unlock_bh(&tn->node_list_lock);
602 return deleted;
603}
604
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400605/* tipc_node_timeout - handle expiration of node timer
606 */
Kees Cook31b102b2017-10-30 14:06:45 -0700607static void tipc_node_timeout(struct timer_list *t)
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400608{
Kees Cook31b102b2017-10-30 14:06:45 -0700609 struct tipc_node *n = from_timer(n, t, timer);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400610 struct tipc_link_entry *le;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400611 struct sk_buff_head xmitq;
Tung Nguyen759f29b2018-06-28 22:39:25 +0200612 int remains = n->link_cnt;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400613 int bearer_id;
614 int rc = 0;
615
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +0200616 if (!node_is_up(n) && tipc_node_cleanup(n)) {
617 /*Removing the reference of Timer*/
618 tipc_node_put(n);
619 return;
620 }
621
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400622 __skb_queue_head_init(&xmitq);
623
Tung Nguyen759f29b2018-06-28 22:39:25 +0200624 for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500625 tipc_node_read_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400626 le = &n->links[bearer_id];
627 if (le->link) {
Tung Nguyen759f29b2018-06-28 22:39:25 +0200628 spin_lock_bh(&le->lock);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400629 /* Link tolerance may change asynchronously: */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400630 tipc_node_calculate_timer(n, le->link);
631 rc = tipc_link_timeout(le->link, &xmitq);
Tung Nguyen759f29b2018-06-28 22:39:25 +0200632 spin_unlock_bh(&le->lock);
633 remains--;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400634 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500635 tipc_node_read_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400636 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
637 if (rc & TIPC_LINK_DOWN_EVT)
638 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400639 }
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400640 mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400641}
642
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400644 * __tipc_node_link_up - handle addition of link
645 * Node lock must be held by caller
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 * Link becomes active (alone or shared) or standby, depending on its priority.
647 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400648static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
649 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400651 int *slot0 = &n->active_links[0];
652 int *slot1 = &n->active_links[1];
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400653 struct tipc_link *ol = node_active_link(n, 0);
654 struct tipc_link *nl = n->links[bearer_id].link;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655
Jon Paul Maloye7142c32016-05-11 19:15:45 -0400656 if (!nl || tipc_link_is_up(nl))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400657 return;
658
659 tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
660 if (!tipc_link_is_up(nl))
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400661 return;
662
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400663 n->working_links++;
664 n->action_flags |= TIPC_NOTIFY_LINK_UP;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500665 n->link_id = tipc_link_id(nl);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400666
667 /* Leave room for tunnel header when returning 'mtu' to users: */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500668 n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
Ying Xue7b8613e2014-10-20 14:44:25 +0800669
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400670 tipc_bearer_add_dest(n->net, bearer_id, n->addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400671 tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400672
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100673 pr_debug("Established link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500674 tipc_link_name(nl), tipc_link_plane(nl));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -0400676 /* Ensure that a STATE message goes first */
677 tipc_link_build_state_msg(nl, xmitq);
678
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400679 /* First link? => give it both slots */
680 if (!ol) {
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400681 *slot0 = bearer_id;
682 *slot1 = bearer_id;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400683 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200684 n->failover_sent = false;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400685 n->action_flags |= TIPC_NOTIFY_NODE_UP;
Jon Paul Maloydef22c42016-04-28 20:16:08 -0400686 tipc_link_set_active(nl, true);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400687 tipc_bcast_add_peer(n->net, nl, xmitq);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400688 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 }
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400690
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400691 /* Second link => redistribute slots */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500692 if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
693 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400694 *slot0 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400695 *slot1 = bearer_id;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400696 tipc_link_set_active(nl, true);
697 tipc_link_set_active(ol, false);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500698 } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400699 tipc_link_set_active(nl, true);
Jon Paul Maloyc49a0a842015-10-22 08:51:47 -0400700 *slot1 = bearer_id;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400701 } else {
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500702 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400705 /* Prepare synchronization with first link */
706 tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707}
708
709/**
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400710 * tipc_node_link_up - handle addition of link
711 *
712 * Link becomes active (alone or shared) or standby, depending on its priority.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400714static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
715 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716{
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400717 struct tipc_media_addr *maddr;
718
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500719 tipc_node_write_lock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400720 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloyde7e07f2016-04-15 13:33:06 -0400721 maddr = &n->links[bearer_id].maddr;
722 tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500723 tipc_node_write_unlock(n);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400724}
725
726/**
727 * __tipc_node_link_down - handle loss of link
728 */
729static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
730 struct sk_buff_head *xmitq,
731 struct tipc_media_addr **maddr)
732{
733 struct tipc_link_entry *le = &n->links[*bearer_id];
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400734 int *slot0 = &n->active_links[0];
735 int *slot1 = &n->active_links[1];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500736 int i, highest = 0, prio;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400737 struct tipc_link *l, *_l, *tnl;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400739 l = n->links[*bearer_id].link;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400740 if (!l || tipc_link_is_reset(l))
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400741 return;
742
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400743 n->working_links--;
744 n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500745 n->link_id = tipc_link_id(l);
Allan Stephens5392d642006-06-25 23:52:50 -0700746
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400747 tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400748
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100749 pr_debug("Lost link <%s> on network plane %c\n",
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500750 tipc_link_name(l), tipc_link_plane(l));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400752 /* Select new active link if any available */
753 *slot0 = INVALID_BEARER_ID;
754 *slot1 = INVALID_BEARER_ID;
755 for (i = 0; i < MAX_BEARERS; i++) {
756 _l = n->links[i].link;
757 if (!_l || !tipc_link_is_up(_l))
758 continue;
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400759 if (_l == l)
760 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500761 prio = tipc_link_prio(_l);
762 if (prio < highest)
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400763 continue;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500764 if (prio > highest) {
765 highest = prio;
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400766 *slot0 = i;
767 *slot1 = i;
768 continue;
769 }
770 *slot1 = i;
771 }
Jon Paul Maloy655fb242015-07-30 18:24:17 -0400772
Jon Maloy38077b82017-10-13 11:04:19 +0200773 if (!node_is_up(n)) {
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400774 if (tipc_link_peer_is_down(l))
775 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
776 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
777 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400778 tipc_link_reset(l);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400779 tipc_link_build_reset_msg(l, xmitq);
780 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400781 node_lost_contact(n, &le->inputq);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400782 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400783 return;
784 }
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400785 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400786
787 /* There is still a working link => initiate failover */
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500788 *bearer_id = n->active_links[0];
789 tnl = n->links[*bearer_id].link;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400790 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
791 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500792 n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400793 tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400794 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400795 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400796 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400797 tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500798 *maddr = &n->links[*bearer_id].maddr;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400799}
800
801static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
802{
803 struct tipc_link_entry *le = &n->links[bearer_id];
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400804 struct tipc_link *l = le->link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400805 struct tipc_media_addr *maddr;
806 struct sk_buff_head xmitq;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400807 int old_bearer_id = bearer_id;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400808
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400809 if (!l)
810 return;
811
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400812 __skb_queue_head_init(&xmitq);
813
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500814 tipc_node_write_lock(n);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400815 if (!tipc_link_is_establishing(l)) {
816 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
817 if (delete) {
818 kfree(l);
819 le->link = NULL;
820 n->link_cnt--;
821 }
822 } else {
823 /* Defuse pending tipc_node_link_up() */
824 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400825 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500826 tipc_node_write_unlock(n);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400827 if (delete)
828 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400829 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
830 tipc_sk_rcv(n->net, &le->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831}
832
Jon Maloy38077b82017-10-13 11:04:19 +0200833static bool node_is_up(struct tipc_node *n)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834{
Jon Paul Maloy36e78a42015-07-16 16:54:22 -0400835 return n->active_links[0] != INVALID_BEARER_ID;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836}
837
Jon Maloy38077b82017-10-13 11:04:19 +0200838bool tipc_node_is_up(struct net *net, u32 addr)
839{
840 struct tipc_node *n;
841 bool retval = false;
842
843 if (in_own_node(net, addr))
844 return true;
845
846 n = tipc_node_find(net, addr);
847 if (!n)
848 return false;
849 retval = node_is_up(n);
850 tipc_node_put(n);
851 return retval;
852}
853
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100854static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
855{
856 struct tipc_node *n;
857
858 addr ^= tipc_net(net)->random;
859 while ((n = tipc_node_find(net, addr))) {
860 tipc_node_put(n);
861 addr++;
862 }
863 return addr;
864}
865
866/* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
Jon Maloy2a57f182018-07-06 20:10:03 +0200867 * Returns suggested address if any, otherwise 0
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100868 */
869u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
870{
871 struct tipc_net *tn = tipc_net(net);
872 struct tipc_node *n;
873
874 /* Suggest new address if some other peer is using this one */
875 n = tipc_node_find(net, addr);
876 if (n) {
877 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
878 addr = 0;
879 tipc_node_put(n);
880 if (!addr)
881 return 0;
882 return tipc_node_suggest_addr(net, addr);
883 }
884
885 /* Suggest previously used address if peer is known */
886 n = tipc_node_find_by_id(net, id);
887 if (n) {
888 addr = n->addr;
889 tipc_node_put(n);
Jon Maloy2a57f182018-07-06 20:10:03 +0200890 return addr;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100891 }
Jon Maloy2a57f182018-07-06 20:10:03 +0200892
893 /* Even this node may be in conflict */
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100894 if (tn->trial_addr == addr)
895 return tipc_node_suggest_addr(net, addr);
896
Jon Maloy2a57f182018-07-06 20:10:03 +0200897 return 0;
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100898}
899
900void tipc_node_check_dest(struct net *net, u32 addr,
901 u8 *peer_id, struct tipc_bearer *b,
Jon Paul Maloycf148812015-07-30 18:24:22 -0400902 u16 capabilities, u32 signature,
903 struct tipc_media_addr *maddr,
904 bool *respond, bool *dupl_addr)
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400905{
Jon Paul Maloycf148812015-07-30 18:24:22 -0400906 struct tipc_node *n;
907 struct tipc_link *l;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400908 struct tipc_link_entry *le;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400909 bool addr_match = false;
910 bool sign_match = false;
911 bool link_up = false;
912 bool accept_addr = false;
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400913 bool reset = true;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400914 char *if_name;
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -0400915 unsigned long intv;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +0200916 u16 session;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400917
Jon Paul Maloycf148812015-07-30 18:24:22 -0400918 *dupl_addr = false;
919 *respond = false;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400920
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100921 n = tipc_node_create(net, addr, peer_id, capabilities);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400922 if (!n)
923 return;
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -0400924
Jon Paul Maloy5405ff62015-11-19 14:30:44 -0500925 tipc_node_write_lock(n);
Jon Paul Maloycf148812015-07-30 18:24:22 -0400926
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400927 le = &n->links[b->identity];
Jon Paul Maloycf148812015-07-30 18:24:22 -0400928
929 /* Prepare to validate requesting node's signature and media address */
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400930 l = le->link;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400931 link_up = l && tipc_link_is_up(l);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400932 addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -0400933 sign_match = (signature == n->signature);
934
935 /* These three flags give us eight permutations: */
936
937 if (sign_match && addr_match && link_up) {
938 /* All is fine. Do nothing. */
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400939 reset = false;
Jon Paul Maloycf148812015-07-30 18:24:22 -0400940 } else if (sign_match && addr_match && !link_up) {
941 /* Respond. The link will come up in due time */
942 *respond = true;
943 } else if (sign_match && !addr_match && link_up) {
944 /* Peer has changed i/f address without rebooting.
945 * If so, the link will reset soon, and the next
946 * discovery will be accepted. So we can ignore it.
947 * It may also be an cloned or malicious peer having
948 * chosen the same node address and signature as an
949 * existing one.
950 * Ignore requests until the link goes down, if ever.
951 */
952 *dupl_addr = true;
953 } else if (sign_match && !addr_match && !link_up) {
954 /* Peer link has changed i/f address without rebooting.
955 * It may also be a cloned or malicious peer; we can't
956 * distinguish between the two.
957 * The signature is correct, so we must accept.
958 */
959 accept_addr = true;
960 *respond = true;
961 } else if (!sign_match && addr_match && link_up) {
962 /* Peer node rebooted. Two possibilities:
963 * - Delayed re-discovery; this link endpoint has already
964 * reset and re-established contact with the peer, before
965 * receiving a discovery message from that node.
966 * (The peer happened to receive one from this node first).
967 * - The peer came back so fast that our side has not
968 * discovered it yet. Probing from this side will soon
969 * reset the link, since there can be no working link
970 * endpoint at the peer end, and the link will re-establish.
971 * Accept the signature, since it comes from a known peer.
972 */
973 n->signature = signature;
974 } else if (!sign_match && addr_match && !link_up) {
975 /* The peer node has rebooted.
976 * Accept signature, since it is a known peer.
977 */
978 n->signature = signature;
979 *respond = true;
980 } else if (!sign_match && !addr_match && link_up) {
981 /* Peer rebooted with new address, or a new/duplicate peer.
982 * Ignore until the link goes down, if ever.
983 */
984 *dupl_addr = true;
985 } else if (!sign_match && !addr_match && !link_up) {
986 /* Peer rebooted with new address, or it is a new peer.
987 * Accept signature and address.
988 */
989 n->signature = signature;
990 accept_addr = true;
991 *respond = true;
992 }
993
994 if (!accept_addr)
995 goto exit;
996
997 /* Now create new link if not already existing */
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400998 if (!l) {
Jon Maloy20263642018-03-22 20:42:47 +0100999 if (n->link_cnt == 2)
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001000 goto exit;
Jon Maloy20263642018-03-22 20:42:47 +01001001
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001002 if_name = strchr(b->name, ':') + 1;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001003 get_random_bytes(&session, sizeof(u16));
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001004 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
Jon Paul Maloy0e054982015-10-22 08:51:36 -04001005 b->net_plane, b->mtu, b->priority,
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001006 b->window, session,
Jon Maloy25b0b9c2018-03-22 20:42:51 +01001007 tipc_own_addr(net), addr, peer_id,
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -04001008 n->capabilities,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001009 tipc_bc_sndlink(n->net), n->bc_entry.link,
1010 &le->inputq,
1011 &n->bc_entry.namedq, &l)) {
Jon Paul Maloycf148812015-07-30 18:24:22 -04001012 *respond = false;
1013 goto exit;
1014 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001015 tipc_link_reset(l);
Jon Paul Maloyc8199302015-10-15 14:52:46 -04001016 tipc_link_fsm_evt(l, LINK_RESET_EVT);
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001017 if (n->state == NODE_FAILINGOVER)
1018 tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001019 le->link = l;
1020 n->link_cnt++;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001021 tipc_node_calculate_timer(n, l);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001022 if (n->link_cnt == 1) {
1023 intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1024 if (!mod_timer(&n->timer, intv))
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001025 tipc_node_get(n);
Jon Paul Maloy5ca509f2016-06-08 12:00:05 -04001026 }
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001027 }
Jon Paul Maloy440d8962015-07-30 18:24:26 -04001028 memcpy(&le->maddr, maddr, sizeof(*maddr));
Jon Paul Maloycf148812015-07-30 18:24:22 -04001029exit:
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001030 tipc_node_write_unlock(n);
Richard Alpe2837f392016-03-03 14:20:41 +01001031 if (reset && l && !tipc_link_is_reset(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001032 tipc_node_link_down(n, b->identity, false);
Jon Paul Maloycf148812015-07-30 18:24:22 -04001033 tipc_node_put(n);
Jon Paul Maloyd3a43b92015-07-16 16:54:20 -04001034}
1035
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001036void tipc_node_delete_links(struct net *net, int bearer_id)
1037{
1038 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001039 struct tipc_node *n;
1040
1041 rcu_read_lock();
1042 list_for_each_entry_rcu(n, &tn->node_list, list) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001043 tipc_node_link_down(n, bearer_id, true);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001044 }
1045 rcu_read_unlock();
1046}
1047
1048static void tipc_node_reset_links(struct tipc_node *n)
1049{
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001050 int i;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001051
Jon Maloyd50ccc22018-03-22 20:42:50 +01001052 pr_warn("Resetting all links to %x\n", n->addr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001053
1054 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001055 tipc_node_link_down(n, i, false);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001056 }
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001057}
1058
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001059/* tipc_node_fsm_evt - node finite state machine
1060 * Determines when contact is allowed with peer node
1061 */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001062static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001063{
1064 int state = n->state;
1065
1066 switch (state) {
1067 case SELF_DOWN_PEER_DOWN:
1068 switch (evt) {
1069 case SELF_ESTABL_CONTACT_EVT:
1070 state = SELF_UP_PEER_COMING;
1071 break;
1072 case PEER_ESTABL_CONTACT_EVT:
1073 state = SELF_COMING_PEER_UP;
1074 break;
1075 case SELF_LOST_CONTACT_EVT:
1076 case PEER_LOST_CONTACT_EVT:
1077 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001078 case NODE_SYNCH_END_EVT:
1079 case NODE_SYNCH_BEGIN_EVT:
1080 case NODE_FAILOVER_BEGIN_EVT:
1081 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001082 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001083 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001084 }
1085 break;
1086 case SELF_UP_PEER_UP:
1087 switch (evt) {
1088 case SELF_LOST_CONTACT_EVT:
1089 state = SELF_DOWN_PEER_LEAVING;
1090 break;
1091 case PEER_LOST_CONTACT_EVT:
1092 state = SELF_LEAVING_PEER_DOWN;
1093 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001094 case NODE_SYNCH_BEGIN_EVT:
1095 state = NODE_SYNCHING;
1096 break;
1097 case NODE_FAILOVER_BEGIN_EVT:
1098 state = NODE_FAILINGOVER;
1099 break;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001100 case SELF_ESTABL_CONTACT_EVT:
1101 case PEER_ESTABL_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001102 case NODE_SYNCH_END_EVT:
1103 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001104 break;
1105 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001106 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001107 }
1108 break;
1109 case SELF_DOWN_PEER_LEAVING:
1110 switch (evt) {
1111 case PEER_LOST_CONTACT_EVT:
1112 state = SELF_DOWN_PEER_DOWN;
1113 break;
1114 case SELF_ESTABL_CONTACT_EVT:
1115 case PEER_ESTABL_CONTACT_EVT:
1116 case SELF_LOST_CONTACT_EVT:
1117 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001118 case NODE_SYNCH_END_EVT:
1119 case NODE_SYNCH_BEGIN_EVT:
1120 case NODE_FAILOVER_BEGIN_EVT:
1121 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001122 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001123 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001124 }
1125 break;
1126 case SELF_UP_PEER_COMING:
1127 switch (evt) {
1128 case PEER_ESTABL_CONTACT_EVT:
1129 state = SELF_UP_PEER_UP;
1130 break;
1131 case SELF_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001132 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001133 break;
1134 case SELF_ESTABL_CONTACT_EVT:
1135 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001136 case NODE_SYNCH_END_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001137 case NODE_FAILOVER_BEGIN_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001138 break;
1139 case NODE_SYNCH_BEGIN_EVT:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001140 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001141 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001142 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001143 }
1144 break;
1145 case SELF_COMING_PEER_UP:
1146 switch (evt) {
1147 case SELF_ESTABL_CONTACT_EVT:
1148 state = SELF_UP_PEER_UP;
1149 break;
1150 case PEER_LOST_CONTACT_EVT:
Jon Paul Maloyc4282ca2016-06-08 12:00:04 -04001151 state = SELF_DOWN_PEER_DOWN;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001152 break;
1153 case SELF_LOST_CONTACT_EVT:
1154 case PEER_ESTABL_CONTACT_EVT:
1155 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001156 case NODE_SYNCH_END_EVT:
1157 case NODE_SYNCH_BEGIN_EVT:
1158 case NODE_FAILOVER_BEGIN_EVT:
1159 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001160 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001161 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001162 }
1163 break;
1164 case SELF_LEAVING_PEER_DOWN:
1165 switch (evt) {
1166 case SELF_LOST_CONTACT_EVT:
1167 state = SELF_DOWN_PEER_DOWN;
1168 break;
1169 case SELF_ESTABL_CONTACT_EVT:
1170 case PEER_ESTABL_CONTACT_EVT:
1171 case PEER_LOST_CONTACT_EVT:
1172 break;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001173 case NODE_SYNCH_END_EVT:
1174 case NODE_SYNCH_BEGIN_EVT:
1175 case NODE_FAILOVER_BEGIN_EVT:
1176 case NODE_FAILOVER_END_EVT:
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001177 default:
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001178 goto illegal_evt;
1179 }
1180 break;
1181 case NODE_FAILINGOVER:
1182 switch (evt) {
1183 case SELF_LOST_CONTACT_EVT:
1184 state = SELF_DOWN_PEER_LEAVING;
1185 break;
1186 case PEER_LOST_CONTACT_EVT:
1187 state = SELF_LEAVING_PEER_DOWN;
1188 break;
1189 case NODE_FAILOVER_END_EVT:
1190 state = SELF_UP_PEER_UP;
1191 break;
1192 case NODE_FAILOVER_BEGIN_EVT:
1193 case SELF_ESTABL_CONTACT_EVT:
1194 case PEER_ESTABL_CONTACT_EVT:
1195 break;
1196 case NODE_SYNCH_BEGIN_EVT:
1197 case NODE_SYNCH_END_EVT:
1198 default:
1199 goto illegal_evt;
1200 }
1201 break;
1202 case NODE_SYNCHING:
1203 switch (evt) {
1204 case SELF_LOST_CONTACT_EVT:
1205 state = SELF_DOWN_PEER_LEAVING;
1206 break;
1207 case PEER_LOST_CONTACT_EVT:
1208 state = SELF_LEAVING_PEER_DOWN;
1209 break;
1210 case NODE_SYNCH_END_EVT:
1211 state = SELF_UP_PEER_UP;
1212 break;
1213 case NODE_FAILOVER_BEGIN_EVT:
1214 state = NODE_FAILINGOVER;
1215 break;
1216 case NODE_SYNCH_BEGIN_EVT:
1217 case SELF_ESTABL_CONTACT_EVT:
1218 case PEER_ESTABL_CONTACT_EVT:
1219 break;
1220 case NODE_FAILOVER_END_EVT:
1221 default:
1222 goto illegal_evt;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001223 }
1224 break;
1225 default:
1226 pr_err("Unknown node fsm state %x\n", state);
1227 break;
1228 }
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001229 n->state = state;
Jon Paul Maloy66996b62015-07-30 18:24:18 -04001230 return;
1231
1232illegal_evt:
1233 pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001234}
1235
Jon Paul Maloy52666982015-10-22 08:51:41 -04001236static void node_lost_contact(struct tipc_node *n,
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001237 struct sk_buff_head *inputq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238{
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001239 struct tipc_sock_conn *conn, *safe;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001240 struct tipc_link *l;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001241 struct list_head *conns = &n->conn_sks;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001242 struct sk_buff *skb;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001243 uint i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244
Jon Maloyd50ccc22018-03-22 20:42:50 +01001245 pr_debug("Lost contact with %x\n", n->addr);
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001246 n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
Allan Stephensc5bd4d82011-04-07 11:58:08 -04001247
Jon Paul Maloy52666982015-10-22 08:51:41 -04001248 /* Clean up broadcast state */
Jon Paul Maloyb06b2812015-10-22 08:51:42 -04001249 tipc_bcast_remove_peer(n->net, n->bc_entry.link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001251 /* Abort any ongoing link failover */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001253 l = n->links[i].link;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001254 if (l)
1255 tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 }
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001257
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001258 /* Notify publications from this node */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001259 n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001260
1261 /* Notify sockets connected to node */
1262 list_for_each_entry_safe(conn, safe, conns, list) {
1263 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy52666982015-10-22 08:51:41 -04001264 SHORT_H_SIZE, 0, tipc_own_addr(n->net),
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001265 conn->peer_node, conn->port,
1266 conn->peer_port, TIPC_ERR_NO_NODE);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001267 if (likely(skb))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001268 skb_queue_tail(inputq, skb);
Jon Paul Maloy708ac322015-02-05 08:36:42 -05001269 list_del(&conn->list);
1270 kfree(conn);
1271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272}
1273
Erik Hugne78acb1f2014-04-24 16:26:47 +02001274/**
1275 * tipc_node_get_linkname - get the name of a link
1276 *
1277 * @bearer_id: id of the bearer
1278 * @node: peer node address
1279 * @linkname: link name output buffer
1280 *
1281 * Returns 0 on success
1282 */
Ying Xuef2f98002015-01-09 15:27:05 +08001283int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1284 char *linkname, size_t len)
Erik Hugne78acb1f2014-04-24 16:26:47 +02001285{
1286 struct tipc_link *link;
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001287 int err = -EINVAL;
Ying Xuef2f98002015-01-09 15:27:05 +08001288 struct tipc_node *node = tipc_node_find(net, addr);
Erik Hugne78acb1f2014-04-24 16:26:47 +02001289
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001290 if (!node)
1291 return err;
1292
1293 if (bearer_id >= MAX_BEARERS)
1294 goto exit;
1295
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001296 tipc_node_read_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001297 link = node->links[bearer_id].link;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001298 if (link) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001299 strncpy(linkname, tipc_link_name(link), len);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001300 err = 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001301 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001302 tipc_node_read_unlock(node);
Parthasarathy Bhuvaragan991ca842017-08-24 16:31:24 +02001303exit:
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001304 tipc_node_put(node);
1305 return err;
Erik Hugne78acb1f2014-04-24 16:26:47 +02001306}
Ying Xue9db9fdd2014-05-05 08:56:12 +08001307
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001308/* Caller should hold node lock for the passed node */
Richard Alped8182802014-11-24 11:10:29 +01001309static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001310{
1311 void *hdr;
1312 struct nlattr *attrs;
1313
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001314 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001315 NLM_F_MULTI, TIPC_NL_NODE_GET);
1316 if (!hdr)
1317 return -EMSGSIZE;
1318
1319 attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1320 if (!attrs)
1321 goto msg_full;
1322
1323 if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1324 goto attr_msg_full;
Jon Maloy38077b82017-10-13 11:04:19 +02001325 if (node_is_up(node))
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001326 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1327 goto attr_msg_full;
1328
1329 nla_nest_end(msg->skb, attrs);
1330 genlmsg_end(msg->skb, hdr);
1331
1332 return 0;
1333
1334attr_msg_full:
1335 nla_nest_cancel(msg->skb, attrs);
1336msg_full:
1337 genlmsg_cancel(msg->skb, hdr);
1338
1339 return -EMSGSIZE;
1340}
1341
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001342/**
1343 * tipc_node_xmit() is the general link level function for message sending
1344 * @net: the applicable net namespace
1345 * @list: chain of buffers containing message
1346 * @dnode: address of destination node
1347 * @selector: a number used for deterministic link selection
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001348 * Consumes the buffer chain.
Richard Alpe4952cd32016-02-11 10:43:15 +01001349 * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001350 */
1351int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1352 u32 dnode, int selector)
1353{
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001354 struct tipc_link_entry *le = NULL;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001355 struct tipc_node *n;
1356 struct sk_buff_head xmitq;
Richard Alpe4952cd32016-02-11 10:43:15 +01001357 int bearer_id;
1358 int rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001359
Richard Alpe4952cd32016-02-11 10:43:15 +01001360 if (in_own_node(net, dnode)) {
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001361 tipc_sk_rcv(net, list);
1362 return 0;
1363 }
Richard Alpe4952cd32016-02-11 10:43:15 +01001364
1365 n = tipc_node_find(net, dnode);
1366 if (unlikely(!n)) {
1367 skb_queue_purge(list);
1368 return -EHOSTUNREACH;
1369 }
1370
1371 tipc_node_read_lock(n);
1372 bearer_id = n->active_links[selector & 1];
1373 if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1374 tipc_node_read_unlock(n);
1375 tipc_node_put(n);
1376 skb_queue_purge(list);
1377 return -EHOSTUNREACH;
1378 }
1379
1380 __skb_queue_head_init(&xmitq);
1381 le = &n->links[bearer_id];
1382 spin_lock_bh(&le->lock);
1383 rc = tipc_link_xmit(le->link, list, &xmitq);
1384 spin_unlock_bh(&le->lock);
1385 tipc_node_read_unlock(n);
1386
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001387 if (unlikely(rc == -ENOBUFS))
Richard Alpe4952cd32016-02-11 10:43:15 +01001388 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001389 else
1390 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
Richard Alpe4952cd32016-02-11 10:43:15 +01001391
1392 tipc_node_put(n);
1393
Jon Paul Maloydc8d1eb2015-12-02 15:19:37 -05001394 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001395}
1396
1397/* tipc_node_xmit_skb(): send single buffer to destination
1398 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1399 * messages, which will not be rejected
1400 * The only exception is datagram messages rerouted after secondary
1401 * lookup, which are rare and safe to dispose of anyway.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001402 */
1403int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1404 u32 selector)
1405{
1406 struct sk_buff_head head;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001407
1408 skb_queue_head_init(&head);
1409 __skb_queue_tail(&head, skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001410 tipc_node_xmit(net, &head, dnode, selector);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001411 return 0;
1412}
1413
Jon Maloyf70d37b2017-10-13 11:04:21 +02001414/* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1415 * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1416 */
1417int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1418{
1419 struct sk_buff *skb;
1420 u32 selector, dnode;
1421
1422 while ((skb = __skb_dequeue(xmitq))) {
1423 selector = msg_origport(buf_msg(skb));
1424 dnode = msg_destnode(buf_msg(skb));
1425 tipc_node_xmit_skb(net, skb, dnode, selector);
1426 }
1427 return 0;
1428}
1429
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001430void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1431{
1432 struct sk_buff *txskb;
1433 struct tipc_node *n;
1434 u32 dst;
1435
1436 rcu_read_lock();
1437 list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1438 dst = n->addr;
1439 if (in_own_node(net, dst))
1440 continue;
Jon Maloy38077b82017-10-13 11:04:19 +02001441 if (!node_is_up(n))
Jon Paul Maloy1d7e1c22015-11-19 14:30:42 -05001442 continue;
1443 txskb = pskb_copy(skb, GFP_ATOMIC);
1444 if (!txskb)
1445 break;
1446 msg_set_destnode(buf_msg(txskb), dst);
1447 tipc_node_xmit_skb(net, txskb, dst, 0);
1448 }
1449 rcu_read_unlock();
1450
1451 kfree_skb(skb);
1452}
1453
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001454static void tipc_node_mcast_rcv(struct tipc_node *n)
1455{
1456 struct tipc_bclink_entry *be = &n->bc_entry;
1457
1458 /* 'arrvq' is under inputq2's lock protection */
1459 spin_lock_bh(&be->inputq2.lock);
1460 spin_lock_bh(&be->inputq1.lock);
1461 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1462 spin_unlock_bh(&be->inputq1.lock);
1463 spin_unlock_bh(&be->inputq2.lock);
1464 tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1465}
1466
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001467static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1468 int bearer_id, struct sk_buff_head *xmitq)
1469{
1470 struct tipc_link *ucl;
1471 int rc;
1472
1473 rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1474
1475 if (rc & TIPC_LINK_DOWN_EVT) {
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001476 tipc_node_reset_links(n);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001477 return;
1478 }
1479
1480 if (!(rc & TIPC_LINK_SND_STATE))
1481 return;
1482
1483 /* If probe message, a STATE response will be sent anyway */
1484 if (msg_probe(hdr))
1485 return;
1486
1487 /* Produce a STATE message carrying broadcast NACK */
1488 tipc_node_read_lock(n);
1489 ucl = n->links[bearer_id].link;
1490 if (ucl)
1491 tipc_link_build_state_msg(ucl, xmitq);
1492 tipc_node_read_unlock(n);
1493}
1494
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001495/**
Jon Paul Maloy52666982015-10-22 08:51:41 -04001496 * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1497 * @net: the applicable net namespace
1498 * @skb: TIPC packet
1499 * @bearer_id: id of bearer message arrived on
1500 *
1501 * Invoked with no locks held.
1502 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001503static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001504{
1505 int rc;
1506 struct sk_buff_head xmitq;
1507 struct tipc_bclink_entry *be;
1508 struct tipc_link_entry *le;
1509 struct tipc_msg *hdr = buf_msg(skb);
1510 int usr = msg_user(hdr);
1511 u32 dnode = msg_destnode(hdr);
1512 struct tipc_node *n;
1513
1514 __skb_queue_head_init(&xmitq);
1515
1516 /* If NACK for other node, let rcv link for that node peek into it */
1517 if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1518 n = tipc_node_find(net, dnode);
1519 else
1520 n = tipc_node_find(net, msg_prevnode(hdr));
1521 if (!n) {
1522 kfree_skb(skb);
1523 return;
1524 }
1525 be = &n->bc_entry;
1526 le = &n->links[bearer_id];
1527
1528 rc = tipc_bcast_rcv(net, be->link, skb);
1529
Jon Paul Maloy52666982015-10-22 08:51:41 -04001530 /* Broadcast ACKs are sent on a unicast link */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001531 if (rc & TIPC_LINK_SND_STATE) {
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001532 tipc_node_read_lock(n);
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001533 tipc_link_build_state_msg(le->link, &xmitq);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001534 tipc_node_read_unlock(n);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001535 }
1536
1537 if (!skb_queue_empty(&xmitq))
1538 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1539
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001540 if (!skb_queue_empty(&be->inputq1))
1541 tipc_node_mcast_rcv(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001542
Jon Paul Maloy40501f92017-08-21 17:59:30 +02001543 /* If reassembly or retransmission failure => reset all links to peer */
1544 if (rc & TIPC_LINK_DOWN_EVT)
1545 tipc_node_reset_links(n);
Jon Paul Maloy1fc07f32016-07-11 16:08:37 -04001546
Jon Paul Maloy52666982015-10-22 08:51:41 -04001547 tipc_node_put(n);
1548}
1549
1550/**
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001551 * tipc_node_check_state - check and if necessary update node state
1552 * @skb: TIPC packet
1553 * @bearer_id: identity of bearer delivering the packet
Jon Maloy7ea817f2018-07-10 01:07:36 +02001554 * Returns true if state and msg are ok, otherwise false
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001555 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001556static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001557 int bearer_id, struct sk_buff_head *xmitq)
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001558{
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001559 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001560 int usr = msg_user(hdr);
1561 int mtyp = msg_type(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001562 u16 oseqno = msg_seqno(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001563 u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1564 u16 exp_pkts = msg_msgcnt(hdr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001565 u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001566 int state = n->state;
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001567 struct tipc_link *l, *tnl, *pl = NULL;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001568 struct tipc_media_addr *maddr;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001569 int pb_id;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001570
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001571 l = n->links[bearer_id].link;
1572 if (!l)
1573 return false;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001574 rcv_nxt = tipc_link_rcv_nxt(l);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001575
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001576
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001577 if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1578 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001579
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001580 /* Find parallel link, if any */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001581 for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1582 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1583 pl = n->links[pb_id].link;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001584 break;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001585 }
1586 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001587
Jon Maloy7ea817f2018-07-10 01:07:36 +02001588 if (!tipc_link_validate_msg(l, hdr))
1589 return false;
1590
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001591 /* Check and update node accesibility if applicable */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001592 if (state == SELF_UP_PEER_COMING) {
1593 if (!tipc_link_is_up(l))
1594 return true;
1595 if (!msg_peer_link_is_up(hdr))
1596 return true;
1597 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1598 }
1599
1600 if (state == SELF_DOWN_PEER_LEAVING) {
1601 if (msg_peer_node_is_up(hdr))
1602 return false;
1603 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001604 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001605 }
1606
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001607 if (state == SELF_LEAVING_PEER_DOWN)
1608 return false;
1609
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001610 /* Ignore duplicate packets */
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001611 if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001612 return true;
1613
1614 /* Initiate or update failover mode if applicable */
1615 if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1616 syncpt = oseqno + exp_pkts - 1;
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001617 if (pl && tipc_link_is_up(pl)) {
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001618 __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001619 tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1620 tipc_link_inputq(l));
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001621 }
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001622 /* If parallel link was already down, and this happened before
1623 * the tunnel link came up, FAILOVER was never sent. Ensure that
1624 * FAILOVER is sent to get peer out of NODE_FAILINGOVER state.
1625 */
1626 if (n->state != NODE_FAILINGOVER && !n->failover_sent) {
1627 tipc_link_create_dummy_tnl_msg(l, xmitq);
1628 n->failover_sent = true;
1629 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001630 /* If pkts arrive out of order, use lowest calculated syncpt */
1631 if (less(syncpt, n->sync_point))
1632 n->sync_point = syncpt;
1633 }
1634
1635 /* Open parallel link when tunnel link reaches synch point */
Jon Paul Maloy17b20632015-08-20 02:12:54 -04001636 if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001637 if (!more(rcv_nxt, n->sync_point))
1638 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001639 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1640 if (pl)
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001641 tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001642 return true;
1643 }
1644
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001645 /* No synching needed if only one link */
1646 if (!pl || !tipc_link_is_up(pl))
1647 return true;
1648
Jon Paul Maloy0f8b8e22015-10-13 12:41:51 -04001649 /* Initiate synch mode if applicable */
1650 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001651 syncpt = iseqno + exp_pkts - 1;
Jon Paul Maloyed435942017-08-08 22:23:56 +02001652 if (!tipc_link_is_up(l))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001653 __tipc_node_link_up(n, bearer_id, xmitq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001654 if (n->state == SELF_UP_PEER_UP) {
1655 n->sync_point = syncpt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001656 tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001657 tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1658 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001659 }
1660
1661 /* Open tunnel link when parallel link reaches synch point */
Jon Paul Maloy5c10e972015-11-19 14:30:41 -05001662 if (n->state == NODE_SYNCHING) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001663 if (tipc_link_is_synching(l)) {
1664 tnl = l;
1665 } else {
1666 tnl = pl;
1667 pl = l;
1668 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001669 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1670 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -04001671 if (more(dlv_nxt, n->sync_point)) {
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001672 tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001673 tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001674 return true;
1675 }
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001676 if (l == pl)
1677 return true;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001678 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1679 return true;
1680 if (usr == LINK_PROTOCOL)
1681 return true;
1682 return false;
1683 }
1684 return true;
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001685}
1686
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001687/**
1688 * tipc_rcv - process TIPC packets/messages arriving from off-node
1689 * @net: the applicable net namespace
1690 * @skb: TIPC packet
1691 * @bearer: pointer to bearer message arrived on
1692 *
1693 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1694 * structure (i.e. cannot be NULL), but bearer can be inactive.
1695 */
1696void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1697{
1698 struct sk_buff_head xmitq;
1699 struct tipc_node *n;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001700 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001701 int bearer_id = b->identity;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001702 struct tipc_link_entry *le;
Hamish Martinefe79052016-04-29 10:40:24 -04001703 u32 self = tipc_own_addr(net);
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001704 int usr, rc = 0;
1705 u16 bc_ack;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001706
1707 __skb_queue_head_init(&xmitq);
1708
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001709 /* Ensure message is well-formed before touching the header */
Jon Maloyd618d092017-11-15 21:23:56 +01001710 if (unlikely(!tipc_msg_validate(&skb)))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001711 goto discard;
Jon Paul Maloy681a55d2017-02-23 11:10:31 -05001712 hdr = buf_msg(skb);
1713 usr = msg_user(hdr);
1714 bc_ack = msg_bcast_ack(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001715
Jon Paul Maloy52666982015-10-22 08:51:41 -04001716 /* Handle arrival of discovery or broadcast packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001717 if (unlikely(msg_non_seq(hdr))) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04001718 if (unlikely(usr == LINK_CONFIG))
1719 return tipc_disc_rcv(net, skb, b);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001720 else
Jon Paul Maloy52666982015-10-22 08:51:41 -04001721 return tipc_node_bc_rcv(net, skb, bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001722 }
1723
Hamish Martinefe79052016-04-29 10:40:24 -04001724 /* Discard unicast link messages destined for another node */
1725 if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1726 goto discard;
1727
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001728 /* Locate neighboring node that sent packet */
1729 n = tipc_node_find(net, msg_prevnode(hdr));
1730 if (unlikely(!n))
1731 goto discard;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001732 le = &n->links[bearer_id];
1733
Jon Paul Maloy52666982015-10-22 08:51:41 -04001734 /* Ensure broadcast reception is in synch with peer's send state */
1735 if (unlikely(usr == LINK_PROTOCOL))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001736 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001737 else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001738 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001739
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001740 /* Receive packet directly if conditions permit */
1741 tipc_node_read_lock(n);
1742 if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001743 spin_lock_bh(&le->lock);
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001744 if (le->link) {
1745 rc = tipc_link_rcv(le->link, skb, &xmitq);
1746 skb = NULL;
1747 }
Jon Paul Maloy2312bf62015-11-19 14:30:43 -05001748 spin_unlock_bh(&le->lock);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001749 }
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001750 tipc_node_read_unlock(n);
1751
1752 /* Check/update node state before receiving */
1753 if (unlikely(skb)) {
Parthasarathy Bhuvaragan27163132017-08-24 16:31:22 +02001754 if (unlikely(skb_linearize(skb)))
1755 goto discard;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001756 tipc_node_write_lock(n);
1757 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1758 if (le->link) {
1759 rc = tipc_link_rcv(le->link, skb, &xmitq);
1760 skb = NULL;
1761 }
1762 }
1763 tipc_node_write_unlock(n);
1764 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001765
1766 if (unlikely(rc & TIPC_LINK_UP_EVT))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001767 tipc_node_link_up(n, bearer_id, &xmitq);
1768
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001769 if (unlikely(rc & TIPC_LINK_DOWN_EVT))
Jon Paul Maloy598411d2015-07-30 18:24:23 -04001770 tipc_node_link_down(n, bearer_id, false);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001771
Jon Paul Maloy52666982015-10-22 08:51:41 -04001772 if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1773 tipc_named_rcv(net, &n->bc_entry.namedq);
Jon Paul Maloy23d83352015-07-30 18:24:24 -04001774
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001775 if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1776 tipc_node_mcast_rcv(n);
1777
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001778 if (!skb_queue_empty(&le->inputq))
1779 tipc_sk_rcv(net, &le->inputq);
1780
1781 if (!skb_queue_empty(&xmitq))
1782 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1783
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001784 tipc_node_put(n);
1785discard:
1786 kfree_skb(skb);
1787}
1788
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001789void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1790 int prop)
Jon Maloy37c64cf2018-02-14 13:34:39 +01001791{
1792 struct tipc_net *tn = tipc_net(net);
1793 int bearer_id = b->identity;
1794 struct sk_buff_head xmitq;
1795 struct tipc_link_entry *e;
1796 struct tipc_node *n;
1797
1798 __skb_queue_head_init(&xmitq);
1799
1800 rcu_read_lock();
1801
1802 list_for_each_entry_rcu(n, &tn->node_list, list) {
1803 tipc_node_write_lock(n);
1804 e = &n->links[bearer_id];
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +02001805 if (e->link) {
1806 if (prop == TIPC_NLA_PROP_TOL)
1807 tipc_link_set_tolerance(e->link, b->tolerance,
1808 &xmitq);
1809 else if (prop == TIPC_NLA_PROP_MTU)
1810 tipc_link_set_mtu(e->link, b->mtu);
1811 }
Jon Maloy37c64cf2018-02-14 13:34:39 +01001812 tipc_node_write_unlock(n);
1813 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1814 }
1815
1816 rcu_read_unlock();
1817}
1818
Richard Alpeb3404022016-08-18 10:33:52 +02001819int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1820{
1821 struct net *net = sock_net(skb->sk);
1822 struct tipc_net *tn = net_generic(net, tipc_net_id);
1823 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1824 struct tipc_node *peer;
1825 u32 addr;
1826 int err;
Richard Alpeb3404022016-08-18 10:33:52 +02001827
1828 /* We identify the peer by its net */
1829 if (!info->attrs[TIPC_NLA_NET])
1830 return -EINVAL;
1831
1832 err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02001833 info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
Johannes Bergfe521452017-04-12 14:34:08 +02001834 info->extack);
Richard Alpeb3404022016-08-18 10:33:52 +02001835 if (err)
1836 return err;
1837
1838 if (!attrs[TIPC_NLA_NET_ADDR])
1839 return -EINVAL;
1840
1841 addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1842
1843 if (in_own_node(net, addr))
1844 return -ENOTSUPP;
1845
1846 spin_lock_bh(&tn->node_list_lock);
1847 peer = tipc_node_find(net, addr);
1848 if (!peer) {
1849 spin_unlock_bh(&tn->node_list_lock);
1850 return -ENXIO;
1851 }
1852
1853 tipc_node_write_lock(peer);
1854 if (peer->state != SELF_DOWN_PEER_DOWN &&
1855 peer->state != SELF_DOWN_PEER_LEAVING) {
1856 tipc_node_write_unlock(peer);
1857 err = -EBUSY;
1858 goto err_out;
1859 }
1860
GhantaKrishnamurthy MohanKrishna6a939f32018-06-29 13:23:41 +02001861 tipc_node_clear_links(peer);
Richard Alpeb3404022016-08-18 10:33:52 +02001862 tipc_node_write_unlock(peer);
1863 tipc_node_delete(peer);
1864
1865 err = 0;
1866err_out:
1867 tipc_node_put(peer);
1868 spin_unlock_bh(&tn->node_list_lock);
1869
1870 return err;
1871}
1872
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001873int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1874{
1875 int err;
Ying Xuef2f98002015-01-09 15:27:05 +08001876 struct net *net = sock_net(skb->sk);
1877 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001878 int done = cb->args[0];
1879 int last_addr = cb->args[1];
1880 struct tipc_node *node;
1881 struct tipc_nl_msg msg;
1882
1883 if (done)
1884 return 0;
1885
1886 msg.skb = skb;
1887 msg.portid = NETLINK_CB(cb->skb).portid;
1888 msg.seq = cb->nlh->nlmsg_seq;
1889
1890 rcu_read_lock();
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001891 if (last_addr) {
1892 node = tipc_node_find(net, last_addr);
1893 if (!node) {
1894 rcu_read_unlock();
1895 /* We never set seq or call nl_dump_check_consistent()
1896 * this means that setting prev_seq here will cause the
1897 * consistence check to fail in the netlink callback
1898 * handler. Resulting in the NLMSG_DONE message having
1899 * the NLM_F_DUMP_INTR flag set if the node state
1900 * changed while we released the lock.
1901 */
1902 cb->prev_seq = 1;
1903 return -EPIPE;
1904 }
1905 tipc_node_put(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001906 }
1907
Ying Xuef2f98002015-01-09 15:27:05 +08001908 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001909 if (last_addr) {
1910 if (node->addr == last_addr)
1911 last_addr = 0;
1912 else
1913 continue;
1914 }
1915
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001916 tipc_node_read_lock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001917 err = __tipc_nl_add_node(&msg, node);
1918 if (err) {
1919 last_addr = node->addr;
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001920 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001921 goto out;
1922 }
1923
Jon Paul Maloy5405ff62015-11-19 14:30:44 -05001924 tipc_node_read_unlock(node);
Richard Alpe3e4b6ab2014-11-20 10:29:17 +01001925 }
1926 done = 1;
1927out:
1928 cb->args[0] = done;
1929 cb->args[1] = last_addr;
1930 rcu_read_unlock();
1931
1932 return skb->len;
1933}
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001934
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001935/* tipc_node_find_by_name - locate owner node of link by link's name
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001936 * @net: the applicable net namespace
1937 * @name: pointer to link name string
1938 * @bearer_id: pointer to index in 'node->links' array where the link was found.
1939 *
1940 * Returns pointer to node owning the link, or 0 if no matching link is found.
1941 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001942static struct tipc_node *tipc_node_find_by_name(struct net *net,
1943 const char *link_name,
1944 unsigned int *bearer_id)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001945{
1946 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001947 struct tipc_link *l;
1948 struct tipc_node *n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001949 struct tipc_node *found_node = NULL;
1950 int i;
1951
1952 *bearer_id = 0;
1953 rcu_read_lock();
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001954 list_for_each_entry_rcu(n, &tn->node_list, list) {
1955 tipc_node_read_lock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001956 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001957 l = n->links[i].link;
1958 if (l && !strcmp(tipc_link_name(l), link_name)) {
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001959 *bearer_id = i;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001960 found_node = n;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001961 break;
1962 }
1963 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05001964 tipc_node_read_unlock(n);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001965 if (found_node)
1966 break;
1967 }
1968 rcu_read_unlock();
1969
1970 return found_node;
1971}
1972
1973int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1974{
1975 int err;
1976 int res = 0;
1977 int bearer_id;
1978 char *name;
1979 struct tipc_link *link;
1980 struct tipc_node *node;
Richard Alped01332f2016-02-01 08:19:56 +01001981 struct sk_buff_head xmitq;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001982 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1983 struct net *net = sock_net(skb->sk);
1984
Richard Alped01332f2016-02-01 08:19:56 +01001985 __skb_queue_head_init(&xmitq);
1986
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001987 if (!info->attrs[TIPC_NLA_LINK])
1988 return -EINVAL;
1989
1990 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1991 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02001992 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05001993 if (err)
1994 return err;
1995
1996 if (!attrs[TIPC_NLA_LINK_NAME])
1997 return -EINVAL;
1998
1999 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2000
2001 if (strcmp(name, tipc_bclink_name) == 0)
2002 return tipc_nl_bc_link_set(net, attrs);
2003
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002004 node = tipc_node_find_by_name(net, name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002005 if (!node)
2006 return -EINVAL;
2007
2008 tipc_node_read_lock(node);
2009
2010 link = node->links[bearer_id].link;
2011 if (!link) {
2012 res = -EINVAL;
2013 goto out;
2014 }
2015
2016 if (attrs[TIPC_NLA_LINK_PROP]) {
2017 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2018
2019 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2020 props);
2021 if (err) {
2022 res = err;
2023 goto out;
2024 }
2025
2026 if (props[TIPC_NLA_PROP_TOL]) {
2027 u32 tol;
2028
2029 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Richard Alped01332f2016-02-01 08:19:56 +01002030 tipc_link_set_tolerance(link, tol, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002031 }
2032 if (props[TIPC_NLA_PROP_PRIO]) {
2033 u32 prio;
2034
2035 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
Richard Alped01332f2016-02-01 08:19:56 +01002036 tipc_link_set_prio(link, prio, &xmitq);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002037 }
2038 if (props[TIPC_NLA_PROP_WIN]) {
2039 u32 win;
2040
2041 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2042 tipc_link_set_queue_limits(link, win);
2043 }
2044 }
2045
2046out:
2047 tipc_node_read_unlock(node);
Richard Alped01332f2016-02-01 08:19:56 +01002048 tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002049 return res;
2050}
2051
2052int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2053{
2054 struct net *net = genl_info_net(info);
Ying Xue94f6a802018-05-08 21:44:06 +08002055 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002056 struct tipc_nl_msg msg;
2057 char *name;
2058 int err;
2059
2060 msg.portid = info->snd_portid;
2061 msg.seq = info->snd_seq;
2062
Ying Xue94f6a802018-05-08 21:44:06 +08002063 if (!info->attrs[TIPC_NLA_LINK])
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002064 return -EINVAL;
Ying Xue94f6a802018-05-08 21:44:06 +08002065
2066 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2067 info->attrs[TIPC_NLA_LINK],
2068 tipc_nl_link_policy, info->extack);
2069 if (err)
2070 return err;
2071
2072 if (!attrs[TIPC_NLA_LINK_NAME])
2073 return -EINVAL;
2074
2075 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002076
2077 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2078 if (!msg.skb)
2079 return -ENOMEM;
2080
2081 if (strcmp(name, tipc_bclink_name) == 0) {
2082 err = tipc_nl_add_bc_link(net, &msg);
Cong Wang59b36612018-01-10 12:50:25 -08002083 if (err)
2084 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002085 } else {
2086 int bearer_id;
2087 struct tipc_node *node;
2088 struct tipc_link *link;
2089
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002090 node = tipc_node_find_by_name(net, name, &bearer_id);
Cong Wang59b36612018-01-10 12:50:25 -08002091 if (!node) {
2092 err = -EINVAL;
2093 goto err_free;
2094 }
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002095
2096 tipc_node_read_lock(node);
2097 link = node->links[bearer_id].link;
2098 if (!link) {
2099 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002100 err = -EINVAL;
2101 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002102 }
2103
2104 err = __tipc_nl_add_link(net, &msg, link, 0);
2105 tipc_node_read_unlock(node);
Cong Wang59b36612018-01-10 12:50:25 -08002106 if (err)
2107 goto err_free;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002108 }
2109
2110 return genlmsg_reply(msg.skb, info);
Cong Wang59b36612018-01-10 12:50:25 -08002111
2112err_free:
2113 nlmsg_free(msg.skb);
2114 return err;
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002115}
2116
2117int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2118{
2119 int err;
2120 char *link_name;
2121 unsigned int bearer_id;
2122 struct tipc_link *link;
2123 struct tipc_node *node;
2124 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2125 struct net *net = sock_net(skb->sk);
2126 struct tipc_link_entry *le;
2127
2128 if (!info->attrs[TIPC_NLA_LINK])
2129 return -EINVAL;
2130
2131 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2132 info->attrs[TIPC_NLA_LINK],
Johannes Bergfe521452017-04-12 14:34:08 +02002133 tipc_nl_link_policy, info->extack);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002134 if (err)
2135 return err;
2136
2137 if (!attrs[TIPC_NLA_LINK_NAME])
2138 return -EINVAL;
2139
2140 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2141
2142 if (strcmp(link_name, tipc_bclink_name) == 0) {
2143 err = tipc_bclink_reset_stats(net);
2144 if (err)
2145 return err;
2146 return 0;
2147 }
2148
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002149 node = tipc_node_find_by_name(net, link_name, &bearer_id);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002150 if (!node)
2151 return -EINVAL;
2152
2153 le = &node->links[bearer_id];
2154 tipc_node_read_lock(node);
2155 spin_lock_bh(&le->lock);
2156 link = node->links[bearer_id].link;
2157 if (!link) {
2158 spin_unlock_bh(&le->lock);
2159 tipc_node_read_unlock(node);
2160 return -EINVAL;
2161 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002162 tipc_link_reset_stats(link);
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002163 spin_unlock_bh(&le->lock);
2164 tipc_node_read_unlock(node);
2165 return 0;
2166}
2167
2168/* Caller should hold node lock */
2169static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2170 struct tipc_node *node, u32 *prev_link)
2171{
2172 u32 i;
2173 int err;
2174
2175 for (i = *prev_link; i < MAX_BEARERS; i++) {
2176 *prev_link = i;
2177
2178 if (!node->links[i].link)
2179 continue;
2180
2181 err = __tipc_nl_add_link(net, msg,
2182 node->links[i].link, NLM_F_MULTI);
2183 if (err)
2184 return err;
2185 }
2186 *prev_link = 0;
2187
2188 return 0;
2189}
2190
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002191int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002192{
2193 struct net *net = sock_net(skb->sk);
2194 struct tipc_net *tn = net_generic(net, tipc_net_id);
2195 struct tipc_node *node;
2196 struct tipc_nl_msg msg;
2197 u32 prev_node = cb->args[0];
2198 u32 prev_link = cb->args[1];
2199 int done = cb->args[2];
2200 int err;
2201
2202 if (done)
2203 return 0;
2204
2205 msg.skb = skb;
2206 msg.portid = NETLINK_CB(cb->skb).portid;
2207 msg.seq = cb->nlh->nlmsg_seq;
2208
2209 rcu_read_lock();
2210 if (prev_node) {
2211 node = tipc_node_find(net, prev_node);
2212 if (!node) {
2213 /* We never set seq or call nl_dump_check_consistent()
2214 * this means that setting prev_seq here will cause the
2215 * consistence check to fail in the netlink callback
2216 * handler. Resulting in the last NLMSG_DONE message
2217 * having the NLM_F_DUMP_INTR flag set.
2218 */
2219 cb->prev_seq = 1;
2220 goto out;
2221 }
2222 tipc_node_put(node);
2223
2224 list_for_each_entry_continue_rcu(node, &tn->node_list,
2225 list) {
2226 tipc_node_read_lock(node);
2227 err = __tipc_nl_add_node_links(net, &msg, node,
2228 &prev_link);
2229 tipc_node_read_unlock(node);
2230 if (err)
2231 goto out;
2232
2233 prev_node = node->addr;
2234 }
2235 } else {
2236 err = tipc_nl_add_bc_link(net, &msg);
2237 if (err)
2238 goto out;
2239
2240 list_for_each_entry_rcu(node, &tn->node_list, list) {
2241 tipc_node_read_lock(node);
2242 err = __tipc_nl_add_node_links(net, &msg, node,
2243 &prev_link);
2244 tipc_node_read_unlock(node);
2245 if (err)
2246 goto out;
2247
2248 prev_node = node->addr;
2249 }
2250 }
2251 done = 1;
2252out:
2253 rcu_read_unlock();
2254
2255 cb->args[0] = prev_node;
2256 cb->args[1] = prev_link;
2257 cb->args[2] = done;
2258
2259 return skb->len;
2260}
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002261
2262int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2263{
2264 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2265 struct net *net = sock_net(skb->sk);
2266 int err;
2267
2268 if (!info->attrs[TIPC_NLA_MON])
2269 return -EINVAL;
2270
2271 err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2272 info->attrs[TIPC_NLA_MON],
Johannes Bergfe521452017-04-12 14:34:08 +02002273 tipc_nl_monitor_policy, info->extack);
Parthasarathy Bhuvaragan7b3f5222016-07-26 08:47:19 +02002274 if (err)
2275 return err;
2276
2277 if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2278 u32 val;
2279
2280 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2281 err = tipc_nl_monitor_set_threshold(net, val);
2282 if (err)
2283 return err;
2284 }
2285
2286 return 0;
2287}
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002288
2289static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2290{
2291 struct nlattr *attrs;
2292 void *hdr;
2293 u32 val;
2294
2295 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2296 0, TIPC_NL_MON_GET);
2297 if (!hdr)
2298 return -EMSGSIZE;
2299
2300 attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2301 if (!attrs)
2302 goto msg_full;
2303
2304 val = tipc_nl_monitor_get_threshold(net);
2305
2306 if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2307 goto attr_msg_full;
2308
2309 nla_nest_end(msg->skb, attrs);
2310 genlmsg_end(msg->skb, hdr);
2311
2312 return 0;
2313
2314attr_msg_full:
2315 nla_nest_cancel(msg->skb, attrs);
2316msg_full:
2317 genlmsg_cancel(msg->skb, hdr);
2318
2319 return -EMSGSIZE;
2320}
2321
2322int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2323{
2324 struct net *net = sock_net(skb->sk);
2325 struct tipc_nl_msg msg;
2326 int err;
2327
2328 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Pan Bian78302fd2017-04-23 15:09:19 +08002329 if (!msg.skb)
2330 return -ENOMEM;
Parthasarathy Bhuvaraganbf1035b2016-07-26 08:47:20 +02002331 msg.portid = info->snd_portid;
2332 msg.seq = info->snd_seq;
2333
2334 err = __tipc_nl_add_monitor_prop(net, &msg);
2335 if (err) {
2336 nlmsg_free(msg.skb);
2337 return err;
2338 }
2339
2340 return genlmsg_reply(msg.skb, info);
2341}
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002342
2343int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2344{
2345 struct net *net = sock_net(skb->sk);
2346 u32 prev_bearer = cb->args[0];
2347 struct tipc_nl_msg msg;
Tung Nguyen36a50a92018-04-17 21:58:27 +02002348 int bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002349 int err;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002350
2351 if (prev_bearer == MAX_BEARERS)
2352 return 0;
2353
2354 msg.skb = skb;
2355 msg.portid = NETLINK_CB(cb->skb).portid;
2356 msg.seq = cb->nlh->nlmsg_seq;
2357
2358 rtnl_lock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002359 for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
Jon Maloy7dbc73e62018-04-25 18:29:25 +02002360 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002361 if (err)
Tung Nguyen36a50a92018-04-17 21:58:27 +02002362 break;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002363 }
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002364 rtnl_unlock();
Tung Nguyen36a50a92018-04-17 21:58:27 +02002365 cb->args[0] = bearer_id;
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002366
2367 return skb->len;
2368}
2369
2370int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2371 struct netlink_callback *cb)
2372{
2373 struct net *net = sock_net(skb->sk);
2374 u32 prev_node = cb->args[1];
2375 u32 bearer_id = cb->args[2];
2376 int done = cb->args[0];
2377 struct tipc_nl_msg msg;
2378 int err;
2379
2380 if (!prev_node) {
2381 struct nlattr **attrs;
2382 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2383
2384 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2385 if (err)
2386 return err;
2387
2388 if (!attrs[TIPC_NLA_MON])
2389 return -EINVAL;
2390
2391 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2392 attrs[TIPC_NLA_MON],
Johannes Bergfceb6432017-04-12 14:34:07 +02002393 tipc_nl_monitor_policy, NULL);
Parthasarathy Bhuvaragancf6f7e12016-07-26 08:47:22 +02002394 if (err)
2395 return err;
2396
2397 if (!mon[TIPC_NLA_MON_REF])
2398 return -EINVAL;
2399
2400 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2401
2402 if (bearer_id >= MAX_BEARERS)
2403 return -EINVAL;
2404 }
2405
2406 if (done)
2407 return 0;
2408
2409 msg.skb = skb;
2410 msg.portid = NETLINK_CB(cb->skb).portid;
2411 msg.seq = cb->nlh->nlmsg_seq;
2412
2413 rtnl_lock();
2414 err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2415 if (!err)
2416 done = 1;
2417
2418 rtnl_unlock();
2419 cb->args[0] = done;
2420 cb->args[1] = prev_node;
2421 cb->args[2] = bearer_id;
2422
2423 return skb->len;
2424}