]>
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 | |
914d6c09 | 11 | #include "sd-device.h" |
3f6fd1ba | 12 | #include "sd-hwdb.h" |
34437b4f | 13 | #include "sd-lldp.h" |
3f6fd1ba LP |
14 | #include "sd-netlink.h" |
15 | #include "sd-network.h" | |
ee8c4568 | 16 | |
b5efdb8a | 17 | #include "alloc-util.h" |
3f6fd1ba | 18 | #include "arphrd-list.h" |
914d6c09 | 19 | #include "device-util.h" |
3f6fd1ba | 20 | #include "ether-addr-util.h" |
34437b4f | 21 | #include "fd-util.h" |
81fd1dd3 | 22 | #include "hwdb-util.h" |
ee8c4568 | 23 | #include "local-addresses.h" |
8752c575 | 24 | #include "locale-util.h" |
d37b7627 | 25 | #include "macro.h" |
4e2ca442 | 26 | #include "main-func.h" |
3f6fd1ba LP |
27 | #include "netlink-util.h" |
28 | #include "pager.h" | |
6bedfcbb | 29 | #include "parse-util.h" |
294bf0c3 | 30 | #include "pretty-print.h" |
db73295a | 31 | #include "socket-util.h" |
760877e9 | 32 | #include "sort-util.h" |
34437b4f | 33 | #include "sparse-endian.h" |
d054f0a4 | 34 | #include "stdio-util.h" |
8b43440b | 35 | #include "string-table.h" |
8752c575 | 36 | #include "string-util.h" |
3f6fd1ba | 37 | #include "strv.h" |
2388b2f4 | 38 | #include "strxcpyx.h" |
288a74cc | 39 | #include "terminal-util.h" |
3f6fd1ba | 40 | #include "verbs.h" |
ee8c4568 | 41 | |
0221d68a | 42 | static PagerFlags arg_pager_flags = 0; |
ee8c4568 | 43 | static bool arg_legend = true; |
9085f64a | 44 | static bool arg_all = false; |
ee8c4568 | 45 | |
b55822c3 | 46 | static char *link_get_type_string(unsigned short iftype, sd_device *d) { |
2c740afd | 47 | const char *t, *devtype; |
ee8c4568 LP |
48 | char *p; |
49 | ||
2c740afd YW |
50 | if (d && |
51 | sd_device_get_devtype(d, &devtype) >= 0 && | |
52 | !isempty(devtype)) | |
53 | return strdup(devtype); | |
ee8c4568 LP |
54 | |
55 | t = arphrd_to_name(iftype); | |
b55822c3 JD |
56 | if (!t) |
57 | return NULL; | |
ee8c4568 LP |
58 | |
59 | p = strdup(t); | |
60 | if (!p) | |
b55822c3 | 61 | return NULL; |
ee8c4568 LP |
62 | |
63 | ascii_strlower(p); | |
b55822c3 | 64 | return p; |
ee8c4568 LP |
65 | } |
66 | ||
7e5a080a LP |
67 | static void operational_state_to_color(const char *state, const char **on, const char **off) { |
68 | assert(on); | |
69 | assert(off); | |
70 | ||
85323805 | 71 | if (STRPTR_IN_SET(state, "routable", "enslaved")) { |
7e5a080a LP |
72 | *on = ansi_highlight_green(); |
73 | *off = ansi_normal(); | |
74 | } else if (streq_ptr(state, "degraded")) { | |
75 | *on = ansi_highlight_yellow(); | |
76 | *off = ansi_normal(); | |
77 | } else | |
78 | *on = *off = ""; | |
79 | } | |
80 | ||
81 | static void setup_state_to_color(const char *state, const char **on, const char **off) { | |
82 | assert(on); | |
83 | assert(off); | |
84 | ||
85 | if (streq_ptr(state, "configured")) { | |
86 | *on = ansi_highlight_green(); | |
87 | *off = ansi_normal(); | |
88 | } else if (streq_ptr(state, "configuring")) { | |
89 | *on = ansi_highlight_yellow(); | |
90 | *off = ansi_normal(); | |
1cf03a4f | 91 | } else if (STRPTR_IN_SET(state, "failed", "linger")) { |
7e5a080a LP |
92 | *on = ansi_highlight_red(); |
93 | *off = ansi_normal(); | |
94 | } else | |
95 | *on = *off = ""; | |
96 | } | |
97 | ||
6d0c65ff | 98 | typedef struct LinkInfo { |
e997c4b0 | 99 | char name[IFNAMSIZ+1]; |
6d0c65ff | 100 | int ifindex; |
1c4a6088 | 101 | unsigned short iftype; |
b147503e LP |
102 | struct ether_addr mac_address; |
103 | uint32_t mtu; | |
2c73f59b SS |
104 | uint32_t min_mtu; |
105 | uint32_t max_mtu; | |
b147503e LP |
106 | |
107 | bool has_mac_address:1; | |
108 | bool has_mtu:1; | |
2c73f59b SS |
109 | bool has_min_mtu:1; |
110 | bool has_max_mtu:1; | |
6d0c65ff LP |
111 | } LinkInfo; |
112 | ||
93bab288 YW |
113 | static int link_info_compare(const LinkInfo *a, const LinkInfo *b) { |
114 | return CMP(a->ifindex, b->ifindex); | |
6d0c65ff LP |
115 | } |
116 | ||
a6962904 | 117 | static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) { |
e997c4b0 | 118 | const char *name; |
e997c4b0 | 119 | uint16_t type; |
a6962904 | 120 | int ifindex, r; |
6d0c65ff | 121 | |
e997c4b0 LP |
122 | assert(m); |
123 | assert(info); | |
6d0c65ff | 124 | |
e997c4b0 LP |
125 | r = sd_netlink_message_get_type(m, &type); |
126 | if (r < 0) | |
127 | return r; | |
6d0c65ff | 128 | |
e997c4b0 LP |
129 | if (type != RTM_NEWLINK) |
130 | return 0; | |
6d0c65ff | 131 | |
a6962904 | 132 | r = sd_rtnl_message_link_get_ifindex(m, &ifindex); |
e997c4b0 LP |
133 | if (r < 0) |
134 | return r; | |
6d0c65ff | 135 | |
e997c4b0 LP |
136 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name); |
137 | if (r < 0) | |
138 | return r; | |
6d0c65ff | 139 | |
a6962904 YW |
140 | if (patterns) { |
141 | char str[DECIMAL_STR_MAX(int)]; | |
142 | ||
143 | xsprintf(str, "%i", ifindex); | |
144 | ||
145 | if (!strv_fnmatch(patterns, str, 0) && !strv_fnmatch(patterns, name, 0)) | |
146 | return 0; | |
147 | } | |
148 | ||
b147503e | 149 | r = sd_rtnl_message_link_get_type(m, &info->iftype); |
e997c4b0 LP |
150 | if (r < 0) |
151 | return r; | |
6d0c65ff | 152 | |
2388b2f4 | 153 | strscpy(info->name, sizeof info->name, name); |
a6962904 | 154 | info->ifindex = ifindex; |
b147503e LP |
155 | |
156 | info->has_mac_address = | |
157 | sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 && | |
72e551f4 | 158 | memcmp(&info->mac_address, ÐER_ADDR_NULL, sizeof(struct ether_addr)) != 0; |
b147503e LP |
159 | |
160 | info->has_mtu = | |
4015d106 | 161 | sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu) >= 0 && |
b147503e | 162 | info->mtu > 0; |
e997c4b0 | 163 | |
2c73f59b SS |
164 | info->has_min_mtu = |
165 | sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu) >= 0 && | |
166 | info->min_mtu > 0; | |
167 | ||
168 | info->has_max_mtu = | |
169 | sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu) >= 0 && | |
170 | info->min_mtu > 0; | |
171 | ||
e997c4b0 LP |
172 | return 1; |
173 | } | |
174 | ||
a6962904 | 175 | static int acquire_link_info(sd_netlink *rtnl, char **patterns, LinkInfo **ret) { |
4afd3348 | 176 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
7e5a080a LP |
177 | _cleanup_free_ LinkInfo *links = NULL; |
178 | size_t allocated = 0, c = 0; | |
179 | sd_netlink_message *i; | |
7d367b45 LP |
180 | int r; |
181 | ||
b147503e | 182 | assert(rtnl); |
7d367b45 | 183 | assert(ret); |
ee8c4568 | 184 | |
ee8c4568 LP |
185 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); |
186 | if (r < 0) | |
187 | return rtnl_log_create_error(r); | |
188 | ||
1c4baffc | 189 | r = sd_netlink_message_request_dump(req, true); |
ee8c4568 LP |
190 | if (r < 0) |
191 | return rtnl_log_create_error(r); | |
192 | ||
1c4baffc | 193 | r = sd_netlink_call(rtnl, req, 0, &reply); |
f647962d MS |
194 | if (r < 0) |
195 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
ee8c4568 | 196 | |
7e5a080a LP |
197 | for (i = reply; i; i = sd_netlink_message_next(i)) { |
198 | if (!GREEDY_REALLOC(links, allocated, c+1)) | |
199 | return -ENOMEM; | |
7d367b45 | 200 | |
a6962904 | 201 | r = decode_link(i, links + c, patterns); |
7e5a080a LP |
202 | if (r < 0) |
203 | return r; | |
204 | if (r > 0) | |
205 | c++; | |
206 | } | |
207 | ||
93bab288 | 208 | typesafe_qsort(links, c, link_info_compare); |
7e5a080a | 209 | |
1cc6c93a | 210 | *ret = TAKE_PTR(links); |
7e5a080a LP |
211 | |
212 | return (int) c; | |
7d367b45 | 213 | } |
ee8c4568 | 214 | |
7d367b45 | 215 | static int list_links(int argc, char *argv[], void *userdata) { |
b147503e | 216 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
7d367b45 | 217 | _cleanup_free_ LinkInfo *links = NULL; |
b147503e LP |
218 | int c, i, r; |
219 | ||
220 | r = sd_netlink_open(&rtnl); | |
221 | if (r < 0) | |
222 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
7d367b45 | 223 | |
a6962904 | 224 | c = acquire_link_info(rtnl, argc > 1 ? argv + 1 : NULL, &links); |
6d0c65ff | 225 | if (c < 0) |
7d367b45 LP |
226 | return c; |
227 | ||
0221d68a | 228 | (void) pager_open(arg_pager_flags); |
7d367b45 LP |
229 | |
230 | if (arg_legend) | |
c9cc0383 | 231 | printf("%3s %-16s %-18s %-16s %-10s\n", |
7d367b45 LP |
232 | "IDX", |
233 | "LINK", | |
234 | "TYPE", | |
235 | "OPERATIONAL", | |
236 | "SETUP"); | |
6d0c65ff LP |
237 | |
238 | for (i = 0; i < c; i++) { | |
ab1525bc | 239 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL; |
4afd3348 | 240 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
d57c365b LP |
241 | const char *on_color_operational, *off_color_operational, |
242 | *on_color_setup, *off_color_setup; | |
7d6884b6 | 243 | char devid[2 + DECIMAL_STR_MAX(int)]; |
ee8c4568 | 244 | _cleanup_free_ char *t = NULL; |
ee8c4568 | 245 | |
4abd866d | 246 | (void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state); |
d57c365b LP |
247 | operational_state_to_color(operational_state, &on_color_operational, &off_color_operational); |
248 | ||
33d5013d LP |
249 | r = sd_network_link_get_setup_state(links[i].ifindex, &setup_state); |
250 | if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */ | |
251 | setup_state = strdup("unmanaged"); | |
d57c365b | 252 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
ee8c4568 | 253 | |
691d4da9 LP |
254 | xsprintf(devid, "n%i", links[i].ifindex); |
255 | (void) sd_device_new_from_device_id(&d, devid); | |
ee8c4568 | 256 | |
b55822c3 | 257 | t = link_get_type_string(links[i].iftype, d); |
ee8c4568 | 258 | |
c9cc0383 | 259 | printf("%3i %-16s %-18s %s%-16s%s %s%-10s%s\n", |
d57c365b LP |
260 | links[i].ifindex, links[i].name, strna(t), |
261 | on_color_operational, strna(operational_state), off_color_operational, | |
262 | on_color_setup, strna(setup_state), off_color_setup); | |
ee8c4568 LP |
263 | } |
264 | ||
265 | if (arg_legend) | |
6d0c65ff | 266 | printf("\n%i links listed.\n", c); |
ee8c4568 LP |
267 | |
268 | return 0; | |
269 | } | |
270 | ||
c09da729 | 271 | /* IEEE Organizationally Unique Identifier vendor string */ |
b147503e | 272 | static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) { |
81fd1dd3 | 273 | const char *description; |
fbd0b64f | 274 | char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc; |
81fd1dd3 TG |
275 | int r; |
276 | ||
277 | assert(ret); | |
c09da729 | 278 | |
888943fc LP |
279 | if (!hwdb) |
280 | return -EINVAL; | |
281 | ||
81fd1dd3 TG |
282 | if (!mac) |
283 | return -EINVAL; | |
284 | ||
c09da729 TG |
285 | /* skip commonly misused 00:00:00 (Xerox) prefix */ |
286 | if (memcmp(mac, "\0\0\0", 3) == 0) | |
287 | return -EINVAL; | |
288 | ||
d054f0a4 DM |
289 | xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, |
290 | ETHER_ADDR_FORMAT_VAL(*mac)); | |
c09da729 | 291 | |
81fd1dd3 TG |
292 | r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description); |
293 | if (r < 0) | |
294 | return r; | |
c09da729 | 295 | |
81fd1dd3 TG |
296 | desc = strdup(description); |
297 | if (!desc) | |
298 | return -ENOMEM; | |
c09da729 | 299 | |
81fd1dd3 TG |
300 | *ret = desc; |
301 | ||
302 | return 0; | |
c09da729 TG |
303 | } |
304 | ||
69fb1176 | 305 | static int get_gateway_description( |
1c4baffc | 306 | sd_netlink *rtnl, |
81fd1dd3 | 307 | sd_hwdb *hwdb, |
69fb1176 LP |
308 | int ifindex, |
309 | int family, | |
310 | union in_addr_union *gateway, | |
311 | char **gateway_description) { | |
4afd3348 | 312 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 313 | sd_netlink_message *m; |
c09da729 TG |
314 | int r; |
315 | ||
316 | assert(rtnl); | |
317 | assert(ifindex >= 0); | |
4c701096 | 318 | assert(IN_SET(family, AF_INET, AF_INET6)); |
c09da729 TG |
319 | assert(gateway); |
320 | assert(gateway_description); | |
321 | ||
322 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); | |
323 | if (r < 0) | |
324 | return r; | |
325 | ||
1c4baffc | 326 | r = sd_netlink_message_request_dump(req, true); |
c09da729 TG |
327 | if (r < 0) |
328 | return r; | |
329 | ||
1c4baffc | 330 | r = sd_netlink_call(rtnl, req, 0, &reply); |
c09da729 TG |
331 | if (r < 0) |
332 | return r; | |
333 | ||
1c4baffc | 334 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
67a46833 YW |
335 | union in_addr_union gw = IN_ADDR_NULL; |
336 | struct ether_addr mac = ETHER_ADDR_NULL; | |
c09da729 TG |
337 | uint16_t type; |
338 | int ifi, fam; | |
339 | ||
1c4baffc | 340 | r = sd_netlink_message_get_errno(m); |
c09da729 TG |
341 | if (r < 0) { |
342 | log_error_errno(r, "got error: %m"); | |
343 | continue; | |
344 | } | |
345 | ||
1c4baffc | 346 | r = sd_netlink_message_get_type(m, &type); |
c09da729 TG |
347 | if (r < 0) { |
348 | log_error_errno(r, "could not get type: %m"); | |
349 | continue; | |
350 | } | |
351 | ||
352 | if (type != RTM_NEWNEIGH) { | |
353 | log_error("type is not RTM_NEWNEIGH"); | |
354 | continue; | |
355 | } | |
356 | ||
357 | r = sd_rtnl_message_neigh_get_family(m, &fam); | |
358 | if (r < 0) { | |
359 | log_error_errno(r, "could not get family: %m"); | |
360 | continue; | |
361 | } | |
362 | ||
363 | if (fam != family) { | |
364 | log_error("family is not correct"); | |
365 | continue; | |
366 | } | |
367 | ||
368 | r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); | |
369 | if (r < 0) { | |
144232a8 | 370 | log_error_errno(r, "could not get ifindex: %m"); |
c09da729 TG |
371 | continue; |
372 | } | |
373 | ||
374 | if (ifindex > 0 && ifi != ifindex) | |
375 | continue; | |
376 | ||
377 | switch (fam) { | |
378 | case AF_INET: | |
1c4baffc | 379 | r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in); |
c09da729 TG |
380 | if (r < 0) |
381 | continue; | |
382 | ||
383 | break; | |
384 | case AF_INET6: | |
1c4baffc | 385 | r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6); |
c09da729 TG |
386 | if (r < 0) |
387 | continue; | |
388 | ||
389 | break; | |
390 | default: | |
391 | continue; | |
392 | } | |
393 | ||
394 | if (!in_addr_equal(fam, &gw, gateway)) | |
395 | continue; | |
396 | ||
1c4baffc | 397 | r = sd_netlink_message_read_ether_addr(m, NDA_LLADDR, &mac); |
c09da729 TG |
398 | if (r < 0) |
399 | continue; | |
400 | ||
401 | r = ieee_oui(hwdb, &mac, gateway_description); | |
402 | if (r < 0) | |
403 | continue; | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | return -ENODATA; | |
409 | } | |
410 | ||
69fb1176 | 411 | static int dump_gateways( |
1c4baffc | 412 | sd_netlink *rtnl, |
81fd1dd3 | 413 | sd_hwdb *hwdb, |
69fb1176 LP |
414 | const char *prefix, |
415 | int ifindex) { | |
b6a3ca6d TG |
416 | _cleanup_free_ struct local_address *local = NULL; |
417 | int r, n, i; | |
c09da729 | 418 | |
837f57da LP |
419 | assert(rtnl); |
420 | assert(prefix); | |
421 | ||
b6a3ca6d TG |
422 | n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local); |
423 | if (n < 0) | |
424 | return n; | |
c09da729 | 425 | |
b6a3ca6d TG |
426 | for (i = 0; i < n; i++) { |
427 | _cleanup_free_ char *gateway = NULL, *description = NULL; | |
c09da729 | 428 | |
b6a3ca6d | 429 | r = in_addr_to_string(local[i].family, &local[i].address, &gateway); |
c09da729 | 430 | if (r < 0) |
b6a3ca6d | 431 | return r; |
c09da729 | 432 | |
69fb1176 | 433 | r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description); |
c09da729 | 434 | if (r < 0) |
b6a3ca6d | 435 | log_debug_errno(r, "Could not get description of gateway: %m"); |
c09da729 | 436 | |
1693a943 LP |
437 | printf("%*s%s", |
438 | (int) strlen(prefix), | |
439 | i == 0 ? prefix : "", | |
440 | gateway); | |
441 | ||
b6a3ca6d | 442 | if (description) |
1693a943 LP |
443 | printf(" (%s)", description); |
444 | ||
445 | /* Show interface name for the entry if we show | |
446 | * entries for all interfaces */ | |
447 | if (ifindex <= 0) { | |
448 | char name[IF_NAMESIZE+1]; | |
449 | ||
450 | if (if_indextoname(local[i].ifindex, name)) { | |
451 | fputs(" on ", stdout); | |
452 | fputs(name, stdout); | |
453 | } else | |
454 | printf(" on %%%i", local[i].ifindex); | |
455 | } | |
456 | ||
457 | fputc('\n', stdout); | |
c09da729 TG |
458 | } |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
69fb1176 | 463 | static int dump_addresses( |
1c4baffc | 464 | sd_netlink *rtnl, |
69fb1176 LP |
465 | const char *prefix, |
466 | int ifindex) { | |
467 | ||
ee8c4568 LP |
468 | _cleanup_free_ struct local_address *local = NULL; |
469 | int r, n, i; | |
470 | ||
837f57da LP |
471 | assert(rtnl); |
472 | assert(prefix); | |
473 | ||
1d050e1e | 474 | n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local); |
ee8c4568 LP |
475 | if (n < 0) |
476 | return n; | |
477 | ||
478 | for (i = 0; i < n; i++) { | |
479 | _cleanup_free_ char *pretty = NULL; | |
480 | ||
481 | r = in_addr_to_string(local[i].family, &local[i].address, &pretty); | |
482 | if (r < 0) | |
483 | return r; | |
484 | ||
1693a943 | 485 | printf("%*s%s", |
ee8c4568 LP |
486 | (int) strlen(prefix), |
487 | i == 0 ? prefix : "", | |
488 | pretty); | |
1693a943 LP |
489 | |
490 | if (ifindex <= 0) { | |
491 | char name[IF_NAMESIZE+1]; | |
492 | ||
493 | if (if_indextoname(local[i].ifindex, name)) { | |
494 | fputs(" on ", stdout); | |
495 | fputs(name, stdout); | |
496 | } else | |
497 | printf(" on %%%i", local[i].ifindex); | |
498 | } | |
499 | ||
500 | fputc('\n', stdout); | |
ee8c4568 LP |
501 | } |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
d37b7627 SS |
506 | static int dump_address_labels(sd_netlink *rtnl) { |
507 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
508 | sd_netlink_message *m; | |
509 | int r; | |
510 | ||
511 | assert(rtnl); | |
512 | ||
513 | r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6); | |
514 | if (r < 0) | |
515 | return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m"); | |
516 | ||
517 | r = sd_netlink_message_request_dump(req, true); | |
518 | if (r < 0) | |
519 | return r; | |
520 | ||
521 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
522 | if (r < 0) | |
523 | return r; | |
524 | ||
525 | printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label"); | |
526 | ||
527 | for (m = reply; m; m = sd_netlink_message_next(m)) { | |
528 | _cleanup_free_ char *pretty = NULL; | |
67a46833 | 529 | union in_addr_union prefix = IN_ADDR_NULL; |
d37b7627 SS |
530 | uint8_t prefixlen; |
531 | uint32_t label; | |
532 | ||
533 | r = sd_netlink_message_get_errno(m); | |
534 | if (r < 0) { | |
535 | log_error_errno(r, "got error: %m"); | |
536 | continue; | |
537 | } | |
538 | ||
539 | r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label); | |
540 | if (r < 0 && r != -ENODATA) { | |
541 | log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m"); | |
542 | continue; | |
543 | } | |
544 | ||
545 | r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6); | |
546 | if (r < 0) | |
547 | continue; | |
548 | ||
549 | r = in_addr_to_string(AF_INET6, &prefix, &pretty); | |
550 | if (r < 0) | |
551 | continue; | |
552 | ||
553 | r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen); | |
554 | if (r < 0) | |
555 | continue; | |
556 | ||
557 | printf("%10s/%-5u %30u\n", pretty, prefixlen, label); | |
558 | } | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
563 | static int list_address_labels(int argc, char *argv[], void *userdata) { | |
564 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
565 | int r; | |
566 | ||
567 | r = sd_netlink_open(&rtnl); | |
568 | if (r < 0) | |
569 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
570 | ||
571 | dump_address_labels(rtnl); | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
837f57da LP |
576 | static int open_lldp_neighbors(int ifindex, FILE **ret) { |
577 | _cleanup_free_ char *p = NULL; | |
578 | FILE *f; | |
579 | ||
580 | if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0) | |
581 | return -ENOMEM; | |
582 | ||
583 | f = fopen(p, "re"); | |
584 | if (!f) | |
585 | return -errno; | |
586 | ||
587 | *ret = f; | |
588 | return 0; | |
589 | } | |
590 | ||
591 | static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) { | |
592 | _cleanup_free_ void *raw = NULL; | |
593 | size_t l; | |
594 | le64_t u; | |
595 | int r; | |
596 | ||
597 | assert(f); | |
598 | assert(ret); | |
599 | ||
600 | l = fread(&u, 1, sizeof(u), f); | |
601 | if (l == 0 && feof(f)) | |
602 | return 0; | |
603 | if (l != sizeof(u)) | |
604 | return -EBADMSG; | |
605 | ||
d23c3e4c FB |
606 | /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */ |
607 | if (le64toh(u) >= 4096) | |
608 | return -EBADMSG; | |
609 | ||
837f57da LP |
610 | raw = new(uint8_t, le64toh(u)); |
611 | if (!raw) | |
612 | return -ENOMEM; | |
613 | ||
614 | if (fread(raw, 1, le64toh(u), f) != le64toh(u)) | |
615 | return -EBADMSG; | |
616 | ||
617 | r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u)); | |
618 | if (r < 0) | |
619 | return r; | |
620 | ||
621 | return 1; | |
622 | } | |
623 | ||
624 | static int dump_lldp_neighbors(const char *prefix, int ifindex) { | |
625 | _cleanup_fclose_ FILE *f = NULL; | |
626 | int r, c = 0; | |
627 | ||
628 | assert(prefix); | |
629 | assert(ifindex > 0); | |
630 | ||
631 | r = open_lldp_neighbors(ifindex, &f); | |
632 | if (r < 0) | |
633 | return r; | |
634 | ||
635 | for (;;) { | |
636 | const char *system_name = NULL, *port_id = NULL, *port_description = NULL; | |
637 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; | |
638 | ||
639 | r = next_lldp_neighbor(f, &n); | |
640 | if (r < 0) | |
641 | return r; | |
642 | if (r == 0) | |
643 | break; | |
644 | ||
645 | printf("%*s", | |
646 | (int) strlen(prefix), | |
647 | c == 0 ? prefix : ""); | |
648 | ||
649 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); | |
650 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
651 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
652 | ||
653 | printf("%s on port %s", strna(system_name), strna(port_id)); | |
654 | ||
655 | if (!isempty(port_description)) | |
656 | printf(" (%s)", port_description); | |
657 | ||
658 | putchar('\n'); | |
659 | ||
660 | c++; | |
661 | } | |
662 | ||
663 | return c; | |
664 | } | |
665 | ||
b295beea LP |
666 | static void dump_ifindexes(const char *prefix, const int *ifindexes) { |
667 | unsigned c; | |
668 | ||
669 | assert(prefix); | |
670 | ||
671 | if (!ifindexes || ifindexes[0] <= 0) | |
672 | return; | |
673 | ||
674 | for (c = 0; ifindexes[c] > 0; c++) { | |
675 | char name[IF_NAMESIZE+1]; | |
676 | ||
677 | printf("%*s", | |
678 | (int) strlen(prefix), | |
679 | c == 0 ? prefix : ""); | |
680 | ||
681 | if (if_indextoname(ifindexes[c], name)) | |
682 | fputs(name, stdout); | |
683 | else | |
684 | printf("%i", ifindexes[c]); | |
685 | ||
686 | fputc('\n', stdout); | |
687 | } | |
688 | } | |
689 | ||
ee8c4568 LP |
690 | static void dump_list(const char *prefix, char **l) { |
691 | char **i; | |
692 | ||
dce83649 LP |
693 | if (strv_isempty(l)) |
694 | return; | |
695 | ||
ee8c4568 LP |
696 | STRV_FOREACH(i, l) { |
697 | printf("%*s%s\n", | |
698 | (int) strlen(prefix), | |
699 | i == l ? prefix : "", | |
700 | *i); | |
701 | } | |
702 | } | |
703 | ||
69fb1176 | 704 | static int link_status_one( |
1c4baffc | 705 | sd_netlink *rtnl, |
81fd1dd3 | 706 | sd_hwdb *hwdb, |
b147503e LP |
707 | const LinkInfo *info) { |
708 | ||
3df9bec5 | 709 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; |
64d6c229 | 710 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL; |
4afd3348 | 711 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
9085f64a | 712 | char devid[2 + DECIMAL_STR_MAX(int)]; |
373d9f17 | 713 | _cleanup_free_ char *t = NULL, *network = NULL; |
af5effc4 | 714 | const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL; |
d57c365b LP |
715 | const char *on_color_operational, *off_color_operational, |
716 | *on_color_setup, *off_color_setup; | |
b295beea | 717 | _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL; |
b147503e | 718 | int r; |
9085f64a LP |
719 | |
720 | assert(rtnl); | |
b147503e | 721 | assert(info); |
9085f64a | 722 | |
b147503e | 723 | (void) sd_network_link_get_operational_state(info->ifindex, &operational_state); |
d57c365b LP |
724 | operational_state_to_color(operational_state, &on_color_operational, &off_color_operational); |
725 | ||
33d5013d LP |
726 | r = sd_network_link_get_setup_state(info->ifindex, &setup_state); |
727 | if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */ | |
728 | setup_state = strdup("unmanaged"); | |
d57c365b | 729 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
9085f64a | 730 | |
b147503e LP |
731 | (void) sd_network_link_get_dns(info->ifindex, &dns); |
732 | (void) sd_network_link_get_search_domains(info->ifindex, &search_domains); | |
733 | (void) sd_network_link_get_route_domains(info->ifindex, &route_domains); | |
734 | (void) sd_network_link_get_ntp(info->ifindex, &ntp); | |
9085f64a | 735 | |
691d4da9 | 736 | xsprintf(devid, "n%i", info->ifindex); |
914d6c09 | 737 | |
dce83649 | 738 | (void) sd_device_new_from_device_id(&d, devid); |
914d6c09 | 739 | |
9085f64a | 740 | if (d) { |
dce83649 LP |
741 | (void) sd_device_get_property_value(d, "ID_NET_LINK_FILE", &link); |
742 | (void) sd_device_get_property_value(d, "ID_NET_DRIVER", &driver); | |
743 | (void) sd_device_get_property_value(d, "ID_PATH", &path); | |
9085f64a | 744 | |
2c740afd | 745 | if (sd_device_get_property_value(d, "ID_VENDOR_FROM_DATABASE", &vendor) < 0) |
dce83649 | 746 | (void) sd_device_get_property_value(d, "ID_VENDOR", &vendor); |
9085f64a | 747 | |
2c740afd | 748 | if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) < 0) |
dce83649 | 749 | (void) sd_device_get_property_value(d, "ID_MODEL", &model); |
9085f64a LP |
750 | } |
751 | ||
b55822c3 | 752 | t = link_get_type_string(info->iftype, d); |
b1acce80 | 753 | |
4abd866d | 754 | (void) sd_network_link_get_network_file(info->ifindex, &network); |
df3fb561 | 755 | |
4abd866d LP |
756 | (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to); |
757 | (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by); | |
0d4ad91d | 758 | |
9a6f746f | 759 | printf("%s%s%s %i: %s\n", on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, info->ifindex, info->name); |
9085f64a | 760 | |
0d4ad91d AR |
761 | printf(" Link File: %s\n" |
762 | " Network File: %s\n" | |
763 | " Type: %s\n" | |
764 | " State: %s%s%s (%s%s%s)\n", | |
af5effc4 | 765 | strna(link), |
373d9f17 | 766 | strna(network), |
9085f64a | 767 | strna(t), |
d57c365b LP |
768 | on_color_operational, strna(operational_state), off_color_operational, |
769 | on_color_setup, strna(setup_state), off_color_setup); | |
9085f64a LP |
770 | |
771 | if (path) | |
0d4ad91d | 772 | printf(" Path: %s\n", path); |
9085f64a | 773 | if (driver) |
0d4ad91d | 774 | printf(" Driver: %s\n", driver); |
9085f64a | 775 | if (vendor) |
0d4ad91d | 776 | printf(" Vendor: %s\n", vendor); |
9085f64a | 777 | if (model) |
0d4ad91d | 778 | printf(" Model: %s\n", model); |
9085f64a | 779 | |
b147503e | 780 | if (info->has_mac_address) { |
888943fc | 781 | _cleanup_free_ char *description = NULL; |
db73295a | 782 | char ea[ETHER_ADDR_TO_STRING_MAX]; |
888943fc | 783 | |
4abd866d | 784 | (void) ieee_oui(hwdb, &info->mac_address, &description); |
888943fc LP |
785 | |
786 | if (description) | |
b147503e | 787 | printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info->mac_address, ea), description); |
888943fc | 788 | else |
b147503e | 789 | printf(" HW Address: %s\n", ether_addr_to_string(&info->mac_address, ea)); |
db73295a | 790 | } |
9085f64a | 791 | |
b147503e | 792 | if (info->has_mtu) |
4e964aa0 | 793 | printf(" MTU: %" PRIu32 "\n", info->mtu); |
2c73f59b SS |
794 | if (info->has_min_mtu) |
795 | printf(" Minimum MTU: %" PRIu32 "\n", info->min_mtu); | |
796 | if (info->has_max_mtu) | |
797 | printf(" Maximum MTU: %" PRIu32 "\n", info->max_mtu); | |
9085f64a | 798 | |
4abd866d LP |
799 | (void) dump_addresses(rtnl, " Address: ", info->ifindex); |
800 | (void) dump_gateways(rtnl, hwdb, " Gateway: ", info->ifindex); | |
9085f64a | 801 | |
dce83649 LP |
802 | dump_list(" DNS: ", dns); |
803 | dump_list(" Search Domains: ", search_domains); | |
804 | dump_list(" Route Domains: ", route_domains); | |
8eb9058d | 805 | |
dce83649 | 806 | dump_list(" NTP: ", ntp); |
0d4ad91d | 807 | |
b295beea LP |
808 | dump_ifindexes("Carrier Bound To: ", carrier_bound_to); |
809 | dump_ifindexes("Carrier Bound By: ", carrier_bound_by); | |
9085f64a | 810 | |
b147503e | 811 | (void) sd_network_link_get_timezone(info->ifindex, &tz); |
64d6c229 | 812 | if (tz) |
f5207c22 | 813 | printf(" Time Zone: %s\n", tz); |
8eb9058d | 814 | |
837f57da LP |
815 | (void) dump_lldp_neighbors(" Connected To: ", info->ifindex); |
816 | ||
9085f64a LP |
817 | return 0; |
818 | } | |
819 | ||
0070333f LP |
820 | static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) { |
821 | _cleanup_free_ char *operational_state = NULL; | |
822 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; | |
823 | const char *on_color_operational, *off_color_operational; | |
824 | ||
825 | assert(rtnl); | |
826 | ||
4abd866d | 827 | (void) sd_network_get_operational_state(&operational_state); |
0070333f LP |
828 | operational_state_to_color(operational_state, &on_color_operational, &off_color_operational); |
829 | ||
830 | printf("%s%s%s State: %s%s%s\n", | |
9a6f746f | 831 | on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, |
0070333f LP |
832 | on_color_operational, strna(operational_state), off_color_operational); |
833 | ||
4abd866d LP |
834 | (void) dump_addresses(rtnl, " Address: ", 0); |
835 | (void) dump_gateways(rtnl, hwdb, " Gateway: ", 0); | |
0070333f | 836 | |
4abd866d | 837 | (void) sd_network_get_dns(&dns); |
0070333f LP |
838 | dump_list(" DNS: ", dns); |
839 | ||
4abd866d | 840 | (void) sd_network_get_search_domains(&search_domains); |
0070333f LP |
841 | dump_list("Search Domains: ", search_domains); |
842 | ||
4abd866d | 843 | (void) sd_network_get_route_domains(&route_domains); |
0070333f LP |
844 | dump_list(" Route Domains: ", route_domains); |
845 | ||
4abd866d | 846 | (void) sd_network_get_ntp(&ntp); |
0070333f LP |
847 | dump_list(" NTP: ", ntp); |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
266b5389 | 852 | static int link_status(int argc, char *argv[], void *userdata) { |
4afd3348 | 853 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b147503e LP |
854 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
855 | _cleanup_free_ LinkInfo *links = NULL; | |
856 | int r, c, i; | |
ee8c4568 | 857 | |
0221d68a | 858 | (void) pager_open(arg_pager_flags); |
0070333f | 859 | |
1c4baffc | 860 | r = sd_netlink_open(&rtnl); |
f647962d MS |
861 | if (r < 0) |
862 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
f7d68aa8 | 863 | |
81fd1dd3 TG |
864 | r = sd_hwdb_new(&hwdb); |
865 | if (r < 0) | |
866 | log_debug_errno(r, "Failed to open hardware database: %m"); | |
69fb1176 | 867 | |
b147503e | 868 | if (arg_all) |
a6962904 | 869 | c = acquire_link_info(rtnl, NULL, &links); |
b147503e | 870 | else if (argc <= 1) |
0070333f | 871 | return system_status(rtnl, hwdb); |
b147503e | 872 | else |
a6962904 | 873 | c = acquire_link_info(rtnl, argv + 1, &links); |
b147503e LP |
874 | if (c < 0) |
875 | return c; | |
ee8c4568 | 876 | |
b147503e LP |
877 | for (i = 0; i < c; i++) { |
878 | if (i > 0) | |
879 | fputc('\n', stdout); | |
ee8c4568 | 880 | |
b147503e | 881 | link_status_one(rtnl, hwdb, links + i); |
ee8c4568 LP |
882 | } |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
34437b4f LP |
887 | static char *lldp_capabilities_to_string(uint16_t x) { |
888 | static const char characters[] = { | |
889 | 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm', | |
890 | }; | |
891 | char *ret; | |
892 | unsigned i; | |
49699bac | 893 | |
34437b4f LP |
894 | ret = new(char, ELEMENTSOF(characters) + 1); |
895 | if (!ret) | |
49699bac SS |
896 | return NULL; |
897 | ||
34437b4f LP |
898 | for (i = 0; i < ELEMENTSOF(characters); i++) |
899 | ret[i] = (x & (1U << i)) ? characters[i] : '.'; | |
49699bac | 900 | |
34437b4f LP |
901 | ret[i] = 0; |
902 | return ret; | |
49699bac SS |
903 | } |
904 | ||
4c3160f1 ZJS |
905 | static void lldp_capabilities_legend(uint16_t x) { |
906 | unsigned w, i, cols = columns(); | |
404d53a9 | 907 | static const char* const table[] = { |
4c3160f1 ZJS |
908 | "o - Other", |
909 | "p - Repeater", | |
910 | "b - Bridge", | |
911 | "w - WLAN Access Point", | |
912 | "r - Router", | |
913 | "t - Telephone", | |
914 | "d - DOCSIS cable device", | |
915 | "a - Station", | |
916 | "c - Customer VLAN", | |
917 | "s - Service VLAN", | |
918 | "m - Two-port MAC Relay (TPMR)", | |
919 | }; | |
920 | ||
921 | if (x == 0) | |
922 | return; | |
923 | ||
924 | printf("\nCapability Flags:\n"); | |
925 | for (w = 0, i = 0; i < ELEMENTSOF(table); i++) | |
926 | if (x & (1U << i) || arg_all) { | |
927 | bool newline; | |
928 | ||
929 | newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols; | |
930 | if (newline) | |
931 | w = 0; | |
932 | w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]); | |
933 | } | |
934 | puts(""); | |
935 | } | |
936 | ||
49699bac | 937 | static int link_lldp_status(int argc, char *argv[], void *userdata) { |
b147503e | 938 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
49699bac | 939 | _cleanup_free_ LinkInfo *links = NULL; |
b147503e | 940 | int i, r, c, m = 0; |
4c3160f1 | 941 | uint16_t all = 0; |
b147503e LP |
942 | |
943 | r = sd_netlink_open(&rtnl); | |
944 | if (r < 0) | |
945 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
49699bac | 946 | |
a6962904 | 947 | c = acquire_link_info(rtnl, argc > 1 ? argv + 1 : NULL, &links); |
49699bac | 948 | if (c < 0) |
7d367b45 LP |
949 | return c; |
950 | ||
0221d68a | 951 | (void) pager_open(arg_pager_flags); |
49699bac | 952 | |
19727828 | 953 | if (arg_legend) |
34437b4f LP |
954 | printf("%-16s %-17s %-16s %-11s %-17s %-16s\n", |
955 | "LINK", | |
956 | "CHASSIS ID", | |
957 | "SYSTEM NAME", | |
958 | "CAPS", | |
959 | "PORT ID", | |
960 | "PORT DESCRIPTION"); | |
49699bac | 961 | |
b147503e | 962 | for (i = 0; i < c; i++) { |
34437b4f | 963 | _cleanup_fclose_ FILE *f = NULL; |
49699bac | 964 | |
837f57da LP |
965 | r = open_lldp_neighbors(links[i].ifindex, &f); |
966 | if (r == -ENOENT) | |
967 | continue; | |
968 | if (r < 0) { | |
969 | log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex); | |
34437b4f LP |
970 | continue; |
971 | } | |
49699bac | 972 | |
34437b4f | 973 | for (;;) { |
d08191a2 | 974 | _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL; |
34437b4f LP |
975 | const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL, *capabilities = NULL; |
976 | _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; | |
34437b4f | 977 | uint16_t cc; |
49699bac | 978 | |
837f57da | 979 | r = next_lldp_neighbor(f, &n); |
34437b4f | 980 | if (r < 0) { |
837f57da | 981 | log_warning_errno(r, "Failed to read neighbor data: %m"); |
34437b4f | 982 | break; |
49699bac | 983 | } |
837f57da LP |
984 | if (r == 0) |
985 | break; | |
34437b4f LP |
986 | |
987 | (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id); | |
988 | (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id); | |
989 | (void) sd_lldp_neighbor_get_system_name(n, &system_name); | |
990 | (void) sd_lldp_neighbor_get_port_description(n, &port_description); | |
991 | ||
d08191a2 LP |
992 | if (chassis_id) { |
993 | cid = ellipsize(chassis_id, 17, 100); | |
994 | if (cid) | |
995 | chassis_id = cid; | |
996 | } | |
997 | ||
998 | if (port_id) { | |
999 | pid = ellipsize(port_id, 17, 100); | |
1000 | if (pid) | |
1001 | port_id = pid; | |
1002 | } | |
1003 | ||
1004 | if (system_name) { | |
1005 | sname = ellipsize(system_name, 16, 100); | |
1006 | if (sname) | |
1007 | system_name = sname; | |
1008 | } | |
1009 | ||
1010 | if (port_description) { | |
1011 | pdesc = ellipsize(port_description, 16, 100); | |
1012 | if (pdesc) | |
1013 | port_description = pdesc; | |
1014 | } | |
1015 | ||
4c3160f1 | 1016 | if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) { |
34437b4f | 1017 | capabilities = lldp_capabilities_to_string(cc); |
4c3160f1 ZJS |
1018 | all |= cc; |
1019 | } | |
34437b4f LP |
1020 | |
1021 | printf("%-16s %-17s %-16s %-11s %-17s %-16s\n", | |
1022 | links[i].name, | |
1023 | strna(chassis_id), | |
1024 | strna(system_name), | |
1025 | strna(capabilities), | |
1026 | strna(port_id), | |
1027 | strna(port_description)); | |
b147503e LP |
1028 | |
1029 | m++; | |
49699bac SS |
1030 | } |
1031 | } | |
1032 | ||
4c3160f1 ZJS |
1033 | if (arg_legend) { |
1034 | lldp_capabilities_legend(all); | |
1035 | printf("\n%i neighbors listed.\n", m); | |
1036 | } | |
49699bac SS |
1037 | |
1038 | return 0; | |
1039 | } | |
1040 | ||
37ec0fdd LP |
1041 | static int help(void) { |
1042 | _cleanup_free_ char *link = NULL; | |
1043 | int r; | |
1044 | ||
1045 | r = terminal_urlify_man("networkctl", "1", &link); | |
1046 | if (r < 0) | |
1047 | return log_oom(); | |
1048 | ||
ee8c4568 LP |
1049 | printf("%s [OPTIONS...]\n\n" |
1050 | "Query and control the networking subsystem.\n\n" | |
1051 | " -h --help Show this help\n" | |
9085f64a LP |
1052 | " --version Show package version\n" |
1053 | " --no-pager Do not pipe output into a pager\n" | |
1054 | " --no-legend Do not show the headers and footers\n" | |
1055 | " -a --all Show status for all links\n\n" | |
ee8c4568 | 1056 | "Commands:\n" |
a6962904 YW |
1057 | " list [PATTERN...] List links\n" |
1058 | " status [PATTERN...] Show link status\n" | |
1059 | " lldp [PATTERN...] Show LLDP neighbors\n" | |
d37b7627 | 1060 | " label Show current address label entries in the kernel\n" |
37ec0fdd LP |
1061 | "\nSee the %s for details.\n" |
1062 | , program_invocation_short_name | |
1063 | , link | |
1064 | ); | |
1065 | ||
1066 | return 0; | |
ee8c4568 LP |
1067 | } |
1068 | ||
1069 | static int parse_argv(int argc, char *argv[]) { | |
1070 | ||
1071 | enum { | |
1072 | ARG_VERSION = 0x100, | |
1073 | ARG_NO_PAGER, | |
1074 | ARG_NO_LEGEND, | |
1075 | }; | |
1076 | ||
1077 | static const struct option options[] = { | |
1078 | { "help", no_argument, NULL, 'h' }, | |
1079 | { "version", no_argument, NULL, ARG_VERSION }, | |
1080 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
1081 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
9085f64a | 1082 | { "all", no_argument, NULL, 'a' }, |
ee8c4568 LP |
1083 | {} |
1084 | }; | |
1085 | ||
1086 | int c; | |
1087 | ||
1088 | assert(argc >= 0); | |
1089 | assert(argv); | |
1090 | ||
9085f64a | 1091 | while ((c = getopt_long(argc, argv, "ha", options, NULL)) >= 0) { |
ee8c4568 LP |
1092 | |
1093 | switch (c) { | |
1094 | ||
1095 | case 'h': | |
37ec0fdd | 1096 | return help(); |
ee8c4568 LP |
1097 | |
1098 | case ARG_VERSION: | |
3f6fd1ba | 1099 | return version(); |
ee8c4568 LP |
1100 | |
1101 | case ARG_NO_PAGER: | |
0221d68a | 1102 | arg_pager_flags |= PAGER_DISABLE; |
ee8c4568 LP |
1103 | break; |
1104 | ||
1105 | case ARG_NO_LEGEND: | |
1106 | arg_legend = false; | |
1107 | break; | |
1108 | ||
9085f64a LP |
1109 | case 'a': |
1110 | arg_all = true; | |
1111 | break; | |
1112 | ||
ee8c4568 LP |
1113 | case '?': |
1114 | return -EINVAL; | |
1115 | ||
1116 | default: | |
1117 | assert_not_reached("Unhandled option"); | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | return 1; | |
1122 | } | |
1123 | ||
1124 | static int networkctl_main(int argc, char *argv[]) { | |
15c3626e YW |
1125 | static const Verb verbs[] = { |
1126 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links }, | |
1127 | { "status", VERB_ANY, VERB_ANY, 0, link_status }, | |
1128 | { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status }, | |
1129 | { "label", VERB_ANY, VERB_ANY, 0, list_address_labels }, | |
266b5389 | 1130 | {} |
ee8c4568 LP |
1131 | }; |
1132 | ||
266b5389 | 1133 | return dispatch_verb(argc, argv, verbs, NULL); |
ee8c4568 LP |
1134 | } |
1135 | ||
58fb3678 LP |
1136 | static void warn_networkd_missing(void) { |
1137 | ||
1138 | if (access("/run/systemd/netif/state", F_OK) >= 0) | |
1139 | return; | |
1140 | ||
1141 | fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n"); | |
1142 | } | |
1143 | ||
4e2ca442 | 1144 | static int run(int argc, char* argv[]) { |
ee8c4568 LP |
1145 | int r; |
1146 | ||
1a043959 | 1147 | log_show_color(true); |
ee8c4568 LP |
1148 | log_parse_environment(); |
1149 | log_open(); | |
1150 | ||
1151 | r = parse_argv(argc, argv); | |
1152 | if (r <= 0) | |
4e2ca442 | 1153 | return r; |
ee8c4568 | 1154 | |
58fb3678 LP |
1155 | warn_networkd_missing(); |
1156 | ||
4e2ca442 | 1157 | return networkctl_main(argc, argv); |
ee8c4568 | 1158 | } |
4e2ca442 ZJS |
1159 | |
1160 | DEFINE_MAIN_FUNCTION(run); |