Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Operations on the network namespace |
| 4 | */ |
| 5 | #ifndef __NET_NET_NAMESPACE_H |
| 6 | #define __NET_NET_NAMESPACE_H |
| 7 | |
| 8 | #include <linux/atomic.h> |
| 9 | #include <linux/refcount.h> |
| 10 | #include <linux/workqueue.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/sysctl.h> |
| 13 | #include <linux/uidgid.h> |
| 14 | |
| 15 | #include <net/flow.h> |
| 16 | #include <net/netns/core.h> |
| 17 | #include <net/netns/mib.h> |
| 18 | #include <net/netns/unix.h> |
| 19 | #include <net/netns/packet.h> |
| 20 | #include <net/netns/ipv4.h> |
| 21 | #include <net/netns/ipv6.h> |
| 22 | #include <net/netns/ieee802154_6lowpan.h> |
| 23 | #include <net/netns/sctp.h> |
| 24 | #include <net/netns/dccp.h> |
| 25 | #include <net/netns/netfilter.h> |
| 26 | #include <net/netns/x_tables.h> |
| 27 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 28 | #include <net/netns/conntrack.h> |
| 29 | #endif |
| 30 | #include <net/netns/nftables.h> |
| 31 | #include <net/netns/xfrm.h> |
| 32 | #include <net/netns/mpls.h> |
| 33 | #include <net/netns/can.h> |
| 34 | #include <linux/ns_common.h> |
| 35 | #include <linux/idr.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | |
| 38 | struct user_namespace; |
| 39 | struct proc_dir_entry; |
| 40 | struct net_device; |
| 41 | struct sock; |
| 42 | struct ctl_table_header; |
| 43 | struct net_generic; |
| 44 | struct uevent_sock; |
| 45 | struct netns_ipvs; |
| 46 | |
| 47 | |
| 48 | #define NETDEV_HASHBITS 8 |
| 49 | #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) |
| 50 | |
| 51 | struct net { |
| 52 | refcount_t passive; /* To decided when the network |
| 53 | * namespace should be freed. |
| 54 | */ |
| 55 | refcount_t count; /* To decided when the network |
| 56 | * namespace should be shut down. |
| 57 | */ |
| 58 | spinlock_t rules_mod_lock; |
| 59 | |
| 60 | atomic64_t cookie_gen; |
| 61 | |
| 62 | struct list_head list; /* list of network namespaces */ |
| 63 | struct list_head exit_list; /* To linked to call pernet exit |
| 64 | * methods on dead net ( |
| 65 | * pernet_ops_rwsem read locked), |
| 66 | * or to unregister pernet ops |
| 67 | * (pernet_ops_rwsem write locked). |
| 68 | */ |
| 69 | struct llist_node cleanup_list; /* namespaces on death row */ |
| 70 | |
| 71 | struct user_namespace *user_ns; /* Owning user namespace */ |
| 72 | struct ucounts *ucounts; |
| 73 | spinlock_t nsid_lock; |
| 74 | struct idr netns_ids; |
| 75 | |
| 76 | struct ns_common ns; |
| 77 | |
| 78 | struct proc_dir_entry *proc_net; |
| 79 | struct proc_dir_entry *proc_net_stat; |
| 80 | |
| 81 | #ifdef CONFIG_SYSCTL |
| 82 | struct ctl_table_set sysctls; |
| 83 | #endif |
| 84 | |
| 85 | struct sock *rtnl; /* rtnetlink socket */ |
| 86 | struct sock *genl_sock; |
| 87 | |
| 88 | struct uevent_sock *uevent_sock; /* uevent socket */ |
| 89 | |
| 90 | struct list_head dev_base_head; |
| 91 | struct hlist_head *dev_name_head; |
| 92 | struct hlist_head *dev_index_head; |
| 93 | unsigned int dev_base_seq; /* protected by rtnl_mutex */ |
| 94 | int ifindex; |
| 95 | unsigned int dev_unreg_count; |
| 96 | |
| 97 | /* core fib_rules */ |
| 98 | struct list_head rules_ops; |
| 99 | |
| 100 | struct list_head fib_notifier_ops; /* Populated by |
| 101 | * register_pernet_subsys() |
| 102 | */ |
| 103 | struct net_device *loopback_dev; /* The loopback */ |
| 104 | struct netns_core core; |
| 105 | struct netns_mib mib; |
| 106 | struct netns_packet packet; |
| 107 | struct netns_unix unx; |
| 108 | struct netns_ipv4 ipv4; |
| 109 | #if IS_ENABLED(CONFIG_IPV6) |
| 110 | struct netns_ipv6 ipv6; |
| 111 | #endif |
| 112 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) |
| 113 | struct netns_ieee802154_lowpan ieee802154_lowpan; |
| 114 | #endif |
| 115 | #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) |
| 116 | struct netns_sctp sctp; |
| 117 | #endif |
| 118 | #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) |
| 119 | struct netns_dccp dccp; |
| 120 | #endif |
| 121 | #ifdef CONFIG_NETFILTER |
| 122 | struct netns_nf nf; |
| 123 | struct netns_xt xt; |
| 124 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 125 | struct netns_ct ct; |
| 126 | #endif |
| 127 | #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) |
| 128 | struct netns_nftables nft; |
| 129 | #endif |
| 130 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
| 131 | struct netns_nf_frag nf_frag; |
| 132 | struct ctl_table_header *nf_frag_frags_hdr; |
| 133 | #endif |
| 134 | struct sock *nfnl; |
| 135 | struct sock *nfnl_stash; |
| 136 | #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT) |
| 137 | struct list_head nfnl_acct_list; |
| 138 | #endif |
| 139 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 140 | struct list_head nfct_timeout_list; |
| 141 | #endif |
| 142 | #endif |
| 143 | #ifdef CONFIG_WEXT_CORE |
| 144 | struct sk_buff_head wext_nlevents; |
| 145 | #endif |
| 146 | struct net_generic __rcu *gen; |
| 147 | |
| 148 | /* Note : following structs are cache line aligned */ |
| 149 | #ifdef CONFIG_XFRM |
| 150 | struct netns_xfrm xfrm; |
| 151 | #endif |
| 152 | #if IS_ENABLED(CONFIG_IP_VS) |
| 153 | struct netns_ipvs *ipvs; |
| 154 | #endif |
| 155 | #if IS_ENABLED(CONFIG_MPLS) |
| 156 | struct netns_mpls mpls; |
| 157 | #endif |
| 158 | #if IS_ENABLED(CONFIG_CAN) |
| 159 | struct netns_can can; |
| 160 | #endif |
| 161 | struct sock *diag_nlsk; |
| 162 | atomic_t fnhe_genid; |
| 163 | } __randomize_layout; |
| 164 | |
| 165 | #include <linux/seq_file_net.h> |
| 166 | |
| 167 | /* Init's network namespace */ |
| 168 | extern struct net init_net; |
| 169 | |
| 170 | #ifdef CONFIG_NET_NS |
| 171 | struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, |
| 172 | struct net *old_net); |
| 173 | |
| 174 | void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid); |
| 175 | |
| 176 | void net_ns_barrier(void); |
| 177 | #else /* CONFIG_NET_NS */ |
| 178 | #include <linux/sched.h> |
| 179 | #include <linux/nsproxy.h> |
| 180 | static inline struct net *copy_net_ns(unsigned long flags, |
| 181 | struct user_namespace *user_ns, struct net *old_net) |
| 182 | { |
| 183 | if (flags & CLONE_NEWNET) |
| 184 | return ERR_PTR(-EINVAL); |
| 185 | return old_net; |
| 186 | } |
| 187 | |
| 188 | static inline void net_ns_get_ownership(const struct net *net, |
| 189 | kuid_t *uid, kgid_t *gid) |
| 190 | { |
| 191 | *uid = GLOBAL_ROOT_UID; |
| 192 | *gid = GLOBAL_ROOT_GID; |
| 193 | } |
| 194 | |
| 195 | static inline void net_ns_barrier(void) {} |
| 196 | #endif /* CONFIG_NET_NS */ |
| 197 | |
| 198 | |
| 199 | extern struct list_head net_namespace_list; |
| 200 | |
| 201 | struct net *get_net_ns_by_pid(pid_t pid); |
| 202 | struct net *get_net_ns_by_fd(int fd); |
| 203 | |
| 204 | #ifdef CONFIG_SYSCTL |
| 205 | void ipx_register_sysctl(void); |
| 206 | void ipx_unregister_sysctl(void); |
| 207 | #else |
| 208 | #define ipx_register_sysctl() |
| 209 | #define ipx_unregister_sysctl() |
| 210 | #endif |
| 211 | |
| 212 | #ifdef CONFIG_NET_NS |
| 213 | void __put_net(struct net *net); |
| 214 | |
| 215 | static inline struct net *get_net(struct net *net) |
| 216 | { |
| 217 | refcount_inc(&net->count); |
| 218 | return net; |
| 219 | } |
| 220 | |
| 221 | static inline struct net *maybe_get_net(struct net *net) |
| 222 | { |
| 223 | /* Used when we know struct net exists but we |
| 224 | * aren't guaranteed a previous reference count |
| 225 | * exists. If the reference count is zero this |
| 226 | * function fails and returns NULL. |
| 227 | */ |
| 228 | if (!refcount_inc_not_zero(&net->count)) |
| 229 | net = NULL; |
| 230 | return net; |
| 231 | } |
| 232 | |
| 233 | static inline void put_net(struct net *net) |
| 234 | { |
| 235 | if (refcount_dec_and_test(&net->count)) |
| 236 | __put_net(net); |
| 237 | } |
| 238 | |
| 239 | static inline |
| 240 | int net_eq(const struct net *net1, const struct net *net2) |
| 241 | { |
| 242 | return net1 == net2; |
| 243 | } |
| 244 | |
| 245 | static inline int check_net(const struct net *net) |
| 246 | { |
| 247 | return refcount_read(&net->count) != 0; |
| 248 | } |
| 249 | |
| 250 | void net_drop_ns(void *); |
| 251 | |
| 252 | #else |
| 253 | |
| 254 | static inline struct net *get_net(struct net *net) |
| 255 | { |
| 256 | return net; |
| 257 | } |
| 258 | |
| 259 | static inline void put_net(struct net *net) |
| 260 | { |
| 261 | } |
| 262 | |
| 263 | static inline struct net *maybe_get_net(struct net *net) |
| 264 | { |
| 265 | return net; |
| 266 | } |
| 267 | |
| 268 | static inline |
| 269 | int net_eq(const struct net *net1, const struct net *net2) |
| 270 | { |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | static inline int check_net(const struct net *net) |
| 275 | { |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | #define net_drop_ns NULL |
| 280 | #endif |
| 281 | |
| 282 | |
| 283 | typedef struct { |
| 284 | #ifdef CONFIG_NET_NS |
| 285 | struct net *net; |
| 286 | #endif |
| 287 | } possible_net_t; |
| 288 | |
| 289 | static inline void write_pnet(possible_net_t *pnet, struct net *net) |
| 290 | { |
| 291 | #ifdef CONFIG_NET_NS |
| 292 | pnet->net = net; |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | static inline struct net *read_pnet(const possible_net_t *pnet) |
| 297 | { |
| 298 | #ifdef CONFIG_NET_NS |
| 299 | return pnet->net; |
| 300 | #else |
| 301 | return &init_net; |
| 302 | #endif |
| 303 | } |
| 304 | |
| 305 | /* Protected by net_rwsem */ |
| 306 | #define for_each_net(VAR) \ |
| 307 | list_for_each_entry(VAR, &net_namespace_list, list) |
| 308 | |
| 309 | #define for_each_net_rcu(VAR) \ |
| 310 | list_for_each_entry_rcu(VAR, &net_namespace_list, list) |
| 311 | |
| 312 | #ifdef CONFIG_NET_NS |
| 313 | #define __net_init |
| 314 | #define __net_exit |
| 315 | #define __net_initdata |
| 316 | #define __net_initconst |
| 317 | #else |
| 318 | #define __net_init __init |
| 319 | #define __net_exit __ref |
| 320 | #define __net_initdata __initdata |
| 321 | #define __net_initconst __initconst |
| 322 | #endif |
| 323 | |
| 324 | int peernet2id_alloc(struct net *net, struct net *peer); |
| 325 | int peernet2id(struct net *net, struct net *peer); |
| 326 | bool peernet_has_id(struct net *net, struct net *peer); |
| 327 | struct net *get_net_ns_by_id(struct net *net, int id); |
| 328 | |
| 329 | struct pernet_operations { |
| 330 | struct list_head list; |
| 331 | /* |
| 332 | * Below methods are called without any exclusive locks. |
| 333 | * More than one net may be constructed and destructed |
| 334 | * in parallel on several cpus. Every pernet_operations |
| 335 | * have to keep in mind all other pernet_operations and |
| 336 | * to introduce a locking, if they share common resources. |
| 337 | * |
| 338 | * The only time they are called with exclusive lock is |
| 339 | * from register_pernet_subsys(), unregister_pernet_subsys() |
| 340 | * register_pernet_device() and unregister_pernet_device(). |
| 341 | * |
| 342 | * Exit methods using blocking RCU primitives, such as |
| 343 | * synchronize_rcu(), should be implemented via exit_batch. |
| 344 | * Then, destruction of a group of net requires single |
| 345 | * synchronize_rcu() related to these pernet_operations, |
| 346 | * instead of separate synchronize_rcu() for every net. |
| 347 | * Please, avoid synchronize_rcu() at all, where it's possible. |
| 348 | */ |
| 349 | int (*init)(struct net *net); |
| 350 | void (*exit)(struct net *net); |
| 351 | void (*exit_batch)(struct list_head *net_exit_list); |
| 352 | unsigned int *id; |
| 353 | size_t size; |
| 354 | }; |
| 355 | |
| 356 | /* |
| 357 | * Use these carefully. If you implement a network device and it |
| 358 | * needs per network namespace operations use device pernet operations, |
| 359 | * otherwise use pernet subsys operations. |
| 360 | * |
| 361 | * Network interfaces need to be removed from a dying netns _before_ |
| 362 | * subsys notifiers can be called, as most of the network code cleanup |
| 363 | * (which is done from subsys notifiers) runs with the assumption that |
| 364 | * dev_remove_pack has been called so no new packets will arrive during |
| 365 | * and after the cleanup functions have been called. dev_remove_pack |
| 366 | * is not per namespace so instead the guarantee of no more packets |
| 367 | * arriving in a network namespace is provided by ensuring that all |
| 368 | * network devices and all sockets have left the network namespace |
| 369 | * before the cleanup methods are called. |
| 370 | * |
| 371 | * For the longest time the ipv4 icmp code was registered as a pernet |
| 372 | * device which caused kernel oops, and panics during network |
| 373 | * namespace cleanup. So please don't get this wrong. |
| 374 | */ |
| 375 | int register_pernet_subsys(struct pernet_operations *); |
| 376 | void unregister_pernet_subsys(struct pernet_operations *); |
| 377 | int register_pernet_device(struct pernet_operations *); |
| 378 | void unregister_pernet_device(struct pernet_operations *); |
| 379 | |
| 380 | struct ctl_table; |
| 381 | struct ctl_table_header; |
| 382 | |
| 383 | #ifdef CONFIG_SYSCTL |
| 384 | int net_sysctl_init(void); |
| 385 | struct ctl_table_header *register_net_sysctl(struct net *net, const char *path, |
| 386 | struct ctl_table *table); |
| 387 | void unregister_net_sysctl_table(struct ctl_table_header *header); |
| 388 | #else |
| 389 | static inline int net_sysctl_init(void) { return 0; } |
| 390 | static inline struct ctl_table_header *register_net_sysctl(struct net *net, |
| 391 | const char *path, struct ctl_table *table) |
| 392 | { |
| 393 | return NULL; |
| 394 | } |
| 395 | static inline void unregister_net_sysctl_table(struct ctl_table_header *header) |
| 396 | { |
| 397 | } |
| 398 | #endif |
| 399 | |
| 400 | static inline int rt_genid_ipv4(struct net *net) |
| 401 | { |
| 402 | return atomic_read(&net->ipv4.rt_genid); |
| 403 | } |
| 404 | |
| 405 | static inline void rt_genid_bump_ipv4(struct net *net) |
| 406 | { |
| 407 | atomic_inc(&net->ipv4.rt_genid); |
| 408 | } |
| 409 | |
| 410 | extern void (*__fib6_flush_trees)(struct net *net); |
| 411 | static inline void rt_genid_bump_ipv6(struct net *net) |
| 412 | { |
| 413 | if (__fib6_flush_trees) |
| 414 | __fib6_flush_trees(net); |
| 415 | } |
| 416 | |
| 417 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) |
| 418 | static inline struct netns_ieee802154_lowpan * |
| 419 | net_ieee802154_lowpan(struct net *net) |
| 420 | { |
| 421 | return &net->ieee802154_lowpan; |
| 422 | } |
| 423 | #endif |
| 424 | |
| 425 | /* For callers who don't really care about whether it's IPv4 or IPv6 */ |
| 426 | static inline void rt_genid_bump_all(struct net *net) |
| 427 | { |
| 428 | rt_genid_bump_ipv4(net); |
| 429 | rt_genid_bump_ipv6(net); |
| 430 | } |
| 431 | |
| 432 | static inline int fnhe_genid(struct net *net) |
| 433 | { |
| 434 | return atomic_read(&net->fnhe_genid); |
| 435 | } |
| 436 | |
| 437 | static inline void fnhe_genid_bump(struct net *net) |
| 438 | { |
| 439 | atomic_inc(&net->fnhe_genid); |
| 440 | } |
| 441 | |
| 442 | #endif /* __NET_NET_NAMESPACE_H */ |