2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 The strace developers.
6 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include "test_nlattr.h"
15 #ifdef HAVE_LINUX_NEIGHBOUR_H
16 # include <linux/neighbour.h>
18 #include <linux/rtnetlink.h>
23 init_ndmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
25 SET_STRUCT(struct nlmsghdr, nlh,
27 .nlmsg_type = RTM_GETNEIGH,
28 .nlmsg_flags = NLM_F_DUMP
31 struct ndmsg *const msg = NLMSG_DATA(nlh);
32 SET_STRUCT(struct ndmsg, msg,
33 .ndm_family = AF_UNIX,
34 .ndm_ifindex = ifindex_lo(),
35 .ndm_state = NUD_PERMANENT,
36 .ndm_flags = NTF_PROXY,
37 .ndm_type = RTN_UNSPEC
42 print_ndmsg(const unsigned int msg_len)
44 printf("{len=%u, type=RTM_GETNEIGH, flags=NLM_F_DUMP"
45 ", seq=0, pid=0}, {ndm_family=AF_UNIX"
46 ", ndm_ifindex=" IFINDEX_LO_STR
47 ", ndm_state=NUD_PERMANENT"
48 ", ndm_flags=NTF_PROXY"
49 ", ndm_type=RTN_UNSPEC}",
56 skip_if_unavailable("/proc/self/fd/");
58 const int fd = create_nl_socket(NETLINK_ROUTE);
59 const unsigned int hdrlen = sizeof(struct ndmsg);
60 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
61 NLA_HDRLEN + sizeof(struct nda_cacheinfo));
63 static char pattern[4096];
64 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
66 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
67 char nla_type_str[256];
68 sprintf(nla_type_str, "%#x /* NDA_??? */", nla_type);
69 TEST_NLATTR_(fd, nlh0, hdrlen,
70 init_ndmsg, print_ndmsg,
71 nla_type, nla_type_str,
73 print_quoted_hex(pattern, 4));
75 TEST_NLATTR(fd, nlh0, hdrlen,
76 init_ndmsg, print_ndmsg,
77 NDA_DST, 4, pattern, 4,
78 print_quoted_hex(pattern, 4));
80 static const struct nda_cacheinfo ci = {
81 .ndm_confirmed = 0xabcdedad,
82 .ndm_used = 0xbcdaedad,
83 .ndm_updated = 0xcdbadeda,
84 .ndm_refcnt = 0xdeadbeda
87 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
88 init_ndmsg, print_ndmsg,
89 NDA_CACHEINFO, pattern, ci,
90 PRINT_FIELD_U("{", ci, ndm_confirmed);
91 PRINT_FIELD_U(", ", ci, ndm_used);
92 PRINT_FIELD_U(", ", ci, ndm_updated);
93 PRINT_FIELD_U(", ", ci, ndm_refcnt);
96 const uint16_t port = 0xabcd;
97 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
98 init_ndmsg, print_ndmsg,
99 NDA_PORT, pattern, port,
100 printf("htons(%u)", ntohs(port)));
102 puts("+++ exited with 0 +++");