| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 
 | /*
 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
 * Copyright (c) 2017-2018 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include "tests.h"
#include <stdio.h>
#include <stddef.h>
#include "test_nlattr.h"
#include <linux/if.h>
#include <linux/if_arp.h>
#ifdef HAVE_LINUX_IF_LINK_H
# include <linux/if_link.h>
#endif
#include <linux/rtnetlink.h>
#ifndef IFLA_LINKINFO
# define IFLA_LINKINFO 18
#endif
#ifndef IFLA_VF_PORTS
# define IFLA_VF_PORTS 24
#endif
#define IFLA_LINK_NETNSID 37
#define IFLA_EVENT 44
#ifndef IFLA_INFO_KIND
# define IFLA_INFO_KIND 1
#endif
#ifndef IFLA_VF_PORT
# define IFLA_VF_PORT 1
#endif
static void
init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
{
	SET_STRUCT(struct nlmsghdr, nlh,
		.nlmsg_len = msg_len,
		.nlmsg_type = RTM_GETLINK,
		.nlmsg_flags = NLM_F_DUMP
	);
	struct ifinfomsg *const msg = NLMSG_DATA(nlh);
	SET_STRUCT(struct ifinfomsg, msg,
		.ifi_family = AF_UNIX,
		.ifi_type = ARPHRD_LOOPBACK,
		.ifi_index = ifindex_lo(),
		.ifi_flags = IFF_UP,
	);
}
static void
print_ifinfomsg(const unsigned int msg_len)
{
	printf("{len=%u, type=RTM_GETLINK, flags=NLM_F_DUMP"
	       ", seq=0, pid=0}, {ifi_family=AF_UNIX"
	       ", ifi_type=ARPHRD_LOOPBACK"
	       ", ifi_index=" IFINDEX_LO_STR
	       ", ifi_flags=IFF_UP, ifi_change=0}",
	       msg_len);
}
int
main(void)
{
	skip_if_unavailable("/proc/self/fd/");
	static const struct rtnl_link_stats st = {
		.rx_packets = 0xabcdefac,
		.tx_packets = 0xbcdacdab,
		.rx_bytes = 0xcdbafaab,
		.tx_bytes = 0xdafabadb,
		.rx_errors = 0xeabcdaeb,
		.tx_errors = 0xfefabeab,
		.rx_dropped = 0xadbafafb,
		.tx_dropped = 0xbdffabda,
		.multicast = 0xcdabdfea,
		.collisions = 0xefadbaeb,
		.rx_length_errors = 0xfabffabd,
		.rx_over_errors = 0xafbafabc,
		.rx_crc_errors = 0xbfdabdad,
		.rx_frame_errors = 0xcfdabfad,
		.rx_fifo_errors = 0xddfdebad,
		.rx_missed_errors = 0xefabdcba,
		.tx_aborted_errors = 0xefdadbfa,
		.tx_carrier_errors = 0xfaefbada,
		.tx_fifo_errors = 0xaebdffab,
		.tx_heartbeat_errors = 0xbadebaaf,
		.tx_window_errors = 0xcdafbada,
		.rx_compressed = 0xdeffadbd,
		.tx_compressed = 0xefdadfab
	};
	const int fd = create_nl_socket(NETLINK_ROUTE);
	const unsigned int hdrlen = sizeof(struct ifinfomsg);
	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
				   NLA_HDRLEN + sizeof(st));
	static char pattern[4096];
	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
	char nla_type_str[256];
	sprintf(nla_type_str, "%#x /* IFLA_??? */", nla_type);
	TEST_NLATTR_(fd, nlh0, hdrlen,
		     init_ifinfomsg, print_ifinfomsg,
		     nla_type, nla_type_str,
		     4, pattern, 4,
		     print_quoted_hex(pattern, 4));
	const int32_t netnsid = 0xacbdabda;
	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
			   init_ifinfomsg, print_ifinfomsg,
			   IFLA_LINK_NETNSID, pattern, netnsid,
			   printf("%d", netnsid));
	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
			   init_ifinfomsg, print_ifinfomsg,
			   IFLA_STATS, pattern, st,
			   PRINT_FIELD_U("{", st, rx_packets);
			   PRINT_FIELD_U(", ", st, tx_packets);
			   PRINT_FIELD_U(", ", st, rx_bytes);
			   PRINT_FIELD_U(", ", st, tx_bytes);
			   PRINT_FIELD_U(", ", st, rx_errors);
			   PRINT_FIELD_U(", ", st, tx_errors);
			   PRINT_FIELD_U(", ", st, rx_dropped);
			   PRINT_FIELD_U(", ", st, tx_dropped);
			   PRINT_FIELD_U(", ", st, multicast);
			   PRINT_FIELD_U(", ", st, collisions);
			   PRINT_FIELD_U(", ", st, rx_length_errors);
			   PRINT_FIELD_U(", ", st, rx_over_errors);
			   PRINT_FIELD_U(", ", st, rx_crc_errors);
			   PRINT_FIELD_U(", ", st, rx_frame_errors);
			   PRINT_FIELD_U(", ", st, rx_fifo_errors);
			   PRINT_FIELD_U(", ", st, rx_missed_errors);
			   PRINT_FIELD_U(", ", st, tx_aborted_errors);
			   PRINT_FIELD_U(", ", st, tx_carrier_errors);
			   PRINT_FIELD_U(", ", st, tx_fifo_errors);
			   PRINT_FIELD_U(", ", st, tx_heartbeat_errors);
			   PRINT_FIELD_U(", ", st, tx_window_errors);
			   PRINT_FIELD_U(", ", st, rx_compressed);
			   PRINT_FIELD_U(", ", st, tx_compressed);
#ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER
			   PRINT_FIELD_U(", ", st, rx_nohandler);
#endif
			   printf("}"));
#ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER
	const unsigned int sizeof_stats =
		offsetofend(struct rtnl_link_stats, tx_compressed);
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_STATS, sizeof_stats, &st, sizeof_stats,
		    PRINT_FIELD_U("{", st, rx_packets);
		    PRINT_FIELD_U(", ", st, tx_packets);
		    PRINT_FIELD_U(", ", st, rx_bytes);
		    PRINT_FIELD_U(", ", st, tx_bytes);
		    PRINT_FIELD_U(", ", st, rx_errors);
		    PRINT_FIELD_U(", ", st, tx_errors);
		    PRINT_FIELD_U(", ", st, rx_dropped);
		    PRINT_FIELD_U(", ", st, tx_dropped);
		    PRINT_FIELD_U(", ", st, multicast);
		    PRINT_FIELD_U(", ", st, collisions);
		    PRINT_FIELD_U(", ", st, rx_length_errors);
		    PRINT_FIELD_U(", ", st, rx_over_errors);
		    PRINT_FIELD_U(", ", st, rx_crc_errors);
		    PRINT_FIELD_U(", ", st, rx_frame_errors);
		    PRINT_FIELD_U(", ", st, rx_fifo_errors);
		    PRINT_FIELD_U(", ", st, rx_missed_errors);
		    PRINT_FIELD_U(", ", st, tx_aborted_errors);
		    PRINT_FIELD_U(", ", st, tx_carrier_errors);
		    PRINT_FIELD_U(", ", st, tx_fifo_errors);
		    PRINT_FIELD_U(", ", st, tx_heartbeat_errors);
		    PRINT_FIELD_U(", ", st, tx_window_errors);
		    PRINT_FIELD_U(", ", st, rx_compressed);
		    PRINT_FIELD_U(", ", st, tx_compressed);
		    printf("}"));
