ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv6 / proc.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              This file implements the various access functions for the
7  *              PROC file system.  This is very similar to the IPv4 version,
8  *              except it reports the sockets in the INET6 address family.
9  *
10  * Version:     $Id: proc.c,v 1.17 2002/02/01 22:01:04 davem Exp $
11  *
12  * Authors:     David S. Miller (davem@caip.rutgers.edu)
13  *              YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
14  *
15  *              This program is free software; you can redistribute it and/or
16  *              modify it under the terms of the GNU General Public License
17  *              as published by the Free Software Foundation; either version
18  *              2 of the License, or (at your option) any later version.
19  */
20 #include <linux/config.h>
21 #include <linux/sched.h>
22 #include <linux/socket.h>
23 #include <linux/net.h>
24 #include <linux/ipv6.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/stddef.h>
28 #include <net/sock.h>
29 #include <net/tcp.h>
30 #include <net/transp_v6.h>
31 #include <net/ipv6.h>
32
33 #ifdef CONFIG_PROC_FS
34 static struct proc_dir_entry *proc_net_devsnmp6;
35
36 static int fold_prot_inuse(struct proto *proto)
37 {
38         int res = 0;
39         int cpu;
40
41         for (cpu=0; cpu<NR_CPUS; cpu++)
42                 res += proto->stats[cpu].inuse;
43
44         return res;
45 }
46
47 static int sockstat6_seq_show(struct seq_file *seq, void *v)
48 {
49         seq_printf(seq, "TCP6: inuse %d\n",
50                        fold_prot_inuse(&tcpv6_prot));
51         seq_printf(seq, "UDP6: inuse %d\n",
52                        fold_prot_inuse(&udpv6_prot));
53         seq_printf(seq, "RAW6: inuse %d\n",
54                        fold_prot_inuse(&rawv6_prot));
55         seq_printf(seq, "FRAG6: inuse %d memory %d\n",
56                        ip6_frag_nqueues, atomic_read(&ip6_frag_mem));
57         return 0;
58 }
59
60 struct snmp6_item
61 {
62         char *name;
63         int   offset;
64 };
65 #define SNMP6_SENTINEL  { .name = NULL, .offset = 0 }
66
67 static struct snmp6_item snmp6_ipv6_list[] = {
68 /* ipv6 mib according to RFC 2465 */
69 #define SNMP6_GEN(x) { .name = #x , .offset = offsetof(struct ipv6_mib, x) }
70         SNMP6_GEN(Ip6InReceives),
71         SNMP6_GEN(Ip6InHdrErrors),
72         SNMP6_GEN(Ip6InTooBigErrors),
73         SNMP6_GEN(Ip6InNoRoutes),
74         SNMP6_GEN(Ip6InAddrErrors),
75         SNMP6_GEN(Ip6InUnknownProtos),
76         SNMP6_GEN(Ip6InTruncatedPkts),
77         SNMP6_GEN(Ip6InDiscards),
78         SNMP6_GEN(Ip6InDelivers),
79         SNMP6_GEN(Ip6OutForwDatagrams),
80         SNMP6_GEN(Ip6OutRequests),
81         SNMP6_GEN(Ip6OutDiscards),
82         SNMP6_GEN(Ip6OutNoRoutes),
83         SNMP6_GEN(Ip6ReasmTimeout),
84         SNMP6_GEN(Ip6ReasmReqds),
85         SNMP6_GEN(Ip6ReasmOKs),
86         SNMP6_GEN(Ip6ReasmFails),
87         SNMP6_GEN(Ip6FragOKs),
88         SNMP6_GEN(Ip6FragFails),
89         SNMP6_GEN(Ip6FragCreates),
90         SNMP6_GEN(Ip6InMcastPkts),
91         SNMP6_GEN(Ip6OutMcastPkts),
92 #undef SNMP6_GEN
93         SNMP6_SENTINEL
94 };
95
96 static struct snmp6_item snmp6_icmp6_list[] = {
97 /* icmpv6 mib according to RFC 2466
98
99    Exceptions:  {In|Out}AdminProhibs are removed, because I see
100                 no good reasons to account them separately
101                 of another dest.unreachs.
102                 OutErrs is zero identically.
103                 OutEchos too.
104                 OutRouterAdvertisements too.
105                 OutGroupMembQueries too.
106  */
107 #define SNMP6_GEN(x) { .name = #x , .offset = offsetof(struct icmpv6_mib, x) }
108         SNMP6_GEN(Icmp6InMsgs),
109         SNMP6_GEN(Icmp6InErrors),
110         SNMP6_GEN(Icmp6InDestUnreachs),
111         SNMP6_GEN(Icmp6InPktTooBigs),
112         SNMP6_GEN(Icmp6InTimeExcds),
113         SNMP6_GEN(Icmp6InParmProblems),
114         SNMP6_GEN(Icmp6InEchos),
115         SNMP6_GEN(Icmp6InEchoReplies),
116         SNMP6_GEN(Icmp6InGroupMembQueries),
117         SNMP6_GEN(Icmp6InGroupMembResponses),
118         SNMP6_GEN(Icmp6InGroupMembReductions),
119         SNMP6_GEN(Icmp6InRouterSolicits),
120         SNMP6_GEN(Icmp6InRouterAdvertisements),
121         SNMP6_GEN(Icmp6InNeighborSolicits),
122         SNMP6_GEN(Icmp6InNeighborAdvertisements),
123         SNMP6_GEN(Icmp6InRedirects),
124         SNMP6_GEN(Icmp6OutMsgs),
125         SNMP6_GEN(Icmp6OutDestUnreachs),
126         SNMP6_GEN(Icmp6OutPktTooBigs),
127         SNMP6_GEN(Icmp6OutTimeExcds),
128         SNMP6_GEN(Icmp6OutParmProblems),
129         SNMP6_GEN(Icmp6OutEchoReplies),
130         SNMP6_GEN(Icmp6OutRouterSolicits),
131         SNMP6_GEN(Icmp6OutNeighborSolicits),
132         SNMP6_GEN(Icmp6OutNeighborAdvertisements),
133         SNMP6_GEN(Icmp6OutRedirects),
134         SNMP6_GEN(Icmp6OutGroupMembResponses),
135         SNMP6_GEN(Icmp6OutGroupMembReductions),
136 #undef SNMP6_GEN
137         SNMP6_SENTINEL
138 };
139
140 static struct snmp6_item snmp6_udp6_list[] = {
141 #define SNMP6_GEN(x) { .name = "Udp6" #x , .offset = offsetof(struct udp_mib, Udp##x) }
142         SNMP6_GEN(InDatagrams),
143         SNMP6_GEN(NoPorts),
144         SNMP6_GEN(InErrors),
145         SNMP6_GEN(OutDatagrams),
146 #undef SNMP6_GEN
147         SNMP6_SENTINEL
148 };
149
150 static unsigned long
151 fold_field(void *mib[], int offt)
152 {
153         unsigned long res = 0;
154         int i;
155  
156         for (i = 0; i < NR_CPUS; i++) {
157                 if (!cpu_possible(i))
158                         continue;
159                 res +=
160                     *((unsigned long *) (((void *)per_cpu_ptr(mib[0], i)) +
161                                          offt));
162                 res +=
163                     *((unsigned long *) (((void *)per_cpu_ptr(mib[1], i)) +
164                                          offt));
165         }
166         return res;
167 }
168
169 static inline void
170 snmp6_seq_show_item(struct seq_file *seq, void **mib, struct snmp6_item *itemlist)
171 {
172         int i;
173         for (i=0; itemlist[i].name; i++)
174                 seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name, 
175                                 fold_field(mib, itemlist[i].offset));
176 }
177
178 static int snmp6_seq_show(struct seq_file *seq, void *v)
179 {
180         struct inet6_dev *idev = (struct inet6_dev *)seq->private;
181
182         if (idev) {
183                 seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
184                 snmp6_seq_show_item(seq, (void **)idev->stats.icmpv6, snmp6_icmp6_list);
185         } else {
186                 snmp6_seq_show_item(seq, (void **)ipv6_statistics, snmp6_ipv6_list);
187                 snmp6_seq_show_item(seq, (void **)icmpv6_statistics, snmp6_icmp6_list);
188                 snmp6_seq_show_item(seq, (void **)udp_stats_in6, snmp6_udp6_list);
189         }
190         return 0;
191 }
192
193 static int sockstat6_seq_open(struct inode *inode, struct file *file)
194 {
195         return single_open(file, sockstat6_seq_show, NULL);
196 }
197
198 static struct file_operations sockstat6_seq_fops = {
199         .owner   = THIS_MODULE,
200         .open    = sockstat6_seq_open,
201         .read    = seq_read,
202         .llseek  = seq_lseek,
203         .release = single_release,
204 };
205
206 static int snmp6_seq_open(struct inode *inode, struct file *file)
207 {
208         return single_open(file, snmp6_seq_show, PDE(inode)->data);
209 }
210
211 static struct file_operations snmp6_seq_fops = {
212         .owner   = THIS_MODULE,
213         .open    = snmp6_seq_open,
214         .read    = seq_read,
215         .llseek  = seq_lseek,
216         .release = single_release,
217 };
218
219 int snmp6_register_dev(struct inet6_dev *idev)
220 {
221         int err = -ENOMEM;
222         struct proc_dir_entry *p;
223
224         if (!idev || !idev->dev)
225                 return -EINVAL;
226
227         if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib),
228                            __alignof__(struct icmpv6_mib)) < 0)
229                 goto err_icmp;
230
231         if (!proc_net_devsnmp6) {
232                 err = -ENOENT;
233                 goto err_proc;
234         }
235         p = create_proc_entry(idev->dev->name, S_IRUGO, proc_net_devsnmp6);
236         if (!p)
237                 goto err_proc;
238         p->data = idev;
239         p->proc_fops = &snmp6_seq_fops;
240
241         idev->stats.proc_dir_entry = p;
242         return 0;
243
244 err_proc:
245         snmp6_mib_free((void **)idev->stats.icmpv6);
246 err_icmp:
247         return err;
248 }
249
250 int snmp6_unregister_dev(struct inet6_dev *idev)
251 {
252         if (!proc_net_devsnmp6)
253                 return -ENOENT;
254         if (!idev || !idev->stats.proc_dir_entry)
255                 return -EINVAL;
256         remove_proc_entry(idev->stats.proc_dir_entry->name,
257                           proc_net_devsnmp6);
258         snmp6_mib_free((void **)idev->stats.icmpv6);
259
260         return 0;
261 }
262
263 int __init ipv6_misc_proc_init(void)
264 {
265         int rc = 0;
266
267         if (!proc_net_fops_create("snmp6", S_IRUGO, &snmp6_seq_fops))
268                 goto proc_snmp6_fail;
269
270         proc_net_devsnmp6 = proc_mkdir("dev_snmp6", proc_net);
271         if (!proc_net_devsnmp6)
272                 goto proc_dev_snmp6_fail;
273
274         if (!proc_net_fops_create("sockstat6", S_IRUGO, &sockstat6_seq_fops))
275                 goto proc_sockstat6_fail;
276 out:
277         return rc;
278
279 proc_sockstat6_fail:
280         proc_net_remove("dev_snmp6");
281 proc_dev_snmp6_fail:
282         proc_net_remove("snmp6");
283 proc_snmp6_fail:
284         rc = -ENOMEM;
285         goto out;
286 }
287
288 void ipv6_misc_proc_exit(void)
289 {
290         proc_net_remove("sockstat6");
291         proc_net_remove("dev_snmp6");
292         proc_net_remove("snmp6");
293 }
294
295 #else   /* CONFIG_PROC_FS */
296
297
298 int snmp6_register_dev(struct inet6_dev *idev)
299 {
300         int err = -ENOMEM;
301
302         if (!idev || !idev->dev)
303                 return -EINVAL;
304
305         if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib),
306                            __alignof__(struct icmpv6_mib)) < 0)
307                 goto err_icmp;
308
309         return 0;
310
311 err_icmp:
312         return err;
313 }
314
315 int snmp6_unregister_dev(struct inet6_dev *idev)
316 {
317         snmp6_mib_free((void **)idev->stats.icmpv6);
318         return 0;
319 }
320
321 #endif
322