Browse Source

Generalise hardware address printing

* defs.h [!MAX_ADDR_LEN] (MAX_ADDR_LEN): New macro.
(sprint_hwaddr): New declaration.
(print_hwaddr): New inline function, a wrapper for sprint_hwaddr.
* print_fields.h (PRINT_FIELD_HWADDR_SZ): New macro.
* print_mac.c: Include "xlat/arp_hardware_types.h" under
XLAT_MACROS_ONLY.
[!MAX_ADDR_LEN] (MAX_ADDR_LEN): Remove.
(sprint_hwaddr): New function.
* sock.c (print_ifreq) <SIOCSIFHWADDR, SIOCGIFHWADDR>: Print hardware
address using PRINT_FIELD_HWADDR_SZ.
Eugene Syromyatnikov 1 year ago
parent
commit
db0a627b62
4 changed files with 124 additions and 85 deletions
  1. 9
    0
      defs.h
  2. 9
    0
      print_fields.h
  3. 103
    3
      print_mac.c
  4. 3
    82
      sock.c

+ 9
- 0
defs.h View File

@@ -818,10 +818,19 @@ extern const char *sprinttime(long long sec);
818 818
 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
819 819
 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
820 820
 
821
+#ifndef MAX_ADDR_LEN
822
+# define MAX_ADDR_LEN 32
823
+#endif
824
+
821 825
 extern const char *sprint_mac_addr(const uint8_t addr[], size_t size);
822 826
 extern void print_mac_addr(const char *prefix,
823 827
 			   const uint8_t addr[], size_t size);
824 828
 
829
+extern const char *sprint_hwaddr(const uint8_t addr[], size_t size,
830
+				 uint32_t devtype);
831
+extern void print_hwaddr(const char *prefix,
832
+			 const uint8_t addr[], size_t size, uint32_t devtype);
833
+
825 834
 extern void print_uuid(const unsigned char *uuid);
826 835
 
827 836
 extern void print_symbolic_mode_t(unsigned int);

+ 9
- 0
print_fields.h View File

@@ -264,4 +264,13 @@
264 264
 			       (size_));				\
265 265
 	} while (0)
266 266
 
267
+#define PRINT_FIELD_HWADDR_SZ(prefix_, where_, field_, size_, hwtype_)	\
268
+	do {								\
269
+		static_assert(sizeof(((where_).field_)[0]) == 1,	\
270
+			      "hwaddress is not a byte array");	\
271
+		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
272
+		print_hwaddr("", (const uint8_t *) ((where_).field_),	\
273
+			       (size_), (hwtype_));			\
274
+	} while (0)
275
+
267 276
 #endif /* !STRACE_PRINT_FIELDS_H */

+ 103
- 3
print_mac.c View File

@@ -10,9 +10,85 @@
10 10
 #include "error_prints.h"
11 11
 #include "xstring.h"
12 12
 