#endif /* HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER */
	static const struct rtnl_link_ifmap map = {
		.mem_start = 0xadcbefedefbcdedb,
		.mem_end = 0xefcbeabdecdcdefa,
		.base_addr = 0xaddbeabdfaacdbae,
		.irq = 0xefaf,
		.dma = 0xab,
		.port = 0xcd
	};
	const unsigned int sizeof_ifmap =
		offsetofend(struct rtnl_link_ifmap, port);
	const unsigned int plen = sizeof_ifmap - 1 > DEFAULT_STRLEN
				  ? DEFAULT_STRLEN
				  : (int) sizeof_ifmap - 1;
	/* len < sizeof_ifmap */
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_MAP, plen, pattern, plen,
		    print_quoted_hex(pattern, plen));
	/* short read of sizeof_ifmap */
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap - 1,
		    printf("%p", RTA_DATA(TEST_NLATTR_nla)));
	/* sizeof_ifmap */
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap,
		    PRINT_FIELD_X("{", map, mem_start);
		    PRINT_FIELD_X(", ", map, mem_end);
		    PRINT_FIELD_X(", ", map, base_addr);
		    PRINT_FIELD_U(", ", map, irq);
		    PRINT_FIELD_U(", ", map, dma);
		    PRINT_FIELD_U(", ", map, port);
		    printf("}"));
#ifdef HAVE_STRUCT_RTNL_LINK_STATS64
	static const struct rtnl_link_stats64 st64 = {
		.rx_packets = 0xadcbefedefbcdedb,
		.tx_packets = 0xbdabdedabdcdeabd,
		.rx_bytes = 0xcdbaefbaeadfabec,
		.tx_bytes = 0xdbaedbafabbeacdb,
		.rx_errors = 0xefabfdaefabaefab,
		.tx_errors = 0xfaebfabfabbaeabf,
		.rx_dropped = 0xacdbaedbadbabeba,
		.tx_dropped = 0xbcdeffebdabeadbe,
		.multicast = 0xeeffbaeabaeffabe,
		.collisions = 0xffbaefcefbafacef,
		.rx_length_errors = 0xaabbdeabceffdecb,
		.rx_over_errors = 0xbbdcdadebadeaeed,
		.rx_crc_errors= 0xccdeabecefaedbef,
		.rx_frame_errors = 0xddbedaedebcedaef,
		.rx_fifo_errors = 0xeffbadefafdaeaab,
		.rx_missed_errors = 0xfefaebccceadeecd,
		.tx_aborted_errors = 0xabcdadefcdadef,
		.tx_carrier_errors = 0xbccdafaeeaaefe,
		.tx_fifo_errors = 0xcddefdbedeadce,
		.tx_heartbeat_errors = 0xedaededdadcdea,
		.tx_window_errors = 0xfdacdeaccedcda,
		.rx_compressed = 0xacdbbcacdbccef,
		.tx_compressed = 0xbcdadefcdedfea
	};
	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
			   init_ifinfomsg, print_ifinfomsg,
			   IFLA_STATS64, pattern, st64,
			   PRINT_FIELD_U("{", st64, rx_packets);
			   PRINT_FIELD_U(", ", st64, tx_packets);
			   PRINT_FIELD_U(", ", st64, rx_bytes);
			   PRINT_FIELD_U(", ", st64, tx_bytes);
			   PRINT_FIELD_U(", ", st64, rx_errors);
			   PRINT_FIELD_U(", ", st64, tx_errors);
			   PRINT_FIELD_U(", ", st64, rx_dropped);
			   PRINT_FIELD_U(", ", st64, tx_dropped);
			   PRINT_FIELD_U(", ", st64, multicast);
			   PRINT_FIELD_U(", ", st64, collisions);
			   PRINT_FIELD_U(", ", st64, rx_length_errors);
			   PRINT_FIELD_U(", ", st64, rx_over_errors);
			   PRINT_FIELD_U(", ", st64, rx_crc_errors);
			   PRINT_FIELD_U(", ", st64, rx_frame_errors);
			   PRINT_FIELD_U(", ", st64, rx_fifo_errors);
			   PRINT_FIELD_U(", ", st64, rx_missed_errors);
			   PRINT_FIELD_U(", ", st64, tx_aborted_errors);
			   PRINT_FIELD_U(", ", st64, tx_carrier_errors);
			   PRINT_FIELD_U(", ", st64, tx_fifo_errors);
			   PRINT_FIELD_U(", ", st64, tx_heartbeat_errors);
			   PRINT_FIELD_U(", ", st64, tx_window_errors);
			   PRINT_FIELD_U(", ", st64, rx_compressed);
			   PRINT_FIELD_U(", ", st64, tx_compressed);
#ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER
			   PRINT_FIELD_U(", ", st64, rx_nohandler);
#endif
			   printf("}"));
#ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER
	const unsigned int sizeof_stats64 =
		offsetofend(struct rtnl_link_stats64, tx_compressed);
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_STATS64, sizeof_stats64, &st64, sizeof_stats64,
		    PRINT_FIELD_U("{", st64, rx_packets);
		    PRINT_FIELD_U(", ", st64, tx_packets);
		    PRINT_FIELD_U(", ", st64, rx_bytes);
		    PRINT_FIELD_U(", ", st64, tx_bytes);
		    PRINT_FIELD_U(", ", st64, rx_errors);
		    PRINT_FIELD_U(", ", st64, tx_errors);
		    PRINT_FIELD_U(", ", st64, rx_dropped);
		    PRINT_FIELD_U(", ", st64, tx_dropped);
		    PRINT_FIELD_U(", ", st64, multicast);
		    PRINT_FIELD_U(", ", st64, collisions);
		    PRINT_FIELD_U(", ", st64, rx_length_errors);
		    PRINT_FIELD_U(", ", st64, rx_over_errors);
		    PRINT_FIELD_U(", ", st64, rx_crc_errors);
		    PRINT_FIELD_U(", ", st64, rx_frame_errors);
		    PRINT_FIELD_U(", ", st64, rx_fifo_errors);
		    PRINT_FIELD_U(", ", st64, rx_missed_errors);
		    PRINT_FIELD_U(", ", st64, tx_aborted_errors);
		    PRINT_FIELD_U(", ", st64, tx_carrier_errors);
		    PRINT_FIELD_U(", ", st64, tx_fifo_errors);
		    PRINT_FIELD_U(", ", st64, tx_heartbeat_errors);
		    PRINT_FIELD_U(", ", st64, tx_window_errors);
		    PRINT_FIELD_U(", ", st64, rx_compressed);
		    PRINT_FIELD_U(", ", st64, tx_compressed);
		    printf("}"));
#endif /* HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER */
#endif /* HAVE_STRUCT_RTNL_LINK_STATS64 */
	struct nlattr nla = {
		.nla_len = sizeof(nla),
		.nla_type = IFLA_INFO_KIND,
	};
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_LINKINFO, sizeof(nla), &nla, sizeof(nla),
		    printf("{nla_len=%u, nla_type=IFLA_INFO_KIND}",
			   nla.nla_len));
	nla.nla_type = IFLA_VF_PORT;
	TEST_NLATTR(fd, nlh0, hdrlen,
		    init_ifinfomsg, print_ifinfomsg,
		    IFLA_VF_PORTS, sizeof(nla), &nla, sizeof(nla),
		    printf("{nla_len=%u, nla_type=IFLA_VF_PORT}",
			   nla.nla_len));
	static const struct {
		uint32_t val;
		const char *str;
	} ifla_events[] = {
		{ 0, "IFLA_EVENT_NONE" },
		{ 6, "IFLA_EVENT_BONDING_OPTIONS" },
		{ ARG_STR(0x7) " /* IFLA_EVENT_??? */" },
		{ ARG_STR(0xdeadfeed) " /* IFLA_EVENT_??? */" },
	};
	for (size_t i = 0; i < ARRAY_SIZE(ifla_events); i++) {
		TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
				   init_ifinfomsg, print_ifinfomsg,
				   IFLA_EVENT, pattern, ifla_events[i].val,
				   printf("%s", ifla_events[i].str));
	}
	puts("+++ exited with 0 +++");
	return 0;
}
 |