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 "test_nlattr.h"
35 # include <linux/if_bridge.h>
36 # include <linux/rtnetlink.h>
37
38 # ifndef MDBA_ROUTER
39 # define MDBA_ROUTER 2
40 # endif
41 # ifndef MDBA_ROUTER_PORT
42 # define MDBA_ROUTER_PORT 1
43 # endif
44 # ifndef MDBA_ROUTER_PATTR_TYPE
45 # define MDBA_ROUTER_PATTR_TYPE 2
46 # endif
47 # ifndef MDB_RTR_TYPE_DISABLED
48 # define MDB_RTR_TYPE_DISABLED 0
49 # endif
50
51 const unsigned int hdrlen = sizeof(struct br_port_msg);
52
53 static void
init_br_port_msg(struct nlmsghdr * const nlh,const unsigned int msg_len)54 init_br_port_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
55 {
56 SET_STRUCT(struct nlmsghdr, nlh,
57 .nlmsg_len = msg_len,
58 .nlmsg_type = RTM_GETMDB,
59 .nlmsg_flags = NLM_F_DUMP
60 );
61
62 struct br_port_msg *const msg = NLMSG_DATA(nlh);
63 SET_STRUCT(struct br_port_msg, msg,
64 .family = AF_UNIX,
65 .ifindex = ifindex_lo()
66 );
67
68 struct nlattr *nla = NLMSG_ATTR(nlh, sizeof(*msg));
69 SET_STRUCT(struct nlattr, nla,
70 .nla_len = msg_len - NLMSG_SPACE(hdrlen),
71 .nla_type = MDBA_ROUTER
72 );
73 }
74
75 static void
print_br_port_msg(const unsigned int msg_len)76 print_br_port_msg(const unsigned int msg_len)
77 {
78 printf("{len=%u, type=RTM_GETMDB, flags=NLM_F_DUMP"
79 ", seq=0, pid=0}, {family=AF_UNIX"
80 ", ifindex=" IFINDEX_LO_STR "}"
81 ", {{nla_len=%u, nla_type=MDBA_ROUTER}",
82 msg_len, msg_len - NLMSG_SPACE(hdrlen));
83 }
84
85 int
main(void)86 main(void)
87 {
88 skip_if_unavailable("/proc/self/fd/");
89
90 const uint32_t ifindex = ifindex_lo();
91 const uint8_t type = MDB_RTR_TYPE_DISABLED;
92 static const struct nlattr nla = {
93 .nla_len = NLA_HDRLEN + sizeof(type),
94 .nla_type = MDBA_ROUTER_PATTR_TYPE
95 };
96 char buf[NLMSG_ALIGN(ifindex) + NLA_HDRLEN + sizeof(type)];
97
98 const int fd = create_nl_socket(NETLINK_ROUTE);
99
100 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
101 NLA_HDRLEN + sizeof(buf));
102
103 static char pattern[4096];
104 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
105
106 TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
107 init_br_port_msg, print_br_port_msg,
108 MDBA_ROUTER_PORT, pattern, ifindex,
109 printf(IFINDEX_LO_STR));
110
111 memcpy(buf, &ifindex, sizeof(ifindex));
112 memcpy(buf + NLMSG_ALIGN(ifindex), &nla, sizeof(nla));
113 memcpy(buf + NLMSG_ALIGN(ifindex) + NLA_HDRLEN, &type, sizeof(type));
114 TEST_NLATTR(fd, nlh0 - NLA_HDRLEN, hdrlen + NLA_HDRLEN,
115 init_br_port_msg, print_br_port_msg,
116 MDBA_ROUTER_PORT, sizeof(buf), buf, sizeof(buf),
117 printf(IFINDEX_LO_STR
118 ", {{nla_len=%u, nla_type=MDBA_ROUTER_PATTR_TYPE}"
119 ", MDB_RTR_TYPE_DISABLED}}",
120 nla.nla_len));
121
122 puts("+++ exited with 0 +++");
123 return 0;
124 }
125
126 #else
127
128 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_BR_PORT_MSG")
129
130 #endif
131