xref: /linux/net/tipc/node.c (revision d5a7fc58da039903b332041e8c67daae36f08b50)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/node.c: TIPC node management routines
3b97bf3fdSPer Liden  *
460020e18SJon Paul Maloy  * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
546651c59SYing Xue  * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
6b97bf3fdSPer Liden  * All rights reserved.
7b97bf3fdSPer Liden  *
8b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
9b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
10b97bf3fdSPer Liden  *
119ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
129ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
139ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
149ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
159ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
169ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
179ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
189ea1fd3cSPer Liden  *    this software without specific prior written permission.
199ea1fd3cSPer Liden  *
209ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
219ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
229ea1fd3cSPer Liden  * Software Foundation.
23b97bf3fdSPer Liden  *
24b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
35b97bf3fdSPer Liden  */
36b97bf3fdSPer Liden 
37b97bf3fdSPer Liden #include "core.h"
3822ae7cffSRichard Alpe #include "link.h"
39b97bf3fdSPer Liden #include "node.h"
40b97bf3fdSPer Liden #include "name_distr.h"
4150100a5eSJon Paul Maloy #include "socket.h"
42a6bf70f7SJon Paul Maloy #include "bcast.h"
4335c55c98SJon Paul Maloy #include "monitor.h"
44d999297cSJon Paul Maloy #include "discover.h"
4549cc66eaSRichard Alpe #include "netlink.h"
46b4b9771bSTuong Lien #include "trace.h"
47fc1b6d6dSTuong Lien #include "crypto.h"
48c8199300SJon Paul Maloy 
495be9c086SJon Paul Maloy #define INVALID_NODE_SIG	0x10000
506a939f36SGhantaKrishnamurthy MohanKrishna #define NODE_CLEANUP_AFTER	300000
515be9c086SJon Paul Maloy 
525be9c086SJon Paul Maloy /* Flags used to take different actions according to flag type
535be9c086SJon Paul Maloy  * TIPC_NOTIFY_NODE_DOWN: notify node is down
545be9c086SJon Paul Maloy  * TIPC_NOTIFY_NODE_UP: notify node is up
555be9c086SJon Paul Maloy  * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
565be9c086SJon Paul Maloy  */
575be9c086SJon Paul Maloy enum {
585be9c086SJon Paul Maloy 	TIPC_NOTIFY_NODE_DOWN		= (1 << 3),
595be9c086SJon Paul Maloy 	TIPC_NOTIFY_NODE_UP		= (1 << 4),
605be9c086SJon Paul Maloy 	TIPC_NOTIFY_LINK_UP		= (1 << 6),
615be9c086SJon Paul Maloy 	TIPC_NOTIFY_LINK_DOWN		= (1 << 7)
625be9c086SJon Paul Maloy };
635be9c086SJon Paul Maloy 
645be9c086SJon Paul Maloy struct tipc_link_entry {
655be9c086SJon Paul Maloy 	struct tipc_link *link;
665be9c086SJon Paul Maloy 	spinlock_t lock; /* per link */
675be9c086SJon Paul Maloy 	u32 mtu;
685be9c086SJon Paul Maloy 	struct sk_buff_head inputq;
695be9c086SJon Paul Maloy 	struct tipc_media_addr maddr;
705be9c086SJon Paul Maloy };
715be9c086SJon Paul Maloy 
725be9c086SJon Paul Maloy struct tipc_bclink_entry {
735be9c086SJon Paul Maloy 	struct tipc_link *link;
745be9c086SJon Paul Maloy 	struct sk_buff_head inputq1;
755be9c086SJon Paul Maloy 	struct sk_buff_head arrvq;
765be9c086SJon Paul Maloy 	struct sk_buff_head inputq2;
775be9c086SJon Paul Maloy 	struct sk_buff_head namedq;
78cad2929dSHoang Huu Le 	u16 named_rcv_nxt;
79cad2929dSHoang Huu Le 	bool named_open;
805be9c086SJon Paul Maloy };
815be9c086SJon Paul Maloy 
825be9c086SJon Paul Maloy /**
835be9c086SJon Paul Maloy  * struct tipc_node - TIPC node structure
845be9c086SJon Paul Maloy  * @addr: network address of node
854476441eSRandy Dunlap  * @kref: reference counter to node object
865be9c086SJon Paul Maloy  * @lock: rwlock governing access to structure
875be9c086SJon Paul Maloy  * @net: the applicable net namespace
885be9c086SJon Paul Maloy  * @hash: links to adjacent nodes in unsorted hash chain
895be9c086SJon Paul Maloy  * @active_links: bearer ids of active links, used as index into links[] array
905be9c086SJon Paul Maloy  * @links: array containing references to all links to node
914476441eSRandy Dunlap  * @bc_entry: broadcast link entry
925be9c086SJon Paul Maloy  * @action_flags: bit mask of different types of node actions
935be9c086SJon Paul Maloy  * @state: connectivity state vs peer node
944cbf8ac2STuong Lien  * @preliminary: a preliminary node or not
954476441eSRandy Dunlap  * @failover_sent: failover sent or not
965be9c086SJon Paul Maloy  * @sync_point: sequence number where synch/failover is finished
975be9c086SJon Paul Maloy  * @list: links to adjacent nodes in sorted list of cluster's nodes
985be9c086SJon Paul Maloy  * @working_links: number of working links to node (both active and standby)
995be9c086SJon Paul Maloy  * @link_cnt: number of links to node
1005be9c086SJon Paul Maloy  * @capabilities: bitmap, indicating peer node's functional capabilities
1015be9c086SJon Paul Maloy  * @signature: node instance identifier
1025be9c086SJon Paul Maloy  * @link_id: local and remote bearer ids of changing link, if any
1034476441eSRandy Dunlap  * @peer_id: 128-bit ID of peer
1044476441eSRandy Dunlap  * @peer_id_string: ID string of peer
1055be9c086SJon Paul Maloy  * @publ_list: list of publications
1064476441eSRandy Dunlap  * @conn_sks: list of connections (FIXME)
1074476441eSRandy Dunlap  * @timer: node's keepalive timer
1084476441eSRandy Dunlap  * @keepalive_intv: keepalive interval in milliseconds
1095be9c086SJon Paul Maloy  * @rcu: rcu struct for tipc_node
1106a939f36SGhantaKrishnamurthy MohanKrishna  * @delete_at: indicates the time for deleting a down node
1114476441eSRandy Dunlap  * @peer_net: peer's net namespace
1124476441eSRandy Dunlap  * @peer_hash_mix: hash for this peer (FIXME)
113fc1b6d6dSTuong Lien  * @crypto_rx: RX crypto handler
1145be9c086SJon Paul Maloy  */
1155be9c086SJon Paul Maloy struct tipc_node {
1165be9c086SJon Paul Maloy 	u32 addr;
1175be9c086SJon Paul Maloy 	struct kref kref;
1185be9c086SJon Paul Maloy 	rwlock_t lock;
1195be9c086SJon Paul Maloy 	struct net *net;
1205be9c086SJon Paul Maloy 	struct hlist_node hash;
1215be9c086SJon Paul Maloy 	int active_links[2];
1225be9c086SJon Paul Maloy 	struct tipc_link_entry links[MAX_BEARERS];
1235be9c086SJon Paul Maloy 	struct tipc_bclink_entry bc_entry;
1245be9c086SJon Paul Maloy 	int action_flags;
1255be9c086SJon Paul Maloy 	struct list_head list;
1265be9c086SJon Paul Maloy 	int state;
1274cbf8ac2STuong Lien 	bool preliminary;
128c140eb16SLUU Duc Canh 	bool failover_sent;
1295be9c086SJon Paul Maloy 	u16 sync_point;
1305be9c086SJon Paul Maloy 	int link_cnt;
1315be9c086SJon Paul Maloy 	u16 working_links;
1325be9c086SJon Paul Maloy 	u16 capabilities;
1335be9c086SJon Paul Maloy 	u32 signature;
1345be9c086SJon Paul Maloy 	u32 link_id;
13525b0b9c4SJon Maloy 	u8 peer_id[16];
1364cbf8ac2STuong Lien 	char peer_id_string[NODE_ID_STR_LEN];
1375be9c086SJon Paul Maloy 	struct list_head publ_list;
1385be9c086SJon Paul Maloy 	struct list_head conn_sks;
1395be9c086SJon Paul Maloy 	unsigned long keepalive_intv;
1405be9c086SJon Paul Maloy 	struct timer_list timer;
1415be9c086SJon Paul Maloy 	struct rcu_head rcu;
1426a939f36SGhantaKrishnamurthy MohanKrishna 	unsigned long delete_at;
143f73b1281SHoang Le 	struct net *peer_net;
144f73b1281SHoang Le 	u32 peer_hash_mix;
145fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
146fc1b6d6dSTuong Lien 	struct tipc_crypto *crypto_rx;
147fc1b6d6dSTuong Lien #endif
1485be9c086SJon Paul Maloy };
1495be9c086SJon Paul Maloy 
1506e498158SJon Paul Maloy /* Node FSM states and events:
1516e498158SJon Paul Maloy  */
1526e498158SJon Paul Maloy enum {
1536e498158SJon Paul Maloy 	SELF_DOWN_PEER_DOWN    = 0xdd,
1546e498158SJon Paul Maloy 	SELF_UP_PEER_UP        = 0xaa,
1556e498158SJon Paul Maloy 	SELF_DOWN_PEER_LEAVING = 0xd1,
1566e498158SJon Paul Maloy 	SELF_UP_PEER_COMING    = 0xac,
1576e498158SJon Paul Maloy 	SELF_COMING_PEER_UP    = 0xca,
1586e498158SJon Paul Maloy 	SELF_LEAVING_PEER_DOWN = 0x1d,
1596e498158SJon Paul Maloy 	NODE_FAILINGOVER       = 0xf0,
1606e498158SJon Paul Maloy 	NODE_SYNCHING          = 0xcc
1616e498158SJon Paul Maloy };
1626e498158SJon Paul Maloy 
1636e498158SJon Paul Maloy enum {
1646e498158SJon Paul Maloy 	SELF_ESTABL_CONTACT_EVT = 0xece,
1656e498158SJon Paul Maloy 	SELF_LOST_CONTACT_EVT   = 0x1ce,
1666e498158SJon Paul Maloy 	PEER_ESTABL_CONTACT_EVT = 0x9ece,
1676e498158SJon Paul Maloy 	PEER_LOST_CONTACT_EVT   = 0x91ce,
1686e498158SJon Paul Maloy 	NODE_FAILOVER_BEGIN_EVT = 0xfbe,
1696e498158SJon Paul Maloy 	NODE_FAILOVER_END_EVT   = 0xfee,
1706e498158SJon Paul Maloy 	NODE_SYNCH_BEGIN_EVT    = 0xcbe,
1716e498158SJon Paul Maloy 	NODE_SYNCH_END_EVT      = 0xcee
1726e498158SJon Paul Maloy };
1736e498158SJon Paul Maloy 
174598411d7SJon Paul Maloy static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
175598411d7SJon Paul Maloy 				  struct sk_buff_head *xmitq,
176598411d7SJon Paul Maloy 				  struct tipc_media_addr **maddr);
177598411d7SJon Paul Maloy static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
178598411d7SJon Paul Maloy 				bool delete);
179598411d7SJon Paul Maloy static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
1808a0f6ebeSYing Xue static void tipc_node_delete(struct tipc_node *node);
18131b102bbSKees Cook static void tipc_node_timeout(struct timer_list *t);
182d999297cSJon Paul Maloy static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
1835be9c086SJon Paul Maloy static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
18425b0b9c4SJon Maloy static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
18538077b8eSJon Maloy static bool node_is_up(struct tipc_node *n);
1866a939f36SGhantaKrishnamurthy MohanKrishna static void tipc_node_delete_from_list(struct tipc_node *node);
187b97bf3fdSPer Liden 
18802be61a9SJon Paul Maloy struct tipc_sock_conn {
18902be61a9SJon Paul Maloy 	u32 port;
19002be61a9SJon Paul Maloy 	u32 peer_port;
19102be61a9SJon Paul Maloy 	u32 peer_node;
19202be61a9SJon Paul Maloy 	struct list_head list;
19302be61a9SJon Paul Maloy };
19402be61a9SJon Paul Maloy 
node_active_link(struct tipc_node * n,int sel)1955be9c086SJon Paul Maloy static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
1965be9c086SJon Paul Maloy {
1975be9c086SJon Paul Maloy 	int bearer_id = n->active_links[sel & 1];
1985be9c086SJon Paul Maloy 
1995be9c086SJon Paul Maloy 	if (unlikely(bearer_id == INVALID_BEARER_ID))
2005be9c086SJon Paul Maloy 		return NULL;
2015be9c086SJon Paul Maloy 
2025be9c086SJon Paul Maloy 	return n->links[bearer_id].link;
2035be9c086SJon Paul Maloy }
2045be9c086SJon Paul Maloy 
tipc_node_get_mtu(struct net * net,u32 addr,u32 sel,bool connected)205f73b1281SHoang Le int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
2065be9c086SJon Paul Maloy {
2075be9c086SJon Paul Maloy 	struct tipc_node *n;
2085be9c086SJon Paul Maloy 	int bearer_id;
2095be9c086SJon Paul Maloy 	unsigned int mtu = MAX_MSG_SIZE;
2105be9c086SJon Paul Maloy 
2115be9c086SJon Paul Maloy 	n = tipc_node_find(net, addr);
2125be9c086SJon Paul Maloy 	if (unlikely(!n))
2135be9c086SJon Paul Maloy 		return mtu;
2145be9c086SJon Paul Maloy 
215f73b1281SHoang Le 	/* Allow MAX_MSG_SIZE when building connection oriented message
216f73b1281SHoang Le 	 * if they are in the same core network
217f73b1281SHoang Le 	 */
218f73b1281SHoang Le 	if (n->peer_net && connected) {
219f73b1281SHoang Le 		tipc_node_put(n);
220f73b1281SHoang Le 		return mtu;
221f73b1281SHoang Le 	}
222f73b1281SHoang Le 
2235be9c086SJon Paul Maloy 	bearer_id = n->active_links[sel & 1];
2245be9c086SJon Paul Maloy 	if (likely(bearer_id != INVALID_BEARER_ID))
2255be9c086SJon Paul Maloy 		mtu = n->links[bearer_id].mtu;
2265be9c086SJon Paul Maloy 	tipc_node_put(n);
2275be9c086SJon Paul Maloy 	return mtu;
2285be9c086SJon Paul Maloy }
22960020e18SJon Paul Maloy 
tipc_node_get_id(struct net * net,u32 addr,u8 * id)2303e5cf362SJon Maloy bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
2313e5cf362SJon Maloy {
2323e5cf362SJon Maloy 	u8 *own_id = tipc_own_id(net);
2333e5cf362SJon Maloy 	struct tipc_node *n;
2343e5cf362SJon Maloy 
2353e5cf362SJon Maloy 	if (!own_id)
2363e5cf362SJon Maloy 		return true;
2373e5cf362SJon Maloy 
2383e5cf362SJon Maloy 	if (addr == tipc_own_addr(net)) {
2393e5cf362SJon Maloy 		memcpy(id, own_id, TIPC_NODEID_LEN);
2403e5cf362SJon Maloy 		return true;
2413e5cf362SJon Maloy 	}
2423e5cf362SJon Maloy 	n = tipc_node_find(net, addr);
2433e5cf362SJon Maloy 	if (!n)
2443e5cf362SJon Maloy 		return false;
2453e5cf362SJon Maloy 
2463e5cf362SJon Maloy 	memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
2473e5cf362SJon Maloy 	tipc_node_put(n);
2483e5cf362SJon Maloy 	return true;
2493e5cf362SJon Maloy }
2503e5cf362SJon Maloy 
tipc_node_get_capabilities(struct net * net,u32 addr)25160020e18SJon Paul Maloy u16 tipc_node_get_capabilities(struct net *net, u32 addr)
25260020e18SJon Paul Maloy {
25360020e18SJon Paul Maloy 	struct tipc_node *n;
25460020e18SJon Paul Maloy 	u16 caps;
25560020e18SJon Paul Maloy 
25660020e18SJon Paul Maloy 	n = tipc_node_find(net, addr);
25760020e18SJon Paul Maloy 	if (unlikely(!n))
25860020e18SJon Paul Maloy 		return TIPC_NODE_CAPABILITIES;
25960020e18SJon Paul Maloy 	caps = n->capabilities;
26060020e18SJon Paul Maloy 	tipc_node_put(n);
26160020e18SJon Paul Maloy 	return caps;
26260020e18SJon Paul Maloy }
26360020e18SJon Paul Maloy 
tipc_node_get_addr(struct tipc_node * node)2644cbf8ac2STuong Lien u32 tipc_node_get_addr(struct tipc_node *node)
2654cbf8ac2STuong Lien {
2664cbf8ac2STuong Lien 	return (node) ? node->addr : 0;
2674cbf8ac2STuong Lien }
2684cbf8ac2STuong Lien 
tipc_node_get_id_str(struct tipc_node * node)2694cbf8ac2STuong Lien char *tipc_node_get_id_str(struct tipc_node *node)
2704cbf8ac2STuong Lien {
2714cbf8ac2STuong Lien 	return node->peer_id_string;
2724cbf8ac2STuong Lien }
2734cbf8ac2STuong Lien 
274fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
275fc1b6d6dSTuong Lien /**
276fc1b6d6dSTuong Lien  * tipc_node_crypto_rx - Retrieve crypto RX handle from node
2774476441eSRandy Dunlap  * @__n: target tipc_node
278fc1b6d6dSTuong Lien  * Note: node ref counter must be held first!
279fc1b6d6dSTuong Lien  */
tipc_node_crypto_rx(struct tipc_node * __n)280fc1b6d6dSTuong Lien struct tipc_crypto *tipc_node_crypto_rx(struct tipc_node *__n)
281fc1b6d6dSTuong Lien {
282fc1b6d6dSTuong Lien 	return (__n) ? __n->crypto_rx : NULL;
283fc1b6d6dSTuong Lien }
284fc1b6d6dSTuong Lien 
tipc_node_crypto_rx_by_list(struct list_head * pos)285fc1b6d6dSTuong Lien struct tipc_crypto *tipc_node_crypto_rx_by_list(struct list_head *pos)
286fc1b6d6dSTuong Lien {
287fc1b6d6dSTuong Lien 	return container_of(pos, struct tipc_node, list)->crypto_rx;
288fc1b6d6dSTuong Lien }
2891ef6f7c9STuong Lien 
tipc_node_crypto_rx_by_addr(struct net * net,u32 addr)2901ef6f7c9STuong Lien struct tipc_crypto *tipc_node_crypto_rx_by_addr(struct net *net, u32 addr)
2911ef6f7c9STuong Lien {
2921ef6f7c9STuong Lien 	struct tipc_node *n;
2931ef6f7c9STuong Lien 
2941ef6f7c9STuong Lien 	n = tipc_node_find(net, addr);
2951ef6f7c9STuong Lien 	return (n) ? n->crypto_rx : NULL;
2961ef6f7c9STuong Lien }
297fc1b6d6dSTuong Lien #endif
298fc1b6d6dSTuong Lien 
tipc_node_free(struct rcu_head * rp)2992437fd7bSChen Wandun static void tipc_node_free(struct rcu_head *rp)
300fc1b6d6dSTuong Lien {
301fc1b6d6dSTuong Lien 	struct tipc_node *n = container_of(rp, struct tipc_node, rcu);
302fc1b6d6dSTuong Lien 
303fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
304fc1b6d6dSTuong Lien 	tipc_crypto_stop(&n->crypto_rx);
305fc1b6d6dSTuong Lien #endif
306fc1b6d6dSTuong Lien 	kfree(n);
307fc1b6d6dSTuong Lien }
308fc1b6d6dSTuong Lien 
tipc_node_kref_release(struct kref * kref)3098a0f6ebeSYing Xue static void tipc_node_kref_release(struct kref *kref)
3108a0f6ebeSYing Xue {
311d25a0125SJon Paul Maloy 	struct tipc_node *n = container_of(kref, struct tipc_node, kref);
3128a0f6ebeSYing Xue 
313d25a0125SJon Paul Maloy 	kfree(n->bc_entry.link);
314fc1b6d6dSTuong Lien 	call_rcu(&n->rcu, tipc_node_free);
3158a0f6ebeSYing Xue }
3168a0f6ebeSYing Xue 
tipc_node_put(struct tipc_node * node)317fc1b6d6dSTuong Lien void tipc_node_put(struct tipc_node *node)
3188a0f6ebeSYing Xue {
3198a0f6ebeSYing Xue 	kref_put(&node->kref, tipc_node_kref_release);
3208a0f6ebeSYing Xue }
3218a0f6ebeSYing Xue 
tipc_node_get(struct tipc_node * node)3221ef6f7c9STuong Lien void tipc_node_get(struct tipc_node *node)
3238a0f6ebeSYing Xue {
3248a0f6ebeSYing Xue 	kref_get(&node->kref);
3258a0f6ebeSYing Xue }
3268a0f6ebeSYing Xue 
327a635b46bSAllan Stephens /*
328672d99e1SAllan Stephens  * tipc_node_find - locate specified node object, if it exists
329672d99e1SAllan Stephens  */
tipc_node_find(struct net * net,u32 addr)3305be9c086SJon Paul Maloy static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
331672d99e1SAllan Stephens {
332b170997aSJon Paul Maloy 	struct tipc_net *tn = tipc_net(net);
333672d99e1SAllan Stephens 	struct tipc_node *node;
334b170997aSJon Paul Maloy 	unsigned int thash = tipc_hashfn(addr);
335672d99e1SAllan Stephens 
3366c7a762eSYing Xue 	rcu_read_lock();
337b170997aSJon Paul Maloy 	hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
3384cbf8ac2STuong Lien 		if (node->addr != addr || node->preliminary)
339b170997aSJon Paul Maloy 			continue;
340b170997aSJon Paul Maloy 		if (!kref_get_unless_zero(&node->kref))
341b170997aSJon Paul Maloy 			node = NULL;
342b170997aSJon Paul Maloy 		break;
343b170997aSJon Paul Maloy 	}
3446c7a762eSYing Xue 	rcu_read_unlock();
345672d99e1SAllan Stephens 	return node;
346672d99e1SAllan Stephens }
347672d99e1SAllan Stephens 
34825b0b9c4SJon Maloy /* tipc_node_find_by_id - locate specified node object by its 128-bit id
34925b0b9c4SJon Maloy  * Note: this function is called only when a discovery request failed
35025b0b9c4SJon Maloy  * to find the node by its 32-bit id, and is not time critical
35125b0b9c4SJon Maloy  */
tipc_node_find_by_id(struct net * net,u8 * id)35225b0b9c4SJon Maloy static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
35325b0b9c4SJon Maloy {
35425b0b9c4SJon Maloy 	struct tipc_net *tn = tipc_net(net);
35525b0b9c4SJon Maloy 	struct tipc_node *n;
35625b0b9c4SJon Maloy 	bool found = false;
35725b0b9c4SJon Maloy 
35825b0b9c4SJon Maloy 	rcu_read_lock();
35925b0b9c4SJon Maloy 	list_for_each_entry_rcu(n, &tn->node_list, list) {
36025b0b9c4SJon Maloy 		read_lock_bh(&n->lock);
36125b0b9c4SJon Maloy 		if (!memcmp(id, n->peer_id, 16) &&
36225b0b9c4SJon Maloy 		    kref_get_unless_zero(&n->kref))
36325b0b9c4SJon Maloy 			found = true;
36425b0b9c4SJon Maloy 		read_unlock_bh(&n->lock);
36525b0b9c4SJon Maloy 		if (found)
36625b0b9c4SJon Maloy 			break;
36725b0b9c4SJon Maloy 	}
36825b0b9c4SJon Maloy 	rcu_read_unlock();
36925b0b9c4SJon Maloy 	return found ? n : NULL;
37025b0b9c4SJon Maloy }
37125b0b9c4SJon Maloy 
tipc_node_read_lock(struct tipc_node * n)3725be9c086SJon Paul Maloy static void tipc_node_read_lock(struct tipc_node *n)
37397bc84bbSHoang Huu Le 	__acquires(n->lock)
3745405ff6eSJon Paul Maloy {
3755405ff6eSJon Paul Maloy 	read_lock_bh(&n->lock);
3765405ff6eSJon Paul Maloy }
3775405ff6eSJon Paul Maloy 
tipc_node_read_unlock(struct tipc_node * n)3785be9c086SJon Paul Maloy static void tipc_node_read_unlock(struct tipc_node *n)
37997bc84bbSHoang Huu Le 	__releases(n->lock)
3805405ff6eSJon Paul Maloy {
3815405ff6eSJon Paul Maloy 	read_unlock_bh(&n->lock);
3825405ff6eSJon Paul Maloy }
3835405ff6eSJon Paul Maloy 
tipc_node_write_lock(struct tipc_node * n)3845405ff6eSJon Paul Maloy static void tipc_node_write_lock(struct tipc_node *n)
38597bc84bbSHoang Huu Le 	__acquires(n->lock)
3865405ff6eSJon Paul Maloy {
3875405ff6eSJon Paul Maloy 	write_lock_bh(&n->lock);
3885405ff6eSJon Paul Maloy }
3895405ff6eSJon Paul Maloy 
tipc_node_write_unlock_fast(struct tipc_node * n)39093f955aaSParthasarathy Bhuvaragan static void tipc_node_write_unlock_fast(struct tipc_node *n)
39197bc84bbSHoang Huu Le 	__releases(n->lock)
39293f955aaSParthasarathy Bhuvaragan {
39393f955aaSParthasarathy Bhuvaragan 	write_unlock_bh(&n->lock);
39493f955aaSParthasarathy Bhuvaragan }
39593f955aaSParthasarathy Bhuvaragan 
tipc_node_write_unlock(struct tipc_node * n)3965405ff6eSJon Paul Maloy static void tipc_node_write_unlock(struct tipc_node *n)
39797bc84bbSHoang Huu Le 	__releases(n->lock)
3985405ff6eSJon Paul Maloy {
39950a3499aSJon Maloy 	struct tipc_socket_addr sk;
4005405ff6eSJon Paul Maloy 	struct net *net = n->net;
4015405ff6eSJon Paul Maloy 	u32 flags = n->action_flags;
4025405ff6eSJon Paul Maloy 	struct list_head *publ_list;
40350a3499aSJon Maloy 	struct tipc_uaddr ua;
404c08e5843SJon Maloy 	u32 bearer_id, node;
4055405ff6eSJon Paul Maloy 
4065405ff6eSJon Paul Maloy 	if (likely(!flags)) {
4075405ff6eSJon Paul Maloy 		write_unlock_bh(&n->lock);
4085405ff6eSJon Paul Maloy 		return;
4095405ff6eSJon Paul Maloy 	}
4105405ff6eSJon Paul Maloy 
41150a3499aSJon Maloy 	tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
41250a3499aSJon Maloy 		   TIPC_LINK_STATE, n->addr, n->addr);
41350a3499aSJon Maloy 	sk.ref = n->link_id;
414032062f3SJon Maloy 	sk.node = tipc_own_addr(net);
415c08e5843SJon Maloy 	node = n->addr;
41650a3499aSJon Maloy 	bearer_id = n->link_id & 0xffff;
4175405ff6eSJon Paul Maloy 	publ_list = &n->publ_list;
4185405ff6eSJon Paul Maloy 
4195405ff6eSJon Paul Maloy 	n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
4205405ff6eSJon Paul Maloy 			     TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
4215405ff6eSJon Paul Maloy 
4225405ff6eSJon Paul Maloy 	write_unlock_bh(&n->lock);
4235405ff6eSJon Paul Maloy 
4245405ff6eSJon Paul Maloy 	if (flags & TIPC_NOTIFY_NODE_DOWN)
425c08e5843SJon Maloy 		tipc_publ_notify(net, publ_list, node, n->capabilities);
4265405ff6eSJon Paul Maloy 
4275405ff6eSJon Paul Maloy 	if (flags & TIPC_NOTIFY_NODE_UP)
428c08e5843SJon Maloy 		tipc_named_node_up(net, node, n->capabilities);
4295405ff6eSJon Paul Maloy 
43035c55c98SJon Paul Maloy 	if (flags & TIPC_NOTIFY_LINK_UP) {
431c08e5843SJon Maloy 		tipc_mon_peer_up(net, node, bearer_id);
4323058e01dSHoang Le 		tipc_nametbl_publish(net, &ua, &sk, sk.ref);
43335c55c98SJon Paul Maloy 	}
43435c55c98SJon Paul Maloy 	if (flags & TIPC_NOTIFY_LINK_DOWN) {
435c08e5843SJon Maloy 		tipc_mon_peer_down(net, node, bearer_id);
4363058e01dSHoang Le 		tipc_nametbl_withdraw(net, &ua, &sk, sk.ref);
4375405ff6eSJon Paul Maloy 	}
43835c55c98SJon Paul Maloy }
4395405ff6eSJon Paul Maloy 
tipc_node_assign_peer_net(struct tipc_node * n,u32 hash_mixes)440f73b1281SHoang Le static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
441f73b1281SHoang Le {
442f73b1281SHoang Le 	int net_id = tipc_netid(n->net);
443f73b1281SHoang Le 	struct tipc_net *tn_peer;
444f73b1281SHoang Le 	struct net *tmp;
445f73b1281SHoang Le 	u32 hash_chk;
446f73b1281SHoang Le 
447f73b1281SHoang Le 	if (n->peer_net)
448f73b1281SHoang Le 		return;
449f73b1281SHoang Le 
450f73b1281SHoang Le 	for_each_net_rcu(tmp) {
451f73b1281SHoang Le 		tn_peer = tipc_net(tmp);
452f73b1281SHoang Le 		if (!tn_peer)
453f73b1281SHoang Le 			continue;
454f73b1281SHoang Le 		/* Integrity checking whether node exists in namespace or not */
455f73b1281SHoang Le 		if (tn_peer->net_id != net_id)
456f73b1281SHoang Le 			continue;
457f73b1281SHoang Le 		if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
458f73b1281SHoang Le 			continue;
459f73b1281SHoang Le 		hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
460f73b1281SHoang Le 		if (hash_mixes ^ hash_chk)
461f73b1281SHoang Le 			continue;
462f73b1281SHoang Le 		n->peer_net = tmp;
463f73b1281SHoang Le 		n->peer_hash_mix = hash_mixes;
464f73b1281SHoang Le 		break;
465f73b1281SHoang Le 	}
466f73b1281SHoang Le }
467f73b1281SHoang Le 
tipc_node_create(struct net * net,u32 addr,u8 * peer_id,u16 capabilities,u32 hash_mixes,bool preliminary)468fc1b6d6dSTuong Lien struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
469fc1b6d6dSTuong Lien 				   u16 capabilities, u32 hash_mixes,
470fc1b6d6dSTuong Lien 				   bool preliminary)
471b97bf3fdSPer Liden {
472f2f9800dSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
473cb8092d7SXin Long 	struct tipc_link *l, *snd_l = tipc_bc_sndlink(net);
4741a90632dSJon Paul Maloy 	struct tipc_node *n, *temp_node;
4754cbf8ac2STuong Lien 	unsigned long intv;
4769012de50SJon Maloy 	int bearer_id;
4775405ff6eSJon Paul Maloy 	int i;
478b97bf3fdSPer Liden 
479f2f9800dSYing Xue 	spin_lock_bh(&tn->node_list_lock);
4804cbf8ac2STuong Lien 	n = tipc_node_find(net, addr) ?:
4814cbf8ac2STuong Lien 		tipc_node_find_by_id(net, peer_id);
48260020e18SJon Paul Maloy 	if (n) {
4834cbf8ac2STuong Lien 		if (!n->preliminary)
4844cbf8ac2STuong Lien 			goto update;
4854cbf8ac2STuong Lien 		if (preliminary)
4864cbf8ac2STuong Lien 			goto exit;
4874cbf8ac2STuong Lien 		/* A preliminary node becomes "real" now, refresh its data */
4884cbf8ac2STuong Lien 		tipc_node_write_lock(n);
489cb8092d7SXin Long 		if (!tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
490cb8092d7SXin Long 					 tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
491cb8092d7SXin Long 					 n->capabilities, &n->bc_entry.inputq1,
492cb8092d7SXin Long 					 &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
493cb8092d7SXin Long 			pr_warn("Broadcast rcv link refresh failed, no memory\n");
494cb8092d7SXin Long 			tipc_node_write_unlock_fast(n);
495cb8092d7SXin Long 			tipc_node_put(n);
496cb8092d7SXin Long 			n = NULL;
497cb8092d7SXin Long 			goto exit;
498cb8092d7SXin Long 		}
4994cbf8ac2STuong Lien 		n->preliminary = false;
5004cbf8ac2STuong Lien 		n->addr = addr;
5014cbf8ac2STuong Lien 		hlist_del_rcu(&n->hash);
5024cbf8ac2STuong Lien 		hlist_add_head_rcu(&n->hash,
5034cbf8ac2STuong Lien 				   &tn->node_htable[tipc_hashfn(addr)]);
5044cbf8ac2STuong Lien 		list_del_rcu(&n->list);
5054cbf8ac2STuong Lien 		list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
5064cbf8ac2STuong Lien 			if (n->addr < temp_node->addr)
5074cbf8ac2STuong Lien 				break;
5084cbf8ac2STuong Lien 		}
5094cbf8ac2STuong Lien 		list_add_tail_rcu(&n->list, &temp_node->list);
5104cbf8ac2STuong Lien 		tipc_node_write_unlock_fast(n);
5114cbf8ac2STuong Lien 
5124cbf8ac2STuong Lien update:
513f73b1281SHoang Le 		if (n->peer_hash_mix ^ hash_mixes)
514f73b1281SHoang Le 			tipc_node_assign_peer_net(n, hash_mixes);
51540999f11SJon Maloy 		if (n->capabilities == capabilities)
51640999f11SJon Maloy 			goto exit;
51760020e18SJon Paul Maloy 		/* Same node may come back with new capabilities */
518909620ffSJon Maloy 		tipc_node_write_lock(n);
51960020e18SJon Paul Maloy 		n->capabilities = capabilities;
5209012de50SJon Maloy 		for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
5219012de50SJon Maloy 			l = n->links[bearer_id].link;
5229012de50SJon Maloy 			if (l)
5239012de50SJon Maloy 				tipc_link_update_caps(l, capabilities);
5249012de50SJon Maloy 		}
525909620ffSJon Maloy 		tipc_node_write_unlock_fast(n);
526909620ffSJon Maloy 
527ff2ebbfbSHoang Le 		/* Calculate cluster capabilities */
528ff2ebbfbSHoang Le 		tn->capabilities = TIPC_NODE_CAPABILITIES;
529ff2ebbfbSHoang Le 		list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
530ff2ebbfbSHoang Le 			tn->capabilities &= temp_node->capabilities;
531ff2ebbfbSHoang Le 		}
532f73b1281SHoang Le 
533ba5f6a86SHoang Le 		tipc_bcast_toggle_rcast(net,
534ba5f6a86SHoang Le 					(tn->capabilities & TIPC_BCAST_RCAST));
535ba5f6a86SHoang Le 
536b45db71bSJon Paul Maloy 		goto exit;
53760020e18SJon Paul Maloy 	}
5381a90632dSJon Paul Maloy 	n = kzalloc(sizeof(*n), GFP_ATOMIC);
5391a90632dSJon Paul Maloy 	if (!n) {
5402cf8aa19SErik Hugne 		pr_warn("Node creation failed, no memory\n");
541b45db71bSJon Paul Maloy 		goto exit;
542a10bd924SAllan Stephens 	}
5434cbf8ac2STuong Lien 	tipc_nodeid2string(n->peer_id_string, peer_id);
544fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
545fc1b6d6dSTuong Lien 	if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
546fc1b6d6dSTuong Lien 		pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
547fc1b6d6dSTuong Lien 		kfree(n);
548fc1b6d6dSTuong Lien 		n = NULL;
549fc1b6d6dSTuong Lien 		goto exit;
550fc1b6d6dSTuong Lien 	}
551fc1b6d6dSTuong Lien #endif
5521a90632dSJon Paul Maloy 	n->addr = addr;
5534cbf8ac2STuong Lien 	n->preliminary = preliminary;
55425b0b9c4SJon Maloy 	memcpy(&n->peer_id, peer_id, 16);
5551a90632dSJon Paul Maloy 	n->net = net;
556f73b1281SHoang Le 	n->peer_net = NULL;
557f73b1281SHoang Le 	n->peer_hash_mix = 0;
558f73b1281SHoang Le 	/* Assign kernel local namespace if exists */
559f73b1281SHoang Le 	tipc_node_assign_peer_net(n, hash_mixes);
5601a90632dSJon Paul Maloy 	n->capabilities = capabilities;
5611a90632dSJon Paul Maloy 	kref_init(&n->kref);
5621a90632dSJon Paul Maloy 	rwlock_init(&n->lock);
5631a90632dSJon Paul Maloy 	INIT_HLIST_NODE(&n->hash);
5641a90632dSJon Paul Maloy 	INIT_LIST_HEAD(&n->list);
5651a90632dSJon Paul Maloy 	INIT_LIST_HEAD(&n->publ_list);
5661a90632dSJon Paul Maloy 	INIT_LIST_HEAD(&n->conn_sks);
5671a90632dSJon Paul Maloy 	skb_queue_head_init(&n->bc_entry.namedq);
5681a90632dSJon Paul Maloy 	skb_queue_head_init(&n->bc_entry.inputq1);
5691a90632dSJon Paul Maloy 	__skb_queue_head_init(&n->bc_entry.arrvq);
5701a90632dSJon Paul Maloy 	skb_queue_head_init(&n->bc_entry.inputq2);
5715405ff6eSJon Paul Maloy 	for (i = 0; i < MAX_BEARERS; i++)
5721a90632dSJon Paul Maloy 		spin_lock_init(&n->links[i].lock);
5731a90632dSJon Paul Maloy 	n->state = SELF_DOWN_PEER_LEAVING;
5746a939f36SGhantaKrishnamurthy MohanKrishna 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
5751a90632dSJon Paul Maloy 	n->signature = INVALID_NODE_SIG;
5761a90632dSJon Paul Maloy 	n->active_links[0] = INVALID_BEARER_ID;
5771a90632dSJon Paul Maloy 	n->active_links[1] = INVALID_BEARER_ID;
578cb8092d7SXin Long 	if (!preliminary &&
579cb8092d7SXin Long 	    !tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
580cb8092d7SXin Long 				 tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
581cb8092d7SXin Long 				 n->capabilities, &n->bc_entry.inputq1,
582cb8092d7SXin Long 				 &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
583cb8092d7SXin Long 		pr_warn("Broadcast rcv link creation failed, no memory\n");
584de52e173SFedor Pchelkin 		tipc_node_put(n);
585cb8092d7SXin Long 		n = NULL;
586cb8092d7SXin Long 		goto exit;
587cb8092d7SXin Long 	}
5881a90632dSJon Paul Maloy 	tipc_node_get(n);
58931b102bbSKees Cook 	timer_setup(&n->timer, tipc_node_timeout, 0);
5904cbf8ac2STuong Lien 	/* Start a slow timer anyway, crypto needs it */
5914cbf8ac2STuong Lien 	n->keepalive_intv = 10000;
5924cbf8ac2STuong Lien 	intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
5934cbf8ac2STuong Lien 	if (!mod_timer(&n->timer, intv))
5944cbf8ac2STuong Lien 		tipc_node_get(n);
595d5c91fb7SJon Paul Maloy 	hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
596d5c91fb7SJon Paul Maloy 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
597d5c91fb7SJon Paul Maloy 		if (n->addr < temp_node->addr)
598d5c91fb7SJon Paul Maloy 			break;
599d5c91fb7SJon Paul Maloy 	}
600d5c91fb7SJon Paul Maloy 	list_add_tail_rcu(&n->list, &temp_node->list);
601ff2ebbfbSHoang Le 	/* Calculate cluster capabilities */
602ff2ebbfbSHoang Le 	tn->capabilities = TIPC_NODE_CAPABILITIES;
603ff2ebbfbSHoang Le 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
604ff2ebbfbSHoang Le 		tn->capabilities &= temp_node->capabilities;
605ff2ebbfbSHoang Le 	}
606ba5f6a86SHoang Le 	tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
607eb18a510STuong Lien 	trace_tipc_node_create(n, true, " ");
608b45db71bSJon Paul Maloy exit:
609f2f9800dSYing Xue 	spin_unlock_bh(&tn->node_list_lock);
6101a90632dSJon Paul Maloy 	return n;
611b97bf3fdSPer Liden }
612b97bf3fdSPer Liden 
tipc_node_calculate_timer(struct tipc_node * n,struct tipc_link * l)6138a1577c9SJon Paul Maloy static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
6148a1577c9SJon Paul Maloy {
61538206d59SJon Paul Maloy 	unsigned long tol = tipc_link_tolerance(l);
6168a1577c9SJon Paul Maloy 	unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
6178a1577c9SJon Paul Maloy 
6188a1577c9SJon Paul Maloy 	/* Link with lowest tolerance determines timer interval */
6195ca509fcSJon Paul Maloy 	if (intv < n->keepalive_intv)
6205ca509fcSJon Paul Maloy 		n->keepalive_intv = intv;
6218a1577c9SJon Paul Maloy 
6225ca509fcSJon Paul Maloy 	/* Ensure link's abort limit corresponds to current tolerance */
6235ca509fcSJon Paul Maloy 	tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
6248a1577c9SJon Paul Maloy }
6258a1577c9SJon Paul Maloy 
tipc_node_delete_from_list(struct tipc_node * node)6266a939f36SGhantaKrishnamurthy MohanKrishna static void tipc_node_delete_from_list(struct tipc_node *node)
627b97bf3fdSPer Liden {
6281ef6f7c9STuong Lien #ifdef CONFIG_TIPC_CRYPTO
6291ef6f7c9STuong Lien 	tipc_crypto_key_flush(node->crypto_rx);
6301ef6f7c9STuong Lien #endif
6318a0f6ebeSYing Xue 	list_del_rcu(&node->list);
6328a0f6ebeSYing Xue 	hlist_del_rcu(&node->hash);
633d25a0125SJon Paul Maloy 	tipc_node_put(node);
6346a939f36SGhantaKrishnamurthy MohanKrishna }
6356a939f36SGhantaKrishnamurthy MohanKrishna 
tipc_node_delete(struct tipc_node * node)6366a939f36SGhantaKrishnamurthy MohanKrishna static void tipc_node_delete(struct tipc_node *node)
6376a939f36SGhantaKrishnamurthy MohanKrishna {
638eb18a510STuong Lien 	trace_tipc_node_delete(node, true, " ");
6396a939f36SGhantaKrishnamurthy MohanKrishna 	tipc_node_delete_from_list(node);
640d25a0125SJon Paul Maloy 
641d25a0125SJon Paul Maloy 	del_timer_sync(&node->timer);
642d25a0125SJon Paul Maloy 	tipc_node_put(node);
643b97bf3fdSPer Liden }
644b97bf3fdSPer Liden 
tipc_node_stop(struct net * net)645f2f9800dSYing Xue void tipc_node_stop(struct net *net)
64646651c59SYing Xue {
647d25a0125SJon Paul Maloy 	struct tipc_net *tn = tipc_net(net);
64846651c59SYing Xue 	struct tipc_node *node, *t_node;
64946651c59SYing Xue 
650f2f9800dSYing Xue 	spin_lock_bh(&tn->node_list_lock);
651d25a0125SJon Paul Maloy 	list_for_each_entry_safe(node, t_node, &tn->node_list, list)
652d25a0125SJon Paul Maloy 		tipc_node_delete(node);
653f2f9800dSYing Xue 	spin_unlock_bh(&tn->node_list_lock);
65446651c59SYing Xue }
65546651c59SYing Xue 
tipc_node_subscribe(struct net * net,struct list_head * subscr,u32 addr)6561d7e1c25SJon Paul Maloy void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
6571d7e1c25SJon Paul Maloy {
6581d7e1c25SJon Paul Maloy 	struct tipc_node *n;
6591d7e1c25SJon Paul Maloy 
6601d7e1c25SJon Paul Maloy 	if (in_own_node(net, addr))
6611d7e1c25SJon Paul Maloy 		return;
6621d7e1c25SJon Paul Maloy 
6631d7e1c25SJon Paul Maloy 	n = tipc_node_find(net, addr);
6641d7e1c25SJon Paul Maloy 	if (!n) {
6651d7e1c25SJon Paul Maloy 		pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
6661d7e1c25SJon Paul Maloy 		return;
6671d7e1c25SJon Paul Maloy 	}
6685405ff6eSJon Paul Maloy 	tipc_node_write_lock(n);
6691d7e1c25SJon Paul Maloy 	list_add_tail(subscr, &n->publ_list);
67093f955aaSParthasarathy Bhuvaragan 	tipc_node_write_unlock_fast(n);
6711d7e1c25SJon Paul Maloy 	tipc_node_put(n);
6721d7e1c25SJon Paul Maloy }
6731d7e1c25SJon Paul Maloy 
tipc_node_unsubscribe(struct net * net,struct list_head * subscr,u32 addr)6741d7e1c25SJon Paul Maloy void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
6751d7e1c25SJon Paul Maloy {
6761d7e1c25SJon Paul Maloy 	struct tipc_node *n;
6771d7e1c25SJon Paul Maloy 
6781d7e1c25SJon Paul Maloy 	if (in_own_node(net, addr))
6791d7e1c25SJon Paul Maloy 		return;
6801d7e1c25SJon Paul Maloy 
6811d7e1c25SJon Paul Maloy 	n = tipc_node_find(net, addr);
6821d7e1c25SJon Paul Maloy 	if (!n) {
6831d7e1c25SJon Paul Maloy 		pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
6841d7e1c25SJon Paul Maloy 		return;
6851d7e1c25SJon Paul Maloy 	}
6865405ff6eSJon Paul Maloy 	tipc_node_write_lock(n);
6871d7e1c25SJon Paul Maloy 	list_del_init(subscr);
68893f955aaSParthasarathy Bhuvaragan 	tipc_node_write_unlock_fast(n);
6891d7e1c25SJon Paul Maloy 	tipc_node_put(n);
6901d7e1c25SJon Paul Maloy }
6911d7e1c25SJon Paul Maloy 
tipc_node_add_conn(struct net * net,u32 dnode,u32 port,u32 peer_port)692f2f9800dSYing Xue int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
69302be61a9SJon Paul Maloy {
69402be61a9SJon Paul Maloy 	struct tipc_node *node;
69502be61a9SJon Paul Maloy 	struct tipc_sock_conn *conn;
6968a0f6ebeSYing Xue 	int err = 0;
69702be61a9SJon Paul Maloy 
69834747539SYing Xue 	if (in_own_node(net, dnode))
69902be61a9SJon Paul Maloy 		return 0;
70002be61a9SJon Paul Maloy 
701f2f9800dSYing Xue 	node = tipc_node_find(net, dnode);
70202be61a9SJon Paul Maloy 	if (!node) {
70302be61a9SJon Paul Maloy 		pr_warn("Connecting sock to node 0x%x failed\n", dnode);
70402be61a9SJon Paul Maloy 		return -EHOSTUNREACH;
70502be61a9SJon Paul Maloy 	}
70602be61a9SJon Paul Maloy 	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
7078a0f6ebeSYing Xue 	if (!conn) {
7088a0f6ebeSYing Xue 		err = -EHOSTUNREACH;
7098a0f6ebeSYing Xue 		goto exit;
7108a0f6ebeSYing Xue 	}
71102be61a9SJon Paul Maloy 	conn->peer_node = dnode;
71202be61a9SJon Paul Maloy 	conn->port = port;
71302be61a9SJon Paul Maloy 	conn->peer_port = peer_port;
71402be61a9SJon Paul Maloy 
7155405ff6eSJon Paul Maloy 	tipc_node_write_lock(node);
71602be61a9SJon Paul Maloy 	list_add_tail(&conn->list, &node->conn_sks);
7175405ff6eSJon Paul Maloy 	tipc_node_write_unlock(node);
7188a0f6ebeSYing Xue exit:
7198a0f6ebeSYing Xue 	tipc_node_put(node);
7208a0f6ebeSYing Xue 	return err;
72102be61a9SJon Paul Maloy }
72202be61a9SJon Paul Maloy 
tipc_node_remove_conn(struct net * net,u32 dnode,u32 port)723f2f9800dSYing Xue void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
72402be61a9SJon Paul Maloy {
72502be61a9SJon Paul Maloy 	struct tipc_node *node;
72602be61a9SJon Paul Maloy 	struct tipc_sock_conn *conn, *safe;
72702be61a9SJon Paul Maloy 
72834747539SYing Xue 	if (in_own_node(net, dnode))
72902be61a9SJon Paul Maloy 		return;
73002be61a9SJon Paul Maloy 
731f2f9800dSYing Xue 	node = tipc_node_find(net, dnode);
73202be61a9SJon Paul Maloy 	if (!node)
73302be61a9SJon Paul Maloy 		return;
73402be61a9SJon Paul Maloy 
7355405ff6eSJon Paul Maloy 	tipc_node_write_lock(node);
73602be61a9SJon Paul Maloy 	list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
73702be61a9SJon Paul Maloy 		if (port != conn->port)
73802be61a9SJon Paul Maloy 			continue;
73902be61a9SJon Paul Maloy 		list_del(&conn->list);
74002be61a9SJon Paul Maloy 		kfree(conn);
74102be61a9SJon Paul Maloy 	}
7425405ff6eSJon Paul Maloy 	tipc_node_write_unlock(node);
7438a0f6ebeSYing Xue 	tipc_node_put(node);
74402be61a9SJon Paul Maloy }
74502be61a9SJon Paul Maloy 
tipc_node_clear_links(struct tipc_node * node)7466a939f36SGhantaKrishnamurthy MohanKrishna static void  tipc_node_clear_links(struct tipc_node *node)
7476a939f36SGhantaKrishnamurthy MohanKrishna {
7486a939f36SGhantaKrishnamurthy MohanKrishna 	int i;
7496a939f36SGhantaKrishnamurthy MohanKrishna 
7506a939f36SGhantaKrishnamurthy MohanKrishna 	for (i = 0; i < MAX_BEARERS; i++) {
7516a939f36SGhantaKrishnamurthy MohanKrishna 		struct tipc_link_entry *le = &node->links[i];
7526a939f36SGhantaKrishnamurthy MohanKrishna 
7536a939f36SGhantaKrishnamurthy MohanKrishna 		if (le->link) {
7546a939f36SGhantaKrishnamurthy MohanKrishna 			kfree(le->link);
7556a939f36SGhantaKrishnamurthy MohanKrishna 			le->link = NULL;
7566a939f36SGhantaKrishnamurthy MohanKrishna 			node->link_cnt--;
7576a939f36SGhantaKrishnamurthy MohanKrishna 		}
7586a939f36SGhantaKrishnamurthy MohanKrishna 	}
7596a939f36SGhantaKrishnamurthy MohanKrishna }
7606a939f36SGhantaKrishnamurthy MohanKrishna 
7616a939f36SGhantaKrishnamurthy MohanKrishna /* tipc_node_cleanup - delete nodes that does not
7626a939f36SGhantaKrishnamurthy MohanKrishna  * have active links for NODE_CLEANUP_AFTER time
7636a939f36SGhantaKrishnamurthy MohanKrishna  */
tipc_node_cleanup(struct tipc_node * peer)764ec835f89SJon Maloy static bool tipc_node_cleanup(struct tipc_node *peer)
7656a939f36SGhantaKrishnamurthy MohanKrishna {
766ff2ebbfbSHoang Le 	struct tipc_node *temp_node;
7676a939f36SGhantaKrishnamurthy MohanKrishna 	struct tipc_net *tn = tipc_net(peer->net);
7686a939f36SGhantaKrishnamurthy MohanKrishna 	bool deleted = false;
7696a939f36SGhantaKrishnamurthy MohanKrishna 
770ec835f89SJon Maloy 	/* If lock held by tipc_node_stop() the node will be deleted anyway */
771ec835f89SJon Maloy 	if (!spin_trylock_bh(&tn->node_list_lock))
772ec835f89SJon Maloy 		return false;
773ec835f89SJon Maloy 
7746a939f36SGhantaKrishnamurthy MohanKrishna 	tipc_node_write_lock(peer);
7756a939f36SGhantaKrishnamurthy MohanKrishna 
7766a939f36SGhantaKrishnamurthy MohanKrishna 	if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
7776a939f36SGhantaKrishnamurthy MohanKrishna 		tipc_node_clear_links(peer);
7786a939f36SGhantaKrishnamurthy MohanKrishna 		tipc_node_delete_from_list(peer);
7796a939f36SGhantaKrishnamurthy MohanKrishna 		deleted = true;
7806a939f36SGhantaKrishnamurthy MohanKrishna 	}
7816a939f36SGhantaKrishnamurthy MohanKrishna 	tipc_node_write_unlock(peer);
782ff2ebbfbSHoang Le 
7836708ef77SHoang Le 	if (!deleted) {
7846708ef77SHoang Le 		spin_unlock_bh(&tn->node_list_lock);
7856708ef77SHoang Le 		return deleted;
7866708ef77SHoang Le 	}
7876708ef77SHoang Le 
788ff2ebbfbSHoang Le 	/* Calculate cluster capabilities */
789ff2ebbfbSHoang Le 	tn->capabilities = TIPC_NODE_CAPABILITIES;
790ff2ebbfbSHoang Le 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
791ff2ebbfbSHoang Le 		tn->capabilities &= temp_node->capabilities;
792ff2ebbfbSHoang Le 	}
793ba5f6a86SHoang Le 	tipc_bcast_toggle_rcast(peer->net,
794ba5f6a86SHoang Le 				(tn->capabilities & TIPC_BCAST_RCAST));
7956a939f36SGhantaKrishnamurthy MohanKrishna 	spin_unlock_bh(&tn->node_list_lock);
7966a939f36SGhantaKrishnamurthy MohanKrishna 	return deleted;
7976a939f36SGhantaKrishnamurthy MohanKrishna }
7986a939f36SGhantaKrishnamurthy MohanKrishna 
7998a1577c9SJon Paul Maloy /* tipc_node_timeout - handle expiration of node timer
8008a1577c9SJon Paul Maloy  */
tipc_node_timeout(struct timer_list * t)80131b102bbSKees Cook static void tipc_node_timeout(struct timer_list *t)
8028a1577c9SJon Paul Maloy {
80331b102bbSKees Cook 	struct tipc_node *n = from_timer(n, t, timer);
804598411d7SJon Paul Maloy 	struct tipc_link_entry *le;
8058a1577c9SJon Paul Maloy 	struct sk_buff_head xmitq;
806759f29b6STung Nguyen 	int remains = n->link_cnt;
8078a1577c9SJon Paul Maloy 	int bearer_id;
8088a1577c9SJon Paul Maloy 	int rc = 0;
8098a1577c9SJon Paul Maloy 
810eb18a510STuong Lien 	trace_tipc_node_timeout(n, false, " ");
8116a939f36SGhantaKrishnamurthy MohanKrishna 	if (!node_is_up(n) && tipc_node_cleanup(n)) {
8126a939f36SGhantaKrishnamurthy MohanKrishna 		/*Removing the reference of Timer*/
8136a939f36SGhantaKrishnamurthy MohanKrishna 		tipc_node_put(n);
8146a939f36SGhantaKrishnamurthy MohanKrishna 		return;
8156a939f36SGhantaKrishnamurthy MohanKrishna 	}
8166a939f36SGhantaKrishnamurthy MohanKrishna 
817fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
818fc1b6d6dSTuong Lien 	/* Take any crypto key related actions first */
819fc1b6d6dSTuong Lien 	tipc_crypto_timeout(n->crypto_rx);
820fc1b6d6dSTuong Lien #endif
8218a1577c9SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
8228a1577c9SJon Paul Maloy 
823f5d6c3e5SHoang Le 	/* Initial node interval to value larger (10 seconds), then it will be
824f5d6c3e5SHoang Le 	 * recalculated with link lowest tolerance
825f5d6c3e5SHoang Le 	 */
826f5d6c3e5SHoang Le 	tipc_node_read_lock(n);
827f5d6c3e5SHoang Le 	n->keepalive_intv = 10000;
828f5d6c3e5SHoang Le 	tipc_node_read_unlock(n);
829759f29b6STung Nguyen 	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
8305405ff6eSJon Paul Maloy 		tipc_node_read_lock(n);
831598411d7SJon Paul Maloy 		le = &n->links[bearer_id];
832598411d7SJon Paul Maloy 		if (le->link) {
833759f29b6STung Nguyen 			spin_lock_bh(&le->lock);
8348a1577c9SJon Paul Maloy 			/* Link tolerance may change asynchronously: */
835598411d7SJon Paul Maloy 			tipc_node_calculate_timer(n, le->link);
836598411d7SJon Paul Maloy 			rc = tipc_link_timeout(le->link, &xmitq);
8372312bf61SJon Paul Maloy 			spin_unlock_bh(&le->lock);
838759f29b6STung Nguyen 			remains--;
839759f29b6STung Nguyen 		}
8405405ff6eSJon Paul Maloy 		tipc_node_read_unlock(n);
841fc1b6d6dSTuong Lien 		tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr, n);
842598411d7SJon Paul Maloy 		if (rc & TIPC_LINK_DOWN_EVT)
843598411d7SJon Paul Maloy 			tipc_node_link_down(n, bearer_id, false);
8448a1577c9SJon Paul Maloy 	}
8455ca509fcSJon Paul Maloy 	mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
8468a1577c9SJon Paul Maloy }
8478a1577c9SJon Paul Maloy 
848b97bf3fdSPer Liden /**
849598411d7SJon Paul Maloy  * __tipc_node_link_up - handle addition of link
8504476441eSRandy Dunlap  * @n: target tipc_node
8514476441eSRandy Dunlap  * @bearer_id: id of the bearer
8524476441eSRandy Dunlap  * @xmitq: queue for messages to be xmited on
853598411d7SJon Paul Maloy  * Node lock must be held by caller
854b97bf3fdSPer Liden  * Link becomes active (alone or shared) or standby, depending on its priority.
855b97bf3fdSPer Liden  */
__tipc_node_link_up(struct tipc_node * n,int bearer_id,struct sk_buff_head * xmitq)856598411d7SJon Paul Maloy static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
8576e498158SJon Paul Maloy 				struct sk_buff_head *xmitq)
858b97bf3fdSPer Liden {
85936e78a46SJon Paul Maloy 	int *slot0 = &n->active_links[0];
86036e78a46SJon Paul Maloy 	int *slot1 = &n->active_links[1];
8616e498158SJon Paul Maloy 	struct tipc_link *ol = node_active_link(n, 0);
8626e498158SJon Paul Maloy 	struct tipc_link *nl = n->links[bearer_id].link;
863b97bf3fdSPer Liden 
864e7142c34SJon Paul Maloy 	if (!nl || tipc_link_is_up(nl))
86573f646ceSJon Paul Maloy 		return;
86673f646ceSJon Paul Maloy 
86773f646ceSJon Paul Maloy 	tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
86873f646ceSJon Paul Maloy 	if (!tipc_link_is_up(nl))
869598411d7SJon Paul Maloy 		return;
870598411d7SJon Paul Maloy 
8719d13ec65SJon Paul Maloy 	n->working_links++;
8729d13ec65SJon Paul Maloy 	n->action_flags |= TIPC_NOTIFY_LINK_UP;
87338206d59SJon Paul Maloy 	n->link_id = tipc_link_id(nl);
8746e498158SJon Paul Maloy 
8756e498158SJon Paul Maloy 	/* Leave room for tunnel header when returning 'mtu' to users: */
876fc1b6d6dSTuong Lien 	n->links[bearer_id].mtu = tipc_link_mss(nl);
8777b8613e0SYing Xue 
878cbeb83caSJon Paul Maloy 	tipc_bearer_add_dest(n->net, bearer_id, n->addr);
879b06b281eSJon Paul Maloy 	tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
880cbeb83caSJon Paul Maloy 
8813fa9cacdSErik Hugne 	pr_debug("Established link <%s> on network plane %c\n",
88238206d59SJon Paul Maloy 		 tipc_link_name(nl), tipc_link_plane(nl));
883eb18a510STuong Lien 	trace_tipc_node_link_up(n, true, " ");
884b97bf3fdSPer Liden 
88534b9cd64SJon Paul Maloy 	/* Ensure that a STATE message goes first */
88634b9cd64SJon Paul Maloy 	tipc_link_build_state_msg(nl, xmitq);
88734b9cd64SJon Paul Maloy 
8886e498158SJon Paul Maloy 	/* First link? => give it both slots */
8896e498158SJon Paul Maloy 	if (!ol) {
89036e78a46SJon Paul Maloy 		*slot0 = bearer_id;
89136e78a46SJon Paul Maloy 		*slot1 = bearer_id;
89252666986SJon Paul Maloy 		tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
89352666986SJon Paul Maloy 		n->action_flags |= TIPC_NOTIFY_NODE_UP;
894def22c47SJon Paul Maloy 		tipc_link_set_active(nl, true);
895b06b281eSJon Paul Maloy 		tipc_bcast_add_peer(n->net, nl, xmitq);
8969d13ec65SJon Paul Maloy 		return;
897b97bf3fdSPer Liden 	}
89836e78a46SJon Paul Maloy 
8996e498158SJon Paul Maloy 	/* Second link => redistribute slots */
90038206d59SJon Paul Maloy 	if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
90138206d59SJon Paul Maloy 		pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
90236e78a46SJon Paul Maloy 		*slot0 = bearer_id;
90336e78a46SJon Paul Maloy 		*slot1 = bearer_id;
904c72fa872SJon Paul Maloy 		tipc_link_set_active(nl, true);
905c72fa872SJon Paul Maloy 		tipc_link_set_active(ol, false);
90638206d59SJon Paul Maloy 	} else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
907c72fa872SJon Paul Maloy 		tipc_link_set_active(nl, true);
908c49a0a84SJon Paul Maloy 		*slot1 = bearer_id;
9096e498158SJon Paul Maloy 	} else {
91038206d59SJon Paul Maloy 		pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
9116e498158SJon Paul Maloy 	}
9126e498158SJon Paul Maloy 
9136e498158SJon Paul Maloy 	/* Prepare synchronization with first link */
9146e498158SJon Paul Maloy 	tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
915b97bf3fdSPer Liden }
916b97bf3fdSPer Liden 
917b97bf3fdSPer Liden /**
918598411d7SJon Paul Maloy  * tipc_node_link_up - handle addition of link
9194476441eSRandy Dunlap  * @n: target tipc_node
9204476441eSRandy Dunlap  * @bearer_id: id of the bearer
9214476441eSRandy Dunlap  * @xmitq: queue for messages to be xmited on
922598411d7SJon Paul Maloy  *
923598411d7SJon Paul Maloy  * Link becomes active (alone or shared) or standby, depending on its priority.
924b97bf3fdSPer Liden  */
tipc_node_link_up(struct tipc_node * n,int bearer_id,struct sk_buff_head * xmitq)925598411d7SJon Paul Maloy static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
926598411d7SJon Paul Maloy 			      struct sk_buff_head *xmitq)
927b97bf3fdSPer Liden {
928de7e07f9SJon Paul Maloy 	struct tipc_media_addr *maddr;
929de7e07f9SJon Paul Maloy 
9305405ff6eSJon Paul Maloy 	tipc_node_write_lock(n);
931598411d7SJon Paul Maloy 	__tipc_node_link_up(n, bearer_id, xmitq);
932de7e07f9SJon Paul Maloy 	maddr = &n->links[bearer_id].maddr;
933fc1b6d6dSTuong Lien 	tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr, n);
9345405ff6eSJon Paul Maloy 	tipc_node_write_unlock(n);
935598411d7SJon Paul Maloy }
936598411d7SJon Paul Maloy 
937598411d7SJon Paul Maloy /**
938c0b14a08STuong Lien  * tipc_node_link_failover() - start failover in case "half-failover"
939c0b14a08STuong Lien  *
940c0b14a08STuong Lien  * This function is only called in a very special situation where link
941c0b14a08STuong Lien  * failover can be already started on peer node but not on this node.
9425fcb7d47SRandy Dunlap  * This can happen when e.g.::
9435fcb7d47SRandy Dunlap  *
944c0b14a08STuong Lien  *	1. Both links <1A-2A>, <1B-2B> down
945c0b14a08STuong Lien  *	2. Link endpoint 2A up, but 1A still down (e.g. due to network
946c0b14a08STuong Lien  *	disturbance, wrong session, etc.)
947c0b14a08STuong Lien  *	3. Link <1B-2B> up
948c0b14a08STuong Lien  *	4. Link endpoint 2A down (e.g. due to link tolerance timeout)
949d0f84d08STuong Lien  *	5. Node 2 starts failover onto link <1B-2B>
950c0b14a08STuong Lien  *
951d0f84d08STuong Lien  *	==> Node 1 does never start link/node failover!
952c0b14a08STuong Lien  *
953c0b14a08STuong Lien  * @n: tipc node structure
954c0b14a08STuong Lien  * @l: link peer endpoint failingover (- can be NULL)
955c0b14a08STuong Lien  * @tnl: tunnel link
956c0b14a08STuong Lien  * @xmitq: queue for messages to be xmited on tnl link later
957c0b14a08STuong Lien  */
tipc_node_link_failover(struct tipc_node * n,struct tipc_link * l,struct tipc_link * tnl,struct sk_buff_head * xmitq)958c0b14a08STuong Lien static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
959c0b14a08STuong Lien 				    struct tipc_link *tnl,
960c0b14a08STuong Lien 				    struct sk_buff_head *xmitq)
961c0b14a08STuong Lien {
962c0b14a08STuong Lien 	/* Avoid to be "self-failover" that can never end */
963c0b14a08STuong Lien 	if (!tipc_link_is_up(tnl))
964c0b14a08STuong Lien 		return;
965c0b14a08STuong Lien 
966d0f84d08STuong Lien 	/* Don't rush, failure link may be in the process of resetting */
967d0f84d08STuong Lien 	if (l && !tipc_link_is_reset(l))
968d0f84d08STuong Lien 		return;
969d0f84d08STuong Lien 
970c0b14a08STuong Lien 	tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
971c0b14a08STuong Lien 	tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
972c0b14a08STuong Lien 
973c0b14a08STuong Lien 	n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
974c0b14a08STuong Lien 	tipc_link_failover_prepare(l, tnl, xmitq);
975c0b14a08STuong Lien 
976c0b14a08STuong Lien 	if (l)
977c0b14a08STuong Lien 		tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
978c0b14a08STuong Lien 	tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
979c0b14a08STuong Lien }
980c0b14a08STuong Lien 
981c0b14a08STuong Lien /**
982598411d7SJon Paul Maloy  * __tipc_node_link_down - handle loss of link
9834476441eSRandy Dunlap  * @n: target tipc_node
9844476441eSRandy Dunlap  * @bearer_id: id of the bearer
9854476441eSRandy Dunlap  * @xmitq: queue for messages to be xmited on
9864476441eSRandy Dunlap  * @maddr: output media address of the bearer
987598411d7SJon Paul Maloy  */
__tipc_node_link_down(struct tipc_node * n,int * bearer_id,struct sk_buff_head * xmitq,struct tipc_media_addr ** maddr)988598411d7SJon Paul Maloy static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
989598411d7SJon Paul Maloy 				  struct sk_buff_head *xmitq,
990598411d7SJon Paul Maloy 				  struct tipc_media_addr **maddr)
991598411d7SJon Paul Maloy {
992598411d7SJon Paul Maloy 	struct tipc_link_entry *le = &n->links[*bearer_id];
99336e78a46SJon Paul Maloy 	int *slot0 = &n->active_links[0];
99436e78a46SJon Paul Maloy 	int *slot1 = &n->active_links[1];
99538206d59SJon Paul Maloy 	int i, highest = 0, prio;
9966e498158SJon Paul Maloy 	struct tipc_link *l, *_l, *tnl;
997b97bf3fdSPer Liden 
998598411d7SJon Paul Maloy 	l = n->links[*bearer_id].link;
999662921cdSJon Paul Maloy 	if (!l || tipc_link_is_reset(l))
1000655fb243SJon Paul Maloy 		return;
1001655fb243SJon Paul Maloy 
10029d13ec65SJon Paul Maloy 	n->working_links--;
10039d13ec65SJon Paul Maloy 	n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
100438206d59SJon Paul Maloy 	n->link_id = tipc_link_id(l);
10055392d646SAllan Stephens 
1006598411d7SJon Paul Maloy 	tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
1007655fb243SJon Paul Maloy 
10083fa9cacdSErik Hugne 	pr_debug("Lost link <%s> on network plane %c\n",
100938206d59SJon Paul Maloy 		 tipc_link_name(l), tipc_link_plane(l));
1010b97bf3fdSPer Liden 
101136e78a46SJon Paul Maloy 	/* Select new active link if any available */
101236e78a46SJon Paul Maloy 	*slot0 = INVALID_BEARER_ID;
101336e78a46SJon Paul Maloy 	*slot1 = INVALID_BEARER_ID;
101436e78a46SJon Paul Maloy 	for (i = 0; i < MAX_BEARERS; i++) {
101536e78a46SJon Paul Maloy 		_l = n->links[i].link;
101636e78a46SJon Paul Maloy 		if (!_l || !tipc_link_is_up(_l))
101736e78a46SJon Paul Maloy 			continue;
1018655fb243SJon Paul Maloy 		if (_l == l)
1019655fb243SJon Paul Maloy 			continue;
102038206d59SJon Paul Maloy 		prio = tipc_link_prio(_l);
102138206d59SJon Paul Maloy 		if (prio < highest)
102236e78a46SJon Paul Maloy 			continue;
102338206d59SJon Paul Maloy 		if (prio > highest) {
102438206d59SJon Paul Maloy 			highest = prio;
102536e78a46SJon Paul Maloy 			*slot0 = i;
102636e78a46SJon Paul Maloy 			*slot1 = i;
102736e78a46SJon Paul Maloy 			continue;
102836e78a46SJon Paul Maloy 		}
102936e78a46SJon Paul Maloy 		*slot1 = i;
103036e78a46SJon Paul Maloy 	}
1031655fb243SJon Paul Maloy 
103238077b8eSJon Maloy 	if (!node_is_up(n)) {
1033c8199300SJon Paul Maloy 		if (tipc_link_peer_is_down(l))
1034c8199300SJon Paul Maloy 			tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1035c8199300SJon Paul Maloy 		tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
103626574db0STuong Lien 		trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
1037c8199300SJon Paul Maloy 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
1038655fb243SJon Paul Maloy 		tipc_link_reset(l);
1039282b3a05SJon Paul Maloy 		tipc_link_build_reset_msg(l, xmitq);
1040282b3a05SJon Paul Maloy 		*maddr = &n->links[*bearer_id].maddr;
1041598411d7SJon Paul Maloy 		node_lost_contact(n, &le->inputq);
1042b06b281eSJon Paul Maloy 		tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
10436e498158SJon Paul Maloy 		return;
10446e498158SJon Paul Maloy 	}
1045b06b281eSJon Paul Maloy 	tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
10466e498158SJon Paul Maloy 
10476e498158SJon Paul Maloy 	/* There is still a working link => initiate failover */
104838206d59SJon Paul Maloy 	*bearer_id = n->active_links[0];
104938206d59SJon Paul Maloy 	tnl = n->links[*bearer_id].link;
10505ae2f8e6SJon Paul Maloy 	tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
10515ae2f8e6SJon Paul Maloy 	tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
105238206d59SJon Paul Maloy 	n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
1053598411d7SJon Paul Maloy 	tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
105426574db0STuong Lien 	trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
10556e498158SJon Paul Maloy 	tipc_link_reset(l);
1056c8199300SJon Paul Maloy 	tipc_link_fsm_evt(l, LINK_RESET_EVT);
1057662921cdSJon Paul Maloy 	tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1058598411d7SJon Paul Maloy 	tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
105938206d59SJon Paul Maloy 	*maddr = &n->links[*bearer_id].maddr;
1060598411d7SJon Paul Maloy }
1061598411d7SJon Paul Maloy 
tipc_node_link_down(struct tipc_node * n,int bearer_id,bool delete)1062598411d7SJon Paul Maloy static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
1063598411d7SJon Paul Maloy {
1064598411d7SJon Paul Maloy 	struct tipc_link_entry *le = &n->links[bearer_id];
1065737889efSJon Maloy 	struct tipc_media_addr *maddr = NULL;
106673f646ceSJon Paul Maloy 	struct tipc_link *l = le->link;
106735c55c98SJon Paul Maloy 	int old_bearer_id = bearer_id;
1068737889efSJon Maloy 	struct sk_buff_head xmitq;
1069598411d7SJon Paul Maloy 
107073f646ceSJon Paul Maloy 	if (!l)
107173f646ceSJon Paul Maloy 		return;
107273f646ceSJon Paul Maloy 
1073598411d7SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
1074598411d7SJon Paul Maloy 
10755405ff6eSJon Paul Maloy 	tipc_node_write_lock(n);
107673f646ceSJon Paul Maloy 	if (!tipc_link_is_establishing(l)) {
1077598411d7SJon Paul Maloy 		__tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
107891986ee1STuong Lien 	} else {
107991986ee1STuong Lien 		/* Defuse pending tipc_node_link_up() */
108091986ee1STuong Lien 		tipc_link_reset(l);
108191986ee1STuong Lien 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
108291986ee1STuong Lien 	}
108373f646ceSJon Paul Maloy 	if (delete) {
108473f646ceSJon Paul Maloy 		kfree(l);
1085598411d7SJon Paul Maloy 		le->link = NULL;
1086598411d7SJon Paul Maloy 		n->link_cnt--;
1087598411d7SJon Paul Maloy 	}
1088eb18a510STuong Lien 	trace_tipc_node_link_down(n, true, "node link down or deleted!");
10895405ff6eSJon Paul Maloy 	tipc_node_write_unlock(n);
109035c55c98SJon Paul Maloy 	if (delete)
109135c55c98SJon Paul Maloy 		tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
1092737889efSJon Maloy 	if (!skb_queue_empty(&xmitq))
1093fc1b6d6dSTuong Lien 		tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr, n);
1094598411d7SJon Paul Maloy 	tipc_sk_rcv(n->net, &le->inputq);
1095b97bf3fdSPer Liden }
1096b97bf3fdSPer Liden 
node_is_up(struct tipc_node * n)109738077b8eSJon Maloy static bool node_is_up(struct tipc_node *n)
1098b97bf3fdSPer Liden {
109936e78a46SJon Paul Maloy 	return n->active_links[0] != INVALID_BEARER_ID;
1100b97bf3fdSPer Liden }
1101b97bf3fdSPer Liden 
tipc_node_is_up(struct net * net,u32 addr)110238077b8eSJon Maloy bool tipc_node_is_up(struct net *net, u32 addr)
110338077b8eSJon Maloy {
110438077b8eSJon Maloy 	struct tipc_node *n;
110538077b8eSJon Maloy 	bool retval = false;
110638077b8eSJon Maloy 
110738077b8eSJon Maloy 	if (in_own_node(net, addr))
110838077b8eSJon Maloy 		return true;
110938077b8eSJon Maloy 
111038077b8eSJon Maloy 	n = tipc_node_find(net, addr);
111138077b8eSJon Maloy 	if (!n)
111238077b8eSJon Maloy 		return false;
111338077b8eSJon Maloy 	retval = node_is_up(n);
111438077b8eSJon Maloy 	tipc_node_put(n);
111538077b8eSJon Maloy 	return retval;
111638077b8eSJon Maloy }
111738077b8eSJon Maloy 
tipc_node_suggest_addr(struct net * net,u32 addr)111825b0b9c4SJon Maloy static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
111925b0b9c4SJon Maloy {
112025b0b9c4SJon Maloy 	struct tipc_node *n;
112125b0b9c4SJon Maloy 
112225b0b9c4SJon Maloy 	addr ^= tipc_net(net)->random;
112325b0b9c4SJon Maloy 	while ((n = tipc_node_find(net, addr))) {
112425b0b9c4SJon Maloy 		tipc_node_put(n);
112525b0b9c4SJon Maloy 		addr++;
112625b0b9c4SJon Maloy 	}
112725b0b9c4SJon Maloy 	return addr;
112825b0b9c4SJon Maloy }
112925b0b9c4SJon Maloy 
113025b0b9c4SJon Maloy /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
11312a57f182SJon Maloy  * Returns suggested address if any, otherwise 0
113225b0b9c4SJon Maloy  */
tipc_node_try_addr(struct net * net,u8 * id,u32 addr)113325b0b9c4SJon Maloy u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
113425b0b9c4SJon Maloy {
113525b0b9c4SJon Maloy 	struct tipc_net *tn = tipc_net(net);
113625b0b9c4SJon Maloy 	struct tipc_node *n;
11374cbf8ac2STuong Lien 	bool preliminary;
11384cbf8ac2STuong Lien 	u32 sugg_addr;
113925b0b9c4SJon Maloy 
114025b0b9c4SJon Maloy 	/* Suggest new address if some other peer is using this one */
114125b0b9c4SJon Maloy 	n = tipc_node_find(net, addr);
114225b0b9c4SJon Maloy 	if (n) {
114325b0b9c4SJon Maloy 		if (!memcmp(n->peer_id, id, NODE_ID_LEN))
114425b0b9c4SJon Maloy 			addr = 0;
114525b0b9c4SJon Maloy 		tipc_node_put(n);
114625b0b9c4SJon Maloy 		if (!addr)
114725b0b9c4SJon Maloy 			return 0;
114825b0b9c4SJon Maloy 		return tipc_node_suggest_addr(net, addr);
114925b0b9c4SJon Maloy 	}
115025b0b9c4SJon Maloy 
115125b0b9c4SJon Maloy 	/* Suggest previously used address if peer is known */
115225b0b9c4SJon Maloy 	n = tipc_node_find_by_id(net, id);
115325b0b9c4SJon Maloy 	if (n) {
11544cbf8ac2STuong Lien 		sugg_addr = n->addr;
11554cbf8ac2STuong Lien 		preliminary = n->preliminary;
115625b0b9c4SJon Maloy 		tipc_node_put(n);
11574cbf8ac2STuong Lien 		if (!preliminary)
11584cbf8ac2STuong Lien 			return sugg_addr;
115925b0b9c4SJon Maloy 	}
11602a57f182SJon Maloy 
11612a57f182SJon Maloy 	/* Even this node may be in conflict */
116225b0b9c4SJon Maloy 	if (tn->trial_addr == addr)
116325b0b9c4SJon Maloy 		return tipc_node_suggest_addr(net, addr);
116425b0b9c4SJon Maloy 
11652a57f182SJon Maloy 	return 0;
116625b0b9c4SJon Maloy }
116725b0b9c4SJon Maloy 
tipc_node_check_dest(struct net * net,u32 addr,u8 * peer_id,struct tipc_bearer * b,u16 capabilities,u32 signature,u32 hash_mixes,struct tipc_media_addr * maddr,bool * respond,bool * dupl_addr)116825b0b9c4SJon Maloy void tipc_node_check_dest(struct net *net, u32 addr,
116925b0b9c4SJon Maloy 			  u8 *peer_id, struct tipc_bearer *b,
1170f73b1281SHoang Le 			  u16 capabilities, u32 signature, u32 hash_mixes,
1171cf148816SJon Paul Maloy 			  struct tipc_media_addr *maddr,
1172cf148816SJon Paul Maloy 			  bool *respond, bool *dupl_addr)
1173d3a43b90SJon Paul Maloy {
1174cf148816SJon Paul Maloy 	struct tipc_node *n;
1175cb8092d7SXin Long 	struct tipc_link *l;
1176440d8963SJon Paul Maloy 	struct tipc_link_entry *le;
1177cf148816SJon Paul Maloy 	bool addr_match = false;
1178cf148816SJon Paul Maloy 	bool sign_match = false;
1179cf148816SJon Paul Maloy 	bool link_up = false;
1180c244c092STung Nguyen 	bool link_is_reset = false;
1181cf148816SJon Paul Maloy 	bool accept_addr = false;
1182c244c092STung Nguyen 	bool reset = false;
11830e05498eSJon Paul Maloy 	char *if_name;
11845ca509fcSJon Paul Maloy 	unsigned long intv;
1185d949cfedSLUU Duc Canh 	u16 session;
1186440d8963SJon Paul Maloy 
1187cf148816SJon Paul Maloy 	*dupl_addr = false;
1188cf148816SJon Paul Maloy 	*respond = false;
1189cf148816SJon Paul Maloy 
11904cbf8ac2STuong Lien 	n = tipc_node_create(net, addr, peer_id, capabilities, hash_mixes,
11914cbf8ac2STuong Lien 			     false);
1192cf148816SJon Paul Maloy 	if (!n)
1193cf148816SJon Paul Maloy 		return;
1194cf148816SJon Paul Maloy 
11955405ff6eSJon Paul Maloy 	tipc_node_write_lock(n);
1196cf148816SJon Paul Maloy 
1197440d8963SJon Paul Maloy 	le = &n->links[b->identity];
1198cf148816SJon Paul Maloy 
1199cf148816SJon Paul Maloy 	/* Prepare to validate requesting node's signature and media address */
1200440d8963SJon Paul Maloy 	l = le->link;
1201cf148816SJon Paul Maloy 	link_up = l && tipc_link_is_up(l);
1202c244c092STung Nguyen 	link_is_reset = l && tipc_link_is_reset(l);
1203440d8963SJon Paul Maloy 	addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
1204cf148816SJon Paul Maloy 	sign_match = (signature == n->signature);
1205cf148816SJon Paul Maloy 
1206cf148816SJon Paul Maloy 	/* These three flags give us eight permutations: */
1207cf148816SJon Paul Maloy 
1208cf148816SJon Paul Maloy 	if (sign_match && addr_match && link_up) {
1209c244c092STung Nguyen 		/* All is fine. Ignore requests. */
1210d408bef4SHoang Le 		/* Peer node is not a container/local namespace */
1211d408bef4SHoang Le 		if (!n->peer_hash_mix)
1212d408bef4SHoang Le 			n->peer_hash_mix = hash_mixes;
1213cf148816SJon Paul Maloy 	} else if (sign_match && addr_match && !link_up) {
1214cf148816SJon Paul Maloy 		/* Respond. The link will come up in due time */
1215cf148816SJon Paul Maloy 		*respond = true;
1216cf148816SJon Paul Maloy 	} else if (sign_match && !addr_match && link_up) {
1217cf148816SJon Paul Maloy 		/* Peer has changed i/f address without rebooting.
1218cf148816SJon Paul Maloy 		 * If so, the link will reset soon, and the next
1219cf148816SJon Paul Maloy 		 * discovery will be accepted. So we can ignore it.
122015139bcbSgushengxian 		 * It may also be a cloned or malicious peer having
1221cf148816SJon Paul Maloy 		 * chosen the same node address and signature as an
1222cf148816SJon Paul Maloy 		 * existing one.
1223cf148816SJon Paul Maloy 		 * Ignore requests until the link goes down, if ever.
1224cf148816SJon Paul Maloy 		 */
1225cf148816SJon Paul Maloy 		*dupl_addr = true;
1226cf148816SJon Paul Maloy 	} else if (sign_match && !addr_match && !link_up) {
1227cf148816SJon Paul Maloy 		/* Peer link has changed i/f address without rebooting.
1228cf148816SJon Paul Maloy 		 * It may also be a cloned or malicious peer; we can't
1229cf148816SJon Paul Maloy 		 * distinguish between the two.
1230cf148816SJon Paul Maloy 		 * The signature is correct, so we must accept.
1231cf148816SJon Paul Maloy 		 */
1232cf148816SJon Paul Maloy 		accept_addr = true;
1233cf148816SJon Paul Maloy 		*respond = true;
1234c244c092STung Nguyen 		reset = true;
1235cf148816SJon Paul Maloy 	} else if (!sign_match && addr_match && link_up) {
1236cf148816SJon Paul Maloy 		/* Peer node rebooted. Two possibilities:
1237cf148816SJon Paul Maloy 		 *  - Delayed re-discovery; this link endpoint has already
1238cf148816SJon Paul Maloy 		 *    reset and re-established contact with the peer, before
1239cf148816SJon Paul Maloy 		 *    receiving a discovery message from that node.
1240cf148816SJon Paul Maloy 		 *    (The peer happened to receive one from this node first).
1241cf148816SJon Paul Maloy 		 *  - The peer came back so fast that our side has not
1242cf148816SJon Paul Maloy 		 *    discovered it yet. Probing from this side will soon
1243cf148816SJon Paul Maloy 		 *    reset the link, since there can be no working link
1244cf148816SJon Paul Maloy 		 *    endpoint at the peer end, and the link will re-establish.
1245cf148816SJon Paul Maloy 		 *  Accept the signature, since it comes from a known peer.
1246cf148816SJon Paul Maloy 		 */
1247cf148816SJon Paul Maloy 		n->signature = signature;
1248cf148816SJon Paul Maloy 	} else if (!sign_match && addr_match && !link_up) {
1249cf148816SJon Paul Maloy 		/*  The peer node has rebooted.
1250cf148816SJon Paul Maloy 		 *  Accept signature, since it is a known peer.
1251cf148816SJon Paul Maloy 		 */
1252cf148816SJon Paul Maloy 		n->signature = signature;
1253cf148816SJon Paul Maloy 		*respond = true;
1254cf148816SJon Paul Maloy 	} else if (!sign_match && !addr_match && link_up) {
1255cf148816SJon Paul Maloy 		/* Peer rebooted with new address, or a new/duplicate peer.
1256cf148816SJon Paul Maloy 		 * Ignore until the link goes down, if ever.
1257cf148816SJon Paul Maloy 		 */
1258cf148816SJon Paul Maloy 		*dupl_addr = true;
1259cf148816SJon Paul Maloy 	} else if (!sign_match && !addr_match && !link_up) {
1260cf148816SJon Paul Maloy 		/* Peer rebooted with new address, or it is a new peer.
1261cf148816SJon Paul Maloy 		 * Accept signature and address.
1262cf148816SJon Paul Maloy 		 */
1263cf148816SJon Paul Maloy 		n->signature = signature;
1264cf148816SJon Paul Maloy 		accept_addr = true;
1265cf148816SJon Paul Maloy 		*respond = true;
1266c244c092STung Nguyen 		reset = true;
1267d3a43b90SJon Paul Maloy 	}
1268d3a43b90SJon Paul Maloy 
1269cf148816SJon Paul Maloy 	if (!accept_addr)
1270cf148816SJon Paul Maloy 		goto exit;
1271d3a43b90SJon Paul Maloy 
1272cf148816SJon Paul Maloy 	/* Now create new link if not already existing */
12738a1577c9SJon Paul Maloy 	if (!l) {
127420263641SJon Maloy 		if (n->link_cnt == 2)
1275440d8963SJon Paul Maloy 			goto exit;
127620263641SJon Maloy 
12770e05498eSJon Paul Maloy 		if_name = strchr(b->name, ':') + 1;
1278d949cfedSLUU Duc Canh 		get_random_bytes(&session, sizeof(u16));
1279c72fa872SJon Paul Maloy 		if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
12800e05498eSJon Paul Maloy 				      b->net_plane, b->mtu, b->priority,
128116ad3f40SJon Maloy 				      b->min_win, b->max_win, session,
128225b0b9c4SJon Maloy 				      tipc_own_addr(net), addr, peer_id,
12832af5ae37SJon Paul Maloy 				      n->capabilities,
128452666986SJon Paul Maloy 				      tipc_bc_sndlink(n->net), n->bc_entry.link,
128552666986SJon Paul Maloy 				      &le->inputq,
128652666986SJon Paul Maloy 				      &n->bc_entry.namedq, &l)) {
1287cf148816SJon Paul Maloy 			*respond = false;
1288cf148816SJon Paul Maloy 			goto exit;
1289cf148816SJon Paul Maloy 		}
129026574db0STuong Lien 		trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
1291440d8963SJon Paul Maloy 		tipc_link_reset(l);
1292c8199300SJon Paul Maloy 		tipc_link_fsm_evt(l, LINK_RESET_EVT);
129317b20630SJon Paul Maloy 		if (n->state == NODE_FAILINGOVER)
129417b20630SJon Paul Maloy 			tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1295c244c092STung Nguyen 		link_is_reset = tipc_link_is_reset(l);
1296440d8963SJon Paul Maloy 		le->link = l;
1297440d8963SJon Paul Maloy 		n->link_cnt++;
12988a1577c9SJon Paul Maloy 		tipc_node_calculate_timer(n, l);
12995ca509fcSJon Paul Maloy 		if (n->link_cnt == 1) {
13005ca509fcSJon Paul Maloy 			intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
13015ca509fcSJon Paul Maloy 			if (!mod_timer(&n->timer, intv))
13028a1577c9SJon Paul Maloy 				tipc_node_get(n);
13038a1577c9SJon Paul Maloy 		}
13045ca509fcSJon Paul Maloy 	}
1305440d8963SJon Paul Maloy 	memcpy(&le->maddr, maddr, sizeof(*maddr));
1306cf148816SJon Paul Maloy exit:
13075405ff6eSJon Paul Maloy 	tipc_node_write_unlock(n);
1308c244c092STung Nguyen 	if (reset && !link_is_reset)
1309598411d7SJon Paul Maloy 		tipc_node_link_down(n, b->identity, false);
1310cf148816SJon Paul Maloy 	tipc_node_put(n);
1311d3a43b90SJon Paul Maloy }
1312d3a43b90SJon Paul Maloy 
tipc_node_delete_links(struct net * net,int bearer_id)13136144a996SJon Paul Maloy void tipc_node_delete_links(struct net *net, int bearer_id)
13146144a996SJon Paul Maloy {
13156144a996SJon Paul Maloy 	struct tipc_net *tn = net_generic(net, tipc_net_id);
13166144a996SJon Paul Maloy 	struct tipc_node *n;
13176144a996SJon Paul Maloy 
13186144a996SJon Paul Maloy 	rcu_read_lock();
13196144a996SJon Paul Maloy 	list_for_each_entry_rcu(n, &tn->node_list, list) {
1320598411d7SJon Paul Maloy 		tipc_node_link_down(n, bearer_id, true);
13216144a996SJon Paul Maloy 	}
13226144a996SJon Paul Maloy 	rcu_read_unlock();
13236144a996SJon Paul Maloy }
13246144a996SJon Paul Maloy 
tipc_node_reset_links(struct tipc_node * n)13256144a996SJon Paul Maloy static void tipc_node_reset_links(struct tipc_node *n)
13266144a996SJon Paul Maloy {
1327598411d7SJon Paul Maloy 	int i;
13286144a996SJon Paul Maloy 
1329d50ccc2dSJon Maloy 	pr_warn("Resetting all links to %x\n", n->addr);
13306144a996SJon Paul Maloy 
1331eb18a510STuong Lien 	trace_tipc_node_reset_links(n, true, " ");
13326144a996SJon Paul Maloy 	for (i = 0; i < MAX_BEARERS; i++) {
1333598411d7SJon Paul Maloy 		tipc_node_link_down(n, i, false);
13346144a996SJon Paul Maloy 	}
13356144a996SJon Paul Maloy }
13366144a996SJon Paul Maloy 
13371a20cc25SJon Paul Maloy /* tipc_node_fsm_evt - node finite state machine
13381a20cc25SJon Paul Maloy  * Determines when contact is allowed with peer node
13391a20cc25SJon Paul Maloy  */
tipc_node_fsm_evt(struct tipc_node * n,int evt)1340d999297cSJon Paul Maloy static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
13411a20cc25SJon Paul Maloy {
13421a20cc25SJon Paul Maloy 	int state = n->state;
13431a20cc25SJon Paul Maloy 
13441a20cc25SJon Paul Maloy 	switch (state) {
13451a20cc25SJon Paul Maloy 	case SELF_DOWN_PEER_DOWN:
13461a20cc25SJon Paul Maloy 		switch (evt) {
13471a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
13481a20cc25SJon Paul Maloy 			state = SELF_UP_PEER_COMING;
13491a20cc25SJon Paul Maloy 			break;
13501a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
13511a20cc25SJon Paul Maloy 			state = SELF_COMING_PEER_UP;
13521a20cc25SJon Paul Maloy 			break;
13531a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
13541a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
13551a20cc25SJon Paul Maloy 			break;
135666996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
135766996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
135866996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
135966996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
13601a20cc25SJon Paul Maloy 		default:
136166996b6cSJon Paul Maloy 			goto illegal_evt;
13621a20cc25SJon Paul Maloy 		}
13631a20cc25SJon Paul Maloy 		break;
13641a20cc25SJon Paul Maloy 	case SELF_UP_PEER_UP:
13651a20cc25SJon Paul Maloy 		switch (evt) {
13661a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
13671a20cc25SJon Paul Maloy 			state = SELF_DOWN_PEER_LEAVING;
13681a20cc25SJon Paul Maloy 			break;
13691a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
13701a20cc25SJon Paul Maloy 			state = SELF_LEAVING_PEER_DOWN;
13711a20cc25SJon Paul Maloy 			break;
137266996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
137366996b6cSJon Paul Maloy 			state = NODE_SYNCHING;
137466996b6cSJon Paul Maloy 			break;
137566996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
137666996b6cSJon Paul Maloy 			state = NODE_FAILINGOVER;
137766996b6cSJon Paul Maloy 			break;
13781a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
13791a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
138066996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
138166996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
13821a20cc25SJon Paul Maloy 			break;
13831a20cc25SJon Paul Maloy 		default:
138466996b6cSJon Paul Maloy 			goto illegal_evt;
13851a20cc25SJon Paul Maloy 		}
13861a20cc25SJon Paul Maloy 		break;
13871a20cc25SJon Paul Maloy 	case SELF_DOWN_PEER_LEAVING:
13881a20cc25SJon Paul Maloy 		switch (evt) {
13891a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
13901a20cc25SJon Paul Maloy 			state = SELF_DOWN_PEER_DOWN;
13911a20cc25SJon Paul Maloy 			break;
13921a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
13931a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
13941a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
13951a20cc25SJon Paul Maloy 			break;
139666996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
139766996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
139866996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
139966996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
14001a20cc25SJon Paul Maloy 		default:
140166996b6cSJon Paul Maloy 			goto illegal_evt;
14021a20cc25SJon Paul Maloy 		}
14031a20cc25SJon Paul Maloy 		break;
14041a20cc25SJon Paul Maloy 	case SELF_UP_PEER_COMING:
14051a20cc25SJon Paul Maloy 		switch (evt) {
14061a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
14071a20cc25SJon Paul Maloy 			state = SELF_UP_PEER_UP;
14081a20cc25SJon Paul Maloy 			break;
14091a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
1410c4282ca7SJon Paul Maloy 			state = SELF_DOWN_PEER_DOWN;
14111a20cc25SJon Paul Maloy 			break;
14121a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
14131a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
141466996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
141566996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
141673f646ceSJon Paul Maloy 			break;
141773f646ceSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
141866996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
14191a20cc25SJon Paul Maloy 		default:
142066996b6cSJon Paul Maloy 			goto illegal_evt;
14211a20cc25SJon Paul Maloy 		}
14221a20cc25SJon Paul Maloy 		break;
14231a20cc25SJon Paul Maloy 	case SELF_COMING_PEER_UP:
14241a20cc25SJon Paul Maloy 		switch (evt) {
14251a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
14261a20cc25SJon Paul Maloy 			state = SELF_UP_PEER_UP;
14271a20cc25SJon Paul Maloy 			break;
14281a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
1429c4282ca7SJon Paul Maloy 			state = SELF_DOWN_PEER_DOWN;
14301a20cc25SJon Paul Maloy 			break;
14311a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
14321a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
14331a20cc25SJon Paul Maloy 			break;
143466996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
143566996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
143666996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
143766996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
14381a20cc25SJon Paul Maloy 		default:
143966996b6cSJon Paul Maloy 			goto illegal_evt;
14401a20cc25SJon Paul Maloy 		}
14411a20cc25SJon Paul Maloy 		break;
14421a20cc25SJon Paul Maloy 	case SELF_LEAVING_PEER_DOWN:
14431a20cc25SJon Paul Maloy 		switch (evt) {
14441a20cc25SJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
14451a20cc25SJon Paul Maloy 			state = SELF_DOWN_PEER_DOWN;
14461a20cc25SJon Paul Maloy 			break;
14471a20cc25SJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
14481a20cc25SJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
14491a20cc25SJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
14501a20cc25SJon Paul Maloy 			break;
145166996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
145266996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
145366996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
145466996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
14551a20cc25SJon Paul Maloy 		default:
145666996b6cSJon Paul Maloy 			goto illegal_evt;
145766996b6cSJon Paul Maloy 		}
145866996b6cSJon Paul Maloy 		break;
145966996b6cSJon Paul Maloy 	case NODE_FAILINGOVER:
146066996b6cSJon Paul Maloy 		switch (evt) {
146166996b6cSJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
146266996b6cSJon Paul Maloy 			state = SELF_DOWN_PEER_LEAVING;
146366996b6cSJon Paul Maloy 			break;
146466996b6cSJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
146566996b6cSJon Paul Maloy 			state = SELF_LEAVING_PEER_DOWN;
146666996b6cSJon Paul Maloy 			break;
146766996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
146866996b6cSJon Paul Maloy 			state = SELF_UP_PEER_UP;
146966996b6cSJon Paul Maloy 			break;
147066996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
147166996b6cSJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
147266996b6cSJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
147366996b6cSJon Paul Maloy 			break;
147466996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
147566996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
147666996b6cSJon Paul Maloy 		default:
147766996b6cSJon Paul Maloy 			goto illegal_evt;
147866996b6cSJon Paul Maloy 		}
147966996b6cSJon Paul Maloy 		break;
148066996b6cSJon Paul Maloy 	case NODE_SYNCHING:
148166996b6cSJon Paul Maloy 		switch (evt) {
148266996b6cSJon Paul Maloy 		case SELF_LOST_CONTACT_EVT:
148366996b6cSJon Paul Maloy 			state = SELF_DOWN_PEER_LEAVING;
148466996b6cSJon Paul Maloy 			break;
148566996b6cSJon Paul Maloy 		case PEER_LOST_CONTACT_EVT:
148666996b6cSJon Paul Maloy 			state = SELF_LEAVING_PEER_DOWN;
148766996b6cSJon Paul Maloy 			break;
148866996b6cSJon Paul Maloy 		case NODE_SYNCH_END_EVT:
148966996b6cSJon Paul Maloy 			state = SELF_UP_PEER_UP;
149066996b6cSJon Paul Maloy 			break;
149166996b6cSJon Paul Maloy 		case NODE_FAILOVER_BEGIN_EVT:
149266996b6cSJon Paul Maloy 			state = NODE_FAILINGOVER;
149366996b6cSJon Paul Maloy 			break;
149466996b6cSJon Paul Maloy 		case NODE_SYNCH_BEGIN_EVT:
149566996b6cSJon Paul Maloy 		case SELF_ESTABL_CONTACT_EVT:
149666996b6cSJon Paul Maloy 		case PEER_ESTABL_CONTACT_EVT:
149766996b6cSJon Paul Maloy 			break;
149866996b6cSJon Paul Maloy 		case NODE_FAILOVER_END_EVT:
149966996b6cSJon Paul Maloy 		default:
150066996b6cSJon Paul Maloy 			goto illegal_evt;
15011a20cc25SJon Paul Maloy 		}
15021a20cc25SJon Paul Maloy 		break;
15031a20cc25SJon Paul Maloy 	default:
15041a20cc25SJon Paul Maloy 		pr_err("Unknown node fsm state %x\n", state);
15051a20cc25SJon Paul Maloy 		break;
15061a20cc25SJon Paul Maloy 	}
1507eb18a510STuong Lien 	trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
15081a20cc25SJon Paul Maloy 	n->state = state;
150966996b6cSJon Paul Maloy 	return;
151066996b6cSJon Paul Maloy 
151166996b6cSJon Paul Maloy illegal_evt:
151266996b6cSJon Paul Maloy 	pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1513eb18a510STuong Lien 	trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
15141a20cc25SJon Paul Maloy }
15151a20cc25SJon Paul Maloy 
node_lost_contact(struct tipc_node * n,struct sk_buff_head * inputq)151652666986SJon Paul Maloy static void node_lost_contact(struct tipc_node *n,
1517598411d7SJon Paul Maloy 			      struct sk_buff_head *inputq)
1518b97bf3fdSPer Liden {
1519708ac32cSJon Paul Maloy 	struct tipc_sock_conn *conn, *safe;
1520598411d7SJon Paul Maloy 	struct tipc_link *l;
152152666986SJon Paul Maloy 	struct list_head *conns = &n->conn_sks;
1522708ac32cSJon Paul Maloy 	struct sk_buff *skb;
1523708ac32cSJon Paul Maloy 	uint i;
1524b97bf3fdSPer Liden 
1525d50ccc2dSJon Maloy 	pr_debug("Lost contact with %x\n", n->addr);
15266a939f36SGhantaKrishnamurthy MohanKrishna 	n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1527eb18a510STuong Lien 	trace_tipc_node_lost_contact(n, true, " ");
1528c5bd4d85SAllan Stephens 
152952666986SJon Paul Maloy 	/* Clean up broadcast state */
1530b06b281eSJon Paul Maloy 	tipc_bcast_remove_peer(n->net, n->bc_entry.link);
15317b50ee3dSHoang Huu Le 	skb_queue_purge(&n->bc_entry.namedq);
1532b97bf3fdSPer Liden 
1533dff29b1aSJon Paul Maloy 	/* Abort any ongoing link failover */
1534b97bf3fdSPer Liden 	for (i = 0; i < MAX_BEARERS; i++) {
153552666986SJon Paul Maloy 		l = n->links[i].link;
1536598411d7SJon Paul Maloy 		if (l)
1537598411d7SJon Paul Maloy 			tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1538b97bf3fdSPer Liden 	}
1539598411d7SJon Paul Maloy 
1540708ac32cSJon Paul Maloy 	/* Notify publications from this node */
154152666986SJon Paul Maloy 	n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1542f73b1281SHoang Le 	n->peer_net = NULL;
1543f73b1281SHoang Le 	n->peer_hash_mix = 0;
1544708ac32cSJon Paul Maloy 	/* Notify sockets connected to node */
1545708ac32cSJon Paul Maloy 	list_for_each_entry_safe(conn, safe, conns, list) {
1546708ac32cSJon Paul Maloy 		skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
154752666986SJon Paul Maloy 				      SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1548708ac32cSJon Paul Maloy 				      conn->peer_node, conn->port,
1549708ac32cSJon Paul Maloy 				      conn->peer_port, TIPC_ERR_NO_NODE);
155023d8335dSJon Paul Maloy 		if (likely(skb))
1551598411d7SJon Paul Maloy 			skb_queue_tail(inputq, skb);
1552708ac32cSJon Paul Maloy 		list_del(&conn->list);
1553708ac32cSJon Paul Maloy 		kfree(conn);
1554708ac32cSJon Paul Maloy 	}
1555b97bf3fdSPer Liden }
1556b97bf3fdSPer Liden 
155778acb1f9SErik Hugne /**
155878acb1f9SErik Hugne  * tipc_node_get_linkname - get the name of a link
155978acb1f9SErik Hugne  *
15604476441eSRandy Dunlap  * @net: the applicable net namespace
156178acb1f9SErik Hugne  * @bearer_id: id of the bearer
1562d8141208SAndrew Lunn  * @addr: peer node address
156378acb1f9SErik Hugne  * @linkname: link name output buffer
15644476441eSRandy Dunlap  * @len: size of @linkname output buffer
156578acb1f9SErik Hugne  *
1566637b77fdSRandy Dunlap  * Return: 0 on success
156778acb1f9SErik Hugne  */
tipc_node_get_linkname(struct net * net,u32 bearer_id,u32 addr,char * linkname,size_t len)1568f2f9800dSYing Xue int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1569f2f9800dSYing Xue 			   char *linkname, size_t len)
157078acb1f9SErik Hugne {
157178acb1f9SErik Hugne 	struct tipc_link *link;
15728a0f6ebeSYing Xue 	int err = -EINVAL;
1573f2f9800dSYing Xue 	struct tipc_node *node = tipc_node_find(net, addr);
157478acb1f9SErik Hugne 
15758a0f6ebeSYing Xue 	if (!node)
15768a0f6ebeSYing Xue 		return err;
15778a0f6ebeSYing Xue 
15788a0f6ebeSYing Xue 	if (bearer_id >= MAX_BEARERS)
15798a0f6ebeSYing Xue 		goto exit;
15808a0f6ebeSYing Xue 
15815405ff6eSJon Paul Maloy 	tipc_node_read_lock(node);
15829d13ec65SJon Paul Maloy 	link = node->links[bearer_id].link;
158378acb1f9SErik Hugne 	if (link) {
158438206d59SJon Paul Maloy 		strncpy(linkname, tipc_link_name(link), len);
15858a0f6ebeSYing Xue 		err = 0;
158678acb1f9SErik Hugne 	}
15875405ff6eSJon Paul Maloy 	tipc_node_read_unlock(node);
1588991ca84dSParthasarathy Bhuvaragan exit:
15898a0f6ebeSYing Xue 	tipc_node_put(node);
15908a0f6ebeSYing Xue 	return err;
159178acb1f9SErik Hugne }
15929db9fdd1SYing Xue 
15933e4b6ab5SRichard Alpe /* Caller should hold node lock for the passed node */
__tipc_nl_add_node(struct tipc_nl_msg * msg,struct tipc_node * node)1594d8182804SRichard Alpe static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
15953e4b6ab5SRichard Alpe {
15963e4b6ab5SRichard Alpe 	void *hdr;
15973e4b6ab5SRichard Alpe 	struct nlattr *attrs;
15983e4b6ab5SRichard Alpe 
1599bfb3e5ddSRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
16003e4b6ab5SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_NODE_GET);
16013e4b6ab5SRichard Alpe 	if (!hdr)
16023e4b6ab5SRichard Alpe 		return -EMSGSIZE;
16033e4b6ab5SRichard Alpe 
1604ae0be8deSMichal Kubecek 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
16053e4b6ab5SRichard Alpe 	if (!attrs)
16063e4b6ab5SRichard Alpe 		goto msg_full;
16073e4b6ab5SRichard Alpe 
16083e4b6ab5SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
16093e4b6ab5SRichard Alpe 		goto attr_msg_full;
161038077b8eSJon Maloy 	if (node_is_up(node))
16113e4b6ab5SRichard Alpe 		if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
16123e4b6ab5SRichard Alpe 			goto attr_msg_full;
16133e4b6ab5SRichard Alpe 
16143e4b6ab5SRichard Alpe 	nla_nest_end(msg->skb, attrs);
16153e4b6ab5SRichard Alpe 	genlmsg_end(msg->skb, hdr);
16163e4b6ab5SRichard Alpe 
16173e4b6ab5SRichard Alpe 	return 0;
16183e4b6ab5SRichard Alpe 
16193e4b6ab5SRichard Alpe attr_msg_full:
16203e4b6ab5SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
16213e4b6ab5SRichard Alpe msg_full:
16223e4b6ab5SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
16233e4b6ab5SRichard Alpe 
16243e4b6ab5SRichard Alpe 	return -EMSGSIZE;
16253e4b6ab5SRichard Alpe }
16263e4b6ab5SRichard Alpe 
tipc_lxc_xmit(struct net * peer_net,struct sk_buff_head * list)1627f73b1281SHoang Le static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
1628f73b1281SHoang Le {
1629f73b1281SHoang Le 	struct tipc_msg *hdr = buf_msg(skb_peek(list));
1630f73b1281SHoang Le 	struct sk_buff_head inputq;
1631f73b1281SHoang Le 
1632f73b1281SHoang Le 	switch (msg_user(hdr)) {
1633f73b1281SHoang Le 	case TIPC_LOW_IMPORTANCE:
1634f73b1281SHoang Le 	case TIPC_MEDIUM_IMPORTANCE:
1635f73b1281SHoang Le 	case TIPC_HIGH_IMPORTANCE:
1636f73b1281SHoang Le 	case TIPC_CRITICAL_IMPORTANCE:
16378b1e5b0aSHoang Le 		if (msg_connected(hdr) || msg_named(hdr) ||
16388b1e5b0aSHoang Le 		    msg_direct(hdr)) {
1639f73b1281SHoang Le 			tipc_loopback_trace(peer_net, list);
1640f73b1281SHoang Le 			spin_lock_init(&list->lock);
1641f73b1281SHoang Le 			tipc_sk_rcv(peer_net, list);
1642f73b1281SHoang Le 			return;
1643f73b1281SHoang Le 		}
1644f73b1281SHoang Le 		if (msg_mcast(hdr)) {
1645f73b1281SHoang Le 			tipc_loopback_trace(peer_net, list);
1646f73b1281SHoang Le 			skb_queue_head_init(&inputq);
1647f73b1281SHoang Le 			tipc_sk_mcast_rcv(peer_net, list, &inputq);
1648f73b1281SHoang Le 			__skb_queue_purge(list);
1649f73b1281SHoang Le 			skb_queue_purge(&inputq);
1650f73b1281SHoang Le 			return;
1651f73b1281SHoang Le 		}
1652f73b1281SHoang Le 		return;
1653f73b1281SHoang Le 	case MSG_FRAGMENTER:
1654f73b1281SHoang Le 		if (tipc_msg_assemble(list)) {
1655f73b1281SHoang Le 			tipc_loopback_trace(peer_net, list);
1656f73b1281SHoang Le 			skb_queue_head_init(&inputq);
1657f73b1281SHoang Le 			tipc_sk_mcast_rcv(peer_net, list, &inputq);
1658f73b1281SHoang Le 			__skb_queue_purge(list);
1659f73b1281SHoang Le 			skb_queue_purge(&inputq);
1660f73b1281SHoang Le 		}
1661f73b1281SHoang Le 		return;
1662f73b1281SHoang Le 	case GROUP_PROTOCOL:
1663f73b1281SHoang Le 	case CONN_MANAGER:
1664f73b1281SHoang Le 		tipc_loopback_trace(peer_net, list);
1665f73b1281SHoang Le 		spin_lock_init(&list->lock);
1666f73b1281SHoang Le 		tipc_sk_rcv(peer_net, list);
1667f73b1281SHoang Le 		return;
1668f73b1281SHoang Le 	case LINK_PROTOCOL:
1669f73b1281SHoang Le 	case NAME_DISTRIBUTOR:
1670f73b1281SHoang Le 	case TUNNEL_PROTOCOL:
1671f73b1281SHoang Le 	case BCAST_PROTOCOL:
1672f73b1281SHoang Le 		return;
1673f73b1281SHoang Le 	default:
1674f73b1281SHoang Le 		return;
1675f2219c32STom Rix 	}
1676f73b1281SHoang Le }
1677f73b1281SHoang Le 
1678af9b028eSJon Paul Maloy /**
167925764779SMauro Carvalho Chehab  * tipc_node_xmit() - general link level function for message sending
1680af9b028eSJon Paul Maloy  * @net: the applicable net namespace
1681af9b028eSJon Paul Maloy  * @list: chain of buffers containing message
1682af9b028eSJon Paul Maloy  * @dnode: address of destination node
1683af9b028eSJon Paul Maloy  * @selector: a number used for deterministic link selection
1684365ad353SJon Paul Maloy  * Consumes the buffer chain.
1685637b77fdSRandy Dunlap  * Return: 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1686af9b028eSJon Paul Maloy  */
tipc_node_xmit(struct net * net,struct sk_buff_head * list,u32 dnode,int selector)1687af9b028eSJon Paul Maloy int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1688af9b028eSJon Paul Maloy 		   u32 dnode, int selector)
1689af9b028eSJon Paul Maloy {
16905405ff6eSJon Paul Maloy 	struct tipc_link_entry *le = NULL;
1691af9b028eSJon Paul Maloy 	struct tipc_node *n;
1692af9b028eSJon Paul Maloy 	struct sk_buff_head xmitq;
1693f73b1281SHoang Le 	bool node_up = false;
169488956177SXin Long 	struct net *peer_net;
16954952cd3eSRichard Alpe 	int bearer_id;
16964952cd3eSRichard Alpe 	int rc;
1697af9b028eSJon Paul Maloy 
16984952cd3eSRichard Alpe 	if (in_own_node(net, dnode)) {
16996c9081a3SJohn Rutherford 		tipc_loopback_trace(net, list);
1700e654f9f5SJon Maloy 		spin_lock_init(&list->lock);
17014952cd3eSRichard Alpe 		tipc_sk_rcv(net, list);
17024952cd3eSRichard Alpe 		return 0;
17034952cd3eSRichard Alpe 	}
17044952cd3eSRichard Alpe 
1705af9b028eSJon Paul Maloy 	n = tipc_node_find(net, dnode);
17064952cd3eSRichard Alpe 	if (unlikely(!n)) {
1707e654f9f5SJon Maloy 		__skb_queue_purge(list);
17084952cd3eSRichard Alpe 		return -EHOSTUNREACH;
17094952cd3eSRichard Alpe 	}
17104952cd3eSRichard Alpe 
171188956177SXin Long 	rcu_read_lock();
17125405ff6eSJon Paul Maloy 	tipc_node_read_lock(n);
1713f73b1281SHoang Le 	node_up = node_is_up(n);
171488956177SXin Long 	peer_net = n->peer_net;
1715f73b1281SHoang Le 	tipc_node_read_unlock(n);
171688956177SXin Long 	if (node_up && peer_net && check_net(peer_net)) {
171788956177SXin Long 		/* xmit inner linux container */
171888956177SXin Long 		tipc_lxc_xmit(peer_net, list);
171988956177SXin Long 		if (likely(skb_queue_empty(list))) {
172088956177SXin Long 			rcu_read_unlock();
1721f73b1281SHoang Le 			tipc_node_put(n);
1722f73b1281SHoang Le 			return 0;
1723f73b1281SHoang Le 		}
1724f73b1281SHoang Le 	}
172588956177SXin Long 	rcu_read_unlock();
1726f73b1281SHoang Le 
172788956177SXin Long 	tipc_node_read_lock(n);
17282312bf61SJon Paul Maloy 	bearer_id = n->active_links[selector & 1];
17294952cd3eSRichard Alpe 	if (unlikely(bearer_id == INVALID_BEARER_ID)) {
17304952cd3eSRichard Alpe 		tipc_node_read_unlock(n);
17314952cd3eSRichard Alpe 		tipc_node_put(n);
1732e654f9f5SJon Maloy 		__skb_queue_purge(list);
17334952cd3eSRichard Alpe 		return -EHOSTUNREACH;
17344952cd3eSRichard Alpe 	}
17354952cd3eSRichard Alpe 
17364952cd3eSRichard Alpe 	__skb_queue_head_init(&xmitq);
17372312bf61SJon Paul Maloy 	le = &n->links[bearer_id];
17382312bf61SJon Paul Maloy 	spin_lock_bh(&le->lock);
17392312bf61SJon Paul Maloy 	rc = tipc_link_xmit(le->link, list, &xmitq);
17402312bf61SJon Paul Maloy 	spin_unlock_bh(&le->lock);
17415405ff6eSJon Paul Maloy 	tipc_node_read_unlock(n);
17424952cd3eSRichard Alpe 
1743365ad353SJon Paul Maloy 	if (unlikely(rc == -ENOBUFS))
1744598411d7SJon Paul Maloy 		tipc_node_link_down(n, bearer_id, false);
1745365ad353SJon Paul Maloy 	else
1746fc1b6d6dSTuong Lien 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
17475405ff6eSJon Paul Maloy 
17484952cd3eSRichard Alpe 	tipc_node_put(n);
17494952cd3eSRichard Alpe 
1750dc8d1eb3SJon Paul Maloy 	return rc;
1751dc8d1eb3SJon Paul Maloy }
1752af9b028eSJon Paul Maloy 
1753af9b028eSJon Paul Maloy /* tipc_node_xmit_skb(): send single buffer to destination
1754a79ace4bSZheng Yongjun  * Buffers sent via this function are generally TIPC_SYSTEM_IMPORTANCE
1755af9b028eSJon Paul Maloy  * messages, which will not be rejected
1756af9b028eSJon Paul Maloy  * The only exception is datagram messages rerouted after secondary
1757af9b028eSJon Paul Maloy  * lookup, which are rare and safe to dispose of anyway.
1758af9b028eSJon Paul Maloy  */
tipc_node_xmit_skb(struct net * net,struct sk_buff * skb,u32 dnode,u32 selector)1759af9b028eSJon Paul Maloy int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1760af9b028eSJon Paul Maloy 		       u32 selector)
1761af9b028eSJon Paul Maloy {
1762af9b028eSJon Paul Maloy 	struct sk_buff_head head;
1763af9b028eSJon Paul Maloy 
1764e654f9f5SJon Maloy 	__skb_queue_head_init(&head);
1765af9b028eSJon Paul Maloy 	__skb_queue_tail(&head, skb);
1766365ad353SJon Paul Maloy 	tipc_node_xmit(net, &head, dnode, selector);
1767af9b028eSJon Paul Maloy 	return 0;
1768af9b028eSJon Paul Maloy }
1769af9b028eSJon Paul Maloy 
1770f70d37b7SJon Maloy /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1771f70d37b7SJon Maloy  * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1772f70d37b7SJon Maloy  */
tipc_node_distr_xmit(struct net * net,struct sk_buff_head * xmitq)1773f70d37b7SJon Maloy int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1774f70d37b7SJon Maloy {
1775f70d37b7SJon Maloy 	struct sk_buff *skb;
1776f70d37b7SJon Maloy 	u32 selector, dnode;
1777f70d37b7SJon Maloy 
1778f70d37b7SJon Maloy 	while ((skb = __skb_dequeue(xmitq))) {
1779f70d37b7SJon Maloy 		selector = msg_origport(buf_msg(skb));
1780f70d37b7SJon Maloy 		dnode = msg_destnode(buf_msg(skb));
1781f70d37b7SJon Maloy 		tipc_node_xmit_skb(net, skb, dnode, selector);
1782f70d37b7SJon Maloy 	}
1783f70d37b7SJon Maloy 	return 0;
1784f70d37b7SJon Maloy }
1785f70d37b7SJon Maloy 
tipc_node_broadcast(struct net * net,struct sk_buff * skb,int rc_dests)1786cad2929dSHoang Huu Le void tipc_node_broadcast(struct net *net, struct sk_buff *skb, int rc_dests)
17871d7e1c25SJon Paul Maloy {
1788cad2929dSHoang Huu Le 	struct sk_buff_head xmitq;
17891d7e1c25SJon Paul Maloy 	struct sk_buff *txskb;
17901d7e1c25SJon Paul Maloy 	struct tipc_node *n;
1791cad2929dSHoang Huu Le 	u16 dummy;
17921d7e1c25SJon Paul Maloy 	u32 dst;
17931d7e1c25SJon Paul Maloy 
1794cad2929dSHoang Huu Le 	/* Use broadcast if all nodes support it */
1795cad2929dSHoang Huu Le 	if (!rc_dests && tipc_bcast_get_mode(net) != BCLINK_MODE_RCAST) {
1796cad2929dSHoang Huu Le 		__skb_queue_head_init(&xmitq);
1797cad2929dSHoang Huu Le 		__skb_queue_tail(&xmitq, skb);
1798cad2929dSHoang Huu Le 		tipc_bcast_xmit(net, &xmitq, &dummy);
1799cad2929dSHoang Huu Le 		return;
1800cad2929dSHoang Huu Le 	}
1801cad2929dSHoang Huu Le 
1802cad2929dSHoang Huu Le 	/* Otherwise use legacy replicast method */
18031d7e1c25SJon Paul Maloy 	rcu_read_lock();
18041d7e1c25SJon Paul Maloy 	list_for_each_entry_rcu(n, tipc_nodes(net), list) {
18051d7e1c25SJon Paul Maloy 		dst = n->addr;
18061d7e1c25SJon Paul Maloy 		if (in_own_node(net, dst))
18071d7e1c25SJon Paul Maloy 			continue;
180838077b8eSJon Maloy 		if (!node_is_up(n))
18091d7e1c25SJon Paul Maloy 			continue;
18101d7e1c25SJon Paul Maloy 		txskb = pskb_copy(skb, GFP_ATOMIC);
18111d7e1c25SJon Paul Maloy 		if (!txskb)
18121d7e1c25SJon Paul Maloy 			break;
18131d7e1c25SJon Paul Maloy 		msg_set_destnode(buf_msg(txskb), dst);
18141d7e1c25SJon Paul Maloy 		tipc_node_xmit_skb(net, txskb, dst, 0);
18151d7e1c25SJon Paul Maloy 	}
18161d7e1c25SJon Paul Maloy 	rcu_read_unlock();
18171d7e1c25SJon Paul Maloy 	kfree_skb(skb);
18181d7e1c25SJon Paul Maloy }
18191d7e1c25SJon Paul Maloy 
tipc_node_mcast_rcv(struct tipc_node * n)1820a853e4c6SJon Paul Maloy static void tipc_node_mcast_rcv(struct tipc_node *n)
1821a853e4c6SJon Paul Maloy {
1822a853e4c6SJon Paul Maloy 	struct tipc_bclink_entry *be = &n->bc_entry;
1823a853e4c6SJon Paul Maloy 
1824a853e4c6SJon Paul Maloy 	/* 'arrvq' is under inputq2's lock protection */
1825a853e4c6SJon Paul Maloy 	spin_lock_bh(&be->inputq2.lock);
1826a853e4c6SJon Paul Maloy 	spin_lock_bh(&be->inputq1.lock);
1827a853e4c6SJon Paul Maloy 	skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1828a853e4c6SJon Paul Maloy 	spin_unlock_bh(&be->inputq1.lock);
1829a853e4c6SJon Paul Maloy 	spin_unlock_bh(&be->inputq2.lock);
1830a853e4c6SJon Paul Maloy 	tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1831a853e4c6SJon Paul Maloy }
1832a853e4c6SJon Paul Maloy 
tipc_node_bc_sync_rcv(struct tipc_node * n,struct tipc_msg * hdr,int bearer_id,struct sk_buff_head * xmitq)183302d11ca2SJon Paul Maloy static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
183402d11ca2SJon Paul Maloy 				  int bearer_id, struct sk_buff_head *xmitq)
183502d11ca2SJon Paul Maloy {
183602d11ca2SJon Paul Maloy 	struct tipc_link *ucl;
183702d11ca2SJon Paul Maloy 	int rc;
183802d11ca2SJon Paul Maloy 
1839a91d55d1STuong Lien 	rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr, xmitq);
184002d11ca2SJon Paul Maloy 
184102d11ca2SJon Paul Maloy 	if (rc & TIPC_LINK_DOWN_EVT) {
184240501f90SJon Paul Maloy 		tipc_node_reset_links(n);
184302d11ca2SJon Paul Maloy 		return;
184402d11ca2SJon Paul Maloy 	}
184502d11ca2SJon Paul Maloy 
184602d11ca2SJon Paul Maloy 	if (!(rc & TIPC_LINK_SND_STATE))
184702d11ca2SJon Paul Maloy 		return;
184802d11ca2SJon Paul Maloy 
184902d11ca2SJon Paul Maloy 	/* If probe message, a STATE response will be sent anyway */
185002d11ca2SJon Paul Maloy 	if (msg_probe(hdr))
185102d11ca2SJon Paul Maloy 		return;
185202d11ca2SJon Paul Maloy 
185302d11ca2SJon Paul Maloy 	/* Produce a STATE message carrying broadcast NACK */
185402d11ca2SJon Paul Maloy 	tipc_node_read_lock(n);
185502d11ca2SJon Paul Maloy 	ucl = n->links[bearer_id].link;
185602d11ca2SJon Paul Maloy 	if (ucl)
185702d11ca2SJon Paul Maloy 		tipc_link_build_state_msg(ucl, xmitq);
185802d11ca2SJon Paul Maloy 	tipc_node_read_unlock(n);
185902d11ca2SJon Paul Maloy }
186002d11ca2SJon Paul Maloy 
18616e498158SJon Paul Maloy /**
186252666986SJon Paul Maloy  * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
186352666986SJon Paul Maloy  * @net: the applicable net namespace
186452666986SJon Paul Maloy  * @skb: TIPC packet
186552666986SJon Paul Maloy  * @bearer_id: id of bearer message arrived on
186652666986SJon Paul Maloy  *
186752666986SJon Paul Maloy  * Invoked with no locks held.
186852666986SJon Paul Maloy  */
tipc_node_bc_rcv(struct net * net,struct sk_buff * skb,int bearer_id)1869742e0383SWu Fengguang static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
187052666986SJon Paul Maloy {
187152666986SJon Paul Maloy 	int rc;
187252666986SJon Paul Maloy 	struct sk_buff_head xmitq;
187352666986SJon Paul Maloy 	struct tipc_bclink_entry *be;
187452666986SJon Paul Maloy 	struct tipc_link_entry *le;
187552666986SJon Paul Maloy 	struct tipc_msg *hdr = buf_msg(skb);
187652666986SJon Paul Maloy 	int usr = msg_user(hdr);
187752666986SJon Paul Maloy 	u32 dnode = msg_destnode(hdr);
187852666986SJon Paul Maloy 	struct tipc_node *n;
187952666986SJon Paul Maloy 
188052666986SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
188152666986SJon Paul Maloy 
188252666986SJon Paul Maloy 	/* If NACK for other node, let rcv link for that node peek into it */
188352666986SJon Paul Maloy 	if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
188452666986SJon Paul Maloy 		n = tipc_node_find(net, dnode);
188552666986SJon Paul Maloy 	else
188652666986SJon Paul Maloy 		n = tipc_node_find(net, msg_prevnode(hdr));
188752666986SJon Paul Maloy 	if (!n) {
188852666986SJon Paul Maloy 		kfree_skb(skb);
188952666986SJon Paul Maloy 		return;
189052666986SJon Paul Maloy 	}
189152666986SJon Paul Maloy 	be = &n->bc_entry;
189252666986SJon Paul Maloy 	le = &n->links[bearer_id];
189352666986SJon Paul Maloy 
189452666986SJon Paul Maloy 	rc = tipc_bcast_rcv(net, be->link, skb);
189552666986SJon Paul Maloy 
189652666986SJon Paul Maloy 	/* Broadcast ACKs are sent on a unicast link */
189702d11ca2SJon Paul Maloy 	if (rc & TIPC_LINK_SND_STATE) {
18985405ff6eSJon Paul Maloy 		tipc_node_read_lock(n);
189934b9cd64SJon Paul Maloy 		tipc_link_build_state_msg(le->link, &xmitq);
19005405ff6eSJon Paul Maloy 		tipc_node_read_unlock(n);
190152666986SJon Paul Maloy 	}
190252666986SJon Paul Maloy 
190352666986SJon Paul Maloy 	if (!skb_queue_empty(&xmitq))
1904fc1b6d6dSTuong Lien 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
190552666986SJon Paul Maloy 
1906a853e4c6SJon Paul Maloy 	if (!skb_queue_empty(&be->inputq1))
1907a853e4c6SJon Paul Maloy 		tipc_node_mcast_rcv(n);
19081fc07f3eSJon Paul Maloy 
19095679ee78SZhenbo Gao 	/* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
19105679ee78SZhenbo Gao 	if (!skb_queue_empty(&n->bc_entry.namedq))
1911cad2929dSHoang Huu Le 		tipc_named_rcv(net, &n->bc_entry.namedq,
1912cad2929dSHoang Huu Le 			       &n->bc_entry.named_rcv_nxt,
1913cad2929dSHoang Huu Le 			       &n->bc_entry.named_open);
19145679ee78SZhenbo Gao 
191540501f90SJon Paul Maloy 	/* If reassembly or retransmission failure => reset all links to peer */
191640501f90SJon Paul Maloy 	if (rc & TIPC_LINK_DOWN_EVT)
19171fc07f3eSJon Paul Maloy 		tipc_node_reset_links(n);
19181fc07f3eSJon Paul Maloy 
191952666986SJon Paul Maloy 	tipc_node_put(n);
192052666986SJon Paul Maloy }
192152666986SJon Paul Maloy 
192252666986SJon Paul Maloy /**
19236e498158SJon Paul Maloy  * tipc_node_check_state - check and if necessary update node state
19244476441eSRandy Dunlap  * @n: target tipc_node
19256e498158SJon Paul Maloy  * @skb: TIPC packet
19266e498158SJon Paul Maloy  * @bearer_id: identity of bearer delivering the packet
19274476441eSRandy Dunlap  * @xmitq: queue for messages to be xmited on
1928637b77fdSRandy Dunlap  * Return: true if state and msg are ok, otherwise false
19296144a996SJon Paul Maloy  */
tipc_node_check_state(struct tipc_node * n,struct sk_buff * skb,int bearer_id,struct sk_buff_head * xmitq)19306e498158SJon Paul Maloy static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1931662921cdSJon Paul Maloy 				  int bearer_id, struct sk_buff_head *xmitq)
19326144a996SJon Paul Maloy {
19336144a996SJon Paul Maloy 	struct tipc_msg *hdr = buf_msg(skb);
19346e498158SJon Paul Maloy 	int usr = msg_user(hdr);
19356e498158SJon Paul Maloy 	int mtyp = msg_type(hdr);
19366144a996SJon Paul Maloy 	u16 oseqno = msg_seqno(hdr);
19376e498158SJon Paul Maloy 	u16 exp_pkts = msg_msgcnt(hdr);
193838206d59SJon Paul Maloy 	u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
19396e498158SJon Paul Maloy 	int state = n->state;
19402be80c2dSJon Paul Maloy 	struct tipc_link *l, *tnl, *pl = NULL;
1941598411d7SJon Paul Maloy 	struct tipc_media_addr *maddr;
194238206d59SJon Paul Maloy 	int pb_id;
19436144a996SJon Paul Maloy 
1944eb18a510STuong Lien 	if (trace_tipc_node_check_state_enabled()) {
1945eb18a510STuong Lien 		trace_tipc_skb_dump(skb, false, "skb for node state check");
1946eb18a510STuong Lien 		trace_tipc_node_check_state(n, true, " ");
1947eb18a510STuong Lien 	}
19486e498158SJon Paul Maloy 	l = n->links[bearer_id].link;
19496e498158SJon Paul Maloy 	if (!l)
19506e498158SJon Paul Maloy 		return false;
195138206d59SJon Paul Maloy 	rcv_nxt = tipc_link_rcv_nxt(l);
19526144a996SJon Paul Maloy 
19536144a996SJon Paul Maloy 
19546e498158SJon Paul Maloy 	if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
19556e498158SJon Paul Maloy 		return true;
19566144a996SJon Paul Maloy 
19576e498158SJon Paul Maloy 	/* Find parallel link, if any */
195838206d59SJon Paul Maloy 	for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
195938206d59SJon Paul Maloy 		if ((pb_id != bearer_id) && n->links[pb_id].link) {
196038206d59SJon Paul Maloy 			pl = n->links[pb_id].link;
19616e498158SJon Paul Maloy 			break;
19626e498158SJon Paul Maloy 		}
19636e498158SJon Paul Maloy 	}
19646144a996SJon Paul Maloy 
196526574db0STuong Lien 	if (!tipc_link_validate_msg(l, hdr)) {
196626574db0STuong Lien 		trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
196726574db0STuong Lien 		trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
19687ea817f4SJon Maloy 		return false;
196926574db0STuong Lien 	}
19707ea817f4SJon Maloy 
19715405ff6eSJon Paul Maloy 	/* Check and update node accesibility if applicable */
19726e498158SJon Paul Maloy 	if (state == SELF_UP_PEER_COMING) {
19736e498158SJon Paul Maloy 		if (!tipc_link_is_up(l))
19746e498158SJon Paul Maloy 			return true;
19756e498158SJon Paul Maloy 		if (!msg_peer_link_is_up(hdr))
19766e498158SJon Paul Maloy 			return true;
19776e498158SJon Paul Maloy 		tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
19786e498158SJon Paul Maloy 	}
19796e498158SJon Paul Maloy 
19806e498158SJon Paul Maloy 	if (state == SELF_DOWN_PEER_LEAVING) {
19816e498158SJon Paul Maloy 		if (msg_peer_node_is_up(hdr))
19826e498158SJon Paul Maloy 			return false;
19836e498158SJon Paul Maloy 		tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
19845c10e979SJon Paul Maloy 		return true;
19856e498158SJon Paul Maloy 	}
19866e498158SJon Paul Maloy 
19875405ff6eSJon Paul Maloy 	if (state == SELF_LEAVING_PEER_DOWN)
19885405ff6eSJon Paul Maloy 		return false;
19895405ff6eSJon Paul Maloy 
19906e498158SJon Paul Maloy 	/* Ignore duplicate packets */
19910f8b8e28SJon Paul Maloy 	if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
19926e498158SJon Paul Maloy 		return true;
19936e498158SJon Paul Maloy 
19946e498158SJon Paul Maloy 	/* Initiate or update failover mode if applicable */
19956e498158SJon Paul Maloy 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
19966e498158SJon Paul Maloy 		syncpt = oseqno + exp_pkts - 1;
1997d0f84d08STuong Lien 		if (pl && !tipc_link_is_reset(pl)) {
1998598411d7SJon Paul Maloy 			__tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1999eb18a510STuong Lien 			trace_tipc_node_link_down(n, true,
2000eb18a510STuong Lien 						  "node link down <- failover!");
200138206d59SJon Paul Maloy 			tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
200238206d59SJon Paul Maloy 							tipc_link_inputq(l));
2003598411d7SJon Paul Maloy 		}
2004c0b14a08STuong Lien 
2005c140eb16SLUU Duc Canh 		/* If parallel link was already down, and this happened before
2006c0b14a08STuong Lien 		 * the tunnel link came up, node failover was never started.
2007c0b14a08STuong Lien 		 * Ensure that a FAILOVER_MSG is sent to get peer out of
2008c0b14a08STuong Lien 		 * NODE_FAILINGOVER state, also this node must accept
2009c0b14a08STuong Lien 		 * TUNNEL_MSGs from peer.
2010c140eb16SLUU Duc Canh 		 */
2011c0b14a08STuong Lien 		if (n->state != NODE_FAILINGOVER)
2012c0b14a08STuong Lien 			tipc_node_link_failover(n, pl, l, xmitq);
2013c0b14a08STuong Lien 
20146e498158SJon Paul Maloy 		/* If pkts arrive out of order, use lowest calculated syncpt */
20156e498158SJon Paul Maloy 		if (less(syncpt, n->sync_point))
20166e498158SJon Paul Maloy 			n->sync_point = syncpt;
20176144a996SJon Paul Maloy 	}
20186e498158SJon Paul Maloy 
20196e498158SJon Paul Maloy 	/* Open parallel link when tunnel link reaches synch point */
202017b20630SJon Paul Maloy 	if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
2021662921cdSJon Paul Maloy 		if (!more(rcv_nxt, n->sync_point))
2022662921cdSJon Paul Maloy 			return true;
20236e498158SJon Paul Maloy 		tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
20246e498158SJon Paul Maloy 		if (pl)
2025662921cdSJon Paul Maloy 			tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
20266e498158SJon Paul Maloy 		return true;
20276e498158SJon Paul Maloy 	}
20286e498158SJon Paul Maloy 
20290184235eSBhaskar Chowdhury 	/* No syncing needed if only one link */
20305ae2f8e6SJon Paul Maloy 	if (!pl || !tipc_link_is_up(pl))
20315ae2f8e6SJon Paul Maloy 		return true;
20325ae2f8e6SJon Paul Maloy 
20330f8b8e28SJon Paul Maloy 	/* Initiate synch mode if applicable */
20340f8b8e28SJon Paul Maloy 	if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
20354929a932STuong Lien 		if (n->capabilities & TIPC_TUNNEL_ENHANCED)
20364929a932STuong Lien 			syncpt = msg_syncpt(hdr);
20374929a932STuong Lien 		else
20384929a932STuong Lien 			syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
2039ed43594aSJon Paul Maloy 		if (!tipc_link_is_up(l))
2040598411d7SJon Paul Maloy 			__tipc_node_link_up(n, bearer_id, xmitq);
20416e498158SJon Paul Maloy 		if (n->state == SELF_UP_PEER_UP) {
20426e498158SJon Paul Maloy 			n->sync_point = syncpt;
2043662921cdSJon Paul Maloy 			tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
20446e498158SJon Paul Maloy 			tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
20456e498158SJon Paul Maloy 		}
20466e498158SJon Paul Maloy 	}
20476e498158SJon Paul Maloy 
20486e498158SJon Paul Maloy 	/* Open tunnel link when parallel link reaches synch point */
20495c10e979SJon Paul Maloy 	if (n->state == NODE_SYNCHING) {
20502be80c2dSJon Paul Maloy 		if (tipc_link_is_synching(l)) {
20512be80c2dSJon Paul Maloy 			tnl = l;
20522be80c2dSJon Paul Maloy 		} else {
20532be80c2dSJon Paul Maloy 			tnl = pl;
20542be80c2dSJon Paul Maloy 			pl = l;
20552be80c2dSJon Paul Maloy 		}
205638206d59SJon Paul Maloy 		inputq_len = skb_queue_len(tipc_link_inputq(pl));
205738206d59SJon Paul Maloy 		dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
20585ae2f8e6SJon Paul Maloy 		if (more(dlv_nxt, n->sync_point)) {
20592be80c2dSJon Paul Maloy 			tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
20606e498158SJon Paul Maloy 			tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
20616e498158SJon Paul Maloy 			return true;
20626e498158SJon Paul Maloy 		}
20632be80c2dSJon Paul Maloy 		if (l == pl)
20642be80c2dSJon Paul Maloy 			return true;
20656e498158SJon Paul Maloy 		if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
20666e498158SJon Paul Maloy 			return true;
20676e498158SJon Paul Maloy 		if (usr == LINK_PROTOCOL)
20686e498158SJon Paul Maloy 			return true;
20696e498158SJon Paul Maloy 		return false;
20706e498158SJon Paul Maloy 	}
20716e498158SJon Paul Maloy 	return true;
20726144a996SJon Paul Maloy }
20736144a996SJon Paul Maloy 
2074d999297cSJon Paul Maloy /**
2075d999297cSJon Paul Maloy  * tipc_rcv - process TIPC packets/messages arriving from off-node
2076d999297cSJon Paul Maloy  * @net: the applicable net namespace
2077d999297cSJon Paul Maloy  * @skb: TIPC packet
2078d8141208SAndrew Lunn  * @b: pointer to bearer message arrived on
2079d999297cSJon Paul Maloy  *
2080d999297cSJon Paul Maloy  * Invoked with no locks held. Bearer pointer must point to a valid bearer
2081d999297cSJon Paul Maloy  * structure (i.e. cannot be NULL), but bearer can be inactive.
2082d999297cSJon Paul Maloy  */
tipc_rcv(struct net * net,struct sk_buff * skb,struct tipc_bearer * b)2083d999297cSJon Paul Maloy void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
2084d999297cSJon Paul Maloy {
2085d999297cSJon Paul Maloy 	struct sk_buff_head xmitq;
20866e498158SJon Paul Maloy 	struct tipc_link_entry *le;
2087fc1b6d6dSTuong Lien 	struct tipc_msg *hdr;
2088fc1b6d6dSTuong Lien 	struct tipc_node *n;
2089fc1b6d6dSTuong Lien 	int bearer_id = b->identity;
2090efe79050SHamish Martin 	u32 self = tipc_own_addr(net);
2091681a55d7SJon Paul Maloy 	int usr, rc = 0;
2092681a55d7SJon Paul Maloy 	u16 bc_ack;
2093fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
2094fc1b6d6dSTuong Lien 	struct tipc_ehdr *ehdr;
2095d999297cSJon Paul Maloy 
2096fc1b6d6dSTuong Lien 	/* Check if message must be decrypted first */
2097fc1b6d6dSTuong Lien 	if (TIPC_SKB_CB(skb)->decrypted || !tipc_ehdr_validate(skb))
2098fc1b6d6dSTuong Lien 		goto rcv;
2099d999297cSJon Paul Maloy 
2100fc1b6d6dSTuong Lien 	ehdr = (struct tipc_ehdr *)skb->data;
2101fc1b6d6dSTuong Lien 	if (likely(ehdr->user != LINK_CONFIG)) {
2102fc1b6d6dSTuong Lien 		n = tipc_node_find(net, ntohl(ehdr->addr));
2103fc1b6d6dSTuong Lien 		if (unlikely(!n))
2104fc1b6d6dSTuong Lien 			goto discard;
2105fc1b6d6dSTuong Lien 	} else {
2106fc1b6d6dSTuong Lien 		n = tipc_node_find_by_id(net, ehdr->id);
2107fc1b6d6dSTuong Lien 	}
2108*2ebe8f84SXin Long 	skb_dst_force(skb);
2109fc1b6d6dSTuong Lien 	tipc_crypto_rcv(net, (n) ? n->crypto_rx : NULL, &skb, b);
2110fc1b6d6dSTuong Lien 	if (!skb)
2111fc1b6d6dSTuong Lien 		return;
2112fc1b6d6dSTuong Lien 
2113fc1b6d6dSTuong Lien rcv:
2114fc1b6d6dSTuong Lien #endif
2115681a55d7SJon Paul Maloy 	/* Ensure message is well-formed before touching the header */
2116d618d09aSJon Maloy 	if (unlikely(!tipc_msg_validate(&skb)))
2117d999297cSJon Paul Maloy 		goto discard;
2118fc1b6d6dSTuong Lien 	__skb_queue_head_init(&xmitq);
2119681a55d7SJon Paul Maloy 	hdr = buf_msg(skb);
2120681a55d7SJon Paul Maloy 	usr = msg_user(hdr);
2121681a55d7SJon Paul Maloy 	bc_ack = msg_bcast_ack(hdr);
2122d999297cSJon Paul Maloy 
212352666986SJon Paul Maloy 	/* Handle arrival of discovery or broadcast packet */
2124d999297cSJon Paul Maloy 	if (unlikely(msg_non_seq(hdr))) {
212552666986SJon Paul Maloy 		if (unlikely(usr == LINK_CONFIG))
212652666986SJon Paul Maloy 			return tipc_disc_rcv(net, skb, b);
2127d999297cSJon Paul Maloy 		else
212852666986SJon Paul Maloy 			return tipc_node_bc_rcv(net, skb, bearer_id);
2129d999297cSJon Paul Maloy 	}
2130d999297cSJon Paul Maloy 
2131efe79050SHamish Martin 	/* Discard unicast link messages destined for another node */
2132efe79050SHamish Martin 	if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
2133efe79050SHamish Martin 		goto discard;
2134efe79050SHamish Martin 
2135d999297cSJon Paul Maloy 	/* Locate neighboring node that sent packet */
2136d999297cSJon Paul Maloy 	n = tipc_node_find(net, msg_prevnode(hdr));
2137d999297cSJon Paul Maloy 	if (unlikely(!n))
2138d999297cSJon Paul Maloy 		goto discard;
21396e498158SJon Paul Maloy 	le = &n->links[bearer_id];
21406e498158SJon Paul Maloy 
214152666986SJon Paul Maloy 	/* Ensure broadcast reception is in synch with peer's send state */
2142d7626b5aSTuong Lien 	if (unlikely(usr == LINK_PROTOCOL)) {
2143d7626b5aSTuong Lien 		if (unlikely(skb_linearize(skb))) {
2144d7626b5aSTuong Lien 			tipc_node_put(n);
2145d7626b5aSTuong Lien 			goto discard;
2146d7626b5aSTuong Lien 		}
2147d7626b5aSTuong Lien 		hdr = buf_msg(skb);
214802d11ca2SJon Paul Maloy 		tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
2149d7626b5aSTuong Lien 	} else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack)) {
215006bd2b1eSJon Paul Maloy 		tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
2151d7626b5aSTuong Lien 	}
215252666986SJon Paul Maloy 
21535405ff6eSJon Paul Maloy 	/* Receive packet directly if conditions permit */
21545405ff6eSJon Paul Maloy 	tipc_node_read_lock(n);
21555405ff6eSJon Paul Maloy 	if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
21562312bf61SJon Paul Maloy 		spin_lock_bh(&le->lock);
21575405ff6eSJon Paul Maloy 		if (le->link) {
21586e498158SJon Paul Maloy 			rc = tipc_link_rcv(le->link, skb, &xmitq);
21596e498158SJon Paul Maloy 			skb = NULL;
21606e498158SJon Paul Maloy 		}
21615405ff6eSJon Paul Maloy 		spin_unlock_bh(&le->lock);
21625405ff6eSJon Paul Maloy 	}
21635405ff6eSJon Paul Maloy 	tipc_node_read_unlock(n);
21645405ff6eSJon Paul Maloy 
21655405ff6eSJon Paul Maloy 	/* Check/update node state before receiving */
21665405ff6eSJon Paul Maloy 	if (unlikely(skb)) {
216727163138SParthasarathy Bhuvaragan 		if (unlikely(skb_linearize(skb)))
2168de058420SXiyu Yang 			goto out_node_put;
21695405ff6eSJon Paul Maloy 		tipc_node_write_lock(n);
21705405ff6eSJon Paul Maloy 		if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
21715405ff6eSJon Paul Maloy 			if (le->link) {
21725405ff6eSJon Paul Maloy 				rc = tipc_link_rcv(le->link, skb, &xmitq);
21735405ff6eSJon Paul Maloy 				skb = NULL;
21745405ff6eSJon Paul Maloy 			}
21755405ff6eSJon Paul Maloy 		}
21765405ff6eSJon Paul Maloy 		tipc_node_write_unlock(n);
21775405ff6eSJon Paul Maloy 	}
2178d999297cSJon Paul Maloy 
2179d999297cSJon Paul Maloy 	if (unlikely(rc & TIPC_LINK_UP_EVT))
21806e498158SJon Paul Maloy 		tipc_node_link_up(n, bearer_id, &xmitq);
21816e498158SJon Paul Maloy 
2182d999297cSJon Paul Maloy 	if (unlikely(rc & TIPC_LINK_DOWN_EVT))
2183598411d7SJon Paul Maloy 		tipc_node_link_down(n, bearer_id, false);
21846e498158SJon Paul Maloy 
218552666986SJon Paul Maloy 	if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
2186cad2929dSHoang Huu Le 		tipc_named_rcv(net, &n->bc_entry.namedq,
2187cad2929dSHoang Huu Le 			       &n->bc_entry.named_rcv_nxt,
2188cad2929dSHoang Huu Le 			       &n->bc_entry.named_open);
218923d8335dSJon Paul Maloy 
2190a853e4c6SJon Paul Maloy 	if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
2191a853e4c6SJon Paul Maloy 		tipc_node_mcast_rcv(n);
2192a853e4c6SJon Paul Maloy 
21936e498158SJon Paul Maloy 	if (!skb_queue_empty(&le->inputq))
21946e498158SJon Paul Maloy 		tipc_sk_rcv(net, &le->inputq);
21956e498158SJon Paul Maloy 
21966e498158SJon Paul Maloy 	if (!skb_queue_empty(&xmitq))
2197fc1b6d6dSTuong Lien 		tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
21986e498158SJon Paul Maloy 
2199de058420SXiyu Yang out_node_put:
2200d999297cSJon Paul Maloy 	tipc_node_put(n);
2201d999297cSJon Paul Maloy discard:
2202d999297cSJon Paul Maloy 	kfree_skb(skb);
2203d999297cSJon Paul Maloy }
2204d999297cSJon Paul Maloy 
tipc_node_apply_property(struct net * net,struct tipc_bearer * b,int prop)2205682cd3cfSGhantaKrishnamurthy MohanKrishna void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
2206682cd3cfSGhantaKrishnamurthy MohanKrishna 			      int prop)
220737c64cf6SJon Maloy {
220837c64cf6SJon Maloy 	struct tipc_net *tn = tipc_net(net);
220937c64cf6SJon Maloy 	int bearer_id = b->identity;
221037c64cf6SJon Maloy 	struct sk_buff_head xmitq;
221137c64cf6SJon Maloy 	struct tipc_link_entry *e;
221237c64cf6SJon Maloy 	struct tipc_node *n;
221337c64cf6SJon Maloy 
221437c64cf6SJon Maloy 	__skb_queue_head_init(&xmitq);
221537c64cf6SJon Maloy 
221637c64cf6SJon Maloy 	rcu_read_lock();
221737c64cf6SJon Maloy 
221837c64cf6SJon Maloy 	list_for_each_entry_rcu(n, &tn->node_list, list) {
221937c64cf6SJon Maloy 		tipc_node_write_lock(n);
222037c64cf6SJon Maloy 		e = &n->links[bearer_id];
2221682cd3cfSGhantaKrishnamurthy MohanKrishna 		if (e->link) {
2222682cd3cfSGhantaKrishnamurthy MohanKrishna 			if (prop == TIPC_NLA_PROP_TOL)
2223682cd3cfSGhantaKrishnamurthy MohanKrishna 				tipc_link_set_tolerance(e->link, b->tolerance,
2224682cd3cfSGhantaKrishnamurthy MohanKrishna 							&xmitq);
2225682cd3cfSGhantaKrishnamurthy MohanKrishna 			else if (prop == TIPC_NLA_PROP_MTU)
2226682cd3cfSGhantaKrishnamurthy MohanKrishna 				tipc_link_set_mtu(e->link, b->mtu);
22270398ba9eSCengiz Can 
222806433349SHoang Le 			/* Update MTU for node link entry */
222906433349SHoang Le 			e->mtu = tipc_link_mss(e->link);
22300398ba9eSCengiz Can 		}
22310398ba9eSCengiz Can 
223237c64cf6SJon Maloy 		tipc_node_write_unlock(n);
2233fc1b6d6dSTuong Lien 		tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr, NULL);
223437c64cf6SJon Maloy 	}
223537c64cf6SJon Maloy 
223637c64cf6SJon Maloy 	rcu_read_unlock();
223737c64cf6SJon Maloy }
223837c64cf6SJon Maloy 
tipc_nl_peer_rm(struct sk_buff * skb,struct genl_info * info)2239b3404022SRichard Alpe int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
2240b3404022SRichard Alpe {
2241b3404022SRichard Alpe 	struct net *net = sock_net(skb->sk);
2242b3404022SRichard Alpe 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2243b3404022SRichard Alpe 	struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
22446708ef77SHoang Le 	struct tipc_node *peer, *temp_node;
224543fcd906SHoang Le 	u8 node_id[NODE_ID_LEN];
224643fcd906SHoang Le 	u64 *w0 = (u64 *)&node_id[0];
224743fcd906SHoang Le 	u64 *w1 = (u64 *)&node_id[8];
2248b3404022SRichard Alpe 	u32 addr;
2249b3404022SRichard Alpe 	int err;
2250b3404022SRichard Alpe 
2251b3404022SRichard Alpe 	/* We identify the peer by its net */
2252b3404022SRichard Alpe 	if (!info->attrs[TIPC_NLA_NET])
2253b3404022SRichard Alpe 		return -EINVAL;
2254b3404022SRichard Alpe 
22558cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
22568cb08174SJohannes Berg 					  info->attrs[TIPC_NLA_NET],
22578cb08174SJohannes Berg 					  tipc_nl_net_policy, info->extack);
2258b3404022SRichard Alpe 	if (err)
2259b3404022SRichard Alpe 		return err;
2260b3404022SRichard Alpe 
226143fcd906SHoang Le 	/* attrs[TIPC_NLA_NET_NODEID] and attrs[TIPC_NLA_NET_ADDR] are
226243fcd906SHoang Le 	 * mutually exclusive cases
226343fcd906SHoang Le 	 */
226443fcd906SHoang Le 	if (attrs[TIPC_NLA_NET_ADDR]) {
2265b3404022SRichard Alpe 		addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
226643fcd906SHoang Le 		if (!addr)
226743fcd906SHoang Le 			return -EINVAL;
226843fcd906SHoang Le 	}
226943fcd906SHoang Le 
227043fcd906SHoang Le 	if (attrs[TIPC_NLA_NET_NODEID]) {
227143fcd906SHoang Le 		if (!attrs[TIPC_NLA_NET_NODEID_W1])
227243fcd906SHoang Le 			return -EINVAL;
227343fcd906SHoang Le 		*w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
227443fcd906SHoang Le 		*w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
227543fcd906SHoang Le 		addr = hash128to32(node_id);
227643fcd906SHoang Le 	}
2277b3404022SRichard Alpe 
2278b3404022SRichard Alpe 	if (in_own_node(net, addr))
2279b3404022SRichard Alpe 		return -ENOTSUPP;
2280b3404022SRichard Alpe 
2281b3404022SRichard Alpe 	spin_lock_bh(&tn->node_list_lock);
2282b3404022SRichard Alpe 	peer = tipc_node_find(net, addr);
2283b3404022SRichard Alpe 	if (!peer) {
2284b3404022SRichard Alpe 		spin_unlock_bh(&tn->node_list_lock);
2285b3404022SRichard Alpe 		return -ENXIO;
2286b3404022SRichard Alpe 	}
2287b3404022SRichard Alpe 
2288b3404022SRichard Alpe 	tipc_node_write_lock(peer);
2289b3404022SRichard Alpe 	if (peer->state != SELF_DOWN_PEER_DOWN &&
2290b3404022SRichard Alpe 	    peer->state != SELF_DOWN_PEER_LEAVING) {
2291b3404022SRichard Alpe 		tipc_node_write_unlock(peer);
2292b3404022SRichard Alpe 		err = -EBUSY;
2293b3404022SRichard Alpe 		goto err_out;
2294b3404022SRichard Alpe 	}
2295b3404022SRichard Alpe 
22966a939f36SGhantaKrishnamurthy MohanKrishna 	tipc_node_clear_links(peer);
2297b3404022SRichard Alpe 	tipc_node_write_unlock(peer);
2298b3404022SRichard Alpe 	tipc_node_delete(peer);
2299b3404022SRichard Alpe 
23006708ef77SHoang Le 	/* Calculate cluster capabilities */
23016708ef77SHoang Le 	tn->capabilities = TIPC_NODE_CAPABILITIES;
23026708ef77SHoang Le 	list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
23036708ef77SHoang Le 		tn->capabilities &= temp_node->capabilities;
23046708ef77SHoang Le 	}
2305ba5f6a86SHoang Le 	tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
2306b3404022SRichard Alpe 	err = 0;
2307b3404022SRichard Alpe err_out:
2308b3404022SRichard Alpe 	tipc_node_put(peer);
2309b3404022SRichard Alpe 	spin_unlock_bh(&tn->node_list_lock);
2310b3404022SRichard Alpe 
2311b3404022SRichard Alpe 	return err;
2312b3404022SRichard Alpe }
2313b3404022SRichard Alpe 
tipc_nl_node_dump(struct sk_buff * skb,struct netlink_callback * cb)23143e4b6ab5SRichard Alpe int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
23153e4b6ab5SRichard Alpe {
23163e4b6ab5SRichard Alpe 	int err;
2317f2f9800dSYing Xue 	struct net *net = sock_net(skb->sk);
2318f2f9800dSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
23193e4b6ab5SRichard Alpe 	int done = cb->args[0];
23203e4b6ab5SRichard Alpe 	int last_addr = cb->args[1];
23213e4b6ab5SRichard Alpe 	struct tipc_node *node;
23223e4b6ab5SRichard Alpe 	struct tipc_nl_msg msg;
23233e4b6ab5SRichard Alpe 
23243e4b6ab5SRichard Alpe 	if (done)
23253e4b6ab5SRichard Alpe 		return 0;
23263e4b6ab5SRichard Alpe 
23273e4b6ab5SRichard Alpe 	msg.skb = skb;
23283e4b6ab5SRichard Alpe 	msg.portid = NETLINK_CB(cb->skb).portid;
23293e4b6ab5SRichard Alpe 	msg.seq = cb->nlh->nlmsg_seq;
23303e4b6ab5SRichard Alpe 
23313e4b6ab5SRichard Alpe 	rcu_read_lock();
23328a0f6ebeSYing Xue 	if (last_addr) {
23338a0f6ebeSYing Xue 		node = tipc_node_find(net, last_addr);
23348a0f6ebeSYing Xue 		if (!node) {
23353e4b6ab5SRichard Alpe 			rcu_read_unlock();
23368a0f6ebeSYing Xue 			/* We never set seq or call nl_dump_check_consistent()
23378a0f6ebeSYing Xue 			 * this means that setting prev_seq here will cause the
23388a0f6ebeSYing Xue 			 * consistence check to fail in the netlink callback
23398a0f6ebeSYing Xue 			 * handler. Resulting in the NLMSG_DONE message having
23408a0f6ebeSYing Xue 			 * the NLM_F_DUMP_INTR flag set if the node state
23418a0f6ebeSYing Xue 			 * changed while we released the lock.
23423e4b6ab5SRichard Alpe 			 */
23433e4b6ab5SRichard Alpe 			cb->prev_seq = 1;
23443e4b6ab5SRichard Alpe 			return -EPIPE;
23453e4b6ab5SRichard Alpe 		}
23468a0f6ebeSYing Xue 		tipc_node_put(node);
23478a0f6ebeSYing Xue 	}
23483e4b6ab5SRichard Alpe 
2349f2f9800dSYing Xue 	list_for_each_entry_rcu(node, &tn->node_list, list) {
23504cbf8ac2STuong Lien 		if (node->preliminary)
23514cbf8ac2STuong Lien 			continue;
23523e4b6ab5SRichard Alpe 		if (last_addr) {
23533e4b6ab5SRichard Alpe 			if (node->addr == last_addr)
23543e4b6ab5SRichard Alpe 				last_addr = 0;
23553e4b6ab5SRichard Alpe 			else
23563e4b6ab5SRichard Alpe 				continue;
23573e4b6ab5SRichard Alpe 		}
23583e4b6ab5SRichard Alpe 
23595405ff6eSJon Paul Maloy 		tipc_node_read_lock(node);
23603e4b6ab5SRichard Alpe 		err = __tipc_nl_add_node(&msg, node);
23613e4b6ab5SRichard Alpe 		if (err) {
23623e4b6ab5SRichard Alpe 			last_addr = node->addr;
23635405ff6eSJon Paul Maloy 			tipc_node_read_unlock(node);
23643e4b6ab5SRichard Alpe 			goto out;
23653e4b6ab5SRichard Alpe 		}
23663e4b6ab5SRichard Alpe 
23675405ff6eSJon Paul Maloy 		tipc_node_read_unlock(node);
23683e4b6ab5SRichard Alpe 	}
23693e4b6ab5SRichard Alpe 	done = 1;
23703e4b6ab5SRichard Alpe out:
23713e4b6ab5SRichard Alpe 	cb->args[0] = done;
23723e4b6ab5SRichard Alpe 	cb->args[1] = last_addr;
23733e4b6ab5SRichard Alpe 	rcu_read_unlock();
23743e4b6ab5SRichard Alpe 
23753e4b6ab5SRichard Alpe 	return skb->len;
23763e4b6ab5SRichard Alpe }
23775be9c086SJon Paul Maloy 
237838206d59SJon Paul Maloy /* tipc_node_find_by_name - locate owner node of link by link's name
23795be9c086SJon Paul Maloy  * @net: the applicable net namespace
23805be9c086SJon Paul Maloy  * @name: pointer to link name string
23815be9c086SJon Paul Maloy  * @bearer_id: pointer to index in 'node->links' array where the link was found.
23825be9c086SJon Paul Maloy  *
23835be9c086SJon Paul Maloy  * Returns pointer to node owning the link, or 0 if no matching link is found.
23845be9c086SJon Paul Maloy  */
tipc_node_find_by_name(struct net * net,const char * link_name,unsigned int * bearer_id)238538206d59SJon Paul Maloy static struct tipc_node *tipc_node_find_by_name(struct net *net,
23865be9c086SJon Paul Maloy 						const char *link_name,
23875be9c086SJon Paul Maloy 						unsigned int *bearer_id)
23885be9c086SJon Paul Maloy {
23895be9c086SJon Paul Maloy 	struct tipc_net *tn = net_generic(net, tipc_net_id);
239038206d59SJon Paul Maloy 	struct tipc_link *l;
239138206d59SJon Paul Maloy 	struct tipc_node *n;
23925be9c086SJon Paul Maloy 	struct tipc_node *found_node = NULL;
23935be9c086SJon Paul Maloy 	int i;
23945be9c086SJon Paul Maloy 
23955be9c086SJon Paul Maloy 	*bearer_id = 0;
23965be9c086SJon Paul Maloy 	rcu_read_lock();
239738206d59SJon Paul Maloy 	list_for_each_entry_rcu(n, &tn->node_list, list) {
239838206d59SJon Paul Maloy 		tipc_node_read_lock(n);
23995be9c086SJon Paul Maloy 		for (i = 0; i < MAX_BEARERS; i++) {
240038206d59SJon Paul Maloy 			l = n->links[i].link;
240138206d59SJon Paul Maloy 			if (l && !strcmp(tipc_link_name(l), link_name)) {
24025be9c086SJon Paul Maloy 				*bearer_id = i;
240338206d59SJon Paul Maloy 				found_node = n;
24045be9c086SJon Paul Maloy 				break;
24055be9c086SJon Paul Maloy 			}
24065be9c086SJon Paul Maloy 		}
240738206d59SJon Paul Maloy 		tipc_node_read_unlock(n);
24085be9c086SJon Paul Maloy 		if (found_node)
24095be9c086SJon Paul Maloy 			break;
24105be9c086SJon Paul Maloy 	}
24115be9c086SJon Paul Maloy 	rcu_read_unlock();
24125be9c086SJon Paul Maloy 
24135be9c086SJon Paul Maloy 	return found_node;
24145be9c086SJon Paul Maloy }
24155be9c086SJon Paul Maloy 
tipc_nl_node_set_link(struct sk_buff * skb,struct genl_info * info)24165be9c086SJon Paul Maloy int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
24175be9c086SJon Paul Maloy {
24185be9c086SJon Paul Maloy 	int err;
24195be9c086SJon Paul Maloy 	int res = 0;
24205be9c086SJon Paul Maloy 	int bearer_id;
24215be9c086SJon Paul Maloy 	char *name;
24225be9c086SJon Paul Maloy 	struct tipc_link *link;
24235be9c086SJon Paul Maloy 	struct tipc_node *node;
2424d01332f1SRichard Alpe 	struct sk_buff_head xmitq;
24255be9c086SJon Paul Maloy 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
24265be9c086SJon Paul Maloy 	struct net *net = sock_net(skb->sk);
24275be9c086SJon Paul Maloy 
2428d01332f1SRichard Alpe 	__skb_queue_head_init(&xmitq);
2429d01332f1SRichard Alpe 
24305be9c086SJon Paul Maloy 	if (!info->attrs[TIPC_NLA_LINK])
24315be9c086SJon Paul Maloy 		return -EINVAL;
24325be9c086SJon Paul Maloy 
24338cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
24345be9c086SJon Paul Maloy 					  info->attrs[TIPC_NLA_LINK],
2435fe52145fSJohannes Berg 					  tipc_nl_link_policy, info->extack);
24365be9c086SJon Paul Maloy 	if (err)
24375be9c086SJon Paul Maloy 		return err;
24385be9c086SJon Paul Maloy 
24395be9c086SJon Paul Maloy 	if (!attrs[TIPC_NLA_LINK_NAME])
24405be9c086SJon Paul Maloy 		return -EINVAL;
24415be9c086SJon Paul Maloy 
24425be9c086SJon Paul Maloy 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
24435be9c086SJon Paul Maloy 
24445be9c086SJon Paul Maloy 	if (strcmp(name, tipc_bclink_name) == 0)
24455be9c086SJon Paul Maloy 		return tipc_nl_bc_link_set(net, attrs);
24465be9c086SJon Paul Maloy 
244738206d59SJon Paul Maloy 	node = tipc_node_find_by_name(net, name, &bearer_id);
24485be9c086SJon Paul Maloy 	if (!node)
24495be9c086SJon Paul Maloy 		return -EINVAL;
24505be9c086SJon Paul Maloy 
24515be9c086SJon Paul Maloy 	tipc_node_read_lock(node);
24525be9c086SJon Paul Maloy 
24535be9c086SJon Paul Maloy 	link = node->links[bearer_id].link;
24545be9c086SJon Paul Maloy 	if (!link) {
24555be9c086SJon Paul Maloy 		res = -EINVAL;
24565be9c086SJon Paul Maloy 		goto out;
24575be9c086SJon Paul Maloy 	}
24585be9c086SJon Paul Maloy 
24595be9c086SJon Paul Maloy 	if (attrs[TIPC_NLA_LINK_PROP]) {
24605be9c086SJon Paul Maloy 		struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
24615be9c086SJon Paul Maloy 
246216ad3f40SJon Maloy 		err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
24635be9c086SJon Paul Maloy 		if (err) {
24645be9c086SJon Paul Maloy 			res = err;
24655be9c086SJon Paul Maloy 			goto out;
24665be9c086SJon Paul Maloy 		}
24675be9c086SJon Paul Maloy 
24685be9c086SJon Paul Maloy 		if (props[TIPC_NLA_PROP_TOL]) {
24695be9c086SJon Paul Maloy 			u32 tol;
24705be9c086SJon Paul Maloy 
24715be9c086SJon Paul Maloy 			tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2472d01332f1SRichard Alpe 			tipc_link_set_tolerance(link, tol, &xmitq);
24735be9c086SJon Paul Maloy 		}
24745be9c086SJon Paul Maloy 		if (props[TIPC_NLA_PROP_PRIO]) {
24755be9c086SJon Paul Maloy 			u32 prio;
24765be9c086SJon Paul Maloy 
24775be9c086SJon Paul Maloy 			prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2478d01332f1SRichard Alpe 			tipc_link_set_prio(link, prio, &xmitq);
24795be9c086SJon Paul Maloy 		}
24805be9c086SJon Paul Maloy 		if (props[TIPC_NLA_PROP_WIN]) {
248116ad3f40SJon Maloy 			u32 max_win;
24825be9c086SJon Paul Maloy 
248316ad3f40SJon Maloy 			max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
248416ad3f40SJon Maloy 			tipc_link_set_queue_limits(link,
248516ad3f40SJon Maloy 						   tipc_link_min_win(link),
248616ad3f40SJon Maloy 						   max_win);
24875be9c086SJon Paul Maloy 		}
24885be9c086SJon Paul Maloy 	}
24895be9c086SJon Paul Maloy 
24905be9c086SJon Paul Maloy out:
24915be9c086SJon Paul Maloy 	tipc_node_read_unlock(node);
2492fc1b6d6dSTuong Lien 	tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr,
2493fc1b6d6dSTuong Lien 			 NULL);
24945be9c086SJon Paul Maloy 	return res;
24955be9c086SJon Paul Maloy }
24965be9c086SJon Paul Maloy 
tipc_nl_node_get_link(struct sk_buff * skb,struct genl_info * info)24975be9c086SJon Paul Maloy int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
24985be9c086SJon Paul Maloy {
24995be9c086SJon Paul Maloy 	struct net *net = genl_info_net(info);
250094f6a80cSYing Xue 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
25015be9c086SJon Paul Maloy 	struct tipc_nl_msg msg;
25025be9c086SJon Paul Maloy 	char *name;
25035be9c086SJon Paul Maloy 	int err;
25045be9c086SJon Paul Maloy 
25055be9c086SJon Paul Maloy 	msg.portid = info->snd_portid;
25065be9c086SJon Paul Maloy 	msg.seq = info->snd_seq;
25075be9c086SJon Paul Maloy 
250894f6a80cSYing Xue 	if (!info->attrs[TIPC_NLA_LINK])
25095be9c086SJon Paul Maloy 		return -EINVAL;
251094f6a80cSYing Xue 
25118cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
251294f6a80cSYing Xue 					  info->attrs[TIPC_NLA_LINK],
251394f6a80cSYing Xue 					  tipc_nl_link_policy, info->extack);
251494f6a80cSYing Xue 	if (err)
251594f6a80cSYing Xue 		return err;
251694f6a80cSYing Xue 
251794f6a80cSYing Xue 	if (!attrs[TIPC_NLA_LINK_NAME])
251894f6a80cSYing Xue 		return -EINVAL;
251994f6a80cSYing Xue 
252094f6a80cSYing Xue 	name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
25215be9c086SJon Paul Maloy 
25225be9c086SJon Paul Maloy 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
25235be9c086SJon Paul Maloy 	if (!msg.skb)
25245be9c086SJon Paul Maloy 		return -ENOMEM;
25255be9c086SJon Paul Maloy 
25265be9c086SJon Paul Maloy 	if (strcmp(name, tipc_bclink_name) == 0) {
252703b6fefdSTuong Lien 		err = tipc_nl_add_bc_link(net, &msg, tipc_net(net)->bcl);
252859b36613SCong Wang 		if (err)
252959b36613SCong Wang 			goto err_free;
25305be9c086SJon Paul Maloy 	} else {
25315be9c086SJon Paul Maloy 		int bearer_id;
25325be9c086SJon Paul Maloy 		struct tipc_node *node;
25335be9c086SJon Paul Maloy 		struct tipc_link *link;
25345be9c086SJon Paul Maloy 
253538206d59SJon Paul Maloy 		node = tipc_node_find_by_name(net, name, &bearer_id);
253659b36613SCong Wang 		if (!node) {
253759b36613SCong Wang 			err = -EINVAL;
253859b36613SCong Wang 			goto err_free;
253959b36613SCong Wang 		}
25405be9c086SJon Paul Maloy 
25415be9c086SJon Paul Maloy 		tipc_node_read_lock(node);
25425be9c086SJon Paul Maloy 		link = node->links[bearer_id].link;
25435be9c086SJon Paul Maloy 		if (!link) {
25445be9c086SJon Paul Maloy 			tipc_node_read_unlock(node);
254559b36613SCong Wang 			err = -EINVAL;
254659b36613SCong Wang 			goto err_free;
25475be9c086SJon Paul Maloy 		}
25485be9c086SJon Paul Maloy 
25495be9c086SJon Paul Maloy 		err = __tipc_nl_add_link(net, &msg, link, 0);
25505be9c086SJon Paul Maloy 		tipc_node_read_unlock(node);
255159b36613SCong Wang 		if (err)
255259b36613SCong Wang 			goto err_free;
25535be9c086SJon Paul Maloy 	}
25545be9c086SJon Paul Maloy 
25555be9c086SJon Paul Maloy 	return genlmsg_reply(msg.skb, info);
255659b36613SCong Wang 
255759b36613SCong Wang err_free:
255859b36613SCong Wang 	nlmsg_free(msg.skb);
255959b36613SCong Wang 	return err;
25605be9c086SJon Paul Maloy }
25615be9c086SJon Paul Maloy 
tipc_nl_node_reset_link_stats(struct sk_buff * skb,struct genl_info * info)25625be9c086SJon Paul Maloy int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
25635be9c086SJon Paul Maloy {
25645be9c086SJon Paul Maloy 	int err;
25655be9c086SJon Paul Maloy 	char *link_name;
25665be9c086SJon Paul Maloy 	unsigned int bearer_id;
25675be9c086SJon Paul Maloy 	struct tipc_link *link;
25685be9c086SJon Paul Maloy 	struct tipc_node *node;
25695be9c086SJon Paul Maloy 	struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
25705be9c086SJon Paul Maloy 	struct net *net = sock_net(skb->sk);
257103b6fefdSTuong Lien 	struct tipc_net *tn = tipc_net(net);
25725be9c086SJon Paul Maloy 	struct tipc_link_entry *le;
25735be9c086SJon Paul Maloy 
25745be9c086SJon Paul Maloy 	if (!info->attrs[TIPC_NLA_LINK])
25755be9c086SJon Paul Maloy 		return -EINVAL;
25765be9c086SJon Paul Maloy 
25778cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
25785be9c086SJon Paul Maloy 					  info->attrs[TIPC_NLA_LINK],
2579fe52145fSJohannes Berg 					  tipc_nl_link_policy, info->extack);
25805be9c086SJon Paul Maloy 	if (err)
25815be9c086SJon Paul Maloy 		return err;
25825be9c086SJon Paul Maloy 
25835be9c086SJon Paul Maloy 	if (!attrs[TIPC_NLA_LINK_NAME])
25845be9c086SJon Paul Maloy 		return -EINVAL;
25855be9c086SJon Paul Maloy 
25865be9c086SJon Paul Maloy 	link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
25875be9c086SJon Paul Maloy 
258803b6fefdSTuong Lien 	err = -EINVAL;
258903b6fefdSTuong Lien 	if (!strcmp(link_name, tipc_bclink_name)) {
259003b6fefdSTuong Lien 		err = tipc_bclink_reset_stats(net, tipc_bc_sndlink(net));
25915be9c086SJon Paul Maloy 		if (err)
25925be9c086SJon Paul Maloy 			return err;
25935be9c086SJon Paul Maloy 		return 0;
259403b6fefdSTuong Lien 	} else if (strstr(link_name, tipc_bclink_name)) {
259503b6fefdSTuong Lien 		rcu_read_lock();
259603b6fefdSTuong Lien 		list_for_each_entry_rcu(node, &tn->node_list, list) {
259703b6fefdSTuong Lien 			tipc_node_read_lock(node);
259803b6fefdSTuong Lien 			link = node->bc_entry.link;
259903b6fefdSTuong Lien 			if (link && !strcmp(link_name, tipc_link_name(link))) {
260003b6fefdSTuong Lien 				err = tipc_bclink_reset_stats(net, link);
260103b6fefdSTuong Lien 				tipc_node_read_unlock(node);
260203b6fefdSTuong Lien 				break;
260303b6fefdSTuong Lien 			}
260403b6fefdSTuong Lien 			tipc_node_read_unlock(node);
260503b6fefdSTuong Lien 		}
260603b6fefdSTuong Lien 		rcu_read_unlock();
260703b6fefdSTuong Lien 		return err;
26085be9c086SJon Paul Maloy 	}
26095be9c086SJon Paul Maloy 
261038206d59SJon Paul Maloy 	node = tipc_node_find_by_name(net, link_name, &bearer_id);
26115be9c086SJon Paul Maloy 	if (!node)
26125be9c086SJon Paul Maloy 		return -EINVAL;
26135be9c086SJon Paul Maloy 
26145be9c086SJon Paul Maloy 	le = &node->links[bearer_id];
26155be9c086SJon Paul Maloy 	tipc_node_read_lock(node);
26165be9c086SJon Paul Maloy 	spin_lock_bh(&le->lock);
26175be9c086SJon Paul Maloy 	link = node->links[bearer_id].link;
26185be9c086SJon Paul Maloy 	if (!link) {
26195be9c086SJon Paul Maloy 		spin_unlock_bh(&le->lock);
26205be9c086SJon Paul Maloy 		tipc_node_read_unlock(node);
26215be9c086SJon Paul Maloy 		return -EINVAL;
26225be9c086SJon Paul Maloy 	}
262338206d59SJon Paul Maloy 	tipc_link_reset_stats(link);
26245be9c086SJon Paul Maloy 	spin_unlock_bh(&le->lock);
26255be9c086SJon Paul Maloy 	tipc_node_read_unlock(node);
26265be9c086SJon Paul Maloy 	return 0;
26275be9c086SJon Paul Maloy }
26285be9c086SJon Paul Maloy 
26295be9c086SJon Paul Maloy /* Caller should hold node lock  */
__tipc_nl_add_node_links(struct net * net,struct tipc_nl_msg * msg,struct tipc_node * node,u32 * prev_link,bool bc_link)26305be9c086SJon Paul Maloy static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
263103b6fefdSTuong Lien 				    struct tipc_node *node, u32 *prev_link,
263203b6fefdSTuong Lien 				    bool bc_link)
26335be9c086SJon Paul Maloy {
26345be9c086SJon Paul Maloy 	u32 i;
26355be9c086SJon Paul Maloy 	int err;
26365be9c086SJon Paul Maloy 
26375be9c086SJon Paul Maloy 	for (i = *prev_link; i < MAX_BEARERS; i++) {
26385be9c086SJon Paul Maloy 		*prev_link = i;
26395be9c086SJon Paul Maloy 
26405be9c086SJon Paul Maloy 		if (!node->links[i].link)
26415be9c086SJon Paul Maloy 			continue;
26425be9c086SJon Paul Maloy 
26435be9c086SJon Paul Maloy 		err = __tipc_nl_add_link(net, msg,
26445be9c086SJon Paul Maloy 					 node->links[i].link, NLM_F_MULTI);
26455be9c086SJon Paul Maloy 		if (err)
26465be9c086SJon Paul Maloy 			return err;
26475be9c086SJon Paul Maloy 	}
264803b6fefdSTuong Lien 
264903b6fefdSTuong Lien 	if (bc_link) {
265003b6fefdSTuong Lien 		*prev_link = i;
265103b6fefdSTuong Lien 		err = tipc_nl_add_bc_link(net, msg, node->bc_entry.link);
265203b6fefdSTuong Lien 		if (err)
265303b6fefdSTuong Lien 			return err;
265403b6fefdSTuong Lien 	}
265503b6fefdSTuong Lien 
26565be9c086SJon Paul Maloy 	*prev_link = 0;
26575be9c086SJon Paul Maloy 
26585be9c086SJon Paul Maloy 	return 0;
26595be9c086SJon Paul Maloy }
26605be9c086SJon Paul Maloy 
tipc_nl_node_dump_link(struct sk_buff * skb,struct netlink_callback * cb)266138206d59SJon Paul Maloy int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
26625be9c086SJon Paul Maloy {
26635be9c086SJon Paul Maloy 	struct net *net = sock_net(skb->sk);
26647288dd2fSJakub Kicinski 	struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
266503b6fefdSTuong Lien 	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
26665be9c086SJon Paul Maloy 	struct tipc_net *tn = net_generic(net, tipc_net_id);
26675be9c086SJon Paul Maloy 	struct tipc_node *node;
26685be9c086SJon Paul Maloy 	struct tipc_nl_msg msg;
26695be9c086SJon Paul Maloy 	u32 prev_node = cb->args[0];
26705be9c086SJon Paul Maloy 	u32 prev_link = cb->args[1];
26715be9c086SJon Paul Maloy 	int done = cb->args[2];
267203b6fefdSTuong Lien 	bool bc_link = cb->args[3];
26735be9c086SJon Paul Maloy 	int err;
26745be9c086SJon Paul Maloy 
26755be9c086SJon Paul Maloy 	if (done)
26765be9c086SJon Paul Maloy 		return 0;
26775be9c086SJon Paul Maloy 
267803b6fefdSTuong Lien 	if (!prev_node) {
267903b6fefdSTuong Lien 		/* Check if broadcast-receiver links dumping is needed */
268003b6fefdSTuong Lien 		if (attrs && attrs[TIPC_NLA_LINK]) {
268103b6fefdSTuong Lien 			err = nla_parse_nested_deprecated(link,
268203b6fefdSTuong Lien 							  TIPC_NLA_LINK_MAX,
268303b6fefdSTuong Lien 							  attrs[TIPC_NLA_LINK],
268403b6fefdSTuong Lien 							  tipc_nl_link_policy,
268503b6fefdSTuong Lien 							  NULL);
268603b6fefdSTuong Lien 			if (unlikely(err))
268703b6fefdSTuong Lien 				return err;
268803b6fefdSTuong Lien 			if (unlikely(!link[TIPC_NLA_LINK_BROADCAST]))
268903b6fefdSTuong Lien 				return -EINVAL;
269003b6fefdSTuong Lien 			bc_link = true;
269103b6fefdSTuong Lien 		}
269203b6fefdSTuong Lien 	}
269303b6fefdSTuong Lien 
26945be9c086SJon Paul Maloy 	msg.skb = skb;
26955be9c086SJon Paul Maloy 	msg.portid = NETLINK_CB(cb->skb).portid;
26965be9c086SJon Paul Maloy 	msg.seq = cb->nlh->nlmsg_seq;
26975be9c086SJon Paul Maloy 
26985be9c086SJon Paul Maloy 	rcu_read_lock();
26995be9c086SJon Paul Maloy 	if (prev_node) {
27005be9c086SJon Paul Maloy 		node = tipc_node_find(net, prev_node);
27015be9c086SJon Paul Maloy 		if (!node) {
27025be9c086SJon Paul Maloy 			/* We never set seq or call nl_dump_check_consistent()
27035be9c086SJon Paul Maloy 			 * this means that setting prev_seq here will cause the
27045be9c086SJon Paul Maloy 			 * consistence check to fail in the netlink callback
27055be9c086SJon Paul Maloy 			 * handler. Resulting in the last NLMSG_DONE message
27065be9c086SJon Paul Maloy 			 * having the NLM_F_DUMP_INTR flag set.
27075be9c086SJon Paul Maloy 			 */
27085be9c086SJon Paul Maloy 			cb->prev_seq = 1;
27095be9c086SJon Paul Maloy 			goto out;
27105be9c086SJon Paul Maloy 		}
27115be9c086SJon Paul Maloy 		tipc_node_put(node);
27125be9c086SJon Paul Maloy 
27135be9c086SJon Paul Maloy 		list_for_each_entry_continue_rcu(node, &tn->node_list,
27145be9c086SJon Paul Maloy 						 list) {
27155be9c086SJon Paul Maloy 			tipc_node_read_lock(node);
27165be9c086SJon Paul Maloy 			err = __tipc_nl_add_node_links(net, &msg, node,
271703b6fefdSTuong Lien 						       &prev_link, bc_link);
27185be9c086SJon Paul Maloy 			tipc_node_read_unlock(node);
27195be9c086SJon Paul Maloy 			if (err)
27205be9c086SJon Paul Maloy 				goto out;
27215be9c086SJon Paul Maloy 
27225be9c086SJon Paul Maloy 			prev_node = node->addr;
27235be9c086SJon Paul Maloy 		}
27245be9c086SJon Paul Maloy 	} else {
272503b6fefdSTuong Lien 		err = tipc_nl_add_bc_link(net, &msg, tn->bcl);
27265be9c086SJon Paul Maloy 		if (err)
27275be9c086SJon Paul Maloy 			goto out;
27285be9c086SJon Paul Maloy 
27295be9c086SJon Paul Maloy 		list_for_each_entry_rcu(node, &tn->node_list, list) {
27305be9c086SJon Paul Maloy 			tipc_node_read_lock(node);
27315be9c086SJon Paul Maloy 			err = __tipc_nl_add_node_links(net, &msg, node,
273203b6fefdSTuong Lien 						       &prev_link, bc_link);
27335be9c086SJon Paul Maloy 			tipc_node_read_unlock(node);
27345be9c086SJon Paul Maloy 			if (err)
27355be9c086SJon Paul Maloy 				goto out;
27365be9c086SJon Paul Maloy 
27375be9c086SJon Paul Maloy 			prev_node = node->addr;
27385be9c086SJon Paul Maloy 		}
27395be9c086SJon Paul Maloy 	}
27405be9c086SJon Paul Maloy 	done = 1;
27415be9c086SJon Paul Maloy out:
27425be9c086SJon Paul Maloy 	rcu_read_unlock();
27435be9c086SJon Paul Maloy 
27445be9c086SJon Paul Maloy 	cb->args[0] = prev_node;
27455be9c086SJon Paul Maloy 	cb->args[1] = prev_link;
27465be9c086SJon Paul Maloy 	cb->args[2] = done;
274703b6fefdSTuong Lien 	cb->args[3] = bc_link;
27485be9c086SJon Paul Maloy 
27495be9c086SJon Paul Maloy 	return skb->len;
27505be9c086SJon Paul Maloy }
27517b3f5229SParthasarathy Bhuvaragan 
tipc_nl_node_set_monitor(struct sk_buff * skb,struct genl_info * info)27527b3f5229SParthasarathy Bhuvaragan int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
27537b3f5229SParthasarathy Bhuvaragan {
27547b3f5229SParthasarathy Bhuvaragan 	struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
27557b3f5229SParthasarathy Bhuvaragan 	struct net *net = sock_net(skb->sk);
27567b3f5229SParthasarathy Bhuvaragan 	int err;
27577b3f5229SParthasarathy Bhuvaragan 
27587b3f5229SParthasarathy Bhuvaragan 	if (!info->attrs[TIPC_NLA_MON])
27597b3f5229SParthasarathy Bhuvaragan 		return -EINVAL;
27607b3f5229SParthasarathy Bhuvaragan 
27618cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
27627b3f5229SParthasarathy Bhuvaragan 					  info->attrs[TIPC_NLA_MON],
27638cb08174SJohannes Berg 					  tipc_nl_monitor_policy,
27648cb08174SJohannes Berg 					  info->extack);
27657b3f5229SParthasarathy Bhuvaragan 	if (err)
27667b3f5229SParthasarathy Bhuvaragan 		return err;
27677b3f5229SParthasarathy Bhuvaragan 
27687b3f5229SParthasarathy Bhuvaragan 	if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
27697b3f5229SParthasarathy Bhuvaragan 		u32 val;
27707b3f5229SParthasarathy Bhuvaragan 
27717b3f5229SParthasarathy Bhuvaragan 		val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
27727b3f5229SParthasarathy Bhuvaragan 		err = tipc_nl_monitor_set_threshold(net, val);
27737b3f5229SParthasarathy Bhuvaragan 		if (err)
27747b3f5229SParthasarathy Bhuvaragan 			return err;
27757b3f5229SParthasarathy Bhuvaragan 	}
27767b3f5229SParthasarathy Bhuvaragan 
27777b3f5229SParthasarathy Bhuvaragan 	return 0;
27787b3f5229SParthasarathy Bhuvaragan }
2779bf1035b2SParthasarathy Bhuvaragan 
__tipc_nl_add_monitor_prop(struct net * net,struct tipc_nl_msg * msg)2780bf1035b2SParthasarathy Bhuvaragan static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2781bf1035b2SParthasarathy Bhuvaragan {
2782bf1035b2SParthasarathy Bhuvaragan 	struct nlattr *attrs;
2783bf1035b2SParthasarathy Bhuvaragan 	void *hdr;
2784bf1035b2SParthasarathy Bhuvaragan 	u32 val;
2785bf1035b2SParthasarathy Bhuvaragan 
2786bf1035b2SParthasarathy Bhuvaragan 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2787bf1035b2SParthasarathy Bhuvaragan 			  0, TIPC_NL_MON_GET);
2788bf1035b2SParthasarathy Bhuvaragan 	if (!hdr)
2789bf1035b2SParthasarathy Bhuvaragan 		return -EMSGSIZE;
2790bf1035b2SParthasarathy Bhuvaragan 
2791ae0be8deSMichal Kubecek 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
2792bf1035b2SParthasarathy Bhuvaragan 	if (!attrs)
2793bf1035b2SParthasarathy Bhuvaragan 		goto msg_full;
2794bf1035b2SParthasarathy Bhuvaragan 
2795bf1035b2SParthasarathy Bhuvaragan 	val = tipc_nl_monitor_get_threshold(net);
2796bf1035b2SParthasarathy Bhuvaragan 
2797bf1035b2SParthasarathy Bhuvaragan 	if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2798bf1035b2SParthasarathy Bhuvaragan 		goto attr_msg_full;
2799bf1035b2SParthasarathy Bhuvaragan 
2800bf1035b2SParthasarathy Bhuvaragan 	nla_nest_end(msg->skb, attrs);
2801bf1035b2SParthasarathy Bhuvaragan 	genlmsg_end(msg->skb, hdr);
2802bf1035b2SParthasarathy Bhuvaragan 
2803bf1035b2SParthasarathy Bhuvaragan 	return 0;
2804bf1035b2SParthasarathy Bhuvaragan 
2805bf1035b2SParthasarathy Bhuvaragan attr_msg_full:
2806bf1035b2SParthasarathy Bhuvaragan 	nla_nest_cancel(msg->skb, attrs);
2807bf1035b2SParthasarathy Bhuvaragan msg_full:
2808bf1035b2SParthasarathy Bhuvaragan 	genlmsg_cancel(msg->skb, hdr);
2809bf1035b2SParthasarathy Bhuvaragan 
2810bf1035b2SParthasarathy Bhuvaragan 	return -EMSGSIZE;
2811bf1035b2SParthasarathy Bhuvaragan }
2812bf1035b2SParthasarathy Bhuvaragan 
tipc_nl_node_get_monitor(struct sk_buff * skb,struct genl_info * info)2813bf1035b2SParthasarathy Bhuvaragan int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2814bf1035b2SParthasarathy Bhuvaragan {
2815bf1035b2SParthasarathy Bhuvaragan 	struct net *net = sock_net(skb->sk);
2816bf1035b2SParthasarathy Bhuvaragan 	struct tipc_nl_msg msg;
2817bf1035b2SParthasarathy Bhuvaragan 	int err;
2818bf1035b2SParthasarathy Bhuvaragan 
2819bf1035b2SParthasarathy Bhuvaragan 	msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
282078302fd4SPan Bian 	if (!msg.skb)
282178302fd4SPan Bian 		return -ENOMEM;
2822bf1035b2SParthasarathy Bhuvaragan 	msg.portid = info->snd_portid;
2823bf1035b2SParthasarathy Bhuvaragan 	msg.seq = info->snd_seq;
2824bf1035b2SParthasarathy Bhuvaragan 
2825bf1035b2SParthasarathy Bhuvaragan 	err = __tipc_nl_add_monitor_prop(net, &msg);
2826bf1035b2SParthasarathy Bhuvaragan 	if (err) {
2827bf1035b2SParthasarathy Bhuvaragan 		nlmsg_free(msg.skb);
2828bf1035b2SParthasarathy Bhuvaragan 		return err;
2829bf1035b2SParthasarathy Bhuvaragan 	}
2830bf1035b2SParthasarathy Bhuvaragan 
2831bf1035b2SParthasarathy Bhuvaragan 	return genlmsg_reply(msg.skb, info);
2832bf1035b2SParthasarathy Bhuvaragan }
2833cf6f7e1dSParthasarathy Bhuvaragan 
tipc_nl_node_dump_monitor(struct sk_buff * skb,struct netlink_callback * cb)2834cf6f7e1dSParthasarathy Bhuvaragan int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2835cf6f7e1dSParthasarathy Bhuvaragan {
2836cf6f7e1dSParthasarathy Bhuvaragan 	struct net *net = sock_net(skb->sk);
2837cf6f7e1dSParthasarathy Bhuvaragan 	u32 prev_bearer = cb->args[0];
2838cf6f7e1dSParthasarathy Bhuvaragan 	struct tipc_nl_msg msg;
283936a50a98STung Nguyen 	int bearer_id;
2840cf6f7e1dSParthasarathy Bhuvaragan 	int err;
2841cf6f7e1dSParthasarathy Bhuvaragan 
2842cf6f7e1dSParthasarathy Bhuvaragan 	if (prev_bearer == MAX_BEARERS)
2843cf6f7e1dSParthasarathy Bhuvaragan 		return 0;
2844cf6f7e1dSParthasarathy Bhuvaragan 
2845cf6f7e1dSParthasarathy Bhuvaragan 	msg.skb = skb;
2846cf6f7e1dSParthasarathy Bhuvaragan 	msg.portid = NETLINK_CB(cb->skb).portid;
2847cf6f7e1dSParthasarathy Bhuvaragan 	msg.seq = cb->nlh->nlmsg_seq;
2848cf6f7e1dSParthasarathy Bhuvaragan 
2849cf6f7e1dSParthasarathy Bhuvaragan 	rtnl_lock();
285036a50a98STung Nguyen 	for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
28517dbc73e6SJon Maloy 		err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2852cf6f7e1dSParthasarathy Bhuvaragan 		if (err)
285336a50a98STung Nguyen 			break;
2854cf6f7e1dSParthasarathy Bhuvaragan 	}
2855cf6f7e1dSParthasarathy Bhuvaragan 	rtnl_unlock();
285636a50a98STung Nguyen 	cb->args[0] = bearer_id;
2857cf6f7e1dSParthasarathy Bhuvaragan 
2858cf6f7e1dSParthasarathy Bhuvaragan 	return skb->len;
2859cf6f7e1dSParthasarathy Bhuvaragan }
2860cf6f7e1dSParthasarathy Bhuvaragan 
tipc_nl_node_dump_monitor_peer(struct sk_buff * skb,struct netlink_callback * cb)2861cf6f7e1dSParthasarathy Bhuvaragan int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2862cf6f7e1dSParthasarathy Bhuvaragan 				   struct netlink_callback *cb)
2863cf6f7e1dSParthasarathy Bhuvaragan {
2864cf6f7e1dSParthasarathy Bhuvaragan 	struct net *net = sock_net(skb->sk);
2865cf6f7e1dSParthasarathy Bhuvaragan 	u32 prev_node = cb->args[1];
2866cf6f7e1dSParthasarathy Bhuvaragan 	u32 bearer_id = cb->args[2];
2867cf6f7e1dSParthasarathy Bhuvaragan 	int done = cb->args[0];
2868cf6f7e1dSParthasarathy Bhuvaragan 	struct tipc_nl_msg msg;
2869cf6f7e1dSParthasarathy Bhuvaragan 	int err;
2870cf6f7e1dSParthasarathy Bhuvaragan 
2871cf6f7e1dSParthasarathy Bhuvaragan 	if (!prev_node) {
28727288dd2fSJakub Kicinski 		struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
2873cf6f7e1dSParthasarathy Bhuvaragan 		struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2874cf6f7e1dSParthasarathy Bhuvaragan 
2875cf6f7e1dSParthasarathy Bhuvaragan 		if (!attrs[TIPC_NLA_MON])
2876cf6f7e1dSParthasarathy Bhuvaragan 			return -EINVAL;
2877cf6f7e1dSParthasarathy Bhuvaragan 
28788cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
2879cf6f7e1dSParthasarathy Bhuvaragan 						  attrs[TIPC_NLA_MON],
28808cb08174SJohannes Berg 						  tipc_nl_monitor_policy,
28818cb08174SJohannes Berg 						  NULL);
2882cf6f7e1dSParthasarathy Bhuvaragan 		if (err)
2883cf6f7e1dSParthasarathy Bhuvaragan 			return err;
2884cf6f7e1dSParthasarathy Bhuvaragan 
2885cf6f7e1dSParthasarathy Bhuvaragan 		if (!mon[TIPC_NLA_MON_REF])
2886cf6f7e1dSParthasarathy Bhuvaragan 			return -EINVAL;
2887cf6f7e1dSParthasarathy Bhuvaragan 
2888cf6f7e1dSParthasarathy Bhuvaragan 		bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2889cf6f7e1dSParthasarathy Bhuvaragan 
2890cf6f7e1dSParthasarathy Bhuvaragan 		if (bearer_id >= MAX_BEARERS)
2891cf6f7e1dSParthasarathy Bhuvaragan 			return -EINVAL;
2892cf6f7e1dSParthasarathy Bhuvaragan 	}
2893cf6f7e1dSParthasarathy Bhuvaragan 
2894cf6f7e1dSParthasarathy Bhuvaragan 	if (done)
2895cf6f7e1dSParthasarathy Bhuvaragan 		return 0;
2896cf6f7e1dSParthasarathy Bhuvaragan 
2897cf6f7e1dSParthasarathy Bhuvaragan 	msg.skb = skb;
2898cf6f7e1dSParthasarathy Bhuvaragan 	msg.portid = NETLINK_CB(cb->skb).portid;
2899cf6f7e1dSParthasarathy Bhuvaragan 	msg.seq = cb->nlh->nlmsg_seq;
2900cf6f7e1dSParthasarathy Bhuvaragan 
2901cf6f7e1dSParthasarathy Bhuvaragan 	rtnl_lock();
2902cf6f7e1dSParthasarathy Bhuvaragan 	err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2903cf6f7e1dSParthasarathy Bhuvaragan 	if (!err)
2904cf6f7e1dSParthasarathy Bhuvaragan 		done = 1;
2905cf6f7e1dSParthasarathy Bhuvaragan 
2906cf6f7e1dSParthasarathy Bhuvaragan 	rtnl_unlock();
2907cf6f7e1dSParthasarathy Bhuvaragan 	cb->args[0] = done;
2908cf6f7e1dSParthasarathy Bhuvaragan 	cb->args[1] = prev_node;
2909cf6f7e1dSParthasarathy Bhuvaragan 	cb->args[2] = bearer_id;
2910cf6f7e1dSParthasarathy Bhuvaragan 
2911cf6f7e1dSParthasarathy Bhuvaragan 	return skb->len;
2912cf6f7e1dSParthasarathy Bhuvaragan }
2913b4b9771bSTuong Lien 
2914e1f32190STuong Lien #ifdef CONFIG_TIPC_CRYPTO
tipc_nl_retrieve_key(struct nlattr ** attrs,struct tipc_aead_key ** pkey)2915e1f32190STuong Lien static int tipc_nl_retrieve_key(struct nlattr **attrs,
29160217ed28SEric Dumazet 				struct tipc_aead_key **pkey)
2917e1f32190STuong Lien {
2918e1f32190STuong Lien 	struct nlattr *attr = attrs[TIPC_NLA_NODE_KEY];
29190217ed28SEric Dumazet 	struct tipc_aead_key *key;
2920e1f32190STuong Lien 
2921e1f32190STuong Lien 	if (!attr)
2922e1f32190STuong Lien 		return -ENODATA;
2923e1f32190STuong Lien 
29240217ed28SEric Dumazet 	if (nla_len(attr) < sizeof(*key))
29250217ed28SEric Dumazet 		return -EINVAL;
29260217ed28SEric Dumazet 	key = (struct tipc_aead_key *)nla_data(attr);
29270217ed28SEric Dumazet 	if (key->keylen > TIPC_AEAD_KEYLEN_MAX ||
29280217ed28SEric Dumazet 	    nla_len(attr) < tipc_aead_key_size(key))
2929e1f32190STuong Lien 		return -EINVAL;
2930e1f32190STuong Lien 
29310217ed28SEric Dumazet 	*pkey = key;
2932e1f32190STuong Lien 	return 0;
2933e1f32190STuong Lien }
2934e1f32190STuong Lien 
tipc_nl_retrieve_nodeid(struct nlattr ** attrs,u8 ** node_id)2935e1f32190STuong Lien static int tipc_nl_retrieve_nodeid(struct nlattr **attrs, u8 **node_id)
2936e1f32190STuong Lien {
2937e1f32190STuong Lien 	struct nlattr *attr = attrs[TIPC_NLA_NODE_ID];
2938e1f32190STuong Lien 
2939e1f32190STuong Lien 	if (!attr)
2940e1f32190STuong Lien 		return -ENODATA;
2941e1f32190STuong Lien 
2942e1f32190STuong Lien 	if (nla_len(attr) < TIPC_NODEID_LEN)
2943e1f32190STuong Lien 		return -EINVAL;
2944e1f32190STuong Lien 
2945e1f32190STuong Lien 	*node_id = (u8 *)nla_data(attr);
2946e1f32190STuong Lien 	return 0;
2947e1f32190STuong Lien }
2948e1f32190STuong Lien 
tipc_nl_retrieve_rekeying(struct nlattr ** attrs,u32 * intv)294923700da2STuong Lien static int tipc_nl_retrieve_rekeying(struct nlattr **attrs, u32 *intv)
295023700da2STuong Lien {
295123700da2STuong Lien 	struct nlattr *attr = attrs[TIPC_NLA_NODE_REKEYING];
295223700da2STuong Lien 
295323700da2STuong Lien 	if (!attr)
295423700da2STuong Lien 		return -ENODATA;
295523700da2STuong Lien 
295623700da2STuong Lien 	*intv = nla_get_u32(attr);
295723700da2STuong Lien 	return 0;
295823700da2STuong Lien }
295923700da2STuong Lien 
__tipc_nl_node_set_key(struct sk_buff * skb,struct genl_info * info)29602437fd7bSChen Wandun static int __tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
2961e1f32190STuong Lien {
2962e1f32190STuong Lien 	struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1];
2963e1f32190STuong Lien 	struct net *net = sock_net(skb->sk);
2964f779bf79STuong Lien 	struct tipc_crypto *tx = tipc_net(net)->crypto_tx, *c = tx;
2965e1f32190STuong Lien 	struct tipc_node *n = NULL;
2966e1f32190STuong Lien 	struct tipc_aead_key *ukey;
296723700da2STuong Lien 	bool rekeying = true, master_key = false;
2968f779bf79STuong Lien 	u8 *id, *own_id, mode;
296923700da2STuong Lien 	u32 intv = 0;
2970e1f32190STuong Lien 	int rc = 0;
2971e1f32190STuong Lien 
2972e1f32190STuong Lien 	if (!info->attrs[TIPC_NLA_NODE])
2973e1f32190STuong Lien 		return -EINVAL;
2974e1f32190STuong Lien 
2975e1f32190STuong Lien 	rc = nla_parse_nested(attrs, TIPC_NLA_NODE_MAX,
2976e1f32190STuong Lien 			      info->attrs[TIPC_NLA_NODE],
2977e1f32190STuong Lien 			      tipc_nl_node_policy, info->extack);
2978e1f32190STuong Lien 	if (rc)
2979f779bf79STuong Lien 		return rc;
2980e1f32190STuong Lien 
2981e1f32190STuong Lien 	own_id = tipc_own_id(net);
2982e1f32190STuong Lien 	if (!own_id) {
2983f779bf79STuong Lien 		GENL_SET_ERR_MSG(info, "not found own node identity (set id?)");
2984f779bf79STuong Lien 		return -EPERM;
2985e1f32190STuong Lien 	}
2986e1f32190STuong Lien 
298723700da2STuong Lien 	rc = tipc_nl_retrieve_rekeying(attrs, &intv);
298823700da2STuong Lien 	if (rc == -ENODATA)
298923700da2STuong Lien 		rekeying = false;
299023700da2STuong Lien 
2991e1f32190STuong Lien 	rc = tipc_nl_retrieve_key(attrs, &ukey);
299223700da2STuong Lien 	if (rc == -ENODATA && rekeying)
299323700da2STuong Lien 		goto rekeying;
299423700da2STuong Lien 	else if (rc)
2995f779bf79STuong Lien 		return rc;
2996e1f32190STuong Lien 
2997f779bf79STuong Lien 	rc = tipc_aead_key_validate(ukey, info);
2998e1f32190STuong Lien 	if (rc)
2999f779bf79STuong Lien 		return rc;
3000e1f32190STuong Lien 
3001e1f32190STuong Lien 	rc = tipc_nl_retrieve_nodeid(attrs, &id);
3002e1f32190STuong Lien 	switch (rc) {
3003e1f32190STuong Lien 	case -ENODATA:
3004f779bf79STuong Lien 		mode = CLUSTER_KEY;
3005daef1ee3STuong Lien 		master_key = !!(attrs[TIPC_NLA_NODE_KEY_MASTER]);
3006e1f32190STuong Lien 		break;
3007e1f32190STuong Lien 	case 0:
3008f779bf79STuong Lien 		mode = PER_NODE_KEY;
3009f779bf79STuong Lien 		if (memcmp(id, own_id, NODE_ID_LEN)) {
3010e1f32190STuong Lien 			n = tipc_node_find_by_id(net, id) ?:
3011e1f32190STuong Lien 				tipc_node_create(net, 0, id, 0xffffu, 0, true);
3012f779bf79STuong Lien 			if (unlikely(!n))
3013f779bf79STuong Lien 				return -ENOMEM;
3014e1f32190STuong Lien 			c = n->crypto_rx;
3015e1f32190STuong Lien 		}
3016e1f32190STuong Lien 		break;
3017e1f32190STuong Lien 	default:
3018f779bf79STuong Lien 		return rc;
3019e1f32190STuong Lien 	}
3020e1f32190STuong Lien 
3021f779bf79STuong Lien 	/* Initiate the TX/RX key */
3022daef1ee3STuong Lien 	rc = tipc_crypto_key_init(c, ukey, mode, master_key);
3023f779bf79STuong Lien 	if (n)
3024f779bf79STuong Lien 		tipc_node_put(n);
3025f779bf79STuong Lien 
3026daef1ee3STuong Lien 	if (unlikely(rc < 0)) {
3027f779bf79STuong Lien 		GENL_SET_ERR_MSG(info, "unable to initiate or attach new key");
3028f779bf79STuong Lien 		return rc;
30291ef6f7c9STuong Lien 	} else if (c == tx) {
30301ef6f7c9STuong Lien 		/* Distribute TX key but not master one */
30311ef6f7c9STuong Lien 		if (!master_key && tipc_crypto_key_distr(tx, rc, NULL))
30321ef6f7c9STuong Lien 			GENL_SET_ERR_MSG(info, "failed to replicate new key");
303323700da2STuong Lien rekeying:
303423700da2STuong Lien 		/* Schedule TX rekeying if needed */
303523700da2STuong Lien 		tipc_crypto_rekeying_sched(tx, rekeying, intv);
3036f779bf79STuong Lien 	}
3037f779bf79STuong Lien 
3038f779bf79STuong Lien 	return 0;
3039e1f32190STuong Lien }
3040e1f32190STuong Lien 
tipc_nl_node_set_key(struct sk_buff * skb,struct genl_info * info)3041e1f32190STuong Lien int tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
3042e1f32190STuong Lien {
3043e1f32190STuong Lien 	int err;
3044e1f32190STuong Lien 
3045e1f32190STuong Lien 	rtnl_lock();
3046e1f32190STuong Lien 	err = __tipc_nl_node_set_key(skb, info);
3047e1f32190STuong Lien 	rtnl_unlock();
3048e1f32190STuong Lien 
3049e1f32190STuong Lien 	return err;
3050e1f32190STuong Lien }
3051e1f32190STuong Lien 
__tipc_nl_node_flush_key(struct sk_buff * skb,struct genl_info * info)30522437fd7bSChen Wandun static int __tipc_nl_node_flush_key(struct sk_buff *skb,
30532437fd7bSChen Wandun 				    struct genl_info *info)
3054e1f32190STuong Lien {
3055e1f32190STuong Lien 	struct net *net = sock_net(skb->sk);
3056e1f32190STuong Lien 	struct tipc_net *tn = tipc_net(net);
3057e1f32190STuong Lien 	struct tipc_node *n;
3058e1f32190STuong Lien 
3059e1f32190STuong Lien 	tipc_crypto_key_flush(tn->crypto_tx);
3060e1f32190STuong Lien 	rcu_read_lock();
3061e1f32190STuong Lien 	list_for_each_entry_rcu(n, &tn->node_list, list)
3062e1f32190STuong Lien 		tipc_crypto_key_flush(n->crypto_rx);
3063e1f32190STuong Lien 	rcu_read_unlock();
3064e1f32190STuong Lien 
3065e1f32190STuong Lien 	return 0;
3066e1f32190STuong Lien }
3067e1f32190STuong Lien 
tipc_nl_node_flush_key(struct sk_buff * skb,struct genl_info * info)3068e1f32190STuong Lien int tipc_nl_node_flush_key(struct sk_buff *skb, struct genl_info *info)
3069e1f32190STuong Lien {
3070e1f32190STuong Lien 	int err;
3071e1f32190STuong Lien 
3072e1f32190STuong Lien 	rtnl_lock();
3073e1f32190STuong Lien 	err = __tipc_nl_node_flush_key(skb, info);
3074e1f32190STuong Lien 	rtnl_unlock();
3075e1f32190STuong Lien 
3076e1f32190STuong Lien 	return err;
3077e1f32190STuong Lien }
3078e1f32190STuong Lien #endif
3079e1f32190STuong Lien 
3080b4b9771bSTuong Lien /**
3081b4b9771bSTuong Lien  * tipc_node_dump - dump TIPC node data
3082b4b9771bSTuong Lien  * @n: tipc node to be dumped
3083b4b9771bSTuong Lien  * @more: dump more?
3084b4b9771bSTuong Lien  *        - false: dump only tipc node data
3085b4b9771bSTuong Lien  *        - true: dump node link data as well
3086b4b9771bSTuong Lien  * @buf: returned buffer of dump data in format
3087b4b9771bSTuong Lien  */
tipc_node_dump(struct tipc_node * n,bool more,char * buf)3088b4b9771bSTuong Lien int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
3089b4b9771bSTuong Lien {
3090b4b9771bSTuong Lien 	int i = 0;
3091b4b9771bSTuong Lien 	size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
3092b4b9771bSTuong Lien 
3093b4b9771bSTuong Lien 	if (!n) {
3094b4b9771bSTuong Lien 		i += scnprintf(buf, sz, "node data: (null)\n");
3095b4b9771bSTuong Lien 		return i;
3096b4b9771bSTuong Lien 	}
3097b4b9771bSTuong Lien 
3098b4b9771bSTuong Lien 	i += scnprintf(buf, sz, "node data: %x", n->addr);
3099b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %x", n->state);
3100b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
3101b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
3102b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
3103b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
3104b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
3105b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
3106b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %u", n->working_links);
3107b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
3108b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
3109b4b9771bSTuong Lien 
3110b4b9771bSTuong Lien 	if (!more)
3111b4b9771bSTuong Lien 		return i;
3112b4b9771bSTuong Lien 
3113b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
3114b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
3115b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " media: ");
3116b4b9771bSTuong Lien 	i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
3117b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, "\n");
3118b4b9771bSTuong Lien 	i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
3119b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " inputq: ");
3120b4b9771bSTuong Lien 	i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
3121b4b9771bSTuong Lien 
3122b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
3123b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
3124b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " media: ");
3125b4b9771bSTuong Lien 	i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
3126b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, "\n");
3127b4b9771bSTuong Lien 	i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
3128b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, " inputq: ");
3129b4b9771bSTuong Lien 	i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
3130b4b9771bSTuong Lien 
3131b4b9771bSTuong Lien 	i += scnprintf(buf + i, sz - i, "bclink:\n ");
3132b4b9771bSTuong Lien 	i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
3133b4b9771bSTuong Lien 
3134b4b9771bSTuong Lien 	return i;
3135b4b9771bSTuong Lien }
3136f73b1281SHoang Le 
tipc_node_pre_cleanup_net(struct net * exit_net)3137f73b1281SHoang Le void tipc_node_pre_cleanup_net(struct net *exit_net)
3138f73b1281SHoang Le {
3139f73b1281SHoang Le 	struct tipc_node *n;
3140f73b1281SHoang Le 	struct tipc_net *tn;
3141f73b1281SHoang Le 	struct net *tmp;
3142f73b1281SHoang Le 
3143f73b1281SHoang Le 	rcu_read_lock();
3144f73b1281SHoang Le 	for_each_net_rcu(tmp) {
3145f73b1281SHoang Le 		if (tmp == exit_net)
3146f73b1281SHoang Le 			continue;
3147f73b1281SHoang Le 		tn = tipc_net(tmp);
3148f73b1281SHoang Le 		if (!tn)
3149f73b1281SHoang Le 			continue;
3150f73b1281SHoang Le 		spin_lock_bh(&tn->node_list_lock);
3151f73b1281SHoang Le 		list_for_each_entry_rcu(n, &tn->node_list, list) {
3152f73b1281SHoang Le 			if (!n->peer_net)
3153f73b1281SHoang Le 				continue;
3154f73b1281SHoang Le 			if (n->peer_net != exit_net)
3155f73b1281SHoang Le 				continue;
3156f73b1281SHoang Le 			tipc_node_write_lock(n);
3157f73b1281SHoang Le 			n->peer_net = NULL;
3158f73b1281SHoang Le 			n->peer_hash_mix = 0;
3159f73b1281SHoang Le 			tipc_node_write_unlock_fast(n);
3160f73b1281SHoang Le 			break;
3161f73b1281SHoang Le 		}
3162f73b1281SHoang Le 		spin_unlock_bh(&tn->node_list_lock);
3163f73b1281SHoang Le 	}
3164f73b1281SHoang Le 	rcu_read_unlock();
3165f73b1281SHoang Le }
3166