diff --git a/tests/netlink_crypto.c b/tests/netlink_crypto.c index d15c17cf..f6d13419 100644 --- a/tests/netlink_crypto.c +++ b/tests/netlink_crypto.c @@ -98,8 +98,6 @@ test_nlmsg_flags(const int fd) static void test_crypto_msg_newalg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); - struct crypto_user_alg alg = { .cru_name = "abcd", .cru_driver_name = "efgh", @@ -109,6 +107,8 @@ test_crypto_msg_newalg(const int fd) .cru_refcnt = 0xbcacfacd, .cru_flags = 0xefacdbad }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(alg)); + TEST_NETLINK_OBJECT_EX(fd, nlh0, CRYPTO_MSG_NEWALG, NLM_F_REQUEST, alg, print_quoted_memory, @@ -149,7 +149,7 @@ test_crypto_msg_newalg(const int fd) static void test_crypto_msg_unspec(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, 4); TEST_NETLINK_(fd, nlh0, 0xffff, "0xffff /* CRYPTO_MSG_??? */", diff --git a/tests/netlink_netfilter.c b/tests/netlink_netfilter.c index 57532f52..2842e154 100644 --- a/tests/netlink_netfilter.c +++ b/tests/netlink_netfilter.c @@ -88,8 +88,8 @@ test_nlmsg_type(const int fd) static void test_nlmsg_done(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const int num = 0xabcdefad; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(num)); TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST, sizeof(num), &num, sizeof(num), @@ -99,13 +99,21 @@ test_nlmsg_done(const int fd) static void test_nfgenmsg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + static const struct nlattr nla = { + .nla_len = sizeof(nla), + .nla_type = 0x0bcd + }; struct nfgenmsg msg = { .nfgen_family = AF_UNIX, .version = NFNETLINK_V0, .res_id = NFNL_SUBSYS_NFTABLES }; + char str_buf[NLMSG_ALIGN(sizeof(msg)) + 4]; + char nla_buf[NLMSG_ALIGN(sizeof(msg)) + sizeof(nla)]; + + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, + MAX(sizeof(str_buf), sizeof(nla_buf))); TEST_NETLINK_OBJECT_EX_(fd, nlh0, NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE, @@ -150,8 +158,6 @@ test_nfgenmsg(const int fd) printf(", version=NFNETLINK_V0"); printf(", res_id=htons(%d)", NFNL_SUBSYS_NFTABLES)); - char str_buf[NLMSG_ALIGN(sizeof(msg)) + 4]; - msg.res_id = htons(0xabcd); memcpy(str_buf, &msg, sizeof(msg)); memcpy(str_buf + NLMSG_ALIGN(sizeof(msg)), "1234", 4); @@ -165,12 +171,6 @@ test_nfgenmsg(const int fd) ", \"\\x31\\x32\\x33\\x34\"", 0xabcd)); # endif /* NFNL_MSG_BATCH_BEGIN */ - static const struct nlattr nla = { - .nla_len = sizeof(nla), - .nla_type = 0x0bcd - }; - char nla_buf[NLMSG_ALIGN(sizeof(msg)) + sizeof(nla)]; - msg.res_id = htons(NFNL_SUBSYS_NFTABLES); memcpy(nla_buf, &msg, sizeof(msg)); memcpy(nla_buf + NLMSG_ALIGN(sizeof(msg)), &nla, sizeof(nla)); diff --git a/tests/netlink_protocol.c b/tests/netlink_protocol.c index be1a6528..b01ac705 100644 --- a/tests/netlink_protocol.c +++ b/tests/netlink_protocol.c @@ -203,7 +203,7 @@ test_nlmsgerr(const int fd) { struct nlmsgerr *err; struct nlmsghdr *nlh; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(*err) + 4); long rc; /* error message without enough room for the error code */ @@ -318,9 +318,9 @@ static void test_nlmsg_done(const int fd) { struct nlmsghdr *nlh; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); - long rc; const int num = 0xfacefeed; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(num)); + long rc; /* NLMSG_DONE message without enough room for an integer payload */ nlh = nlh0; diff --git a/tests/netlink_route.c b/tests/netlink_route.c index a1c7d1f8..3a5ba39a 100644 --- a/tests/netlink_route.c +++ b/tests/netlink_route.c @@ -143,8 +143,8 @@ test_nlmsg_flags(const int fd) static void test_nlmsg_done(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const int num = 0xabcdefad; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(num)); TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST, sizeof(num), &num, sizeof(num), @@ -154,10 +154,11 @@ test_nlmsg_done(const int fd) static void test_rtnl_unspec(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + uint8_t family = 0; + char buf[sizeof(family) + 4]; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf)); /* unspecified family only */ - uint8_t family = 0; TEST_NETLINK_(fd, nlh0, 0xffff, "0xffff /* RTM_??? */", NLM_F_REQUEST, "NLM_F_REQUEST", @@ -180,7 +181,6 @@ test_rtnl_unspec(const int fd) printf("%p", NLMSG_DATA(TEST_NETLINK_nlh))); /* unspecified family and string */ - char buf[sizeof(family) + 4]; family = 0; memcpy(buf, &family, sizeof(family)); memcpy(buf + sizeof(family), "1234", 4); @@ -204,7 +204,6 @@ test_rtnl_unspec(const int fd) static void test_rtnl_link(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct ifinfomsg ifinfo = { .ifi_family = AF_UNIX, .ifi_type = ARPHRD_LOOPBACK, @@ -212,6 +211,7 @@ test_rtnl_link(const int fd) .ifi_flags = IFF_UP, .ifi_change = 0xfabcdeba }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(ifinfo)); TEST_NL_ROUTE(fd, nlh0, RTM_GETLINK, ifinfo, printf("{ifi_family=AF_UNIX"), @@ -225,7 +225,6 @@ test_rtnl_link(const int fd) static void test_rtnl_addr(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct ifaddrmsg msg = { .ifa_family = AF_UNIX, .ifa_prefixlen = 0xde, @@ -233,6 +232,7 @@ test_rtnl_addr(const int fd) .ifa_scope = RT_SCOPE_UNIVERSE, .ifa_index = ifindex_lo() }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETADDR, msg, printf("{ifa_family=AF_UNIX"), @@ -246,7 +246,6 @@ test_rtnl_addr(const int fd) static void test_rtnl_route(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct rtmsg msg = { .rtm_family = AF_UNIX, .rtm_dst_len = 0xaf, @@ -258,6 +257,7 @@ test_rtnl_route(const int fd) .rtm_type = RTN_LOCAL, .rtm_flags = RTM_F_NOTIFY }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETROUTE, msg, printf("{rtm_family=AF_UNIX"), @@ -275,7 +275,6 @@ test_rtnl_route(const int fd) static void test_rtnl_rule(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct rtmsg msg = { .rtm_family = AF_UNIX, .rtm_dst_len = 0xaf, @@ -285,6 +284,7 @@ test_rtnl_rule(const int fd) .rtm_type = FR_ACT_TO_TBL, .rtm_flags = FIB_RULE_INVERT }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETRULE, msg, printf("{family=AF_UNIX"), @@ -301,7 +301,6 @@ test_rtnl_rule(const int fd) static void test_rtnl_neigh(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct ndmsg msg = { .ndm_family = AF_UNIX, .ndm_ifindex = ifindex_lo(), @@ -309,6 +308,7 @@ test_rtnl_neigh(const int fd) .ndm_flags = NTF_PROXY, .ndm_type = RTN_UNSPEC }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETNEIGH, msg, printf("{ndm_family=AF_UNIX"), @@ -321,10 +321,10 @@ test_rtnl_neigh(const int fd) static void test_rtnl_neightbl(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct ndtmsg msg = { .ndtm_family = AF_NETLINK }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NETLINK(fd, nlh0, RTM_GETNEIGHTBL, NLM_F_REQUEST, @@ -335,7 +335,6 @@ test_rtnl_neightbl(const int fd) static void test_rtnl_tc(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct tcmsg msg = { .tcm_family = AF_UNIX, .tcm_ifindex = ifindex_lo(), @@ -343,6 +342,7 @@ test_rtnl_tc(const int fd) .tcm_parent = 0xafbcadab, .tcm_info = 0xbcaedafa }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETQDISC, msg, printf("{tcm_family=AF_UNIX"), @@ -356,10 +356,10 @@ test_rtnl_tc(const int fd) static void test_rtnl_tca(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct tcamsg msg = { .tca_family = AF_INET }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NETLINK(fd, nlh0, RTM_GETACTION, NLM_F_REQUEST, @@ -371,7 +371,6 @@ test_rtnl_tca(const int fd) static void test_rtnl_addrlabel(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct ifaddrlblmsg msg = { .ifal_family = AF_UNIX, .ifal_prefixlen = 0xaf, @@ -379,6 +378,7 @@ test_rtnl_addrlabel(const int fd) .ifal_index = ifindex_lo(), .ifal_seq = 0xfadcdafb }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETADDRLABEL, msg, printf("{ifal_family=AF_UNIX"), @@ -394,11 +394,11 @@ test_rtnl_addrlabel(const int fd) static void test_rtnl_dcb(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct dcbmsg msg = { .dcb_family = AF_UNIX, .cmd = DCB_CMD_UNDEFINED }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETDCB, msg, printf("{dcb_family=AF_UNIX"), @@ -410,10 +410,10 @@ test_rtnl_dcb(const int fd) static void test_rtnl_netconf(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct netconfmsg msg = { .ncm_family = AF_INET }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NETLINK(fd, nlh0, RTM_GETNETCONF, NLM_F_REQUEST, @@ -426,11 +426,11 @@ test_rtnl_netconf(const int fd) static void test_rtnl_mdb(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct br_port_msg msg = { .family = AF_UNIX, .ifindex = ifindex_lo() }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NL_ROUTE(fd, nlh0, RTM_GETMDB, msg, printf("{family=AF_UNIX"), @@ -442,10 +442,10 @@ test_rtnl_mdb(const int fd) static void test_rtnl_nsid(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct rtgenmsg msg = { .rtgen_family = AF_UNIX }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_NETLINK(fd, nlh0, RTM_GETNSID, NLM_F_REQUEST, diff --git a/tests/netlink_selinux.c b/tests/netlink_selinux.c index 2d768222..43922013 100644 --- a/tests/netlink_selinux.c +++ b/tests/netlink_selinux.c @@ -53,7 +53,7 @@ test_nlmsg_type(const int fd) static void test_selnl_msg_unspec(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, 4); TEST_NETLINK_(fd, nlh0, 0xffff, "0xffff /* SELNL_MSG_??? */", @@ -65,11 +65,11 @@ test_selnl_msg_unspec(const int fd) static void test_selnl_msg_setenforce(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); - static const struct selnl_msg_setenforce msg = { .val = 0xfbdcdfab }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); + TEST_NETLINK_OBJECT(fd, nlh0, SELNL_MSG_SETENFORCE, NLM_F_REQUEST, msg, PRINT_FIELD_D("{", msg, val); @@ -79,11 +79,11 @@ test_selnl_msg_setenforce(const int fd) static void test_selnl_msg_policyload(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); - static const struct selnl_msg_policyload msg = { .seqno = 0xabdcfabc }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); + TEST_NETLINK_OBJECT(fd, nlh0, SELNL_MSG_POLICYLOAD, NLM_F_REQUEST, msg, PRINT_FIELD_U("{", msg, seqno); diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c index c2ebf129..c9e1232f 100644 --- a/tests/netlink_sock_diag.c +++ b/tests/netlink_sock_diag.c @@ -127,10 +127,11 @@ test_nlmsg_flags(const int fd) static void test_odd_family_req(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + uint8_t family = 0; + char buf[sizeof(family) + 4]; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf)); /* unspecified family only */ - uint8_t family = 0; TEST_NETLINK(fd, nlh0, SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, @@ -153,7 +154,6 @@ test_odd_family_req(const int fd) printf("%p", NLMSG_DATA(TEST_NETLINK_nlh))); /* unspecified family and string */ - char buf[sizeof(family) + 4]; family = 0; memcpy(buf, &family, sizeof(family)); memcpy(buf + sizeof(family), "1234", 4); @@ -177,10 +177,11 @@ test_odd_family_req(const int fd) static void test_odd_family_msg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + uint8_t family = 0; + char buf[sizeof(family) + 4]; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf)); /* unspecified family only */ - uint8_t family = 0; TEST_NETLINK(fd, nlh0, SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, sizeof(family), &family, sizeof(family), @@ -200,7 +201,6 @@ test_odd_family_msg(const int fd) printf("%p", NLMSG_DATA(TEST_NETLINK_nlh))); /* unspecified family and string */ - char buf[sizeof(family) + 4]; family = 0; memcpy(buf, &family, sizeof(family)); memcpy(buf + sizeof(family), "1234", 4); @@ -222,7 +222,6 @@ test_odd_family_msg(const int fd) static void test_unix_diag_req(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct unix_diag_req req = { .sdiag_family = AF_UNIX, .sdiag_protocol = 253, @@ -231,6 +230,7 @@ test_unix_diag_req(const int fd) .udiag_show = UDIAG_SHOW_NAME, .udiag_cookie = { 0xdeadbeef, 0xbadc0ded } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); TEST_SOCK_DIAG(fd, nlh0, AF_UNIX, SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req, printf("{sdiag_family=AF_UNIX"), @@ -245,7 +245,6 @@ test_unix_diag_req(const int fd) static void test_unix_diag_msg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct unix_diag_msg msg = { .udiag_family = AF_UNIX, .udiag_type = SOCK_STREAM, @@ -253,6 +252,7 @@ test_unix_diag_msg(const int fd) .udiag_ino = 0xfacefeed, .udiag_cookie = { 0xdeadbeef, 0xbadc0ded } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_SOCK_DIAG(fd, nlh0, AF_UNIX, SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg, printf("{udiag_family=AF_UNIX"), @@ -266,7 +266,6 @@ test_unix_diag_msg(const int fd) static void test_netlink_diag_req(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct netlink_diag_req req = { .sdiag_family = AF_NETLINK, .sdiag_protocol = NDIAG_PROTO_ALL, @@ -274,6 +273,7 @@ test_netlink_diag_req(const int fd) .ndiag_show = NDIAG_SHOW_MEMINFO, .ndiag_cookie = { 0xdeadbeef, 0xbadc0ded } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); TEST_SOCK_DIAG(fd, nlh0, AF_NETLINK, SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req, printf("{sdiag_family=AF_NETLINK"), @@ -298,7 +298,6 @@ test_netlink_diag_req(const int fd) static void test_netlink_diag_msg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct netlink_diag_msg msg = { .ndiag_family = AF_NETLINK, .ndiag_type = SOCK_RAW, @@ -310,6 +309,7 @@ test_netlink_diag_msg(const int fd) .ndiag_ino = 0xdaeefacd, .ndiag_cookie = { 0xbadc0ded, 0xdeadbeef } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_SOCK_DIAG(fd, nlh0, AF_NETLINK, SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg, printf("{ndiag_family=AF_NETLINK"), @@ -327,7 +327,6 @@ test_netlink_diag_msg(const int fd) static void test_packet_diag_req(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct packet_diag_req req = { .sdiag_family = AF_PACKET, .sdiag_protocol = ETH_P_LOOP, @@ -335,6 +334,7 @@ test_packet_diag_req(const int fd) .pdiag_show = PACKET_SHOW_INFO, .pdiag_cookie = { 0xdeadbeef, 0xbadc0ded } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); TEST_SOCK_DIAG(fd, nlh0, AF_PACKET, SOCK_DIAG_BY_FAMILY, NLM_F_REQUEST, req, printf("{sdiag_family=AF_PACKET"), @@ -348,7 +348,6 @@ test_packet_diag_req(const int fd) static void test_packet_diag_msg(const int fd) { - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct packet_diag_msg msg = { .pdiag_family = AF_PACKET, .pdiag_type = SOCK_STREAM, @@ -356,6 +355,7 @@ test_packet_diag_msg(const int fd) .pdiag_ino = 0xfacefeed, .pdiag_cookie = { 0xdeadbeef, 0xbadc0ded } }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); TEST_SOCK_DIAG(fd, nlh0, AF_PACKET, SOCK_DIAG_BY_FAMILY, NLM_F_DUMP, msg, printf("{pdiag_family=AF_PACKET"), @@ -371,7 +371,6 @@ test_inet_diag_sockid(const int fd) { const char address[] = "12.34.56.78"; const char address6[] = "12:34:56:78:90:ab:cd:ef"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct inet_diag_req_v2 req = { .sdiag_family = AF_INET, .idiag_ext = 1 << (INET_DIAG_CONG - 1), @@ -384,6 +383,7 @@ test_inet_diag_sockid(const int fd) .idiag_cookie = { 0xdeadbeef, 0xbadc0ded } }, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); if (!inet_pton(AF_INET, address, &req.id.idiag_src) || !inet_pton(AF_INET, address, &req.id.idiag_dst)) @@ -435,7 +435,6 @@ static void test_inet_diag_req(const int fd) { const char address[] = "12.34.56.78"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct inet_diag_req req = { .idiag_family = AF_INET, .idiag_src_len = 0xde, @@ -450,6 +449,7 @@ test_inet_diag_req(const int fd) .idiag_states = 1 << TCP_LAST_ACK, .idiag_dbs = 0xfacefeed, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); if (!inet_pton(AF_INET, address, &req.id.idiag_src) || !inet_pton(AF_INET, address, &req.id.idiag_dst)) @@ -479,7 +479,6 @@ static void test_inet_diag_req_v2(const int fd) { const char address[] = "87.65.43.21"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct inet_diag_req_v2 req = { .sdiag_family = AF_INET, .idiag_ext = 1 << (INET_DIAG_CONG - 1), @@ -492,6 +491,7 @@ test_inet_diag_req_v2(const int fd) .idiag_cookie = { 0xdeadbeef, 0xbadc0ded } }, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); if (!inet_pton(AF_INET, address, &req.id.idiag_src) || !inet_pton(AF_INET, address, &req.id.idiag_dst)) @@ -519,7 +519,6 @@ static void test_inet_diag_msg(const int fd) { const char address[] = "11.22.33.44"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct inet_diag_msg msg = { .idiag_family = AF_INET, .idiag_state = TCP_LISTEN, @@ -537,6 +536,7 @@ test_inet_diag_msg(const int fd) .idiag_uid = 0xdecefaeb, .idiag_inode = 0xbadc0ded, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); if (!inet_pton(AF_INET, address, &msg.id.idiag_src) || !inet_pton(AF_INET, address, &msg.id.idiag_dst)) @@ -570,7 +570,6 @@ static void test_smc_diag_req(const int fd) { const char address[] = "43.21.56.78"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct smc_diag_req req = { .diag_family = AF_SMC, .diag_ext = 1 << (SMC_DIAG_CONNINFO - 1), @@ -581,6 +580,7 @@ test_smc_diag_req(const int fd) .idiag_cookie = { 0xdeadbeef, 0xbadc0ded }, }, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(req)); if (!inet_pton(AF_INET, address, &req.id.idiag_src) || !inet_pton(AF_INET, address, &req.id.idiag_dst)) @@ -606,7 +606,6 @@ static void test_smc_diag_msg(const int fd) { const char address[] = "34.87.12.90"; - void *const nlh0 = tail_alloc(NLMSG_HDRLEN); struct smc_diag_msg msg = { .diag_family = AF_SMC, .diag_state = SMC_ACTIVE, @@ -621,6 +620,7 @@ test_smc_diag_msg(const int fd) .diag_uid = 0xadcdfafc, .diag_inode = 0xbadc0ded, }; + void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); if (!inet_pton(AF_INET, address, &msg.id.idiag_src) || !inet_pton(AF_INET, address, &msg.id.idiag_dst)) diff --git a/tests/nlattr_br_port_msg.c b/tests/nlattr_br_port_msg.c index a2a4792d..ef34fcad 100644 --- a/tests/nlattr_br_port_msg.c +++ b/tests/nlattr_br_port_msg.c @@ -69,7 +69,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct br_port_msg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_crypto_user_alg.c b/tests/nlattr_crypto_user_alg.c index 2482b33b..c8c93acb 100644 --- a/tests/nlattr_crypto_user_alg.c +++ b/tests/nlattr_crypto_user_alg.c @@ -72,7 +72,12 @@ main(void) const int fd = create_nl_socket(NETLINK_CRYPTO); const unsigned int hdrlen = sizeof(struct crypto_user_alg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + /* + * There are also other structures, but they are not bigger than + * DEFAULT_STRLEN so far. + */ + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + DEFAULT_STRLEN); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_dcbmsg.c b/tests/nlattr_dcbmsg.c index cd63bb60..e8f0287e 100644 --- a/tests/nlattr_dcbmsg.c +++ b/tests/nlattr_dcbmsg.c @@ -67,7 +67,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct dcbmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_fib_rule_hdr.c b/tests/nlattr_fib_rule_hdr.c index 394ab103..a8242d03 100644 --- a/tests/nlattr_fib_rule_hdr.c +++ b/tests/nlattr_fib_rule_hdr.c @@ -80,7 +80,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct rtmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 8); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_ifaddrlblmsg.c b/tests/nlattr_ifaddrlblmsg.c index 60036340..e1fecc52 100644 --- a/tests/nlattr_ifaddrlblmsg.c +++ b/tests/nlattr_ifaddrlblmsg.c @@ -69,7 +69,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ifaddrlblmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_ifaddrmsg.c b/tests/nlattr_ifaddrmsg.c index 93fbef1b..6f4098c5 100644 --- a/tests/nlattr_ifaddrmsg.c +++ b/tests/nlattr_ifaddrmsg.c @@ -83,9 +83,23 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); + static const char address4[] = "12.34.56.78"; + static const char address6[] = "12:34:56:78:90:ab:cd:ef"; + static const struct ifa_cacheinfo ci = { + .ifa_prefered = 0xabcdefac, + .ifa_valid = 0xbcdadbca, + .cstamp = 0xcdabedba, + .tstamp = 0xdebabdac + }; + + struct in_addr a4; + struct in6_addr a6; + const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT; + const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ifaddrmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + MAX(sizeof(ci), sizeof(a6))); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); @@ -106,8 +120,6 @@ main(void) print_quoted_hex(pattern, 4)); SET_IFA_FAMILY(AF_INET); - static const char address4[] = "12.34.56.78"; - struct in_addr a4; if (!inet_pton(AF_INET, address4, &a4)) perror_msg_and_skip("inet_pton"); @@ -118,8 +130,6 @@ main(void) printf("%s", address4)); SET_IFA_FAMILY(AF_INET6); - static const char address6[] = "12:34:56:78:90:ab:cd:ef"; - struct in6_addr a6; if (!inet_pton(AF_INET6, address6, &a6)) perror_msg_and_skip("inet_pton"); @@ -129,12 +139,6 @@ main(void) IFA_ADDRESS, pattern, a6, printf("%s", address6)); - static const struct ifa_cacheinfo ci = { - .ifa_prefered = 0xabcdefac, - .ifa_valid = 0xbcdadbca, - .cstamp = 0xcdabedba, - .tstamp = 0xdebabdac - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifaddrmsg, print_ifaddrmsg, IFA_CACHEINFO, pattern, ci, @@ -144,7 +148,6 @@ main(void) PRINT_FIELD_U(", ", ci, tstamp); printf("}")); - const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifaddrmsg, print_ifaddrmsg, IFA_FLAGS, pattern, ifa_flags, diff --git a/tests/nlattr_ifinfomsg.c b/tests/nlattr_ifinfomsg.c index 1fff52ca..fde9f8c4 100644 --- a/tests/nlattr_ifinfomsg.c +++ b/tests/nlattr_ifinfomsg.c @@ -89,28 +89,6 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - const int fd = create_nl_socket(NETLINK_ROUTE); - const unsigned int hdrlen = sizeof(struct ifinfomsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - - const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; - char nla_type_str[256]; - sprintf(nla_type_str, "%#x /* IFLA_??? */", nla_type); - TEST_NLATTR_(fd, nlh0, hdrlen, - init_ifinfomsg, print_ifinfomsg, - nla_type, nla_type_str, - 4, pattern, 4, - print_quoted_hex(pattern, 4)); - - const int32_t netnsid = 0xacbdabda; - TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, - init_ifinfomsg, print_ifinfomsg, - IFLA_LINK_NETNSID, pattern, netnsid, - printf("%d", netnsid)); - static const struct rtnl_link_stats st = { .rx_packets = 0xabcdefac, .tx_packets = 0xbcdacdab, @@ -136,6 +114,29 @@ main(void) .rx_compressed = 0xdeffadbd, .tx_compressed = 0xefdadfab }; + const int fd = create_nl_socket(NETLINK_ROUTE); + const unsigned int hdrlen = sizeof(struct ifinfomsg); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(st)); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + + const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; + char nla_type_str[256]; + sprintf(nla_type_str, "%#x /* IFLA_??? */", nla_type); + TEST_NLATTR_(fd, nlh0, hdrlen, + init_ifinfomsg, print_ifinfomsg, + nla_type, nla_type_str, + 4, pattern, 4, + print_quoted_hex(pattern, 4)); + + const int32_t netnsid = 0xacbdabda; + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, + init_ifinfomsg, print_ifinfomsg, + IFLA_LINK_NETNSID, pattern, netnsid, + printf("%d", netnsid)); + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_STATS, pattern, st, diff --git a/tests/nlattr_ifla_brport.c b/tests/nlattr_ifla_brport.c index ae7c9da9..dfc1b3d1 100644 --- a/tests/nlattr_ifla_brport.c +++ b/tests/nlattr_ifla_brport.c @@ -84,19 +84,20 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); + const uint16_t u16 = 0xabcd; + const uint64_t u64 = 0xabcdedeeefeafeab; const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN * 2 + sizeof(u64)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - const uint16_t u16 = 0xabcd; TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_BRPORT_PRIORITY, pattern, u16, printf("%u", u16)); - const uint64_t u64 = 0xabcdedeeefeafeab; TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_BRPORT_MESSAGE_AGE_TIMER, pattern, u64, diff --git a/tests/nlattr_ifla_port.c b/tests/nlattr_ifla_port.c index 61e90fe0..0691394e 100644 --- a/tests/nlattr_ifla_port.c +++ b/tests/nlattr_ifla_port.c @@ -88,7 +88,7 @@ main(void) skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), 2 * NLA_HDRLEN + 8); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_ifla_xdp.c b/tests/nlattr_ifla_xdp.c index cca5219e..cede9963 100644 --- a/tests/nlattr_ifla_xdp.c +++ b/tests/nlattr_ifla_xdp.c @@ -87,13 +87,14 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); + const int32_t num = 0xabacdbcd; const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(num)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - const int32_t num = 0xabacdbcd; TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_XDP_FD, pattern, num, diff --git a/tests/nlattr_inet_diag_msg.c b/tests/nlattr_inet_diag_msg.c index ccc62aed..6c724d8d 100644 --- a/tests/nlattr_inet_diag_msg.c +++ b/tests/nlattr_inet_diag_msg.c @@ -87,19 +87,46 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - const int fd = create_nl_socket(NETLINK_SOCK_DIAG); - const unsigned int hdrlen = sizeof(struct inet_diag_msg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - static const struct inet_diag_meminfo minfo = { .idiag_rmem = 0xfadcacdb, .idiag_wmem = 0xbdabcada, .idiag_fmem = 0xbadbfafb, .idiag_tmem = 0xfdacdadf }; + static const struct tcpvegas_info vegas = { + .tcpv_enabled = 0xfadcacdb, + .tcpv_rttcnt = 0xbdabcada, + .tcpv_rtt = 0xbadbfafb, + .tcpv_minrtt = 0xfdacdadf + }; + static const struct tcp_dctcp_info dctcp = { + .dctcp_enabled = 0xfdac, + .dctcp_ce_state = 0xfadc, + .dctcp_alpha = 0xbdabcada, + .dctcp_ab_ecn = 0xbadbfafb, + .dctcp_ab_tot = 0xfdacdadf + }; + static const struct tcp_bbr_info bbr = { + .bbr_bw_lo = 0xfdacdadf, + .bbr_bw_hi = 0xfadcacdb, + .bbr_min_rtt = 0xbdabcada, + .bbr_pacing_gain = 0xbadbfafb, + .bbr_cwnd_gain = 0xfdacdadf + }; + static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab }; + static uint32_t bigmem[SK_MEMINFO_VARS + 1]; + static const uint32_t mark = 0xabdfadca; + static const uint8_t shutdown = 0xcd; + + const int fd = create_nl_socket(NETLINK_SOCK_DIAG); + const unsigned int hdrlen = sizeof(struct inet_diag_msg); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + + MAX(sizeof(bigmem), DEFAULT_STRLEN)); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_MEMINFO, pattern, minfo, @@ -109,12 +136,6 @@ main(void) PRINT_FIELD_U(", ", minfo, idiag_tmem); printf("}")); - static const struct tcpvegas_info vegas = { - .tcpv_enabled = 0xfadcacdb, - .tcpv_rttcnt = 0xbdabcada, - .tcpv_rtt = 0xbadbfafb, - .tcpv_minrtt = 0xfdacdadf - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_VEGASINFO, pattern, vegas, @@ -125,13 +146,6 @@ main(void) printf("}")); - static const struct tcp_dctcp_info dctcp = { - .dctcp_enabled = 0xfdac, - .dctcp_ce_state = 0xfadc, - .dctcp_alpha = 0xbdabcada, - .dctcp_ab_ecn = 0xbadbfafb, - .dctcp_ab_tot = 0xfdacdadf - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_DCTCPINFO, pattern, dctcp, @@ -142,13 +156,6 @@ main(void) PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot); printf("}")); - static const struct tcp_bbr_info bbr = { - .bbr_bw_lo = 0xfdacdadf, - .bbr_bw_hi = 0xfadcacdb, - .bbr_min_rtt = 0xbdabcada, - .bbr_pacing_gain = 0xbadbfafb, - .bbr_cwnd_gain = 0xfdacdadf - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_BBRINFO, pattern, bbr, @@ -159,12 +166,10 @@ main(void) PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain); printf("}")); - static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab }; TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SKMEMINFO, pattern, mem, print_uint); - static uint32_t bigmem[SK_MEMINFO_VARS + 1]; memcpy(bigmem, pattern, sizeof(bigmem)); TEST_NLATTR(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, @@ -176,7 +181,6 @@ main(void) } printf(", ...]")); - static const uint32_t mark = 0xabdfadca; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_MARK, pattern, mark, @@ -187,7 +191,6 @@ main(void) INET_DIAG_CLASS_ID, pattern, mark, printf("%u", mark)); - static const uint8_t shutdown = 0xcd; TEST_NLATTR(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SHUTDOWN, sizeof(shutdown), &shutdown, sizeof(shutdown), diff --git a/tests/nlattr_inet_diag_req_compat.c b/tests/nlattr_inet_diag_req_compat.c index b0b21713..a3e773d9 100644 --- a/tests/nlattr_inet_diag_req_compat.c +++ b/tests/nlattr_inet_diag_req_compat.c @@ -86,7 +86,7 @@ main(void) int fd = create_nl_socket(NETLINK_SOCK_DIAG); const unsigned int hdrlen = sizeof(struct inet_diag_req); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_inet_diag_req_v2.c b/tests/nlattr_inet_diag_req_v2.c index 1e09abe6..8d4cc2e6 100644 --- a/tests/nlattr_inet_diag_req_v2.c +++ b/tests/nlattr_inet_diag_req_v2.c @@ -407,7 +407,10 @@ main(void) skip_if_unavailable("/proc/self/fd/"); int fd = create_nl_socket(NETLINK_SOCK_DIAG); - nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + + sizeof(struct inet_diag_bc_op) + + sizeof(struct inet_diag_hostcond) + + sizeof(struct in6_addr) + DEFAULT_STRLEN); fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); test_inet_diag_bc_op(fd); diff --git a/tests/nlattr_mdba_mdb_entry.c b/tests/nlattr_mdba_mdb_entry.c index 124fe1d4..6bd21fab 100644 --- a/tests/nlattr_mdba_mdb_entry.c +++ b/tests/nlattr_mdba_mdb_entry.c @@ -99,7 +99,12 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4 +# ifdef HAVE_STRUCT_BR_MDB_ENTRY + - 4 + NLA_HDRLEN * 2 + sizeof(struct nlattr) + + sizeof(struct br_mdb_entry) +# endif + ); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_mdba_router_port.c b/tests/nlattr_mdba_router_port.c index af908cd8..651492e4 100644 --- a/tests/nlattr_mdba_router_port.c +++ b/tests/nlattr_mdba_router_port.c @@ -87,25 +87,27 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - const int fd = create_nl_socket(NETLINK_ROUTE); - - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - const uint32_t ifindex = ifindex_lo(); - TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, - init_br_port_msg, print_br_port_msg, - MDBA_ROUTER_PORT, pattern, ifindex, - printf(IFINDEX_LO_STR)); - const uint8_t type = MDB_RTR_TYPE_DISABLED; static const struct nlattr nla = { .nla_len = NLA_HDRLEN + sizeof(type), .nla_type = MDBA_ROUTER_PATTR_TYPE }; char buf[NLMSG_ALIGN(ifindex) + NLA_HDRLEN + sizeof(type)]; + + const int fd = create_nl_socket(NETLINK_ROUTE); + + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(buf)); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + + TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, + init_br_port_msg, print_br_port_msg, + MDBA_ROUTER_PORT, pattern, ifindex, + printf(IFINDEX_LO_STR)); + memcpy(buf, &ifindex, sizeof(ifindex)); memcpy(buf + NLMSG_ALIGN(ifindex), &nla, sizeof(nla)); memcpy(buf + NLMSG_ALIGN(ifindex) + NLA_HDRLEN, &type, sizeof(type)); diff --git a/tests/nlattr_ndmsg.c b/tests/nlattr_ndmsg.c index 8538a827..abb7c39d 100644 --- a/tests/nlattr_ndmsg.c +++ b/tests/nlattr_ndmsg.c @@ -77,7 +77,8 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ndmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(struct nda_cacheinfo)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_ndtmsg.c b/tests/nlattr_ndtmsg.c index d3f1bdb7..98978e93 100644 --- a/tests/nlattr_ndtmsg.c +++ b/tests/nlattr_ndtmsg.c @@ -68,7 +68,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ndtmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 11 * 8); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_netconfmsg.c b/tests/nlattr_netconfmsg.c index 6866adc9..e4644afa 100644 --- a/tests/nlattr_netconfmsg.c +++ b/tests/nlattr_netconfmsg.c @@ -66,7 +66,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct netconfmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_netlink_diag_msg.c b/tests/nlattr_netlink_diag_msg.c index 76a2d19e..17afc58b 100644 --- a/tests/nlattr_netlink_diag_msg.c +++ b/tests/nlattr_netlink_diag_msg.c @@ -76,27 +76,32 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - const int fd = create_nl_socket(NETLINK_SOCK_DIAG); - const unsigned int hdrlen = sizeof(struct netlink_diag_msg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - static const unsigned long groups[] = { (unsigned long) 0xdeadbeefbadc0dedULL, (unsigned long) 0xdeadbeefbadc0dedULL }; - TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, - init_netlink_diag_msg, print_netlink_diag_msg, - NETLINK_DIAG_GROUPS, pattern, groups, print_xlong); - static const struct netlink_diag_ring ndr = { .ndr_block_size = 0xfabfabdc, .ndr_block_nr = 0xabcdabda, .ndr_frame_size = 0xcbadbafa, .ndr_frame_nr = 0xdbcafadb }; + static const uint32_t flags = + NDIAG_FLAG_CB_RUNNING | NDIAG_FLAG_PKTINFO; + + const int fd = create_nl_socket(NETLINK_SOCK_DIAG); + const unsigned int hdrlen = sizeof(struct netlink_diag_msg); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + + MAX(sizeof(groups), sizeof(ndr))); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + + TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, + init_netlink_diag_msg, print_netlink_diag_msg, + NETLINK_DIAG_GROUPS, pattern, groups, print_xlong); + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_netlink_diag_msg, print_netlink_diag_msg, NETLINK_DIAG_RX_RING, pattern, ndr, @@ -106,8 +111,6 @@ main(void) PRINT_FIELD_U(", ", ndr, ndr_frame_nr); printf("}")); - static const uint32_t flags = - NDIAG_FLAG_CB_RUNNING | NDIAG_FLAG_PKTINFO; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_netlink_diag_msg, print_netlink_diag_msg, NETLINK_DIAG_FLAGS, pattern, flags, diff --git a/tests/nlattr_nlmsgerr.c b/tests/nlattr_nlmsgerr.c index 837283c0..be1f3599 100644 --- a/tests/nlattr_nlmsgerr.c +++ b/tests/nlattr_nlmsgerr.c @@ -69,11 +69,13 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); + static const uint8_t cookie[] = { 0xab, 0xfe }; + const int fd = create_nl_socket(NETLINK_SOCK_DIAG); const unsigned int hdrlen = sizeof(struct nlmsgerr); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(cookie)); - static const uint8_t cookie[] = { 0xab, 0xfe }; TEST_NLATTR(fd, nlh0, hdrlen, init_nlmsgerr, print_nlmsgerr, NLMSGERR_ATTR_COOKIE, diff --git a/tests/nlattr_packet_diag_msg.c b/tests/nlattr_packet_diag_msg.c index 4211e201..12cefcf9 100644 --- a/tests/nlattr_packet_diag_msg.c +++ b/tests/nlattr_packet_diag_msg.c @@ -98,13 +98,6 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - int fd = create_nl_socket(NETLINK_SOCK_DIAG); - const unsigned int hdrlen = sizeof(struct packet_diag_msg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - static const struct packet_diag_info pinfo = { .pdi_index = 0xabcddafa, .pdi_version = 0xbabcdafb, @@ -113,16 +106,6 @@ main(void) .pdi_tstamp = 0xeafbaadf, .pdi_flags = PDI_RUNNING }; - TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, - init_packet_diag_msg, print_packet_diag_msg, - PACKET_DIAG_INFO, pattern, pinfo, - PRINT_FIELD_U("{", pinfo, pdi_index); - PRINT_FIELD_U(", ", pinfo, pdi_version); - PRINT_FIELD_U(", ", pinfo, pdi_reserve); - PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh); - PRINT_FIELD_U(", ", pinfo, pdi_tstamp); - printf(", pdi_flags=PDI_RUNNING}")); - const struct packet_diag_mclist dml[] = { { .pdmc_index = ifindex_lo(), @@ -139,11 +122,6 @@ main(void) .pdmc_addr = "5678" } }; - TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, - init_packet_diag_msg, print_packet_diag_msg, - PACKET_DIAG_MCLIST, pattern, dml, - print_packet_diag_mclist); - static const struct packet_diag_ring pdr = { .pdr_block_size = 0xabcdafed, .pdr_block_nr = 0xbcadefae, @@ -153,6 +131,30 @@ main(void) .pdr_sizeof_priv = 0xfeadeacd, .pdr_features = 0xadebadea }; + + int fd = create_nl_socket(NETLINK_SOCK_DIAG); + const unsigned int hdrlen = sizeof(struct packet_diag_msg); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(dml)); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, + init_packet_diag_msg, print_packet_diag_msg, + PACKET_DIAG_INFO, pattern, pinfo, + PRINT_FIELD_U("{", pinfo, pdi_index); + PRINT_FIELD_U(", ", pinfo, pdi_version); + PRINT_FIELD_U(", ", pinfo, pdi_reserve); + PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh); + PRINT_FIELD_U(", ", pinfo, pdi_tstamp); + printf(", pdi_flags=PDI_RUNNING}")); + + TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, + init_packet_diag_msg, print_packet_diag_msg, + PACKET_DIAG_MCLIST, pattern, dml, + print_packet_diag_mclist); + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_packet_diag_msg, print_packet_diag_msg, PACKET_DIAG_RX_RING, pattern, pdr, diff --git a/tests/nlattr_rtgenmsg.c b/tests/nlattr_rtgenmsg.c index ceb061df..24bd3772 100644 --- a/tests/nlattr_rtgenmsg.c +++ b/tests/nlattr_rtgenmsg.c @@ -66,13 +66,14 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct rtgenmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; + char nla_type_str[256]; + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(nla_type_str)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; - char nla_type_str[256]; sprintf(nla_type_str, "%#x /* NETNSA_??? */", nla_type); TEST_NLATTR_(fd, nlh0, hdrlen, init_rtgenmsg, print_rtgenmsg, diff --git a/tests/nlattr_rtmsg.c b/tests/nlattr_rtmsg.c index 5f29f0c0..8f1afbf7 100644 --- a/tests/nlattr_rtmsg.c +++ b/tests/nlattr_rtmsg.c @@ -81,13 +81,14 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct rtmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; + char nla_type_str[256]; + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(nla_type_str)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; - char nla_type_str[256]; sprintf(nla_type_str, "%#x /* RTA_??? */", nla_type); TEST_NLATTR_(fd, nlh0, hdrlen, init_rtmsg, print_rtmsg, diff --git a/tests/nlattr_smc_diag_msg.c b/tests/nlattr_smc_diag_msg.c index a06ad624..3e7e7209 100644 --- a/tests/nlattr_smc_diag_msg.c +++ b/tests/nlattr_smc_diag_msg.c @@ -97,13 +97,6 @@ int main(void) { skip_if_unavailable("/proc/self/fd/"); - int fd = create_nl_socket(NETLINK_SOCK_DIAG); - const unsigned int hdrlen = sizeof(struct smc_diag_msg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - static const struct smc_diag_conninfo cinfo = { .token = 0xabcdefac, .sndbuf_size = 0xbcdaefad, @@ -149,6 +142,25 @@ int main(void) .count = 0xcdedbad7 } }; + static const struct smc_diag_lgrinfo linfo = { + .lnk[0] = { + .link_id = 0xaf, + .ibport = 0xfa, + .ibname = "123", + .gid = "456", + .peer_gid = "789" + }, + .role = SMC_CLNT + }; + + int fd = create_nl_socket(NETLINK_SOCK_DIAG); + const unsigned int hdrlen = sizeof(struct smc_diag_msg); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + + MAX(sizeof(cinfo), sizeof(linfo))); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_smc_diag_msg, print_smc_diag_msg, @@ -170,16 +182,6 @@ int main(void) PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_fin); printf("}")); - static const struct smc_diag_lgrinfo linfo = { - .lnk[0] = { - .link_id = 0xaf, - .ibport = 0xfa, - .ibname = "123", - .gid = "456", - .peer_gid = "789" - }, - .role = SMC_CLNT - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_smc_diag_msg, print_smc_diag_msg, SMC_DIAG_LGRINFO, pattern, linfo, diff --git a/tests/nlattr_tc_stats.c b/tests/nlattr_tc_stats.c index dd76cc7d..cb9d8b55 100644 --- a/tests/nlattr_tc_stats.c +++ b/tests/nlattr_tc_stats.c @@ -77,7 +77,7 @@ main(void) skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 8 * 5); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_tca_stab.c b/tests/nlattr_tca_stab.c index de6d3569..690a5053 100644 --- a/tests/nlattr_tca_stab.c +++ b/tests/nlattr_tca_stab.c @@ -87,7 +87,11 @@ main(void) skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4 +#ifdef HAVE_STRUCT_TC_SIZESPEC + - 4 + sizeof(struct tc_sizespec) +#endif + ); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_tcamsg.c b/tests/nlattr_tcamsg.c index 7f611532..ced74d2f 100644 --- a/tests/nlattr_tcamsg.c +++ b/tests/nlattr_tcamsg.c @@ -62,7 +62,7 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct tcamsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_tcmsg.c b/tests/nlattr_tcmsg.c index 32ce4790..a8741c60 100644 --- a/tests/nlattr_tcmsg.c +++ b/tests/nlattr_tcmsg.c @@ -68,7 +68,8 @@ main(void) const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct tcmsg); - void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); + void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(struct tc_stats)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); diff --git a/tests/nlattr_unix_diag_msg.c b/tests/nlattr_unix_diag_msg.c index 22954042..488891c3 100644 --- a/tests/nlattr_unix_diag_msg.c +++ b/tests/nlattr_unix_diag_msg.c @@ -75,17 +75,24 @@ main(void) { skip_if_unavailable("/proc/self/fd/"); - const int fd = create_nl_socket(NETLINK_SOCK_DIAG); - const unsigned int hdrlen = sizeof(struct unix_diag_msg); - void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); - - static char pattern[4096]; - fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); - static const struct unix_diag_vfs uv = { .udiag_vfs_dev = 0xabcddafa, .udiag_vfs_ino = 0xbafabcda }; + static const struct unix_diag_rqlen rql = { + .udiag_rqueue = 0xfabdcdad, + .udiag_wqueue = 0xbacdadcf + }; + static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac }; + + const int fd = create_nl_socket(NETLINK_SOCK_DIAG); + const unsigned int hdrlen = sizeof(struct unix_diag_msg); + void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), + NLA_HDRLEN + sizeof(inode)); + + static char pattern[4096]; + fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); + TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_unix_diag_msg, print_unix_diag_msg, UNIX_DIAG_VFS, pattern, uv, @@ -95,10 +102,6 @@ main(void) PRINT_FIELD_U(", ", uv, udiag_vfs_ino); printf("}")); - static const struct unix_diag_rqlen rql = { - .udiag_rqueue = 0xfabdcdad, - .udiag_wqueue = 0xbacdadcf - }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_unix_diag_msg, print_unix_diag_msg, UNIX_DIAG_RQLEN, pattern, rql, @@ -106,7 +109,6 @@ main(void) PRINT_FIELD_U(", ", rql, udiag_wqueue); printf("}")); - static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac }; TEST_NLATTR_ARRAY(fd, nlh0, hdrlen, init_unix_diag_msg, print_unix_diag_msg, UNIX_DIAG_ICONS, pattern, inode, print_uint); diff --git a/tests/tests.h b/tests/tests.h index 1712294c..7c323dc6 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -117,6 +117,9 @@ void *tail_alloc(const size_t) void *tail_memdup(const void *, const size_t) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2)); +#define midtail_alloc(after_, before_) \ + ((void *) ((char *) tail_alloc(((before_) + (after_))) + (before_))) + /* * Allocate an object of the specified type at the end * of a mapped memory region.