netlink_sock_diag: ensure that structure field names are printed properly

* netlink_sock_diag.c (PRINT_FIELD_U, PRINT_FIELD_COOKIE,
PRINT_FIELD_FLAGS, PRINT_FIELD_XVAL): New macros.
(decode_unix_diag_req, decode_unix_diag_msg, decode_netlink_diag_req,
decode_netlink_diag_msg, decode_packet_diag_req, decode_packet_diag_msg,
print_inet_diag_sockid, decode_inet_diag_req_compat,
decode_inet_diag_req_v2, decode_inet_diag_msg, decode_smc_diag_req,
decode_smc_diag_msg): Use them to print structure fields.
This commit is contained in:
Дмитрий Левин 2017-06-27 23:07:37 +00:00
parent 89e2e64936
commit 4817a2925d

View File

@ -58,6 +58,27 @@
#include "xlat/unix_diag_attrs.h" #include "xlat/unix_diag_attrs.h"
#include "xlat/unix_diag_show.h" #include "xlat/unix_diag_show.h"
#define PRINT_FIELD_U(prefix_, where_, field_) \
tprintf("%s%s=%llu", (prefix_), #field_, \
zero_extend_signed_to_ull((where_).field_))
#define PRINT_FIELD_COOKIE(prefix_, where_, field_) \
tprintf("%s%s=[%llu, %llu]", (prefix_), #field_, \
zero_extend_signed_to_ull((where_).field_[0]), \
zero_extend_signed_to_ull((where_).field_[1]))
#define PRINT_FIELD_FLAGS(prefix_, where_, field_, xlat_, dflt_) \
do { \
tprintf("%s%s=", (prefix_), #field_); \
printflags((xlat_), (where_).field_, (dflt_)); \
} while (0)
#define PRINT_FIELD_XVAL(prefix_, where_, field_, xlat_, dflt_) \
do { \
tprintf("%s%s=", (prefix_), #field_); \
printxval((xlat_), (where_).field_, (dflt_)); \
} while (0)
static void static void
decode_family(struct tcb *const tcp, const uint8_t family, decode_family(struct tcb *const tcp, const uint8_t family,
const kernel_ulong_t addr, const kernel_ulong_t len) const kernel_ulong_t addr, const kernel_ulong_t len)
@ -82,24 +103,19 @@ decode_unix_diag_req(struct tcb *const tcp,
struct unix_diag_req req = { .sdiag_family = family }; struct unix_diag_req req = { .sdiag_family = family };
const size_t offset = sizeof(req.sdiag_family); const size_t offset = sizeof(req.sdiag_family);
tprints("{sdiag_family="); PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
printxval(addrfams, req.sdiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprintf("sdiag_protocol=%" PRIu8 ", udiag_states=", PRINT_FIELD_U("", req, sdiag_protocol);
req.sdiag_protocol); PRINT_FIELD_FLAGS(", ", req, udiag_states,
printflags(tcp_state_flags, req.udiag_states, tcp_state_flags, "1<<TCP_???");
"1<<TCP_???"); PRINT_FIELD_U(", ", req, udiag_ino);
tprintf(", udiag_ino=%" PRIu32 ", udiag_show=", PRINT_FIELD_FLAGS(", ", req, udiag_show,
req.udiag_ino); unix_diag_show, "UDIAG_SHOW_???");
printflags(unix_diag_show, req.udiag_show, PRINT_FIELD_COOKIE(", ", req, udiag_cookie);
"UDIAG_SHOW_???");
tprintf(", udiag_cookie=[%" PRIu32 ", %" PRIu32 "]",
req.udiag_cookie[0], req.udiag_cookie[1]);
} }
} else } else
tprints("..."); tprints("...");
@ -117,22 +133,18 @@ decode_unix_diag_msg(struct tcb *const tcp,
size_t offset = sizeof(msg.udiag_family); size_t offset = sizeof(msg.udiag_family);
bool decode_nla = false; bool decode_nla = false;
tprints("{udiag_family="); PRINT_FIELD_XVAL("{", msg, udiag_family, addrfams, "AF_???");
printxval(addrfams, msg.udiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(msg)) { if (len >= sizeof(msg)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(msg) - offset, sizeof(msg) - offset,
(void *) &msg + offset)) { (void *) &msg + offset)) {
tprints("udiag_type="); PRINT_FIELD_XVAL("", msg, udiag_type,
printxval(socktypes, msg.udiag_type, "SOCK_???"); socktypes, "SOCK_???");
tprintf(", udiag_state="); PRINT_FIELD_XVAL(", ", msg, udiag_state,
printxval(tcp_states, msg.udiag_state, "TCP_???"); tcp_states, "TCP_???");
tprintf(", udiag_ino=%" PRIu32 PRINT_FIELD_U(", ", msg, udiag_ino);
", udiag_cookie=[%" PRIu32 ", %" PRIu32 "]", PRINT_FIELD_COOKIE(", ", msg, udiag_cookie);
msg.udiag_ino,
msg.udiag_cookie[0], msg.udiag_cookie[1]);
decode_nla = true; decode_nla = true;
} }
} else } else
@ -157,26 +169,23 @@ decode_netlink_diag_req(struct tcb *const tcp,
struct netlink_diag_req req = { .sdiag_family = family }; struct netlink_diag_req req = { .sdiag_family = family };
const size_t offset = sizeof(req.sdiag_family); const size_t offset = sizeof(req.sdiag_family);
tprints("{sdiag_family="); PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
printxval(addrfams, req.sdiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprints("sdiag_protocol=");
if (NDIAG_PROTO_ALL == req.sdiag_protocol) if (NDIAG_PROTO_ALL == req.sdiag_protocol)
tprints("NDIAG_PROTO_ALL"); tprintf("%s=%s",
"sdiag_protocol", "NDIAG_PROTO_ALL");
else else
printxval(netlink_protocols, PRINT_FIELD_XVAL("", req, sdiag_protocol,
req.sdiag_protocol, "NETLINK_???"); netlink_protocols,
tprintf(", ndiag_ino=%" PRIu32 ", ndiag_show=", "NETLINK_???");
req.ndiag_ino); PRINT_FIELD_U(", ", req, ndiag_ino);
printflags(netlink_diag_show, req.ndiag_show, PRINT_FIELD_FLAGS(", ", req, ndiag_show,
"NDIAG_SHOW_???"); netlink_diag_show, "NDIAG_SHOW_???");
tprintf(", ndiag_cookie=[%" PRIu32 ", %" PRIu32 "]", PRINT_FIELD_COOKIE(", ", req, ndiag_cookie);
req.ndiag_cookie[0], req.ndiag_cookie[1]);
} }
} else } else
tprints("..."); tprints("...");
@ -193,34 +202,23 @@ decode_netlink_diag_msg(struct tcb *const tcp,
struct netlink_diag_msg msg = { .ndiag_family = family }; struct netlink_diag_msg msg = { .ndiag_family = family };
const size_t offset = sizeof(msg.ndiag_family); const size_t offset = sizeof(msg.ndiag_family);
tprints("{ndiag_family="); PRINT_FIELD_XVAL("{", msg, ndiag_family, addrfams, "AF_???");
printxval(addrfams, msg.ndiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(msg)) { if (len >= sizeof(msg)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(msg) - offset, sizeof(msg) - offset,
(void *) &msg + offset)) { (void *) &msg + offset)) {
tprints("ndiag_type="); PRINT_FIELD_XVAL("", msg, ndiag_type,
printxval(socktypes, msg.ndiag_type, "SOCK_???"); socktypes, "SOCK_???");
tprints(", ndiag_protocol="); PRINT_FIELD_XVAL(", ", msg, ndiag_protocol,
printxval(netlink_protocols, msg.ndiag_protocol, netlink_protocols, "NETLINK_???");
"NETLINK_???"); PRINT_FIELD_XVAL(", ", msg, ndiag_state,
tprints(", ndiag_state="); netlink_states, "NETLINK_???");
printxval(netlink_states, msg.ndiag_state, PRINT_FIELD_U(", ", msg, ndiag_portid);
"NETLINK_???"); PRINT_FIELD_U(", ", msg, ndiag_dst_portid);
tprintf(", ndiag_portid=%" PRIu32 PRINT_FIELD_U(", ", msg, ndiag_dst_group);
", ndiag_dst_portid=%" PRIu32 PRINT_FIELD_U(", ", msg, ndiag_ino);
", ndiag_dst_group=%" PRIu32 PRINT_FIELD_COOKIE(", ", msg, ndiag_cookie);
", ndiag_ino=%" PRIu32
", ndiag_cookie=[%" PRIu32
", %" PRIu32 "]",
msg.ndiag_portid,
msg.ndiag_dst_portid,
msg.ndiag_dst_group,
msg.ndiag_ino,
msg.ndiag_cookie[0],
msg.ndiag_cookie[1]);
} }
} else } else
tprints("..."); tprints("...");
@ -237,22 +235,18 @@ decode_packet_diag_req(struct tcb *const tcp,
struct packet_diag_req req = { .sdiag_family = family }; struct packet_diag_req req = { .sdiag_family = family };
const size_t offset = sizeof(req.sdiag_family); const size_t offset = sizeof(req.sdiag_family);
tprints("{sdiag_family="); PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
printxval(addrfams, req.sdiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprints("sdiag_protocol="); PRINT_FIELD_XVAL("", req, sdiag_protocol,
printxval(ethernet_protocols, req.sdiag_protocol, ethernet_protocols, "ETH_P_???");
"ETH_P_???"); PRINT_FIELD_U(", ", req, pdiag_ino);
tprintf(", pdiag_ino=%" PRIu32 ", pdiag_show=", PRINT_FIELD_FLAGS(", ", req, pdiag_show,
req.pdiag_ino); packet_diag_show, "PACKET_SHOW_???");
printflags(packet_diag_show, req.pdiag_show, PRINT_FIELD_COOKIE(", ", req, pdiag_cookie);
"PACKET_SHOW_???");
tprintf(", pdiag_cookie=[%" PRIu32 ", %" PRIu32 "]",
req.pdiag_cookie[0], req.pdiag_cookie[1]);
} }
} else } else
tprints("..."); tprints("...");
@ -269,20 +263,17 @@ decode_packet_diag_msg(struct tcb *const tcp,
struct packet_diag_msg msg = { .pdiag_family = family }; struct packet_diag_msg msg = { .pdiag_family = family };
const size_t offset = sizeof(msg.pdiag_family); const size_t offset = sizeof(msg.pdiag_family);
tprints("{pdiag_family="); PRINT_FIELD_XVAL("{", msg, pdiag_family, addrfams, "AF_???");
printxval(addrfams, msg.pdiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(msg)) { if (len >= sizeof(msg)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(msg) - offset, sizeof(msg) - offset,
(void *) &msg + offset)) { (void *) &msg + offset)) {
tprints("pdiag_type="); PRINT_FIELD_XVAL("", msg, pdiag_type,
printxval(socktypes, msg.pdiag_type, "SOCK_???"); socktypes, "SOCK_???");
tprintf(", pdiag_num=%" PRIu16 ", pdiag_ino=%" PRIu32 PRINT_FIELD_U(", ", msg, pdiag_num);
", pdiag_cookie=[%" PRIu32 ", %" PRIu32 "]", PRINT_FIELD_U(", ", msg, pdiag_ino);
msg.pdiag_num, msg.pdiag_ino, msg.pdiag_cookie[0], PRINT_FIELD_COOKIE(", ", msg, pdiag_cookie);
msg.pdiag_cookie[1]);
} }
} else } else
tprints("..."); tprints("...");
@ -302,9 +293,10 @@ print_inet_diag_sockid(const struct inet_diag_sockid *id, const uint8_t family)
print_inet_addr(family, id->idiag_dst, print_inet_addr(family, id->idiag_dst,
sizeof(id->idiag_dst), "idiag_dst"); sizeof(id->idiag_dst), "idiag_dst");
tprintf(", idiag_if=%" PRIu32 PRINT_FIELD_U(", ", *id, idiag_if);
", idiag_cookie=[%" PRIu32 ", %" PRIu32 "]}", PRINT_FIELD_COOKIE(", ", *id, idiag_cookie);
id->idiag_if, id->idiag_cookie[0], id->idiag_cookie[1]);
tprints("}");
} }
static void static void
@ -317,27 +309,22 @@ decode_inet_diag_req_compat(struct tcb *const tcp,
struct inet_diag_req req = { .idiag_family = family }; struct inet_diag_req req = { .idiag_family = family };
const size_t offset = sizeof(req.idiag_family); const size_t offset = sizeof(req.idiag_family);
tprints("{idiag_family="); PRINT_FIELD_XVAL("{", req, idiag_family, addrfams, "AF_???");
printxval(addrfams, req.idiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprintf("idiag_src_len=%" PRIu8 PRINT_FIELD_U("", req, idiag_src_len);
", idiag_dst_len=%" PRIu8, PRINT_FIELD_U(", ", req, idiag_dst_len);
req.idiag_src_len, PRINT_FIELD_FLAGS(", ", req, idiag_ext,
req.idiag_dst_len); inet_diag_extended_flags,
tprints(", idiag_ext="); "1<<INET_DIAG_\?\?\?-1");
printflags(inet_diag_extended_flags, req.idiag_ext,
"1<<INET_DIAG_\?\?\?-1");
tprints(", id="); tprints(", id=");
print_inet_diag_sockid(&req.id, req.idiag_family); print_inet_diag_sockid(&req.id, req.idiag_family);
tprints(", idiag_states="); PRINT_FIELD_FLAGS(", ", req, idiag_states,
printflags(tcp_state_flags, req.idiag_states, tcp_state_flags, "1<<TCP_???");
"1<<TCP_???"); PRINT_FIELD_U(", ", req, idiag_dbs);
tprintf(", idiag_dbs=%" PRIu32, req.idiag_dbs);
} }
} else } else
tprints("..."); tprints("...");
@ -354,23 +341,19 @@ decode_inet_diag_req_v2(struct tcb *const tcp,
struct inet_diag_req_v2 req = { .sdiag_family = family }; struct inet_diag_req_v2 req = { .sdiag_family = family };
const size_t offset = sizeof(req.sdiag_family); const size_t offset = sizeof(req.sdiag_family);
tprints("{sdiag_family="); PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???");
printxval(addrfams, req.sdiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprints("sdiag_protocol="); PRINT_FIELD_XVAL("", req, sdiag_protocol,
printxval(inet_protocols, req.sdiag_protocol, inet_protocols, "IPPROTO_???");
"IPPROTO_???"); PRINT_FIELD_FLAGS(", ", req, idiag_ext,
tprints(", idiag_ext="); inet_diag_extended_flags,
printflags(inet_diag_extended_flags, req.idiag_ext, "1<<INET_DIAG_\?\?\?-1");
"1<<INET_DIAG_\?\?\?-1"); PRINT_FIELD_FLAGS(", ", req, idiag_states,
tprints(", idiag_states="); tcp_state_flags, "1<<TCP_???");
printflags(tcp_state_flags, req.idiag_states,
"1<<TCP_???");
tprints(", id="); tprints(", id=");
print_inet_diag_sockid(&req.id, req.sdiag_family); print_inet_diag_sockid(&req.id, req.sdiag_family);
} }
@ -405,31 +388,23 @@ decode_inet_diag_msg(struct tcb *const tcp,
struct inet_diag_msg msg = { .idiag_family = family }; struct inet_diag_msg msg = { .idiag_family = family };
const size_t offset = sizeof(msg.idiag_family); const size_t offset = sizeof(msg.idiag_family);
tprints("{idiag_family="); PRINT_FIELD_XVAL("{", msg, idiag_family, addrfams, "AF_???");
printxval(addrfams, msg.idiag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(msg)) { if (len >= sizeof(msg)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(msg) - offset, sizeof(msg) - offset,
(void *) &msg + offset)) { (void *) &msg + offset)) {
tprints("idiag_state="); PRINT_FIELD_XVAL("", msg, idiag_state,
printxval(tcp_states, msg.idiag_state, "TCP_???"); tcp_states, "TCP_???");
tprintf(", idiag_timer=%" PRIu8 PRINT_FIELD_U(", ", msg, idiag_timer);
", idiag_retrans=%" PRIu8, PRINT_FIELD_U(", ", msg, idiag_retrans);
msg.idiag_timer, msg.idiag_retrans);
tprints(", id="); tprints(", id=");
print_inet_diag_sockid(&msg.id, msg.idiag_family); print_inet_diag_sockid(&msg.id, msg.idiag_family);
tprintf(", idiag_expires=%" PRIu32 PRINT_FIELD_U(", ", msg, idiag_expires);
", idiag_rqueue=%" PRIu32 PRINT_FIELD_U(", ", msg, idiag_rqueue);
", idiag_wqueue=%" PRIu32 PRINT_FIELD_U(", ", msg, idiag_wqueue);
", idiag_uid=%" PRIu32 PRINT_FIELD_U(", ", msg, idiag_uid);
", idiag_inode=%" PRIu32, PRINT_FIELD_U(", ", msg, idiag_inode);
msg.idiag_expires,
msg.idiag_rqueue,
msg.idiag_wqueue,
msg.idiag_uid,
msg.idiag_inode);
} }
} else } else
tprints("..."); tprints("...");
@ -447,17 +422,15 @@ decode_smc_diag_req(struct tcb *const tcp,
struct smc_diag_req req = { .diag_family = family }; struct smc_diag_req req = { .diag_family = family };
const size_t offset = sizeof(req.diag_family); const size_t offset = sizeof(req.diag_family);
tprints("{diag_family="); PRINT_FIELD_XVAL("{", req, diag_family, addrfams, "AF_???");
printxval(addrfams, req.diag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(req)) { if (len >= sizeof(req)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(req) - offset, sizeof(req) - offset,
(void *) &req + offset)) { (void *) &req + offset)) {
tprints("diag_ext="); PRINT_FIELD_FLAGS("", req, diag_ext,
printflags(smc_diag_extended_flags, req.diag_ext, smc_diag_extended_flags,
"1<<SMC_DIAG_\?\?\?-1"); "1<<SMC_DIAG_\?\?\?-1");
tprints(", id="); tprints(", id=");
/* /*
* AF_SMC protocol family socket handler * AF_SMC protocol family socket handler
@ -480,27 +453,24 @@ decode_smc_diag_msg(struct tcb *const tcp,
struct smc_diag_msg msg = { .diag_family = family }; struct smc_diag_msg msg = { .diag_family = family };
const size_t offset = sizeof(msg.diag_family); const size_t offset = sizeof(msg.diag_family);
tprints("{diag_family="); PRINT_FIELD_XVAL("{", msg, diag_family, addrfams, "AF_???");
printxval(addrfams, msg.diag_family, "AF_???");
tprints(", "); tprints(", ");
if (len >= sizeof(msg)) { if (len >= sizeof(msg)) {
if (!umoven_or_printaddr(tcp, addr + offset, if (!umoven_or_printaddr(tcp, addr + offset,
sizeof(msg) - offset, sizeof(msg) - offset,
(void *) &msg + offset)) { (void *) &msg + offset)) {
tprints("diag_state="); PRINT_FIELD_XVAL("", msg, diag_state,
printxval(smc_states, msg.diag_state, "SMC_???"); smc_states, "SMC_???");
tprintf(", diag_fallback=%" PRIu8 PRINT_FIELD_U(", ", msg, diag_fallback);
", diag_shutdown=%" PRIu8, PRINT_FIELD_U(", ", msg, diag_shutdown);
msg.diag_fallback, msg.diag_shutdown);
tprints(", id="); tprints(", id=");
/* /*
* AF_SMC protocol family socket handler * AF_SMC protocol family socket handler
* keeping the AF_INET sock address. * keeping the AF_INET sock address.
*/ */
print_inet_diag_sockid(&msg.id, AF_INET); print_inet_diag_sockid(&msg.id, AF_INET);
tprintf(", diag_uid=%" PRIu32 ", diag_inode=%" PRIu64, PRINT_FIELD_U(", ", msg, diag_uid);
msg.diag_uid, msg.diag_inode); PRINT_FIELD_U(", ", msg, diag_inode);
} }
} else } else
tprints("..."); tprints("...");