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/if_bridge.h>
14 #include <linux/rtnetlink.h>
15
16 static void
17 init_br_port_msg(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_GETMDB,
22 .nlmsg_flags = NLM_F_DUMP
23 );
24
25 struct br_port_msg *const msg = NLMSG_DATA(nlh);
26 SET_STRUCT(struct br_port_msg, msg,
27 .family = AF_UNIX,
28 .ifindex = ifindex_lo()
29 );
30 }
31
32 static void
33 print_br_port_msg(const unsigned int msg_len)
34 {
35 printf("{nlmsg_len=%u, nlmsg_type=RTM_GETMDB, nlmsg_flags=NLM_F_DUMP"
36 ", nlmsg_seq=0, nlmsg_pid=0}, {family=AF_UNIX"
37 ", ifindex=" IFINDEX_LO_STR "}",
38 msg_len);
39 }
40
41 int
42 main(void)
43 {
44 skip_if_unavailable("/proc/self/fd/");
45
46 const int fd = create_nl_socket(NETLINK_ROUTE);
47
48 const unsigned int hdrlen = sizeof(struct br_port_msg);
49 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4);
50
51 static char pattern[4096];
52 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
53
54 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
55 char nla_type_str[256];
56 sprintf(nla_type_str, "%#x /* MDBA_??? */", nla_type);
57 TEST_NLATTR_(fd, nlh0, hdrlen,
58 init_br_port_msg, print_br_port_msg,
59 nla_type, nla_type_str,
60 4, pattern, 4,
61 print_quoted_hex(pattern, 4));
62
63 puts("+++ exited with 0 +++");
64 return 0;
65 }