proc.c 11 KB

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