blob: 13b0e3b547f09fec9dcbec224ba3ccd5e08906b6 [file] [log] [blame]
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001/*
2 * Operations on the network namespace
3 */
4#ifndef __NET_NET_NAMESPACE_H
5#define __NET_NET_NAMESPACE_H
6
7#include <asm/atomic.h>
8#include <linux/workqueue.h>
9#include <linux/list.h>
10
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020011struct proc_dir_entry;
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070012struct net_device;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020013struct net {
14 atomic_t count; /* To decided when the network
15 * namespace should be freed.
16 */
17 atomic_t use_count; /* To track references we
18 * destroy on demand
19 */
20 struct list_head list; /* list of network namespaces */
21 struct work_struct work; /* work struct for freeing */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020022
23 struct proc_dir_entry *proc_net;
24 struct proc_dir_entry *proc_net_stat;
25 struct proc_dir_entry *proc_net_root;
Eric W. Biederman881d9662007-09-17 11:56:21 -070026
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070027 struct net_device *loopback_dev; /* The loopback */
28
Eric W. Biederman881d9662007-09-17 11:56:21 -070029 struct list_head dev_base_head;
30 struct hlist_head *dev_name_head;
31 struct hlist_head *dev_index_head;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020032};
33
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020034#ifdef CONFIG_NET
35/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020036extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020037#define INIT_NET_NS(net_ns) .net_ns = &init_net,
38#else
39#define INIT_NET_NS(net_ns)
40#endif
41
Eric W. Biederman5f256be2007-09-12 11:50:50 +020042extern struct list_head net_namespace_list;
43
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070044#ifdef CONFIG_NET
45extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
46#else
47static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
48{
49 /* There is nothing to copy so this is a noop */
50 return net_ns;
51}
52#endif
53
Eric W. Biederman5f256be2007-09-12 11:50:50 +020054extern void __put_net(struct net *net);
55
56static inline struct net *get_net(struct net *net)
57{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070058#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020059 atomic_inc(&net->count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070060#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020061 return net;
62}
63
Eric W. Biederman077130c2007-09-13 09:18:57 +020064static inline struct net *maybe_get_net(struct net *net)
65{
66 /* Used when we know struct net exists but we
67 * aren't guaranteed a previous reference count
68 * exists. If the reference count is zero this
69 * function fails and returns NULL.
70 */
71 if (!atomic_inc_not_zero(&net->count))
72 net = NULL;
73 return net;
74}
75
Eric W. Biederman5f256be2007-09-12 11:50:50 +020076static inline void put_net(struct net *net)
77{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070078#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020079 if (atomic_dec_and_test(&net->count))
80 __put_net(net);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070081#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020082}
83
84static inline struct net *hold_net(struct net *net)
85{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070086#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020087 atomic_inc(&net->use_count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070088#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020089 return net;
90}
91
92static inline void release_net(struct net *net)
93{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070094#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020095 atomic_dec(&net->use_count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070096#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020097}
98
99extern void net_lock(void);
100extern void net_unlock(void);
101
102#define for_each_net(VAR) \
103 list_for_each_entry(VAR, &net_namespace_list, list)
104
105
106struct pernet_operations {
107 struct list_head list;
108 int (*init)(struct net *net);
109 void (*exit)(struct net *net);
110};
111
112extern int register_pernet_subsys(struct pernet_operations *);
113extern void unregister_pernet_subsys(struct pernet_operations *);
114extern int register_pernet_device(struct pernet_operations *);
115extern void unregister_pernet_device(struct pernet_operations *);
116
117#endif /* __NET_NET_NAMESPACE_H */