blob: b0cf07519b81eb4f1ac71d674fc56d817b51ec3c [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. Biederman95bdfcc2007-11-30 23:55:42 +110028 struct list_head sysctl_table_headers;
29
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070030 struct net_device *loopback_dev; /* The loopback */
31
Eric W. Biederman881d9662007-09-17 11:56:21 -070032 struct list_head dev_base_head;
33 struct hlist_head *dev_name_head;
34 struct hlist_head *dev_index_head;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080035
36 struct sock *rtnl; /* rtnetlink socket */
Denis V. Lunevd12d01d2007-11-19 22:28:35 -080037
38 /* List of all packet sockets. */
39 rwlock_t packet_sklist_lock;
40 struct hlist_head packet_sklist;
Pavel Emelyanovd392e492007-12-01 23:44:15 +110041
42 /* unix sockets */
43 int sysctl_unix_max_dgram_qlen;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020044};
45
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020046#ifdef CONFIG_NET
47/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020048extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020049#define INIT_NET_NS(net_ns) .net_ns = &init_net,
50#else
51#define INIT_NET_NS(net_ns)
52#endif
53
Eric W. Biederman5f256be2007-09-12 11:50:50 +020054extern struct list_head net_namespace_list;
55
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070056#ifdef CONFIG_NET
57extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
58#else
59static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
60{
61 /* There is nothing to copy so this is a noop */
62 return net_ns;
63}
64#endif
65
Pavel Emelyanovd4655792007-11-01 00:43:49 -070066#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020067extern void __put_net(struct net *net);
68
69static inline struct net *get_net(struct net *net)
70{
71 atomic_inc(&net->count);
72 return net;
73}
74
Eric W. Biederman077130c2007-09-13 09:18:57 +020075static inline struct net *maybe_get_net(struct net *net)
76{
77 /* Used when we know struct net exists but we
78 * aren't guaranteed a previous reference count
79 * exists. If the reference count is zero this
80 * function fails and returns NULL.
81 */
82 if (!atomic_inc_not_zero(&net->count))
83 net = NULL;
84 return net;
85}
86
Eric W. Biederman5f256be2007-09-12 11:50:50 +020087static inline void put_net(struct net *net)
88{
89 if (atomic_dec_and_test(&net->count))
90 __put_net(net);
91}
92
93static inline struct net *hold_net(struct net *net)
94{
95 atomic_inc(&net->use_count);
96 return net;
97}
98
99static inline void release_net(struct net *net)
100{
101 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200102}
Pavel Emelyanovd4655792007-11-01 00:43:49 -0700103#else
104static inline struct net *get_net(struct net *net)
105{
106 return net;
107}
108
109static inline void put_net(struct net *net)
110{
111}
112
113static inline struct net *hold_net(struct net *net)
114{
115 return net;
116}
117
118static inline void release_net(struct net *net)
119{
120}
121
122static inline struct net *maybe_get_net(struct net *net)
123{
124 return net;
125}
126#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200127
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200128#define for_each_net(VAR) \
129 list_for_each_entry(VAR, &net_namespace_list, list)
130
Pavel Emelyanov46650792007-10-08 20:38:39 -0700131#ifdef CONFIG_NET_NS
132#define __net_init
133#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800134#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700135#else
136#define __net_init __init
137#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800138#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700139#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200140
141struct pernet_operations {
142 struct list_head list;
143 int (*init)(struct net *net);
144 void (*exit)(struct net *net);
145};
146
147extern int register_pernet_subsys(struct pernet_operations *);
148extern void unregister_pernet_subsys(struct pernet_operations *);
149extern int register_pernet_device(struct pernet_operations *);
150extern void unregister_pernet_device(struct pernet_operations *);
151
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +1100152struct ctl_path;
153struct ctl_table;
154struct ctl_table_header;
155extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
156 const struct ctl_path *path, struct ctl_table *table);
157extern void unregister_net_sysctl_table(struct ctl_table_header *header);
158
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200159#endif /* __NET_NET_NAMESPACE_H */