1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25f256becSEric W. Biederman /*
35f256becSEric W. Biederman * Operations on the network namespace
45f256becSEric W. Biederman */
55f256becSEric W. Biederman #ifndef __NET_NET_NAMESPACE_H
65f256becSEric W. Biederman #define __NET_NET_NAMESPACE_H
75f256becSEric W. Biederman
860063497SArun Sharma #include <linux/atomic.h>
9c122e14dSReshetova, Elena #include <linux/refcount.h>
105f256becSEric W. Biederman #include <linux/workqueue.h>
115f256becSEric W. Biederman #include <linux/list.h>
12bee95250SDavid S. Miller #include <linux/sysctl.h>
13fbdeaed4STyler Hicks #include <linux/uidgid.h>
145f256becSEric W. Biederman
156a662719SCong Wang #include <net/flow.h>
168efa6e93SPavel Emelyanov #include <net/netns/core.h>
17852566f5SPavel Emelyanov #include <net/netns/mib.h>
18a0a53c8bSDenis V. Lunev #include <net/netns/unix.h>
192aaef4e4SDenis V. Lunev #include <net/netns/packet.h>
208afd351cSPavel Emelyanov #include <net/netns/ipv4.h>
21b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h>
22ab84be7eSDavid Ahern #include <net/netns/nexthop.h>
23633fc86fSAlexander Aring #include <net/netns/ieee802154_6lowpan.h>
244db67e80SEric W. Biederman #include <net/netns/sctp.h>
25f3c1a44aSGao feng #include <net/netns/netfilter.h>
26dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
27dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
28dfdb8d79SAlexey Dobriyan #endif
29b0381776SVlad Buslov #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
30b0381776SVlad Buslov #include <net/netns/flow_table.h>
31b0381776SVlad Buslov #endif
3299633ab2SPablo Neira Ayuso #include <net/netns/nftables.h>
33d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
340189197fSEric W. Biederman #include <net/netns/mpls.h>
358e8cda6dSMario Kicherer #include <net/netns/can.h>
361d0dc069SBjörn Töpel #include <net/netns/xdp.h>
37194730a9SGuvenc Gulce #include <net/netns/smc.h>
38a3fd7ceeSJakub Sitnicki #include <net/netns/bpf.h>
39889b7da2SJeremy Kerr #include <net/netns/mctp.h>
409ba74e6cSEric Dumazet #include <net/net_trackers.h>
41435d5f4bSAl Viro #include <linux/ns_common.h>
4204c52decSPablo Neira Ayuso #include <linux/idr.h>
4304c52decSPablo Neira Ayuso #include <linux/skbuff.h>
44a30c7b42SJiri Pirko #include <linux/notifier.h>
45759ab1edSJakub Kicinski #include <linux/xarray.h>
46a0a53c8bSDenis V. Lunev
47038e7332SEric W. Biederman struct user_namespace;
48457c4cbcSEric W. Biederman struct proc_dir_entry;
492774c7abSEric W. Biederman struct net_device;
5097c53cacSDenis V. Lunev struct sock;
511597fbc0SPavel Emelyanov struct ctl_table_header;
52dec827d1SPavel Emelyanov struct net_generic;
5394e5e308SChristian Brauner struct uevent_sock;
542553d064SJulian Anastasov struct netns_ipvs;
55d58e468bSPetar Penkov struct bpf_prog;
561597fbc0SPavel Emelyanov
577c28bd0bSEric Dumazet
587c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8
597c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
607c28bd0bSEric Dumazet
615f256becSEric W. Biederman struct net {
622a06b898SEric Dumazet /* First cache line can be often dirtied.
632a06b898SEric Dumazet * Do not place here read-mostly fields.
642a06b898SEric Dumazet */
65aad12c23SMike Rapoport refcount_t passive; /* To decide when the network
665f256becSEric W. Biederman * namespace should be freed.
675f256becSEric W. Biederman */
688e602ce2SEric Dumazet spinlock_t rules_mod_lock;
698e602ce2SEric Dumazet
70ede6c39cSEric Dumazet atomic_t dev_unreg_count;
712a06b898SEric Dumazet
722a06b898SEric Dumazet unsigned int dev_base_seq; /* protected by rtnl_mutex */
73759ab1edSJakub Kicinski u32 ifindex;
742a06b898SEric Dumazet
752a06b898SEric Dumazet spinlock_t nsid_lock;
762a06b898SEric Dumazet atomic_t fnhe_genid;
7733cf7c90SEric Dumazet
785f256becSEric W. Biederman struct list_head list; /* list of network namespaces */
7919efbd93SKirill Tkhai struct list_head exit_list; /* To linked to call pernet exit
804420bf21SKirill Tkhai * methods on dead net (
814420bf21SKirill Tkhai * pernet_ops_rwsem read locked),
824420bf21SKirill Tkhai * or to unregister pernet ops
834420bf21SKirill Tkhai * (pernet_ops_rwsem write locked).
8419efbd93SKirill Tkhai */
85b7a79e51SEric Dumazet struct llist_node defer_free_list;
8665b7b5b9SKirill Tkhai struct llist_node cleanup_list; /* namespaces on death row */
8765b7b5b9SKirill Tkhai
889b242610SDavid Howells #ifdef CONFIG_KEYS
899b242610SDavid Howells struct key_tag *key_domain; /* Key domain of operation tag */
909b242610SDavid Howells #endif
91038e7332SEric W. Biederman struct user_namespace *user_ns; /* Owning user namespace */
9270328660SEric W. Biederman struct ucounts *ucounts;
930c7aecd4SNicolas Dichtel struct idr netns_ids;
94038e7332SEric W. Biederman
95435d5f4bSAl Viro struct ns_common ns;
969ba74e6cSEric Dumazet struct ref_tracker_dir refcnt_tracker;
970cafd77dSEric Dumazet struct ref_tracker_dir notrefcnt_tracker; /* tracker for objects not
980cafd77dSEric Dumazet * refcounted against netns
990cafd77dSEric Dumazet */
1002a06b898SEric Dumazet struct list_head dev_base_head;
101457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net;
102457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat;
103881d966bSEric W. Biederman
10473455092SAl Viro #ifdef CONFIG_SYSCTL
10573455092SAl Viro struct ctl_table_set sysctls;
10673455092SAl Viro #endif
10795bdfccbSEric W. Biederman
1088e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */
1098e602ce2SEric Dumazet struct sock *genl_sock;
1102774c7abSEric W. Biederman
11194e5e308SChristian Brauner struct uevent_sock *uevent_sock; /* uevent socket */
11294e5e308SChristian Brauner
113881d966bSEric W. Biederman struct hlist_head *dev_name_head;
114881d966bSEric W. Biederman struct hlist_head *dev_index_head;
115759ab1edSJakub Kicinski struct xarray dev_by_index;
116a30c7b42SJiri Pirko struct raw_notifier_head netdev_chain;
117a30c7b42SJiri Pirko
1182a06b898SEric Dumazet /* Note that @hash_mix can be read millions times per second,
1192a06b898SEric Dumazet * it is critical that it is on a read_mostly cache line.
1202a06b898SEric Dumazet */
1212a06b898SEric Dumazet u32 hash_mix;
1222a06b898SEric Dumazet
1232a06b898SEric Dumazet struct net_device *loopback_dev; /* The loopback */
12497c53cacSDenis V. Lunev
1255fd30ee7SDenis V. Lunev /* core fib_rules */
1265fd30ee7SDenis V. Lunev struct list_head rules_ops;
1275fd30ee7SDenis V. Lunev
1288efa6e93SPavel Emelyanov struct netns_core core;
129852566f5SPavel Emelyanov struct netns_mib mib;
1302aaef4e4SDenis V. Lunev struct netns_packet packet;
131b6e81138SKuniyuki Iwashima #if IS_ENABLED(CONFIG_UNIX)
132a0a53c8bSDenis V. Lunev struct netns_unix unx;
133b6e81138SKuniyuki Iwashima #endif
134ab84be7eSDavid Ahern struct netns_nexthop nexthop;
1358afd351cSPavel Emelyanov struct netns_ipv4 ipv4;
136dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
137b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6;
138b0f159dbSDaniel Lezcano #endif
139633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
140633fc86fSAlexander Aring struct netns_ieee802154_lowpan ieee802154_lowpan;
141633fc86fSAlexander Aring #endif
1424db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1434db67e80SEric W. Biederman struct netns_sctp sctp;
1444db67e80SEric W. Biederman #endif
1458d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
146f3c1a44aSGao feng struct netns_nf nf;
147dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
148dfdb8d79SAlexey Dobriyan struct netns_ct ct;
149dfdb8d79SAlexey Dobriyan #endif
15099633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
15199633ab2SPablo Neira Ayuso struct netns_nftables nft;
15299633ab2SPablo Neira Ayuso #endif
153b0381776SVlad Buslov #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
154b0381776SVlad Buslov struct netns_ft ft;
155b0381776SVlad Buslov #endif
1568d870052SAlexey Dobriyan #endif
1573d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
158b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents;
159b333b3d2SJohannes Berg #endif
1601c87733dSEric Dumazet struct net_generic __rcu *gen;
1618e602ce2SEric Dumazet
162a3fd7ceeSJakub Sitnicki /* Used to store attached BPF programs */
163a3fd7ceeSJakub Sitnicki struct netns_bpf bpf;
164d58e468bSPetar Penkov
1658e602ce2SEric Dumazet /* Note : following structs are cache line aligned */
1668e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1678e602ce2SEric Dumazet struct netns_xfrm xfrm;
1688e602ce2SEric Dumazet #endif
169f318903cSDaniel Borkmann
1703d368ab8SEric Dumazet u64 net_cookie; /* written once */
171f318903cSDaniel Borkmann
1728b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
17361b1ab45SHans Schillstrom struct netns_ipvs *ipvs;
1748b4d14d8SJunweiZhang #endif
1750189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1760189197fSEric W. Biederman struct netns_mpls mpls;
1770189197fSEric W. Biederman #endif
1788e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1798e8cda6dSMario Kicherer struct netns_can can;
1808e8cda6dSMario Kicherer #endif
1811d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1821d0dc069SBjörn Töpel struct netns_xdp xdp;
1831d0dc069SBjörn Töpel #endif
184889b7da2SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP)
185889b7da2SJeremy Kerr struct netns_mctp mctp;
186889b7da2SJeremy Kerr #endif
18791b05a7eSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER)
18891b05a7eSOndrej Mosnacek struct sock *crypto_nlsk;
18991b05a7eSOndrej Mosnacek #endif
19051d7cccfSAndrey Vagin struct sock *diag_nlsk;
191194730a9SGuvenc Gulce #if IS_ENABLED(CONFIG_SMC)
192194730a9SGuvenc Gulce struct netns_smc smc;
193194730a9SGuvenc Gulce #endif
1943859a271SKees Cook } __randomize_layout;
1955f256becSEric W. Biederman
196c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
197c0f39322SDenis V. Lunev
1984fabcd71SDaniel Lezcano /* Init's network namespace */
1995f256becSEric W. Biederman extern struct net init_net;
200a4aa834aSDenis V. Lunev
201d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
202e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
203e67e16eaSJoe Perches struct net *old_net);
204225c0a01SDenis V. Lunev
205fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
206fbdeaed4STyler Hicks
2077866cc57SFlorian Westphal void net_ns_barrier(void);
208ea6932d7SChangbin Du
209ea6932d7SChangbin Du struct ns_common *get_net_ns(struct ns_common *ns);
210e34492deSChangbin Du struct net *get_net_ns_by_fd(int fd);
211d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
212d727abcbSEric W. Biederman #include <linux/sched.h>
213d727abcbSEric W. Biederman #include <linux/nsproxy.h>
copy_net_ns(unsigned long flags,struct user_namespace * user_ns,struct net * old_net)214038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
215038e7332SEric W. Biederman struct user_namespace *user_ns, struct net *old_net)
2169dd776b6SEric W. Biederman {
217d727abcbSEric W. Biederman if (flags & CLONE_NEWNET)
218d727abcbSEric W. Biederman return ERR_PTR(-EINVAL);
219d727abcbSEric W. Biederman return old_net;
2209dd776b6SEric W. Biederman }
2217866cc57SFlorian Westphal
net_ns_get_ownership(const struct net * net,kuid_t * uid,kgid_t * gid)222fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
223fbdeaed4STyler Hicks kuid_t *uid, kgid_t *gid)
224fbdeaed4STyler Hicks {
225fbdeaed4STyler Hicks *uid = GLOBAL_ROOT_UID;
226fbdeaed4STyler Hicks *gid = GLOBAL_ROOT_GID;
227fbdeaed4STyler Hicks }
228fbdeaed4STyler Hicks
net_ns_barrier(void)2297866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
230ea6932d7SChangbin Du
get_net_ns(struct ns_common * ns)231ea6932d7SChangbin Du static inline struct ns_common *get_net_ns(struct ns_common *ns)
232ea6932d7SChangbin Du {
233ea6932d7SChangbin Du return ERR_PTR(-EINVAL);
234ea6932d7SChangbin Du }
235e34492deSChangbin Du
get_net_ns_by_fd(int fd)236e34492deSChangbin Du static inline struct net *get_net_ns_by_fd(int fd)
237e34492deSChangbin Du {
238e34492deSChangbin Du return ERR_PTR(-EINVAL);
239e34492deSChangbin Du }
240d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
241225c0a01SDenis V. Lunev
242225c0a01SDenis V. Lunev
243225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2449dd776b6SEric W. Biederman
245e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
24630ffee84SJohannes Berg
247535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
248535d3ae9SRashika Kheria void ipx_register_sysctl(void);
249535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
250535d3ae9SRashika Kheria #else
251535d3ae9SRashika Kheria #define ipx_register_sysctl()
252535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
253535d3ae9SRashika Kheria #endif
254535d3ae9SRashika Kheria
255d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
256e67e16eaSJoe Perches void __put_net(struct net *net);
2575f256becSEric W. Biederman
2589ba74e6cSEric Dumazet /* Try using get_net_track() instead */
get_net(struct net * net)2595f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2605f256becSEric W. Biederman {
2618b8f3e66SChristian Brauner refcount_inc(&net->ns.count);
2625f256becSEric W. Biederman return net;
2635f256becSEric W. Biederman }
2645f256becSEric W. Biederman
maybe_get_net(struct net * net)265077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
266077130c0SEric W. Biederman {
267077130c0SEric W. Biederman /* Used when we know struct net exists but we
268077130c0SEric W. Biederman * aren't guaranteed a previous reference count
269077130c0SEric W. Biederman * exists. If the reference count is zero this
270077130c0SEric W. Biederman * function fails and returns NULL.
271077130c0SEric W. Biederman */
2728b8f3e66SChristian Brauner if (!refcount_inc_not_zero(&net->ns.count))
273077130c0SEric W. Biederman net = NULL;
274077130c0SEric W. Biederman return net;
275077130c0SEric W. Biederman }
276077130c0SEric W. Biederman
2779ba74e6cSEric Dumazet /* Try using put_net_track() instead */
put_net(struct net * net)2785f256becSEric W. Biederman static inline void put_net(struct net *net)
2795f256becSEric W. Biederman {
2808b8f3e66SChristian Brauner if (refcount_dec_and_test(&net->ns.count))
2815f256becSEric W. Biederman __put_net(net);
2825f256becSEric W. Biederman }
2835f256becSEric W. Biederman
284878628fbSYOSHIFUJI Hideaki static inline
net_eq(const struct net * net1,const struct net * net2)285878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
286878628fbSYOSHIFUJI Hideaki {
287878628fbSYOSHIFUJI Hideaki return net1 == net2;
288878628fbSYOSHIFUJI Hideaki }
289a685e089SAl Viro
check_net(const struct net * net)2904ee806d5SDan Streetman static inline int check_net(const struct net *net)
2914ee806d5SDan Streetman {
2928b8f3e66SChristian Brauner return refcount_read(&net->ns.count) != 0;
2934ee806d5SDan Streetman }
2944ee806d5SDan Streetman
295e67e16eaSJoe Perches void net_drop_ns(void *);
296a685e089SAl Viro
297d4655795SPavel Emelyanov #else
298b9f75f45SEric W. Biederman
get_net(struct net * net)299d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
300d4655795SPavel Emelyanov {
301d4655795SPavel Emelyanov return net;
302d4655795SPavel Emelyanov }
303d4655795SPavel Emelyanov
put_net(struct net * net)304d4655795SPavel Emelyanov static inline void put_net(struct net *net)
305d4655795SPavel Emelyanov {
306d4655795SPavel Emelyanov }
307d4655795SPavel Emelyanov
maybe_get_net(struct net * net)308d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
309d4655795SPavel Emelyanov {
310d4655795SPavel Emelyanov return net;
311d4655795SPavel Emelyanov }
312878628fbSYOSHIFUJI Hideaki
313878628fbSYOSHIFUJI Hideaki static inline
net_eq(const struct net * net1,const struct net * net2)314878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
315878628fbSYOSHIFUJI Hideaki {
316878628fbSYOSHIFUJI Hideaki return 1;
317878628fbSYOSHIFUJI Hideaki }
318a685e089SAl Viro
check_net(const struct net * net)3194ee806d5SDan Streetman static inline int check_net(const struct net *net)
3204ee806d5SDan Streetman {
3214ee806d5SDan Streetman return 1;
3224ee806d5SDan Streetman }
3234ee806d5SDan Streetman
324a685e089SAl Viro #define net_drop_ns NULL
325d4655795SPavel Emelyanov #endif
3265f256becSEric W. Biederman
3275d1e4468SDenis V. Lunev
__netns_tracker_alloc(struct net * net,netns_tracker * tracker,bool refcounted,gfp_t gfp)3280cafd77dSEric Dumazet static inline void __netns_tracker_alloc(struct net *net,
3290cafd77dSEric Dumazet netns_tracker *tracker,
3300cafd77dSEric Dumazet bool refcounted,
3310cafd77dSEric Dumazet gfp_t gfp)
3329ba74e6cSEric Dumazet {
3339ba74e6cSEric Dumazet #ifdef CONFIG_NET_NS_REFCNT_TRACKER
3340cafd77dSEric Dumazet ref_tracker_alloc(refcounted ? &net->refcnt_tracker :
3350cafd77dSEric Dumazet &net->notrefcnt_tracker,
3360cafd77dSEric Dumazet tracker, gfp);
3379ba74e6cSEric Dumazet #endif
3389ba74e6cSEric Dumazet }
3399ba74e6cSEric Dumazet
netns_tracker_alloc(struct net * net,netns_tracker * tracker,gfp_t gfp)3400cafd77dSEric Dumazet static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker,
3410cafd77dSEric Dumazet gfp_t gfp)
3420cafd77dSEric Dumazet {
3430cafd77dSEric Dumazet __netns_tracker_alloc(net, tracker, true, gfp);
3440cafd77dSEric Dumazet }
3450cafd77dSEric Dumazet
__netns_tracker_free(struct net * net,netns_tracker * tracker,bool refcounted)3460cafd77dSEric Dumazet static inline void __netns_tracker_free(struct net *net,
3470cafd77dSEric Dumazet netns_tracker *tracker,
3480cafd77dSEric Dumazet bool refcounted)
3499ba74e6cSEric Dumazet {
3509ba74e6cSEric Dumazet #ifdef CONFIG_NET_NS_REFCNT_TRACKER
3510cafd77dSEric Dumazet ref_tracker_free(refcounted ? &net->refcnt_tracker :
3520cafd77dSEric Dumazet &net->notrefcnt_tracker, tracker);
3539ba74e6cSEric Dumazet #endif
3549ba74e6cSEric Dumazet }
3559ba74e6cSEric Dumazet
get_net_track(struct net * net,netns_tracker * tracker,gfp_t gfp)3569ba74e6cSEric Dumazet static inline struct net *get_net_track(struct net *net,
3579ba74e6cSEric Dumazet netns_tracker *tracker, gfp_t gfp)
3589ba74e6cSEric Dumazet {
3599ba74e6cSEric Dumazet get_net(net);
3609ba74e6cSEric Dumazet netns_tracker_alloc(net, tracker, gfp);
3619ba74e6cSEric Dumazet return net;
3629ba74e6cSEric Dumazet }
3639ba74e6cSEric Dumazet
put_net_track(struct net * net,netns_tracker * tracker)3649ba74e6cSEric Dumazet static inline void put_net_track(struct net *net, netns_tracker *tracker)
3659ba74e6cSEric Dumazet {
3660cafd77dSEric Dumazet __netns_tracker_free(net, tracker, true);
3679ba74e6cSEric Dumazet put_net(net);
3689ba74e6cSEric Dumazet }
3699ba74e6cSEric Dumazet
3700c5c9fb5SEric W. Biederman typedef struct {
3718f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
3724cfecb7fSJiri Pirko struct net __rcu *net;
3738f424b5fSEric Dumazet #endif
3740c5c9fb5SEric W. Biederman } possible_net_t;
3750c5c9fb5SEric W. Biederman
write_pnet(possible_net_t * pnet,struct net * net)3760c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
3770c5c9fb5SEric W. Biederman {
3780c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3794cfecb7fSJiri Pirko rcu_assign_pointer(pnet->net, net);
3800c5c9fb5SEric W. Biederman #endif
3810c5c9fb5SEric W. Biederman }
3820c5c9fb5SEric W. Biederman
read_pnet(const possible_net_t * pnet)3830c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3840c5c9fb5SEric W. Biederman {
3850c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3864cfecb7fSJiri Pirko return rcu_dereference_protected(pnet->net, true);
3874cfecb7fSJiri Pirko #else
3884cfecb7fSJiri Pirko return &init_net;
3894cfecb7fSJiri Pirko #endif
3904cfecb7fSJiri Pirko }
3914cfecb7fSJiri Pirko
read_pnet_rcu(const possible_net_t * pnet)392*81f369b6SEric Dumazet static inline struct net *read_pnet_rcu(const possible_net_t *pnet)
3934cfecb7fSJiri Pirko {
3944cfecb7fSJiri Pirko #ifdef CONFIG_NET_NS
3954cfecb7fSJiri Pirko return rcu_dereference(pnet->net);
3960c5c9fb5SEric W. Biederman #else
3970c5c9fb5SEric W. Biederman return &init_net;
3980c5c9fb5SEric W. Biederman #endif
3990c5c9fb5SEric W. Biederman }
4005d1e4468SDenis V. Lunev
401f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
4025f256becSEric W. Biederman #define for_each_net(VAR) \
4035f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list)
404afa0df59SJiri Pirko #define for_each_net_continue_reverse(VAR) \
405afa0df59SJiri Pirko list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
40611a28d37SJohannes Berg #define for_each_net_rcu(VAR) \
40711a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list)
40811a28d37SJohannes Berg
4094665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
4104665079cSPavel Emelyanov #define __net_init
4114665079cSPavel Emelyanov #define __net_exit
412022cbae6SDenis V. Lunev #define __net_initdata
41304a6f82cSAndi Kleen #define __net_initconst
4144665079cSPavel Emelyanov #else
4154665079cSPavel Emelyanov #define __net_init __init
416bd721ea7SFabian Frederick #define __net_exit __ref
417022cbae6SDenis V. Lunev #define __net_initdata __initdata
41804a6f82cSAndi Kleen #define __net_initconst __initconst
4194665079cSPavel Emelyanov #endif
4205f256becSEric W. Biederman
421d4e4fdf9SGuillaume Nault int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
42256f200c7SGuillaume Nault int peernet2id(const struct net *net, struct net *peer);
42356f200c7SGuillaume Nault bool peernet_has_id(const struct net *net, struct net *peer);
42456f200c7SGuillaume Nault struct net *get_net_ns_by_id(const struct net *net, int id);
4250c7aecd4SNicolas Dichtel
4265f256becSEric W. Biederman struct pernet_operations {
4275f256becSEric W. Biederman struct list_head list;
4286056415dSKirill Tkhai /*
4296056415dSKirill Tkhai * Below methods are called without any exclusive locks.
4306056415dSKirill Tkhai * More than one net may be constructed and destructed
4316056415dSKirill Tkhai * in parallel on several cpus. Every pernet_operations
4326056415dSKirill Tkhai * have to keep in mind all other pernet_operations and
4336056415dSKirill Tkhai * to introduce a locking, if they share common resources.
4346056415dSKirill Tkhai *
4358518e9bbSKirill Tkhai * The only time they are called with exclusive lock is
4368518e9bbSKirill Tkhai * from register_pernet_subsys(), unregister_pernet_subsys()
4378518e9bbSKirill Tkhai * register_pernet_device() and unregister_pernet_device().
4388518e9bbSKirill Tkhai *
4396056415dSKirill Tkhai * Exit methods using blocking RCU primitives, such as
4406056415dSKirill Tkhai * synchronize_rcu(), should be implemented via exit_batch.
4416056415dSKirill Tkhai * Then, destruction of a group of net requires single
4426056415dSKirill Tkhai * synchronize_rcu() related to these pernet_operations,
4436056415dSKirill Tkhai * instead of separate synchronize_rcu() for every net.
4446056415dSKirill Tkhai * Please, avoid synchronize_rcu() at all, where it's possible.
445d7d99872SEric Dumazet *
446d7d99872SEric Dumazet * Note that a combination of pre_exit() and exit() can
447d7d99872SEric Dumazet * be used, since a synchronize_rcu() is guaranteed between
448d7d99872SEric Dumazet * the calls.
4496056415dSKirill Tkhai */
4505f256becSEric W. Biederman int (*init)(struct net *net);
451d7d99872SEric Dumazet void (*pre_exit)(struct net *net);
4525f256becSEric W. Biederman void (*exit)(struct net *net);
45372ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list);
4541e222169SEric Dumazet /* Following method is called with RTNL held. */
4551e222169SEric Dumazet void (*exit_batch_rtnl)(struct list_head *net_exit_list,
4561e222169SEric Dumazet struct list_head *dev_kill_list);
457c7d03a00SAlexey Dobriyan unsigned int *id;
458f875bae0SEric W. Biederman size_t size;
4595f256becSEric W. Biederman };
4605f256becSEric W. Biederman
46117edde52SEric W. Biederman /*
46217edde52SEric W. Biederman * Use these carefully. If you implement a network device and it
46317edde52SEric W. Biederman * needs per network namespace operations use device pernet operations,
46417edde52SEric W. Biederman * otherwise use pernet subsys operations.
46517edde52SEric W. Biederman *
4664edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_
4674edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup
4684edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that
4694edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during
4704edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack
4714edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets
4724edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all
4734edf547bSJohannes Berg * network devices and all sockets have left the network namespace
4744edf547bSJohannes Berg * before the cleanup methods are called.
47517edde52SEric W. Biederman *
47617edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet
47717edde52SEric W. Biederman * device which caused kernel oops, and panics during network
47817edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong.
47917edde52SEric W. Biederman */
480e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
481e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
482e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
483e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
484f875bae0SEric W. Biederman
48595bdfccbSEric W. Biederman struct ctl_table;
486d62c612eSPavel Emelyanov
48795d49778SJoel Granados #define register_net_sysctl(net, path, table) \
488e1b41e4fSJoel Granados register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
4892ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
490e67e16eaSJoe Perches int net_sysctl_init(void);
49195d49778SJoel Granados struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
49295d49778SJoel Granados struct ctl_table *table, size_t table_size);
493e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
49448c74958SEric W. Biederman #else
net_sysctl_init(void)49548c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
register_net_sysctl_sz(struct net * net,const char * path,struct ctl_table * table,size_t table_size)49695d49778SJoel Granados static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
49795d49778SJoel Granados const char *path, struct ctl_table *table, size_t table_size)
49848c74958SEric W. Biederman {
49948c74958SEric W. Biederman return NULL;
50048c74958SEric W. Biederman }
unregister_net_sysctl_table(struct ctl_table_header * header)50148c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
50248c74958SEric W. Biederman {
50348c74958SEric W. Biederman }
50448c74958SEric W. Biederman #endif
50548c74958SEric W. Biederman
rt_genid_ipv4(const struct net * net)50656f200c7SGuillaume Nault static inline int rt_genid_ipv4(const struct net *net)
507b42664f8SNicolas Dichtel {
508ca4c3fc2Sfan.du return atomic_read(&net->ipv4.rt_genid);
509b42664f8SNicolas Dichtel }
510b42664f8SNicolas Dichtel
5118f34e53bSDavid Ahern #if IS_ENABLED(CONFIG_IPV6)
rt_genid_ipv6(const struct net * net)5128f34e53bSDavid Ahern static inline int rt_genid_ipv6(const struct net *net)
5138f34e53bSDavid Ahern {
5148f34e53bSDavid Ahern return atomic_read(&net->ipv6.fib6_sernum);
5158f34e53bSDavid Ahern }
5168f34e53bSDavid Ahern #endif
5178f34e53bSDavid Ahern
rt_genid_bump_ipv4(struct net * net)518ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
519b42664f8SNicolas Dichtel {
520ca4c3fc2Sfan.du atomic_inc(&net->ipv4.rt_genid);
521ca4c3fc2Sfan.du }
522ca4c3fc2Sfan.du
523705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
rt_genid_bump_ipv6(struct net * net)524ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
525ca4c3fc2Sfan.du {
526705f1c86SHannes Frederic Sowa if (__fib6_flush_trees)
527705f1c86SHannes Frederic Sowa __fib6_flush_trees(net);
528ca4c3fc2Sfan.du }
529ca4c3fc2Sfan.du
530599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
531599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
net_ieee802154_lowpan(struct net * net)532599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
533599018a7SLuis R. Rodriguez {
534599018a7SLuis R. Rodriguez return &net->ieee802154_lowpan;
535599018a7SLuis R. Rodriguez }
536599018a7SLuis R. Rodriguez #endif
537599018a7SLuis R. Rodriguez
538ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
rt_genid_bump_all(struct net * net)539ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
540ca4c3fc2Sfan.du {
541ca4c3fc2Sfan.du rt_genid_bump_ipv4(net);
542ca4c3fc2Sfan.du rt_genid_bump_ipv6(net);
543b42664f8SNicolas Dichtel }
54495bdfccbSEric W. Biederman
fnhe_genid(const struct net * net)54556f200c7SGuillaume Nault static inline int fnhe_genid(const struct net *net)
5465aad1de5STimo Teräs {
5475aad1de5STimo Teräs return atomic_read(&net->fnhe_genid);
5485aad1de5STimo Teräs }
5495aad1de5STimo Teräs
fnhe_genid_bump(struct net * net)5505aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
5515aad1de5STimo Teräs {
5525aad1de5STimo Teräs atomic_inc(&net->fnhe_genid);
5535aad1de5STimo Teräs }
5545aad1de5STimo Teräs
5559c1be193SEric Dumazet #ifdef CONFIG_NET
5569c1be193SEric Dumazet void net_ns_init(void);
5579c1be193SEric Dumazet #else
net_ns_init(void)5589c1be193SEric Dumazet static inline void net_ns_init(void) {}
5599c1be193SEric Dumazet #endif
5609c1be193SEric Dumazet
5615f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
562