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