Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print_mac.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2019 The strace developers.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. */
  7. #include "defs.h"
  8. #include "error_prints.h"
  9. #include "xstring.h"
  10. #define XLAT_MACROS_ONLY
  11. # include "xlat/arp_hardware_types.h"
  12. #undef XLAT_MACROS_ONLY
  13. DIAG_PUSH_IGNORE_OVERRIDE_INIT
  14. static uint8_t hwaddr_sizes[] = {
  15. [0 ... ARPHRD_IEEE802_TR] = 255,
  16. [ARPHRD_NETROM] = 7 /* AX25_ADDR_LEN */,
  17. [ARPHRD_ETHER] = 6 /* ETH_ALEN */,
  18. /* ARPHRD_EETHER - no actual devices in Linux */
  19. [ARPHRD_AX25] = 7 /* AX25_ADDR_LEN */,
  20. /* ARPHRD_PRONET - no actual devices in Linux */
  21. /* ARPHRD_CHAOS - no actual devices in Linux */
  22. [ARPHRD_IEEE802] = 6 /* FC_ALEN */,
  23. [ARPHRD_ARCNET] = 1 /* ARCNET_ALEN */,
  24. /* ARPHRD_APPLETLK - no actual devices in Linux */
  25. [ARPHRD_DLCI] = sizeof(short),
  26. /* ARPHRD_ATM - no explicit setting */
  27. /* ARPHRD_METRICOM - no actual devices in Linux */
  28. [ARPHRD_IEEE1394] = 16 /* FWNET_ALEN */,
  29. [ARPHRD_EUI64] = 8 /* EUI64_ADDR_LEN */,
  30. [ARPHRD_INFINIBAND] = 20 /* INFINIBAND_ALEN */,
  31. [ARPHRD_SLIP] = 0,
  32. /* ARPHRD_CSLIP - no actual devices in Linux */
  33. /* ARPHRD_SLIP6 - no actual devices in Linux */
  34. /* ARPHRD_CSLIP6 - no actual devices in Linux */
  35. /* ARPHRD_RSRVD - no actual devices in Linux */
  36. /* ARPHRD_ADAPT - no actual devices in Linux */
  37. [ARPHRD_ROSE] = 5 /* ROSE_ADDR_LEN */,
  38. [ARPHRD_X25] = 0,
  39. /* ARPHRD_HWX25 - no actual devices in Linux */
  40. [ARPHRD_CAN] = 0,
  41. [ARPHRD_PPP] = 0,
  42. /* ARPHRD_CISCO - no actual devices in Linux */
  43. /* ARPHRD_LAPB - no actual devices in Linux */
  44. /* ARPHRD_DDCMP - no actual devices in Linux */
  45. [ARPHRD_RAWHDLC] = 0,
  46. [ARPHRD_RAWIP] = 0,
  47. [ARPHRD_TUNNEL] = 4 /* IPIP */,
  48. [ARPHRD_TUNNEL6] = 16 /* sizeof(struct in6_addr) */,
  49. /* ARPHRD_FRAD - no actual devices in Linux */
  50. /* ARPHRD_SKIP - no actual devices in Linux */
  51. [ARPHRD_LOOPBACK] = 6 /* ETH_ALEN */,
  52. [ARPHRD_LOCALTLK] = 1 /* LTALK_ALEN */,
  53. [ARPHRD_FDDI] = 6 /* FDDI_K_ALEN */,
  54. /* ARPHRD_BIF - no actual devices in Linux */
  55. [ARPHRD_SIT] = 4,
  56. [ARPHRD_IPDDP] = 0,
  57. [ARPHRD_IPGRE] = 4,
  58. [ARPHRD_PIMREG] = 0,
  59. [ARPHRD_HIPPI] = 6 /* HIPPI_ALEN */,
  60. /* ARPHRD_ASH - no actual devices in Linux */
  61. /* ARPHRD_ECONET - no actual devices in Linux */
  62. [ARPHRD_IRDA] = 4 /* LAP_ALEN */,
  63. /* ARPHRD_FCPP - no actual devices in Linux */
  64. /* ARPHRD_FCAL - no actual devices in Linux */
  65. /* ARPHRD_FCPL - no actual devices in Linux */
  66. /* ARPHRD_FCFABRIC - no actual devices in Linux */
  67. /* ARPHRD_IEEE802_TR - no actual devices in Linux */
  68. [ARPHRD_IEEE80211] = 6 /* ETH_ALEN */,
  69. [ARPHRD_IEEE80211_PRISM] = 6 /* ETH_ALEN */,
  70. [ARPHRD_IEEE80211_RADIOTAP] = 6 /* ETH_ALEN */,
  71. [ARPHRD_IEEE802154]
  72. = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
  73. [ARPHRD_IEEE802154_MONITOR]
  74. = 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
  75. [ARPHRD_PHONET] = 1,
  76. [ARPHRD_PHONET_PIPE] = 1,
  77. [ARPHRD_CAIF] = 0,
  78. [ARPHRD_IP6GRE] = 16 /* sizeof(struct in6_addr) */,
  79. [ARPHRD_NETLINK] = 0,
  80. [ARPHRD_6LOWPAN] = 8 /* EUI64_ADDR_LEN */
  81. /* ^ or ETH_ALEN, depending on lltype */,
  82. [ARPHRD_VSOCKMON] = 0,
  83. };
  84. DIAG_POP_IGNORE_OVERRIDE_INIT
  85. const char *
  86. sprint_mac_addr(const uint8_t addr[], size_t size)
  87. {
  88. static char res[MAX_ADDR_LEN * 3];
  89. if (size > MAX_ADDR_LEN) {
  90. error_func_msg("Address size (%zu) is more than maximum "
  91. "supported (%u)", size, MAX_ADDR_LEN);
  92. return NULL;
  93. }
  94. char *ptr = res;
  95. for (size_t i = 0; i < size; i++)
  96. ptr = xappendstr(res, ptr, "%s%02x", i ? ":" : "", addr[i]);
  97. return res;
  98. }
  99. void
  100. print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
  101. {
  102. tprints(prefix);
  103. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
  104. print_quoted_string((const char *) addr, size,
  105. QUOTE_FORCE_HEX);
  106. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
  107. return;
  108. (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
  109. ? tprints_comment : tprints)(sprint_mac_addr(addr, size));
  110. }
  111. const char *
  112. sprint_hwaddr(const uint8_t hwaddr[], size_t size, uint32_t devtype)
  113. {
  114. uint8_t sz = (devtype < ARRAY_SIZE(hwaddr_sizes))
  115. ? hwaddr_sizes[devtype] : 255;
  116. return sprint_mac_addr(hwaddr, MIN(size, sz));
  117. }
  118. void
  119. print_hwaddr(const char *prefix, const uint8_t addr[], size_t size,
  120. uint32_t devtype)
  121. {
  122. tprints(prefix);
  123. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
  124. print_quoted_string((const char *) addr, size,
  125. QUOTE_FORCE_HEX);
  126. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
  127. return;
  128. (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
  129. ? tprints_comment : tprints)(sprint_hwaddr(addr, size,
  130. devtype));
  131. }