[PATCH 1/2] netlink: implement generic nlmsg_flags decoding
JingPiao Chen
chenjingpiao at gmail.com
Wed Jun 7 04:47:45 UTC 2017
* netlink.c: Include xlat/netlink_get_flags.h,
xlat/netlink_new_flags.h, xlat/nl_route_get_types.h,
xlat/nl_route_new_types.h, xlat/nl_xfrm_get_types.h,
and xlat/nl_xfrm_new_types.h.
(decode_nlmsg_flags): New function.
(print_nlmsghdr): Use it.
* xlat/netlink_get_flags.in: New file.
* xlat/netlink_new_flags.in: Likewise.
* xlat/nl_route_get_types.in: Likewise.
* xlat/nl_route_new_types.in: Likewise.
* xlat/nl_xfrm_get_types.in: Likewise.
* xlat/nl_xfrm_new_types.in: Likewise.
---
This idea I have disscussed in irc, I do not have other idea, so I trying
to implement this idea.
netlink.c | 48 +++++++++++++++++++++++++++++++++++++++++++++-
xlat/netlink_get_flags.in | 11 +++++++++++
xlat/netlink_new_flags.in | 11 +++++++++++
xlat/nl_route_get_types.in | 19 ++++++++++++++++++
xlat/nl_route_new_types.in | 17 ++++++++++++++++
xlat/nl_xfrm_get_types.in | 5 +++++
xlat/nl_xfrm_new_types.in | 5 +++++
7 files changed, 115 insertions(+), 1 deletion(-)
create mode 100644 xlat/netlink_get_flags.in
create mode 100644 xlat/netlink_new_flags.in
create mode 100644 xlat/nl_route_get_types.in
create mode 100644 xlat/nl_route_new_types.in
create mode 100644 xlat/nl_xfrm_get_types.in
create mode 100644 xlat/nl_xfrm_new_types.in
diff --git a/netlink.c b/netlink.c
index f9ff465..3168d73 100644
--- a/netlink.c
+++ b/netlink.c
@@ -34,14 +34,20 @@
#include <linux/rtnetlink.h>
#include <linux/xfrm.h>
#include "xlat/netlink_flags.h"
+#include "xlat/netlink_get_flags.h"
+#include "xlat/netlink_new_flags.h"
#include "xlat/netlink_protocols.h"
#include "xlat/netlink_types.h"
#include "xlat/nl_audit_types.h"
#include "xlat/nl_netfilter_msg_types.h"
#include "xlat/nl_netfilter_subsys_ids.h"
+#include "xlat/nl_route_get_types.h"
+#include "xlat/nl_route_new_types.h"
#include "xlat/nl_route_types.h"
#include "xlat/nl_selinux_types.h"
#include "xlat/nl_sock_diag_types.h"
+#include "xlat/nl_xfrm_get_types.h"
+#include "xlat/nl_xfrm_new_types.h"
#include "xlat/nl_xfrm_types.h"
#undef NLMSG_HDRLEN
@@ -152,6 +158,45 @@ decode_nlmsg_type(const uint16_t type, const unsigned int family)
}
}
+static const struct {
+ const struct xlat *gets;
+ const struct xlat *news;
+} type_class[] = {
+ [NETLINK_ROUTE] = {
+ .gets = nl_route_get_types,
+ .news = nl_route_new_types,
+ },
+ [NETLINK_SOCK_DIAG] = {
+ .gets = nl_sock_diag_types,
+ },
+ [NETLINK_XFRM] = {
+ .gets = nl_xfrm_get_types,
+ .news = nl_xfrm_new_types,
+ },
+};
+
+/*
+ * As all valid netlink families are positive integers, use unsigned int
+ * for family here to filter out NL_FAMILY_ERROR and NL_FAMILY_DEFAULT.
+ */
+static void
+decode_nlmsg_flags(const uint16_t flags, const uint16_t type,
+ const unsigned int family)
+{
+ if (family < ARRAY_SIZE(type_class)) {
+ if (type_class[family].gets
+ && xlookup(type_class[family].gets, type))
+ printflags(netlink_get_flags, flags, "NLMSG_F_???");
+ else if (type_class[family].news
+ && xlookup(type_class[family].news, type))
+ printflags(netlink_new_flags, flags, "NLMSG_F_???");
+ else
+ printflags(netlink_flags, flags, "NLMSG_F_???");
+ } else {
+ printflags(netlink_flags, flags, "NLMSG_F_???");
+ }
+}
+
static int
print_nlmsghdr(struct tcb *tcp,
const int fd,
@@ -170,7 +215,8 @@ print_nlmsghdr(struct tcb *tcp,
decode_nlmsg_type(nlmsghdr->nlmsg_type, hdr_family);
tprints(", flags=");
- printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
+ decode_nlmsg_flags(nlmsghdr->nlmsg_flags,
+ nlmsghdr->nlmsg_type, hdr_family);
tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
nlmsghdr->nlmsg_pid);
diff --git a/xlat/netlink_get_flags.in b/xlat/netlink_get_flags.in
new file mode 100644
index 0000000..0b68ba9
--- /dev/null
+++ b/xlat/netlink_get_flags.in
@@ -0,0 +1,11 @@
+NLM_F_REQUEST
+NLM_F_MULTI
+NLM_F_ACK
+NLM_F_ECHO
+NLM_F_DUMP_INTR
+NLM_F_DUMP_FILTERED
+
+NLM_F_DUMP
+NLM_F_ROOT
+NLM_F_MATCH
+NLM_F_ATOMIC
diff --git a/xlat/netlink_new_flags.in b/xlat/netlink_new_flags.in
new file mode 100644
index 0000000..fa0c859
--- /dev/null
+++ b/xlat/netlink_new_flags.in
@@ -0,0 +1,11 @@
+NLM_F_REQUEST
+NLM_F_MULTI
+NLM_F_ACK
+NLM_F_ECHO
+NLM_F_DUMP_INTR
+NLM_F_DUMP_FILTERED
+
+NLM_F_REPLACE
+NLM_F_EXCL
+NLM_F_CREATE
+NLM_F_APPEND
diff --git a/xlat/nl_route_get_types.in b/xlat/nl_route_get_types.in
new file mode 100644
index 0000000..48022c9
--- /dev/null
+++ b/xlat/nl_route_get_types.in
@@ -0,0 +1,19 @@
+RTM_GETLINK 18
+RTM_GETADDR 22
+RTM_GETROUTE 26
+RTM_GETNEIGH 30
+RTM_GETRULE 34
+RTM_GETQDISC 38
+RTM_GETTCLASS 42
+RTM_GETTFILTER 46
+RTM_GETACTION 50
+RTM_GETPREFIX 54
+RTM_GETMULTICAST 58
+RTM_GETANYCAST 62
+RTM_GETNEIGHTBL 66
+RTM_GETADDRLABEL 74
+RTM_GETDCB 78
+RTM_GETNETCONF 82
+RTM_GETMDB 86
+RTM_GETNSID 90
+RTM_GETSTATS 94
diff --git a/xlat/nl_route_new_types.in b/xlat/nl_route_new_types.in
new file mode 100644
index 0000000..72b652b
--- /dev/null
+++ b/xlat/nl_route_new_types.in
@@ -0,0 +1,17 @@
+RTM_NEWLINK 16
+RTM_NEWADDR 20
+RTM_NEWROUTE 24
+RTM_NEWNEIGH 28
+RTM_NEWRULE 32
+RTM_NEWQDISC 36
+RTM_NEWTCLASS 40
+RTM_NEWTFILTER 44
+RTM_NEWACTION 48
+RTM_NEWPREFIX 52
+RTM_NEWNEIGHTBL 64
+RTM_NEWNDUSEROPT 68
+RTM_NEWADDRLABEL 72
+RTM_NEWNETCONF 80
+RTM_NEWMDB 84
+RTM_NEWNSID 88
+RTM_NEWSTATS 92
diff --git a/xlat/nl_xfrm_get_types.in b/xlat/nl_xfrm_get_types.in
new file mode 100644
index 0000000..5b69157
--- /dev/null
+++ b/xlat/nl_xfrm_get_types.in
@@ -0,0 +1,5 @@
+XFRM_MSG_GETSA
+XFRM_MSG_GETPOLICY
+XFRM_MSG_GETAE
+XFRM_MSG_GETSADINFO
+XFRM_MSG_GETSPDINFO
diff --git a/xlat/nl_xfrm_new_types.in b/xlat/nl_xfrm_new_types.in
new file mode 100644
index 0000000..1c2b66d
--- /dev/null
+++ b/xlat/nl_xfrm_new_types.in
@@ -0,0 +1,5 @@
+XFRM_MSG_NEWSA
+XFRM_MSG_NEWPOLICY
+XFRM_MSG_NEWAE
+XFRM_MSG_NEWSADINFO
+XFRM_MSG_NEWSPDINFO
--
2.7.4
More information about the Strace-devel
mailing list