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