[PATCH v2 2/3] rtnl_addr: decode ifaddrmsg netlink attributes

JingPiao Chen chenjingpiao at gmail.com
Tue Aug 22 06:45:48 UTC 2017


* address.c: New file.
* Makefile.am (strace_SOURCES): Add it.
* defs.h (decode_inet_address, decode_proto_address): New prototypes.
* rtnl_addr.c (decode_ifa_address,
decode_ifa_cacheinfo, decode_ifa_flags): New functions.
(ifaddrmsg_nla_decoders): New array.
(decode_ifaddrmsg): Use it.
---
 Makefile.am |  1 +
 address.c   | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 defs.h      |  6 +++++
 rtnl_addr.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 address.c

diff --git a/Makefile.am b/Makefile.am
index 2a477f6..32d17b0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -83,6 +83,7 @@ libstrace_a_SOURCES =	\
 
 strace_SOURCES =	\
 	access.c	\
+	address.c	\
 	affinity.c	\
 	aio.c		\
 	alpha.c		\
diff --git a/address.c b/address.c
new file mode 100644
index 0000000..44b26f5
--- /dev/null
+++ b/address.c
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "defs.h"
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+bool
+decode_inet_address(struct tcb *const tcp,
+		    const kernel_ulong_t addr,
+		    const unsigned int len,
+		    const int family)
+{
+	union {
+		struct in_addr  a4;
+		struct in6_addr a6;
+	} buf;
+	char str[INET6_ADDRSTRLEN];
+	size_t size = 0;
+
+	switch (family) {
+	case AF_INET:
+		size = sizeof(buf.a4);
+		break;
+	case AF_INET6:
+		size = sizeof(buf.a6);
+		break;
+	}
+
+	if (!size || len < size)
+		goto end;
+	if (umoven_or_printaddr(tcp, addr, size, &buf) < 0)
+		return false;
+	if (inet_ntop(family, &buf, str, sizeof(str))) {
+		tprints(str);
+		return true;
+	}
+end:
+	printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
+	return false;
+}
+
+bool
+decode_proto_address(struct tcb *const tcp,
+		     const kernel_ulong_t addr,
+		     const unsigned int len,
+		     const int family)
+{
+	switch (family) {
+	case AF_INET:
+	case AF_INET6:
+		return decode_inet_address(tcp, addr, len, family);
+	}
+
+	printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
+	return false;
+}
diff --git a/defs.h b/defs.h
index e6659a5..a3931b2 100644
--- a/defs.h
+++ b/defs.h
@@ -610,6 +610,12 @@ extern void printfd(struct tcb *, int);
 extern void print_sockaddr(const void *sa, int len);
 extern bool
 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
+extern bool
+decode_inet_address(struct tcb *, const kernel_ulong_t addr,
+		    const unsigned int len, const int family);
+extern bool
+decode_proto_address(struct tcb *, const kernel_ulong_t addr,
+		     const unsigned int len, const int family);
 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
 extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
 extern void print_dirfd(struct tcb *, int);
diff --git a/rtnl_addr.c b/rtnl_addr.c
index c7e37ea..9c43085 100644
--- a/rtnl_addr.c
+++ b/rtnl_addr.c
@@ -42,6 +42,67 @@
 #include "xlat/routing_scopes.h"
 #include "xlat/rtnl_addr_attrs.h"
 
+static bool
+decode_ifa_address(struct tcb *const tcp,
+		   const kernel_ulong_t addr,
+		   const unsigned int len,
+		   const void *const opaque_data)
+{
+	const struct ifaddrmsg *const ifaddr = opaque_data;
+
+	decode_proto_address(tcp, addr, len, ifaddr->ifa_family);
+
+	return true;
+}
+
+static bool
+decode_ifa_cacheinfo(struct tcb *const tcp,
+		     const kernel_ulong_t addr,
+		     const unsigned int len,
+		     const void *const opaque_data)
+{
+	struct ifa_cacheinfo ci;
+
+	if (len < sizeof(ci))
+		return false;
+	else if (!umove_or_printaddr(tcp, addr, &ci)) {
+		PRINT_FIELD_U("{", ci, ifa_prefered);
+		PRINT_FIELD_U(", ", ci, ifa_valid);
+		PRINT_FIELD_U(", ", ci, cstamp);
+		PRINT_FIELD_U(", ", ci, tstamp);
+		tprintf("}");
+	}
+
+	return true;
+}
+
+static bool
+decode_ifa_flags(struct tcb *const tcp,
+		 const kernel_ulong_t addr,
+		 const unsigned int len,
+		 const void *const opaque_data)
+{
+	uint32_t ifa_flags;
+
+	if (len < sizeof(ifa_flags))
+		return false;
+	else if (!umove_or_printaddr(tcp, addr, &ifa_flags))
+		printflags(ifaddrflags, ifa_flags, "IFA_F_???");
+
+	return true;
+}
+
+static const nla_decoder_t ifaddrmsg_nla_decoders[] = {
+	[IFA_ADDRESS]	= decode_ifa_address,
+	[IFA_LOCAL]	= decode_ifa_address,
+	[IFA_LABEL]	= decode_nla_str,
+	[IFA_BROADCAST]	= decode_ifa_address,
+	[IFA_ANYCAST]	= decode_ifa_address,
+	[IFA_CACHEINFO]	= decode_ifa_cacheinfo,
+	[IFA_MULTICAST]	= decode_ifa_address,
+	[IFA_FLAGS]	= decode_ifa_flags
+};
+
 DECL_NETLINK_ROUTE_DECODER(decode_ifaddrmsg)
 {
 	struct ifaddrmsg ifaddr = { .ifa_family = family };
@@ -71,6 +132,8 @@ DECL_NETLINK_ROUTE_DECODER(decode_ifaddrmsg)
 	if (decode_nla && len > offset) {
 		tprints(", ");
 		decode_nlattr(tcp, addr + offset, len - offset,
-			      rtnl_addr_attrs, "IFA_???", NULL, 0, NULL);
+			      rtnl_addr_attrs, "IFA_???",
+			      ifaddrmsg_nla_decoders,
+			      ARRAY_SIZE(ifaddrmsg_nla_decoders), &ifaddr);
 	}
 }
-- 
2.7.4





More information about the Strace-devel mailing list