blob: b62e31fca4742543a3a9df86841305d9e7faad53 [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;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -080042 int sysctl_somaxconn;
Pavel Emelyanov024626e2007-12-08 00:09:24 -080043
Denis V. Lunevd12d01d2007-11-19 22:28:35 -080044 /* List of all packet sockets. */
45 rwlock_t packet_sklist_lock;
46 struct hlist_head packet_sklist;
Pavel Emelyanovd392e492007-12-01 23:44:15 +110047
48 /* unix sockets */
49 int sysctl_unix_max_dgram_qlen;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +110050 struct ctl_table_header *unix_ctl;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020051};
52
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020053#ifdef CONFIG_NET
54/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020055extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020056#define INIT_NET_NS(net_ns) .net_ns = &init_net,
57#else
58#define INIT_NET_NS(net_ns)
59#endif
60
Eric W. Biederman5f256be2007-09-12 11:50:50 +020061extern struct list_head net_namespace_list;
62
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070063#ifdef CONFIG_NET
64extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
65#else
66static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
67{
68 /* There is nothing to copy so this is a noop */
69 return net_ns;
70}
71#endif
72
Pavel Emelyanovd4655792007-11-01 00:43:49 -070073#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020074extern void __put_net(struct net *net);
75
76static inline struct net *get_net(struct net *net)
77{
78 atomic_inc(&net->count);
79 return net;
80}
81
Eric W. Biederman077130c2007-09-13 09:18:57 +020082static inline struct net *maybe_get_net(struct net *net)
83{
84 /* Used when we know struct net exists but we
85 * aren't guaranteed a previous reference count
86 * exists. If the reference count is zero this
87 * function fails and returns NULL.
88 */
89 if (!atomic_inc_not_zero(&net->count))
90 net = NULL;
91 return net;
92}
93
Eric W. Biederman5f256be2007-09-12 11:50:50 +020094static inline void put_net(struct net *net)
95{
96 if (atomic_dec_and_test(&net->count))
97 __put_net(net);
98}
99
100static inline struct net *hold_net(struct net *net)
101{
102 atomic_inc(&net->use_count);
103 return net;
104}
105
106static inline void release_net(struct net *net)
107{
108 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200109}
Pavel Emelyanovd4655792007-11-01 00:43:49 -0700110#else
111static inline struct net *get_net(struct net *net)
112{
113 return net;
114}
115
116static inline void put_net(struct net *net)
117{
118}
119
120static inline struct net *hold_net(struct net *net)
121{
122 return net;
123}
124
125static inline void release_net(struct net *net)
126{
127}
128
129static inline struct net *maybe_get_net(struct net *net)
130{
131 return net;
132}
133#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200134
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200135#define for_each_net(VAR) \
136 list_for_each_entry(VAR, &net_namespace_list, list)
137
Pavel Emelyanov46650792007-10-08 20:38:39 -0700138#ifdef CONFIG_NET_NS
139#define __net_init
140#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800141#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700142#else
143#define __net_init __init
144#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800145#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700146#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200147
148struct pernet_operations {
149 struct list_head list;
150 int (*init)(struct net *net);
151 void (*exit)(struct net *net);
152};
153
154extern int register_pernet_subsys(struct pernet_operations *);
155extern void unregister_pernet_subsys(struct pernet_operations *);
156extern int register_pernet_device(struct pernet_operations *);
157extern void unregister_pernet_device(struct pernet_operations *);
158
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +1100159struct ctl_path;
160struct ctl_table;
161struct ctl_table_header;
162extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
163 const struct ctl_path *path, struct ctl_table *table);
164extern void unregister_net_sysctl_table(struct ctl_table_header *header);
165
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200166#endif /* __NET_NET_NAMESPACE_H */