blob: aba94316b77386ec3a2cd8ca23f2f9e5d0605ed4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/socket.h>
21#include <linux/net.h>
22#include <linux/ipv6.h>
23#include <linux/proc_fs.h>
24#include <linux/seq_file.h>
25#include <linux/stddef.h>
26#include <net/sock.h>
27#include <net/tcp.h>
28#include <net/transp_v6.h>
29#include <net/ipv6.h>
30
31#ifdef CONFIG_PROC_FS
32static struct proc_dir_entry *proc_net_devsnmp6;
33
34static int fold_prot_inuse(struct proto *proto)
35{
36 int res = 0;
37 int cpu;
38
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070039 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 res += proto->stats[cpu].inuse;
41
42 return res;
43}
44
45static int sockstat6_seq_show(struct seq_file *seq, void *v)
46{
47 seq_printf(seq, "TCP6: inuse %d\n",
48 fold_prot_inuse(&tcpv6_prot));
49 seq_printf(seq, "UDP6: inuse %d\n",
50 fold_prot_inuse(&udpv6_prot));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080051 seq_printf(seq, "UDPLITE6: inuse %d\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090052 fold_prot_inuse(&udplitev6_prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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
60static struct snmp_mib snmp6_ipstats_list[] = {
61/* ipv6 mib according to RFC 2465 */
62 SNMP_MIB_ITEM("Ip6InReceives", IPSTATS_MIB_INRECEIVES),
63 SNMP_MIB_ITEM("Ip6InHdrErrors", IPSTATS_MIB_INHDRERRORS),
64 SNMP_MIB_ITEM("Ip6InTooBigErrors", IPSTATS_MIB_INTOOBIGERRORS),
65 SNMP_MIB_ITEM("Ip6InNoRoutes", IPSTATS_MIB_INNOROUTES),
66 SNMP_MIB_ITEM("Ip6InAddrErrors", IPSTATS_MIB_INADDRERRORS),
67 SNMP_MIB_ITEM("Ip6InUnknownProtos", IPSTATS_MIB_INUNKNOWNPROTOS),
68 SNMP_MIB_ITEM("Ip6InTruncatedPkts", IPSTATS_MIB_INTRUNCATEDPKTS),
69 SNMP_MIB_ITEM("Ip6InDiscards", IPSTATS_MIB_INDISCARDS),
70 SNMP_MIB_ITEM("Ip6InDelivers", IPSTATS_MIB_INDELIVERS),
71 SNMP_MIB_ITEM("Ip6OutForwDatagrams", IPSTATS_MIB_OUTFORWDATAGRAMS),
72 SNMP_MIB_ITEM("Ip6OutRequests", IPSTATS_MIB_OUTREQUESTS),
73 SNMP_MIB_ITEM("Ip6OutDiscards", IPSTATS_MIB_OUTDISCARDS),
74 SNMP_MIB_ITEM("Ip6OutNoRoutes", IPSTATS_MIB_OUTNOROUTES),
75 SNMP_MIB_ITEM("Ip6ReasmTimeout", IPSTATS_MIB_REASMTIMEOUT),
76 SNMP_MIB_ITEM("Ip6ReasmReqds", IPSTATS_MIB_REASMREQDS),
77 SNMP_MIB_ITEM("Ip6ReasmOKs", IPSTATS_MIB_REASMOKS),
78 SNMP_MIB_ITEM("Ip6ReasmFails", IPSTATS_MIB_REASMFAILS),
79 SNMP_MIB_ITEM("Ip6FragOKs", IPSTATS_MIB_FRAGOKS),
80 SNMP_MIB_ITEM("Ip6FragFails", IPSTATS_MIB_FRAGFAILS),
81 SNMP_MIB_ITEM("Ip6FragCreates", IPSTATS_MIB_FRAGCREATES),
82 SNMP_MIB_ITEM("Ip6InMcastPkts", IPSTATS_MIB_INMCASTPKTS),
83 SNMP_MIB_ITEM("Ip6OutMcastPkts", IPSTATS_MIB_OUTMCASTPKTS),
84 SNMP_MIB_SENTINEL
85};
86
87static struct snmp_mib snmp6_icmp6_list[] = {
88/* icmpv6 mib according to RFC 2466
89
90 Exceptions: {In|Out}AdminProhibs are removed, because I see
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090091 no good reasons to account them separately
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 of another dest.unreachs.
93 OutErrs is zero identically.
94 OutEchos too.
95 OutRouterAdvertisements too.
96 OutGroupMembQueries too.
97 */
98 SNMP_MIB_ITEM("Icmp6InMsgs", ICMP6_MIB_INMSGS),
99 SNMP_MIB_ITEM("Icmp6InErrors", ICMP6_MIB_INERRORS),
100 SNMP_MIB_ITEM("Icmp6InDestUnreachs", ICMP6_MIB_INDESTUNREACHS),
101 SNMP_MIB_ITEM("Icmp6InPktTooBigs", ICMP6_MIB_INPKTTOOBIGS),
102 SNMP_MIB_ITEM("Icmp6InTimeExcds", ICMP6_MIB_INTIMEEXCDS),
103 SNMP_MIB_ITEM("Icmp6InParmProblems", ICMP6_MIB_INPARMPROBLEMS),
104 SNMP_MIB_ITEM("Icmp6InEchos", ICMP6_MIB_INECHOS),
105 SNMP_MIB_ITEM("Icmp6InEchoReplies", ICMP6_MIB_INECHOREPLIES),
106 SNMP_MIB_ITEM("Icmp6InGroupMembQueries", ICMP6_MIB_INGROUPMEMBQUERIES),
107 SNMP_MIB_ITEM("Icmp6InGroupMembResponses", ICMP6_MIB_INGROUPMEMBRESPONSES),
108 SNMP_MIB_ITEM("Icmp6InGroupMembReductions", ICMP6_MIB_INGROUPMEMBREDUCTIONS),
109 SNMP_MIB_ITEM("Icmp6InRouterSolicits", ICMP6_MIB_INROUTERSOLICITS),
110 SNMP_MIB_ITEM("Icmp6InRouterAdvertisements", ICMP6_MIB_INROUTERADVERTISEMENTS),
111 SNMP_MIB_ITEM("Icmp6InNeighborSolicits", ICMP6_MIB_INNEIGHBORSOLICITS),
112 SNMP_MIB_ITEM("Icmp6InNeighborAdvertisements", ICMP6_MIB_INNEIGHBORADVERTISEMENTS),
113 SNMP_MIB_ITEM("Icmp6InRedirects", ICMP6_MIB_INREDIRECTS),
114 SNMP_MIB_ITEM("Icmp6OutMsgs", ICMP6_MIB_OUTMSGS),
115 SNMP_MIB_ITEM("Icmp6OutDestUnreachs", ICMP6_MIB_OUTDESTUNREACHS),
116 SNMP_MIB_ITEM("Icmp6OutPktTooBigs", ICMP6_MIB_OUTPKTTOOBIGS),
117 SNMP_MIB_ITEM("Icmp6OutTimeExcds", ICMP6_MIB_OUTTIMEEXCDS),
118 SNMP_MIB_ITEM("Icmp6OutParmProblems", ICMP6_MIB_OUTPARMPROBLEMS),
119 SNMP_MIB_ITEM("Icmp6OutEchoReplies", ICMP6_MIB_OUTECHOREPLIES),
120 SNMP_MIB_ITEM("Icmp6OutRouterSolicits", ICMP6_MIB_OUTROUTERSOLICITS),
121 SNMP_MIB_ITEM("Icmp6OutNeighborSolicits", ICMP6_MIB_OUTNEIGHBORSOLICITS),
122 SNMP_MIB_ITEM("Icmp6OutNeighborAdvertisements", ICMP6_MIB_OUTNEIGHBORADVERTISEMENTS),
123 SNMP_MIB_ITEM("Icmp6OutRedirects", ICMP6_MIB_OUTREDIRECTS),
124 SNMP_MIB_ITEM("Icmp6OutGroupMembResponses", ICMP6_MIB_OUTGROUPMEMBRESPONSES),
125 SNMP_MIB_ITEM("Icmp6OutGroupMembReductions", ICMP6_MIB_OUTGROUPMEMBREDUCTIONS),
126 SNMP_MIB_SENTINEL
127};
128
129static struct snmp_mib snmp6_udp6_list[] = {
130 SNMP_MIB_ITEM("Udp6InDatagrams", UDP_MIB_INDATAGRAMS),
131 SNMP_MIB_ITEM("Udp6NoPorts", UDP_MIB_NOPORTS),
132 SNMP_MIB_ITEM("Udp6InErrors", UDP_MIB_INERRORS),
133 SNMP_MIB_ITEM("Udp6OutDatagrams", UDP_MIB_OUTDATAGRAMS),
134 SNMP_MIB_SENTINEL
135};
136
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800137static struct snmp_mib snmp6_udplite6_list[] = {
138 SNMP_MIB_ITEM("UdpLite6InDatagrams", UDP_MIB_INDATAGRAMS),
139 SNMP_MIB_ITEM("UdpLite6NoPorts", UDP_MIB_NOPORTS),
140 SNMP_MIB_ITEM("UdpLite6InErrors", UDP_MIB_INERRORS),
141 SNMP_MIB_ITEM("UdpLite6OutDatagrams", UDP_MIB_OUTDATAGRAMS),
142 SNMP_MIB_SENTINEL
143};
YOSHIFUJI Hideaki1370b5a2007-04-20 15:57:45 -0700144#endif /* CONFIG_PROC_FS */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static unsigned long
147fold_field(void *mib[], int offt)
148{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900149 unsigned long res = 0;
150 int i;
151
152 for_each_possible_cpu(i) {
153 res += *(((unsigned long *)per_cpu_ptr(mib[0], i)) + offt);
154 res += *(((unsigned long *)per_cpu_ptr(mib[1], i)) + offt);
155 }
156 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
YOSHIFUJI Hideaki1370b5a2007-04-20 15:57:45 -0700159#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static inline void
161snmp6_seq_show_item(struct seq_file *seq, void **mib, struct snmp_mib *itemlist)
162{
163 int i;
164 for (i=0; itemlist[i].name; i++)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900165 seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 fold_field(mib, itemlist[i].entry));
167}
168
169static int snmp6_seq_show(struct seq_file *seq, void *v)
170{
171 struct inet6_dev *idev = (struct inet6_dev *)seq->private;
172
173 if (idev) {
174 seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900175 snmp6_seq_show_item(seq, (void **)idev->stats.ipv6, snmp6_ipstats_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 snmp6_seq_show_item(seq, (void **)idev->stats.icmpv6, snmp6_icmp6_list);
177 } else {
178 snmp6_seq_show_item(seq, (void **)ipv6_statistics, snmp6_ipstats_list);
179 snmp6_seq_show_item(seq, (void **)icmpv6_statistics, snmp6_icmp6_list);
180 snmp6_seq_show_item(seq, (void **)udp_stats_in6, snmp6_udp6_list);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800181 snmp6_seq_show_item(seq, (void **)udplite_stats_in6, snmp6_udplite6_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 return 0;
184}
185
186static int sockstat6_seq_open(struct inode *inode, struct file *file)
187{
188 return single_open(file, sockstat6_seq_show, NULL);
189}
190
Arjan van de Ven9a321442007-02-12 00:55:35 -0800191static const struct file_operations sockstat6_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .owner = THIS_MODULE,
193 .open = sockstat6_seq_open,
194 .read = seq_read,
195 .llseek = seq_lseek,
196 .release = single_release,
197};
198
199static int snmp6_seq_open(struct inode *inode, struct file *file)
200{
201 return single_open(file, snmp6_seq_show, PDE(inode)->data);
202}
203
Arjan van de Ven9a321442007-02-12 00:55:35 -0800204static const struct file_operations snmp6_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .owner = THIS_MODULE,
206 .open = snmp6_seq_open,
207 .read = seq_read,
208 .llseek = seq_lseek,
209 .release = single_release,
210};
YOSHIFUJI Hideaki1370b5a2007-04-20 15:57:45 -0700211#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
YOSHIFUJI Hideaki2334e9732007-04-21 20:12:43 +0900213/*
214 * Stats may not be aligned for u64, so use memcpy to avoid
215 * unaligned accesses.
216 */
217static inline void __set_u64(void *p, u64 v)
218{
219 memcpy(p, &v, sizeof(u64));
220}
221
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700222static inline void
YOSHIFUJI Hideaki2334e9732007-04-21 20:12:43 +0900223__snmp6_fill_stats(void *stats, void **mib, int items, int bytes)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700224{
225 int i;
YOSHIFUJI Hideaki2334e9732007-04-21 20:12:43 +0900226 u8 *p = stats;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700227 int pad = bytes - sizeof(u64) * items;
228 BUG_ON(pad < 0);
YOSHIFUJI Hideaki2334e9732007-04-21 20:12:43 +0900229 __set_u64(p, items);
230 for (i = 1, p += sizeof(u64); i < items; i++, p += sizeof(u64))
231 __set_u64(p, fold_field(mib, i));
232 memset(p, 0, pad);
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700233}
234
235void
YOSHIFUJI Hideaki2334e9732007-04-21 20:12:43 +0900236snmp6_fill_stats(void *stats, struct inet6_dev *idev, int attrtype, int bytes)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700237{
238 switch(attrtype) {
239 case IFLA_INET6_STATS:
240 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
241 break;
242 case IFLA_INET6_ICMP6STATS:
243 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
244 break;
245 }
246}
247
YOSHIFUJI Hideaki1370b5a2007-04-20 15:57:45 -0700248#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249int snmp6_register_dev(struct inet6_dev *idev)
250{
251 struct proc_dir_entry *p;
252
253 if (!idev || !idev->dev)
254 return -EINVAL;
255
256 if (!proc_net_devsnmp6)
257 return -ENOENT;
258
259 p = create_proc_entry(idev->dev->name, S_IRUGO, proc_net_devsnmp6);
260 if (!p)
261 return -ENOMEM;
262
263 p->data = idev;
264 p->proc_fops = &snmp6_seq_fops;
265
266 idev->stats.proc_dir_entry = p;
267 return 0;
268}
269
270int snmp6_unregister_dev(struct inet6_dev *idev)
271{
272 if (!proc_net_devsnmp6)
273 return -ENOENT;
274 if (!idev || !idev->stats.proc_dir_entry)
275 return -EINVAL;
276 remove_proc_entry(idev->stats.proc_dir_entry->name,
277 proc_net_devsnmp6);
278 return 0;
279}
280
281int __init ipv6_misc_proc_init(void)
282{
283 int rc = 0;
284
285 if (!proc_net_fops_create("snmp6", S_IRUGO, &snmp6_seq_fops))
286 goto proc_snmp6_fail;
287
288 proc_net_devsnmp6 = proc_mkdir("dev_snmp6", proc_net);
289 if (!proc_net_devsnmp6)
290 goto proc_dev_snmp6_fail;
291
292 if (!proc_net_fops_create("sockstat6", S_IRUGO, &sockstat6_seq_fops))
293 goto proc_sockstat6_fail;
294out:
295 return rc;
296
297proc_sockstat6_fail:
298 proc_net_remove("dev_snmp6");
299proc_dev_snmp6_fail:
300 proc_net_remove("snmp6");
301proc_snmp6_fail:
302 rc = -ENOMEM;
303 goto out;
304}
305
306void ipv6_misc_proc_exit(void)
307{
308 proc_net_remove("sockstat6");
309 proc_net_remove("dev_snmp6");
310 proc_net_remove("snmp6");
311}
312
313#else /* CONFIG_PROC_FS */
314
315
316int snmp6_register_dev(struct inet6_dev *idev)
317{
318 return 0;
319}
320
321int snmp6_unregister_dev(struct inet6_dev *idev)
322{
323 return 0;
324}
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif /* CONFIG_PROC_FS */
327
328int snmp6_alloc_dev(struct inet6_dev *idev)
329{
330 int err = -ENOMEM;
331
332 if (!idev || !idev->dev)
333 return -EINVAL;
334
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900335 if (snmp6_mib_init((void **)idev->stats.ipv6, sizeof(struct ipstats_mib),
336 __alignof__(struct ipstats_mib)) < 0)
337 goto err_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib),
339 __alignof__(struct icmpv6_mib)) < 0)
340 goto err_icmp;
341
342 return 0;
343
344err_icmp:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900345 snmp6_mib_free((void **)idev->stats.ipv6);
346err_ip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return err;
348}
349
350int snmp6_free_dev(struct inet6_dev *idev)
351{
352 snmp6_mib_free((void **)idev->stats.icmpv6);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900353 snmp6_mib_free((void **)idev->stats.ipv6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
YOSHIFUJI Hideaki49ed67a92007-04-20 15:56:48 -0700357int snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
358{
359 if (ptr == NULL)
360 return -EINVAL;
361
362 ptr[0] = __alloc_percpu(mibsize);
363 if (!ptr[0])
364 goto err0;
365
366 ptr[1] = __alloc_percpu(mibsize);
367 if (!ptr[1])
368 goto err1;
369
370 return 0;
371
372err1:
373 free_percpu(ptr[0]);
374 ptr[0] = NULL;
375err0:
376 return -ENOMEM;
377}
378
379void snmp6_mib_free(void *ptr[2])
380{
381 if (ptr == NULL)
382 return;
383 free_percpu(ptr[0]);
384 free_percpu(ptr[1]);
385 ptr[0] = ptr[1] = NULL;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387