Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET is implemented using the  BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      This file implements the various access functions for the
0008  *      PROC file system.  This is very similar to the IPv4 version,
0009  *      except it reports the sockets in the INET6 address family.
0010  *
0011  * Authors: David S. Miller (davem@caip.rutgers.edu)
0012  *      YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
0013  */
0014 #include <linux/socket.h>
0015 #include <linux/net.h>
0016 #include <linux/ipv6.h>
0017 #include <linux/proc_fs.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/stddef.h>
0020 #include <linux/export.h>
0021 #include <net/net_namespace.h>
0022 #include <net/ip.h>
0023 #include <net/sock.h>
0024 #include <net/tcp.h>
0025 #include <net/udp.h>
0026 #include <net/transp_v6.h>
0027 #include <net/ipv6.h>
0028 
0029 #define MAX4(a, b, c, d) \
0030     max_t(u32, max_t(u32, a, b), max_t(u32, c, d))
0031 #define SNMP_MIB_MAX MAX4(UDP_MIB_MAX, TCP_MIB_MAX, \
0032             IPSTATS_MIB_MAX, ICMP_MIB_MAX)
0033 
0034 static int sockstat6_seq_show(struct seq_file *seq, void *v)
0035 {
0036     struct net *net = seq->private;
0037 
0038     seq_printf(seq, "TCP6: inuse %d\n",
0039                sock_prot_inuse_get(net, &tcpv6_prot));
0040     seq_printf(seq, "UDP6: inuse %d\n",
0041                sock_prot_inuse_get(net, &udpv6_prot));
0042     seq_printf(seq, "UDPLITE6: inuse %d\n",
0043             sock_prot_inuse_get(net, &udplitev6_prot));
0044     seq_printf(seq, "RAW6: inuse %d\n",
0045                sock_prot_inuse_get(net, &rawv6_prot));
0046     seq_printf(seq, "FRAG6: inuse %u memory %lu\n",
0047            atomic_read(&net->ipv6.fqdir->rhashtable.nelems),
0048            frag_mem_limit(net->ipv6.fqdir));
0049     return 0;
0050 }
0051 
0052 static const struct snmp_mib snmp6_ipstats_list[] = {
0053 /* ipv6 mib according to RFC 2465 */
0054     SNMP_MIB_ITEM("Ip6InReceives", IPSTATS_MIB_INPKTS),
0055     SNMP_MIB_ITEM("Ip6InHdrErrors", IPSTATS_MIB_INHDRERRORS),
0056     SNMP_MIB_ITEM("Ip6InTooBigErrors", IPSTATS_MIB_INTOOBIGERRORS),
0057     SNMP_MIB_ITEM("Ip6InNoRoutes", IPSTATS_MIB_INNOROUTES),
0058     SNMP_MIB_ITEM("Ip6InAddrErrors", IPSTATS_MIB_INADDRERRORS),
0059     SNMP_MIB_ITEM("Ip6InUnknownProtos", IPSTATS_MIB_INUNKNOWNPROTOS),
0060     SNMP_MIB_ITEM("Ip6InTruncatedPkts", IPSTATS_MIB_INTRUNCATEDPKTS),
0061     SNMP_MIB_ITEM("Ip6InDiscards", IPSTATS_MIB_INDISCARDS),
0062     SNMP_MIB_ITEM("Ip6InDelivers", IPSTATS_MIB_INDELIVERS),
0063     SNMP_MIB_ITEM("Ip6OutForwDatagrams", IPSTATS_MIB_OUTFORWDATAGRAMS),
0064     SNMP_MIB_ITEM("Ip6OutRequests", IPSTATS_MIB_OUTPKTS),
0065     SNMP_MIB_ITEM("Ip6OutDiscards", IPSTATS_MIB_OUTDISCARDS),
0066     SNMP_MIB_ITEM("Ip6OutNoRoutes", IPSTATS_MIB_OUTNOROUTES),
0067     SNMP_MIB_ITEM("Ip6ReasmTimeout", IPSTATS_MIB_REASMTIMEOUT),
0068     SNMP_MIB_ITEM("Ip6ReasmReqds", IPSTATS_MIB_REASMREQDS),
0069     SNMP_MIB_ITEM("Ip6ReasmOKs", IPSTATS_MIB_REASMOKS),
0070     SNMP_MIB_ITEM("Ip6ReasmFails", IPSTATS_MIB_REASMFAILS),
0071     SNMP_MIB_ITEM("Ip6FragOKs", IPSTATS_MIB_FRAGOKS),
0072     SNMP_MIB_ITEM("Ip6FragFails", IPSTATS_MIB_FRAGFAILS),
0073     SNMP_MIB_ITEM("Ip6FragCreates", IPSTATS_MIB_FRAGCREATES),
0074     SNMP_MIB_ITEM("Ip6InMcastPkts", IPSTATS_MIB_INMCASTPKTS),
0075     SNMP_MIB_ITEM("Ip6OutMcastPkts", IPSTATS_MIB_OUTMCASTPKTS),
0076     SNMP_MIB_ITEM("Ip6InOctets", IPSTATS_MIB_INOCTETS),
0077     SNMP_MIB_ITEM("Ip6OutOctets", IPSTATS_MIB_OUTOCTETS),
0078     SNMP_MIB_ITEM("Ip6InMcastOctets", IPSTATS_MIB_INMCASTOCTETS),
0079     SNMP_MIB_ITEM("Ip6OutMcastOctets", IPSTATS_MIB_OUTMCASTOCTETS),
0080     SNMP_MIB_ITEM("Ip6InBcastOctets", IPSTATS_MIB_INBCASTOCTETS),
0081     SNMP_MIB_ITEM("Ip6OutBcastOctets", IPSTATS_MIB_OUTBCASTOCTETS),
0082     /* IPSTATS_MIB_CSUMERRORS is not relevant in IPv6 (no checksum) */
0083     SNMP_MIB_ITEM("Ip6InNoECTPkts", IPSTATS_MIB_NOECTPKTS),
0084     SNMP_MIB_ITEM("Ip6InECT1Pkts", IPSTATS_MIB_ECT1PKTS),
0085     SNMP_MIB_ITEM("Ip6InECT0Pkts", IPSTATS_MIB_ECT0PKTS),
0086     SNMP_MIB_ITEM("Ip6InCEPkts", IPSTATS_MIB_CEPKTS),
0087     SNMP_MIB_SENTINEL
0088 };
0089 
0090 static const struct snmp_mib snmp6_icmp6_list[] = {
0091 /* icmpv6 mib according to RFC 2466 */
0092     SNMP_MIB_ITEM("Icmp6InMsgs", ICMP6_MIB_INMSGS),
0093     SNMP_MIB_ITEM("Icmp6InErrors", ICMP6_MIB_INERRORS),
0094     SNMP_MIB_ITEM("Icmp6OutMsgs", ICMP6_MIB_OUTMSGS),
0095     SNMP_MIB_ITEM("Icmp6OutErrors", ICMP6_MIB_OUTERRORS),
0096     SNMP_MIB_ITEM("Icmp6InCsumErrors", ICMP6_MIB_CSUMERRORS),
0097     SNMP_MIB_SENTINEL
0098 };
0099 
0100 /* RFC 4293 v6 ICMPMsgStatsTable; named items for RFC 2466 compatibility */
0101 static const char *const icmp6type2name[256] = {
0102     [ICMPV6_DEST_UNREACH] = "DestUnreachs",
0103     [ICMPV6_PKT_TOOBIG] = "PktTooBigs",
0104     [ICMPV6_TIME_EXCEED] = "TimeExcds",
0105     [ICMPV6_PARAMPROB] = "ParmProblems",
0106     [ICMPV6_ECHO_REQUEST] = "Echos",
0107     [ICMPV6_ECHO_REPLY] = "EchoReplies",
0108     [ICMPV6_MGM_QUERY] = "GroupMembQueries",
0109     [ICMPV6_MGM_REPORT] = "GroupMembResponses",
0110     [ICMPV6_MGM_REDUCTION] = "GroupMembReductions",
0111     [ICMPV6_MLD2_REPORT] = "MLDv2Reports",
0112     [NDISC_ROUTER_ADVERTISEMENT] = "RouterAdvertisements",
0113     [NDISC_ROUTER_SOLICITATION] = "RouterSolicits",
0114     [NDISC_NEIGHBOUR_ADVERTISEMENT] = "NeighborAdvertisements",
0115     [NDISC_NEIGHBOUR_SOLICITATION] = "NeighborSolicits",
0116     [NDISC_REDIRECT] = "Redirects",
0117 };
0118 
0119 
0120 static const struct snmp_mib snmp6_udp6_list[] = {
0121     SNMP_MIB_ITEM("Udp6InDatagrams", UDP_MIB_INDATAGRAMS),
0122     SNMP_MIB_ITEM("Udp6NoPorts", UDP_MIB_NOPORTS),
0123     SNMP_MIB_ITEM("Udp6InErrors", UDP_MIB_INERRORS),
0124     SNMP_MIB_ITEM("Udp6OutDatagrams", UDP_MIB_OUTDATAGRAMS),
0125     SNMP_MIB_ITEM("Udp6RcvbufErrors", UDP_MIB_RCVBUFERRORS),
0126     SNMP_MIB_ITEM("Udp6SndbufErrors", UDP_MIB_SNDBUFERRORS),
0127     SNMP_MIB_ITEM("Udp6InCsumErrors", UDP_MIB_CSUMERRORS),
0128     SNMP_MIB_ITEM("Udp6IgnoredMulti", UDP_MIB_IGNOREDMULTI),
0129     SNMP_MIB_ITEM("Udp6MemErrors", UDP_MIB_MEMERRORS),
0130     SNMP_MIB_SENTINEL
0131 };
0132 
0133 static const struct snmp_mib snmp6_udplite6_list[] = {
0134     SNMP_MIB_ITEM("UdpLite6InDatagrams", UDP_MIB_INDATAGRAMS),
0135     SNMP_MIB_ITEM("UdpLite6NoPorts", UDP_MIB_NOPORTS),
0136     SNMP_MIB_ITEM("UdpLite6InErrors", UDP_MIB_INERRORS),
0137     SNMP_MIB_ITEM("UdpLite6OutDatagrams", UDP_MIB_OUTDATAGRAMS),
0138     SNMP_MIB_ITEM("UdpLite6RcvbufErrors", UDP_MIB_RCVBUFERRORS),
0139     SNMP_MIB_ITEM("UdpLite6SndbufErrors", UDP_MIB_SNDBUFERRORS),
0140     SNMP_MIB_ITEM("UdpLite6InCsumErrors", UDP_MIB_CSUMERRORS),
0141     SNMP_MIB_ITEM("UdpLite6MemErrors", UDP_MIB_MEMERRORS),
0142     SNMP_MIB_SENTINEL
0143 };
0144 
0145 static void snmp6_seq_show_icmpv6msg(struct seq_file *seq, atomic_long_t *smib)
0146 {
0147     char name[32];
0148     int i;
0149 
0150     /* print by name -- deprecated items */
0151     for (i = 0; i < ICMP6MSG_MIB_MAX; i++) {
0152         int icmptype;
0153         const char *p;
0154 
0155         icmptype = i & 0xff;
0156         p = icmp6type2name[icmptype];
0157         if (!p) /* don't print un-named types here */
0158             continue;
0159         snprintf(name, sizeof(name), "Icmp6%s%s",
0160             i & 0x100 ? "Out" : "In", p);
0161         seq_printf(seq, "%-32s\t%lu\n", name,
0162                atomic_long_read(smib + i));
0163     }
0164 
0165     /* print by number (nonzero only) - ICMPMsgStat format */
0166     for (i = 0; i < ICMP6MSG_MIB_MAX; i++) {
0167         unsigned long val;
0168 
0169         val = atomic_long_read(smib + i);
0170         if (!val)
0171             continue;
0172         snprintf(name, sizeof(name), "Icmp6%sType%u",
0173             i & 0x100 ?  "Out" : "In", i & 0xff);
0174         seq_printf(seq, "%-32s\t%lu\n", name, val);
0175     }
0176 }
0177 
0178 /* can be called either with percpu mib (pcpumib != NULL),
0179  * or shared one (smib != NULL)
0180  */
0181 static void snmp6_seq_show_item(struct seq_file *seq, void __percpu *pcpumib,
0182                 atomic_long_t *smib,
0183                 const struct snmp_mib *itemlist)
0184 {
0185     unsigned long buff[SNMP_MIB_MAX];
0186     int i;
0187 
0188     if (pcpumib) {
0189         memset(buff, 0, sizeof(unsigned long) * SNMP_MIB_MAX);
0190 
0191         snmp_get_cpu_field_batch(buff, itemlist, pcpumib);
0192         for (i = 0; itemlist[i].name; i++)
0193             seq_printf(seq, "%-32s\t%lu\n",
0194                    itemlist[i].name, buff[i]);
0195     } else {
0196         for (i = 0; itemlist[i].name; i++)
0197             seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name,
0198                    atomic_long_read(smib + itemlist[i].entry));
0199     }
0200 }
0201 
0202 static void snmp6_seq_show_item64(struct seq_file *seq, void __percpu *mib,
0203                   const struct snmp_mib *itemlist, size_t syncpoff)
0204 {
0205     u64 buff64[SNMP_MIB_MAX];
0206     int i;
0207 
0208     memset(buff64, 0, sizeof(u64) * SNMP_MIB_MAX);
0209 
0210     snmp_get_cpu_field64_batch(buff64, itemlist, mib, syncpoff);
0211     for (i = 0; itemlist[i].name; i++)
0212         seq_printf(seq, "%-32s\t%llu\n", itemlist[i].name, buff64[i]);
0213 }
0214 
0215 static int snmp6_seq_show(struct seq_file *seq, void *v)
0216 {
0217     struct net *net = (struct net *)seq->private;
0218 
0219     snmp6_seq_show_item64(seq, net->mib.ipv6_statistics,
0220                 snmp6_ipstats_list, offsetof(struct ipstats_mib, syncp));
0221     snmp6_seq_show_item(seq, net->mib.icmpv6_statistics,
0222                 NULL, snmp6_icmp6_list);
0223     snmp6_seq_show_icmpv6msg(seq, net->mib.icmpv6msg_statistics->mibs);
0224     snmp6_seq_show_item(seq, net->mib.udp_stats_in6,
0225                 NULL, snmp6_udp6_list);
0226     snmp6_seq_show_item(seq, net->mib.udplite_stats_in6,
0227                 NULL, snmp6_udplite6_list);
0228     return 0;
0229 }
0230 
0231 static int snmp6_dev_seq_show(struct seq_file *seq, void *v)
0232 {
0233     struct inet6_dev *idev = (struct inet6_dev *)seq->private;
0234 
0235     seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
0236     snmp6_seq_show_item64(seq, idev->stats.ipv6,
0237                 snmp6_ipstats_list, offsetof(struct ipstats_mib, syncp));
0238     snmp6_seq_show_item(seq, NULL, idev->stats.icmpv6dev->mibs,
0239                 snmp6_icmp6_list);
0240     snmp6_seq_show_icmpv6msg(seq, idev->stats.icmpv6msgdev->mibs);
0241     return 0;
0242 }
0243 
0244 int snmp6_register_dev(struct inet6_dev *idev)
0245 {
0246     struct proc_dir_entry *p;
0247     struct net *net;
0248 
0249     if (!idev || !idev->dev)
0250         return -EINVAL;
0251 
0252     net = dev_net(idev->dev);
0253     if (!net->mib.proc_net_devsnmp6)
0254         return -ENOENT;
0255 
0256     p = proc_create_single_data(idev->dev->name, 0444,
0257             net->mib.proc_net_devsnmp6, snmp6_dev_seq_show, idev);
0258     if (!p)
0259         return -ENOMEM;
0260 
0261     idev->stats.proc_dir_entry = p;
0262     return 0;
0263 }
0264 
0265 int snmp6_unregister_dev(struct inet6_dev *idev)
0266 {
0267     struct net *net = dev_net(idev->dev);
0268     if (!net->mib.proc_net_devsnmp6)
0269         return -ENOENT;
0270     if (!idev->stats.proc_dir_entry)
0271         return -EINVAL;
0272     proc_remove(idev->stats.proc_dir_entry);
0273     idev->stats.proc_dir_entry = NULL;
0274     return 0;
0275 }
0276 
0277 static int __net_init ipv6_proc_init_net(struct net *net)
0278 {
0279     if (!proc_create_net_single("sockstat6", 0444, net->proc_net,
0280             sockstat6_seq_show, NULL))
0281         return -ENOMEM;
0282 
0283     if (!proc_create_net_single("snmp6", 0444, net->proc_net,
0284             snmp6_seq_show, NULL))
0285         goto proc_snmp6_fail;
0286 
0287     net->mib.proc_net_devsnmp6 = proc_mkdir("dev_snmp6", net->proc_net);
0288     if (!net->mib.proc_net_devsnmp6)
0289         goto proc_dev_snmp6_fail;
0290     return 0;
0291 
0292 proc_dev_snmp6_fail:
0293     remove_proc_entry("snmp6", net->proc_net);
0294 proc_snmp6_fail:
0295     remove_proc_entry("sockstat6", net->proc_net);
0296     return -ENOMEM;
0297 }
0298 
0299 static void __net_exit ipv6_proc_exit_net(struct net *net)
0300 {
0301     remove_proc_entry("sockstat6", net->proc_net);
0302     remove_proc_entry("dev_snmp6", net->proc_net);
0303     remove_proc_entry("snmp6", net->proc_net);
0304 }
0305 
0306 static struct pernet_operations ipv6_proc_ops = {
0307     .init = ipv6_proc_init_net,
0308     .exit = ipv6_proc_exit_net,
0309 };
0310 
0311 int __init ipv6_misc_proc_init(void)
0312 {
0313     return register_pernet_subsys(&ipv6_proc_ops);
0314 }
0315 
0316 void ipv6_misc_proc_exit(void)
0317 {
0318     unregister_pernet_subsys(&ipv6_proc_ops);
0319 }