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)
91 old_ng = rcu_dereference_protected(net->gen,
105 * The net_generic explores the net->gen array inside rcu
106 * read section. Besides once set the net->gen->ptr[x]
117 rcu_assign_pointer(net->gen, ng);
122 static int ops_init(const struct pernet_operations *ops, struct net *net)
133 err = net_assign_generic(net, *ops->id, data);
139 err = ops->init(net);
144 ng = rcu_dereference_protected(net->gen,
159 struct net *net;
162 list_for_each_entry(net, net_exit_list, exit_list)
163 ops->pre_exit(net);
170 struct net *net;
172 list_for_each_entry(net, net_exit_list, exit_list) {
173 ops->exit(net);
184 struct net *net;
186 list_for_each_entry(net, net_exit_list, exit_list)
187 kfree(net_generic(net, *ops->id));
192 static int alloc_netid(struct net *net, struct net *peer, int reqid)
201 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
204 /* This function is used by idr_for_each(). If net is equal to peer, the
210 static int net_eq_idr(int id, void *net, void *peer)
212 if (net_eq(net, peer))
218 static int __peernet2id(const struct net *net, struct net *peer)
220 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
231 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
236 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp)
240 if (refcount_read(&net->ns.count) == 0)
243 spin_lock_bh(&net->nsid_lock);
244 id = __peernet2id(net, peer);
246 spin_unlock_bh(&net->nsid_lock);
252 * we never hash a peer back to net->netns_ids, after it has
256 spin_unlock_bh(&net->nsid_lock);
260 id = alloc_netid(net, peer, -1);
261 spin_unlock_bh(&net->nsid_lock);
267 rtnl_net_notifyid(net, RTM_NEWNSID, id, 0, NULL, gfp);
274 int peernet2id(const struct net *net, struct net *peer)
279 id = __peernet2id(net, peer);
289 bool peernet_has_id(const struct net *net, struct net *peer)
291 return peernet2id(net, peer) >= 0;
294 struct net *get_net_ns_by_id(const struct net *net, int id)
296 struct net *peer;
302 peer = idr_find(&net->netns_ids, id);
312 static __net_init void preinit_net(struct net *net)
314 ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt");
320 static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
328 refcount_set(&net->ns.count, 1);
329 ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
331 refcount_set(&net->passive, 1);
332 get_random_bytes(&net->hash_mix, sizeof(u32));
334 net->net_cookie = gen_cookie_next(&net_cookie);
336 net->dev_base_seq = 1;
337 net->user_ns = user_ns;
338 idr_init(&net->netns_ids);
339 spin_lock_init(&net->nsid_lock);
340 mutex_init(&net->ipv4.ra_mutex);
343 error = ops_init(ops, net);
348 list_add_tail_rcu(&net->list, &net_namespace_list);
357 list_add(&net->exit_list, &net_exit_list);
385 static int __net_init net_defaults_init_net(struct net *net)
387 net->core.sysctl_somaxconn = SOMAXCONN;
388 net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
400 panic("Cannot initialize net default settings");
421 static struct net *net_alloc(void)
423 struct net *net = NULL;
430 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
431 if (!net)
435 net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL);
436 if (!net->key_domain)
438 refcount_set(&net->key_domain->usage, 1);
441 rcu_assign_pointer(net->gen, ng);
443 return net;
447 kmem_cache_free(net_cachep, net);
448 net = NULL;
460 struct net *net, *next;
465 llist_for_each_entry_safe(net, next, kill_list, defer_free_list)
466 kmem_cache_free(net_cachep, net);
470 static void net_free(struct net *net)
472 if (refcount_dec_and_test(&net->passive)) {
473 kfree(rcu_access_pointer(net->gen));
476 ref_tracker_dir_exit(&net->notrefcnt_tracker);
479 llist_add(&net->defer_free_list, &defer_free_list);
485 struct net *net = (struct net *)p;
487 if (net)
488 net_free(net);
491 struct net *copy_net_ns(unsigned long flags,
492 struct user_namespace *user_ns, struct net *old_net)
495 struct net *net;
505 net = net_alloc();
506 if (!net) {
511 preinit_net(net);
512 refcount_set(&net->passive, 1);
513 net->ucounts = ucounts;
520 rv = setup_net(net, user_ns);
527 key_remove_domain(net->key_domain);
530 net_free(net);
535 return net;
539 * net_ns_get_ownership - get sysfs ownership data for @net
540 * @net: network namespace in question (can be NULL)
547 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
549 if (net) {
550 kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
551 kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
565 static void unhash_nsid(struct net *net, struct net *last)
567 struct net *tmp;
570 * a net from net_namespace_list. So, when the below
578 id = __peernet2id(tmp, net);
588 spin_lock_bh(&net->nsid_lock);
589 idr_destroy(&net->netns_ids);
590 spin_unlock_bh(&net->nsid_lock);
598 struct net *net, *tmp, *last;
610 llist_for_each_entry(net, net_kill_list, cleanup_list)
611 list_del_rcu(&net->list);
612 /* Cache last net. After we unlock rtnl, no one new net
614 * to a net from net_kill_list (see peernet2id_alloc()).
622 last = list_last_entry(&net_namespace_list, struct net, list);
625 llist_for_each_entry(net, net_kill_list, cleanup_list) {
626 unhash_nsid(net, last);
627 list_add_tail(&net->exit_list, &net_exit_list);
654 /* Free the net generic variables */
668 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
669 list_del_init(&net->exit_list);
670 dec_net_namespaces(net->ucounts);
672 key_remove_domain(net->key_domain);
674 put_user_ns(net->user_ns);
675 net_free(net);
683 * from the global list, then run net exit functions.
697 void __put_net(struct net *net)
699 ref_tracker_dir_exit(&net->refcnt_tracker);
701 if (llist_add(&net->cleanup_list, &cleanup_list))
708 * @ns: common namespace (net)
710 * Returns the net's common namespace or ERR_PTR() if ref is zero.
714 struct net *net;
716 net = maybe_get_net(container_of(ns, struct net, ns));
717 if (net)
718 return &net->ns;
723 struct net *get_net_ns_by_fd(int fd)
726 struct net *net = ERR_PTR(-EINVAL);
734 net = get_net(container_of(ns, struct net, ns));
738 return net;
743 struct net *get_net_ns_by_pid(pid_t pid)
746 struct net *net;
749 net = ERR_PTR(-ESRCH);
757 net = get_net(nsproxy->net_ns);
761 return net;
765 static __net_init int net_ns_net_init(struct net *net)
768 net->ns.ops = &netns_operations;
770 return ns_alloc_inum(&net->ns);
773 static __net_exit void net_ns_net_exit(struct net *net)
775 ns_free_inum(&net->ns);
794 struct net *net = sock_net(skb->sk);
797 struct net *peer;
826 spin_lock_bh(&net->nsid_lock);
827 if (__peernet2id(net, peer) >= 0) {
828 spin_unlock_bh(&net->nsid_lock);
836 err = alloc_netid(net, peer, nsid);
837 spin_unlock_bh(&net->nsid_lock);
839 rtnl_net_notifyid(net, RTM_NEWNSID, err, NETLINK_CB(skb).portid,
938 struct net *net = sock_net(skb->sk);
945 struct net *peer, *target = net;
960 peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID]));
987 fillargs.ref_nsid = peernet2id(net, peer);
1001 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
1014 struct net *tgt_net;
1015 struct net *ref_net;
1062 struct net *net;
1064 net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i]));
1065 if (IS_ERR(net)) {
1069 return PTR_ERR(net);
1073 net_cb->tgt_net = net;
1118 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
1138 rtnl_notify(msg, net, portid, RTNLGRP_NSID, nlh, gfp);
1144 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
1152 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
1210 struct net *net;
1219 for_each_net(net) {
1220 error = ops_init(ops, net);
1223 list_add_tail(&net->exit_list, &net_exit_list);
1237 struct net *net;
1242 for_each_net(net)
1243 list_add_tail(&net->exit_list, &net_exit_list);
1410 struct net *net = NULL;
1416 net = get_net(nsproxy->net_ns);
1419 return net ? &net->ns : NULL;
1422 static inline struct net *to_net_ns(struct ns_common *ns)
1424 return container_of(ns, struct net, ns);
1435 struct net *net = to_net_ns(ns);
1437 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
1442 nsproxy->net_ns = get_net(net);
1452 .name = "net",