1/*
2 * net/tipc/node.c: TIPC node management routines
3 *
4 * Copyright (c) 2000-2006, 2012-2014, Ericsson AB
5 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 *    contributors may be used to endorse or promote products derived from
18 *    this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
39#include "node.h"
40#include "name_distr.h"
41#include "socket.h"
42
43#define NODE_HTABLE_SIZE 512
44
45static void node_lost_contact(struct tipc_node *n_ptr);
46static void node_established_contact(struct tipc_node *n_ptr);
47
48static struct hlist_head node_htable[NODE_HTABLE_SIZE];
49LIST_HEAD(tipc_node_list);
50static u32 tipc_num_nodes;
51static u32 tipc_num_links;
52static DEFINE_SPINLOCK(node_list_lock);
53
54struct tipc_sock_conn {
55	u32 port;
56	u32 peer_port;
57	u32 peer_node;
58	struct list_head list;
59};
60
61/*
62 * A trivial power-of-two bitmask technique is used for speed, since this
63 * operation is done for every incoming TIPC packet. The number of hash table
64 * entries has been chosen so that no hash chain exceeds 8 nodes and will
65 * usually be much smaller (typically only a single node).
66 */
67static unsigned int tipc_hashfn(u32 addr)
68{
69	return addr & (NODE_HTABLE_SIZE - 1);
70}
71
72/*
73 * tipc_node_find - locate specified node object, if it exists
74 */
75struct tipc_node *tipc_node_find(u32 addr)
76{
77	struct tipc_node *node;
78
79	if (unlikely(!in_own_cluster_exact(addr)))
80		return NULL;
81
82	rcu_read_lock();
83	hlist_for_each_entry_rcu(node, &node_htable[tipc_hashfn(addr)], hash) {
84		if (node->addr == addr) {
85			rcu_read_unlock();
86			return node;
87		}
88	}
89	rcu_read_unlock();
90	return NULL;
91}
92
93struct tipc_node *tipc_node_create(u32 addr)
94{
95	struct tipc_node *n_ptr, *temp_node;
96
97	spin_lock_bh(&node_list_lock);
98
99	n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
100	if (!n_ptr) {
101		spin_unlock_bh(&node_list_lock);
102		pr_warn("Node creation failed, no memory\n");
103		return NULL;
104	}
105
106	n_ptr->addr = addr;
107	spin_lock_init(&n_ptr->lock);
108	INIT_HLIST_NODE(&n_ptr->hash);
109	INIT_LIST_HEAD(&n_ptr->list);
110	INIT_LIST_HEAD(&n_ptr->nsub);
111	INIT_LIST_HEAD(&n_ptr->conn_sks);
112	__skb_queue_head_init(&n_ptr->waiting_sks);
113
114	hlist_add_head_rcu(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
115
116	list_for_each_entry_rcu(temp_node, &tipc_node_list, list) {
117		if (n_ptr->addr < temp_node->addr)
118			break;
119	}
120	list_add_tail_rcu(&n_ptr->list, &temp_node->list);
121	n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN;
122	n_ptr->signature = INVALID_NODE_SIG;
123
124	tipc_num_nodes++;
125
126	spin_unlock_bh(&node_list_lock);
127	return n_ptr;
128}
129
130static void tipc_node_delete(struct tipc_node *n_ptr)
131{
132	list_del_rcu(&n_ptr->list);
133	hlist_del_rcu(&n_ptr->hash);
134	kfree_rcu(n_ptr, rcu);
135
136	tipc_num_nodes--;
137}
138
139void tipc_node_stop(void)
140{
141	struct tipc_node *node, *t_node;
142
143	spin_lock_bh(&node_list_lock);
144	list_for_each_entry_safe(node, t_node, &tipc_node_list, list)
145		tipc_node_delete(node);
146	spin_unlock_bh(&node_list_lock);
147}
148
149int tipc_node_add_conn(u32 dnode, u32 port, u32 peer_port)
150{
151	struct tipc_node *node;
152	struct tipc_sock_conn *conn;
153
154	if (in_own_node(dnode))
155		return 0;
156
157	node = tipc_node_find(dnode);
158	if (!node) {
159		pr_warn("Connecting sock to node 0x%x failed\n", dnode);
160		return -EHOSTUNREACH;
161	}
162	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
163	if (!conn)
164		return -EHOSTUNREACH;
165	conn->peer_node = dnode;
166	conn->port = port;
167	conn->peer_port = peer_port;
168
169	tipc_node_lock(node);
170	list_add_tail(&conn->list, &node->conn_sks);
171	tipc_node_unlock(node);
172	return 0;
173}
174
175void tipc_node_remove_conn(u32 dnode, u32 port)
176{
177	struct tipc_node *node;
178	struct tipc_sock_conn *conn, *safe;
179
180	if (in_own_node(dnode))
181		return;
182
183	node = tipc_node_find(dnode);
184	if (!node)
185		return;
186
187	tipc_node_lock(node);
188	list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
189		if (port != conn->port)
190			continue;
191		list_del(&conn->list);
192		kfree(conn);
193	}
194	tipc_node_unlock(node);
195}
196
197void tipc_node_abort_sock_conns(struct list_head *conns)
198{
199	struct tipc_sock_conn *conn, *safe;
200	struct sk_buff *buf;
201
202	list_for_each_entry_safe(conn, safe, conns, list) {
203		buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
204				      SHORT_H_SIZE, 0, tipc_own_addr,
205				      conn->peer_node, conn->port,
206				      conn->peer_port, TIPC_ERR_NO_NODE);
207		if (likely(buf))
208			tipc_sk_rcv(buf);
209		list_del(&conn->list);
210		kfree(conn);
211	}
212}
213
214/**
215 * tipc_node_link_up - handle addition of link
216 *
217 * Link becomes active (alone or shared) or standby, depending on its priority.
218 */
219void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
220{
221	struct tipc_link **active = &n_ptr->active_links[0];
222
223	n_ptr->working_links++;
224	n_ptr->action_flags |= TIPC_NOTIFY_LINK_UP;
225	n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id;
226
227	pr_info("Established link <%s> on network plane %c\n",
228		l_ptr->name, l_ptr->net_plane);
229
230	if (!active[0]) {
231		active[0] = active[1] = l_ptr;
232		node_established_contact(n_ptr);
233		goto exit;
234	}
235	if (l_ptr->priority < active[0]->priority) {
236		pr_info("New link <%s> becomes standby\n", l_ptr->name);
237		goto exit;
238	}
239	tipc_link_dup_queue_xmit(active[0], l_ptr);
240	if (l_ptr->priority == active[0]->priority) {
241		active[0] = l_ptr;
242		goto exit;
243	}
244	pr_info("Old link <%s> becomes standby\n", active[0]->name);
245	if (active[1] != active[0])
246		pr_info("Old link <%s> becomes standby\n", active[1]->name);
247	active[0] = active[1] = l_ptr;
248exit:
249	/* Leave room for changeover header when returning 'mtu' to users: */
250	n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
251	n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
252}
253
254/**
255 * node_select_active_links - select active link
256 */
257static void node_select_active_links(struct tipc_node *n_ptr)
258{
259	struct tipc_link **active = &n_ptr->active_links[0];
260	u32 i;
261	u32 highest_prio = 0;
262
263	active[0] = active[1] = NULL;
264
265	for (i = 0; i < MAX_BEARERS; i++) {
266		struct tipc_link *l_ptr = n_ptr->links[i];
267
268		if (!l_ptr || !tipc_link_is_up(l_ptr) ||
269		    (l_ptr->priority < highest_prio))
270			continue;
271
272		if (l_ptr->priority > highest_prio) {
273			highest_prio = l_ptr->priority;
274			active[0] = active[1] = l_ptr;
275		} else {
276			active[1] = l_ptr;
277		}
278	}
279}
280
281/**
282 * tipc_node_link_down - handle loss of link
283 */
284void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
285{
286	struct tipc_link **active;
287
288	n_ptr->working_links--;
289	n_ptr->action_flags |= TIPC_NOTIFY_LINK_DOWN;
290	n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id;
291
292	if (!tipc_link_is_active(l_ptr)) {
293		pr_info("Lost standby link <%s> on network plane %c\n",
294			l_ptr->name, l_ptr->net_plane);
295		return;
296	}
297	pr_info("Lost link <%s> on network plane %c\n",
298		l_ptr->name, l_ptr->net_plane);
299
300	active = &n_ptr->active_links[0];
301	if (active[0] == l_ptr)
302		active[0] = active[1];
303	if (active[1] == l_ptr)
304		active[1] = active[0];
305	if (active[0] == l_ptr)
306		node_select_active_links(n_ptr);
307	if (tipc_node_is_up(n_ptr))
308		tipc_link_failover_send_queue(l_ptr);
309	else
310		node_lost_contact(n_ptr);
311
312	/* Leave room for changeover header when returning 'mtu' to users: */
313	if (active[0]) {
314		n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
315		n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
316		return;
317	}
318
319	/* Loopback link went down? No fragmentation needed from now on. */
320	if (n_ptr->addr == tipc_own_addr) {
321		n_ptr->act_mtus[0] = MAX_MSG_SIZE;
322		n_ptr->act_mtus[1] = MAX_MSG_SIZE;
323	}
324}
325
326int tipc_node_active_links(struct tipc_node *n_ptr)
327{
328	return n_ptr->active_links[0] != NULL;
329}
330
331int tipc_node_is_up(struct tipc_node *n_ptr)
332{
333	return tipc_node_active_links(n_ptr);
334}
335
336void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
337{
338	n_ptr->links[l_ptr->bearer_id] = l_ptr;
339	spin_lock_bh(&node_list_lock);
340	tipc_num_links++;
341	spin_unlock_bh(&node_list_lock);
342	n_ptr->link_cnt++;
343}
344
345void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
346{
347	int i;
348
349	for (i = 0; i < MAX_BEARERS; i++) {
350		if (l_ptr != n_ptr->links[i])
351			continue;
352		n_ptr->links[i] = NULL;
353		spin_lock_bh(&node_list_lock);
354		tipc_num_links--;
355		spin_unlock_bh(&node_list_lock);
356		n_ptr->link_cnt--;
357	}
358}
359
360static void node_established_contact(struct tipc_node *n_ptr)
361{
362	n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
363	n_ptr->bclink.oos_state = 0;
364	n_ptr->bclink.acked = tipc_bclink_get_last_sent();
365	tipc_bclink_add_node(n_ptr->addr);
366}
367
368static void node_lost_contact(struct tipc_node *n_ptr)
369{
370	char addr_string[16];
371	u32 i;
372
373	pr_info("Lost contact with %s\n",
374		tipc_addr_string_fill(addr_string, n_ptr->addr));
375
376	/* Flush broadcast link info associated with lost node */
377	if (n_ptr->bclink.recv_permitted) {
378		kfree_skb_list(n_ptr->bclink.deferred_head);
379		n_ptr->bclink.deferred_size = 0;
380
381		if (n_ptr->bclink.reasm_buf) {
382			kfree_skb(n_ptr->bclink.reasm_buf);
383			n_ptr->bclink.reasm_buf = NULL;
384		}
385
386		tipc_bclink_remove_node(n_ptr->addr);
387		tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
388
389		n_ptr->bclink.recv_permitted = false;
390	}
391
392	/* Abort link changeover */
393	for (i = 0; i < MAX_BEARERS; i++) {
394		struct tipc_link *l_ptr = n_ptr->links[i];
395		if (!l_ptr)
396			continue;
397		l_ptr->reset_checkpoint = l_ptr->next_in_no;
398		l_ptr->exp_msg_count = 0;
399		tipc_link_reset_fragments(l_ptr);
400	}
401
402	n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN;
403
404	/* Notify subscribers and prevent re-contact with node until
405	 * cleanup is done.
406	 */
407	n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN |
408			       TIPC_NOTIFY_NODE_DOWN;
409}
410
411struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
412{
413	u32 domain;
414	struct sk_buff *buf;
415	struct tipc_node *n_ptr;
416	struct tipc_node_info node_info;
417	u32 payload_size;
418
419	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
420		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
421
422	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
423	if (!tipc_addr_domain_valid(domain))
424		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
425						   " (network address)");
426
427	spin_lock_bh(&node_list_lock);
428	if (!tipc_num_nodes) {
429		spin_unlock_bh(&node_list_lock);
430		return tipc_cfg_reply_none();
431	}
432
433	/* For now, get space for all other nodes */
434	payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
435	if (payload_size > 32768u) {
436		spin_unlock_bh(&node_list_lock);
437		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
438						   " (too many nodes)");
439	}
440	spin_unlock_bh(&node_list_lock);
441
442	buf = tipc_cfg_reply_alloc(payload_size);
443	if (!buf)
444		return NULL;
445
446	/* Add TLVs for all nodes in scope */
447	rcu_read_lock();
448	list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
449		if (!tipc_in_scope(domain, n_ptr->addr))
450			continue;
451		node_info.addr = htonl(n_ptr->addr);
452		node_info.up = htonl(tipc_node_is_up(n_ptr));
453		tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
454				    &node_info, sizeof(node_info));
455	}
456	rcu_read_unlock();
457	return buf;
458}
459
460struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
461{
462	u32 domain;
463	struct sk_buff *buf;
464	struct tipc_node *n_ptr;
465	struct tipc_link_info link_info;
466	u32 payload_size;
467
468	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
469		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
470
471	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
472	if (!tipc_addr_domain_valid(domain))
473		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
474						   " (network address)");
475
476	if (!tipc_own_addr)
477		return tipc_cfg_reply_none();
478
479	spin_lock_bh(&node_list_lock);
480	/* Get space for all unicast links + broadcast link */
481	payload_size = TLV_SPACE((sizeof(link_info)) * (tipc_num_links + 1));
482	if (payload_size > 32768u) {
483		spin_unlock_bh(&node_list_lock);
484		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
485						   " (too many links)");
486	}
487	spin_unlock_bh(&node_list_lock);
488
489	buf = tipc_cfg_reply_alloc(payload_size);
490	if (!buf)
491		return NULL;
492
493	/* Add TLV for broadcast link */
494	link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
495	link_info.up = htonl(1);
496	strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
497	tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
498
499	/* Add TLVs for any other links in scope */
500	rcu_read_lock();
501	list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
502		u32 i;
503
504		if (!tipc_in_scope(domain, n_ptr->addr))
505			continue;
506		tipc_node_lock(n_ptr);
507		for (i = 0; i < MAX_BEARERS; i++) {
508			if (!n_ptr->links[i])
509				continue;
510			link_info.dest = htonl(n_ptr->addr);
511			link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
512			strcpy(link_info.str, n_ptr->links[i]->name);
513			tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
514					    &link_info, sizeof(link_info));
515		}
516		tipc_node_unlock(n_ptr);
517	}
518	rcu_read_unlock();
519	return buf;
520}
521
522/**
523 * tipc_node_get_linkname - get the name of a link
524 *
525 * @bearer_id: id of the bearer
526 * @node: peer node address
527 * @linkname: link name output buffer
528 *
529 * Returns 0 on success
530 */
531int tipc_node_get_linkname(u32 bearer_id, u32 addr, char *linkname, size_t len)
532{
533	struct tipc_link *link;
534	struct tipc_node *node = tipc_node_find(addr);
535
536	if ((bearer_id >= MAX_BEARERS) || !node)
537		return -EINVAL;
538	tipc_node_lock(node);
539	link = node->links[bearer_id];
540	if (link) {
541		strncpy(linkname, link->name, len);
542		tipc_node_unlock(node);
543		return 0;
544	}
545	tipc_node_unlock(node);
546	return -EINVAL;
547}
548
549void tipc_node_unlock(struct tipc_node *node)
550{
551	LIST_HEAD(nsub_list);
552	LIST_HEAD(conn_sks);
553	struct sk_buff_head waiting_sks;
554	u32 addr = 0;
555	int flags = node->action_flags;
556	u32 link_id = 0;
557
558	if (likely(!flags)) {
559		spin_unlock_bh(&node->lock);
560		return;
561	}
562
563	addr = node->addr;
564	link_id = node->link_id;
565	__skb_queue_head_init(&waiting_sks);
566
567	if (flags & TIPC_WAKEUP_USERS)
568		skb_queue_splice_init(&node->waiting_sks, &waiting_sks);
569
570	if (flags & TIPC_NOTIFY_NODE_DOWN) {
571		list_replace_init(&node->nsub, &nsub_list);
572		list_replace_init(&node->conn_sks, &conn_sks);
573	}
574	node->action_flags &= ~(TIPC_WAKEUP_USERS | TIPC_NOTIFY_NODE_DOWN |
575				TIPC_NOTIFY_NODE_UP | TIPC_NOTIFY_LINK_UP |
576				TIPC_NOTIFY_LINK_DOWN |
577				TIPC_WAKEUP_BCAST_USERS);
578
579	spin_unlock_bh(&node->lock);
580
581	while (!skb_queue_empty(&waiting_sks))
582		tipc_sk_rcv(__skb_dequeue(&waiting_sks));
583
584	if (!list_empty(&conn_sks))
585		tipc_node_abort_sock_conns(&conn_sks);
586
587	if (!list_empty(&nsub_list))
588		tipc_nodesub_notify(&nsub_list);
589
590	if (flags & TIPC_WAKEUP_BCAST_USERS)
591		tipc_bclink_wakeup_users();
592
593	if (flags & TIPC_NOTIFY_NODE_UP)
594		tipc_named_node_up(addr);
595
596	if (flags & TIPC_NOTIFY_LINK_UP)
597		tipc_nametbl_publish(TIPC_LINK_STATE, addr, addr,
598				     TIPC_NODE_SCOPE, link_id, addr);
599
600	if (flags & TIPC_NOTIFY_LINK_DOWN)
601		tipc_nametbl_withdraw(TIPC_LINK_STATE, addr,
602				      link_id, addr);
603}
604