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