1 /*
2 * netlink attribute ifinfomsg common code.
3 *
4 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
5 * Copyright (c) 2017-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #ifndef STRACE_TESTS_NLATTR_IFLA_H
12 # define STRACE_TESTS_NLATTR_IFLA_H
13
14 # include "tests.h"
15
16 # ifndef IFLA_ATTR
17 # error "Please define IFLA_ATTR before including this file"
18 # endif
19
20 # ifndef IFLA_AF
21 # define IFLA_AF AF_UNIX
22 # endif
23 # ifndef IFLA_AF_STR
24 # define IFLA_AF_STR "AF_UNIX"
25 # endif
26
27 static const unsigned int hdrlen = sizeof(struct ifinfomsg);
28
29 static void
30 init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
31 {
32 SET_STRUCT(struct nlmsghdr, nlh,
33 .nlmsg_len = msg_len,
34 .nlmsg_type = RTM_GETLINK,
35 .nlmsg_flags = NLM_F_DUMP
36 );
37
38 struct ifinfomsg *const msg = NLMSG_DATA(nlh);
39 SET_STRUCT(struct ifinfomsg, msg,
40 .ifi_family = IFLA_AF,
41 .ifi_type = ARPHRD_LOOPBACK,
42 .ifi_index = ifindex_lo(),
43 .ifi_flags = IFF_UP,
44 );
45
46 struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
47 SET_STRUCT(struct nlattr, nla,
48 .nla_len = msg_len - NLMSG_SPACE(hdrlen),
49 .nla_type = IFLA_ATTR
50 );
51 }
52
53 static void
54 print_ifinfomsg(const unsigned int msg_len)
55 {
56 printf("{nlmsg_len=%u, nlmsg_type=" XLAT_FMT ", nlmsg_flags=" XLAT_FMT
57 ", nlmsg_seq=0, nlmsg_pid=0}, {ifi_family=" XLAT_FMT
58 ", ifi_type=" XLAT_FMT ", ifi_index=" XLAT_FMT_U
59 ", ifi_flags=" XLAT_FMT ", ifi_change=0}"
60 ", [{nla_len=%u, nla_type=" XLAT_FMT "}",
61 msg_len, XLAT_ARGS(RTM_GETLINK), XLAT_ARGS(NLM_F_DUMP),
62 XLAT_SEL(IFLA_AF, IFLA_AF_STR), XLAT_ARGS(ARPHRD_LOOPBACK),
63 XLAT_SEL(ifindex_lo(), IFINDEX_LO_STR), XLAT_ARGS(IFF_UP),
64 msg_len - NLMSG_SPACE(hdrlen),
65 XLAT_SEL(IFLA_ATTR, STRINGIFY_VAL(IFLA_ATTR)));
66 }
67
68 #endif /* STRACE_TESTS_NLATTR_IFLA_H */