1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10
11 #include <stdio.h>
12 #include "test_nlattr.h"
13 #include <linux/neighbour.h>
14 #include <linux/rtnetlink.h>
15
16 static void
17 init_ndtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
18 {
19 SET_STRUCT(struct nlmsghdr, nlh,
20 .nlmsg_len = msg_len,
21 .nlmsg_type = RTM_GETNEIGHTBL,
22 .nlmsg_flags = NLM_F_DUMP
23 );
24
25 struct ndtmsg *const msg = NLMSG_DATA(nlh);
26 SET_STRUCT(struct ndtmsg, msg,
27 .ndtm_family = AF_NETLINK
28 );
29 }
30
31 static void
32 print_ndtmsg(const unsigned int msg_len)
33 {
34 printf("{nlmsg_len=%u, nlmsg_type=RTM_GETNEIGHTBL"
35 ", nlmsg_flags=NLM_F_DUMP, nlmsg_seq=0, nlmsg_pid=0}"
36 ", {ndtm_family=AF_NETLINK}",
37 msg_len);
38 }
39
40 int
41 main(void)
42 {
43 skip_if_unavailable("/proc/self/fd/");
44
45 const int fd = create_nl_socket(NETLINK_ROUTE);
46 const unsigned int hdrlen = sizeof(struct ndtmsg);
47 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 11 * 8);
48
49 static char pattern[4096];
50 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
51
52 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
53 char nla_type_str[256];
54 sprintf(nla_type_str, "%#x /* NDTA_??? */", nla_type);
55 TEST_NLATTR_(fd, nlh0, hdrlen,
56 init_ndtmsg, print_ndtmsg,
57 nla_type, nla_type_str,
58 4, pattern, 4,
59 print_quoted_hex(pattern, 4));
60
61 static const struct ndt_config ndtc = {
62 .ndtc_key_len = 0xabcd,
63 .ndtc_entry_size = 0xbcda,
64 .ndtc_entries = 0xcdabedad,
65 .ndtc_last_flush = 0xdebaedba,
66 .ndtc_last_rand = 0xedadedab,
67 .ndtc_hash_rnd = 0xfeadedaf,
68 .ndtc_hash_mask = 0xadbcdead,
69 .ndtc_hash_chain_gc = 0xbdaedacd,
70 .ndtc_proxy_qlen = 0xcdeaedab
71 };
72 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
73 init_ndtmsg, print_ndtmsg,
74 NDTA_CONFIG, pattern, ndtc,
75 printf("{");
76 PRINT_FIELD_U(ndtc, ndtc_key_len);
77 printf(", ");
78 PRINT_FIELD_U(ndtc, ndtc_entry_size);
79 printf(", ");
80 PRINT_FIELD_U(ndtc, ndtc_entries);
81 printf(", ");
82 PRINT_FIELD_U(ndtc, ndtc_last_flush);
83 printf(", ");
84 PRINT_FIELD_U(ndtc, ndtc_last_rand);
85 printf(", ");
86 PRINT_FIELD_U(ndtc, ndtc_hash_rnd);
87 printf(", ");
88 PRINT_FIELD_0X(ndtc, ndtc_hash_mask);
89 printf(", ");
90 PRINT_FIELD_U(ndtc, ndtc_hash_chain_gc);
91 printf(", ");
92 PRINT_FIELD_U(ndtc, ndtc_proxy_qlen);
93 printf("}"));
94
95 static const struct nlattr nla = {
96 .nla_len = sizeof(nla),
97 .nla_type = NDTPA_IFINDEX
98 };
99 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
100 init_ndtmsg, print_ndtmsg,
101 NDTA_PARMS, pattern, nla,
102 printf("{");
103 PRINT_FIELD_U(nla, nla_len);
104 printf(", nla_type=NDTPA_IFINDEX}"));
105
106 static const struct ndt_stats ndtst = {
107 .ndts_allocs = 0xabcdedabedadedfa,
108 .ndts_destroys = 0xbcdefabefacdbaad,
109 .ndts_hash_grows = 0xcdbadefacdcbaede,
110 .ndts_res_failed = 0xdedbaecfdbcadcfe,
111 .ndts_lookups = 0xedfafdedbdadedec,
112 .ndts_hits = 0xfebdeadebcddeade,
113 .ndts_rcv_probes_mcast = 0xadebfeadecddeafe,
114 .ndts_rcv_probes_ucast = 0xbcdefeacdadecdfe,
115 .ndts_periodic_gc_runs = 0xedffeadedeffbecc,
116 .ndts_forced_gc_runs = 0xfeefefeabedeedcd,
117 .ndts_table_fulls = 0xadebfefaecdfeade
118 };
119 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
120 init_ndtmsg, print_ndtmsg,
121 NDTA_STATS, pattern, ndtst,
122 printf("{");
123 PRINT_FIELD_U(ndtst, ndts_allocs);
124 printf(", ");
125 PRINT_FIELD_U(ndtst, ndts_destroys);
126 printf(", ");
127 PRINT_FIELD_U(ndtst, ndts_hash_grows);
128 printf(", ");
129 PRINT_FIELD_U(ndtst, ndts_res_failed);
130 printf(", ");
131 PRINT_FIELD_U(ndtst, ndts_lookups);
132 printf(", ");
133 PRINT_FIELD_U(ndtst, ndts_hits);
134 printf(", ");
135 PRINT_FIELD_U(ndtst, ndts_rcv_probes_mcast);
136 printf(", ");
137 PRINT_FIELD_U(ndtst, ndts_rcv_probes_ucast);
138 printf(", ");
139 PRINT_FIELD_U(ndtst, ndts_periodic_gc_runs);
140 printf(", ");
141 PRINT_FIELD_U(ndtst, ndts_forced_gc_runs);
142 printf(", ");
143 PRINT_FIELD_U(ndtst, ndts_table_fulls);
144 printf("}"));
145
146 puts("+++ exited with 0 +++");
147 return 0;
148 }