18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * net/tipc/core.h: Include file for TIPC global declarations
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2005-2006, 2013-2018 Ericsson AB
58c2ecf20Sopenharmony_ci * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
68c2ecf20Sopenharmony_ci * All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
128c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
138c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
148c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
158c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
168c2ecf20Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
178c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
188c2ecf20Sopenharmony_ci *    this software without specific prior written permission.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
218c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
228c2ecf20Sopenharmony_ci * Software Foundation.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
258c2ecf20Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
288c2ecf20Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
298c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
308c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
318c2ecf20Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
328c2ecf20Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
338c2ecf20Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
348c2ecf20Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifndef _TIPC_CORE_H
388c2ecf20Sopenharmony_ci#define _TIPC_CORE_H
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <linux/tipc.h>
418c2ecf20Sopenharmony_ci#include <linux/tipc_config.h>
428c2ecf20Sopenharmony_ci#include <linux/tipc_netlink.h>
438c2ecf20Sopenharmony_ci#include <linux/types.h>
448c2ecf20Sopenharmony_ci#include <linux/kernel.h>
458c2ecf20Sopenharmony_ci#include <linux/errno.h>
468c2ecf20Sopenharmony_ci#include <linux/mm.h>
478c2ecf20Sopenharmony_ci#include <linux/timer.h>
488c2ecf20Sopenharmony_ci#include <linux/string.h>
498c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
508c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
518c2ecf20Sopenharmony_ci#include <linux/atomic.h>
528c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
538c2ecf20Sopenharmony_ci#include <linux/in.h>
548c2ecf20Sopenharmony_ci#include <linux/list.h>
558c2ecf20Sopenharmony_ci#include <linux/slab.h>
568c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
578c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
588c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
598c2ecf20Sopenharmony_ci#include <net/netns/generic.h>
608c2ecf20Sopenharmony_ci#include <linux/rhashtable.h>
618c2ecf20Sopenharmony_ci#include <net/genetlink.h>
628c2ecf20Sopenharmony_ci#include <net/netns/hash.h>
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#ifdef pr_fmt
658c2ecf20Sopenharmony_ci#undef pr_fmt
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct tipc_node;
718c2ecf20Sopenharmony_cistruct tipc_bearer;
728c2ecf20Sopenharmony_cistruct tipc_bc_base;
738c2ecf20Sopenharmony_cistruct tipc_link;
748c2ecf20Sopenharmony_cistruct tipc_name_table;
758c2ecf20Sopenharmony_cistruct tipc_topsrv;
768c2ecf20Sopenharmony_cistruct tipc_monitor;
778c2ecf20Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
788c2ecf20Sopenharmony_cistruct tipc_crypto;
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define TIPC_MOD_VER "2.0.0"
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define NODE_HTABLE_SIZE       512
848c2ecf20Sopenharmony_ci#define MAX_BEARERS	         3
858c2ecf20Sopenharmony_ci#define TIPC_DEF_MON_THRESHOLD  32
868c2ecf20Sopenharmony_ci#define NODE_ID_LEN             16
878c2ecf20Sopenharmony_ci#define NODE_ID_STR_LEN        (NODE_ID_LEN * 2 + 1)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciextern unsigned int tipc_net_id __read_mostly;
908c2ecf20Sopenharmony_ciextern int sysctl_tipc_rmem[3] __read_mostly;
918c2ecf20Sopenharmony_ciextern int sysctl_tipc_named_timeout __read_mostly;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct tipc_net {
948c2ecf20Sopenharmony_ci	u8  node_id[NODE_ID_LEN];
958c2ecf20Sopenharmony_ci	u32 node_addr;
968c2ecf20Sopenharmony_ci	u32 trial_addr;
978c2ecf20Sopenharmony_ci	unsigned long addr_trial_end;
988c2ecf20Sopenharmony_ci	char node_id_string[NODE_ID_STR_LEN];
998c2ecf20Sopenharmony_ci	int net_id;
1008c2ecf20Sopenharmony_ci	int random;
1018c2ecf20Sopenharmony_ci	bool legacy_addr_format;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* Node table and node list */
1048c2ecf20Sopenharmony_ci	spinlock_t node_list_lock;
1058c2ecf20Sopenharmony_ci	struct hlist_head node_htable[NODE_HTABLE_SIZE];
1068c2ecf20Sopenharmony_ci	struct list_head node_list;
1078c2ecf20Sopenharmony_ci	u32 num_nodes;
1088c2ecf20Sopenharmony_ci	u32 num_links;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Neighbor monitoring list */
1118c2ecf20Sopenharmony_ci	struct tipc_monitor *monitors[MAX_BEARERS];
1128c2ecf20Sopenharmony_ci	int mon_threshold;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* Bearer list */
1158c2ecf20Sopenharmony_ci	struct tipc_bearer __rcu *bearer_list[MAX_BEARERS + 1];
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* Broadcast link */
1188c2ecf20Sopenharmony_ci	spinlock_t bclock;
1198c2ecf20Sopenharmony_ci	struct tipc_bc_base *bcbase;
1208c2ecf20Sopenharmony_ci	struct tipc_link *bcl;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Socket hash table */
1238c2ecf20Sopenharmony_ci	struct rhashtable sk_rht;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Name table */
1268c2ecf20Sopenharmony_ci	spinlock_t nametbl_lock;
1278c2ecf20Sopenharmony_ci	struct name_table *nametbl;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* Name dist queue */
1308c2ecf20Sopenharmony_ci	struct list_head dist_queue;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/* Topology subscription server */
1338c2ecf20Sopenharmony_ci	struct tipc_topsrv *topsrv;
1348c2ecf20Sopenharmony_ci	atomic_t subscription_count;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* Cluster capabilities */
1378c2ecf20Sopenharmony_ci	u16 capabilities;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* Tracing of node internal messages */
1408c2ecf20Sopenharmony_ci	struct packet_type loopback_pt;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
1438c2ecf20Sopenharmony_ci	/* TX crypto handler */
1448c2ecf20Sopenharmony_ci	struct tipc_crypto *crypto_tx;
1458c2ecf20Sopenharmony_ci#endif
1468c2ecf20Sopenharmony_ci	/* Work item for net finalize */
1478c2ecf20Sopenharmony_ci	struct work_struct work;
1488c2ecf20Sopenharmony_ci	/* The numbers of work queues in schedule */
1498c2ecf20Sopenharmony_ci	atomic_t wq_count;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic inline struct tipc_net *tipc_net(struct net *net)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	return net_generic(net, tipc_net_id);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic inline int tipc_netid(struct net *net)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	return tipc_net(net)->net_id;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic inline struct list_head *tipc_nodes(struct net *net)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return &tipc_net(net)->node_list;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic inline struct name_table *tipc_name_table(struct net *net)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return tipc_net(net)->nametbl;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic inline struct tipc_topsrv *tipc_topsrv(struct net *net)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return tipc_net(net)->topsrv;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic inline unsigned int tipc_hashfn(u32 addr)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return addr & (NODE_HTABLE_SIZE - 1);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline u16 mod(u16 x)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return x & 0xffffu;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline int less_eq(u16 left, u16 right)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return mod(right - left) < 32768u;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline int more(u16 left, u16 right)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	return !less_eq(left, right);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic inline int less(u16 left, u16 right)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	return less_eq(left, right) && (mod(right) != mod(left));
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic inline int in_range(u16 val, u16 min, u16 max)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	return !less(val, min) && !more(val, max);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline u32 tipc_net_hash_mixes(struct net *net, int tn_rand)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	return net_hash_mix(&init_net) ^ net_hash_mix(net) ^ tn_rand;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSCTL
2138c2ecf20Sopenharmony_ciint tipc_register_sysctl(void);
2148c2ecf20Sopenharmony_civoid tipc_unregister_sysctl(void);
2158c2ecf20Sopenharmony_ci#else
2168c2ecf20Sopenharmony_ci#define tipc_register_sysctl() 0
2178c2ecf20Sopenharmony_ci#define tipc_unregister_sysctl()
2188c2ecf20Sopenharmony_ci#endif
2198c2ecf20Sopenharmony_ci#endif
220