xref: /openbmc/linux/net/tipc/core.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/core.c: TIPC module code
3b97bf3fdSPer Liden  *
4c61dd61dSYing Xue  * Copyright (c) 2003-2006, 2013, Ericsson AB
513a2e898SYing Xue  * Copyright (c) 2005-2006, 2010-2013, 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"
38b97bf3fdSPer Liden #include "name_table.h"
39b97bf3fdSPer Liden #include "subscr.h"
4022ae7cffSRichard Alpe #include "bearer.h"
4122ae7cffSRichard Alpe #include "net.h"
422e84c60bSJon Paul Maloy #include "socket.h"
435fd9fd63SJon Paul Maloy #include "bcast.h"
44ff2ebbfbSHoang Le #include "node.h"
45fc1b6d6dSTuong Lien #include "crypto.h"
46b97bf3fdSPer Liden 
472cf8aa19SErik Hugne #include <linux/module.h>
48b97bf3fdSPer Liden 
49b97bf3fdSPer Liden /* configurable TIPC parameters */
50c7d03a00SAlexey Dobriyan unsigned int tipc_net_id __read_mostly;
51cc79dd1bSYing Xue int sysctl_tipc_rmem[3] __read_mostly;	/* min/default/max */
52b97bf3fdSPer Liden 
tipc_init_net(struct net * net)53c93d3baaSYing Xue static int __net_init tipc_init_net(struct net *net)
54c93d3baaSYing Xue {
55c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
56e05b31f4SYing Xue 	int err;
57c93d3baaSYing Xue 
58c93d3baaSYing Xue 	tn->net_id = 4711;
59d50ccc2dSJon Maloy 	tn->node_addr = 0;
6025b0b9c4SJon Maloy 	tn->trial_addr = 0;
6125b0b9c4SJon Maloy 	tn->addr_trial_end = 0;
62ff2ebbfbSHoang Le 	tn->capabilities = TIPC_NODE_CAPABILITIES;
63be07f056SXin Long 	INIT_WORK(&tn->work, tipc_net_finalize_work);
64d50ccc2dSJon Maloy 	memset(tn->node_id, 0, sizeof(tn->node_id));
65d50ccc2dSJon Maloy 	memset(tn->node_id_string, 0, sizeof(tn->node_id_string));
6635c55c98SJon Paul Maloy 	tn->mon_threshold = TIPC_DEF_MON_THRESHOLD;
67bafa29e3SYing Xue 	get_random_bytes(&tn->random, sizeof(int));
68f2f9800dSYing Xue 	INIT_LIST_HEAD(&tn->node_list);
69f2f9800dSYing Xue 	spin_lock_init(&tn->node_list_lock);
70c93d3baaSYing Xue 
71fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
72fc1b6d6dSTuong Lien 	err = tipc_crypto_start(&tn->crypto_tx, net, NULL);
73fc1b6d6dSTuong Lien 	if (err)
74fc1b6d6dSTuong Lien 		goto out_crypto;
75fc1b6d6dSTuong Lien #endif
76e05b31f4SYing Xue 	err = tipc_sk_rht_init(net);
774ac1c8d0SYing Xue 	if (err)
784ac1c8d0SYing Xue 		goto out_sk_rht;
794ac1c8d0SYing Xue 
804ac1c8d0SYing Xue 	err = tipc_nametbl_init(net);
814ac1c8d0SYing Xue 	if (err)
824ac1c8d0SYing Xue 		goto out_nametbl;
83a62fbcceSYing Xue 
845fd9fd63SJon Paul Maloy 	err = tipc_bcast_init(net);
855fd9fd63SJon Paul Maloy 	if (err)
865fd9fd63SJon Paul Maloy 		goto out_bclink;
875fd9fd63SJon Paul Maloy 
886c9081a3SJohn Rutherford 	err = tipc_attach_loopback(net);
896c9081a3SJohn Rutherford 	if (err)
906c9081a3SJohn Rutherford 		goto out_bclink;
916c9081a3SJohn Rutherford 
924ac1c8d0SYing Xue 	return 0;
934ac1c8d0SYing Xue 
945fd9fd63SJon Paul Maloy out_bclink:
95a62fbcceSYing Xue 	tipc_nametbl_stop(net);
964ac1c8d0SYing Xue out_nametbl:
974ac1c8d0SYing Xue 	tipc_sk_rht_destroy(net);
984ac1c8d0SYing Xue out_sk_rht:
99fc1b6d6dSTuong Lien 
100fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
101fc1b6d6dSTuong Lien 	tipc_crypto_stop(&tn->crypto_tx);
102fc1b6d6dSTuong Lien out_crypto:
103fc1b6d6dSTuong Lien #endif
104e05b31f4SYing Xue 	return err;
105c93d3baaSYing Xue }
106c93d3baaSYing Xue 
tipc_exit_net(struct net * net)107c93d3baaSYing Xue static void __net_exit tipc_exit_net(struct net *net)
108c93d3baaSYing Xue {
109d966ddccSHoang Huu Le 	struct tipc_net *tn = tipc_net(net);
110d966ddccSHoang Huu Le 
1116c9081a3SJohn Rutherford 	tipc_detach_loopback(net);
112*911600bfSHoang Le 	tipc_net_stop(net);
113d966ddccSHoang Huu Le 	/* Make sure the tipc_net_finalize_work() finished */
114be07f056SXin Long 	cancel_work_sync(&tn->work);
1155fd9fd63SJon Paul Maloy 	tipc_bcast_stop(net);
1164ac1c8d0SYing Xue 	tipc_nametbl_stop(net);
117e05b31f4SYing Xue 	tipc_sk_rht_destroy(net);
118fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
119fc1b6d6dSTuong Lien 	tipc_crypto_stop(&tipc_net(net)->crypto_tx);
120fc1b6d6dSTuong Lien #endif
12104c26faaSXin Long 	while (atomic_read(&tn->wq_count))
12204c26faaSXin Long 		cond_resched();
123c93d3baaSYing Xue }
124c93d3baaSYing Xue 
tipc_pernet_pre_exit(struct net * net)125f73b1281SHoang Le static void __net_exit tipc_pernet_pre_exit(struct net *net)
126f73b1281SHoang Le {
127f73b1281SHoang Le 	tipc_node_pre_cleanup_net(net);
128f73b1281SHoang Le }
129f73b1281SHoang Le 
130f73b1281SHoang Le static struct pernet_operations tipc_pernet_pre_exit_ops = {
131f73b1281SHoang Le 	.pre_exit = tipc_pernet_pre_exit,
132f73b1281SHoang Le };
133f73b1281SHoang Le 
134c93d3baaSYing Xue static struct pernet_operations tipc_net_ops = {
135c93d3baaSYing Xue 	.init = tipc_init_net,
136c93d3baaSYing Xue 	.exit = tipc_exit_net,
137c93d3baaSYing Xue 	.id   = &tipc_net_id,
138c93d3baaSYing Xue 	.size = sizeof(struct tipc_net),
139c93d3baaSYing Xue };
140c93d3baaSYing Xue 
141526f5b85SJunwei Hu static struct pernet_operations tipc_topsrv_net_ops = {
142526f5b85SJunwei Hu 	.init = tipc_topsrv_init_net,
143526f5b85SJunwei Hu 	.exit = tipc_topsrv_exit_net,
144526f5b85SJunwei Hu };
145526f5b85SJunwei Hu 
tipc_init(void)1466b8326edSYing Xue static int __init tipc_init(void)
147b97bf3fdSPer Liden {
1489fe7ed47SYing Xue 	int err;
149b97bf3fdSPer Liden 
1506b8326edSYing Xue 	pr_info("Activated (version " TIPC_MOD_VER ")\n");
1516b8326edSYing Xue 
15210724cc7SJon Paul Maloy 	sysctl_tipc_rmem[0] = RCVBUF_MIN;
15310724cc7SJon Paul Maloy 	sysctl_tipc_rmem[1] = RCVBUF_DEF;
15410724cc7SJon Paul Maloy 	sysctl_tipc_rmem[2] = RCVBUF_MAX;
1556b8326edSYing Xue 
1569fe7ed47SYing Xue 	err = tipc_register_sysctl();
1579fe7ed47SYing Xue 	if (err)
1589fe7ed47SYing Xue 		goto out_sysctl;
1599fe7ed47SYing Xue 
160c492d4c7SXin Long 	err = register_pernet_device(&tipc_net_ops);
1619fe7ed47SYing Xue 	if (err)
162a62fbcceSYing Xue 		goto out_pernet;
1639fe7ed47SYing Xue 
1645593530eSDavid S. Miller 	err = tipc_socket_init();
1655593530eSDavid S. Miller 	if (err)
1665593530eSDavid S. Miller 		goto out_socket;
1675593530eSDavid S. Miller 
168c492d4c7SXin Long 	err = register_pernet_device(&tipc_topsrv_net_ops);
169526f5b85SJunwei Hu 	if (err)
170526f5b85SJunwei Hu 		goto out_pernet_topsrv;
171526f5b85SJunwei Hu 
172f73b1281SHoang Le 	err = register_pernet_subsys(&tipc_pernet_pre_exit_ops);
173f73b1281SHoang Le 	if (err)
174f73b1281SHoang Le 		goto out_register_pernet_subsys;
175f73b1281SHoang Le 
176970122fdSYing Xue 	err = tipc_bearer_setup();
177970122fdSYing Xue 	if (err)
178970122fdSYing Xue 		goto out_bearer;
179970122fdSYing Xue 
1809cf1cd8eSTaehee Yoo 	err = tipc_netlink_start();
1819cf1cd8eSTaehee Yoo 	if (err)
1829cf1cd8eSTaehee Yoo 		goto out_netlink;
1839cf1cd8eSTaehee Yoo 
1849cf1cd8eSTaehee Yoo 	err = tipc_netlink_compat_start();
1859cf1cd8eSTaehee Yoo 	if (err)
1869cf1cd8eSTaehee Yoo 		goto out_netlink_compat;
1879cf1cd8eSTaehee Yoo 
1886b8326edSYing Xue 	pr_info("Started in single node mode\n");
1899fe7ed47SYing Xue 	return 0;
1909cf1cd8eSTaehee Yoo 
1919cf1cd8eSTaehee Yoo out_netlink_compat:
1929cf1cd8eSTaehee Yoo 	tipc_netlink_stop();
1939cf1cd8eSTaehee Yoo out_netlink:
1949cf1cd8eSTaehee Yoo 	tipc_bearer_cleanup();
195970122fdSYing Xue out_bearer:
196f73b1281SHoang Le 	unregister_pernet_subsys(&tipc_pernet_pre_exit_ops);
197f73b1281SHoang Le out_register_pernet_subsys:
198c492d4c7SXin Long 	unregister_pernet_device(&tipc_topsrv_net_ops);
199526f5b85SJunwei Hu out_pernet_topsrv:
2005593530eSDavid S. Miller 	tipc_socket_stop();
2015593530eSDavid S. Miller out_socket:
202c492d4c7SXin Long 	unregister_pernet_device(&tipc_net_ops);
203a62fbcceSYing Xue out_pernet:
2049fe7ed47SYing Xue 	tipc_unregister_sysctl();
2059fe7ed47SYing Xue out_sysctl:
2062cf8aa19SErik Hugne 	pr_err("Unable to start in single node mode\n");
2076b8326edSYing Xue 	return err;
208b97bf3fdSPer Liden }
209b97bf3fdSPer Liden 
tipc_exit(void)210b97bf3fdSPer Liden static void __exit tipc_exit(void)
211b97bf3fdSPer Liden {
2129cf1cd8eSTaehee Yoo 	tipc_netlink_compat_stop();
2139cf1cd8eSTaehee Yoo 	tipc_netlink_stop();
2146b8326edSYing Xue 	tipc_bearer_cleanup();
215f73b1281SHoang Le 	unregister_pernet_subsys(&tipc_pernet_pre_exit_ops);
216c492d4c7SXin Long 	unregister_pernet_device(&tipc_topsrv_net_ops);
2175593530eSDavid S. Miller 	tipc_socket_stop();
218c492d4c7SXin Long 	unregister_pernet_device(&tipc_net_ops);
2196b8326edSYing Xue 	tipc_unregister_sysctl();
2206b8326edSYing Xue 
2212cf8aa19SErik Hugne 	pr_info("Deactivated\n");
222b97bf3fdSPer Liden }
223b97bf3fdSPer Liden 
224b97bf3fdSPer Liden module_init(tipc_init);
225b97bf3fdSPer Liden module_exit(tipc_exit);
226b97bf3fdSPer Liden 
227b97bf3fdSPer Liden MODULE_DESCRIPTION("TIPC: Transparent Inter Process Communication");
228b97bf3fdSPer Liden MODULE_LICENSE("Dual BSD/GPL");
229a592ea63SAllan Stephens MODULE_VERSION(TIPC_MOD_VER);
230