f97b2a4469ae2f46339b76fb9171bd2fc529583e
[safe/jmp/linux-2.6] / include / net / net_namespace.h
1 /*
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
11 struct proc_dir_entry;
12 struct net_device;
13 struct sock;
14 struct ctl_table_header;
15
16 struct net {
17         atomic_t                count;          /* To decided when the network
18                                                  *  namespace should be freed.
19                                                  */
20         atomic_t                use_count;      /* To track references we
21                                                  * destroy on demand
22                                                  */
23         struct list_head        list;           /* list of network namespaces */
24         struct work_struct      work;           /* work struct for freeing */
25
26         struct proc_dir_entry   *proc_net;
27         struct proc_dir_entry   *proc_net_stat;
28         struct proc_dir_entry   *proc_net_root;
29
30         struct list_head        sysctl_table_headers;
31
32         struct net_device       *loopback_dev;          /* The loopback */
33
34         struct list_head        dev_base_head;
35         struct hlist_head       *dev_name_head;
36         struct hlist_head       *dev_index_head;
37
38         struct sock             *rtnl;                  /* rtnetlink socket */
39
40         /* List of all packet sockets. */
41         rwlock_t                packet_sklist_lock;
42         struct hlist_head       packet_sklist;
43
44         /* unix sockets */
45         int                     sysctl_unix_max_dgram_qlen;
46         struct ctl_table_header *unix_ctl;
47 };
48
49 #ifdef CONFIG_NET
50 /* Init's network namespace */
51 extern struct net init_net;
52 #define INIT_NET_NS(net_ns) .net_ns = &init_net,
53 #else
54 #define INIT_NET_NS(net_ns)
55 #endif
56
57 extern struct list_head net_namespace_list;
58
59 #ifdef CONFIG_NET
60 extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
61 #else
62 static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
63 {
64         /* There is nothing to copy so this is a noop */
65         return net_ns;
66 }
67 #endif
68
69 #ifdef CONFIG_NET_NS
70 extern void __put_net(struct net *net);
71
72 static inline struct net *get_net(struct net *net)
73 {
74         atomic_inc(&net->count);
75         return net;
76 }
77
78 static inline struct net *maybe_get_net(struct net *net)
79 {
80         /* Used when we know struct net exists but we
81          * aren't guaranteed a previous reference count
82          * exists.  If the reference count is zero this
83          * function fails and returns NULL.
84          */
85         if (!atomic_inc_not_zero(&net->count))
86                 net = NULL;
87         return net;
88 }
89
90 static inline void put_net(struct net *net)
91 {
92         if (atomic_dec_and_test(&net->count))
93                 __put_net(net);
94 }
95
96 static inline struct net *hold_net(struct net *net)
97 {
98         atomic_inc(&net->use_count);
99         return net;
100 }
101
102 static inline void release_net(struct net *net)
103 {
104         atomic_dec(&net->use_count);
105 }
106 #else
107 static inline struct net *get_net(struct net *net)
108 {
109         return net;
110 }
111
112 static inline void put_net(struct net *net)
113 {
114 }
115
116 static inline struct net *hold_net(struct net *net)
117 {
118         return net;
119 }
120
121 static inline void release_net(struct net *net)
122 {
123 }
124
125 static inline struct net *maybe_get_net(struct net *net)
126 {
127         return net;
128 }
129 #endif
130
131 #define for_each_net(VAR)                               \
132         list_for_each_entry(VAR, &net_namespace_list, list)
133
134 #ifdef CONFIG_NET_NS
135 #define __net_init
136 #define __net_exit
137 #define __net_initdata
138 #else
139 #define __net_init      __init
140 #define __net_exit      __exit_refok
141 #define __net_initdata  __initdata
142 #endif
143
144 struct pernet_operations {
145         struct list_head list;
146         int (*init)(struct net *net);
147         void (*exit)(struct net *net);
148 };
149
150 extern int register_pernet_subsys(struct pernet_operations *);
151 extern void unregister_pernet_subsys(struct pernet_operations *);
152 extern int register_pernet_device(struct pernet_operations *);
153 extern void unregister_pernet_device(struct pernet_operations *);
154
155 struct ctl_path;
156 struct ctl_table;
157 struct ctl_table_header;
158 extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
159         const struct ctl_path *path, struct ctl_table *table);
160 extern void unregister_net_sysctl_table(struct ctl_table_header *header);
161
162 #endif /* __NET_NET_NAMESPACE_H */