blob: d5936115d97230c2a39009bbd269fd33827b4868 [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;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +110014struct ctl_table_header;
15
Eric W. Biederman5f256be2007-09-12 11:50:50 +020016struct 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 */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020025
26 struct proc_dir_entry *proc_net;
27 struct proc_dir_entry *proc_net_stat;
28 struct proc_dir_entry *proc_net_root;
Eric W. Biederman881d9662007-09-17 11:56:21 -070029
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +110030 struct list_head sysctl_table_headers;
31
Eric W. Biederman2774c7a2007-09-26 22:10:56 -070032 struct net_device *loopback_dev; /* The loopback */
33
Eric W. Biederman881d9662007-09-17 11:56:21 -070034 struct list_head dev_base_head;
35 struct hlist_head *dev_name_head;
36 struct hlist_head *dev_index_head;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -080037
38 struct sock *rtnl; /* rtnetlink socket */
Denis V. Lunevd12d01d2007-11-19 22:28:35 -080039
Pavel Emelyanov024626e2007-12-08 00:09:24 -080040 /* core sysctls */
41 struct ctl_table_header *sysctl_core_hdr;
42
Denis V. Lunevd12d01d2007-11-19 22:28:35 -080043 /* List of all packet sockets. */
44 rwlock_t packet_sklist_lock;
45 struct hlist_head packet_sklist;
Pavel Emelyanovd392e492007-12-01 23:44:15 +110046
47 /* unix sockets */
48 int sysctl_unix_max_dgram_qlen;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +110049 struct ctl_table_header *unix_ctl;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020050};
51
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020052#ifdef CONFIG_NET
53/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020054extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020055#define INIT_NET_NS(net_ns) .net_ns = &init_net,
56#else
57#define INIT_NET_NS(net_ns)
58#endif
59
Eric W. Biederman5f256be2007-09-12 11:50:50 +020060extern struct list_head net_namespace_list;
61
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070062#ifdef CONFIG_NET
63extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
64#else
65static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
66{
67 /* There is nothing to copy so this is a noop */
68 return net_ns;
69}
70#endif
71
Pavel Emelyanovd4655792007-11-01 00:43:49 -070072#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020073extern void __put_net(struct net *net);
74
75static inline struct net *get_net(struct net *net)
76{
77 atomic_inc(&net->count);
78 return net;
79}
80
Eric W. Biederman077130c2007-09-13 09:18:57 +020081static inline struct net *maybe_get_net(struct net *net)
82{
83 /* Used when we know struct net exists but we
84 * aren't guaranteed a previous reference count
85 * exists. If the reference count is zero this
86 * function fails and returns NULL.
87 */
88 if (!atomic_inc_not_zero(&net->count))
89 net = NULL;
90 return net;
91}
92
Eric W. Biederman5f256be2007-09-12 11:50:50 +020093static inline void put_net(struct net *net)
94{
95 if (atomic_dec_and_test(&net->count))
96 __put_net(net);
97}
98
99static inline struct net *hold_net(struct net *net)
100{
101 atomic_inc(&net->use_count);
102 return net;
103}
104
105static inline void release_net(struct net *net)
106{
107 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200108}
Pavel Emelyanovd4655792007-11-01 00:43:49 -0700109#else
110static inline struct net *get_net(struct net *net)
111{
112 return net;
113}
114
115static inline void put_net(struct net *net)
116{
117}
118
119static inline struct net *hold_net(struct net *net)
120{
121 return net;
122}
123
124static inline void release_net(struct net *net)
125{
126}
127
128static inline struct net *maybe_get_net(struct net *net)
129{
130 return net;
131}
132#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200133
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200134#define for_each_net(VAR) \
135 list_for_each_entry(VAR, &net_namespace_list, list)
136
Pavel Emelyanov46650792007-10-08 20:38:39 -0700137#ifdef CONFIG_NET_NS
138#define __net_init
139#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800140#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700141#else
142#define __net_init __init
143#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800144#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700145#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200146
147struct pernet_operations {
148 struct list_head list;
149 int (*init)(struct net *net);
150 void (*exit)(struct net *net);
151};
152
153extern int register_pernet_subsys(struct pernet_operations *);
154extern void unregister_pernet_subsys(struct pernet_operations *);
155extern int register_pernet_device(struct pernet_operations *);
156extern void unregister_pernet_device(struct pernet_operations *);
157
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +1100158struct ctl_path;
159struct ctl_table;
160struct ctl_table_header;
161extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
162 const struct ctl_path *path, struct ctl_table *table);
163extern void unregister_net_sysctl_table(struct ctl_table_header *header);
164
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200165#endif /* __NET_NET_NAMESPACE_H */