blob: 18da0af6192f2a667f66731f797435d34cecc539 [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
Denis V. Luneva0a53c82007-12-11 04:19:17 -080011#include <net/netns/unix.h>
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -080012#include <net/netns/packet.h>
Denis V. Luneva0a53c82007-12-11 04:19:17 -080013
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020014struct proc_dir_entry;
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070015struct net_device;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080016struct sock;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +110017struct ctl_table_header;
18
Eric W. Biederman5f256be2007-09-12 11:50:50 +020019struct net {
20 atomic_t count; /* To decided when the network
21 * namespace should be freed.
22 */
23 atomic_t use_count; /* To track references we
24 * destroy on demand
25 */
26 struct list_head list; /* list of network namespaces */
27 struct work_struct work; /* work struct for freeing */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028
29 struct proc_dir_entry *proc_net;
30 struct proc_dir_entry *proc_net_stat;
31 struct proc_dir_entry *proc_net_root;
Eric W. Biederman881d9662007-09-17 11:56:21 -070032
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +110033 struct list_head sysctl_table_headers;
34
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070035 struct net_device *loopback_dev; /* The loopback */
36
Eric W. Biederman881d9662007-09-17 11:56:21 -070037 struct list_head dev_base_head;
38 struct hlist_head *dev_name_head;
39 struct hlist_head *dev_index_head;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080040
41 struct sock *rtnl; /* rtnetlink socket */
Denis V. Lunevd12d01d2007-11-19 22:28:35 -080042
Pavel Emelyanov024626e2007-12-08 00:09:24 -080043 /* core sysctls */
44 struct ctl_table_header *sysctl_core_hdr;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -080045 int sysctl_somaxconn;
Pavel Emelyanov024626e2007-12-08 00:09:24 -080046
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -080047 struct netns_packet packet;
Denis V. Luneva0a53c82007-12-11 04:19:17 -080048 struct netns_unix unx;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020049};
50
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020051#ifdef CONFIG_NET
52/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020053extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020054#define INIT_NET_NS(net_ns) .net_ns = &init_net,
55#else
56#define INIT_NET_NS(net_ns)
57#endif
58
Eric W. Biederman5f256be2007-09-12 11:50:50 +020059extern struct list_head net_namespace_list;
60
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070061#ifdef CONFIG_NET
62extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
63#else
64static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
65{
66 /* There is nothing to copy so this is a noop */
67 return net_ns;
68}
69#endif
70
Pavel Emelyanovd4655792007-11-01 00:43:49 -070071#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020072extern void __put_net(struct net *net);
73
74static inline struct net *get_net(struct net *net)
75{
76 atomic_inc(&net->count);
77 return net;
78}
79
Eric W. Biederman077130c2007-09-13 09:18:57 +020080static inline struct net *maybe_get_net(struct net *net)
81{
82 /* Used when we know struct net exists but we
83 * aren't guaranteed a previous reference count
84 * exists. If the reference count is zero this
85 * function fails and returns NULL.
86 */
87 if (!atomic_inc_not_zero(&net->count))
88 net = NULL;
89 return net;
90}
91
Eric W. Biederman5f256be2007-09-12 11:50:50 +020092static inline void put_net(struct net *net)
93{
94 if (atomic_dec_and_test(&net->count))
95 __put_net(net);
96}
97
98static inline struct net *hold_net(struct net *net)
99{
100 atomic_inc(&net->use_count);
101 return net;
102}
103
104static inline void release_net(struct net *net)
105{
106 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200107}
Pavel Emelyanovd4655792007-11-01 00:43:49 -0700108#else
109static inline struct net *get_net(struct net *net)
110{
111 return net;
112}
113
114static inline void put_net(struct net *net)
115{
116}
117
118static inline struct net *hold_net(struct net *net)
119{
120 return net;
121}
122
123static inline void release_net(struct net *net)
124{
125}
126
127static inline struct net *maybe_get_net(struct net *net)
128{
129 return net;
130}
131#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200132
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200133#define for_each_net(VAR) \
134 list_for_each_entry(VAR, &net_namespace_list, list)
135
Pavel Emelyanov46650792007-10-08 20:38:39 -0700136#ifdef CONFIG_NET_NS
137#define __net_init
138#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800139#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700140#else
141#define __net_init __init
142#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800143#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700144#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200145
146struct pernet_operations {
147 struct list_head list;
148 int (*init)(struct net *net);
149 void (*exit)(struct net *net);
150};
151
152extern int register_pernet_subsys(struct pernet_operations *);
153extern void unregister_pernet_subsys(struct pernet_operations *);
154extern int register_pernet_device(struct pernet_operations *);
155extern void unregister_pernet_device(struct pernet_operations *);
156
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +1100157struct ctl_path;
158struct ctl_table;
159struct ctl_table_header;
160extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
161 const struct ctl_path *path, struct ctl_table *table);
162extern void unregister_net_sysctl_table(struct ctl_table_header *header);
163
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200164#endif /* __NET_NET_NAMESPACE_H */