]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
ee8c4568 | 2 | |
3d0c8750 | 3 | #include <arpa/inet.h> |
ee8c4568 | 4 | #include <getopt.h> |
d37b7627 | 5 | #include <linux/if_addrlabel.h> |
1693a943 | 6 | #include <net/if.h> |
3f6fd1ba | 7 | #include <stdbool.h> |
ca78ad1d ZJS |
8 | #include <sys/stat.h> |
9 | #include <sys/types.h> | |
10 | #include <unistd.h> | |
12ef8fb6 | 11 | #include <linux/if_bridge.h> |
dca35224 | 12 | #include <linux/if_tunnel.h> |
ee8c4568 | 13 | |
335dd8ba | 14 | #include "sd-bus.h" |
914d6c09 | 15 | #include "sd-device.h" |
d9ce1c24 | 16 | #include "sd-dhcp-client.h" |
3f6fd1ba LP |
17 | #include "sd-hwdb.h" |
18 | #include "sd-netlink.h" | |
19 | #include "sd-network.h" | |
ee8c4568 | 20 | |
b5efdb8a | 21 | #include "alloc-util.h" |
b55818fd | 22 | #include "bond-util.h" |
9b71e4ab | 23 | #include "bridge-util.h" |
d6b4d1c7 | 24 | #include "build.h" |
335dd8ba YW |
25 | #include "bus-common-errors.h" |
26 | #include "bus-error.h" | |
9b71e4ab | 27 | #include "bus-locator.h" |
914d6c09 | 28 | #include "device-util.h" |
8d07de25 | 29 | #include "escape.h" |
3f6fd1ba | 30 | #include "ether-addr-util.h" |
c967d2c7 | 31 | #include "ethtool-util.h" |
34437b4f | 32 | #include "fd-util.h" |
ff7c88a2 | 33 | #include "format-table.h" |
518a66ec | 34 | #include "format-util.h" |
8fff78a1 | 35 | #include "fs-util.h" |
e7b38d7d | 36 | #include "geneve-util.h" |
0ef84b80 | 37 | #include "glob-util.h" |
81fd1dd3 | 38 | #include "hwdb-util.h" |
851ef1ed | 39 | #include "ipvlan-util.h" |
ee8c4568 | 40 | #include "local-addresses.h" |
8752c575 | 41 | #include "locale-util.h" |
10c71c36 | 42 | #include "logs-show.h" |
d37b7627 | 43 | #include "macro.h" |
cf217a09 | 44 | #include "macvlan-util.h" |
4e2ca442 | 45 | #include "main-func.h" |
b5cc5591 | 46 | #include "netif-util.h" |
3f6fd1ba | 47 | #include "netlink-util.h" |
ef62949a | 48 | #include "network-internal.h" |
1929ed0e | 49 | #include "network-util.h" |
15fde154 MY |
50 | #include "networkctl.h" |
51 | #include "networkctl-config-file.h" | |
3f6fd1ba | 52 | #include "pager.h" |
bfc2b05e | 53 | #include "parse-argument.h" |
6bedfcbb | 54 | #include "parse-util.h" |
96bab8fd MY |
55 | #include "path-lookup.h" |
56 | #include "path-util.h" | |
294bf0c3 | 57 | #include "pretty-print.h" |
9cd8c766 | 58 | #include "set.h" |
955fc5d8 | 59 | #include "sigbus.h" |
d308bb99 | 60 | #include "socket-netlink.h" |
db73295a | 61 | #include "socket-util.h" |
760877e9 | 62 | #include "sort-util.h" |
34437b4f | 63 | #include "sparse-endian.h" |
d054f0a4 | 64 | #include "stdio-util.h" |
8b43440b | 65 | #include "string-table.h" |
8752c575 | 66 | #include "string-util.h" |
3f6fd1ba | 67 | #include "strv.h" |
2388b2f4 | 68 | #include "strxcpyx.h" |
288a74cc | 69 | #include "terminal-util.h" |
3cc7a9fd | 70 | #include "udev-util.h" |
10c71c36 | 71 | #include "unit-def.h" |
4591eccf | 72 | #include "varlink.h" |
3f6fd1ba | 73 | #include "verbs.h" |
8d07de25 | 74 | #include "wifi-util.h" |
ee8c4568 | 75 | |
c82d1bf2 SS |
76 | /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */ |
77 | #define NETDEV_KIND_MAX 64 | |
78 | ||
8d07de25 ZJS |
79 | /* use 128 kB for receive socket kernel queue, we shouldn't need more here */ |
80 | #define RCVBUF_SIZE (128*1024) | |
81 | ||
15fde154 MY |
82 | PagerFlags arg_pager_flags = 0; |
83 | bool arg_legend = true; | |
84 | bool arg_no_reload = false; | |
85 | bool arg_all = false; | |
86 | bool arg_stats = false; | |
87 | bool arg_full = false; | |
88 | bool arg_runtime = false; | |
89 | unsigned arg_lines = 10; | |
90 | char *arg_drop_in = NULL; | |
91 | JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; | |
bfc2b05e | 92 | |
96bab8fd MY |
93 | STATIC_DESTRUCTOR_REGISTER(arg_drop_in, freep); |
94 | ||
1dbd2cc7 | 95 | static int varlink_connect_networkd(Varlink **ret_varlink) { |
7742d03e | 96 | _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; |
1dbd2cc7 YW |
97 | JsonVariant *reply; |
98 | uint64_t id; | |
445f0d8b MY |
99 | int r; |
100 | ||
4591eccf ML |
101 | r = varlink_connect_address(&vl, "/run/systemd/netif/io.systemd.Network"); |
102 | if (r < 0) | |
103 | return log_error_errno(r, "Failed to connect to network service /run/systemd/netif/io.systemd.Network: %m"); | |
104 | ||
bfd8f70c YW |
105 | r = varlink_set_allow_fd_passing_output(vl, true); |
106 | if (r < 0) | |
107 | return log_error_errno(r, "Failed to allow passing file descriptor through varlink: %m"); | |
108 | ||
71d0ecc5 | 109 | r = varlink_call_and_log(vl, "io.systemd.Network.GetNamespaceId", /* parameters= */ NULL, &reply); |
4591eccf | 110 | if (r < 0) |
71d0ecc5 | 111 | return r; |
4591eccf ML |
112 | |
113 | static const JsonDispatch dispatch_table[] = { | |
114 | { "NamespaceId", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, 0, JSON_MANDATORY }, | |
115 | {}, | |
116 | }; | |
117 | ||
f0e4244b | 118 | r = json_dispatch(reply, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &id); |
4591eccf ML |
119 | if (r < 0) |
120 | return r; | |
43449ca3 | 121 | |
1dbd2cc7 | 122 | if (id == 0) |
445f0d8b | 123 | log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check."); |
1dbd2cc7 YW |
124 | else { |
125 | struct stat st; | |
445f0d8b | 126 | |
1dbd2cc7 YW |
127 | if (stat("/proc/self/ns/net", &st) < 0) |
128 | return log_error_errno(errno, "Failed to determine our own network namespace ID: %m"); | |
445f0d8b | 129 | |
1dbd2cc7 YW |
130 | if (id != st.st_ino) |
131 | return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), | |
132 | "networkctl must be invoked in same network namespace as systemd-networkd.service."); | |
133 | } | |
445f0d8b | 134 | |
1dbd2cc7 YW |
135 | if (ret_varlink) |
136 | *ret_varlink = TAKE_PTR(vl); | |
445f0d8b MY |
137 | return 0; |
138 | } | |
139 | ||
15fde154 | 140 | bool networkd_is_running(void) { |
96bab8fd MY |
141 | static int cached = -1; |
142 | int r; | |
143 | ||
144 | if (cached < 0) { | |
145 | r = access("/run/systemd/netif/state", F_OK); | |
146 | if (r < 0) { | |
147 | if (errno != ENOENT) | |
148 | log_debug_errno(errno, | |
149 | "Failed to determine whether networkd is running, assuming it's not: %m"); | |
150 | ||
151 | cached = false; | |
152 | } else | |
153 | cached = true; | |
154 | } | |
155 | ||
156 | return cached; | |
445f0d8b MY |
157 | } |
158 | ||
15fde154 | 159 | int acquire_bus(sd_bus **ret) { |
445f0d8b MY |
160 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
161 | int r; | |
162 | ||
163 | assert(ret); | |
164 | ||
165 | r = sd_bus_open_system(&bus); | |
166 | if (r < 0) | |
8dfc3bf5 | 167 | return log_error_errno(r, "Failed to connect to system bus: %m"); |
445f0d8b | 168 | |
6c693007 | 169 | if (networkd_is_running()) { |
1dbd2cc7 | 170 | r = varlink_connect_networkd(/* ret_varlink = */ NULL); |
6c693007 MY |
171 | if (r < 0) |
172 | return r; | |
173 | } else | |
174 | log_warning("systemd-networkd is not running, output might be incomplete."); | |
445f0d8b MY |
175 | |
176 | *ret = TAKE_PTR(bus); | |
177 | return 0; | |
178 | } | |
179 | ||
d821e40c | 180 | static int get_description(sd_bus *bus, JsonVariant **ret) { |
bfc2b05e YW |
181 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
182 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
61c84edf | 183 | const char *text; |
bfc2b05e YW |
184 | int r; |
185 | ||
43449ca3 MY |
186 | assert(bus); |
187 | assert(ret); | |
188 | ||
bfc2b05e YW |
189 | r = bus_call_method(bus, bus_network_mgr, "Describe", &error, &reply, NULL); |
190 | if (r < 0) | |
191 | return log_error_errno(r, "Failed to get description: %s", bus_error_message(&error, r)); | |
192 | ||
193 | r = sd_bus_message_read(reply, "s", &text); | |
194 | if (r < 0) | |
195 | return bus_log_parse_error(r); | |
196 | ||
197 | r = json_parse(text, 0, ret, NULL, NULL); | |
198 | if (r < 0) | |
199 | return log_error_errno(r, "Failed to parse JSON: %m"); | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
d821e40c | 204 | static int dump_manager_description(sd_bus *bus) { |
bfc2b05e YW |
205 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
206 | int r; | |
207 | ||
43449ca3 MY |
208 | assert(bus); |
209 | ||
d821e40c | 210 | r = get_description(bus, &v); |
bfc2b05e YW |
211 | if (r < 0) |
212 | return r; | |
213 | ||
214 | json_variant_dump(v, arg_json_format_flags, NULL, NULL); | |
215 | return 0; | |
216 | } | |
217 | ||
43449ca3 | 218 | static int dump_link_description(sd_bus *bus, char * const *patterns) { |
bfc2b05e YW |
219 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
220 | _cleanup_free_ bool *matched_patterns = NULL; | |
221 | JsonVariant *i; | |
222 | size_t c = 0; | |
223 | int r; | |
224 | ||
43449ca3 MY |
225 | assert(bus); |
226 | assert(patterns); | |
227 | ||
d821e40c | 228 | r = get_description(bus, &v); |
bfc2b05e YW |
229 | if (r < 0) |
230 | return r; | |
231 | ||
232 | matched_patterns = new0(bool, strv_length(patterns)); | |
233 | if (!matched_patterns) | |
234 | return log_oom(); | |
235 | ||
236 | JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(v, "Interfaces")) { | |
718ca772 | 237 | char ifindex_str[DECIMAL_STR_MAX(int64_t)]; |
bfc2b05e | 238 | const char *name; |
718ca772 | 239 | int64_t index; |
bfc2b05e YW |
240 | size_t pos; |
241 | ||
242 | name = json_variant_string(json_variant_by_key(i, "Name")); | |
243 | index = json_variant_integer(json_variant_by_key(i, "Index")); | |
c0f86d66 | 244 | xsprintf(ifindex_str, "%" PRIi64, index); |
bfc2b05e YW |
245 | |
246 | if (!strv_fnmatch_full(patterns, ifindex_str, 0, &pos) && | |
247 | !strv_fnmatch_full(patterns, name, 0, &pos)) { | |
248 | bool match = false; | |
249 | JsonVariant *a; | |
250 | ||
251 | JSON_VARIANT_ARRAY_FOREACH(a, json_variant_by_key(i, "AlternativeNames")) | |
252 | if (strv_fnmatch_full(patterns, json_variant_string(a), 0, &pos)) { | |
253 | match = true; | |
254 | break; | |
255 | } | |
256 | ||
257 | if (!match) | |
258 | continue; | |
259 | } | |
260 | ||
261 | matched_patterns[pos] = true; | |
262 | json_variant_dump(i, arg_json_format_flags, NULL, NULL); | |
263 | c++; | |
264 | } | |
265 | ||
266 | /* Look if we matched all our arguments that are not globs. It is OK for a glob to match | |
267 | * nothing, but not for an exact argument. */ | |
268 | for (size_t pos = 0; pos < strv_length(patterns); pos++) { | |
269 | if (matched_patterns[pos]) | |
270 | continue; | |
271 | ||
272 | if (string_is_glob(patterns[pos])) | |
273 | log_debug("Pattern \"%s\" doesn't match any interface, ignoring.", | |
274 | patterns[pos]); | |
275 | else | |
276 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), | |
277 | "Interface \"%s\" not found.", patterns[pos]); | |
278 | } | |
279 | ||
280 | if (c == 0) | |
281 | log_warning("No interfaces matched."); | |
282 | ||
283 | return 0; | |
284 | } | |
ee8c4568 | 285 | |
43449ca3 MY |
286 | static void operational_state_to_color( |
287 | const char *name, | |
288 | const char *state, | |
289 | const char **on, | |
290 | const char **off) { | |
291 | ||
ceb366df ZJS |
292 | if (STRPTR_IN_SET(state, "routable", "enslaved") || |
293 | (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) { | |
37da8bca YW |
294 | if (on) |
295 | *on = ansi_highlight_green(); | |
296 | if (off) | |
297 | *off = ansi_normal(); | |
7e5a080a | 298 | } else if (streq_ptr(state, "degraded")) { |
37da8bca YW |
299 | if (on) |
300 | *on = ansi_highlight_yellow(); | |
301 | if (off) | |
302 | *off = ansi_normal(); | |
303 | } else { | |
304 | if (on) | |
305 | *on = ""; | |
306 | if (off) | |
307 | *off = ""; | |
308 | } | |
7e5a080a LP |
309 | } |
310 | ||
311 | static void setup_state_to_color(const char *state, const char **on, const char **off) { | |
7e5a080a | 312 | if (streq_ptr(state, "configured")) { |
37da8bca YW |
313 | if (on) |
314 | *on = ansi_highlight_green(); | |
315 | if (off) | |
316 | *off = ansi_normal(); | |
7e5a080a | 317 | } else if (streq_ptr(state, "configuring")) { |
37da8bca YW |
318 | if (on) |
319 | *on = ansi_highlight_yellow(); | |
320 | if (off) | |
321 | *off = ansi_normal(); | |
1cf03a4f | 322 | } else if (STRPTR_IN_SET(state, "failed", "linger")) { |
37da8bca YW |
323 | if (on) |
324 | *on = ansi_highlight_red(); | |
325 | if (off) | |
326 | *off = ansi_normal(); | |
327 | } else { | |
328 | if (on) | |
329 | *on = ""; | |
330 | if (off) | |
331 | *off = ""; | |
332 | } | |
7e5a080a LP |
333 | } |
334 | ||
4a481ec4 AŠ |
335 | static void online_state_to_color(const char *state, const char **on, const char **off) { |
336 | if (streq_ptr(state, "online")) { | |
337 | if (on) | |
338 | *on = ansi_highlight_green(); | |
339 | if (off) | |
340 | *off = ansi_normal(); | |
341 | } else if (streq_ptr(state, "partial")) { | |
342 | if (on) | |
343 | *on = ansi_highlight_yellow(); | |
344 | if (off) | |
345 | *off = ansi_normal(); | |
346 | } else { | |
347 | if (on) | |
348 | *on = ""; | |
349 | if (off) | |
350 | *off = ""; | |
351 | } | |
352 | } | |
353 | ||
b24281aa SS |
354 | typedef struct VxLanInfo { |
355 | uint32_t vni; | |
356 | uint32_t link; | |
357 | ||
358 | int local_family; | |
359 | int group_family; | |
360 | ||
361 | union in_addr_union local; | |
362 | union in_addr_union group; | |
363 | ||
364 | uint16_t dest_port; | |
365 | ||
22ae6c7d SS |
366 | uint8_t proxy; |
367 | uint8_t learning; | |
22ae6c7d SS |
368 | uint8_t rsc; |
369 | uint8_t l2miss; | |
370 | uint8_t l3miss; | |
371 | uint8_t tos; | |
372 | uint8_t ttl; | |
b24281aa SS |
373 | } VxLanInfo; |
374 | ||
6d0c65ff | 375 | typedef struct LinkInfo { |
e997c4b0 | 376 | char name[IFNAMSIZ+1]; |
430f07f4 | 377 | char *netdev_kind; |
172353b1 | 378 | sd_device *sd_device; |
6d0c65ff | 379 | int ifindex; |
1c4a6088 | 380 | unsigned short iftype; |
ca2b7cd8 | 381 | struct hw_addr_data hw_address; |
ed9fa69f | 382 | struct hw_addr_data permanent_hw_address; |
6cfef1b3 | 383 | uint32_t master; |
b147503e | 384 | uint32_t mtu; |
2c73f59b SS |
385 | uint32_t min_mtu; |
386 | uint32_t max_mtu; | |
0307afc6 SS |
387 | uint32_t tx_queues; |
388 | uint32_t rx_queues; | |
d69b62de | 389 | uint8_t addr_gen_mode; |
e810df37 | 390 | char *qdisc; |
511070ee | 391 | char **alternative_names; |
b147503e | 392 | |
a459b24f YW |
393 | union { |
394 | struct rtnl_link_stats64 stats64; | |
395 | struct rtnl_link_stats stats; | |
396 | }; | |
397 | ||
42a63431 YW |
398 | uint64_t tx_bitrate; |
399 | uint64_t rx_bitrate; | |
335dd8ba | 400 | |
c82d1bf2 SS |
401 | /* bridge info */ |
402 | uint32_t forward_delay; | |
403 | uint32_t hello_time; | |
404 | uint32_t max_age; | |
405 | uint32_t ageing_time; | |
406 | uint32_t stp_state; | |
12ef8fb6 | 407 | uint32_t cost; |
c82d1bf2 SS |
408 | uint16_t priority; |
409 | uint8_t mcast_igmp_version; | |
12ef8fb6 | 410 | uint8_t port_state; |
c82d1bf2 | 411 | |
b24281aa SS |
412 | /* vxlan info */ |
413 | VxLanInfo vxlan_info; | |
414 | ||
2b2a1ae6 SS |
415 | /* vlan info */ |
416 | uint16_t vlan_id; | |
417 | ||
dca35224 | 418 | /* tunnel info */ |
4e1a1991 SS |
419 | uint8_t ttl; |
420 | uint8_t tos; | |
e7b38d7d SS |
421 | uint8_t inherit; |
422 | uint8_t df; | |
423 | uint8_t csum; | |
424 | uint8_t csum6_tx; | |
425 | uint8_t csum6_rx; | |
4e1a1991 SS |
426 | uint16_t tunnel_port; |
427 | uint32_t vni; | |
e7b38d7d | 428 | uint32_t label; |
dca35224 SS |
429 | union in_addr_union local; |
430 | union in_addr_union remote; | |
431 | ||
b1d6fe70 SS |
432 | /* bonding info */ |
433 | uint8_t mode; | |
434 | uint32_t miimon; | |
435 | uint32_t updelay; | |
436 | uint32_t downdelay; | |
437 | ||
cf217a09 SS |
438 | /* macvlan and macvtap info */ |
439 | uint32_t macvlan_mode; | |
440 | ||
851ef1ed SS |
441 | /* ipvlan info */ |
442 | uint16_t ipvlan_mode; | |
443 | uint16_t ipvlan_flags; | |
444 | ||
c967d2c7 YW |
445 | /* ethtool info */ |
446 | int autonegotiation; | |
50299121 | 447 | uint64_t speed; |
c967d2c7 YW |
448 | Duplex duplex; |
449 | NetDevPort port; | |
450 | ||
8d07de25 | 451 | /* wlan info */ |
78404d22 | 452 | enum nl80211_iftype wlan_iftype; |
8d07de25 ZJS |
453 | char *ssid; |
454 | struct ether_addr bssid; | |
455 | ||
57ae8cd8 | 456 | bool has_hw_address:1; |
ed9fa69f | 457 | bool has_permanent_hw_address:1; |
0307afc6 SS |
458 | bool has_tx_queues:1; |
459 | bool has_rx_queues:1; | |
a459b24f YW |
460 | bool has_stats64:1; |
461 | bool has_stats:1; | |
335dd8ba | 462 | bool has_bitrates:1; |
c967d2c7 | 463 | bool has_ethtool_link_info:1; |
8d07de25 | 464 | bool has_wlan_link_info:1; |
4e1a1991 | 465 | bool has_tunnel_ipv4:1; |
d69b62de | 466 | bool has_ipv6_address_generation_mode:1; |
172353b1 ZJS |
467 | |
468 | bool needs_freeing:1; | |
6d0c65ff LP |
469 | } LinkInfo; |
470 | ||
93bab288 YW |
471 | static int link_info_compare(const LinkInfo *a, const LinkInfo *b) { |
472 | return CMP(a->ifindex, b->ifindex); | |
6d0c65ff LP |
473 | } |
474 | ||
75db809a | 475 | static LinkInfo* link_info_array_free(LinkInfo *array) { |
172353b1 ZJS |
476 | for (unsigned i = 0; array && array[i].needs_freeing; i++) { |
477 | sd_device_unref(array[i].sd_device); | |
430f07f4 | 478 | free(array[i].netdev_kind); |
8d07de25 | 479 | free(array[i].ssid); |
e810df37 | 480 | free(array[i].qdisc); |
511070ee | 481 | strv_free(array[i].alternative_names); |
172353b1 ZJS |
482 | } |
483 | ||
484 | return mfree(array); | |
485 | } | |
486 | DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo*, link_info_array_free); | |
487 | ||
c82d1bf2 | 488 | static int decode_netdev(sd_netlink_message *m, LinkInfo *info) { |
c82d1bf2 SS |
489 | int r; |
490 | ||
491 | assert(m); | |
492 | assert(info); | |
493 | ||
494 | r = sd_netlink_message_enter_container(m, IFLA_LINKINFO); | |
495 | if (r < 0) | |
496 | return r; | |
497 | ||
430f07f4 YW |
498 | r = sd_netlink_message_read_string_strdup(m, IFLA_INFO_KIND, &info->netdev_kind); |
499 | if (r < 0) { | |
500 | (void) sd_netlink_message_exit_container(m); | |
c82d1bf2 | 501 | return r; |
430f07f4 | 502 | } |
c82d1bf2 SS |
503 | |
504 | r = sd_netlink_message_enter_container(m, IFLA_INFO_DATA); | |
505 | if (r < 0) | |
506 | return r; | |
507 | ||
430f07f4 | 508 | if (streq(info->netdev_kind, "bridge")) { |
c82d1bf2 SS |
509 | (void) sd_netlink_message_read_u32(m, IFLA_BR_FORWARD_DELAY, &info->forward_delay); |
510 | (void) sd_netlink_message_read_u32(m, IFLA_BR_HELLO_TIME, &info->hello_time); | |
511 | (void) sd_netlink_message_read_u32(m, IFLA_BR_MAX_AGE, &info->max_age); | |
512 | (void) sd_netlink_message_read_u32(m, IFLA_BR_AGEING_TIME, &info->ageing_time); | |
513 | (void) sd_netlink_message_read_u32(m, IFLA_BR_STP_STATE, &info->stp_state); | |
12ef8fb6 | 514 | (void) sd_netlink_message_read_u32(m, IFLA_BRPORT_COST, &info->cost); |
c82d1bf2 SS |
515 | (void) sd_netlink_message_read_u16(m, IFLA_BR_PRIORITY, &info->priority); |
516 | (void) sd_netlink_message_read_u8(m, IFLA_BR_MCAST_IGMP_VERSION, &info->mcast_igmp_version); | |
12ef8fb6 | 517 | (void) sd_netlink_message_read_u8(m, IFLA_BRPORT_STATE, &info->port_state); |
430f07f4 | 518 | } if (streq(info->netdev_kind, "bond")) { |
b1d6fe70 SS |
519 | (void) sd_netlink_message_read_u8(m, IFLA_BOND_MODE, &info->mode); |
520 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_MIIMON, &info->miimon); | |
521 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_DOWNDELAY, &info->downdelay); | |
522 | (void) sd_netlink_message_read_u32(m, IFLA_BOND_UPDELAY, &info->updelay); | |
430f07f4 | 523 | } else if (streq(info->netdev_kind, "vxlan")) { |
b24281aa SS |
524 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_ID, &info->vxlan_info.vni); |
525 | ||
526 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_GROUP, &info->vxlan_info.group.in); | |
527 | if (r >= 0) | |
528 | info->vxlan_info.group_family = AF_INET; | |
529 | else { | |
530 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_GROUP6, &info->vxlan_info.group.in6); | |
531 | if (r >= 0) | |
532 | info->vxlan_info.group_family = AF_INET6; | |
533 | } | |
534 | ||
535 | r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_LOCAL, &info->vxlan_info.local.in); | |
536 | if (r >= 0) | |
537 | info->vxlan_info.local_family = AF_INET; | |
538 | else { | |
539 | r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_LOCAL6, &info->vxlan_info.local.in6); | |
540 | if (r >= 0) | |
541 | info->vxlan_info.local_family = AF_INET6; | |
542 | } | |
543 | ||
544 | (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_LINK, &info->vxlan_info.link); | |
545 | (void) sd_netlink_message_read_u16(m, IFLA_VXLAN_PORT, &info->vxlan_info.dest_port); | |
22ae6c7d SS |
546 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_PROXY, &info->vxlan_info.proxy); |
547 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_LEARNING, &info->vxlan_info.learning); | |
548 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_RSC, &info->vxlan_info.rsc); | |
549 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L3MISS, &info->vxlan_info.l3miss); | |
550 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L2MISS, &info->vxlan_info.l2miss); | |
551 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TOS, &info->vxlan_info.tos); | |
552 | (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TTL, &info->vxlan_info.ttl); | |
430f07f4 | 553 | } else if (streq(info->netdev_kind, "vlan")) |
2b2a1ae6 | 554 | (void) sd_netlink_message_read_u16(m, IFLA_VLAN_ID, &info->vlan_id); |
430f07f4 | 555 | else if (STR_IN_SET(info->netdev_kind, "ipip", "sit")) { |
dca35224 SS |
556 | (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_LOCAL, &info->local.in); |
557 | (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_REMOTE, &info->remote.in); | |
430f07f4 | 558 | } else if (streq(info->netdev_kind, "geneve")) { |
4e1a1991 SS |
559 | (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_ID, &info->vni); |
560 | ||
561 | r = sd_netlink_message_read_in_addr(m, IFLA_GENEVE_REMOTE, &info->remote.in); | |
562 | if (r >= 0) | |
563 | info->has_tunnel_ipv4 = true; | |
564 | else | |
565 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GENEVE_REMOTE6, &info->remote.in6); | |
566 | ||
567 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL, &info->ttl); | |
e7b38d7d | 568 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL_INHERIT, &info->inherit); |
4e1a1991 | 569 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TOS, &info->tos); |
e7b38d7d SS |
570 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_DF, &info->df); |
571 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_CSUM, &info->csum); | |
572 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, &info->csum6_tx); | |
573 | (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, &info->csum6_rx); | |
4e1a1991 | 574 | (void) sd_netlink_message_read_u16(m, IFLA_GENEVE_PORT, &info->tunnel_port); |
e7b38d7d | 575 | (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_LABEL, &info->label); |
430f07f4 | 576 | } else if (STR_IN_SET(info->netdev_kind, "gre", "gretap", "erspan")) { |
4d75ea1e SS |
577 | (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_LOCAL, &info->local.in); |
578 | (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_REMOTE, &info->remote.in); | |
430f07f4 | 579 | } else if (STR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan")) { |
ad760bc1 SS |
580 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_LOCAL, &info->local.in6); |
581 | (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_REMOTE, &info->remote.in6); | |
430f07f4 | 582 | } else if (streq(info->netdev_kind, "vti")) { |
5712d689 SS |
583 | (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_LOCAL, &info->local.in); |
584 | (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_REMOTE, &info->remote.in); | |
430f07f4 | 585 | } else if (streq(info->netdev_kind, "vti6")) { |
5712d689 SS |
586 | (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_LOCAL, &info->local.in6); |
587 | (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_REMOTE, &info->remote.in6); | |
430f07f4 | 588 | } else if (STR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) |
cf217a09 | 589 | (void) sd_netlink_message_read_u32(m, IFLA_MACVLAN_MODE, &info->macvlan_mode); |
430f07f4 | 590 | else if (streq(info->netdev_kind, "ipvlan")) { |
851ef1ed SS |
591 | (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_MODE, &info->ipvlan_mode); |
592 | (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_FLAGS, &info->ipvlan_flags); | |
593 | } | |
c82d1bf2 | 594 | |
c82d1bf2 SS |
595 | (void) sd_netlink_message_exit_container(m); |
596 | (void) sd_netlink_message_exit_container(m); | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
43449ca3 MY |
601 | static int decode_link( |
602 | sd_netlink_message *m, | |
603 | LinkInfo *info, | |
604 | char * const *patterns, | |
605 | bool matched_patterns[]) { | |
606 | ||
f7581ed6 | 607 | _cleanup_strv_free_ char **altnames = NULL; |
e810df37 | 608 | const char *name, *qdisc; |
a6962904 | 609 | int ifindex, r; |
c82d1bf2 | 610 | uint16_t type; |
6d0c65ff | 611 | |
e997c4b0 LP |
612 | assert(m); |
613 | assert(info); | |
6d0c65ff | 614 | |
e997c4b0 LP |
615 | r = sd_netlink_message_get_type(m, &type); |
616 | if (r < 0) | |
617 | return r; | |
6d0c65ff | 618 | |
e997c4b0 LP |
619 | if (type != RTM_NEWLINK) |
620 | return 0; | |
6d0c65ff | 621 | |
a6962904 | 622 | r = sd_rtnl_message_link_get_ifindex(m, &ifindex); |
e997c4b0 LP |
623 | if (r < 0) |
624 | return r; | |
6d0c65ff | 625 | |
e997c4b0 LP |
626 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name); |
627 | if (r < 0) | |
628 | return r; | |
6d0c65ff | 629 | |
f7581ed6 | 630 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames); |
48c53989 | 631 | if (r < 0 && r != -ENODATA) |
f7581ed6 YW |
632 | return r; |
633 | ||
a6962904 YW |
634 | if (patterns) { |
635 | char str[DECIMAL_STR_MAX(int)]; | |
0ef84b80 ZJS |
636 | size_t pos; |
637 | ||
638 | assert(matched_patterns); | |
a6962904 YW |
639 | |
640 | xsprintf(str, "%i", ifindex); | |
0ef84b80 ZJS |
641 | if (!strv_fnmatch_full(patterns, str, 0, &pos) && |
642 | !strv_fnmatch_full(patterns, name, 0, &pos)) { | |
f7581ed6 | 643 | bool match = false; |
f7581ed6 YW |
644 | |
645 | STRV_FOREACH(p, altnames) | |
0ef84b80 | 646 | if (strv_fnmatch_full(patterns, *p, 0, &pos)) { |
f7581ed6 YW |
647 | match = true; |
648 | break; | |
649 | } | |
650 | if (!match) | |
651 | return 0; | |
652 | } | |
0ef84b80 ZJS |
653 | |
654 | matched_patterns[pos] = true; | |
a6962904 YW |
655 | } |
656 | ||
b147503e | 657 | r = sd_rtnl_message_link_get_type(m, &info->iftype); |
e997c4b0 LP |
658 | if (r < 0) |
659 | return r; | |
6d0c65ff | 660 | |
2388b2f4 | 661 | strscpy(info->name, sizeof info->name, name); |
a6962904 | 662 | info->ifindex = ifindex; |
f7581ed6 | 663 | info->alternative_names = TAKE_PTR(altnames); |
b147503e | 664 | |
57ae8cd8 | 665 | info->has_hw_address = |
f1f17144 | 666 | netlink_message_read_hw_addr(m, IFLA_ADDRESS, &info->hw_address) >= 0 && |
de0f1579 | 667 | info->hw_address.length > 0; |
b147503e | 668 | |
ed9fa69f | 669 | info->has_permanent_hw_address = |
49c351a8 YW |
670 | (netlink_message_read_hw_addr(m, IFLA_PERM_ADDRESS, &info->permanent_hw_address) >= 0 || |
671 | ethtool_get_permanent_hw_addr(NULL, info->name, &info->permanent_hw_address) >= 0) && | |
ed9fa69f YW |
672 | !hw_addr_is_null(&info->permanent_hw_address) && |
673 | !hw_addr_equal(&info->permanent_hw_address, &info->hw_address); | |
caa8538a | 674 | |
c06ff86e YW |
675 | (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu); |
676 | (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu); | |
677 | (void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu); | |
2c73f59b | 678 | |
0307afc6 SS |
679 | info->has_rx_queues = |
680 | sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 && | |
681 | info->rx_queues > 0; | |
682 | ||
683 | info->has_tx_queues = | |
684 | sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 && | |
685 | info->tx_queues > 0; | |
686 | ||
a459b24f YW |
687 | if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0) |
688 | info->has_stats64 = true; | |
689 | else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0) | |
690 | info->has_stats = true; | |
691 | ||
e810df37 SS |
692 | r = sd_netlink_message_read_string(m, IFLA_QDISC, &qdisc); |
693 | if (r >= 0) { | |
694 | info->qdisc = strdup(qdisc); | |
695 | if (!info->qdisc) | |
696 | return log_oom(); | |
697 | } | |
698 | ||
6cfef1b3 SS |
699 | (void) sd_netlink_message_read_u32(m, IFLA_MASTER, &info->master); |
700 | ||
d69b62de SS |
701 | r = sd_netlink_message_enter_container(m, IFLA_AF_SPEC); |
702 | if (r >= 0) { | |
703 | r = sd_netlink_message_enter_container(m, AF_INET6); | |
704 | if (r >= 0) { | |
705 | r = sd_netlink_message_read_u8(m, IFLA_INET6_ADDR_GEN_MODE, &info->addr_gen_mode); | |
7f20a9e5 SS |
706 | if (r >= 0 && IN_SET(info->addr_gen_mode, |
707 | IN6_ADDR_GEN_MODE_EUI64, | |
708 | IN6_ADDR_GEN_MODE_NONE, | |
709 | IN6_ADDR_GEN_MODE_STABLE_PRIVACY, | |
710 | IN6_ADDR_GEN_MODE_RANDOM)) | |
d69b62de SS |
711 | info->has_ipv6_address_generation_mode = true; |
712 | ||
713 | (void) sd_netlink_message_exit_container(m); | |
714 | } | |
715 | (void) sd_netlink_message_exit_container(m); | |
716 | } | |
717 | ||
c82d1bf2 SS |
718 | /* fill kind info */ |
719 | (void) decode_netdev(m, info); | |
720 | ||
e997c4b0 LP |
721 | return 1; |
722 | } | |
723 | ||
090c923b MAL |
724 | static int link_get_property( |
725 | sd_bus *bus, | |
726 | const LinkInfo *link, | |
727 | sd_bus_error *error, | |
728 | sd_bus_message **reply, | |
729 | const char *iface, | |
8bdece74 FS |
730 | const char *propname, |
731 | const char *type) { | |
100433e0 | 732 | |
100433e0 | 733 | _cleanup_free_ char *path = NULL; |
61c84edf | 734 | char ifindex_str[DECIMAL_STR_MAX(int)]; |
335dd8ba YW |
735 | int r; |
736 | ||
43449ca3 MY |
737 | assert(bus); |
738 | assert(link); | |
739 | assert(link->ifindex >= 0); | |
740 | assert(error); | |
741 | assert(reply); | |
742 | assert(iface); | |
743 | assert(propname); | |
8bdece74 | 744 | assert(type); |
43449ca3 | 745 | |
100433e0 | 746 | xsprintf(ifindex_str, "%i", link->ifindex); |
335dd8ba YW |
747 | |
748 | r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path); | |
749 | if (r < 0) | |
750 | return r; | |
751 | ||
8bdece74 | 752 | return sd_bus_get_property(bus, "org.freedesktop.network1", path, iface, propname, error, reply, type); |
090c923b MAL |
753 | } |
754 | ||
755 | static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) { | |
756 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
757 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
758 | int r; | |
759 | ||
43449ca3 MY |
760 | assert(bus); |
761 | assert(link); | |
762 | ||
8bdece74 | 763 | r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.Link", "BitRates", "(tt)"); |
335dd8ba | 764 | if (r < 0) { |
955a6329 ZJS |
765 | bool quiet = sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY, |
766 | BUS_ERROR_SPEED_METER_INACTIVE); | |
8210a61a ZJS |
767 | |
768 | return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, | |
769 | r, "Failed to query link bit rates: %s", bus_error_message(&error, r)); | |
335dd8ba YW |
770 | } |
771 | ||
42a63431 | 772 | r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate); |
335dd8ba YW |
773 | if (r < 0) |
774 | return bus_log_parse_error(r); | |
775 | ||
776 | r = sd_bus_message_exit_container(reply); | |
777 | if (r < 0) | |
778 | return bus_log_parse_error(r); | |
779 | ||
e813de54 | 780 | link->has_bitrates = link->tx_bitrate != UINT64_MAX && link->rx_bitrate != UINT64_MAX; |
335dd8ba YW |
781 | |
782 | return 0; | |
783 | } | |
784 | ||
516e9c80 | 785 | static void acquire_ether_link_info(int *fd, LinkInfo *link) { |
43449ca3 MY |
786 | assert(fd); |
787 | assert(link); | |
788 | ||
789 | if (ethtool_get_link_info(fd, | |
790 | link->name, | |
516e9c80 ZJS |
791 | &link->autonegotiation, |
792 | &link->speed, | |
793 | &link->duplex, | |
794 | &link->port) >= 0) | |
795 | link->has_ethtool_link_info = true; | |
796 | } | |
797 | ||
8d07de25 ZJS |
798 | static void acquire_wlan_link_info(LinkInfo *link) { |
799 | _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL; | |
78404d22 | 800 | int r, k = 0; |
8d07de25 | 801 | |
43449ca3 MY |
802 | assert(link); |
803 | ||
fb53ee0a YW |
804 | if (!link->sd_device) |
805 | return; | |
806 | ||
807 | if (!device_is_devtype(link->sd_device, "wlan")) | |
8d07de25 ZJS |
808 | return; |
809 | ||
810 | r = sd_genl_socket_open(&genl); | |
811 | if (r < 0) { | |
812 | log_debug_errno(r, "Failed to open generic netlink socket: %m"); | |
813 | return; | |
814 | } | |
815 | ||
78cca583 | 816 | (void) sd_netlink_increase_rxbuf(genl, RCVBUF_SIZE); |
8d07de25 | 817 | |
78404d22 | 818 | r = wifi_get_interface(genl, link->ifindex, &link->wlan_iftype, &link->ssid); |
8d07de25 ZJS |
819 | if (r < 0) |
820 | log_debug_errno(r, "%s: failed to query ssid: %m", link->name); | |
821 | ||
33ebda2e | 822 | if (link->wlan_iftype == NL80211_IFTYPE_STATION) { |
78404d22 YW |
823 | k = wifi_get_station(genl, link->ifindex, &link->bssid); |
824 | if (k < 0) | |
825 | log_debug_errno(k, "%s: failed to query bssid: %m", link->name); | |
826 | } | |
8d07de25 ZJS |
827 | |
828 | link->has_wlan_link_info = r > 0 || k > 0; | |
829 | } | |
830 | ||
43449ca3 | 831 | static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char * const *patterns, LinkInfo **ret) { |
4afd3348 | 832 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
172353b1 | 833 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
61c84edf | 834 | _cleanup_free_ bool *matched_patterns = NULL; |
254d1313 | 835 | _cleanup_close_ int fd = -EBADF; |
319a4f4b | 836 | size_t c = 0; |
7d367b45 LP |
837 | int r; |
838 | ||
b147503e | 839 | assert(rtnl); |
7d367b45 | 840 | assert(ret); |
ee8c4568 | 841 | |
ee8c4568 LP |
842 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); |
843 | if (r < 0) | |
844 | return rtnl_log_create_error(r); | |
845 | ||
24c0f385 | 846 | r = sd_netlink_message_set_request_dump(req, true); |
ee8c4568 LP |
847 | if (r < 0) |
848 | return rtnl_log_create_error(r); | |
849 | ||
1c4baffc | 850 | r = sd_netlink_call(rtnl, req, 0, &reply); |
f647962d MS |
851 | if (r < 0) |
852 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
ee8c4568 | 853 | |
0ef84b80 ZJS |
854 | if (patterns) { |
855 | matched_patterns = new0(bool, strv_length(patterns)); | |
856 | if (!matched_patterns) | |
857 | return log_oom(); | |
858 | } | |
859 | ||
cda7fc8d | 860 | for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
319a4f4b | 861 | if (!GREEDY_REALLOC0(links, c + 2)) /* We keep one trailing one as marker */ |
7e5a080a | 862 | return -ENOMEM; |
7d367b45 | 863 | |
0ef84b80 | 864 | r = decode_link(i, links + c, patterns, matched_patterns); |
7e5a080a LP |
865 | if (r < 0) |
866 | return r; | |
c967d2c7 YW |
867 | if (r == 0) |
868 | continue; | |
869 | ||
172353b1 ZJS |
870 | links[c].needs_freeing = true; |
871 | ||
0ac655a6 | 872 | (void) sd_device_new_from_ifindex(&links[c].sd_device, links[c].ifindex); |
172353b1 | 873 | |
516e9c80 | 874 | acquire_ether_link_info(&fd, &links[c]); |
8d07de25 | 875 | acquire_wlan_link_info(&links[c]); |
c967d2c7 YW |
876 | |
877 | c++; | |
7e5a080a LP |
878 | } |
879 | ||
0ef84b80 ZJS |
880 | /* Look if we matched all our arguments that are not globs. It |
881 | * is OK for a glob to match nothing, but not for an exact argument. */ | |
882 | for (size_t pos = 0; pos < strv_length(patterns); pos++) { | |
883 | if (matched_patterns[pos]) | |
884 | continue; | |
885 | ||
886 | if (string_is_glob(patterns[pos])) | |
887 | log_debug("Pattern \"%s\" doesn't match any interface, ignoring.", | |
888 | patterns[pos]); | |
889 | else | |
890 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), | |
891 | "Interface \"%s\" not found.", patterns[pos]); | |
892 | } | |
893 | ||
93bab288 | 894 | typesafe_qsort(links, c, link_info_compare); |
7e5a080a | 895 | |
335dd8ba | 896 | if (bus) |
7db07767 MY |
897 | FOREACH_ARRAY(link, links, c) |
898 | (void) acquire_link_bitrates(bus, link); | |
335dd8ba | 899 | |
1cc6c93a | 900 | *ret = TAKE_PTR(links); |
7e5a080a | 901 | |
0ef84b80 ZJS |
902 | if (patterns && c == 0) |
903 | log_warning("No interfaces matched."); | |
904 | ||
7e5a080a | 905 | return (int) c; |
7d367b45 | 906 | } |
ee8c4568 | 907 | |
7d367b45 | 908 | static int list_links(int argc, char *argv[], void *userdata) { |
445f0d8b | 909 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
b147503e | 910 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
172353b1 | 911 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
ff7c88a2 YW |
912 | _cleanup_(table_unrefp) Table *table = NULL; |
913 | TableCell *cell; | |
b48e277f | 914 | int c, r; |
b147503e | 915 | |
445f0d8b MY |
916 | r = acquire_bus(&bus); |
917 | if (r < 0) | |
918 | return r; | |
919 | ||
bfc2b05e YW |
920 | if (arg_json_format_flags != JSON_FORMAT_OFF) { |
921 | if (arg_all || argc <= 1) | |
d821e40c | 922 | return dump_manager_description(bus); |
bfc2b05e | 923 | else |
d821e40c | 924 | return dump_link_description(bus, strv_skip(argv, 1)); |
bfc2b05e YW |
925 | } |
926 | ||
b147503e LP |
927 | r = sd_netlink_open(&rtnl); |
928 | if (r < 0) | |
929 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
7d367b45 | 930 | |
335dd8ba | 931 | c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
6d0c65ff | 932 | if (c < 0) |
7d367b45 LP |
933 | return c; |
934 | ||
384c2c32 | 935 | pager_open(arg_pager_flags); |
7d367b45 | 936 | |
4252171a | 937 | table = table_new("idx", "link", "type", "operational", "setup"); |
ff7c88a2 YW |
938 | if (!table) |
939 | return log_oom(); | |
940 | ||
a42d9490 YW |
941 | if (arg_full) |
942 | table_set_width(table, 0); | |
943 | ||
ff7c88a2 | 944 | table_set_header(table, arg_legend); |
c8b62cf6 | 945 | table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
ff7c88a2 YW |
946 | |
947 | assert_se(cell = table_get_cell(table, 0, 0)); | |
948 | (void) table_set_minimum_width(table, cell, 3); | |
949 | (void) table_set_weight(table, cell, 0); | |
81914d9f | 950 | (void) table_set_ellipsize_percent(table, cell, 100); |
ff7c88a2 YW |
951 | (void) table_set_align_percent(table, cell, 100); |
952 | ||
953 | assert_se(cell = table_get_cell(table, 0, 1)); | |
81914d9f | 954 | (void) table_set_ellipsize_percent(table, cell, 100); |
6d0c65ff | 955 | |
7db07767 | 956 | FOREACH_ARRAY(link, links, c) { |
ab1525bc | 957 | _cleanup_free_ char *setup_state = NULL, *operational_state = NULL; |
ee8c4568 | 958 | _cleanup_free_ char *t = NULL; |
7db07767 | 959 | const char *on_color_operational, *on_color_setup; |
ee8c4568 | 960 | |
7db07767 MY |
961 | (void) sd_network_link_get_operational_state(link->ifindex, &operational_state); |
962 | operational_state_to_color(link->name, operational_state, &on_color_operational, NULL); | |
d57c365b | 963 | |
7db07767 | 964 | (void) sd_network_link_get_setup_state(link->ifindex, &setup_state); |
37da8bca | 965 | setup_state_to_color(setup_state, &on_color_setup, NULL); |
ee8c4568 | 966 | |
7db07767 | 967 | r = net_get_type_string(link->sd_device, link->iftype, &t); |
1a3caa49 YW |
968 | if (r == -ENOMEM) |
969 | return log_oom(); | |
ee8c4568 | 970 | |
ff7c88a2 | 971 | r = table_add_many(table, |
7db07767 MY |
972 | TABLE_INT, link->ifindex, |
973 | TABLE_STRING, link->name, | |
484f22dd YW |
974 | TABLE_STRING, t, |
975 | TABLE_STRING, operational_state, | |
8d0e0af2 | 976 | TABLE_SET_COLOR, on_color_operational, |
8f60c998 | 977 | TABLE_STRING, setup_state ?: "unmanaged", |
8d0e0af2 | 978 | TABLE_SET_COLOR, on_color_setup); |
ff7c88a2 | 979 | if (r < 0) |
bd17fa8c | 980 | return table_log_add_error(r); |
ee8c4568 LP |
981 | } |
982 | ||
ff7c88a2 YW |
983 | r = table_print(table, NULL); |
984 | if (r < 0) | |
4b6607d9 | 985 | return table_log_print_error(r); |
ff7c88a2 | 986 | |
ee8c4568 | 987 | if (arg_legend) |
6d0c65ff | 988 | printf("\n%i links listed.\n", c); |
ee8c4568 LP |
989 | |
990 | return 0; | |
991 | } | |
992 | ||
c09da729 | 993 | /* IEEE Organizationally Unique Identifier vendor string */ |
b147503e | 994 | static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) { |
61c84edf | 995 | _cleanup_free_ char *desc = NULL; |
81fd1dd3 | 996 | const char *description; |
61c84edf | 997 | char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1]; |
81fd1dd3 TG |
998 | int r; |
999 | ||
1000 | assert(ret); | |
c09da729 | 1001 | |
61c84edf | 1002 | if (!hwdb || !mac) |
81fd1dd3 TG |
1003 | return -EINVAL; |
1004 | ||
c09da729 TG |
1005 | /* skip commonly misused 00:00:00 (Xerox) prefix */ |
1006 | if (memcmp(mac, "\0\0\0", 3) == 0) | |
1007 | return -EINVAL; | |
1008 | ||
61c84edf | 1009 | xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac)); |
c09da729 | 1010 | |
81fd1dd3 TG |
1011 | r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description); |
1012 | if (r < 0) | |
1013 | return r; | |
c09da729 | 1014 | |
81fd1dd3 TG |
1015 | desc = strdup(description); |
1016 | if (!desc) | |
1017 | return -ENOMEM; | |
c09da729 | 1018 | |
61c84edf | 1019 | *ret = TAKE_PTR(desc); |
81fd1dd3 TG |
1020 | |
1021 | return 0; | |
c09da729 TG |
1022 | } |
1023 | ||
69fb1176 | 1024 | static int get_gateway_description( |
1c4baffc | 1025 | sd_netlink *rtnl, |
81fd1dd3 | 1026 | sd_hwdb *hwdb, |
69fb1176 LP |
1027 | int ifindex, |
1028 | int family, | |
1029 | union in_addr_union *gateway, | |
61c84edf MY |
1030 | char **ret) { |
1031 | ||
4afd3348 | 1032 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
c09da729 TG |
1033 | int r; |
1034 | ||
1035 | assert(rtnl); | |
1036 | assert(ifindex >= 0); | |
4c701096 | 1037 | assert(IN_SET(family, AF_INET, AF_INET6)); |
c09da729 | 1038 | assert(gateway); |
61c84edf | 1039 | assert(ret); |
c09da729 TG |
1040 | |
1041 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); | |
1042 | if (r < 0) | |
1043 | return r; | |
1044 | ||
24c0f385 | 1045 | r = sd_netlink_message_set_request_dump(req, true); |
c09da729 TG |
1046 | if (r < 0) |
1047 | return r; | |
1048 | ||
1c4baffc | 1049 | r = sd_netlink_call(rtnl, req, 0, &reply); |
c09da729 TG |
1050 | if (r < 0) |
1051 | return r; | |
1052 | ||
b48e277f | 1053 | for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
67a46833 YW |
1054 | union in_addr_union gw = IN_ADDR_NULL; |
1055 | struct ether_addr mac = ETHER_ADDR_NULL; | |
c09da729 TG |
1056 | uint16_t type; |
1057 | int ifi, fam; | |
1058 | ||
1c4baffc | 1059 | r = sd_netlink_message_get_errno(m); |
c09da729 | 1060 | if (r < 0) { |
61c84edf | 1061 | log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
c09da729 TG |
1062 | continue; |
1063 | } | |
1064 | ||
1c4baffc | 1065 | r = sd_netlink_message_get_type(m, &type); |
c09da729 | 1066 | if (r < 0) { |
61c84edf | 1067 | log_error_errno(r, "Failed to get netlink message type, ignoring: %m"); |
c09da729 TG |
1068 | continue; |
1069 | } | |
1070 | ||
1071 | if (type != RTM_NEWNEIGH) { | |
61c84edf MY |
1072 | log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1073 | "Got unexpected netlink message type %u, ignoring", | |
1074 | type); | |
c09da729 TG |
1075 | continue; |
1076 | } | |
1077 | ||
1078 | r = sd_rtnl_message_neigh_get_family(m, &fam); | |
1079 | if (r < 0) { | |
61c84edf | 1080 | log_error_errno(r, "Failed to get rtnl family, ignoring: %m"); |
c09da729 TG |
1081 | continue; |
1082 | } | |
1083 | ||
1084 | if (fam != family) { | |
61c84edf | 1085 | log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got invalid rtnl family %d, ignoring", fam); |
c09da729 TG |
1086 | continue; |
1087 | } | |
1088 | ||
1089 | r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); | |
1090 | if (r < 0) { | |
61c84edf | 1091 | log_error_errno(r, "Failed to get rtnl ifindex, ignoring: %m"); |
c09da729 TG |
1092 | continue; |
1093 | } | |
1094 | ||
1095 | if (ifindex > 0 && ifi != ifindex) | |
1096 | continue; | |
1097 | ||
1098 | switch (fam) { | |
61c84edf | 1099 | |
c09da729 | 1100 | case AF_INET: |
1c4baffc | 1101 | r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in); |
c09da729 TG |
1102 | if (r < 0) |
1103 | continue; | |
1104 | ||
1105 | break; | |
61c84edf | 1106 | |
c09da729 | 1107 | case AF_INET6: |
1c4baffc | 1108 | r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6); |
c09da729 TG |
1109 | if (r < 0) |
1110 | continue; | |
1111 | ||
1112 | break; | |
61c84edf | 1113 | |
c09da729 | 1114 | default: |
8cbc701d | 1115 | assert_not_reached(); |
c09da729 TG |
1116 | } |
1117 | ||
1118 | if (!in_addr_equal(fam, &gw, gateway)) | |
1119 | continue; | |
1120 | ||
49808e0e | 1121 | r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac); |
c09da729 TG |
1122 | if (r < 0) |
1123 | continue; | |
1124 | ||
61c84edf | 1125 | r = ieee_oui(hwdb, &mac, ret); |
c09da729 TG |
1126 | if (r < 0) |
1127 | continue; | |
1128 | ||
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | return -ENODATA; | |
1133 | } | |
1134 | ||
8fff78a1 | 1135 | static int dump_list(Table *table, const char *key, char * const *l) { |
536cdd07 YW |
1136 | int r; |
1137 | ||
43449ca3 | 1138 | assert(table); |
8fff78a1 | 1139 | assert(key); |
43449ca3 | 1140 | |
536cdd07 YW |
1141 | if (strv_isempty(l)) |
1142 | return 0; | |
1143 | ||
1144 | r = table_add_many(table, | |
8fff78a1 | 1145 | TABLE_FIELD, key, |
536cdd07 YW |
1146 | TABLE_STRV, l); |
1147 | if (r < 0) | |
1148 | return table_log_add_error(r); | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
7db07767 MY |
1153 | static int dump_gateways(sd_netlink *rtnl, sd_hwdb *hwdb, Table *table, int ifindex) { |
1154 | _cleanup_free_ struct local_address *local_addrs = NULL; | |
536cdd07 | 1155 | _cleanup_strv_free_ char **buf = NULL; |
cda7fc8d | 1156 | int r, n; |
c09da729 | 1157 | |
837f57da | 1158 | assert(rtnl); |
98d5bef3 | 1159 | assert(table); |
837f57da | 1160 | |
7db07767 | 1161 | n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
536cdd07 | 1162 | if (n <= 0) |
b6a3ca6d | 1163 | return n; |
c09da729 | 1164 | |
7db07767 | 1165 | FOREACH_ARRAY(local, local_addrs, n) { |
94a77962 | 1166 | _cleanup_free_ char *description = NULL; |
c09da729 | 1167 | |
7db07767 | 1168 | r = get_gateway_description(rtnl, hwdb, local->ifindex, local->family, &local->address, &description); |
c09da729 | 1169 | if (r < 0) |
536cdd07 | 1170 | log_debug_errno(r, "Could not get description of gateway, ignoring: %m"); |
c09da729 | 1171 | |
536cdd07 | 1172 | /* Show interface name for the entry if we show entries for all interfaces */ |
94a77962 | 1173 | r = strv_extendf(&buf, "%s%s%s%s%s%s", |
7db07767 | 1174 | IN_ADDR_TO_STRING(local->family, &local->address), |
94a77962 ZJS |
1175 | description ? " (" : "", |
1176 | strempty(description), | |
1177 | description ? ")" : "", | |
536cdd07 | 1178 | ifindex <= 0 ? " on " : "", |
7db07767 | 1179 | ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
98d5bef3 | 1180 | if (r < 0) |
536cdd07 | 1181 | return log_oom(); |
c09da729 TG |
1182 | } |
1183 | ||
8fff78a1 | 1184 | return dump_list(table, "Gateway", buf); |
c09da729 TG |
1185 | } |
1186 | ||
69fb1176 | 1187 | static int dump_addresses( |
1c4baffc | 1188 | sd_netlink *rtnl, |
d41fa6ee | 1189 | sd_dhcp_lease *lease, |
98d5bef3 | 1190 | Table *table, |
69fb1176 LP |
1191 | int ifindex) { |
1192 | ||
7db07767 | 1193 | _cleanup_free_ struct local_address *local_addrs = NULL; |
536cdd07 | 1194 | _cleanup_strv_free_ char **buf = NULL; |
d41fa6ee | 1195 | struct in_addr dhcp4_address = {}; |
b48e277f | 1196 | int r, n; |
ee8c4568 | 1197 | |
837f57da | 1198 | assert(rtnl); |
98d5bef3 | 1199 | assert(table); |
837f57da | 1200 | |
7db07767 | 1201 | n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
536cdd07 | 1202 | if (n <= 0) |
ee8c4568 LP |
1203 | return n; |
1204 | ||
d41fa6ee YW |
1205 | if (lease) |
1206 | (void) sd_dhcp_lease_get_address(lease, &dhcp4_address); | |
cdf01b36 | 1207 | |
7db07767 | 1208 | FOREACH_ARRAY(local, local_addrs, n) { |
94a77962 ZJS |
1209 | struct in_addr server_address; |
1210 | bool dhcp4 = false; | |
3d0c8750 | 1211 | |
7db07767 | 1212 | if (local->family == AF_INET && in4_addr_equal(&local->address.in, &dhcp4_address)) |
94a77962 | 1213 | dhcp4 = sd_dhcp_lease_get_server_identifier(lease, &server_address) >= 0; |
cdf01b36 | 1214 | |
94a77962 | 1215 | r = strv_extendf(&buf, "%s%s%s%s%s%s", |
7db07767 | 1216 | IN_ADDR_TO_STRING(local->family, &local->address), |
94a77962 ZJS |
1217 | dhcp4 ? " (DHCP4 via " : "", |
1218 | dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "", | |
1219 | dhcp4 ? ")" : "", | |
536cdd07 | 1220 | ifindex <= 0 ? " on " : "", |
7db07767 | 1221 | ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
98d5bef3 | 1222 | if (r < 0) |
536cdd07 | 1223 | return log_oom(); |
ee8c4568 LP |
1224 | } |
1225 | ||
8fff78a1 | 1226 | return dump_list(table, "Address", buf); |
ee8c4568 LP |
1227 | } |
1228 | ||
d37b7627 SS |
1229 | static int dump_address_labels(sd_netlink *rtnl) { |
1230 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
0232beed | 1231 | _cleanup_(table_unrefp) Table *table = NULL; |
0232beed | 1232 | TableCell *cell; |
d37b7627 SS |
1233 | int r; |
1234 | ||
1235 | assert(rtnl); | |
1236 | ||
1237 | r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6); | |
1238 | if (r < 0) | |
1239 | return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m"); | |
1240 | ||
24c0f385 | 1241 | r = sd_netlink_message_set_request_dump(req, true); |
d37b7627 SS |
1242 | if (r < 0) |
1243 | return r; | |
1244 | ||
1245 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
1246 | if (r < 0) | |
1247 | return r; | |
1248 | ||
4252171a | 1249 | table = table_new("label", "prefix/prefixlen"); |
0232beed | 1250 | if (!table) |
bd17fa8c | 1251 | return log_oom(); |
0232beed | 1252 | |
a42d9490 YW |
1253 | if (arg_full) |
1254 | table_set_width(table, 0); | |
1255 | ||
ef1e0b9a | 1256 | r = table_set_sort(table, (size_t) 0); |
0232beed YW |
1257 | if (r < 0) |
1258 | return r; | |
1259 | ||
1260 | assert_se(cell = table_get_cell(table, 0, 0)); | |
1261 | (void) table_set_align_percent(table, cell, 100); | |
81914d9f | 1262 | (void) table_set_ellipsize_percent(table, cell, 100); |
0232beed YW |
1263 | |
1264 | assert_se(cell = table_get_cell(table, 0, 1)); | |
1265 | (void) table_set_align_percent(table, cell, 100); | |
d37b7627 | 1266 | |
b48e277f | 1267 | for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
94a77962 | 1268 | struct in6_addr prefix; |
d37b7627 SS |
1269 | uint8_t prefixlen; |
1270 | uint32_t label; | |
1271 | ||
1272 | r = sd_netlink_message_get_errno(m); | |
1273 | if (r < 0) { | |
61c84edf | 1274 | log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
d37b7627 SS |
1275 | continue; |
1276 | } | |
1277 | ||
1278 | r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label); | |
1279 | if (r < 0 && r != -ENODATA) { | |
1280 | log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m"); | |
1281 | continue; | |
1282 | } | |
1283 | ||
94a77962 | 1284 | r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix); |
d37b7627 SS |
1285 | if (r < 0) |
1286 | continue; | |
1287 | ||
1288 | r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen); | |
1289 | if (r < 0) | |
1290 | continue; | |
1291 | ||
81914d9f | 1292 | r = table_add_cell(table, NULL, TABLE_UINT32, &label); |
0232beed | 1293 | if (r < 0) |
bd17fa8c | 1294 | return table_log_add_error(r); |
0232beed | 1295 | |
94a77962 | 1296 | r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen); |
0232beed | 1297 | if (r < 0) |
bd17fa8c | 1298 | return table_log_add_error(r); |
d37b7627 SS |
1299 | } |
1300 | ||
bd17fa8c YW |
1301 | r = table_print(table, NULL); |
1302 | if (r < 0) | |
4b6607d9 | 1303 | return table_log_print_error(r); |
bd17fa8c YW |
1304 | |
1305 | return 0; | |
d37b7627 SS |
1306 | } |
1307 | ||
1308 | static int list_address_labels(int argc, char *argv[], void *userdata) { | |
1309 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
1310 | int r; | |
1311 | ||
1312 | r = sd_netlink_open(&rtnl); | |
1313 | if (r < 0) | |
1314 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
1315 | ||
61c84edf | 1316 | return dump_address_labels(rtnl); |
d37b7627 SS |
1317 | } |
1318 | ||
14a5c07a YW |
1319 | typedef struct InterfaceInfo { |
1320 | int ifindex; | |
1321 | const char *ifname; | |
1322 | char **altnames; | |
1323 | JsonVariant *v; | |
1324 | } InterfaceInfo; | |
837f57da | 1325 | |
14a5c07a YW |
1326 | static void interface_info_done(InterfaceInfo *p) { |
1327 | if (!p) | |
1328 | return; | |
837f57da | 1329 | |
14a5c07a YW |
1330 | strv_free(p->altnames); |
1331 | json_variant_unref(p->v); | |
837f57da LP |
1332 | } |
1333 | ||
14a5c07a YW |
1334 | static const JsonDispatch interface_info_dispatch_table[] = { |
1335 | { "InterfaceIndex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(InterfaceInfo, ifindex), JSON_MANDATORY }, | |
1336 | { "InterfaceName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(InterfaceInfo, ifname), JSON_MANDATORY }, | |
1337 | { "InterfaceAlternativeNames", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(InterfaceInfo, altnames), 0 }, | |
1338 | { "Neighbors", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(InterfaceInfo, v), 0 }, | |
1339 | {}, | |
1340 | }; | |
1341 | ||
1342 | typedef struct LLDPNeighborInfo { | |
1343 | const char *chassis_id; | |
1344 | const char *port_id; | |
1345 | const char *port_description; | |
1346 | const char *system_name; | |
1347 | const char *system_description; | |
1348 | uint16_t capabilities; | |
1349 | } LLDPNeighborInfo; | |
1350 | ||
1351 | static const JsonDispatch lldp_neighbor_dispatch_table[] = { | |
1352 | { "ChassisID", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, chassis_id), 0 }, | |
1353 | { "PortID", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_id), 0 }, | |
1354 | { "PortDescription", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_description), 0 }, | |
1355 | { "SystemName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_name), 0 }, | |
1356 | { "SystemDescription", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_description), 0 }, | |
1357 | { "EnabledCapabilities", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(LLDPNeighborInfo, capabilities), 0 }, | |
1358 | {}, | |
1359 | }; | |
1360 | ||
1361 | static int dump_lldp_neighbors(Varlink *vl, Table *table, int ifindex) { | |
536cdd07 | 1362 | _cleanup_strv_free_ char **buf = NULL; |
14a5c07a | 1363 | JsonVariant *reply; |
536cdd07 | 1364 | int r; |
837f57da | 1365 | |
14a5c07a | 1366 | assert(vl); |
98d5bef3 | 1367 | assert(table); |
837f57da LP |
1368 | assert(ifindex > 0); |
1369 | ||
14a5c07a YW |
1370 | r = varlink_callb_and_log(vl, "io.systemd.Network.GetLLDPNeighbors", &reply, |
1371 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR_INTEGER("InterfaceIndex", ifindex))); | |
837f57da LP |
1372 | if (r < 0) |
1373 | return r; | |
1374 | ||
14a5c07a YW |
1375 | JsonVariant *i; |
1376 | JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { | |
1377 | _cleanup_(interface_info_done) InterfaceInfo info = {}; | |
837f57da | 1378 | |
14a5c07a | 1379 | r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); |
837f57da LP |
1380 | if (r < 0) |
1381 | return r; | |
837f57da | 1382 | |
14a5c07a YW |
1383 | if (info.ifindex != ifindex) |
1384 | continue; | |
837f57da | 1385 | |
14a5c07a YW |
1386 | JsonVariant *neighbor; |
1387 | JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { | |
1388 | LLDPNeighborInfo neighbor_info = {}; | |
1389 | ||
1390 | r = json_dispatch(neighbor, lldp_neighbor_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &neighbor_info); | |
1391 | if (r < 0) | |
1392 | return r; | |
1393 | ||
1394 | r = strv_extendf(&buf, "%s%s%s%s on port %s%s%s%s", | |
1395 | strna(neighbor_info.system_name), | |
1396 | isempty(neighbor_info.system_description) ? "" : " (", | |
1397 | strempty(neighbor_info.system_description), | |
1398 | isempty(neighbor_info.system_description) ? "" : ")", | |
1399 | strna(neighbor_info.port_id), | |
1400 | isempty(neighbor_info.port_description) ? "" : " (", | |
1401 | strempty(neighbor_info.port_description), | |
1402 | isempty(neighbor_info.port_description) ? "" : ")"); | |
1403 | if (r < 0) | |
1404 | return log_oom(); | |
1405 | } | |
837f57da LP |
1406 | } |
1407 | ||
14a5c07a | 1408 | return dump_list(table, "Connected To", buf); |
837f57da LP |
1409 | } |
1410 | ||
d9ce1c24 MAL |
1411 | static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) { |
1412 | _cleanup_strv_free_ char **buf = NULL; | |
1413 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1414 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1415 | int r; | |
1416 | ||
43449ca3 MY |
1417 | assert(table); |
1418 | assert(prefix); | |
1419 | assert(bus); | |
1420 | assert(link); | |
1421 | ||
8bdece74 | 1422 | r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.DHCPServer", "Leases", "a(uayayayayt)"); |
d9ce1c24 MAL |
1423 | if (r < 0) { |
1424 | bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY); | |
1425 | ||
1426 | log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, | |
1427 | r, "Failed to query link DHCP leases: %s", bus_error_message(&error, r)); | |
1428 | return 0; | |
1429 | } | |
1430 | ||
d9ce1c24 MAL |
1431 | r = sd_bus_message_enter_container(reply, 'a', "(uayayayayt)"); |
1432 | if (r < 0) | |
1433 | return bus_log_parse_error(r); | |
1434 | ||
1435 | while ((r = sd_bus_message_enter_container(reply, 'r', "uayayayayt")) > 0) { | |
1436 | _cleanup_free_ char *id = NULL, *ip = NULL; | |
1437 | const void *client_id, *addr, *gtw, *hwaddr; | |
1438 | size_t client_id_sz, sz; | |
1439 | uint64_t expiration; | |
1440 | uint32_t family; | |
1441 | ||
1442 | r = sd_bus_message_read(reply, "u", &family); | |
1443 | if (r < 0) | |
1444 | return bus_log_parse_error(r); | |
1445 | ||
1446 | r = sd_bus_message_read_array(reply, 'y', &client_id, &client_id_sz); | |
1447 | if (r < 0) | |
1448 | return bus_log_parse_error(r); | |
1449 | ||
1450 | r = sd_bus_message_read_array(reply, 'y', &addr, &sz); | |
1451 | if (r < 0 || sz != 4) | |
1452 | return bus_log_parse_error(r); | |
1453 | ||
1454 | r = sd_bus_message_read_array(reply, 'y', >w, &sz); | |
1455 | if (r < 0 || sz != 4) | |
1456 | return bus_log_parse_error(r); | |
1457 | ||
1458 | r = sd_bus_message_read_array(reply, 'y', &hwaddr, &sz); | |
1459 | if (r < 0) | |
1460 | return bus_log_parse_error(r); | |
1461 | ||
1462 | r = sd_bus_message_read_basic(reply, 't', &expiration); | |
1463 | if (r < 0) | |
1464 | return bus_log_parse_error(r); | |
1465 | ||
18091320 | 1466 | r = sd_dhcp_client_id_to_string_from_raw(client_id, client_id_sz, &id); |
d9ce1c24 MAL |
1467 | if (r < 0) |
1468 | return bus_log_parse_error(r); | |
1469 | ||
1470 | r = in_addr_to_string(family, addr, &ip); | |
1471 | if (r < 0) | |
1472 | return bus_log_parse_error(r); | |
1473 | ||
1474 | r = strv_extendf(&buf, "%s (to %s)", ip, id); | |
1475 | if (r < 0) | |
1476 | return log_oom(); | |
1477 | ||
1478 | r = sd_bus_message_exit_container(reply); | |
1479 | if (r < 0) | |
1480 | return bus_log_parse_error(r); | |
1481 | } | |
1482 | if (r < 0) | |
1483 | return bus_log_parse_error(r); | |
1484 | ||
1485 | r = sd_bus_message_exit_container(reply); | |
1486 | if (r < 0) | |
1487 | return bus_log_parse_error(r); | |
1488 | ||
1489 | r = sd_bus_message_exit_container(reply); | |
1490 | if (r < 0) | |
1491 | return bus_log_parse_error(r); | |
1492 | ||
1493 | if (strv_isempty(buf)) { | |
1494 | r = strv_extendf(&buf, "none"); | |
1495 | if (r < 0) | |
1496 | return log_oom(); | |
1497 | } | |
1498 | ||
1499 | return dump_list(table, prefix, buf); | |
1500 | } | |
1501 | ||
98d5bef3 | 1502 | static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) { |
98d5bef3 | 1503 | int r; |
b295beea | 1504 | |
43449ca3 | 1505 | assert(table); |
b295beea LP |
1506 | assert(prefix); |
1507 | ||
8fff78a1 | 1508 | if (!ifindexes) |
98d5bef3 | 1509 | return 0; |
b295beea | 1510 | |
b48e277f | 1511 | for (unsigned c = 0; ifindexes[c] > 0; c++) { |
8fff78a1 MY |
1512 | if (c == 0) |
1513 | r = table_add_cell(table, NULL, TABLE_FIELD, prefix); | |
1514 | else | |
1515 | r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); | |
1516 | if (r < 0) | |
1517 | return table_log_add_error(r); | |
1518 | ||
1519 | r = table_add_cell(table, NULL, TABLE_IFINDEX, &ifindexes[c]); | |
98d5bef3 | 1520 | if (r < 0) |
bd17fa8c | 1521 | return table_log_add_error(r); |
b295beea | 1522 | } |
98d5bef3 YW |
1523 | |
1524 | return 0; | |
b295beea LP |
1525 | } |
1526 | ||
8fff78a1 MY |
1527 | #define DUMP_STATS_ONE(name, val_name) \ |
1528 | ({ \ | |
1529 | r = table_add_cell(table, NULL, TABLE_FIELD, name); \ | |
1530 | if (r < 0) \ | |
1531 | return table_log_add_error(r); \ | |
1532 | r = table_add_cell(table, NULL, \ | |
1533 | info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \ | |
1534 | info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \ | |
1535 | if (r < 0) \ | |
1536 | return table_log_add_error(r); \ | |
1537 | }) | |
a459b24f YW |
1538 | |
1539 | static int dump_statistics(Table *table, const LinkInfo *info) { | |
1540 | int r; | |
1541 | ||
43449ca3 MY |
1542 | assert(table); |
1543 | assert(info); | |
1544 | ||
a459b24f YW |
1545 | if (!arg_stats) |
1546 | return 0; | |
1547 | ||
1548 | if (!info->has_stats64 && !info->has_stats) | |
1549 | return 0; | |
1550 | ||
1551 | DUMP_STATS_ONE("Rx Packets", rx_packets); | |
1552 | DUMP_STATS_ONE("Tx Packets", tx_packets); | |
1553 | DUMP_STATS_ONE("Rx Bytes", rx_bytes); | |
1554 | DUMP_STATS_ONE("Tx Bytes", tx_bytes); | |
1555 | DUMP_STATS_ONE("Rx Errors", rx_errors); | |
1556 | DUMP_STATS_ONE("Tx Errors", tx_errors); | |
1557 | DUMP_STATS_ONE("Rx Dropped", rx_dropped); | |
1558 | DUMP_STATS_ONE("Tx Dropped", tx_dropped); | |
1559 | DUMP_STATS_ONE("Multicast Packets", multicast); | |
1560 | DUMP_STATS_ONE("Collisions", collisions); | |
1561 | ||
1562 | return 0; | |
1563 | } | |
1564 | ||
57ae8cd8 YW |
1565 | static int dump_hw_address(Table *table, sd_hwdb *hwdb, const char *field, const struct hw_addr_data *addr) { |
1566 | _cleanup_free_ char *description = NULL; | |
1567 | int r; | |
1568 | ||
1569 | assert(table); | |
1570 | assert(field); | |
1571 | assert(addr); | |
1572 | ||
1573 | if (addr->length == ETH_ALEN) | |
1574 | (void) ieee_oui(hwdb, &addr->ether, &description); | |
1575 | ||
8fff78a1 | 1576 | r = table_add_cell(table, NULL, TABLE_FIELD, field); |
57ae8cd8 YW |
1577 | if (r < 0) |
1578 | return table_log_add_error(r); | |
1579 | ||
1580 | r = table_add_cell_stringf(table, NULL, "%s%s%s%s", | |
1581 | HW_ADDR_TO_STR(addr), | |
1582 | description ? " (" : "", | |
1583 | strempty(description), | |
1584 | description ? ")" : ""); | |
1585 | if (r < 0) | |
1586 | return table_log_add_error(r); | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
10c71c36 YW |
1591 | static OutputFlags get_output_flags(void) { |
1592 | return | |
1593 | arg_all * OUTPUT_SHOW_ALL | | |
1594 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | | |
1595 | colors_enabled() * OUTPUT_COLOR; | |
1596 | } | |
1597 | ||
1598 | static int show_logs(const LinkInfo *info) { | |
1599 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
1600 | int r; | |
1601 | ||
1602 | if (arg_lines == 0) | |
1603 | return 0; | |
1604 | ||
f7f062bf | 1605 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE); |
10c71c36 YW |
1606 | if (r < 0) |
1607 | return log_error_errno(r, "Failed to open journal: %m"); | |
1608 | ||
1609 | r = add_match_this_boot(j, NULL); | |
1610 | if (r < 0) | |
1611 | return log_error_errno(r, "Failed to add boot matches: %m"); | |
1612 | ||
1613 | if (info) { | |
1614 | char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)]; | |
1615 | const char *m2, *m3; | |
1616 | ||
1617 | /* kernel */ | |
1618 | xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex); | |
1619 | /* networkd */ | |
1620 | m2 = strjoina("INTERFACE=", info->name); | |
1621 | /* udevd */ | |
1622 | m3 = strjoina("DEVICE=", info->name); | |
1623 | ||
1624 | (void)( | |
1625 | (r = sd_journal_add_match(j, m1, 0)) || | |
1626 | (r = sd_journal_add_disjunction(j)) || | |
1627 | (r = sd_journal_add_match(j, m2, 0)) || | |
1628 | (r = sd_journal_add_disjunction(j)) || | |
1629 | (r = sd_journal_add_match(j, m3, 0)) | |
1630 | ); | |
1631 | if (r < 0) | |
1632 | return log_error_errno(r, "Failed to add link matches: %m"); | |
1633 | } else { | |
1634 | r = add_matches_for_unit(j, "systemd-networkd.service"); | |
1635 | if (r < 0) | |
1636 | return log_error_errno(r, "Failed to add unit matches: %m"); | |
1637 | ||
1638 | r = add_matches_for_unit(j, "systemd-networkd-wait-online.service"); | |
1639 | if (r < 0) | |
1640 | return log_error_errno(r, "Failed to add unit matches: %m"); | |
1641 | } | |
1642 | ||
1643 | return show_journal( | |
1644 | stdout, | |
1645 | j, | |
1646 | OUTPUT_SHORT, | |
1647 | 0, | |
1648 | 0, | |
1649 | arg_lines, | |
1650 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
1651 | NULL); | |
1652 | } | |
1653 | ||
f8d7c0c5 YW |
1654 | static int table_add_string_line(Table *table, const char *key, const char *value) { |
1655 | int r; | |
1656 | ||
1657 | assert(table); | |
1658 | assert(key); | |
1659 | ||
1660 | if (isempty(value)) | |
1661 | return 0; | |
1662 | ||
1663 | r = table_add_many(table, | |
8fff78a1 | 1664 | TABLE_FIELD, key, |
f8d7c0c5 YW |
1665 | TABLE_STRING, value); |
1666 | if (r < 0) | |
1667 | return table_log_add_error(r); | |
1668 | ||
1669 | return 0; | |
1670 | } | |
1671 | ||
a2640646 DDM |
1672 | static int format_dropins(char **dropins) { |
1673 | STRV_FOREACH(d, dropins) { | |
1674 | _cleanup_free_ char *s = NULL; | |
1675 | int glyph = *(d + 1) == NULL ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH; | |
1676 | ||
1677 | s = strjoin(special_glyph(glyph), *d); | |
1678 | if (!s) | |
1679 | return log_oom(); | |
1680 | ||
1681 | free_and_replace(*d, s); | |
1682 | } | |
1683 | ||
1684 | return 0; | |
1685 | } | |
1686 | ||
69fb1176 | 1687 | static int link_status_one( |
d9ce1c24 | 1688 | sd_bus *bus, |
1c4baffc | 1689 | sd_netlink *rtnl, |
81fd1dd3 | 1690 | sd_hwdb *hwdb, |
14a5c07a | 1691 | Varlink *vl, |
b147503e LP |
1692 | const LinkInfo *info) { |
1693 | ||
a2640646 DDM |
1694 | _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL, |
1695 | **route_domains = NULL, **link_dropins = NULL, **network_dropins = NULL; | |
e469d2a2 | 1696 | _cleanup_free_ char *t = NULL, *network = NULL, *iaid = NULL, *duid = NULL, *captive_portal = NULL, |
100433e0 | 1697 | *setup_state = NULL, *operational_state = NULL, *online_state = NULL, *activation_policy = NULL; |
35cab5f9 | 1698 | const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL, |
4a481ec4 | 1699 | *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup, *on_color_online; |
b295beea | 1700 | _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL; |
35cab5f9 | 1701 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
98d5bef3 | 1702 | _cleanup_(table_unrefp) Table *table = NULL; |
b147503e | 1703 | int r; |
9085f64a | 1704 | |
43449ca3 | 1705 | assert(bus); |
9085f64a | 1706 | assert(rtnl); |
14a5c07a | 1707 | assert(vl); |
b147503e | 1708 | assert(info); |
9085f64a | 1709 | |
b147503e | 1710 | (void) sd_network_link_get_operational_state(info->ifindex, &operational_state); |
ceb366df | 1711 | operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational); |
d57c365b | 1712 | |
4a481ec4 AŠ |
1713 | (void) sd_network_link_get_online_state(info->ifindex, &online_state); |
1714 | online_state_to_color(online_state, &on_color_online, NULL); | |
1715 | ||
8f60c998 | 1716 | (void) sd_network_link_get_setup_state(info->ifindex, &setup_state); |
d57c365b | 1717 | setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); |
9085f64a | 1718 | |
b147503e LP |
1719 | (void) sd_network_link_get_dns(info->ifindex, &dns); |
1720 | (void) sd_network_link_get_search_domains(info->ifindex, &search_domains); | |
1721 | (void) sd_network_link_get_route_domains(info->ifindex, &route_domains); | |
1722 | (void) sd_network_link_get_ntp(info->ifindex, &ntp); | |
1f807af6 | 1723 | (void) sd_network_link_get_sip(info->ifindex, &sip); |
e469d2a2 | 1724 | (void) sd_network_link_get_captive_portal(info->ifindex, &captive_portal); |
f1d07d83 | 1725 | (void) sd_network_link_get_network_file(info->ifindex, &network); |
a2640646 | 1726 | (void) sd_network_link_get_network_file_dropins(info->ifindex, &network_dropins); |
f1d07d83 YW |
1727 | (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to); |
1728 | (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by); | |
1729 | (void) sd_network_link_get_activation_policy(info->ifindex, &activation_policy); | |
9085f64a | 1730 | |
172353b1 | 1731 | if (info->sd_device) { |
a2640646 DDM |
1732 | const char *joined; |
1733 | ||
172353b1 | 1734 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link); |
a2640646 DDM |
1735 | |
1736 | if (sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) { | |
1737 | r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE); | |
1738 | if (r < 0) | |
1739 | return r; | |
1740 | } | |
1741 | ||
172353b1 ZJS |
1742 | (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver); |
1743 | (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path); | |
3cc7a9fd LP |
1744 | (void) device_get_vendor_string(info->sd_device, &vendor); |
1745 | (void) device_get_model_string(info->sd_device, &model); | |
9085f64a LP |
1746 | } |
1747 | ||
4eef6fad | 1748 | r = net_get_type_string(info->sd_device, info->iftype, &t); |
1a3caa49 YW |
1749 | if (r == -ENOMEM) |
1750 | return log_oom(); | |
b1acce80 | 1751 | |
100433e0 ZJS |
1752 | char lease_file[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)]; |
1753 | xsprintf(lease_file, "/run/systemd/netif/leases/%i", info->ifindex); | |
35cab5f9 YW |
1754 | |
1755 | (void) dhcp_lease_load(&lease, lease_file); | |
1756 | ||
a2640646 DDM |
1757 | r = format_dropins(network_dropins); |
1758 | if (r < 0) | |
1759 | return r; | |
1760 | ||
1761 | if (strv_prepend(&network_dropins, network) < 0) | |
1762 | return log_oom(); | |
1763 | ||
1764 | r = format_dropins(link_dropins); | |
1765 | if (r < 0) | |
1766 | return r; | |
1767 | ||
1768 | if (strv_prepend(&link_dropins, link) < 0) | |
1769 | return log_oom(); | |
1770 | ||
8fff78a1 | 1771 | table = table_new_vertical(); |
98d5bef3 | 1772 | if (!table) |
bd17fa8c | 1773 | return log_oom(); |
98d5bef3 | 1774 | |
a42d9490 YW |
1775 | if (arg_full) |
1776 | table_set_width(table, 0); | |
1777 | ||
f8d7c0c5 | 1778 | /* unit files and basic states. */ |
8d0e0af2 | 1779 | r = table_add_many(table, |
8fff78a1 | 1780 | TABLE_FIELD, "Link File", |
a2640646 | 1781 | TABLE_STRV, link_dropins ?: STRV_MAKE("n/a"), |
8fff78a1 | 1782 | TABLE_FIELD, "Network File", |
a2640646 | 1783 | TABLE_STRV, network_dropins ?: STRV_MAKE("n/a"), |
8fff78a1 | 1784 | TABLE_FIELD, "State"); |
98d5bef3 | 1785 | if (r < 0) |
bd17fa8c | 1786 | return table_log_add_error(r); |
f8d7c0c5 | 1787 | |
98d5bef3 | 1788 | r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)", |
64e7ebde | 1789 | on_color_operational, strna(operational_state), off_color_operational, |
8f60c998 | 1790 | on_color_setup, setup_state ?: "unmanaged", off_color_setup); |
98d5bef3 | 1791 | if (r < 0) |
bd17fa8c | 1792 | return table_log_add_error(r); |
98d5bef3 | 1793 | |
4a481ec4 | 1794 | r = table_add_many(table, |
8fff78a1 | 1795 | TABLE_FIELD, "Online state", |
4a481ec4 AŠ |
1796 | TABLE_STRING, online_state ?: "unknown", |
1797 | TABLE_SET_COLOR, on_color_online); | |
1798 | if (r < 0) | |
1799 | return table_log_add_error(r); | |
1800 | ||
8fff78a1 | 1801 | r = table_add_string_line(table, "Type", t); |
f8d7c0c5 YW |
1802 | if (r < 0) |
1803 | return r; | |
1804 | ||
8fff78a1 | 1805 | r = table_add_string_line(table, "Kind", info->netdev_kind); |
f8d7c0c5 YW |
1806 | if (r < 0) |
1807 | return r; | |
1808 | ||
8fff78a1 | 1809 | r = table_add_string_line(table, "Path", path); |
f8d7c0c5 YW |
1810 | if (r < 0) |
1811 | return r; | |
1812 | ||
8fff78a1 | 1813 | r = table_add_string_line(table, "Driver", driver); |
f8d7c0c5 YW |
1814 | if (r < 0) |
1815 | return r; | |
1816 | ||
8fff78a1 | 1817 | r = table_add_string_line(table, "Vendor", vendor); |
f8d7c0c5 YW |
1818 | if (r < 0) |
1819 | return r; | |
1820 | ||
8fff78a1 | 1821 | r = table_add_string_line(table, "Model", model); |
f8d7c0c5 YW |
1822 | if (r < 0) |
1823 | return r; | |
1824 | ||
ae5b7792 | 1825 | strv_sort(info->alternative_names); |
8fff78a1 | 1826 | r = dump_list(table, "Alternative Names", info->alternative_names); |
536cdd07 YW |
1827 | if (r < 0) |
1828 | return r; | |
511070ee | 1829 | |
57ae8cd8 | 1830 | if (info->has_hw_address) { |
8fff78a1 | 1831 | r = dump_hw_address(table, hwdb, "Hardware Address", &info->hw_address); |
98d5bef3 | 1832 | if (r < 0) |
57ae8cd8 | 1833 | return r; |
db73295a | 1834 | } |
9085f64a | 1835 | |
ed9fa69f | 1836 | if (info->has_permanent_hw_address) { |
8fff78a1 | 1837 | r = dump_hw_address(table, hwdb, "Permanent Hardware Address", &info->permanent_hw_address); |
caa8538a | 1838 | if (r < 0) |
ed9fa69f | 1839 | return r; |
caa8538a YW |
1840 | } |
1841 | ||
c06ff86e YW |
1842 | if (info->mtu > 0) { |
1843 | char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)]; | |
1844 | ||
1845 | xsprintf(min_str, "%" PRIu32, info->min_mtu); | |
1846 | xsprintf(max_str, "%" PRIu32, info->max_mtu); | |
1847 | ||
8fff78a1 | 1848 | r = table_add_cell(table, NULL, TABLE_FIELD, "MTU"); |
98d5bef3 | 1849 | if (r < 0) |
bd17fa8c | 1850 | return table_log_add_error(r); |
8fff78a1 | 1851 | |
c06ff86e YW |
1852 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s", |
1853 | info->mtu, | |
1854 | info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "", | |
90e29fe1 | 1855 | info->min_mtu > 0 ? "min: " : "", |
c06ff86e YW |
1856 | info->min_mtu > 0 ? min_str : "", |
1857 | info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "", | |
90e29fe1 | 1858 | info->max_mtu > 0 ? "max: " : "", |
c06ff86e YW |
1859 | info->max_mtu > 0 ? max_str : "", |
1860 | info->min_mtu > 0 || info->max_mtu > 0 ? ")" : ""); | |
98d5bef3 | 1861 | if (r < 0) |
e810df37 SS |
1862 | return table_log_add_error(r); |
1863 | } | |
1864 | ||
8fff78a1 | 1865 | r = table_add_string_line(table, "QDisc", info->qdisc); |
f8d7c0c5 YW |
1866 | if (r < 0) |
1867 | return r; | |
6cfef1b3 SS |
1868 | |
1869 | if (info->master > 0) { | |
1870 | r = table_add_many(table, | |
8fff78a1 | 1871 | TABLE_FIELD, "Master", |
6cfef1b3 SS |
1872 | TABLE_IFINDEX, info->master); |
1873 | if (r < 0) | |
bd17fa8c | 1874 | return table_log_add_error(r); |
98d5bef3 | 1875 | } |
8eb9058d | 1876 | |
d69b62de SS |
1877 | if (info->has_ipv6_address_generation_mode) { |
1878 | static const struct { | |
1879 | const char *mode; | |
1880 | } mode_table[] = { | |
1881 | { "eui64" }, | |
1882 | { "none" }, | |
1883 | { "stable-privacy" }, | |
1884 | { "random" }, | |
1885 | }; | |
1886 | ||
1887 | r = table_add_many(table, | |
8fff78a1 | 1888 | TABLE_FIELD, "IPv6 Address Generation Mode", |
d69b62de SS |
1889 | TABLE_STRING, mode_table[info->addr_gen_mode]); |
1890 | if (r < 0) | |
1891 | return table_log_add_error(r); | |
1892 | } | |
1893 | ||
c82d1bf2 SS |
1894 | if (streq_ptr(info->netdev_kind, "bridge")) { |
1895 | r = table_add_many(table, | |
8fff78a1 | 1896 | TABLE_FIELD, "Forward Delay", |
c82d1bf2 | 1897 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay), |
8fff78a1 | 1898 | TABLE_FIELD, "Hello Time", |
c82d1bf2 | 1899 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time), |
8fff78a1 | 1900 | TABLE_FIELD, "Max Age", |
c82d1bf2 | 1901 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age), |
8fff78a1 | 1902 | TABLE_FIELD, "Ageing Time", |
c82d1bf2 | 1903 | TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time), |
8fff78a1 | 1904 | TABLE_FIELD, "Priority", |
c82d1bf2 | 1905 | TABLE_UINT16, info->priority, |
8fff78a1 | 1906 | TABLE_FIELD, "STP", |
c82d1bf2 | 1907 | TABLE_BOOLEAN, info->stp_state > 0, |
8fff78a1 | 1908 | TABLE_FIELD, "Multicast IGMP Version", |
12ef8fb6 | 1909 | TABLE_UINT8, info->mcast_igmp_version, |
8fff78a1 | 1910 | TABLE_FIELD, "Cost", |
12ef8fb6 | 1911 | TABLE_UINT32, info->cost); |
c82d1bf2 | 1912 | if (r < 0) |
bd17fa8c | 1913 | return table_log_add_error(r); |
b24281aa | 1914 | |
f8d7c0c5 | 1915 | if (info->port_state <= BR_STATE_BLOCKING) { |
12ef8fb6 | 1916 | r = table_add_many(table, |
8fff78a1 | 1917 | TABLE_FIELD, "Port State", |
a8389a33 | 1918 | TABLE_STRING, bridge_state_to_string(info->port_state)); |
f8d7c0c5 YW |
1919 | if (r < 0) |
1920 | return table_log_add_error(r); | |
1921 | } | |
1922 | ||
b1d6fe70 | 1923 | } else if (streq_ptr(info->netdev_kind, "bond")) { |
b1d6fe70 | 1924 | r = table_add_many(table, |
8fff78a1 | 1925 | TABLE_FIELD, "Mode", |
43bf2874 | 1926 | TABLE_STRING, bond_mode_to_string(info->mode), |
8fff78a1 | 1927 | TABLE_FIELD, "Miimon", |
05e022a9 | 1928 | TABLE_TIMESPAN_MSEC, info->miimon * USEC_PER_MSEC, |
8fff78a1 | 1929 | TABLE_FIELD, "Updelay", |
05e022a9 | 1930 | TABLE_TIMESPAN_MSEC, info->updelay * USEC_PER_MSEC, |
8fff78a1 | 1931 | TABLE_FIELD, "Downdelay", |
05e022a9 | 1932 | TABLE_TIMESPAN_MSEC, info->downdelay * USEC_PER_MSEC); |
b1d6fe70 SS |
1933 | if (r < 0) |
1934 | return table_log_add_error(r); | |
1935 | ||
b24281aa | 1936 | } else if (streq_ptr(info->netdev_kind, "vxlan")) { |
22ae6c7d SS |
1937 | char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))]; |
1938 | ||
b24281aa SS |
1939 | if (info->vxlan_info.vni > 0) { |
1940 | r = table_add_many(table, | |
8fff78a1 | 1941 | TABLE_FIELD, "VNI", |
b24281aa SS |
1942 | TABLE_UINT32, info->vxlan_info.vni); |
1943 | if (r < 0) | |
bd17fa8c | 1944 | return table_log_add_error(r); |
b24281aa SS |
1945 | } |
1946 | ||
1947 | if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) { | |
659f85a5 SS |
1948 | const char *p; |
1949 | ||
1950 | r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group); | |
1951 | if (r <= 0) | |
8fff78a1 | 1952 | p = "Remote"; |
659f85a5 | 1953 | else |
8fff78a1 | 1954 | p = "Group"; |
659f85a5 | 1955 | |
b24281aa | 1956 | r = table_add_many(table, |
8fff78a1 MY |
1957 | TABLE_FIELD, p, |
1958 | info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.group); | |
b24281aa | 1959 | if (r < 0) |
bd17fa8c | 1960 | return table_log_add_error(r); |
b24281aa SS |
1961 | } |
1962 | ||
1963 | if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) { | |
1964 | r = table_add_many(table, | |
8fff78a1 MY |
1965 | TABLE_FIELD, "Local", |
1966 | info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.local); | |
b24281aa | 1967 | if (r < 0) |
bd17fa8c | 1968 | return table_log_add_error(r); |
b24281aa SS |
1969 | } |
1970 | ||
1971 | if (info->vxlan_info.dest_port > 0) { | |
1972 | r = table_add_many(table, | |
8fff78a1 | 1973 | TABLE_FIELD, "Destination Port", |
b24281aa SS |
1974 | TABLE_UINT16, be16toh(info->vxlan_info.dest_port)); |
1975 | if (r < 0) | |
bd17fa8c | 1976 | return table_log_add_error(r); |
b24281aa SS |
1977 | } |
1978 | ||
1979 | if (info->vxlan_info.link > 0) { | |
1980 | r = table_add_many(table, | |
8fff78a1 | 1981 | TABLE_FIELD, "Underlying Device", |
b24281aa SS |
1982 | TABLE_IFINDEX, info->vxlan_info.link); |
1983 | if (r < 0) | |
bd17fa8c | 1984 | return table_log_add_error(r); |
b24281aa | 1985 | } |
22ae6c7d SS |
1986 | |
1987 | r = table_add_many(table, | |
8fff78a1 MY |
1988 | TABLE_FIELD, "Learning", |
1989 | TABLE_BOOLEAN, info->vxlan_info.learning, | |
1990 | TABLE_FIELD, "RSC", | |
1991 | TABLE_BOOLEAN, info->vxlan_info.rsc, | |
1992 | TABLE_FIELD, "L3MISS", | |
1993 | TABLE_BOOLEAN, info->vxlan_info.l3miss, | |
1994 | TABLE_FIELD, "L2MISS", | |
22ae6c7d SS |
1995 | TABLE_BOOLEAN, info->vxlan_info.l2miss); |
1996 | if (r < 0) | |
1997 | return table_log_add_error(r); | |
1998 | ||
1999 | if (info->vxlan_info.tos > 1) { | |
2000 | r = table_add_many(table, | |
8fff78a1 | 2001 | TABLE_FIELD, "TOS", |
22ae6c7d SS |
2002 | TABLE_UINT8, info->vxlan_info.tos); |
2003 | if (r < 0) | |
2004 | return table_log_add_error(r); | |
2005 | } | |
2006 | ||
2007 | if (info->vxlan_info.ttl > 0) | |
2008 | xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl); | |
2009 | else | |
2010 | strcpy(ttl, "auto"); | |
2011 | ||
2012 | r = table_add_many(table, | |
8fff78a1 | 2013 | TABLE_FIELD, "TTL", |
22ae6c7d SS |
2014 | TABLE_STRING, ttl); |
2015 | if (r < 0) | |
2016 | return table_log_add_error(r); | |
f8d7c0c5 | 2017 | |
2b2a1ae6 SS |
2018 | } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) { |
2019 | r = table_add_many(table, | |
8fff78a1 | 2020 | TABLE_FIELD, "VLan Id", |
2b2a1ae6 SS |
2021 | TABLE_UINT16, info->vlan_id); |
2022 | if (r < 0) | |
2023 | return table_log_add_error(r); | |
f8d7c0c5 | 2024 | |
5712d689 | 2025 | } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) { |
94876904 | 2026 | if (in_addr_is_set(AF_INET, &info->local)) { |
dca35224 | 2027 | r = table_add_many(table, |
8fff78a1 | 2028 | TABLE_FIELD, "Local", |
dca35224 SS |
2029 | TABLE_IN_ADDR, &info->local); |
2030 | if (r < 0) | |
2031 | return table_log_add_error(r); | |
2032 | } | |
2033 | ||
94876904 | 2034 | if (in_addr_is_set(AF_INET, &info->remote)) { |
dca35224 | 2035 | r = table_add_many(table, |
8fff78a1 | 2036 | TABLE_FIELD, "Remote", |
dca35224 SS |
2037 | TABLE_IN_ADDR, &info->remote); |
2038 | if (r < 0) | |
2039 | return table_log_add_error(r); | |
2040 | } | |
f8d7c0c5 | 2041 | |
5712d689 | 2042 | } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) { |
94876904 | 2043 | if (in_addr_is_set(AF_INET6, &info->local)) { |
ad760bc1 | 2044 | r = table_add_many(table, |
8fff78a1 | 2045 | TABLE_FIELD, "Local", |
ad760bc1 SS |
2046 | TABLE_IN6_ADDR, &info->local); |
2047 | if (r < 0) | |
2048 | return table_log_add_error(r); | |
2049 | } | |
2050 | ||
94876904 | 2051 | if (in_addr_is_set(AF_INET6, &info->remote)) { |
ad760bc1 | 2052 | r = table_add_many(table, |
8fff78a1 | 2053 | TABLE_FIELD, "Remote", |
ad760bc1 SS |
2054 | TABLE_IN6_ADDR, &info->remote); |
2055 | if (r < 0) | |
2056 | return table_log_add_error(r); | |
2057 | } | |
f8d7c0c5 | 2058 | |
4e1a1991 SS |
2059 | } else if (streq_ptr(info->netdev_kind, "geneve")) { |
2060 | r = table_add_many(table, | |
8fff78a1 | 2061 | TABLE_FIELD, "VNI", |
4e1a1991 SS |
2062 | TABLE_UINT32, info->vni); |
2063 | if (r < 0) | |
2064 | return table_log_add_error(r); | |
2065 | ||
94876904 | 2066 | if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) { |
4e1a1991 | 2067 | r = table_add_many(table, |
8fff78a1 | 2068 | TABLE_FIELD, "Remote", |
4e1a1991 SS |
2069 | TABLE_IN_ADDR, &info->remote); |
2070 | if (r < 0) | |
2071 | return table_log_add_error(r); | |
94876904 | 2072 | } else if (in_addr_is_set(AF_INET6, &info->remote)) { |
4e1a1991 | 2073 | r = table_add_many(table, |
8fff78a1 | 2074 | TABLE_FIELD, "Remote", |
4e1a1991 SS |
2075 | TABLE_IN6_ADDR, &info->remote); |
2076 | if (r < 0) | |
2077 | return table_log_add_error(r); | |
2078 | } | |
2079 | ||
2080 | if (info->ttl > 0) { | |
2081 | r = table_add_many(table, | |
8fff78a1 | 2082 | TABLE_FIELD, "TTL", |
4e1a1991 SS |
2083 | TABLE_UINT8, info->ttl); |
2084 | if (r < 0) | |
2085 | return table_log_add_error(r); | |
2086 | } | |
2087 | ||
2088 | if (info->tos > 0) { | |
2089 | r = table_add_many(table, | |
8fff78a1 | 2090 | TABLE_FIELD, "TOS", |
4e1a1991 SS |
2091 | TABLE_UINT8, info->tos); |
2092 | if (r < 0) | |
2093 | return table_log_add_error(r); | |
2094 | } | |
2095 | ||
2096 | r = table_add_many(table, | |
8fff78a1 MY |
2097 | TABLE_FIELD, "Port", |
2098 | TABLE_UINT16, info->tunnel_port, | |
2099 | TABLE_FIELD, "Inherit", | |
e7b38d7d SS |
2100 | TABLE_STRING, geneve_df_to_string(info->inherit)); |
2101 | if (r < 0) | |
2102 | return table_log_add_error(r); | |
2103 | ||
2104 | if (info->df > 0) { | |
2105 | r = table_add_many(table, | |
8fff78a1 | 2106 | TABLE_FIELD, "IPDoNotFragment", |
e7b38d7d SS |
2107 | TABLE_UINT8, info->df); |
2108 | if (r < 0) | |
2109 | return table_log_add_error(r); | |
2110 | } | |
2111 | ||
2112 | r = table_add_many(table, | |
8fff78a1 MY |
2113 | TABLE_FIELD, "UDPChecksum", |
2114 | TABLE_BOOLEAN, info->csum, | |
2115 | TABLE_FIELD, "UDP6ZeroChecksumTx", | |
2116 | TABLE_BOOLEAN, info->csum6_tx, | |
2117 | TABLE_FIELD, "UDP6ZeroChecksumRx", | |
e7b38d7d SS |
2118 | TABLE_BOOLEAN, info->csum6_rx); |
2119 | if (r < 0) | |
2120 | return table_log_add_error(r); | |
2121 | ||
2122 | if (info->label > 0) { | |
2123 | r = table_add_many(table, | |
8fff78a1 | 2124 | TABLE_FIELD, "FlowLabel", |
e7b38d7d SS |
2125 | TABLE_UINT32, info->label); |
2126 | if (r < 0) | |
2127 | return table_log_add_error(r); | |
2128 | } | |
f8d7c0c5 | 2129 | |
cf217a09 SS |
2130 | } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) { |
2131 | r = table_add_many(table, | |
8fff78a1 | 2132 | TABLE_FIELD, "Mode", |
cf217a09 SS |
2133 | TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode)); |
2134 | if (r < 0) | |
2135 | return table_log_add_error(r); | |
f8d7c0c5 | 2136 | |
851ef1ed | 2137 | } else if (streq_ptr(info->netdev_kind, "ipvlan")) { |
8fff78a1 | 2138 | const char *p; |
851ef1ed SS |
2139 | |
2140 | if (info->ipvlan_flags & IPVLAN_F_PRIVATE) | |
8fff78a1 | 2141 | p = "private"; |
851ef1ed | 2142 | else if (info->ipvlan_flags & IPVLAN_F_VEPA) |
8fff78a1 | 2143 | p = "vepa"; |
851ef1ed | 2144 | else |
8fff78a1 | 2145 | p = "bridge"; |
851ef1ed | 2146 | |
8fff78a1 MY |
2147 | r = table_add_cell(table, NULL, TABLE_FIELD, "Mode"); |
2148 | if (r < 0) | |
2149 | return table_log_add_error(r); | |
851ef1ed | 2150 | |
8fff78a1 MY |
2151 | r = table_add_cell_stringf(table, NULL, "%s (%s)", |
2152 | ipvlan_mode_to_string(info->ipvlan_mode), p); | |
851ef1ed SS |
2153 | if (r < 0) |
2154 | return table_log_add_error(r); | |
c82d1bf2 SS |
2155 | } |
2156 | ||
8d07de25 ZJS |
2157 | if (info->has_wlan_link_info) { |
2158 | _cleanup_free_ char *esc = NULL; | |
8d07de25 | 2159 | |
8fff78a1 | 2160 | r = table_add_cell(table, NULL, TABLE_FIELD, "Wi-Fi access point"); |
8d07de25 | 2161 | if (r < 0) |
bd17fa8c | 2162 | return table_log_add_error(r); |
8d07de25 ZJS |
2163 | |
2164 | if (info->ssid) | |
2165 | esc = cescape(info->ssid); | |
2166 | ||
2167 | r = table_add_cell_stringf(table, NULL, "%s (%s)", | |
2168 | strnull(esc), | |
4b574fd8 | 2169 | ETHER_ADDR_TO_STR(&info->bssid)); |
8d07de25 | 2170 | if (r < 0) |
bd17fa8c | 2171 | return table_log_add_error(r); |
8d07de25 ZJS |
2172 | } |
2173 | ||
335dd8ba | 2174 | if (info->has_bitrates) { |
8fff78a1 | 2175 | r = table_add_cell(table, NULL, TABLE_FIELD, "Bit Rate (Tx/Rx)"); |
335dd8ba | 2176 | if (r < 0) |
bd17fa8c | 2177 | return table_log_add_error(r); |
8fff78a1 | 2178 | |
42a63431 | 2179 | r = table_add_cell_stringf(table, NULL, "%sbps/%sbps", |
46e23f9a ZJS |
2180 | FORMAT_BYTES_FULL(info->tx_bitrate, 0), |
2181 | FORMAT_BYTES_FULL(info->rx_bitrate, 0)); | |
335dd8ba | 2182 | if (r < 0) |
bd17fa8c | 2183 | return table_log_add_error(r); |
335dd8ba YW |
2184 | } |
2185 | ||
98d5bef3 | 2186 | if (info->has_tx_queues || info->has_rx_queues) { |
8fff78a1 | 2187 | r = table_add_cell(table, NULL, TABLE_FIELD, "Number of Queues (Tx/Rx)"); |
98d5bef3 | 2188 | if (r < 0) |
bd17fa8c | 2189 | return table_log_add_error(r); |
8fff78a1 | 2190 | |
98d5bef3 YW |
2191 | r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues); |
2192 | if (r < 0) | |
bd17fa8c | 2193 | return table_log_add_error(r); |
98d5bef3 | 2194 | } |
0d4ad91d | 2195 | |
c967d2c7 | 2196 | if (info->has_ethtool_link_info) { |
c967d2c7 | 2197 | if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) { |
8d0e0af2 | 2198 | r = table_add_many(table, |
8fff78a1 | 2199 | TABLE_FIELD, "Auto negotiation", |
8d0e0af2 | 2200 | TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE); |
c967d2c7 | 2201 | if (r < 0) |
bd17fa8c | 2202 | return table_log_add_error(r); |
c967d2c7 YW |
2203 | } |
2204 | ||
f8d7c0c5 | 2205 | if (info->speed > 0 && info->speed != UINT64_MAX) { |
8d0e0af2 | 2206 | r = table_add_many(table, |
8fff78a1 | 2207 | TABLE_FIELD, "Speed", |
50299121 | 2208 | TABLE_BPS, info->speed); |
c967d2c7 | 2209 | if (r < 0) |
bd17fa8c | 2210 | return table_log_add_error(r); |
c967d2c7 YW |
2211 | } |
2212 | ||
8fff78a1 | 2213 | r = table_add_string_line(table, "Duplex", duplex_to_string(info->duplex)); |
f8d7c0c5 YW |
2214 | if (r < 0) |
2215 | return r; | |
c967d2c7 | 2216 | |
8fff78a1 | 2217 | r = table_add_string_line(table, "Port", port_to_string(info->port)); |
f8d7c0c5 YW |
2218 | if (r < 0) |
2219 | return r; | |
c967d2c7 YW |
2220 | } |
2221 | ||
d41fa6ee | 2222 | r = dump_addresses(rtnl, lease, table, info->ifindex); |
98d5bef3 YW |
2223 | if (r < 0) |
2224 | return r; | |
8fff78a1 | 2225 | |
98d5bef3 YW |
2226 | r = dump_gateways(rtnl, hwdb, table, info->ifindex); |
2227 | if (r < 0) | |
2228 | return r; | |
8fff78a1 MY |
2229 | |
2230 | r = dump_list(table, "DNS", dns); | |
98d5bef3 YW |
2231 | if (r < 0) |
2232 | return r; | |
8fff78a1 MY |
2233 | |
2234 | r = dump_list(table, "Search Domains", search_domains); | |
98d5bef3 YW |
2235 | if (r < 0) |
2236 | return r; | |
8fff78a1 MY |
2237 | |
2238 | r = dump_list(table, "Route Domains", route_domains); | |
98d5bef3 YW |
2239 | if (r < 0) |
2240 | return r; | |
8fff78a1 MY |
2241 | |
2242 | r = dump_list(table, "NTP", ntp); | |
1f807af6 SS |
2243 | if (r < 0) |
2244 | return r; | |
8fff78a1 MY |
2245 | |
2246 | r = dump_list(table, "SIP", sip); | |
98d5bef3 YW |
2247 | if (r < 0) |
2248 | return r; | |
8fff78a1 MY |
2249 | |
2250 | r = dump_ifindexes(table, "Carrier Bound To", carrier_bound_to); | |
98d5bef3 YW |
2251 | if (r < 0) |
2252 | return r; | |
8fff78a1 MY |
2253 | |
2254 | r = dump_ifindexes(table, "Carrier Bound By", carrier_bound_by); | |
98d5bef3 YW |
2255 | if (r < 0) |
2256 | return r; | |
9085f64a | 2257 | |
8fff78a1 | 2258 | r = table_add_string_line(table, "Activation Policy", activation_policy); |
f1d07d83 YW |
2259 | if (r < 0) |
2260 | return r; | |
a853652a | 2261 | |
003015af DS |
2262 | r = sd_network_link_get_required_for_online(info->ifindex); |
2263 | if (r >= 0) { | |
2264 | r = table_add_many(table, | |
8fff78a1 | 2265 | TABLE_FIELD, "Required For Online", |
003015af DS |
2266 | TABLE_BOOLEAN, r); |
2267 | if (r < 0) | |
2268 | return table_log_add_error(r); | |
2269 | } | |
2270 | ||
52ee8ecf YW |
2271 | if (captive_portal) { |
2272 | r = table_add_many(table, | |
8fff78a1 | 2273 | TABLE_FIELD, "Captive Portal", |
52ee8ecf YW |
2274 | TABLE_STRING, captive_portal, |
2275 | TABLE_SET_URL, captive_portal); | |
2276 | if (r < 0) | |
2277 | return table_log_add_error(r); | |
2278 | } | |
e469d2a2 | 2279 | |
35cab5f9 | 2280 | if (lease) { |
d59a2b3b | 2281 | const sd_dhcp_client_id *client_id; |
35cab5f9 YW |
2282 | const char *tz; |
2283 | ||
2284 | r = sd_dhcp_lease_get_timezone(lease, &tz); | |
2285 | if (r >= 0) { | |
2286 | r = table_add_many(table, | |
8fff78a1 | 2287 | TABLE_FIELD, "Time Zone", |
35cab5f9 YW |
2288 | TABLE_STRING, tz); |
2289 | if (r < 0) | |
2290 | return table_log_add_error(r); | |
2291 | } | |
8eb9058d | 2292 | |
d59a2b3b | 2293 | r = sd_dhcp_lease_get_client_id(lease, &client_id); |
862e7108 YW |
2294 | if (r >= 0) { |
2295 | _cleanup_free_ char *id = NULL; | |
2296 | ||
d59a2b3b | 2297 | r = sd_dhcp_client_id_to_string(client_id, &id); |
862e7108 YW |
2298 | if (r >= 0) { |
2299 | r = table_add_many(table, | |
8fff78a1 | 2300 | TABLE_FIELD, "DHCP4 Client ID", |
862e7108 YW |
2301 | TABLE_STRING, id); |
2302 | if (r < 0) | |
2303 | return table_log_add_error(r); | |
2304 | } | |
2305 | } | |
2153bbc8 SS |
2306 | } |
2307 | ||
331ee15f SS |
2308 | r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid); |
2309 | if (r >= 0) { | |
2310 | r = table_add_many(table, | |
8fff78a1 | 2311 | TABLE_FIELD, "DHCP6 Client IAID", |
331ee15f SS |
2312 | TABLE_STRING, iaid); |
2313 | if (r < 0) | |
2314 | return table_log_add_error(r); | |
2315 | } | |
2316 | ||
63911885 SS |
2317 | r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid); |
2318 | if (r >= 0) { | |
2319 | r = table_add_many(table, | |
8fff78a1 | 2320 | TABLE_FIELD, "DHCP6 Client DUID", |
63911885 SS |
2321 | TABLE_STRING, duid); |
2322 | if (r < 0) | |
2323 | return table_log_add_error(r); | |
2324 | } | |
2325 | ||
14a5c07a | 2326 | r = dump_lldp_neighbors(vl, table, info->ifindex); |
98d5bef3 YW |
2327 | if (r < 0) |
2328 | return r; | |
837f57da | 2329 | |
8fff78a1 | 2330 | r = dump_dhcp_leases(table, "Offered DHCP leases", bus, info); |
d9ce1c24 MAL |
2331 | if (r < 0) |
2332 | return r; | |
2333 | ||
a459b24f YW |
2334 | r = dump_statistics(table, info); |
2335 | if (r < 0) | |
2336 | return r; | |
2337 | ||
8fff78a1 MY |
2338 | /* First line: circle, ifindex, ifname. */ |
2339 | printf("%s%s%s %d: %s\n", | |
2340 | on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, | |
2341 | info->ifindex, info->name); | |
2342 | ||
10c71c36 YW |
2343 | r = table_print(table, NULL); |
2344 | if (r < 0) | |
4b6607d9 | 2345 | return table_log_print_error(r); |
10c71c36 YW |
2346 | |
2347 | return show_logs(info); | |
9085f64a LP |
2348 | } |
2349 | ||
0070333f | 2350 | static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) { |
8fff78a1 MY |
2351 | _cleanup_free_ char *operational_state = NULL, *online_state = NULL, *netifs_joined = NULL; |
2352 | _cleanup_strv_free_ char **netifs = NULL, **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; | |
2353 | const char *on_color_operational, *off_color_operational, *on_color_online; | |
98d5bef3 | 2354 | _cleanup_(table_unrefp) Table *table = NULL; |
98d5bef3 | 2355 | int r; |
0070333f LP |
2356 | |
2357 | assert(rtnl); | |
2358 | ||
4abd866d | 2359 | (void) sd_network_get_operational_state(&operational_state); |
8fff78a1 | 2360 | operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational); |
0070333f | 2361 | |
4a481ec4 AŠ |
2362 | (void) sd_network_get_online_state(&online_state); |
2363 | online_state_to_color(online_state, &on_color_online, NULL); | |
2364 | ||
8fff78a1 | 2365 | table = table_new_vertical(); |
98d5bef3 | 2366 | if (!table) |
bd17fa8c | 2367 | return log_oom(); |
98d5bef3 | 2368 | |
a42d9490 YW |
2369 | if (arg_full) |
2370 | table_set_width(table, 0); | |
2371 | ||
8fff78a1 MY |
2372 | r = get_files_in_directory("/run/systemd/netif/links/", &netifs); |
2373 | if (r < 0 && r != -ENOENT) | |
2374 | return log_error_errno(r, "Failed to list network interfaces: %m"); | |
2375 | else if (r > 0) { | |
2376 | netifs_joined = strv_join(netifs, ", "); | |
2377 | if (!netifs_joined) | |
2378 | return log_oom(); | |
2379 | } | |
98d5bef3 | 2380 | |
8d0e0af2 | 2381 | r = table_add_many(table, |
8fff78a1 | 2382 | TABLE_FIELD, "State", |
8d0e0af2 | 2383 | TABLE_STRING, strna(operational_state), |
4a481ec4 | 2384 | TABLE_SET_COLOR, on_color_operational, |
8fff78a1 | 2385 | TABLE_FIELD, "Online state", |
4a481ec4 AŠ |
2386 | TABLE_STRING, online_state ?: "unknown", |
2387 | TABLE_SET_COLOR, on_color_online); | |
bd17fa8c YW |
2388 | if (r < 0) |
2389 | return table_log_add_error(r); | |
0070333f | 2390 | |
d41fa6ee | 2391 | r = dump_addresses(rtnl, NULL, table, 0); |
98d5bef3 YW |
2392 | if (r < 0) |
2393 | return r; | |
8fff78a1 | 2394 | |
98d5bef3 YW |
2395 | r = dump_gateways(rtnl, hwdb, table, 0); |
2396 | if (r < 0) | |
2397 | return r; | |
0070333f | 2398 | |
4abd866d | 2399 | (void) sd_network_get_dns(&dns); |
8fff78a1 | 2400 | r = dump_list(table, "DNS", dns); |
98d5bef3 YW |
2401 | if (r < 0) |
2402 | return r; | |
0070333f | 2403 | |
4abd866d | 2404 | (void) sd_network_get_search_domains(&search_domains); |
8fff78a1 | 2405 | r = dump_list(table, "Search Domains", search_domains); |
98d5bef3 YW |
2406 | if (r < 0) |
2407 | return r; | |
0070333f | 2408 | |
4abd866d | 2409 | (void) sd_network_get_route_domains(&route_domains); |
8fff78a1 | 2410 | r = dump_list(table, "Route Domains", route_domains); |
98d5bef3 YW |
2411 | if (r < 0) |
2412 | return r; | |
0070333f | 2413 | |
4abd866d | 2414 | (void) sd_network_get_ntp(&ntp); |
8fff78a1 | 2415 | r = dump_list(table, "NTP", ntp); |
98d5bef3 YW |
2416 | if (r < 0) |
2417 | return r; | |
0070333f | 2418 | |
8fff78a1 MY |
2419 | printf("%s%s%s Interfaces: %s\n", |
2420 | on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, | |
2421 | strna(netifs_joined)); | |
2422 | ||
10c71c36 YW |
2423 | r = table_print(table, NULL); |
2424 | if (r < 0) | |
4b6607d9 | 2425 | return table_log_print_error(r); |
10c71c36 YW |
2426 | |
2427 | return show_logs(NULL); | |
0070333f LP |
2428 | } |
2429 | ||
266b5389 | 2430 | static int link_status(int argc, char *argv[], void *userdata) { |
445f0d8b | 2431 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
4afd3348 | 2432 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b147503e | 2433 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
7742d03e | 2434 | _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; |
172353b1 | 2435 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
b48e277f | 2436 | int r, c; |
ee8c4568 | 2437 | |
445f0d8b MY |
2438 | r = acquire_bus(&bus); |
2439 | if (r < 0) | |
2440 | return r; | |
2441 | ||
bfc2b05e YW |
2442 | if (arg_json_format_flags != JSON_FORMAT_OFF) { |
2443 | if (arg_all || argc <= 1) | |
d821e40c | 2444 | return dump_manager_description(bus); |
bfc2b05e | 2445 | else |
d821e40c | 2446 | return dump_link_description(bus, strv_skip(argv, 1)); |
bfc2b05e YW |
2447 | } |
2448 | ||
384c2c32 | 2449 | pager_open(arg_pager_flags); |
0070333f | 2450 | |
1c4baffc | 2451 | r = sd_netlink_open(&rtnl); |
f647962d MS |
2452 | if (r < 0) |
2453 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
f7d68aa8 | 2454 | |
81fd1dd3 TG |
2455 | r = sd_hwdb_new(&hwdb); |
2456 | if (r < 0) | |
2457 | log_debug_errno(r, "Failed to open hardware database: %m"); | |
69fb1176 | 2458 | |
14a5c07a YW |
2459 | r = varlink_connect_networkd(&vl); |
2460 | if (r < 0) | |
2461 | return r; | |
2462 | ||
b147503e | 2463 | if (arg_all) |
335dd8ba | 2464 | c = acquire_link_info(bus, rtnl, NULL, &links); |
b147503e | 2465 | else if (argc <= 1) |
0070333f | 2466 | return system_status(rtnl, hwdb); |
b147503e | 2467 | else |
335dd8ba | 2468 | c = acquire_link_info(bus, rtnl, argv + 1, &links); |
b147503e LP |
2469 | if (c < 0) |
2470 | return c; | |
ee8c4568 | 2471 | |
8fff78a1 MY |
2472 | r = 0; |
2473 | ||
2474 | bool first = true; | |
2475 | FOREACH_ARRAY(i, links, c) { | |
2476 | if (!first) | |
2477 | putchar('\n'); | |
ee8c4568 | 2478 | |
14a5c07a | 2479 | RET_GATHER(r, link_status_one(bus, rtnl, hwdb, vl, i)); |
8fff78a1 MY |
2480 | |
2481 | first = false; | |
ee8c4568 LP |
2482 | } |
2483 | ||
8fff78a1 | 2484 | return r; |
ee8c4568 LP |
2485 | } |
2486 | ||
14a5c07a | 2487 | static char *lldp_capabilities_to_string(uint64_t x) { |
34437b4f LP |
2488 | static const char characters[] = { |
2489 | 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm', | |
2490 | }; | |
2491 | char *ret; | |
2492 | unsigned i; | |
49699bac | 2493 | |
34437b4f LP |
2494 | ret = new(char, ELEMENTSOF(characters) + 1); |
2495 | if (!ret) | |
49699bac SS |
2496 | return NULL; |
2497 | ||
34437b4f LP |
2498 | for (i = 0; i < ELEMENTSOF(characters); i++) |
2499 | ret[i] = (x & (1U << i)) ? characters[i] : '.'; | |
49699bac | 2500 | |
34437b4f LP |
2501 | ret[i] = 0; |
2502 | return ret; | |
49699bac SS |
2503 | } |
2504 | ||
4c3160f1 | 2505 | static void lldp_capabilities_legend(uint16_t x) { |
b48e277f | 2506 | unsigned cols = columns(); |
404d53a9 | 2507 | static const char* const table[] = { |
4c3160f1 ZJS |
2508 | "o - Other", |
2509 | "p - Repeater", | |
2510 | "b - Bridge", | |
2511 | "w - WLAN Access Point", | |
2512 | "r - Router", | |
2513 | "t - Telephone", | |
2514 | "d - DOCSIS cable device", | |
2515 | "a - Station", | |
2516 | "c - Customer VLAN", | |
2517 | "s - Service VLAN", | |
2518 | "m - Two-port MAC Relay (TPMR)", | |
2519 | }; | |
2520 | ||
2521 | if (x == 0) | |
2522 | return; | |
2523 | ||
2524 | printf("\nCapability Flags:\n"); | |
b48e277f | 2525 | for (unsigned w = 0, i = 0; i < ELEMENTSOF(table); i++) |
4c3160f1 ZJS |
2526 | if (x & (1U << i) || arg_all) { |
2527 | bool newline; | |
2528 | ||
2529 | newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols; | |
2530 | if (newline) | |
2531 | w = 0; | |
2532 | w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]); | |
2533 | } | |
2534 | puts(""); | |
2535 | } | |
2536 | ||
14a5c07a YW |
2537 | static bool interface_match_pattern(const InterfaceInfo *info, char * const *patterns) { |
2538 | assert(info); | |
2539 | ||
2540 | if (strv_isempty(patterns)) | |
2541 | return true; | |
2542 | ||
2543 | if (strv_fnmatch(patterns, info->ifname)) | |
2544 | return true; | |
2545 | ||
2546 | char str[DECIMAL_STR_MAX(int)]; | |
2547 | xsprintf(str, "%i", info->ifindex); | |
2548 | if (strv_fnmatch(patterns, str)) | |
2549 | return true; | |
2550 | ||
2551 | STRV_FOREACH(a, info->altnames) | |
2552 | if (strv_fnmatch(patterns, *a)) | |
2553 | return true; | |
2554 | ||
2555 | return false; | |
2556 | } | |
2557 | ||
2558 | static int dump_lldp_neighbors_json(JsonVariant *reply, char * const *patterns) { | |
2559 | _cleanup_(json_variant_unrefp) JsonVariant *array = NULL, *v = NULL; | |
2560 | int r; | |
2561 | ||
2562 | assert(reply); | |
2563 | ||
2564 | if (strv_isempty(patterns)) | |
2565 | return json_variant_dump(reply, arg_json_format_flags, NULL, NULL); | |
2566 | ||
2567 | /* Filter and dump the result. */ | |
2568 | ||
2569 | JsonVariant *i; | |
2570 | JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { | |
2571 | _cleanup_(interface_info_done) InterfaceInfo info = {}; | |
2572 | ||
2573 | r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); | |
2574 | if (r < 0) | |
2575 | return r; | |
2576 | ||
2577 | if (!interface_match_pattern(&info, patterns)) | |
2578 | continue; | |
2579 | ||
2580 | r = json_variant_append_array(&array, i); | |
2581 | if (r < 0) | |
2582 | return log_error_errno(r, "Failed to append json variant to array: %m"); | |
2583 | } | |
2584 | ||
2585 | r = json_build(&v, | |
2586 | JSON_BUILD_OBJECT( | |
2587 | JSON_BUILD_PAIR_CONDITION(json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_EMPTY_ARRAY), | |
2588 | JSON_BUILD_PAIR_CONDITION(!json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_VARIANT(array)))); | |
2589 | if (r < 0) | |
2590 | return log_error_errno(r, "Failed to build json varinat: %m"); | |
2591 | ||
2592 | return json_variant_dump(v, arg_json_format_flags, NULL, NULL); | |
2593 | } | |
2594 | ||
49699bac | 2595 | static int link_lldp_status(int argc, char *argv[], void *userdata) { |
7742d03e | 2596 | _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; |
658e9106 | 2597 | _cleanup_(table_unrefp) Table *table = NULL; |
14a5c07a YW |
2598 | JsonVariant *reply; |
2599 | uint64_t all = 0; | |
658e9106 | 2600 | TableCell *cell; |
14a5c07a YW |
2601 | size_t m = 0; |
2602 | int r; | |
b147503e | 2603 | |
14a5c07a | 2604 | r = varlink_connect_networkd(&vl); |
b147503e | 2605 | if (r < 0) |
14a5c07a | 2606 | return r; |
49699bac | 2607 | |
14a5c07a YW |
2608 | r = varlink_call_and_log(vl, "io.systemd.Network.GetLLDPNeighbors", NULL, &reply); |
2609 | if (r < 0) | |
2610 | return r; | |
2611 | ||
2612 | if (arg_json_format_flags != JSON_FORMAT_OFF) | |
2613 | return dump_lldp_neighbors_json(reply, strv_skip(argv, 1)); | |
7d367b45 | 2614 | |
384c2c32 | 2615 | pager_open(arg_pager_flags); |
49699bac | 2616 | |
14a5c07a YW |
2617 | table = table_new("index", |
2618 | "link", | |
27cd31c0 | 2619 | "system-name", |
14a5c07a YW |
2620 | "system-description", |
2621 | "chassis-id", | |
27cd31c0 | 2622 | "port-id", |
14a5c07a YW |
2623 | "port-description", |
2624 | "caps"); | |
658e9106 | 2625 | if (!table) |
bd17fa8c | 2626 | return log_oom(); |
658e9106 | 2627 | |
a42d9490 YW |
2628 | if (arg_full) |
2629 | table_set_width(table, 0); | |
2630 | ||
658e9106 | 2631 | table_set_header(table, arg_legend); |
14a5c07a YW |
2632 | table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
2633 | table_set_sort(table, (size_t) 0, (size_t) 2); | |
2634 | table_hide_column_from_display(table, (size_t) 0); | |
658e9106 | 2635 | |
14a5c07a YW |
2636 | /* Make the capabilities not truncated */ |
2637 | assert_se(cell = table_get_cell(table, 0, 7)); | |
658e9106 YW |
2638 | table_set_minimum_width(table, cell, 11); |
2639 | ||
14a5c07a YW |
2640 | JsonVariant *i; |
2641 | JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { | |
2642 | _cleanup_(interface_info_done) InterfaceInfo info = {}; | |
49699bac | 2643 | |
14a5c07a YW |
2644 | r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); |
2645 | if (r < 0) | |
2646 | return r; | |
2647 | ||
2648 | if (!interface_match_pattern(&info, strv_skip(argv, 1))) | |
34437b4f | 2649 | continue; |
49699bac | 2650 | |
14a5c07a YW |
2651 | JsonVariant *neighbor; |
2652 | JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { | |
2653 | LLDPNeighborInfo neighbor_info = {}; | |
49699bac | 2654 | |
14a5c07a YW |
2655 | r = json_dispatch(neighbor, lldp_neighbor_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &neighbor_info); |
2656 | if (r < 0) | |
2657 | return r; | |
34437b4f | 2658 | |
14a5c07a | 2659 | all |= neighbor_info.capabilities; |
34437b4f | 2660 | |
14a5c07a | 2661 | _cleanup_free_ char *cap_str = lldp_capabilities_to_string(neighbor_info.capabilities); |
34437b4f | 2662 | |
658e9106 | 2663 | r = table_add_many(table, |
14a5c07a YW |
2664 | TABLE_INT, info.ifindex, |
2665 | TABLE_STRING, info.ifname, | |
2666 | TABLE_STRING, neighbor_info.system_name, | |
2667 | TABLE_STRING, neighbor_info.system_description, | |
2668 | TABLE_STRING, neighbor_info.chassis_id, | |
2669 | TABLE_STRING, neighbor_info.port_id, | |
2670 | TABLE_STRING, neighbor_info.port_description, | |
2671 | TABLE_STRING, cap_str); | |
658e9106 | 2672 | if (r < 0) |
bd17fa8c | 2673 | return table_log_add_error(r); |
b147503e LP |
2674 | |
2675 | m++; | |
49699bac SS |
2676 | } |
2677 | } | |
2678 | ||
658e9106 YW |
2679 | r = table_print(table, NULL); |
2680 | if (r < 0) | |
4b6607d9 | 2681 | return table_log_print_error(r); |
658e9106 | 2682 | |
4c3160f1 ZJS |
2683 | if (arg_legend) { |
2684 | lldp_capabilities_legend(all); | |
14a5c07a | 2685 | printf("\n%zu neighbor(s) listed.\n", m); |
4c3160f1 | 2686 | } |
49699bac SS |
2687 | |
2688 | return 0; | |
2689 | } | |
2690 | ||
9cd8c766 SS |
2691 | static int link_delete_send_message(sd_netlink *rtnl, int index) { |
2692 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2693 | int r; | |
2694 | ||
2695 | assert(rtnl); | |
43449ca3 | 2696 | assert(index >= 0); |
9cd8c766 SS |
2697 | |
2698 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index); | |
2699 | if (r < 0) | |
2700 | return rtnl_log_create_error(r); | |
2701 | ||
2702 | r = sd_netlink_call(rtnl, req, 0, NULL); | |
2703 | if (r < 0) | |
2704 | return r; | |
2705 | ||
2706 | return 0; | |
2707 | } | |
2708 | ||
c30ffcee SS |
2709 | static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) { |
2710 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2711 | int r; | |
2712 | ||
2713 | assert(rtnl); | |
43449ca3 | 2714 | assert(index >= 0); |
c30ffcee SS |
2715 | |
2716 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index); | |
2717 | if (r < 0) | |
2718 | return rtnl_log_create_error(r); | |
2719 | ||
2720 | if (streq(command, "up")) | |
2721 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
2722 | else | |
2723 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
2724 | if (r < 0) | |
2725 | return log_error_errno(r, "Could not set link flags: %m"); | |
2726 | ||
2727 | r = sd_netlink_call(rtnl, req, 0, NULL); | |
2728 | if (r < 0) | |
2729 | return r; | |
2730 | ||
2731 | return 0; | |
2732 | } | |
2733 | ||
2734 | static int link_up_down(int argc, char *argv[], void *userdata) { | |
2735 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2736 | _cleanup_set_free_ Set *indexes = NULL; | |
b48e277f | 2737 | int index, r; |
c30ffcee SS |
2738 | void *p; |
2739 | ||
2740 | r = sd_netlink_open(&rtnl); | |
2741 | if (r < 0) | |
2742 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2743 | ||
2744 | indexes = set_new(NULL); | |
2745 | if (!indexes) | |
2746 | return log_oom(); | |
2747 | ||
b48e277f | 2748 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2749 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
c30ffcee SS |
2750 | if (index < 0) |
2751 | return index; | |
2752 | ||
2753 | r = set_put(indexes, INT_TO_PTR(index)); | |
2754 | if (r < 0) | |
2755 | return log_oom(); | |
2756 | } | |
2757 | ||
90e74a66 | 2758 | SET_FOREACH(p, indexes) { |
c30ffcee SS |
2759 | index = PTR_TO_INT(p); |
2760 | r = link_up_down_send_message(rtnl, argv[0], index); | |
01afd0f7 | 2761 | if (r < 0) |
6f50c94d | 2762 | return log_error_errno(r, "Failed to bring %s interface %s: %m", |
01afd0f7 | 2763 | argv[0], FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX)); |
c30ffcee SS |
2764 | } |
2765 | ||
2766 | return r; | |
2767 | } | |
2768 | ||
9cd8c766 SS |
2769 | static int link_delete(int argc, char *argv[], void *userdata) { |
2770 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
2771 | _cleanup_set_free_ Set *indexes = NULL; | |
b48e277f | 2772 | int index, r; |
38b9af61 | 2773 | void *p; |
9cd8c766 SS |
2774 | |
2775 | r = sd_netlink_open(&rtnl); | |
2776 | if (r < 0) | |
2777 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2778 | ||
2779 | indexes = set_new(NULL); | |
2780 | if (!indexes) | |
2781 | return log_oom(); | |
2782 | ||
b48e277f | 2783 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2784 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2785 | if (index < 0) |
2786 | return index; | |
9cd8c766 SS |
2787 | |
2788 | r = set_put(indexes, INT_TO_PTR(index)); | |
2789 | if (r < 0) | |
2790 | return log_oom(); | |
2791 | } | |
2792 | ||
90e74a66 | 2793 | SET_FOREACH(p, indexes) { |
d56d6cb8 YW |
2794 | index = PTR_TO_INT(p); |
2795 | r = link_delete_send_message(rtnl, index); | |
01afd0f7 | 2796 | if (r < 0) |
e4857ee2 | 2797 | return log_error_errno(r, "Failed to delete interface %s: %m", |
01afd0f7 | 2798 | FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX)); |
9cd8c766 SS |
2799 | } |
2800 | ||
2801 | return r; | |
2802 | } | |
2803 | ||
308e7dfd YW |
2804 | static int link_renew_one(sd_bus *bus, int index, const char *name) { |
2805 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2806 | int r; | |
2807 | ||
43449ca3 MY |
2808 | assert(bus); |
2809 | assert(index >= 0); | |
2810 | assert(name); | |
2811 | ||
8a048c8c | 2812 | r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index); |
308e7dfd YW |
2813 | if (r < 0) |
2814 | return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s", | |
2815 | name, bus_error_message(&error, r)); | |
2816 | ||
2817 | return 0; | |
2818 | } | |
2819 | ||
2820 | static int link_renew(int argc, char *argv[], void *userdata) { | |
445f0d8b | 2821 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
f7581ed6 | 2822 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
e9b7408e | 2823 | int r; |
308e7dfd | 2824 | |
445f0d8b MY |
2825 | r = acquire_bus(&bus); |
2826 | if (r < 0) | |
2827 | return r; | |
2828 | ||
e9b7408e MY |
2829 | r = 0; |
2830 | ||
b48e277f | 2831 | for (int i = 1; i < argc; i++) { |
e9b7408e MY |
2832 | int index; |
2833 | ||
f6e49154 | 2834 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2835 | if (index < 0) |
2836 | return index; | |
308e7dfd | 2837 | |
e9b7408e | 2838 | RET_GATHER(r, link_renew_one(bus, index, argv[i])); |
308e7dfd YW |
2839 | } |
2840 | ||
e9b7408e | 2841 | return r; |
308e7dfd YW |
2842 | } |
2843 | ||
3efdd6af SS |
2844 | static int link_force_renew_one(sd_bus *bus, int index, const char *name) { |
2845 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2846 | int r; | |
2847 | ||
43449ca3 MY |
2848 | assert(bus); |
2849 | assert(index >= 0); | |
2850 | assert(name); | |
2851 | ||
8a048c8c | 2852 | r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index); |
3efdd6af SS |
2853 | if (r < 0) |
2854 | return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s", | |
2855 | name, bus_error_message(&error, r)); | |
2856 | ||
2857 | return 0; | |
2858 | } | |
2859 | ||
2860 | static int link_force_renew(int argc, char *argv[], void *userdata) { | |
445f0d8b | 2861 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
3efdd6af | 2862 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
b48e277f | 2863 | int k = 0, r; |
3efdd6af | 2864 | |
445f0d8b MY |
2865 | r = acquire_bus(&bus); |
2866 | if (r < 0) | |
2867 | return r; | |
2868 | ||
b48e277f | 2869 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2870 | int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
3efdd6af SS |
2871 | if (index < 0) |
2872 | return index; | |
2873 | ||
2874 | r = link_force_renew_one(bus, index, argv[i]); | |
2875 | if (r < 0 && k >= 0) | |
2876 | k = r; | |
2877 | } | |
2878 | ||
2879 | return k; | |
2880 | } | |
2881 | ||
a227674c | 2882 | static int verb_reload(int argc, char *argv[], void *userdata) { |
445f0d8b | 2883 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a227674c | 2884 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a227674c YW |
2885 | int r; |
2886 | ||
445f0d8b MY |
2887 | r = acquire_bus(&bus); |
2888 | if (r < 0) | |
2889 | return r; | |
2890 | ||
8a048c8c | 2891 | r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL); |
a227674c | 2892 | if (r < 0) |
1246d097 | 2893 | return log_error_errno(r, "Failed to reload network settings: %s", bus_error_message(&error, r)); |
a227674c YW |
2894 | |
2895 | return 0; | |
2896 | } | |
2897 | ||
8dc85c5e | 2898 | static int verb_reconfigure(int argc, char *argv[], void *userdata) { |
445f0d8b | 2899 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
8dc85c5e | 2900 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f7581ed6 | 2901 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
8dc85c5e | 2902 | _cleanup_set_free_ Set *indexes = NULL; |
b48e277f | 2903 | int index, r; |
8dc85c5e YW |
2904 | void *p; |
2905 | ||
445f0d8b MY |
2906 | r = acquire_bus(&bus); |
2907 | if (r < 0) | |
2908 | return r; | |
2909 | ||
8dc85c5e YW |
2910 | indexes = set_new(NULL); |
2911 | if (!indexes) | |
2912 | return log_oom(); | |
2913 | ||
b48e277f | 2914 | for (int i = 1; i < argc; i++) { |
f6e49154 | 2915 | index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]); |
231d9de1 ZJS |
2916 | if (index < 0) |
2917 | return index; | |
8dc85c5e YW |
2918 | |
2919 | r = set_put(indexes, INT_TO_PTR(index)); | |
2920 | if (r < 0) | |
2921 | return log_oom(); | |
2922 | } | |
2923 | ||
90e74a66 | 2924 | SET_FOREACH(p, indexes) { |
8dc85c5e | 2925 | index = PTR_TO_INT(p); |
8a048c8c | 2926 | r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index); |
01afd0f7 | 2927 | if (r < 0) |
1246d097 YW |
2928 | return log_error_errno(r, "Failed to reconfigure network interface %s: %s", |
2929 | FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX), | |
2930 | bus_error_message(&error, r)); | |
8dc85c5e YW |
2931 | } |
2932 | ||
2933 | return 0; | |
2934 | } | |
2935 | ||
91676b64 | 2936 | static int verb_persistent_storage(int argc, char *argv[], void *userdata) { |
7742d03e | 2937 | _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; |
91676b64 YW |
2938 | bool ready; |
2939 | int r; | |
2940 | ||
2941 | r = parse_boolean(argv[1]); | |
2942 | if (r < 0) | |
2943 | return log_error_errno(r, "Failed to parse argument: %s", argv[1]); | |
2944 | ready = r; | |
2945 | ||
2946 | r = varlink_connect_networkd(&vl); | |
2947 | if (r < 0) | |
2948 | return r; | |
2949 | ||
bfd8f70c YW |
2950 | if (ready) { |
2951 | _cleanup_close_ int fd = -EBADF; | |
2952 | ||
2953 | fd = open("/var/lib/systemd/network/", O_CLOEXEC | O_DIRECTORY); | |
2954 | if (fd < 0) | |
2955 | return log_error_errno(errno, "Failed to open /var/lib/systemd/network/: %m"); | |
2956 | ||
2957 | r = varlink_push_fd(vl, fd); | |
2958 | if (r < 0) | |
2959 | return log_error_errno(r, "Failed to push file descriptor of /var/lib/systemd/network/ into varlink: %m"); | |
2960 | ||
2961 | TAKE_FD(fd); | |
2962 | } | |
2963 | ||
91676b64 YW |
2964 | return varlink_callb_and_log(vl, "io.systemd.Network.SetPersistentStorage", /* reply = */ NULL, |
2965 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR_BOOLEAN("Ready", ready))); | |
2966 | } | |
2967 | ||
37ec0fdd LP |
2968 | static int help(void) { |
2969 | _cleanup_free_ char *link = NULL; | |
2970 | int r; | |
2971 | ||
2972 | r = terminal_urlify_man("networkctl", "1", &link); | |
2973 | if (r < 0) | |
2974 | return log_oom(); | |
2975 | ||
353b2baa LP |
2976 | printf("%s [OPTIONS...] COMMAND\n\n" |
2977 | "%sQuery and control the networking subsystem.%s\n" | |
a459b24f | 2978 | "\nCommands:\n" |
8dc85c5e YW |
2979 | " list [PATTERN...] List links\n" |
2980 | " status [PATTERN...] Show link status\n" | |
2981 | " lldp [PATTERN...] Show LLDP neighbors\n" | |
2982 | " label Show current address label entries in the kernel\n" | |
2983 | " delete DEVICES... Delete virtual netdevs\n" | |
c30ffcee SS |
2984 | " up DEVICES... Bring devices up\n" |
2985 | " down DEVICES... Bring devices down\n" | |
8dc85c5e | 2986 | " renew DEVICES... Renew dynamic configurations\n" |
3efdd6af | 2987 | " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n" |
8dc85c5e YW |
2988 | " reconfigure DEVICES... Reconfigure interfaces\n" |
2989 | " reload Reload .network and .netdev files\n" | |
96bab8fd MY |
2990 | " edit FILES|DEVICES... Edit network configuration files\n" |
2991 | " cat FILES|DEVICES... Show network configuration files\n" | |
28bb29cb MY |
2992 | " mask FILES... Mask network configuration files\n" |
2993 | " unmask FILES... Unmask network configuration files\n" | |
91676b64 YW |
2994 | " persistent-storage BOOL\n" |
2995 | " Notify systemd-networkd if persistent storage is ready\n" | |
353b2baa | 2996 | "\nOptions:\n" |
8dc85c5e YW |
2997 | " -h --help Show this help\n" |
2998 | " --version Show package version\n" | |
2999 | " --no-pager Do not pipe output into a pager\n" | |
3000 | " --no-legend Do not show the headers and footers\n" | |
3001 | " -a --all Show status for all links\n" | |
ba669952 | 3002 | " -s --stats Show detailed link statistics\n" |
10c71c36 YW |
3003 | " -l --full Do not ellipsize output\n" |
3004 | " -n --lines=INTEGER Number of journal entries to show\n" | |
bfc2b05e YW |
3005 | " --json=pretty|short|off\n" |
3006 | " Generate JSON output\n" | |
96bab8fd MY |
3007 | " --no-reload Do not reload systemd-networkd or systemd-udevd\n" |
3008 | " after editing network config\n" | |
3009 | " --drop-in=NAME Edit specified drop-in instead of main config file\n" | |
2b98926f | 3010 | " --runtime Edit runtime config files\n" |
bc556335 DDM |
3011 | "\nSee the %s for details.\n", |
3012 | program_invocation_short_name, | |
3013 | ansi_highlight(), | |
3014 | ansi_normal(), | |
3015 | link); | |
37ec0fdd LP |
3016 | |
3017 | return 0; | |
ee8c4568 LP |
3018 | } |
3019 | ||
3020 | static int parse_argv(int argc, char *argv[]) { | |
ee8c4568 LP |
3021 | enum { |
3022 | ARG_VERSION = 0x100, | |
3023 | ARG_NO_PAGER, | |
3024 | ARG_NO_LEGEND, | |
bfc2b05e | 3025 | ARG_JSON, |
96bab8fd MY |
3026 | ARG_NO_RELOAD, |
3027 | ARG_DROP_IN, | |
2b98926f | 3028 | ARG_RUNTIME, |
ee8c4568 LP |
3029 | }; |
3030 | ||
3031 | static const struct option options[] = { | |
3032 | { "help", no_argument, NULL, 'h' }, | |
3033 | { "version", no_argument, NULL, ARG_VERSION }, | |
3034 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
3035 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
9085f64a | 3036 | { "all", no_argument, NULL, 'a' }, |
a459b24f | 3037 | { "stats", no_argument, NULL, 's' }, |
10c71c36 YW |
3038 | { "full", no_argument, NULL, 'l' }, |
3039 | { "lines", required_argument, NULL, 'n' }, | |
bfc2b05e | 3040 | { "json", required_argument, NULL, ARG_JSON }, |
96bab8fd MY |
3041 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
3042 | { "drop-in", required_argument, NULL, ARG_DROP_IN }, | |
2b98926f | 3043 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
ee8c4568 LP |
3044 | {} |
3045 | }; | |
3046 | ||
bfc2b05e | 3047 | int c, r; |
ee8c4568 LP |
3048 | |
3049 | assert(argc >= 0); | |
3050 | assert(argv); | |
3051 | ||
10c71c36 | 3052 | while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) { |
ee8c4568 LP |
3053 | |
3054 | switch (c) { | |
3055 | ||
3056 | case 'h': | |
37ec0fdd | 3057 | return help(); |
ee8c4568 LP |
3058 | |
3059 | case ARG_VERSION: | |
3f6fd1ba | 3060 | return version(); |
ee8c4568 LP |
3061 | |
3062 | case ARG_NO_PAGER: | |
0221d68a | 3063 | arg_pager_flags |= PAGER_DISABLE; |
ee8c4568 LP |
3064 | break; |
3065 | ||
3066 | case ARG_NO_LEGEND: | |
3067 | arg_legend = false; | |
3068 | break; | |
3069 | ||
96bab8fd MY |
3070 | case ARG_NO_RELOAD: |
3071 | arg_no_reload = true; | |
3072 | break; | |
3073 | ||
2b98926f MY |
3074 | case ARG_RUNTIME: |
3075 | arg_runtime = true; | |
3076 | break; | |
3077 | ||
96bab8fd MY |
3078 | case ARG_DROP_IN: |
3079 | if (isempty(optarg)) | |
3080 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name."); | |
3081 | ||
5de8264b FS |
3082 | if (!endswith(optarg, ".conf")) { |
3083 | char *conf; | |
3084 | ||
3085 | conf = strjoin(optarg, ".conf"); | |
3086 | if (!conf) | |
3087 | return log_oom(); | |
3088 | ||
3089 | free_and_replace(arg_drop_in, conf); | |
3090 | } else { | |
3091 | r = free_and_strdup(&arg_drop_in, optarg); | |
3092 | if (r < 0) | |
3093 | return log_oom(); | |
3094 | } | |
96bab8fd MY |
3095 | |
3096 | if (!filename_is_valid(arg_drop_in)) | |
3097 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3098 | "Invalid drop-in file name '%s'.", arg_drop_in); | |
3099 | ||
3100 | break; | |
3101 | ||
9085f64a LP |
3102 | case 'a': |
3103 | arg_all = true; | |
3104 | break; | |
3105 | ||
a459b24f YW |
3106 | case 's': |
3107 | arg_stats = true; | |
3108 | break; | |
3109 | ||
10c71c36 YW |
3110 | case 'l': |
3111 | arg_full = true; | |
3112 | break; | |
3113 | ||
3114 | case 'n': | |
3115 | if (safe_atou(optarg, &arg_lines) < 0) | |
3116 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3117 | "Failed to parse lines '%s'", optarg); | |
3118 | break; | |
3119 | ||
bfc2b05e YW |
3120 | case ARG_JSON: |
3121 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
3122 | if (r <= 0) | |
3123 | return r; | |
3124 | break; | |
3125 | ||
ee8c4568 LP |
3126 | case '?': |
3127 | return -EINVAL; | |
3128 | ||
3129 | default: | |
04499a70 | 3130 | assert_not_reached(); |
ee8c4568 LP |
3131 | } |
3132 | } | |
3133 | ||
3134 | return 1; | |
3135 | } | |
3136 | ||
445f0d8b | 3137 | static int networkctl_main(int argc, char *argv[]) { |
15c3626e | 3138 | static const Verb verbs[] = { |
91676b64 YW |
3139 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_links }, |
3140 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, link_status }, | |
3141 | { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status }, | |
3142 | { "label", 1, 1, 0, list_address_labels }, | |
3143 | { "delete", 2, VERB_ANY, 0, link_delete }, | |
3144 | { "up", 2, VERB_ANY, 0, link_up_down }, | |
3145 | { "down", 2, VERB_ANY, 0, link_up_down }, | |
3146 | { "renew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_renew }, | |
3147 | { "forcerenew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_force_renew }, | |
3148 | { "reconfigure", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_reconfigure }, | |
3149 | { "reload", 1, 1, VERB_ONLINE_ONLY, verb_reload }, | |
3150 | { "edit", 2, VERB_ANY, 0, verb_edit }, | |
3151 | { "cat", 2, VERB_ANY, 0, verb_cat }, | |
3152 | { "mask", 2, VERB_ANY, 0, verb_mask }, | |
3153 | { "unmask", 2, VERB_ANY, 0, verb_unmask }, | |
3154 | { "persistent-storage", 2, 2, 0, verb_persistent_storage }, | |
266b5389 | 3155 | {} |
ee8c4568 LP |
3156 | }; |
3157 | ||
445f0d8b | 3158 | return dispatch_verb(argc, argv, verbs, NULL); |
58fb3678 LP |
3159 | } |
3160 | ||
4e2ca442 | 3161 | static int run(int argc, char* argv[]) { |
ee8c4568 LP |
3162 | int r; |
3163 | ||
d2acb93d | 3164 | log_setup(); |
ee8c4568 | 3165 | |
955fc5d8 YW |
3166 | sigbus_install(); |
3167 | ||
ee8c4568 LP |
3168 | r = parse_argv(argc, argv); |
3169 | if (r <= 0) | |
4e2ca442 | 3170 | return r; |
ee8c4568 | 3171 | |
445f0d8b | 3172 | return networkctl_main(argc, argv); |
ee8c4568 | 3173 | } |
4e2ca442 ZJS |
3174 | |
3175 | DEFINE_MAIN_FUNCTION(run); |