GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / core / net_namespace.c
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
3 #include <linux/workqueue.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/cache.h>
6 #include <linux/slab.h>
7 #include <linux/list.h>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <linux/idr.h>
11 #include <linux/rculist.h>
12 #include <linux/nsproxy.h>
13 #include <linux/fs.h>
14 #include <linux/proc_ns.h>
15 #include <linux/file.h>
16 #include <linux/export.h>
17 #include <linux/user_namespace.h>
18 #include <linux/net_namespace.h>
19 #include <linux/sched/task.h>
20
21 #include <net/sock.h>
22 #include <net/netlink.h>
23 #include <net/net_namespace.h>
24 #include <net/netns/generic.h>
25
26 /*
27  *      Our network namespace constructor/destructor lists
28  */
29
30 static LIST_HEAD(pernet_list);
31 static struct list_head *first_device = &pernet_list;
32 DEFINE_MUTEX(net_mutex);
33
34 LIST_HEAD(net_namespace_list);
35 EXPORT_SYMBOL_GPL(net_namespace_list);
36
37 struct net init_net = {
38         .count          = ATOMIC_INIT(1),
39         .dev_base_head  = LIST_HEAD_INIT(init_net.dev_base_head),
40 };
41 EXPORT_SYMBOL(init_net);
42
43 static bool init_net_initialized;
44
45 #define MIN_PERNET_OPS_ID       \
46         ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
47
48 #define INITIAL_NET_GEN_PTRS    13 /* +1 for len +2 for rcu_head */
49
50 static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
51
52 static struct net_generic *net_alloc_generic(void)
53 {
54         struct net_generic *ng;
55         unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
56
57         ng = kzalloc(generic_size, GFP_KERNEL);
58         if (ng)
59                 ng->s.len = max_gen_ptrs;
60
61         return ng;
62 }
63
64 static int net_assign_generic(struct net *net, unsigned int id, void *data)
65 {
66         struct net_generic *ng, *old_ng;
67
68         BUG_ON(!mutex_is_locked(&net_mutex));
69         BUG_ON(id < MIN_PERNET_OPS_ID);
70
71         old_ng = rcu_dereference_protected(net->gen,
72                                            lockdep_is_held(&net_mutex));
73         if (old_ng->s.len > id) {
74                 old_ng->ptr[id] = data;
75                 return 0;
76         }
77
78         ng = net_alloc_generic();
79         if (ng == NULL)
80                 return -ENOMEM;
81
82         /*
83          * Some synchronisation notes:
84          *
85          * The net_generic explores the net->gen array inside rcu
86          * read section. Besides once set the net->gen->ptr[x]
87          * pointer never changes (see rules in netns/generic.h).
88          *
89          * That said, we simply duplicate this array and schedule
90          * the old copy for kfree after a grace period.
91          */
92
93         memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
94                (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
95         ng->ptr[id] = data;
96
97         rcu_assign_pointer(net->gen, ng);
98         kfree_rcu(old_ng, s.rcu);
99         return 0;
100 }
101
102 static int ops_init(const struct pernet_operations *ops, struct net *net)
103 {
104         int err = -ENOMEM;
105         void *data = NULL;
106
107         if (ops->id && ops->size) {
108                 data = kzalloc(ops->size, GFP_KERNEL);
109                 if (!data)
110                         goto out;
111
112                 err = net_assign_generic(net, *ops->id, data);
113                 if (err)
114                         goto cleanup;
115         }
116         err = 0;
117         if (ops->init)
118                 err = ops->init(net);
119         if (!err)
120                 return 0;
121
122 cleanup:
123         kfree(data);
124
125 out:
126         return err;
127 }
128
129 static void ops_free(const struct pernet_operations *ops, struct net *net)
130 {
131         if (ops->id && ops->size) {
132                 kfree(net_generic(net, *ops->id));
133         }
134 }
135
136 static void ops_exit_list(const struct pernet_operations *ops,
137                           struct list_head *net_exit_list)
138 {
139         struct net *net;
140         if (ops->exit) {
141                 list_for_each_entry(net, net_exit_list, exit_list) {
142                         ops->exit(net);
143                         cond_resched();
144                 }
145         }
146         if (ops->exit_batch)
147                 ops->exit_batch(net_exit_list);
148 }
149
150 static void ops_free_list(const struct pernet_operations *ops,
151                           struct list_head *net_exit_list)
152 {
153         struct net *net;
154         if (ops->size && ops->id) {
155                 list_for_each_entry(net, net_exit_list, exit_list)
156                         ops_free(ops, net);
157         }
158 }
159
160 /* should be called with nsid_lock held */
161 static int alloc_netid(struct net *net, struct net *peer, int reqid)
162 {
163         int min = 0, max = 0;
164
165         if (reqid >= 0) {
166                 min = reqid;
167                 max = reqid + 1;
168         }
169
170         return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
171 }
172
173 /* This function is used by idr_for_each(). If net is equal to peer, the
174  * function returns the id so that idr_for_each() stops. Because we cannot
175  * returns the id 0 (idr_for_each() will not stop), we return the magic value
176  * NET_ID_ZERO (-1) for it.
177  */
178 #define NET_ID_ZERO -1
179 static int net_eq_idr(int id, void *net, void *peer)
180 {
181         if (net_eq(net, peer))
182                 return id ? : NET_ID_ZERO;
183         return 0;
184 }
185
186 /* Must be called from RCU-critical section or with nsid_lock held. If
187  * a new id is assigned, the bool alloc is set to true, thus the
188  * caller knows that the new id must be notified via rtnl.
189  */
190 static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
191 {
192         int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
193         bool alloc_it = *alloc;
194
195         *alloc = false;
196
197         /* Magic value for id 0. */
198         if (id == NET_ID_ZERO)
199                 return 0;
200         if (id > 0)
201                 return id;
202
203         if (alloc_it) {
204                 id = alloc_netid(net, peer, -1);
205                 *alloc = true;
206                 return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
207         }
208
209         return NETNSA_NSID_NOT_ASSIGNED;
210 }
211
212 /* Must be called from RCU-critical section or with nsid_lock held */
213 static int __peernet2id(struct net *net, struct net *peer)
214 {
215         bool no = false;
216
217         return __peernet2id_alloc(net, peer, &no);
218 }
219
220 static void rtnl_net_notifyid(struct net *net, int cmd, int id);
221 /* This function returns the id of a peer netns. If no id is assigned, one will
222  * be allocated and returned.
223  */
224 int peernet2id_alloc(struct net *net, struct net *peer)
225 {
226         bool alloc;
227         int id;
228
229         if (atomic_read(&net->count) == 0)
230                 return NETNSA_NSID_NOT_ASSIGNED;
231         spin_lock_bh(&net->nsid_lock);
232         alloc = atomic_read(&peer->count) == 0 ? false : true;
233         id = __peernet2id_alloc(net, peer, &alloc);
234         spin_unlock_bh(&net->nsid_lock);
235         if (alloc && id >= 0)
236                 rtnl_net_notifyid(net, RTM_NEWNSID, id);
237         return id;
238 }
239
240 /* This function returns, if assigned, the id of a peer netns. */
241 int peernet2id(struct net *net, struct net *peer)
242 {
243         int id;
244
245         rcu_read_lock();
246         id = __peernet2id(net, peer);
247         rcu_read_unlock();
248
249         return id;
250 }
251 EXPORT_SYMBOL(peernet2id);
252
253 /* This function returns true is the peer netns has an id assigned into the
254  * current netns.
255  */
256 bool peernet_has_id(struct net *net, struct net *peer)
257 {
258         return peernet2id(net, peer) >= 0;
259 }
260
261 struct net *get_net_ns_by_id(struct net *net, int id)
262 {
263         struct net *peer;
264
265         if (id < 0)
266                 return NULL;
267
268         rcu_read_lock();
269         spin_lock_bh(&net->nsid_lock);
270         peer = idr_find(&net->netns_ids, id);
271         if (peer)
272                 peer = maybe_get_net(peer);
273         spin_unlock_bh(&net->nsid_lock);
274         rcu_read_unlock();
275
276         return peer;
277 }
278
279 /*
280  * setup_net runs the initializers for the network namespace object.
281  */
282 static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
283 {
284         /* Must be called with net_mutex held */
285         const struct pernet_operations *ops, *saved_ops;
286         int error = 0;
287         LIST_HEAD(net_exit_list);
288
289         atomic_set(&net->count, 1);
290         refcount_set(&net->passive, 1);
291         get_random_bytes(&net->hash_mix, sizeof(u32));
292         net->dev_base_seq = 1;
293         net->user_ns = user_ns;
294         idr_init(&net->netns_ids);
295         spin_lock_init(&net->nsid_lock);
296
297         list_for_each_entry(ops, &pernet_list, list) {
298                 error = ops_init(ops, net);
299                 if (error < 0)
300                         goto out_undo;
301         }
302 out:
303         return error;
304
305 out_undo:
306         /* Walk through the list backwards calling the exit functions
307          * for the pernet modules whose init functions did not fail.
308          */
309         list_add(&net->exit_list, &net_exit_list);
310         saved_ops = ops;
311         list_for_each_entry_continue_reverse(ops, &pernet_list, list)
312                 ops_exit_list(ops, &net_exit_list);
313
314         ops = saved_ops;
315         list_for_each_entry_continue_reverse(ops, &pernet_list, list)
316                 ops_free_list(ops, &net_exit_list);
317
318         rcu_barrier();
319         goto out;
320 }
321
322 static int __net_init net_defaults_init_net(struct net *net)
323 {
324         net->core.sysctl_somaxconn = SOMAXCONN;
325         return 0;
326 }
327
328 static struct pernet_operations net_defaults_ops = {
329         .init = net_defaults_init_net,
330 };
331
332 static __init int net_defaults_init(void)
333 {
334         if (register_pernet_subsys(&net_defaults_ops))
335                 panic("Cannot initialize net default settings");
336
337         return 0;
338 }
339
340 core_initcall(net_defaults_init);
341
342 #ifdef CONFIG_NET_NS
343 static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
344 {
345         return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
346 }
347
348 static void dec_net_namespaces(struct ucounts *ucounts)
349 {
350         dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
351 }
352
353 static struct kmem_cache *net_cachep;
354 static struct workqueue_struct *netns_wq;
355
356 static struct net *net_alloc(void)
357 {
358         struct net *net = NULL;
359         struct net_generic *ng;
360
361         ng = net_alloc_generic();
362         if (!ng)
363                 goto out;
364
365         net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
366         if (!net)
367                 goto out_free;
368
369         rcu_assign_pointer(net->gen, ng);
370 out:
371         return net;
372
373 out_free:
374         kfree(ng);
375         goto out;
376 }
377
378 static void net_free(struct net *net)
379 {
380         kfree(rcu_access_pointer(net->gen));
381         kmem_cache_free(net_cachep, net);
382 }
383
384 void net_drop_ns(void *p)
385 {
386         struct net *ns = p;
387         if (ns && refcount_dec_and_test(&ns->passive))
388                 net_free(ns);
389 }
390
391 struct net *copy_net_ns(unsigned long flags,
392                         struct user_namespace *user_ns, struct net *old_net)
393 {
394         struct ucounts *ucounts;
395         struct net *net;
396         int rv;
397
398         if (!(flags & CLONE_NEWNET))
399                 return get_net(old_net);
400
401         ucounts = inc_net_namespaces(user_ns);
402         if (!ucounts)
403                 return ERR_PTR(-ENOSPC);
404
405         net = net_alloc();
406         if (!net) {
407                 dec_net_namespaces(ucounts);
408                 return ERR_PTR(-ENOMEM);
409         }
410
411         get_user_ns(user_ns);
412
413         rv = mutex_lock_killable(&net_mutex);
414         if (rv < 0) {
415                 net_free(net);
416                 dec_net_namespaces(ucounts);
417                 put_user_ns(user_ns);
418                 return ERR_PTR(rv);
419         }
420
421         net->ucounts = ucounts;
422         rv = setup_net(net, user_ns);
423         if (rv == 0) {
424                 rtnl_lock();
425                 list_add_tail_rcu(&net->list, &net_namespace_list);
426                 rtnl_unlock();
427         }
428         mutex_unlock(&net_mutex);
429         if (rv < 0) {
430                 dec_net_namespaces(ucounts);
431                 put_user_ns(user_ns);
432                 net_drop_ns(net);
433                 return ERR_PTR(rv);
434         }
435         return net;
436 }
437
438 static DEFINE_SPINLOCK(cleanup_list_lock);
439 static LIST_HEAD(cleanup_list);  /* Must hold cleanup_list_lock to touch */
440
441 static void cleanup_net(struct work_struct *work)
442 {
443         const struct pernet_operations *ops;
444         struct net *net, *tmp;
445         struct list_head net_kill_list;
446         LIST_HEAD(net_exit_list);
447
448         /* Atomically snapshot the list of namespaces to cleanup */
449         spin_lock_irq(&cleanup_list_lock);
450         list_replace_init(&cleanup_list, &net_kill_list);
451         spin_unlock_irq(&cleanup_list_lock);
452
453         mutex_lock(&net_mutex);
454
455         /* Don't let anyone else find us. */
456         rtnl_lock();
457         list_for_each_entry(net, &net_kill_list, cleanup_list) {
458                 list_del_rcu(&net->list);
459                 list_add_tail(&net->exit_list, &net_exit_list);
460                 for_each_net(tmp) {
461                         int id;
462
463                         spin_lock_bh(&tmp->nsid_lock);
464                         id = __peernet2id(tmp, net);
465                         if (id >= 0)
466                                 idr_remove(&tmp->netns_ids, id);
467                         spin_unlock_bh(&tmp->nsid_lock);
468                         if (id >= 0)
469                                 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
470                 }
471                 spin_lock_bh(&net->nsid_lock);
472                 idr_destroy(&net->netns_ids);
473                 spin_unlock_bh(&net->nsid_lock);
474
475         }
476         rtnl_unlock();
477
478         /*
479          * Another CPU might be rcu-iterating the list, wait for it.
480          * This needs to be before calling the exit() notifiers, so
481          * the rcu_barrier() below isn't sufficient alone.
482          */
483         synchronize_rcu();
484
485         /* Run all of the network namespace exit methods */
486         list_for_each_entry_reverse(ops, &pernet_list, list)
487                 ops_exit_list(ops, &net_exit_list);
488
489         /* Free the net generic variables */
490         list_for_each_entry_reverse(ops, &pernet_list, list)
491                 ops_free_list(ops, &net_exit_list);
492
493         mutex_unlock(&net_mutex);
494
495         /* Ensure there are no outstanding rcu callbacks using this
496          * network namespace.
497          */
498         rcu_barrier();
499
500         /* Finally it is safe to free my network namespace structure */
501         list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
502                 list_del_init(&net->exit_list);
503                 dec_net_namespaces(net->ucounts);
504                 put_user_ns(net->user_ns);
505                 net_drop_ns(net);
506         }
507 }
508
509 /**
510  * net_ns_barrier - wait until concurrent net_cleanup_work is done
511  *
512  * cleanup_net runs from work queue and will first remove namespaces
513  * from the global list, then run net exit functions.
514  *
515  * Call this in module exit path to make sure that all netns
516  * ->exit ops have been invoked before the function is removed.
517  */
518 void net_ns_barrier(void)
519 {
520         mutex_lock(&net_mutex);
521         mutex_unlock(&net_mutex);
522 }
523 EXPORT_SYMBOL(net_ns_barrier);
524
525 static DECLARE_WORK(net_cleanup_work, cleanup_net);
526
527 void __put_net(struct net *net)
528 {
529         /* Cleanup the network namespace in process context */
530         unsigned long flags;
531
532         spin_lock_irqsave(&cleanup_list_lock, flags);
533         list_add(&net->cleanup_list, &cleanup_list);
534         spin_unlock_irqrestore(&cleanup_list_lock, flags);
535
536         queue_work(netns_wq, &net_cleanup_work);
537 }
538 EXPORT_SYMBOL_GPL(__put_net);
539
540 struct net *get_net_ns_by_fd(int fd)
541 {
542         struct file *file;
543         struct ns_common *ns;
544         struct net *net;
545
546         file = proc_ns_fget(fd);
547         if (IS_ERR(file))
548                 return ERR_CAST(file);
549
550         ns = get_proc_ns(file_inode(file));
551         if (ns->ops == &netns_operations)
552                 net = get_net(container_of(ns, struct net, ns));
553         else
554                 net = ERR_PTR(-EINVAL);
555
556         fput(file);
557         return net;
558 }
559
560 #else
561 struct net *get_net_ns_by_fd(int fd)
562 {
563         return ERR_PTR(-EINVAL);
564 }
565 #endif
566 EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
567
568 struct net *get_net_ns_by_pid(pid_t pid)
569 {
570         struct task_struct *tsk;
571         struct net *net;
572
573         /* Lookup the network namespace */
574         net = ERR_PTR(-ESRCH);
575         rcu_read_lock();
576         tsk = find_task_by_vpid(pid);
577         if (tsk) {
578                 struct nsproxy *nsproxy;
579                 task_lock(tsk);
580                 nsproxy = tsk->nsproxy;
581                 if (nsproxy)
582                         net = get_net(nsproxy->net_ns);
583                 task_unlock(tsk);
584         }
585         rcu_read_unlock();
586         return net;
587 }
588 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
589
590 static __net_init int net_ns_net_init(struct net *net)
591 {
592 #ifdef CONFIG_NET_NS
593         net->ns.ops = &netns_operations;
594 #endif
595         return ns_alloc_inum(&net->ns);
596 }
597
598 static __net_exit void net_ns_net_exit(struct net *net)
599 {
600         ns_free_inum(&net->ns);
601 }
602
603 static struct pernet_operations __net_initdata net_ns_ops = {
604         .init = net_ns_net_init,
605         .exit = net_ns_net_exit,
606 };
607
608 static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
609         [NETNSA_NONE]           = { .type = NLA_UNSPEC },
610         [NETNSA_NSID]           = { .type = NLA_S32 },
611         [NETNSA_PID]            = { .type = NLA_U32 },
612         [NETNSA_FD]             = { .type = NLA_U32 },
613 };
614
615 static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
616                           struct netlink_ext_ack *extack)
617 {
618         struct net *net = sock_net(skb->sk);
619         struct nlattr *tb[NETNSA_MAX + 1];
620         struct nlattr *nla;
621         struct net *peer;
622         int nsid, err;
623
624         err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
625                           rtnl_net_policy, extack);
626         if (err < 0)
627                 return err;
628         if (!tb[NETNSA_NSID]) {
629                 NL_SET_ERR_MSG(extack, "nsid is missing");
630                 return -EINVAL;
631         }
632         nsid = nla_get_s32(tb[NETNSA_NSID]);
633
634         if (tb[NETNSA_PID]) {
635                 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
636                 nla = tb[NETNSA_PID];
637         } else if (tb[NETNSA_FD]) {
638                 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
639                 nla = tb[NETNSA_FD];
640         } else {
641                 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
642                 return -EINVAL;
643         }
644         if (IS_ERR(peer)) {
645                 NL_SET_BAD_ATTR(extack, nla);
646                 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
647                 return PTR_ERR(peer);
648         }
649
650         spin_lock_bh(&net->nsid_lock);
651         if (__peernet2id(net, peer) >= 0) {
652                 spin_unlock_bh(&net->nsid_lock);
653                 err = -EEXIST;
654                 NL_SET_BAD_ATTR(extack, nla);
655                 NL_SET_ERR_MSG(extack,
656                                "Peer netns already has a nsid assigned");
657                 goto out;
658         }
659
660         err = alloc_netid(net, peer, nsid);
661         spin_unlock_bh(&net->nsid_lock);
662         if (err >= 0) {
663                 rtnl_net_notifyid(net, RTM_NEWNSID, err);
664                 err = 0;
665         } else if (err == -ENOSPC && nsid >= 0) {
666                 err = -EEXIST;
667                 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
668                 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
669         }
670 out:
671         put_net(peer);
672         return err;
673 }
674
675 static int rtnl_net_get_size(void)
676 {
677         return NLMSG_ALIGN(sizeof(struct rtgenmsg))
678                + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
679                ;
680 }
681
682 static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
683                          int cmd, struct net *net, int nsid)
684 {
685         struct nlmsghdr *nlh;
686         struct rtgenmsg *rth;
687
688         nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
689         if (!nlh)
690                 return -EMSGSIZE;
691
692         rth = nlmsg_data(nlh);
693         rth->rtgen_family = AF_UNSPEC;
694
695         if (nla_put_s32(skb, NETNSA_NSID, nsid))
696                 goto nla_put_failure;
697
698         nlmsg_end(skb, nlh);
699         return 0;
700
701 nla_put_failure:
702         nlmsg_cancel(skb, nlh);
703         return -EMSGSIZE;
704 }
705
706 static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
707                           struct netlink_ext_ack *extack)
708 {
709         struct net *net = sock_net(skb->sk);
710         struct nlattr *tb[NETNSA_MAX + 1];
711         struct nlattr *nla;
712         struct sk_buff *msg;
713         struct net *peer;
714         int err, id;
715
716         err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
717                           rtnl_net_policy, extack);
718         if (err < 0)
719                 return err;
720         if (tb[NETNSA_PID]) {
721                 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
722                 nla = tb[NETNSA_PID];
723         } else if (tb[NETNSA_FD]) {
724                 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
725                 nla = tb[NETNSA_FD];
726         } else {
727                 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
728                 return -EINVAL;
729         }
730
731         if (IS_ERR(peer)) {
732                 NL_SET_BAD_ATTR(extack, nla);
733                 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
734                 return PTR_ERR(peer);
735         }
736
737         msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
738         if (!msg) {
739                 err = -ENOMEM;
740                 goto out;
741         }
742
743         id = peernet2id(net, peer);
744         err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
745                             RTM_NEWNSID, net, id);
746         if (err < 0)
747                 goto err_out;
748
749         err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
750         goto out;
751
752 err_out:
753         nlmsg_free(msg);
754 out:
755         put_net(peer);
756         return err;
757 }
758
759 struct rtnl_net_dump_cb {
760         struct net *net;
761         struct sk_buff *skb;
762         struct netlink_callback *cb;
763         int idx;
764         int s_idx;
765 };
766
767 /* Runs in RCU-critical section. */
768 static int rtnl_net_dumpid_one(int id, void *peer, void *data)
769 {
770         struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
771         int ret;
772
773         if (net_cb->idx < net_cb->s_idx)
774                 goto cont;
775
776         ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
777                             net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
778                             RTM_NEWNSID, net_cb->net, id);
779         if (ret < 0)
780                 return ret;
781
782 cont:
783         net_cb->idx++;
784         return 0;
785 }
786
787 static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
788 {
789         struct net *net = sock_net(skb->sk);
790         struct rtnl_net_dump_cb net_cb = {
791                 .net = net,
792                 .skb = skb,
793                 .cb = cb,
794                 .idx = 0,
795                 .s_idx = cb->args[0],
796         };
797
798         rcu_read_lock();
799         idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
800         rcu_read_unlock();
801
802         cb->args[0] = net_cb.idx;
803         return skb->len;
804 }
805
806 static void rtnl_net_notifyid(struct net *net, int cmd, int id)
807 {
808         struct sk_buff *msg;
809         int err = -ENOMEM;
810
811         msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
812         if (!msg)
813                 goto out;
814
815         err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
816         if (err < 0)
817                 goto err_out;
818
819         rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
820         return;
821
822 err_out:
823         nlmsg_free(msg);
824 out:
825         rtnl_set_sk_err(net, RTNLGRP_NSID, err);
826 }
827
828 static int __init net_ns_init(void)
829 {
830         struct net_generic *ng;
831
832 #ifdef CONFIG_NET_NS
833         net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
834                                         SMP_CACHE_BYTES,
835                                         SLAB_PANIC, NULL);
836
837         /* Create workqueue for cleanup */
838         netns_wq = create_singlethread_workqueue("netns");
839         if (!netns_wq)
840                 panic("Could not create netns workq");
841 #endif
842
843         ng = net_alloc_generic();
844         if (!ng)
845                 panic("Could not allocate generic netns");
846
847         rcu_assign_pointer(init_net.gen, ng);
848
849         mutex_lock(&net_mutex);
850         if (setup_net(&init_net, &init_user_ns))
851                 panic("Could not setup the initial network namespace");
852
853         init_net_initialized = true;
854
855         rtnl_lock();
856         list_add_tail_rcu(&init_net.list, &net_namespace_list);
857         rtnl_unlock();
858
859         mutex_unlock(&net_mutex);
860
861         if (register_pernet_subsys(&net_ns_ops))
862                 panic("Could not register network namespace subsystems");
863
864         rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
865                       RTNL_FLAG_DOIT_UNLOCKED);
866         rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
867                       RTNL_FLAG_DOIT_UNLOCKED);
868
869         return 0;
870 }
871
872 pure_initcall(net_ns_init);
873
874 #ifdef CONFIG_NET_NS
875 static int __register_pernet_operations(struct list_head *list,
876                                         struct pernet_operations *ops)
877 {
878         struct net *net;
879         int error;
880         LIST_HEAD(net_exit_list);
881
882         list_add_tail(&ops->list, list);
883         if (ops->init || (ops->id && ops->size)) {
884                 for_each_net(net) {
885                         error = ops_init(ops, net);
886                         if (error)
887                                 goto out_undo;
888                         list_add_tail(&net->exit_list, &net_exit_list);
889                 }
890         }
891         return 0;
892
893 out_undo:
894         /* If I have an error cleanup all namespaces I initialized */
895         list_del(&ops->list);
896         ops_exit_list(ops, &net_exit_list);
897         ops_free_list(ops, &net_exit_list);
898         return error;
899 }
900
901 static void __unregister_pernet_operations(struct pernet_operations *ops)
902 {
903         struct net *net;
904         LIST_HEAD(net_exit_list);
905
906         list_del(&ops->list);
907         for_each_net(net)
908                 list_add_tail(&net->exit_list, &net_exit_list);
909         ops_exit_list(ops, &net_exit_list);
910         ops_free_list(ops, &net_exit_list);
911 }
912
913 #else
914
915 static int __register_pernet_operations(struct list_head *list,
916                                         struct pernet_operations *ops)
917 {
918         if (!init_net_initialized) {
919                 list_add_tail(&ops->list, list);
920                 return 0;
921         }
922
923         return ops_init(ops, &init_net);
924 }
925
926 static void __unregister_pernet_operations(struct pernet_operations *ops)
927 {
928         if (!init_net_initialized) {
929                 list_del(&ops->list);
930         } else {
931                 LIST_HEAD(net_exit_list);
932                 list_add(&init_net.exit_list, &net_exit_list);
933                 ops_exit_list(ops, &net_exit_list);
934                 ops_free_list(ops, &net_exit_list);
935         }
936 }
937
938 #endif /* CONFIG_NET_NS */
939
940 static DEFINE_IDA(net_generic_ids);
941
942 static int register_pernet_operations(struct list_head *list,
943                                       struct pernet_operations *ops)
944 {
945         int error;
946
947         if (ops->id) {
948 again:
949                 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
950                 if (error < 0) {
951                         if (error == -EAGAIN) {
952                                 ida_pre_get(&net_generic_ids, GFP_KERNEL);
953                                 goto again;
954                         }
955                         return error;
956                 }
957                 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
958         }
959         error = __register_pernet_operations(list, ops);
960         if (error) {
961                 rcu_barrier();
962                 if (ops->id)
963                         ida_remove(&net_generic_ids, *ops->id);
964         }
965
966         return error;
967 }
968
969 static void unregister_pernet_operations(struct pernet_operations *ops)
970 {
971         
972         __unregister_pernet_operations(ops);
973         rcu_barrier();
974         if (ops->id)
975                 ida_remove(&net_generic_ids, *ops->id);
976 }
977
978 /**
979  *      register_pernet_subsys - register a network namespace subsystem
980  *      @ops:  pernet operations structure for the subsystem
981  *
982  *      Register a subsystem which has init and exit functions
983  *      that are called when network namespaces are created and
984  *      destroyed respectively.
985  *
986  *      When registered all network namespace init functions are
987  *      called for every existing network namespace.  Allowing kernel
988  *      modules to have a race free view of the set of network namespaces.
989  *
990  *      When a new network namespace is created all of the init
991  *      methods are called in the order in which they were registered.
992  *
993  *      When a network namespace is destroyed all of the exit methods
994  *      are called in the reverse of the order with which they were
995  *      registered.
996  */
997 int register_pernet_subsys(struct pernet_operations *ops)
998 {
999         int error;
1000         mutex_lock(&net_mutex);
1001         error =  register_pernet_operations(first_device, ops);
1002         mutex_unlock(&net_mutex);
1003         return error;
1004 }
1005 EXPORT_SYMBOL_GPL(register_pernet_subsys);
1006
1007 /**
1008  *      unregister_pernet_subsys - unregister a network namespace subsystem
1009  *      @ops: pernet operations structure to manipulate
1010  *
1011  *      Remove the pernet operations structure from the list to be
1012  *      used when network namespaces are created or destroyed.  In
1013  *      addition run the exit method for all existing network
1014  *      namespaces.
1015  */
1016 void unregister_pernet_subsys(struct pernet_operations *ops)
1017 {
1018         mutex_lock(&net_mutex);
1019         unregister_pernet_operations(ops);
1020         mutex_unlock(&net_mutex);
1021 }
1022 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1023
1024 /**
1025  *      register_pernet_device - register a network namespace device
1026  *      @ops:  pernet operations structure for the subsystem
1027  *
1028  *      Register a device which has init and exit functions
1029  *      that are called when network namespaces are created and
1030  *      destroyed respectively.
1031  *
1032  *      When registered all network namespace init functions are
1033  *      called for every existing network namespace.  Allowing kernel
1034  *      modules to have a race free view of the set of network namespaces.
1035  *
1036  *      When a new network namespace is created all of the init
1037  *      methods are called in the order in which they were registered.
1038  *
1039  *      When a network namespace is destroyed all of the exit methods
1040  *      are called in the reverse of the order with which they were
1041  *      registered.
1042  */
1043 int register_pernet_device(struct pernet_operations *ops)
1044 {
1045         int error;
1046         mutex_lock(&net_mutex);
1047         error = register_pernet_operations(&pernet_list, ops);
1048         if (!error && (first_device == &pernet_list))
1049                 first_device = &ops->list;
1050         mutex_unlock(&net_mutex);
1051         return error;
1052 }
1053 EXPORT_SYMBOL_GPL(register_pernet_device);
1054
1055 /**
1056  *      unregister_pernet_device - unregister a network namespace netdevice
1057  *      @ops: pernet operations structure to manipulate
1058  *
1059  *      Remove the pernet operations structure from the list to be
1060  *      used when network namespaces are created or destroyed.  In
1061  *      addition run the exit method for all existing network
1062  *      namespaces.
1063  */
1064 void unregister_pernet_device(struct pernet_operations *ops)
1065 {
1066         mutex_lock(&net_mutex);
1067         if (&ops->list == first_device)
1068                 first_device = first_device->next;
1069         unregister_pernet_operations(ops);
1070         mutex_unlock(&net_mutex);
1071 }
1072 EXPORT_SYMBOL_GPL(unregister_pernet_device);
1073
1074 #ifdef CONFIG_NET_NS
1075 static struct ns_common *netns_get(struct task_struct *task)
1076 {
1077         struct net *net = NULL;
1078         struct nsproxy *nsproxy;
1079
1080         task_lock(task);
1081         nsproxy = task->nsproxy;
1082         if (nsproxy)
1083                 net = get_net(nsproxy->net_ns);
1084         task_unlock(task);
1085
1086         return net ? &net->ns : NULL;
1087 }
1088
1089 static inline struct net *to_net_ns(struct ns_common *ns)
1090 {
1091         return container_of(ns, struct net, ns);
1092 }
1093
1094 static void netns_put(struct ns_common *ns)
1095 {
1096         put_net(to_net_ns(ns));
1097 }
1098
1099 static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
1100 {
1101         struct net *net = to_net_ns(ns);
1102
1103         if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
1104             !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
1105                 return -EPERM;
1106
1107         put_net(nsproxy->net_ns);
1108         nsproxy->net_ns = get_net(net);
1109         return 0;
1110 }
1111
1112 static struct user_namespace *netns_owner(struct ns_common *ns)
1113 {
1114         return to_net_ns(ns)->user_ns;
1115 }
1116
1117 const struct proc_ns_operations netns_operations = {
1118         .name           = "net",
1119         .type           = CLONE_NEWNET,
1120         .get            = netns_get,
1121         .put            = netns_put,
1122         .install        = netns_install,
1123         .owner          = netns_owner,
1124 };
1125 #endif