Lines Matching refs:net

47 struct net init_net;
81 static int net_assign_generic(struct net *net, unsigned int id, void *data) in net_assign_generic() argument
87 old_ng = rcu_dereference_protected(net->gen, in net_assign_generic()
113 rcu_assign_pointer(net->gen, ng); in net_assign_generic()
118 static int ops_init(const struct pernet_operations *ops, struct net *net) in ops_init() argument
129 err = net_assign_generic(net, *ops->id, data); in ops_init()
135 err = ops->init(net); in ops_init()
141 ng = rcu_dereference_protected(net->gen, in ops_init()
155 struct net *net; in ops_pre_exit_list() local
158 list_for_each_entry(net, net_exit_list, exit_list) in ops_pre_exit_list()
159 ops->pre_exit(net); in ops_pre_exit_list()
166 struct net *net; in ops_exit_list() local
168 list_for_each_entry(net, net_exit_list, exit_list) { in ops_exit_list()
169 ops->exit(net); in ops_exit_list()
180 struct net *net; in ops_free_list() local
182 list_for_each_entry(net, net_exit_list, exit_list) in ops_free_list()
183 kfree(net_generic(net, *ops->id)); in ops_free_list()
188 static int alloc_netid(struct net *net, struct net *peer, int reqid) in alloc_netid() argument
197 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC); in alloc_netid()
206 static int net_eq_idr(int id, void *net, void *peer) in net_eq_idr() argument
208 if (net_eq(net, peer)) in net_eq_idr()
214 static int __peernet2id(const struct net *net, struct net *peer) in __peernet2id() argument
216 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer); in __peernet2id()
227 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
232 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp) in peernet2id_alloc() argument
236 if (refcount_read(&net->ns.count) == 0) in peernet2id_alloc()
239 spin_lock_bh(&net->nsid_lock); in peernet2id_alloc()
240 id = __peernet2id(net, peer); in peernet2id_alloc()
242 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
252 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
256 id = alloc_netid(net, peer, -1); in peernet2id_alloc()
257 spin_unlock_bh(&net->nsid_lock); in peernet2id_alloc()
263 rtnl_net_notifyid(net, RTM_NEWNSID, id, 0, NULL, gfp); in peernet2id_alloc()
270 int peernet2id(const struct net *net, struct net *peer) in peernet2id() argument
275 id = __peernet2id(net, peer); in peernet2id()
285 bool peernet_has_id(const struct net *net, struct net *peer) in peernet_has_id() argument
287 return peernet2id(net, peer) >= 0; in peernet_has_id()
290 struct net *get_net_ns_by_id(const struct net *net, int id) in get_net_ns_by_id() argument
292 struct net *peer; in get_net_ns_by_id()
298 peer = idr_find(&net->netns_ids, id); in get_net_ns_by_id()
310 static __net_init int setup_net(struct net *net, struct user_namespace *user_ns) in setup_net() argument
317 refcount_set(&net->ns.count, 1); in setup_net()
318 ref_tracker_dir_init(&net->refcnt_tracker, 128); in setup_net()
320 refcount_set(&net->passive, 1); in setup_net()
321 get_random_bytes(&net->hash_mix, sizeof(u32)); in setup_net()
323 net->net_cookie = gen_cookie_next(&net_cookie); in setup_net()
325 net->dev_base_seq = 1; in setup_net()
326 net->user_ns = user_ns; in setup_net()
327 idr_init(&net->netns_ids); in setup_net()
328 spin_lock_init(&net->nsid_lock); in setup_net()
329 mutex_init(&net->ipv4.ra_mutex); in setup_net()
332 error = ops_init(ops, net); in setup_net()
337 list_add_tail_rcu(&net->list, &net_namespace_list); in setup_net()
346 list_add(&net->exit_list, &net_exit_list); in setup_net()
365 static int __net_init net_defaults_init_net(struct net *net) in net_defaults_init_net() argument
367 net->core.sysctl_somaxconn = SOMAXCONN; in net_defaults_init_net()
368 net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED; in net_defaults_init_net()
401 static struct net *net_alloc(void) in net_alloc()
403 struct net *net = NULL; in net_alloc() local
410 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL); in net_alloc()
411 if (!net) in net_alloc()
415 net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL); in net_alloc()
416 if (!net->key_domain) in net_alloc()
418 refcount_set(&net->key_domain->usage, 1); in net_alloc()
421 rcu_assign_pointer(net->gen, ng); in net_alloc()
423 return net; in net_alloc()
427 kmem_cache_free(net_cachep, net); in net_alloc()
428 net = NULL; in net_alloc()
435 static void net_free(struct net *net) in net_free() argument
437 if (refcount_dec_and_test(&net->passive)) { in net_free()
438 kfree(rcu_access_pointer(net->gen)); in net_free()
439 kmem_cache_free(net_cachep, net); in net_free()
445 struct net *net = (struct net *)p; in net_drop_ns() local
447 if (net) in net_drop_ns()
448 net_free(net); in net_drop_ns()
451 struct net *copy_net_ns(unsigned long flags, in copy_net_ns()
452 struct user_namespace *user_ns, struct net *old_net) in copy_net_ns()
455 struct net *net; in copy_net_ns() local
465 net = net_alloc(); in copy_net_ns()
466 if (!net) { in copy_net_ns()
470 refcount_set(&net->passive, 1); in copy_net_ns()
471 net->ucounts = ucounts; in copy_net_ns()
478 rv = setup_net(net, user_ns); in copy_net_ns()
485 key_remove_domain(net->key_domain); in copy_net_ns()
488 net_free(net); in copy_net_ns()
493 return net; in copy_net_ns()
505 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) in net_ns_get_ownership() argument
507 if (net) { in net_ns_get_ownership()
508 kuid_t ns_root_uid = make_kuid(net->user_ns, 0); in net_ns_get_ownership()
509 kgid_t ns_root_gid = make_kgid(net->user_ns, 0); in net_ns_get_ownership()
523 static void unhash_nsid(struct net *net, struct net *last) in unhash_nsid() argument
525 struct net *tmp; in unhash_nsid()
536 id = __peernet2id(tmp, net); in unhash_nsid()
546 spin_lock_bh(&net->nsid_lock); in unhash_nsid()
547 idr_destroy(&net->netns_ids); in unhash_nsid()
548 spin_unlock_bh(&net->nsid_lock); in unhash_nsid()
556 struct net *net, *tmp, *last; in cleanup_net() local
567 llist_for_each_entry(net, net_kill_list, cleanup_list) in cleanup_net()
568 list_del_rcu(&net->list); in cleanup_net()
579 last = list_last_entry(&net_namespace_list, struct net, list); in cleanup_net()
582 llist_for_each_entry(net, net_kill_list, cleanup_list) { in cleanup_net()
583 unhash_nsid(net, last); in cleanup_net()
584 list_add_tail(&net->exit_list, &net_exit_list); in cleanup_net()
615 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) { in cleanup_net()
616 list_del_init(&net->exit_list); in cleanup_net()
617 dec_net_namespaces(net->ucounts); in cleanup_net()
619 key_remove_domain(net->key_domain); in cleanup_net()
621 put_user_ns(net->user_ns); in cleanup_net()
622 net_free(net); in cleanup_net()
644 void __put_net(struct net *net) in __put_net() argument
646 ref_tracker_dir_exit(&net->refcnt_tracker); in __put_net()
648 if (llist_add(&net->cleanup_list, &cleanup_list)) in __put_net()
661 return &get_net(container_of(ns, struct net, ns))->ns; in get_net_ns()
665 struct net *get_net_ns_by_fd(int fd) in get_net_ns_by_fd()
669 struct net *net; in get_net_ns_by_fd() local
677 net = get_net(container_of(ns, struct net, ns)); in get_net_ns_by_fd()
679 net = ERR_PTR(-EINVAL); in get_net_ns_by_fd()
682 return net; in get_net_ns_by_fd()
687 struct net *get_net_ns_by_pid(pid_t pid) in get_net_ns_by_pid()
690 struct net *net; in get_net_ns_by_pid() local
693 net = ERR_PTR(-ESRCH); in get_net_ns_by_pid()
701 net = get_net(nsproxy->net_ns); in get_net_ns_by_pid()
705 return net; in get_net_ns_by_pid()
709 static __net_init int net_ns_net_init(struct net *net) in net_ns_net_init() argument
712 net->ns.ops = &netns_operations; in net_ns_net_init()
714 return ns_alloc_inum(&net->ns); in net_ns_net_init()
717 static __net_exit void net_ns_net_exit(struct net *net) in net_ns_net_exit() argument
719 ns_free_inum(&net->ns); in net_ns_net_exit()
738 struct net *net = sock_net(skb->sk); in rtnl_net_newid() local
741 struct net *peer; in rtnl_net_newid()
770 spin_lock_bh(&net->nsid_lock); in rtnl_net_newid()
771 if (__peernet2id(net, peer) >= 0) { in rtnl_net_newid()
772 spin_unlock_bh(&net->nsid_lock); in rtnl_net_newid()
780 err = alloc_netid(net, peer, nsid); in rtnl_net_newid()
781 spin_unlock_bh(&net->nsid_lock); in rtnl_net_newid()
783 rtnl_net_notifyid(net, RTM_NEWNSID, err, NETLINK_CB(skb).portid, in rtnl_net_newid()
882 struct net *net = sock_net(skb->sk); in rtnl_net_getid() local
889 struct net *peer, *target = net; in rtnl_net_getid()
904 peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID])); in rtnl_net_getid()
931 fillargs.ref_nsid = peernet2id(net, peer); in rtnl_net_getid()
945 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid); in rtnl_net_getid()
958 struct net *tgt_net;
959 struct net *ref_net;
1006 struct net *net; in rtnl_valid_dump_net_req() local
1008 net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i])); in rtnl_valid_dump_net_req()
1009 if (IS_ERR(net)) { in rtnl_valid_dump_net_req()
1013 return PTR_ERR(net); in rtnl_valid_dump_net_req()
1017 net_cb->tgt_net = net; in rtnl_valid_dump_net_req()
1062 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid, in rtnl_net_notifyid() argument
1082 rtnl_notify(msg, net, portid, RTNLGRP_NSID, nlh, gfp); in rtnl_net_notifyid()
1088 rtnl_set_sk_err(net, RTNLGRP_NSID, err); in rtnl_net_notifyid()
1096 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), in net_ns_init()
1143 struct net *net; in __register_pernet_operations() local
1152 for_each_net(net) { in __register_pernet_operations()
1153 error = ops_init(ops, net); in __register_pernet_operations()
1156 list_add_tail(&net->exit_list, &net_exit_list); in __register_pernet_operations()
1170 struct net *net; in __unregister_pernet_operations() local
1175 for_each_net(net) in __unregister_pernet_operations()
1176 list_add_tail(&net->exit_list, &net_exit_list); in __unregister_pernet_operations()
1339 struct net *net = NULL; in netns_get() local
1345 net = get_net(nsproxy->net_ns); in netns_get()
1348 return net ? &net->ns : NULL; in netns_get()
1351 static inline struct net *to_net_ns(struct ns_common *ns) in to_net_ns()
1353 return container_of(ns, struct net, ns); in to_net_ns()
1364 struct net *net = to_net_ns(ns); in netns_install() local
1366 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) || in netns_install()
1371 nsproxy->net_ns = get_net(net); in netns_install()