1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden * net/tipc/core.h: Include file for TIPC global declarations
3b97bf3fdSPer Liden *
4b89afb11SJon Maloy * Copyright (c) 2005-2006, 2013-2018 Ericsson AB
5c5fa7b3cSYing Xue * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
65f75e0a0SJon Maloy * Copyright (c) 2020, Red Hat Inc
7b97bf3fdSPer Liden * All rights reserved.
8b97bf3fdSPer Liden *
9b97bf3fdSPer Liden * Redistribution and use in source and binary forms, with or without
10b97bf3fdSPer Liden * modification, are permitted provided that the following conditions are met:
11b97bf3fdSPer Liden *
129ea1fd3cSPer Liden * 1. Redistributions of source code must retain the above copyright
139ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer.
149ea1fd3cSPer Liden * 2. Redistributions in binary form must reproduce the above copyright
159ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer in the
169ea1fd3cSPer Liden * documentation and/or other materials provided with the distribution.
179ea1fd3cSPer Liden * 3. Neither the names of the copyright holders nor the names of its
189ea1fd3cSPer Liden * contributors may be used to endorse or promote products derived from
199ea1fd3cSPer Liden * this software without specific prior written permission.
209ea1fd3cSPer Liden *
219ea1fd3cSPer Liden * Alternatively, this software may be distributed under the terms of the
229ea1fd3cSPer Liden * GNU General Public License ("GPL") version 2 as published by the Free
239ea1fd3cSPer Liden * Software Foundation.
24b97bf3fdSPer Liden *
25b97bf3fdSPer Liden * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26b97bf3fdSPer Liden * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27b97bf3fdSPer Liden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28b97bf3fdSPer Liden * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29b97bf3fdSPer Liden * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30b97bf3fdSPer Liden * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31b97bf3fdSPer Liden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32b97bf3fdSPer Liden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33b97bf3fdSPer Liden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34b97bf3fdSPer Liden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35b97bf3fdSPer Liden * POSSIBILITY OF SUCH DAMAGE.
36b97bf3fdSPer Liden */
37b97bf3fdSPer Liden
38b97bf3fdSPer Liden #ifndef _TIPC_CORE_H
39b97bf3fdSPer Liden #define _TIPC_CORE_H
40b97bf3fdSPer Liden
415f7c3ff6SJon Maloy #include <linux/tipc.h>
425f7c3ff6SJon Maloy #include <linux/tipc_config.h>
430655f6a8SRichard Alpe #include <linux/tipc_netlink.h>
44b97bf3fdSPer Liden #include <linux/types.h>
45b97bf3fdSPer Liden #include <linux/kernel.h>
46b97bf3fdSPer Liden #include <linux/errno.h>
47b97bf3fdSPer Liden #include <linux/mm.h>
48b97bf3fdSPer Liden #include <linux/timer.h>
49b97bf3fdSPer Liden #include <linux/string.h>
50776a74ceSwangweidong #include <linux/uaccess.h>
51b97bf3fdSPer Liden #include <linux/interrupt.h>
5260063497SArun Sharma #include <linux/atomic.h>
53b97bf3fdSPer Liden #include <linux/netdevice.h>
54b97bf3fdSPer Liden #include <linux/in.h>
55b97bf3fdSPer Liden #include <linux/list.h>
565a0e3ad6STejun Heo #include <linux/slab.h>
57b97bf3fdSPer Liden #include <linux/vmalloc.h>
58ef13a262SYing Xue #include <linux/rtnetlink.h>
5938504c28SJon Paul Maloy #include <linux/etherdevice.h>
60c93d3baaSYing Xue #include <net/netns/generic.h>
61e05b31f4SYing Xue #include <linux/rhashtable.h>
62218527feSJon Maloy #include <net/genetlink.h>
63f73b1281SHoang Le #include <net/netns/hash.h>
64107e7be6SAllan Stephens
65d7f9f47dSMatt Bennett #ifdef pr_fmt
66d7f9f47dSMatt Bennett #undef pr_fmt
67d7f9f47dSMatt Bennett #endif
68d7f9f47dSMatt Bennett
69d7f9f47dSMatt Bennett #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
70d7f9f47dSMatt Bennett
71a6bf70f7SJon Paul Maloy struct tipc_node;
72a6bf70f7SJon Paul Maloy struct tipc_bearer;
736beb19a6SJon Paul Maloy struct tipc_bc_base;
74a6bf70f7SJon Paul Maloy struct tipc_link;
75a6bf70f7SJon Paul Maloy struct tipc_name_table;
76026321c6SJon Maloy struct tipc_topsrv;
7735c55c98SJon Paul Maloy struct tipc_monitor;
78fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
79fc1b6d6dSTuong Lien struct tipc_crypto;
80fc1b6d6dSTuong Lien #endif
81f2f9800dSYing Xue
82107e7be6SAllan Stephens #define TIPC_MOD_VER "2.0.0"
83107e7be6SAllan Stephens
84a6bf70f7SJon Paul Maloy #define NODE_HTABLE_SIZE 512
85a6bf70f7SJon Paul Maloy #define MAX_BEARERS 3
8635c55c98SJon Paul Maloy #define TIPC_DEF_MON_THRESHOLD 32
87d50ccc2dSJon Maloy #define NODE_ID_LEN 16
88d50ccc2dSJon Maloy #define NODE_ID_STR_LEN (NODE_ID_LEN * 2 + 1)
89a6bf70f7SJon Paul Maloy
90c7d03a00SAlexey Dobriyan extern unsigned int tipc_net_id __read_mostly;
91cc79dd1bSYing Xue extern int sysctl_tipc_rmem[3] __read_mostly;
92a5325ae5SErik Hugne extern int sysctl_tipc_named_timeout __read_mostly;
93b97bf3fdSPer Liden
94c93d3baaSYing Xue struct tipc_net {
95d50ccc2dSJon Maloy u8 node_id[NODE_ID_LEN];
96d50ccc2dSJon Maloy u32 node_addr;
9725b0b9c4SJon Maloy u32 trial_addr;
9825b0b9c4SJon Maloy unsigned long addr_trial_end;
99d50ccc2dSJon Maloy char node_id_string[NODE_ID_STR_LEN];
100c93d3baaSYing Xue int net_id;
101bafa29e3SYing Xue int random;
102b89afb11SJon Maloy bool legacy_addr_format;
103f2f9800dSYing Xue
104f2f9800dSYing Xue /* Node table and node list */
105f2f9800dSYing Xue spinlock_t node_list_lock;
106f2f9800dSYing Xue struct hlist_head node_htable[NODE_HTABLE_SIZE];
107f2f9800dSYing Xue struct list_head node_list;
108f2f9800dSYing Xue u32 num_nodes;
109f2f9800dSYing Xue u32 num_links;
1107f9f95d9SYing Xue
11135c55c98SJon Paul Maloy /* Neighbor monitoring list */
11235c55c98SJon Paul Maloy struct tipc_monitor *monitors[MAX_BEARERS];
11335c55c98SJon Paul Maloy int mon_threshold;
11435c55c98SJon Paul Maloy
1157f9f95d9SYing Xue /* Bearer list */
1167f9f95d9SYing Xue struct tipc_bearer __rcu *bearer_list[MAX_BEARERS + 1];
1171da46568SYing Xue
1181da46568SYing Xue /* Broadcast link */
1190043550bSJon Paul Maloy spinlock_t bclock;
1206beb19a6SJon Paul Maloy struct tipc_bc_base *bcbase;
1211da46568SYing Xue struct tipc_link *bcl;
122e05b31f4SYing Xue
123e05b31f4SYing Xue /* Socket hash table */
124e05b31f4SYing Xue struct rhashtable sk_rht;
1254ac1c8d0SYing Xue
1264ac1c8d0SYing Xue /* Name table */
1274ac1c8d0SYing Xue spinlock_t nametbl_lock;
1284ac1c8d0SYing Xue struct name_table *nametbl;
129a62fbcceSYing Xue
130a62fbcceSYing Xue /* Topology subscription server */
131026321c6SJon Maloy struct tipc_topsrv *topsrv;
132a62fbcceSYing Xue atomic_t subscription_count;
133ff2ebbfbSHoang Le
134ff2ebbfbSHoang Le /* Cluster capabilities */
135ff2ebbfbSHoang Le u16 capabilities;
1366c9081a3SJohn Rutherford
1376c9081a3SJohn Rutherford /* Tracing of node internal messages */
1386c9081a3SJohn Rutherford struct packet_type loopback_pt;
139fc1b6d6dSTuong Lien
140fc1b6d6dSTuong Lien #ifdef CONFIG_TIPC_CRYPTO
141fc1b6d6dSTuong Lien /* TX crypto handler */
142fc1b6d6dSTuong Lien struct tipc_crypto *crypto_tx;
143fc1b6d6dSTuong Lien #endif
144d966ddccSHoang Huu Le /* Work item for net finalize */
145be07f056SXin Long struct work_struct work;
14604c26faaSXin Long /* The numbers of work queues in schedule */
14704c26faaSXin Long atomic_t wq_count;
148c93d3baaSYing Xue };
149c93d3baaSYing Xue
tipc_net(struct net * net)150440d8963SJon Paul Maloy static inline struct tipc_net *tipc_net(struct net *net)
151440d8963SJon Paul Maloy {
152440d8963SJon Paul Maloy return net_generic(net, tipc_net_id);
153440d8963SJon Paul Maloy }
154440d8963SJon Paul Maloy
tipc_netid(struct net * net)15552666986SJon Paul Maloy static inline int tipc_netid(struct net *net)
15652666986SJon Paul Maloy {
15752666986SJon Paul Maloy return tipc_net(net)->net_id;
15852666986SJon Paul Maloy }
15952666986SJon Paul Maloy
tipc_nodes(struct net * net)1601d7e1c25SJon Paul Maloy static inline struct list_head *tipc_nodes(struct net *net)
1611d7e1c25SJon Paul Maloy {
1621d7e1c25SJon Paul Maloy return &tipc_net(net)->node_list;
1631d7e1c25SJon Paul Maloy }
1641d7e1c25SJon Paul Maloy
tipc_name_table(struct net * net)16564a52b26SJon Maloy static inline struct name_table *tipc_name_table(struct net *net)
16664a52b26SJon Maloy {
16764a52b26SJon Maloy return tipc_net(net)->nametbl;
16864a52b26SJon Maloy }
16964a52b26SJon Maloy
tipc_topsrv(struct net * net)170026321c6SJon Maloy static inline struct tipc_topsrv *tipc_topsrv(struct net *net)
17114c04493SJon Maloy {
17214c04493SJon Maloy return tipc_net(net)->topsrv;
17314c04493SJon Maloy }
17414c04493SJon Maloy
tipc_hashfn(u32 addr)17535c55c98SJon Paul Maloy static inline unsigned int tipc_hashfn(u32 addr)
17635c55c98SJon Paul Maloy {
17735c55c98SJon Paul Maloy return addr & (NODE_HTABLE_SIZE - 1);
17835c55c98SJon Paul Maloy }
17935c55c98SJon Paul Maloy
mod(u16 x)180e4bf4f76SJon Paul Maloy static inline u16 mod(u16 x)
181e4bf4f76SJon Paul Maloy {
182e4bf4f76SJon Paul Maloy return x & 0xffffu;
183e4bf4f76SJon Paul Maloy }
184e4bf4f76SJon Paul Maloy
less_eq(u16 left,u16 right)185e4bf4f76SJon Paul Maloy static inline int less_eq(u16 left, u16 right)
186e4bf4f76SJon Paul Maloy {
187e4bf4f76SJon Paul Maloy return mod(right - left) < 32768u;
188e4bf4f76SJon Paul Maloy }
189e4bf4f76SJon Paul Maloy
more(u16 left,u16 right)190e4bf4f76SJon Paul Maloy static inline int more(u16 left, u16 right)
191e4bf4f76SJon Paul Maloy {
192e4bf4f76SJon Paul Maloy return !less_eq(left, right);
193e4bf4f76SJon Paul Maloy }
194e4bf4f76SJon Paul Maloy
less(u16 left,u16 right)195e4bf4f76SJon Paul Maloy static inline int less(u16 left, u16 right)
196e4bf4f76SJon Paul Maloy {
197e4bf4f76SJon Paul Maloy return less_eq(left, right) && (mod(right) != mod(left));
198e4bf4f76SJon Paul Maloy }
199e4bf4f76SJon Paul Maloy
tipc_in_range(u16 val,u16 min,u16 max)200*f9bff0e3SMatthew Wilcox (Oracle) static inline int tipc_in_range(u16 val, u16 min, u16 max)
201d999297cSJon Paul Maloy {
202d999297cSJon Paul Maloy return !less(val, min) && !more(val, max);
203d999297cSJon Paul Maloy }
204d999297cSJon Paul Maloy
tipc_net_hash_mixes(struct net * net,int tn_rand)205f73b1281SHoang Le static inline u32 tipc_net_hash_mixes(struct net *net, int tn_rand)
206f73b1281SHoang Le {
207f73b1281SHoang Le return net_hash_mix(&init_net) ^ net_hash_mix(net) ^ tn_rand;
208f73b1281SHoang Le }
209f73b1281SHoang Le
hash128to32(char * bytes)2105f75e0a0SJon Maloy static inline u32 hash128to32(char *bytes)
2115f75e0a0SJon Maloy {
2125f75e0a0SJon Maloy __be32 *tmp = (__be32 *)bytes;
2135f75e0a0SJon Maloy u32 res;
2145f75e0a0SJon Maloy
2155f75e0a0SJon Maloy res = ntohl(tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3]);
2165f75e0a0SJon Maloy if (likely(res))
2175f75e0a0SJon Maloy return res;
2185f75e0a0SJon Maloy return ntohl(tmp[0] | tmp[1] | tmp[2] | tmp[3]);
2195f75e0a0SJon Maloy }
2205f75e0a0SJon Maloy
221cc79dd1bSYing Xue #ifdef CONFIG_SYSCTL
222c1b1203dSJoe Perches int tipc_register_sysctl(void);
223c1b1203dSJoe Perches void tipc_unregister_sysctl(void);
224cc79dd1bSYing Xue #else
225cc79dd1bSYing Xue #define tipc_register_sysctl() 0
226cc79dd1bSYing Xue #define tipc_unregister_sysctl()
227cc79dd1bSYing Xue #endif
228b97bf3fdSPer Liden #endif
229