patch-2_6_7-vs1_9_1_12
[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 static struct snmp_item snmp6_ipstats_list[] = {
61 /* ipv6 mib according to RFC 2465 */
62 #define SNMP6_GEN(x)    SNMP_ITEM(struct ipstats_mib, x, "Ip6" #x)
63         SNMP6_GEN(InReceives),
64         SNMP6_GEN(InHdrErrors),
65         SNMP6_GEN(InTooBigErrors),
66         SNMP6_GEN(InNoRoutes),
67         SNMP6_GEN(InAddrErrors),
68         SNMP6_GEN(InUnknownProtos),
69         SNMP6_GEN(InTruncatedPkts),
70         SNMP6_GEN(InDiscards),
71         SNMP6_GEN(InDelivers),
72         SNMP6_GEN(OutForwDatagrams),
73         SNMP6_GEN(OutRequests),
74         SNMP6_GEN(OutDiscards),
75         SNMP6_GEN(OutNoRoutes),
76         SNMP6_GEN(ReasmTimeout),
77         SNMP6_GEN(ReasmReqds),
78         SNMP6_GEN(ReasmOKs),
79         SNMP6_GEN(ReasmFails),
80         SNMP6_GEN(FragOKs),
81         SNMP6_GEN(FragFails),
82         SNMP6_GEN(FragCreates),
83         SNMP6_GEN(InMcastPkts),
84         SNMP6_GEN(OutMcastPkts),
85 #undef SNMP6_GEN
86         SNMP_ITEM_SENTINEL
87 };
88
89 static struct snmp_item snmp6_icmp6_list[] = {
90 /* icmpv6 mib according to RFC 2466
91
92    Exceptions:  {In|Out}AdminProhibs are removed, because I see
93                 no good reasons to account them separately
94                 of another dest.unreachs.
95                 OutErrs is zero identically.
96                 OutEchos too.
97                 OutRouterAdvertisements too.
98                 OutGroupMembQueries too.
99  */
100 #define SNMP6_GEN(x)    SNMP_ITEM(struct icmpv6_mib, x, #x)
101         SNMP6_GEN(Icmp6InMsgs),
102         SNMP6_GEN(Icmp6InErrors),
103         SNMP6_GEN(Icmp6InDestUnreachs),
104         SNMP6_GEN(Icmp6InPktTooBigs),
105         SNMP6_GEN(Icmp6InTimeExcds),
106         SNMP6_GEN(Icmp6InParmProblems),
107         SNMP6_GEN(Icmp6InEchos),
108         SNMP6_GEN(Icmp6InEchoReplies),
109         SNMP6_GEN(Icmp6InGroupMembQueries),
110         SNMP6_GEN(Icmp6InGroupMembResponses),
111         SNMP6_GEN(Icmp6InGroupMembReductions),
112         SNMP6_GEN(Icmp6InRouterSolicits),
113         SNMP6_GEN(Icmp6InRouterAdvertisements),
114         SNMP6_GEN(Icmp6InNeighborSolicits),
115         SNMP6_GEN(Icmp6InNeighborAdvertisements),
116         SNMP6_GEN(Icmp6InRedirects),
117         SNMP6_GEN(Icmp6OutMsgs),
118         SNMP6_GEN(Icmp6OutDestUnreachs),
119         SNMP6_GEN(Icmp6OutPktTooBigs),
120         SNMP6_GEN(Icmp6OutTimeExcds),
121         SNMP6_GEN(Icmp6OutParmProblems),
122         SNMP6_GEN(Icmp6OutEchoReplies),
123         SNMP6_GEN(Icmp6OutRouterSolicits),
124         SNMP6_GEN(Icmp6OutNeighborSolicits),
125         SNMP6_GEN(Icmp6OutNeighborAdvertisements),
126         SNMP6_GEN(Icmp6OutRedirects),
127         SNMP6_GEN(Icmp6OutGroupMembResponses),
128         SNMP6_GEN(Icmp6OutGroupMembReductions),
129 #undef SNMP6_GEN
130         SNMP_ITEM_SENTINEL
131 };
132
133 static struct snmp_item snmp6_udp6_list[] = {
134 #define SNMP6_GEN(x)    SNMP_ITEM(struct udp_mib, Udp##x, "Udp6" #x)
135         SNMP6_GEN(InDatagrams),
136         SNMP6_GEN(NoPorts),
137         SNMP6_GEN(InErrors),
138         SNMP6_GEN(OutDatagrams),
139 #undef SNMP6_GEN
140         SNMP_ITEM_SENTINEL
141 };
142
143 static unsigned long
144 fold_field(void *mib[], int offt)
145 {
146         unsigned long res = 0;
147         int i;
148  
149         for (i = 0; i < NR_CPUS; i++) {
150                 if (!cpu_possible(i))
151                         continue;
152                 res +=
153                     *((unsigned long *) (((void *)per_cpu_ptr(mib[0], i)) +
154                                          offt));
155                 res +=
156                     *((unsigned long *) (((void *)per_cpu_ptr(mib[1], i)) +
157                                          offt));
158         }
159         return res;
160 }
161
162 static inline void
163 snmp6_seq_show_item(struct seq_file *seq, void **mib, struct snmp_item *itemlist)
164 {
165         int i;
166         for (i=0; itemlist[i].name; i++)
167                 seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name, 
168                                 fold_field(mib, itemlist[i].offset));
169 }
170
171 static int snmp6_seq_show(struct seq_file *seq, void *v)
172 {
173         struct inet6_dev *idev = (struct inet6_dev *)seq->private;
174
175         if (idev) {
176                 seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
177                 snmp6_seq_show_item(seq, (void **)idev->stats.icmpv6, snmp6_icmp6_list);
178         } else {
179                 snmp6_seq_show_item(seq, (void **)ipv6_statistics, snmp6_ipstats_list);
180                 snmp6_seq_show_item(seq, (void **)icmpv6_statistics, snmp6_icmp6_list);
181                 snmp6_seq_show_item(seq, (void **)udp_stats_in6, snmp6_udp6_list);
182         }
183         return 0;
184 }
185
186 static int sockstat6_seq_open(struct inode *inode, struct file *file)
187 {
188         return single_open(file, sockstat6_seq_show, NULL);
189 }
190
191 static struct file_operations sockstat6_seq_fops = {
192         .owner   = THIS_MODULE,
193         .open    = sockstat6_seq_open,
194         .read    = seq_read,
195         .llseek  = seq_lseek,
196         .release = single_release,
197 };
198
199 static int snmp6_seq_open(struct inode *inode, struct file *file)
200 {
201         return single_open(file, snmp6_seq_show, PDE(inode)->data);
202 }
203
204 static struct file_operations snmp6_seq_fops = {
205         .owner   = THIS_MODULE,
206         .open    = snmp6_seq_open,
207         .read    = seq_read,
208         .llseek  = seq_lseek,
209         .release = single_release,
210 };
211
212 int snmp6_register_dev(struct inet6_dev *idev)
213 {
214         int err = -ENOMEM;
215         struct proc_dir_entry *p;
216
217         if (!idev || !idev->dev)
218                 return -EINVAL;
219
220         if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib),
221                            __alignof__(struct icmpv6_mib)) < 0)
222                 goto err_icmp;
223
224         if (!proc_net_devsnmp6) {
225                 err = -ENOENT;
226                 goto err_proc;
227         }
228         p = create_proc_entry(idev->dev->name, S_IRUGO, proc_net_devsnmp6);
229         if (!p)
230                 goto err_proc;
231         p->data = idev;
232         p->proc_fops = &snmp6_seq_fops;
233
234         idev->stats.proc_dir_entry = p;
235         return 0;
236
237 err_proc:
238         snmp6_mib_free((void **)idev->stats.icmpv6);
239 err_icmp:
240         return err;
241 }
242
243 int snmp6_unregister_dev(struct inet6_dev *idev)
244 {
245         if (!proc_net_devsnmp6)
246                 return -ENOENT;
247         if (!idev || !idev->stats.proc_dir_entry)
248                 return -EINVAL;
249         remove_proc_entry(idev->stats.proc_dir_entry->name,
250                           proc_net_devsnmp6);
251         snmp6_mib_free((void **)idev->stats.icmpv6);
252
253         return 0;
254 }
255
256 int __init ipv6_misc_proc_init(void)
257 {
258         int rc = 0;
259
260         if (!proc_net_fops_create("snmp6", S_IRUGO, &snmp6_seq_fops))
261                 goto proc_snmp6_fail;
262
263         proc_net_devsnmp6 = proc_mkdir("dev_snmp6", proc_net);
264         if (!proc_net_devsnmp6)
265                 goto proc_dev_snmp6_fail;
266
267         if (!proc_net_fops_create("sockstat6", S_IRUGO, &sockstat6_seq_fops))
268                 goto proc_sockstat6_fail;
269 out:
270         return rc;
271
272 proc_sockstat6_fail:
273         proc_net_remove("dev_snmp6");
274 proc_dev_snmp6_fail:
275         proc_net_remove("snmp6");
276 proc_snmp6_fail:
277         rc = -ENOMEM;
278         goto out;
279 }
280
281 void ipv6_misc_proc_exit(void)
282 {
283         proc_net_remove("sockstat6");
284         proc_net_remove("dev_snmp6");
285         proc_net_remove("snmp6");
286 }
287
288 #else   /* CONFIG_PROC_FS */
289
290
291 int snmp6_register_dev(struct inet6_dev *idev)
292 {
293         int err = -ENOMEM;
294
295         if (!idev || !idev->dev)
296                 return -EINVAL;
297
298         if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib),
299                            __alignof__(struct icmpv6_mib)) < 0)
300                 goto err_icmp;
301
302         return 0;
303
304 err_icmp:
305         return err;
306 }
307
308 int snmp6_unregister_dev(struct inet6_dev *idev)
309 {
310         snmp6_mib_free((void **)idev->stats.icmpv6);
311         return 0;
312 }
313
314 #endif
315