Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/sched.h> |
| 21 | #include <linux/socket.h> |
| 22 | #include <linux/net.h> |
| 23 | #include <linux/ipv6.h> |
| 24 | #include <linux/proc_fs.h> |
| 25 | #include <linux/seq_file.h> |
| 26 | #include <linux/stddef.h> |
| 27 | #include <net/sock.h> |
| 28 | #include <net/tcp.h> |
| 29 | #include <net/transp_v6.h> |
| 30 | #include <net/ipv6.h> |
| 31 | |
| 32 | #ifdef CONFIG_PROC_FS |
| 33 | static struct proc_dir_entry *proc_net_devsnmp6; |
| 34 | |
| 35 | static int fold_prot_inuse(struct proto *proto) |
| 36 | { |
| 37 | int res = 0; |
| 38 | int cpu; |
| 39 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 40 | for_each_possible_cpu(cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | res += proto->stats[cpu].inuse; |
| 42 | |
| 43 | return res; |
| 44 | } |
| 45 | |
| 46 | static int sockstat6_seq_show(struct seq_file *seq, void *v) |
| 47 | { |
| 48 | seq_printf(seq, "TCP6: inuse %d\n", |
| 49 | fold_prot_inuse(&tcpv6_prot)); |
| 50 | seq_printf(seq, "UDP6: inuse %d\n", |
| 51 | fold_prot_inuse(&udpv6_prot)); |
| 52 | seq_printf(seq, "RAW6: inuse %d\n", |
| 53 | fold_prot_inuse(&rawv6_prot)); |
| 54 | seq_printf(seq, "FRAG6: inuse %d memory %d\n", |
| 55 | ip6_frag_nqueues, atomic_read(&ip6_frag_mem)); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static struct snmp_mib snmp6_ipstats_list[] = { |
| 60 | /* ipv6 mib according to RFC 2465 */ |
| 61 | SNMP_MIB_ITEM("Ip6InReceives", IPSTATS_MIB_INRECEIVES), |
| 62 | SNMP_MIB_ITEM("Ip6InHdrErrors", IPSTATS_MIB_INHDRERRORS), |
| 63 | SNMP_MIB_ITEM("Ip6InTooBigErrors", IPSTATS_MIB_INTOOBIGERRORS), |
| 64 | SNMP_MIB_ITEM("Ip6InNoRoutes", IPSTATS_MIB_INNOROUTES), |
| 65 | SNMP_MIB_ITEM("Ip6InAddrErrors", IPSTATS_MIB_INADDRERRORS), |
| 66 | SNMP_MIB_ITEM("Ip6InUnknownProtos", IPSTATS_MIB_INUNKNOWNPROTOS), |
| 67 | SNMP_MIB_ITEM("Ip6InTruncatedPkts", IPSTATS_MIB_INTRUNCATEDPKTS), |
| 68 | SNMP_MIB_ITEM("Ip6InDiscards", IPSTATS_MIB_INDISCARDS), |
| 69 | SNMP_MIB_ITEM("Ip6InDelivers", IPSTATS_MIB_INDELIVERS), |
| 70 | SNMP_MIB_ITEM("Ip6OutForwDatagrams", IPSTATS_MIB_OUTFORWDATAGRAMS), |
| 71 | SNMP_MIB_ITEM("Ip6OutRequests", IPSTATS_MIB_OUTREQUESTS), |
| 72 | SNMP_MIB_ITEM("Ip6OutDiscards", IPSTATS_MIB_OUTDISCARDS), |
| 73 | SNMP_MIB_ITEM("Ip6OutNoRoutes", IPSTATS_MIB_OUTNOROUTES), |
| 74 | SNMP_MIB_ITEM("Ip6ReasmTimeout", IPSTATS_MIB_REASMTIMEOUT), |
| 75 | SNMP_MIB_ITEM("Ip6ReasmReqds", IPSTATS_MIB_REASMREQDS), |
| 76 | SNMP_MIB_ITEM("Ip6ReasmOKs", IPSTATS_MIB_REASMOKS), |
| 77 | SNMP_MIB_ITEM("Ip6ReasmFails", IPSTATS_MIB_REASMFAILS), |
| 78 | SNMP_MIB_ITEM("Ip6FragOKs", IPSTATS_MIB_FRAGOKS), |
| 79 | SNMP_MIB_ITEM("Ip6FragFails", IPSTATS_MIB_FRAGFAILS), |
| 80 | SNMP_MIB_ITEM("Ip6FragCreates", IPSTATS_MIB_FRAGCREATES), |
| 81 | SNMP_MIB_ITEM("Ip6InMcastPkts", IPSTATS_MIB_INMCASTPKTS), |
| 82 | SNMP_MIB_ITEM("Ip6OutMcastPkts", IPSTATS_MIB_OUTMCASTPKTS), |
| 83 | SNMP_MIB_SENTINEL |
| 84 | }; |
| 85 | |
| 86 | static struct snmp_mib snmp6_icmp6_list[] = { |
| 87 | /* icmpv6 mib according to RFC 2466 |
| 88 | |
| 89 | Exceptions: {In|Out}AdminProhibs are removed, because I see |
| 90 | no good reasons to account them separately |
| 91 | of another dest.unreachs. |
| 92 | OutErrs is zero identically. |
| 93 | OutEchos too. |
| 94 | OutRouterAdvertisements too. |
| 95 | OutGroupMembQueries too. |
| 96 | */ |
| 97 | SNMP_MIB_ITEM("Icmp6InMsgs", ICMP6_MIB_INMSGS), |
| 98 | SNMP_MIB_ITEM("Icmp6InErrors", ICMP6_MIB_INERRORS), |
| 99 | SNMP_MIB_ITEM("Icmp6InDestUnreachs", ICMP6_MIB_INDESTUNREACHS), |
| 100 | SNMP_MIB_ITEM("Icmp6InPktTooBigs", ICMP6_MIB_INPKTTOOBIGS), |
| 101 | SNMP_MIB_ITEM("Icmp6InTimeExcds", ICMP6_MIB_INTIMEEXCDS), |
| 102 | SNMP_MIB_ITEM("Icmp6InParmProblems", ICMP6_MIB_INPARMPROBLEMS), |
| 103 | SNMP_MIB_ITEM("Icmp6InEchos", ICMP6_MIB_INECHOS), |
| 104 | SNMP_MIB_ITEM("Icmp6InEchoReplies", ICMP6_MIB_INECHOREPLIES), |
| 105 | SNMP_MIB_ITEM("Icmp6InGroupMembQueries", ICMP6_MIB_INGROUPMEMBQUERIES), |
| 106 | SNMP_MIB_ITEM("Icmp6InGroupMembResponses", ICMP6_MIB_INGROUPMEMBRESPONSES), |
| 107 | SNMP_MIB_ITEM("Icmp6InGroupMembReductions", ICMP6_MIB_INGROUPMEMBREDUCTIONS), |
| 108 | SNMP_MIB_ITEM("Icmp6InRouterSolicits", ICMP6_MIB_INROUTERSOLICITS), |
| 109 | SNMP_MIB_ITEM("Icmp6InRouterAdvertisements", ICMP6_MIB_INROUTERADVERTISEMENTS), |
| 110 | SNMP_MIB_ITEM("Icmp6InNeighborSolicits", ICMP6_MIB_INNEIGHBORSOLICITS), |
| 111 | SNMP_MIB_ITEM("Icmp6InNeighborAdvertisements", ICMP6_MIB_INNEIGHBORADVERTISEMENTS), |
| 112 | SNMP_MIB_ITEM("Icmp6InRedirects", ICMP6_MIB_INREDIRECTS), |
| 113 | SNMP_MIB_ITEM("Icmp6OutMsgs", ICMP6_MIB_OUTMSGS), |
| 114 | SNMP_MIB_ITEM("Icmp6OutDestUnreachs", ICMP6_MIB_OUTDESTUNREACHS), |
| 115 | SNMP_MIB_ITEM("Icmp6OutPktTooBigs", ICMP6_MIB_OUTPKTTOOBIGS), |
| 116 | SNMP_MIB_ITEM("Icmp6OutTimeExcds", ICMP6_MIB_OUTTIMEEXCDS), |
| 117 | SNMP_MIB_ITEM("Icmp6OutParmProblems", ICMP6_MIB_OUTPARMPROBLEMS), |
| 118 | SNMP_MIB_ITEM("Icmp6OutEchoReplies", ICMP6_MIB_OUTECHOREPLIES), |
| 119 | SNMP_MIB_ITEM("Icmp6OutRouterSolicits", ICMP6_MIB_OUTROUTERSOLICITS), |
| 120 | SNMP_MIB_ITEM("Icmp6OutNeighborSolicits", ICMP6_MIB_OUTNEIGHBORSOLICITS), |
| 121 | SNMP_MIB_ITEM("Icmp6OutNeighborAdvertisements", ICMP6_MIB_OUTNEIGHBORADVERTISEMENTS), |
| 122 | SNMP_MIB_ITEM("Icmp6OutRedirects", ICMP6_MIB_OUTREDIRECTS), |
| 123 | SNMP_MIB_ITEM("Icmp6OutGroupMembResponses", ICMP6_MIB_OUTGROUPMEMBRESPONSES), |
| 124 | SNMP_MIB_ITEM("Icmp6OutGroupMembReductions", ICMP6_MIB_OUTGROUPMEMBREDUCTIONS), |
| 125 | SNMP_MIB_SENTINEL |
| 126 | }; |
| 127 | |
| 128 | static struct snmp_mib snmp6_udp6_list[] = { |
| 129 | SNMP_MIB_ITEM("Udp6InDatagrams", UDP_MIB_INDATAGRAMS), |
| 130 | SNMP_MIB_ITEM("Udp6NoPorts", UDP_MIB_NOPORTS), |
| 131 | SNMP_MIB_ITEM("Udp6InErrors", UDP_MIB_INERRORS), |
| 132 | SNMP_MIB_ITEM("Udp6OutDatagrams", UDP_MIB_OUTDATAGRAMS), |
| 133 | SNMP_MIB_SENTINEL |
| 134 | }; |
| 135 | |
| 136 | static unsigned long |
| 137 | fold_field(void *mib[], int offt) |
| 138 | { |
| 139 | unsigned long res = 0; |
| 140 | int i; |
| 141 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 142 | for_each_possible_cpu(i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | res += *(((unsigned long *)per_cpu_ptr(mib[0], i)) + offt); |
| 144 | res += *(((unsigned long *)per_cpu_ptr(mib[1], i)) + offt); |
| 145 | } |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | static inline void |
| 150 | snmp6_seq_show_item(struct seq_file *seq, void **mib, struct snmp_mib *itemlist) |
| 151 | { |
| 152 | int i; |
| 153 | for (i=0; itemlist[i].name; i++) |
| 154 | seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name, |
| 155 | fold_field(mib, itemlist[i].entry)); |
| 156 | } |
| 157 | |
| 158 | static int snmp6_seq_show(struct seq_file *seq, void *v) |
| 159 | { |
| 160 | struct inet6_dev *idev = (struct inet6_dev *)seq->private; |
| 161 | |
| 162 | if (idev) { |
| 163 | seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex); |
| 164 | snmp6_seq_show_item(seq, (void **)idev->stats.icmpv6, snmp6_icmp6_list); |
| 165 | } else { |
| 166 | snmp6_seq_show_item(seq, (void **)ipv6_statistics, snmp6_ipstats_list); |
| 167 | snmp6_seq_show_item(seq, (void **)icmpv6_statistics, snmp6_icmp6_list); |
| 168 | snmp6_seq_show_item(seq, (void **)udp_stats_in6, snmp6_udp6_list); |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int sockstat6_seq_open(struct inode *inode, struct file *file) |
| 174 | { |
| 175 | return single_open(file, sockstat6_seq_show, NULL); |
| 176 | } |
| 177 | |
| 178 | static struct file_operations sockstat6_seq_fops = { |
| 179 | .owner = THIS_MODULE, |
| 180 | .open = sockstat6_seq_open, |
| 181 | .read = seq_read, |
| 182 | .llseek = seq_lseek, |
| 183 | .release = single_release, |
| 184 | }; |
| 185 | |
| 186 | static int snmp6_seq_open(struct inode *inode, struct file *file) |
| 187 | { |
| 188 | return single_open(file, snmp6_seq_show, PDE(inode)->data); |
| 189 | } |
| 190 | |
| 191 | static struct file_operations snmp6_seq_fops = { |
| 192 | .owner = THIS_MODULE, |
| 193 | .open = snmp6_seq_open, |
| 194 | .read = seq_read, |
| 195 | .llseek = seq_lseek, |
| 196 | .release = single_release, |
| 197 | }; |
| 198 | |
| 199 | int snmp6_register_dev(struct inet6_dev *idev) |
| 200 | { |
| 201 | struct proc_dir_entry *p; |
| 202 | |
| 203 | if (!idev || !idev->dev) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | if (!proc_net_devsnmp6) |
| 207 | return -ENOENT; |
| 208 | |
| 209 | p = create_proc_entry(idev->dev->name, S_IRUGO, proc_net_devsnmp6); |
| 210 | if (!p) |
| 211 | return -ENOMEM; |
| 212 | |
| 213 | p->data = idev; |
| 214 | p->proc_fops = &snmp6_seq_fops; |
| 215 | |
| 216 | idev->stats.proc_dir_entry = p; |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int snmp6_unregister_dev(struct inet6_dev *idev) |
| 221 | { |
| 222 | if (!proc_net_devsnmp6) |
| 223 | return -ENOENT; |
| 224 | if (!idev || !idev->stats.proc_dir_entry) |
| 225 | return -EINVAL; |
| 226 | remove_proc_entry(idev->stats.proc_dir_entry->name, |
| 227 | proc_net_devsnmp6); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int __init ipv6_misc_proc_init(void) |
| 232 | { |
| 233 | int rc = 0; |
| 234 | |
| 235 | if (!proc_net_fops_create("snmp6", S_IRUGO, &snmp6_seq_fops)) |
| 236 | goto proc_snmp6_fail; |
| 237 | |
| 238 | proc_net_devsnmp6 = proc_mkdir("dev_snmp6", proc_net); |
| 239 | if (!proc_net_devsnmp6) |
| 240 | goto proc_dev_snmp6_fail; |
| 241 | |
| 242 | if (!proc_net_fops_create("sockstat6", S_IRUGO, &sockstat6_seq_fops)) |
| 243 | goto proc_sockstat6_fail; |
| 244 | out: |
| 245 | return rc; |
| 246 | |
| 247 | proc_sockstat6_fail: |
| 248 | proc_net_remove("dev_snmp6"); |
| 249 | proc_dev_snmp6_fail: |
| 250 | proc_net_remove("snmp6"); |
| 251 | proc_snmp6_fail: |
| 252 | rc = -ENOMEM; |
| 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | void ipv6_misc_proc_exit(void) |
| 257 | { |
| 258 | proc_net_remove("sockstat6"); |
| 259 | proc_net_remove("dev_snmp6"); |
| 260 | proc_net_remove("snmp6"); |
| 261 | } |
| 262 | |
| 263 | #else /* CONFIG_PROC_FS */ |
| 264 | |
| 265 | |
| 266 | int snmp6_register_dev(struct inet6_dev *idev) |
| 267 | { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int snmp6_unregister_dev(struct inet6_dev *idev) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
| 275 | #endif /* CONFIG_PROC_FS */ |
| 276 | |
| 277 | int snmp6_alloc_dev(struct inet6_dev *idev) |
| 278 | { |
| 279 | int err = -ENOMEM; |
| 280 | |
| 281 | if (!idev || !idev->dev) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | if (snmp6_mib_init((void **)idev->stats.icmpv6, sizeof(struct icmpv6_mib), |
| 285 | __alignof__(struct icmpv6_mib)) < 0) |
| 286 | goto err_icmp; |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | err_icmp: |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | int snmp6_free_dev(struct inet6_dev *idev) |
| 295 | { |
| 296 | snmp6_mib_free((void **)idev->stats.icmpv6); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | |