| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "ansi-color.h" |
| 5 |
#include "arphrd-util.h" |
| 6 |
#include "dhcp-message.h" |
| 7 |
#include "dhcp-message-dump.h" |
| 8 |
#include "dhcp-route.h" |
| 9 |
#include "dns-resolver-internal.h" |
| 10 |
#include "escape.h" |
| 11 |
#include "ether-addr-util.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "hexdecoct.h" |
| 15 |
#include "iovec-util.h" |
| 16 |
#include "iovec-wrapper.h" |
| 17 |
#include "parse-util.h" |
| 18 |
#include "stdio-util.h" |
| 19 |
#include "string-table.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
|
| 23 |
static int iovec_to_hex(const struct iovec *iov, char **ret) { |
| 24 |
assert(iov); |
| 25 |
assert(ret); |
| 26 |
|
| 27 |
_cleanup_free_ char *str = new(char, iov->iov_len * 3); |
| 28 |
if (!str) |
| 29 |
return log_oom(); |
| 30 |
|
| 31 |
char *p = str; |
| 32 |
FOREACH_ARRAY(v, ((uint8_t*) iov->iov_base), iov->iov_len) { |
| 33 |
if (p != str) |
| 34 |
*p++ = ':'; |
| 35 |
*p++ = hexchar(*v >> 4); |
| 36 |
*p++ = hexchar(*v & 0x0f); |
| 37 |
} |
| 38 |
*p = '\0'; |
| 39 |
|
| 40 |
*ret = TAKE_PTR(str); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int iovw_to_strv(const struct iovec_wrapper *iovw, char ***ret) { |
| 45 |
int r; |
| 46 |
|
| 47 |
assert(iovw); |
| 48 |
assert(ret); |
| 49 |
|
| 50 |
_cleanup_strv_free_ char **strv = NULL; |
| 51 |
size_t n_strv = 0; |
| 52 |
FOREACH_ARRAY(iov, iovw->iovec, iovw->count) { |
| 53 |
_cleanup_free_ char *escaped = cescape_length(iov->iov_base, iov->iov_len); |
| 54 |
if (!escaped) |
| 55 |
return log_oom(); |
| 56 |
|
| 57 |
r = strv_consume_with_size(&strv, &n_strv, TAKE_PTR(escaped)); |
| 58 |
if (r < 0) |
| 59 |
return log_oom(); |
| 60 |
} |
| 61 |
|
| 62 |
*ret = TAKE_PTR(strv); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static void table_apply_flags(Table *table, DumpDHCPMessageFlag flags) { |
| 67 |
assert(table); |
| 68 |
|
| 69 |
table_set_header(table, FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)); |
| 70 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_FULL)) |
| 71 |
table_set_width(table, 0); |
| 72 |
} |
| 73 |
|
| 74 |
static int dump_dhcp_option_vendor_specific_information(sd_dhcp_message *m, DumpDHCPMessageFlag flags) { |
| 75 |
int r; |
| 76 |
|
| 77 |
assert(m); |
| 78 |
|
| 79 |
_cleanup_(table_unrefp) Table *table = table_new("code", "data"); |
| 80 |
if (!table) |
| 81 |
return log_oom(); |
| 82 |
|
| 83 |
table_apply_flags(table, flags); |
| 84 |
|
| 85 |
(void) table_set_sort(table, (size_t) 0); |
| 86 |
|
| 87 |
TableCell *cell = table_get_cell(table, 0, 0); |
| 88 |
if (!cell) |
| 89 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell."); |
| 90 |
|
| 91 |
(void) table_set_align_percent(table, cell, 100); |
| 92 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 93 |
|
| 94 |
_cleanup_(tlv_unrefp) TLV *tlv = NULL; |
| 95 |
r = dhcp_message_get_option_sub_tlv(m, SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, TLV_DHCP4_SUBOPTION, &tlv); |
| 96 |
if (r < 0) |
| 97 |
return log_error_errno(r, "Failed to read DHCP option %i: %m", SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION); |
| 98 |
|
| 99 |
void *tagp; |
| 100 |
struct iovec_wrapper *iovw; |
| 101 |
HASHMAP_FOREACH_KEY(iovw, tagp, tlv->entries) |
| 102 |
FOREACH_ARRAY(iov, iovw->iovec, iovw->count) { |
| 103 |
_cleanup_free_ char *str = NULL; |
| 104 |
r = iovec_to_hex(iov, &str); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
|
| 108 |
r = table_add_many( |
| 109 |
table, |
| 110 |
TABLE_UINT32, PTR_TO_UINT32(tagp), |
| 111 |
TABLE_STRING, str); |
| 112 |
if (r < 0) |
| 113 |
return table_log_add_error(r); |
| 114 |
} |
| 115 |
|
| 116 |
putchar('\n'); |
| 117 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)) |
| 118 |
printf("%s%sVendor-Specific Information:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal()); |
| 119 |
|
| 120 |
return table_print_or_warn(table); |
| 121 |
} |
| 122 |
|
| 123 |
static int dump_dhcp_option_vendor_identifying_vendor_class(sd_dhcp_message *m, DumpDHCPMessageFlag flags) { |
| 124 |
int r; |
| 125 |
|
| 126 |
assert(m); |
| 127 |
|
| 128 |
_cleanup_(table_unrefp) Table *table = table_new("enterprise-number", "data"); |
| 129 |
if (!table) |
| 130 |
return log_oom(); |
| 131 |
|
| 132 |
table_apply_flags(table, flags); |
| 133 |
|
| 134 |
(void) table_set_sort(table, (size_t) 0); |
| 135 |
|
| 136 |
TableCell *cell = table_get_cell(table, 0, 0); |
| 137 |
if (!cell) |
| 138 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell."); |
| 139 |
|
| 140 |
(void) table_set_align_percent(table, cell, 100); |
| 141 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 142 |
|
| 143 |
_cleanup_(tlv_unrefp) TLV *tlv = NULL; |
| 144 |
r = dhcp_message_get_option_sub_tlv(m, SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS, TLV_DHCP4_VENDOR_IDENTIFYING_OPTION, &tlv); |
| 145 |
if (r < 0) |
| 146 |
return log_error_errno(r, "Failed to read DHCP option %i: %m", SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS); |
| 147 |
|
| 148 |
void *key, *value; |
| 149 |
HASHMAP_FOREACH_KEY(value, key, tlv->entries) { |
| 150 |
uint32_t enterprise_number = PTR_TO_UINT32(key); |
| 151 |
|
| 152 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 153 |
r = tlv_get_alloc(tlv, enterprise_number, &iov); |
| 154 |
if (r < 0) |
| 155 |
return log_error_errno(r, "Failed to read vendor class of enterprise number %"PRIu32": %m", enterprise_number); |
| 156 |
|
| 157 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 158 |
r = iovec_split(&iov, /* length_size= */ 1, &iovw); |
| 159 |
if (r < 0) |
| 160 |
return log_error_errno(r, "Failed to parse vendor class of enterprise number %"PRIu32": %m", enterprise_number); |
| 161 |
|
| 162 |
_cleanup_strv_free_ char **strv = NULL; |
| 163 |
r = iovw_to_strv(&iovw, &strv); |
| 164 |
if (r < 0) |
| 165 |
return r; |
| 166 |
|
| 167 |
r = table_add_many( |
| 168 |
table, |
| 169 |
TABLE_UINT32, enterprise_number, |
| 170 |
TABLE_STRV, strv); |
| 171 |
if (r < 0) |
| 172 |
return table_log_add_error(r); |
| 173 |
} |
| 174 |
|
| 175 |
putchar('\n'); |
| 176 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)) |
| 177 |
printf("%s%sVendor-Identifying Vendor Class:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal()); |
| 178 |
|
| 179 |
return table_print_or_warn(table); |
| 180 |
} |
| 181 |
|
| 182 |
static int dump_dhcp_option_vendor_identifying_vendor_specific_information(sd_dhcp_message *m, DumpDHCPMessageFlag flags) { |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(m); |
| 186 |
|
| 187 |
_cleanup_(table_unrefp) Table *table = table_new("enterprise-number", "code", "data"); |
| 188 |
if (!table) |
| 189 |
return log_oom(); |
| 190 |
|
| 191 |
table_apply_flags(table, flags); |
| 192 |
|
| 193 |
(void) table_set_sort(table, (size_t) 0, (size_t) 1); |
| 194 |
|
| 195 |
for (unsigned i = 0; i <= 1; i++) { |
| 196 |
TableCell *cell = table_get_cell(table, 0, i); |
| 197 |
if (!cell) |
| 198 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell."); |
| 199 |
|
| 200 |
(void) table_set_align_percent(table, cell, 100); |
| 201 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 202 |
} |
| 203 |
|
| 204 |
_cleanup_(tlv_unrefp) TLV *tlv = NULL; |
| 205 |
r = dhcp_message_get_option_sub_tlv(m, SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION, TLV_DHCP4_VENDOR_IDENTIFYING_OPTION, &tlv); |
| 206 |
if (r < 0) |
| 207 |
return log_error_errno(r, "Failed to read DHCP option %i: %m", SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION); |
| 208 |
|
| 209 |
void *key, *value; |
| 210 |
HASHMAP_FOREACH_KEY(value, key, tlv->entries) { |
| 211 |
uint32_t enterprise_number = PTR_TO_UINT32(key); |
| 212 |
|
| 213 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 214 |
r = tlv_get_alloc(tlv, enterprise_number, &iov); |
| 215 |
if (r < 0) |
| 216 |
return log_error_errno(r, "Failed to read vendor specific information of enterprise number %"PRIu32": %m", enterprise_number); |
| 217 |
|
| 218 |
_cleanup_(tlv_done) TLV sub_tlv = TLV_INIT(TLV_DHCP4_SUBOPTION); |
| 219 |
r = tlv_parse(&sub_tlv, &iov); |
| 220 |
if (r < 0) |
| 221 |
return log_error_errno(r, "Failed to parse vendor specific information of enterprise number %"PRIu32": %m", enterprise_number); |
| 222 |
|
| 223 |
void *tagp; |
| 224 |
struct iovec_wrapper *iovw; |
| 225 |
HASHMAP_FOREACH_KEY(iovw, tagp, sub_tlv.entries) { |
| 226 |
uint32_t code = PTR_TO_UINT32(tagp); |
| 227 |
|
| 228 |
FOREACH_ARRAY(i, iovw->iovec, iovw->count) { |
| 229 |
_cleanup_free_ char *str = NULL; |
| 230 |
r = iovec_to_hex(i, &str); |
| 231 |
if (r < 0) |
| 232 |
return r; |
| 233 |
|
| 234 |
r = table_add_many( |
| 235 |
table, |
| 236 |
TABLE_UINT32, enterprise_number, |
| 237 |
TABLE_UINT32, code, |
| 238 |
TABLE_STRING, str); |
| 239 |
if (r < 0) |
| 240 |
return table_log_add_error(r); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
putchar('\n'); |
| 246 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)) |
| 247 |
printf("%s%sVendor-Identifying Vendor-Specific Information:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal()); |
| 248 |
|
| 249 |
return table_print_or_warn(table); |
| 250 |
} |
| 251 |
|
| 252 |
typedef enum DHCPOptionType { |
| 253 |
DHCP_OPTION_TYPE_AUTO, |
| 254 |
DHCP_OPTION_TYPE_HEX, |
| 255 |
DHCP_OPTION_TYPE_FLAG, |
| 256 |
DHCP_OPTION_TYPE_BOOL, |
| 257 |
DHCP_OPTION_TYPE_UINT8, |
| 258 |
DHCP_OPTION_TYPE_UINT16, |
| 259 |
DHCP_OPTION_TYPE_TIME, |
| 260 |
DHCP_OPTION_TYPE_STRING, |
| 261 |
DHCP_OPTION_TYPE_ADDRESS, |
| 262 |
_DHCP_OPTION_TYPE_AUTO_MAX, |
| 263 |
DHCP_OPTION_TYPE_SIP = _DHCP_OPTION_TYPE_AUTO_MAX, |
| 264 |
DHCP_OPTION_TYPE_FQDN, |
| 265 |
DHCP_OPTION_TYPE_ROUTE, |
| 266 |
DHCP_OPTION_TYPE_LENGTH_PREFIXED_DATA, |
| 267 |
DHCP_OPTION_TYPE_SEARCH_DOMAINS, |
| 268 |
DHCP_OPTION_TYPE_DNR, |
| 269 |
DHCP_OPTION_TYPE_6RD, |
| 270 |
DHCP_OPTION_TYPE_TBD, |
| 271 |
_DHCP_OPTION_TYPE_MAX, |
| 272 |
_DHCP_OPTION_TYPE_INVALID = -EINVAL, |
| 273 |
} DHCPOptionType; |
| 274 |
|
| 275 |
static const char * const dhcp_option_type_table[_DHCP_OPTION_TYPE_AUTO_MAX] = { |
| 276 |
[DHCP_OPTION_TYPE_AUTO] = "auto", |
| 277 |
[DHCP_OPTION_TYPE_HEX] = "hex", |
| 278 |
[DHCP_OPTION_TYPE_FLAG] = "flag", |
| 279 |
[DHCP_OPTION_TYPE_BOOL] = "bool", |
| 280 |
[DHCP_OPTION_TYPE_UINT8] = "uint8", |
| 281 |
[DHCP_OPTION_TYPE_UINT16] = "uint16", |
| 282 |
[DHCP_OPTION_TYPE_TIME] = "time", |
| 283 |
[DHCP_OPTION_TYPE_STRING] = "string", |
| 284 |
[DHCP_OPTION_TYPE_ADDRESS] = "address", |
| 285 |
}; |
| 286 |
|
| 287 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_option_type, DHCPOptionType); |
| 288 |
|
| 289 |
static DHCPOptionType dhcp_option_type_from_code(uint8_t code) { |
| 290 |
switch (code) { |
| 291 |
case SD_DHCP_OPTION_PAD: |
| 292 |
return -EINVAL; |
| 293 |
case SD_DHCP_OPTION_SUBNET_MASK: |
| 294 |
case SD_DHCP_OPTION_ROUTER: |
| 295 |
case SD_DHCP_OPTION_TIME_SERVER: |
| 296 |
case SD_DHCP_OPTION_NAME_SERVER: |
| 297 |
case SD_DHCP_OPTION_DOMAIN_NAME_SERVER: |
| 298 |
case SD_DHCP_OPTION_LOG_SERVER: |
| 299 |
case SD_DHCP_OPTION_QUOTES_SERVER: |
| 300 |
case SD_DHCP_OPTION_LPR_SERVER: |
| 301 |
case SD_DHCP_OPTION_IMPRESS_SERVER: |
| 302 |
case SD_DHCP_OPTION_RLP_SERVER: |
| 303 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 304 |
case SD_DHCP_OPTION_HOST_NAME: |
| 305 |
return DHCP_OPTION_TYPE_STRING; |
| 306 |
case SD_DHCP_OPTION_BOOT_FILE_SIZE: |
| 307 |
return DHCP_OPTION_TYPE_UINT16; |
| 308 |
case SD_DHCP_OPTION_MERIT_DUMP_FILE: |
| 309 |
case SD_DHCP_OPTION_DOMAIN_NAME: |
| 310 |
return DHCP_OPTION_TYPE_STRING; |
| 311 |
case SD_DHCP_OPTION_SWAP_SERVER: |
| 312 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 313 |
case SD_DHCP_OPTION_ROOT_PATH: |
| 314 |
case SD_DHCP_OPTION_EXTENSION_FILE: |
| 315 |
return DHCP_OPTION_TYPE_STRING; |
| 316 |
case SD_DHCP_OPTION_FORWARD: |
| 317 |
case SD_DHCP_OPTION_SOURCE_ROUTE: |
| 318 |
return DHCP_OPTION_TYPE_BOOL; |
| 319 |
case SD_DHCP_OPTION_MAX_DATAGRAM_ASSEMBLY: |
| 320 |
return DHCP_OPTION_TYPE_UINT16; |
| 321 |
case SD_DHCP_OPTION_DEFAULT_IP_TTL: |
| 322 |
return DHCP_OPTION_TYPE_UINT8; |
| 323 |
case SD_DHCP_OPTION_MTU_TIMEOUT: |
| 324 |
return DHCP_OPTION_TYPE_TIME; |
| 325 |
case SD_DHCP_OPTION_MTU_INTERFACE: |
| 326 |
return DHCP_OPTION_TYPE_UINT16; |
| 327 |
case SD_DHCP_OPTION_MTU_SUBNET: |
| 328 |
return DHCP_OPTION_TYPE_BOOL; |
| 329 |
case SD_DHCP_OPTION_BROADCAST: |
| 330 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 331 |
case SD_DHCP_OPTION_MASK_DISCOVERY: |
| 332 |
case SD_DHCP_OPTION_MASK_SUPPLIER: |
| 333 |
case SD_DHCP_OPTION_ROUTER_DISCOVERY: |
| 334 |
return DHCP_OPTION_TYPE_BOOL; |
| 335 |
case SD_DHCP_OPTION_ROUTER_REQUEST: |
| 336 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 337 |
case SD_DHCP_OPTION_STATIC_ROUTE: |
| 338 |
return DHCP_OPTION_TYPE_ROUTE; |
| 339 |
case SD_DHCP_OPTION_TRAILERS: |
| 340 |
return DHCP_OPTION_TYPE_BOOL; |
| 341 |
case SD_DHCP_OPTION_ARP_TIMEOUT: |
| 342 |
return DHCP_OPTION_TYPE_TIME; |
| 343 |
case SD_DHCP_OPTION_ETHERNET: |
| 344 |
return DHCP_OPTION_TYPE_BOOL; |
| 345 |
case SD_DHCP_OPTION_DEFAULT_TCP_TTL: |
| 346 |
return DHCP_OPTION_TYPE_UINT8; |
| 347 |
case SD_DHCP_OPTION_KEEPALIVE_TIME: |
| 348 |
return DHCP_OPTION_TYPE_TIME; |
| 349 |
case SD_DHCP_OPTION_KEEPALIVE_DATA: |
| 350 |
return DHCP_OPTION_TYPE_BOOL; |
| 351 |
case SD_DHCP_OPTION_NIS_DOMAIN: |
| 352 |
return DHCP_OPTION_TYPE_STRING; |
| 353 |
case SD_DHCP_OPTION_NIS_SERVER: |
| 354 |
case SD_DHCP_OPTION_NTP_SERVER: |
| 355 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 356 |
case SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION: |
| 357 |
return DHCP_OPTION_TYPE_TBD; |
| 358 |
case SD_DHCP_OPTION_NETBIOS_NAME_SERVER: |
| 359 |
case SD_DHCP_OPTION_NETBIOS_DIST_SERVER: |
| 360 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 361 |
case SD_DHCP_OPTION_NETBIOS_NODE_TYPE: |
| 362 |
return DHCP_OPTION_TYPE_UINT8; |
| 363 |
case SD_DHCP_OPTION_NETBIOS_SCOPE: |
| 364 |
return DHCP_OPTION_TYPE_STRING; |
| 365 |
case SD_DHCP_OPTION_X_WINDOW_FONT: |
| 366 |
case SD_DHCP_OPTION_X_WINDOW_MANAGER: |
| 367 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 368 |
case SD_DHCP_OPTION_REQUESTED_IP_ADDRESS: |
| 369 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 370 |
case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME: |
| 371 |
return DHCP_OPTION_TYPE_TIME; |
| 372 |
case SD_DHCP_OPTION_OVERLOAD: |
| 373 |
case SD_DHCP_OPTION_MESSAGE_TYPE: |
| 374 |
return DHCP_OPTION_TYPE_UINT8; |
| 375 |
case SD_DHCP_OPTION_SERVER_IDENTIFIER: |
| 376 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 377 |
case SD_DHCP_OPTION_ERROR_MESSAGE: |
| 378 |
return DHCP_OPTION_TYPE_STRING; |
| 379 |
case SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE: |
| 380 |
return DHCP_OPTION_TYPE_UINT16; |
| 381 |
case SD_DHCP_OPTION_RENEWAL_TIME: |
| 382 |
case SD_DHCP_OPTION_REBINDING_TIME: |
| 383 |
return DHCP_OPTION_TYPE_TIME; |
| 384 |
case SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER: |
| 385 |
return DHCP_OPTION_TYPE_STRING; |
| 386 |
case SD_DHCP_OPTION_NETWARE_IP_DOMAIN: |
| 387 |
return DHCP_OPTION_TYPE_STRING; |
| 388 |
case SD_DHCP_OPTION_NIS_DOMAIN_NAME: |
| 389 |
return DHCP_OPTION_TYPE_STRING; |
| 390 |
case SD_DHCP_OPTION_NIS_SERVER_ADDR: |
| 391 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 392 |
case SD_DHCP_OPTION_BOOT_SERVER_NAME: |
| 393 |
case SD_DHCP_OPTION_BOOT_FILENAME: |
| 394 |
return DHCP_OPTION_TYPE_STRING; |
| 395 |
case SD_DHCP_OPTION_HOME_AGENT_ADDRESS: |
| 396 |
case SD_DHCP_OPTION_SMTP_SERVER: |
| 397 |
case SD_DHCP_OPTION_POP3_SERVER: |
| 398 |
case SD_DHCP_OPTION_NNTP_SERVER: |
| 399 |
case SD_DHCP_OPTION_WWW_SERVER: |
| 400 |
case SD_DHCP_OPTION_FINGER_SERVER: |
| 401 |
case SD_DHCP_OPTION_IRC_SERVER: |
| 402 |
case SD_DHCP_OPTION_STREETTALK_SERVER: |
| 403 |
case SD_DHCP_OPTION_STDA_SERVER: |
| 404 |
return DHCP_OPTION_TYPE_ADDRESS; |
| 405 |
case SD_DHCP_OPTION_USER_CLASS: |
| 406 |
return DHCP_OPTION_TYPE_LENGTH_PREFIXED_DATA; |
| 407 |
case SD_DHCP_OPTION_RAPID_COMMIT: |
| 408 |
return DHCP_OPTION_TYPE_FLAG; |
| 409 |
case SD_DHCP_OPTION_FQDN: |
| 410 |
return DHCP_OPTION_TYPE_FQDN; |
| 411 |
case SD_DHCP_OPTION_POSIX_TIMEZONE: |
| 412 |
case SD_DHCP_OPTION_TZDB_TIMEZONE: |
| 413 |
return DHCP_OPTION_TYPE_STRING; |
| 414 |
case SD_DHCP_OPTION_IPV6_ONLY_PREFERRED: |
| 415 |
return DHCP_OPTION_TYPE_TIME; |
| 416 |
case SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL: |
| 417 |
return DHCP_OPTION_TYPE_STRING; |
| 418 |
case SD_DHCP_OPTION_DOMAIN_SEARCH: |
| 419 |
return DHCP_OPTION_TYPE_SEARCH_DOMAINS; |
| 420 |
case SD_DHCP_OPTION_SIP_SERVER: |
| 421 |
return DHCP_OPTION_TYPE_SIP; |
| 422 |
case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
| 423 |
return DHCP_OPTION_TYPE_ROUTE; |
| 424 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS: |
| 425 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION: |
| 426 |
return DHCP_OPTION_TYPE_TBD; |
| 427 |
case SD_DHCP_OPTION_MUD_URL: |
| 428 |
return DHCP_OPTION_TYPE_STRING; |
| 429 |
case SD_DHCP_OPTION_V4_DNR: |
| 430 |
return DHCP_OPTION_TYPE_DNR; |
| 431 |
case SD_DHCP_OPTION_6RD: |
| 432 |
return DHCP_OPTION_TYPE_6RD; |
| 433 |
case SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE: |
| 434 |
return DHCP_OPTION_TYPE_ROUTE; |
| 435 |
case SD_DHCP_OPTION_END: |
| 436 |
return -EINVAL; |
| 437 |
default: |
| 438 |
return DHCP_OPTION_TYPE_HEX; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
static int dump_dhcp_option_hex(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 443 |
int r; |
| 444 |
|
| 445 |
assert(table); |
| 446 |
assert(message); |
| 447 |
|
| 448 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 449 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 450 |
if (r < 0) |
| 451 |
return log_error_errno(r, "Failed to read DHCP option %u: %m", code); |
| 452 |
|
| 453 |
_cleanup_free_ char *str = NULL; |
| 454 |
r = iovec_to_hex(&iov, &str); |
| 455 |
if (r < 0) |
| 456 |
return r; |
| 457 |
|
| 458 |
r = table_add_many( |
| 459 |
table, |
| 460 |
TABLE_UINT8, code, |
| 461 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 462 |
TABLE_STRING, str); |
| 463 |
if (r < 0) |
| 464 |
return table_log_add_error(r); |
| 465 |
|
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
static int dump_dhcp_option_flag(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 470 |
int r; |
| 471 |
|
| 472 |
assert(table); |
| 473 |
assert(message); |
| 474 |
|
| 475 |
r = dhcp_message_get_option_flag(message, code); |
| 476 |
if (r < 0) { |
| 477 |
if (fallback) |
| 478 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 479 |
return log_error_errno(r, "Failed to dump DHCP option %u as flag: %m", code); |
| 480 |
} |
| 481 |
|
| 482 |
r = table_add_many( |
| 483 |
table, |
| 484 |
TABLE_UINT8, code, |
| 485 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 486 |
TABLE_BOOLEAN, true); |
| 487 |
if (r < 0) |
| 488 |
return table_log_add_error(r); |
| 489 |
|
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
static int dump_dhcp_option_bool(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 494 |
int r; |
| 495 |
|
| 496 |
assert(table); |
| 497 |
assert(message); |
| 498 |
|
| 499 |
uint8_t u; |
| 500 |
r = dhcp_message_get_option_u8(message, code, &u); |
| 501 |
if (r < 0) { |
| 502 |
if (fallback) |
| 503 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 504 |
return log_error_errno(r, "Failed to read option %u as boolean: %m", code); |
| 505 |
} |
| 506 |
|
| 507 |
r = table_add_many( |
| 508 |
table, |
| 509 |
TABLE_UINT8, code, |
| 510 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 511 |
TABLE_BOOLEAN, !!u); |
| 512 |
if (r < 0) |
| 513 |
return table_log_add_error(r); |
| 514 |
|
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
static int dump_dhcp_option_uint8(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 519 |
int r; |
| 520 |
|
| 521 |
assert(table); |
| 522 |
assert(message); |
| 523 |
|
| 524 |
uint8_t u; |
| 525 |
r = dhcp_message_get_option_u8(message, code, &u); |
| 526 |
if (r < 0) { |
| 527 |
if (fallback) |
| 528 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 529 |
return log_error_errno(r, "Failed to read option %u as uint8: %m", code); |
| 530 |
} |
| 531 |
|
| 532 |
r = table_add_many( |
| 533 |
table, |
| 534 |
TABLE_UINT8, code, |
| 535 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 536 |
TABLE_UINT8, u); |
| 537 |
if (r < 0) |
| 538 |
return table_log_add_error(r); |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
static int dump_dhcp_option_uint16(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 544 |
int r; |
| 545 |
|
| 546 |
assert(table); |
| 547 |
assert(message); |
| 548 |
|
| 549 |
uint16_t u; |
| 550 |
r = dhcp_message_get_option_u16(message, code, &u); |
| 551 |
if (r < 0) { |
| 552 |
if (fallback) |
| 553 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 554 |
return log_error_errno(r, "Failed to read option %u as uint16: %m", code); |
| 555 |
} |
| 556 |
|
| 557 |
r = table_add_many( |
| 558 |
table, |
| 559 |
TABLE_UINT8, code, |
| 560 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 561 |
TABLE_UINT16, u); |
| 562 |
if (r < 0) |
| 563 |
return table_log_add_error(r); |
| 564 |
|
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
static int dump_dhcp_option_time(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 569 |
int r; |
| 570 |
|
| 571 |
assert(table); |
| 572 |
assert(message); |
| 573 |
|
| 574 |
usec_t usec; |
| 575 |
r = dhcp_message_get_option_sec(message, code, /* max_as_infinity= */ true, &usec); |
| 576 |
if (r < 0) { |
| 577 |
if (fallback) |
| 578 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 579 |
return log_error_errno(r, "Failed to read option %u as time: %m", code); |
| 580 |
} |
| 581 |
|
| 582 |
r = table_add_many( |
| 583 |
table, |
| 584 |
TABLE_UINT8, code, |
| 585 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 586 |
TABLE_TIMESPAN, usec); |
| 587 |
if (r < 0) |
| 588 |
return table_log_add_error(r); |
| 589 |
|
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
static int dump_dhcp_option_string(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 594 |
int r; |
| 595 |
|
| 596 |
assert(table); |
| 597 |
assert(message); |
| 598 |
|
| 599 |
_cleanup_free_ char *str = NULL; |
| 600 |
r = dhcp_message_get_option_string(message, code, &str); |
| 601 |
if (r < 0) { |
| 602 |
if (fallback) |
| 603 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 604 |
return log_error_errno(r, "Failed to read option %u as string: %m", code); |
| 605 |
} |
| 606 |
|
| 607 |
r = table_add_many( |
| 608 |
table, |
| 609 |
TABLE_UINT8, code, |
| 610 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 611 |
TABLE_STRING, str); |
| 612 |
if (r < 0) |
| 613 |
return table_log_add_error(r); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
static int dump_dhcp_option_address(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 619 |
int r; |
| 620 |
|
| 621 |
assert(table); |
| 622 |
assert(message); |
| 623 |
|
| 624 |
_cleanup_free_ struct in_addr *addrs = NULL; |
| 625 |
size_t n_addrs; |
| 626 |
r = dhcp_message_get_option_addresses(message, code, &n_addrs, &addrs); |
| 627 |
if (r < 0) { |
| 628 |
if (fallback) |
| 629 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 630 |
return log_error_errno(r, "Failed to read option %u as address: %m", code); |
| 631 |
} |
| 632 |
|
| 633 |
_cleanup_strv_free_ char **strv = NULL; |
| 634 |
size_t n_strv = 0; |
| 635 |
FOREACH_ARRAY(a, addrs, n_addrs) { |
| 636 |
r = strv_extend_with_size(&strv, &n_strv, IN4_ADDR_TO_STRING(a)); |
| 637 |
if (r < 0) |
| 638 |
return log_oom(); |
| 639 |
} |
| 640 |
|
| 641 |
r = table_add_many( |
| 642 |
table, |
| 643 |
TABLE_UINT8, code, |
| 644 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 645 |
TABLE_STRV, strv); |
| 646 |
if (r < 0) |
| 647 |
return table_log_add_error(r); |
| 648 |
|
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
static int dump_dhcp_option_sip(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 653 |
int r; |
| 654 |
|
| 655 |
assert(table); |
| 656 |
assert(message); |
| 657 |
assert(code == SD_DHCP_OPTION_SIP_SERVER); |
| 658 |
|
| 659 |
_cleanup_strv_free_ char **strv = NULL; |
| 660 |
if (dhcp_message_get_option_domains(message, code, &strv) < 0) |
| 661 |
return dump_dhcp_option_address(table, message, code, /* fallback= */ true); |
| 662 |
|
| 663 |
r = table_add_many( |
| 664 |
table, |
| 665 |
TABLE_UINT8, code, |
| 666 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 667 |
TABLE_STRV, strv); |
| 668 |
if (r < 0) |
| 669 |
return table_log_add_error(r); |
| 670 |
|
| 671 |
return 0; |
| 672 |
} |
| 673 |
|
| 674 |
static int dump_dhcp_option_fqdn(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 675 |
int r; |
| 676 |
|
| 677 |
assert(table); |
| 678 |
assert(message); |
| 679 |
assert(code == SD_DHCP_OPTION_FQDN); |
| 680 |
|
| 681 |
_cleanup_free_ char *fqdn = NULL; |
| 682 |
uint8_t flags; |
| 683 |
if (dhcp_message_get_option_fqdn(message, &flags, &fqdn) < 0) |
| 684 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 685 |
|
| 686 |
r = table_add_many( |
| 687 |
table, |
| 688 |
TABLE_UINT8, code, |
| 689 |
TABLE_STRING, dhcp_option_code_to_string(code)); |
| 690 |
if (r < 0) |
| 691 |
return table_log_add_error(r); |
| 692 |
|
| 693 |
r = table_add_cell_stringf(table, /* ret_cell= */ NULL, "flags: 0x%x, fqdn: %s", flags, fqdn); |
| 694 |
if (r < 0) |
| 695 |
return table_log_add_error(r); |
| 696 |
|
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
static int dump_dhcp_option_route(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 701 |
int r; |
| 702 |
|
| 703 |
assert(table); |
| 704 |
assert(message); |
| 705 |
|
| 706 |
_cleanup_free_ sd_dhcp_route *routes = NULL; |
| 707 |
size_t n_routes; |
| 708 |
if (dhcp_message_get_option_routes(message, code, &n_routes, &routes) < 0) |
| 709 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 710 |
|
| 711 |
_cleanup_strv_free_ char **strv = NULL; |
| 712 |
size_t n_strv = 0; |
| 713 |
FOREACH_ARRAY(route, routes, n_routes) { |
| 714 |
r = strv_extendf_with_size(&strv, &n_strv, "%s via %s", |
| 715 |
IN4_ADDR_PREFIX_TO_STRING(&route->dst_addr, route->dst_prefixlen), |
| 716 |
IN4_ADDR_TO_STRING(&route->gw_addr)); |
| 717 |
if (r < 0) |
| 718 |
return log_oom(); |
| 719 |
} |
| 720 |
|
| 721 |
r = table_add_many( |
| 722 |
table, |
| 723 |
TABLE_UINT8, code, |
| 724 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 725 |
TABLE_STRV, strv); |
| 726 |
if (r < 0) |
| 727 |
return table_log_add_error(r); |
| 728 |
|
| 729 |
return 0; |
| 730 |
} |
| 731 |
|
| 732 |
static int dump_dhcp_option_length_prefixed_data(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 733 |
int r; |
| 734 |
|
| 735 |
assert(table); |
| 736 |
assert(message); |
| 737 |
|
| 738 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 739 |
if (dhcp_message_get_option_length_prefixed_data(message, code, /* length_size= */ 1, &iovw) < 0) |
| 740 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 741 |
|
| 742 |
_cleanup_strv_free_ char **strv = NULL; |
| 743 |
r = iovw_to_strv(&iovw, &strv); |
| 744 |
if (r < 0) |
| 745 |
return r; |
| 746 |
|
| 747 |
r = table_add_many( |
| 748 |
table, |
| 749 |
TABLE_UINT8, code, |
| 750 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 751 |
TABLE_STRV, strv); |
| 752 |
if (r < 0) |
| 753 |
return table_log_add_error(r); |
| 754 |
|
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
static int dump_dhcp_option_tbd(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 759 |
int r; |
| 760 |
|
| 761 |
assert(table); |
| 762 |
assert(message); |
| 763 |
|
| 764 |
if (!dhcp_message_has_option(message, code)) |
| 765 |
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "The DHCP message does not have option %u.", code); |
| 766 |
|
| 767 |
r = table_add_many( |
| 768 |
table, |
| 769 |
TABLE_UINT8, code, |
| 770 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 771 |
TABLE_STRING, "See below."); |
| 772 |
if (r < 0) |
| 773 |
return table_log_add_error(r); |
| 774 |
|
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
static int dump_dhcp_option_search_domains(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 779 |
int r; |
| 780 |
|
| 781 |
assert(table); |
| 782 |
assert(message); |
| 783 |
assert(code == SD_DHCP_OPTION_DOMAIN_SEARCH); |
| 784 |
|
| 785 |
_cleanup_strv_free_ char **strv = NULL; |
| 786 |
if (dhcp_message_get_option_domains(message, code, &strv) < 0) |
| 787 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 788 |
|
| 789 |
r = table_add_many( |
| 790 |
table, |
| 791 |
TABLE_UINT8, code, |
| 792 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 793 |
TABLE_STRV, strv); |
| 794 |
if (r < 0) |
| 795 |
return table_log_add_error(r); |
| 796 |
|
| 797 |
return 0; |
| 798 |
} |
| 799 |
|
| 800 |
static int dump_dhcp_option_dnr(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 801 |
int r; |
| 802 |
|
| 803 |
assert(table); |
| 804 |
assert(message); |
| 805 |
assert(code == SD_DHCP_OPTION_V4_DNR); |
| 806 |
|
| 807 |
sd_dns_resolver *resolvers = NULL; |
| 808 |
size_t n_resolvers = 0; |
| 809 |
CLEANUP_ARRAY(resolvers, n_resolvers, dns_resolver_free_array); |
| 810 |
|
| 811 |
if (dhcp_message_get_option_dnr(message, &n_resolvers, &resolvers) < 0) |
| 812 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 813 |
|
| 814 |
_cleanup_strv_free_ char **strv = NULL; |
| 815 |
r = dns_resolvers_to_dot_strv(resolvers, n_resolvers, &strv); |
| 816 |
if (r < 0) |
| 817 |
return log_oom(); |
| 818 |
|
| 819 |
r = table_add_many( |
| 820 |
table, |
| 821 |
TABLE_UINT8, code, |
| 822 |
TABLE_STRING, dhcp_option_code_to_string(code), |
| 823 |
TABLE_STRV, strv); |
| 824 |
if (r < 0) |
| 825 |
return table_log_add_error(r); |
| 826 |
|
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
static int dump_dhcp_option_6rd(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback) { |
| 831 |
int r; |
| 832 |
|
| 833 |
assert(table); |
| 834 |
assert(message); |
| 835 |
assert(code == SD_DHCP_OPTION_6RD); |
| 836 |
|
| 837 |
uint8_t ipv4masklen, prefixlen; |
| 838 |
struct in6_addr prefix; |
| 839 |
size_t n_br_addresses; |
| 840 |
_cleanup_free_ struct in_addr *br_addresses = NULL; |
| 841 |
if (dhcp_message_get_option_6rd(message, &ipv4masklen, &prefixlen, &prefix, &n_br_addresses, &br_addresses) < 0) |
| 842 |
return dump_dhcp_option_hex(table, message, code, /* fallback= */ false); |
| 843 |
|
| 844 |
_cleanup_free_ char *str = asprintf_safe("ipv4masklen: %u, prefix: %s, br_addresses: ", |
| 845 |
ipv4masklen, IN6_ADDR_PREFIX_TO_STRING(&prefix, prefixlen)); |
| 846 |
if (!str) |
| 847 |
return log_oom(); |
| 848 |
|
| 849 |
assert(n_br_addresses > 0); |
| 850 |
_cleanup_free_ char *br_addresses_str = NULL; |
| 851 |
FOREACH_ARRAY(a, br_addresses, n_br_addresses) |
| 852 |
if (!strextend_with_separator(&br_addresses_str, ", ", IN4_ADDR_TO_STRING(a))) |
| 853 |
return log_oom(); |
| 854 |
|
| 855 |
r = table_add_many( |
| 856 |
table, |
| 857 |
TABLE_UINT8, code, |
| 858 |
TABLE_STRING, dhcp_option_code_to_string(code)); |
| 859 |
if (r < 0) |
| 860 |
return table_log_add_error(r); |
| 861 |
|
| 862 |
r = table_add_cell_stringf(table, /* ret_cell= */ NULL, "%s%s", str, br_addresses_str); |
| 863 |
if (r < 0) |
| 864 |
return table_log_add_error(r); |
| 865 |
|
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
static int dump_dhcp_option_one(Table *table, sd_dhcp_message *message, uint8_t code, DHCPOptionType type) { |
| 870 |
assert(table); |
| 871 |
assert(message); |
| 872 |
assert(!IN_SET(code, SD_DHCP_OPTION_PAD, SD_DHCP_OPTION_END)); |
| 873 |
|
| 874 |
typedef int (*dump_dhcp_option_t)(Table *table, sd_dhcp_message *message, uint8_t code, bool fallback); |
| 875 |
|
| 876 |
static const dump_dhcp_option_t functions[_DHCP_OPTION_TYPE_MAX] = { |
| 877 |
[DHCP_OPTION_TYPE_HEX] = dump_dhcp_option_hex, |
| 878 |
[DHCP_OPTION_TYPE_FLAG] = dump_dhcp_option_flag, |
| 879 |
[DHCP_OPTION_TYPE_BOOL] = dump_dhcp_option_bool, |
| 880 |
[DHCP_OPTION_TYPE_UINT8] = dump_dhcp_option_uint8, |
| 881 |
[DHCP_OPTION_TYPE_UINT16] = dump_dhcp_option_uint16, |
| 882 |
[DHCP_OPTION_TYPE_TIME] = dump_dhcp_option_time, |
| 883 |
[DHCP_OPTION_TYPE_STRING] = dump_dhcp_option_string, |
| 884 |
[DHCP_OPTION_TYPE_ADDRESS] = dump_dhcp_option_address, |
| 885 |
[DHCP_OPTION_TYPE_SIP] = dump_dhcp_option_sip, |
| 886 |
[DHCP_OPTION_TYPE_FQDN] = dump_dhcp_option_fqdn, |
| 887 |
[DHCP_OPTION_TYPE_ROUTE] = dump_dhcp_option_route, |
| 888 |
[DHCP_OPTION_TYPE_LENGTH_PREFIXED_DATA] = dump_dhcp_option_length_prefixed_data, |
| 889 |
[DHCP_OPTION_TYPE_SEARCH_DOMAINS] = dump_dhcp_option_search_domains, |
| 890 |
[DHCP_OPTION_TYPE_DNR] = dump_dhcp_option_dnr, |
| 891 |
[DHCP_OPTION_TYPE_6RD] = dump_dhcp_option_6rd, |
| 892 |
[DHCP_OPTION_TYPE_TBD] = dump_dhcp_option_tbd, |
| 893 |
}; |
| 894 |
|
| 895 |
bool fallback = false; |
| 896 |
if (type == DHCP_OPTION_TYPE_AUTO) { |
| 897 |
type = dhcp_option_type_from_code(code); |
| 898 |
fallback = true; |
| 899 |
} |
| 900 |
|
| 901 |
assert(functions[type]); |
| 902 |
return functions[type](table, message, code, fallback); |
| 903 |
} |
| 904 |
|
| 905 |
static int parse_arg(const char *arg, uint8_t *ret_code, DHCPOptionType *ret_type) { |
| 906 |
_cleanup_free_ char *buf = NULL; |
| 907 |
const char *code_str, *type_str; |
| 908 |
int r; |
| 909 |
|
| 910 |
assert(arg); |
| 911 |
|
| 912 |
const char *colon = strchr(arg, ':'); |
| 913 |
if (colon) { |
| 914 |
buf = strndup(arg, colon - arg); |
| 915 |
if (!buf) |
| 916 |
return log_oom(); |
| 917 |
|
| 918 |
code_str = buf; |
| 919 |
type_str = colon + 1; |
| 920 |
} else { |
| 921 |
code_str = arg; |
| 922 |
type_str = NULL; |
| 923 |
} |
| 924 |
|
| 925 |
uint8_t code; |
| 926 |
r = safe_atou8(code_str, &code); |
| 927 |
if (r < 0) |
| 928 |
return log_error_errno(r, "Failed to parse option code number '%s': %m", code_str); |
| 929 |
|
| 930 |
if (IN_SET(code, SD_DHCP_OPTION_PAD, SD_DHCP_OPTION_END)) |
| 931 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 932 |
"Invalid option code number: %u", code); |
| 933 |
|
| 934 |
DHCPOptionType type = DHCP_OPTION_TYPE_AUTO; |
| 935 |
if (type_str) { |
| 936 |
type = dhcp_option_type_from_string(type_str); |
| 937 |
if (type < 0) |
| 938 |
return log_error_errno(type, "Failed to parse option type '%s': %m", type_str); |
| 939 |
} |
| 940 |
|
| 941 |
if (ret_code) |
| 942 |
*ret_code = code; |
| 943 |
if (ret_type) |
| 944 |
*ret_type = type; |
| 945 |
return 0; |
| 946 |
} |
| 947 |
|
| 948 |
static int dump_dhcp_options(sd_dhcp_message *message, char * const *args, DumpDHCPMessageFlag flags) { |
| 949 |
int r; |
| 950 |
|
| 951 |
assert(message); |
| 952 |
|
| 953 |
_cleanup_(table_unrefp) Table *table = table_new("code", "name", "data"); |
| 954 |
if (!table) |
| 955 |
return log_oom(); |
| 956 |
|
| 957 |
table_apply_flags(table, flags); |
| 958 |
|
| 959 |
(void) table_set_sort(table, (size_t) 0); |
| 960 |
|
| 961 |
TableCell *cell = table_get_cell(table, 0, 0); |
| 962 |
if (!cell) |
| 963 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell."); |
| 964 |
|
| 965 |
(void) table_set_align_percent(table, cell, 100); |
| 966 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 967 |
|
| 968 |
bool |
| 969 |
has_vendor_specific_information = false, |
| 970 |
has_vendor_identifying_vendor_class = false, |
| 971 |
has_vendor_identifying_vendor_specific_information = false; |
| 972 |
|
| 973 |
if (strv_isempty(args)) { |
| 974 |
void *tagp; |
| 975 |
struct iovec_wrapper *iovw; |
| 976 |
HASHMAP_FOREACH_KEY(iovw, tagp, message->options.entries) { |
| 977 |
uint32_t tag = PTR_TO_UINT32(tagp); |
| 978 |
assert(tag > 0); |
| 979 |
assert(tag < UINT8_MAX); |
| 980 |
|
| 981 |
r = dump_dhcp_option_one(table, message, tag, DHCP_OPTION_TYPE_AUTO); |
| 982 |
if (r < 0) |
| 983 |
return r; |
| 984 |
|
| 985 |
switch (tag) { |
| 986 |
case SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION: |
| 987 |
has_vendor_specific_information = true; |
| 988 |
break; |
| 989 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS: |
| 990 |
has_vendor_identifying_vendor_class = true; |
| 991 |
break; |
| 992 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION: |
| 993 |
has_vendor_identifying_vendor_specific_information = true; |
| 994 |
break; |
| 995 |
} |
| 996 |
} |
| 997 |
} else |
| 998 |
STRV_FOREACH(arg, args) { |
| 999 |
uint8_t code = SD_DHCP_OPTION_PAD; /* avoid false maybe-uninitialized warning */ |
| 1000 |
DHCPOptionType type = DHCP_OPTION_TYPE_AUTO; /* avoid false maybe-uninitialized warning */ |
| 1001 |
r = parse_arg(*arg, &code, &type); |
| 1002 |
if (r < 0) |
| 1003 |
return r; |
| 1004 |
assert(!IN_SET(code, SD_DHCP_OPTION_PAD, SD_DHCP_OPTION_END)); |
| 1005 |
|
| 1006 |
r = dump_dhcp_option_one(table, message, code, type); |
| 1007 |
if (r < 0) |
| 1008 |
return r; |
| 1009 |
|
| 1010 |
if (type == DHCP_OPTION_TYPE_AUTO) |
| 1011 |
switch (code) { |
| 1012 |
case SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION: |
| 1013 |
has_vendor_specific_information = true; |
| 1014 |
break; |
| 1015 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS: |
| 1016 |
has_vendor_identifying_vendor_class = true; |
| 1017 |
break; |
| 1018 |
case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION: |
| 1019 |
has_vendor_identifying_vendor_specific_information = true; |
| 1020 |
break; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)) |
| 1025 |
printf("%s%sOptions:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal()); |
| 1026 |
|
| 1027 |
r = table_print_or_warn(table); |
| 1028 |
if (r < 0) |
| 1029 |
return r; |
| 1030 |
|
| 1031 |
if (has_vendor_specific_information) { |
| 1032 |
r = dump_dhcp_option_vendor_specific_information(message, flags); |
| 1033 |
if (r < 0) |
| 1034 |
return r; |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (has_vendor_identifying_vendor_class) { |
| 1038 |
r = dump_dhcp_option_vendor_identifying_vendor_class(message, flags); |
| 1039 |
if (r < 0) |
| 1040 |
return r; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (has_vendor_identifying_vendor_specific_information) { |
| 1044 |
r = dump_dhcp_option_vendor_identifying_vendor_specific_information(message, flags); |
| 1045 |
if (r < 0) |
| 1046 |
return r; |
| 1047 |
} |
| 1048 |
|
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
|
| 1052 |
static int dump_buffer(Table *table, const char *field, uint8_t *buf, size_t len) { |
| 1053 |
int r; |
| 1054 |
|
| 1055 |
assert(table); |
| 1056 |
assert(field); |
| 1057 |
assert(buf); |
| 1058 |
assert(len > 0); |
| 1059 |
|
| 1060 |
uint8_t *nul = memchr(buf, 0, len); |
| 1061 |
if (nul) |
| 1062 |
len = nul - buf; |
| 1063 |
|
| 1064 |
if (len == 0) |
| 1065 |
return 0; |
| 1066 |
|
| 1067 |
_cleanup_free_ char *str = NULL; |
| 1068 |
r = make_cstring(buf, len, MAKE_CSTRING_REFUSE_TRAILING_NUL, &str); |
| 1069 |
if (r < 0) |
| 1070 |
return log_error_errno(r, "Failed to parse buffer for field '%s': %m", field); |
| 1071 |
|
| 1072 |
if (isempty(str)) |
| 1073 |
return 0; |
| 1074 |
|
| 1075 |
r = table_add_many( |
| 1076 |
table, |
| 1077 |
TABLE_FIELD, field, |
| 1078 |
TABLE_STRING, str); |
| 1079 |
if (r < 0) |
| 1080 |
return table_log_add_error(r); |
| 1081 |
|
| 1082 |
return 0; |
| 1083 |
} |
| 1084 |
|
| 1085 |
static int dump_dhcp_header(sd_dhcp_message *message, DumpDHCPMessageFlag flags) { |
| 1086 |
int r; |
| 1087 |
|
| 1088 |
assert(message); |
| 1089 |
|
| 1090 |
_cleanup_(table_unrefp) Table *table = table_new_vertical(); |
| 1091 |
if (!table) |
| 1092 |
return log_oom(); |
| 1093 |
|
| 1094 |
table_apply_flags(table, flags); |
| 1095 |
|
| 1096 |
TableCell *cell = table_get_cell(table, 0, 0); |
| 1097 |
if (!cell) |
| 1098 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell."); |
| 1099 |
|
| 1100 |
(void) table_set_align_percent(table, cell, 100); |
| 1101 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 1102 |
|
| 1103 |
struct hw_addr_data hw_addr; |
| 1104 |
r = dhcp_message_get_hw_addr(message, &hw_addr); |
| 1105 |
if (r < 0) |
| 1106 |
return log_error_errno(r, "Failed to get hardware address from DHCP message: %m"); |
| 1107 |
|
| 1108 |
struct in_addr yiaddr = { .s_addr = message->header.yiaddr }; |
| 1109 |
|
| 1110 |
r = table_add_many( |
| 1111 |
table, |
| 1112 |
TABLE_FIELD, "Hardware Type", |
| 1113 |
TABLE_STRING, arphrd_to_name(message->header.htype), |
| 1114 |
TABLE_FIELD, "Hardware Address", |
| 1115 |
TABLE_STRING, HW_ADDR_TO_STR(&hw_addr), |
| 1116 |
TABLE_FIELD, "Client Address", |
| 1117 |
TABLE_IN_ADDR, &yiaddr); |
| 1118 |
if (r < 0) |
| 1119 |
return table_log_add_error(r); |
| 1120 |
|
| 1121 |
if (message->header.siaddr != INADDR_ANY) { |
| 1122 |
struct in_addr siaddr = { .s_addr = message->header.siaddr }; |
| 1123 |
|
| 1124 |
r = table_add_many( |
| 1125 |
table, |
| 1126 |
TABLE_FIELD, "Server Address", |
| 1127 |
TABLE_IN_ADDR, &siaddr); |
| 1128 |
if (r < 0) |
| 1129 |
return table_log_add_error(r); |
| 1130 |
} |
| 1131 |
|
| 1132 |
if (message->header.giaddr != INADDR_ANY) { |
| 1133 |
struct in_addr giaddr = { .s_addr = message->header.giaddr }; |
| 1134 |
|
| 1135 |
r = table_add_many( |
| 1136 |
table, |
| 1137 |
TABLE_FIELD, "Relay Agent Address", |
| 1138 |
TABLE_IN_ADDR, &giaddr); |
| 1139 |
if (r < 0) |
| 1140 |
return table_log_add_error(r); |
| 1141 |
} |
| 1142 |
|
| 1143 |
uint8_t overload = DHCP_OVERLOAD_NONE; |
| 1144 |
(void) dhcp_message_get_option_u8(message, SD_DHCP_OPTION_OVERLOAD, &overload); |
| 1145 |
|
| 1146 |
if (!FLAGS_SET(overload, DHCP_OVERLOAD_SNAME)) { |
| 1147 |
r = dump_buffer(table, "Server Host Name", message->header.sname, sizeof(message->header.sname)); |
| 1148 |
if (r < 0) |
| 1149 |
return r; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if (!FLAGS_SET(overload, DHCP_OVERLOAD_FILE)) { |
| 1153 |
r = dump_buffer(table, "Boot File Name", message->header.file, sizeof(message->header.file)); |
| 1154 |
if (r < 0) |
| 1155 |
return r; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND)) |
| 1159 |
printf("%s%sHeader:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal()); |
| 1160 |
|
| 1161 |
return table_print_or_warn(table); |
| 1162 |
} |
| 1163 |
|
| 1164 |
int dump_dhcp_message(sd_dhcp_message *m, char * const *args, DumpDHCPMessageFlag flags) { |
| 1165 |
int r; |
| 1166 |
|
| 1167 |
assert(m); |
| 1168 |
|
| 1169 |
if (strv_isempty(args)) { |
| 1170 |
r = dump_dhcp_header(m, flags); |
| 1171 |
if (r < 0) |
| 1172 |
return r; |
| 1173 |
|
| 1174 |
putchar('\n'); |
| 1175 |
} |
| 1176 |
|
| 1177 |
return dump_dhcp_options(m, args, flags); |
| 1178 |
} |