13
-#ifndef MAX_ADDR_LEN
14
-# define MAX_ADDR_LEN 32
15
-#endif
13
+#define XLAT_MACROS_ONLY
14
+# include "xlat/arp_hardware_types.h"
15
+#undef XLAT_MACROS_ONLY
16
+
17
+DIAG_PUSH_IGNORE_OVERRIDE_INIT
18
+
19
+static uint8_t hwaddr_sizes[] = {
20
+	[0 ... ARPHRD_IEEE802_TR] = 255,
21
+
22
+	[ARPHRD_NETROM]     =  7 /* AX25_ADDR_LEN */,
23
+	[ARPHRD_ETHER]      =  6 /* ETH_ALEN */,
24
+	/* ARPHRD_EETHER - no actual devices in Linux */
25
+	[ARPHRD_AX25]       =  7 /* AX25_ADDR_LEN */,
26
+	/* ARPHRD_PRONET - no actual devices in Linux */
27
+	/* ARPHRD_CHAOS - no actual devices in Linux */
28
+	[ARPHRD_IEEE802]    =  6 /* FC_ALEN */,
29
+	[ARPHRD_ARCNET]     =  1 /* ARCNET_ALEN */,
30
+	/* ARPHRD_APPLETLK - no actual devices in Linux */
31
+	[ARPHRD_DLCI]       = sizeof(short),
32
+	/* ARPHRD_ATM - no explicit setting */
33
+	/* ARPHRD_METRICOM - no actual devices in Linux */
34
+	[ARPHRD_IEEE1394]   = 16 /* FWNET_ALEN */,
35
+	[ARPHRD_EUI64]      =  8 /* EUI64_ADDR_LEN */,
36
+	[ARPHRD_INFINIBAND] = 20 /* INFINIBAND_ALEN */,
37
+	[ARPHRD_SLIP]       =  0,
38
+	/* ARPHRD_CSLIP - no actual devices in Linux */
39
+	/* ARPHRD_SLIP6 - no actual devices in Linux */
40
+	/* ARPHRD_CSLIP6 - no actual devices in Linux */
41
+	/* ARPHRD_RSRVD - no actual devices in Linux */
42
+	/* ARPHRD_ADAPT - no actual devices in Linux */
43
+	[ARPHRD_ROSE]       =  5 /* ROSE_ADDR_LEN */,
44
+	[ARPHRD_X25]        =  0,
45
+	/* ARPHRD_HWX25 - no actual devices in Linux */
46
+	[ARPHRD_CAN]        =  0,
47
+	[ARPHRD_PPP]        =  0,
48
+	/* ARPHRD_CISCO - no actual devices in Linux */
49
+	/* ARPHRD_LAPB - no actual devices in Linux */
50
+	/* ARPHRD_DDCMP - no actual devices in Linux */
51
+	[ARPHRD_RAWHDLC]    =  0,
52
+	[ARPHRD_RAWIP]      =  0,
53
+	[ARPHRD_TUNNEL]     =  4 /* IPIP */,
54
+	[ARPHRD_TUNNEL6]    = 16 /* sizeof(struct in6_addr) */,
55
+	/* ARPHRD_FRAD - no actual devices in Linux */
56
+	/* ARPHRD_SKIP - no actual devices in Linux */
57
+	[ARPHRD_LOOPBACK]   =  6 /* ETH_ALEN */,
58
+	[ARPHRD_LOCALTLK]   =  1 /* LTALK_ALEN */,
59
+	[ARPHRD_FDDI]       =  6 /* FDDI_K_ALEN */,
60
+	/* ARPHRD_BIF - no actual devices in Linux */
61
+	[ARPHRD_SIT]        =  4,
62
+	[ARPHRD_IPDDP]      =  0,
63
+	[ARPHRD_IPGRE]      =  4,
64
+	[ARPHRD_PIMREG]     =  0,
65
+	[ARPHRD_HIPPI]      =  6 /* HIPPI_ALEN */,
66
+	/* ARPHRD_ASH - no actual devices in Linux */
67
+	/* ARPHRD_ECONET - no actual devices in Linux */
68
+	[ARPHRD_IRDA]       =  4 /* LAP_ALEN */,
69
+	/* ARPHRD_FCPP - no actual devices in Linux */
70
+	/* ARPHRD_FCAL - no actual devices in Linux */
71
+	/* ARPHRD_FCPL - no actual devices in Linux */
72
+	/* ARPHRD_FCFABRIC - no actual devices in Linux */
73
+	/* ARPHRD_IEEE802_TR - no actual devices in Linux */
74
+	[ARPHRD_IEEE80211]  =  6 /* ETH_ALEN */,
75
+	[ARPHRD_IEEE80211_PRISM] = 6 /* ETH_ALEN */,
76
+	[ARPHRD_IEEE80211_RADIOTAP] = 6 /* ETH_ALEN */,
77
+	[ARPHRD_IEEE802154]
78
+		= 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
79
+	[ARPHRD_IEEE802154_MONITOR]
80
+		= 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
81
+	[ARPHRD_PHONET]     =  1,
82
+	[ARPHRD_PHONET_PIPE] = 1,
83
+	[ARPHRD_CAIF]       =  0,
84
+	[ARPHRD_IP6GRE]     = 16 /* sizeof(struct in6_addr) */,
85
+	[ARPHRD_NETLINK]    =  0,
86
+	[ARPHRD_6LOWPAN]    =  8 /* EUI64_ADDR_LEN */
87
+		/* ^ or ETH_ALEN, depending on lltype */,
88
+	[ARPHRD_VSOCKMON]   =  0,
89
+};
90
+
91
+DIAG_POP_IGNORE_OVERRIDE_INIT
16 92
 
17 93
 const char *
18 94
 sprint_mac_addr(const uint8_t addr[], size_t size)
