Lines Matching full:net
25 #include <net/sock.h>
26 #include <net/netlink.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
48 struct net init_net;
55 * This is internal net namespace object. Please, don't use it
85 static int net_assign_generic(struct net *net, unsigned int id, void *data) in net_assign_generic() argument
91 old_ng = rcu_dereference_protected(net->gen, in net_assign_generic()
105 * The net_generic explores the net->gen array inside rcu in net_assign_generic()
106 * read section. Besides once set the net->gen->ptr[x] in net_assign_generic()
117 rcu_assign_pointer(net->gen, ng); in net_assign_generic()
122 static int ops_init(const struct pernet_operations *ops, struct net *net) in ops_init() argument
133 err = net_assign_generic(net, *ops->id, data); in ops_init()
139 err = ops->init(net); in ops_init()
144 ng = rcu_dereference_protected(net->gen, in ops_init()
159 struct net *net; in ops_pre_exit_list() local
162 list_for_each_entry(net, net_exit_list, exit_list) in ops_pre_exit_list()
163 ops->pre_exit(net); in ops_pre_exit_list()
170 struct net *net; in ops_exit_list() local
172 list_for_each_entry(net, net_exit_list, exit_list) { in ops_exit_list()
173 ops->exit(net); in ops_exit_list()
184 struct net *net; in ops_free_list() local
187 list_for_each_entry(net, net_exit_list, exit_list) in ops_free_list()
188 kfree(net_generic(net, *ops->id)); in ops_free_list()
193 static int alloc_netid(struct net *net, struct net *peer, int reqid) in alloc_netid() argument
202 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC); in alloc_netid()
205 /* This function is used by idr_for_each(). If net is equal to peer, the
211 static int net_eq_idr(int id, void *net, void *peer) in net_eq_idr() argument
213 if (net_eq(net, peer)) in net_eq_idr()
219 static int __peernet2id(const struct net *net, struct net *peer) in __peernet2id() argument
221 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer); in __peernet2id()
232 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
237 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp) in peernet2id_alloc() argument
241 if (refcount_read(&net->ns.count) == 0) in peernet2id_alloc()
244 spin_lock_bh(&net->nsid_lock); in peernet2id_alloc()
245 id = __peernet2id(net, peer); in peernet2id_alloc()
247 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
253 * we never hash a peer back to net->netns_ids, after it has in peernet2id_alloc()
257 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
261 id = alloc_netid(net, peer, -1); in peernet2id_alloc()
262 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
268 rtnl_net_notifyid(net, RTM_NEWNSID, id, 0, NULL, gfp); in peernet2id_alloc()
275 int peernet2id(const struct net *net, struct net *peer) in peernet2id() argument
280 id = __peernet2id(net, peer); in peernet2id()
290 bool peernet_has_id(const struct net *net, struct net *peer) in peernet_has_id() argument
292 return peernet2id(net, peer) >= 0; in peernet_has_id()
295 struct net *get_net_ns_by_id(const struct net *net, int id) in get_net_ns_by_id() argument
297 struct net *peer; in get_net_ns_by_id()
303 peer = idr_find(&net->netns_ids, id); in get_net_ns_by_id()
312 static __net_init void preinit_net_sysctl(struct net *net) in preinit_net_sysctl() argument
314 net->core.sysctl_somaxconn = SOMAXCONN; in preinit_net_sysctl()
318 net->core.sysctl_optmem_max = 128 * 1024; in preinit_net_sysctl()
319 net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED; in preinit_net_sysctl()
323 static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns) in preinit_net() argument
325 refcount_set(&net->passive, 1); in preinit_net()
326 refcount_set(&net->ns.count, 1); in preinit_net()
327 ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt"); in preinit_net()
328 ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt"); in preinit_net()
330 get_random_bytes(&net->hash_mix, sizeof(u32)); in preinit_net()
331 net->dev_base_seq = 1; in preinit_net()
332 net->user_ns = user_ns; in preinit_net()
334 idr_init(&net->netns_ids); in preinit_net()
335 spin_lock_init(&net->nsid_lock); in preinit_net()
336 mutex_init(&net->ipv4.ra_mutex); in preinit_net()
337 preinit_net_sysctl(net); in preinit_net()
343 static __net_init int setup_net(struct net *net) in setup_net() argument
352 net->net_cookie = gen_cookie_next(&net_cookie); in setup_net()
356 error = ops_init(ops, net); in setup_net()
361 list_add_tail_rcu(&net->list, &net_namespace_list); in setup_net()
370 list_add(&net->exit_list, &net_exit_list); in setup_net()
412 static struct net *net_alloc(void) in net_alloc()
414 struct net *net = NULL; in net_alloc() local
421 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL); in net_alloc()
422 if (!net) in net_alloc()
426 net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL); in net_alloc()
427 if (!net->key_domain) in net_alloc()
429 refcount_set(&net->key_domain->usage, 1); in net_alloc()
432 rcu_assign_pointer(net->gen, ng); in net_alloc()
434 return net; in net_alloc()
438 kmem_cache_free(net_cachep, net); in net_alloc()
439 net = NULL; in net_alloc()
446 static void net_free(struct net *net) in net_free() argument
448 if (refcount_dec_and_test(&net->passive)) { in net_free()
449 kfree(rcu_access_pointer(net->gen)); in net_free()
452 ref_tracker_dir_exit(&net->notrefcnt_tracker); in net_free()
454 kmem_cache_free(net_cachep, net); in net_free()
460 struct net *net = (struct net *)p; in net_drop_ns() local
462 if (net) in net_drop_ns()
463 net_free(net); in net_drop_ns()
466 struct net *copy_net_ns(unsigned long flags, in copy_net_ns()
467 struct user_namespace *user_ns, struct net *old_net) in copy_net_ns()
470 struct net *net; in copy_net_ns() local
480 net = net_alloc(); in copy_net_ns()
481 if (!net) { in copy_net_ns()
486 preinit_net(net, user_ns); in copy_net_ns()
487 net->ucounts = ucounts; in copy_net_ns()
494 rv = setup_net(net); in copy_net_ns()
501 key_remove_domain(net->key_domain); in copy_net_ns()
504 net_free(net); in copy_net_ns()
509 return net; in copy_net_ns()
513 * net_ns_get_ownership - get sysfs ownership data for @net
514 * @net: network namespace in question (can be NULL)
521 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) in net_ns_get_ownership() argument
523 if (net) { in net_ns_get_ownership()
524 kuid_t ns_root_uid = make_kuid(net->user_ns, 0); in net_ns_get_ownership()
525 kgid_t ns_root_gid = make_kgid(net->user_ns, 0); in net_ns_get_ownership()
539 static void unhash_nsid(struct net *net, struct net *last) in unhash_nsid() argument
541 struct net *tmp; in unhash_nsid()
544 * a net from net_namespace_list. So, when the below in unhash_nsid()
552 id = __peernet2id(tmp, net); in unhash_nsid()
562 spin_lock_bh(&net->nsid_lock); in unhash_nsid()
563 idr_destroy(&net->netns_ids); in unhash_nsid()
564 spin_unlock_bh(&net->nsid_lock); in unhash_nsid()
572 struct net *net, *tmp, *last; in cleanup_net() local
584 llist_for_each_entry(net, net_kill_list, cleanup_list) in cleanup_net()
585 list_del_rcu(&net->list); in cleanup_net()
586 /* Cache last net. After we unlock rtnl, no one new net in cleanup_net()
588 * to a net from net_kill_list (see peernet2id_alloc()). in cleanup_net()
596 last = list_last_entry(&net_namespace_list, struct net, list); in cleanup_net()
599 llist_for_each_entry(net, net_kill_list, cleanup_list) { in cleanup_net()
600 unhash_nsid(net, last); in cleanup_net()
601 list_add_tail(&net->exit_list, &net_exit_list); in cleanup_net()
628 /* Free the net generic variables */ in cleanup_net()
640 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) { in cleanup_net()
641 list_del_init(&net->exit_list); in cleanup_net()
642 dec_net_namespaces(net->ucounts); in cleanup_net()
644 key_remove_domain(net->key_domain); in cleanup_net()
646 put_user_ns(net->user_ns); in cleanup_net()
647 net_free(net); in cleanup_net()
655 * from the global list, then run net exit functions.
669 void __put_net(struct net *net) in __put_net() argument
671 ref_tracker_dir_exit(&net->refcnt_tracker); in __put_net()
673 if (llist_add(&net->cleanup_list, &cleanup_list)) in __put_net()
680 * @ns: common namespace (net)
682 * Returns the net's common namespace or ERR_PTR() if ref is zero.
686 struct net *net; in get_net_ns() local
688 net = maybe_get_net(container_of(ns, struct net, ns)); in get_net_ns()
689 if (net) in get_net_ns()
690 return &net->ns; in get_net_ns()
695 struct net *get_net_ns_by_fd(int fd) in get_net_ns_by_fd()
698 struct net *net = ERR_PTR(-EINVAL); in get_net_ns_by_fd() local
706 net = get_net(container_of(ns, struct net, ns)); in get_net_ns_by_fd()
710 return net; in get_net_ns_by_fd()
715 struct net *get_net_ns_by_pid(pid_t pid) in get_net_ns_by_pid()
718 struct net *net; in get_net_ns_by_pid() local
721 net = ERR_PTR(-ESRCH); in get_net_ns_by_pid()
729 net = get_net(nsproxy->net_ns); in get_net_ns_by_pid()
733 return net; in get_net_ns_by_pid()
737 static __net_init int net_ns_net_init(struct net *net) in net_ns_net_init() argument
740 net->ns.ops = &netns_operations; in net_ns_net_init()
742 return ns_alloc_inum(&net->ns); in net_ns_net_init()
745 static __net_exit void net_ns_net_exit(struct net *net) in net_ns_net_exit() argument
747 ns_free_inum(&net->ns); in net_ns_net_exit()
766 struct net *net = sock_net(skb->sk); in rtnl_net_newid() local
769 struct net *peer; in rtnl_net_newid()
798 spin_lock_bh(&net->nsid_lock); in rtnl_net_newid()
799 if (__peernet2id(net, peer) >= 0) { in rtnl_net_newid()
800 spin_unlock_bh(&net->nsid_lock); in rtnl_net_newid()
808 err = alloc_netid(net, peer, nsid); in rtnl_net_newid()
809 spin_unlock_bh(&net->nsid_lock); in rtnl_net_newid()
811 rtnl_net_notifyid(net, RTM_NEWNSID, err, NETLINK_CB(skb).portid, in rtnl_net_newid()
910 struct net *net = sock_net(skb->sk); in rtnl_net_getid() local
917 struct net *peer, *target = net; in rtnl_net_getid()
932 peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID])); in rtnl_net_getid()
959 fillargs.ref_nsid = peernet2id(net, peer); in rtnl_net_getid()
973 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid); in rtnl_net_getid()
986 struct net *tgt_net;
987 struct net *ref_net;
1034 struct net *net; in rtnl_valid_dump_net_req() local
1036 net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i])); in rtnl_valid_dump_net_req()
1037 if (IS_ERR(net)) { in rtnl_valid_dump_net_req()
1041 return PTR_ERR(net); in rtnl_valid_dump_net_req()
1045 net_cb->tgt_net = net; in rtnl_valid_dump_net_req()
1090 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid, in rtnl_net_notifyid() argument
1110 rtnl_notify(msg, net, portid, RTNLGRP_NSID, nlh, gfp); in rtnl_net_notifyid()
1116 rtnl_set_sk_err(net, RTNLGRP_NSID, err); in rtnl_net_notifyid()
1169 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), in net_ns_init()
1229 struct net *net; in __register_pernet_operations() local
1238 for_each_net(net) { in __register_pernet_operations()
1239 error = ops_init(ops, net); in __register_pernet_operations()
1242 list_add_tail(&net->exit_list, &net_exit_list); in __register_pernet_operations()
1256 struct net *net; in __unregister_pernet_operations() local
1261 for_each_net(net) in __unregister_pernet_operations()
1262 list_add_tail(&net->exit_list, &net_exit_list); in __unregister_pernet_operations()
1432 struct net *net = NULL; in netns_get() local
1438 net = get_net(nsproxy->net_ns); in netns_get()
1441 return net ? &net->ns : NULL; in netns_get()
1444 static inline struct net *to_net_ns(struct ns_common *ns) in to_net_ns()
1446 return container_of(ns, struct net, ns); in to_net_ns()
1457 struct net *net = to_net_ns(ns); in netns_install() local
1459 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) || in netns_install()
1464 nsproxy->net_ns = get_net(net); in netns_install()
1474 .name = "net",