blob: 90802a668c235f20f9b40f63db4b001d7123de3b [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;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080013struct sock;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020014struct net {
15 atomic_t count; /* To decided when the network
16 * namespace should be freed.
17 */
18 atomic_t use_count; /* To track references we
19 * destroy on demand
20 */
21 struct list_head list; /* list of network namespaces */
22 struct work_struct work; /* work struct for freeing */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020023
24 struct proc_dir_entry *proc_net;
25 struct proc_dir_entry *proc_net_stat;
26 struct proc_dir_entry *proc_net_root;
Eric W. Biederman881d9662007-09-17 11:56:21 -070027
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070028 struct net_device *loopback_dev; /* The loopback */
29
Eric W. Biederman881d9662007-09-17 11:56:21 -070030 struct list_head dev_base_head;
31 struct hlist_head *dev_name_head;
32 struct hlist_head *dev_index_head;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080033
34 struct sock *rtnl; /* rtnetlink socket */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020035};
36
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020037#ifdef CONFIG_NET
38/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020039extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020040#define INIT_NET_NS(net_ns) .net_ns = &init_net,
41#else
42#define INIT_NET_NS(net_ns)
43#endif
44
Eric W. Biederman5f256be2007-09-12 11:50:50 +020045extern struct list_head net_namespace_list;
46
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070047#ifdef CONFIG_NET
48extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
49#else
50static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
51{
52 /* There is nothing to copy so this is a noop */
53 return net_ns;
54}
55#endif
56
Pavel Emelyanovd4655792007-11-01 00:43:49 -070057#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020058extern void __put_net(struct net *net);
59
60static inline struct net *get_net(struct net *net)
61{
62 atomic_inc(&net->count);
63 return net;
64}
65
Eric W. Biederman077130c2007-09-13 09:18:57 +020066static inline struct net *maybe_get_net(struct net *net)
67{
68 /* Used when we know struct net exists but we
69 * aren't guaranteed a previous reference count
70 * exists. If the reference count is zero this
71 * function fails and returns NULL.
72 */
73 if (!atomic_inc_not_zero(&net->count))
74 net = NULL;
75 return net;
76}
77
Eric W. Biederman5f256be2007-09-12 11:50:50 +020078static inline void put_net(struct net *net)
79{
80 if (atomic_dec_and_test(&net->count))
81 __put_net(net);
82}
83
84static inline struct net *hold_net(struct net *net)
85{
86 atomic_inc(&net->use_count);
87 return net;
88}
89
90static inline void release_net(struct net *net)
91{
92 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020093}
Pavel Emelyanovd4655792007-11-01 00:43:49 -070094#else
95static inline struct net *get_net(struct net *net)
96{
97 return net;
98}
99
100static inline void put_net(struct net *net)
101{
102}
103
104static inline struct net *hold_net(struct net *net)
105{
106 return net;
107}
108
109static inline void release_net(struct net *net)
110{
111}
112
113static inline struct net *maybe_get_net(struct net *net)
114{
115 return net;
116}
117#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200118
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200119#define for_each_net(VAR) \
120 list_for_each_entry(VAR, &net_namespace_list, list)
121
Pavel Emelyanov46650792007-10-08 20:38:39 -0700122#ifdef CONFIG_NET_NS
123#define __net_init
124#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800125#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700126#else
127#define __net_init __init
128#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800129#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700130#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200131
132struct pernet_operations {
133 struct list_head list;
134 int (*init)(struct net *net);
135 void (*exit)(struct net *net);
136};
137
138extern int register_pernet_subsys(struct pernet_operations *);
139extern void unregister_pernet_subsys(struct pernet_operations *);
140extern int register_pernet_device(struct pernet_operations *);
141extern void unregister_pernet_device(struct pernet_operations *);
142
143#endif /* __NET_NET_NAMESPACE_H */