@@ -46,3 +122,27 @@ print_mac_addr(const char *prefix, const uint8_t addr[], size_t size)
46 122
 	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
47 123
 		? tprints_comment : tprints)(sprint_mac_addr(addr, size));
48 124
 }
125
+
126
+const char *
127
+sprint_hwaddr(const uint8_t hwaddr[], size_t size, uint32_t devtype)
128
+{
129
+	uint8_t sz = (devtype < ARRAY_SIZE(hwaddr_sizes))
130
+			? hwaddr_sizes[devtype] : 255;
131
+
132
+	return sprint_mac_addr(hwaddr, MIN(size, sz));
133
+}
134
+
135
+void
136
+print_hwaddr(const char *prefix, const uint8_t addr[], size_t size,
137
+	     uint32_t devtype)
138
+{
139
+	tprints(prefix);
140
+	if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
141
+		print_quoted_string((const char *) addr, size,
142
+				    QUOTE_FORCE_HEX);
143
+	if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
144
+		return;
145
+	(xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
146
+		? tprints_comment : tprints)(sprint_hwaddr(addr, size,
147
+							   devtype));
148
+}

+ 3
- 82
sock.c View File

@@ -37,8 +37,6 @@ print_ifname(const char *ifname)
37 37
 	print_quoted_string(ifname, IFNAMSIZ + 1, QUOTE_0_TERMINATED);
38 38
 }
39 39
 
40
-DIAG_PUSH_IGNORE_OVERRIDE_INIT
41
-
42 40
 static void
43 41
 print_ifreq(struct tcb *const tcp, const unsigned int code,
44 42
 	    const kernel_ulong_t arg, const struct_ifreq *const ifr)
