Debian Codegraph @

src/libsystemd-network/test-ndisc-send.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/icmp6.h>
4
5 #include "sd-ndisc-protocol.h"
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "build.h"
10 #include "ether-addr-util.h"
11 #include "fd-util.h"
12 #include "hexdecoct.h"
13 #include "icmp6-util.h"
14 #include "in-addr-util.h"
15 #include "main-func.h"
16 #include "ndisc-option.h"
17 #include "netlink-util.h"
18 #include "network-common.h"
19 #include "options.h"
20 #include "parse-util.h"
21 #include "set.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "time-util.h"
25
26 static int arg_ifindex = 0;
27 static int arg_icmp6_type = 0;
28 static union in_addr_union arg_dest = IN_ADDR_NULL;
29 static uint8_t arg_hop_limit = 0;
30 static uint8_t arg_ra_flags = 0;
31 static uint8_t arg_preference = false;
32 static usec_t arg_lifetime = 0;
33 static usec_t arg_reachable = 0;
34 static usec_t arg_retransmit = 0;
35 static uint32_t arg_na_flags = 0;
36 static union in_addr_union arg_target_address = IN_ADDR_NULL;
37 static union in_addr_union arg_redirect_destination = IN_ADDR_NULL;
38 static bool arg_set_source_mac = false;
39 static struct ether_addr arg_source_mac = {};
40 static bool arg_set_target_mac = false;
41 static struct ether_addr arg_target_mac = {};
42 static struct ip6_hdr *arg_redirected_header = NULL;
43 static bool arg_set_mtu = false;
44 static uint32_t arg_mtu = 0;
45
46 STATIC_DESTRUCTOR_REGISTER(arg_redirected_header, freep);
47
48 static int parse_icmp6_type(const char *str) {
49 if (STR_IN_SET(str, "router-solicit", "rs", "RS"))
50 return ND_ROUTER_SOLICIT;
51 if (STR_IN_SET(str, "router-advertisement", "ra", "RA"))
52 return ND_ROUTER_ADVERT;
53 if (STR_IN_SET(str, "neighbor-solicit", "ns", "NS"))
54 return ND_NEIGHBOR_SOLICIT;
55 if (STR_IN_SET(str, "neighbor-advertisement", "na", "NA"))
56 return ND_NEIGHBOR_ADVERT;
57 if (STR_IN_SET(str, "redirect", "rd", "RD"))
58 return ND_REDIRECT;
59 return -EINVAL;
60 }
61
62 static int parse_preference(const char *str) {
63 if (streq(str, "low"))
64 return SD_NDISC_PREFERENCE_LOW;
65 if (streq(str, "medium"))
66 return SD_NDISC_PREFERENCE_MEDIUM;
67 if (streq(str, "high"))
68 return SD_NDISC_PREFERENCE_HIGH;
69 if (streq(str, "reserved"))
70 return SD_NDISC_PREFERENCE_RESERVED;
71 return -EINVAL;
72 }
73
74 static int parse_argv(int argc, char *argv[]) {
75 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
76 int r;
77
78 assert(argc >= 0);
79 assert(argv);
80
81 OptionParser opts = { argc, argv };
82
83 FOREACH_OPTION_OR_RETURN(c, &opts)
84 switch (c) {
85
86 OPTION_COMMON_VERSION:
87 return version();
88
89 OPTION('i', "interface", "INTERFACE", "Network interface"):
90 r = rtnl_resolve_interface_or_warn(&rtnl, opts.arg);
91 if (r < 0)
92 return r;
93 arg_ifindex = r;
94 break;
95
96 OPTION('t', "type", "TYPE", "ICMPv6 message type"):
97 r = parse_icmp6_type(opts.arg);
98 if (r < 0)
99 return log_error_errno(r, "Failed to parse message type: %m");
100 arg_icmp6_type = r;
101 break;
102
103 OPTION('d', "dest", "ADDRESS", "Destination address"):
104 r = in_addr_from_string(AF_INET6, opts.arg, &arg_dest);
105 if (r < 0)
106 return log_error_errno(r, "Failed to parse destination address: %m");
107 if (!in6_addr_is_link_local(&arg_dest.in6))
108 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
109 "The destination address %s is not a link-local address.", opts.arg);
110 break;
111
112 OPTION_GROUP("Router Advertisement"): {}
113
114 OPTION_LONG("hop-limit", "LIMIT", "Hop limit"):
115 r = safe_atou8(opts.arg, &arg_hop_limit);
116 if (r < 0)
117 return log_error_errno(r, "Failed to parse hop limit: %m");
118 break;
119
120 OPTION_LONG("managed", "BOOL", "Managed flag"):
121 r = parse_boolean(opts.arg);
122 if (r < 0)
123 return log_error_errno(r, "Failed to parse managed flag: %m");
124 SET_FLAG(arg_ra_flags, ND_RA_FLAG_MANAGED, r);
125 break;
126
127 OPTION_LONG("other", "BOOL", "Other flag"):
128 r = parse_boolean(opts.arg);
129 if (r < 0)
130 return log_error_errno(r, "Failed to parse other flag: %m");
131 SET_FLAG(arg_ra_flags, ND_RA_FLAG_OTHER, r);
132 break;
133
134 OPTION_LONG("home-agent", "BOOL", "Home-agent flag"):
135 r = parse_boolean(opts.arg);
136 if (r < 0)
137 return log_error_errno(r, "Failed to parse home-agent flag: %m");
138 SET_FLAG(arg_ra_flags, ND_RA_FLAG_HOME_AGENT, r);
139 break;
140
141 OPTION_LONG("preference", "PREF", "Preference"):
142 r = parse_preference(opts.arg);
143 if (r < 0)
144 return log_error_errno(r, "Failed to parse preference: %m");
145 arg_preference = r;
146 break;
147
148 OPTION_LONG("lifetime", "SECS", "Lifetime"):
149 r = parse_sec(opts.arg, &arg_lifetime);
150 if (r < 0)
151 return log_error_errno(r, "Failed to parse lifetime: %m");
152 break;
153
154 OPTION_LONG("reachable-time", "SECS", "Reachable time"):
155 r = parse_sec(opts.arg, &arg_reachable);
156 if (r < 0)
157 return log_error_errno(r, "Failed to parse reachable time: %m");
158 break;
159
160 OPTION_LONG("retransmit-timer", "SECS", "Retransmit timer"):
161 r = parse_sec(opts.arg, &arg_retransmit);
162 if (r < 0)
163 return log_error_errno(r, "Failed to parse retransmit timer: %m");
164 break;
165
166 OPTION_GROUP("Neighbor Advertisement"): {}
167
168 OPTION_LONG("is-router", "BOOL", "Router flag"):
169 r = parse_boolean(opts.arg);
170 if (r < 0)
171 return log_error_errno(r, "Failed to parse is-router flag: %m");
172 SET_FLAG(arg_na_flags, ND_NA_FLAG_ROUTER, r);
173 break;
174
175 OPTION_LONG("is-solicited", "BOOL", "Solicited flag"):
176 r = parse_boolean(opts.arg);
177 if (r < 0)
178 return log_error_errno(r, "Failed to parse is-solicited flag: %m");
179 SET_FLAG(arg_na_flags, ND_NA_FLAG_SOLICITED, r);
180 break;
181
182 OPTION_LONG("is-override", "BOOL", "Override flag"):
183 r = parse_boolean(opts.arg);
184 if (r < 0)
185 return log_error_errno(r, "Failed to parse is-override flag: %m");
186 SET_FLAG(arg_na_flags, ND_NA_FLAG_OVERRIDE, r);
187 break;
188
189 OPTION_GROUP("Neighbor Solicit/Advertisement and Redirect"): {}
190
191 OPTION_LONG("target-address", "ADDRESS", "Target address"):
192 r = in_addr_from_string(AF_INET6, opts.arg, &arg_target_address);
193 if (r < 0)
194 return log_error_errno(r, "Failed to parse target address: %m");
195 break;
196
197 OPTION_GROUP("Redirect"): {}
198
199 OPTION_LONG("redirect-destination", "ADDRESS", "Redirect destination address"):
200 r = in_addr_from_string(AF_INET6, opts.arg, &arg_redirect_destination);
201 if (r < 0)
202 return log_error_errno(r, "Failed to parse destination address: %m");
203 break;
204
205 OPTION_GROUP("NDisc Options"): {}
206
207 OPTION_LONG("source-ll-address", "BOOL", "Include source link-layer address"):
208 r = parse_boolean(opts.arg);
209 if (r < 0)
210 return log_error_errno(r, "Failed to parse source LL address option: %m");
211 arg_set_source_mac = r;
212 break;
213
214 OPTION_LONG("target-ll-address", "ADDRESS", "Target link-layer address"):
215 r = parse_ether_addr(opts.arg, &arg_target_mac);
216 if (r < 0)
217 return log_error_errno(r, "Failed to parse target LL address option: %m");
218 arg_set_target_mac = true;
219 break;
220
221 OPTION_LONG("redirected-header", "BASE64", "Redirected header (base64)"): {
222 _cleanup_free_ void *p = NULL;
223 size_t len;
224
225 r = unbase64mem(opts.arg, &p, &len);
226 if (r < 0)
227 return log_error_errno(r, "Failed to parse redirected header: %m");
228
229 if (len < sizeof(struct ip6_hdr))
230 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid redirected header.");
231
232 arg_redirected_header = TAKE_PTR(p);
233 break;
234 }
235
236 OPTION_LONG("mtu", "MTU", "MTU"):
237 r = safe_atou32(opts.arg, &arg_mtu);
238 if (r < 0)
239 return log_error_errno(r, "Failed to parse MTU: %m");
240 arg_set_mtu = true;
241 break;
242 }
243
244 if (arg_ifindex <= 0)
245 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--interface/-i option is mandatory.");
246
247 if (arg_icmp6_type <= 0)
248 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--type/-t option is mandatory.");
249
250 if (in6_addr_is_null(&arg_dest.in6)) {
251 if (IN_SET(arg_icmp6_type, ND_ROUTER_ADVERT, ND_NEIGHBOR_ADVERT, ND_REDIRECT))
252 arg_dest.in6 = IN6_ADDR_ALL_NODES_MULTICAST;
253 else
254 arg_dest.in6 = IN6_ADDR_ALL_ROUTERS_MULTICAST;
255 }
256
257 if (arg_set_source_mac) {
258 struct hw_addr_data hw_addr;
259
260 r = rtnl_get_link_hw_addr(&rtnl, arg_ifindex, &hw_addr);
261 if (r < 0)
262 return log_error_errno(r, "Failed to get the source link-layer address: %m");
263
264 if (hw_addr.length != sizeof(struct ether_addr))
265 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
266 "Unsupported hardware address length %zu.",
267 hw_addr.length);
268
269 arg_source_mac = hw_addr.ether;
270 }
271
272 return 1;
273 }
274
275 static int send_icmp6(int fd, const struct icmp6_hdr *hdr) {
276 _cleanup_set_free_ Set *options = NULL;
277 int r;
278
279 assert(fd >= 0);
280 assert(hdr);
281
282 if (arg_set_source_mac) {
283 r = ndisc_option_add_link_layer_address(&options, 0, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, &arg_source_mac);
284 if (r < 0)
285 return r;
286 }
287
288 if (arg_set_target_mac) {
289 r = ndisc_option_add_link_layer_address(&options, 0, SD_NDISC_OPTION_TARGET_LL_ADDRESS, &arg_target_mac);
290 if (r < 0)
291 return r;
292 }
293
294 if (arg_redirected_header) {
295 r = ndisc_option_add_redirected_header(&options, 0, arg_redirected_header);
296 if (r < 0)
297 return r;
298 }
299
300 if (arg_set_mtu) {
301 r = ndisc_option_add_mtu(&options, 0, arg_mtu);
302 if (r < 0)
303 return r;
304 }
305
306 return ndisc_send(fd, &arg_dest.in6, hdr, options, now(CLOCK_BOOTTIME));
307 }
308
309 static int send_router_solicit(int fd) {
310 struct nd_router_solicit hdr = {
311 .nd_rs_type = ND_ROUTER_SOLICIT,
312 };
313
314 assert(fd >= 0);
315
316 return send_icmp6(fd, &hdr.nd_rs_hdr);
317 }
318
319 static int send_router_advertisement(int fd) {
320 struct nd_router_advert hdr = {
321 .nd_ra_type = ND_ROUTER_ADVERT,
322 .nd_ra_router_lifetime = usec_to_be16_sec(arg_lifetime),
323 .nd_ra_reachable = usec_to_be32_msec(arg_reachable),
324 .nd_ra_retransmit = usec_to_be32_msec(arg_retransmit),
325 };
326
327 assert(fd >= 0);
328
329 /* The nd_ra_curhoplimit and nd_ra_flags_reserved fields cannot specified with nd_ra_router_lifetime
330 * simultaneously in the structured initializer in the above. */
331 hdr.nd_ra_curhoplimit = arg_hop_limit;
332 hdr.nd_ra_flags_reserved = arg_ra_flags;
333
334 return send_icmp6(fd, &hdr.nd_ra_hdr);
335 }
336
337 static int send_neighbor_solicit(int fd) {
338 struct nd_neighbor_solicit hdr = {
339 .nd_ns_type = ND_NEIGHBOR_SOLICIT,
340 .nd_ns_target = arg_target_address.in6,
341 };
342
343 assert(fd >= 0);
344
345 return send_icmp6(fd, &hdr.nd_ns_hdr);
346 }
347
348 static int send_neighbor_advertisement(int fd) {
349 struct nd_neighbor_advert hdr = {
350 .nd_na_type = ND_NEIGHBOR_ADVERT,
351 .nd_na_flags_reserved = arg_na_flags,
352 .nd_na_target = arg_target_address.in6,
353 };
354
355 assert(fd >= 0);
356
357 return send_icmp6(fd, &hdr.nd_na_hdr);
358 }
359
360 static int send_redirect(int fd) {
361 struct nd_redirect hdr = {
362 .nd_rd_type = ND_REDIRECT,
363 .nd_rd_target = arg_target_address.in6,
364 .nd_rd_dst = arg_redirect_destination.in6,
365 };
366
367 assert(fd >= 0);
368
369 return send_icmp6(fd, &hdr.nd_rd_hdr);
370 }
371
372 static int run(int argc, char *argv[]) {
373 _cleanup_close_ int fd = -EBADF;
374 int r;
375
376 log_setup();
377
378 r = parse_argv(argc, argv);
379 if (r <= 0)
380 return r;
381
382 fd = icmp6_bind(arg_ifindex, /* is_router= */ false);
383 if (fd < 0)
384 return log_error_errno(fd, "Failed to bind socket to interface: %m");
385
386 switch (arg_icmp6_type) {
387 case ND_ROUTER_SOLICIT:
388 return send_router_solicit(fd);
389 case ND_ROUTER_ADVERT:
390 return send_router_advertisement(fd);
391 case ND_NEIGHBOR_SOLICIT:
392 return send_neighbor_solicit(fd);
393 case ND_NEIGHBOR_ADVERT:
394 return send_neighbor_advertisement(fd);
395 case ND_REDIRECT:
396 return send_redirect(fd);
397 default:
398 assert_not_reached();
399 }
400
401 return 0;
402 }
403
404 DEFINE_MAIN_FUNCTION(run);