162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/core.c: TIPC module code
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2003-2006, 2013, Ericsson AB
562306a36Sopenharmony_ci * Copyright (c) 2005-2006, 2010-2013, Wind River Systems
662306a36Sopenharmony_ci * All rights reserved.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
962306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1262306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1362306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1462306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1562306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
1662306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
1762306a36Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
1862306a36Sopenharmony_ci *    this software without specific prior written permission.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2162306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2262306a36Sopenharmony_ci * Software Foundation.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2562306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2662306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2762306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2862306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2962306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3062306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3162306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3262306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3362306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3462306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include "core.h"
3862306a36Sopenharmony_ci#include "name_table.h"
3962306a36Sopenharmony_ci#include "subscr.h"
4062306a36Sopenharmony_ci#include "bearer.h"
4162306a36Sopenharmony_ci#include "net.h"
4262306a36Sopenharmony_ci#include "socket.h"
4362306a36Sopenharmony_ci#include "bcast.h"
4462306a36Sopenharmony_ci#include "node.h"
4562306a36Sopenharmony_ci#include "crypto.h"
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#include <linux/module.h>
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* configurable TIPC parameters */
5062306a36Sopenharmony_ciunsigned int tipc_net_id __read_mostly;
5162306a36Sopenharmony_ciint sysctl_tipc_rmem[3] __read_mostly;	/* min/default/max */
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int __net_init tipc_init_net(struct net *net)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	struct tipc_net *tn = net_generic(net, tipc_net_id);
5662306a36Sopenharmony_ci	int err;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	tn->net_id = 4711;
5962306a36Sopenharmony_ci	tn->node_addr = 0;
6062306a36Sopenharmony_ci	tn->trial_addr = 0;
6162306a36Sopenharmony_ci	tn->addr_trial_end = 0;
6262306a36Sopenharmony_ci	tn->capabilities = TIPC_NODE_CAPABILITIES;
6362306a36Sopenharmony_ci	INIT_WORK(&tn->work, tipc_net_finalize_work);
6462306a36Sopenharmony_ci	memset(tn->node_id, 0, sizeof(tn->node_id));
6562306a36Sopenharmony_ci	memset(tn->node_id_string, 0, sizeof(tn->node_id_string));
6662306a36Sopenharmony_ci	tn->mon_threshold = TIPC_DEF_MON_THRESHOLD;
6762306a36Sopenharmony_ci	get_random_bytes(&tn->random, sizeof(int));
6862306a36Sopenharmony_ci	INIT_LIST_HEAD(&tn->node_list);
6962306a36Sopenharmony_ci	spin_lock_init(&tn->node_list_lock);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
7262306a36Sopenharmony_ci	err = tipc_crypto_start(&tn->crypto_tx, net, NULL);
7362306a36Sopenharmony_ci	if (err)
7462306a36Sopenharmony_ci		goto out_crypto;
7562306a36Sopenharmony_ci#endif
7662306a36Sopenharmony_ci	err = tipc_sk_rht_init(net);
7762306a36Sopenharmony_ci	if (err)
7862306a36Sopenharmony_ci		goto out_sk_rht;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	err = tipc_nametbl_init(net);
8162306a36Sopenharmony_ci	if (err)
8262306a36Sopenharmony_ci		goto out_nametbl;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	err = tipc_bcast_init(net);
8562306a36Sopenharmony_ci	if (err)
8662306a36Sopenharmony_ci		goto out_bclink;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	err = tipc_attach_loopback(net);
8962306a36Sopenharmony_ci	if (err)
9062306a36Sopenharmony_ci		goto out_bclink;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return 0;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciout_bclink:
9562306a36Sopenharmony_ci	tipc_nametbl_stop(net);
9662306a36Sopenharmony_ciout_nametbl:
9762306a36Sopenharmony_ci	tipc_sk_rht_destroy(net);
9862306a36Sopenharmony_ciout_sk_rht:
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
10162306a36Sopenharmony_ci	tipc_crypto_stop(&tn->crypto_tx);
10262306a36Sopenharmony_ciout_crypto:
10362306a36Sopenharmony_ci#endif
10462306a36Sopenharmony_ci	return err;
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic void __net_exit tipc_exit_net(struct net *net)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	struct tipc_net *tn = tipc_net(net);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	tipc_detach_loopback(net);
11262306a36Sopenharmony_ci	tipc_net_stop(net);
11362306a36Sopenharmony_ci	/* Make sure the tipc_net_finalize_work() finished */
11462306a36Sopenharmony_ci	cancel_work_sync(&tn->work);
11562306a36Sopenharmony_ci	tipc_bcast_stop(net);
11662306a36Sopenharmony_ci	tipc_nametbl_stop(net);
11762306a36Sopenharmony_ci	tipc_sk_rht_destroy(net);
11862306a36Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
11962306a36Sopenharmony_ci	tipc_crypto_stop(&tipc_net(net)->crypto_tx);
12062306a36Sopenharmony_ci#endif
12162306a36Sopenharmony_ci	while (atomic_read(&tn->wq_count))
12262306a36Sopenharmony_ci		cond_resched();
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic void __net_exit tipc_pernet_pre_exit(struct net *net)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	tipc_node_pre_cleanup_net(net);
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic struct pernet_operations tipc_pernet_pre_exit_ops = {
13162306a36Sopenharmony_ci	.pre_exit = tipc_pernet_pre_exit,
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic struct pernet_operations tipc_net_ops = {
13562306a36Sopenharmony_ci	.init = tipc_init_net,
13662306a36Sopenharmony_ci	.exit = tipc_exit_net,
13762306a36Sopenharmony_ci	.id   = &tipc_net_id,
13862306a36Sopenharmony_ci	.size = sizeof(struct tipc_net),
13962306a36Sopenharmony_ci};
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cistatic struct pernet_operations tipc_topsrv_net_ops = {
14262306a36Sopenharmony_ci	.init = tipc_topsrv_init_net,
14362306a36Sopenharmony_ci	.exit = tipc_topsrv_exit_net,
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic int __init tipc_init(void)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	int err;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	pr_info("Activated (version " TIPC_MOD_VER ")\n");
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	sysctl_tipc_rmem[0] = RCVBUF_MIN;
15362306a36Sopenharmony_ci	sysctl_tipc_rmem[1] = RCVBUF_DEF;
15462306a36Sopenharmony_ci	sysctl_tipc_rmem[2] = RCVBUF_MAX;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	err = tipc_register_sysctl();
15762306a36Sopenharmony_ci	if (err)
15862306a36Sopenharmony_ci		goto out_sysctl;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	err = register_pernet_device(&tipc_net_ops);
16162306a36Sopenharmony_ci	if (err)
16262306a36Sopenharmony_ci		goto out_pernet;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	err = tipc_socket_init();
16562306a36Sopenharmony_ci	if (err)
16662306a36Sopenharmony_ci		goto out_socket;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	err = register_pernet_device(&tipc_topsrv_net_ops);
16962306a36Sopenharmony_ci	if (err)
17062306a36Sopenharmony_ci		goto out_pernet_topsrv;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	err = register_pernet_subsys(&tipc_pernet_pre_exit_ops);
17362306a36Sopenharmony_ci	if (err)
17462306a36Sopenharmony_ci		goto out_register_pernet_subsys;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	err = tipc_bearer_setup();
17762306a36Sopenharmony_ci	if (err)
17862306a36Sopenharmony_ci		goto out_bearer;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	err = tipc_netlink_start();
18162306a36Sopenharmony_ci	if (err)
18262306a36Sopenharmony_ci		goto out_netlink;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	err = tipc_netlink_compat_start();
18562306a36Sopenharmony_ci	if (err)
18662306a36Sopenharmony_ci		goto out_netlink_compat;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	pr_info("Started in single node mode\n");
18962306a36Sopenharmony_ci	return 0;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ciout_netlink_compat:
19262306a36Sopenharmony_ci	tipc_netlink_stop();
19362306a36Sopenharmony_ciout_netlink:
19462306a36Sopenharmony_ci	tipc_bearer_cleanup();
19562306a36Sopenharmony_ciout_bearer:
19662306a36Sopenharmony_ci	unregister_pernet_subsys(&tipc_pernet_pre_exit_ops);
19762306a36Sopenharmony_ciout_register_pernet_subsys:
19862306a36Sopenharmony_ci	unregister_pernet_device(&tipc_topsrv_net_ops);
19962306a36Sopenharmony_ciout_pernet_topsrv:
20062306a36Sopenharmony_ci	tipc_socket_stop();
20162306a36Sopenharmony_ciout_socket:
20262306a36Sopenharmony_ci	unregister_pernet_device(&tipc_net_ops);
20362306a36Sopenharmony_ciout_pernet:
20462306a36Sopenharmony_ci	tipc_unregister_sysctl();
20562306a36Sopenharmony_ciout_sysctl:
20662306a36Sopenharmony_ci	pr_err("Unable to start in single node mode\n");
20762306a36Sopenharmony_ci	return err;
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic void __exit tipc_exit(void)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	tipc_netlink_compat_stop();
21362306a36Sopenharmony_ci	tipc_netlink_stop();
21462306a36Sopenharmony_ci	tipc_bearer_cleanup();
21562306a36Sopenharmony_ci	unregister_pernet_subsys(&tipc_pernet_pre_exit_ops);
21662306a36Sopenharmony_ci	unregister_pernet_device(&tipc_topsrv_net_ops);
21762306a36Sopenharmony_ci	tipc_socket_stop();
21862306a36Sopenharmony_ci	unregister_pernet_device(&tipc_net_ops);
21962306a36Sopenharmony_ci	tipc_unregister_sysctl();
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	pr_info("Deactivated\n");
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cimodule_init(tipc_init);
22562306a36Sopenharmony_cimodule_exit(tipc_exit);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciMODULE_DESCRIPTION("TIPC: Transparent Inter Process Communication");
22862306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
22962306a36Sopenharmony_ciMODULE_VERSION(TIPC_MOD_VER);
230