]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/net/net_namespace.h
inet: frags: remove inet_frag_maybe_warn_overflow()
[mirror_ubuntu-bionic-kernel.git] / include / net / net_namespace.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
5f256bec
EB
2/*
3 * Operations on the network namespace
4 */
5#ifndef __NET_NET_NAMESPACE_H
6#define __NET_NET_NAMESPACE_H
7
60063497 8#include <linux/atomic.h>
c122e14d 9#include <linux/refcount.h>
5f256bec
EB
10#include <linux/workqueue.h>
11#include <linux/list.h>
bee95250 12#include <linux/sysctl.h>
de645b47 13#include <linux/uidgid.h>
5f256bec 14
6a662719 15#include <net/flow.h>
8efa6e93 16#include <net/netns/core.h>
852566f5 17#include <net/netns/mib.h>
a0a53c8b 18#include <net/netns/unix.h>
2aaef4e4 19#include <net/netns/packet.h>
8afd351c 20#include <net/netns/ipv4.h>
b0f159db 21#include <net/netns/ipv6.h>
633fc86f 22#include <net/netns/ieee802154_6lowpan.h>
4db67e80 23#include <net/netns/sctp.h>
67019cc9 24#include <net/netns/dccp.h>
f3c1a44a 25#include <net/netns/netfilter.h>
8d870052 26#include <net/netns/x_tables.h>
dfdb8d79
AD
27#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
28#include <net/netns/conntrack.h>
29#endif
99633ab2 30#include <net/netns/nftables.h>
d62ddc21 31#include <net/netns/xfrm.h>
0189197f 32#include <net/netns/mpls.h>
8e8cda6d 33#include <net/netns/can.h>
435d5f4b 34#include <linux/ns_common.h>
04c52dec
PNA
35#include <linux/idr.h>
36#include <linux/skbuff.h>
a0a53c8b 37
038e7332 38struct user_namespace;
457c4cbc 39struct proc_dir_entry;
2774c7ab 40struct net_device;
97c53cac 41struct sock;
1597fbc0 42struct ctl_table_header;
dec827d1 43struct net_generic;
134e6375 44struct sock;
2553d064 45struct netns_ipvs;
1597fbc0 46
7c28bd0b
ED
47
48#define NETDEV_HASHBITS 8
49#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
50
5f256bec 51struct net {
c122e14d 52 refcount_t passive; /* To decided when the network
a685e089
AV
53 * namespace should be freed.
54 */
5f256bec 55 atomic_t count; /* To decided when the network
a685e089 56 * namespace should be shut down.
5f256bec 57 */
8e602ce2
ED
58 spinlock_t rules_mod_lock;
59
33cf7c90
ED
60 atomic64_t cookie_gen;
61
5f256bec 62 struct list_head list; /* list of network namespaces */
2b035b39 63 struct list_head cleanup_list; /* namespaces on death row */
72ad937a 64 struct list_head exit_list; /* Use only net_mutex */
457c4cbc 65
038e7332 66 struct user_namespace *user_ns; /* Owning user namespace */
70328660 67 struct ucounts *ucounts;
de133464 68 spinlock_t nsid_lock;
0c7aecd4 69 struct idr netns_ids;
038e7332 70
435d5f4b 71 struct ns_common ns;
98f842e6 72
457c4cbc
EB
73 struct proc_dir_entry *proc_net;
74 struct proc_dir_entry *proc_net_stat;
881d966b 75
73455092
AV
76#ifdef CONFIG_SYSCTL
77 struct ctl_table_set sysctls;
78#endif
95bdfccb 79
8e602ce2
ED
80 struct sock *rtnl; /* rtnetlink socket */
81 struct sock *genl_sock;
2774c7ab 82
881d966b
EB
83 struct list_head dev_base_head;
84 struct hlist_head *dev_name_head;
85 struct hlist_head *dev_index_head;
4e985ada 86 unsigned int dev_base_seq; /* protected by rtnl_mutex */
aa79e66e 87 int ifindex;
50624c93 88 unsigned int dev_unreg_count;
97c53cac 89
5fd30ee7
DL
90 /* core fib_rules */
91 struct list_head rules_ops;
5fd30ee7 92
04b1d4e5 93 struct list_head fib_notifier_ops; /* protected by net_mutex */
d12d01d6 94
8e602ce2 95 struct net_device *loopback_dev; /* The loopback */
8efa6e93 96 struct netns_core core;
852566f5 97 struct netns_mib mib;
2aaef4e4 98 struct netns_packet packet;
a0a53c8b 99 struct netns_unix unx;
8afd351c 100 struct netns_ipv4 ipv4;
dfd56b8b 101#if IS_ENABLED(CONFIG_IPV6)
b0f159db
DL
102 struct netns_ipv6 ipv6;
103#endif
633fc86f
AA
104#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
105 struct netns_ieee802154_lowpan ieee802154_lowpan;
106#endif
4db67e80
EB
107#if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
108 struct netns_sctp sctp;
109#endif
67019cc9
PE
110#if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
111 struct netns_dccp dccp;
112#endif
8d870052 113#ifdef CONFIG_NETFILTER
f3c1a44a 114 struct netns_nf nf;
8d870052 115 struct netns_xt xt;
dfdb8d79
AD
116#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
117 struct netns_ct ct;
c038a767 118#endif
99633ab2
PNA
119#if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
120 struct netns_nftables nft;
121#endif
c038a767
AW
122#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
123 struct netns_nf_frag nf_frag;
581e73c0 124 struct ctl_table_header *nf_frag_frags_hdr;
dfdb8d79 125#endif
cd8c20b6
AD
126 struct sock *nfnl;
127 struct sock *nfnl_stash;
3499abb2
AS
128#if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
129 struct list_head nfnl_acct_list;
130#endif
19576c94
PN
131#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
132 struct list_head nfct_timeout_list;
133#endif
d62ddc21 134#endif
3d23e349 135#ifdef CONFIG_WEXT_CORE
b333b3d2 136 struct sk_buff_head wext_nlevents;
8d870052 137#endif
1c87733d 138 struct net_generic __rcu *gen;
8e602ce2
ED
139
140 /* Note : following structs are cache line aligned */
141#ifdef CONFIG_XFRM
142 struct netns_xfrm xfrm;
143#endif
8b4d14d8 144#if IS_ENABLED(CONFIG_IP_VS)
61b1ab45 145 struct netns_ipvs *ipvs;
0189197f
EB
146#endif
147#if IS_ENABLED(CONFIG_MPLS)
148 struct netns_mpls mpls;
8e8cda6d
MK
149#endif
150#if IS_ENABLED(CONFIG_CAN)
151 struct netns_can can;
8b4d14d8 152#endif
51d7cccf 153 struct sock *diag_nlsk;
5aad1de5 154 atomic_t fnhe_genid;
3859a271 155} __randomize_layout;
5f256bec 156
c0f39322
DL
157#include <linux/seq_file_net.h>
158
4fabcd71 159/* Init's network namespace */
5f256bec 160extern struct net init_net;
a4aa834a 161
d727abcb 162#ifdef CONFIG_NET_NS
e67e16ea
JP
163struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
164 struct net *old_net);
225c0a01 165
de645b47
TH
166void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
167
7866cc57 168void net_ns_barrier(void);
d727abcb
EB
169#else /* CONFIG_NET_NS */
170#include <linux/sched.h>
171#include <linux/nsproxy.h>
038e7332
EB
172static inline struct net *copy_net_ns(unsigned long flags,
173 struct user_namespace *user_ns, struct net *old_net)
9dd776b6 174{
d727abcb
EB
175 if (flags & CLONE_NEWNET)
176 return ERR_PTR(-EINVAL);
177 return old_net;
9dd776b6 178}
7866cc57 179
de645b47
TH
180static inline void net_ns_get_ownership(const struct net *net,
181 kuid_t *uid, kgid_t *gid)
182{
183 *uid = GLOBAL_ROOT_UID;
184 *gid = GLOBAL_ROOT_GID;
185}
186
7866cc57 187static inline void net_ns_barrier(void) {}
d727abcb 188#endif /* CONFIG_NET_NS */
225c0a01
DL
189
190
191extern struct list_head net_namespace_list;
9dd776b6 192
e67e16ea 193struct net *get_net_ns_by_pid(pid_t pid);
0f5258cd 194struct net *get_net_ns_by_fd(int fd);
30ffee84 195
535d3ae9
RK
196#ifdef CONFIG_SYSCTL
197void ipx_register_sysctl(void);
198void ipx_unregister_sysctl(void);
199#else
200#define ipx_register_sysctl()
201#define ipx_unregister_sysctl()
202#endif
203
d4655795 204#ifdef CONFIG_NET_NS
e67e16ea 205void __put_net(struct net *net);
5f256bec
EB
206
207static inline struct net *get_net(struct net *net)
208{
209 atomic_inc(&net->count);
210 return net;
211}
212
077130c0
EB
213static inline struct net *maybe_get_net(struct net *net)
214{
215 /* Used when we know struct net exists but we
216 * aren't guaranteed a previous reference count
217 * exists. If the reference count is zero this
218 * function fails and returns NULL.
219 */
220 if (!atomic_inc_not_zero(&net->count))
221 net = NULL;
222 return net;
223}
224
5f256bec
EB
225static inline void put_net(struct net *net)
226{
227 if (atomic_dec_and_test(&net->count))
228 __put_net(net);
229}
230
878628fb
YH
231static inline
232int net_eq(const struct net *net1, const struct net *net2)
233{
234 return net1 == net2;
235}
a685e089 236
4ee806d5
DS
237static inline int check_net(const struct net *net)
238{
239 return atomic_read(&net->count) != 0;
240}
241
e67e16ea 242void net_drop_ns(void *);
a685e089 243
d4655795 244#else
b9f75f45 245
d4655795
PE
246static inline struct net *get_net(struct net *net)
247{
248 return net;
249}
250
251static inline void put_net(struct net *net)
252{
253}
254
5d1e4468
DL
255static inline struct net *maybe_get_net(struct net *net)
256{
257 return net;
258}
259
260static inline
261int net_eq(const struct net *net1, const struct net *net2)
262{
263 return 1;
264}
a685e089 265
4ee806d5
DS
266static inline int check_net(const struct net *net)
267{
268 return 1;
269}
270
a685e089 271#define net_drop_ns NULL
5d1e4468
DL
272#endif
273
274
0c5c9fb5 275typedef struct {
8f424b5f 276#ifdef CONFIG_NET_NS
0c5c9fb5
EB
277 struct net *net;
278#endif
279} possible_net_t;
8f424b5f 280
0c5c9fb5 281static inline void write_pnet(possible_net_t *pnet, struct net *net)
8f424b5f 282{
0c5c9fb5
EB
283#ifdef CONFIG_NET_NS
284 pnet->net = net;
285#endif
8f424b5f
ED
286}
287
0c5c9fb5 288static inline struct net *read_pnet(const possible_net_t *pnet)
8f424b5f 289{
0c5c9fb5
EB
290#ifdef CONFIG_NET_NS
291 return pnet->net;
8f424b5f 292#else
0c5c9fb5 293 return &init_net;
8f424b5f 294#endif
0c5c9fb5 295}
5d1e4468 296
5f256bec
EB
297#define for_each_net(VAR) \
298 list_for_each_entry(VAR, &net_namespace_list, list)
299
11a28d37
JB
300#define for_each_net_rcu(VAR) \
301 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
302
4665079c
PE
303#ifdef CONFIG_NET_NS
304#define __net_init
305#define __net_exit
022cbae6 306#define __net_initdata
04a6f82c 307#define __net_initconst
4665079c
PE
308#else
309#define __net_init __init
bd721ea7 310#define __net_exit __ref
022cbae6 311#define __net_initdata __initdata
04a6f82c 312#define __net_initconst __initconst
4665079c 313#endif
5f256bec 314
7a0877d4 315int peernet2id_alloc(struct net *net, struct net *peer);
59324cf3
ND
316int peernet2id(struct net *net, struct net *peer);
317bool peernet_has_id(struct net *net, struct net *peer);
0c7aecd4
ND
318struct net *get_net_ns_by_id(struct net *net, int id);
319
5f256bec
EB
320struct pernet_operations {
321 struct list_head list;
322 int (*init)(struct net *net);
323 void (*exit)(struct net *net);
72ad937a 324 void (*exit_batch)(struct list_head *net_exit_list);
c7d03a00 325 unsigned int *id;
f875bae0 326 size_t size;
5f256bec
EB
327};
328
17edde52
EB
329/*
330 * Use these carefully. If you implement a network device and it
331 * needs per network namespace operations use device pernet operations,
332 * otherwise use pernet subsys operations.
333 *
4edf547b
JB
334 * Network interfaces need to be removed from a dying netns _before_
335 * subsys notifiers can be called, as most of the network code cleanup
336 * (which is done from subsys notifiers) runs with the assumption that
337 * dev_remove_pack has been called so no new packets will arrive during
338 * and after the cleanup functions have been called. dev_remove_pack
339 * is not per namespace so instead the guarantee of no more packets
340 * arriving in a network namespace is provided by ensuring that all
341 * network devices and all sockets have left the network namespace
342 * before the cleanup methods are called.
17edde52
EB
343 *
344 * For the longest time the ipv4 icmp code was registered as a pernet
345 * device which caused kernel oops, and panics during network
346 * namespace cleanup. So please don't get this wrong.
347 */
e67e16ea
JP
348int register_pernet_subsys(struct pernet_operations *);
349void unregister_pernet_subsys(struct pernet_operations *);
350int register_pernet_device(struct pernet_operations *);
351void unregister_pernet_device(struct pernet_operations *);
f875bae0 352
95bdfccb
EB
353struct ctl_table;
354struct ctl_table_header;
d62c612e 355
2ca794e5 356#ifdef CONFIG_SYSCTL
e67e16ea
JP
357int net_sysctl_init(void);
358struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
359 struct ctl_table *table);
360void unregister_net_sysctl_table(struct ctl_table_header *header);
48c74958
EB
361#else
362static inline int net_sysctl_init(void) { return 0; }
363static inline struct ctl_table_header *register_net_sysctl(struct net *net,
364 const char *path, struct ctl_table *table)
365{
366 return NULL;
367}
368static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
369{
370}
371#endif
372
ca4c3fc2 373static inline int rt_genid_ipv4(struct net *net)
b42664f8 374{
ca4c3fc2 375 return atomic_read(&net->ipv4.rt_genid);
b42664f8
ND
376}
377
ca4c3fc2 378static inline void rt_genid_bump_ipv4(struct net *net)
b42664f8 379{
ca4c3fc2 380 atomic_inc(&net->ipv4.rt_genid);
381}
382
705f1c86 383extern void (*__fib6_flush_trees)(struct net *net);
ca4c3fc2 384static inline void rt_genid_bump_ipv6(struct net *net)
385{
705f1c86
HFS
386 if (__fib6_flush_trees)
387 __fib6_flush_trees(net);
ca4c3fc2 388}
ca4c3fc2 389
599018a7
LR
390#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
391static inline struct netns_ieee802154_lowpan *
392net_ieee802154_lowpan(struct net *net)
393{
394 return &net->ieee802154_lowpan;
395}
599018a7
LR
396#endif
397
ca4c3fc2 398/* For callers who don't really care about whether it's IPv4 or IPv6 */
399static inline void rt_genid_bump_all(struct net *net)
400{
401 rt_genid_bump_ipv4(net);
402 rt_genid_bump_ipv6(net);
b42664f8 403}
95bdfccb 404
5aad1de5
TT
405static inline int fnhe_genid(struct net *net)
406{
407 return atomic_read(&net->fnhe_genid);
408}
409
410static inline void fnhe_genid_bump(struct net *net)
411{
412 atomic_inc(&net->fnhe_genid);
413}
414
5f256bec 415#endif /* __NET_NET_NAMESPACE_H */