@@ -62,88 +60,13 @@ print_ifreq(struct tcb *const tcp, const unsigned int code,
62 60
 		break;
63 61
 	case SIOCSIFHWADDR:
64 62
 	case SIOCGIFHWADDR: {
65
-		static uint8_t hwaddr_sizes[] = {
66
-			[0 ... ARPHRD_IEEE802_TR] = 255,
67
-
68
-			[ARPHRD_NETROM]     =  7 /* AX25_ADDR_LEN */,
69
-			[ARPHRD_ETHER]      =  6 /* ETH_ALEN */,
70
-			/* ARPHRD_EETHER - no actual devices in Linux */
71
-			[ARPHRD_AX25]       =  7 /* AX25_ADDR_LEN */,
72
-			/* ARPHRD_PRONET - no actual devices in Linux */
73
-			/* ARPHRD_CHAOS - no actual devices in Linux */
74
-			[ARPHRD_IEEE802]    =  6 /* FC_ALEN */,
75
-			[ARPHRD_ARCNET]     =  1 /* ARCNET_ALEN */,
76
-			/* ARPHRD_APPLETLK - no actual devices in Linux */
77
-			[ARPHRD_DLCI]       = sizeof(short),
78
-			/* ARPHRD_ATM - no explicit setting */
79
-			/* ARPHRD_METRICOM - no actual devices in Linux */
80
-			[ARPHRD_IEEE1394]   = 16 /* FWNET_ALEN */,
81
-			[ARPHRD_EUI64]      =  8 /* EUI64_ADDR_LEN */,
82
-			[ARPHRD_INFINIBAND] = 20 /* INFINIBAND_ALEN */,
83
-			[ARPHRD_SLIP]       =  0,
84
-			/* ARPHRD_CSLIP - no actual devices in Linux */
85
-			/* ARPHRD_SLIP6 - no actual devices in Linux */
86
-			/* ARPHRD_CSLIP6 - no actual devices in Linux */
87
-			/* ARPHRD_RSRVD - no actual devices in Linux */
88
-			/* ARPHRD_ADAPT - no actual devices in Linux */
89
-			[ARPHRD_ROSE]       =  5 /* ROSE_ADDR_LEN */,
90
-			[ARPHRD_X25]        =  0,
91
-			/* ARPHRD_HWX25 - no actual devices in Linux */
92
-			[ARPHRD_CAN]        =  0,
93
-			[ARPHRD_PPP]        =  0,
94
-			/* ARPHRD_CISCO - no actual devices in Linux */
95
-			/* ARPHRD_LAPB - no actual devices in Linux */
96
-			/* ARPHRD_DDCMP - no actual devices in Linux */
97
-			[ARPHRD_RAWHDLC]    =  0,
98
-			[ARPHRD_RAWIP]      =  0,
99
-			[ARPHRD_TUNNEL]     =  4 /* IPIP */,
100
-			[ARPHRD_TUNNEL6]    = 16 /* sizeof(struct in6_addr) */,
101
-			/* ARPHRD_FRAD - no actual devices in Linux */
102
-			/* ARPHRD_SKIP - no actual devices in Linux */
103
-			[ARPHRD_LOOPBACK]   =  6 /* ETH_ALEN */,
104
-			[ARPHRD_LOCALTLK]   =  1 /* LTALK_ALEN */,
105
-			[ARPHRD_FDDI]       =  6 /* FDDI_K_ALEN */,
106
-			/* ARPHRD_BIF - no actual devices in Linux */
107
-			[ARPHRD_SIT]        =  4,
108
-			[ARPHRD_IPDDP]      =  0,
109
-			[ARPHRD_IPGRE]      =  4,
110
-			[ARPHRD_PIMREG]     =  0,
111
-			[ARPHRD_HIPPI]      =  6 /* HIPPI_ALEN */,
112
-			/* ARPHRD_ASH - no actual devices in Linux */
113
-			/* ARPHRD_ECONET - no actual devices in Linux */
114
-			[ARPHRD_IRDA]       =  4 /* LAP_ALEN */,
115
-			/* ARPHRD_FCPP - no actual devices in Linux */
116
-			/* ARPHRD_FCAL - no actual devices in Linux */
117
-			/* ARPHRD_FCPL - no actual devices in Linux */
118
-			/* ARPHRD_FCFABRIC - no actual devices in Linux */
119
-			/* ARPHRD_IEEE802_TR - no actual devices in Linux */
120
-			[ARPHRD_IEEE80211]  =  6 /* ETH_ALEN */,
121
-			[ARPHRD_IEEE80211_PRISM] = 6 /* ETH_ALEN */,
122
-			[ARPHRD_IEEE80211_RADIOTAP] = 6 /* ETH_ALEN */,
123
-			[ARPHRD_IEEE802154]
124
-				= 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
125
-			[ARPHRD_IEEE802154_MONITOR]
126
-				= 8 /* IEEE802154_EXTENDED_ADDR_LEN */,
127
-			[ARPHRD_PHONET]     =  1,
128
-			[ARPHRD_PHONET_PIPE] = 1,
129
-			[ARPHRD_CAIF]       =  0,
130
-			[ARPHRD_IP6GRE]     = 16 /* sizeof(struct in6_addr) */,
131
-			[ARPHRD_NETLINK]    =  0,
132
-			[ARPHRD_6LOWPAN]    =  8 /* EUI64_ADDR_LEN */
133
-				/* ^ or ETH_ALEN, depending on lltype */,
134
-			[ARPHRD_VSOCKMON]   =  0,
135
-		};
136
-
137
-		uint16_t proto = ifr->ifr_hwaddr.sa_family;
138
-		uint8_t sz = (proto < ARRAY_SIZE(hwaddr_sizes))
139
-				? hwaddr_sizes[proto] : 255;
140
-
141 63
 		PRINT_FIELD_XVAL_SORTED_SIZED("ifr_hwaddr={", ifr->ifr_hwaddr,
142 64
 					      sa_family, arp_hardware_types,
143 65
 					      arp_hardware_types_size,
144 66
 					      "ARPHRD_???");
145
-		PRINT_FIELD_MAC_SZ(", ", ifr->ifr_hwaddr, sa_data,
146
-				   MIN(sizeof(ifr->ifr_hwaddr.sa_data), sz));
67
+		PRINT_FIELD_HWADDR_SZ(", ", ifr->ifr_hwaddr, sa_data,
68
+				      sizeof(ifr->ifr_hwaddr.sa_data),
69
+				      ifr->ifr_hwaddr.sa_family);
147 70
 		tprints("}");
148 71
 		break;
149 72
 	}
@@ -184,8 +107,6 @@ print_ifreq(struct tcb *const tcp, const unsigned int code,
184 107
 	}
185 108
 }
186 109
 
187
-DIAG_POP_IGNORE_OVERRIDE_INIT
188
-
189 110
 static unsigned int
190 111
 print_ifc_len(int len)
191 112
 {

Loading…
Cancel
Save