From 0260891c96d2e23b78d23e329381671e0730442a Mon Sep 17 00:00:00 2001 From: Masayuki Ishikawa Date: Thu, 18 May 2017 16:30:12 +0900 Subject: [PATCH] net procfs: Fix buffer corruption and refactor netdev_statistics.c Signed-off-by: Masayuki Ishikawa --- net/procfs/netdev_statistics.c | 50 ++++++++++++++++++++++------------ net/procfs/procfs.h | 2 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/net/procfs/netdev_statistics.c b/net/procfs/netdev_statistics.c index 109f4cdca2..cd7dde177d 100644 --- a/net/procfs/netdev_statistics.c +++ b/net/procfs/netdev_statistics.c @@ -61,9 +61,12 @@ ****************************************************************************/ static int netprocfs_linklayer(FAR struct netprocfs_file_s *netfile); -static int netprocfs_ipaddresses(FAR struct netprocfs_file_s *netfile); +#ifdef CONFIG_NET_IPv4 +static int netprocfs_inet4addresses(FAR struct netprocfs_file_s *netfile); +#endif #ifdef CONFIG_NET_IPv6 -static int netprocfs_dripaddress(FAR struct netprocfs_file_s *netfile); +static int netprocfs_inet6address(FAR struct netprocfs_file_s *netfile); +static int netprocfs_inet6draddress(FAR struct netprocfs_file_s *netfile); #endif #ifdef CONFIG_NETDEV_STATISTICS static int netprocfs_rxstatistics_header(FAR struct netprocfs_file_s *netfile); @@ -83,10 +86,13 @@ static int netprocfs_errors(FAR struct netprocfs_file_s *netfile); static const linegen_t g_linegen[] = { - netprocfs_linklayer, - netprocfs_ipaddresses + netprocfs_linklayer +#ifdef CONFIG_NET_IPv4 + , netprocfs_inet4addresses +#endif #ifdef CONFIG_NET_IPv6 - , netprocfs_dripaddress + , netprocfs_inet6address + , netprocfs_inet6draddress #endif #ifdef CONFIG_NETDEV_STATISTICS , netprocfs_rxstatistics_header, @@ -255,25 +261,19 @@ static int netprocfs_linklayer(FAR struct netprocfs_file_s *netfile) } /**************************************************************************** - * Name: netprocfs_ipaddresses + * Name: netprocfs_inet4addresses ****************************************************************************/ -static int netprocfs_ipaddresses(FAR struct netprocfs_file_s *netfile) +#ifdef CONFIG_NET_IPv4 +static int netprocfs_inet4addresses(FAR struct netprocfs_file_s *netfile) { FAR struct net_driver_s *dev; -#ifdef CONFIG_NET_IPv4 struct in_addr addr; -#endif -#ifdef CONFIG_NET_IPv6 - char addrstr[INET6_ADDRSTRLEN]; - uint8_t preflen; -#endif int len = 0; DEBUGASSERT(netfile != NULL && netfile->dev != NULL); dev = netfile->dev; -#ifdef CONFIG_NET_IPv4 /* Show the IPv4 address */ addr.s_addr = dev->d_ipaddr; @@ -291,9 +291,25 @@ static int netprocfs_ipaddresses(FAR struct netprocfs_file_s *netfile) addr.s_addr = dev->d_netmask; len += snprintf(&netfile->line[len], NET_LINELEN - len, "Mask:%s\n\n", inet_ntoa(addr)); + return len; +} #endif +/**************************************************************************** + * Name: netprocfs_inet6address + ****************************************************************************/ + #ifdef CONFIG_NET_IPv6 +static int netprocfs_inet6address(FAR struct netprocfs_file_s *netfile) +{ + FAR struct net_driver_s *dev; + char addrstr[INET6_ADDRSTRLEN]; + uint8_t preflen; + int len = 0; + + DEBUGASSERT(netfile != NULL && netfile->dev != NULL); + dev = netfile->dev; + /* Convert the 128 network mask to a human friendly prefix length */ preflen = net_ipv6_mask2pref(dev->d_ipv6netmask); @@ -305,17 +321,17 @@ static int netprocfs_ipaddresses(FAR struct netprocfs_file_s *netfile) len += snprintf(&netfile->line[len], NET_LINELEN - len, "\tinet6 addr:%s/%d\n", addrstr, preflen); } -#endif return len; } +#endif /**************************************************************************** - * Name: netprocfs_dripaddress + * Name: netprocfs_inet6draddress ****************************************************************************/ #ifdef CONFIG_NET_IPv6 -static int netprocfs_dripaddress(FAR struct netprocfs_file_s *netfile) +static int netprocfs_inet6draddress(FAR struct netprocfs_file_s *netfile) { FAR struct net_driver_s *dev; char addrstr[INET6_ADDRSTRLEN]; diff --git a/net/procfs/procfs.h b/net/procfs/procfs.h index 889f350725..9b0d7bfd49 100644 --- a/net/procfs/procfs.h +++ b/net/procfs/procfs.h @@ -54,7 +54,7 @@ * to handle the longest line generated by this logic. */ -#define NET_LINELEN 64 +#define NET_LINELEN 80 /**************************************************************************** * Public Type Definitions