]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
ee8c4568 | 2 | |
3d0c8750 | 3 | #include <arpa/inet.h> |
ee8c4568 | 4 | #include <getopt.h> |
d37b7627 | 5 | #include <linux/if_addrlabel.h> |
1693a943 | 6 | #include <net/if.h> |
3f6fd1ba | 7 | #include <stdbool.h> |
ca78ad1d ZJS |
8 | #include <sys/stat.h> |
9 | #include <sys/types.h> | |
10 | #include <unistd.h> | |
12ef8fb6 | 11 | #include <linux/if_bridge.h> |
dca35224 | 12 | #include <linux/if_tunnel.h> |
ee8c4568 | 13 | |
335dd8ba | 14 | #include "sd-bus.h" |
914d6c09 | 15 | #include "sd-device.h" |
d9ce1c24 | 16 | #include "sd-dhcp-client.h" |
3f6fd1ba | 17 | #include "sd-hwdb.h" |
3a2ee855 | 18 | #include "sd-lldp-rx.h" |
3f6fd1ba LP |
19 | #include "sd-netlink.h" |
20 | #include "sd-network.h" | |
ee8c4568 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
b55818fd | 23 | #include "bond-util.h" |
9b71e4ab | 24 | #include "bridge-util.h" |
d6b4d1c7 | 25 | #include "build.h" |
335dd8ba YW |
26 | #include "bus-common-errors.h" |
27 | #include "bus-error.h" | |
9b71e4ab | 28 | #include "bus-locator.h" |
914d6c09 | 29 | #include "device-util.h" |
8d07de25 | 30 | #include "escape.h" |
3f6fd1ba | 31 | #include "ether-addr-util.h" |
c967d2c7 | 32 | #include "ethtool-util.h" |
34437b4f | 33 | #include "fd-util.h" |
ff7c88a2 | 34 | #include "format-table.h" |
518a66ec | 35 | #include "format-util.h" |
8fff78a1 | 36 | #include "fs-util.h" |
e7b38d7d | 37 | #include "geneve-util.h" |
0ef84b80 | 38 | #include "glob-util.h" |
81fd1dd3 | 39 | #include "hwdb-util.h" |
851ef1ed | 40 | #include "ipvlan-util.h" |
ee8c4568 | 41 | #include "local-addresses.h" |
8752c575 | 42 | #include "locale-util.h" |
10c71c36 | 43 | #include "logs-show.h" |
d37b7627 | 44 | #include "macro.h" |
cf217a09 | 45 | #include "macvlan-util.h" |
4e2ca442 | 46 | #include "main-func.h" |
b5cc5591 | 47 | #include "netif-util.h" |
3f6fd1ba | 48 | #include "netlink-util.h" |
ef62949a | 49 | #include "network-internal.h" |
1929ed0e | 50 | #include "network-util.h" |
15fde154 MY |
51 | #include "networkctl.h" |
52 | #include "networkctl-config-file.h" | |
3f6fd1ba | 53 | #include "pager.h" |
bfc2b05e | 54 | #include "parse-argument.h" |
6bedfcbb | 55 | #include "parse-util.h" |
96bab8fd MY |
56 | #include "path-lookup.h" |
57 | #include "path-util.h" | |
294bf0c3 | 58 | #include "pretty-print.h" |
9cd8c766 | 59 | #include "set.h" |
955fc5d8 | 60 | #include "sigbus.h" |
d308bb99 | 61 | #include "socket-netlink.h" |
db73295a | 62 | #include "socket-util.h" |
760877e9 | 63 | #include "sort-util.h" |
34437b4f | 64 | #include "sparse-endian.h" |
d054f0a4 | 65 | #include "stdio-util.h" |
8b43440b | 66 | #include "string-table.h" |
8752c575 | 67 | #include "string-util.h" |
3f6fd1ba | 68 | #include "strv.h" |
2388b2f4 | 69 | #include "strxcpyx.h" |
288a74cc | 70 | #include "terminal-util.h" |
3cc7a9fd | 71 | #include "udev-util.h" |
10c71c36 | 72 | #include "unit-def.h" |
4591eccf | 73 | #include "varlink.h" |
3f6fd1ba | 74 | #include "verbs.h" |
8d07de25 | 75 | #include "wifi-util.h" |
ee8c4568 | 76 | |
c82d1bf2 SS |
77 | /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */ |
78 | #define NETDEV_KIND_MAX 64 | |
79 | ||
8d07de25 ZJS |
80 | /* use 128 kB for receive socket kernel queue, we shouldn't need more here */ |
81 | #define RCVBUF_SIZE (128*1024) | |
82 | ||
15fde154 MY |
83 | PagerFlags arg_pager_flags = 0; |
84 | bool arg_legend = true; | |
85 | bool arg_no_reload = false; | |
86 | bool arg_all = false; | |
87 | bool arg_stats = false; | |
88 | bool arg_full = false; | |
89 | bool arg_runtime = false; | |
90 | unsigned arg_lines = 10; | |
91 | char *arg_drop_in = NULL; | |
92 | JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; | |
bfc2b05e | 93 | |
96bab8fd MY |
94 | STATIC_DESTRUCTOR_REGISTER(arg_drop_in, freep); |
95 | ||
4591eccf | 96 | static int check_netns_match(void) { |
445f0d8b MY |
97 | struct stat st; |
98 | uint64_t id; | |
4591eccf ML |
99 | JsonVariant *reply = NULL; |
100 | _cleanup_(varlink_unrefp) Varlink *vl = NULL; | |
445f0d8b MY |
101 | int r; |
102 | ||
4591eccf ML |
103 | r = varlink_connect_address(&vl, "/run/systemd/netif/io.systemd.Network"); |
104 | if (r < 0) | |
105 | return log_error_errno(r, "Failed to connect to network service /run/systemd/netif/io.systemd.Network: %m"); | |
106 | ||
0444391d | 107 | r = varlink_call(vl, "io.systemd.Network.GetNamespaceId", /* parameters= */ NULL, &reply, /* ret_error_id= */ NULL); |
4591eccf ML |
108 | if (r < 0) |
109 | return log_error_errno(r, "Failed to issue GetNamespaceId() varlink call: %m"); | |
110 | ||
111 | static const JsonDispatch dispatch_table[] = { | |
112 | { "NamespaceId", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, 0, JSON_MANDATORY }, | |
113 | {}, | |
114 | }; | |
115 | ||
f0e4244b | 116 | r = json_dispatch(reply, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &id); |
4591eccf ML |
117 | if (r < 0) |
118 | return r; | |
43449ca3 | 119 | |
445f0d8b MY |
120 | if (id == 0) { |
121 | log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check."); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | if (stat("/proc/self/ns/net", &st) < 0) | |
126 | return log_error_errno(errno, "Failed to determine our own network namespace ID: %m"); | |
127 | ||
128 | if (id != st.st_ino) | |
129 | return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), | |
130 | "networkctl must be invoked in same network namespace as systemd-networkd.service."); | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
15fde154 | 135 | bool networkd_is_running(void) { |
96bab8fd MY |
136 | static int cached = -1; |
137 | int r; | |
138 | ||
139 | if (cached < 0) { | |
140 | r = access("/run/systemd/netif/state", F_OK); | |
141 | if (r < 0) { | |
142 | if (errno != ENOENT) | |
143 | log_debug_errno(errno, | |
144 | "Failed to determine whether networkd is running, assuming it's not: %m"); | |
145 | ||
146 | cached = false; | |
147 | } else | |
148 | cached = true; | |
149 | } | |
150 | ||
151 | return cached; | |
445f0d8b MY |
152 | } |
153 | ||
15fde154 | 154 | int acquire_bus(sd_bus **ret) { |
445f0d8b MY |
155 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
156 | int r; | |
157 | ||
158 | assert(ret); | |
159 | ||
160 | r = sd_bus_open_system(&bus); | |
161 | if (r < 0) | |
8dfc3bf5 | 162 | return log_error_errno(r, "Failed to connect to system bus: %m"); |
445f0d8b | 163 | |
6c693007 | 164 | if (networkd_is_running()) { |
4591eccf | 165 | r = check_netns_match(); |
6c693007 MY |
166 | if (r < 0) |
167 | return r; | |
168 | } else | |
169 | log_warning("systemd-networkd is not running, output might be incomplete."); | |
445f0d8b MY |
170 | |
171 | *ret = TAKE_PTR(bus); | |
172 | return 0; | |
173 | } | |
174 | ||
d821e40c | 175 | static int get_description(sd_bus *bus, JsonVariant **ret) { |
bfc2b05e YW |
176 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
177 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
61c84edf | 178 | const char *text; |
bfc2b05e YW |
179 | int r; |
180 | ||
43449ca3 MY |
181 | assert(bus); |
182 | assert(ret); | |
183 | ||
bfc2b05e YW |
184 | r = bus_call_method(bus, bus_network_mgr, "Describe", &error, &reply, NULL); |
185 | if (r < 0) | |
186 | return log_error_errno(r, "Failed to get description: %s", bus_error_message(&error, r)); | |
187 | ||
188 | r = sd_bus_message_read(reply, "s", &text); | |
189 | if (r < 0) | |
190 | return bus_log_parse_error(r); | |
191 | ||
192 | r = json_parse(text, 0, ret, NULL, NULL); | |
193 | if (r < 0) | |
194 | return log_error_errno(r, "Failed to parse JSON: %m"); | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
d821e40c | 199 | static int dump_manager_description(sd_bus *bus) { |
bfc2b05e YW |
200 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
201 | int r; | |
202 | ||
43449ca3 MY |
203 | assert(bus); |
204 | ||
d821e40c | 205 | r = get_description(bus, &v); |
bfc2b05e YW |
206 | if (r < 0) |
207 | return r; | |
208 | ||
209 | json_variant_dump(v, arg_json_format_flags, NULL, NULL); | |
210 | return 0; | |
211 | } | |
212 | ||
43449ca3 | 213 | static int dump_link_description(sd_bus *bus, char * const *patterns) { |
bfc2b05e YW |
214 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
215 | _cleanup_free_ bool *matched_patterns = NULL; | |
216 | JsonVariant *i; | |
217 | size_t c = 0; | |
218 | int r; | |
219 | ||
43449ca3 MY |
220 | assert(bus); |
221 | assert(patterns); | |
222 | ||
d821e40c | 223 | r = get_description(bus, &v); |
bfc2b05e YW |
224 | if (r < 0) |
225 | return r; | |
226 | ||
227 | matched_patterns = new0(bool, strv_length(patterns)); | |
228 | if (!matched_patterns) | |
229 | return log_oom(); | |
230 | ||
231 | JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(v, "Interfaces")) { | |
718ca772 | 232 | char ifindex_str[DECIMAL_STR_MAX(int64_t)]; |
bfc2b05e | 233 | const char *name; |
718ca772 | 234 | int64_t index; |
bfc2b05e YW |
235 | size_t pos; |
236 | ||
237 | name = json_variant_string(json_variant_by_key(i, "Name")); | |
238 | index = json_variant_integer(json_variant_by_key(i, "Index")); | |
c0f86d66 | 239 | xsprintf(ifindex_str, "%" PRIi64, index); |
bfc2b05e YW |
240 | |
241 | if (!strv_fnmatch_full(patterns, ifindex_str, 0, &pos) && | |
242 | !strv_fnmatch_full(patterns, name, 0, &pos)) { | |
243 | bool match = false; | |
244 | JsonVariant *a; | |
245 | ||
246 | JSON_VARIANT_ARRAY_FOREACH(a, json_variant_by_key(i, "AlternativeNames")) | |
247 | if (strv_fnmatch_full(patterns, json_variant_string(a), 0, &pos)) { | |
248 | match = true; | |
249 | break; | |
250 | } | |
251 | ||
252 | if (!match) | |
253 | continue; | |
254 | } | |
255 | ||
256 | matched_patterns[pos] = true; | |
257 | json_variant_dump(i, arg_json_format_flags, NULL, NULL); | |
258 | c++; | |
259 | } | |
260 | ||
261 | /* Look if we matched all our arguments that are not globs. It is OK for a glob to match | |
262 | * nothing, but not for an exact argument. */ | |
263 | for (size_t pos = 0; pos < strv_length(patterns); pos++) { | |
264 | if (matched_patterns[pos]) | |
265 | continue; | |
266 | ||
267 | if (string_is_glob(patterns[pos])) | |
268 | log_debug("Pattern \"%s\" doesn't match any interface, ignoring.", | |
269 | patterns[pos]); | |
270 | else | |
271 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), | |
272 | "Interface \"%s\" not found.", patterns[pos]); | |
273 | } | |
274 | ||
275 | if (c == 0) | |
276 | log_warning("No interfaces matched."); | |
277 | ||
278 | return 0; | |
279 | } | |
ee8c4568 | 280 | |
43449ca3 MY |
281 | static void operational_state_to_color( |
282 | const char *name, | |
283 | const char *state, | |
284 | const char **on, | |
285 | const char **off) { | |
286 | ||
ceb366df ZJS |
287 | if (STRPTR_IN_SET(state, "routable", "enslaved") || |
288 | (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) { | |
37da8bca YW |
289 | if (on) |
290 | *on = ansi_highlight_green(); | |
291 | if (off) | |
292 | *off = ansi_normal(); | |
7e5a080a | 293 | } else if (streq_ptr(state, "degraded")) { |
37da8bca YW |
294 | if (on) |
295 | *on = ansi_highlight_yellow(); | |
296 | if (off) | |
297 | *off = ansi_normal(); | |
298 | } else { | |
299 | if (on) | |
300 | *on = ""; | |
301 | if (off) | |
302 | *off = ""; | |
303 | } | |
7e5a080a LP |
304 | } |
305 | ||
306 | static void setup_state_to_color(const char *state, const char **on, const char **off) { | |
7e5a080a | 307 | if (streq_ptr(state, "configured")) { |
37da8bca YW |
308 | if (on) |
309 | *on = ansi_highlight_green(); | |
310 | if (off) | |
311 | *off = ansi_normal(); | |
7e5a080a | 312 | } else if (streq_ptr(state, "configuring")) { |
37da8bca YW |
313 | if (on) |
314 | *on = ansi_highlight_yellow(); | |
315 | if (off) | |
316 | *off = ansi_normal(); | |
1cf03a4f | 317 | } else if (STRPTR_IN_SET(state, "failed", "linger")) { |
37da8bca YW |
318 | if (on) |
319 | *on = ansi_highlight_red(); | |
320 | if (off) | |
321 | *off = ansi_normal(); | |
322 | } else { | |
323 | if (on) | |
324 | *on = ""; | |
325 | if (off) | |
326 | *off = ""; | |
327 | } | |
7e5a080a LP |
328 | } |
329 | ||
4a481ec4 AŠ |
330 | static void online_state_to_color(const char *state, const char **on, const char **off) { |
331 | if (streq_ptr(state, "online")) { | |
332 | if (on) | |
333 | *on = ansi_highlight_green(); | |
334 | if (off) | |
335 | *off = ansi_normal(); | |
336 | } else if (streq_ptr(state, "partial")) { | |
337 | if (on) | |
338 | *on = ansi_highlight_yellow(); | |
339 | if (off) | |
340 | *off = ansi_normal(); | |
341 | } else { | |
342 | if (on) | |
343 | *on = ""; | |
344 | if (off) | |
345 | *off = ""; | |
346 | } | |
347 | } | |
348 | ||
b24281aa SS |
349 | typedef struct VxLanInfo { |
350 | uint32_t vni; | |
351 | uint32_t link; | |
352 | ||
353 | int local_family; | |
354 | int group_family; | |
355 | ||
356 | union in_addr_union local; | |
357 | union in_addr_union group; | |
358 | ||
359 | uint16_t dest_port; | |
360 | ||
22ae6c7d SS |
361 | uint8_t proxy; |
362 | uint8_t learning; | |
22ae6c7d SS |
363 | uint8_t rsc; |
364 | uint8_t l2miss; | |
365 | uint8_t l3miss; | |
366 | uint8_t tos; | |
367 | uint8_t ttl; | |
b24281aa SS |
368 | } VxLanInfo; |
369 | ||
6d0c65ff | 370 | typedef struct LinkInfo { |
e997c4b0 | 371 | char name[IFNAMSIZ+1]; |
430f07f4 | 372 | char *netdev_kind; |
172353b1 | 373 | sd_device *sd_device; |
6d0c65ff | 374 | int ifindex; |
1c4a6088 | 375 | unsigned short iftype; |
ca2b7cd8 | 376 | struct hw_addr_data hw_address; |
ed9fa69f | 377 | struct hw_addr_data permanent_hw_address; |
6cfef1b3 | 378 | uint32_t master; |
b147503e | 379 | uint32_t mtu; |
2c73f59b SS |
380 | uint32_t min_mtu; |
381 | uint32_t max_mtu; | |
0307afc6 SS |
382 | uint32_t tx_queues; |
383 | uint32_t rx_queues; | |
d69b62de | 384 | uint8_t addr_gen_mode; |
e810df37 | 385 | char *qdisc; |
511070ee | 386 | char **alternative_names; |
b147503e | 387 | |
a459b24f YW |
388 | union { |
389 | struct rtnl_link_stats64 stats64; | |
390 | struct rtnl_link_stats stats; | |
391 | }; | |
392 | ||
42a63431 YW |
393 | uint64_t tx_bitrate; |
394 | uint64_t rx_bitrate; | |
335dd8ba | 395 | |
c82d1bf2 SS |
396 | /* bridge info */ |
397 | uint32_t forward_delay; | |
398 | uint32_t hello_time; | |
399 | uint32_t max_age; | |
400 | uint32_t ageing_time; | |
401 | uint32_t stp_state; | |
12ef8fb6 | 402 | uint32_t cost; |
c82d1bf2 SS |
403 | uint16_t priority; |
404 | uint8_t mcast_igmp_version; | |
12ef8fb6 | 405 | uint8_t port_state; |
c82d1bf2 | 406 | |
b24281aa SS |
407 | /* vxlan info */ |
408 | VxLanInfo vxlan_info; | |
409 | ||
2b2a1ae6 SS |
410 | /* vlan info */ |
411 | uint16_t vlan_id; | |
412 | ||
dca35224 | 413 | /* tunnel info */ |
4e1a1991 SS |
414 | uint8_t ttl; |
415 | uint8_t tos; | |
e7b38d7d SS |
416 | uint8_t inherit; |
417 | uint8_t df; | |
418 | uint8_t csum; | |
419 | uint8_t csum6_tx; | |
420 | uint8_t csum6_rx; | |
4e1a1991 SS |
421 | uint16_t tunnel_port; |
422 | uint32_t vni; | |
e7b38d7d | 423 | uint32_t label; |
dca35224 SS |
424 | union in_addr_union local; |
425 | union in_addr_union remote; | |
426 | ||
b1d6fe70 SS |
427 | /* bonding info */ |
428 | uint8_t mode; | |
429 | uint32_t miimon; | |
430 | uint32_t updelay; | |
431 | uint32_t downdelay; | |
432 | ||
cf217a09 SS |
433 | /* macvlan and macvtap info */ |
434 | uint32_t macvlan_mode; | |
435 | ||
851ef1ed SS |
436 | /* ipvlan info */ |
437 | uint16_t ipvlan_mode; | |
438 | uint16_t ipvlan_flags; | |
439 | ||
c967d2c7 YW |
440 | /* ethtool info */ |
441 | int autonegotiation; | |
50299121 | 442 | uint64_t speed; |
c967d2c7 YW |
443 | Duplex duplex; |
444 | NetDevPort port; | |
445 | ||
8d07de25 | 446 | /* wlan info */ |
78404d22 | 447 | enum nl80211_iftype wlan_iftype; |
8d07de25 ZJS |
448 | char *ssid; |
449 | struct ether_addr bssid; | |
450 | ||
57ae8cd8 | 451 | bool has_hw_address:1; |
ed9fa69f | 452 | bool has_permanent_hw_address:1; |
0307afc6 SS |
453 | bool has_tx_queues:1; |
454 | bool has_rx_queues:1; | |
a459b24f YW |
455 | bool has_stats64:1; |
456 | bool has_stats:1; | |
335dd8ba | 457 | bool has_bitrates:1; |
c967d2c7 | 458 | bool has_ethtool_link_info:1; |
8d07de25 | 459 | bool has_wlan_link_info:1; |
4e1a1991 | 460 | bool has_tunnel_ipv4:1; |
d69b62de | 461 | bool has_ipv6_address_generation_mode:1; |
172353b1 ZJS |
462 | |
463 | bool needs_freeing:1; | |
6d0c65ff LP |
464 | } LinkInfo; |
465 | ||
93bab288 YW |
466 | static int link_info_compare(const LinkInfo *a, const LinkInfo *b) { |
467 | return CMP(a->ifindex, b->ifindex); | |
6d0c65ff LP |
468 | } |
469 | ||
75db809a | 470 | static LinkInfo* link_info_array_free(LinkInfo *array) { |
172353b1 ZJS |
471 | for (unsigned i = 0; array && array[i].needs_freeing; i++) { |
472 | sd_device_unref(array[i].sd_device); | |
430f07f4 | 473 | free(array[i].netdev_kind); |
8d07de25 | 474 | free(array[i].ssid); |
e810df37 | 475 | free(array[i].qdisc); |
511070ee | 476 | strv_free(array[i].alternative_names); |
172353b1 ZJS |
477 | } |
478 | ||
479 | return mfree(array); | |
480 | } | |
481 | DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo*, link_info_array_free); | |
482 | ||
c82d1bf2 | 483 | static int decode_netdev(sd_netlink_message *m, LinkInfo *info) { |
c82d1bf2 SS |
484 | int r; |
485 | ||
486 | assert(m); | |
487 | assert(info); | |
488 | ||
489 | r = sd_netlink_message_enter_container(m, IFLA_LINKINFO); | |
490 | if (r < 0) | |
491 | return r; | |
492 | ||
430f07f4 YW |
493 | r = sd_netlink_message_read_string_strdup(m, IFLA_INFO_KIND, &info->netdev_kind); |
494 | if (r < 0) { | |
495 | (void) sd_netlink_message_exit_container(m); | |
c82d1bf2 | 496 | return r; |
430f07f4 | 497 | } |
c82d1bf2 SS |
498 | |
499 | r = sd_netlink_message_enter_container(m, IFLA_INFO_DATA); | |
500 | if (r < 0) | |
501 | return r; | |
502 | ||
430f07f4 | 503 | if (streq(info->netdev_kind, "bridge")) { |
c82d1bf2 SS |
504 | (void) sd_netlink_message_read_u32(m, IFLA_BR_FORWARD_DELAY, &info->forward_delay); |
505 | (void) sd_netlink_message_read_u32(m, IFLA_BR_HELLO_TIME, &info->hello_time); | |
506 | (void) sd_netlink_message_read_u32(m, IFLA_BR_MAX_AGE, &info->max_age); | |
507 | (void) sd_netlink_message_read_u32(m, IFLA_BR_AGEING_TIME, &info->ageing_time); | |
508 | (void) sd_netlink_message_read_u32(m, IFLA_BR_STP_STATE, &info->stp_state); | |
12ef8fb6 | 509 | (void) sd_netlink_message_read_u32(m, IFLA_BRPORT_COST, &info->cost); |
c82d1bf2 SS |
510 | (void) sd_netlink_message_read_u16(m, IFLA_BR_PRIORITY, &info->priority); |
511 | (void) sd_netlink_message_read_u8(m, IFLA_BR_MCAST_IGMP_VERSION, &info->mcast_igmp_version); | |
12ef8fb6 | 512 | (void) sd_netlink_message_read_u8(m, IFLA_BRPORT_STATE, &info->port_state); |
430f07f4 | 513 | } if (streq(info->netdev_kind, "bond")) { |
b1d6fe70 SS |
514 | (void) sd_netlink_message_read_u8(m, IFLA_BOND_MODE, &info->mode); |
515 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_MIIMON, &info->miimon); | |
516 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_DOWNDELAY, &info->downdelay); | |
517 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_UPDELAY, &info->updelay); | |
430f07f4 | 518 | } else if (streq(info->netdev_kind, "vxlan")) { |
b24281aa SS |
519 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_ID, &info->vxlan_info.vni); |
520 | ||
521 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_GROUP, &info->vxlan_info.group.in); | |
522 | if (r >= 0) | |
523 | info->vxlan_info.group_family = AF_INET; | |
524 | else { | |
525 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_GROUP6, &info->vxlan_info.group.in6); | |
526 | if (r >= 0) | |
527 | info->vxlan_info.group_family = AF_INET6; | |
528 | } | |
529 | ||
530 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_LOCAL, &info->vxlan_info.local.in); | |
531 | if (r >= 0) | |
532 | info->vxlan_info.local_family = AF_INET; | |
533 | else { | |
534 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_LOCAL6, &info->vxlan_info.local.in6); | |
535 | if (r >= 0) | |
536 | info->vxlan_info.local_family = AF_INET6; | |
537 | } | |
538 | ||
539 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_LINK, &info->vxlan_info.link); | |
540 | (void) sd_netlink_message_read_u16(m, IFLA_VXLAN_PORT, &info->vxlan_info.dest_port); | |
22ae6c7d SS |
541 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_PROXY, &info->vxlan_info.proxy); |
542 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_LEARNING, &info->vxlan_info.learning); | |
543 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_RSC, &info->vxlan_info.rsc); | |
544 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L3MISS, &info->vxlan_info.l3miss); | |
545 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L2MISS, &info->vxlan_info.l2miss); | |
546 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TOS, &info->vxlan_info.tos); | |
547 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TTL, &info->vxlan_info.ttl); | |
430f07f4 | 548 | } else if (streq(info->netdev_kind, "vlan")) |
2b2a1ae6 | 549 | (void) sd_netlink_message_read_u16(m, IFLA_VLAN_ID, &info->vlan_id); |
430f07f4 | 550 | else if (STR_IN_SET(info->netdev_kind, "ipip", "sit")) { |
dca35224 SS |
551 | (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_LOCAL, &info->local.in); |
552 | (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_REMOTE, &info->remote.in); | |
430f07f4 | 553 | } else if (streq(info->netdev_kind, "geneve")) { |
4e1a1991 SS |
554 | (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_ID, &info->vni); |
555 | ||
556 | r = sd_netlink_message_read_in_addr(m, IFLA_GENEVE_REMOTE, &info->remote.in); | |
557 | if (r >= 0) | |
558 | info->has_tunnel_ipv4 = true; | |
559 | else | |
560 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GENEVE_REMOTE6, &info->remote.in6); | |
561 | ||
562 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL, &info->ttl); | |
e7b38d7d | 563 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL_INHERIT, &info->inherit); |
4e1a1991 | 564 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TOS, &info->tos); |
e7b38d7d SS |
565 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_DF, &info->df); |
566 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_CSUM, &info->csum); | |
567 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, &info->csum6_tx); | |
568 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, &info->csum6_rx); | |
4e1a1991 | 569 | (void) sd_netlink_message_read_u16(m, IFLA_GENEVE_PORT, &info->tunnel_port); |
e7b38d7d | 570 | (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_LABEL, &info->label); |
430f07f4 | 571 | } else if (STR_IN_SET(info->netdev_kind, "gre", "gretap", "erspan")) { |
4d75ea1e SS |
572 | (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_LOCAL, &info->local.in); |
573 | (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_REMOTE, &info->remote.in); | |
430f07f4 | 574 | } else if (STR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan")) { |
ad760bc1 SS |
575 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_LOCAL, &info->local.in6); |
576 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_REMOTE, &info->remote.in6); | |
430f07f4 | 577 | } else if (streq(info->netdev_kind, "vti")) { |
5712d689 SS |
578 | (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_LOCAL, &info->local.in); |
579 | (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_REMOTE, &info->remote.in); | |
430f07f4 | 580 | } else if (streq(info->netdev_kind, "vti6")) { |
5712d689 SS |
581 | (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_LOCAL, &info->local.in6); |
582 | (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_REMOTE, &info->remote.in6); | |
430f07f4 | 583 | } else if (STR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) |
cf217a09 | 584 | (void) sd_netlink_message_read_u32(m, IFLA_MACVLAN_MODE, &info->macvlan_mode); |
430f07f4 | 585 | else if (streq(info->netdev_kind, "ipvlan")) { |
851ef1ed SS |
586 | (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_MODE, &info->ipvlan_mode); |
587 | (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_FLAGS, &info->ipvlan_flags); | |
588 | } | |
c82d1bf2 | 589 | |
c82d1bf2 SS |
590 | (void) sd_netlink_message_exit_container(m); |
591 | (void) sd_netlink_message_exit_container(m); | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
43449ca3 MY |
596 | static int decode_link( |
597 | sd_netlink_message *m, | |
598 | LinkInfo *info, | |
599 | char * const *patterns, | |
600 | bool matched_patterns[]) { | |
601 | ||
f7581ed6 | 602 | _cleanup_strv_free_ char **altnames = NULL; |
e810df37 | 603 | const char *name, *qdisc; |
a6962904 | 604 | int ifindex, r; |
c82d1bf2 | 605 | uint16_t type; |
6d0c65ff | 606 | |
e997c4b0 LP |
607 | assert(m); |
608 | assert(info); | |
6d0c65ff | 609 | |
e997c4b0 LP |
610 | r = sd_netlink_message_get_type(m, &type); |
611 | if (r < 0) | |
612 | return r; | |
6d0c65ff | 613 | |
e997c4b0 LP |
614 | if (type != RTM_NEWLINK) |
615 | return 0; | |
6d0c65ff | 616 | |
a6962904 | 617 | r = sd_rtnl_message_link_get_ifindex(m, &ifindex); |
e997c4b0 LP |
618 | if (r < 0) |
619 | return r; | |
6d0c65ff | 620 | |
e997c4b0 LP |
621 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name); |
622 | if (r < 0) | |
623 | return r; | |
6d0c65ff | 624 | |
f7581ed6 | 625 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames); |
48c53989 | 626 | if (r < 0 && r != -ENODATA) |
f7581ed6 YW |
627 | return r; |
628 | ||
a6962904 YW |
629 | if (patterns) { |
630 | char str[DECIMAL_STR_MAX(int)]; | |
0ef84b80 ZJS |
631 | size_t pos; |
632 | ||
633 | assert(matched_patterns); | |
a6962904 YW |
634 | |
635 | xsprintf(str, "%i", ifindex); | |
0ef84b80 ZJS |
636 | if (!strv_fnmatch_full(patterns, str, 0, &pos) && |
637 | !strv_fnmatch_full(patterns, name, 0, &pos)) { | |
f7581ed6 | 638 | bool match = false; |
f7581ed6 YW |
639 | |
640 | STRV_FOREACH(p, altnames) | |
0ef84b80 | 641 | if (strv_fnmatch_full(patterns, *p, 0, &pos)) { |
f7581ed6 YW |
642 | match = true; |
643 | break; | |
644 | } | |
645 | if (!match) | |
646 | return 0; | |
647 | } | |
0ef84b80 ZJS |
648 | |
649 | matched_patterns[pos] = true; | |
a6962904 YW |
650 | } |
651 | ||
b147503e | 652 | r = sd_rtnl_message_link_get_type(m, &info->iftype); |
e997c4b0 LP |
653 | if (r < 0) |
654 | return r; | |
6d0c65ff | 655 | |
2388b2f4 | 656 | strscpy(info->name, sizeof info->name, name); |
a6962904 | 657 | info->ifindex = ifindex; |
f7581ed6 | 658 | info->alternative_names = TAKE_PTR(altnames); |
b147503e | 659 | |
57ae8cd8 | 660 | info->has_hw_address = |
f1f17144 | 661 | netlink_message_read_hw_addr(m, IFLA_ADDRESS, &info->hw_address) >= 0 && |
de0f1579 | 662 | info->hw_address.length > 0; |
b147503e | 663 | |
ed9fa69f | 664 | info->has_permanent_hw_address = |
49c351a8 YW |
665 | (netlink_message_read_hw_addr(m, IFLA_PERM_ADDRESS, &info->permanent_hw_address) >= 0 || |
666 | ethtool_get_permanent_hw_addr(NULL, info->name, &info->permanent_hw_address) >= 0) && | |
ed9fa69f YW |
667 | !hw_addr_is_null(&info->permanent_hw_address) && |
668 | !hw_addr_equal(&info->permanent_hw_address, &info->hw_address); | |
caa8538a | 669 | |
c06ff86e YW |
670 | (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu); |
671 | (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu); | |
672 | (void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu); | |
2c73f59b | 673 | |
0307afc6 SS |
674 | info->has_rx_queues = |
675 | sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 && | |
676 | info->rx_queues > 0; | |
677 | ||
678 | info->has_tx_queues = | |
679 | sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 && | |
680 | info->tx_queues > 0; | |
681 | ||
a459b24f YW |
682 | if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0) |
683 | info->has_stats64 = true; | |
684 | else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0) | |
685 | info->has_stats = true; | |
686 | ||
e810df37 SS |
687 | r = sd_netlink_message_read_string(m, IFLA_QDISC, &qdisc); |
688 | if (r >= 0) { | |
689 | info->qdisc = strdup(qdisc); | |
690 | if (!info->qdisc) | |
691 | return log_oom(); | |
692 | } | |
693 | ||
6cfef1b3 SS |
694 | (void) sd_netlink_message_read_u32(m, IFLA_MASTER, &info->master); |
695 | ||
d69b62de SS |
696 | r = sd_netlink_message_enter_container(m, IFLA_AF_SPEC); |
697 | if (r >= 0) { | |
698 | r = sd_netlink_message_enter_container(m, AF_INET6); | |
699 | if (r >= 0) { | |
700 | r = sd_netlink_message_read_u8(m, IFLA_INET6_ADDR_GEN_MODE, &info->addr_gen_mode); | |
7f20a9e5 SS |
701 | if (r >= 0 && IN_SET(info->addr_gen_mode, |
702 | IN6_ADDR_GEN_MODE_EUI64, | |
703 | IN6_ADDR_GEN_MODE_NONE, | |
704 | IN6_ADDR_GEN_MODE_STABLE_PRIVACY, | |
705 | IN6_ADDR_GEN_MODE_RANDOM)) | |
d69b62de SS |
706 | info->has_ipv6_address_generation_mode = true; |
707 | ||
708 | (void) sd_netlink_message_exit_container(m); | |
709 | } | |
710 | (void) sd_netlink_message_exit_container(m); | |
711 | } | |
712 | ||
c82d1bf2 SS |
713 | /* fill kind info */ |
714 | (void) decode_netdev(m, info); | |
715 | ||
e997c4b0 LP |
716 | return 1; |
717 | } | |
718 | ||
090c923b MAL |
719 | static int link_get_property( |
720 | sd_bus *bus, | |
721 | const LinkInfo *link, | |
722 | sd_bus_error *error, | |
723 | sd_bus_message **reply, | |
724 | const char *iface, | |
8bdece74 FS |
725 | const char *propname, |
726 | const char *type) { | |
100433e0 | 727 | |
100433e0 | 728 | _cleanup_free_ char *path = NULL; |
61c84edf | 729 | char ifindex_str[DECIMAL_STR_MAX(int)]; |
335dd8ba YW |
730 | int r; |
731 | ||
43449ca3 MY |
732 | assert(bus); |
733 | assert(link); | |
734 | assert(link->ifindex >= 0); | |
735 | assert(error); | |
736 | assert(reply); | |
737 | assert(iface); | |
738 | assert(propname); | |
8bdece74 | 739 | assert(type); |
43449ca3 | 740 | |
100433e0 | 741 | xsprintf(ifindex_str, "%i", link->ifindex); |
335dd8ba YW |
742 | |
743 | r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path); | |
744 | if (r < 0) | |
745 | return r; | |
746 | ||
8bdece74 | 747 | return sd_bus_get_property(bus, "org.freedesktop.network1", path, iface, propname, error, reply, type); |
090c923b MAL |
748 | } |
749 | ||
750 | static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) { | |
751 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
752 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
753 | int r; | |
754 | ||
43449ca3 MY |
755 | assert(bus); |
756 | assert(link); | |
757 | ||
8bdece74 | 758 | r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.Link", "BitRates", "(tt)"); |
335dd8ba | 759 | if (r < 0) { |
955a6329 ZJS |
760 | bool quiet = sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY, |
761 | BUS_ERROR_SPEED_METER_INACTIVE); | |
8210a61a ZJS |
762 | |
763 | return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, | |
764 | r, "Failed to query link bit rates: %s", bus_error_message(&error, r)); | |
335dd8ba YW |
765 | } |
766 | ||
42a63431 | 767 | r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate); |
335dd8ba YW |
768 | if (r < 0) |
769 | return bus_log_parse_error(r); | |
770 | ||
771 | r = sd_bus_message_exit_container(reply); | |
772 | if (r < 0) | |
773 | return bus_log_parse_error(r); | |
774 | ||
e813de54 | 775 | link->has_bitrates = link->tx_bitrate != UINT64_MAX && link->rx_bitrate != UINT64_MAX; |
335dd8ba YW |
776 | |
777 | return 0; | |
778 | } | |
779 | ||
516e9c80 | 780 | static void acquire_ether_link_info(int *fd, LinkInfo *link) { |
43449ca3 MY |
781 | assert(fd); |
782 | assert(link); | |
783 | ||
784 | if (ethtool_get_link_info(fd, | |
785 | link->name, | |
516e9c80 ZJS |
786 | &link->autonegotiation, |
787 | &link->speed, | |
788 | &link->duplex, | |
789 | &link->port) >= 0) | |
790 | link->has_ethtool_link_info = true; | |
791 | } | |
792 | ||
8d07de25 ZJS |
793 | static void acquire_wlan_link_info(LinkInfo *link) { |
794 | _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL; | |
78404d22 | 795 | int r, k = 0; |
8d07de25 | 796 | |
43449ca3 MY |
797 | assert(link); |
798 | ||
fb53ee0a YW |
799 | if (!link->sd_device) |
800 | return; | |
801 | ||
802 | if (!device_is_devtype(link->sd_device, "wlan")) | |
8d07de25 ZJS |
803 | return; |
804 | ||
805 | r = sd_genl_socket_open(&genl); | |
806 | if (r < 0) { | |
807 | log_debug_errno(r, "Failed to open generic netlink socket: %m"); | |
808 | return; | |
809 | } | |
810 | ||
78cca583 | 811 | (void) sd_netlink_increase_rxbuf(genl, RCVBUF_SIZE); |
8d07de25 | 812 | |
78404d22 | 813 | r = wifi_get_interface(genl, link->ifindex, &link->wlan_iftype, &link->ssid); |
8d07de25 ZJS |
814 | if (r < 0) |
815 | log_debug_errno(r, "%s: failed to query ssid: %m", link->name); | |
816 | ||
33ebda2e | 817 | if (link->wlan_iftype == NL80211_IFTYPE_STATION) { |
78404d22 YW |
818 | k = wifi_get_station(genl, link->ifindex, &link->bssid); |
819 | if (k < 0) | |
820 | log_debug_errno(k, "%s: failed to query bssid: %m", link->name); | |
821 | } | |
8d07de25 ZJS |
822 | |
823 | link->has_wlan_link_info = r > 0 || k > 0; | |
824 | } | |
825 | ||
43449ca3 | 826 | static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char * const *patterns, LinkInfo **ret) { |
4afd3348 | 827 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
172353b1 | 828 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
61c84edf | 829 | _cleanup_free_ bool *matched_patterns = NULL; |
254d1313 | 830 | _cleanup_close_ int fd = -EBADF; |
319a4f4b | 831 | size_t c = 0; |
7d367b45 LP |
832 | int r; |
833 | ||
b147503e | 834 | assert(rtnl); |
7d367b45 | 835 | assert(ret); |
ee8c4568 | 836 | |
ee8c4568 LP |
837 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); |
838 | if (r < 0) | |
839 | return rtnl_log_create_error(r); | |
840 | ||
24c0f385 | 841 | r = sd_netlink_message_set_request_dump(req, true); |
ee8c4568 LP |
842 | if (r < 0) |
843 | return rtnl_log_create_error(r); | |
844 | ||
1c4baffc | 845 | r = sd_netlink_call(rtnl, req, 0, &reply); |
f647962d MS |
846 | if (r < 0) |
847 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
ee8c4568 | 848 | |
0ef84b80 ZJS |
849 | if (patterns) { |
850 | matched_patterns = new0(bool, strv_length(patterns)); | |
851 | if (!matched_patterns) | |
852 | return log_oom(); | |
853 | } | |
854 | ||
cda7fc8d | 855 | for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
319a4f4b | 856 | if (!GREEDY_REALLOC0(links, c + 2)) /* We keep one trailing one as marker */ |
7e5a080a | 857 | return -ENOMEM; |
7d367b45 | 858 | |
0ef84b80 | 859 | r = decode_link(i, links + c, patterns, matched_patterns); |
7e5a080a LP |
860 | if (r < 0) |
861 | return r; | |
c967d2c7 YW |
862 | if (r == 0) |
863 | continue; | |
864 | ||
172353b1 ZJS |
865 | links[c].needs_freeing = true; |
866 | ||
0ac655a6 | 867 | (void) sd_device_new_from_ifindex(&links[c].sd_device, links[c].ifindex); |
172353b1 | 868 | |
516e9c80 | 869 | acquire_ether_link_info(&fd, &links[c]); |
8d07de25 | 870 | acquire_wlan_link_info(&links[c]); |
c967d2c7 YW |
871 | |
872 | c++; | |
7e5a080a LP |
873 | } |
874 | ||
0ef84b80 ZJS |
875 | /* Look if we matched all our arguments that are not globs. It |
876 | * is OK for a glob to match nothing, but not for an exact argument. */ | |
877 | for (size_t pos = 0; pos < strv_length(patterns); pos++) { | |
878 | if (matched_patterns[pos]) | |
879 | continue; | |
880 | ||
881 | if (string_is_glob(patterns[pos])) | |
882 | log_debug("Pattern \"%s\" doesn't match any interface, ignoring.", | |
883 | patterns[pos]); | |
884 | else | |
885 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), | |
886 | "Interface \"%s\" not found.", patterns[pos]); | |
887 | } | |
888 | ||
93bab288 | 889 | typesafe_qsort(links, c, link_info_compare); |
7e5a080a | 890 | |
335dd8ba | 891 | if (bus) |
7db07767 MY |
892 | FOREACH_ARRAY(link, links, c) |
893 | (void) acquire_link_bitrates(bus, link); | |
335dd8ba | 894 | |
1cc6c93a | 895 | *ret = TAKE_PTR(links); |
7e5a080a | 896 | |
0ef84b80 ZJS |
897 | if (patterns && c == 0) |
898 | log_warning("No interfaces matched."); | |
899 | ||
7e5a080a | 900 | return (int) c; |
7d367b45 | 901 | } |
ee8c4568 | 902 | |
7d367b45 | 903 | static int list_links(int argc, char *argv[], void *userdata) { |
445f0d8b | 904 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
b147503e | 905 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
172353b1 | 906 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
ff7c88a2 YW |
907 | _cleanup_(table_unrefp) Table *table = NULL; |
908 | TableCell *cell; | |
b48e277f | 909 | int c, r; |
b147503e | 910 | |
445f0d8b MY |
911 | r = acquire_bus(&bus); |
912 | if (r < 0) | |
913 | return r; | |
914 | ||
bfc2b05e YW |
915 | if (arg_json_format_flags != JSON_FORMAT_OFF) { |
916 | if (arg_all || argc <= 1) | |
d821e40c | 917 | return dump_manager_description(bus); |
bfc2b05e | 918 | else |
d821e40c | 919 | return dump_link_description(bus, strv_skip(argv, 1)); |
bfc2b05e YW |
920 | } |
921 | ||
b147503e LP |
922 | r = sd_netlink_open(&rtnl); |
923 | if (r < 0) | |
924 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
7d367b45 | 925 | |
335dd8ba | 926 | c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
6d0c65ff | 927 | if (c < 0) |
7d367b45 LP |
928 | return c; |
929 | ||
384c2c32 | 930 | pager_open(arg_pager_flags); |
7d367b45 | 931 | |
4252171a | 932 | table = table_new("idx", "link", "type", "operational", "setup"); |
ff7c88a2 YW |
933 | if (!table) |
934 | return log_oom(); | |
935 | ||
a42d9490 YW |
936 | if (arg_full) |
937 | table_set_width(table, 0); | |
938 | ||
ff7c88a2 | 939 | table_set_header(table, arg_legend); |
c8b62cf6 | 940 | table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
ff7c88a2 YW |
941 | |
942 | assert_se(cell = table_get_cell(table, 0, 0)); | |
943 | (void) table_set_minimum_width(table, cell, 3); | |
944 | (void) table_set_weight(table, cell, 0); | |
81914d9f | 945 | (void) table_set_ellipsize_percent(table, cell, 100); |
ff7c88a2 YW |
946 | (void) table_set_align_percent(table, cell, 100); |
947 | ||
948 | assert_se(cell = table_get_cell(table, 0, 1)); | |
81914d9f | 949 | (void) table_set_ellipsize_percent(table, cell, 100); |
6d0c65ff | 950 | |
7db07767 | 951 | FOREACH_ARRAY(link, links, c) { |
ab1525bc | 952 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL; |
ee8c4568 | 953 | _cleanup_free_ char *t = NULL; |
7db07767 | 954 | const char *on_color_operational, *on_color_setup; |
ee8c4568 | 955 | |
7db07767 MY |
956 | (void) sd_network_link_get_operational_state(link->ifindex, &operational_state); |
957 | operational_state_to_color(link->name, operational_state, &on_color_operational, NULL); | |
d57c365b | 958 | |
7db07767 | 959 | (void) sd_network_link_get_setup_state(link->ifindex, &setup_state); |
37da8bca | 960 | setup_state_to_color(setup_state, &on_color_setup, NULL); |
ee8c4568 | 961 | |
7db07767 | 962 | r = net_get_type_string(link->sd_device, link->iftype, &t); |
1a3caa49 YW |
963 | if (r == -ENOMEM) |
964 | return log_oom(); | |
ee8c4568 | 965 | |
ff7c88a2 | 966 | r = table_add_many(table, |
7db07767 MY |
967 | TABLE_INT, link->ifindex, |
968 | TABLE_STRING, link->name, | |
484f22dd YW |
969 | TABLE_STRING, t, |
970 | TABLE_STRING, operational_state, | |
8d0e0af2 | 971 | TABLE_SET_COLOR, on_color_operational, |
8f60c998 | 972 | TABLE_STRING, setup_state ?: "unmanaged", |
8d0e0af2 | 973 | TABLE_SET_COLOR, on_color_setup); |
ff7c88a2 | 974 | if (r < 0) |
bd17fa8c | 975 | return table_log_add_error(r); |
ee8c4568 LP |
976 | } |
977 | ||
ff7c88a2 YW |
978 | r = table_print(table, NULL); |
979 | if (r < 0) | |
4b6607d9 | 980 | return table_log_print_error(r); |
ff7c88a2 | 981 | |
ee8c4568 | 982 | if (arg_legend) |
6d0c65ff | 983 | printf("\n%i links listed.\n", c); |
ee8c4568 LP |
984 | |
985 | return 0; | |
986 | } | |
987 | ||
c09da729 | 988 | /* IEEE Organizationally Unique Identifier vendor string */ |
b147503e | 989 | static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) { |
61c84edf | 990 | _cleanup_free_ char *desc = NULL; |
81fd1dd3 | 991 | const char *description; |
61c84edf | 992 | char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1]; |
81fd1dd3 TG |
993 | int r; |
994 | ||
995 | assert(ret); | |
c09da729 | 996 | |
61c84edf | 997 | if (!hwdb || !mac) |
81fd1dd3 TG |
998 | return -EINVAL; |
999 | ||
c09da729 TG |
1000 | /* skip commonly misused 00:00:00 (Xerox) prefix */ |
1001 | if (memcmp(mac, "\0\0\0", 3) == 0) | |
1002 | return -EINVAL; | |
1003 | ||
61c84edf | 1004 | xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac)); |
c09da729 | 1005 | |
81fd1dd3 TG |
1006 | r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description); |
1007 | if (r < 0) | |
1008 | return r; | |
c09da729 | 1009 | |
81fd1dd3 TG |
1010 | desc = strdup(description); |
1011 | if (!desc) | |
1012 | return -ENOMEM; | |
c09da729 | 1013 | |
61c84edf | 1014 | *ret = TAKE_PTR(desc); |
81fd1dd3 TG |
1015 | |
1016 | return 0; | |
c09da729 TG |
1017 | } |
1018 | ||
69fb1176 | 1019 | static int get_gateway_description( |
1c4baffc | 1020 | sd_netlink *rtnl, |
81fd1dd3 | 1021 | sd_hwdb *hwdb, |
69fb1176 LP |
1022 | int ifindex, |
1023 | int family, | |
1024 | union in_addr_union *gateway, | |
61c84edf MY |
1025 | char **ret) { |
1026 | ||
4afd3348 | 1027 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
c09da729 TG |
1028 | int r; |
1029 | ||
1030 | assert(rtnl); | |
1031 | assert(ifindex >= 0); | |
4c701096 | 1032 | assert(IN_SET(family, AF_INET, AF_INET6)); |
c09da729 | 1033 | assert(gateway); |
61c84edf | 1034 | assert(ret); |
c09da729 TG |
1035 | |
1036 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); | |
1037 | if (r < 0) | |
1038 | return r; | |
1039 | ||
24c0f385 | 1040 | r = sd_netlink_message_set_request_dump(req, true); |
c09da729 TG |
1041 | if (r < 0) |
1042 | return r; | |
1043 | ||
1c4baffc | 1044 | r = sd_netlink_call(rtnl, req, 0, &reply); |
c09da729 TG |
1045 | if (r < 0) |
1046 | return r; | |
1047 | ||
b48e277f | 1048 | for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
67a46833 YW |
1049 | union in_addr_union gw = IN_ADDR_NULL; |
1050 | struct ether_addr mac = ETHER_ADDR_NULL; | |
c09da729 TG |
1051 | uint16_t type; |
1052 | int ifi, fam; | |
1053 | ||
1c4baffc | 1054 | r = sd_netlink_message_get_errno(m); |
c09da729 | 1055 | if (r < 0) { |
61c84edf | 1056 | log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
c09da729 TG |
1057 | continue; |
1058 | } | |
1059 | ||
1c4baffc | 1060 | r = sd_netlink_message_get_type(m, &type); |
c09da729 | 1061 | if (r < 0) { |
61c84edf | 1062 | log_error_errno(r, "Failed to get netlink message type, ignoring: %m"); |
c09da729 TG |
1063 | continue; |
1064 | } | |
1065 | ||
1066 | if (type != RTM_NEWNEIGH) { | |
61c84edf MY |
1067 | log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1068 | "Got unexpected netlink message type %u, ignoring", | |
1069 | type); | |
c09da729 TG |
1070 | continue; |
1071 | } | |
1072 | ||
1073 | r = sd_rtnl_message_neigh_get_family(m, &fam); | |
1074 | if (r < 0) { | |
61c84edf | 1075 | log_error_errno(r, "Failed to get rtnl family, ignoring: %m"); |
c09da729 TG |
1076 | continue; |
1077 | } | |
1078 | ||
1079 | if (fam != family) { | |
61c84edf | 1080 | log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got invalid rtnl family %d, ignoring", fam); |
c09da729 TG |
1081 | continue; |
1082 | } | |
1083 | ||
1084 | r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); | |
1085 | if (r < 0) { | |
61c84edf | 1086 | log_error_errno(r, "Failed to get rtnl ifindex, ignoring: %m"); |
c09da729 TG |
1087 | continue; |
1088 | } | |
1089 | ||
1090 | if (ifindex > 0 && ifi != ifindex) | |
1091 | continue; | |
1092 | ||
1093 | switch (fam) { | |
61c84edf | 1094 | |
c09da729 | 1095 | case AF_INET: |
1c4baffc | 1096 | r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in); |
c09da729 TG |
1097 | if (r < 0) |
1098 | continue; | |
1099 | ||
1100 | break; | |
61c84edf | 1101 | |
c09da729 | 1102 | case AF_INET6: |
1c4baffc | 1103 | r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6); |
c09da729 TG |
1104 | if (r < 0) |
1105 | continue; | |
1106 | ||
1107 | break; | |
61c84edf | 1108 | |
c09da729 TG |
1109 | default: |
1110 | continue; | |
1111 | } | |
1112 | ||
1113 | if (!in_addr_equal(fam, &gw, gateway)) | |
1114 | continue; | |
1115 | ||
49808e0e | 1116 | r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac); |
c09da729 TG |
1117 | if (r < 0) |
1118 | continue; | |
1119 | ||
61c84edf | 1120 | r = ieee_oui(hwdb, &mac, ret); |
c09da729 TG |
1121 | if (r < 0) |
1122 | continue; | |
1123 | ||
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | return -ENODATA; | |
1128 | } | |
1129 | ||
8fff78a1 | 1130 | static int dump_list(Table *table, const char *key, char * const *l) { |
536cdd07 YW |
1131 | int r; |
1132 | ||
43449ca3 | 1133 | assert(table); |
8fff78a1 | 1134 | assert(key); |
43449ca3 | 1135 | |
536cdd07 YW |
1136 | if (strv_isempty(l)) |
1137 | return 0; | |
1138 | ||
1139 | r = table_add_many(table, | |
8fff78a1 | 1140 | TABLE_FIELD, key, |
536cdd07 YW |
1141 | TABLE_STRV, l); |
1142 | if (r < 0) | |
1143 | return table_log_add_error(r); | |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
7db07767 MY |
1148 | static int dump_gateways(sd_netlink *rtnl, sd_hwdb *hwdb, Table *table, int ifindex) { |
1149 | _cleanup_free_ struct local_address *local_addrs = NULL; | |
536cdd07 | 1150 | _cleanup_strv_free_ char **buf = NULL; |
cda7fc8d | 1151 | int r, n; |
c09da729 | 1152 | |
837f57da | 1153 | assert(rtnl); |
98d5bef3 | 1154 | assert(table); |
837f57da | 1155 | |
7db07767 | 1156 | n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
536cdd07 | 1157 | if (n <= 0) |
b6a3ca6d | 1158 | return n; |
c09da729 | 1159 | |
7db07767 | 1160 | FOREACH_ARRAY(local, local_addrs, n) { |
94a77962 | 1161 | _cleanup_free_ char *description = NULL; |
c09da729 | 1162 | |
7db07767 | 1163 | r = get_gateway_description(rtnl, hwdb, local->ifindex, local->family, &local->address, &description); |
c09da729 | 1164 | if (r < 0) |
536cdd07 | 1165 | log_debug_errno(r, "Could not get description of gateway, ignoring: %m"); |
c09da729 | 1166 | |
536cdd07 | 1167 | /* Show interface name for the entry if we show entries for all interfaces */ |
94a77962 | 1168 | r = strv_extendf(&buf, "%s%s%s%s%s%s", |
7db07767 | 1169 | IN_ADDR_TO_STRING(local->family, &local->address), |
94a77962 ZJS |
1170 | description ? " (" : "", |
1171 | strempty(description), | |
1172 | description ? ")" : "", | |
536cdd07 | 1173 | ifindex <= 0 ? " on " : "", |
7db07767 | 1174 | ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
98d5bef3 | 1175 | if (r < 0) |
536cdd07 | 1176 | return log_oom(); |
c09da729 TG |
1177 | } |
1178 | ||
8fff78a1 | 1179 | return dump_list(table, "Gateway", buf); |
c09da729 TG |
1180 | } |
1181 | ||
69fb1176 | 1182 | static int dump_addresses( |
1c4baffc | 1183 | sd_netlink *rtnl, |
d41fa6ee | 1184 | sd_dhcp_lease *lease, |
98d5bef3 | 1185 | Table *table, |
69fb1176 LP |
1186 | int ifindex) { |
1187 | ||
7db07767 | 1188 | _cleanup_free_ struct local_address *local_addrs = NULL; |
536cdd07 | 1189 | _cleanup_strv_free_ char **buf = NULL; |
d41fa6ee | 1190 | struct in_addr dhcp4_address = {}; |
b48e277f | 1191 | int r, n; |
ee8c4568 | 1192 | |
837f57da | 1193 | assert(rtnl); |
98d5bef3 | 1194 | assert(table); |
837f57da | 1195 | |
7db07767 | 1196 | n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
536cdd07 | 1197 | if (n <= 0) |
ee8c4568 LP |
1198 | return n; |
1199 | ||
d41fa6ee YW |
1200 | if (lease) |
1201 | (void) sd_dhcp_lease_get_address(lease, &dhcp4_address); | |
cdf01b36 | 1202 | |
7db07767 | 1203 | FOREACH_ARRAY(local, local_addrs, n) { |
94a77962 ZJS |
1204 | struct in_addr server_address; |
1205 | bool dhcp4 = false; | |
3d0c8750 | 1206 | |
7db07767 | 1207 | if (local->family == AF_INET && in4_addr_equal(&local->address.in, &dhcp4_address)) |
94a77962 | 1208 | dhcp4 = sd_dhcp_lease_get_server_identifier(lease, &server_address) >= 0; |
cdf01b36 | 1209 | |
94a77962 | 1210 | r = strv_extendf(&buf, "%s%s%s%s%s%s", |
7db07767 | 1211 | IN_ADDR_TO_STRING(local->family, &local->address), |
94a77962 ZJS |
1212 | dhcp4 ? " (DHCP4 via " : "", |
1213 | dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "", | |
1214 | dhcp4 ? ")" : "", | |
536cdd07 | 1215 | ifindex <= 0 ? " on " : "", |
7db07767 | 1216 | ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
98d5bef3 | 1217 | if (r < 0) |
536cdd07 | 1218 | return log_oom(); |
ee8c4568 LP |
1219 | } |
1220 | ||
8fff78a1 | 1221 | return dump_list(table, "Address", buf); |
ee8c4568 LP |
1222 | } |
1223 | ||
d37b7627 SS |
1224 | static int dump_address_labels(sd_netlink *rtnl) { |
1225 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
0232beed | 1226 | _cleanup_(table_unrefp) Table *table = NULL; |
0232beed | 1227 | TableCell *cell; |
d37b7627 SS |
1228 | int r; |
1229 | ||
1230 | assert(rtnl); | |
1231 | ||
1232 | r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6); | |
1233 | if (r < 0) | |
1234 | return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m"); | |
1235 | ||
24c0f385 | 1236 | r = sd_netlink_message_set_request_dump(req, true); |
d37b7627 SS |
1237 | if (r < 0) |
1238 | return r; | |
1239 | ||
1240 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
1241 | if (r < 0) | |
1242 | return r; | |
1243 | ||
4252171a | 1244 | table = table_new("label", "prefix/prefixlen"); |
0232beed | 1245 | if (!table) |
bd17fa8c | 1246 | return log_oom(); |
0232beed | 1247 | |
a42d9490 YW |
1248 | if (arg_full) |
1249 | table_set_width(table, 0); | |
1250 | ||
ef1e0b9a | 1251 | r = table_set_sort(table, (size_t) 0); |
0232beed YW |
1252 | if (r < 0) |
1253 | return r; | |
1254 | ||
1255 | assert_se(cell = table_get_cell(table, 0, 0)); | |
1256 | (void) table_set_align_percent(table, cell, 100); | |
81914d9f | 1257 | (void) table_set_ellipsize_percent(table, cell, 100); |
0232beed YW |
1258 | |
1259 | assert_se(cell = table_get_cell(table, 0, 1)); | |
1260 | (void) table_set_align_percent(table, cell, 100); | |
d37b7627 | 1261 | |
b48e277f | 1262 | for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
94a77962 | 1263 | struct in6_addr prefix; |
d37b7627 SS |
1264 | uint8_t prefixlen; |
1265 | uint32_t label; | |
1266 | ||
1267 | r = sd_netlink_message_get_errno(m); | |
1268 | if (r < 0) { | |
61c84edf | 1269 | log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
d37b7627 SS |
1270 | continue; |
1271 | } | |
1272 | ||
1273 | r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label); | |
1274 | if (r < 0 && r != -ENODATA) { | |
1275 | log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m"); | |
1276 | continue; | |
1277 | } | |
1278 | ||
94a77962 | 1279 | r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix); |
d37b7627 SS |
1280 | if (r < 0) |
1281 | continue; | |
1282 | ||
1283 | r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen); | |
1284 | if (r < 0) | |
1285 | continue; | |
1286 | ||
81914d9f | 1287 | r = table_add_cell(table, NULL, TABLE_UINT32, &label); |
0232beed | 1288 | if (r < 0) |
bd17fa8c | 1289 | return table_log_add_error(r); |
0232beed | 1290 | |
94a77962 | 1291 | r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen); |
0232beed | 1292 | if (r < 0) |
bd17fa8c | 1293 | return table_log_add_error(r); |
d37b7627 SS |
1294 | } |
1295 | ||
bd17fa8c YW |
1296 | r = table_print(table, NULL); |
1297 | if (r < 0) | |
4b6607d9 | 1298 | return table_log_print_error(r); |
bd17fa8c YW |
1299 | |
1300 | return 0; | |
d37b7627 SS |
1301 | } |
1302 | ||
1303 | static int list_address_labels(int argc, char *argv[], void *userdata) { | |
1304 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
1305 | int r; | |
1306 | ||
1307 | r = sd_netlink_open(&rtnl); | |
1308 | if (r < 0) | |
1309 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
1310 | ||
61c84edf | 1311 | return dump_address_labels(rtnl); |
d37b7627 SS |
1312 | } |
1313 | ||
837f57da | 1314 | static int open_lldp_neighbors(int ifindex, FILE **ret) { |
61c84edf | 1315 | _cleanup_fclose_ FILE *f = NULL; |
100433e0 | 1316 | char p[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)]; |
837f57da | 1317 | |
43449ca3 MY |
1318 | assert(ifindex >= 0); |
1319 | assert(ret); | |
1320 | ||
100433e0 | 1321 | xsprintf(p, "/run/systemd/netif/lldp/%i", ifindex); |
61c84edf | 1322 | |
837f57da LP |
1323 | f = fopen(p, "re"); |
1324 | if (!f) | |
1325 | return -errno; | |
1326 | ||
61c84edf | 1327 | *ret = TAKE_PTR(f); |
837f57da LP |
1328 | return 0; |
1329 | } | |
1330 | ||
1331 | static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) { | |
1332 | _cleanup_free_ void *raw = NULL; | |
1333 | size_t l; | |
1334 | le64_t u; | |
1335 | int r; | |
1336 | ||
1337 | assert(f); | |
1338 | assert(ret); | |
1339 | ||
1340 | l = fread(&u, 1, sizeof(u), f); | |
1341 | if (l == 0 && feof(f)) | |
1342 | return 0; | |
1343 | if (l != sizeof(u)) | |
1344 | return -EBADMSG; | |
1345 | ||
d23c3e4c FB |
1346 | /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */ |
1347 | if (le64toh(u) >= 4096) | |
1348 | return -EBADMSG; | |
1349 | ||
837f57da LP |
1350 | raw = new(uint8_t, le64toh(u)); |
1351 | if (!raw) | |
1352 | return -ENOMEM; | |
1353 | ||
1354 | if (fread(raw, 1, le64toh(u), f) != le64toh(u)) | |
1355 | return -EBADMSG; | |
1356 | ||
1357 | r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u)); | |
1358 | if (r < 0) | |
1359 | return r; | |
1360 | ||
1361 | return 1; | |
1362 | } | |
1363 | ||
98d5bef3 | 1364 | static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) { |
536cdd07 | 1365 | _cleanup_strv_free_ char **buf = NULL; |
837f57da | 1366 | _cleanup_fclose_ FILE *f = NULL; |
536cdd07 | 1367 | int r; |
837f57da | 1368 | |
98d5bef3 | 1369 | assert(table); |
837f57da LP |
1370 | assert(prefix); |
1371 | assert(ifindex > 0); | |
1372 | ||
1373 | r = open_lldp_neighbors(ifindex, &f); | |
98d5bef3 YW |
1374 | if (r == -ENOENT) |
1375 | return 0; | |
837f57da LP |
1376 | if (r < 0) |
1377 | return r; | |
1378 | ||
1379 | for (;;) { | |
1380 | const char *system_name = NULL, *port_id = NULL, *port_description = NULL; | |
1381 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; | |
1382 | ||
1383 | r = next_lldp_neighbor(f, &n); | |
1384 | if (r < 0) | |
1385 | return r; | |
1386 | if (r == 0) | |
1387 | break; | |
1388 | ||
837f57da LP |
1389 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); |
1390 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
1391 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
1392 | ||
536cdd07 YW |
1393 | r = strv_extendf(&buf, "%s on port %s%s%s%s", |
1394 | strna(system_name), | |
1395 | strna(port_id), | |
1396 | isempty(port_description) ? "" : " (", | |
1397 | strempty(port_description), | |
1398 | isempty(port_description) ? "" : ")"); | |
98d5bef3 | 1399 | if (r < 0) |
536cdd07 | 1400 | return log_oom(); |
837f57da LP |
1401 | } |
1402 | ||
536cdd07 | 1403 | return dump_list(table, prefix, buf); |
837f57da LP |
1404 | } |
1405 | ||
d9ce1c24 MAL |
1406 | static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) { |
1407 | _cleanup_strv_free_ char **buf = NULL; | |
1408 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1409 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1410 | int r; | |
1411 | ||
43449ca3 MY |
1412 | assert(table); |
1413 | assert(prefix); | |
1414 | assert(bus); | |
1415 | assert(link); | |
1416 | ||
8bdece74 | 1417 | r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.DHCPServer", "Leases", "a(uayayayayt)"); |
d9ce1c24 MAL |
1418 | if (r < 0) { |
1419 | bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY); | |
1420 | ||
1421 | log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, | |
1422 | r, "Failed to query link DHCP leases: %s", bus_error_message(&error, r)); | |
1423 | return 0; | |
1424 | } | |
1425 | ||
d9ce1c24 MAL |
1426 | r = sd_bus_message_enter_container(reply, 'a', "(uayayayayt)"); |
1427 | if (r < 0) | |
1428 | return bus_log_parse_error(r); | |
1429 | ||
1430 | while ((r = sd_bus_message_enter_container(reply, 'r', "uayayayayt")) > 0) { | |
1431 | _cleanup_free_ char *id = NULL, *ip = NULL; | |
1432 | const void *client_id, *addr, *gtw, *hwaddr; | |
1433 | size_t client_id_sz, sz; | |
1434 | uint64_t expiration; | |
1435 | uint32_t family; | |
1436 | ||
1437 | r = sd_bus_message_read(reply, "u", &family); | |
1438 | if (r < 0) | |
1439 | return bus_log_parse_error(r); | |
1440 | ||
1441 | r = sd_bus_message_read_array(reply, 'y', &client_id, &client_id_sz); | |
1442 | if (r < 0) | |
1443 | return bus_log_parse_error(r); | |
1444 | ||
1445 | r = sd_bus_message_read_array(reply, 'y', &addr, &sz); | |
1446 | if (r < 0 || sz != 4) | |
1447 | return bus_log_parse_error(r); | |
1448 | ||
1449 | r = sd_bus_message_read_array(reply, 'y', >w, &sz); | |
1450 | if (r < 0 || sz != 4) | |
1451 | return bus_log_parse_error(r); | |
1452 | ||
1453 | r = sd_bus_message_read_array(reply, 'y', &hwaddr, &sz); | |
1454 | if (r < 0) | |
1455 | return bus_log_parse_error(r); | |
1456 | ||
1457 | r = sd_bus_message_read_basic(reply, 't', &expiration); | |
1458 | if (r < 0) | |
1459 | return bus_log_parse_error(r); | |
1460 | ||
18091320 | 1461 | r = sd_dhcp_client_id_to_string_from_raw(client_id, client_id_sz, &id); |
d9ce1c24 MAL |
1462 | if (r < 0) |
1463 | return bus_log_parse_error(r); | |
1464 | ||
1465 | r = in_addr_to_string(family, addr, &ip); | |
1466 | if (r < 0) | |
1467 | return bus_log_parse_error(r); | |
1468 | ||
1469 | r = strv_extendf(&buf, "%s (to %s)", ip, id); | |
1470 | if (r < 0) | |
1471 | return log_oom(); | |
1472 | ||
1473 | r = sd_bus_message_exit_container(reply); | |
1474 | if (r < 0) | |
1475 | return bus_log_parse_error(r); | |
1476 | } | |
1477 | if (r < 0) | |
1478 | return bus_log_parse_error(r); | |
1479 | ||
1480 | r = sd_bus_message_exit_container(reply); | |
1481 | if (r < 0) | |
1482 | return bus_log_parse_error(r); | |
1483 | ||
1484 | r = sd_bus_message_exit_container(reply); | |
1485 | if (r < 0) | |
1486 | return bus_log_parse_error(r); | |
1487 | ||
1488 | if (strv_isempty(buf)) { | |
1489 | r = strv_extendf(&buf, "none"); | |
1490 | if (r < 0) | |
1491 | return log_oom(); | |
1492 | } | |
1493 | ||
1494 | return dump_list(table, prefix, buf); | |
1495 | } | |
1496 | ||
98d5bef3 | 1497 | static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) { |
98d5bef3 | 1498 | int r; |
b295beea | 1499 | |
43449ca3 | 1500 | assert(table); |
b295beea LP |
1501 | assert(prefix); |
1502 | ||
8fff78a1 | 1503 | if (!ifindexes) |
98d5bef3 | 1504 | return 0; |
b295beea | 1505 | |
b48e277f | 1506 | for (unsigned c = 0; ifindexes[c] > 0; c++) { |
8fff78a1 MY |
1507 | if (c == 0) |
1508 | r = table_add_cell(table, NULL, TABLE_FIELD, prefix); | |
1509 | else | |
1510 | r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); | |
1511 | if (r < 0) | |
1512 | return table_log_add_error(r); | |
1513 | ||
1514 | r = table_add_cell(table, NULL, TABLE_IFINDEX, &ifindexes[c]); | |
98d5bef3 | 1515 | if (r < 0) |
bd17fa8c | 1516 | return table_log_add_error(r); |
b295beea | 1517 | } |
98d5bef3 YW |
1518 | |
1519 | return 0; | |
b295beea LP |
1520 | } |
1521 | ||
8fff78a1 MY |
1522 | #define DUMP_STATS_ONE(name, val_name) \ |
1523 | ({ \ | |
1524 | r = table_add_cell(table, NULL, TABLE_FIELD, name); \ | |
1525 | if (r < 0) \ | |
1526 | return table_log_add_error(r); \ | |
1527 | r = table_add_cell(table, NULL, \ | |
1528 | info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \ | |
1529 | info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \ | |
1530 | if (r < 0) \ | |
1531 | return table_log_add_error(r); \ | |
1532 | }) | |
a459b24f YW |
1533 | |
1534 | static int dump_statistics(Table *table, const LinkInfo *info) { | |
1535 | int r; | |
1536 | ||
43449ca3 MY |
1537 | assert(table); |
1538 | assert(info); | |
1539 | ||
a459b24f YW |
1540 | if (!arg_stats) |
1541 | return 0; | |
1542 | ||
1543 | if (!info->has_stats64 && !info->has_stats) | |
1544 | return 0; | |
1545 | ||
1546 | DUMP_STATS_ONE("Rx Packets", rx_packets); | |
1547 | DUMP_STATS_ONE("Tx Packets", tx_packets); | |
1548 | DUMP_STATS_ONE("Rx Bytes", rx_bytes); | |
1549 | DUMP_STATS_ONE("Tx Bytes", tx_bytes); | |
1550 | DUMP_STATS_ONE("Rx Errors", rx_errors); | |
1551 | DUMP_STATS_ONE("Tx Errors", tx_errors); | |
1552 | DUMP_STATS_ONE("Rx Dropped", rx_dropped); | |
1553 | DUMP_STATS_ONE("Tx Dropped", tx_dropped); | |
1554 | DUMP_STATS_ONE("Multicast Packets", multicast); | |
1555 | DUMP_STATS_ONE("Collisions", collisions); | |
1556 | ||
1557 | return 0; | |
1558 | } | |
1559 | ||
57ae8cd8 YW |
1560 | static int dump_hw_address(Table *table, sd_hwdb *hwdb, const char *field, const struct hw_addr_data *addr) { |
1561 | _cleanup_free_ char *description = NULL; | |
1562 | int r; | |
1563 | ||
1564 | assert(table); | |
1565 | assert(field); | |
1566 | assert(addr); | |
1567 | ||
1568 | if (addr->length == ETH_ALEN) | |
1569 | (void) ieee_oui(hwdb, &addr->ether, &description); | |
1570 | ||
8fff78a1 | 1571 | r = table_add_cell(table, NULL, TABLE_FIELD, field); |
57ae8cd8 YW |
1572 | if (r < 0) |
1573 | return table_log_add_error(r); | |
1574 | ||
1575 | r = table_add_cell_stringf(table, NULL, "%s%s%s%s", | |
1576 | HW_ADDR_TO_STR(addr), | |
1577 | description ? " (" : "", | |
1578 | strempty(description), | |
1579 | description ? ")" : ""); | |
1580 | if (r < 0) | |
1581 | return table_log_add_error(r); | |
1582 | ||
1583 | return 0; | |
1584 | } | |
1585 | ||
10c71c36 YW |
1586 | static OutputFlags get_output_flags(void) { |
1587 | return | |
1588 | arg_all * OUTPUT_SHOW_ALL | | |
1589 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | | |
1590 | colors_enabled() * OUTPUT_COLOR; | |
1591 | } | |
1592 | ||
1593 | static int show_logs(const LinkInfo *info) { | |
1594 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
1595 | int r; | |
1596 | ||
1597 | if (arg_lines == 0) | |
1598 | return 0; | |
1599 | ||
1600 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); | |
1601 | if (r < 0) | |
1602 | return log_error_errno(r, "Failed to open journal: %m"); | |
1603 | ||
1604 | r = add_match_this_boot(j, NULL); | |
1605 | if (r < 0) | |
1606 | return log_error_errno(r, "Failed to add boot matches: %m"); | |
1607 | ||
1608 | if (info) { | |
1609 | char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)]; | |
1610 | const char *m2, *m3; | |
1611 | ||
1612 | /* kernel */ | |
1613 | xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex); | |
1614 | /* networkd */ | |
1615 | m2 = strjoina("INTERFACE=", info->name); | |
1616 | /* udevd */ | |
1617 | m3 = strjoina("DEVICE=", info->name); | |
1618 | ||
1619 | (void)( | |
1620 | (r = sd_journal_add_match(j, m1, 0)) || | |
1621 | (r = sd_journal_add_disjunction(j)) || | |
1622 | (r = sd_journal_add_match(j, m2, 0)) || | |
1623 | (r = sd_journal_add_disjunction(j)) || | |
1624 | (r = sd_journal_add_match(j, m3, 0)) | |
1625 | ); | |
1626 | if (r < 0) | |
1627 | return log_error_errno(r, "Failed to add link matches: %m"); | |
1628 | } else { | |
1629 | r = add_matches_for_unit(j, "systemd-networkd.service"); | |
1630 | if (r < 0) | |
1631 | return log_error_errno(r, "Failed to add unit matches: %m"); | |
1632 | ||
1633 | r = add_matches_for_unit(j, "systemd-networkd-wait-online.service"); | |
1634 | if (r < 0) | |
1635 | return log_error_errno(r, "Failed to add unit matches: %m"); | |
1636 | } | |
1637 | ||
1638 | return show_journal( | |
1639 | stdout, | |
1640 | j, | |
1641 | OUTPUT_SHORT, | |
1642 | 0, | |
1643 | 0, | |
1644 | arg_lines, | |
1645 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
1646 | NULL); | |
1647 | } | |
1648 | ||
f8d7c0c5 YW |
1649 | static int table_add_string_line(Table *table, const char *key, const char *value) { |
1650 | int r; | |
1651 | ||
1652 | assert(table); | |
1653 | assert(key); | |
1654 | ||
1655 | if (isempty(value)) | |
1656 | return 0; | |
1657 | ||
1658 | r = table_add_many(table, | |
8fff78a1 | 1659 | TABLE_FIELD, key, |
f8d7c0c5 YW |
1660 | TABLE_STRING, value); |
1661 | if (r < 0) | |
1662 | return table_log_add_error(r); | |
1663 | ||
1664 | return 0; | |
1665 | } | |
1666 | ||
a2640646 DDM |
1667 | static int format_dropins(char **dropins) { |
1668 | STRV_FOREACH(d, dropins) { | |
1669 | _cleanup_free_ char *s = NULL; | |
1670 | int glyph = *(d + 1) == NULL ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH; | |
1671 | ||
1672 | s = strjoin(special_glyph(glyph), *d); | |
1673 | if (!s) | |
1674 | return log_oom(); | |
1675 | ||
1676 | free_and_replace(*d, s); | |
1677 | } | |
1678 | ||
1679 | return 0; | |
1680 | } | |
1681 | ||
69fb1176 | 1682 | static int link_status_one( |
d9ce1c24 | 1683 | sd_bus *bus, |
1c4baffc | 1684 | sd_netlink *rtnl, |
81fd1dd3 | 1685 | sd_hwdb *hwdb, |
b147503e LP |
1686 | const LinkInfo *info) { |
1687 | ||
a2640646 DDM |
1688 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL, |
1689 | **route_domains = NULL, **link_dropins = NULL, **network_dropins = NULL; | |
e469d2a2 | 1690 | _cleanup_free_ char *t = NULL, *network = NULL, *iaid = NULL, *duid = NULL, *captive_portal = NULL, |
100433e0 | 1691 | *setup_state = NULL, *operational_state = NULL, *online_state = NULL, *activation_policy = NULL; |
35cab5f9 | 1692 | const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL, |
4a481ec4 | 1693 | *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup, *on_color_online; |
b295beea | 1694 | _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL; |
35cab5f9 | 1695 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
98d5bef3 | 1696 | _cleanup_(table_unrefp) Table *table = NULL; |
b147503e | 1697 | int r; |
9085f64a | 1698 | |
43449ca3 | 1699 | assert(bus); |
9085f64a | 1700 | assert(rtnl); |
b147503e | 1701 | assert(info); |
9085f64a | 1702 | |
b147503e | 1703 | (void) sd_network_link_get_operational_state(info->ifindex, &operational_state); |
ceb366df | 1704 | operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational); |
d57c365b | 1705 | |
4a481ec4 AŠ |
1706 | (void) sd_network_link_get_online_state(info->ifindex, &online_state); |
1707 | online_state_to_color(online_state, &on_color_online, NULL); | |
1708 | ||
8f60c998 | 1709 | (void) sd_network_link_get_setup_state(info->ifindex, &setup_state); |
d57c365b | 1710 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
9085f64a | 1711 | |
b147503e LP |
1712 | (void) sd_network_link_get_dns(info->ifindex, &dns); |
1713 | (void) sd_network_link_get_search_domains(info->ifindex, &search_domains); | |
1714 | (void) sd_network_link_get_route_domains(info->ifindex, &route_domains); | |
1715 | (void) sd_network_link_get_ntp(info->ifindex, &ntp); | |
1f807af6 | 1716 | (void) sd_network_link_get_sip(info->ifindex, &sip); |
e469d2a2 | 1717 | (void) sd_network_link_get_captive_portal(info->ifindex, &captive_portal); |
f1d07d83 | 1718 | (void) sd_network_link_get_network_file(info->ifindex, &network); |
a2640646 | 1719 | (void) sd_network_link_get_network_file_dropins(info->ifindex, &network_dropins); |
f1d07d83 YW |
1720 | (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to); |
1721 | (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by); | |
1722 | (void) sd_network_link_get_activation_policy(info->ifindex, &activation_policy); | |
9085f64a | 1723 | |
172353b1 | 1724 | if (info->sd_device) { |
a2640646 DDM |
1725 | const char *joined; |
1726 | ||
172353b1 | 1727 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link); |
a2640646 DDM |
1728 | |
1729 | if (sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) { | |
1730 | r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE); | |
1731 | if (r < 0) | |
1732 | return r; | |
1733 | } | |
1734 | ||
172353b1 ZJS |
1735 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver); |
1736 | (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path); | |
3cc7a9fd LP |
1737 | (void) device_get_vendor_string(info->sd_device, &vendor); |
1738 | (void) device_get_model_string(info->sd_device, &model); | |
9085f64a LP |
1739 | } |
1740 | ||
4eef6fad | 1741 | r = net_get_type_string(info->sd_device, info->iftype, &t); |
1a3caa49 YW |
1742 | if (r == -ENOMEM) |
1743 | return log_oom(); | |
b1acce80 | 1744 | |
100433e0 ZJS |
1745 | char lease_file[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)]; |
1746 | xsprintf(lease_file, "/run/systemd/netif/leases/%i", info->ifindex); | |
35cab5f9 YW |
1747 | |
1748 | (void) dhcp_lease_load(&lease, lease_file); | |
1749 | ||
a2640646 DDM |
1750 | r = format_dropins(network_dropins); |
1751 | if (r < 0) | |
1752 | return r; | |
1753 | ||
1754 | if (strv_prepend(&network_dropins, network) < 0) | |
1755 | return log_oom(); | |
1756 | ||
1757 | r = format_dropins(link_dropins); | |
1758 | if (r < 0) | |
1759 | return r; | |
1760 | ||
1761 | if (strv_prepend(&link_dropins, link) < 0) | |
1762 | return log_oom(); | |
1763 | ||
8fff78a1 | 1764 | table = table_new_vertical(); |
98d5bef3 | 1765 | if (!table) |
bd17fa8c | 1766 | return log_oom(); |
98d5bef3 | 1767 | |
a42d9490 YW |
1768 | if (arg_full) |
1769 | table_set_width(table, 0); | |
1770 | ||
f8d7c0c5 | 1771 | /* unit files and basic states. */ |
8d0e0af2 | 1772 | r = table_add_many(table, |
8fff78a1 | 1773 | TABLE_FIELD, "Link File", |
a2640646 | 1774 | TABLE_STRV, link_dropins ?: STRV_MAKE("n/a"), |
8fff78a1 | 1775 | TABLE_FIELD, "Network File", |
a2640646 | 1776 | TABLE_STRV, network_dropins ?: STRV_MAKE("n/a"), |
8fff78a1 | 1777 | TABLE_FIELD, "State"); |
98d5bef3 | 1778 | if (r < 0) |
bd17fa8c | 1779 | return table_log_add_error(r); |
f8d7c0c5 | 1780 | |
98d5bef3 | 1781 | r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)", |
64e7ebde | 1782 | on_color_operational, strna(operational_state), off_color_operational, |
8f60c998 | 1783 | on_color_setup, setup_state ?: "unmanaged", off_color_setup); |
98d5bef3 | 1784 | if (r < 0) |
bd17fa8c | 1785 | return table_log_add_error(r); |
98d5bef3 | 1786 | |
4a481ec4 | 1787 | r = table_add_many(table, |
8fff78a1 | 1788 | TABLE_FIELD, "Online state", |
4a481ec4 AŠ |
1789 | TABLE_STRING, online_state ?: "unknown", |
1790 | TABLE_SET_COLOR, on_color_online); | |
1791 | if (r < 0) | |
1792 | return table_log_add_error(r); | |
1793 | ||
8fff78a1 | 1794 | r = table_add_string_line(table, "Type", t); |
f8d7c0c5 YW |
1795 | if (r < 0) |
1796 | return r; | |
1797 | ||
8fff78a1 | 1798 | r = table_add_string_line(table, "Kind", info->netdev_kind); |
f8d7c0c5 YW |
1799 | if (r < 0) |
1800 | return r; | |
1801 | ||
8fff78a1 | 1802 | r = table_add_string_line(table, "Path", path); |
f8d7c0c5 YW |
1803 | if (r < 0) |
1804 | return r; | |
1805 | ||
8fff78a1 | 1806 | r = table_add_string_line(table, "Driver", driver); |
f8d7c0c5 YW |
1807 | if (r < 0) |
1808 | return r; | |
1809 | ||
8fff78a1 | 1810 | r = table_add_string_line(table, "Vendor", vendor); |
f8d7c0c5 YW |
1811 | if (r < 0) |
1812 | return r; | |
1813 | ||
8fff78a1 | 1814 | r = table_add_string_line(table, "Model", model); |
f8d7c0c5 YW |
1815 | if (r < 0) |
1816 | return r; | |
1817 | ||
ae5b7792 | 1818 | strv_sort(info->alternative_names); |
8fff78a1 | 1819 | r = dump_list(table, "Alternative Names", info->alternative_names); |
536cdd07 YW |
1820 | if (r < 0) |
1821 | return r; | |
511070ee | 1822 | |
57ae8cd8 | 1823 | if (info->has_hw_address) { |
8fff78a1 | 1824 | r = dump_hw_address(table, hwdb, "Hardware Address", &info->hw_address); |
98d5bef3 | 1825 | if (r < 0) |
57ae8cd8 | 1826 | return r; |
db73295a | 1827 | } |
9085f64a | 1828 | |
ed9fa69f | 1829 | if (info->has_permanent_hw_address) { |
8fff78a1 | 1830 | r = dump_hw_address(table, hwdb, "Permanent Hardware Address", &info->permanent_hw_address); |
caa8538a | 1831 | if (r < 0) |
ed9fa69f | 1832 | return r; |
caa8538a YW |
1833 | } |
1834 | ||
c06ff86e YW |
1835 | if (info->mtu > 0) { |
1836 | char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)]; | |
1837 | ||
1838 | xsprintf(min_str, "%" PRIu32, info->min_mtu); | |
1839 | xsprintf(max_str, "%" PRIu32, info->max_mtu); | |
1840 | ||
8fff78a1 | 1841 | r = table_add_cell(table, NULL, TABLE_FIELD, "MTU"); |
98d5bef3 | 1842 | if (r < 0) |
bd17fa8c | 1843 | return table_log_add_error(r); |
8fff78a1 | 1844 | |
c06ff86e YW |
1845 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s", |
1846 | info->mtu, | |
1847 | info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "", | |
90e29fe1 | 1848 | info->min_mtu > 0 ? "min: " : "", |
c06ff86e YW |
1849 | info->min_mtu > 0 ? min_str : "", |
1850 | info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "", | |
90e29fe1 | 1851 | info->max_mtu > 0 ? "max: " : "", |
c06ff86e YW |
1852 | info->max_mtu > 0 ? max_str : "", |
1853 | info->min_mtu > 0 || info->max_mtu > 0 ? ")" : ""); | |
98d5bef3 | 1854 | if (r < 0) |
e810df37 SS |
1855 | return table_log_add_error(r); |
1856 | } | |
1857 | ||
8fff78a1 | 1858 | r = table_add_string_line(table, "QDisc", info->qdisc); |
f8d7c0c5 YW |
1859 | if (r < 0) |
1860 | return r; | |
6cfef1b3 SS |
1861 | |
1862 | if (info->master > 0) { | |
1863 | r = table_add_many(table, | |
8fff78a1 | 1864 | TABLE_FIELD, "Master", |
6cfef1b3 SS |
1865 | TABLE_IFINDEX, info->master); |
1866 | if (r < 0) | |
bd17fa8c | 1867 | return table_log_add_error(r); |
98d5bef3 | 1868 | } |
8eb9058d | 1869 | |
d69b62de SS |
1870 | if (info->has_ipv6_address_generation_mode) { |
1871 | static const struct { | |
1872 | const char *mode; | |
1873 | } mode_table[] = { | |
1874 | { "eui64" }, | |
1875 | { "none" }, | |
1876 | { "stable-privacy" }, | |
1877 | { "random" }, | |
1878 | }; | |
1879 | ||
1880 | r = table_add_many(table, | |
8fff78a1 | 1881 | TABLE_FIELD, "IPv6 Address Generation Mode", |
d69b62de SS |
1882 | TABLE_STRING, mode_table[info->addr_gen_mode]); |
1883 | if (r < 0) | |
1884 | return table_log_add_error(r); | |
1885 | } | |
1886 | ||
c82d1bf2 SS |
1887 | if (streq_ptr(info->netdev_kind, "bridge")) { |
1888 | r = table_add_many(table, | |
8fff78a1 | 1889 | TABLE_FIELD, "Forward Delay", |
c82d1bf2 | 1890 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay), |
8fff78a1 | 1891 | TABLE_FIELD, "Hello Time", |
c82d1bf2 | 1892 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time), |
8fff78a1 | 1893 | TABLE_FIELD, "Max Age", |
c82d1bf2 | 1894 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age), |
8fff78a1 | 1895 | TABLE_FIELD, "Ageing Time", |
c82d1bf2 | 1896 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time), |
8fff78a1 | 1897 | TABLE_FIELD, "Priority", |
c82d1bf2 | 1898 | TABLE_UINT16, info->priority, |
8fff78a1 | 1899 | TABLE_FIELD, "STP", |
c82d1bf2 | 1900 | TABLE_BOOLEAN, info->stp_state > 0, |
8fff78a1 | 1901 | TABLE_FIELD, "Multicast IGMP Version", |
12ef8fb6 | 1902 | TABLE_UINT8, info->mcast_igmp_version, |
8fff78a1 | 1903 | TABLE_FIELD, "Cost", |
12ef8fb6 | 1904 | TABLE_UINT32, info->cost); |
c82d1bf2 | 1905 | if (r < 0) |
bd17fa8c | 1906 | return table_log_add_error(r); |
b24281aa | 1907 | |
f8d7c0c5 | 1908 | if (info->port_state <= BR_STATE_BLOCKING) { |
12ef8fb6 | 1909 | r = table_add_many(table, |
8fff78a1 | 1910 | TABLE_FIELD, "Port State", |
a8389a33 | 1911 | TABLE_STRING, bridge_state_to_string(info->port_state)); |
f8d7c0c5 YW |
1912 | if (r < 0) |
1913 | return table_log_add_error(r); | |
1914 | } | |
1915 | ||
b1d6fe70 | 1916 | } else if (streq_ptr(info->netdev_kind, "bond")) { |
b1d6fe70 | 1917 | r = table_add_many(table, |
8fff78a1 | 1918 | TABLE_FIELD, "Mode", |
43bf2874 | 1919 | TABLE_STRING, bond_mode_to_string(info->mode), |
8fff78a1 | 1920 | TABLE_FIELD, "Miimon", |
05e022a9 | 1921 | TABLE_TIMESPAN_MSEC, info->miimon * USEC_PER_MSEC, |
8fff78a1 | 1922 | TABLE_FIELD, "Updelay", |
05e022a9 | 1923 | TABLE_TIMESPAN_MSEC, info->updelay * USEC_PER_MSEC, |
8fff78a1 | 1924 | TABLE_FIELD, "Downdelay", |
05e022a9 | 1925 | TABLE_TIMESPAN_MSEC, info->downdelay * USEC_PER_MSEC); |
b1d6fe70 SS |
1926 | if (r < 0) |
1927 | return table_log_add_error(r); | |
1928 | ||
b24281aa | 1929 | } else if (streq_ptr(info->netdev_kind, "vxlan")) { |
22ae6c7d SS |
1930 | char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))]; |
1931 | ||
b24281aa SS |
1932 | if (info->vxlan_info.vni > 0) { |
1933 | r = table_add_many(table, | |
8fff78a1 | 1934 | TABLE_FIELD, "VNI", |
b24281aa SS |
1935 | TABLE_UINT32, info->vxlan_info.vni); |
1936 | if (r < 0) | |
bd17fa8c | 1937 | return table_log_add_error(r); |
b24281aa SS |
1938 | } |
1939 | ||
1940 | if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) { | |
659f85a5 SS |
1941 | const char *p; |
1942 | ||
1943 | r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group); | |
1944 | if (r <= 0) | |
8fff78a1 | 1945 | p = "Remote"; |
659f85a5 | 1946 | else |
8fff78a1 | 1947 | p = "Group"; |
659f85a5 | 1948 | |
b24281aa | 1949 | r = table_add_many(table, |
8fff78a1 MY |
1950 | TABLE_FIELD, p, |
1951 | info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.group); | |
b24281aa | 1952 | if (r < 0) |
bd17fa8c | 1953 | return table_log_add_error(r); |
b24281aa SS |
1954 | } |
1955 | ||
1956 | if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) { | |
1957 | r = table_add_many(table, | |
8fff78a1 MY |
1958 | TABLE_FIELD, "Local", |
1959 | info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.local); | |
b24281aa | 1960 | if (r < 0) |
bd17fa8c | 1961 | return table_log_add_error(r); |
b24281aa SS |
1962 | } |
1963 | ||
1964 | if (info->vxlan_info.dest_port > 0) { | |
1965 | r = table_add_many(table, | |
8fff78a1 | 1966 | TABLE_FIELD, "Destination Port", |
b24281aa SS |
1967 | TABLE_UINT16, be16toh(info->vxlan_info.dest_port)); |
1968 | if (r < 0) | |
bd17fa8c | 1969 | return table_log_add_error(r); |
b24281aa SS |
1970 | } |
1971 | ||
1972 | if (info->vxlan_info.link > 0) { | |
1973 | r = table_add_many(table, | |
8fff78a1 | 1974 | TABLE_FIELD, "Underlying Device", |
b24281aa SS |
1975 | TABLE_IFINDEX, info->vxlan_info.link); |
1976 | if (r < 0) | |
bd17fa8c | 1977 | return table_log_add_error(r); |
b24281aa | 1978 | } |
22ae6c7d SS |
1979 | |
1980 | r = table_add_many(table, | |
8fff78a1 MY |
1981 | TABLE_FIELD, "Learning", |
1982 | TABLE_BOOLEAN, info->vxlan_info.learning, | |
1983 | TABLE_FIELD, "RSC", | |
1984 | TABLE_BOOLEAN, info->vxlan_info.rsc, | |
1985 | TABLE_FIELD, "L3MISS", | |
1986 | TABLE_BOOLEAN, info->vxlan_info.l3miss, | |
1987 | TABLE_FIELD, "L2MISS", | |
22ae6c7d SS |
1988 | TABLE_BOOLEAN, info->vxlan_info.l2miss); |
1989 | if (r < 0) | |
1990 | return table_log_add_error(r); | |
1991 | ||
1992 | if (info->vxlan_info.tos > 1) { | |
1993 | r = table_add_many(table, | |
8fff78a1 | 1994 | TABLE_FIELD, "TOS", |
22ae6c7d SS |
1995 | TABLE_UINT8, info->vxlan_info.tos); |
1996 | if (r < 0) | |
1997 | return table_log_add_error(r); | |
1998 | } | |
1999 | ||
2000 | if (info->vxlan_info.ttl > 0) | |
2001 | xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl); | |
2002 | else | |
2003 | strcpy(ttl, "auto"); | |
2004 | ||
2005 | r = table_add_many(table, | |
8fff78a1 | 2006 | TABLE_FIELD, "TTL", |
22ae6c7d SS |
2007 | TABLE_STRING, ttl); |
2008 | if (r < 0) | |
2009 | return table_log_add_error(r); | |
f8d7c0c5 | 2010 | |
2b2a1ae6 SS |
2011 | } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) { |
2012 | r = table_add_many(table, | |
8fff78a1 | 2013 | TABLE_FIELD, "VLan Id", |
2b2a1ae6 SS |
2014 | TABLE_UINT16, info->vlan_id); |
2015 | if (r < 0) | |
2016 | return table_log_add_error(r); | |
f8d7c0c5 | 2017 | |
5712d689 | 2018 | } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) { |
94876904 | 2019 | if (in_addr_is_set(AF_INET, &info->local)) { |
dca35224 | 2020 | r = table_add_many(table, |
8fff78a1 | 2021 | TABLE_FIELD, "Local", |
dca35224 SS |
2022 | TABLE_IN_ADDR, &info->local); |
2023 | if (r < 0) | |
2024 | return table_log_add_error(r); | |
2025 | } | |
2026 | ||
94876904 | 2027 | if (in_addr_is_set(AF_INET, &info->remote)) { |
dca35224 | 2028 | r = table_add_many(table, |
8fff78a1 | 2029 | TABLE_FIELD, "Remote", |
dca35224 SS |
2030 | TABLE_IN_ADDR, &info->remote); |
2031 | if (r < 0) | |
2032 | return table_log_add_error(r); | |
2033 | } | |
f8d7c0c5 | 2034 | |
5712d689 | 2035 | } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) { |
94876904 | 2036 | if (in_addr_is_set(AF_INET6, &info->local)) { |
ad760bc1 | 2037 | r = table_add_many(table, |
8fff78a1 | 2038 | TABLE_FIELD, "Local", |
ad760bc1 SS |
2039 | TABLE_IN6_ADDR, &info->local); |
2040 | if (r < 0) | |
2041 | return table_log_add_error(r); | |
2042 | } | |
2043 | ||
94876904 | 2044 | if (in_addr_is_set(AF_INET6, &info->remote)) { |
ad760bc1 | 2045 | r = table_add_many(table, |
8fff78a1 | 2046 | TABLE_FIELD, "Remote", |
ad760bc1 SS |
2047 | TABLE_IN6_ADDR, &info->remote); |
2048 | if (r < 0) | |
2049 | return table_log_add_error(r); | |
2050 | } | |
f8d7c0c5 | 2051 | |
4e1a1991 SS |
2052 | } else if (streq_ptr(info->netdev_kind, "geneve")) { |
2053 | r = table_add_many(table, | |
8fff78a1 | 2054 | TABLE_FIELD, "VNI", |
4e1a1991 SS |
2055 | TABLE_UINT32, info->vni); |
2056 | if (r < 0) | |
2057 | return table_log_add_error(r); | |
2058 | ||
94876904 | 2059 | if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) { |
4e1a1991 | 2060 | r = table_add_many(table, |
8fff78a1 | 2061 | TABLE_FIELD, "Remote", |
4e1a1991 SS |
2062 | TABLE_IN_ADDR, &info->remote); |
2063 | if (r < 0) | |
2064 | return table_log_add_error(r); | |
94876904 | 2065 | } else if (in_addr_is_set(AF_INET6, &info->remote)) { |
4e1a1991 | 2066 | r = table_add_many(table, |
8fff78a1 | 2067 | TABLE_FIELD, "Remote", |
4e1a1991 SS |
2068 | TABLE_IN6_ADDR, &info->remote); |
2069 | if (r < 0) | |
2070 | return table_log_add_error(r); | |
2071 | } | |
2072 | ||
2073 | if (info->ttl > 0) { | |
2074 | r = table_add_many(table, | |
8fff78a1 | 2075 | TABLE_FIELD, "TTL", |
4e1a1991 SS |
2076 | TABLE_UINT8, info->ttl); |
2077 | if (r < 0) | |
2078 | return table_log_add_error(r); | |
2079 | } | |
2080 | ||
2081 | if (info->tos > 0) { | |
2082 | r = table_add_many(table, | |
8fff78a1 | 2083 | TABLE_FIELD, "TOS", |
4e1a1991 SS |
2084 | TABLE_UINT8, info->tos); |
2085 | if (r < 0) | |
2086 | return table_log_add_error(r); | |
2087 | } | |
2088 | ||
2089 | r = table_add_many(table, | |
8fff78a1 MY |
2090 | TABLE_FIELD, "Port", |
2091 | TABLE_UINT16, info->tunnel_port, | |
2092 | TABLE_FIELD, "Inherit", | |
e7b38d7d SS |
2093 | TABLE_STRING, geneve_df_to_string(info->inherit)); |
2094 | if (r < 0) | |
2095 | return table_log_add_error(r); | |
2096 | ||
2097 | if (info->df > 0) { | |
2098 | r = table_add_many(table, | |
8fff78a1 | 2099 | TABLE_FIELD, "IPDoNotFragment", |
e7b38d7d SS |
2100 | TABLE_UINT8, info->df); |
2101 | if (r < 0) | |
2102 | return table_log_add_error(r); | |
2103 | } | |
2104 | ||
2105 | r = table_add_many(table, | |
8fff78a1 MY |
2106 | TABLE_FIELD, "UDPChecksum", |
2107 | TABLE_BOOLEAN, info->csum, | |
2108 | TABLE_FIELD, "UDP6ZeroChecksumTx", | |
2109 | TABLE_BOOLEAN, info->csum6_tx, | |
2110 | TABLE_FIELD, "UDP6ZeroChecksumRx", | |
e7b38d7d SS |
2111 | TABLE_BOOLEAN, info->csum6_rx); |
2112 | if (r < 0) | |
2113 | return table_log_add_error(r); | |
2114 | ||
2115 | if (info->label > 0) { | |
2116 | r = table_add_many(table, | |
8fff78a1 | 2117 | TABLE_FIELD, "FlowLabel", |
e7b38d7d SS |
2118 | TABLE_UINT32, info->label); |
2119 | if (r < 0) | |
2120 | return table_log_add_error(r); | |
2121 | } | |
f8d7c0c5 | 2122 | |
cf217a09 SS |
2123 | } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) { |
2124 | r = table_add_many(table, | |
8fff78a1 | 2125 | TABLE_FIELD, "Mode", |
cf217a09 SS |
2126 | TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode)); |
2127 | if (r < 0) | |
2128 | return table_log_add_error(r); | |
f8d7c0c5 | 2129 | |
851ef1ed | 2130 | } else if (streq_ptr(info->netdev_kind, "ipvlan")) { |
8fff78a1 | 2131 | const char *p; |
851ef1ed SS |
2132 | |
2133 | if (info->ipvlan_flags & IPVLAN_F_PRIVATE) | |
8fff78a1 | 2134 | p = "private"; |
851ef1ed | 2135 | else if (info->ipvlan_flags & IPVLAN_F_VEPA) |
8fff78a1 | 2136 | p = "vepa"; |
851ef1ed | 2137 | else |
8fff78a1 | 2138 | p = "bridge"; |
851ef1ed | 2139 | |
8fff78a1 MY |
2140 | r = table_add_cell(table, NULL, TABLE_FIELD, "Mode"); |
2141 | if (r < 0) | |
2142 | return table_log_add_error(r); | |
851ef1ed | 2143 | |
8fff78a1 MY |
2144 | r = table_add_cell_stringf(table, NULL, "%s (%s)", |
2145 | ipvlan_mode_to_string(info->ipvlan_mode), p); | |
851ef1ed SS |
2146 | if (r < 0) |
2147 | return table_log_add_error(r); | |
c82d1bf2 SS |
2148 | } |
2149 | ||
8d07de25 ZJS |
2150 | if (info->has_wlan_link_info) { |
2151 | _cleanup_free_ char *esc = NULL; | |
8d07de25 | 2152 | |
8fff78a1 | 2153 | r = table_add_cell(table, NULL, TABLE_FIELD, "Wi-Fi access point"); |
8d07de25 | 2154 | if (r < 0) |
bd17fa8c | 2155 | return table_log_add_error(r); |
8d07de25 ZJS |
2156 | |
2157 | if (info->ssid) | |
2158 | esc = cescape(info->ssid); | |
2159 | ||
2160 | r = table_add_cell_stringf(table, NULL, "%s (%s)", | |
2161 | strnull(esc), | |
4b574fd8 | 2162 | ETHER_ADDR_TO_STR(&info->bssid)); |
8d07de25 | 2163 | if (r < 0) |
bd17fa8c | 2164 | return table_log_add_error(r); |
8d07de25 ZJS |
2165 | } |
2166 | ||
335dd8ba | 2167 | if (info->has_bitrates) { |
8fff78a1 | 2168 | r = table_add_cell(table, NULL, TABLE_FIELD, "Bit Rate (Tx/Rx)"); |
335dd8ba | 2169 | if (r < 0) |
bd17fa8c | 2170 | return table_log_add_error(r); |
8fff78a1 | 2171 | |
42a63431 | 2172 | r = table_add_cell_stringf(table, NULL, "%sbps/%sbps", |
46e23f9a ZJS |
2173 | FORMAT_BYTES_FULL(info->tx_bitrate, 0), |
2174 | FORMAT_BYTES_FULL(info->rx_bitrate, 0)); | |
335dd8ba | 2175 | if (r < 0) |
bd17fa8c | 2176 | return table_log_add_error(r); |
335dd8ba YW |
2177 | } |
2178 | ||
98d5bef3 | 2179 | if (info->has_tx_queues || info->has_rx_queues) { |
8fff78a1 | 2180 | r = table_add_cell(table, NULL, TABLE_FIELD, "Number of Queues (Tx/Rx)"); |
98d5bef3 | 2181 | if (r < 0) |
bd17fa8c | 2182 | return table_log_add_error(r); |
8fff78a1 | 2183 | |
98d5bef3 YW |
2184 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues); |
2185 | if (r < 0) | |
bd17fa8c | 2186 | return table_log_add_error(r); |
98d5bef3 | 2187 | } |
0d4ad91d | 2188 | |
c967d2c7 | 2189 | if (info->has_ethtool_link_info) { |
c967d2c7 | 2190 | if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) { |
8d0e0af2 | 2191 | r = table_add_many(table, |
8fff78a1 | 2192 | TABLE_FIELD, "Auto negotiation", |
8d0e0af2 | 2193 | TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE); |
c967d2c7 | 2194 | if (r < 0) |
bd17fa8c | 2195 | return table_log_add_error(r); |
c967d2c7 YW |
2196 | } |
2197 | ||
f8d7c0c5 | 2198 | if (info->speed > 0 && info->speed != UINT64_MAX) { |
8d0e0af2 | 2199 | r = table_add_many(table, |
8fff78a1 | 2200 | TABLE_FIELD, "Speed", |
50299121 | 2201 | TABLE_BPS, info->speed); |
c967d2c7 | 2202 | if (r < 0) |
bd17fa8c | 2203 | return table_log_add_error(r); |
c967d2c7 YW |
2204 | } |
2205 | ||
8fff78a1 | 2206 | r = table_add_string_line(table, "Duplex", duplex_to_string(info->duplex)); |
f8d7c0c5 YW |
2207 | if (r < 0) |
2208 | return r; | |
c967d2c7 | 2209 | |
8fff78a1 | 2210 | r = table_add_string_line(table, "Port", port_to_string(info->port)); |
f8d7c0c5 YW |
2211 | if (r < 0) |
2212 | return r; | |
c967d2c7 YW |
2213 | } |
2214 | ||
d41fa6ee | 2215 | r = dump_addresses(rtnl, lease, table, info->ifindex); |
98d5bef3 YW |
2216 | if (r < 0) |
2217 | return r; | |
8fff78a1 | 2218 | |
98d5bef3 YW |
2219 | r = dump_gateways(rtnl, hwdb, table, info->ifindex); |
2220 | if (r < 0) | |
2221 | return r; | |
8fff78a1 MY |
2222 | |
2223 | r = dump_list(table, "DNS", dns); | |
98d5bef3 YW |
2224 | if (r < 0) |
2225 | return r; | |
8fff78a1 MY |
2226 | |
2227 | r = dump_list(table, "Search Domains", search_domains); | |
98d5bef3 YW |
2228 | if (r < 0) |
2229 | return r; | |
8fff78a1 MY |
2230 | |
2231 | r = dump_list(table, "Route Domains", route_domains); | |
98d5bef3 YW |
2232 | if (r < 0) |
2233 | return r; | |
8fff78a1 MY |
2234 | |
2235 | r = dump_list(table, "NTP", ntp); | |
1f807af6 SS |
2236 | if (r < 0) |
2237 | return r; | |
8fff78a1 MY |
2238 | |
2239 | r = dump_list(table, "SIP", sip); | |
98d5bef3 YW |
2240 | if (r < 0) |
2241 | return r; | |
8fff78a1 MY |
2242 | |
2243 | r = dump_ifindexes(table, "Carrier Bound To", carrier_bound_to); | |
98d5bef3 YW |
2244 | if (r < 0) |
2245 | return r; | |
8fff78a1 MY |
2246 | |
2247 | r = dump_ifindexes(table, "Carrier Bound By", carrier_bound_by); | |
98d5bef3 YW |
2248 | if (r < 0) |
2249 | return r; | |
9085f64a | 2250 | |
8fff78a1 | 2251 | r = table_add_string_line(table, "Activation Policy", activation_policy); |
f1d07d83 YW |
2252 | if (r < 0) |
2253 | return r; | |
a853652a | 2254 | |
003015af DS |
2255 | r = sd_network_link_get_required_for_online(info->ifindex); |
2256 | if (r >= 0) { | |
2257 | r = table_add_many(table, | |
8fff78a1 | 2258 | TABLE_FIELD, "Required For Online", |
003015af DS |
2259 | TABLE_BOOLEAN, r); |
2260 | if (r < 0) | |
2261 | return table_log_add_error(r); | |
2262 | } | |
2263 | ||
52ee8ecf YW |
2264 | if (captive_portal) { |
2265 | r = table_add_many(table, | |
8fff78a1 | 2266 | TABLE_FIELD, "Captive Portal", |
52ee8ecf YW |
2267 | TABLE_STRING, captive_portal, |
2268 | TABLE_SET_URL, captive_portal); | |
2269 | if (r < 0) | |
2270 | return table_log_add_error(r); | |
2271 | } | |
e469d2a2 | 2272 | |
35cab5f9 | 2273 | if (lease) { |
d59a2b3b | 2274 | const sd_dhcp_client_id *client_id; |
35cab5f9 YW |
2275 | const char *tz; |
2276 | ||
2277 | r = sd_dhcp_lease_get_timezone(lease, &tz); | |
2278 | if (r >= 0) { | |
2279 | r = table_add_many(table, | |
8fff78a1 | 2280 | TABLE_FIELD, "Time Zone", |
35cab5f9 YW |
2281 | TABLE_STRING, tz); |
2282 | if (r < 0) | |
2283 | return table_log_add_error(r); | |
2284 | } | |
8eb9058d | 2285 | |
d59a2b3b | 2286 | r = sd_dhcp_lease_get_client_id(lease, &client_id); |
862e7108 YW |
2287 | if (r >= 0) { |
2288 | _cleanup_free_ char *id = NULL; | |
2289 | ||
d59a2b3b | 2290 | r = sd_dhcp_client_id_to_string(client_id, &id); |
862e7108 YW |
2291 | if (r >= 0) { |
2292 | r = table_add_many(table, | |
8fff78a1 | 2293 | TABLE_FIELD, "DHCP4 Client ID", |
862e7108 YW |
2294 | TABLE_STRING, id); |
2295 | if (r < 0) | |
2296 | return table_log_add_error(r); | |
2297 | } | |
2298 | } | |
2153bbc8 SS |
2299 | } |
2300 | ||
331ee15f SS |
2301 | r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid); |
2302 | if (r >= 0) { | |
2303 | r = table_add_many(table, | |
8fff78a1 | 2304 | TABLE_FIELD, "DHCP6 Client IAID", |
331ee15f SS |
2305 | TABLE_STRING, iaid); |
2306 | if (r < 0) | |
2307 | return table_log_add_error(r); | |
2308 | } | |
2309 | ||
63911885 SS |
2310 | r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid); |
2311 | if (r >= 0) { | |
2312 | r = table_add_many(table, | |
8fff78a1 | 2313 | TABLE_FIELD, "DHCP6 Client DUID", |
63911885 SS |
2314 | TABLE_STRING, duid); |
2315 | if (r < 0) | |
2316 | return table_log_add_error(r); | |
2317 | } | |
2318 | ||
8fff78a1 | 2319 | r = dump_lldp_neighbors(table, "Connected To", info->ifindex); |
98d5bef3 YW |
2320 | if (r < 0) |
2321 | return r; | |
837f57da | 2322 | |
8fff78a1 | 2323 | r = dump_dhcp_leases(table, "Offered DHCP leases", bus, info); |
d9ce1c24 MAL |
2324 | if (r < 0) |
2325 | return r; | |
2326 | ||
a459b24f YW |
2327 | r = dump_statistics(table, info); |
2328 | if (r < 0) | |
2329 | return r; | |
2330 | ||
8fff78a1 MY |
2331 | /* First line: circle, ifindex, ifname. */ |
2332 | printf("%s%s%s %d: %s\n", | |
2333 | on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, | |
2334 | info->ifindex, info->name); | |
2335 | ||
10c71c36 YW |
2336 | r = table_print(table, NULL); |
2337 | if (r < 0) | |
4b6607d9 | 2338 | return table_log_print_error(r); |
10c71c36 YW |
2339 | |
2340 | return show_logs(info); | |
9085f64a LP |
2341 | } |
2342 | ||
0070333f | 2343 | static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) { |
8fff78a1 MY |
2344 | _cleanup_free_ char *operational_state = NULL, *online_state = NULL, *netifs_joined = NULL; |
2345 | _cleanup_strv_free_ char **netifs = NULL, **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; | |
2346 | const char *on_color_operational, *off_color_operational, *on_color_online; | |
98d5bef3 | 2347 | _cleanup_(table_unrefp) Table *table = NULL; |
98d5bef3 | 2348 | int r; |
0070333f LP |
2349 | |
2350 | assert(rtnl); | |
2351 | ||
4abd866d | 2352 | (void) sd_network_get_operational_state(&operational_state); |
8fff78a1 | 2353 | operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational); |
0070333f | 2354 | |
4a481ec4 AŠ |
2355 | (void) sd_network_get_online_state(&online_state); |
2356 | online_state_to_color(online_state, &on_color_online, NULL); | |
2357 | ||
8fff78a1 | 2358 | table = table_new_vertical(); |
98d5bef3 | 2359 | if (!table) |
bd17fa8c | 2360 | return log_oom(); |
98d5bef3 | 2361 | |
a42d9490 YW |
2362 | if (arg_full) |
2363 | table_set_width(table, 0); | |
2364 | ||
8fff78a1 MY |
2365 | r = get_files_in_directory("/run/systemd/netif/links/", &netifs); |
2366 | if (r < 0 && r != -ENOENT) | |
2367 | return log_error_errno(r, "Failed to list network interfaces: %m"); | |
2368 | else if (r > 0) { | |
2369 | netifs_joined = strv_join(netifs, ", "); | |
2370 | if (!netifs_joined) | |
2371 | return log_oom(); | |
2372 | } | |
98d5bef3 | 2373 | |
8d0e0af2 | 2374 | r = table_add_many(table, |
8fff78a1 | 2375 | TABLE_FIELD, "State", |
8d0e0af2 | 2376 | TABLE_STRING, strna(operational_state), |
4a481ec4 | 2377 | TABLE_SET_COLOR, on_color_operational, |
8fff78a1 | 2378 | TABLE_FIELD, "Online state", |
4a481ec4 AŠ |
2379 | TABLE_STRING, online_state ?: "unknown", |
2380 | TABLE_SET_COLOR, on_color_online); | |
bd17fa8c YW |
2381 | if (r < 0) |
2382 | return table_log_add_error(r); | |
0070333f | 2383 | |
d41fa6ee | 2384 | r = dump_addresses(rtnl, NULL, table, 0); |
98d5bef3 YW |
2385 | if (r < 0) |
2386 | return r; | |
8fff78a1 | 2387 | |
98d5bef3 YW |
2388 | r = dump_gateways(rtnl, hwdb, table, 0); |
2389 | if (r < 0) | |
2390 | return r; | |
0070333f | 2391 | |
4abd866d | 2392 | (void) sd_network_get_dns(&dns); |
8fff78a1 | 2393 | r = dump_list(table, "DNS", dns); |
98d5bef3 YW |
2394 | if (r < 0) |
2395 | return r; | |
0070333f | 2396 | |
4abd866d | 2397 | (void) sd_network_get_search_domains(&search_domains); |
8fff78a1 | 2398 | r = dump_list(table, "Search Domains", search_domains); |
98d5bef3 YW |
2399 | if (r < 0) |
2400 | return r; | |
0070333f | 2401 | |
4abd866d | 2402 | (void) sd_network_get_route_domains(&route_domains); |
8fff78a1 | 2403 | r = dump_list(table, "Route Domains", route_domains); |
98d5bef3 YW |
2404 | if (r < 0) |
2405 | return r; | |
0070333f | 2406 | |
4abd866d | 2407 | (void) sd_network_get_ntp(&ntp); |
8fff78a1 | 2408 | r = dump_list(table, "NTP", ntp); |
98d5bef3 YW |
2409 | if (r < 0) |
2410 | return r; | |
0070333f | 2411 | |
8fff78a1 MY |
2412 | printf("%s%s%s Interfaces: %s\n", |
2413 | on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, | |
2414 | strna(netifs_joined)); | |
2415 | ||
10c71c36 YW |
2416 | r = table_print(table, NULL); |
2417 | if (r < 0) | |
4b6607d9 | 2418 | return table_log_print_error(r); |
10c71c36 YW |
2419 | |
2420 | return show_logs(NULL); | |
0070333f LP |
2421 | } |
2422 | ||
266b5389 | 2423 | static int link_status(int argc, char *argv[], void *userdata) { |
445f0d8b | 2424 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
4afd3348 | 2425 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b147503e | 2426 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
172353b1 | 2427 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
b48e277f | 2428 | int r, c; |
ee8c4568 | 2429 | |
445f0d8b MY |
2430 | r = acquire_bus(&bus); |
2431 | if (r < 0) | |
2432 | return r; | |
2433 | ||
bfc2b05e YW |
2434 | if (arg_json_format_flags != JSON_FORMAT_OFF) { |
2435 | if (arg_all || argc <= 1) | |
d821e40c | 2436 | return dump_manager_description(bus); |
bfc2b05e | 2437 | else |
d821e40c | 2438 | return dump_link_description(bus, strv_skip(argv, 1)); |
bfc2b05e YW |
2439 | } |
2440 | ||
384c2c32 | 2441 | pager_open(arg_pager_flags); |
0070333f | 2442 | |
1c4baffc | 2443 | r = sd_netlink_open(&rtnl); |
f647962d MS |
2444 | if (r < 0) |
2445 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
f7d68aa8 | 2446 | |
81fd1dd3 TG |
2447 | r = sd_hwdb_new(&hwdb); |
2448 | if (r < 0) | |
2449 | log_debug_errno(r, "Failed to open hardware database: %m"); | |
69fb1176 | 2450 | |
b147503e | 2451 | if (arg_all) |
335dd8ba | 2452 | c = acquire_link_info(bus, rtnl, NULL, &links); |
b147503e | 2453 | else if (argc <= 1) |
0070333f | 2454 | return system_status(rtnl, hwdb); |
b147503e | 2455 | else |
335dd8ba | 2456 | c = acquire_link_info(bus, rtnl, argv + 1, &links); |
b147503e LP |
2457 | if (c < 0) |
2458 | return c; | |
ee8c4568 | 2459 | |
8fff78a1 MY |
2460 | r = 0; |
2461 | ||
2462 | bool first = true; | |
2463 | FOREACH_ARRAY(i, links, c) { | |
2464 | if (!first) | |
2465 | putchar('\n'); | |
ee8c4568 | 2466 | |
8fff78a1 MY |
2467 | RET_GATHER(r, link_status_one(bus, rtnl, hwdb, i)); |
2468 | ||
2469 | first = false; | |
ee8c4568 LP |
2470 | } |
2471 | ||
8fff78a1 | 2472 | return r; |
ee8c4568 LP |
2473 | } |
2474 | ||
34437b4f LP |
2475 | static char *lldp_capabilities_to_string(uint16_t x) { |
2476 | static const char characters[] = { | |
2477 | 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm', | |
2478 | }; | |
2479 | char *ret; | |
2480 | unsigned i; | |
49699bac | 2481 | |
34437b4f LP |
2482 | ret = new(char, ELEMENTSOF(characters) + 1); |
2483 | if (!ret) | |
49699bac SS |
2484 | return NULL; |
2485 | ||
34437b4f LP |
2486 | for (i = 0; i < ELEMENTSOF(characters); i++) |
2487 | ret[i] = (x & (1U << i)) ? characters[i] : '.'; | |
49699bac | 2488 | |
34437b4f LP |
2489 | ret[i] = 0; |
2490 | return ret; | |
49699bac SS |
2491 | } |
2492 | ||
4c3160f1 | 2493 | static void lldp_capabilities_legend(uint16_t x) { |
b48e277f | 2494 | unsigned cols = columns(); |
404d53a9 | 2495 | static const char* const table[] = { |
4c3160f1 ZJS |
2496 | "o - Other", |
2497 | "p - Repeater", | |
2498 | "b - Bridge", | |
2499 | "w - WLAN Access Point", | |
2500 | "r - Router", | |
2501 | "t - Telephone", | |
2502 | "d - DOCSIS cable device", | |
2503 | "a - Station", | |
2504 | "c - Customer VLAN", | |
2505 | "s - Service VLAN", | |
2506 | "m - Two-port MAC Relay (TPMR)", | |
2507 | }; | |
2508 | ||
2509 | if (x == 0) | |
2510 | return; | |
2511 | ||
2512 | printf("\nCapability Flags:\n"); | |
b48e277f | 2513 | for (unsigned w = 0, i = 0; i < ELEMENTSOF(table); i++) |
4c3160f1 ZJS |
2514 | if (x & (1U << i) || arg_all) { |
2515 | bool newline; | |
2516 | ||
2517 | newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols; | |
2518 | if (newline) | |
2519 | w = 0; | |
2520 | w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]); | |
2521 | } | |
2522 | puts(""); | |
2523 | } | |
2524 | ||
49699bac | 2525 | static int link_lldp_status(int argc, char *argv[], void *userdata) { |
b147503e | 2526 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
172353b1 | 2527 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
658e9106 | 2528 | _cleanup_(table_unrefp) Table *table = NULL; |
b48e277f | 2529 | int r, c, m = 0; |
4c3160f1 | 2530 | uint16_t all = 0; |
658e9106 | 2531 | TableCell *cell; |
b147503e LP |
2532 | |
2533 | r = sd_netlink_open(&rtnl); | |
2534 | if (r < 0) | |
2535 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
49699bac | 2536 | |
335dd8ba | 2537 | c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
49699bac | 2538 | if (c < 0) |
7d367b45 LP |
2539 | return c; |
2540 | ||
384c2c32 | 2541 | pager_open(arg_pager_flags); |
49699bac | 2542 | |
4252171a | 2543 | table = table_new("link", |
27cd31c0 SE |
2544 | "chassis-id", |
2545 | "system-name", | |
4252171a | 2546 | "caps", |
27cd31c0 SE |
2547 | "port-id", |
2548 | "port-description"); | |
658e9106 | 2549 | if (!table) |
bd17fa8c | 2550 | return log_oom(); |
658e9106 | 2551 | |
a42d9490 YW |
2552 | if (arg_full) |
2553 | table_set_width(table, 0); | |
2554 | ||
658e9106 YW |
2555 | table_set_header(table, arg_legend); |
2556 | ||
658e9106 YW |
2557 | assert_se(cell = table_get_cell(table, 0, 3)); |
2558 | table_set_minimum_width(table, cell, 11); | |
38747657 | 2559 | table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
658e9106 | 2560 | |
7db07767 | 2561 | FOREACH_ARRAY(link, links, c) { |
34437b4f | 2562 | _cleanup_fclose_ FILE *f = NULL; |
49699bac | 2563 | |
7db07767 | 2564 | r = open_lldp_neighbors(link->ifindex, &f); |
837f57da LP |
2565 | if (r == -ENOENT) |
2566 | continue; | |
2567 | if (r < 0) { | |
7db07767 | 2568 | log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", link->ifindex); |
34437b4f LP |
2569 | continue; |
2570 | } | |
49699bac | 2571 | |
34437b4f | 2572 | for (;;) { |
e2835111 | 2573 | const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL; |
34437b4f | 2574 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; |
27cd31c0 | 2575 | _cleanup_free_ char *capabilities = NULL; |
34437b4f | 2576 | uint16_t cc; |
49699bac | 2577 | |
837f57da | 2578 | r = next_lldp_neighbor(f, &n); |
34437b4f | 2579 | if (r < 0) { |
837f57da | 2580 | log_warning_errno(r, "Failed to read neighbor data: %m"); |
34437b4f | 2581 | break; |
49699bac | 2582 | } |
837f57da LP |
2583 | if (r == 0) |
2584 | break; | |
34437b4f LP |
2585 | |
2586 | (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id); | |
2587 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
2588 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); | |
2589 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
2590 | ||
4c3160f1 | 2591 | if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) { |
34437b4f | 2592 | capabilities = lldp_capabilities_to_string(cc); |
4c3160f1 ZJS |
2593 | all |= cc; |
2594 | } | |
34437b4f | 2595 | |
658e9106 | 2596 | r = table_add_many(table, |
7db07767 | 2597 | TABLE_STRING, link->name, |
38747657 YW |
2598 | TABLE_STRING, chassis_id, |
2599 | TABLE_STRING, system_name, | |
2600 | TABLE_STRING, capabilities, | |
2601 | TABLE_STRING, port_id, | |
2602 | TABLE_STRING, port_description); | |
658e9106 | 2603 | if (r < 0) |
bd17fa8c | 2604 | return table_log_add_error(r); |
b147503e LP |
2605 | |
2606 | m++; | |
49699bac SS |
2607 | } |
2608 | } | |
2609 | ||
658e9106 YW |
2610 | r = table_print(table, NULL); |
2611 | if (r < 0) | |
4b6607d9 | 2612 | return table_log_print_error(r); |
658e9106 | 2613 | |
4c3160f1 ZJS |
2614 | if (arg_legend) { |
2615 | lldp_capabilities_legend(all); | |
2616 | printf("\n%i neighbors listed.\n", m); | |
2617 | } | |
49699bac SS |
2618 | |
2619 | return 0; | |
2620 | } | |
2621 | ||
9cd8c766 SS |
2622 | static int link_delete_send_message(sd_netlink *rtnl, int index) { |
2623 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2624 | int r; | |
2625 | ||
2626 | assert(rtnl); | |
43449ca3 | 2627 | assert(index >= 0); |
9cd8c766 SS |
2628 | |
2629 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index); | |
2630 | if (r < 0) | |
2631 | return rtnl_log_create_error(r); | |
2632 | ||
2633 | r = sd_netlink_call(rtnl, req, 0, NULL); | |
2634 | if (r < 0) | |
2635 | return r; | |
2636 | ||
2637 | return 0; | |
2638 | } | |
2639 | ||
c30ffcee SS |
2640 | static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) { |
2641 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2642 | int r; | |
2643 | ||
2644 | assert(rtnl); | |
43449ca3 | 2645 | assert(index >= 0); |
c30ffcee SS |
2646 | |
2647 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index); | |
2648 | if (r < 0) | |
2649 | return rtnl_log_create_error(r); | |
2650 | ||
2651 | if (streq(command, "up")) | |
2652 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
2653 | else | |
2654 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
2655 | if (r < 0) | |
2656 | return log_error_errno(r, "Could not set link flags: %m"); | |
2657 | ||
2658 | r = sd_netlink_call(rtnl, req, 0, NULL); | |
2659 | if (r < 0) | |
2660 | return r; | |
2661 | ||
2662 | return 0; | |
2663 | } | |
2664 | ||
2665 | static int link_up_down(int argc, char *argv[], void *userdata) { | |
2666 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2667 | _cleanup_set_free_ Set *indexes = NULL; | |
b48e277f | 2668 | int index, r; |
c30ffcee SS |
2669 | void *p; |
2670 | ||
2671 | r = sd_netlink_open(&rtnl); | |
2672 | if (r < 0) | |
2673 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2674 | ||
2675 | indexes = set_new(NULL); | |
2676 | if (!indexes) | |
2677 | return log_oom(); | |
2678 | ||
b48e277f | 2679 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2680 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
c30ffcee SS |
2681 | if (index < 0) |
2682 | return index; | |
2683 | ||
2684 | r = set_put(indexes, INT_TO_PTR(index)); | |
2685 | if (r < 0) | |
2686 | return log_oom(); | |
2687 | } | |
2688 | ||
90e74a66 | 2689 | SET_FOREACH(p, indexes) { |
c30ffcee SS |
2690 | index = PTR_TO_INT(p); |
2691 | r = link_up_down_send_message(rtnl, argv[0], index); | |
01afd0f7 | 2692 | if (r < 0) |
6f50c94d | 2693 | return log_error_errno(r, "Failed to bring %s interface %s: %m", |
01afd0f7 | 2694 | argv[0], FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX)); |
c30ffcee SS |
2695 | } |
2696 | ||
2697 | return r; | |
2698 | } | |
2699 | ||
9cd8c766 SS |
2700 | static int link_delete(int argc, char *argv[], void *userdata) { |
2701 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2702 | _cleanup_set_free_ Set *indexes = NULL; | |
b48e277f | 2703 | int index, r; |
38b9af61 | 2704 | void *p; |
9cd8c766 SS |
2705 | |
2706 | r = sd_netlink_open(&rtnl); | |
2707 | if (r < 0) | |
2708 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2709 | ||
2710 | indexes = set_new(NULL); | |
2711 | if (!indexes) | |
2712 | return log_oom(); | |
2713 | ||
b48e277f | 2714 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2715 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2716 | if (index < 0) |
2717 | return index; | |
9cd8c766 SS |
2718 | |
2719 | r = set_put(indexes, INT_TO_PTR(index)); | |
2720 | if (r < 0) | |
2721 | return log_oom(); | |
2722 | } | |
2723 | ||
90e74a66 | 2724 | SET_FOREACH(p, indexes) { |
d56d6cb8 YW |
2725 | index = PTR_TO_INT(p); |
2726 | r = link_delete_send_message(rtnl, index); | |
01afd0f7 | 2727 | if (r < 0) |
e4857ee2 | 2728 | return log_error_errno(r, "Failed to delete interface %s: %m", |
01afd0f7 | 2729 | FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX)); |
9cd8c766 SS |
2730 | } |
2731 | ||
2732 | return r; | |
2733 | } | |
2734 | ||
308e7dfd YW |
2735 | static int link_renew_one(sd_bus *bus, int index, const char *name) { |
2736 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2737 | int r; | |
2738 | ||
43449ca3 MY |
2739 | assert(bus); |
2740 | assert(index >= 0); | |
2741 | assert(name); | |
2742 | ||
8a048c8c | 2743 | r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index); |
308e7dfd YW |
2744 | if (r < 0) |
2745 | return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s", | |
2746 | name, bus_error_message(&error, r)); | |
2747 | ||
2748 | return 0; | |
2749 | } | |
2750 | ||
2751 | static int link_renew(int argc, char *argv[], void *userdata) { | |
445f0d8b | 2752 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
f7581ed6 | 2753 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
e9b7408e | 2754 | int r; |
308e7dfd | 2755 | |
445f0d8b MY |
2756 | r = acquire_bus(&bus); |
2757 | if (r < 0) | |
2758 | return r; | |
2759 | ||
e9b7408e MY |
2760 | r = 0; |
2761 | ||
b48e277f | 2762 | for (int i = 1; i < argc; i++) { |
e9b7408e MY |
2763 | int index; |
2764 | ||
f6e49154 | 2765 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2766 | if (index < 0) |
2767 | return index; | |
308e7dfd | 2768 | |
e9b7408e | 2769 | RET_GATHER(r, link_renew_one(bus, index, argv[i])); |
308e7dfd YW |
2770 | } |
2771 | ||
e9b7408e | 2772 | return r; |
308e7dfd YW |
2773 | } |
2774 | ||
3efdd6af SS |
2775 | static int link_force_renew_one(sd_bus *bus, int index, const char *name) { |
2776 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2777 | int r; | |
2778 | ||
43449ca3 MY |
2779 | assert(bus); |
2780 | assert(index >= 0); | |
2781 | assert(name); | |
2782 | ||
8a048c8c | 2783 | r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index); |
3efdd6af SS |
2784 | if (r < 0) |
2785 | return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s", | |
2786 | name, bus_error_message(&error, r)); | |
2787 | ||
2788 | return 0; | |
2789 | } | |
2790 | ||
2791 | static int link_force_renew(int argc, char *argv[], void *userdata) { | |
445f0d8b | 2792 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
3efdd6af | 2793 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b48e277f | 2794 | int k = 0, r; |
3efdd6af | 2795 | |
445f0d8b MY |
2796 | r = acquire_bus(&bus); |
2797 | if (r < 0) | |
2798 | return r; | |
2799 | ||
b48e277f | 2800 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2801 | int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
3efdd6af SS |
2802 | if (index < 0) |
2803 | return index; | |
2804 | ||
2805 | r = link_force_renew_one(bus, index, argv[i]); | |
2806 | if (r < 0 && k >= 0) | |
2807 | k = r; | |
2808 | } | |
2809 | ||
2810 | return k; | |
2811 | } | |
2812 | ||
a227674c | 2813 | static int verb_reload(int argc, char *argv[], void *userdata) { |
445f0d8b | 2814 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a227674c | 2815 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a227674c YW |
2816 | int r; |
2817 | ||
445f0d8b MY |
2818 | r = acquire_bus(&bus); |
2819 | if (r < 0) | |
2820 | return r; | |
2821 | ||
8a048c8c | 2822 | r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL); |
a227674c | 2823 | if (r < 0) |
1246d097 | 2824 | return log_error_errno(r, "Failed to reload network settings: %s", bus_error_message(&error, r)); |
a227674c YW |
2825 | |
2826 | return 0; | |
2827 | } | |
2828 | ||
8dc85c5e | 2829 | static int verb_reconfigure(int argc, char *argv[], void *userdata) { |
445f0d8b | 2830 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
8dc85c5e | 2831 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f7581ed6 | 2832 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
8dc85c5e | 2833 | _cleanup_set_free_ Set *indexes = NULL; |
b48e277f | 2834 | int index, r; |
8dc85c5e YW |
2835 | void *p; |
2836 | ||
445f0d8b MY |
2837 | r = acquire_bus(&bus); |
2838 | if (r < 0) | |
2839 | return r; | |
2840 | ||
8dc85c5e YW |
2841 | indexes = set_new(NULL); |
2842 | if (!indexes) | |
2843 | return log_oom(); | |
2844 | ||
b48e277f | 2845 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2846 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2847 | if (index < 0) |
2848 | return index; | |
8dc85c5e YW |
2849 | |
2850 | r = set_put(indexes, INT_TO_PTR(index)); | |
2851 | if (r < 0) | |
2852 | return log_oom(); | |
2853 | } | |
2854 | ||
90e74a66 | 2855 | SET_FOREACH(p, indexes) { |
8dc85c5e | 2856 | index = PTR_TO_INT(p); |
8a048c8c | 2857 | r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index); |
01afd0f7 | 2858 | if (r < 0) |
1246d097 YW |
2859 | return log_error_errno(r, "Failed to reconfigure network interface %s: %s", |
2860 | FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX), | |
2861 | bus_error_message(&error, r)); | |
8dc85c5e YW |
2862 | } |
2863 | ||
2864 | return 0; | |
2865 | } | |
2866 | ||
37ec0fdd LP |
2867 | static int help(void) { |
2868 | _cleanup_free_ char *link = NULL; | |
2869 | int r; | |
2870 | ||
2871 | r = terminal_urlify_man("networkctl", "1", &link); | |
2872 | if (r < 0) | |
2873 | return log_oom(); | |
2874 | ||
353b2baa LP |
2875 | printf("%s [OPTIONS...] COMMAND\n\n" |
2876 | "%sQuery and control the networking subsystem.%s\n" | |
a459b24f | 2877 | "\nCommands:\n" |
8dc85c5e YW |
2878 | " list [PATTERN...] List links\n" |
2879 | " status [PATTERN...] Show link status\n" | |
2880 | " lldp [PATTERN...] Show LLDP neighbors\n" | |
2881 | " label Show current address label entries in the kernel\n" | |
2882 | " delete DEVICES... Delete virtual netdevs\n" | |
c30ffcee SS |
2883 | " up DEVICES... Bring devices up\n" |
2884 | " down DEVICES... Bring devices down\n" | |
8dc85c5e | 2885 | " renew DEVICES... Renew dynamic configurations\n" |
3efdd6af | 2886 | " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n" |
8dc85c5e YW |
2887 | " reconfigure DEVICES... Reconfigure interfaces\n" |
2888 | " reload Reload .network and .netdev files\n" | |
96bab8fd MY |
2889 | " edit FILES|DEVICES... Edit network configuration files\n" |
2890 | " cat FILES|DEVICES... Show network configuration files\n" | |
28bb29cb MY |
2891 | " mask FILES... Mask network configuration files\n" |
2892 | " unmask FILES... Unmask network configuration files\n" | |
353b2baa | 2893 | "\nOptions:\n" |
8dc85c5e YW |
2894 | " -h --help Show this help\n" |
2895 | " --version Show package version\n" | |
2896 | " --no-pager Do not pipe output into a pager\n" | |
2897 | " --no-legend Do not show the headers and footers\n" | |
2898 | " -a --all Show status for all links\n" | |
ba669952 | 2899 | " -s --stats Show detailed link statistics\n" |
10c71c36 YW |
2900 | " -l --full Do not ellipsize output\n" |
2901 | " -n --lines=INTEGER Number of journal entries to show\n" | |
bfc2b05e YW |
2902 | " --json=pretty|short|off\n" |
2903 | " Generate JSON output\n" | |
96bab8fd MY |
2904 | " --no-reload Do not reload systemd-networkd or systemd-udevd\n" |
2905 | " after editing network config\n" | |
2906 | " --drop-in=NAME Edit specified drop-in instead of main config file\n" | |
2b98926f | 2907 | " --runtime Edit runtime config files\n" |
bc556335 DDM |
2908 | "\nSee the %s for details.\n", |
2909 | program_invocation_short_name, | |
2910 | ansi_highlight(), | |
2911 | ansi_normal(), | |
2912 | link); | |
37ec0fdd LP |
2913 | |
2914 | return 0; | |
ee8c4568 LP |
2915 | } |
2916 | ||
2917 | static int parse_argv(int argc, char *argv[]) { | |
ee8c4568 LP |
2918 | enum { |
2919 | ARG_VERSION = 0x100, | |
2920 | ARG_NO_PAGER, | |
2921 | ARG_NO_LEGEND, | |
bfc2b05e | 2922 | ARG_JSON, |
96bab8fd MY |
2923 | ARG_NO_RELOAD, |
2924 | ARG_DROP_IN, | |
2b98926f | 2925 | ARG_RUNTIME, |
ee8c4568 LP |
2926 | }; |
2927 | ||
2928 | static const struct option options[] = { | |
2929 | { "help", no_argument, NULL, 'h' }, | |
2930 | { "version", no_argument, NULL, ARG_VERSION }, | |
2931 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
2932 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
9085f64a | 2933 | { "all", no_argument, NULL, 'a' }, |
a459b24f | 2934 | { "stats", no_argument, NULL, 's' }, |
10c71c36 YW |
2935 | { "full", no_argument, NULL, 'l' }, |
2936 | { "lines", required_argument, NULL, 'n' }, | |
bfc2b05e | 2937 | { "json", required_argument, NULL, ARG_JSON }, |
96bab8fd MY |
2938 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
2939 | { "drop-in", required_argument, NULL, ARG_DROP_IN }, | |
2b98926f | 2940 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
ee8c4568 LP |
2941 | {} |
2942 | }; | |
2943 | ||
bfc2b05e | 2944 | int c, r; |
ee8c4568 LP |
2945 | |
2946 | assert(argc >= 0); | |
2947 | assert(argv); | |
2948 | ||
10c71c36 | 2949 | while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) { |
ee8c4568 LP |
2950 | |
2951 | switch (c) { | |
2952 | ||
2953 | case 'h': | |
37ec0fdd | 2954 | return help(); |
ee8c4568 LP |
2955 | |
2956 | case ARG_VERSION: | |
3f6fd1ba | 2957 | return version(); |
ee8c4568 LP |
2958 | |
2959 | case ARG_NO_PAGER: | |
0221d68a | 2960 | arg_pager_flags |= PAGER_DISABLE; |
ee8c4568 LP |
2961 | break; |
2962 | ||
2963 | case ARG_NO_LEGEND: | |
2964 | arg_legend = false; | |
2965 | break; | |
2966 | ||
96bab8fd MY |
2967 | case ARG_NO_RELOAD: |
2968 | arg_no_reload = true; | |
2969 | break; | |
2970 | ||
2b98926f MY |
2971 | case ARG_RUNTIME: |
2972 | arg_runtime = true; | |
2973 | break; | |
2974 | ||
96bab8fd MY |
2975 | case ARG_DROP_IN: |
2976 | if (isempty(optarg)) | |
2977 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name."); | |
2978 | ||
5de8264b FS |
2979 | if (!endswith(optarg, ".conf")) { |
2980 | char *conf; | |
2981 | ||
2982 | conf = strjoin(optarg, ".conf"); | |
2983 | if (!conf) | |
2984 | return log_oom(); | |
2985 | ||
2986 | free_and_replace(arg_drop_in, conf); | |
2987 | } else { | |
2988 | r = free_and_strdup(&arg_drop_in, optarg); | |
2989 | if (r < 0) | |
2990 | return log_oom(); | |
2991 | } | |
96bab8fd MY |
2992 | |
2993 | if (!filename_is_valid(arg_drop_in)) | |
2994 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2995 | "Invalid drop-in file name '%s'.", arg_drop_in); | |
2996 | ||
2997 | break; | |
2998 | ||
9085f64a LP |
2999 | case 'a': |
3000 | arg_all = true; | |
3001 | break; | |
3002 | ||
a459b24f YW |
3003 | case 's': |
3004 | arg_stats = true; | |
3005 | break; | |
3006 | ||
10c71c36 YW |
3007 | case 'l': |
3008 | arg_full = true; | |
3009 | break; | |
3010 | ||
3011 | case 'n': | |
3012 | if (safe_atou(optarg, &arg_lines) < 0) | |
3013 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3014 | "Failed to parse lines '%s'", optarg); | |
3015 | break; | |
3016 | ||
bfc2b05e YW |
3017 | case ARG_JSON: |
3018 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
3019 | if (r <= 0) | |
3020 | return r; | |
3021 | break; | |
3022 | ||
ee8c4568 LP |
3023 | case '?': |
3024 | return -EINVAL; | |
3025 | ||
3026 | default: | |
04499a70 | 3027 | assert_not_reached(); |
ee8c4568 LP |
3028 | } |
3029 | } | |
3030 | ||
3031 | return 1; | |
3032 | } | |
3033 | ||
445f0d8b | 3034 | static int networkctl_main(int argc, char *argv[]) { |
15c3626e | 3035 | static const Verb verbs[] = { |
d260875f MY |
3036 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_links }, |
3037 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, link_status }, | |
3038 | { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status }, | |
3039 | { "label", 1, 1, 0, list_address_labels }, | |
3040 | { "delete", 2, VERB_ANY, 0, link_delete }, | |
3041 | { "up", 2, VERB_ANY, 0, link_up_down }, | |
3042 | { "down", 2, VERB_ANY, 0, link_up_down }, | |
3043 | { "renew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_renew }, | |
3044 | { "forcerenew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_force_renew }, | |
3045 | { "reconfigure", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_reconfigure }, | |
3046 | { "reload", 1, 1, VERB_ONLINE_ONLY, verb_reload }, | |
96bab8fd MY |
3047 | { "edit", 2, VERB_ANY, 0, verb_edit }, |
3048 | { "cat", 2, VERB_ANY, 0, verb_cat }, | |
28bb29cb MY |
3049 | { "mask", 2, VERB_ANY, 0, verb_mask }, |
3050 | { "unmask", 2, VERB_ANY, 0, verb_unmask }, | |
266b5389 | 3051 | {} |
ee8c4568 LP |
3052 | }; |
3053 | ||
445f0d8b | 3054 | return dispatch_verb(argc, argv, verbs, NULL); |
58fb3678 LP |
3055 | } |
3056 | ||
4e2ca442 | 3057 | static int run(int argc, char* argv[]) { |
ee8c4568 LP |
3058 | int r; |
3059 | ||
d2acb93d | 3060 | log_setup(); |
ee8c4568 | 3061 | |
955fc5d8 YW |
3062 | sigbus_install(); |
3063 | ||
ee8c4568 LP |
3064 | r = parse_argv(argc, argv); |
3065 | if (r <= 0) | |
4e2ca442 | 3066 | return r; |
ee8c4568 | 3067 | |
445f0d8b | 3068 | return networkctl_main(argc, argv); |
ee8c4568 | 3069 | } |
4e2ca442 ZJS |
3070 | |
3071 | DEFINE_MAIN_FUNCTION(run); |