blob: f97b2a4469ae2f46339b76fb9171bd2fc529583e [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
40 /* List of all packet sockets. */
41 rwlock_t packet_sklist_lock;
42 struct hlist_head packet_sklist;
Pavel Emelyanovd392e492007-12-01 23:44:15 +110043
44 /* unix sockets */
45 int sysctl_unix_max_dgram_qlen;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +110046 struct ctl_table_header *unix_ctl;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020047};
48
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020049#ifdef CONFIG_NET
50/* Init's network namespace */
Eric W. Biederman5f256be2007-09-12 11:50:50 +020051extern struct net init_net;
Daniel Lezcano4fabcd72007-09-13 09:16:29 +020052#define INIT_NET_NS(net_ns) .net_ns = &init_net,
53#else
54#define INIT_NET_NS(net_ns)
55#endif
56
Eric W. Biederman5f256be2007-09-12 11:50:50 +020057extern struct list_head net_namespace_list;
58
Eric W. Biederman9dd776b2007-09-26 22:04:26 -070059#ifdef CONFIG_NET
60extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
61#else
62static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
63{
64 /* There is nothing to copy so this is a noop */
65 return net_ns;
66}
67#endif
68
Pavel Emelyanovd4655792007-11-01 00:43:49 -070069#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020070extern void __put_net(struct net *net);
71
72static inline struct net *get_net(struct net *net)
73{
74 atomic_inc(&net->count);
75 return net;
76}
77
Eric W. Biederman077130c2007-09-13 09:18:57 +020078static inline struct net *maybe_get_net(struct net *net)
79{
80 /* Used when we know struct net exists but we
81 * aren't guaranteed a previous reference count
82 * exists. If the reference count is zero this
83 * function fails and returns NULL.
84 */
85 if (!atomic_inc_not_zero(&net->count))
86 net = NULL;
87 return net;
88}
89
Eric W. Biederman5f256be2007-09-12 11:50:50 +020090static inline void put_net(struct net *net)
91{
92 if (atomic_dec_and_test(&net->count))
93 __put_net(net);
94}
95
96static inline struct net *hold_net(struct net *net)
97{
98 atomic_inc(&net->use_count);
99 return net;
100}
101
102static inline void release_net(struct net *net)
103{
104 atomic_dec(&net->use_count);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200105}
Pavel Emelyanovd4655792007-11-01 00:43:49 -0700106#else
107static inline struct net *get_net(struct net *net)
108{
109 return net;
110}
111
112static inline void put_net(struct net *net)
113{
114}
115
116static inline struct net *hold_net(struct net *net)
117{
118 return net;
119}
120
121static inline void release_net(struct net *net)
122{
123}
124
125static inline struct net *maybe_get_net(struct net *net)
126{
127 return net;
128}
129#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200130
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200131#define for_each_net(VAR) \
132 list_for_each_entry(VAR, &net_namespace_list, list)
133
Pavel Emelyanov46650792007-10-08 20:38:39 -0700134#ifdef CONFIG_NET_NS
135#define __net_init
136#define __net_exit
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800137#define __net_initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700138#else
139#define __net_init __init
140#define __net_exit __exit_refok
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800141#define __net_initdata __initdata
Pavel Emelyanov46650792007-10-08 20:38:39 -0700142#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200143
144struct pernet_operations {
145 struct list_head list;
146 int (*init)(struct net *net);
147 void (*exit)(struct net *net);
148};
149
150extern int register_pernet_subsys(struct pernet_operations *);
151extern void unregister_pernet_subsys(struct pernet_operations *);
152extern int register_pernet_device(struct pernet_operations *);
153extern void unregister_pernet_device(struct pernet_operations *);
154
Eric W. Biederman95bdfcc2007-11-30 23:55:42 +1100155struct ctl_path;
156struct ctl_table;
157struct ctl_table_header;
158extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
159 const struct ctl_path *path, struct ctl_table *table);
160extern void unregister_net_sysctl_table(struct ctl_table_header *header);
161
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200162#endif /* __NET_NET_NAMESPACE_H */