blob: 0afe21ee0607e6b5fe883ad5a3c93a8742d8b585 [file] [log] [blame]
David S. Miller3c12afe2007-09-12 14:18:18 +02001/*
2 * linux/fs/proc/net.c
3 *
4 * Copyright (C) 2007
5 *
6 * Author: Eric Biederman <ebiederm@xmission.com>
7 *
8 * proc net directory handling functions
9 */
10
11#include <asm/uaccess.h>
12
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/proc_fs.h>
16#include <linux/stat.h>
17#include <linux/init.h>
18#include <linux/sched.h>
19#include <linux/module.h>
20#include <linux/bitops.h>
21#include <linux/smp_lock.h>
22#include <linux/mount.h>
23#include <linux/nsproxy.h>
24#include <net/net_namespace.h>
25
26#include "internal.h"
27
28
David S. Miller3c12afe2007-09-12 14:18:18 +020029struct proc_dir_entry *proc_net_fops_create(struct net *net,
30 const char *name, mode_t mode, const struct file_operations *fops)
31{
32 struct proc_dir_entry *res;
33
34 res = create_proc_entry(name, mode, net->proc_net);
35 if (res)
36 res->proc_fops = fops;
37 return res;
38}
Daniel Lezcano36ac3132007-09-12 14:51:47 +020039EXPORT_SYMBOL_GPL(proc_net_fops_create);
David S. Miller3c12afe2007-09-12 14:18:18 +020040
41void proc_net_remove(struct net *net, const char *name)
42{
43 remove_proc_entry(name, net->proc_net);
44}
Daniel Lezcano36ac3132007-09-12 14:51:47 +020045EXPORT_SYMBOL_GPL(proc_net_remove);
David S. Miller3c12afe2007-09-12 14:18:18 +020046
Eric W. Biederman077130c2007-09-13 09:18:57 +020047struct net *get_proc_net(const struct inode *inode)
48{
49 return maybe_get_net(PDE_NET(PDE(inode)));
50}
51EXPORT_SYMBOL_GPL(get_proc_net);
52
Eric W. Biederman2b1e3002007-12-02 00:33:17 +110053static struct proc_dir_entry *shadow_pde;
David S. Miller3c12afe2007-09-12 14:18:18 +020054
Eric W. Biederman2b1e3002007-12-02 00:33:17 +110055static struct proc_dir_entry *proc_net_shadow(struct task_struct *task,
David S. Miller3c12afe2007-09-12 14:18:18 +020056 struct proc_dir_entry *de)
57{
Eric W. Biederman2b1e3002007-12-02 00:33:17 +110058 return task->nsproxy->net_ns->proc_net;
David S. Miller3c12afe2007-09-12 14:18:18 +020059}
60
Pavel Emelyanov46650792007-10-08 20:38:39 -070061static __net_init int proc_net_ns_init(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +020062{
63 struct proc_dir_entry *root, *netd, *net_statd;
64 int err;
65
66 err = -ENOMEM;
67 root = kzalloc(sizeof(*root), GFP_KERNEL);
68 if (!root)
69 goto out;
70
71 err = -EEXIST;
72 netd = proc_mkdir("net", root);
73 if (!netd)
74 goto free_root;
75
76 err = -EEXIST;
77 net_statd = proc_mkdir("stat", netd);
78 if (!net_statd)
79 goto free_net;
80
81 root->data = net;
82 netd->data = net;
83 net_statd->data = net;
84
85 net->proc_net_root = root;
86 net->proc_net = netd;
87 net->proc_net_stat = net_statd;
88 err = 0;
89
90out:
91 return err;
92free_net:
93 remove_proc_entry("net", root);
94free_root:
95 kfree(root);
96 goto out;
97}
98
Pavel Emelyanov46650792007-10-08 20:38:39 -070099static __net_exit void proc_net_ns_exit(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +0200100{
101 remove_proc_entry("stat", net->proc_net);
102 remove_proc_entry("net", net->proc_net_root);
103 kfree(net->proc_net_root);
104}
105
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800106static struct pernet_operations __net_initdata proc_net_ns_ops = {
David S. Miller3c12afe2007-09-12 14:18:18 +0200107 .init = proc_net_ns_init,
108 .exit = proc_net_ns_exit,
109};
110
Pavel Emelyanov46650792007-10-08 20:38:39 -0700111int __init proc_net_init(void)
David S. Miller3c12afe2007-09-12 14:18:18 +0200112{
Eric W. Biederman2b1e3002007-12-02 00:33:17 +1100113 shadow_pde = proc_mkdir("net", NULL);
114 shadow_pde->shadow_proc = proc_net_shadow;
David S. Miller3c12afe2007-09-12 14:18:18 +0200115
116 return register_pernet_subsys(&proc_net_ns_ops);
117}