1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_STRUCT_BR_PORT_MSG
32 
33 # include <stdio.h>
34 # include <netinet/in.h>
35 # include "test_nlattr.h"
36 # include <linux/if_bridge.h>
37 # include <linux/rtnetlink.h>
38 
39 static void
init_br_port_msg(struct nlmsghdr * const nlh,const unsigned int msg_len)40 init_br_port_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
41 {
42 	SET_STRUCT(struct nlmsghdr, nlh,
43 		.nlmsg_len = msg_len,
44 		.nlmsg_type = RTM_GETMDB,
45 		.nlmsg_flags = NLM_F_DUMP
46 	);
47 
48 	struct br_port_msg *const msg = NLMSG_DATA(nlh);
49 	SET_STRUCT(struct br_port_msg, msg,
50 		.family = AF_UNIX,
51 		.ifindex = ifindex_lo()
52 	);
53 }
54 
55 static void
print_br_port_msg(const unsigned int msg_len)56 print_br_port_msg(const unsigned int msg_len)
57 {
58 	printf("{len=%u, type=RTM_GETMDB, flags=NLM_F_DUMP"
59 	       ", seq=0, pid=0}, {family=AF_UNIX"
60 	       ", ifindex=" IFINDEX_LO_STR "}",
61 	       msg_len);
62 }
63 
64 int
main(void)65 main(void)
66 {
67 	skip_if_unavailable("/proc/self/fd/");
68 
69 	const int fd = create_nl_socket(NETLINK_ROUTE);
70 
71 	const unsigned int hdrlen = sizeof(struct br_port_msg);
72 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4);
73 
74 	static char pattern[4096];
75 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
76 
77 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
78 	char nla_type_str[256];
79 	sprintf(nla_type_str, "%#x /* MDBA_??? */", nla_type);
80 	TEST_NLATTR_(fd, nlh0, hdrlen,
81 		     init_br_port_msg, print_br_port_msg,
82 		     nla_type, nla_type_str,
83 		     4, pattern, 4,
84 		     print_quoted_hex(pattern, 4));
85 
86 	puts("+++ exited with 0 +++");
87 	return 0;
88 }
89 
90 #else
91 
92 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_BR_PORT_MSG")
93 
94 #endif
95