1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
* Copyright (c) 2017-2021 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "test_nlattr.h"
#include <linux/ip.h>
#include <linux/rtnetlink.h>
#if !defined HAVE_MEMPCPY
# undef mempcpy
# define mempcpy strace_mempcpy
static void *
mempcpy(void *dest, const void *src, size_t n)
{
memcpy(dest, src, n);
return dest + n;
}
#endif
static void
print_quoted_hex_ellipsis(const void *const instr, const size_t len)
{
const unsigned char *str = instr;
printf("\"");
for (size_t i = 0; i < MIN(len, DEFAULT_STRLEN); ++i)
printf("\\x%02x", str[i]);
printf("\"");
if (len > DEFAULT_STRLEN)
printf("...");
}
#define LWTUNNEL_ENCAP_NONE 0
#define DEF_NLATTR_RTMSG_FUNCS(sfx_, af_) \
static void \
init_##sfx_(struct nlmsghdr *const nlh, const unsigned int msg_len) \
{ \
SET_STRUCT(struct nlmsghdr, nlh, \
.nlmsg_len = msg_len, \
.nlmsg_type = RTM_GETROUTE, \
.nlmsg_flags = NLM_F_DUMP \
); \
\
struct rtmsg *const msg = NLMSG_DATA(nlh); \
SET_STRUCT(struct rtmsg, msg, \
.rtm_family = (af_), \
.rtm_tos = IPTOS_LOWDELAY, \
.rtm_table = RT_TABLE_DEFAULT, \
.rtm_protocol = RTPROT_KERNEL, \
.rtm_scope = RT_SCOPE_UNIVERSE, \
.rtm_type = RTN_LOCAL, \
.rtm_flags = RTM_F_NOTIFY \
); \
} \
\
static void \
print_##sfx_(const unsigned int msg_len) \
{ \
printf("{nlmsg_len=%u, nlmsg_type=RTM_GETROUTE" \
", nlmsg_flags=NLM_F_DUMP" \
", nlmsg_seq=0, nlmsg_pid=0}, {rtm_family=" #af_ \
", rtm_dst_len=0, rtm_src_len=0" \
", rtm_tos=IPTOS_LOWDELAY" \
", rtm_table=RT_TABLE_DEFAULT" \
", rtm_protocol=RTPROT_KERNEL" \
", rtm_scope=RT_SCOPE_UNIVERSE" \
", rtm_type=RTN_LOCAL" \
", rtm_flags=RTM_F_NOTIFY}", \
msg_len); \
} \
/* End of DEF_NLATTR_RTMSG_FUNCS */
DEF_NLATTR_RTMSG_FUNCS(rtmsg, AF_UNIX)
DEF_NLATTR_RTMSG_FUNCS(rtmsg_inet, AF_INET)
DEF_NLATTR_RTMSG_FUNCS(rtmsg_inet6, AF_INET6)
int
main(void)
{
skip_if_unavailable("/proc/self/fd/");
const int fd = create_nl_socket(NETLINK_ROUTE);
const unsigned int hdrlen = sizeof(struct rtmsg);
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);
sprintf(nla_type_str, "%#x /* RTA_??? */", nla_type);
TEST_NLATTR_(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
nla_type, nla_type_str,
4, pattern, 4,
print_quoted_hex(pattern, 4));
TEST_NLATTR(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_DST, 4, pattern, 4,
print_quoted_hex(pattern, 4));
#define MAX_ADDR_SZ 35
static const struct {
uint8_t af;
uint8_t addr[MAX_ADDR_SZ];
const char *str;
void (* init_fn)(struct nlmsghdr *, unsigned int);
void (* print_fn)(unsigned int);
uint32_t len;
} addrs[] = {
{ AF_UNIX, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
"\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\"",
init_rtmsg, print_rtmsg, 10 },
{ AF_UNIX,
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, [MAX_ADDR_SZ - 1] = 0xea },
"\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09"
"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
"\\x00\\x00"
#if DEFAULT_STRLEN == 32
"\"...",
#else
"\\x00\\x00\\xea\"",
#endif
init_rtmsg, print_rtmsg, MAX_ADDR_SZ },
{ AF_INET, { 0xde, 0xca, 0xff, 0xed },
"inet_addr(\"222.202.255.237\")",
init_rtmsg_inet, print_rtmsg_inet, 4 },
{ AF_INET6, { 0xfa, 0xce, 0xbe, 0xef, [15] = 0xda },
"inet_pton(AF_INET6, \"face:beef::da\")",
init_rtmsg_inet6, print_rtmsg_inet6, 16 },
};
static const struct strval32 addr_attrs[] = {
{ ARG_STR(RTA_DST) },
{ ARG_STR(RTA_SRC) },
{ ARG_STR(RTA_GATEWAY) },
{ ARG_STR(RTA_PREFSRC) },
{ ARG_STR(RTA_NEWDST) },
};
for (size_t i = 0; i < ARRAY_SIZE(addrs); i++) {
for (size_t j = 0; j < ARRAY_SIZE(addr_attrs); j++) {
TEST_NLATTR_(fd, nlh0, hdrlen,
addrs[i].init_fn, addrs[i].print_fn,
addr_attrs[j].val, addr_attrs[j].str,
addrs[i].len - 1, addrs[i].addr,
addrs[i].len - 1,
print_quoted_hex_ellipsis(addrs[i].addr,
addrs[i].len - 1)
);
TEST_NLATTR_(fd, nlh0, hdrlen,
addrs[i].init_fn, addrs[i].print_fn,
addr_attrs[j].val, addr_attrs[j].str,
addrs[i].len, addrs[i].addr, addrs[i].len,
printf("%s", addrs[i].str));
}
}
const uint32_t ifindex = ifindex_lo();
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_OIF, pattern, ifindex,
printf(IFINDEX_LO_STR));
const uint32_t rt_class_id = RT_TABLE_DEFAULT;
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_TABLE, pattern, rt_class_id,
printf("RT_TABLE_DEFAULT"));
struct nlattr nla = {
.nla_type = RTAX_LOCK,
.nla_len = sizeof(nla)
};
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_METRICS, pattern, nla,
printf("{nla_len=%u, nla_type=RTAX_LOCK}",
nla.nla_len));
struct rtnexthop nh = {
.rtnh_len = sizeof(nh) - 1,
.rtnh_flags = RTNH_F_DEAD,
.rtnh_hops = 0xab,
.rtnh_ifindex = ifindex_lo()
};
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_MULTIPATH, pattern, nh,
printf("[{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
", rtnh_hops=%u"
", rtnh_ifindex=" IFINDEX_LO_STR "}]",
nh.rtnh_len, nh.rtnh_hops));
char buf[RTNH_ALIGN(sizeof(nh)) + sizeof(nla)];
nh.rtnh_len = sizeof(buf);
nla.nla_type = RTA_DST;
memcpy(buf, &nh, sizeof(nh));
memcpy(buf + RTNH_ALIGN(sizeof(nh)), &nla, sizeof(nla));
TEST_NLATTR(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_MULTIPATH, sizeof(buf), buf, sizeof(buf),
printf("[{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
", {nla_len=%u, nla_type=RTA_DST}]",
nh.rtnh_len, nh.rtnh_hops, nla.nla_len));
static const struct in_addr gw_inet_addr = { .s_addr = BE32(0xdeadbeef) };
static const uint8_t via_inet6_addr[16] = {
0xde, 0xad, 0xfa, 0xce, 0xbe, 0xef, 0xca, 0xfe,
0xfe, 0xed, 0xba, 0x5e, 0x00, 0x00, 0xfa, 0xde };
static const struct rtvia rtvia = { .rtvia_family = AF_INET6 };
char buf2[2 * (RTNH_ALIGN(sizeof(nh)) + NLMSG_ALIGN(sizeof(nla))) +
+ NLMSG_ALIGN(sizeof(gw_inet_addr))
+ NLMSG_ALIGN(offsetof(struct rtvia, rtvia_addr)
+ sizeof(via_inet6_addr))];
char *pos = buf2;
nh.rtnh_len = RTNH_ALIGN(sizeof(nh)) + NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(sizeof(gw_inet_addr));
nh.rtnh_flags = 0xc0;
nla.nla_type = RTA_GATEWAY;
nla.nla_len = NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(sizeof(gw_inet_addr));
pos = mempcpy(pos, &nh, sizeof(nh));
pos = mempcpy(pos, &nla, sizeof(nla));
pos = mempcpy(pos, &gw_inet_addr, sizeof(gw_inet_addr));
nh.rtnh_len = RTNH_ALIGN(sizeof(nh)) + NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(offsetof(struct rtvia, rtvia_addr)
+ sizeof(via_inet6_addr));
nla.nla_type = RTA_VIA;
nla.nla_len = NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(offsetof(struct rtvia, rtvia_addr)
+ sizeof(via_inet6_addr));
pos = mempcpy(pos, &nh, sizeof(nh));
pos = mempcpy(pos, &nla, sizeof(nla));
pos = mempcpy(pos, &rtvia, sizeof(rtvia));
pos = mempcpy(pos, &via_inet6_addr, sizeof(via_inet6_addr));
TEST_NLATTR(fd, nlh0, hdrlen,
init_rtmsg_inet, print_rtmsg_inet,
RTA_MULTIPATH, sizeof(buf2), buf2, sizeof(buf2),
printf("[[{rtnh_len=%u, rtnh_flags=RTNH_F_TRAP|0x80"
", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
", [{nla_len=%u, nla_type=RTA_GATEWAY}"
", inet_addr(\"222.173.190.239\")]]"
", [{rtnh_len=%u, rtnh_flags=RTNH_F_TRAP|0x80"
", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
", [{nla_len=%u, nla_type=RTA_VIA}"
", {rtvia_family=AF_INET6"
", inet_pton(AF_INET6"
", \"dead:face:beef:cafe:feed:ba5e:0:fade\""
", &rtvia_addr)}]]]",
(uint32_t) (RTNH_ALIGN(sizeof(nh))
+ NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(sizeof(gw_inet_addr))),
nh.rtnh_hops,
(uint32_t) (NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(sizeof(gw_inet_addr))),
(uint32_t) (RTNH_ALIGN(sizeof(nh))
+ NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(offsetof(struct rtvia, rtvia_addr)
+ sizeof(via_inet6_addr))),
nh.rtnh_hops,
(uint32_t) (NLMSG_ALIGN(sizeof(nla))
+ NLMSG_ALIGN(offsetof(struct rtvia, rtvia_addr)
+ sizeof(via_inet6_addr)))
));
static const struct rta_cacheinfo ci = {
.rta_clntref = 0xabcdefab,
.rta_lastuse = 0xbdadaedc,
.rta_expires = 0xcdadebad,
.rta_error = 0xdaedadeb,
.rta_used = 0xedfabdad,
.rta_id = 0xfeadbcda,
.rta_ts = 0xacdbaded,
.rta_tsage = 0xbadeadef
};
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_CACHEINFO, pattern, ci,
printf("{");
PRINT_FIELD_U(ci, rta_clntref);
printf(", ");
PRINT_FIELD_U(ci, rta_lastuse);
printf(", ");
PRINT_FIELD_U(ci, rta_expires);
printf(", ");
PRINT_FIELD_U(ci, rta_error);
printf(", ");
PRINT_FIELD_U(ci, rta_used);
printf(", ");
PRINT_FIELD_X(ci, rta_id);
printf(", ");
PRINT_FIELD_U(ci, rta_ts);
printf(", ");
PRINT_FIELD_U(ci, rta_tsage);
printf("}"));
static const struct rta_mfc_stats mfcs = {
.mfcs_packets = 0xadcdedfdadefadcd,
.mfcs_bytes = 0xbaedadedcdedadbd,
.mfcs_wrong_if = 0xcddeabeedaedabfa
};
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_MFC_STATS, pattern, mfcs,
printf("{");
PRINT_FIELD_U(mfcs, mfcs_packets);
printf(", ");
PRINT_FIELD_U(mfcs, mfcs_bytes);
printf(", ");
PRINT_FIELD_U(mfcs, mfcs_wrong_if);
printf("}"));
static const struct rtvia via = {
.rtvia_family = AF_INET
};
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_VIA, pattern, via,
printf("{rtvia_family=AF_INET}"));
static const char address4[] = "12.34.56.78";
struct in_addr a4 = {
.s_addr = inet_addr(address4)
};
char rtviabuf[sizeof(via) + sizeof(a4)];
memcpy(rtviabuf, &via, sizeof(via));
memcpy(rtviabuf + sizeof(via), &a4, sizeof(a4));
TEST_NLATTR(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_VIA, sizeof(rtviabuf), rtviabuf, sizeof(rtviabuf),
printf("{rtvia_family=AF_INET"
", rtvia_addr=inet_addr(\"%s\")}", address4));
const uint16_t encap_type = LWTUNNEL_ENCAP_NONE;
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,
RTA_ENCAP_TYPE, pattern, encap_type,
printf("LWTUNNEL_ENCAP_NONE"));
puts("+++ exited with 0 +++");
return 0;
}
|