Debian Codegraph @

src/network/networkctl-status-link.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-device.h"
4 #include "sd-dhcp-client-id.h"
5 #include "sd-dhcp-protocol.h"
6 #include "sd-hwdb.h"
7 #include "sd-netlink.h"
8 #include "sd-network.h"
9 #include "sd-varlink.h"
10
11 #include "alloc-util.h"
12 #include "bond-util.h"
13 #include "bridge-util.h"
14 #include "errno-util.h"
15 #include "escape.h"
16 #include "extract-word.h"
17 #include "format-table.h"
18 #include "format-util.h"
19 #include "geneve-util.h"
20 #include "glyph-util.h"
21 #include "iovec-util.h"
22 #include "ipvlan-util.h"
23 #include "json-util.h"
24 #include "macvlan-util.h"
25 #include "netif-util.h"
26 #include "networkctl.h"
27 #include "networkctl-description.h"
28 #include "networkctl-dump-util.h"
29 #include "networkctl-journal.h"
30 #include "networkctl-link-info.h"
31 #include "networkctl-lldp.h"
32 #include "networkctl-status-link.h"
33 #include "networkctl-status-system.h"
34 #include "networkctl-util.h"
35 #include "stdio-util.h"
36 #include "string-util.h"
37 #include "strv.h"
38 #include "time-util.h"
39 #include "udev-util.h"
40
41 typedef struct LeaseInfo {
42 const char *address;
43 struct iovec client_id;
44 } LeaseInfo;
45
46 static void lease_info_done(LeaseInfo *p) {
47 assert(p);
48
49 iovec_done(&p->client_id);
50 }
51
52 static int dump_dhcp_leases(Table *table, const char *prefix, const LinkInfo *link) {
53 _cleanup_strv_free_ char **buf = NULL;
54 int r;
55
56 assert(table);
57 assert(prefix);
58 assert(link);
59
60 sd_json_variant *leases;
61 r = json_variant_find_object(link->description, STRV_MAKE("Interface", "DHCPServer", "Leases"), &leases);
62 if (r == -ENODATA)
63 return 0;
64 if (r < 0)
65 return r;
66
67 static const sd_json_dispatch_field dispatch_table[] = {
68 { "AddressString", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LeaseInfo, address), SD_JSON_MANDATORY },
69 { "ClientId", SD_JSON_VARIANT_ARRAY, json_dispatch_byte_array_iovec, offsetof(LeaseInfo, client_id), 0 },
70 {}
71 };
72
73 sd_json_variant *lease;
74 JSON_VARIANT_ARRAY_FOREACH(lease, leases) {
75 _cleanup_(lease_info_done) LeaseInfo info = {};
76 _cleanup_free_ char *client_id = NULL;
77
78 r = sd_json_dispatch(lease, dispatch_table, SD_JSON_LOG | SD_JSON_WARNING | SD_JSON_ALLOW_EXTENSIONS, &info);
79 if (r < 0)
80 continue;
81
82 if (info.client_id.iov_len > 0)
83 (void) sd_dhcp_client_id_to_string_from_raw(info.client_id.iov_base, info.client_id.iov_len, &client_id);
84
85 r = strv_extendf(&buf, "%s%s%s%s",
86 info.address,
87 client_id ? " (to " : "",
88 strempty(client_id),
89 client_id ? ")" : "");
90 if (r < 0)
91 return log_oom();
92 }
93
94 if (strv_isempty(buf)) {
95 r = strv_extendf(&buf, "none");
96 if (r < 0)
97 return log_oom();
98 }
99
100 return dump_list(table, prefix, buf);
101 }
102
103 static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
104 int r;
105
106 assert(table);
107 assert(prefix);
108
109 if (!ifindexes)
110 return 0;
111
112 for (unsigned c = 0; ifindexes[c] > 0; c++) {
113 if (c == 0)
114 r = table_add_cell(table, NULL, TABLE_FIELD, prefix);
115 else
116 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
117 if (r < 0)
118 return table_log_add_error(r);
119
120 r = table_add_cell(table, NULL, TABLE_IFINDEX, &ifindexes[c]);
121 if (r < 0)
122 return table_log_add_error(r);
123 }
124
125 return 0;
126 }
127
128 #define DUMP_STATS_ONE(name, val_name) \
129 ({ \
130 r = table_add_cell(table, NULL, TABLE_FIELD, name); \
131 if (r < 0) \
132 return table_log_add_error(r); \
133 r = table_add_cell(table, NULL, \
134 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
135 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
136 if (r < 0) \
137 return table_log_add_error(r); \
138 })
139
140 static int dump_statistics(Table *table, const LinkInfo *info) {
141 int r;
142
143 assert(table);
144 assert(info);
145
146 if (!arg_stats)
147 return 0;
148
149 if (!info->has_stats64 && !info->has_stats)
150 return 0;
151
152 DUMP_STATS_ONE("Rx Packets", rx_packets);
153 DUMP_STATS_ONE("Tx Packets", tx_packets);
154 DUMP_STATS_ONE("Rx Bytes", rx_bytes);
155 DUMP_STATS_ONE("Tx Bytes", tx_bytes);
156 DUMP_STATS_ONE("Rx Errors", rx_errors);
157 DUMP_STATS_ONE("Tx Errors", tx_errors);
158 DUMP_STATS_ONE("Rx Dropped", rx_dropped);
159 DUMP_STATS_ONE("Tx Dropped", tx_dropped);
160 DUMP_STATS_ONE("Multicast Packets", multicast);
161 DUMP_STATS_ONE("Collisions", collisions);
162
163 return 0;
164 }
165
166 static int dump_hw_address(Table *table, sd_hwdb *hwdb, const char *field, const struct hw_addr_data *addr) {
167 _cleanup_free_ char *description = NULL;
168 int r;
169
170 assert(table);
171 assert(field);
172 assert(addr);
173
174 if (addr->length == ETH_ALEN)
175 (void) ieee_oui(hwdb, &addr->ether, &description);
176
177 r = table_add_cell(table, NULL, TABLE_FIELD, field);
178 if (r < 0)
179 return table_log_add_error(r);
180
181 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
182 HW_ADDR_TO_STR(addr),
183 description ? " (" : "",
184 strempty(description),
185 description ? ")" : "");
186 if (r < 0)
187 return table_log_add_error(r);
188
189 return 0;
190 }
191
192 static int table_add_string_line(Table *table, const char *key, const char *value) {
193 int r;
194
195 assert(table);
196 assert(key);
197
198 if (isempty(value))
199 return 0;
200
201 r = table_add_many(table,
202 TABLE_FIELD, key,
203 TABLE_STRING, value);
204 if (r < 0)
205 return table_log_add_error(r);
206
207 return 0;
208 }
209
210 static int format_config_files(char ***files, const char *main_config) {
211 assert(files);
212
213 STRV_FOREACH(d, *files) {
214 _cleanup_free_ char *s = NULL;
215 Glyph tree = *(d + 1) == NULL ? GLYPH_TREE_RIGHT : GLYPH_TREE_BRANCH;
216
217 s = strjoin(glyph(tree), *d);
218 if (!s)
219 return log_oom();
220
221 free_and_replace(*d, s);
222 }
223
224 if (strv_prepend(files, main_config) < 0)
225 return log_oom();
226
227 return 0;
228 }
229
230 static int link_status_one(
231 sd_netlink *rtnl,
232 sd_hwdb *hwdb,
233 sd_varlink *vl,
234 const LinkInfo *info) {
235
236 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL,
237 **route_domains = NULL, **link_dropins = NULL, **network_dropins = NULL, **netdev_dropins = NULL;
238 _cleanup_free_ char *t = NULL, *network = NULL, *netdev = NULL, *iaid = NULL, *duid = NULL, *captive_portal = NULL,
239 *setup_state = NULL, *operational_state = NULL, *online_state = NULL, *activation_policy = NULL;
240 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL,
241 *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup, *on_color_online;
242 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
243 _cleanup_(table_unrefp) Table *table = NULL;
244 int r;
245
246 assert(rtnl);
247 assert(vl);
248 assert(info);
249
250 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
251 operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational);
252
253 (void) sd_network_link_get_online_state(info->ifindex, &online_state);
254 online_state_to_color(online_state, &on_color_online, NULL);
255
256 (void) sd_network_link_get_setup_state(info->ifindex, &setup_state);
257 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
258
259 (void) sd_network_link_get_dns(info->ifindex, &dns);
260 (void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
261 (void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
262 (void) sd_network_link_get_ntp(info->ifindex, &ntp);
263 (void) sd_network_link_get_sip(info->ifindex, &sip);
264 (void) sd_network_link_get_captive_portal(info->ifindex, &captive_portal);
265 (void) sd_network_link_get_network_file(info->ifindex, &network);
266 (void) sd_network_link_get_network_file_dropins(info->ifindex, &network_dropins);
267 (void) sd_network_link_get_netdev_file(info->ifindex, &netdev);
268 (void) sd_network_link_get_netdev_file_dropins(info->ifindex, &netdev_dropins);
269 (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
270 (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
271 (void) sd_network_link_get_activation_policy(info->ifindex, &activation_policy);
272
273 if (info->sd_device) {
274 const char *joined;
275
276 (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link);
277
278 if (sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) {
279 r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE);
280 if (r < 0)
281 return r;
282 }
283
284 (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver);
285 (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path);
286 (void) device_get_vendor_string(info->sd_device, &vendor);
287 (void) device_get_model_string(info->sd_device, &model);
288 }
289
290 r = net_get_type_string(info->sd_device, info->iftype, &t);
291 if (r == -ENOMEM)
292 return log_oom();
293
294 r = format_config_files(&network_dropins, network);
295 if (r < 0)
296 return r;
297
298 r = format_config_files(&link_dropins, link);
299 if (r < 0)
300 return r;
301
302 r = format_config_files(&netdev_dropins, netdev);
303 if (r < 0)
304 return r;
305
306 table = table_new_vertical();
307 if (!table)
308 return log_oom();
309
310 if (arg_full)
311 table_set_width(table, 0);
312
313 /* Config files and basic states. */
314 if (netdev_dropins) {
315 r = table_add_many(table,
316 TABLE_FIELD, "NetDev File",
317 TABLE_STRV, netdev_dropins);
318 if (r < 0)
319 return table_log_add_error(r);
320 }
321
322 r = table_add_many(table,
323 TABLE_FIELD, "Link File",
324 TABLE_STRV, link_dropins ?: STRV_MAKE("n/a"),
325 TABLE_FIELD, "Network File",
326 TABLE_STRV, network_dropins ?: STRV_MAKE("n/a"),
327 TABLE_FIELD, "State");
328 if (r < 0)
329 return table_log_add_error(r);
330
331 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
332 on_color_operational, strna(operational_state), off_color_operational,
333 on_color_setup, setup_state ?: "unmanaged", off_color_setup);
334 if (r < 0)
335 return table_log_add_error(r);
336
337 r = table_add_many(table,
338 TABLE_FIELD, "Online state",
339 TABLE_STRING, online_state ?: "unknown",
340 TABLE_SET_COLOR, on_color_online);
341 if (r < 0)
342 return table_log_add_error(r);
343
344 r = table_add_string_line(table, "Type", t);
345 if (r < 0)
346 return r;
347
348 r = table_add_string_line(table, "Kind", info->netdev_kind);
349 if (r < 0)
350 return r;
351
352 r = table_add_string_line(table, "Path", path);
353 if (r < 0)
354 return r;
355
356 r = table_add_string_line(table, "Driver", driver);
357 if (r < 0)
358 return r;
359
360 r = table_add_string_line(table, "Vendor", vendor);
361 if (r < 0)
362 return r;
363
364 r = table_add_string_line(table, "Model", model);
365 if (r < 0)
366 return r;
367
368 strv_sort(info->alternative_names);
369 r = dump_list(table, "Alternative Names", info->alternative_names);
370 if (r < 0)
371 return r;
372
373 if (info->has_hw_address) {
374 r = dump_hw_address(table, hwdb, "Hardware Address", &info->hw_address);
375 if (r < 0)
376 return r;
377 }
378
379 if (info->has_permanent_hw_address) {
380 r = dump_hw_address(table, hwdb, "Permanent Hardware Address", &info->permanent_hw_address);
381 if (r < 0)
382 return r;
383 }
384
385 if (info->mtu > 0) {
386 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
387
388 xsprintf(min_str, "%" PRIu32, info->min_mtu);
389 xsprintf(max_str, "%" PRIu32, info->max_mtu);
390
391 r = table_add_cell(table, NULL, TABLE_FIELD, "MTU");
392 if (r < 0)
393 return table_log_add_error(r);
394
395 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
396 info->mtu,
397 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
398 info->min_mtu > 0 ? "min: " : "",
399 info->min_mtu > 0 ? min_str : "",
400 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
401 info->max_mtu > 0 ? "max: " : "",
402 info->max_mtu > 0 ? max_str : "",
403 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
404 if (r < 0)
405 return table_log_add_error(r);
406 }
407
408 r = table_add_string_line(table, "QDisc", info->qdisc);
409 if (r < 0)
410 return r;
411
412 if (info->master > 0) {
413 r = table_add_many(table,
414 TABLE_FIELD, "Master",
415 TABLE_IFINDEX, info->master);
416 if (r < 0)
417 return table_log_add_error(r);
418 }
419
420 if (info->has_ipv6_address_generation_mode) {
421 static const struct {
422 const char *mode;
423 } mode_table[] = {
424 { "eui64" },
425 { "none" },
426 { "stable-privacy" },
427 { "random" },
428 };
429
430 r = table_add_many(table,
431 TABLE_FIELD, "IPv6 Address Generation Mode",
432 TABLE_STRING, mode_table[info->addr_gen_mode]);
433 if (r < 0)
434 return table_log_add_error(r);
435 }
436
437 if (streq_ptr(info->netdev_kind, "bridge")) {
438 r = table_add_many(table,
439 TABLE_FIELD, "Forward Delay",
440 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay),
441 TABLE_FIELD, "Hello Time",
442 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time),
443 TABLE_FIELD, "Max Age",
444 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age),
445 TABLE_FIELD, "Ageing Time",
446 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time),
447 TABLE_FIELD, "Priority",
448 TABLE_UINT16, info->priority,
449 TABLE_FIELD, "STP",
450 TABLE_BOOLEAN, info->stp_state > 0,
451 TABLE_FIELD, "Multicast IGMP Version",
452 TABLE_UINT8, info->mcast_igmp_version,
453 TABLE_FIELD, "Cost",
454 TABLE_UINT32, info->cost);
455 if (r < 0)
456 return table_log_add_error(r);
457
458 if (info->has_fdb_learned) {
459 r = table_add_many(table,
460 TABLE_FIELD, "FDB Learned",
461 TABLE_UINT32, info->fdb_n_learned,
462 TABLE_FIELD, "FDB Max Learned",
463 TABLE_UINT32, info->fdb_max_learned);
464 if (r < 0)
465 return table_log_add_error(r);
466 }
467
468 if (info->port_state <= BR_STATE_BLOCKING) {
469 r = table_add_many(table,
470 TABLE_FIELD, "Port State",
471 TABLE_STRING, bridge_state_to_string(info->port_state));
472 if (r < 0)
473 return table_log_add_error(r);
474 }
475
476 } else if (streq_ptr(info->netdev_kind, "bond")) {
477 r = table_add_many(table,
478 TABLE_FIELD, "Mode",
479 TABLE_STRING, bond_mode_to_string(info->mode),
480 TABLE_FIELD, "Miimon",
481 TABLE_TIMESPAN_MSEC, info->miimon * USEC_PER_MSEC,
482 TABLE_FIELD, "Updelay",
483 TABLE_TIMESPAN_MSEC, info->updelay * USEC_PER_MSEC,
484 TABLE_FIELD, "Downdelay",
485 TABLE_TIMESPAN_MSEC, info->downdelay * USEC_PER_MSEC);
486 if (r < 0)
487 return table_log_add_error(r);
488
489 } else if (streq_ptr(info->netdev_kind, "vxlan")) {
490 char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
491
492 if (info->vxlan_info.vni > 0) {
493 r = table_add_many(table,
494 TABLE_FIELD, "VNI",
495 TABLE_UINT32, info->vxlan_info.vni);
496 if (r < 0)
497 return table_log_add_error(r);
498 }
499
500 if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) {
501 const char *p;
502
503 r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group);
504 if (r <= 0)
505 p = "Remote";
506 else
507 p = "Group";
508
509 r = table_add_many(table,
510 TABLE_FIELD, p,
511 info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.group);
512 if (r < 0)
513 return table_log_add_error(r);
514 }
515
516 if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) {
517 r = table_add_many(table,
518 TABLE_FIELD, "Local",
519 info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.local);
520 if (r < 0)
521 return table_log_add_error(r);
522 }
523
524 if (info->vxlan_info.dest_port > 0) {
525 r = table_add_many(table,
526 TABLE_FIELD, "Destination Port",
527 TABLE_UINT16, be16toh(info->vxlan_info.dest_port));
528 if (r < 0)
529 return table_log_add_error(r);
530 }
531
532 if (info->vxlan_info.link > 0) {
533 r = table_add_many(table,
534 TABLE_FIELD, "Underlying Device",
535 TABLE_IFINDEX, info->vxlan_info.link);
536 if (r < 0)
537 return table_log_add_error(r);
538 }
539
540 r = table_add_many(table,
541 TABLE_FIELD, "Learning",
542 TABLE_BOOLEAN, info->vxlan_info.learning,
543 TABLE_FIELD, "RSC",
544 TABLE_BOOLEAN, info->vxlan_info.rsc,
545 TABLE_FIELD, "L3MISS",
546 TABLE_BOOLEAN, info->vxlan_info.l3miss,
547 TABLE_FIELD, "L2MISS",
548 TABLE_BOOLEAN, info->vxlan_info.l2miss);
549 if (r < 0)
550 return table_log_add_error(r);
551
552 if (info->vxlan_info.tos > 1) {
553 r = table_add_many(table,
554 TABLE_FIELD, "TOS",
555 TABLE_UINT8, info->vxlan_info.tos);
556 if (r < 0)
557 return table_log_add_error(r);
558 }
559
560 if (info->vxlan_info.ttl > 0)
561 xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl);
562 else
563 strcpy(ttl, "auto");
564
565 r = table_add_many(table,
566 TABLE_FIELD, "TTL",
567 TABLE_STRING, ttl);
568 if (r < 0)
569 return table_log_add_error(r);
570
571 } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) {
572 r = table_add_many(table,
573 TABLE_FIELD, "VLAN ID",
574 TABLE_UINT16, info->vlan_id);
575 if (r < 0)
576 return table_log_add_error(r);
577
578 } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
579 if (in_addr_is_set(AF_INET, &info->local)) {
580 r = table_add_many(table,
581 TABLE_FIELD, "Local",
582 TABLE_IN_ADDR, &info->local);
583 if (r < 0)
584 return table_log_add_error(r);
585 }
586
587 if (in_addr_is_set(AF_INET, &info->remote)) {
588 r = table_add_many(table,
589 TABLE_FIELD, "Remote",
590 TABLE_IN_ADDR, &info->remote);
591 if (r < 0)
592 return table_log_add_error(r);
593 }
594
595 } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
596 if (in_addr_is_set(AF_INET6, &info->local)) {
597 r = table_add_many(table,
598 TABLE_FIELD, "Local",
599 TABLE_IN6_ADDR, &info->local);
600 if (r < 0)
601 return table_log_add_error(r);
602 }
603
604 if (in_addr_is_set(AF_INET6, &info->remote)) {
605 r = table_add_many(table,
606 TABLE_FIELD, "Remote",
607 TABLE_IN6_ADDR, &info->remote);
608 if (r < 0)
609 return table_log_add_error(r);
610 }
611
612 } else if (streq_ptr(info->netdev_kind, "geneve")) {
613 r = table_add_many(table,
614 TABLE_FIELD, "VNI",
615 TABLE_UINT32, info->vni);
616 if (r < 0)
617 return table_log_add_error(r);
618
619 if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) {
620 r = table_add_many(table,
621 TABLE_FIELD, "Remote",
622 TABLE_IN_ADDR, &info->remote);
623 if (r < 0)
624 return table_log_add_error(r);
625 } else if (in_addr_is_set(AF_INET6, &info->remote)) {
626 r = table_add_many(table,
627 TABLE_FIELD, "Remote",
628 TABLE_IN6_ADDR, &info->remote);
629 if (r < 0)
630 return table_log_add_error(r);
631 }
632
633 if (info->ttl > 0) {
634 r = table_add_many(table,
635 TABLE_FIELD, "TTL",
636 TABLE_UINT8, info->ttl);
637 if (r < 0)
638 return table_log_add_error(r);
639 }
640
641 if (info->tos > 0) {
642 r = table_add_many(table,
643 TABLE_FIELD, "TOS",
644 TABLE_UINT8, info->tos);
645 if (r < 0)
646 return table_log_add_error(r);
647 }
648
649 r = table_add_many(table,
650 TABLE_FIELD, "Port",
651 TABLE_UINT16, info->tunnel_port,
652 TABLE_FIELD, "Inherit",
653 TABLE_STRING, geneve_df_to_string(info->inherit));
654 if (r < 0)
655 return table_log_add_error(r);
656
657 if (info->df > 0) {
658 r = table_add_many(table,
659 TABLE_FIELD, "IPDoNotFragment",
660 TABLE_UINT8, info->df);
661 if (r < 0)
662 return table_log_add_error(r);
663 }
664
665 r = table_add_many(table,
666 TABLE_FIELD, "UDPChecksum",
667 TABLE_BOOLEAN, info->csum,
668 TABLE_FIELD, "UDP6ZeroChecksumTx",
669 TABLE_BOOLEAN, info->csum6_tx,
670 TABLE_FIELD, "UDP6ZeroChecksumRx",
671 TABLE_BOOLEAN, info->csum6_rx);
672 if (r < 0)
673 return table_log_add_error(r);
674
675 if (info->label > 0) {
676 r = table_add_many(table,
677 TABLE_FIELD, "FlowLabel",
678 TABLE_UINT32, info->label);
679 if (r < 0)
680 return table_log_add_error(r);
681 }
682
683 } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) {
684 r = table_add_many(table,
685 TABLE_FIELD, "Mode",
686 TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode));
687 if (r < 0)
688 return table_log_add_error(r);
689
690 } else if (streq_ptr(info->netdev_kind, "ipvlan")) {
691 const char *p;
692
693 if (info->ipvlan_flags & IPVLAN_F_PRIVATE)
694 p = "private";
695 else if (info->ipvlan_flags & IPVLAN_F_VEPA)
696 p = "vepa";
697 else
698 p = "bridge";
699
700 r = table_add_cell(table, NULL, TABLE_FIELD, "Mode");
701 if (r < 0)
702 return table_log_add_error(r);
703
704 r = table_add_cell_stringf(table, NULL, "%s (%s)",
705 ipvlan_mode_to_string(info->ipvlan_mode), p);
706 if (r < 0)
707 return table_log_add_error(r);
708 }
709
710 if (info->has_wlan_link_info) {
711 _cleanup_free_ char *esc = NULL;
712
713 r = table_add_cell(table, NULL, TABLE_FIELD, "Wi-Fi access point");
714 if (r < 0)
715 return table_log_add_error(r);
716
717 if (info->ssid)
718 esc = cescape(info->ssid);
719
720 r = table_add_cell_stringf(table, NULL, "%s (%s)",
721 strnull(esc),
722 ETHER_ADDR_TO_STR(&info->bssid));
723 if (r < 0)
724 return table_log_add_error(r);
725 }
726
727 if (info->has_bitrates) {
728 r = table_add_cell(table, NULL, TABLE_FIELD, "Bit Rate (Tx/Rx)");
729 if (r < 0)
730 return table_log_add_error(r);
731
732 r = table_add_cell_stringf(table, NULL, "%sBps/%sBps",
733 FORMAT_BYTES_FULL(info->tx_bitrate, 0),
734 FORMAT_BYTES_FULL(info->rx_bitrate, 0));
735 if (r < 0)
736 return table_log_add_error(r);
737 }
738
739 if (info->has_tx_queues || info->has_rx_queues) {
740 r = table_add_cell(table, NULL, TABLE_FIELD, "Number of Queues (Tx/Rx)");
741 if (r < 0)
742 return table_log_add_error(r);
743
744 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
745 if (r < 0)
746 return table_log_add_error(r);
747 }
748
749 if (info->has_ethtool_link_info) {
750 if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) {
751 r = table_add_many(table,
752 TABLE_FIELD, "Auto negotiation",
753 TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE);
754 if (r < 0)
755 return table_log_add_error(r);
756 }
757
758 if (info->speed > 0 && info->speed != UINT64_MAX) {
759 r = table_add_many(table,
760 TABLE_FIELD, "Speed",
761 TABLE_BPS, info->speed);
762 if (r < 0)
763 return table_log_add_error(r);
764 }
765
766 r = table_add_string_line(table, "Duplex", duplex_to_string(info->duplex));
767 if (r < 0)
768 return r;
769
770 r = table_add_string_line(table, "Port", port_to_string(info->port));
771 if (r < 0)
772 return r;
773 }
774
775 r = dump_addresses(rtnl, info->dhcp_message, table, info->ifindex);
776 if (r < 0)
777 return r;
778
779 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
780 if (r < 0)
781 return r;
782
783 r = dump_list(table, "DNS", dns);
784 if (r < 0)
785 return r;
786
787 r = dump_list(table, "Search Domains", search_domains);
788 if (r < 0)
789 return r;
790
791 r = dump_list(table, "Route Domains", route_domains);
792 if (r < 0)
793 return r;
794
795 r = dump_list(table, "NTP", ntp);
796 if (r < 0)
797 return r;
798
799 r = dump_list(table, "SIP", sip);
800 if (r < 0)
801 return r;
802
803 r = dump_ifindexes(table, "Carrier Bound To", carrier_bound_to);
804 if (r < 0)
805 return r;
806
807 r = dump_ifindexes(table, "Carrier Bound By", carrier_bound_by);
808 if (r < 0)
809 return r;
810
811 r = table_add_string_line(table, "Activation Policy", activation_policy);
812 if (r < 0)
813 return r;
814
815 r = sd_network_link_get_required_for_online(info->ifindex);
816 if (r >= 0) {
817 r = table_add_many(table,
818 TABLE_FIELD, "Required For Online",
819 TABLE_BOOLEAN, r);
820 if (r < 0)
821 return table_log_add_error(r);
822 }
823
824 if (captive_portal) {
825 r = table_add_many(table,
826 TABLE_FIELD, "Captive Portal",
827 TABLE_STRING, captive_portal,
828 TABLE_SET_URL, captive_portal);
829 if (r < 0)
830 return table_log_add_error(r);
831 }
832
833 if (info->dhcp_message) {
834 _cleanup_free_ char *tz = NULL;
835
836 if (dhcp_message_get_option_string(info->dhcp_message, SD_DHCP_OPTION_TZDB_TIMEZONE, &tz) >= 0
837 && timezone_is_valid(tz, LOG_DEBUG)) {
838 r = table_add_many(table,
839 TABLE_FIELD, "Time Zone",
840 TABLE_STRING, tz);
841 if (r < 0)
842 return table_log_add_error(r);
843 }
844 }
845
846 if (sd_dhcp_client_id_is_set(&info->dhcp_client_id)) {
847 _cleanup_free_ char *id = NULL;
848
849 r = sd_dhcp_client_id_to_string(&info->dhcp_client_id, &id);
850 if (r >= 0) {
851 r = table_add_many(table,
852 TABLE_FIELD, "DHCPv4 Client ID",
853 TABLE_STRING, id);
854 if (r < 0)
855 return table_log_add_error(r);
856 }
857 }
858
859 r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid);
860 if (r >= 0) {
861 r = table_add_many(table,
862 TABLE_FIELD, "DHCPv6 Client IAID",
863 TABLE_STRING, iaid);
864 if (r < 0)
865 return table_log_add_error(r);
866 }
867
868 r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid);
869 if (r >= 0) {
870 r = table_add_many(table,
871 TABLE_FIELD, "DHCPv6 Client DUID",
872 TABLE_STRING, duid);
873 if (r < 0)
874 return table_log_add_error(r);
875 }
876
877 r = dump_lldp_neighbors(vl, table, info->ifindex);
878 if (r < 0)
879 return r;
880
881 r = dump_dhcp_leases(table, "Offered DHCP leases", info);
882 if (r < 0)
883 return r;
884
885 r = dump_statistics(table, info);
886 if (r < 0)
887 return r;
888
889 /* First line: circle, ifindex, ifname. */
890 printf("%s%s%s %d: %s\n",
891 on_color_operational, glyph(GLYPH_BLACK_CIRCLE), off_color_operational,
892 info->ifindex, info->name);
893
894 r = table_print_or_warn(table);
895 if (r < 0)
896 return r;
897
898 return show_logs(info->ifindex, info->name);
899 }
900
901 int verb_link_status(int argc, char *argv[], uintptr_t _data, void *userdata) {
902 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
903 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
904 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
905 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
906 int r, c;
907
908 r = dump_description(argc, argv);
909 if (r != 0)
910 return r;
911
912 pager_open(arg_pager_flags);
913
914 r = sd_netlink_open(&rtnl);
915 if (r < 0)
916 return log_error_errno(r, "Failed to connect to netlink: %m");
917
918 r = sd_hwdb_new(&hwdb);
919 if (r < 0)
920 log_debug_errno(r, "Failed to open hardware database: %m");
921
922 r = varlink_connect_networkd(&vl);
923 if (r < 0)
924 return r;
925
926 if (arg_all)
927 c = acquire_link_info(vl, rtnl, NULL, &links);
928 else if (argc <= 1)
929 return system_status(rtnl, hwdb);
930 else
931 c = acquire_link_info(vl, rtnl, argv + 1, &links);
932 if (c < 0)
933 return c;
934
935 r = 0;
936
937 bool first = true;
938 FOREACH_ARRAY(i, links, c) {
939 if (!first)
940 putchar('\n');
941
942 RET_GATHER(r, link_status_one(rtnl, hwdb, vl, i));
943
944 first = false;
945 }
946
947 return r;
948 }