]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
ee8c4568 | 2 | |
ee8c4568 | 3 | #include <getopt.h> |
d37b7627 | 4 | #include <linux/if_addrlabel.h> |
1693a943 | 5 | #include <net/if.h> |
3f6fd1ba | 6 | #include <stdbool.h> |
ca78ad1d ZJS |
7 | #include <sys/stat.h> |
8 | #include <sys/types.h> | |
9 | #include <unistd.h> | |
ee8c4568 | 10 | |
335dd8ba | 11 | #include "sd-bus.h" |
914d6c09 | 12 | #include "sd-device.h" |
3f6fd1ba | 13 | #include "sd-hwdb.h" |
34437b4f | 14 | #include "sd-lldp.h" |
3f6fd1ba LP |
15 | #include "sd-netlink.h" |
16 | #include "sd-network.h" | |
ee8c4568 | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
3f6fd1ba | 19 | #include "arphrd-list.h" |
335dd8ba YW |
20 | #include "bus-common-errors.h" |
21 | #include "bus-error.h" | |
22 | #include "bus-util.h" | |
914d6c09 | 23 | #include "device-util.h" |
8d07de25 | 24 | #include "escape.h" |
3f6fd1ba | 25 | #include "ether-addr-util.h" |
c967d2c7 | 26 | #include "ethtool-util.h" |
34437b4f | 27 | #include "fd-util.h" |
ff7c88a2 | 28 | #include "format-table.h" |
518a66ec | 29 | #include "format-util.h" |
81fd1dd3 | 30 | #include "hwdb-util.h" |
ee8c4568 | 31 | #include "local-addresses.h" |
8752c575 | 32 | #include "locale-util.h" |
d37b7627 | 33 | #include "macro.h" |
4e2ca442 | 34 | #include "main-func.h" |
3f6fd1ba LP |
35 | #include "netlink-util.h" |
36 | #include "pager.h" | |
6bedfcbb | 37 | #include "parse-util.h" |
294bf0c3 | 38 | #include "pretty-print.h" |
9cd8c766 | 39 | #include "set.h" |
db73295a | 40 | #include "socket-util.h" |
760877e9 | 41 | #include "sort-util.h" |
34437b4f | 42 | #include "sparse-endian.h" |
d054f0a4 | 43 | #include "stdio-util.h" |
8b43440b | 44 | #include "string-table.h" |
8752c575 | 45 | #include "string-util.h" |
3f6fd1ba | 46 | #include "strv.h" |
2388b2f4 | 47 | #include "strxcpyx.h" |
288a74cc | 48 | #include "terminal-util.h" |
3f6fd1ba | 49 | #include "verbs.h" |
8d07de25 | 50 | #include "wifi-util.h" |
ee8c4568 | 51 | |
c82d1bf2 SS |
52 | /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */ |
53 | #define NETDEV_KIND_MAX 64 | |
54 | ||
8d07de25 ZJS |
55 | /* use 128 kB for receive socket kernel queue, we shouldn't need more here */ |
56 | #define RCVBUF_SIZE (128*1024) | |
57 | ||
0221d68a | 58 | static PagerFlags arg_pager_flags = 0; |
ee8c4568 | 59 | static bool arg_legend = true; |
9085f64a | 60 | static bool arg_all = false; |
a459b24f | 61 | static bool arg_stats = false; |
ee8c4568 | 62 | |
b55822c3 | 63 | static char *link_get_type_string(unsigned short iftype, sd_device *d) { |
2c740afd | 64 | const char *t, *devtype; |
ee8c4568 LP |
65 | char *p; |
66 | ||
2c740afd YW |
67 | if (d && |
68 | sd_device_get_devtype(d, &devtype) >= 0 && | |
69 | !isempty(devtype)) | |
70 | return strdup(devtype); | |
ee8c4568 LP |
71 | |
72 | t = arphrd_to_name(iftype); | |
b55822c3 JD |
73 | if (!t) |
74 | return NULL; | |
ee8c4568 LP |
75 | |
76 | p = strdup(t); | |
77 | if (!p) | |
b55822c3 | 78 | return NULL; |
ee8c4568 LP |
79 | |
80 | ascii_strlower(p); | |
b55822c3 | 81 | return p; |
ee8c4568 LP |
82 | } |
83 | ||
ceb366df | 84 | static void operational_state_to_color(const char *name, const char *state, const char **on, const char **off) { |
7e5a080a LP |
85 | assert(on); |
86 | assert(off); | |
87 | ||
ceb366df ZJS |
88 | if (STRPTR_IN_SET(state, "routable", "enslaved") || |
89 | (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) { | |
7e5a080a LP |
90 | *on = ansi_highlight_green(); |
91 | *off = ansi_normal(); | |
92 | } else if (streq_ptr(state, "degraded")) { | |
93 | *on = ansi_highlight_yellow(); | |
94 | *off = ansi_normal(); | |
95 | } else | |
96 | *on = *off = ""; | |
97 | } | |
98 | ||
99 | static void setup_state_to_color(const char *state, const char **on, const char **off) { | |
100 | assert(on); | |
101 | assert(off); | |
102 | ||
103 | if (streq_ptr(state, "configured")) { | |
104 | *on = ansi_highlight_green(); | |
105 | *off = ansi_normal(); | |
106 | } else if (streq_ptr(state, "configuring")) { | |
107 | *on = ansi_highlight_yellow(); | |
108 | *off = ansi_normal(); | |
1cf03a4f | 109 | } else if (STRPTR_IN_SET(state, "failed", "linger")) { |
7e5a080a LP |
110 | *on = ansi_highlight_red(); |
111 | *off = ansi_normal(); | |
112 | } else | |
113 | *on = *off = ""; | |
114 | } | |
115 | ||
b24281aa SS |
116 | typedef struct VxLanInfo { |
117 | uint32_t vni; | |
118 | uint32_t link; | |
119 | ||
120 | int local_family; | |
121 | int group_family; | |
122 | ||
123 | union in_addr_union local; | |
124 | union in_addr_union group; | |
125 | ||
126 | uint16_t dest_port; | |
127 | ||
128 | } VxLanInfo; | |
129 | ||
6d0c65ff | 130 | typedef struct LinkInfo { |
e997c4b0 | 131 | char name[IFNAMSIZ+1]; |
c82d1bf2 | 132 | char netdev_kind[NETDEV_KIND_MAX]; |
172353b1 | 133 | sd_device *sd_device; |
6d0c65ff | 134 | int ifindex; |
1c4a6088 | 135 | unsigned short iftype; |
b147503e LP |
136 | struct ether_addr mac_address; |
137 | uint32_t mtu; | |
2c73f59b SS |
138 | uint32_t min_mtu; |
139 | uint32_t max_mtu; | |
0307afc6 SS |
140 | uint32_t tx_queues; |
141 | uint32_t rx_queues; | |
b147503e | 142 | |
a459b24f YW |
143 | union { |
144 | struct rtnl_link_stats64 stats64; | |
145 | struct rtnl_link_stats stats; | |
146 | }; | |
147 | ||
42a63431 YW |
148 | uint64_t tx_bitrate; |
149 | uint64_t rx_bitrate; | |
335dd8ba | 150 | |
c82d1bf2 SS |
151 | /* bridge info */ |
152 | uint32_t forward_delay; | |
153 | uint32_t hello_time; | |
154 | uint32_t max_age; | |
155 | uint32_t ageing_time; | |
156 | uint32_t stp_state; | |
157 | uint16_t priority; | |
158 | uint8_t mcast_igmp_version; | |
159 | ||
b24281aa SS |
160 | /* vxlan info */ |
161 | VxLanInfo vxlan_info; | |
162 | ||
c967d2c7 YW |
163 | /* ethtool info */ |
164 | int autonegotiation; | |
165 | size_t speed; | |
166 | Duplex duplex; | |
167 | NetDevPort port; | |
168 | ||
8d07de25 | 169 | /* wlan info */ |
78404d22 | 170 | enum nl80211_iftype wlan_iftype; |
8d07de25 ZJS |
171 | char *ssid; |
172 | struct ether_addr bssid; | |
173 | ||
b147503e | 174 | bool has_mac_address:1; |
0307afc6 SS |
175 | bool has_tx_queues:1; |
176 | bool has_rx_queues:1; | |
a459b24f YW |
177 | bool has_stats64:1; |
178 | bool has_stats:1; | |
335dd8ba | 179 | bool has_bitrates:1; |
c967d2c7 | 180 | bool has_ethtool_link_info:1; |
8d07de25 | 181 | bool has_wlan_link_info:1; |
172353b1 ZJS |
182 | |
183 | bool needs_freeing:1; | |
6d0c65ff LP |
184 | } LinkInfo; |
185 | ||
93bab288 YW |
186 | static int link_info_compare(const LinkInfo *a, const LinkInfo *b) { |
187 | return CMP(a->ifindex, b->ifindex); | |
6d0c65ff LP |
188 | } |
189 | ||
172353b1 ZJS |
190 | static const LinkInfo* link_info_array_free(LinkInfo *array) { |
191 | for (unsigned i = 0; array && array[i].needs_freeing; i++) { | |
192 | sd_device_unref(array[i].sd_device); | |
8d07de25 | 193 | free(array[i].ssid); |
172353b1 ZJS |
194 | } |
195 | ||
196 | return mfree(array); | |
197 | } | |
198 | DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo*, link_info_array_free); | |
199 | ||
c82d1bf2 SS |
200 | static int decode_netdev(sd_netlink_message *m, LinkInfo *info) { |
201 | const char *received_kind; | |
202 | int r; | |
203 | ||
204 | assert(m); | |
205 | assert(info); | |
206 | ||
207 | r = sd_netlink_message_enter_container(m, IFLA_LINKINFO); | |
208 | if (r < 0) | |
209 | return r; | |
210 | ||
211 | r = sd_netlink_message_read_string(m, IFLA_INFO_KIND, &received_kind); | |
212 | if (r < 0) | |
213 | return r; | |
214 | ||
215 | r = sd_netlink_message_enter_container(m, IFLA_INFO_DATA); | |
216 | if (r < 0) | |
217 | return r; | |
218 | ||
219 | if (streq(received_kind, "bridge")) { | |
220 | (void) sd_netlink_message_read_u32(m, IFLA_BR_FORWARD_DELAY, &info->forward_delay); | |
221 | (void) sd_netlink_message_read_u32(m, IFLA_BR_HELLO_TIME, &info->hello_time); | |
222 | (void) sd_netlink_message_read_u32(m, IFLA_BR_MAX_AGE, &info->max_age); | |
223 | (void) sd_netlink_message_read_u32(m, IFLA_BR_AGEING_TIME, &info->ageing_time); | |
224 | (void) sd_netlink_message_read_u32(m, IFLA_BR_STP_STATE, &info->stp_state); | |
225 | (void) sd_netlink_message_read_u16(m, IFLA_BR_PRIORITY, &info->priority); | |
226 | (void) sd_netlink_message_read_u8(m, IFLA_BR_MCAST_IGMP_VERSION, &info->mcast_igmp_version); | |
b24281aa SS |
227 | |
228 | } else if (streq(received_kind, "vxlan")) { | |
229 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_ID, &info->vxlan_info.vni); | |
230 | ||
231 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_GROUP, &info->vxlan_info.group.in); | |
232 | if (r >= 0) | |
233 | info->vxlan_info.group_family = AF_INET; | |
234 | else { | |
235 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_GROUP6, &info->vxlan_info.group.in6); | |
236 | if (r >= 0) | |
237 | info->vxlan_info.group_family = AF_INET6; | |
238 | } | |
239 | ||
240 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_LOCAL, &info->vxlan_info.local.in); | |
241 | if (r >= 0) | |
242 | info->vxlan_info.local_family = AF_INET; | |
243 | else { | |
244 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_LOCAL6, &info->vxlan_info.local.in6); | |
245 | if (r >= 0) | |
246 | info->vxlan_info.local_family = AF_INET6; | |
247 | } | |
248 | ||
249 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_LINK, &info->vxlan_info.link); | |
250 | (void) sd_netlink_message_read_u16(m, IFLA_VXLAN_PORT, &info->vxlan_info.dest_port); | |
c82d1bf2 SS |
251 | } |
252 | ||
253 | strncpy(info->netdev_kind, received_kind, IFNAMSIZ); | |
254 | ||
255 | (void) sd_netlink_message_exit_container(m); | |
256 | (void) sd_netlink_message_exit_container(m); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
a6962904 | 261 | static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) { |
e997c4b0 | 262 | const char *name; |
a6962904 | 263 | int ifindex, r; |
c82d1bf2 | 264 | uint16_t type; |
6d0c65ff | 265 | |
e997c4b0 LP |
266 | assert(m); |
267 | assert(info); | |
6d0c65ff | 268 | |
e997c4b0 LP |
269 | r = sd_netlink_message_get_type(m, &type); |
270 | if (r < 0) | |
271 | return r; | |
6d0c65ff | 272 | |
e997c4b0 LP |
273 | if (type != RTM_NEWLINK) |
274 | return 0; | |
6d0c65ff | 275 | |
a6962904 | 276 | r = sd_rtnl_message_link_get_ifindex(m, &ifindex); |
e997c4b0 LP |
277 | if (r < 0) |
278 | return r; | |
6d0c65ff | 279 | |
e997c4b0 LP |
280 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name); |
281 | if (r < 0) | |
282 | return r; | |
6d0c65ff | 283 | |
a6962904 YW |
284 | if (patterns) { |
285 | char str[DECIMAL_STR_MAX(int)]; | |
286 | ||
287 | xsprintf(str, "%i", ifindex); | |
288 | ||
289 | if (!strv_fnmatch(patterns, str, 0) && !strv_fnmatch(patterns, name, 0)) | |
290 | return 0; | |
291 | } | |
292 | ||
b147503e | 293 | r = sd_rtnl_message_link_get_type(m, &info->iftype); |
e997c4b0 LP |
294 | if (r < 0) |
295 | return r; | |
6d0c65ff | 296 | |
2388b2f4 | 297 | strscpy(info->name, sizeof info->name, name); |
a6962904 | 298 | info->ifindex = ifindex; |
b147503e LP |
299 | |
300 | info->has_mac_address = | |
301 | sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 && | |
72e551f4 | 302 | memcmp(&info->mac_address, ÐER_ADDR_NULL, sizeof(struct ether_addr)) != 0; |
b147503e | 303 | |
c06ff86e YW |
304 | (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu); |
305 | (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu); | |
306 | (void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu); | |
2c73f59b | 307 | |
0307afc6 SS |
308 | info->has_rx_queues = |
309 | sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 && | |
310 | info->rx_queues > 0; | |
311 | ||
312 | info->has_tx_queues = | |
313 | sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 && | |
314 | info->tx_queues > 0; | |
315 | ||
a459b24f YW |
316 | if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0) |
317 | info->has_stats64 = true; | |
318 | else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0) | |
319 | info->has_stats = true; | |
320 | ||
c82d1bf2 SS |
321 | /* fill kind info */ |
322 | (void) decode_netdev(m, info); | |
323 | ||
e997c4b0 LP |
324 | return 1; |
325 | } | |
326 | ||
335dd8ba YW |
327 | static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) { |
328 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
329 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
330 | _cleanup_free_ char *path = NULL, *ifindex_str = NULL; | |
331 | int r; | |
332 | ||
333 | if (asprintf(&ifindex_str, "%i", link->ifindex) < 0) | |
334 | return -ENOMEM; | |
335 | ||
336 | r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path); | |
337 | if (r < 0) | |
338 | return r; | |
339 | ||
340 | r = sd_bus_call_method( | |
341 | bus, | |
342 | "org.freedesktop.network1", | |
343 | path, | |
344 | "org.freedesktop.DBus.Properties", | |
345 | "Get", | |
346 | &error, | |
347 | &reply, | |
348 | "ss", | |
349 | "org.freedesktop.network1.Link", | |
350 | "BitRates"); | |
351 | if (r < 0) { | |
e813de54 | 352 | bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY); |
8210a61a ZJS |
353 | |
354 | return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, | |
355 | r, "Failed to query link bit rates: %s", bus_error_message(&error, r)); | |
335dd8ba YW |
356 | } |
357 | ||
42a63431 | 358 | r = sd_bus_message_enter_container(reply, 'v', "(tt)"); |
335dd8ba YW |
359 | if (r < 0) |
360 | return bus_log_parse_error(r); | |
361 | ||
42a63431 | 362 | r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate); |
335dd8ba YW |
363 | if (r < 0) |
364 | return bus_log_parse_error(r); | |
365 | ||
366 | r = sd_bus_message_exit_container(reply); | |
367 | if (r < 0) | |
368 | return bus_log_parse_error(r); | |
369 | ||
e813de54 | 370 | link->has_bitrates = link->tx_bitrate != UINT64_MAX && link->rx_bitrate != UINT64_MAX; |
335dd8ba YW |
371 | |
372 | return 0; | |
373 | } | |
374 | ||
516e9c80 ZJS |
375 | static void acquire_ether_link_info(int *fd, LinkInfo *link) { |
376 | if (ethtool_get_link_info(fd, link->name, | |
377 | &link->autonegotiation, | |
378 | &link->speed, | |
379 | &link->duplex, | |
380 | &link->port) >= 0) | |
381 | link->has_ethtool_link_info = true; | |
382 | } | |
383 | ||
8d07de25 ZJS |
384 | static void acquire_wlan_link_info(LinkInfo *link) { |
385 | _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL; | |
386 | const char *type = NULL; | |
78404d22 | 387 | int r, k = 0; |
8d07de25 ZJS |
388 | |
389 | if (link->sd_device) | |
390 | (void) sd_device_get_devtype(link->sd_device, &type); | |
391 | if (!streq_ptr(type, "wlan")) | |
392 | return; | |
393 | ||
394 | r = sd_genl_socket_open(&genl); | |
395 | if (r < 0) { | |
396 | log_debug_errno(r, "Failed to open generic netlink socket: %m"); | |
397 | return; | |
398 | } | |
399 | ||
400 | (void) sd_netlink_inc_rcvbuf(genl, RCVBUF_SIZE); | |
401 | ||
78404d22 | 402 | r = wifi_get_interface(genl, link->ifindex, &link->wlan_iftype, &link->ssid); |
8d07de25 ZJS |
403 | if (r < 0) |
404 | log_debug_errno(r, "%s: failed to query ssid: %m", link->name); | |
405 | ||
33ebda2e | 406 | if (link->wlan_iftype == NL80211_IFTYPE_STATION) { |
78404d22 YW |
407 | k = wifi_get_station(genl, link->ifindex, &link->bssid); |
408 | if (k < 0) | |
409 | log_debug_errno(k, "%s: failed to query bssid: %m", link->name); | |
410 | } | |
8d07de25 ZJS |
411 | |
412 | link->has_wlan_link_info = r > 0 || k > 0; | |
413 | } | |
414 | ||
335dd8ba | 415 | static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, LinkInfo **ret) { |
4afd3348 | 416 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
172353b1 | 417 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
c967d2c7 | 418 | _cleanup_close_ int fd = -1; |
335dd8ba | 419 | size_t allocated = 0, c = 0, j; |
7e5a080a | 420 | sd_netlink_message *i; |
7d367b45 LP |
421 | int r; |
422 | ||
b147503e | 423 | assert(rtnl); |
7d367b45 | 424 | assert(ret); |
ee8c4568 | 425 | |
ee8c4568 LP |
426 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); |
427 | if (r < 0) | |
428 | return rtnl_log_create_error(r); | |
429 | ||
1c4baffc | 430 | r = sd_netlink_message_request_dump(req, true); |
ee8c4568 LP |
431 | if (r < 0) |
432 | return rtnl_log_create_error(r); | |
433 | ||
1c4baffc | 434 | r = sd_netlink_call(rtnl, req, 0, &reply); |
f647962d MS |
435 | if (r < 0) |
436 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
ee8c4568 | 437 | |
7e5a080a | 438 | for (i = reply; i; i = sd_netlink_message_next(i)) { |
172353b1 | 439 | if (!GREEDY_REALLOC0(links, allocated, c + 2)) /* We keep one trailing one as marker */ |
7e5a080a | 440 | return -ENOMEM; |
7d367b45 | 441 | |
a6962904 | 442 | r = decode_link(i, links + c, patterns); |
7e5a080a LP |
443 | if (r < 0) |
444 | return r; | |
c967d2c7 YW |
445 | if (r == 0) |
446 | continue; | |
447 | ||
172353b1 ZJS |
448 | links[c].needs_freeing = true; |
449 | ||
450 | char devid[2 + DECIMAL_STR_MAX(int)]; | |
451 | xsprintf(devid, "n%i", links[c].ifindex); | |
452 | (void) sd_device_new_from_device_id(&links[c].sd_device, devid); | |
453 | ||
516e9c80 | 454 | acquire_ether_link_info(&fd, &links[c]); |
8d07de25 | 455 | acquire_wlan_link_info(&links[c]); |
c967d2c7 YW |
456 | |
457 | c++; | |
7e5a080a LP |
458 | } |
459 | ||
93bab288 | 460 | typesafe_qsort(links, c, link_info_compare); |
7e5a080a | 461 | |
335dd8ba YW |
462 | if (bus) |
463 | for (j = 0; j < c; j++) | |
464 | (void) acquire_link_bitrates(bus, links + j); | |
465 | ||
1cc6c93a | 466 | *ret = TAKE_PTR(links); |
7e5a080a LP |
467 | |
468 | return (int) c; | |
7d367b45 | 469 | } |
ee8c4568 | 470 | |
7d367b45 | 471 | static int list_links(int argc, char *argv[], void *userdata) { |
b147503e | 472 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
172353b1 | 473 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
ff7c88a2 YW |
474 | _cleanup_(table_unrefp) Table *table = NULL; |
475 | TableCell *cell; | |
b147503e LP |
476 | int c, i, r; |
477 | ||
478 | r = sd_netlink_open(&rtnl); | |
479 | if (r < 0) | |
480 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
7d367b45 | 481 | |
335dd8ba | 482 | c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
6d0c65ff | 483 | if (c < 0) |
7d367b45 LP |
484 | return c; |
485 | ||
0221d68a | 486 | (void) pager_open(arg_pager_flags); |
7d367b45 | 487 | |
4252171a | 488 | table = table_new("idx", "link", "type", "operational", "setup"); |
ff7c88a2 YW |
489 | if (!table) |
490 | return log_oom(); | |
491 | ||
492 | table_set_header(table, arg_legend); | |
493 | ||
494 | assert_se(cell = table_get_cell(table, 0, 0)); | |
495 | (void) table_set_minimum_width(table, cell, 3); | |
496 | (void) table_set_weight(table, cell, 0); | |
81914d9f | 497 | (void) table_set_ellipsize_percent(table, cell, 100); |
ff7c88a2 YW |
498 | (void) table_set_align_percent(table, cell, 100); |
499 | ||
500 | assert_se(cell = table_get_cell(table, 0, 1)); | |
81914d9f | 501 | (void) table_set_ellipsize_percent(table, cell, 100); |
6d0c65ff LP |
502 | |
503 | for (i = 0; i < c; i++) { | |
ab1525bc | 504 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL; |
d57c365b LP |
505 | const char *on_color_operational, *off_color_operational, |
506 | *on_color_setup, *off_color_setup; | |
ee8c4568 | 507 | _cleanup_free_ char *t = NULL; |
ee8c4568 | 508 | |
4abd866d | 509 | (void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state); |
ceb366df | 510 | operational_state_to_color(links[i].name, operational_state, &on_color_operational, &off_color_operational); |
d57c365b | 511 | |
33d5013d LP |
512 | r = sd_network_link_get_setup_state(links[i].ifindex, &setup_state); |
513 | if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */ | |
514 | setup_state = strdup("unmanaged"); | |
d57c365b | 515 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
ee8c4568 | 516 | |
172353b1 | 517 | t = link_get_type_string(links[i].iftype, links[i].sd_device); |
ee8c4568 | 518 | |
ff7c88a2 | 519 | r = table_add_many(table, |
8d0e0af2 | 520 | TABLE_INT, links[i].ifindex, |
ff7c88a2 | 521 | TABLE_STRING, links[i].name, |
8d0e0af2 YW |
522 | TABLE_STRING, strna(t), |
523 | TABLE_STRING, strna(operational_state), | |
524 | TABLE_SET_COLOR, on_color_operational, | |
525 | TABLE_STRING, strna(setup_state), | |
526 | TABLE_SET_COLOR, on_color_setup); | |
ff7c88a2 YW |
527 | if (r < 0) |
528 | return r; | |
ee8c4568 LP |
529 | } |
530 | ||
ff7c88a2 YW |
531 | r = table_print(table, NULL); |
532 | if (r < 0) | |
533 | return log_error_errno(r, "Failed to print table: %m"); | |
534 | ||
ee8c4568 | 535 | if (arg_legend) |
6d0c65ff | 536 | printf("\n%i links listed.\n", c); |
ee8c4568 LP |
537 | |
538 | return 0; | |
539 | } | |
540 | ||
c09da729 | 541 | /* IEEE Organizationally Unique Identifier vendor string */ |
b147503e | 542 | static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) { |
81fd1dd3 | 543 | const char *description; |
fbd0b64f | 544 | char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc; |
81fd1dd3 TG |
545 | int r; |
546 | ||
547 | assert(ret); | |
c09da729 | 548 | |
888943fc LP |
549 | if (!hwdb) |
550 | return -EINVAL; | |
551 | ||
81fd1dd3 TG |
552 | if (!mac) |
553 | return -EINVAL; | |
554 | ||
c09da729 TG |
555 | /* skip commonly misused 00:00:00 (Xerox) prefix */ |
556 | if (memcmp(mac, "\0\0\0", 3) == 0) | |
557 | return -EINVAL; | |
558 | ||
d054f0a4 DM |
559 | xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, |
560 | ETHER_ADDR_FORMAT_VAL(*mac)); | |
c09da729 | 561 | |
81fd1dd3 TG |
562 | r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description); |
563 | if (r < 0) | |
564 | return r; | |
c09da729 | 565 | |
81fd1dd3 TG |
566 | desc = strdup(description); |
567 | if (!desc) | |
568 | return -ENOMEM; | |
c09da729 | 569 | |
81fd1dd3 TG |
570 | *ret = desc; |
571 | ||
572 | return 0; | |
c09da729 TG |
573 | } |
574 | ||
69fb1176 | 575 | static int get_gateway_description( |
1c4baffc | 576 | sd_netlink *rtnl, |
81fd1dd3 | 577 | sd_hwdb *hwdb, |
69fb1176 LP |
578 | int ifindex, |
579 | int family, | |
580 | union in_addr_union *gateway, | |
581 | char **gateway_description) { | |
4afd3348 | 582 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 583 | sd_netlink_message *m; |
c09da729 TG |
584 | int r; |
585 | ||
586 | assert(rtnl); | |
587 | assert(ifindex >= 0); | |
4c701096 | 588 | assert(IN_SET(family, AF_INET, AF_INET6)); |
c09da729 TG |
589 | assert(gateway); |
590 | assert(gateway_description); | |
591 | ||
592 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); | |
593 | if (r < 0) | |
594 | return r; | |
595 | ||
1c4baffc | 596 | r = sd_netlink_message_request_dump(req, true); |
c09da729 TG |
597 | if (r < 0) |
598 | return r; | |
599 | ||
1c4baffc | 600 | r = sd_netlink_call(rtnl, req, 0, &reply); |
c09da729 TG |
601 | if (r < 0) |
602 | return r; | |
603 | ||
1c4baffc | 604 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
67a46833 YW |
605 | union in_addr_union gw = IN_ADDR_NULL; |
606 | struct ether_addr mac = ETHER_ADDR_NULL; | |
c09da729 TG |
607 | uint16_t type; |
608 | int ifi, fam; | |
609 | ||
1c4baffc | 610 | r = sd_netlink_message_get_errno(m); |
c09da729 TG |
611 | if (r < 0) { |
612 | log_error_errno(r, "got error: %m"); | |
613 | continue; | |
614 | } | |
615 | ||
1c4baffc | 616 | r = sd_netlink_message_get_type(m, &type); |
c09da729 TG |
617 | if (r < 0) { |
618 | log_error_errno(r, "could not get type: %m"); | |
619 | continue; | |
620 | } | |
621 | ||
622 | if (type != RTM_NEWNEIGH) { | |
623 | log_error("type is not RTM_NEWNEIGH"); | |
624 | continue; | |
625 | } | |
626 | ||
627 | r = sd_rtnl_message_neigh_get_family(m, &fam); | |
628 | if (r < 0) { | |
629 | log_error_errno(r, "could not get family: %m"); | |
630 | continue; | |
631 | } | |
632 | ||
633 | if (fam != family) { | |
634 | log_error("family is not correct"); | |
635 | continue; | |
636 | } | |
637 | ||
638 | r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); | |
639 | if (r < 0) { | |
144232a8 | 640 | log_error_errno(r, "could not get ifindex: %m"); |
c09da729 TG |
641 | continue; |
642 | } | |
643 | ||
644 | if (ifindex > 0 && ifi != ifindex) | |
645 | continue; | |
646 | ||
647 | switch (fam) { | |
648 | case AF_INET: | |
1c4baffc | 649 | r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in); |
c09da729 TG |
650 | if (r < 0) |
651 | continue; | |
652 | ||
653 | break; | |
654 | case AF_INET6: | |
1c4baffc | 655 | r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6); |
c09da729 TG |
656 | if (r < 0) |
657 | continue; | |
658 | ||
659 | break; | |
660 | default: | |
661 | continue; | |
662 | } | |
663 | ||
664 | if (!in_addr_equal(fam, &gw, gateway)) | |
665 | continue; | |
666 | ||
49808e0e | 667 | r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac); |
c09da729 TG |
668 | if (r < 0) |
669 | continue; | |
670 | ||
671 | r = ieee_oui(hwdb, &mac, gateway_description); | |
672 | if (r < 0) | |
673 | continue; | |
674 | ||
675 | return 0; | |
676 | } | |
677 | ||
678 | return -ENODATA; | |
679 | } | |
680 | ||
69fb1176 | 681 | static int dump_gateways( |
1c4baffc | 682 | sd_netlink *rtnl, |
81fd1dd3 | 683 | sd_hwdb *hwdb, |
98d5bef3 | 684 | Table *table, |
69fb1176 | 685 | int ifindex) { |
b6a3ca6d TG |
686 | _cleanup_free_ struct local_address *local = NULL; |
687 | int r, n, i; | |
c09da729 | 688 | |
837f57da | 689 | assert(rtnl); |
98d5bef3 | 690 | assert(table); |
837f57da | 691 | |
b6a3ca6d TG |
692 | n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local); |
693 | if (n < 0) | |
694 | return n; | |
c09da729 | 695 | |
b6a3ca6d | 696 | for (i = 0; i < n; i++) { |
98d5bef3 YW |
697 | _cleanup_free_ char *gateway = NULL, *description = NULL, *with_description = NULL; |
698 | ||
8d0e0af2 YW |
699 | r = table_add_many(table, |
700 | TABLE_EMPTY, | |
701 | TABLE_STRING, i == 0 ? "Gateway:" : ""); | |
98d5bef3 YW |
702 | if (r < 0) |
703 | return r; | |
c09da729 | 704 | |
b6a3ca6d | 705 | r = in_addr_to_string(local[i].family, &local[i].address, &gateway); |
c09da729 | 706 | if (r < 0) |
b6a3ca6d | 707 | return r; |
c09da729 | 708 | |
69fb1176 | 709 | r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description); |
c09da729 | 710 | if (r < 0) |
b6a3ca6d | 711 | log_debug_errno(r, "Could not get description of gateway: %m"); |
c09da729 | 712 | |
98d5bef3 YW |
713 | if (description) { |
714 | with_description = strjoin(gateway, " (", description, ")"); | |
715 | if (!with_description) | |
716 | return -ENOMEM; | |
717 | } | |
1693a943 LP |
718 | |
719 | /* Show interface name for the entry if we show | |
720 | * entries for all interfaces */ | |
721 | if (ifindex <= 0) { | |
518a66ec | 722 | char name[IF_NAMESIZE+1]; |
98d5bef3 | 723 | |
e4857ee2 YW |
724 | r = table_add_cell_stringf(table, NULL, "%s on %s", with_description ?: gateway, |
725 | format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)); | |
98d5bef3 YW |
726 | } else |
727 | r = table_add_cell(table, NULL, TABLE_STRING, with_description ?: gateway); | |
728 | if (r < 0) | |
729 | return r; | |
c09da729 TG |
730 | } |
731 | ||
732 | return 0; | |
733 | } | |
734 | ||
69fb1176 | 735 | static int dump_addresses( |
1c4baffc | 736 | sd_netlink *rtnl, |
98d5bef3 | 737 | Table *table, |
69fb1176 LP |
738 | int ifindex) { |
739 | ||
ee8c4568 | 740 | _cleanup_free_ struct local_address *local = NULL; |
cdf01b36 | 741 | _cleanup_free_ char *dhcp4_address = NULL; |
ee8c4568 LP |
742 | int r, n, i; |
743 | ||
837f57da | 744 | assert(rtnl); |
98d5bef3 | 745 | assert(table); |
837f57da | 746 | |
1d050e1e | 747 | n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local); |
ee8c4568 LP |
748 | if (n < 0) |
749 | return n; | |
750 | ||
cdf01b36 SS |
751 | (void) sd_network_link_get_dhcp4_address(ifindex, &dhcp4_address); |
752 | ||
ee8c4568 LP |
753 | for (i = 0; i < n; i++) { |
754 | _cleanup_free_ char *pretty = NULL; | |
755 | ||
8d0e0af2 YW |
756 | r = table_add_many(table, |
757 | TABLE_EMPTY, | |
758 | TABLE_STRING, i == 0 ? "Address:" : ""); | |
98d5bef3 YW |
759 | if (r < 0) |
760 | return r; | |
1693a943 | 761 | |
98d5bef3 YW |
762 | r = in_addr_to_string(local[i].family, &local[i].address, &pretty); |
763 | if (r < 0) | |
764 | return r; | |
1693a943 | 765 | |
cdf01b36 SS |
766 | if (dhcp4_address && streq(pretty, dhcp4_address)) { |
767 | _cleanup_free_ char *p = NULL; | |
768 | ||
769 | p = pretty; | |
770 | pretty = strjoin(pretty , " (DHCP4)"); | |
771 | if (!pretty) | |
772 | return log_oom(); | |
773 | } | |
774 | ||
98d5bef3 | 775 | if (ifindex <= 0) { |
518a66ec | 776 | char name[IF_NAMESIZE+1]; |
98d5bef3 | 777 | |
e4857ee2 YW |
778 | r = table_add_cell_stringf(table, NULL, "%s on %s", pretty, |
779 | format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)); | |
98d5bef3 YW |
780 | } else |
781 | r = table_add_cell(table, NULL, TABLE_STRING, pretty); | |
782 | if (r < 0) | |
783 | return r; | |
ee8c4568 LP |
784 | } |
785 | ||
786 | return 0; | |
787 | } | |
788 | ||
d37b7627 SS |
789 | static int dump_address_labels(sd_netlink *rtnl) { |
790 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
0232beed | 791 | _cleanup_(table_unrefp) Table *table = NULL; |
d37b7627 | 792 | sd_netlink_message *m; |
0232beed | 793 | TableCell *cell; |
d37b7627 SS |
794 | int r; |
795 | ||
796 | assert(rtnl); | |
797 | ||
798 | r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6); | |
799 | if (r < 0) | |
800 | return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m"); | |
801 | ||
802 | r = sd_netlink_message_request_dump(req, true); | |
803 | if (r < 0) | |
804 | return r; | |
805 | ||
806 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
807 | if (r < 0) | |
808 | return r; | |
809 | ||
4252171a | 810 | table = table_new("label", "prefix/prefixlen"); |
0232beed YW |
811 | if (!table) |
812 | return -ENOMEM; | |
813 | ||
814 | r = table_set_sort(table, 0, SIZE_MAX); | |
815 | if (r < 0) | |
816 | return r; | |
817 | ||
818 | assert_se(cell = table_get_cell(table, 0, 0)); | |
819 | (void) table_set_align_percent(table, cell, 100); | |
81914d9f | 820 | (void) table_set_ellipsize_percent(table, cell, 100); |
0232beed YW |
821 | |
822 | assert_se(cell = table_get_cell(table, 0, 1)); | |
823 | (void) table_set_align_percent(table, cell, 100); | |
d37b7627 SS |
824 | |
825 | for (m = reply; m; m = sd_netlink_message_next(m)) { | |
826 | _cleanup_free_ char *pretty = NULL; | |
67a46833 | 827 | union in_addr_union prefix = IN_ADDR_NULL; |
d37b7627 SS |
828 | uint8_t prefixlen; |
829 | uint32_t label; | |
830 | ||
831 | r = sd_netlink_message_get_errno(m); | |
832 | if (r < 0) { | |
833 | log_error_errno(r, "got error: %m"); | |
834 | continue; | |
835 | } | |
836 | ||
837 | r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label); | |
838 | if (r < 0 && r != -ENODATA) { | |
839 | log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m"); | |
840 | continue; | |
841 | } | |
842 | ||
843 | r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6); | |
844 | if (r < 0) | |
845 | continue; | |
846 | ||
847 | r = in_addr_to_string(AF_INET6, &prefix, &pretty); | |
848 | if (r < 0) | |
849 | continue; | |
850 | ||
851 | r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen); | |
852 | if (r < 0) | |
853 | continue; | |
854 | ||
81914d9f | 855 | r = table_add_cell(table, NULL, TABLE_UINT32, &label); |
0232beed YW |
856 | if (r < 0) |
857 | return r; | |
858 | ||
8d0e0af2 | 859 | r = table_add_cell_stringf(table, NULL, "%s/%u", pretty, prefixlen); |
0232beed YW |
860 | if (r < 0) |
861 | return r; | |
d37b7627 SS |
862 | } |
863 | ||
0232beed | 864 | return table_print(table, NULL); |
d37b7627 SS |
865 | } |
866 | ||
867 | static int list_address_labels(int argc, char *argv[], void *userdata) { | |
868 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
869 | int r; | |
870 | ||
871 | r = sd_netlink_open(&rtnl); | |
872 | if (r < 0) | |
873 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
874 | ||
875 | dump_address_labels(rtnl); | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
837f57da LP |
880 | static int open_lldp_neighbors(int ifindex, FILE **ret) { |
881 | _cleanup_free_ char *p = NULL; | |
882 | FILE *f; | |
883 | ||
884 | if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0) | |
885 | return -ENOMEM; | |
886 | ||
887 | f = fopen(p, "re"); | |
888 | if (!f) | |
889 | return -errno; | |
890 | ||
891 | *ret = f; | |
892 | return 0; | |
893 | } | |
894 | ||
895 | static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) { | |
896 | _cleanup_free_ void *raw = NULL; | |
897 | size_t l; | |
898 | le64_t u; | |
899 | int r; | |
900 | ||
901 | assert(f); | |
902 | assert(ret); | |
903 | ||
904 | l = fread(&u, 1, sizeof(u), f); | |
905 | if (l == 0 && feof(f)) | |
906 | return 0; | |
907 | if (l != sizeof(u)) | |
908 | return -EBADMSG; | |
909 | ||
d23c3e4c FB |
910 | /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */ |
911 | if (le64toh(u) >= 4096) | |
912 | return -EBADMSG; | |
913 | ||
837f57da LP |
914 | raw = new(uint8_t, le64toh(u)); |
915 | if (!raw) | |
916 | return -ENOMEM; | |
917 | ||
918 | if (fread(raw, 1, le64toh(u), f) != le64toh(u)) | |
919 | return -EBADMSG; | |
920 | ||
921 | r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u)); | |
922 | if (r < 0) | |
923 | return r; | |
924 | ||
925 | return 1; | |
926 | } | |
927 | ||
98d5bef3 | 928 | static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) { |
837f57da LP |
929 | _cleanup_fclose_ FILE *f = NULL; |
930 | int r, c = 0; | |
931 | ||
98d5bef3 | 932 | assert(table); |
837f57da LP |
933 | assert(prefix); |
934 | assert(ifindex > 0); | |
935 | ||
936 | r = open_lldp_neighbors(ifindex, &f); | |
98d5bef3 YW |
937 | if (r == -ENOENT) |
938 | return 0; | |
837f57da LP |
939 | if (r < 0) |
940 | return r; | |
941 | ||
942 | for (;;) { | |
943 | const char *system_name = NULL, *port_id = NULL, *port_description = NULL; | |
944 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; | |
945 | ||
946 | r = next_lldp_neighbor(f, &n); | |
947 | if (r < 0) | |
948 | return r; | |
949 | if (r == 0) | |
950 | break; | |
951 | ||
8d0e0af2 YW |
952 | r = table_add_many(table, |
953 | TABLE_EMPTY, | |
954 | TABLE_STRING, c == 0 ? prefix : ""); | |
98d5bef3 YW |
955 | if (r < 0) |
956 | return r; | |
837f57da LP |
957 | |
958 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); | |
959 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
960 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
961 | ||
8d0e0af2 YW |
962 | r = table_add_cell_stringf(table, NULL, |
963 | "%s on port %s%s%s%s", | |
964 | strna(system_name), strna(port_id), | |
965 | isempty(port_description) ? "" : " (", | |
e226badc | 966 | strempty(port_description), |
8d0e0af2 | 967 | isempty(port_description) ? "" : ")"); |
98d5bef3 YW |
968 | if (r < 0) |
969 | return r; | |
837f57da LP |
970 | |
971 | c++; | |
972 | } | |
973 | ||
974 | return c; | |
975 | } | |
976 | ||
98d5bef3 | 977 | static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) { |
b295beea | 978 | unsigned c; |
98d5bef3 | 979 | int r; |
b295beea LP |
980 | |
981 | assert(prefix); | |
982 | ||
983 | if (!ifindexes || ifindexes[0] <= 0) | |
98d5bef3 | 984 | return 0; |
b295beea LP |
985 | |
986 | for (c = 0; ifindexes[c] > 0; c++) { | |
8d0e0af2 YW |
987 | r = table_add_many(table, |
988 | TABLE_EMPTY, | |
989 | TABLE_STRING, c == 0 ? prefix : "", | |
990 | TABLE_IFINDEX, ifindexes[c]); | |
98d5bef3 YW |
991 | if (r < 0) |
992 | return r; | |
b295beea | 993 | } |
98d5bef3 YW |
994 | |
995 | return 0; | |
b295beea LP |
996 | } |
997 | ||
98d5bef3 | 998 | static int dump_list(Table *table, const char *prefix, char **l) { |
ee8c4568 | 999 | char **i; |
98d5bef3 | 1000 | int r; |
ee8c4568 | 1001 | |
dce83649 | 1002 | if (strv_isempty(l)) |
98d5bef3 | 1003 | return 0; |
dce83649 | 1004 | |
ee8c4568 | 1005 | STRV_FOREACH(i, l) { |
8d0e0af2 YW |
1006 | r = table_add_many(table, |
1007 | TABLE_EMPTY, | |
1008 | TABLE_STRING, i == l ? prefix : "", | |
1009 | TABLE_STRING, *i); | |
98d5bef3 YW |
1010 | if (r < 0) |
1011 | return r; | |
ee8c4568 | 1012 | } |
98d5bef3 YW |
1013 | |
1014 | return 0; | |
ee8c4568 LP |
1015 | } |
1016 | ||
a459b24f | 1017 | #define DUMP_STATS_ONE(name, val_name) \ |
8d0e0af2 YW |
1018 | r = table_add_many(table, \ |
1019 | TABLE_EMPTY, \ | |
1020 | TABLE_STRING, name ":"); \ | |
a459b24f YW |
1021 | if (r < 0) \ |
1022 | return r; \ | |
8d0e0af2 YW |
1023 | r = table_add_cell(table, NULL, \ |
1024 | info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \ | |
a459b24f YW |
1025 | info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \ |
1026 | if (r < 0) \ | |
1027 | return r; | |
1028 | ||
1029 | static int dump_statistics(Table *table, const LinkInfo *info) { | |
1030 | int r; | |
1031 | ||
1032 | if (!arg_stats) | |
1033 | return 0; | |
1034 | ||
1035 | if (!info->has_stats64 && !info->has_stats) | |
1036 | return 0; | |
1037 | ||
1038 | DUMP_STATS_ONE("Rx Packets", rx_packets); | |
1039 | DUMP_STATS_ONE("Tx Packets", tx_packets); | |
1040 | DUMP_STATS_ONE("Rx Bytes", rx_bytes); | |
1041 | DUMP_STATS_ONE("Tx Bytes", tx_bytes); | |
1042 | DUMP_STATS_ONE("Rx Errors", rx_errors); | |
1043 | DUMP_STATS_ONE("Tx Errors", tx_errors); | |
1044 | DUMP_STATS_ONE("Rx Dropped", rx_dropped); | |
1045 | DUMP_STATS_ONE("Tx Dropped", tx_dropped); | |
1046 | DUMP_STATS_ONE("Multicast Packets", multicast); | |
1047 | DUMP_STATS_ONE("Collisions", collisions); | |
1048 | ||
1049 | return 0; | |
1050 | } | |
1051 | ||
69fb1176 | 1052 | static int link_status_one( |
1c4baffc | 1053 | sd_netlink *rtnl, |
81fd1dd3 | 1054 | sd_hwdb *hwdb, |
b147503e LP |
1055 | const LinkInfo *info) { |
1056 | ||
3df9bec5 | 1057 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; |
64d6c229 | 1058 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL; |
373d9f17 | 1059 | _cleanup_free_ char *t = NULL, *network = NULL; |
af5effc4 | 1060 | const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL; |
d57c365b | 1061 | const char *on_color_operational, *off_color_operational, |
98d5bef3 | 1062 | *on_color_setup, *off_color_setup; |
b295beea | 1063 | _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL; |
98d5bef3 YW |
1064 | _cleanup_(table_unrefp) Table *table = NULL; |
1065 | TableCell *cell; | |
b147503e | 1066 | int r; |
9085f64a LP |
1067 | |
1068 | assert(rtnl); | |
b147503e | 1069 | assert(info); |
9085f64a | 1070 | |
b147503e | 1071 | (void) sd_network_link_get_operational_state(info->ifindex, &operational_state); |
ceb366df | 1072 | operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational); |
d57c365b | 1073 | |
33d5013d LP |
1074 | r = sd_network_link_get_setup_state(info->ifindex, &setup_state); |
1075 | if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */ | |
1076 | setup_state = strdup("unmanaged"); | |
d57c365b | 1077 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
9085f64a | 1078 | |
b147503e LP |
1079 | (void) sd_network_link_get_dns(info->ifindex, &dns); |
1080 | (void) sd_network_link_get_search_domains(info->ifindex, &search_domains); | |
1081 | (void) sd_network_link_get_route_domains(info->ifindex, &route_domains); | |
1082 | (void) sd_network_link_get_ntp(info->ifindex, &ntp); | |
9085f64a | 1083 | |
172353b1 ZJS |
1084 | if (info->sd_device) { |
1085 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link); | |
1086 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver); | |
1087 | (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path); | |
9085f64a | 1088 | |
172353b1 ZJS |
1089 | if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0) |
1090 | (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor); | |
9085f64a | 1091 | |
172353b1 ZJS |
1092 | if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0) |
1093 | (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model); | |
9085f64a LP |
1094 | } |
1095 | ||
172353b1 | 1096 | t = link_get_type_string(info->iftype, info->sd_device); |
b1acce80 | 1097 | |
4abd866d | 1098 | (void) sd_network_link_get_network_file(info->ifindex, &network); |
df3fb561 | 1099 | |
4abd866d LP |
1100 | (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to); |
1101 | (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by); | |
0d4ad91d | 1102 | |
4252171a | 1103 | table = table_new("dot", "key", "value"); |
98d5bef3 YW |
1104 | if (!table) |
1105 | return -ENOMEM; | |
1106 | ||
81914d9f YW |
1107 | assert_se(cell = table_get_cell(table, 0, 0)); |
1108 | (void) table_set_ellipsize_percent(table, cell, 100); | |
1109 | ||
1110 | assert_se(cell = table_get_cell(table, 0, 1)); | |
1111 | (void) table_set_ellipsize_percent(table, cell, 100); | |
1112 | ||
98d5bef3 YW |
1113 | table_set_header(table, false); |
1114 | ||
8d0e0af2 YW |
1115 | r = table_add_many(table, |
1116 | TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), | |
1117 | TABLE_SET_COLOR, on_color_operational); | |
98d5bef3 YW |
1118 | if (r < 0) |
1119 | return r; | |
81914d9f | 1120 | r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name); |
98d5bef3 YW |
1121 | if (r < 0) |
1122 | return r; | |
81914d9f | 1123 | (void) table_set_align_percent(table, cell, 0); |
98d5bef3 | 1124 | |
8d0e0af2 YW |
1125 | r = table_add_many(table, |
1126 | TABLE_EMPTY, | |
1127 | TABLE_EMPTY, | |
1128 | TABLE_STRING, "Link File:", | |
1129 | TABLE_SET_ALIGN_PERCENT, 100, | |
1130 | TABLE_STRING, strna(link), | |
1131 | TABLE_EMPTY, | |
1132 | TABLE_STRING, "Network File:", | |
1133 | TABLE_STRING, strna(network), | |
1134 | TABLE_EMPTY, | |
1135 | TABLE_STRING, "Type:", | |
1136 | TABLE_STRING, strna(t), | |
1137 | TABLE_EMPTY, | |
1138 | TABLE_STRING, "State:"); | |
98d5bef3 YW |
1139 | if (r < 0) |
1140 | return r; | |
1141 | r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)", | |
64e7ebde YW |
1142 | on_color_operational, strna(operational_state), off_color_operational, |
1143 | on_color_setup, strna(setup_state), off_color_setup); | |
98d5bef3 YW |
1144 | if (r < 0) |
1145 | return r; | |
1146 | ||
1147 | if (path) { | |
8d0e0af2 YW |
1148 | r = table_add_many(table, |
1149 | TABLE_EMPTY, | |
1150 | TABLE_STRING, "Path:", | |
1151 | TABLE_STRING, path); | |
98d5bef3 YW |
1152 | if (r < 0) |
1153 | return r; | |
1154 | } | |
1155 | if (driver) { | |
8d0e0af2 YW |
1156 | r = table_add_many(table, |
1157 | TABLE_EMPTY, | |
1158 | TABLE_STRING, "Driver:", | |
1159 | TABLE_STRING, driver); | |
98d5bef3 YW |
1160 | if (r < 0) |
1161 | return r; | |
1162 | } | |
1163 | if (vendor) { | |
8d0e0af2 YW |
1164 | r = table_add_many(table, |
1165 | TABLE_EMPTY, | |
1166 | TABLE_STRING, "Vendor:", | |
1167 | TABLE_STRING, vendor); | |
98d5bef3 YW |
1168 | if (r < 0) |
1169 | return r; | |
1170 | } | |
1171 | if (model) { | |
8d0e0af2 YW |
1172 | r = table_add_many(table, |
1173 | TABLE_EMPTY, | |
1174 | TABLE_STRING, "Model:", | |
1175 | TABLE_STRING, model); | |
98d5bef3 YW |
1176 | if (r < 0) |
1177 | return r; | |
1178 | } | |
9085f64a | 1179 | |
b147503e | 1180 | if (info->has_mac_address) { |
888943fc | 1181 | _cleanup_free_ char *description = NULL; |
db73295a | 1182 | char ea[ETHER_ADDR_TO_STRING_MAX]; |
888943fc | 1183 | |
4abd866d | 1184 | (void) ieee_oui(hwdb, &info->mac_address, &description); |
888943fc | 1185 | |
8d0e0af2 YW |
1186 | r = table_add_many(table, |
1187 | TABLE_EMPTY, | |
1188 | TABLE_STRING, "HW Address:"); | |
98d5bef3 YW |
1189 | if (r < 0) |
1190 | return r; | |
1191 | r = table_add_cell_stringf(table, NULL, "%s%s%s%s", | |
64e7ebde YW |
1192 | ether_addr_to_string(&info->mac_address, ea), |
1193 | description ? " (" : "", | |
1194 | strempty(description), | |
1195 | description ? ")" : ""); | |
98d5bef3 YW |
1196 | if (r < 0) |
1197 | return r; | |
db73295a | 1198 | } |
9085f64a | 1199 | |
c06ff86e YW |
1200 | if (info->mtu > 0) { |
1201 | char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)]; | |
1202 | ||
1203 | xsprintf(min_str, "%" PRIu32, info->min_mtu); | |
1204 | xsprintf(max_str, "%" PRIu32, info->max_mtu); | |
1205 | ||
8d0e0af2 YW |
1206 | r = table_add_many(table, |
1207 | TABLE_EMPTY, | |
1208 | TABLE_STRING, "MTU:"); | |
98d5bef3 YW |
1209 | if (r < 0) |
1210 | return r; | |
c06ff86e YW |
1211 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s", |
1212 | info->mtu, | |
1213 | info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "", | |
90e29fe1 | 1214 | info->min_mtu > 0 ? "min: " : "", |
c06ff86e YW |
1215 | info->min_mtu > 0 ? min_str : "", |
1216 | info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "", | |
90e29fe1 | 1217 | info->max_mtu > 0 ? "max: " : "", |
c06ff86e YW |
1218 | info->max_mtu > 0 ? max_str : "", |
1219 | info->min_mtu > 0 || info->max_mtu > 0 ? ")" : ""); | |
98d5bef3 YW |
1220 | if (r < 0) |
1221 | return r; | |
1222 | } | |
8eb9058d | 1223 | |
c82d1bf2 SS |
1224 | if (streq_ptr(info->netdev_kind, "bridge")) { |
1225 | r = table_add_many(table, | |
1226 | TABLE_EMPTY, | |
1227 | TABLE_STRING, "Forward Delay:", | |
1228 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay), | |
1229 | TABLE_EMPTY, | |
1230 | TABLE_STRING, "Hello Time:", | |
1231 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time), | |
1232 | TABLE_EMPTY, | |
1233 | TABLE_STRING, "Max Age:", | |
1234 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age), | |
1235 | TABLE_EMPTY, | |
1236 | TABLE_STRING, "Ageing Time:", | |
1237 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time), | |
1238 | TABLE_EMPTY, | |
1239 | TABLE_STRING, "Priority:", | |
1240 | TABLE_UINT16, info->priority, | |
1241 | TABLE_EMPTY, | |
1242 | TABLE_STRING, "STP:", | |
1243 | TABLE_BOOLEAN, info->stp_state > 0, | |
1244 | TABLE_EMPTY, | |
1245 | TABLE_STRING, "Multicast IGMP Version:", | |
1246 | TABLE_UINT8, info->mcast_igmp_version); | |
1247 | if (r < 0) | |
1248 | return r; | |
b24281aa SS |
1249 | |
1250 | } else if (streq_ptr(info->netdev_kind, "vxlan")) { | |
1251 | if (info->vxlan_info.vni > 0) { | |
1252 | r = table_add_many(table, | |
1253 | TABLE_EMPTY, | |
1254 | TABLE_STRING, "VNI:", | |
1255 | TABLE_UINT32, info->vxlan_info.vni); | |
1256 | if (r < 0) | |
1257 | return r; | |
1258 | } | |
1259 | ||
1260 | if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) { | |
1261 | r = table_add_many(table, | |
1262 | TABLE_EMPTY, | |
1263 | TABLE_STRING, "Group:", | |
1264 | info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, | |
1265 | &info->vxlan_info.group); | |
1266 | if (r < 0) | |
1267 | return r; | |
1268 | } | |
1269 | ||
1270 | if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) { | |
1271 | r = table_add_many(table, | |
1272 | TABLE_EMPTY, | |
1273 | TABLE_STRING, "Local:", | |
1274 | info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, | |
1275 | &info->vxlan_info.local); | |
1276 | if (r < 0) | |
1277 | return r; | |
1278 | } | |
1279 | ||
1280 | if (info->vxlan_info.dest_port > 0) { | |
1281 | r = table_add_many(table, | |
1282 | TABLE_EMPTY, | |
1283 | TABLE_STRING, "Destination Port:", | |
1284 | TABLE_UINT16, be16toh(info->vxlan_info.dest_port)); | |
1285 | if (r < 0) | |
1286 | return r; | |
1287 | } | |
1288 | ||
1289 | if (info->vxlan_info.link > 0) { | |
1290 | r = table_add_many(table, | |
1291 | TABLE_EMPTY, | |
1292 | TABLE_STRING, "Underlying Device:", | |
1293 | TABLE_IFINDEX, info->vxlan_info.link); | |
1294 | if (r < 0) | |
1295 | return r; | |
1296 | } | |
c82d1bf2 SS |
1297 | } |
1298 | ||
8d07de25 ZJS |
1299 | if (info->has_wlan_link_info) { |
1300 | _cleanup_free_ char *esc = NULL; | |
1301 | char buf[ETHER_ADDR_TO_STRING_MAX]; | |
1302 | ||
1303 | r = table_add_many(table, | |
1304 | TABLE_EMPTY, | |
1305 | TABLE_STRING, "WiFi access point:"); | |
1306 | if (r < 0) | |
1307 | return r; | |
1308 | ||
1309 | if (info->ssid) | |
1310 | esc = cescape(info->ssid); | |
1311 | ||
1312 | r = table_add_cell_stringf(table, NULL, "%s (%s)", | |
1313 | strnull(esc), | |
1314 | ether_addr_to_string(&info->bssid, buf)); | |
1315 | if (r < 0) | |
1316 | return r; | |
1317 | } | |
1318 | ||
335dd8ba | 1319 | if (info->has_bitrates) { |
42a63431 | 1320 | char tx[FORMAT_BYTES_MAX], rx[FORMAT_BYTES_MAX]; |
335dd8ba | 1321 | |
8d0e0af2 YW |
1322 | r = table_add_many(table, |
1323 | TABLE_EMPTY, | |
1324 | TABLE_STRING, "Bit Rate (Tx/Rx):"); | |
335dd8ba YW |
1325 | if (r < 0) |
1326 | return r; | |
42a63431 YW |
1327 | r = table_add_cell_stringf(table, NULL, "%sbps/%sbps", |
1328 | format_bytes_full(tx, sizeof tx, info->tx_bitrate, 0), | |
1329 | format_bytes_full(rx, sizeof rx, info->rx_bitrate, 0)); | |
335dd8ba YW |
1330 | if (r < 0) |
1331 | return r; | |
1332 | } | |
1333 | ||
98d5bef3 | 1334 | if (info->has_tx_queues || info->has_rx_queues) { |
8d0e0af2 YW |
1335 | r = table_add_many(table, |
1336 | TABLE_EMPTY, | |
1337 | TABLE_STRING, "Queue Length (Tx/Rx):"); | |
98d5bef3 YW |
1338 | if (r < 0) |
1339 | return r; | |
1340 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues); | |
1341 | if (r < 0) | |
1342 | return r; | |
1343 | } | |
0d4ad91d | 1344 | |
c967d2c7 YW |
1345 | if (info->has_ethtool_link_info) { |
1346 | const char *duplex = duplex_to_string(info->duplex); | |
1347 | const char *port = port_to_string(info->port); | |
1348 | ||
1349 | if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) { | |
8d0e0af2 YW |
1350 | r = table_add_many(table, |
1351 | TABLE_EMPTY, | |
1352 | TABLE_STRING, "Auto negotiation:", | |
1353 | TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE); | |
c967d2c7 YW |
1354 | if (r < 0) |
1355 | return r; | |
1356 | } | |
1357 | ||
1358 | if (info->speed > 0) { | |
8d0e0af2 YW |
1359 | r = table_add_many(table, |
1360 | TABLE_EMPTY, | |
1361 | TABLE_STRING, "Speed:", | |
45160216 | 1362 | TABLE_BPS, (uint64_t) info->speed); |
c967d2c7 YW |
1363 | if (r < 0) |
1364 | return r; | |
1365 | } | |
1366 | ||
1367 | if (duplex) { | |
8d0e0af2 YW |
1368 | r = table_add_many(table, |
1369 | TABLE_EMPTY, | |
1370 | TABLE_STRING, "Duplex:", | |
1371 | TABLE_STRING, duplex); | |
c967d2c7 YW |
1372 | if (r < 0) |
1373 | return r; | |
1374 | } | |
1375 | ||
1376 | if (port) { | |
8d0e0af2 YW |
1377 | r = table_add_many(table, |
1378 | TABLE_EMPTY, | |
1379 | TABLE_STRING, "Port:", | |
1380 | TABLE_STRING, port); | |
c967d2c7 YW |
1381 | if (r < 0) |
1382 | return r; | |
1383 | } | |
1384 | } | |
1385 | ||
98d5bef3 YW |
1386 | r = dump_addresses(rtnl, table, info->ifindex); |
1387 | if (r < 0) | |
1388 | return r; | |
1389 | r = dump_gateways(rtnl, hwdb, table, info->ifindex); | |
1390 | if (r < 0) | |
1391 | return r; | |
1392 | r = dump_list(table, "DNS:", dns); | |
1393 | if (r < 0) | |
1394 | return r; | |
1395 | r = dump_list(table, "Search Domains:", search_domains); | |
1396 | if (r < 0) | |
1397 | return r; | |
1398 | r = dump_list(table, "Route Domains:", route_domains); | |
1399 | if (r < 0) | |
1400 | return r; | |
1401 | r = dump_list(table, "NTP:", ntp); | |
1402 | if (r < 0) | |
1403 | return r; | |
1404 | r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to); | |
1405 | if (r < 0) | |
1406 | return r; | |
1407 | r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by); | |
1408 | if (r < 0) | |
1409 | return r; | |
9085f64a | 1410 | |
b147503e | 1411 | (void) sd_network_link_get_timezone(info->ifindex, &tz); |
98d5bef3 | 1412 | if (tz) { |
8d0e0af2 YW |
1413 | r = table_add_many(table, |
1414 | TABLE_EMPTY, | |
1415 | TABLE_STRING, "Time Zone:", | |
1416 | TABLE_STRING, tz); | |
98d5bef3 YW |
1417 | if (r < 0) |
1418 | return r; | |
1419 | } | |
8eb9058d | 1420 | |
98d5bef3 YW |
1421 | r = dump_lldp_neighbors(table, "Connected To:", info->ifindex); |
1422 | if (r < 0) | |
1423 | return r; | |
837f57da | 1424 | |
a459b24f YW |
1425 | r = dump_statistics(table, info); |
1426 | if (r < 0) | |
1427 | return r; | |
1428 | ||
98d5bef3 | 1429 | return table_print(table, NULL); |
9085f64a LP |
1430 | } |
1431 | ||
0070333f LP |
1432 | static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) { |
1433 | _cleanup_free_ char *operational_state = NULL; | |
1434 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; | |
1435 | const char *on_color_operational, *off_color_operational; | |
98d5bef3 YW |
1436 | _cleanup_(table_unrefp) Table *table = NULL; |
1437 | TableCell *cell; | |
1438 | int r; | |
0070333f LP |
1439 | |
1440 | assert(rtnl); | |
1441 | ||
4abd866d | 1442 | (void) sd_network_get_operational_state(&operational_state); |
ceb366df | 1443 | operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational); |
0070333f | 1444 | |
4252171a | 1445 | table = table_new("dot", "key", "value"); |
98d5bef3 YW |
1446 | if (!table) |
1447 | return -ENOMEM; | |
1448 | ||
81914d9f YW |
1449 | assert_se(cell = table_get_cell(table, 0, 0)); |
1450 | (void) table_set_ellipsize_percent(table, cell, 100); | |
1451 | ||
1452 | assert_se(cell = table_get_cell(table, 0, 1)); | |
1453 | (void) table_set_align_percent(table, cell, 100); | |
1454 | (void) table_set_ellipsize_percent(table, cell, 100); | |
1455 | ||
98d5bef3 YW |
1456 | table_set_header(table, false); |
1457 | ||
8d0e0af2 YW |
1458 | r = table_add_many(table, |
1459 | TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), | |
1460 | TABLE_SET_COLOR, on_color_operational, | |
1461 | TABLE_STRING, "State:", | |
1462 | TABLE_STRING, strna(operational_state), | |
1463 | TABLE_SET_COLOR, on_color_operational); | |
0070333f | 1464 | |
98d5bef3 YW |
1465 | r = dump_addresses(rtnl, table, 0); |
1466 | if (r < 0) | |
1467 | return r; | |
1468 | r = dump_gateways(rtnl, hwdb, table, 0); | |
1469 | if (r < 0) | |
1470 | return r; | |
0070333f | 1471 | |
4abd866d | 1472 | (void) sd_network_get_dns(&dns); |
98d5bef3 YW |
1473 | r = dump_list(table, "DNS:", dns); |
1474 | if (r < 0) | |
1475 | return r; | |
0070333f | 1476 | |
4abd866d | 1477 | (void) sd_network_get_search_domains(&search_domains); |
98d5bef3 YW |
1478 | r = dump_list(table, "Search Domains:", search_domains); |
1479 | if (r < 0) | |
1480 | return r; | |
0070333f | 1481 | |
4abd866d | 1482 | (void) sd_network_get_route_domains(&route_domains); |
98d5bef3 YW |
1483 | r = dump_list(table, "Route Domains:", route_domains); |
1484 | if (r < 0) | |
1485 | return r; | |
0070333f | 1486 | |
4abd866d | 1487 | (void) sd_network_get_ntp(&ntp); |
98d5bef3 YW |
1488 | r = dump_list(table, "NTP:", ntp); |
1489 | if (r < 0) | |
1490 | return r; | |
0070333f | 1491 | |
98d5bef3 | 1492 | return table_print(table, NULL); |
0070333f LP |
1493 | } |
1494 | ||
266b5389 | 1495 | static int link_status(int argc, char *argv[], void *userdata) { |
335dd8ba | 1496 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
4afd3348 | 1497 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b147503e | 1498 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
172353b1 | 1499 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
b147503e | 1500 | int r, c, i; |
ee8c4568 | 1501 | |
0221d68a | 1502 | (void) pager_open(arg_pager_flags); |
0070333f | 1503 | |
335dd8ba YW |
1504 | r = sd_bus_open_system(&bus); |
1505 | if (r < 0) | |
1506 | return log_error_errno(r, "Failed to connect system bus: %m"); | |
1507 | ||
1c4baffc | 1508 | r = sd_netlink_open(&rtnl); |
f647962d MS |
1509 | if (r < 0) |
1510 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
f7d68aa8 | 1511 | |
81fd1dd3 TG |
1512 | r = sd_hwdb_new(&hwdb); |
1513 | if (r < 0) | |
1514 | log_debug_errno(r, "Failed to open hardware database: %m"); | |
69fb1176 | 1515 | |
b147503e | 1516 | if (arg_all) |
335dd8ba | 1517 | c = acquire_link_info(bus, rtnl, NULL, &links); |
b147503e | 1518 | else if (argc <= 1) |
0070333f | 1519 | return system_status(rtnl, hwdb); |
b147503e | 1520 | else |
335dd8ba | 1521 | c = acquire_link_info(bus, rtnl, argv + 1, &links); |
b147503e LP |
1522 | if (c < 0) |
1523 | return c; | |
ee8c4568 | 1524 | |
b147503e LP |
1525 | for (i = 0; i < c; i++) { |
1526 | if (i > 0) | |
1527 | fputc('\n', stdout); | |
ee8c4568 | 1528 | |
b147503e | 1529 | link_status_one(rtnl, hwdb, links + i); |
ee8c4568 LP |
1530 | } |
1531 | ||
1532 | return 0; | |
1533 | } | |
1534 | ||
34437b4f LP |
1535 | static char *lldp_capabilities_to_string(uint16_t x) { |
1536 | static const char characters[] = { | |
1537 | 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm', | |
1538 | }; | |
1539 | char *ret; | |
1540 | unsigned i; | |
49699bac | 1541 | |
34437b4f LP |
1542 | ret = new(char, ELEMENTSOF(characters) + 1); |
1543 | if (!ret) | |
49699bac SS |
1544 | return NULL; |
1545 | ||
34437b4f LP |
1546 | for (i = 0; i < ELEMENTSOF(characters); i++) |
1547 | ret[i] = (x & (1U << i)) ? characters[i] : '.'; | |
49699bac | 1548 | |
34437b4f LP |
1549 | ret[i] = 0; |
1550 | return ret; | |
49699bac SS |
1551 | } |
1552 | ||
4c3160f1 ZJS |
1553 | static void lldp_capabilities_legend(uint16_t x) { |
1554 | unsigned w, i, cols = columns(); | |
404d53a9 | 1555 | static const char* const table[] = { |
4c3160f1 ZJS |
1556 | "o - Other", |
1557 | "p - Repeater", | |
1558 | "b - Bridge", | |
1559 | "w - WLAN Access Point", | |
1560 | "r - Router", | |
1561 | "t - Telephone", | |
1562 | "d - DOCSIS cable device", | |
1563 | "a - Station", | |
1564 | "c - Customer VLAN", | |
1565 | "s - Service VLAN", | |
1566 | "m - Two-port MAC Relay (TPMR)", | |
1567 | }; | |
1568 | ||
1569 | if (x == 0) | |
1570 | return; | |
1571 | ||
1572 | printf("\nCapability Flags:\n"); | |
1573 | for (w = 0, i = 0; i < ELEMENTSOF(table); i++) | |
1574 | if (x & (1U << i) || arg_all) { | |
1575 | bool newline; | |
1576 | ||
1577 | newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols; | |
1578 | if (newline) | |
1579 | w = 0; | |
1580 | w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]); | |
1581 | } | |
1582 | puts(""); | |
1583 | } | |
1584 | ||
49699bac | 1585 | static int link_lldp_status(int argc, char *argv[], void *userdata) { |
b147503e | 1586 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
172353b1 | 1587 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
658e9106 | 1588 | _cleanup_(table_unrefp) Table *table = NULL; |
b147503e | 1589 | int i, r, c, m = 0; |
4c3160f1 | 1590 | uint16_t all = 0; |
658e9106 | 1591 | TableCell *cell; |
b147503e LP |
1592 | |
1593 | r = sd_netlink_open(&rtnl); | |
1594 | if (r < 0) | |
1595 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
49699bac | 1596 | |
335dd8ba | 1597 | c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
49699bac | 1598 | if (c < 0) |
7d367b45 LP |
1599 | return c; |
1600 | ||
0221d68a | 1601 | (void) pager_open(arg_pager_flags); |
49699bac | 1602 | |
4252171a ZJS |
1603 | table = table_new("link", |
1604 | "chassis id", | |
1605 | "system name", | |
1606 | "caps", | |
1607 | "port id", | |
1608 | "port description"); | |
658e9106 YW |
1609 | if (!table) |
1610 | return -ENOMEM; | |
1611 | ||
1612 | table_set_header(table, arg_legend); | |
1613 | ||
1614 | assert_se(cell = table_get_cell(table, 0, 0)); | |
1615 | table_set_minimum_width(table, cell, 16); | |
1616 | ||
1617 | assert_se(cell = table_get_cell(table, 0, 1)); | |
1618 | table_set_minimum_width(table, cell, 17); | |
1619 | ||
1620 | assert_se(cell = table_get_cell(table, 0, 2)); | |
1621 | table_set_minimum_width(table, cell, 16); | |
1622 | ||
1623 | assert_se(cell = table_get_cell(table, 0, 3)); | |
1624 | table_set_minimum_width(table, cell, 11); | |
1625 | ||
1626 | assert_se(cell = table_get_cell(table, 0, 4)); | |
1627 | table_set_minimum_width(table, cell, 17); | |
1628 | ||
1629 | assert_se(cell = table_get_cell(table, 0, 5)); | |
1630 | table_set_minimum_width(table, cell, 16); | |
49699bac | 1631 | |
b147503e | 1632 | for (i = 0; i < c; i++) { |
34437b4f | 1633 | _cleanup_fclose_ FILE *f = NULL; |
49699bac | 1634 | |
837f57da LP |
1635 | r = open_lldp_neighbors(links[i].ifindex, &f); |
1636 | if (r == -ENOENT) | |
1637 | continue; | |
1638 | if (r < 0) { | |
1639 | log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex); | |
34437b4f LP |
1640 | continue; |
1641 | } | |
49699bac | 1642 | |
34437b4f | 1643 | for (;;) { |
e2835111 YW |
1644 | _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL; |
1645 | const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL; | |
34437b4f | 1646 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; |
34437b4f | 1647 | uint16_t cc; |
49699bac | 1648 | |
837f57da | 1649 | r = next_lldp_neighbor(f, &n); |
34437b4f | 1650 | if (r < 0) { |
837f57da | 1651 | log_warning_errno(r, "Failed to read neighbor data: %m"); |
34437b4f | 1652 | break; |
49699bac | 1653 | } |
837f57da LP |
1654 | if (r == 0) |
1655 | break; | |
34437b4f LP |
1656 | |
1657 | (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id); | |
1658 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
1659 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); | |
1660 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
1661 | ||
d08191a2 LP |
1662 | if (chassis_id) { |
1663 | cid = ellipsize(chassis_id, 17, 100); | |
1664 | if (cid) | |
1665 | chassis_id = cid; | |
1666 | } | |
1667 | ||
1668 | if (port_id) { | |
1669 | pid = ellipsize(port_id, 17, 100); | |
1670 | if (pid) | |
1671 | port_id = pid; | |
1672 | } | |
1673 | ||
1674 | if (system_name) { | |
1675 | sname = ellipsize(system_name, 16, 100); | |
1676 | if (sname) | |
1677 | system_name = sname; | |
1678 | } | |
1679 | ||
1680 | if (port_description) { | |
1681 | pdesc = ellipsize(port_description, 16, 100); | |
1682 | if (pdesc) | |
1683 | port_description = pdesc; | |
1684 | } | |
1685 | ||
4c3160f1 | 1686 | if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) { |
34437b4f | 1687 | capabilities = lldp_capabilities_to_string(cc); |
4c3160f1 ZJS |
1688 | all |= cc; |
1689 | } | |
34437b4f | 1690 | |
658e9106 YW |
1691 | r = table_add_many(table, |
1692 | TABLE_STRING, links[i].name, | |
1693 | TABLE_STRING, strna(chassis_id), | |
1694 | TABLE_STRING, strna(system_name), | |
1695 | TABLE_STRING, strna(capabilities), | |
1696 | TABLE_STRING, strna(port_id), | |
1697 | TABLE_STRING, strna(port_description)); | |
1698 | if (r < 0) | |
1699 | return r; | |
b147503e LP |
1700 | |
1701 | m++; | |
49699bac SS |
1702 | } |
1703 | } | |
1704 | ||
658e9106 YW |
1705 | r = table_print(table, NULL); |
1706 | if (r < 0) | |
1707 | return r; | |
1708 | ||
4c3160f1 ZJS |
1709 | if (arg_legend) { |
1710 | lldp_capabilities_legend(all); | |
1711 | printf("\n%i neighbors listed.\n", m); | |
1712 | } | |
49699bac SS |
1713 | |
1714 | return 0; | |
1715 | } | |
1716 | ||
9cd8c766 SS |
1717 | static int link_delete_send_message(sd_netlink *rtnl, int index) { |
1718 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1719 | int r; | |
1720 | ||
1721 | assert(rtnl); | |
1722 | ||
1723 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index); | |
1724 | if (r < 0) | |
1725 | return rtnl_log_create_error(r); | |
1726 | ||
1727 | r = sd_netlink_call(rtnl, req, 0, NULL); | |
1728 | if (r < 0) | |
1729 | return r; | |
1730 | ||
1731 | return 0; | |
1732 | } | |
1733 | ||
1734 | static int link_delete(int argc, char *argv[], void *userdata) { | |
1735 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
1736 | _cleanup_set_free_ Set *indexes = NULL; | |
9cd8c766 SS |
1737 | int index, r, i; |
1738 | Iterator j; | |
38b9af61 | 1739 | void *p; |
9cd8c766 SS |
1740 | |
1741 | r = sd_netlink_open(&rtnl); | |
1742 | if (r < 0) | |
1743 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
1744 | ||
1745 | indexes = set_new(NULL); | |
1746 | if (!indexes) | |
1747 | return log_oom(); | |
1748 | ||
1749 | for (i = 1; i < argc; i++) { | |
4bddccc6 YW |
1750 | r = parse_ifindex_or_ifname(argv[i], &index); |
1751 | if (r < 0) | |
1752 | return log_error_errno(r, "Failed to resolve interface %s", argv[i]); | |
9cd8c766 SS |
1753 | |
1754 | r = set_put(indexes, INT_TO_PTR(index)); | |
1755 | if (r < 0) | |
1756 | return log_oom(); | |
1757 | } | |
1758 | ||
38b9af61 | 1759 | SET_FOREACH(p, indexes, j) { |
d56d6cb8 YW |
1760 | index = PTR_TO_INT(p); |
1761 | r = link_delete_send_message(rtnl, index); | |
9cd8c766 | 1762 | if (r < 0) { |
518a66ec YW |
1763 | char ifname[IF_NAMESIZE + 1]; |
1764 | ||
e4857ee2 YW |
1765 | return log_error_errno(r, "Failed to delete interface %s: %m", |
1766 | format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX)); | |
9cd8c766 SS |
1767 | } |
1768 | } | |
1769 | ||
1770 | return r; | |
1771 | } | |
1772 | ||
308e7dfd YW |
1773 | static int link_renew_one(sd_bus *bus, int index, const char *name) { |
1774 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1775 | int r; | |
1776 | ||
1777 | r = sd_bus_call_method( | |
1778 | bus, | |
1779 | "org.freedesktop.network1", | |
1780 | "/org/freedesktop/network1", | |
1781 | "org.freedesktop.network1.Manager", | |
1782 | "RenewLink", | |
1783 | &error, | |
1784 | NULL, | |
1785 | "i", index); | |
1786 | if (r < 0) | |
1787 | return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s", | |
1788 | name, bus_error_message(&error, r)); | |
1789 | ||
1790 | return 0; | |
1791 | } | |
1792 | ||
1793 | static int link_renew(int argc, char *argv[], void *userdata) { | |
1794 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1795 | int index, i, k = 0, r; | |
1796 | ||
1797 | r = sd_bus_open_system(&bus); | |
1798 | if (r < 0) | |
1799 | return log_error_errno(r, "Failed to connect system bus: %m"); | |
1800 | ||
1801 | for (i = 1; i < argc; i++) { | |
1802 | r = parse_ifindex_or_ifname(argv[i], &index); | |
1803 | if (r < 0) | |
1804 | return log_error_errno(r, "Failed to resolve interface %s", argv[i]); | |
1805 | ||
1806 | r = link_renew_one(bus, index, argv[i]); | |
1807 | if (r < 0 && k >= 0) | |
1808 | k = r; | |
1809 | } | |
1810 | ||
1811 | return k; | |
1812 | } | |
1813 | ||
a227674c YW |
1814 | static int verb_reload(int argc, char *argv[], void *userdata) { |
1815 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1816 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1817 | int r; | |
1818 | ||
1819 | r = sd_bus_open_system(&bus); | |
1820 | if (r < 0) | |
1821 | return log_error_errno(r, "Failed to connect system bus: %m"); | |
1822 | ||
1823 | r = sd_bus_call_method( | |
1824 | bus, | |
1825 | "org.freedesktop.network1", | |
1826 | "/org/freedesktop/network1", | |
1827 | "org.freedesktop.network1.Manager", | |
1828 | "Reload", | |
1829 | &error, NULL, NULL); | |
1830 | if (r < 0) | |
1831 | return log_error_errno(r, "Failed to reload network settings: %m"); | |
1832 | ||
1833 | return 0; | |
1834 | } | |
1835 | ||
8dc85c5e YW |
1836 | static int verb_reconfigure(int argc, char *argv[], void *userdata) { |
1837 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1838 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1839 | _cleanup_set_free_ Set *indexes = NULL; | |
1840 | int index, i, r; | |
1841 | Iterator j; | |
1842 | void *p; | |
1843 | ||
1844 | r = sd_bus_open_system(&bus); | |
1845 | if (r < 0) | |
1846 | return log_error_errno(r, "Failed to connect system bus: %m"); | |
1847 | ||
1848 | indexes = set_new(NULL); | |
1849 | if (!indexes) | |
1850 | return log_oom(); | |
1851 | ||
1852 | for (i = 1; i < argc; i++) { | |
1853 | r = parse_ifindex_or_ifname(argv[i], &index); | |
1854 | if (r < 0) | |
1855 | return log_error_errno(r, "Failed to resolve interface %s", argv[i]); | |
1856 | ||
1857 | r = set_put(indexes, INT_TO_PTR(index)); | |
1858 | if (r < 0) | |
1859 | return log_oom(); | |
1860 | } | |
1861 | ||
1862 | SET_FOREACH(p, indexes, j) { | |
1863 | index = PTR_TO_INT(p); | |
1864 | r = sd_bus_call_method( | |
1865 | bus, | |
1866 | "org.freedesktop.network1", | |
1867 | "/org/freedesktop/network1", | |
1868 | "org.freedesktop.network1.Manager", | |
1869 | "ReconfigureLink", | |
1870 | &error, NULL, "i", index); | |
1871 | if (r < 0) { | |
1872 | char ifname[IF_NAMESIZE + 1]; | |
1873 | ||
1874 | return log_error_errno(r, "Failed to reconfigure network interface %s: %m", format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX)); | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | return 0; | |
1879 | } | |
1880 | ||
37ec0fdd LP |
1881 | static int help(void) { |
1882 | _cleanup_free_ char *link = NULL; | |
1883 | int r; | |
1884 | ||
1885 | r = terminal_urlify_man("networkctl", "1", &link); | |
1886 | if (r < 0) | |
1887 | return log_oom(); | |
1888 | ||
353b2baa LP |
1889 | printf("%s [OPTIONS...] COMMAND\n\n" |
1890 | "%sQuery and control the networking subsystem.%s\n" | |
a459b24f | 1891 | "\nCommands:\n" |
8dc85c5e YW |
1892 | " list [PATTERN...] List links\n" |
1893 | " status [PATTERN...] Show link status\n" | |
1894 | " lldp [PATTERN...] Show LLDP neighbors\n" | |
1895 | " label Show current address label entries in the kernel\n" | |
1896 | " delete DEVICES... Delete virtual netdevs\n" | |
1897 | " renew DEVICES... Renew dynamic configurations\n" | |
1898 | " reconfigure DEVICES... Reconfigure interfaces\n" | |
1899 | " reload Reload .network and .netdev files\n" | |
353b2baa | 1900 | "\nOptions:\n" |
8dc85c5e YW |
1901 | " -h --help Show this help\n" |
1902 | " --version Show package version\n" | |
1903 | " --no-pager Do not pipe output into a pager\n" | |
1904 | " --no-legend Do not show the headers and footers\n" | |
1905 | " -a --all Show status for all links\n" | |
1906 | " -s --stats Show detailed link statics\n" | |
37ec0fdd LP |
1907 | "\nSee the %s for details.\n" |
1908 | , program_invocation_short_name | |
353b2baa | 1909 | , ansi_highlight() |
ce2529b4 | 1910 | , ansi_normal() |
37ec0fdd LP |
1911 | , link |
1912 | ); | |
1913 | ||
1914 | return 0; | |
ee8c4568 LP |
1915 | } |
1916 | ||
1917 | static int parse_argv(int argc, char *argv[]) { | |
1918 | ||
1919 | enum { | |
1920 | ARG_VERSION = 0x100, | |
1921 | ARG_NO_PAGER, | |
1922 | ARG_NO_LEGEND, | |
1923 | }; | |
1924 | ||
1925 | static const struct option options[] = { | |
1926 | { "help", no_argument, NULL, 'h' }, | |
1927 | { "version", no_argument, NULL, ARG_VERSION }, | |
1928 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
1929 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
9085f64a | 1930 | { "all", no_argument, NULL, 'a' }, |
a459b24f | 1931 | { "stats", no_argument, NULL, 's' }, |
ee8c4568 LP |
1932 | {} |
1933 | }; | |
1934 | ||
1935 | int c; | |
1936 | ||
1937 | assert(argc >= 0); | |
1938 | assert(argv); | |
1939 | ||
a459b24f | 1940 | while ((c = getopt_long(argc, argv, "has", options, NULL)) >= 0) { |
ee8c4568 LP |
1941 | |
1942 | switch (c) { | |
1943 | ||
1944 | case 'h': | |
37ec0fdd | 1945 | return help(); |
ee8c4568 LP |
1946 | |
1947 | case ARG_VERSION: | |
3f6fd1ba | 1948 | return version(); |
ee8c4568 LP |
1949 | |
1950 | case ARG_NO_PAGER: | |
0221d68a | 1951 | arg_pager_flags |= PAGER_DISABLE; |
ee8c4568 LP |
1952 | break; |
1953 | ||
1954 | case ARG_NO_LEGEND: | |
1955 | arg_legend = false; | |
1956 | break; | |
1957 | ||
9085f64a LP |
1958 | case 'a': |
1959 | arg_all = true; | |
1960 | break; | |
1961 | ||
a459b24f YW |
1962 | case 's': |
1963 | arg_stats = true; | |
1964 | break; | |
1965 | ||
ee8c4568 LP |
1966 | case '?': |
1967 | return -EINVAL; | |
1968 | ||
1969 | default: | |
1970 | assert_not_reached("Unhandled option"); | |
1971 | } | |
1972 | } | |
1973 | ||
1974 | return 1; | |
1975 | } | |
1976 | ||
1977 | static int networkctl_main(int argc, char *argv[]) { | |
15c3626e | 1978 | static const Verb verbs[] = { |
8dc85c5e YW |
1979 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links }, |
1980 | { "status", VERB_ANY, VERB_ANY, 0, link_status }, | |
1981 | { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status }, | |
1982 | { "label", VERB_ANY, VERB_ANY, 0, list_address_labels }, | |
1983 | { "delete", 2, VERB_ANY, 0, link_delete }, | |
1984 | { "renew", 2, VERB_ANY, 0, link_renew }, | |
1985 | { "reconfigure", 2, VERB_ANY, 0, verb_reconfigure }, | |
1986 | { "reload", 1, 1, 0, verb_reload }, | |
266b5389 | 1987 | {} |
ee8c4568 LP |
1988 | }; |
1989 | ||
266b5389 | 1990 | return dispatch_verb(argc, argv, verbs, NULL); |
ee8c4568 LP |
1991 | } |
1992 | ||
58fb3678 LP |
1993 | static void warn_networkd_missing(void) { |
1994 | ||
1995 | if (access("/run/systemd/netif/state", F_OK) >= 0) | |
1996 | return; | |
1997 | ||
1998 | fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n"); | |
1999 | } | |
2000 | ||
4e2ca442 | 2001 | static int run(int argc, char* argv[]) { |
ee8c4568 LP |
2002 | int r; |
2003 | ||
1a043959 | 2004 | log_show_color(true); |
ee8c4568 LP |
2005 | log_parse_environment(); |
2006 | log_open(); | |
2007 | ||
2008 | r = parse_argv(argc, argv); | |
2009 | if (r <= 0) | |
4e2ca442 | 2010 | return r; |
ee8c4568 | 2011 | |
58fb3678 LP |
2012 | warn_networkd_missing(); |
2013 | ||
4e2ca442 | 2014 | return networkctl_main(argc, argv); |
ee8c4568 | 2015 | } |
4e2ca442 ZJS |
2016 | |
2017 | DEFINE_MAIN_FUNCTION(run); |