[PATCH v2 2/7] rtnl_rule: decode fib_rule_hdr netlink attributes

JingPiao Chen chenjingpiao at gmail.com
Tue Aug 29 07:45:18 UTC 2017


* configure.ac (AC_CHECK_FUNCS): Add be64toh.
(AC_CHECK_TYPES): Check for struct fib_rule_uid_range
in <linux/fib_rules.h>.
* nlattr.c: Include <endian.h>.
(decode_nla_be64): New function.
* nlattr.h (decode_nla_be64): New prototype.
* rtnl_rule.c (decode_rule_addr,
decode_fib_rule_uid_range): New functions.
(fib_rule_hdr_nla_decoders): New array.
(decode_fib_rule_hdr): Use it.
---
 configure.ac |  3 +++
 nlattr.c     | 23 +++++++++++++++++++++++
 nlattr.h     |  1 +
 rtnl_rule.c  | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index f09ae5d..bcf5129 100644
--- a/configure.ac
+++ b/configure.ac
@@ -268,6 +268,7 @@ AC_TYPE_UID_T
 
 AC_CHECK_FUNCS(m4_normalize([
 	accept4
+	be64toh
 	fallocate
 	fanotify_mark
 	fopen64
@@ -459,6 +460,8 @@ AC_CHECK_MEMBERS(m4_normalize([
 	struct rtnl_link_stats64.rx_nohandler
 ]),,, [#include <linux/if_link.h>])
 
+AC_CHECK_TYPES([struct fib_rule_uid_range],,, [#include <linux/fib_rules.h>])
+
 AC_CHECK_TYPES([struct statfs], [
 	AC_CHECK_MEMBERS(m4_normalize([
 		struct statfs.f_frsize,
diff --git a/nlattr.c b/nlattr.c
index 76010c4..396db35 100644
--- a/nlattr.c
+++ b/nlattr.c
@@ -30,6 +30,9 @@
 #include "defs.h"
 #include "netlink.h"
 #include "nlattr.h"
+
+#include <endian.h>
+
 #include <linux/sock_diag.h>
 
 static bool
@@ -220,6 +223,26 @@ decode_nla_ifindex(struct tcb *const tcp,
 	return true;
 }
 
+bool
+decode_nla_be64(struct tcb *const tcp,
+		const kernel_ulong_t addr,
+		const unsigned int len,
+		const void *const opaque_data)
+{
+#if defined HAVE_BE64TOH || defined be64toh
+	uint64_t num;
+
+	if (len < sizeof(num))
+		return false;
+	else if (!umove_or_printaddr(tcp, addr, &num))
+		tprintf("htobe64(%" PRIu64 ")", be64toh(num));
+
+	return true;
+#else
+	return false;
+#endif
+}
+
 #define DECODE_NLA_INTEGER(name, type, fmt)		\
 bool							\
 decode_nla_ ## name(struct tcb *const tcp,		\
diff --git a/nlattr.h b/nlattr.h
index 636c787..db1fd69 100644
--- a/nlattr.h
+++ b/nlattr.h
@@ -56,6 +56,7 @@ DECL_NLA(s8);
 DECL_NLA(s16);
 DECL_NLA(s32);
 DECL_NLA(s64);
+DECL_NLA(be64);
 DECL_NLA(str);
 DECL_NLA(strn);
 DECL_NLA(ifindex);
diff --git a/rtnl_rule.c b/rtnl_rule.c
index 5a1dd28..6bc49f5 100644
--- a/rtnl_rule.c
+++ b/rtnl_rule.c
@@ -43,6 +43,61 @@
 #include "xlat/fib_rule_flags.h"
 #include "xlat/rtnl_rule_attrs.h"
 
+static bool
+decode_rule_addr(struct tcb *const tcp,
+		 const kernel_ulong_t addr,
+		 const unsigned int len,
+		 const void *const opaque_data)
+{
+	const struct rtmsg *const rtmsg = opaque_data;
+
+	decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
+
+	return true;
+}
+
+static bool
+decode_fib_rule_uid_range(struct tcb *const tcp,
+			  const kernel_ulong_t addr,
+			  const unsigned int len,
+			  const void *const opaque_data)
+{
+#ifdef HAVE_STRUCT_FIB_RULE_UID_RANGE
+	struct fib_rule_uid_range range;
+
+	if (len < sizeof(range))
+		return false;
+	else if (!umove_or_printaddr(tcp, addr, &range)) {
+		PRINT_FIELD_U("{", range, start);
+		PRINT_FIELD_U(", ", range, end);
+		tprints("}");
+	}
+
+	return true;
+#else
+	return false;
+#endif
+}
+
+static const nla_decoder_t fib_rule_hdr_nla_decoders[] = {
+	[FRA_DST]			= decode_rule_addr,
+	[FRA_SRC]			= decode_rule_addr,
+	[FRA_IIFNAME]			= decode_nla_str,
+	[FRA_GOTO]			= decode_nla_u32,
+	[FRA_PRIORITY]			= decode_nla_u32,
+	[FRA_FWMARK]			= decode_nla_u32,
+	[FRA_FLOW]			= decode_nla_u32,
+	[FRA_TUN_ID]			= decode_nla_be64,
+	[FRA_SUPPRESS_IFGROUP]		= decode_nla_u32,
+	[FRA_SUPPRESS_PREFIXLEN]	= decode_nla_u32,
+	[FRA_TABLE]			= decode_nla_u32,
+	[FRA_FWMASK]			= decode_nla_u32,
+	[FRA_OIFNAME]			= decode_nla_str,
+	[FRA_PAD]			= NULL,
+	[FRA_L3MDEV]			= decode_nla_u8,
+	[FRA_UID_RANGE]			= decode_fib_rule_uid_range
+};
+
 DECL_NETLINK_ROUTE_DECODER(decode_fib_rule_hdr)
 {
 	/*
@@ -84,6 +139,8 @@ DECL_NETLINK_ROUTE_DECODER(decode_fib_rule_hdr)
 	if (decode_nla && len > offset) {
 		tprints(", ");
 		decode_nlattr(tcp, addr + offset, len - offset,
-			      rtnl_rule_attrs, "FRA_???", NULL, 0, NULL);
+			      rtnl_rule_attrs, "FRA_???",
+			      fib_rule_hdr_nla_decoders,
+			      ARRAY_SIZE(fib_rule_hdr_nla_decoders), &msg);
 	}
 }
-- 
2.7.4





More information about the Strace-devel mailing list