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 #include <stdio.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include "test_nlattr.h"
35 #include <linux/ip.h>
36 #include <linux/rtnetlink.h>
37
38 #define RTA_ENCAP_TYPE 21
39 #define LWTUNNEL_ENCAP_NONE 0
40
41 static void
init_rtmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)42 init_rtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
43 {
44 SET_STRUCT(struct nlmsghdr, nlh,
45 .nlmsg_len = msg_len,
46 .nlmsg_type = RTM_GETROUTE,
47 .nlmsg_flags = NLM_F_DUMP
48 );
49
50 struct rtmsg *const msg = NLMSG_DATA(nlh);
51 SET_STRUCT(struct rtmsg, msg,
52 .rtm_family = AF_UNIX,
53 .rtm_tos = IPTOS_LOWDELAY,
54 .rtm_table = RT_TABLE_DEFAULT,
55 .rtm_protocol = RTPROT_KERNEL,
56 .rtm_scope = RT_SCOPE_UNIVERSE,
57 .rtm_type = RTN_LOCAL,
58 .rtm_flags = RTM_F_NOTIFY
59 );
60 }
61
62 static void
print_rtmsg(const unsigned int msg_len)63 print_rtmsg(const unsigned int msg_len)
64 {
65 printf("{len=%u, type=RTM_GETROUTE, flags=NLM_F_DUMP"
66 ", seq=0, pid=0}, {rtm_family=AF_UNIX"
67 ", rtm_dst_len=0, rtm_src_len=0"
68 ", rtm_tos=IPTOS_LOWDELAY"
69 ", rtm_table=RT_TABLE_DEFAULT"
70 ", rtm_protocol=RTPROT_KERNEL"
71 ", rtm_scope=RT_SCOPE_UNIVERSE"
72 ", rtm_type=RTN_LOCAL"
73 ", rtm_flags=RTM_F_NOTIFY}",
74 msg_len);
75 }
76
77 int
main(void)78 main(void)
79 {
80 skip_if_unavailable("/proc/self/fd/");
81
82 const int fd = create_nl_socket(NETLINK_ROUTE);
83 const unsigned int hdrlen = sizeof(struct rtmsg);
84 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
85 char nla_type_str[256];
86 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
87 NLA_HDRLEN + sizeof(nla_type_str));
88
89 static char pattern[4096];
90 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
91
92 sprintf(nla_type_str, "%#x /* RTA_??? */", nla_type);
93 TEST_NLATTR_(fd, nlh0, hdrlen,
94 init_rtmsg, print_rtmsg,
95 nla_type, nla_type_str,
96 4, pattern, 4,
97 print_quoted_hex(pattern, 4));
98
99 TEST_NLATTR(fd, nlh0, hdrlen,
100 init_rtmsg, print_rtmsg,
101 RTA_DST, 4, pattern, 4,
102 print_quoted_hex(pattern, 4));
103
104 const uint32_t ifindex = ifindex_lo();
105 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
106 init_rtmsg, print_rtmsg,
107 RTA_OIF, pattern, ifindex,
108 printf(IFINDEX_LO_STR));
109
110 const uint32_t rt_class_id = RT_TABLE_DEFAULT;
111 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
112 init_rtmsg, print_rtmsg,
113 RTA_TABLE, pattern, rt_class_id,
114 printf("RT_TABLE_DEFAULT"));
115
116 struct nlattr nla = {
117 .nla_type = RTAX_LOCK,
118 .nla_len = sizeof(nla)
119 };
120 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
121 init_rtmsg, print_rtmsg,
122 RTA_METRICS, pattern, nla,
123 printf("{nla_len=%u, nla_type=RTAX_LOCK}",
124 nla.nla_len));
125 struct rtnexthop nh = {
126 .rtnh_len = sizeof(nh) - 1,
127 .rtnh_flags = RTNH_F_DEAD,
128 .rtnh_hops = 0xab,
129 .rtnh_ifindex = ifindex_lo()
130 };
131 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
132 init_rtmsg, print_rtmsg,
133 RTA_MULTIPATH, pattern, nh,
134 printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
135 ", rtnh_hops=%u"
136 ", rtnh_ifindex=" IFINDEX_LO_STR "}",
137 nh.rtnh_len, nh.rtnh_hops));
138
139 char buf[RTNH_ALIGN(sizeof(nh)) + sizeof(nla)];
140 nh.rtnh_len = sizeof(buf);
141 nla.nla_type = RTA_DST;
142 memcpy(buf, &nh, sizeof(nh));
143 memcpy(buf + RTNH_ALIGN(sizeof(nh)), &nla, sizeof(nla));
144 TEST_NLATTR(fd, nlh0, hdrlen,
145 init_rtmsg, print_rtmsg,
146 RTA_MULTIPATH, sizeof(buf), buf, sizeof(buf),
147 printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
148 ", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
149 ", {nla_len=%u, nla_type=RTA_DST}",
150 nh.rtnh_len, nh.rtnh_hops, nla.nla_len));
151
152 static const struct rta_cacheinfo ci = {
153 .rta_clntref = 0xabcdefab,
154 .rta_lastuse = 0xbdadaedc,
155 .rta_expires = 0xcdadebad,
156 .rta_error = 0xdaedadeb,
157 .rta_used = 0xedfabdad,
158 .rta_id = 0xfeadbcda,
159 .rta_ts = 0xacdbaded,
160 .rta_tsage = 0xbadeadef
161 };
162 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
163 init_rtmsg, print_rtmsg,
164 RTA_CACHEINFO, pattern, ci,
165 PRINT_FIELD_U("{", ci, rta_clntref);
166 PRINT_FIELD_U(", ", ci, rta_lastuse);
167 PRINT_FIELD_U(", ", ci, rta_expires);
168 PRINT_FIELD_U(", ", ci, rta_error);
169 PRINT_FIELD_U(", ", ci, rta_used);
170 PRINT_FIELD_X(", ", ci, rta_id);
171 PRINT_FIELD_U(", ", ci, rta_ts);
172 PRINT_FIELD_U(", ", ci, rta_tsage);
173 printf("}"));
174
175 #ifdef HAVE_STRUCT_RTA_MFC_STATS
176 static const struct rta_mfc_stats mfcs = {
177 .mfcs_packets = 0xadcdedfdadefadcd,
178 .mfcs_bytes = 0xbaedadedcdedadbd,
179 .mfcs_wrong_if = 0xcddeabeedaedabfa
180 };
181 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
182 init_rtmsg, print_rtmsg,
183 RTA_MFC_STATS, pattern, mfcs,
184 PRINT_FIELD_U("{", mfcs, mfcs_packets);
185 PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
186 PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
187 printf("}"));
188 #endif
189
190 #ifdef HAVE_STRUCT_RTVIA
191 static const struct rtvia via = {
192 .rtvia_family = AF_INET
193 };
194
195 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
196 init_rtmsg, print_rtmsg,
197 RTA_VIA, pattern, via,
198 printf("{rtvia_family=AF_INET}"));
199
200 static const char address4[] = "12.34.56.78";
201 struct in_addr a4 = {
202 .s_addr = inet_addr(address4)
203 };
204 char rtviabuf[sizeof(via) + sizeof(a4)];
205 memcpy(rtviabuf, &via, sizeof(via));
206 memcpy(rtviabuf + sizeof(via), &a4, sizeof(a4));
207
208 TEST_NLATTR(fd, nlh0, hdrlen,
209 init_rtmsg, print_rtmsg,
210 RTA_VIA, sizeof(rtviabuf), rtviabuf, sizeof(rtviabuf),
211 printf("{rtvia_family=AF_INET"
212 ", rtvia_addr=inet_addr(\"%s\")}", address4));
213 #endif
214
215 const uint16_t encap_type = LWTUNNEL_ENCAP_NONE;
216 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
217 init_rtmsg, print_rtmsg,
218 RTA_ENCAP_TYPE, pattern, encap_type,
219 printf("LWTUNNEL_ENCAP_NONE"));
220
221 puts("+++ exited with 0 +++");
222 return 0;
223 }
224