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