| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-hwdb.h" |
| 4 |
#include "sd-netlink.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "dhcp-protocol.h" |
| 8 |
#include "ether-addr-util.h" |
| 9 |
#include "format-ifname.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "in-addr-util.h" |
| 12 |
#include "local-addresses.h" |
| 13 |
#include "log.h" |
| 14 |
#include "networkctl-dump-util.h" |
| 15 |
#include "stdio-util.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "strv.h" |
| 18 |
|
| 19 |
int dump_list(Table *table, const char *key, char * const *l) { |
| 20 |
int r; |
| 21 |
|
| 22 |
assert(table); |
| 23 |
assert(key); |
| 24 |
|
| 25 |
if (strv_isempty(l)) |
| 26 |
return 0; |
| 27 |
|
| 28 |
r = table_add_many(table, |
| 29 |
TABLE_FIELD, key, |
| 30 |
TABLE_STRV, l); |
| 31 |
if (r < 0) |
| 32 |
return table_log_add_error(r); |
| 33 |
|
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
/* IEEE Organizationally Unique Identifier vendor string */ |
| 38 |
int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) { |
| 39 |
_cleanup_free_ char *desc = NULL; |
| 40 |
const char *description; |
| 41 |
char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1]; |
| 42 |
int r; |
| 43 |
|
| 44 |
assert(ret); |
| 45 |
|
| 46 |
if (!hwdb || !mac) |
| 47 |
return -EINVAL; |
| 48 |
|
| 49 |
/* skip commonly misused 00:00:00 (Xerox) prefix */ |
| 50 |
if (memcmp(mac, "\0\0\0", 3) == 0) |
| 51 |
return -EINVAL; |
| 52 |
|
| 53 |
xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac)); |
| 54 |
|
| 55 |
r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
|
| 59 |
desc = strdup(description); |
| 60 |
if (!desc) |
| 61 |
return -ENOMEM; |
| 62 |
|
| 63 |
*ret = TAKE_PTR(desc); |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int get_gateway_description( |
| 69 |
sd_netlink *rtnl, |
| 70 |
sd_hwdb *hwdb, |
| 71 |
int ifindex, |
| 72 |
int family, |
| 73 |
union in_addr_union *gateway, |
| 74 |
char **ret) { |
| 75 |
|
| 76 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
| 77 |
int r; |
| 78 |
|
| 79 |
assert(rtnl); |
| 80 |
assert(ifindex >= 0); |
| 81 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 82 |
assert(gateway); |
| 83 |
assert(ret); |
| 84 |
|
| 85 |
r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); |
| 86 |
if (r < 0) |
| 87 |
return r; |
| 88 |
|
| 89 |
r = sd_netlink_message_set_request_dump(req, true); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
r = sd_netlink_call(rtnl, req, 0, &reply); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
| 98 |
union in_addr_union gw = IN_ADDR_NULL; |
| 99 |
struct ether_addr mac = ETHER_ADDR_NULL; |
| 100 |
uint16_t type; |
| 101 |
int ifi, fam; |
| 102 |
|
| 103 |
r = sd_netlink_message_get_errno(m); |
| 104 |
if (r < 0) { |
| 105 |
log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
r = sd_netlink_message_get_type(m, &type); |
| 110 |
if (r < 0) { |
| 111 |
log_error_errno(r, "Failed to get netlink message type, ignoring: %m"); |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
if (type != RTM_NEWNEIGH) { |
| 116 |
log_error("Got unexpected netlink message type %u, ignoring.", type); |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
r = sd_rtnl_message_neigh_get_family(m, &fam); |
| 121 |
if (r < 0) { |
| 122 |
log_error_errno(r, "Failed to get rtnl family, ignoring: %m"); |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
if (fam != family) { |
| 127 |
log_error("Got invalid rtnl family %d, ignoring.", fam); |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); |
| 132 |
if (r < 0) { |
| 133 |
log_error_errno(r, "Failed to get rtnl ifindex, ignoring: %m"); |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
if (ifindex > 0 && ifi != ifindex) |
| 138 |
continue; |
| 139 |
|
| 140 |
switch (fam) { |
| 141 |
|
| 142 |
case AF_INET: |
| 143 |
r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in); |
| 144 |
if (r < 0) |
| 145 |
continue; |
| 146 |
|
| 147 |
break; |
| 148 |
|
| 149 |
case AF_INET6: |
| 150 |
r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6); |
| 151 |
if (r < 0) |
| 152 |
continue; |
| 153 |
|
| 154 |
break; |
| 155 |
|
| 156 |
default: |
| 157 |
assert_not_reached(); |
| 158 |
} |
| 159 |
|
| 160 |
if (!in_addr_equal(fam, &gw, gateway)) |
| 161 |
continue; |
| 162 |
|
| 163 |
r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac); |
| 164 |
if (r < 0) |
| 165 |
continue; |
| 166 |
|
| 167 |
r = ieee_oui(hwdb, &mac, ret); |
| 168 |
if (r < 0) |
| 169 |
continue; |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
return -ENODATA; |
| 175 |
} |
| 176 |
|
| 177 |
int dump_gateways(sd_netlink *rtnl, sd_hwdb *hwdb, Table *table, int ifindex) { |
| 178 |
_cleanup_free_ struct local_address *local_addrs = NULL; |
| 179 |
_cleanup_strv_free_ char **buf = NULL; |
| 180 |
int r, n; |
| 181 |
|
| 182 |
assert(rtnl); |
| 183 |
assert(table); |
| 184 |
|
| 185 |
n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
| 186 |
if (n <= 0) |
| 187 |
return n; |
| 188 |
|
| 189 |
FOREACH_ARRAY(local, local_addrs, n) { |
| 190 |
_cleanup_free_ char *description = NULL; |
| 191 |
|
| 192 |
r = get_gateway_description(rtnl, hwdb, local->ifindex, local->family, &local->address, &description); |
| 193 |
if (r < 0) |
| 194 |
log_debug_errno(r, "Could not get description of gateway, ignoring: %m"); |
| 195 |
|
| 196 |
/* Show interface name for the entry if we show entries for all interfaces */ |
| 197 |
r = strv_extendf(&buf, "%s%s%s%s%s%s", |
| 198 |
IN_ADDR_TO_STRING(local->family, &local->address), |
| 199 |
description ? " (" : "", |
| 200 |
strempty(description), |
| 201 |
description ? ")" : "", |
| 202 |
ifindex <= 0 ? " on " : "", |
| 203 |
ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
| 204 |
if (r < 0) |
| 205 |
return log_oom(); |
| 206 |
} |
| 207 |
|
| 208 |
return dump_list(table, "Gateway", buf); |
| 209 |
} |
| 210 |
|
| 211 |
int dump_addresses( |
| 212 |
sd_netlink *rtnl, |
| 213 |
sd_dhcp_message *message, |
| 214 |
Table *table, |
| 215 |
int ifindex) { |
| 216 |
|
| 217 |
_cleanup_free_ struct local_address *local_addrs = NULL; |
| 218 |
_cleanup_strv_free_ char **buf = NULL; |
| 219 |
struct in_addr dhcp4_address = {}, server_address = {}; |
| 220 |
int r, n; |
| 221 |
|
| 222 |
assert(rtnl); |
| 223 |
assert(table); |
| 224 |
|
| 225 |
n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local_addrs); |
| 226 |
if (n <= 0) |
| 227 |
return n; |
| 228 |
|
| 229 |
if (message) { |
| 230 |
dhcp4_address.s_addr = message->header.yiaddr; |
| 231 |
if (dhcp_message_get_option_address(message, SD_DHCP_OPTION_SERVER_IDENTIFIER, &server_address) < 0) |
| 232 |
/* The message should be BOOTP, let's fallback to the siaddr field. */ |
| 233 |
server_address.s_addr = message->header.siaddr; |
| 234 |
} |
| 235 |
|
| 236 |
FOREACH_ARRAY(local, local_addrs, n) { |
| 237 |
bool dhcp4 = local->family == AF_INET && in4_addr_equal(&local->address.in, &dhcp4_address); |
| 238 |
|
| 239 |
r = strv_extendf(&buf, "%s%s%s%s%s%s", |
| 240 |
IN_ADDR_TO_STRING(local->family, &local->address), |
| 241 |
dhcp4 ? " (DHCPv4 via " : "", |
| 242 |
dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "", |
| 243 |
dhcp4 ? ")" : "", |
| 244 |
ifindex <= 0 ? " on " : "", |
| 245 |
ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); |
| 246 |
if (r < 0) |
| 247 |
return log_oom(); |
| 248 |
} |
| 249 |
|
| 250 |
return dump_list(table, "Address", buf); |
| 251 |
} |