]> granicus.if.org Git - strace/commitdiff
socketutils: build a dynamic xlat table for NETLINK_GENERIC decoding
authorMasatake YAMATO <yamato@redhat.com>
Tue, 13 Jun 2017 08:26:43 +0000 (17:26 +0900)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 19 Jun 2017 21:45:58 +0000 (21:45 +0000)
* configure.ac (AC_CHECK_HEADERS): Add linux/genetlink.h.
* defs.h (genl_families_xlat): New prototype.
* socketutils.c [HAVE_LINUX_GENETLINK_H]: Include <linux/genetlink.h>.
[HAVE_LINUX_GENETLINK_H] (genl_send_dump_families,
genl_parse_families_response): New helper functions.
(genl_families_xlat): New function for building the xlat table
for NETLINK_GENERIC decoding.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
configure.ac
defs.h
socketutils.c

index d510f41ecdd1dc16cd2d535f234e15f4a2e442c9..bc7b637dc960314c195ef586b8e52b28c33046fd 100644 (file)
@@ -367,6 +367,7 @@ AC_CHECK_HEADERS(m4_normalize([
        linux/falloc.h
        linux/fiemap.h
        linux/filter.h
+       linux/genetlink.h
        linux/hiddev.h
        linux/ip_vs.h
        linux/ipc.h
diff --git a/defs.h b/defs.h
index f7b57090bd8f9e3812c0fa498e7a5a7819844af1..29a681b9162f4e0a4b66b756832478407b2345e9 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -514,6 +514,8 @@ void dyxlat_free(struct dyxlat *);
 const struct xlat *dyxlat_get(const struct dyxlat *);
 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
 
+const struct xlat *genl_families_xlat(void);
+
 extern unsigned long get_pagesize(void);
 extern int
 string_to_uint_ex(const char *str, char **endptr,
index b93c98febeecd5e7871996719ab9913a4972695b..61d657897b28cfee523b533280687311373c657e 100644 (file)
@@ -37,6 +37,9 @@
 #include <linux/unix_diag.h>
 #include <linux/netlink_diag.h>
 #include <linux/rtnetlink.h>
+#if HAVE_LINUX_GENETLINK_H
+#include <linux/genetlink.h>
+#endif
 
 #include <sys/un.h>
 #ifndef UNIX_PATH_MAX
@@ -541,3 +544,110 @@ print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
        return print_sockaddr_by_inode_cached(inode) ? true :
                print_sockaddr_by_inode_uncached(inode, getfdproto(tcp, fd));
 }
+
+#ifdef HAVE_LINUX_GENETLINK_H
+/*
+ * Managing the cache for decoding communications of Netlink GENERIC protocol
+ *
+ * As name shown Netlink GENERIC protocol is generic protocol. The
+ * numbers of msg types used in the protocol are not defined
+ * statically. Kernel defines them on demand.  So the xlat converted
+ * from header files doesn't help for decoding the protocol. Following
+ * codes are building xlat(dyxlat) at runtime.
+ */
+static bool
+genl_send_dump_families(const int fd)
+{
+       struct {
+               const struct nlmsghdr nlh;
+               struct genlmsghdr gnlh;
+       } req = {
+               .nlh = {
+                       .nlmsg_len = sizeof(req),
+                       .nlmsg_type = GENL_ID_CTRL,
+                       .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
+               },
+               .gnlh = {
+                       .cmd = CTRL_CMD_GETFAMILY,
+               }
+       };
+       return send_query(fd, &req, sizeof(req));
+}
+
+static int
+genl_parse_families_response(const void *const data,
+                            const int data_len, const unsigned long inode,
+                            void *opaque_data)
+{
+       struct dyxlat *const dyxlat = opaque_data;
+       const struct genlmsghdr *const gnlh = data;
+       struct rtattr *attr;
+       int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
+
+       char *name = NULL;
+       unsigned int name_len = 0;
+       uint16_t *id = NULL;
+
+       if (rta_len < 0)
+               return -1;
+       if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
+               return -1;
+       if (gnlh->version != 2)
+               return -1;
+
+       for (attr = (struct rtattr *) (gnlh + 1);
+            RTA_OK(attr, rta_len);
+            attr = RTA_NEXT(attr, rta_len)) {
+               switch (attr->rta_type) {
+               case CTRL_ATTR_FAMILY_NAME:
+                       if (!name) {
+                               name = RTA_DATA(attr);
+                               name_len = RTA_PAYLOAD(attr);
+                       }
+                       break;
+               case CTRL_ATTR_FAMILY_ID:
+                       if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
+                               id = RTA_DATA(attr);
+                       break;
+               }
+
+               if (name && id) {
+                       dyxlat_add_pair(dyxlat, *id, name, name_len);
+                       name = NULL;
+                       id = NULL;
+               }
+       }
+
+       return 0;
+}
+
+const struct xlat *
+genl_families_xlat(void)
+{
+       static struct dyxlat *dyxlat;
+
+       if (!dyxlat) {
+               dyxlat = dyxlat_alloc(32);
+
+               int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
+               if (fd < 0)
+                       goto out;
+
+               if (genl_send_dump_families(fd))
+                       receive_responses(fd, 0, GENL_ID_CTRL,
+                                         genl_parse_families_response, dyxlat);
+               close(fd);
+       }
+
+out:
+       return dyxlat_get(dyxlat);
+}
+
+#else /* !HAVE_LINUX_GENETLINK_H */
+
+const struct xlat *
+genl_families_xlat(void)
+{
+       return NULL;
+}
+#endif