Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1 | /* |
| 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. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 11 | struct proc_dir_entry; |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 12 | struct net_device; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 13 | struct net { |
| 14 | atomic_t count; /* To decided when the network |
| 15 | * namespace should be freed. |
| 16 | */ |
| 17 | atomic_t use_count; /* To track references we |
| 18 | * destroy on demand |
| 19 | */ |
| 20 | struct list_head list; /* list of network namespaces */ |
| 21 | struct work_struct work; /* work struct for freeing */ |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 22 | |
| 23 | struct proc_dir_entry *proc_net; |
| 24 | struct proc_dir_entry *proc_net_stat; |
| 25 | struct proc_dir_entry *proc_net_root; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 26 | |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 27 | struct net_device *loopback_dev; /* The loopback */ |
| 28 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 29 | struct list_head dev_base_head; |
| 30 | struct hlist_head *dev_name_head; |
| 31 | struct hlist_head *dev_index_head; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Daniel Lezcano | 4fabcd7 | 2007-09-13 09:16:29 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_NET |
| 35 | /* Init's network namespace */ |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 36 | extern struct net init_net; |
Daniel Lezcano | 4fabcd7 | 2007-09-13 09:16:29 +0200 | [diff] [blame] | 37 | #define INIT_NET_NS(net_ns) .net_ns = &init_net, |
| 38 | #else |
| 39 | #define INIT_NET_NS(net_ns) |
| 40 | #endif |
| 41 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 42 | extern struct list_head net_namespace_list; |
| 43 | |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_NET |
| 45 | extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns); |
| 46 | #else |
| 47 | static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns) |
| 48 | { |
| 49 | /* There is nothing to copy so this is a noop */ |
| 50 | return net_ns; |
| 51 | } |
| 52 | #endif |
| 53 | |
Pavel Emelyanov | d465579 | 2007-11-01 00:43:49 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 55 | extern void __put_net(struct net *net); |
| 56 | |
| 57 | static inline struct net *get_net(struct net *net) |
| 58 | { |
| 59 | atomic_inc(&net->count); |
| 60 | return net; |
| 61 | } |
| 62 | |
Eric W. Biederman | 077130c | 2007-09-13 09:18:57 +0200 | [diff] [blame] | 63 | static inline struct net *maybe_get_net(struct net *net) |
| 64 | { |
| 65 | /* Used when we know struct net exists but we |
| 66 | * aren't guaranteed a previous reference count |
| 67 | * exists. If the reference count is zero this |
| 68 | * function fails and returns NULL. |
| 69 | */ |
| 70 | if (!atomic_inc_not_zero(&net->count)) |
| 71 | net = NULL; |
| 72 | return net; |
| 73 | } |
| 74 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 75 | static inline void put_net(struct net *net) |
| 76 | { |
| 77 | if (atomic_dec_and_test(&net->count)) |
| 78 | __put_net(net); |
| 79 | } |
| 80 | |
| 81 | static inline struct net *hold_net(struct net *net) |
| 82 | { |
| 83 | atomic_inc(&net->use_count); |
| 84 | return net; |
| 85 | } |
| 86 | |
| 87 | static inline void release_net(struct net *net) |
| 88 | { |
| 89 | atomic_dec(&net->use_count); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 90 | } |
Pavel Emelyanov | d465579 | 2007-11-01 00:43:49 -0700 | [diff] [blame] | 91 | #else |
| 92 | static inline struct net *get_net(struct net *net) |
| 93 | { |
| 94 | return net; |
| 95 | } |
| 96 | |
| 97 | static inline void put_net(struct net *net) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | static inline struct net *hold_net(struct net *net) |
| 102 | { |
| 103 | return net; |
| 104 | } |
| 105 | |
| 106 | static inline void release_net(struct net *net) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | static inline struct net *maybe_get_net(struct net *net) |
| 111 | { |
| 112 | return net; |
| 113 | } |
| 114 | #endif |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 115 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 116 | #define for_each_net(VAR) \ |
| 117 | list_for_each_entry(VAR, &net_namespace_list, list) |
| 118 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 119 | #ifdef CONFIG_NET_NS |
| 120 | #define __net_init |
| 121 | #define __net_exit |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 122 | #define __net_initdata |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 123 | #else |
| 124 | #define __net_init __init |
| 125 | #define __net_exit __exit_refok |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 126 | #define __net_initdata __initdata |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 127 | #endif |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 128 | |
| 129 | struct pernet_operations { |
| 130 | struct list_head list; |
| 131 | int (*init)(struct net *net); |
| 132 | void (*exit)(struct net *net); |
| 133 | }; |
| 134 | |
| 135 | extern int register_pernet_subsys(struct pernet_operations *); |
| 136 | extern void unregister_pernet_subsys(struct pernet_operations *); |
| 137 | extern int register_pernet_device(struct pernet_operations *); |
| 138 | extern void unregister_pernet_device(struct pernet_operations *); |
| 139 | |
| 140 | #endif /* __NET_NET_NAMESPACE_H */ |