blob: 3ea4194613edfa434f18c903510733b4ba4cc329 [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. Biederman5f256be2007-09-12 11:50:50 +020012struct net {
13 atomic_t count; /* To decided when the network
14 * namespace should be freed.
15 */
16 atomic_t use_count; /* To track references we
17 * destroy on demand
18 */
19 struct list_head list; /* list of network namespaces */
20 struct work_struct work; /* work struct for freeing */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020021
22 struct proc_dir_entry *proc_net;
23 struct proc_dir_entry *proc_net_stat;
24 struct proc_dir_entry *proc_net_root;
Eric W. Biederman881d9662007-09-17 11:56:21 -070025
26 struct list_head dev_base_head;
27 struct hlist_head *dev_name_head;
28 struct hlist_head *dev_index_head;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020029};
30
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020031#ifdef CONFIG_NET
32/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020033extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020034#define INIT_NET_NS(net_ns) .net_ns = &init_net,
35#else
36#define INIT_NET_NS(net_ns)
37#endif
38
Eric W. Biederman5f256be2007-09-12 11:50:50 +020039extern struct list_head net_namespace_list;
40
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070041#ifdef CONFIG_NET
42extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
43#else
44static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
45{
46 /* There is nothing to copy so this is a noop */
47 return net_ns;
48}
49#endif
50
Eric W. Biederman5f256be2007-09-12 11:50:50 +020051extern void __put_net(struct net *net);
52
53static inline struct net *get_net(struct net *net)
54{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070055#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020056 atomic_inc(&net->count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070057#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020058 return net;
59}
60
Eric W. Biederman077130c2007-09-13 09:18:57 +020061static inline struct net *maybe_get_net(struct net *net)
62{
63 /* Used when we know struct net exists but we
64 * aren't guaranteed a previous reference count
65 * exists. If the reference count is zero this
66 * function fails and returns NULL.
67 */
68 if (!atomic_inc_not_zero(&net->count))
69 net = NULL;
70 return net;
71}
72
Eric W. Biederman5f256be2007-09-12 11:50:50 +020073static inline void put_net(struct net *net)
74{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070075#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020076 if (atomic_dec_and_test(&net->count))
77 __put_net(net);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070078#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020079}
80
81static inline struct net *hold_net(struct net *net)
82{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070083#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020084 atomic_inc(&net->use_count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070085#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020086 return net;
87}
88
89static inline void release_net(struct net *net)
90{
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070091#ifdef CONFIG_NET
Eric W. Biederman5f256be2007-09-12 11:50:50 +020092 atomic_dec(&net->use_count);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070093#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020094}
95
96extern void net_lock(void);
97extern void net_unlock(void);
98
99#define for_each_net(VAR) \
100 list_for_each_entry(VAR, &net_namespace_list, list)
101
102
103struct pernet_operations {
104 struct list_head list;
105 int (*init)(struct net *net);
106 void (*exit)(struct net *net);
107};
108
109extern int register_pernet_subsys(struct pernet_operations *);
110extern void unregister_pernet_subsys(struct pernet_operations *);
111extern int register_pernet_device(struct pernet_operations *);
112extern void unregister_pernet_device(struct pernet_operations *);
113
114#endif /* __NET_NET_NAMESPACE_H */