lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
 * Copyright (c) 2017-2018 The strace developers.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#include "tests.h"
 
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "test_nlattr.h"
#include <linux/ip.h>
#include <linux/rtnetlink.h>
 
#define RTA_ENCAP_TYPE 21
#define LWTUNNEL_ENCAP_NONE 0
 
static void
init_rtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
{
   SET_STRUCT(struct nlmsghdr, nlh,
       .nlmsg_len = msg_len,
       .nlmsg_type = RTM_GETROUTE,
       .nlmsg_flags = NLM_F_DUMP
   );
 
   struct rtmsg *const msg = NLMSG_DATA(nlh);
   SET_STRUCT(struct rtmsg, msg,
       .rtm_family = AF_UNIX,
       .rtm_tos = IPTOS_LOWDELAY,
       .rtm_table = RT_TABLE_DEFAULT,
       .rtm_protocol = RTPROT_KERNEL,
       .rtm_scope = RT_SCOPE_UNIVERSE,
       .rtm_type = RTN_LOCAL,
       .rtm_flags = RTM_F_NOTIFY
   );
}
 
static void
print_rtmsg(const unsigned int msg_len)
{
   printf("{len=%u, type=RTM_GETROUTE, flags=NLM_F_DUMP"
          ", seq=0, pid=0}, {rtm_family=AF_UNIX"
          ", rtm_dst_len=0, rtm_src_len=0"
          ", rtm_tos=IPTOS_LOWDELAY"
          ", rtm_table=RT_TABLE_DEFAULT"
          ", rtm_protocol=RTPROT_KERNEL"
          ", rtm_scope=RT_SCOPE_UNIVERSE"
          ", rtm_type=RTN_LOCAL"
          ", rtm_flags=RTM_F_NOTIFY}",
          msg_len);
}
 
int
main(void)
{
   skip_if_unavailable("/proc/self/fd/");
 
   const int fd = create_nl_socket(NETLINK_ROUTE);
   const unsigned int hdrlen = sizeof(struct rtmsg);
   const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
   char nla_type_str[256];
   void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
                  NLA_HDRLEN + sizeof(nla_type_str));
 
   static char pattern[4096];
   fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
 
   sprintf(nla_type_str, "%#x /* RTA_??? */", nla_type);
   TEST_NLATTR_(fd, nlh0, hdrlen,
            init_rtmsg, print_rtmsg,
            nla_type, nla_type_str,
            4, pattern, 4,
            print_quoted_hex(pattern, 4));
 
   TEST_NLATTR(fd, nlh0, hdrlen,
           init_rtmsg, print_rtmsg,
           RTA_DST, 4, pattern, 4,
           print_quoted_hex(pattern, 4));
 
   const uint32_t ifindex = ifindex_lo();
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_OIF, pattern, ifindex,
              printf(IFINDEX_LO_STR));
 
   const uint32_t rt_class_id = RT_TABLE_DEFAULT;
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_TABLE, pattern, rt_class_id,
              printf("RT_TABLE_DEFAULT"));
 
   struct nlattr nla = {
       .nla_type = RTAX_LOCK,
       .nla_len = sizeof(nla)
   };
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_METRICS, pattern, nla,
              printf("{nla_len=%u, nla_type=RTAX_LOCK}",
                 nla.nla_len));
   struct rtnexthop nh = {
       .rtnh_len = sizeof(nh) - 1,
       .rtnh_flags = RTNH_F_DEAD,
       .rtnh_hops = 0xab,
       .rtnh_ifindex = ifindex_lo()
   };
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_MULTIPATH, pattern, nh,
              printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
                 ", rtnh_hops=%u"
                 ", rtnh_ifindex=" IFINDEX_LO_STR "}",
                 nh.rtnh_len, nh.rtnh_hops));
 
   char buf[RTNH_ALIGN(sizeof(nh)) + sizeof(nla)];
   nh.rtnh_len = sizeof(buf);
   nla.nla_type = RTA_DST;
   memcpy(buf, &nh, sizeof(nh));
   memcpy(buf + RTNH_ALIGN(sizeof(nh)), &nla, sizeof(nla));
   TEST_NLATTR(fd, nlh0, hdrlen,
           init_rtmsg, print_rtmsg,
           RTA_MULTIPATH, sizeof(buf), buf, sizeof(buf),
           printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
              ", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
              ", {nla_len=%u, nla_type=RTA_DST}",
              nh.rtnh_len, nh.rtnh_hops, nla.nla_len));
 
   static const struct rta_cacheinfo ci = {
       .rta_clntref = 0xabcdefab,
       .rta_lastuse = 0xbdadaedc,
       .rta_expires = 0xcdadebad,
       .rta_error = 0xdaedadeb,
       .rta_used = 0xedfabdad,
       .rta_id = 0xfeadbcda,
       .rta_ts = 0xacdbaded,
       .rta_tsage = 0xbadeadef
   };
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_CACHEINFO, pattern, ci,
              PRINT_FIELD_U("{", ci, rta_clntref);
              PRINT_FIELD_U(", ", ci, rta_lastuse);
              PRINT_FIELD_U(", ", ci, rta_expires);
              PRINT_FIELD_U(", ", ci, rta_error);
              PRINT_FIELD_U(", ", ci, rta_used);
              PRINT_FIELD_X(", ", ci, rta_id);
              PRINT_FIELD_U(", ", ci, rta_ts);
              PRINT_FIELD_U(", ", ci, rta_tsage);
              printf("}"));
 
#ifdef HAVE_STRUCT_RTA_MFC_STATS
   static const struct rta_mfc_stats mfcs = {
       .mfcs_packets = 0xadcdedfdadefadcd,
       .mfcs_bytes = 0xbaedadedcdedadbd,
       .mfcs_wrong_if = 0xcddeabeedaedabfa
   };
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_MFC_STATS, pattern, mfcs,
              PRINT_FIELD_U("{", mfcs, mfcs_packets);
              PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
              PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
              printf("}"));
#endif
 
#ifdef HAVE_STRUCT_RTVIA
   static const struct rtvia via = {
       .rtvia_family = AF_INET
   };
 
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_VIA, pattern, via,
              printf("{rtvia_family=AF_INET}"));
 
   static const char address4[] = "12.34.56.78";
   struct in_addr a4 = {
       .s_addr = inet_addr(address4)
   };
   char rtviabuf[sizeof(via) + sizeof(a4)];
   memcpy(rtviabuf, &via, sizeof(via));
   memcpy(rtviabuf + sizeof(via), &a4, sizeof(a4));
 
   TEST_NLATTR(fd, nlh0, hdrlen,
           init_rtmsg, print_rtmsg,
           RTA_VIA, sizeof(rtviabuf), rtviabuf, sizeof(rtviabuf),
           printf("{rtvia_family=AF_INET"
              ", rtvia_addr=inet_addr(\"%s\")}", address4));
#endif
 
   const uint16_t encap_type = LWTUNNEL_ENCAP_NONE;
   TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
              init_rtmsg, print_rtmsg,
              RTA_ENCAP_TYPE, pattern, encap_type,
              printf("LWTUNNEL_ENCAP_NONE"));
 
   puts("+++ exited with 0 +++");
   return 0;
}