eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

nlmon.c (228985B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <ctype.h>
     31 #include <unistd.h>
     32 #include <time.h>
     33 #include <sys/ioctl.h>
     34 #include <sys/socket.h>
     35 #include <arpa/inet.h>
     36 #include <net/ethernet.h>
     37 #include <netinet/ether.h>
     38 #include <linux/if.h>
     39 #include <linux/if_packet.h>
     40 #include <linux/if_ether.h>
     41 #include <linux/if_link.h>
     42 #include <linux/netlink.h>
     43 #include <linux/genetlink.h>
     44 #include <linux/rtnetlink.h>
     45 #include <linux/filter.h>
     46 #include <linux/limits.h>
     47 #include <ell/ell.h>
     48 
     49 #ifndef ARPHRD_NETLINK
     50 #define ARPHRD_NETLINK	824
     51 #endif
     52 
     53 #ifndef RMNET_FLAGS_INGRESS_MAP_CKSUMV5
     54 #define RMNET_FLAGS_INGRESS_MAP_CKSUMV5 (1U << 4)
     55 #endif
     56 
     57 #ifndef RMNET_FLAGS_EGRESS_MAP_CKSUMV5
     58 #define RMNET_FLAGS_EGRESS_MAP_CKSUMV5 (1U << 5)
     59 #endif
     60 
     61 #include "linux/nl80211.h"
     62 
     63 #include "ell/useful.h"
     64 #include "src/ie.h"
     65 #include "src/mpdu.h"
     66 #include "src/eapol.h"
     67 #include "src/util.h"
     68 #include "src/p2putil.h"
     69 #include "src/nl80211cmd.h"
     70 #include "monitor/pcap.h"
     71 #include "monitor/display.h"
     72 #include "monitor/nlmon.h"
     73 #include "src/anqputil.h"
     74 
     75 #define COLOR_TIMESTAMP		COLOR_YELLOW
     76 
     77 #define COLOR_REQUEST		COLOR_BLUE
     78 #define COLOR_RESPONSE		COLOR_MAGENTA
     79 #define COLOR_COMPLETE		COLOR_MAGENTA
     80 #define COLOR_RESULT		COLOR_MAGENTA
     81 #define COLOR_EVENT		COLOR_CYAN
     82 
     83 /* BSS Capabilities */
     84 #define BSS_CAPABILITY_ESS		(1<<0)
     85 #define BSS_CAPABILITY_IBSS		(1<<1)
     86 #define BSS_CAPABILITY_CF_POLLABLE	(1<<2)
     87 #define BSS_CAPABILITY_CF_POLL_REQUEST	(1<<3)
     88 #define BSS_CAPABILITY_PRIVACY		(1<<4)
     89 #define BSS_CAPABILITY_SHORT_PREAMBLE	(1<<5)
     90 #define BSS_CAPABILITY_PBCC		(1<<6)
     91 #define BSS_CAPABILITY_CHANNEL_AGILITY	(1<<7)
     92 #define BSS_CAPABILITY_SPECTRUM_MGMT	(1<<8)
     93 #define BSS_CAPABILITY_QOS		(1<<9)
     94 #define BSS_CAPABILITY_SHORT_SLOT_TIME	(1<<10)
     95 #define BSS_CAPABILITY_APSD		(1<<11)
     96 #define BSS_CAPABILITY_DSSS_OFDM	(1<<13)
     97 
     98 #define BYTES_PER_MB			1000000
     99 
    100 struct nlmon *cur_nlmon;
    101 
    102 enum msg_type {
    103 	MSG_REQUEST,
    104 	MSG_RESPONSE,
    105 	MSG_COMPLETE,
    106 	MSG_RESULT,
    107 	MSG_EVENT,
    108 };
    109 
    110 struct nlmon {
    111 	uint16_t id;
    112 	struct l_io *pae_io;
    113 	struct l_queue *req_list;
    114 	struct pcap *pcap;
    115 	bool nortnl;
    116 	bool nowiphy;
    117 	bool noscan;
    118 	bool noies;
    119 	bool read;
    120 	enum time_format time_format;
    121 
    122 	char *file_prefix;
    123 	unsigned int file_idx;
    124 	unsigned int max_files;
    125 	unsigned int max_size;
    126 };
    127 
    128 struct nlmon_req {
    129 	uint32_t seq;
    130 	uint32_t pid;
    131 	uint16_t flags;
    132 	uint8_t cmd;
    133 	uint8_t version;
    134 };
    135 
    136 typedef void (*attr_func_t) (unsigned int level, const char *label,
    137 					const void *data, uint16_t size);
    138 enum attr_type {
    139 	ATTR_UNSPEC,
    140 	ATTR_FLAG,
    141 	ATTR_U8,
    142 	ATTR_U16,
    143 	ATTR_U32,
    144 	ATTR_U64,
    145 	ATTR_S8,
    146 	ATTR_S32,
    147 	ATTR_S64,
    148 	ATTR_STRING,
    149 	ATTR_ADDRESS,
    150 	ATTR_BINARY,
    151 	ATTR_NESTED,
    152 	ATTR_ARRAY,
    153 	ATTR_FLAG_OR_U16,
    154 	ATTR_CUSTOM,
    155 };
    156 
    157 struct attr_entry {
    158 	uint16_t attr;
    159 	const char *str;
    160 	enum attr_type type;
    161 
    162 	struct {
    163 		const struct attr_entry *nested;
    164 		enum attr_type array_type;
    165 		attr_func_t function;
    166 	};
    167 };
    168 
    169 static void print_attributes(int indent, const struct attr_entry *table,
    170 						const void *buf, uint32_t len);
    171 
    172 struct flag_names {
    173 	uint16_t flag;
    174 	const char *name;
    175 };
    176 
    177 struct wlan_iface {
    178 	int index;
    179 };
    180 
    181 static struct l_hashmap *wlan_iface_list = NULL;
    182 
    183 static void wlan_iface_list_free(void *data)
    184 {
    185 	struct wlan_iface *iface = data;
    186 
    187 	l_free(iface);
    188 }
    189 
    190 static void nlmon_req_free(void *data)
    191 {
    192 	struct nlmon_req *req = data;
    193 
    194 	l_free(req);
    195 }
    196 
    197 static time_t time_offset = ((time_t) -1);
    198 static enum time_format time_format;
    199 
    200 static inline void update_time_offset(const struct timeval *tv,
    201 					enum time_format tf)
    202 {
    203 	if (tv && time_offset == ((time_t) -1)) {
    204 		time_offset = tv->tv_sec;
    205 		time_format = tf;
    206 	}
    207 }
    208 
    209 #define print_indent(indent, color1, prefix, title, color2, fmt, args...) \
    210 do { \
    211 	printf("%*c%s%s%s%s" fmt "%s\n", (indent), ' ', \
    212 		use_color() ? (color1) : "", prefix, title, \
    213 		use_color() ? (color2) : "", ## args, \
    214 		use_color() ? COLOR_OFF : ""); \
    215 } while (0)
    216 
    217 #define print_text(color, fmt, args...) \
    218 		print_indent(4, COLOR_OFF, "", "", color, fmt, ## args)
    219 
    220 #define print_field(fmt, args...) \
    221 		print_indent(4, COLOR_OFF, "", "", COLOR_OFF, fmt, ## args)
    222 
    223 #define print_attr(level, fmt, args...) \
    224 		print_indent(4 + (level) * 4, COLOR_OFF, "", "", COLOR_OFF, \
    225 								fmt, ## args)
    226 
    227 #define print_attr_color(level, color, fmt, args...) \
    228 		print_indent(4 + (level) * 4, COLOR_OFF, "", "", color, \
    229 								fmt, ## args)
    230 
    231 #define print_space(x) printf("%*c", (x), ' ')
    232 
    233 static void print_packet(const struct timeval *tv, char ident,
    234 					const char *color, const char *label,
    235 					const char *text, const char *extra)
    236 {
    237 	int col = num_columns();
    238 	char line[256], ts_str[64];
    239 	int n, ts_len = 0, ts_pos = 0, len = 0, pos = 0;
    240 
    241 	if (tv) {
    242 		struct tm *tm;
    243 
    244 		if (use_color()) {
    245 			n = sprintf(ts_str + ts_pos, "%s", COLOR_TIMESTAMP);
    246 			if (n > 0)
    247 				ts_pos += n;
    248 		}
    249 
    250 		switch (time_format) {
    251 		case TIME_FORMAT_DELTA:
    252 			n = sprintf(ts_str + ts_pos, " %" PRId64 ".%06" PRId64,
    253 					(int64_t)tv->tv_sec - time_offset,
    254 					(int64_t)tv->tv_usec);
    255 			break;
    256 		case TIME_FORMAT_UTC:
    257 			tm = gmtime(&tv->tv_sec);
    258 			if (!tm) {
    259 				n = sprintf(ts_str + ts_pos, "%s",
    260 						"Time error");
    261 				break;
    262 			}
    263 
    264 			n = strftime(ts_str + ts_pos, sizeof(ts_str) - ts_pos,
    265 					"%b %d %H:%M:%S", tm);
    266 			break;
    267 		default:
    268 			/* Should never happen */
    269 			printf("Unknown time format");
    270 			l_main_quit();
    271 			return;
    272 		}
    273 
    274 		if (n > 0) {
    275 			ts_pos += n;
    276 			ts_len += n;
    277 		}
    278 	}
    279 
    280 	if (use_color()) {
    281 		n = sprintf(ts_str + ts_pos, "%s", COLOR_OFF);
    282 		if (n > 0)
    283 			ts_pos += n;
    284 	}
    285 
    286 	if (use_color()) {
    287 		n = sprintf(line + pos, "%s", color);
    288 		if (n > 0)
    289 			pos += n;
    290 	}
    291 
    292 	n = sprintf(line + pos, "%c %s", ident, label);
    293 	if (n > 0) {
    294 		pos += n;
    295 		len += n;
    296 	}
    297 
    298 	if (text) {
    299 		int extra_len = extra ? strlen(extra) : 0;
    300 		int max_len = col - len - extra_len - ts_len - 3;
    301 
    302 		n = snprintf(line + pos, max_len + 1, ": %s", text);
    303 		if (n > max_len) {
    304 			line[pos + max_len - 1] = '.';
    305 			line[pos + max_len - 2] = '.';
    306 			if (line[pos + max_len - 3] == ' ')
    307 				line[pos + max_len - 3] = '.';
    308 
    309 			n = max_len;
    310 		}
    311 
    312 		if (n > 0) {
    313 			pos += n;
    314 			len += n;
    315 		}
    316 	}
    317 
    318 	if (use_color()) {
    319 		n = sprintf(line + pos, "%s", COLOR_OFF);
    320 		if (n > 0)
    321 			pos += n;
    322 	}
    323 
    324 	if (extra) {
    325 		n = sprintf(line + pos, " %s", extra);
    326 		if (n > 0) {
    327 			pos += n;
    328 			len += n;
    329 		}
    330 	}
    331 
    332 	if (ts_len > 0) {
    333 		printf("%s", line);
    334 		if (len < col)
    335 			print_space(col - len - ts_len - 1);
    336 		printf("%s%s\n", use_color() ? COLOR_TIMESTAMP : "", ts_str);
    337 	} else
    338 		printf("%s\n", line);
    339 }
    340 
    341 static void print_hexdump(unsigned int level,
    342 				const unsigned char *buf, uint16_t len)
    343 {
    344 	static const char hexdigits[] = "0123456789abcdef";
    345 	char str[68];
    346 	uint16_t i;
    347 
    348 	if (!len)
    349 		return;
    350 
    351 	for (i = 0; i < len; i++) {
    352 		str[((i % 16) * 3) + 0] = hexdigits[buf[i] >> 4];
    353 		str[((i % 16) * 3) + 1] = hexdigits[buf[i] & 0xf];
    354 		str[((i % 16) * 3) + 2] = ' ';
    355 		str[(i % 16) + 49] = isprint(buf[i]) ? buf[i] : '.';
    356 
    357 		if ((i + 1) % 16 == 0) {
    358 			str[47] = ' ';
    359 			str[48] = ' ';
    360 			str[65] = '\0';
    361 			print_attr_color(level, COLOR_WHITE, "%s", str);
    362 			str[0] = ' ';
    363 		}
    364 	}
    365 
    366 	if (i % 16 > 0) {
    367 		uint16_t j;
    368 		for (j = (i % 16); j < 16; j++) {
    369 			str[(j * 3) + 0] = ' ';
    370 			str[(j * 3) + 1] = ' ';
    371 			str[(j * 3) + 2] = ' ';
    372 			str[j + 49] = ' ';
    373 		}
    374 		str[47] = ' ';
    375 		str[48] = ' ';
    376 		str[65] = '\0';
    377 		print_attr_color(level, COLOR_WHITE, "%s", str);
    378 	}
    379 }
    380 
    381 static void print_address(unsigned int level, const char *label,
    382 					const unsigned char address[6])
    383 {
    384 	char addr[18];
    385 
    386 	snprintf(addr, sizeof(addr), "%02X:%02X:%02X:%02X:%02X:%02X",
    387 					address[0], address[1], address[2],
    388 					address[3], address[4], address[5]);
    389 
    390 	print_attr(level, "%s %s", label, addr);
    391 }
    392 
    393 static const struct {
    394 	const uint8_t oui[3];
    395 	const char *str;
    396 } oui_table[] = {
    397 	{ { 0x00, 0x03, 0x7f }, "Atheros"		},
    398 	{ { 0x00, 0x03, 0x93 }, "Apple"			},
    399 	{ { 0x00, 0x04, 0x0e }, "AVM"			},
    400 	{ { 0x00, 0x0f, 0xac }, "IEEE 802.11"		},
    401 	{ { 0x00, 0x10, 0x18 }, "Broadcom"		},
    402 	{ { 0x00, 0x17, 0xf2 }, "Apple"			},
    403 	{ { 0x00, 0x40, 0x96 }, "Cisco Systems"		},
    404 	{ { 0x00, 0x50, 0xf2 }, "Microsoft"		},
    405 	{ { 0x00, 0x90, 0x4c }, "Epigram"		},
    406 	{ { 0x50, 0x6f, 0x9a }, "Wi-Fi Alliance"	},
    407 	{ { 0x00, 0x18, 0x0a }, "Cisco Meraki"		},
    408 	{ }
    409 };
    410 
    411 static void print_ie_error(unsigned int level, const char *label,
    412 				uint16_t len, int err)
    413 {
    414 	print_attr(level, "Error decoding %s IE len %d: %s (%d)", label, len,
    415 			strerror(-err), err);
    416 }
    417 
    418 static void print_ie_ssid(unsigned int level, const char *label,
    419 				const void *data, uint16_t size)
    420 {
    421 	print_attr(level, "%s: %s", label, util_ssid_to_utf8(size, data));
    422 }
    423 
    424 static void print_ie_rate(unsigned int level, const char *label,
    425 				const void *data, uint16_t size)
    426 {
    427 	uint8_t *rate = (uint8_t *)data;
    428 	int pos = 0, i = 0;
    429 	char str[128];
    430 
    431 	if (!size) {
    432 		print_ie_error(level, label, size, -EINVAL);
    433 		return;
    434 	}
    435 
    436 	print_attr(level, "%s:", label);
    437 
    438 	while (i < size) {
    439 		bool mandatory = (rate[i] & 0x80);
    440 
    441 		if (rate[i] == 0xff) {
    442 			print_attr(level + 1, "BSS membership HT_PHY");
    443 			i++;
    444 			continue;
    445 		}
    446 
    447 		pos += snprintf(&str[pos], sizeof(str) - pos, "%.1f%s ",
    448 				(rate[i] & 127) * 0.5, mandatory? "(B)": "");
    449 
    450 		i++;
    451 
    452 		if (i % 8 && i != size)
    453 			continue;
    454 
    455 		if (pos) {
    456 			pos += snprintf(&str[pos], sizeof(str) - pos, "Mbit/s");
    457 			print_attr(level + 1, "%s", str);
    458 			pos = 0;
    459 		}
    460 
    461 	}
    462 }
    463 
    464 static void print_ie_ds(unsigned int level, const char *label,
    465 				const void *data, uint16_t size)
    466 {
    467 	uint8_t *channel = (uint8_t *)data;
    468 
    469 	if (!size) {
    470 		print_ie_error(level, label, size, -EINVAL);
    471 		return;
    472 	}
    473 
    474 	print_attr(level, "%s: channel %d", label, *channel);
    475 }
    476 
    477 static void print_ie_tim(unsigned int level, const char *label,
    478 				const void *data, uint16_t size)
    479 {
    480 	const char *dtim = data;
    481 	int t, len = size - 3, pos = 0;
    482 	uint8_t bit;
    483 	char str[128];
    484 
    485 	if (size < 4) {
    486 		print_ie_error(level, label, size, -EINVAL);
    487 		return;
    488 	}
    489 
    490 	print_attr(level, "%s:", label);
    491 	print_attr(level + 1, "DTIM count    %2d %s", dtim[0],
    492 			dtim[0] ? "beacon frame(s)" :
    493 				"this beacon frame is DTIM");
    494 	print_attr(level + 1, "DTIM period   %2d beacon frame(s)", dtim[1]);
    495 	print_attr(level + 1, "Group buffered %d offset %d",
    496 			!!(dtim[2] & 0x01), dtim[2] >> 1);
    497 
    498 	len = size - 3;
    499 
    500 	for (t = 0; t < len ; t++) {
    501 		if (((t + 1) % 4) == 1) {
    502 			pos = 0;
    503 			pos += snprintf(&str[pos], sizeof(str) - pos,
    504 					"AID  %4d - %4d ",
    505 					t * 8 + 1,
    506 					t + 4 > len ? len * 8 : (t + 4) * 8);
    507 		}
    508 
    509 		for (bit = 0x01; bit; bit <<= 1)
    510 			pos += snprintf(&str[pos], sizeof(str) - pos,
    511 					"%d", !!(dtim[t + 3] & bit));
    512 
    513 		pos += snprintf(&str[pos], sizeof(str) - pos, " ");
    514 
    515 		if ((t + 1) % 4 == 0 || t + 1 == len)
    516 			print_attr(level + 1, "%s", str);
    517 	}
    518 
    519 }
    520 
    521 static void print_ie_country(unsigned int level, const char *label,
    522 				const void *data, uint16_t size)
    523 {
    524 	uint8_t *code = (uint8_t *)data;
    525 	int i = 3;
    526 
    527 	if (size < 6 || size % 2) {
    528 		print_ie_error(level, label, size, -EINVAL);
    529 		return;
    530 	}
    531 
    532 	print_attr(level, "%s: %c%c", label, code[0], code[1]);
    533 
    534 	switch (code[2]) {
    535 	case ' ':
    536 		print_attr(level + 1,
    537 			"3rd octet: 0x%02x: All environments", code[2]);
    538 		break;
    539 	case 'O':
    540 		print_attr(level + 1,
    541 			"3rd octet: 0x%02x: Outdoor environments", code[2]);
    542 		break;
    543 	case 'I':
    544 		print_attr(level + 1,
    545 			"3rd octet: 0x%02x: Indoor environments", code[2]);
    546 		break;
    547 	case 'X':
    548 		print_attr(level + 1,
    549 			"3rd octet: 0x%02x: Non-country entity", code[2]);
    550 		break;
    551 	default:
    552 		print_attr(level + 1,
    553 			"3rd octet: 0x%02x: Annex E table", code[2]);
    554 		break;
    555 	}
    556 
    557 	while (i < size) {
    558 		if (code[i] > 200) {
    559 			print_attr(level + 1, "Regulatory ID %3d class %3d "
    560 				"coverage class %3d",
    561 				code[i], code[i + 1], code[i + 2]);
    562 
    563 			if (code[i + 2] < 32)
    564 				print_attr(level + 1, "%27c (air propagation "
    565 					"time %2d µs)", ' ', 3 * code[i + 2]);
    566 		} else if (code[i] != 0) {
    567 			print_attr(level + 1, "First channel %3d number of "
    568 				"channels %2d max tx power %2d dBm",
    569 				code[i], code[i + 1], code[i + 2]);
    570 		}
    571 
    572 		i += 3;
    573 	}
    574 }
    575 
    576 static void print_ie_bss_load(unsigned int level, const char *label,
    577 				const void *data, uint16_t size)
    578 {
    579 	uint16_t stations, capacity;
    580 	uint8_t utilization;
    581 	const uint8_t *bytes = data;
    582 
    583 	if (size != 5) {
    584 		print_ie_error(level, label, size, -EINVAL);
    585 		return;
    586 	}
    587 
    588 	stations = bytes[0] | bytes[1] << 8;
    589 	utilization = bytes[2];
    590 	capacity = bytes[3] | bytes[4] << 8;
    591 
    592 	print_attr(level, "%s: %2d station(s) utilization %d/255 available "
    593 			"capacity %d 32µs/s units",
    594 			label, stations, utilization, capacity);
    595 }
    596 
    597 static void print_ie_power_constraint(unsigned int level, const char *label,
    598 					const void *data, uint16_t size)
    599 {
    600 	uint8_t *dB = (uint8_t *)data;
    601 
    602 	if (!size) {
    603 		print_ie_error(level, label, size, -EINVAL);
    604 		return;
    605 	}
    606 
    607 	print_attr(level, "%s: %2d dB", label, *dB);
    608 }
    609 
    610 static void print_ie_tpc(unsigned int level, const char *label,
    611 				const void *data, uint16_t size)
    612 {
    613 	signed char *dB = (signed char*)data;
    614 
    615 	if (size != 2) {
    616 		print_ie_error(level, label, size, -EINVAL);
    617 		return;
    618 	}
    619 
    620 	print_attr(level, "%s: transmit power %2d dB link margin %2d dB",
    621 			label, dB[0], dB[1]);
    622 }
    623 
    624 static void print_ie_erp(unsigned int level, const char *label,
    625 				const void *data, uint16_t size)
    626 {
    627 	uint8_t *flags = (uint8_t *)data;
    628 
    629 	if (!size) {
    630 		print_ie_error(level, label, size, -EINVAL);
    631 		return;
    632 	}
    633 
    634 	print_attr(level, "%s:", label);
    635 	print_attr(level + 1, "non-ERP present      %d", !!(*flags & 0x01));
    636 	print_attr(level + 1, "use protection       %d", !!(*flags & 0x02));
    637 	print_attr(level + 1, "Barker preamble mode %d", !!(*flags & 0x04));
    638 }
    639 
    640 struct cipher_suites {
    641 	uint32_t cipher;
    642 	const char *str;
    643 };
    644 
    645 static const struct cipher_suites rsn_cipher_selectors[] = {
    646 	{ 0x000fac00, "Use group cipher suite"		},
    647 	{ 0x000fac01, "WEP-40"				},
    648 	{ 0x000fac02, "TKIP"				},
    649 	{ 0x000fac04, "CCMP"				},
    650 	{ 0x000fac05, "WEP-104"				},
    651 	{ 0x000fac06, "BIP"				},
    652 	{ 0x000fac07, "Group traffic not allowed"	},
    653 	{ 0x00147201, "WPI-SMS4"			},
    654 	{ },
    655 };
    656 
    657 static const struct cipher_suites rsn_akm_selectors[] = {
    658 	{ 0x000fac01, "IEEE 802.1X/PMKSA; RSNA/PMKSA caching"                                     },
    659 	{ 0x000fac02, "PSK; RSNA PSK"                                                             },
    660 	{ 0x000fac03, "IEEE 802.1X FT; FT"                                                        },
    661 	{ 0x000fac04, "PSK FT; FT"                                                                },
    662 	{ 0x000fac05, "IEEE 802.1X/PMKSA caching SHA256; RSNA/RSNA caching SHA256"                },
    663 	{ 0x000fac06, "PSK SHA256; RSNA PSK SHA256"                                               },
    664 	{ 0x000fac07, "TDLS; TPK"                                                                 },
    665 	{ 0x000fac08, "SAE/PMKSA caching SHA256; RSNA PMKSA caching SHA256/mesh peering exchange" },
    666 	{ 0x000fac09, "FT SAE SHA256; FT"                                                         },
    667 	{ 0x000fac0e, "FILS SHA256"                                                               },
    668 	{ 0x000fac0f, "FILS SHA384"                                                               },
    669 	{ 0x000fac10, "FILS FT SHA256"                                                            },
    670 	{ 0x000fac11, "FILS FT SHA3854"                                                           },
    671 	{ 0x000fac12, "OWE"                                                                       },
    672 	{ 0x506f9a01, "WFA OSEN"                                                                  },
    673 	{ }
    674 };
    675 
    676 static const struct cipher_suites wpa_cipher_selectors[] = {
    677 	{ 0x0050f200, "Use group cipher suite"		},
    678 	{ 0x0050f201, "WEP-40"				},
    679 	{ 0x0050f202, "TKIP"				},
    680 	{ 0x0050f204, "CCMP"				},
    681 	{ 0x0050f205, "WEP-104"				},
    682 	{ },
    683 };
    684 
    685 static const struct cipher_suites wpa_akm_selectors[] = {
    686 	{ 0x0050f201, "IEEE 802.1X/PMKSA; RSNA/PMKSA caching"	},
    687 	{ 0x0050f202, "PSK; RSNA PSK"				},
    688 	{ }
    689 };
    690 
    691 static void print_ie_cipher_suite(unsigned int level, const char *label,
    692 				const uint32_t cipher,
    693 				const struct cipher_suites cipher_table[])
    694 {
    695 	const char *str = NULL;
    696 	unsigned int i;
    697 	unsigned char oui[] = {
    698 		(cipher & 0xff000000) >> 24,
    699 		(cipher & 0x00ff0000) >> 16,
    700 		(cipher & 0x0000ff00) >> 8,
    701 	};
    702 	char suite_value[32] = "";
    703 
    704 	for (i = 0; cipher_table[i].str; i++) {
    705 		if (cipher_table[i].cipher == cipher) {
    706 			str = cipher_table[i].str;
    707 			snprintf(suite_value, sizeof(suite_value), " %02x",
    708 				cipher & 0x000000ff);
    709 			break;
    710 		}
    711 	}
    712 
    713 	if (!str) {
    714 		for (i = 0; oui_table[i].str; i++) {
    715 			if (!memcmp(oui_table[i].oui, oui, 3)) {
    716 				str = oui_table[i].str;
    717 				snprintf(suite_value, sizeof(suite_value),
    718 					" %02x (vendor specific)",
    719 					cipher & 0x000000ff);
    720 				break;
    721 			}
    722 		}
    723 	}
    724 
    725 	if (!str) {
    726 		str = "unknown";
    727 		snprintf(suite_value, sizeof(suite_value), "%02x (unknown)",
    728 			cipher & 0x000000ff);
    729 	}
    730 
    731 	if (label)
    732 		print_attr(level, "%s: %s (%02x:%02x:%02x) suite %s",
    733 			label, str, oui[0], oui[1], oui[2], suite_value);
    734 	else
    735 		print_attr(level, "%s (%02x:%02x:%02x) suite %s",
    736 			str, oui[0], oui[1], oui[2], suite_value);
    737 }
    738 
    739 static void print_ie_cipher_suites(unsigned int level, const char *label,
    740 				const void *data, uint16_t size,
    741 				const struct cipher_suites cipher_table[])
    742 {
    743 	uint32_t cipher;
    744 
    745 	print_attr(level, "%s: len %u", label, size);
    746 
    747 	while (size >= 4) {
    748 		cipher = l_get_be32(data);
    749 
    750 		print_ie_cipher_suite(level + 1, NULL, cipher, cipher_table);
    751 
    752 		data += 4;
    753 		size -= 4;
    754 	}
    755 }
    756 
    757 static const char *rsn_capabilities_bitfield[] = {
    758 	"Preauthentication",
    759 	"No Pairwise",
    760 	"",
    761 	"",
    762 	"",
    763 	"",
    764 	"Management Frame Protection Required",
    765 	"Management Frame Protection Capable",
    766 	"Reserved",
    767 	"Peerkey Enabled",
    768 	"SPP A-MSDU Capable",
    769 	"SPP A-MSDU Required",
    770 	"PBAC",
    771 	"Extended Key ID for Individually Addressed Frames",
    772 	"Operating Channel Validation Capable",
    773 	"Reserved",
    774 	NULL
    775 };
    776 
    777 static void print_ie_bitfield(unsigned int level, const char *label,
    778 			const uint8_t *bytes, const uint8_t *mask, size_t len,
    779 			const char *bitfield_table[])
    780 {
    781 	unsigned int i;
    782 
    783 	for (i = 0; i < len * 8; i++) {
    784 		uint8_t byte = i / 8;
    785 		uint8_t bit = i % 8;
    786 		uint8_t masked = bytes[byte] & mask[byte];
    787 
    788 		if (!test_bit(&masked, bit))
    789 			continue;
    790 
    791 		print_attr(level, "%s: bit %2d: %s", label, i,
    792 			bitfield_table[i]);
    793 	}
    794 }
    795 
    796 static size_t print_ie_rsn_suites(unsigned int level, const char *label,
    797 				const void *data, uint16_t size)
    798 {
    799 	uint16_t count;
    800 	uint16_t orig_size = size;
    801 
    802 	print_ie_cipher_suites(level + 1, "Group Data Cipher Suite", data, 4,
    803 				rsn_cipher_selectors);
    804 
    805 	data += 4;
    806 	size -= 4;
    807 
    808 	if (size < 2)
    809 		goto end;
    810 
    811 	count = l_get_le16(data) * 4;
    812 	data += 2;
    813 	size -= 2;
    814 
    815 	if (size < count)
    816 		goto end;
    817 
    818 	print_ie_cipher_suites(level + 1, "Pairwise Cipher Suite", data,
    819 					count, rsn_cipher_selectors);
    820 	data += count;
    821 	size -= count;
    822 
    823 	if (size < 2)
    824 		goto end;
    825 
    826 	count = l_get_le16(data) * 4;
    827 	data += 2;
    828 	size -= 2;
    829 
    830 	if (size < count)
    831 		goto end;
    832 
    833 	print_ie_cipher_suites(level + 1, "AKM Suite", data, count,
    834 				rsn_akm_selectors);
    835 	data += count;
    836 	size -= count;
    837 
    838 	if (size < 2)
    839 		goto end;
    840 
    841 	return orig_size - size;
    842 
    843 end:
    844 	if (size)
    845 		print_ie_error(level, label, size, -EINVAL);
    846 
    847 	return orig_size - size;
    848 }
    849 
    850 static void print_ie_rsn(unsigned int level, const char *label,
    851 			const void *data, uint16_t size)
    852 {
    853 	const void *end = data + size;
    854 
    855 	uint16_t version, count;
    856 	uint8_t bytemask[2];
    857 	int i;
    858 	const char *rsn_capabilities_replay_counter[] = {
    859 		"1 replay counter",
    860 		"2 replay counters",
    861 		"4 replay counters",
    862 		"16 replay counters"
    863 	};
    864 
    865 	print_attr(level, "RSN:");
    866 
    867 	if (end - data < 2) {
    868 		print_ie_error(level, label, size, -EINVAL);
    869 		return;
    870 	}
    871 
    872 	version = l_get_le16(data);
    873 	if (version != 1) {
    874 		print_attr(level, "Unknown RSN version %d", version);
    875 		return;
    876 	}
    877 
    878 	data += 2;
    879 
    880 	if (end - data < 4)
    881 		goto end;
    882 
    883 	data += print_ie_rsn_suites(level, label, data, size);
    884 
    885 	bytemask[0] = 0x03;
    886 	bytemask[1] = 0x00;
    887 	print_ie_bitfield(level + 1, "RSN capabilities", data, bytemask,
    888 			sizeof(bytemask), rsn_capabilities_bitfield);
    889 
    890 	count = (*((uint8_t *)data) & 0x0c) >> 2;
    891 	print_attr(level + 1, "RSN capabilities: bits  2 - 3: %s per PTKSA",
    892 		rsn_capabilities_replay_counter[count]);
    893 
    894 	count = (*((uint8_t *)data) & 0x30) >> 4;
    895 	print_attr(level + 1, "RSN capabilities: bits  4 - 5: %s per GTKSA",
    896 		rsn_capabilities_replay_counter[count]);
    897 
    898 	bytemask[0] = 0xc0;
    899 	bytemask[1] = 0xff;
    900 	print_ie_bitfield(level + 1, "RSN capabilities", data, bytemask,
    901 			sizeof(bytemask), rsn_capabilities_bitfield);
    902 
    903 	data += 2;
    904 
    905 	if (end - data < 2)
    906 		goto end;
    907 
    908 	count = l_get_le16(data) * 16;
    909 	data += 2;
    910 
    911 	if (end - data < count)
    912 		goto end;
    913 
    914 	for (i = 0; i < count; i += 16) {
    915 		const uint8_t *bytes = data;
    916 
    917 		print_attr(level + 1, "PMKID: %02x:%02x:%02x:%02x:"
    918 			"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
    919 			"%02x:%02x:%02x:%02x",
    920 			bytes[i], bytes[i + 1],
    921 			bytes[i + 2], bytes[i + 3],
    922 			bytes[i + 4], bytes[i + 5],
    923 			bytes[i + 6], bytes[i + 7],
    924 			bytes[i + 8], bytes[i + 9],
    925 			bytes[i + 10], bytes[i + 11],
    926 			bytes[i + 12], bytes[i + 13],
    927 			bytes[i + 14], bytes[i + 15]);
    928 	}
    929 
    930 	data += count;
    931 
    932 	if (end - data < 4)
    933 		goto end;
    934 
    935 	print_ie_cipher_suites(level + 1, "Group Management Cipher Suite",
    936 			data, 4, rsn_cipher_selectors);
    937 
    938 	data += 4;
    939 
    940 end:
    941 	if (end - data)
    942 		print_ie_error(level, label, size, -EINVAL);
    943 }
    944 
    945 static void print_ie_wpa(unsigned int level, const char *label,
    946 						const void *data, uint16_t size)
    947 {
    948 	uint8_t offset;
    949 	uint16_t version, count;
    950 
    951 	if (size < 2)
    952 		return;
    953 
    954 	offset = 0;
    955 	version = l_get_le16(data + offset);
    956 	offset += 2;
    957 
    958 	if (version != 1)
    959 		return;
    960 
    961 	print_attr(level, "WPA:");
    962 	print_attr(level + 1, "Version: %d(%04x)", version, version);
    963 
    964 	if (offset + 4 > size)
    965 		goto end;
    966 
    967 	print_ie_cipher_suites(level + 1, "Group Data Cipher Suite",
    968 					data + offset, 4, wpa_cipher_selectors);
    969 
    970 	offset += 4;
    971 	if (offset + 2 > size)
    972 		goto end;
    973 
    974 	count = l_get_le16(data + offset) * 4;
    975 	offset += 2;
    976 
    977 	if (offset + count > size)
    978 		goto end;
    979 
    980 	print_ie_cipher_suites(level + 1, "Pairwise Cipher Suite",
    981 				data + offset, count, wpa_cipher_selectors);
    982 	offset += count;
    983 	if (offset + 2 > size)
    984 		goto end;
    985 
    986 	count = l_get_le16(data + offset) * 4;
    987 	offset += 2;
    988 	if (offset + count > size)
    989 		goto end;
    990 
    991 	print_ie_cipher_suites(level + 1, "AKM Suite", data + offset, count,
    992 							wpa_akm_selectors);
    993 	return;
    994 
    995 end:
    996 	print_ie_error(level, label, size, -EINVAL);
    997 }
    998 
    999 static void print_ie_wfa_hs20(unsigned int level, const char *label,
   1000 						const void *data, uint16_t size)
   1001 {
   1002 	const uint8_t *ptr = data;
   1003 	bool pps_mo_id_present;
   1004 	bool anpq_domain_id_present;
   1005 
   1006 	if (size < 1)
   1007 		return;
   1008 
   1009 	pps_mo_id_present = test_bit(ptr, 1);
   1010 	anpq_domain_id_present = test_bit(ptr, 2);
   1011 
   1012 	print_attr(level + 1, "HS2.0 Indication Element:");
   1013 	print_attr(level + 2, "DGAF Disabled: %u", test_bit(ptr, 0));
   1014 	print_attr(level + 2, "PPS MO ID Present: %u", pps_mo_id_present);
   1015 	print_attr(level + 2, "ANQP Domain ID Present: %u",
   1016 				anpq_domain_id_present);
   1017 
   1018 	switch (bit_field(ptr[0], 4, 7)) {
   1019 	case 0:
   1020 		print_attr(level + 2, "Version Number: 1.x");
   1021 		break;
   1022 	case 1:
   1023 		print_attr(level + 2, "Version Number: 2.x");
   1024 		break;
   1025 	case 2:
   1026 		print_attr(level + 2, "Version Number: 3.x");
   1027 		break;
   1028 	}
   1029 
   1030 	ptr += 1;
   1031 	size -= 1;
   1032 
   1033 	if (pps_mo_id_present) {
   1034 		if (size < 2)
   1035 			return;
   1036 
   1037 		print_attr(level + 2, "PPS MO ID: %02x %02x", ptr[0], ptr[1]);
   1038 		ptr += 2;
   1039 		size -= 2;
   1040 	}
   1041 
   1042 	if (anpq_domain_id_present) {
   1043 		if (size < 2)
   1044 			return;
   1045 
   1046 		print_attr(level + 2, "ANQP Domain ID: %02x %02x",
   1047 				ptr[0], ptr[1]);
   1048 	}
   1049 }
   1050 
   1051 static bool print_oui(unsigned int level, const uint8_t *oui)
   1052 {
   1053 	const char *str = NULL;
   1054 	unsigned int i;
   1055 
   1056 	for (i = 0; oui_table[i].str; i++) {
   1057 		if (!memcmp(oui_table[i].oui, oui, 3)) {
   1058 			str = oui_table[i].str;
   1059 			break;
   1060 		}
   1061 	}
   1062 
   1063 	if (!str) {
   1064 		print_attr(level + 1, "OUI: %02x:%02x:%02x type:%02x",
   1065 							oui[0], oui[1], oui[2],
   1066 							oui[3]);
   1067 		return false;
   1068 	}
   1069 
   1070 	print_attr(level + 1, "%s (%02x:%02x:%02x) type: %02x", str,
   1071 							oui[0], oui[1], oui[2],
   1072 							oui[3]);
   1073 	return true;
   1074 }
   1075 
   1076 static void print_ipv4(unsigned int level, const char *label,
   1077 				const uint8_t *addr)
   1078 {
   1079 	print_attr(level, "%s: %u.%u.%u.%u", label,
   1080 			addr[0], addr[1], addr[2], addr[3]);
   1081 }
   1082 
   1083 static void print_ie_wfa_owe_transition(unsigned int level, const char *label,
   1084 						const void *data, uint16_t size)
   1085 {
   1086 	struct ie_owe_transition_info info;
   1087 
   1088 	if (ie_parse_owe_transition(data, size, &info) < 0) {
   1089 		print_attr(level + 1, "Error parsing");
   1090 		return;
   1091 	}
   1092 
   1093 	print_attr(level + 1, "BSSID: "MAC, MAC_STR(info.bssid));
   1094 	print_attr(level + 1, "SSID: %s", util_ssid_to_utf8(info.ssid_len,
   1095 								info.ssid));
   1096 }
   1097 
   1098 static void print_ie_vendor(unsigned int level, const char *label,
   1099 				const void *data, uint16_t size)
   1100 {
   1101 	const uint8_t *oui = data;
   1102 
   1103 	print_attr(level, "%s: len %u", label, size);
   1104 
   1105 	if (size < 4)
   1106 		return;
   1107 
   1108 	if (!print_oui(level, oui))
   1109 		return;
   1110 
   1111 	data += 4;
   1112 	size -= 4;
   1113 
   1114 	if (!memcmp(oui, microsoft_oui, 3)) {
   1115 		switch (oui[3]) {
   1116 		case 1:		/* MSoft WPA IE */
   1117 			print_ie_wpa(level + 2, label, data, size);
   1118 			return;
   1119 		default:
   1120 			return;
   1121 		}
   1122 	} else if (!memcmp(oui, wifi_alliance_oui, 3)) {
   1123 		switch (oui[3]) {
   1124 		case 0x04:
   1125 			print_attr(level + 1, "IP Address Request KDE");
   1126 			return;
   1127 		case 0x05:
   1128 			print_attr(level + 1, "IP Address Allocation KDE");
   1129 
   1130 			if (size < 12)
   1131 				return;
   1132 
   1133 			print_ipv4(level + 2, "Client IP Address", data + 0);
   1134 			print_ipv4(level + 2, "Subnet Mask", data + 4);
   1135 			print_ipv4(level + 2, "GO IP Address", data + 8);
   1136 			return;
   1137 		case 0x10:
   1138 			print_ie_wfa_hs20(level + 1, label, data, size);
   1139 			return;
   1140 		case 0x12:
   1141 			print_ie_rsn_suites(level + 1, label, data, size);
   1142 			return;
   1143 		case 0x1c:
   1144 			print_ie_wfa_owe_transition(level + 1, label,
   1145 							data - 6, size + 6);
   1146 			return;
   1147 		default:
   1148 			return;
   1149 		}
   1150 	} else if (!memcmp(oui, ieee_oui, 3)) {
   1151 		const char *kde;
   1152 
   1153 		/* EAPoL-Key KDEs */
   1154 		switch (oui[3]) {
   1155 		case 1:
   1156 			kde = "GTK";
   1157 			break;
   1158 		case 3:
   1159 			kde = "MAC address";
   1160 			break;
   1161 		case 4:
   1162 			kde = "PMKID";
   1163 			break;
   1164 		case 5:
   1165 			kde = "SMK";
   1166 			break;
   1167 		case 6:
   1168 			kde = "Nonce";
   1169 			break;
   1170 		case 7:
   1171 			kde = "Lifetime";
   1172 			break;
   1173 		case 8:
   1174 			kde = "Error";
   1175 			break;
   1176 		case 9:
   1177 			kde = "IGTK";
   1178 			break;
   1179 		case 10:
   1180 			kde = "Key ID";
   1181 			break;
   1182 		case 11:
   1183 			kde = "Multi-band GTK";
   1184 			break;
   1185 		case 12:
   1186 			kde = "Multi-band Key ID";
   1187 			break;
   1188 		default:
   1189 			return;
   1190 		}
   1191 
   1192 		print_attr(level + 1, "%s KDE", kde);
   1193 		return;
   1194 	}
   1195 }
   1196 
   1197 static void print_ht_capabilities(unsigned int level, const char *label,
   1198 					const void *data, uint16_t size)
   1199 {
   1200 	static const char *ht_capabilities_info_bitfield[16] = {
   1201 		[0] = "LDPC Coding Capability",
   1202 		[1] = "Supported Channel Width Set",
   1203 		[2] = "SM Power Save",
   1204 		[3] = "SM Power Save",
   1205 		[4] = "HT-Greenfield",
   1206 		[5] = "Short GI for 20Mhz",
   1207 		[6] = "Short GI for 40Mhz",
   1208 		[7] = "Tx STBC",
   1209 		[8] = "Rx STBC",
   1210 		[9] = "Rx STBC",
   1211 		[10] = "HT-Delayed Block Ack",
   1212 		[11] = "Maximum A-MSDU Length",
   1213 		[12] = "DSSS/CCK Mode in 40Mhz",
   1214 		[13] = "Reserved",
   1215 		[14] = "40 Mhz Intolerant",
   1216 		[15] = "L-SIG TXOP Protection Support",
   1217 	};
   1218 	static const char *ht_capabilities_sm_power_save[4] = {
   1219 		"Static", "Dynamic", "Reserved", "Disabled",
   1220 	};
   1221 	static const char *ht_capabilities_rx_stbc[4] = {
   1222 		"Disabled", "One spatial stream", "One and two spatial streams",
   1223 		"One, two and three spatial streams"
   1224 	};
   1225 	uint8_t info_mask[] = { 0x03, 0xfc };
   1226 	const uint8_t *htc = data;
   1227 	uint8_t sm_power_save;
   1228 	uint8_t rx_stbc;
   1229 
   1230 	if (size != 2)
   1231 		return print_ie_error(level, label, size, -EINVAL);
   1232 
   1233 	/* Print bits 0-1 */
   1234 	print_ie_bitfield(level, "HT Capabilities Info", data, info_mask,
   1235 				1, ht_capabilities_info_bitfield);
   1236 
   1237 	/* Print SM Power Save */
   1238 	sm_power_save = bit_field(htc[0], 2, 2);
   1239 	print_attr(level, "HT Capabilities Info: bits 2-3: %s",
   1240 			ht_capabilities_sm_power_save[sm_power_save]);
   1241 
   1242 	/* Print bits 4-7 */
   1243 	info_mask[0] = 0xf0;
   1244 	print_ie_bitfield(level, "HT Capabilities Info", data, info_mask,
   1245 				1, ht_capabilities_info_bitfield);
   1246 
   1247 	rx_stbc = bit_field(htc[1], 0, 2);
   1248 	print_attr(level, "HT Capabilities Info: bits 8-9: %s",
   1249 			ht_capabilities_rx_stbc[rx_stbc]);
   1250 
   1251 	/* Print bits 10-15 */
   1252 	info_mask[0] = 0x00;
   1253 	print_ie_bitfield(level, "HT Capabilities Info", data, info_mask,
   1254 				2, ht_capabilities_info_bitfield);
   1255 }
   1256 
   1257 static void print_ht_mcs_set(unsigned int level, const char *label,
   1258 					const void *data, uint16_t size)
   1259 {
   1260 	const uint8_t *bytes = data;
   1261 	int i;
   1262 	uint8_t bytemask[16];
   1263 	uint16_t data_rate;
   1264 	const char *mcs_set[128] = {
   1265 		[77] = "Reserved",
   1266 		[78] = "Reserved",
   1267 		[79] = "Reserved",
   1268 
   1269 		[90] = "Reserved",
   1270 		[91] = "Reserved",
   1271 		[92] = "Reserved",
   1272 		[93] = "Reserved",
   1273 		[94] = "Reserved",
   1274 		[95] = "Reserved",
   1275 		[96] = "Tx MCS set defined",
   1276 
   1277 		[97]  = "Tx Rx MCS set not equal",
   1278 		[100] = "Tx unequal modulation supported",
   1279 		[101] = "Reserved",
   1280 		[102] = "Reserved",
   1281 		[103] = "Reserved",
   1282 
   1283 		[104] = "Reserved",
   1284 		[105] = "Reserved",
   1285 		[106] = "Reserved",
   1286 		[107] = "Reserved",
   1287 		[108] = "Reserved",
   1288 		[109] = "Reserved",
   1289 		[110] = "Reserved",
   1290 		[111] = "Reserved",
   1291 
   1292 		[112] = "Reserved",
   1293 		[113] = "Reserved",
   1294 		[114] = "Reserved",
   1295 		[115] = "Reserved",
   1296 		[116] = "Reserved",
   1297 		[117] = "Reserved",
   1298 		[118] = "Reserved",
   1299 		[119] = "Reserved",
   1300 
   1301 		[120] = "Reserved",
   1302 		[121] = "Reserved",
   1303 		[122] = "Reserved",
   1304 		[123] = "Reserved",
   1305 		[124] = "Reserved",
   1306 		[125] = "Reserved",
   1307 		[126] = "Reserved",
   1308 		[127] = "Reserved",
   1309 	};
   1310 
   1311 	if (size != 16)
   1312 		return print_ie_error(level, label, size, -EINVAL);
   1313 
   1314 	for (i = 0; i < 77; i++) {
   1315 		if (test_bit(bytes, i))
   1316 			print_attr(level, "%s: MCS %d", label, i);
   1317 	}
   1318 
   1319 	memset(bytemask, 0, sizeof(bytemask));
   1320 
   1321 	bytemask[9] = 0xe0;
   1322 
   1323 	print_ie_bitfield(level, "MCS set", bytes, bytemask, sizeof(bytemask),
   1324 			mcs_set);
   1325 
   1326 	data_rate = l_get_le16(&bytes[10]) & 0x3ff;
   1327 
   1328 	if (data_rate)
   1329 		print_attr(level, "MCS set: Rx Highest data rate: %d Mbit/s",
   1330 			data_rate);
   1331 
   1332 	bytemask[9]  = 0x00;
   1333 	bytemask[11] = 0xfc;
   1334 	bytemask[12] = 0x03;
   1335 
   1336 	print_ie_bitfield(level, "MCS Set", bytes, bytemask, sizeof(bytemask),
   1337 			mcs_set);
   1338 
   1339 	if (bytes[12] & 0x0c)
   1340 		print_attr(level,
   1341 			"MCS set: Tx max spatial streams supported: %d",
   1342 			((bytes[12] & 0x0c) >> 2) + 1);
   1343 
   1344 	bytemask[11] = 0x00;
   1345 	bytemask[12] = 0xf0;
   1346 	bytemask[13] = 0xff;
   1347 	bytemask[14] = 0xff;
   1348 	bytemask[15] = 0xff;
   1349 
   1350 	print_ie_bitfield(level, "MCS set", bytes, bytemask, sizeof(bytemask),
   1351 			mcs_set);
   1352 }
   1353 
   1354 static void print_ie_ht_operation(unsigned int level, const char *label,
   1355 					const void *data, uint16_t size)
   1356 {
   1357 	const char *secondary_offset[] = {
   1358 		"no secondary channel",
   1359 		"above primary channel",
   1360 		"reserved",
   1361 		"below primary channel"
   1362 	};
   1363 	const char *channel_width[] = {
   1364 		"20 MHz channel width",
   1365 		"Any supported channel width"
   1366 	};
   1367 	const char *ht_protection[] = {
   1368 		"No protection",
   1369 		"Nonmember protection mode",
   1370 		"20 MHz protection mode",
   1371 		"non-HT mixed mode"
   1372 	};
   1373 	const char *ht_ops_bitfield[] = {
   1374 		"",
   1375 		"",
   1376 		"",
   1377 		"RIFS permitted",
   1378 		"Reserved",
   1379 		"Reserved",
   1380 		"Reserved",
   1381 		"Reserved",
   1382 
   1383 		"",
   1384 		"",
   1385 		"Non-greenfield HT STAs present",
   1386 		"Reserved",
   1387 		"OBSS non-HT STAs present",
   1388 		"Reserved",
   1389 		"Reserved",
   1390 		"Reserved",
   1391 
   1392 		"Reserved",
   1393 		"Reserved",
   1394 		"Reserved",
   1395 		"Reserved",
   1396 		"Reserved",
   1397 		"Reserved",
   1398 		"Reserved",
   1399 
   1400 		"Reserved",
   1401 		"Reserved",
   1402 		"Reserved",
   1403 		"Reserved",
   1404 		"Reserved",
   1405 		"Reserved",
   1406 		"Dual beacon",
   1407 		"Dual CTS protection",
   1408 
   1409 		"STBC beacon",
   1410 		"L-SIG TXOP protection full support",
   1411 		"PCO active",
   1412 		"PCO Phase",
   1413 		"Reserved",
   1414 		"Reserved",
   1415 		"Reserved",
   1416 		"Reserved",
   1417 
   1418 		NULL
   1419 	};
   1420 	uint8_t *bytes = (uint8_t *) data;
   1421 	uint8_t bytemask[5];
   1422 	int i;
   1423 
   1424 	if (size < 22) {
   1425 		print_ie_error(level, label, size, -EINVAL);
   1426 		return;
   1427 	}
   1428 
   1429 	print_attr (level, "%s:", label);
   1430 	print_attr (level + 1, "Primary channel %d", bytes[0]);
   1431 
   1432 	i = bytes[1] & 0x03;
   1433 	print_attr (level + 1,
   1434 		"Information: Secondary Channel Offset: %s",
   1435 		secondary_offset[i]);
   1436 
   1437 	i = (bytes[1] & 0x04) >> 2;
   1438 	print_attr (level + 1,
   1439 		"Information: Channel width: bit  2: %s",
   1440 		channel_width[i]);
   1441 
   1442 	memset(bytemask, 0, sizeof(bytemask));
   1443 	bytemask[0] = 0xf8;
   1444 
   1445 	print_ie_bitfield(level + 1,
   1446 			"Information", &bytes[1], bytemask,
   1447 			sizeof(bytemask), ht_ops_bitfield);
   1448 
   1449 	i = bytes[2] & 0x03;
   1450 	print_attr(level + 1,
   1451 		"Information: HT Protection: bits  8 -  9: %s",
   1452 		ht_protection[i]);
   1453 
   1454 	bytemask[0] = 0x00;
   1455 	bytemask[1] = 0xfc;
   1456 	bytemask[2] = 0xff;
   1457 	bytemask[3] = 0xff;
   1458 	bytemask[4] = 0xff;
   1459 	print_ie_bitfield(level + 1, "Information", &bytes[1],
   1460 			bytemask, sizeof(bytemask), ht_ops_bitfield);
   1461 
   1462 	print_ht_mcs_set(level + 1, "Basic MCS set", &bytes[6], 16);
   1463 }
   1464 
   1465 static void print_spatial_stream_map(unsigned int level, const char *label,
   1466 					const uint8_t *map,
   1467 					const char **stream_map)
   1468 {
   1469 	unsigned int i;
   1470 	unsigned int j = 0;
   1471 
   1472 	for (i = 0; i < 8; i++, j += 2) {
   1473 		unsigned int byte = (i < 4) ? 0 : 1;
   1474 		uint8_t field = bit_field(map[byte], j % 8, 2);
   1475 
   1476 		if (field == 3)
   1477 			continue;
   1478 
   1479 		print_attr(level + 1, "%s %u Streams: %s", label, i + 1,
   1480 				stream_map[field]);
   1481 	}
   1482 }
   1483 
   1484 static void print_ie_vht_operation(unsigned int level, const char *label,
   1485 					const void *data, uint16_t size)
   1486 {
   1487 	static const char *channel_width[] = {
   1488 		"0 - 20 or 40 Mhz channel width",
   1489 		"1 - 80 Mhz",
   1490 		"2 - 160 Mhz",
   1491 		"3 - 80+80 Mhz",
   1492 	};
   1493 
   1494 	static const char *spatial_streams[] = {
   1495 		"MCS 0-7 supported",
   1496 		"MCS 0-8 supported",
   1497 		"MCS 0-9 supported",
   1498 	};
   1499 
   1500 	uint8_t *bytes = (uint8_t *) data;
   1501 
   1502 	if (size < 5) {
   1503 		print_ie_error(level, label, size, -EINVAL);
   1504 		return;
   1505 	}
   1506 
   1507 	print_attr(level, "%s:", label);
   1508 	print_attr(level + 1, "Channel Width %s", bytes[0] > 3 ? "Reserved" :
   1509 						channel_width[bytes[0]]);
   1510 	print_attr(level + 1, "Channel Center Frequency 1: %d", bytes[1]);
   1511 	print_attr(level + 1, "Channel Center Frequency 2: %d", bytes[2]);
   1512 
   1513 	print_spatial_stream_map(level + 1, "Basic VHT-MCS", bytes + 3,
   1514 					spatial_streams);
   1515 }
   1516 
   1517 static const char *extended_capabilities_bitfield[80] = {
   1518 	[0] = "20/40 BSS coexistence management support",
   1519 	[1] = "Reserved",
   1520 	[2] = "Extended channel switching",
   1521 	[3] = "Reserved",
   1522 	[4] = "PSMP capability",
   1523 	[5] = "Reserved",
   1524 	[6] = "S-PSMP support",
   1525 	[7] = "Event",
   1526 	[8] = "Diagnostics",
   1527 	[9] = "Multicast diagnostics",
   1528 	[10] = "Location tracking",
   1529 	[11] = "FMS",
   1530 	[12] = "Proxy ARP service",
   1531 	[13] = "Collocated interference reporting",
   1532 	[14] = "Civic location",
   1533 	[15] = "Geospatial location",
   1534 	[16] = "TFS",
   1535 	[17] = "WNM-Sleep mode",
   1536 	[18] = "TIM broadcast",
   1537 	[19] = "BSS transition",
   1538 	[20] = "QoS traffic capability",
   1539 	[21] = "AC station count",
   1540 	[22] = "Multiple BSSID",
   1541 	[23] = "Timing measurement",
   1542 	[24] = "Channel usage",
   1543 	[25] = "SSID list",
   1544 	[26] = "DMS",
   1545 	[27] = "UTC TSF offset",
   1546 	[28] = "TDLS Peer U-APSD buffer STA support",
   1547 	[29] = "TDLS Peer PSM support",
   1548 	[30] = "TDLS channel switching",
   1549 	[31] = "Interworking",
   1550 	[32] = "QoS Map",
   1551 	[33] = "EBR",
   1552 	[34] = "SSPN Interface",
   1553 	[35] = "Reserved",
   1554 	[36] = "MSGCF Capability",
   1555 	[37] = "TDLS Support",
   1556 	[38] = "TDLS Prohibited",
   1557 	[39] = "TDLS Channel Switching Prohibited",
   1558 	[40] = "Reject Unadmitted Frame",
   1559 	[41 ... 43] = "Reserved",
   1560 	[44] = "Identifier Location",
   1561 	[45] = "U-APSD Coexistence",
   1562 	[46] = "WNM- Notification",
   1563 	[47] = "Reserved",
   1564 	[48] = "UTF-8 SSID",
   1565 	[49] = "QMF Activated",
   1566 	[50] = "QMF Reconfiguration Activated",
   1567 	[51] = "Robust AV Streaming",
   1568 	[52] = "Advanced GCR",
   1569 	[53] = "Mesh GCR",
   1570 	[54] = "SCS",
   1571 	[55] = "QLoad Report",
   1572 	[56] = "Alternate EDCA",
   1573 	[57] = "Unprotected TXOP Negotiation",
   1574 	[58] = "Protected TXOP Negotiation",
   1575 	[59] = "Reserved",
   1576 	[60] = "Protected QLoad Report",
   1577 	[61] = "TDLS Wider Bandwidth",
   1578 	[62] = "Opmode Notification",
   1579 	[65] = "Channel Schedule Management",
   1580 	[66] = "Geodatabase Inband Enabling Signal",
   1581 	[67] = "Network Channel Control",
   1582 	[68] = "White Space Map",
   1583 	[69] = "Channel Availability Query",
   1584 	[70] = "Fine Timing Measurement Responder",
   1585 	[71] = "Fine Timing Measurement Initiator",
   1586 	[72] = "FILS Capability",
   1587 	[73] = "Extended Spectrum Management Capable",
   1588 	[74] = "Future Channel Guidance",
   1589 };
   1590 
   1591 static void print_ie_extended_capabilities(unsigned int level,
   1592 					const char *label,
   1593 					const void *data, uint16_t size)
   1594 {
   1595 	uint8_t bytemask1[] = { 0xff, 0xff, 0xff, 0xff,
   1596 				0xff, 0x01 };
   1597 	uint8_t bytemask2[] = { 0x00, 0x00, 0x00, 0x00,
   1598 				0x00, 0xf0, 0xff, 0xff };
   1599 	uint8_t interval;
   1600 	size_t bytes;
   1601 	bool spsmp;
   1602 
   1603 	print_attr(level, "%s: len %u", label, size);
   1604 
   1605 	if (size == 0)
   1606 		return;
   1607 
   1608 	spsmp = test_bit(data, 6);
   1609 
   1610 	bytes = size < sizeof(bytemask1) ? size : sizeof(bytemask1);
   1611 
   1612 	/* Print first 40 bits */
   1613 	print_ie_bitfield(level + 1, "Capability", data, bytemask1,
   1614 				bytes, extended_capabilities_bitfield);
   1615 
   1616 	if (size <= bytes)
   1617 		return;
   1618 
   1619 	/* Print Service Interval Granularity */
   1620 	if (spsmp) {
   1621 		interval = bit_field(*((uint8_t *) data + 5), 1, 3);
   1622 		print_attr(level + 1,
   1623 			"Shortest Service Interval Granularity: %d ms",
   1624 			interval * 5 + 5);
   1625 	}
   1626 
   1627 	bytes = size < sizeof(bytemask2) ? size : sizeof(bytemask2);
   1628 
   1629 	/* Print remainder */
   1630 	print_ie_bitfield(level + 1, "Capability", data, bytemask2,
   1631 			bytes, extended_capabilities_bitfield);
   1632 }
   1633 
   1634 static void print_ie_ht_capabilities(unsigned int level,
   1635 					const char *label,
   1636 					const void *data, uint16_t size)
   1637 {
   1638 	static const char *ht_capabilities_min_mpdu_start_spacing[8] = {
   1639 		"No restriction", "1/4 us", "1/2 us", "1 us", "2 us",
   1640 		"4 us", "8 us", "16 us",
   1641 	};
   1642 	static const char *ht_capabilities_pco_transition_time[4] = {
   1643 		"No transition", "400 us", "1.5 ms", "5 ms",
   1644 	};
   1645 	static const char *ht_capabilities_mcs_feedback[4] = {
   1646 		"No feedback", "Reserved", "Unsolicited", "Both",
   1647 	};
   1648 	const uint8_t *htc = data;
   1649 	uint8_t ampdu_exponent;
   1650 	bool pco;
   1651 	bool plus_htc;
   1652 	bool rd_responder;
   1653 	uint8_t bits;
   1654 
   1655 	print_attr(level, "%s: len %u", label, size);
   1656 
   1657 	if (size != 26)
   1658 		return;
   1659 
   1660 	print_ht_capabilities(level + 1, "HT Capabilities Info", htc, 2);
   1661 
   1662 	ampdu_exponent = bit_field(htc[2], 0, 2);
   1663 	print_attr(level + 1, "A-MPDU Parameters: "
   1664 			"Maximum A-MPDU Length Exponent: %d", ampdu_exponent);
   1665 
   1666 	bits = bit_field(htc[2], 2, 3);
   1667 	print_attr(level + 1, "A-MPDU Parameters: "
   1668 			"Minimum MPDU Start Spacing: %s",
   1669 			ht_capabilities_min_mpdu_start_spacing[bits]);
   1670 
   1671 	print_ht_mcs_set(level + 1, "Supported MCS", htc + 3, 16);
   1672 
   1673 	pco = test_bit(htc + 18, 0);
   1674 	print_attr(level + 1, "HT Extended Capabilities: PCO: %s",
   1675 			bits ? "supported" : "not supported");
   1676 
   1677 	if (pco) {
   1678 		bits = bit_field(htc[18], 1, 2);
   1679 		print_attr(level + 1, "HT Extended Capabilities: "
   1680 				"PCO Transition Time: %s",
   1681 				ht_capabilities_pco_transition_time[bits]);
   1682 	}
   1683 
   1684 	bits = bit_field(htc[19], 0, 2);
   1685 	print_attr(level + 1, "HT Extended Capabilities: "
   1686 			"MCS Feedback: %s", ht_capabilities_mcs_feedback[bits]);
   1687 
   1688 	plus_htc = test_bit(htc + 19, 2);
   1689 	print_attr(level + 1, "HT Extended Capabilities: "
   1690 			"+HTC: %s", plus_htc ? "supported" : "not supported");
   1691 
   1692 	rd_responder = test_bit(htc + 19, 3);
   1693 	print_attr(level + 1, "HT Extended Capabilities: "
   1694 			"RD Responder: %s",
   1695 			rd_responder ? "supported" : "not supported");
   1696 
   1697 	/* TODO: Transmit Beamforming Capabilities field */
   1698 	/* TODO: ASEL Capability field */
   1699 }
   1700 
   1701 static void print_ie_vht_capabilities(unsigned int level,
   1702 					const char *label,
   1703 					const void *data, uint16_t size)
   1704 {
   1705 	static const char *vht_capabilities_info_bitfield[] = {
   1706 		[0 ... 1] = "Maximum MPDU Length",
   1707 		[2 ... 3] = "Supported Channel Width Set",
   1708 		[4] = "RX LDPC",
   1709 		[5] = "Short GI for 80 Mhz",
   1710 		[6] = "Short GI for 160 and 80+80 Mhz",
   1711 		[7] = "Tx STBC",
   1712 		[8 ... 10] = "Rx STBC",
   1713 		[11] = "SU Beamformer Capable",
   1714 		[12] = "SU Beamformee Capable",
   1715 		[13 ... 15] = "Beamformee STS Capability",
   1716 		[16 ... 18] = "Number of Sounding Dimensions",
   1717 		[19] = "MU Beamformer Capable",
   1718 		[20] = "MU Beamformee Capable",
   1719 		[21] = "TXOP PS",
   1720 		[22] = "+HTC-VHT Capable",
   1721 		[23 ... 25] = "Maximum A-MPDU Length Exponent",
   1722 		[26 ... 27] = "VHT Link Adaptation Capable",
   1723 		[28] = "RX Antenna Pattern Consistency",
   1724 		[29] = "TX Antenna Pattern Consistency",
   1725 		[30 ... 31] = "Extended NSS BW Support",
   1726 	};
   1727 
   1728 	static const char *spatial_streams[] = {
   1729 		"MCS 0-7 supported",
   1730 		"MCS 0-8 supported",
   1731 		"MCS 0-9 supported",
   1732 	};
   1733 
   1734 	uint8_t info_mask[] = { 0xf0, 0x18, 0x78, 0x30 };
   1735 
   1736 	print_attr(level, "%s: len %u", label, size);
   1737 
   1738 	if (size != 12)
   1739 		return;
   1740 
   1741 	print_ie_bitfield(level + 1, "VHT Capabilities Info", data, info_mask,
   1742 				4, vht_capabilities_info_bitfield);
   1743 
   1744 	print_spatial_stream_map(level + 1, "RxVHT-MCS", data + 4,
   1745 					spatial_streams);
   1746 	print_attr(level + 1, "Rx Highest Supported Long GI Data Rate: %d",
   1747 			l_get_le16(data + 6) & 0x1f);
   1748 	print_spatial_stream_map(level + 1, "TxVHT-MCS", data + 8,
   1749 					spatial_streams);
   1750 	print_attr(level + 1, "Tx Highest Supported Long GI Data Rate: %d",
   1751 			l_get_le16(data + 10) & 0x1f);
   1752 	if (test_bit(data + 4, 61))
   1753 		print_attr(level + 1, "VHT Extended NSS BW Capable");
   1754 }
   1755 
   1756 static void print_ie_he_capabilities(unsigned int level,
   1757 					const char *label,
   1758 					const void *data, uint16_t size)
   1759 {
   1760 	const uint8_t *ptr = data;
   1761 	uint8_t width_set = bit_field((ptr + 6)[0], 1, 7);
   1762 	uint8_t mask = 0x3f;
   1763 
   1764 	const char *he_channel_width_bitfield[] = {
   1765 		[0] = "40MHz supported (2.4GHz)",
   1766 		[1] = "40MHz/80MHz supported (5GHz/6GHz)",
   1767 		[2] = "160MHz supported (5GHz/6GHz)",
   1768 		[3] = "160MHz/80+80MHz supported (5GHz/6GHz)",
   1769 		[4] = "242-tone RUs (2.4GHz)",
   1770 		[5] = "242-tone RUs (5GHz/6GHz)",
   1771 	};
   1772 
   1773 	static const char *spatial_streams[] = {
   1774 		"MCS 0-7 supported",
   1775 		"MCS 0-9 supported",
   1776 		"MCS 0-11 supported",
   1777 	};
   1778 
   1779 	print_attr(level, "%s: len %u", label, size);
   1780 
   1781 	print_ie_bitfield(level + 1, "HE supported channel width set",
   1782 			&width_set, &mask, 1, he_channel_width_bitfield);
   1783 
   1784 	if (test_bit(&width_set, 1) || test_bit(&width_set, 0)) {
   1785 		print_spatial_stream_map(level + 1, "Rx HE-MCS Map <= 80MHz",
   1786 						data + 17, spatial_streams);
   1787 		print_spatial_stream_map(level + 1, "Tx HE-MCS Map <= 80MHz",
   1788 						data + 19, spatial_streams);
   1789 	}
   1790 
   1791 	if (test_bit(&width_set, 2)) {
   1792 		print_spatial_stream_map(level + 1, "Rx HE-MCS Map 160MHz",
   1793 						data + 21, spatial_streams);
   1794 		print_spatial_stream_map(level + 1, "Tx HE-MCS Map 160MHz",
   1795 						data + 23, spatial_streams);
   1796 	}
   1797 
   1798 	if (test_bit(&width_set, 3)) {
   1799 		print_spatial_stream_map(level + 1, "Rx HE-MCS Map 80+80MHz",
   1800 						data + 25, spatial_streams);
   1801 		print_spatial_stream_map(level + 1, "Tx HE-MCS Map 80+80MHz",
   1802 						data + 27, spatial_streams);
   1803 	}
   1804 }
   1805 
   1806 static void print_ie_rm_enabled_caps(unsigned int level,
   1807 					const char *label,
   1808 					const void *data, uint16_t size)
   1809 {
   1810 	static const char *capabilities[40] = {
   1811 		[0] = "Link Measurement",
   1812 		[1] = "Neighbor Report",
   1813 		[2] = "Parallel Measurements",
   1814 		[3] = "Repeated Measurements",
   1815 		[4] = "Beacon Passive Measurement",
   1816 		[5] = "Beacon Active Measurement",
   1817 		[6] = "Beacon Table Measurement",
   1818 		[7] = "Beacon Measurement Reporting Conditions",
   1819 		[8] = "Frame Measurement",
   1820 		[9] = "Channel Load Measurement",
   1821 		[10] = "Noise Histogram Measurement",
   1822 		[11] = "Statistics Measurement",
   1823 		[12] = "LCI Measurement",
   1824 		[13] = "LCI Azimuth",
   1825 		[14] = "Transmit Stream / Category Measurement",
   1826 		[15] = "Triggered Transmit Stream / Category Measurement",
   1827 		[16] = "AP Channel Report",
   1828 		[17] = "RM MIB",
   1829 		[27] = "Measurement Pilot Transmission Information",
   1830 		[28] = "Neighbor Report TSF Offset",
   1831 		[29] = "RCPI Measurement capability enabled",
   1832 		[30] = "RSNI Measurement",
   1833 		[31] = "BSS Average Access Delay",
   1834 		[32] = "BSS Available Admission Capacity",
   1835 		[33] = "Antenna capability",
   1836 	};
   1837 	const uint8_t *bytes;
   1838 	uint8_t bytemask1[3] = { 0xff, 0xff, 0x03 };
   1839 	uint8_t bytemask2[2] = { 0xf8, 0x03 };
   1840 	uint8_t byte;
   1841 
   1842 	print_attr(level, "%s: len %u", label, size);
   1843 
   1844 	if (size != 5)
   1845 		return;
   1846 
   1847 	bytes = data;
   1848 
   1849 	print_ie_bitfield(level + 1, "Enabled", bytes,
   1850 				bytemask1, sizeof(bytemask1), capabilities);
   1851 
   1852 	byte = bit_field(bytes[2], 2, 3);
   1853 	print_attr(level + 1, "Operating Channel Max Measurement Duration: %u",
   1854 			byte);
   1855 
   1856 	byte = bit_field(bytes[2], 5, 3);
   1857 	print_attr(level + 1, "Non-Operating Channel Max Measurement "
   1858 			"Duration: %u", byte);
   1859 
   1860 	byte = bit_field(bytes[3], 0, 3);
   1861 	print_attr(level + 1, "Measurement Pilot Capability: %u", byte);
   1862 
   1863 	print_ie_bitfield(level + 1, "Enabled", bytes + sizeof(bytemask1),
   1864 				bytemask2, sizeof(bytemask2), capabilities);
   1865 }
   1866 
   1867 static void print_ie_interworking(unsigned int level,
   1868 					const char *label,
   1869 					const void *data, uint16_t size)
   1870 {
   1871 	const uint8_t *ptr = data;
   1872 	const char *msg;
   1873 	uint8_t type;
   1874 	bool venue = false;
   1875 	bool hessid = false;
   1876 
   1877 	print_attr(level, "%s: len %u", label, size);
   1878 
   1879 	type = bit_field(ptr[0], 0, 3);
   1880 
   1881 	switch (type) {
   1882 	case 0:
   1883 		msg = "Private network";
   1884 		break;
   1885 	case 1:
   1886 		msg = "Private network w/ guest access";
   1887 		break;
   1888 	case 2:
   1889 		msg = "Chargeable public network";
   1890 		break;
   1891 	case 3:
   1892 		msg = "Free public network";
   1893 		break;
   1894 	case 4:
   1895 		msg = "Personal device network";
   1896 		break;
   1897 	case 5:
   1898 		msg = "Emergency services only network";
   1899 		break;
   1900 	case 14:
   1901 		msg = "Test/Experimental";
   1902 		break;
   1903 	case 15:
   1904 		msg = "Wildcard";
   1905 		break;
   1906 	default:
   1907 		return;
   1908 	}
   1909 
   1910 	print_attr(level + 1, "Network Type: %s", msg);
   1911 	print_attr(level + 1, "Internet: %u", test_bit(ptr, 4));
   1912 	print_attr(level + 1, "ASRA: %u", test_bit(ptr, 5));
   1913 	print_attr(level + 1, "ESR: %u", test_bit(ptr, 6));
   1914 	print_attr(level + 1, "UESA: %u", test_bit(ptr, 7));
   1915 
   1916 	size--;
   1917 	ptr++;
   1918 
   1919 	if (size < 2)
   1920 		return;
   1921 
   1922 	/*
   1923 	 * There is venue/hessid info optionally, and no way of determining if
   1924 	 * they exist except looking at the length.
   1925 	 */
   1926 	if (size == 2)
   1927 		venue = true;
   1928 	else if (size == 6)
   1929 		hessid = true;
   1930 	else if (size == 8) {
   1931 		venue = true;
   1932 		hessid = true;
   1933 	}
   1934 
   1935 	if (venue) {
   1936 		switch (ptr[0]) {
   1937 		case 0:
   1938 			msg = "Unspecified";
   1939 			break;
   1940 		case 1:
   1941 			msg = "Assembly";
   1942 			break;
   1943 		case 2:
   1944 			msg = "Business";
   1945 			break;
   1946 		case 3:
   1947 			msg = "Educational";
   1948 			break;
   1949 		case 5:
   1950 			msg = "Factory and Industrial";
   1951 			break;
   1952 		case 6:
   1953 			msg = "Institutional";
   1954 			break;
   1955 		case 7:
   1956 			msg = "Mercantile";
   1957 			break;
   1958 		case 8:
   1959 			msg = "Residential";
   1960 			break;
   1961 		case 9:
   1962 			msg = "Utility and Miscellaneous";
   1963 			break;
   1964 		case 10:
   1965 			msg = "Vehicular";
   1966 			break;
   1967 		case 11:
   1968 			msg = "Outdoor";
   1969 			break;
   1970 		default:
   1971 			return;
   1972 		}
   1973 
   1974 		/*
   1975 		 * Each of the above groups have many group types, but if
   1976 		 * anyone really cares they can cross reference the integer
   1977 		 * type with IEEE 802.11-2016 Table 9-62
   1978 		 */
   1979 		print_attr(level + 1, "Venue: %s, type: %u", msg, ptr[1]);
   1980 
   1981 		ptr += 2;
   1982 		size -= 2;
   1983 	}
   1984 
   1985 	if (hessid)
   1986 		print_attr(level + 1, "HESSID: "MAC, MAC_STR(ptr));
   1987 }
   1988 
   1989 static void print_ie_advertisement(unsigned int level,
   1990 					const char *label,
   1991 					const void *data, uint16_t size)
   1992 {
   1993 	const uint8_t *ptr = data;
   1994 	const char *msg = NULL;
   1995 
   1996 	print_attr(level, "%s: len %u", label, size);
   1997 
   1998 	while (size) {
   1999 		uint8_t qr_len = bit_field(ptr[0], 0, 7);
   2000 		uint8_t id = ptr[1];
   2001 
   2002 		switch (id) {
   2003 		case IE_ADVERTISEMENT_ANQP:
   2004 			msg = "ANQP";
   2005 			break;
   2006 		case IE_ADVERTISEMENT_MIH_SERVICE:
   2007 			msg = "MIH Information Service";
   2008 			break;
   2009 		case IE_ADVERTISEMENT_MIH_DISCOVERY:
   2010 			msg = "MIH Command and Event Services";
   2011 			break;
   2012 		case IE_ADVERTISEMENT_EAS:
   2013 			msg = "EAS";
   2014 			break;
   2015 		case IE_ADVERTISEMENT_RLQP:
   2016 			msg = "RLQP";
   2017 			break;
   2018 		case IE_ADVERTISEMENT_VENDOR_SPECIFIC:
   2019 			msg = "Vendor Specific";
   2020 			break;
   2021 		default:
   2022 			return;
   2023 		}
   2024 
   2025 		if (id == IE_ADVERTISEMENT_VENDOR_SPECIFIC) {
   2026 			size -= ptr[3];
   2027 			ptr += ptr[3];
   2028 		} else {
   2029 			size -= 2;
   2030 			ptr += 2;
   2031 		}
   2032 
   2033 		print_attr(level + 1, "Protocol: %s, Query Resp Limit: %u",
   2034 				msg, qr_len);
   2035 	}
   2036 }
   2037 
   2038 static void print_ie_owe(unsigned int level,
   2039 					const char *label,
   2040 					const void *data, uint16_t size)
   2041 {
   2042 	uint16_t group;
   2043 
   2044 	print_attr(level, "%s: len %u", label, size);
   2045 
   2046 	group = l_get_le16(data);
   2047 
   2048 	print_attr(level + 1, "ECC Group: %u", group);
   2049 	print_attr(level + 1, "Public Key:");
   2050 	print_hexdump(level + 2, data + 2, size - 2);
   2051 }
   2052 
   2053 static void print_fils_indication(unsigned int level,
   2054 					const char *label,
   2055 					const void *data, uint16_t size)
   2056 {
   2057 	const uint8_t *bytes = data;
   2058 
   2059 	print_attr(level, "FILS Indication: len %u", size);
   2060 
   2061 	print_attr(level + 1, "Num Public Key Identifiers: %u",
   2062 				bit_field(*bytes, 0, 3));
   2063 	print_attr(level + 1, "Num Realm Identifiers: %u",
   2064 				bit_field(*bytes, 3, 3));
   2065 	print_attr(level + 1, "IP configuration: %u", test_bit(bytes, 6));
   2066 	print_attr(level + 1, "Cache Identifier Included: %u",
   2067 				test_bit(bytes, 7));
   2068 
   2069 	bytes++;
   2070 
   2071 	print_attr(level + 1, "HES-SID Included: %u", test_bit(bytes, 0));
   2072 	print_attr(level + 1, "SK Auth without PFS supported: %u",
   2073 				test_bit(bytes, 1));
   2074 	print_attr(level + 1, "SK Auth with PFS supported: %u",
   2075 				test_bit(bytes, 2));
   2076 	print_attr(level + 1, "PK Auth supported: %u", test_bit(bytes, 3));
   2077 
   2078 	bytes++;
   2079 
   2080 	print_hexdump(level + 1, bytes, size - 2);
   2081 }
   2082 
   2083 static void print_fils_key_confirmation(unsigned int level, const char *label,
   2084 					const void *data, uint16_t size)
   2085 {
   2086 	print_attr(level, "FILS Key Confirmation (KeyAuth): len %u", size);
   2087 }
   2088 
   2089 static void print_fils_session(unsigned int level, const char *label,
   2090 					const void *data, uint16_t size)
   2091 {
   2092 	print_attr(level, "FILS Session: len %u", size);
   2093 }
   2094 
   2095 static void print_ie_supported_operating_classes(unsigned int level,
   2096 							const char *label,
   2097 							const void *data,
   2098 							uint16_t size)
   2099 {
   2100 	const void *end = data + size;
   2101 
   2102 	if (size < 1) {
   2103 		print_ie_error(level, label, size, -EINVAL);
   2104 		return;
   2105 	}
   2106 
   2107 	print_attr(level, "Current Operating Class: %u", l_get_u8(data));
   2108 
   2109 	data += 1;
   2110 
   2111 	while (end - data) {
   2112 		uint8_t cls = l_get_u8(data);
   2113 
   2114 		/*
   2115 		 * TODO: Support Current Operating Class Extension Sequence
   2116 		 *       and Operating Class Duple Sequence
   2117 		*/
   2118 		if (cls == 130 || cls == 0) {
   2119 			data = end;
   2120 			break;
   2121 		}
   2122 
   2123 		print_attr(level, "Supported Operating Class: %u", cls);
   2124 		data += 1;
   2125 	}
   2126 
   2127 	if (end - data)
   2128 		print_ie_error(level, label, size, -EINVAL);
   2129 }
   2130 
   2131 static void print_qos_map(unsigned int level, const char *label,
   2132 							const void *data,
   2133 							uint16_t size)
   2134 {
   2135 	print_attr(level, "QoS Map");
   2136 }
   2137 
   2138 static void print_measurement_request_beacon(unsigned int level,
   2139 						const void *data,
   2140 						uint16_t size)
   2141 {
   2142 	uint8_t mode;
   2143 
   2144 	if (size < 13)
   2145 		return;
   2146 
   2147 	print_attr(level, "Operating Class: %u", l_get_u8(data));
   2148 	print_attr(level, "Channel: %u", l_get_u8(data + 1));
   2149 	print_attr(level, "Randomization Interval: %u", l_get_le16(data + 2));
   2150 	print_attr(level, "Duration: %u", l_get_le16(data + 4));
   2151 
   2152 	mode = l_get_u8(data + 6);
   2153 
   2154 	switch (mode) {
   2155 	case 0:
   2156 		print_attr(level, "Measurement: Passive");
   2157 		break;
   2158 	case 1:
   2159 		print_attr(level, "Measurement: Active");
   2160 		break;
   2161 	case 2:
   2162 		print_attr(level, "Measurement: Table");
   2163 		break;
   2164 	default:
   2165 		print_attr(level, "Measurement: Invalid (%u)", mode);
   2166 		return;
   2167 	}
   2168 
   2169 	print_attr(level, "BSSID: "MAC, MAC_STR(((const uint8_t *)data + 7)));
   2170 }
   2171 
   2172 static const char *rrm_measurement_types[] = {
   2173 	[0] = "Basic",
   2174 	[1] = "Clear Channel Assessment",
   2175 	[2] = "Receive Power Indication",
   2176 	[3] = "Channel Load",
   2177 	[4] = "Noise Histogram",
   2178 	[5] = "Beacon",
   2179 	[6] = "Frame",
   2180 	[7] = "STA Statistics",
   2181 	[8] = "LCI",
   2182 	[9] = "Transmit Stream/Category Measurement",
   2183 	[10] = "Multicast Diagnostics",
   2184 	[11] = "Location Civic",
   2185 	[12] = "Location Identifier",
   2186 	[13] = "Directional Channel Quality",
   2187 	[14] = "Directional Measurement",
   2188 	[15] = "Directional Statistics",
   2189 	[16] = "Fine Timing Measurement Range",
   2190 	[255] = "Measurement Pause"
   2191 };
   2192 
   2193 static void print_measurement_request(unsigned int level, const char *label,
   2194 							const void *data,
   2195 							uint16_t size)
   2196 {
   2197 	uint8_t mode;
   2198 	uint8_t type;
   2199 
   2200 	print_attr(level, "Measurement Request");
   2201 
   2202 	if (size < 3)
   2203 		return;
   2204 
   2205 	print_attr(level + 1, "Token: %u", l_get_u8(data));
   2206 
   2207 	mode = l_get_u8(data + 1);
   2208 	print_attr(level + 1, "Request Mode: %u", mode);
   2209 
   2210 	if (test_bit(&mode, 0))
   2211 		print_attr(level + 2, "Parallel bit set");
   2212 
   2213 	if (test_bit(&mode, 1))
   2214 		print_attr(level + 2, "Enable bit set");
   2215 
   2216 	if (test_bit(&mode, 2))
   2217 		print_attr(level + 2, "Request bit set");
   2218 
   2219 	if (test_bit(&mode, 3))
   2220 		print_attr(level + 2, "Report bit set");
   2221 
   2222 	if (test_bit(&mode, 4))
   2223 		print_attr(level + 2, "Duration Mandatory set");
   2224 
   2225 	type = l_get_u8(data + 2);
   2226 
   2227 	if (type > 16 && type != 255) {
   2228 		print_attr(level + 1, "Type: Invalid (%u)", type);
   2229 		return;
   2230 	}
   2231 
   2232 	print_attr(level + 1, "Type: %s", rrm_measurement_types[type]);
   2233 
   2234 	switch (type) {
   2235 	case 5:
   2236 		print_measurement_request_beacon(level + 1, data + 3, size - 3);
   2237 		break;
   2238 	}
   2239 }
   2240 
   2241 static void print_measurement_report_beacon(unsigned int level,
   2242 						const void *data,
   2243 						uint16_t size)
   2244 {
   2245 	uint8_t frame_info;
   2246 
   2247 	if (size < 26)
   2248 		return;
   2249 
   2250 	print_attr(level, "Operating Class: %u", l_get_u8(data));
   2251 	print_attr(level, "Channel: %u", l_get_u8(data + 1));
   2252 	print_attr(level, "Start Time: %"PRIu64, l_get_le64(data + 2));
   2253 	print_attr(level, "Duration: %u", l_get_le16(data + 10));
   2254 
   2255 	frame_info = l_get_u8(data + 12);
   2256 
   2257 	print_attr(level, "PHY Type: %u", bit_field(frame_info, 0, 7));
   2258 	print_attr(level, "Frame Type: %u", test_bit(&frame_info, 7));
   2259 	print_attr(level, "RCPI: %u", l_get_u8(data + 13));
   2260 	print_attr(level, "RSNI: %u", l_get_u8(data + 14));
   2261 	print_attr(level, "BSSID: "MAC, MAC_STR(((const uint8_t *)data + 15)));
   2262 	print_attr(level, "Antenna ID: %u", l_get_u8(data + 21));
   2263 	print_attr(level, "Parent TSF: %u", l_get_le32(data + 22));
   2264 }
   2265 
   2266 static void print_measurement_report(unsigned int level, const char *label,
   2267 							const void *data,
   2268 							uint16_t size)
   2269 {
   2270 	uint8_t mode;
   2271 	uint8_t type;
   2272 
   2273 	print_attr(level, "Measurement Report");
   2274 
   2275 	if (size < 3)
   2276 		return;
   2277 
   2278 	print_attr(level + 1, "Token: %u", l_get_u8(data));
   2279 
   2280 	mode = l_get_u8(data + 1);
   2281 	print_attr(level + 1, "Report Mode: %u", mode);
   2282 
   2283 	if (test_bit(&mode, 0))
   2284 		print_attr(level + 2, "Late bit set");
   2285 
   2286 	if (test_bit(&mode, 1))
   2287 		print_attr(level + 2, "Incapable bit set");
   2288 
   2289 	if (test_bit(&mode, 2))
   2290 		print_attr(level + 2, "Refused bit set");
   2291 
   2292 	type = l_get_u8(data + 2);
   2293 
   2294 	if (type > 16 && type != 255) {
   2295 		print_attr(level + 1, "Type: Invalid (%u)", type);
   2296 		return;
   2297 	}
   2298 
   2299 	print_attr(level + 1, "Type: %s", rrm_measurement_types[type]);
   2300 
   2301 	switch (type) {
   2302 	case 5:
   2303 		print_measurement_report_beacon(level + 1, data + 3, size - 3);
   2304 		break;
   2305 	}
   2306 }
   2307 
   2308 static void print_fast_bss_transition(unsigned int level, const char *label,
   2309 							const void *data,
   2310 							uint16_t size)
   2311 {
   2312 	struct ie_ft_info ft_info;
   2313 	uint32_t mic_len = 16;
   2314 
   2315 	/* Most likely the MIC is 16 bytes */
   2316 	if (ie_parse_fast_bss_transition_from_data(data - 2, size + 2,
   2317 						mic_len, &ft_info) < 0) {
   2318 		mic_len = 24;
   2319 
   2320 		/* Must be FILS, try 24 */
   2321 		if (ie_parse_fast_bss_transition_from_data(data - 2, size + 2,
   2322 						mic_len, &ft_info) < 0) {
   2323 			print_attr(level + 1, "error parsing");
   2324 			return;
   2325 		}
   2326 	}
   2327 
   2328 	print_attr(level, "%s", label);
   2329 
   2330 	if (ft_info.rsnxe_used)
   2331 		print_attr(level + 1, "RSNXE Used set");
   2332 
   2333 	print_attr(level + 1, "MIC Element Count: %u",
   2334 			ft_info.mic_element_count);
   2335 	print_attr(level + 1, "MIC:");
   2336 	print_hexdump(level + 2, ft_info.mic, mic_len);
   2337 	print_attr(level + 1, "ANONCE:");
   2338 	print_hexdump(level + 2, ft_info.anonce, sizeof(ft_info.anonce));
   2339 	print_attr(level + 1, "SNONCE:");
   2340 	print_hexdump(level + 2, ft_info.snonce, sizeof(ft_info.snonce));
   2341 	print_attr(level + 1, "R0KHID:");
   2342 	print_hexdump(level + 2, ft_info.r0khid, ft_info.r0khid_len);
   2343 
   2344 	if (ft_info.r1khid_present) {
   2345 		print_attr(level + 1, "R1KHID:");
   2346 		print_hexdump(level + 2, ft_info.r1khid,
   2347 						sizeof(ft_info.r1khid));
   2348 	}
   2349 
   2350 	if (ft_info.gtk_len) {
   2351 		print_attr(level + 1, "GTK Key ID: %u", ft_info.gtk_key_id);
   2352 		print_attr(level + 1, "GTK RSC:");
   2353 		print_hexdump(level + 2, ft_info.gtk_rsc,
   2354 						sizeof(ft_info.gtk_rsc));
   2355 		print_attr(level + 1, "GTK:");
   2356 		print_hexdump(level + 2, ft_info.gtk, ft_info.gtk_len);
   2357 	}
   2358 
   2359 	if (ft_info.igtk_len) {
   2360 		print_attr(level + 1, "IGTK Key ID: %u", ft_info.igtk_key_id);
   2361 		print_attr(level + 1, "IGTK IPN:");
   2362 		print_hexdump(level + 2, ft_info.igtk_ipn,
   2363 						sizeof(ft_info.igtk_ipn));
   2364 		print_attr(level + 1, "IGTK:");
   2365 		print_hexdump(level + 2, ft_info.igtk, ft_info.igtk_len);
   2366 	}
   2367 }
   2368 
   2369 static void print_mobility_domain(unsigned int level, const char *label,
   2370 					const void *data, uint16_t size)
   2371 {
   2372 	uint16_t mdid;
   2373 	bool ft_over_ds;
   2374 	bool resource_req;
   2375 
   2376 	print_attr(level, "%s", label);
   2377 
   2378 	if (ie_parse_mobility_domain_from_data(data - 2, size + 2, &mdid,
   2379 					&ft_over_ds, &resource_req) < 0) {
   2380 		print_attr(level + 1, "error parsing");
   2381 		return;
   2382 	}
   2383 
   2384 	print_attr(level + 1, "MDID: %04x", mdid);
   2385 
   2386 	if (ft_over_ds)
   2387 		print_attr(level + 1, "FT-over-DS bit set");
   2388 
   2389 	if (resource_req)
   2390 		print_attr(level + 1, "Resource Request Protocol bit set");
   2391 }
   2392 
   2393 static void print_rsnx(unsigned int level, const char *label,
   2394 					const void *data, uint16_t size)
   2395 {
   2396 	const uint8_t *ptr = data;
   2397 	uint8_t field_size = bit_field(ptr[0], 0, 4);
   2398 
   2399 	print_attr(level, "%s", label);
   2400 
   2401 	print_attr(level + 1, "Field Size: %u", field_size);
   2402 
   2403 	if (test_bit(ptr, 4))
   2404 		print_attr(level + 1, "Protected TWT Operations Support");
   2405 
   2406 	if (test_bit(ptr, 5))
   2407 		print_attr(level + 1, "SAE Hash-to-Element");
   2408 }
   2409 
   2410 static void print_bss_parameters(unsigned int level, uint8_t parameters)
   2411 {
   2412 	print_attr(level, "BSS Parameters");
   2413 
   2414 	if (test_bit(&parameters, 0))
   2415 		print_attr(level + 1, "OCT Recommended");
   2416 
   2417 	if (test_bit(&parameters, 1))
   2418 		print_attr(level + 1, "Same SSID");
   2419 
   2420 	if (test_bit(&parameters, 2))
   2421 		print_attr(level + 1, "Multiple BSSID");
   2422 
   2423 	if (test_bit(&parameters, 3))
   2424 		print_attr(level + 1, "Transmitted BSSID");
   2425 
   2426 	if (test_bit(&parameters, 4))
   2427 		print_attr(level + 1, "2.4/5GHz Co-Located ESS member");
   2428 
   2429 	if (test_bit(&parameters, 5))
   2430 		print_attr(level + 1, "Unsolicited Probe Responses Active");
   2431 
   2432 	if (test_bit(&parameters, 6))
   2433 		print_attr(level + 1, "Co-Located AP");
   2434 }
   2435 
   2436 static void print_reduced_neighbor_report(unsigned int level, const char *label,
   2437 					const void *data, uint16_t size)
   2438 {
   2439 	const uint8_t *ptr = data;
   2440 	unsigned int field_count = 0;
   2441 
   2442 	print_attr(level, "%s", label);
   2443 
   2444 	if (size < 5) {
   2445 		print_attr(level + 1, "error parsing");
   2446 		return;
   2447 	}
   2448 
   2449 	level++;
   2450 
   2451 	while (ptr < (uint8_t *)data + size) {
   2452 		uint16_t hdr = l_get_le16(ptr);
   2453 		uint8_t oper = l_get_u8(ptr + 2);
   2454 		uint8_t chan = l_get_u8(ptr + 3);
   2455 		uint8_t count = bit_field((uint8_t)(hdr & 0xff), 4, 4);
   2456 		uint8_t type = bit_field((uint8_t)(hdr & 0xff), 0, 2);
   2457 		uint8_t filtered = test_bit(&hdr, 2);
   2458 		uint8_t info_size = (hdr & 0xff00) >> 8;
   2459 		uint8_t i;
   2460 
   2461 		ptr += 4;
   2462 
   2463 		print_attr(level, "Field #%u", field_count);
   2464 
   2465 		/* TBTT Information Header */
   2466 		print_attr(level + 1, "Info Field Type: %u", type);
   2467 		print_attr(level + 1, "Filtered Neighbor AP: %u", filtered);
   2468 		print_attr(level + 1, "Information Count: %u", count);
   2469 		print_attr(level + 1, "Information Length: %u", info_size);
   2470 
   2471 		print_attr(level + 1, "Operating Class: %u", oper);
   2472 		print_attr(level + 1, "Channel: %u", chan);
   2473 
   2474 		level++;
   2475 
   2476 		/* Information Set(s) */
   2477 		for (i = 0; i <= count; i++) {
   2478 			const uint8_t *info = ptr;
   2479 
   2480 			if (info + info_size > (uint8_t *)data + size ||
   2481 					info_size == 0 || info_size == 3 ||
   2482 					info_size == 4 || info_size == 10) {
   2483 				print_attr(level + 1, "error parsing");
   2484 				return;
   2485 			}
   2486 
   2487 			print_attr(level, "Information Set #%u", i);
   2488 
   2489 			if (info_size >= 1)
   2490 				print_attr(level + 1,
   2491 						"Neighbor TBTT Offset: %u TU's",
   2492 						l_get_u8(info++));
   2493 
   2494 			if (info_size >= 7) {
   2495 				print_attr(level + 1, "BSSID: "MAC,
   2496 						MAC_STR(info));
   2497 				info += 6;
   2498 			}
   2499 
   2500 			if (info_size == 5 || info_size == 6 ||
   2501 					info_size >= 11) {
   2502 				print_attr(level + 1, "Short SSID: %08x",
   2503 						l_get_u32(info));
   2504 				info += 4;
   2505 			}
   2506 
   2507 			if (info_size == 2 || info_size == 6 ||
   2508 					info_size == 8 || info_size == 9 ||
   2509 					info_size >= 12)
   2510 				print_bss_parameters(level + 1, *info++);
   2511 
   2512 			if (info_size == 9 || info_size >= 13)
   2513 				print_attr(level + 1, "20 MHz PSD: %d",
   2514 						(int8_t)*info++);
   2515 
   2516 			ptr += info_size;
   2517 		}
   2518 
   2519 		level--;
   2520 
   2521 		field_count++;
   2522 	}
   2523 }
   2524 
   2525 static void print_neighbor_report(unsigned int level, const char *label,
   2526 					const void *data, uint16_t size)
   2527 {
   2528 	struct ie_tlv_iter iter;
   2529 	struct ie_neighbor_report_info info;
   2530 
   2531 	const char *phy_type_table[] = {
   2532 		[0] = NULL,
   2533 		[1] = NULL,
   2534 		[2] = "DSSS",
   2535 		[3] = NULL,
   2536 		[4] = "OFDM",
   2537 		[5] = "HDRSSS",
   2538 		[6] = "ERP",
   2539 		[7] = "HT",
   2540 		[8] = "DMG",
   2541 		[9] = "VHT",
   2542 		[10] = "TVHT",
   2543 		[11] = "S1G",
   2544 		[12] = "CDMG",
   2545 		[13] = "CMMG",
   2546 		[14] = "HE"
   2547 	};
   2548 
   2549 	ie_tlv_iter_init(&iter, data - 2, size + 2);
   2550 
   2551 	if (!ie_tlv_iter_next(&iter))
   2552 		return;
   2553 
   2554 	if (ie_parse_neighbor_report(&iter, &info) < 0) {
   2555 		print_attr(level, "Invalid Neighbor report");
   2556 		return;
   2557 	}
   2558 
   2559 	print_attr(level, "Neighbor Report for "MAC, MAC_STR(info.addr));
   2560 	print_attr(level + 1, "Operating Class: %u", info.oper_class);
   2561 	print_attr(level + 1, "Channel Number: %u", info.channel_num);
   2562 
   2563 	if (info.phy_type <= 14 && phy_type_table[info.phy_type])
   2564 		print_attr(level + 1, "Phy Type: %s",
   2565 				phy_type_table[info.phy_type]);
   2566 	else
   2567 		print_attr(level + 1, "Phy Type: Unknown (%u)", info.phy_type);
   2568 
   2569 	if (info.bss_transition_pref_present)
   2570 		print_attr(level + 1, "BSS Transition Preference: %u",
   2571 				info.bss_transition_pref);
   2572 
   2573 	switch (info.reachable) {
   2574 	case 1:
   2575 		print_attr(level + 1, "Reachability: Not Reachable");
   2576 		break;
   2577 	case 2:
   2578 		print_attr(level + 1, "Reachability: Unknown");
   2579 		break;
   2580 	case 3:
   2581 		print_attr(level + 1, "Reachability: Reachable");
   2582 		break;
   2583 	default:
   2584 		break;
   2585 	}
   2586 
   2587 	print_attr(level + 1, "BSSID Information");
   2588 
   2589 	if (info.security)
   2590 		print_attr(level + 2, "Security bit set");
   2591 	if (info.key_scope)
   2592 		print_attr(level + 2, "Key scope bit set");
   2593 	if (info.spectrum_mgmt)
   2594 		print_attr(level + 2, "Spectrum Mgmt bit set");
   2595 	if (info.qos)
   2596 		print_attr(level + 2, "QoS bit set");
   2597 	if (info.apsd)
   2598 		print_attr(level + 2, "APSD bit set");
   2599 	if (info.md)
   2600 		print_attr(level + 2, "MD bit set");
   2601 	if (info.ht)
   2602 		print_attr(level + 2, "HT bit set");
   2603 }
   2604 
   2605 static struct attr_entry ie_entry[] = {
   2606 	{ IE_TYPE_SSID,				"SSID",
   2607 		ATTR_CUSTOM,	{ .function = print_ie_ssid } },
   2608 	{ IE_TYPE_SUPPORTED_RATES,		"Supported rates",
   2609 		ATTR_CUSTOM,	{ .function = print_ie_rate } },
   2610 	{ IE_TYPE_DSSS_PARAMETER_SET,		"DSSS parameter set",
   2611 		ATTR_CUSTOM,	{ .function = print_ie_ds } },
   2612 	{ IE_TYPE_TIM,				"TIM",
   2613 		ATTR_CUSTOM,	{ .function = print_ie_tim } },
   2614 	{ IE_TYPE_COUNTRY,			"Country",
   2615 		ATTR_CUSTOM,	{ .function = print_ie_country } },
   2616 	{ IE_TYPE_BSS_LOAD,			"BSS load",
   2617 		ATTR_CUSTOM,	{ .function = print_ie_bss_load } },
   2618 	{ IE_TYPE_POWER_CONSTRAINT,		"Power constraint",
   2619 		ATTR_CUSTOM,	{ .function = print_ie_power_constraint } },
   2620 	{ IE_TYPE_TPC_REPORT,			"TPC report",
   2621 		ATTR_CUSTOM,	{ .function = print_ie_tpc } },
   2622 	{ IE_TYPE_ERP,				"ERP Information",
   2623 		ATTR_CUSTOM,	{ .function = print_ie_erp } },
   2624 	{ IE_TYPE_RSN,				"RSN",
   2625 		ATTR_CUSTOM,	{ .function = print_ie_rsn } },
   2626 	{ IE_TYPE_EXTENDED_SUPPORTED_RATES,	"Extended supported rates",
   2627 		ATTR_CUSTOM,	{ .function = print_ie_rate } },
   2628 	{ IE_TYPE_HT_OPERATION,			"HT Operation",
   2629 		ATTR_CUSTOM,	{ .function = print_ie_ht_operation } },
   2630 	{ IE_TYPE_VHT_OPERATION,		"VHT Operation",
   2631 		ATTR_CUSTOM,	{ .function = print_ie_vht_operation } },
   2632 	{ IE_TYPE_VENDOR_SPECIFIC,		"Vendor specific",
   2633 		ATTR_CUSTOM,	{ .function = print_ie_vendor } },
   2634 	{ IE_TYPE_EXTENDED_CAPABILITIES,	"Extended Capabilities",
   2635 		ATTR_CUSTOM,	{ .function = print_ie_extended_capabilities } },
   2636 	{ IE_TYPE_HT_CAPABILITIES,		"HT Capabilities",
   2637 		ATTR_CUSTOM,	{ .function = print_ie_ht_capabilities } },
   2638 	{ IE_TYPE_VHT_CAPABILITIES,		"VHT Capabilities",
   2639 		ATTR_CUSTOM,	{ .function = print_ie_vht_capabilities } },
   2640 	{ IE_TYPE_HE_CAPABILITIES, 		"HE Capabilities",
   2641 		ATTR_CUSTOM,	{ .function = print_ie_he_capabilities } },
   2642 	{ IE_TYPE_RM_ENABLED_CAPABILITIES,	"RM Enabled Capabilities",
   2643 		ATTR_CUSTOM,	{ .function = print_ie_rm_enabled_caps } },
   2644 	{ IE_TYPE_INTERWORKING,			"Interworking",
   2645 		ATTR_CUSTOM,	{ .function = print_ie_interworking } },
   2646 	{ IE_TYPE_ADVERTISEMENT_PROTOCOL,	"Advertisement Protocol",
   2647 		ATTR_CUSTOM,	{ .function = print_ie_advertisement } },
   2648 	{ IE_TYPE_OWE_DH_PARAM,			"OWE Diffie-Hellman Parameter",
   2649 		ATTR_CUSTOM,	{ .function = print_ie_owe } },
   2650 	{ IE_TYPE_FILS_INDICATION,		"FILS Indication",
   2651 		ATTR_CUSTOM,	{ .function = print_fils_indication } },
   2652 	{ IE_TYPE_FILS_KEY_CONFIRMATION,	"FILS Key Confirmation",
   2653 		ATTR_CUSTOM,	{ .function = print_fils_key_confirmation } },
   2654 	{ IE_TYPE_FILS_SESSION,			"FILS Session",
   2655 		ATTR_CUSTOM,	{ .function = print_fils_session } },
   2656 	{ IE_TYPE_SUPPORTED_OPERATING_CLASSES,	"Supported Operating Classes",
   2657 		ATTR_CUSTOM,
   2658 		{ .function = print_ie_supported_operating_classes } },
   2659 	{ IE_TYPE_QOS_MAP_SET,			"QoS Map",
   2660 		ATTR_CUSTOM,	{ .function = print_qos_map } },
   2661 	{ IE_TYPE_MEASUREMENT_REQUEST,		"Measurement Request",
   2662 		ATTR_CUSTOM,	{ .function = print_measurement_request } },
   2663 	{ IE_TYPE_MEASUREMENT_REPORT,		"Measurement Report",
   2664 		ATTR_CUSTOM,	{ .function = print_measurement_report } },
   2665 	{ IE_TYPE_FAST_BSS_TRANSITION,		"Fast BSS Transition",
   2666 		ATTR_CUSTOM,	{ .function = print_fast_bss_transition } },
   2667 	{ IE_TYPE_MOBILITY_DOMAIN,		"Mobility Domain",
   2668 		ATTR_CUSTOM,	{ .function = print_mobility_domain } },
   2669 	{ IE_TYPE_REDUCED_NEIGHBOR_REPORT,	"Reduced Neighbor Report",
   2670 		ATTR_CUSTOM,	{ .function = print_reduced_neighbor_report } },
   2671 	{ IE_TYPE_RSNX,				"RSNX",
   2672 		ATTR_CUSTOM,	{ .function = print_rsnx } },
   2673 	{ IE_TYPE_NEIGHBOR_REPORT,		"Neighbor Report",
   2674 		ATTR_CUSTOM,	{ .function = print_neighbor_report } },
   2675 	{ },
   2676 };
   2677 
   2678 static void print_ie(unsigned int level, const char *label,
   2679 					const void *data, uint16_t size)
   2680 {
   2681 	struct ie_tlv_iter iter;
   2682 	int i;
   2683 
   2684 	print_attr(level, "%s: len %u", label, size);
   2685 
   2686 	ie_tlv_iter_init(&iter, data, size);
   2687 
   2688 	while (ie_tlv_iter_next(&iter)) {
   2689 		uint16_t tag = ie_tlv_iter_get_tag(&iter);
   2690 		struct attr_entry *entry = NULL;
   2691 
   2692 		for (i = 0; ie_entry[i].str; i++) {
   2693 			if (ie_entry[i].attr == tag) {
   2694 				entry = &ie_entry[i];
   2695 				break;
   2696 			}
   2697 		}
   2698 
   2699 		if (cur_nlmon && cur_nlmon->noies && tag != IE_TYPE_SSID)
   2700 			continue;
   2701 
   2702 		if (entry && entry->function)
   2703 			entry->function(level + 1, entry->str,
   2704 					iter.data, iter.len);
   2705 		else
   2706 			print_attr(level + 1, "Tag %u: len %u", tag,
   2707 					iter.len);
   2708 
   2709 		print_hexdump(level + 2, iter.data, iter.len);
   2710 	}
   2711 }
   2712 
   2713 static void print_wsc_byte(unsigned int level, const char *label,
   2714 				const void *data, uint16_t size)
   2715 {
   2716 	const uint8_t *bytes = data;
   2717 
   2718 	if (size != 1) {
   2719 		printf("malformed packet\n");
   2720 		return;
   2721 	}
   2722 
   2723 	print_attr(level, "%s: %u", label, bytes[0]);
   2724 }
   2725 
   2726 static void print_wsc_bytes(unsigned int level, const char *label,
   2727 				const void *data, uint16_t size)
   2728 {
   2729 	print_attr(level, "%s: (len: %d)", label, size);
   2730 	print_hexdump(level + 1, data, size);
   2731 }
   2732 
   2733 static void print_wsc_bool(unsigned int level, const char *label,
   2734 				const void *data, uint16_t size)
   2735 {
   2736 	const uint8_t *bytes = data;
   2737 
   2738 	if (size != 1) {
   2739 		printf("malformed packet\n");
   2740 		return;
   2741 	}
   2742 
   2743 	print_attr(level, "%s: %s", label, bytes[0] ? "True" : "False");
   2744 }
   2745 
   2746 static void print_wsc_ascii_string(unsigned int level, const char *label,
   2747 					const void *data, uint16_t size,
   2748 					uint16_t max_len)
   2749 {
   2750 	const char *p = data;
   2751 	unsigned int i;
   2752 
   2753 	if (size >= max_len) {
   2754 		printf("malformed packet\n");
   2755 		return;
   2756 	}
   2757 
   2758 	for (i = 0; i < size; i++) {
   2759 		if (!p[i])
   2760 			break;
   2761 
   2762 		if (!l_ascii_isprint(p[i]))
   2763 			goto invalid_ascii;
   2764 	}
   2765 
   2766 	print_attr(level, "%s: %.*s", label, i, p);
   2767 	return;
   2768 
   2769 invalid_ascii:
   2770 	print_attr(level, "%s: (Non-Ascii, len: %d)", label, size);
   2771 	print_hexdump(level + 1, data, size);
   2772 }
   2773 
   2774 static void print_wsc_utf8_string(unsigned int level, const char *label,
   2775 					const void *data, uint16_t size,
   2776 					uint16_t max_len)
   2777 {
   2778 	const char *p = data;
   2779 	unsigned int i;
   2780 
   2781 	if (size >= max_len) {
   2782 		printf("malformed packet\n");
   2783 		return;
   2784 	}
   2785 
   2786 	for (i = 0; i < size; i++) {
   2787 		if (!p[i])
   2788 			break;
   2789 	}
   2790 
   2791 	if (!l_utf8_validate((const char *) p, i, NULL))
   2792 		goto invalid_utf8;
   2793 
   2794 	print_attr(level, "%s: %.*s", label, i, p);
   2795 	return;
   2796 
   2797 invalid_utf8:
   2798 	print_attr(level, "%s: (Non-utf8, len: %d)", label, size);
   2799 	print_hexdump(level + 1, data, size);
   2800 }
   2801 
   2802 static void print_wsc_uuid(unsigned int level, const char *label,
   2803 				const void *data, uint16_t size)
   2804 {
   2805 	const uint8_t *bytes = data;
   2806 
   2807 	if (size != 16) {
   2808 		printf("malformed packet\n");
   2809 		return;
   2810 	}
   2811 
   2812 	print_attr(level, "%s: %02x%02x%02x%02x-%02x%02x-%02x%02x-"
   2813 				"%02x%02x-%02x%02x%02x%02x%02x%02x",
   2814 				label,
   2815 				bytes[0], bytes[1], bytes[2], bytes[3],
   2816 				bytes[4], bytes[5], bytes[6], bytes[7],
   2817 				bytes[8], bytes[9], bytes[10], bytes[11],
   2818 				bytes[12], bytes[13], bytes[14], bytes[15]);
   2819 }
   2820 
   2821 static void print_wsc_association_state(unsigned int level, const char *label,
   2822 					const void *data, uint16_t size)
   2823 {
   2824 	uint16_t state;
   2825 	static const char *state_table[] = {
   2826 		"Not Associated",
   2827 		"Connection Success",
   2828 		"Configuration Failure",
   2829 		"Association Failure",
   2830 		"IP Failure",
   2831 	};
   2832 
   2833 	if (size != 2) {
   2834 		printf("malformed packet\n");
   2835 		return;
   2836 	}
   2837 
   2838 	state = l_get_be16(data);
   2839 
   2840 	if (state > 4)
   2841 		print_attr(level, "%s: Reserved", label);
   2842 	else
   2843 		print_attr(level, "%s: %s", label, state_table[state]);
   2844 }
   2845 
   2846 static void print_wsc_auth_type_flags(unsigned int level, const char *label,
   2847 					const void *data, uint16_t size)
   2848 {
   2849 	uint16_t v;
   2850 
   2851 	if (size != 2)
   2852 		return;
   2853 
   2854 	v = l_get_be16(data);
   2855 	print_attr(level, "%s:", label);
   2856 
   2857 	if (v & WSC_AUTHENTICATION_TYPE_OPEN)
   2858 		print_attr(level + 1, "Open");
   2859 
   2860 	if (v & WSC_AUTHENTICATION_TYPE_WPA_PERSONAL)
   2861 		print_attr(level + 1, "WPA-Personal");
   2862 
   2863 	if (v & WSC_AUTHENTICATION_TYPE_SHARED)
   2864 		print_attr(level + 1, "Shared");
   2865 
   2866 	if (v & WSC_AUTHENTICATION_TYPE_WPA_ENTERPRISE)
   2867 		print_attr(level + 1, "WPA-Enterprise");
   2868 
   2869 	if (v & WSC_AUTHENTICATION_TYPE_WPA2_ENTERPRISE)
   2870 		print_attr(level + 1, "WPA2-Enterprise");
   2871 
   2872 	if (v & WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL)
   2873 		print_attr(level + 1, "WPA2-Personal");
   2874 
   2875 	if (v & 0xffc0)
   2876 		print_attr(level + 1, "Unknown: %04x", v & 0xffc0);
   2877 }
   2878 
   2879 static void print_wsc_authenticator(unsigned int level, const char *label,
   2880 					const void *data, uint16_t size)
   2881 {
   2882 	const uint8_t *a = data;
   2883 
   2884 	if (size != 8)
   2885 		return;
   2886 
   2887 	print_attr(level, "%s: %02x%02x%02x%02x%02x%02x%02x%02x",
   2888 			label, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
   2889 }
   2890 
   2891 static void print_wsc_configuration_error(unsigned int level, const char *label,
   2892 						const void *data, uint16_t size)
   2893 {
   2894 	uint16_t error;
   2895 	static const char *error_table[] = {
   2896 		"No Error",
   2897 		"OOB Interface Read Error",
   2898 		"Decryption CRC Failure",
   2899 		"2.4 channel not supported",
   2900 		"5.0 channel not supported",
   2901 		"Signal too weak",
   2902 		"Network auth failure",
   2903 		"Network association failure",
   2904 		"No DHCP response",
   2905 		"Failed DHCP config",
   2906 		"IP Address conflict",
   2907 		"Couldn't connect to Registrar",
   2908 		"Multiple PBC sessions detected",
   2909 		"Rogue activity suspected",
   2910 		"Device busy",
   2911 		"Setup locked",
   2912 		"Message timeout",
   2913 		"Registration session timeout",
   2914 		"Device Password Auth Failure",
   2915 		"60 GHz channel not supported",
   2916 		"Public Key Hash Mismatch",
   2917 	};
   2918 
   2919 	if (size != 2) {
   2920 		printf("malformed packet\n");
   2921 		return;
   2922 	}
   2923 
   2924 	error = l_get_be16(data);
   2925 
   2926 	if (error > 20)
   2927 		print_attr(level, "%s: Reserved", label);
   2928 	else
   2929 		print_attr(level, "%s: %s", label, error_table[error]);
   2930 }
   2931 
   2932 static void print_wsc_config_methods(unsigned int level, const char *label,
   2933 					const void *data, uint16_t size)
   2934 {
   2935 	uint16_t v;
   2936 	uint16_t flags;
   2937 
   2938 	if (size != 2) {
   2939 		printf("malformed packet\n");
   2940 		return;
   2941 	}
   2942 
   2943 	v = l_get_be16(data);
   2944 	print_attr(level, "%s:", label);
   2945 
   2946 	if ((v & WSC_CONFIGURATION_METHOD_PHYSICAL_DISPLAY_PIN) ==
   2947 			WSC_CONFIGURATION_METHOD_PHYSICAL_DISPLAY_PIN)
   2948 		print_attr(level + 1, "Physical Display PIN");
   2949 
   2950 	if ((v & WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN) ==
   2951 			WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN)
   2952 		print_attr(level + 1, "Virtual Display PIN");
   2953 
   2954 	flags = WSC_CONFIGURATION_METHOD_PHYSICAL_DISPLAY_PIN |
   2955 			WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN;
   2956 	if (v & flags)
   2957 		v &= ~flags;
   2958 
   2959 	if (v & WSC_CONFIGURATION_METHOD_P2P) {
   2960 		print_attr(level + 1, "P2P");
   2961 		v &= ~WSC_CONFIGURATION_METHOD_P2P;
   2962 	}
   2963 
   2964 	if ((v & WSC_CONFIGURATION_METHOD_PHYSICAL_PUSH_BUTTON) ==
   2965 			WSC_CONFIGURATION_METHOD_PHYSICAL_PUSH_BUTTON)
   2966 		print_attr(level + 1, "Physical PushButton");
   2967 
   2968 	if ((v & WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON) ==
   2969 			WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON)
   2970 		print_attr(level + 1, "Virtual PushButton");
   2971 
   2972 	flags = WSC_CONFIGURATION_METHOD_PHYSICAL_PUSH_BUTTON |
   2973 			WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON;
   2974 	if (v & flags)
   2975 		v &= ~flags;
   2976 
   2977 	if (v & WSC_CONFIGURATION_METHOD_KEYPAD) {
   2978 		print_attr(level + 1, "Keypad");
   2979 		v &= ~WSC_CONFIGURATION_METHOD_KEYPAD;
   2980 	}
   2981 
   2982 	if (v & WSC_CONFIGURATION_METHOD_PUSH_BUTTON) {
   2983 		print_attr(level + 1, "PushButton");
   2984 		v &= ~WSC_CONFIGURATION_METHOD_PUSH_BUTTON;
   2985 	}
   2986 
   2987 	if (v & WSC_CONFIGURATION_METHOD_NFC_INTERFACE) {
   2988 		print_attr(level + 1, "NFC Interface");
   2989 		v &= ~WSC_CONFIGURATION_METHOD_NFC_INTERFACE;
   2990 	}
   2991 
   2992 	if (v & WSC_CONFIGURATION_METHOD_INTEGRATED_NFC_TOKEN) {
   2993 		print_attr(level + 1, "Integrated NFC Token");
   2994 		v &= ~WSC_CONFIGURATION_METHOD_INTEGRATED_NFC_TOKEN;
   2995 	}
   2996 
   2997 	if (v & WSC_CONFIGURATION_METHOD_EXTERNAL_NFC_TOKEN) {
   2998 		print_attr(level + 1, "External NFC Token");
   2999 		v &= ~WSC_CONFIGURATION_METHOD_EXTERNAL_NFC_TOKEN;
   3000 	}
   3001 
   3002 	if (v & WSC_CONFIGURATION_METHOD_DISPLAY) {
   3003 		print_attr(level + 1, "Display");
   3004 		v &= ~WSC_CONFIGURATION_METHOD_DISPLAY;
   3005 	}
   3006 
   3007 	if (v & WSC_CONFIGURATION_METHOD_LABEL) {
   3008 		print_attr(level + 1, "Label");
   3009 		v &= ~WSC_CONFIGURATION_METHOD_LABEL;
   3010 	}
   3011 
   3012 	if (v & WSC_CONFIGURATION_METHOD_ETHERNET) {
   3013 		print_attr(level + 1, "Ethernet");
   3014 		v &= ~WSC_CONFIGURATION_METHOD_ETHERNET;
   3015 	}
   3016 
   3017 	if (v & WSC_CONFIGURATION_METHOD_USBA) {
   3018 		print_attr(level + 1, "USBA");
   3019 		v &= ~WSC_CONFIGURATION_METHOD_USBA;
   3020 	}
   3021 
   3022 	if (v)
   3023 		print_attr(level + 1, "Unknown: %04x", v);
   3024 }
   3025 
   3026 static void print_wsc_connection_type_flags(unsigned int level,
   3027 						const char *label,
   3028 						const void *data, uint16_t size)
   3029 {
   3030 	uint8_t v;
   3031 
   3032 	if (size != 1)
   3033 		return;
   3034 
   3035 	v = *((uint8_t *) data);
   3036 	print_attr(level, "%s:", label);
   3037 
   3038 	if (v & WSC_CONNECTION_TYPE_ESS)
   3039 		print_attr(level + 1, "ESS");
   3040 
   3041 	if (v & WSC_CONNECTION_TYPE_IBSS)
   3042 		print_attr(level + 1, "IBSS");
   3043 
   3044 	if (v & 0xfffc)
   3045 		print_attr(level + 1, "Unknown: %04x", v & 0xfffc);
   3046 }
   3047 
   3048 static void print_wsc_device_name(unsigned int level, const char *label,
   3049 					const void *data, uint16_t size)
   3050 {
   3051 	print_wsc_utf8_string(level, label, data, size, 32);
   3052 }
   3053 
   3054 static void print_wsc_device_password_id(unsigned int level, const char *label,
   3055 						const void *data, uint16_t size)
   3056 {
   3057 	uint16_t v;
   3058 	static const char *device_password_id_table[] = {
   3059 		"Default (PIN)",
   3060 		"User-specified",
   3061 		"Machine-specified",
   3062 		"Rekey",
   3063 		"PushButton",
   3064 		"Registrar-specified",
   3065 		"Reserved (for IBSS with WPS)",
   3066 		"NFC-Connection-Handover",
   3067 		"P2Ps (Reserved for WPS P2P Services Specification",
   3068 	};
   3069 
   3070 	if (size != 2) {
   3071 		printf("malformed packet\n");
   3072 		return;
   3073 	}
   3074 
   3075 	v = l_get_be16(data);
   3076 	if (v <= 0x0008)
   3077 		print_attr(level, "%s: %s", label, device_password_id_table[v]);
   3078 	else if (v <= 0x000F)
   3079 		print_attr(level, "%s: Reserved (%02x)", label, v);
   3080 	else
   3081 		print_attr(level, "%s: Random via OOB (%02x)", label, v);
   3082 }
   3083 
   3084 static void print_wsc_encryption_type_flags(unsigned int level,
   3085 						const char *label,
   3086 						const void *data, uint16_t size)
   3087 {
   3088 	uint16_t v;
   3089 
   3090 	if (size != 2)
   3091 		return;
   3092 
   3093 	v = l_get_be16(data);
   3094 	print_attr(level, "%s:", label);
   3095 
   3096 	if (v & WSC_ENCRYPTION_TYPE_NONE)
   3097 		print_attr(level + 1, "None");
   3098 
   3099 	if (v & WSC_ENCRYPTION_TYPE_WEP)
   3100 		print_attr(level + 1, "WEP");
   3101 
   3102 	if (v & WSC_ENCRYPTION_TYPE_TKIP)
   3103 		print_attr(level + 1, "TKIP");
   3104 
   3105 	if (v & WSC_ENCRYPTION_TYPE_AES)
   3106 		print_attr(level + 1, "AES");
   3107 
   3108 	if (v & 0xfff0)
   3109 		print_attr(level + 1, "Unknown: %04x", v & 0xfff0);
   3110 }
   3111 
   3112 static void print_wsc_mac_address(unsigned int level, const char *label,
   3113 					const void *data, uint16_t size)
   3114 {
   3115 	const char *str;
   3116 
   3117 	if (size != 6)
   3118 		return;
   3119 
   3120 	str = util_address_to_string(data);
   3121 	print_attr(level, "%s: %s", label, str);
   3122 }
   3123 
   3124 static void print_wsc_manufacturer(unsigned int level, const char *label,
   3125 					const void *data, uint16_t size)
   3126 {
   3127 	print_wsc_ascii_string(level, label, data, size, 64);
   3128 }
   3129 
   3130 static void print_wsc_message_type(unsigned int level, const char *label,
   3131 					const void *data, uint16_t size)
   3132 {
   3133 	static const char *message_type_table[] = {
   3134 		"Reserved",
   3135 		"Beacon",
   3136 		"Probe Request",
   3137 		"Probe Response",
   3138 		"M1",
   3139 		"M2",
   3140 		"M2D",
   3141 		"M3",
   3142 		"M4",
   3143 		"M5",
   3144 		"M6",
   3145 		"M7",
   3146 		"M8",
   3147 		"WSC_ACK",
   3148 		"WSC_NACK",
   3149 		"WSC_DONE"
   3150 	};
   3151 	const char *s = "Reserved";
   3152 	uint8_t t = ((uint8_t *) data)[0];
   3153 
   3154 	if (size != 1) {
   3155 		printf("malformed packet\n");
   3156 		return;
   3157 	}
   3158 
   3159 	if (t <= 0x0f)
   3160 		s = message_type_table[t];
   3161 
   3162 	print_attr(level, "%s: %s", label, s);
   3163 }
   3164 
   3165 static void print_wsc_model_name(unsigned int level, const char *label,
   3166 					const void *data, uint16_t size)
   3167 {
   3168 	print_wsc_ascii_string(level, label, data, size, 32);
   3169 }
   3170 
   3171 static void print_wsc_model_number(unsigned int level, const char *label,
   3172 					const void *data, uint16_t size)
   3173 {
   3174 	print_wsc_ascii_string(level, label, data, size, 32);
   3175 }
   3176 
   3177 static void print_wsc_os_version(unsigned int level, const char *label,
   3178 					const void *data, uint16_t size)
   3179 {
   3180 	uint32_t version;
   3181 
   3182 	if (size != 4)
   3183 		return;
   3184 
   3185 	version = l_get_be32(data);
   3186 	print_attr(level, "%s: %u", label, version & 0x7fffffff);
   3187 }
   3188 
   3189 static void print_wsc_primary_device_type(unsigned int level, const char *label,
   3190 						const void *data, uint16_t size)
   3191 {
   3192 	const uint8_t *bytes = data;
   3193 	uint16_t category;
   3194 	uint16_t subcategory;
   3195 	uint8_t oui[4];
   3196 	static const char *category_table[256] = {
   3197 		[0] = "Reserved",
   3198 		[1] = "Computer",
   3199 		[2] = "Input Device",
   3200 		[3] = "Printer/Scanner/Fax/Copier",
   3201 		[4] = "Camera",
   3202 		[5] = "Storage",
   3203 		[6] = "Network Infrastructure",
   3204 		[7] = "Displays",
   3205 		[8] = "Multimedia Devices",
   3206 		[9] = "Gaming Devices",
   3207 		[10] = "Telephony",
   3208 		[11] = "Audio Devices",
   3209 		[12] = "Docking Devices",
   3210 		[13 ... 254] = "Reserved",
   3211 		[255] = "Others",
   3212 	};
   3213 
   3214 	if (size != 8) {
   3215 		printf("malformed packet\n");
   3216 		return;
   3217 	}
   3218 
   3219 	category = l_get_be16(bytes);
   3220 	subcategory = l_get_be16(bytes + 6);
   3221 	memcpy(oui, bytes + 2, 4);
   3222 
   3223 	if (category > 255) {
   3224 		print_attr(level, "%s: %04x-%02x%02x%02x%02x-%04x",
   3225 				label, category, oui[0], oui[1], oui[2], oui[3],
   3226 				subcategory);
   3227 		return;
   3228 	}
   3229 
   3230 	print_attr(level, "%s: %s", label, category_table[category]);
   3231 	print_attr(level + 1, "OUI: %02x%02x%02x%02x",
   3232 			oui[0], oui[1], oui[2], oui[3]);
   3233 	print_attr(level + 1, "SubCategory: %02x", subcategory);
   3234 }
   3235 
   3236 static void print_wsc_request_type(unsigned int level, const char *label,
   3237 					const void *data, uint16_t size)
   3238 {
   3239 	const uint8_t *bytes = data;
   3240 	static const char *request_type_table[] = {
   3241 		"Enrollee Info",
   3242 		"Enrollee Open 802.1X",
   3243 		"Registrar",
   3244 		"WLAN Manager Registrar",
   3245 	};
   3246 
   3247 	if (size != 1 || bytes[0] > 3) {
   3248 		printf("malformed packet\n");
   3249 		return;
   3250 	}
   3251 
   3252 	print_attr(level, "%s: %s", label, request_type_table[bytes[0]]);
   3253 }
   3254 
   3255 static void print_wsc_response_type(unsigned int level, const char *label,
   3256 					const void *data, uint16_t size)
   3257 {
   3258 	const uint8_t *bytes = data;
   3259 	static const char *response_type_table[] = {
   3260 		"Enrollee Info",
   3261 		"Enrollee Open 802.1X",
   3262 		"Registrar",
   3263 		"AP",
   3264 	};
   3265 
   3266 	if (size != 1 || bytes[0] > 3) {
   3267 		printf("malformed packet\n");
   3268 		return;
   3269 	}
   3270 
   3271 	print_attr(level, "%s: %s", label, response_type_table[bytes[0]]);
   3272 }
   3273 
   3274 static void print_wsc_rf_bands(unsigned int level, const char *label,
   3275 					const void *data, uint16_t size)
   3276 {
   3277 	const uint8_t *bytes = data;
   3278 	char bands[256];
   3279 	uint16_t pos = 0;
   3280 
   3281 	if (size != 1) {
   3282 		printf("malformed packet\n");
   3283 		return;
   3284 	}
   3285 
   3286 	if (bytes[0] >= 0x08) {
   3287 		print_attr(level, "%s: %02x", label, bytes[0]);
   3288 		return;
   3289 	}
   3290 
   3291 	if (bytes[0] & WSC_RF_BAND_2_4_GHZ)
   3292 		pos += sprintf(bands + pos, " 2.4 GHz,");
   3293 
   3294 	if (bytes[0] & WSC_RF_BAND_5_0_GHZ)
   3295 		pos += sprintf(bands + pos, " 5 GHz,");
   3296 
   3297 	if (bytes[0] & WSC_RF_BAND_60_GHZ)
   3298 		pos += sprintf(bands + pos, " 60 GHz,");
   3299 
   3300 	bands[pos - 1] = '\0';
   3301 	print_attr(level, "%s: %s", label, bands);
   3302 }
   3303 
   3304 static void print_wsc_serial_number(unsigned int level, const char *label,
   3305 					const void *data, uint16_t size)
   3306 {
   3307 	print_wsc_ascii_string(level, label, data, size, 32);
   3308 }
   3309 
   3310 static void print_wsc_version(unsigned int level, const char *label,
   3311 				const void *data, uint16_t size)
   3312 {
   3313 	const uint8_t *bytes = data;
   3314 
   3315 	if (size != 1) {
   3316 		printf("malformed packet\n");
   3317 		return;
   3318 	}
   3319 
   3320 	print_attr(level, "%s: %x", label, bytes[0]);
   3321 }
   3322 
   3323 static void print_wsc_state(unsigned int level, const char *label,
   3324 				const void *data, uint16_t size)
   3325 {
   3326 	const uint8_t *bytes = data;
   3327 	static const char *state_table[3] = {
   3328 		"Reserved",
   3329 		"Not Configured",
   3330 		"Configured"
   3331 	};
   3332 
   3333 	if (size != 1 || bytes[0] == 0 || bytes[0] > 2) {
   3334 		printf("malformed packet\n");
   3335 		return;
   3336 	}
   3337 
   3338 	print_attr(level, "%s: %s", label, state_table[bytes[0]]);
   3339 }
   3340 
   3341 static void print_wsc_wfa_ext_version2(unsigned int level, const char *label,
   3342 					const void *data, uint16_t size)
   3343 {
   3344 	const uint8_t *bytes = data;
   3345 
   3346 	if (size != 1) {
   3347 		printf("malformed packet\n");
   3348 		return;
   3349 	}
   3350 
   3351 	print_attr(level, "%s: %x.%x", label, bytes[0] >> 4, bytes[0] & 0xf);
   3352 }
   3353 
   3354 static void print_wsc_wfa_ext_authorized_macs(unsigned int level,
   3355 						const char *label,
   3356 						const void *data, uint16_t size)
   3357 {
   3358 	if (size > 30 || size % 6 != 0) {
   3359 		printf("malformed packet\n");
   3360 		return;
   3361 	}
   3362 
   3363 	for (; size; size -= 6, data += 6)
   3364 		print_attr(level, "%s: %s", label, util_address_to_string(data));
   3365 }
   3366 
   3367 static struct attr_entry wsc_wfa_ext_attr_entry[] = {
   3368 	{ WSC_WFA_EXTENSION_VERSION2,			"Version2",
   3369 		ATTR_CUSTOM,	{ .function = print_wsc_wfa_ext_version2 } },
   3370 	{ WSC_WFA_EXTENSION_AUTHORIZED_MACS,		"Authorized MAC",
   3371 		ATTR_CUSTOM,
   3372 		{ .function = print_wsc_wfa_ext_authorized_macs } },
   3373 	{ WSC_WFA_EXTENSION_NETWORK_KEY_SHAREABLE,
   3374 						"Network Key Shareable",
   3375 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3376 	{ WSC_WFA_EXTENSION_REQUEST_TO_ENROLL,		"Request to Enroll",
   3377 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3378 	{ },
   3379 };
   3380 
   3381 static void print_wsc_wfa_ext_attributes(unsigned int level, const char *label,
   3382 						const void *data, uint16_t size)
   3383 {
   3384 	struct wsc_wfa_ext_iter iter;
   3385 	int i;
   3386 
   3387 	print_attr(level, "%s: len %u", label, size);
   3388 
   3389 	wsc_wfa_ext_iter_init(&iter, data, size);
   3390 
   3391 	while (wsc_wfa_ext_iter_next(&iter)) {
   3392 		uint8_t type = wsc_wfa_ext_iter_get_type(&iter);
   3393 		uint8_t len = wsc_wfa_ext_iter_get_length(&iter);
   3394 		const void *attr = wsc_wfa_ext_iter_get_data(&iter);
   3395 		struct attr_entry *entry = NULL;
   3396 
   3397 		for (i = 0; wsc_wfa_ext_attr_entry[i].str; i++) {
   3398 			if (wsc_wfa_ext_attr_entry[i].attr == type) {
   3399 				entry = &wsc_wfa_ext_attr_entry[i];
   3400 				break;
   3401 			}
   3402 		}
   3403 
   3404 		if (entry && entry->function)
   3405 			entry->function(level + 1, entry->str, attr, len);
   3406 		else {
   3407 			print_attr(level + 1, "Type: 0x%02x: len %u",
   3408 								type, len);
   3409 			print_hexdump(level + 2, attr, len);
   3410 		}
   3411 	}
   3412 }
   3413 
   3414 static void print_wsc_vendor_extension(unsigned int level, const char *label,
   3415 						const void *data, uint16_t size){
   3416 	const uint8_t *bytes = data;
   3417 
   3418 	if (size < 3) {
   3419 		printf("malformed packet\n");
   3420 		return;
   3421 	}
   3422 
   3423 	if (memcmp(data, wsc_wfa_oui, sizeof(wsc_wfa_oui))) {
   3424 		print_attr(level, "%s: OUI: 0x%02x 0x%02x 0x%02x: len %u",
   3425 				label, bytes[0], bytes[1], bytes[2], size);
   3426 		print_hexdump(level + 1, data + 3, size - 3);
   3427 		return;
   3428 	}
   3429 
   3430 	print_wsc_wfa_ext_attributes(level, "WFA Vendor Extension",
   3431 							data + 3, size - 3);
   3432 }
   3433 
   3434 static struct attr_entry wsc_attr_entry[] = {
   3435 	{ WSC_ATTR_8021X_ENABLED,		"802.1X Enabled",
   3436 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3437 	{ WSC_ATTR_AP_SETUP_LOCKED,		"AP Setup Locked",
   3438 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3439 	{ WSC_ATTR_ASSOCIATION_STATE,		"Association State",
   3440 		ATTR_CUSTOM,	{ .function = print_wsc_association_state } },
   3441 	{ WSC_ATTR_AUTHENTICATION_TYPE_FLAGS,	"Authentication Type Flags",
   3442 		ATTR_CUSTOM,	{ .function = print_wsc_auth_type_flags } },
   3443 	{ WSC_ATTR_AUTHENTICATOR,		"Authenticator",
   3444 		ATTR_CUSTOM,	{ .function = print_wsc_authenticator } },
   3445 	{ WSC_ATTR_CONFIGURATION_ERROR,		"Configuration Error",
   3446 		ATTR_CUSTOM,	{ .function = print_wsc_configuration_error } },
   3447 	{ WSC_ATTR_CONFIGURATION_METHODS,	"Configuration Methods",
   3448 		ATTR_CUSTOM,	{ .function = print_wsc_config_methods } },
   3449 	{ WSC_ATTR_CONNECTION_TYPE_FLAGS,	"Connection Type Flags",
   3450 		ATTR_CUSTOM,	{ .function =
   3451 					print_wsc_connection_type_flags } },
   3452 	{ WSC_ATTR_DEVICE_NAME,			"Device Name",
   3453 		ATTR_CUSTOM,	{ .function = print_wsc_device_name } },
   3454 	{ WSC_ATTR_DEVICE_PASSWORD_ID,		"Device Password Id",
   3455 		ATTR_CUSTOM,	{ .function = print_wsc_device_password_id } },
   3456 	{ WSC_ATTR_E_HASH1,			"E-Hash1",
   3457 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3458 	{ WSC_ATTR_E_HASH2,			"E-Hash2",
   3459 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3460 	{ WSC_ATTR_ENCRYPTED_SETTINGS,		"Encrypted Settings",
   3461 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3462 	{ WSC_ATTR_ENCRYPTION_TYPE_FLAGS,	"Encryption Type Flags",
   3463 		ATTR_CUSTOM,	{ .function =
   3464 					print_wsc_encryption_type_flags } },
   3465 	{ WSC_ATTR_ENROLLEE_NONCE,		"Enrollee Nonce",
   3466 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3467 	{ WSC_ATTR_KEY_PROVIDED_AUTOMATICALLY,	"Key Provided Automatically",
   3468 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3469 	{ WSC_ATTR_MAC_ADDRESS,			"MAC Address",
   3470 		ATTR_CUSTOM,	{ .function = print_wsc_mac_address } },
   3471 	{ WSC_ATTR_MANUFACTURER,		"Manufacturer",
   3472 		ATTR_CUSTOM,	{ .function = print_wsc_manufacturer } },
   3473 	{ WSC_ATTR_MESSAGE_TYPE,		"Message Type",
   3474 		ATTR_CUSTOM,	{ .function = print_wsc_message_type } },
   3475 	{ WSC_ATTR_MODEL_NAME,			"Model Name",
   3476 		ATTR_CUSTOM,	{ .function = print_wsc_model_name } },
   3477 	{ WSC_ATTR_MODEL_NUMBER,		"Model Number",
   3478 		ATTR_CUSTOM,	{ .function = print_wsc_model_number } },
   3479 	{ WSC_ATTR_NETWORK_INDEX,		"Network Index",
   3480 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3481 	{ WSC_ATTR_NETWORK_KEY_INDEX,		"Network Key Index (Reserved)",
   3482 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3483 	{ WSC_ATTR_OS_VERSION,			"OS Version",
   3484 		ATTR_CUSTOM,	{ .function = print_wsc_os_version } },
   3485 	{ WSC_ATTR_PORTABLE_DEVICE,		"Portable Device",
   3486 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3487 	{ WSC_ATTR_PRIMARY_DEVICE_TYPE,		"Primary Device Type",
   3488 		ATTR_CUSTOM,	{ .function = print_wsc_primary_device_type } },
   3489 	{ WSC_ATTR_PSK_CURRENT,			"PSK Current",
   3490 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3491 	{ WSC_ATTR_PSK_MAX,			"PSK Max",
   3492 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3493 	{ WSC_ATTR_PUBLIC_KEY,			"Public Key",
   3494 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3495 	{ WSC_ATTR_RADIO_ENABLED,		"Radio Enabled",
   3496 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3497 	{ WSC_ATTR_REBOOT,			"Reboot",
   3498 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3499 	{ WSC_ATTR_REGISTRAR_CURRENT,		"Registrar Current",
   3500 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3501 	{ WSC_ATTR_REGISTRAR_ESTABLISHED,	"Registrar Established",
   3502 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3503 	{ WSC_ATTR_REGISTRAR_MAX,		"Registrar Max",
   3504 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3505 	{ WSC_ATTR_REGISTRAR_NONCE,		"Registrar Nonce",
   3506 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3507 	{ WSC_ATTR_REQUEST_TYPE,		"Request Type",
   3508 		ATTR_CUSTOM,	{ .function = print_wsc_request_type } },
   3509 	{ WSC_ATTR_RESPONSE_TYPE,		"Response Type",
   3510 		ATTR_CUSTOM,	{ .function = print_wsc_response_type } },
   3511 	{ WSC_ATTR_RF_BANDS,			"RF Bands",
   3512 		ATTR_CUSTOM,	{ .function = print_wsc_rf_bands } },
   3513 	{ WSC_ATTR_R_HASH1,			"R_Hash1",
   3514 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3515 	{ WSC_ATTR_R_HASH2,			"R_Hash2",
   3516 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   3517 	{ WSC_ATTR_SELECTED_REGISTRAR,		"Selected Registrar",
   3518 		ATTR_CUSTOM,	{ .function = print_wsc_bool } },
   3519 	{ WSC_ATTR_SERIAL_NUMBER,		"Serial Number",
   3520 		ATTR_CUSTOM,	{ .function = print_wsc_serial_number } },
   3521 	{ WSC_ATTR_TOTAL_NETWORKS,		"Total Networks",
   3522 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   3523 	{ WSC_ATTR_UUID_E,			"UUID-E",
   3524 		ATTR_CUSTOM,	{ .function = print_wsc_uuid } },
   3525 	{ WSC_ATTR_UUID_R,			"UUID-R",
   3526 		ATTR_CUSTOM,	{ .function = print_wsc_uuid } },
   3527 	{ WSC_ATTR_VENDOR_EXTENSION,		"Vendor Extension",
   3528 		ATTR_CUSTOM,	{ .function = print_wsc_vendor_extension } },
   3529 	{ WSC_ATTR_VERSION,			"Version",
   3530 		ATTR_CUSTOM,	{ .function = print_wsc_version } },
   3531 	{ WSC_ATTR_WSC_STATE,			"WSC State",
   3532 		ATTR_CUSTOM,	{ .function = print_wsc_state } },
   3533 	{ },
   3534 };
   3535 
   3536 static void print_wsc_attributes(unsigned int level, const char *label,
   3537 					const void *data, uint16_t size)
   3538 {
   3539 	struct wsc_attr_iter iter;
   3540 	int i;
   3541 
   3542 	print_attr(level, "%s: len %u", label, size);
   3543 
   3544 	wsc_attr_iter_init(&iter, data, size);
   3545 
   3546 	while (wsc_attr_iter_next(&iter)) {
   3547 		uint16_t type = wsc_attr_iter_get_type(&iter);
   3548 		uint16_t len = wsc_attr_iter_get_length(&iter);
   3549 		const void *attr = wsc_attr_iter_get_data(&iter);
   3550 		struct attr_entry *entry = NULL;
   3551 
   3552 		for (i = 0; wsc_attr_entry[i].str; i++) {
   3553 			if (wsc_attr_entry[i].attr == type) {
   3554 				entry = &wsc_attr_entry[i];
   3555 				break;
   3556 			}
   3557 		}
   3558 
   3559 		if (entry && entry->function)
   3560 			entry->function(level + 1, entry->str, attr, len);
   3561 		else {
   3562 			print_attr(level + 1, "Type: 0x%02x: len %u",
   3563 								type, len);
   3564 			print_hexdump(level + 2, attr, len);
   3565 		}
   3566 	}
   3567 }
   3568 
   3569 static void print_p2p_status(unsigned int level, const char *label,
   3570 				const void *data, uint16_t size)
   3571 {
   3572 	const uint8_t *bytes = data;
   3573 	static const struct p2p_status_desc {
   3574 		uint8_t code;
   3575 		const char *desc;
   3576 	} descs[] = {
   3577 		{ P2P_STATUS_SUCCESS, "Success" },
   3578 		{ P2P_STATUS_FAIL_INFO_NOT_AVAIL, "Fail; information is "
   3579 			"currently unavailable" },
   3580 		{ P2P_STATUS_FAIL_INCOMPATIBLE_PARAMS, "Fail; incompatible "
   3581 			"parameters" },
   3582 		{ P2P_STATUS_FAIL_LIMIT_REACHED, "Fail; limit reached" },
   3583 		{ P2P_STATUS_FAIL_INVALID_PARAMS, "Fail; invalid parameters" },
   3584 		{ P2P_STATUS_FAIL_UNABLE_TO_ACCOMMODATE_REQUEST, "Fail; unable "
   3585 			"to accommodate request" },
   3586 		{ P2P_STATUS_FAIL_PREV_PROTOCOL_ERROR, "Fail; previous protocol"
   3587 			" error, or distruptive behavior" },
   3588 		{ P2P_STATUS_FAIL_NO_COMMON_CHANNELS, "Fail; no common "
   3589 			"channels" },
   3590 		{ P2P_STATUS_FAIL_UNKNOWN_P2P_GROUP, "Fail; unknown P2P "
   3591 			"Group" },
   3592 		{ P2P_STATUS_FAIL_INTENT_15_IN_GO_NEGOTIATION, "Fail; both P2P "
   3593 			"Devices indicated an Intent of 15 in Group Owner "
   3594 			"Negotiation" },
   3595 		{ P2P_STATUS_FAIL_INCOMPATIBLE_PROVISIONING, "Fail; "
   3596 			"incompatible provisioning method" },
   3597 		{ P2P_STATUS_FAIL_REJECTED_BY_USER, "Fail; rejected by user" },
   3598 		{ P2P_STATUS_SUCCESS_ACCEPTED_BY_USER, "Success; accepted by "
   3599 			"user" },
   3600 		{}
   3601 	};
   3602 	int i;
   3603 
   3604 	if (size != 1) {
   3605 		printf("malformed P2P %s\n", label);
   3606 		return;
   3607 	}
   3608 
   3609 	for (i = 0; descs[i].desc; i++)
   3610 		if (descs[i].code == bytes[0])
   3611 			break;
   3612 
   3613 	if (descs[i].desc)
   3614 		print_attr(level, "%s: %s", label, descs[i].desc);
   3615 	else
   3616 		print_attr(level, "%s: 0x%02x", label, bytes[0]);
   3617 }
   3618 
   3619 #define CHECK_CAPS_BIT(v, str)	\
   3620 	do {			\
   3621 		if (caps & (v)) {				\
   3622 			print_attr(level + 1, "%s", (str));	\
   3623 			caps &= ~(v);				\
   3624 		} 						\
   3625 	} while(0)
   3626 
   3627 static void print_p2p_device_capability(unsigned int level, const char *label,
   3628 					const void *data, uint16_t size)
   3629 {
   3630 	uint8_t caps;
   3631 
   3632 	if (size != 1) {
   3633 		printf("malformed P2P %s\n", label);
   3634 		return;
   3635 	}
   3636 
   3637 	caps = *(const uint8_t *) data;
   3638 
   3639 	print_attr(level, "%s:%s", label, !caps ? " None" : "");
   3640 
   3641 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_SVC_DISCOVERY,
   3642 			"Service Discovery");
   3643 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_CLIENT_DISCOVERABILITY,
   3644 			"P2P Client Discoverability");
   3645 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_CONCURRENT_OP,
   3646 			"Concurrent Operation");
   3647 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_INFRASTRUCTURE_MANAGED,
   3648 			"P2P Infrastructure Managed");
   3649 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_DEVICE_LIMIT,
   3650 			"P2P Device Limit");
   3651 	CHECK_CAPS_BIT(P2P_DEVICE_CAP_INVITATION_PROCEDURE,
   3652 			"P2P Invitation Procedure");
   3653 
   3654 	if (caps)
   3655 		print_attr(level + 1, "Reserved: 0x%02x", caps);
   3656 }
   3657 
   3658 static void print_p2p_capability(unsigned int level, const char *label,
   3659 					const void *data, uint16_t size)
   3660 {
   3661 	uint8_t caps;
   3662 
   3663 	if (size != 2) {
   3664 		printf("malformed P2P %s\n", label);
   3665 		return;
   3666 	}
   3667 
   3668 	print_p2p_device_capability(level, "P2P Device Capability", data++, 1);
   3669 
   3670 	caps = *(const uint8_t *) data++;
   3671 
   3672 	print_attr(level, "P2P Group Capability:%s", !caps ? " None" : "");
   3673 
   3674 	CHECK_CAPS_BIT(P2P_GROUP_CAP_GO,
   3675 			"P2P Group Owner");
   3676 	CHECK_CAPS_BIT(P2P_GROUP_CAP_PERSISTENT_GROUP,
   3677 			"Persistent P2P Group");
   3678 	CHECK_CAPS_BIT(P2P_GROUP_CAP_GROUP_LIMIT,
   3679 			"P2P Group Limit");
   3680 	CHECK_CAPS_BIT(P2P_GROUP_CAP_INTRA_BSS_DISTRIBUTION,
   3681 			"Intra-BSS Distribution");
   3682 	CHECK_CAPS_BIT(P2P_GROUP_CAP_CROSS_CONNECT,
   3683 			"Cross Connection");
   3684 	CHECK_CAPS_BIT(P2P_GROUP_CAP_PERSISTENT_RECONNECT,
   3685 			"Persistent Reconnect");
   3686 	CHECK_CAPS_BIT(P2P_GROUP_CAP_GROUP_FORMATION,
   3687 			"Group Formation");
   3688 	CHECK_CAPS_BIT(P2P_GROUP_CAP_IP_ALLOCATION,
   3689 			"IP Address Allocation");
   3690 }
   3691 
   3692 static void print_p2p_go_intent(unsigned int level, const char *label,
   3693 				const void *data, uint16_t size)
   3694 {
   3695 	const uint8_t *bytes = data;
   3696 
   3697 	if (size != 1) {
   3698 		printf("malformed P2P %s\n", label);
   3699 		return;
   3700 	}
   3701 
   3702 	print_attr(level, "%s: Intent %u out of 15, tie breaker %u", label,
   3703 			bytes[0] >> 1, bytes[0] & 1);
   3704 }
   3705 
   3706 static void print_p2p_config_timeout(unsigned int level, const char *label,
   3707 					const void *data, uint16_t size)
   3708 {
   3709 	const uint8_t *bytes = data;
   3710 
   3711 	if (size != 2) {
   3712 		printf("malformed P2P %s\n", label);
   3713 		return;
   3714 	}
   3715 
   3716 	print_attr(level, "%s: GO Timeout %ums, Client Timeout %ums", label,
   3717 			bytes[0] * 10, bytes[1] * 10);
   3718 }
   3719 
   3720 static void print_p2p_oper_channel(unsigned int level, const char *label,
   3721 					const void *data, uint16_t size)
   3722 {
   3723 	const uint8_t *bytes = data;
   3724 
   3725 	if (size != 5) {
   3726 		printf("malformed P2P %s\n", label);
   3727 		return;
   3728 	}
   3729 
   3730 	print_attr(level, "%s:", label);
   3731 	print_attr(level + 1, "Country %c%c table %u", bytes[0], bytes[1],
   3732 			bytes[2]);
   3733 	print_attr(level + 1, "Operating Class %u", bytes[3]);
   3734 	print_attr(level + 1, "Channel Number %u", bytes[4]);
   3735 }
   3736 
   3737 static void print_p2p_extended_timing(unsigned int level, const char *label,
   3738 					const void *data, uint16_t size)
   3739 {
   3740 	if (size != 4) {
   3741 		printf("malformed P2P %s\n", label);
   3742 		return;
   3743 	}
   3744 
   3745 	print_attr(level, "%s: Availability period: %ums, interval: %ums", label,
   3746 			l_get_le16(data + 0), l_get_le16(data + 2));
   3747 }
   3748 
   3749 static void print_p2p_manageability(unsigned int level, const char *label,
   3750 					const void *data, uint16_t size)
   3751 {
   3752 	uint8_t val;
   3753 
   3754 	if (size != 1) {
   3755 		printf("malformed P2P %s\n", label);
   3756 		return;
   3757 	}
   3758 
   3759 #define CHECK_BIT(v, str)					\
   3760 	do {							\
   3761 		if (val & (v)) {				\
   3762 			print_attr(level + 1, "%s", (str));	\
   3763 			val &= ~(v);				\
   3764 		}						\
   3765 	} while(0)
   3766 
   3767 	val = *(const uint8_t *) data;
   3768 
   3769 	print_attr(level, "%s:%s", label, !val ? " None" : "");
   3770 
   3771 	CHECK_BIT(P2P_MANAGEABILITY_DEVICE_MGMT, "P2P Device Management");
   3772 	CHECK_BIT(P2P_MANAGEABILITY_CROSS_CONNECT,
   3773 			"Cross Connection Permitted");
   3774 	CHECK_BIT(P2P_MANAGEABILITY_COEXIST_OPTIONAL, "Coexistence Optional");
   3775 
   3776 	if (val)
   3777 		print_attr(level + 1, "Reserved: 0x%02x", val);
   3778 #undef CHECK_BIT
   3779 }
   3780 
   3781 static void print_p2p_channel_list(unsigned int level, const char *label,
   3782 					const void *data, uint16_t size)
   3783 {
   3784 	const uint8_t *bytes = data;
   3785 
   3786 	if (size < 3) {
   3787 		printf("malformed P2P %s\n", label);
   3788 		return;
   3789 	}
   3790 
   3791 	print_attr(level, "%s:", label);
   3792 	print_attr(level + 1, "Country %c%c table %u", bytes[0], bytes[1],
   3793 			bytes[2]);
   3794 	bytes += 3;
   3795 	size -= 3;
   3796 
   3797 	while (size) {
   3798 		uint8_t channels;
   3799 		struct l_string *string;
   3800 		char *str;
   3801 		bool first = true;
   3802 
   3803 		if (size < 2 || size < 2 + bytes[1]) {
   3804 			printf("malformed P2P %s\n", label);
   3805 			return;
   3806 		}
   3807 
   3808 		print_attr(level + 1, "Operating Class %u channels:", *bytes++);
   3809 		channels = *bytes++;
   3810 		size -= 2 + channels;
   3811 
   3812 		string = l_string_new(128);
   3813 
   3814 		while (channels--) {
   3815 			l_string_append_printf(string, "%s%u",
   3816 						first ? "" : ", ",
   3817 						(int ) *bytes++);
   3818 			first = false;
   3819 		}
   3820 
   3821 		str = l_string_unwrap(string);
   3822 		print_attr(level + 2, "%s", str);
   3823 		l_free(str);
   3824 	}
   3825 }
   3826 
   3827 static void print_p2p_notice_of_absence(unsigned int level, const char *label,
   3828 					const void *data, uint16_t size)
   3829 {
   3830 	const uint8_t *bytes = data;
   3831 
   3832 	if (size < 2) {
   3833 		printf("malformed P2P %s\n", label);
   3834 		return;
   3835 	}
   3836 
   3837 	print_attr(level, "%s %u:", label, bytes[0]);
   3838 	print_attr(level + 1, "GO uses Opportunistic Power Save: %s",
   3839 			(bytes[1] & 0x80) ? "true" : "false");
   3840 	print_attr(level + 1, "Client Traffic window len: %u TUs",
   3841 			bytes[1] & 0x7f);
   3842 	bytes += 2;
   3843 	size -= 2;
   3844 
   3845 	while (size) {
   3846 		if (size < 13) {
   3847 			printf("malformed P2P Channel List\n");
   3848 			return;
   3849 		}
   3850 
   3851 		print_attr(level + 1, "Notice:");
   3852 		print_attr(level + 2, "Count/Type: %u", bytes[0]);
   3853 		print_attr(level + 2, "Duration: %uus", l_get_le32(bytes + 1));
   3854 		print_attr(level + 2, "Interval: %uus", l_get_le32(bytes + 5));
   3855 		print_attr(level + 2, "Start Time: %u", l_get_le32(bytes + 8));
   3856 		bytes += 13;
   3857 		size -= 13;
   3858 	}
   3859 }
   3860 
   3861 static void print_p2p_device_info(unsigned int level, const char *label,
   3862 					const void *data, uint16_t size)
   3863 {
   3864 	const uint8_t *bytes = data;
   3865 	int secondary_types;
   3866 
   3867 	if (size < 17 || size < 17 + bytes[16] * 8 + 4) {
   3868 		printf("malformed P2P %s\n", label);
   3869 		return;
   3870 	}
   3871 
   3872 	print_attr(level, "%s:", label);
   3873 	print_wsc_mac_address(level + 1, "P2P Device address", bytes, 6);
   3874 	print_wsc_config_methods(level + 1, "Config Methods", bytes + 6, 2);
   3875 	print_wsc_primary_device_type(level + 1, "Primary Device Type",
   3876 					bytes + 8, 8);
   3877 	secondary_types = bytes[16];
   3878 	bytes += 17;
   3879 	size -= 17;
   3880 
   3881 	while (secondary_types--) {
   3882 		print_wsc_primary_device_type(level + 1,
   3883 						"Secondary Device Type",
   3884 						bytes, 8);
   3885 		bytes += 8;
   3886 		size -= 8;
   3887 	}
   3888 
   3889 	if (l_get_be16(bytes) != WSC_ATTR_DEVICE_NAME ||
   3890 			4 + l_get_be16(bytes + 2) > size) {
   3891 		printf("malformed P2P %s\n", label);
   3892 		return;
   3893 	}
   3894 
   3895 	print_wsc_device_name(level + 1, "Device Name", bytes + 4,
   3896 				l_get_be16(bytes + 2));
   3897 }
   3898 
   3899 static void print_p2p_group_info(unsigned int level, const char *label,
   3900 					const void *data, uint16_t size)
   3901 {
   3902 	const uint8_t *bytes = data;
   3903 
   3904 	print_attr(level, "%s:", label);
   3905 
   3906 	while (size) {
   3907 		size_t desc_size = bytes[0];
   3908 		int secondary_types;
   3909 
   3910 		if (1 + desc_size > size || desc_size < 24 ||
   3911 				desc_size < (size_t) 24 + bytes[24] * 8 + 4) {
   3912 			printf("malformed P2P Client Info Descriptor\n");
   3913 			return;
   3914 		}
   3915 
   3916 		size -= 1 + desc_size;
   3917 
   3918 		print_attr(level + 1, "P2P Client Info Descriptor:");
   3919 		print_wsc_mac_address(level + 2, "P2P Device address",
   3920 					bytes + 1, 6);
   3921 		print_wsc_mac_address(level + 2, "P2P Interface address",
   3922 					bytes + 7, 6);
   3923 		print_p2p_device_capability(level + 2, "P2P Device Capability",
   3924 						bytes + 13, 1);
   3925 		print_wsc_config_methods(level + 2, "Config Methods",
   3926 						bytes + 14, 2);
   3927 		print_wsc_primary_device_type(level + 2, "Primary Device Type",
   3928 						bytes + 16, 8);
   3929 		secondary_types = bytes[24];
   3930 		bytes += 25;
   3931 		desc_size -= 24;
   3932 
   3933 		while (secondary_types--) {
   3934 			print_wsc_primary_device_type(level + 2,
   3935 							"Secondary Device Type",
   3936 							bytes, 8);
   3937 			bytes += 8;
   3938 			desc_size -= 8;
   3939 		}
   3940 
   3941 		if (l_get_be16(bytes) != WSC_ATTR_DEVICE_NAME ||
   3942 				(size_t) 4 + l_get_be16(bytes + 2) >
   3943 				desc_size) {
   3944 			printf("malformed P2P Client Info Descriptor\n");
   3945 			return;
   3946 		}
   3947 
   3948 		print_wsc_device_name(level + 1, "Device Name", bytes + 4,
   3949 					l_get_be16(bytes + 2));
   3950 		bytes += 4 + l_get_be16(bytes + 2);
   3951 	}
   3952 }
   3953 
   3954 static void print_p2p_group_id(unsigned int level, const char *label,
   3955 					const void *data, uint16_t size)
   3956 {
   3957 	const uint8_t *bytes = data;
   3958 
   3959 	if (size < 6 || size > 38) {
   3960 		printf("malformed P2P %s\n", label);
   3961 		return;
   3962 	}
   3963 
   3964 	print_attr(level, "%s:", label);
   3965 
   3966 	print_wsc_mac_address(level + 1, "P2P Device address",
   3967 				bytes, 6);
   3968 	print_ie_ssid(level + 1, "SSID", bytes + 6, size - 6);
   3969 }
   3970 
   3971 static void print_p2p_interface(unsigned int level, const char *label,
   3972 					const void *data, uint16_t size)
   3973 {
   3974 	const uint8_t *bytes = data;
   3975 	int addr_count;
   3976 
   3977 	if (size < 7 || size < 7 + bytes[6] * 6) {
   3978 		printf("malformed P2P %s\n", label);
   3979 		return;
   3980 	}
   3981 
   3982 	print_attr(level, "%s:", label);
   3983 
   3984 	print_wsc_mac_address(level + 1, "P2P Device Address",
   3985 				bytes, 6);
   3986 	addr_count = bytes[6];
   3987 	print_attr(level + 1, "P2P Interface Address Count: %u", addr_count);
   3988 	bytes += 7;
   3989 	size -= 7;
   3990 
   3991 	while (addr_count--) {
   3992 		print_wsc_mac_address(level + 2, "Interface Address", bytes, 6);
   3993 		bytes += 6;
   3994 	}
   3995 }
   3996 
   3997 static void print_p2p_invite_flags(unsigned int level, const char *label,
   3998 					const void *data, uint16_t size)
   3999 {
   4000 	uint8_t flags;
   4001 
   4002 	if (size != 1) {
   4003 		printf("malformed P2P %s\n", label);
   4004 		return;
   4005 	}
   4006 
   4007 	flags = *(const uint8_t *) data;
   4008 	print_attr(level, "Invitation Type: %s",
   4009 			(flags & 1) ? "re-invoke a Persistent Group" :
   4010 			"join active group");
   4011 	flags &= ~1;
   4012 
   4013 	if (flags)
   4014 		print_attr(level, "Invitation Flags: reserved 0x%02x", flags);
   4015 }
   4016 
   4017 static void print_p2p_oob_neg_channel(unsigned int level, const char *label,
   4018 					const void *data, uint16_t size)
   4019 {
   4020 	const uint8_t *bytes = data;
   4021 	const char *role;
   4022 
   4023 	if (size != 6) {
   4024 		printf("malformed P2P %s\n", label);
   4025 		return;
   4026 	}
   4027 
   4028 	print_p2p_oper_channel(level, label, bytes + 0, 5);
   4029 
   4030 	switch (bytes[5]) {
   4031 	case 0x00:
   4032 		role = "not in a group";
   4033 		break;
   4034 	case 0x01:
   4035 		role = "client";
   4036 		break;
   4037 	case 0x02:
   4038 		role = "Group Owner";
   4039 		break;
   4040 	default:
   4041 		role = "reserved";
   4042 		break;
   4043 	}
   4044 
   4045 	print_attr(level + 1, "Current group role indication: %s", role);
   4046 }
   4047 
   4048 static void print_p2p_service_hash(unsigned int level, const char *label,
   4049 					const void *data, uint16_t size)
   4050 {
   4051 	if (size % 6 != 0) {
   4052 		printf("malformed P2P %s\n", label);
   4053 		return;
   4054 	}
   4055 
   4056 	while (size) {
   4057 		print_wsc_bytes(level, "Service Hash", data, 6);
   4058 		data += 6;
   4059 		size -= 6;
   4060 	}
   4061 }
   4062 
   4063 static void print_p2p_connection_caps(unsigned int level, const char *label,
   4064 					const void *data, uint16_t size)
   4065 {
   4066 	uint8_t caps;
   4067 	const char *first;
   4068 
   4069 	if (size != 1) {
   4070 		printf("malformed P2P %s\n", label);
   4071 		return;
   4072 	}
   4073 
   4074 	caps = *(const uint8_t *) data;
   4075 
   4076 	if (caps & 1)
   4077 		first = "New";
   4078 	else if (caps & 2)
   4079 		first = "Cli";
   4080 	else if (caps & ~4)
   4081 		first = "Unknown";
   4082 	else
   4083 		first = "GO";
   4084 
   4085 	print_attr(level, "%s: %s%s", label, first,
   4086 			(caps & ~4) && (caps & 4) ? ", GO" : "");
   4087 }
   4088 
   4089 static void print_p2p_advertisement_id(unsigned int level, const char *label,
   4090 					const void *data, uint16_t size)
   4091 {
   4092 	if (size != 10) {
   4093 		printf("malformed P2P %s\n", label);
   4094 		return;
   4095 	}
   4096 
   4097 	print_attr(level, "%s: 0x%08x", label, l_get_le32(data + 0));
   4098 	print_wsc_mac_address(level + 1, "Service MAC Address", data + 4, 6);
   4099 }
   4100 
   4101 static void print_p2p_advertised_svc_info(unsigned int level, const char *label,
   4102 						const void *data, uint16_t size)
   4103 {
   4104 	const uint8_t *bytes = data;
   4105 
   4106 	while (size) {
   4107 		if (size < 7 || size < 7 + bytes[6]) {
   4108 			printf("malformed P2P %s\n", label);
   4109 			return;
   4110 		}
   4111 
   4112 		print_attr(level, "%s:", label);
   4113 
   4114 		print_attr(level, "Service advertisement: ID 0x%08x",
   4115 				l_get_le32(bytes + 0));
   4116 		print_wsc_utf8_string(level + 1, "Service Name",
   4117 					bytes + 6, bytes[6], 255);
   4118 		print_wsc_config_methods(level + 1, "Service Config Methods",
   4119 						bytes + 4, 2);
   4120 		size -= 7 + bytes[6];
   4121 		bytes += 7 + bytes[6];
   4122 	}
   4123 }
   4124 
   4125 static void print_p2p_session_id(unsigned int level, const char *label,
   4126 					const void *data, uint16_t size)
   4127 {
   4128 	if (size != 10) {
   4129 		printf("malformed P2P %s\n", label);
   4130 		return;
   4131 	}
   4132 
   4133 	print_attr(level, "%s: 0x%08x", label, l_get_le32(data + 0));
   4134 	print_wsc_mac_address(level + 1, "Session MAC Address", data + 4, 6);
   4135 }
   4136 
   4137 static void print_p2p_feature_caps(unsigned int level, const char *label,
   4138 					const void *data, uint16_t size)
   4139 {
   4140 	const uint8_t *bytes = data;
   4141 
   4142 	if (size != 2) {
   4143 		printf("malformed P2P %s\n", label);
   4144 		return;
   4145 	}
   4146 
   4147 	print_attr(level, "%s:", label);
   4148 
   4149 	if (bytes[0] == 0x01)
   4150 		print_attr(level + 1, "Coordination Protocol Transport: UDP");
   4151 	else
   4152 		print_attr(level + 1, "Coordination Protocol Transport: "
   4153 				"reserved 0x%02x", bytes[1]);
   4154 }
   4155 
   4156 static struct attr_entry p2p_attr_entry[] = {
   4157 	{ P2P_ATTR_STATUS,			"Status",
   4158 		ATTR_CUSTOM,	{ .function = print_p2p_status } },
   4159 	{ P2P_ATTR_MINOR_REASON_CODE,		"Minor Reason Code",
   4160 		ATTR_CUSTOM,	{ .function = print_wsc_byte } },
   4161 	{ P2P_ATTR_P2P_CAPABILITY,		"P2P Capability",
   4162 		ATTR_CUSTOM,	{ .function = print_p2p_capability } },
   4163 	{ P2P_ATTR_P2P_DEVICE_ID,		"P2P Device ID",
   4164 		ATTR_CUSTOM,	{ .function = print_wsc_mac_address } },
   4165 	{ P2P_ATTR_GO_INTENT,			"Group Owner Intent",
   4166 		ATTR_CUSTOM,	{ .function = print_p2p_go_intent } },
   4167 	{ P2P_ATTR_CONFIGURATION_TIMEOUT,	"Configuration Timeout",
   4168 		ATTR_CUSTOM,	{ .function = print_p2p_config_timeout } },
   4169 	{ P2P_ATTR_LISTEN_CHANNEL,		"Listen Channel",
   4170 		ATTR_CUSTOM,	{ .function = print_p2p_oper_channel } },
   4171 	{ P2P_ATTR_P2P_GROUP_BSSID,		"P2P Group BSSID",
   4172 		ATTR_CUSTOM,	{ .function = print_wsc_mac_address } },
   4173 	{ P2P_ATTR_EXTENDED_LISTEN_TIMING,	"Extended Listen Timing",
   4174 		ATTR_CUSTOM,	{ .function = print_p2p_extended_timing } },
   4175 	{ P2P_ATTR_INTENDED_P2P_INTERFACE_ADDR,	"Intended P2P Interface "
   4176 		"Address",
   4177 		ATTR_CUSTOM,	{ .function = print_wsc_mac_address } },
   4178 	{ P2P_ATTR_P2P_MANAGEABILITY,		"P2P Manageability",
   4179 		ATTR_CUSTOM,	{ .function = print_p2p_manageability } },
   4180 	{ P2P_ATTR_CHANNEL_LIST,		"Channel List",
   4181 		ATTR_CUSTOM,	{ .function = print_p2p_channel_list } },
   4182 	{ P2P_ATTR_NOTICE_OF_ABSENCE,		"Notice of Absence",
   4183 		ATTR_CUSTOM,	{ .function = print_p2p_notice_of_absence } },
   4184 	{ P2P_ATTR_P2P_DEVICE_INFO,		"P2P Device Info",
   4185 		ATTR_CUSTOM,	{ .function = print_p2p_device_info } },
   4186 	{ P2P_ATTR_P2P_GROUP_INFO,		"P2P Group Info",
   4187 		ATTR_CUSTOM,	{ .function = print_p2p_group_info } },
   4188 	{ P2P_ATTR_P2P_GROUP_ID,		"P2P Group ID",
   4189 		ATTR_CUSTOM,	{ .function = print_p2p_group_id } },
   4190 	{ P2P_ATTR_P2P_INTERFACE,		"P2P Interface",
   4191 		ATTR_CUSTOM,	{ .function = print_p2p_interface } },
   4192 	{ P2P_ATTR_OPERATING_CHANNEL,		"Operating Channel",
   4193 		ATTR_CUSTOM,	{ .function = print_p2p_oper_channel } },
   4194 	{ P2P_ATTR_INVITATION_FLAGS,		"Invitation Flags",
   4195 		ATTR_CUSTOM,	{ .function = print_p2p_invite_flags } },
   4196 	{ P2P_ATTR_OOB_GO_NEGOTIATION_CHANNEL,	"Out-of-Band Group Owner "
   4197 		"Negotiation Channel",
   4198 		ATTR_CUSTOM,	{ .function = print_p2p_oob_neg_channel } },
   4199 	{ P2P_ATTR_SVC_HASH,			"Service Hash",
   4200 		ATTR_CUSTOM,	{ .function = print_p2p_service_hash } },
   4201 	{ P2P_ATTR_SESSION_INFO_DATA_INFO,	"Session Information Data Info",
   4202 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   4203 	{ P2P_ATTR_CONNECTION_CAPABILITY_INFO,	"Connection Capability Info",
   4204 		ATTR_CUSTOM,	{ .function = print_p2p_connection_caps } },
   4205 	{ P2P_ATTR_ADVERTISEMENT_ID_INFO,	"Advertisement_ID Info",
   4206 		ATTR_CUSTOM,	{ .function = print_p2p_advertisement_id } },
   4207 	{ P2P_ATTR_ADVERTISED_SVC_INFO,		"Advertised Service Info",
   4208 		ATTR_CUSTOM,	{ .function = print_p2p_advertised_svc_info } },
   4209 	{ P2P_ATTR_SESSION_ID_INFO,		"Session ID Info",
   4210 		ATTR_CUSTOM,	{ .function = print_p2p_session_id } },
   4211 	{ P2P_ATTR_FEATURE_CAPABILITY,		"Feature Capability",
   4212 		ATTR_CUSTOM,	{ .function = print_p2p_feature_caps } },
   4213 	{ P2P_ATTR_PERSISTENT_GROUP_INFO,	"Persistent Group Info",
   4214 		ATTR_CUSTOM,	{ .function = print_p2p_group_id } },
   4215 	{ P2P_ATTR_VENDOR_SPECIFIC_ATTR,	"Vendor specific attribute",
   4216 		ATTR_CUSTOM,	{ .function = print_wsc_bytes } },
   4217 	{ },
   4218 };
   4219 
   4220 static void print_p2p_attributes(unsigned int level, const char *label,
   4221 					const void *data, uint16_t size)
   4222 {
   4223 	struct p2p_attr_iter iter;
   4224 	int i;
   4225 
   4226 	print_attr(level, "%s: len %u", label, size);
   4227 
   4228 	p2p_attr_iter_init(&iter, data, size);
   4229 
   4230 	while (p2p_attr_iter_next(&iter)) {
   4231 		uint16_t type = p2p_attr_iter_get_type(&iter);
   4232 		uint16_t len = p2p_attr_iter_get_length(&iter);
   4233 		const void *attr = p2p_attr_iter_get_data(&iter);
   4234 		struct attr_entry *entry = NULL;
   4235 
   4236 		for (i = 0; p2p_attr_entry[i].str; i++) {
   4237 			if (p2p_attr_entry[i].attr == type) {
   4238 				entry = &p2p_attr_entry[i];
   4239 				break;
   4240 			}
   4241 		}
   4242 
   4243 		if (entry && entry->function)
   4244 			entry->function(level + 1, entry->str, attr, len);
   4245 		else {
   4246 			print_attr(level + 1, "Type: 0x%02x: len %u",
   4247 								type, len);
   4248 			print_hexdump(level + 2, attr, len);
   4249 		}
   4250 	}
   4251 }
   4252 
   4253 static void print_wfd_device_info_flags(unsigned int level, const char *label,
   4254 					uint16_t caps)
   4255 {
   4256 	static const char *dev_type[] = {
   4257 		[WFD_DEV_INFO_TYPE_SOURCE] = "Source",
   4258 		[WFD_DEV_INFO_TYPE_PRIMARY_SINK] = "Primary sink",
   4259 		[WFD_DEV_INFO_TYPE_SECONDARY_SINK] = "Secondary sink",
   4260 		[WFD_DEV_INFO_TYPE_DUAL_ROLE] = "Dual-role possible",
   4261 	};
   4262 	static const char *session_avail[] = {
   4263 		[0] = "Not available for WFD Session",
   4264 		[1] = "Available for WFD Session",
   4265 		[2] = "Reserved (0b10)",
   4266 		[3] = "Reserved (0b11)",
   4267 	};
   4268 
   4269 	print_attr(level, "%s:", label);
   4270 
   4271 	print_attr(level + 1, "Device Type: %s",
   4272 			dev_type[caps & WFD_DEV_INFO_DEVICE_TYPE]);
   4273 	CHECK_CAPS_BIT(WFD_DEV_INFO_COUPLED_SINK_AT_SOURCE_OK,
   4274 			"Coupled Sink Operation supported by WFD Source");
   4275 	CHECK_CAPS_BIT(WFD_DEV_INFO_COUPLED_SINK_AT_SINK_OK,
   4276 			"Coupled Sink Operation supported by WFD Sink");
   4277 	print_attr(level + 1, "Session Availability: %s",
   4278 			session_avail[(caps &
   4279 				WFD_DEV_INFO_SESSION_AVAILABILITY) >> 4]);
   4280 	CHECK_CAPS_BIT(WFD_DEV_INFO_SERVICE_DISCOVERY_SUPPORT,
   4281 			"WFD Service Discovery (WSD) supported");
   4282 	print_attr(level + 1, "Preferred Connectivity (PC): %s",
   4283 			(caps & WFD_DEV_INFO_PREFER_TDLS_CONNECTIVITY) ?
   4284 			"TLDS" : "P2P");
   4285 	CHECK_CAPS_BIT(WFD_DEV_INFO_CONTENT_PROTECTION_SUPPORT,
   4286 			"Content Protection using HDCP system 2.x supported");
   4287 	CHECK_CAPS_BIT(WFD_DEV_INFO_8021AS_TIME_SYNC_SUPPORT,
   4288 			"Time Synchronization using 802.1AS supported");
   4289 	CHECK_CAPS_BIT(WFD_DEV_INFO_NO_AUDIO_AT_PRIMARY_SINK,
   4290 			"WFD Primary Sink does not support audio rendering");
   4291 	CHECK_CAPS_BIT(WFD_DEV_INFO_AUDIO_ONLY_AT_SOURCE,
   4292 			"WFD Source supports audio-only element stream");
   4293 	CHECK_CAPS_BIT(WFD_DEV_INFO_TDLS_PERSISTENT_GROUP,
   4294 			"TDLS persistent group intended");
   4295 	CHECK_CAPS_BIT(WFD_DEV_INFO_REINVOKE_TDLS_GROUP,
   4296 			"Request for re-invocation of TDLS persistent group");
   4297 
   4298 	caps &= ~0x00ff;
   4299 	if (caps)
   4300 		print_attr(level + 1, "Reserved: 0x%04x", caps);
   4301 }
   4302 
   4303 static void print_wfd_device_info(unsigned int level, const char *label,
   4304 					const void *data, uint16_t size)
   4305 {
   4306 	const uint8_t *bytes = data;
   4307 
   4308 	if (size != 6) {
   4309 		printf("malformed WFD %s\n", label);
   4310 		return;
   4311 	}
   4312 
   4313 	print_wfd_device_info_flags(level, label, l_get_be16(bytes + 0));
   4314 
   4315 	print_attr(level, "%s: Session Management Control port %i",
   4316 			label, l_get_be16(bytes + 2));
   4317 	print_attr(level, "%s: Maximum Throughput %i Mbps",
   4318 			label, l_get_be16(bytes + 4));
   4319 }
   4320 
   4321 static void print_wfd_coupled_sink_info(unsigned int level, const char *label,
   4322 					const void *data, uint16_t size)
   4323 {
   4324 	const uint8_t *bytes = data;
   4325 	static const char *status[4] = {
   4326 		[0] = "Not couple/Available for Coupling",
   4327 		[1] = "Coupled",
   4328 		[2] = "Teardown Coupling",
   4329 		[3] = "Reserved (0b11)",
   4330 	};
   4331 
   4332 	if (size != 7) {
   4333 		printf("malformed WFD %s\n", label);
   4334 		return;
   4335 	}
   4336 
   4337 	print_attr(level, "%s:", label);
   4338 
   4339 	print_attr(level + 1, "Status: %s", status[bytes[0] & 3]);
   4340 
   4341 	if (bytes[0] & ~3)
   4342 		print_attr(level + 1, "Reserved: 0x%02x", bytes[0] & ~3);
   4343 
   4344 	print_address(level + 1, "Coupled Sink MAC Address", bytes + 1);
   4345 }
   4346 
   4347 static void print_wfd_extended_caps(unsigned int level, const char *label,
   4348 					const void *data, uint16_t size)
   4349 {
   4350 	const uint8_t *bytes = data;
   4351 	uint16_t caps;
   4352 
   4353 	if (size != 2) {
   4354 		printf("malformed WFD %s\n", label);
   4355 		return;
   4356 	}
   4357 
   4358 	print_attr(level, "%s:", label);
   4359 
   4360 	caps = l_get_be16(bytes + 0);
   4361 	CHECK_CAPS_BIT(0x0001, "UIBC support");
   4362 	CHECK_CAPS_BIT(0x0002, "I2C Read/Write support");
   4363 	CHECK_CAPS_BIT(0x0004, "Preferred Display mode support");
   4364 	CHECK_CAPS_BIT(0x0008, "Standby and Resume Control support");
   4365 	CHECK_CAPS_BIT(0x0010, "TDLS Persistent support");
   4366 	CHECK_CAPS_BIT(0x0020, "TDLS Persistent BSSID support");
   4367 
   4368 	if (caps)
   4369 		print_attr(level + 1, "Reserved: 0x%04x", caps);
   4370 }
   4371 
   4372 static void print_wfd_local_ip(unsigned int level, const char *label,
   4373 				const void *data, uint16_t size)
   4374 {
   4375 	const uint8_t *bytes = data;
   4376 
   4377 	if (size != 5) {
   4378 		printf("malformed WFD %s\n", label);
   4379 		return;
   4380 	}
   4381 
   4382 	if (bytes[0] != 1) {
   4383 		print_attr(level, "%s: Unknown version", label);
   4384 		return;
   4385 	}
   4386 
   4387 	print_attr(level, "%s: %i.%i.%i.%i", label,
   4388 			bytes[1], bytes[2], bytes[3], bytes[4]);
   4389 }
   4390 
   4391 static void print_wfd_session_info(unsigned int level, const char *label,
   4392 					const void *data, uint16_t size)
   4393 {
   4394 	int i = 1;
   4395 
   4396 	if (size % 24 != 0) {
   4397 		printf("malformed WFD %s\n", label);
   4398 		return;
   4399 	}
   4400 
   4401 	print_attr(level, "%s:", label);
   4402 
   4403 	while (size) {
   4404 		const uint8_t *bytes = data;
   4405 
   4406 		if (bytes[0] != 23) {
   4407 			print_attr(level + 1,
   4408 					"malformed WFD Device Info Descriptor");
   4409 			continue;
   4410 		}
   4411 
   4412 		print_attr(level + 1, "Device Info for client %i:", i++);
   4413 
   4414 		if (bytes[0] != 1) {
   4415 			print_attr(level, "%s: Unknown version", label);
   4416 			return;
   4417 		}
   4418 
   4419 		print_address(level + 2, "Device address", bytes + 1);
   4420 
   4421 		if (l_memeqzero(bytes + 7, 6))
   4422 			print_attr(level+ + 2, "Not associated to an "
   4423 					"infrastructure AP");
   4424 		else
   4425 			print_address(level + 2, "Associated BSSID", bytes + 7);
   4426 
   4427 		print_wfd_device_info_flags(level + 2, "WFD Device Information",
   4428 						l_get_be16(bytes + 13));
   4429 		print_attr(level + 2, "WFD Device Maximum Throughput %i Mbps",
   4430 						l_get_be16(bytes + 15));
   4431 		print_wfd_coupled_sink_info(level + 2,
   4432 						"Coupled Sink Information",
   4433 						bytes + 17, 7);
   4434 
   4435 		data += 24;
   4436 		size -= 24;
   4437 	}
   4438 }
   4439 
   4440 static void print_wfd_r2_device_info(unsigned int level, const char *label,
   4441 					const void *data, uint16_t size)
   4442 {
   4443 	static const char *dev_type[4] = {
   4444 		[0] = "WFD R2 Source",
   4445 		[1] = "WFD R2 Primary sink",
   4446 		[2] = "Reserved (0b10)",
   4447 		[3] = "Dual-role possible",
   4448 	};
   4449 	const uint8_t *bytes = data;
   4450 	uint16_t caps;
   4451 
   4452 	if (size < 2) {
   4453 		printf("malformed WFD %s\n", label);
   4454 		return;
   4455 	}
   4456 
   4457 	print_attr(level, "%s:", label);
   4458 
   4459 	caps = l_get_be16(bytes + 0);
   4460 	print_attr(level + 1, "WFD R2 Device Type: %s", dev_type[caps & 3]);
   4461 
   4462 	if (caps & ~3)
   4463 		print_attr(level + 1, "Reserved: 0x%04x", caps & ~3);
   4464 }
   4465 
   4466 static struct attr_entry wfd_subelem_entry[] = {
   4467 	{ WFD_SUBELEM_WFD_DEVICE_INFORMATION,	"WFD Device Information",
   4468 		ATTR_CUSTOM,	{ .function = print_wfd_device_info } },
   4469 	{ WFD_SUBELEM_ASSOCIATED_BSSID, 	"Associated BSSID",
   4470 		ATTR_ADDRESS },
   4471 	{ WFD_SUBELEM_COUPLED_SINK_INFORMATION,	"Coupled Sink Information",
   4472 		ATTR_CUSTOM,	{ .function = print_wfd_coupled_sink_info } },
   4473 	{ WFD_SUBELEM_EXTENDED_CAPABILITY,	"WFD Extended Capability",
   4474 		ATTR_CUSTOM,	{ .function = print_wfd_extended_caps } },
   4475 	{ WFD_SUBELEM_LOCAL_IP_ADDRESS,		"Local IP Address",
   4476 		ATTR_CUSTOM,	{ .function = print_wfd_local_ip } },
   4477 	{ WFD_SUBELEM_SESION_INFORMATION,	"WFD Session Information",
   4478 		ATTR_CUSTOM,	{ .function = print_wfd_session_info } },
   4479 	{ WFD_SUBELEM_ALTERNATIVE_MAC_ADDRESS,	"Alternative MAC Address",
   4480 		ATTR_ADDRESS },
   4481 	{ WFD_SUBELEM_R2_DEVICE_INFORMATION,	"WFD R2 Device Information",
   4482 		ATTR_CUSTOM,	{ .function = print_wfd_r2_device_info } },
   4483 	{ },
   4484 };
   4485 
   4486 static void print_wfd_subelements(unsigned int level, const char *label,
   4487 					const void *data, uint16_t size)
   4488 {
   4489 	struct wfd_subelem_iter iter;
   4490 	int i;
   4491 
   4492 	print_attr(level, "%s: len %u", label, size);
   4493 
   4494 	wfd_subelem_iter_init(&iter, data, size);
   4495 
   4496 	while (wfd_subelem_iter_next(&iter)) {
   4497 		uint16_t type = wfd_subelem_iter_get_type(&iter);
   4498 		uint16_t len = wfd_subelem_iter_get_length(&iter);
   4499 		const void *attr = wfd_subelem_iter_get_data(&iter);
   4500 		struct attr_entry *entry = NULL;
   4501 
   4502 		for (i = 0; wfd_subelem_entry[i].str; i++) {
   4503 			if (wfd_subelem_entry[i].attr == type) {
   4504 				entry = &wfd_subelem_entry[i];
   4505 				break;
   4506 			}
   4507 		}
   4508 
   4509 		if (!entry)
   4510 			continue;
   4511 
   4512 		switch (entry->type) {
   4513 		case ATTR_ADDRESS:
   4514 			if (len != 6) {
   4515 				print_attr(level + 1, "malformed %s",
   4516 						entry->str);
   4517 				break;
   4518 			}
   4519 
   4520 			print_address(level + 1, entry->str, attr);
   4521 			break;
   4522 		default:
   4523 			if (entry->function)
   4524 				entry->function(level + 1, entry->str, attr,
   4525 						len);
   4526 			else {
   4527 				print_attr(level + 1, "Type: 0x%02x: len %u",
   4528 						type, len);
   4529 				print_hexdump(level + 2, attr, len);
   4530 			}
   4531 			break;
   4532 		}
   4533 	}
   4534 }
   4535 
   4536 static void print_management_ies(unsigned int level, const char *label,
   4537 					const void *data, uint16_t size)
   4538 {
   4539 	void *wsc_data, *p2p_data, *wfd_data;
   4540 	ssize_t wsc_len, p2p_len, wfd_len;
   4541 
   4542 	print_ie(level, label, data, size);
   4543 
   4544 	if (cur_nlmon && cur_nlmon->noies)
   4545 		return;
   4546 
   4547 	wsc_data = ie_tlv_extract_wsc_payload(data, size, &wsc_len);
   4548 	if (wsc_data) {
   4549 		print_wsc_attributes(level + 1, "WSC Payload",
   4550 					wsc_data, wsc_len);
   4551 		l_free(wsc_data);
   4552 	}
   4553 
   4554 	p2p_data = ie_tlv_extract_p2p_payload(data, size, &p2p_len);
   4555 	if (p2p_data) {
   4556 		print_p2p_attributes(level + 1, "P2P Attributes",
   4557 					p2p_data, p2p_len);
   4558 		l_free(p2p_data);
   4559 	}
   4560 
   4561 	wfd_data = ie_tlv_extract_wfd_payload(data, size, &wfd_len);
   4562 	if (wfd_data) {
   4563 		print_wfd_subelements(level + 1, "WFD Payload",
   4564 					wfd_data, wfd_len);
   4565 		l_free(wfd_data);
   4566 	}
   4567 }
   4568 
   4569 static void print_reason_code(unsigned int level, const char *label,
   4570 				const void *data, uint16_t size)
   4571 {
   4572 	uint16_t rc;
   4573 	/* 802.11-2012, Table 8-36 */
   4574 	static const char *reason_code_table[] = {
   4575 		[0] = "Reserved",
   4576 		[1] = "Unspecified reason",
   4577 		[2] = "Previous authentication no longer valid",
   4578 		[3] = "Leaving",
   4579 		[4] = "Disassociation due to inactivity",
   4580 		[5] = "Disassociated because AP is unable to handle all"
   4581 			" currently associated STAs",
   4582 		[6] = "Class 2 frame received from nonauthenticated STA",
   4583 		[7] = "Class 3 frame received from nonassociated STA",
   4584 		[8] = "Disassociated because sending STA is leaving",
   4585 		[9] = "STA requesting (re)association is not authenticated "
   4586 			"with responding STA",
   4587 		[10] = "Disassociated because the information in the Power "
   4588 			"Capability element is unacceptable",
   4589 		[11] = "Disassociated because the information in the Supported "
   4590 			"Channels element is unacceptable",
   4591 		[12] = "Disassociated due to BSS Transition Management",
   4592 		[13] = "Invalid element",
   4593 		[14] = "MIC failure",
   4594 		[15] = "4-Way Handshake timeout",
   4595 		[16] = "Group Key Handshake timeout",
   4596 		[17] = "Element in 4-Way Handshake different from "
   4597 			"(Re)Association Request/Probe Response/Beacon frame",
   4598 		[18] = "Invalid group cipher",
   4599 		[19] = "Invalid pairwise cipher",
   4600 		[20] = "Invalid AKMP",
   4601 		[21] = "Unsupported RSNE version",
   4602 		[22] = "Invalid RSNE capabilities",
   4603 		[23] = "IEEE 802.1X authentication failed",
   4604 		[24] = "Cipher suite rejected because of the security policy",
   4605 		[25] = "TDLS direct-link teardown due to TDLS peer STA "
   4606 			"unreachable via the TDLS direct link",
   4607 		[26] = "TDLS direct-link teardown for unspecified reason",
   4608 		[27] = "Disassociated because session terminated by SSP "
   4609 			"request",
   4610 		[28] = "Disassociated because of lack of SSP roaming agreement",
   4611 		[29] = "Requested service rejected because of SSP cipher "
   4612 			"suite or AKM requirement",
   4613 		[30] = "Requested service not authorized in this location",
   4614 		[31] = "TS deleted because QoS AP lacks sufficient bandwidth "
   4615 			"for this QoS STA due to a change in BSS service "
   4616 			"characteristics or operational mode",
   4617 		[32] = "Disassociated for unspecified, QoS-related reason",
   4618 		[33] = "Disassociated because QoS AP lacks sufficient "
   4619 			"bandwidth for this QoS STA",
   4620 		[34] = "Disassociated because excessive number of frames need "
   4621 			"to be acknowledged, but are not acknowledged due to "
   4622 			"AP transmissions and/or poor channel conditions",
   4623 		[35] = "Disassociated because STA is transmitting outside the "
   4624 			"limits of its TXOPs",
   4625 		[36] = "Requested from peer STA as the STA is leaving",
   4626 		[37] = "Requested from peer STA as it does not want to use the "
   4627 			"mechanism",
   4628 		[38] = "Requested from peer STA as the STA received frames "
   4629 			"using the mechanism for which a setup is required",
   4630 		[39] = "Requested from peer STA due to timeout",
   4631 		[40 ... 44] = "Reserved",
   4632 		[45] = "Peer STA does not support the requested cipher suite",
   4633 		[46] = "The teardown was initiated by the DLS peer | "
   4634 			"Disassociated because authorized access limit reached",
   4635 		[47] = "The teardown was initiated by the AP | "
   4636 			"Disassociated due to external service requirements",
   4637 		[48] = "Invalid FT Action frame count",
   4638 		[49] = "Invalid PMKI",
   4639 		[50] = "Invalid MDE",
   4640 		[51] = "Invalid FTE",
   4641 		[52] = "SME cancels the mesh peering instance with the reason "
   4642 			"other than reaching the maximum number of peer mesh "
   4643 			"STAs",
   4644 		[53] = "The mesh STA has reached the supported maximum number "
   4645 			"of peer mesh STAs",
   4646 		[54] = "The received information violates the Mesh "
   4647 			"Configuration policy configured in the mesh STA "
   4648 			"profile",
   4649 		[55] = "The mesh STA has received a Mesh Peering Close message "
   4650 			"requesting to close the mesh peering.",
   4651 		[56] = "The mesh STA has resent dot11MeshMaxRetries Mesh "
   4652 			"Peering Open messages, without receiving a Mesh "
   4653 			"Peering Confirm message.",
   4654 		[57] = "The confirmTimer for the mesh peering instance times "
   4655 			"out.",
   4656 		[58] = "The mesh STA fails to unwrap the GTK or the values in "
   4657 			"the wrapped contents do not match",
   4658 		[59] = "The mesh STA receives inconsistent information about "
   4659 			"the mesh parameters between Mesh Peering Management "
   4660 			"frames",
   4661 		[60] = "The mesh STA fails the authenticated mesh peering "
   4662 			"exchange because due to failure in selecting either "
   4663 			"the pairwise ciphersuite or group ciphersuite",
   4664 		[61] = "The mesh STA does not have proxy information for this "
   4665 			"external destination.",
   4666 		[62] = "The mesh STA does not have forwarding information for "
   4667 			"this destination.",
   4668 		[63] = "The mesh STA determines that the link to the next hop "
   4669 			"of an active path in its forwarding information is "
   4670 			"no longer usable.",
   4671 		[64] = "The Deauthentication frame was sent because the MAC "
   4672 			"address of the STA already exists in the mesh BSS. "
   4673 			"See 10.3.6.",
   4674 		[65] = "The mesh STA performs channel switch to meet "
   4675 			"regulatory requirements.",
   4676 		[66] = "The mesh STA performs channel switch with unspecified "
   4677 			"reason.",
   4678 	};
   4679 
   4680 	if (size != 2)
   4681 		return;
   4682 
   4683 	rc = *((uint16_t *) data);
   4684 
   4685 	if (rc >= L_ARRAY_SIZE(reason_code_table))
   4686 		print_attr(level, "%s: Reserved", label);
   4687 	else
   4688 		print_attr(level, "%s: %s", label, reason_code_table[rc]);
   4689 }
   4690 
   4691 static void print_mpdu_frame_control(unsigned int level,
   4692 						const struct mpdu_fc *fc)
   4693 {
   4694 	print_attr(level, "Frame Control: protocol: %02u type: %02u "
   4695 			"subtype: %02u to: %02u from: %02u more_frags: %02u",
   4696 			fc->protocol_version, fc->type, fc->subtype,
   4697 			fc->to_ds, fc->from_ds, fc->more_fragments);
   4698 	print_attr(level + 1, "retry: %02u power_mgmt: %02u more_data: %02u "
   4699 				"protected: %02u order: %02u",
   4700 				fc->retry, fc->power_mgmt, fc->more_data,
   4701 				fc->protected_frame, fc->order);
   4702 }
   4703 
   4704 static void print_mmpdu_header(unsigned int level,
   4705 					const struct mmpdu_header *mmpdu)
   4706 {
   4707 	print_attr(level, "Duration: %u", L_LE16_TO_CPU(mmpdu->duration));
   4708 
   4709 	print_address(level, "Address 1 (RA):", mmpdu->address_1);
   4710 	print_address(level, "Address 2 (TA):", mmpdu->address_2);
   4711 	print_address(level, "Address 3:", mmpdu->address_3);
   4712 
   4713 	print_attr(level, "Fragment Number: %u", mmpdu->fragment_number);
   4714 	print_attr(level, "Sequence Number: %u",
   4715 					MPDU_SEQUENCE_NUMBER(*mmpdu));
   4716 }
   4717 
   4718 static void print_association_mgmt_frame(unsigned int level,
   4719 					const struct mmpdu_header *mmpdu,
   4720 					size_t size)
   4721 {
   4722 	const struct mmpdu_association_response *body;
   4723 
   4724 	body = mmpdu_body(mmpdu);
   4725 
   4726 	print_attr(level, "Association Response:");
   4727 
   4728 	print_mpdu_frame_control(level + 1, &mmpdu->fc);
   4729 	print_mmpdu_header(level + 1, mmpdu);
   4730 
   4731 	print_attr(level + 1, "Status Code: %u", body->status_code);
   4732 	print_attr(level + 1, "AID: %u", body->aid);
   4733 
   4734 	print_management_ies(level, "IEs", body->ies,
   4735 				size - 6 - sizeof(struct mmpdu_header));
   4736 }
   4737 
   4738 static void print_authentication_mgmt_frame(unsigned int level,
   4739 					const struct mmpdu_header *mmpdu,
   4740 					size_t size)
   4741 {
   4742 	const char *str;
   4743 	const struct mmpdu_authentication *body;
   4744 	uint16_t alg;
   4745 
   4746 	if (!mmpdu)
   4747 		return;
   4748 
   4749 	body = mmpdu_body(mmpdu);
   4750 
   4751 	print_attr(level, "Authentication:");
   4752 
   4753 	print_mpdu_frame_control(level + 1, &mmpdu->fc);
   4754 	print_mmpdu_header(level + 1, mmpdu);
   4755 	alg = L_LE16_TO_CPU(body->algorithm);
   4756 
   4757 	switch (alg) {
   4758 	case MMPDU_AUTH_ALGO_OPEN_SYSTEM:
   4759 		str = "Open";
   4760 		break;
   4761 	case MMPDU_AUTH_ALGO_SHARED_KEY:
   4762 		str = "Shared key";
   4763 		break;
   4764 	case MMPDU_AUTH_ALGO_FT:
   4765 		str = "FT";
   4766 		break;
   4767 	case MMPDU_AUTH_ALGO_SAE:
   4768 		str = "SAE";
   4769 		break;
   4770 	default:
   4771 		str = "Reserved";
   4772 		break;
   4773 	}
   4774 
   4775 	print_attr(level + 1, "Algorithm: %s (seq: %u, status: %u)", str,
   4776 				L_LE16_TO_CPU(body->transaction_sequence),
   4777 				L_LE16_TO_CPU(body->status));
   4778 
   4779 	if (!L_IN_SET(alg, MMPDU_AUTH_ALGO_SHARED_KEY,
   4780 				MMPDU_AUTH_ALGO_FT, MMPDU_AUTH_ALGO_SAE))
   4781 		return;
   4782 
   4783 	if (L_LE16_TO_CPU(body->transaction_sequence) < 2 ||
   4784 			L_LE16_TO_CPU(body->transaction_sequence) > 3)
   4785 		return;
   4786 
   4787 	print_ie(level + 1, "IEs", body->ies,
   4788 			(const uint8_t *) mmpdu + size - body->ies);
   4789 }
   4790 
   4791 static void print_deauthentication_mgmt_frame(unsigned int level,
   4792 					const struct mmpdu_header *mmpdu)
   4793 {
   4794 	const struct mmpdu_deauthentication *body;
   4795 
   4796 	if (!mmpdu)
   4797 		return;
   4798 
   4799 	body = mmpdu_body(mmpdu);
   4800 
   4801 	print_attr(level, "Deauthentication:");
   4802 
   4803 	print_mpdu_frame_control(level + 1, &mmpdu->fc);
   4804 	print_mmpdu_header(level + 1, mmpdu);
   4805 
   4806 	print_attr(level + 1, "Reason code: %u",
   4807 				L_LE16_TO_CPU(body->reason_code));
   4808 }
   4809 
   4810 static void print_p2p_public_action_frame(unsigned int level,
   4811 						const uint8_t *body,
   4812 						size_t body_len)
   4813 {
   4814 	const char *subtype;
   4815 
   4816 	/* P2P v1.7 Table 60 */
   4817 	static const char *p2p_public_table[] = {
   4818 		[0] = "GO Negotiation Request",
   4819 		[1] = "GO Negotiation Response",
   4820 		[2] = "GO Negotiation Confirmation",
   4821 		[3] = "P2P Invitation Request",
   4822 		[4] = "P2P Invitation Response",
   4823 		[5] = "Device Discoverability Request",
   4824 		[6] = "Device Discoverability Response",
   4825 		[7] = "Provision Discovery Request",
   4826 		[8] = "Provision Discovery Response",
   4827 	};
   4828 
   4829 	if (body_len < 2)
   4830 		return;
   4831 
   4832 	if (body[1] < L_ARRAY_SIZE(p2p_public_table) &&
   4833 			p2p_public_table[body[0]])
   4834 		subtype = p2p_public_table[body[0]];
   4835 	else
   4836 		subtype = "Unknown";
   4837 
   4838 	print_attr(level, "OUI Type: P2P public action frame");
   4839 	print_attr(level, "OUI Subtype: %s (%u)", subtype, body[0]);
   4840 	print_attr(level + 1, "Dialog Token: %u", body[1]);
   4841 
   4842 	print_management_ies(level, "IEs", body + 2, body_len - 2);
   4843 }
   4844 
   4845 static void print_p2p_action_frame(unsigned int level, const uint8_t *body,
   4846 					size_t body_len)
   4847 {
   4848 	const char *subtype;
   4849 
   4850 	/* P2P v1.7 Table 74 */
   4851 	static const char *p2p_action_table[] = {
   4852 		[0] = "Notice of Absence",
   4853 		[1] = "P2P Presence Request",
   4854 		[2] = "P2P Presence Response",
   4855 		[3] = "GO Discoverability Request",
   4856 	};
   4857 
   4858 	if (body_len < 2)
   4859 		return;
   4860 
   4861 	if (body[1] < L_ARRAY_SIZE(p2p_action_table) &&
   4862 			p2p_action_table[body[0]])
   4863 		subtype = p2p_action_table[body[0]];
   4864 	else
   4865 		subtype = "Unknown";
   4866 
   4867 	print_attr(level, "OUI Type: P2P action frame");
   4868 	print_attr(level, "OUI Subtype: %s (%u)", subtype, body[0]);
   4869 	print_attr(level + 1, "Dialog Token: %u", body[1]);
   4870 
   4871 	print_management_ies(level, "IEs", body + 2, body_len - 2);
   4872 }
   4873 
   4874 static void print_anqp_frame(unsigned int level, const uint8_t *anqp,
   4875 				size_t anqp_len)
   4876 {
   4877 	struct anqp_iter iter;
   4878 	uint16_t id, len;
   4879 	const void *data;
   4880 
   4881 	static const char *anqp_elements[] = {
   4882 		[ANQP_QUERY_LIST] = "Query List",
   4883 		[ANQP_CAPABILITY_LIST] = "Capability List",
   4884 		[ANQP_VENUE_NAME] = "Venue Name",
   4885 		[ANQP_EMERGENCY_CALL_NUMBER] = "Emergency Call Number",
   4886 		[ANQP_NETWORK_AUTH_TYPE] = "Network Authentication Type",
   4887 		[ANQP_ROAMING_CONSORTIUM] = "Roaming Consortium",
   4888 		[ANQP_IP_ADDRESS_TYPE_AVAILABILITY] = "IP Address type avail",
   4889 		[ANQP_NAI_REALM] = "NAI Realm",
   4890 		[ANQP_3GPP_CELLULAR_NETWORK] = "3GPP Cellular Network",
   4891 		[ANQP_AP_GEOSPATIAL_LOCATION] = "AP Geospatial location",
   4892 		[ANQP_AP_CIVIC_LOCATION] = "AP Civic Location",
   4893 		[ANQP_AP_LOCATION_PUBLIC_ID] = "AP Location Public ID",
   4894 		[ANQP_DOMAIN_NAME] = "Domain Name",
   4895 		[ANQP_EMERGENCY_ALERT_ID_URI] = "Emergency Alery ID URI",
   4896 		[ANQP_TDLS_CAPABILITY] = "TDLS Capability",
   4897 		[ANQP_EMERGENCY_NAI] = "Emergency NAI",
   4898 		[ANQP_NEIGHBOR_REPORT] = "Neighbor Report",
   4899 		[ANQP_VENUE_URI] = "Venue URI",
   4900 		[ANQP_ADVICE_OF_CHARGE] = "Advice of Charge",
   4901 		[ANQP_LOCAL_CONTENT] = "Local Content",
   4902 		[ANQP_NETWORK_AUTH_TYPE_WITH_TIMESTAMP] =
   4903 					"Network Auth Type with Timestamp",
   4904 		[ANQP_VENDOR_SPECIFIC] = "Vendor Specific"
   4905 	};
   4906 
   4907 	anqp_iter_init(&iter, anqp, anqp_len);
   4908 
   4909 	while (anqp_iter_next(&iter, &id, &len, &data)) {
   4910 		const char *str;
   4911 		char **nai_realms;
   4912 		int i;
   4913 
   4914 		if (id >= L_ARRAY_SIZE(anqp_elements) || id < ANQP_QUERY_LIST)
   4915 			str = "Unknown";
   4916 		else
   4917 			str = anqp_elements[id];
   4918 
   4919 		print_attr(level, "ANQP ID: %s, Len: %u", str, len);
   4920 
   4921 		switch (id) {
   4922 		case ANQP_NAI_REALM:
   4923 			nai_realms = anqp_parse_nai_realms(data, len);
   4924 			if (!nai_realms) {
   4925 				print_attr(level + 1, "bad NAI Realm data");
   4926 				break;
   4927 			}
   4928 
   4929 			for (i = 0; nai_realms[i]; i++)
   4930 				print_attr(level + 2, "Realm[%u] %s", i,
   4931 						nai_realms[i]);
   4932 
   4933 			l_strv_free(nai_realms);
   4934 
   4935 			break;
   4936 		default:
   4937 			print_hexdump(level + 1, anqp + 4, len);
   4938 		}
   4939 	}
   4940 }
   4941 
   4942 static void print_dpp_public_action_frame(unsigned int level,
   4943 					const uint8_t *data, size_t len)
   4944 {
   4945 	print_attr(level, "DPP Action Frame");
   4946 
   4947 	print_attr(level + 1, "Crypto Suite: %u", *data);
   4948 	data++;
   4949 
   4950 	switch (*data) {
   4951 	case 0:
   4952 		print_attr(level + 1, "Type: Authentication Request");
   4953 		break;
   4954 	case 1:
   4955 		print_attr(level + 1, "Type: Authentication Response");
   4956 		break;
   4957 	case 2:
   4958 		print_attr(level + 1, "Type: Authentication Confirm");
   4959 		break;
   4960 	case 11:
   4961 		print_attr(level + 1, "Type: Configuration Result");
   4962 		break;
   4963 	case 13:
   4964 		print_attr(level + 1, "Type: Presence Announcement");
   4965 		break;
   4966 	default:
   4967 		print_attr(level + 1, "Type: Unknown (%u)", *data);
   4968 		break;
   4969 	}
   4970 }
   4971 
   4972 static void print_public_action_frame(unsigned int level, const uint8_t *body,
   4973 					size_t body_len)
   4974 {
   4975 	const char *category;
   4976 	const uint8_t *oui = body + 1;
   4977 
   4978 	/* 802.11-2016, Table 9-307 */
   4979 	static const char *public_action_table[] = {
   4980 		[0] = "20/40 BSS Coexistence Management",
   4981 		[1] = "DSE enablement",
   4982 		[2] = "DSE deenablement",
   4983 		[3] = "DSE Registered Location Announcement",
   4984 		[4] = "Extended Channel Switch Announcement",
   4985 		[5] = "DSE measurement request",
   4986 		[6] = "DSE measurement report",
   4987 		[7] = "Measurement Pilot",
   4988 		[8] = "DSE power constraint",
   4989 		[9] = "Vendor-specific",
   4990 		[10] = "GAS Initial Request",
   4991 		[11] = "GAS Initial Response",
   4992 		[12] = "GAS Comeback Request",
   4993 		[13] = "GAS Comeback Response",
   4994 		[14] = "TDLS Discovery Response",
   4995 		[15] = "Location Track Notification",
   4996 		[16] = "QAB Request frame",
   4997 		[17] = "QAB Response frame",
   4998 		[18] = "QMF Policy",
   4999 		[19] = "QMF Policy Change",
   5000 		[20] = "QLoad Request",
   5001 		[21] = "QLoad Report",
   5002 		[22] = "HCCA TXOP Advertisement",
   5003 		[23] = "HCCA TXOP Response",
   5004 		[24] = "Public Key",
   5005 		[25] = "Channel Availability Query",
   5006 		[26] = "Channel Schedule Management",
   5007 		[27] = "Contact Verification Signal",
   5008 		[28] = "GDD Enablement Request",
   5009 		[29] = "GDD Enablement Response",
   5010 		[30] = "Network Channel Control",
   5011 		[31] = "White Space Map Announcement",
   5012 		[32] = "Fine Timing Measurement Request",
   5013 		[33] = "Fine Timing Measurement",
   5014 	};
   5015 
   5016 	if (body_len < 1)
   5017 		return;
   5018 
   5019 	if (body[0] < L_ARRAY_SIZE(public_action_table) &&
   5020 			public_action_table[body[0]])
   5021 		category = public_action_table[body[0]];
   5022 	else
   5023 		category = "Unknown";
   5024 
   5025 	print_attr(level, "Public Action: %s (%u)", category, body[0]);
   5026 
   5027 	if (body_len < 5)
   5028 		return;
   5029 
   5030 	if (!memcmp(oui, wifi_alliance_oui, 3) && oui[3] == 0x09) {
   5031 		if (body[0] != 9)
   5032 			return;
   5033 
   5034 		if (!print_oui(level, oui))
   5035 			return;
   5036 
   5037 		print_p2p_public_action_frame(level + 1, body + 5,
   5038 						body_len - 5);
   5039 	} else if (!memcmp(oui, wifi_alliance_oui, 3) && oui[3] == 0x1a) {
   5040 		if (!print_oui(level, oui))
   5041 			return;
   5042 
   5043 		print_dpp_public_action_frame(level + 1, body + 5,
   5044 						body_len - 5);
   5045 	} else if (body[0] == 0x0a) {
   5046 		if (body_len < 9)
   5047 			return;
   5048 
   5049 		if (body[2] != IE_TYPE_ADVERTISEMENT_PROTOCOL)
   5050 			return;
   5051 
   5052 		if (body[5] != IE_ADVERTISEMENT_ANQP)
   5053 			return;
   5054 
   5055 		if (body_len < l_get_le16(body + 6) + 8u)
   5056 			return;
   5057 
   5058 		print_anqp_frame(level + 1, body + 8, l_get_le16(body + 6));
   5059 	} else if (body[0] == 0x0b) {
   5060 		if (body_len < 10)
   5061 			return;
   5062 
   5063 		print_attr(level + 1, "Dialog Token: %u", body[1]);
   5064 		print_attr(level + 1, "Status: %u", l_get_le16(body + 2));
   5065 		print_attr(level + 1, "Delay: %u", l_get_le16(body + 4));
   5066 
   5067 		if (body[6] != IE_TYPE_ADVERTISEMENT_PROTOCOL)
   5068 			return;
   5069 
   5070 		if (body_len < body[7] + 7u)
   5071 			return;
   5072 
   5073 		if (body_len < l_get_le16(body + 8 + body[7]) + 10u)
   5074 			return;
   5075 
   5076 		print_anqp_frame(level + 1, body + 10 + body[7],
   5077 					l_get_le16(body + 8 + body[7]));
   5078 	}
   5079 }
   5080 
   5081 static void print_rm_request(unsigned int level, const uint8_t *body,
   5082 				size_t body_len)
   5083 {
   5084 	if (body_len < 3)
   5085 		return;
   5086 
   5087 	print_attr(level, "Dialog Token: %u", body[0]);
   5088 	print_attr(level, "Repetitions: %u", l_get_le16(body + 1));
   5089 
   5090 	print_ie(level, "IEs", body + 3, body_len - 3);
   5091 }
   5092 
   5093 static void print_rm_report(unsigned int level, const uint8_t *body,
   5094 				size_t body_len)
   5095 {
   5096 	if (body_len < 1)
   5097 		return;
   5098 
   5099 	print_attr(level, "Dialog Token: %u", body[0]);
   5100 
   5101 	print_ie(level, "IEs", body + 1, body_len - 1);
   5102 }
   5103 
   5104 static void print_rm_action_frame(unsigned int level, const uint8_t *body,
   5105 					size_t body_len)
   5106 {
   5107 	const char *category;
   5108 
   5109 	/* 802.11-2016, Table 9-306 */
   5110 	static const char *rm_action_table[] = {
   5111 		[0] = "Radio Measurement Request",
   5112 		[1] = "Radio Measurement Report",
   5113 		[2] = "Link Measurement Request",
   5114 		[3] = "Link Measurement Report",
   5115 		[4] = "Neighbor Report Request",
   5116 		[5] = "Neighbor Report Response",
   5117 	};
   5118 
   5119 	if (body_len < 1)
   5120 		return;
   5121 
   5122 	if (body[0] < L_ARRAY_SIZE(rm_action_table) && rm_action_table[body[0]])
   5123 		category = rm_action_table[body[0]];
   5124 	else
   5125 		category = "Unknown";
   5126 
   5127 	print_attr(level, "Radio Measurement Action: %s (%u)", category, body[0]);
   5128 
   5129 	switch (body[0]) {
   5130 	case 0:
   5131 		print_rm_request(level + 1, body + 1, body_len - 1);
   5132 		break;
   5133 	case 1:
   5134 	case 5:
   5135 		print_rm_report(level + 1, body + 1, body_len - 1);
   5136 		break;
   5137 	}
   5138 }
   5139 
   5140 static void print_ft_request(unsigned int level,
   5141 				const uint8_t *body, size_t body_len)
   5142 {
   5143 	struct ie_tlv_iter iter;
   5144 
   5145 	switch (body[0]) {
   5146 	case 1:
   5147 		if (body_len < 13)
   5148 			return;
   5149 
   5150 		print_attr(level + 1, "FT Request");
   5151 		break;
   5152 	case 2:
   5153 		if (body_len < 15)
   5154 			return;
   5155 
   5156 		print_attr(level + 1, "FT Response");
   5157 		break;
   5158 	case 3:
   5159 		/* FT Confirm/Ack are not yet supported */
   5160 		print_attr(level + 1, "FT Confirm");
   5161 		return;
   5162 	case 4:
   5163 		print_attr(level + 1, "FT ACK");
   5164 		return;
   5165 	}
   5166 
   5167 	print_attr(level + 1, "STA Address: "MAC, MAC_STR(body + 1));
   5168 	print_attr(level + 1, "Target AP Address: "MAC, MAC_STR(body + 7));
   5169 
   5170 	if (body[0] == 2) {
   5171 		print_attr(level + 1, "Status: %u", l_get_le16(body + 13));
   5172 		body += 15;
   5173 		body_len -= 15;
   5174 	} else {
   5175 		body += 13;
   5176 		body_len -= 15;
   5177 	}
   5178 
   5179 	ie_tlv_iter_init(&iter, body, body_len);
   5180 
   5181 	while (ie_tlv_iter_next(&iter)) {
   5182 		uint8_t tag = ie_tlv_iter_get_tag(&iter);
   5183 		size_t len = ie_tlv_iter_get_length(&iter);
   5184 		const void *data = ie_tlv_iter_get_data(&iter);
   5185 
   5186 		switch (tag) {
   5187 		case IE_TYPE_RSN:
   5188 			print_ie_rsn(level + 1, "RSN", data, len);
   5189 			break;
   5190 		case IE_TYPE_MOBILITY_DOMAIN:
   5191 			print_mobility_domain(level + 1, "Mobility Domain",
   5192 						data, len);
   5193 			break;
   5194 		case IE_TYPE_FAST_BSS_TRANSITION:
   5195 			print_fast_bss_transition(level + 1,
   5196 						"Fast BSS Transition",
   5197 						data, len);
   5198 			break;
   5199 		}
   5200 	}
   5201 }
   5202 static void print_action_mgmt_frame(unsigned int level,
   5203 					const struct mmpdu_header *mmpdu,
   5204 					size_t total_len, bool no_ack)
   5205 {
   5206 	const uint8_t *body;
   5207 	size_t body_len;
   5208 	const char *category;
   5209 
   5210 	/* 802.11-2016, Table 9-47 */
   5211 	static const char *category_table[] = {
   5212 		[0] = "Sepctrum Management",
   5213 		[1] = "QoS",
   5214 		[2] = "DLS",
   5215 		[3] = "Block Ack",
   5216 		[4] = "Public",
   5217 		[5] = "Radio Measurement",
   5218 		[6] = "Fast BSS Transition",
   5219 		[7] = "HT",
   5220 		[8] = "SA Query",
   5221 		[9] = "Protected Dual of Public Action",
   5222 		[10] = "WNM",
   5223 		[11] = "Unprotected WNM",
   5224 		[12] = "TDLS",
   5225 		[13] = "Mesh",
   5226 		[14] = "Multihop",
   5227 		[15] = "Self-protected",
   5228 		[16] = "DMG",
   5229 		[18] = "Fast Session Transfer",
   5230 		[19] = "Robust AV Streaming",
   5231 		[20] = "Unprotected DMG",
   5232 		[21] = "VHT",
   5233 		[126] = "Vendor-specific Protected",
   5234 		[127] = "Vendor-specific",
   5235 		[128 ... 255] = "Error",
   5236 	};
   5237 
   5238 	body = mmpdu_body(mmpdu);
   5239 	body_len = total_len - (body - (uint8_t *) mmpdu);
   5240 
   5241 	if (category_table[body[0]])
   5242 		category = category_table[body[0]];
   5243 	else
   5244 		category = "Unknown";
   5245 
   5246 	print_attr(level, "Subtype: Action%s", no_ack ? " No Ack" : "");
   5247 	print_attr(level, "Action Category: %s (%u)", category, body[0]);
   5248 
   5249 	if (body[0] == 4) {
   5250 		print_public_action_frame(level, body + 1, body_len - 1);
   5251 		return;
   5252 	} else if (body[0] == 5) {
   5253 		print_rm_action_frame(level, body + 1, body_len - 1);
   5254 		return;
   5255 	} else if ((body[0] == 126 || body[0] == 127) && body_len >= 5) {
   5256 		const uint8_t *oui = body + 1;
   5257 
   5258 		if (!print_oui(level, oui))
   5259 			return;
   5260 
   5261 		if (!memcmp(oui, wifi_alliance_oui, 3) && oui[3] == 0x09)
   5262 			print_p2p_action_frame(level + 1, body + 5,
   5263 						body_len - 5);
   5264 	} else if (body[0] == 6) {
   5265 		print_ft_request(level, body + 1, body_len - 1);
   5266 		return;
   5267 	}
   5268 
   5269 	print_mpdu_frame_control(level + 1, &mmpdu->fc);
   5270 	print_mmpdu_header(level + 1, mmpdu);
   5271 }
   5272 
   5273 static void print_probe_response(unsigned int level,
   5274 				const struct mmpdu_header *mmpdu, size_t len)
   5275 {
   5276 	const struct mmpdu_probe_response *resp = mmpdu_body(mmpdu);
   5277 
   5278 	print_attr(level, "Subtype: Probe Response");
   5279 	print_ie(level + 1, "Probe Response IEs", resp->ies,
   5280 			(const uint8_t *) mmpdu + len - resp->ies);
   5281 }
   5282 
   5283 static void print_probe_request(unsigned int level,
   5284 				const struct mmpdu_header *mmpdu, size_t len)
   5285 {
   5286 	const struct mmpdu_probe_request *req = mmpdu_body(mmpdu);
   5287 
   5288 	print_attr(level, "Subtype: Probe Request");
   5289 	print_ie(level + 1, "Probe Request IEs", req->ies,
   5290 			(const uint8_t *) mmpdu + len - req->ies);
   5291 }
   5292 
   5293 static void print_beacon(unsigned int level,
   5294 				const struct mmpdu_header *mmpdu, size_t len)
   5295 {
   5296 	const struct mmpdu_beacon *beacon = mmpdu_body(mmpdu);
   5297 
   5298 	print_attr(level, "Subtype: Beacon");
   5299 	print_ie(level + 1, "Beacon IEs", beacon->ies,
   5300 			(const uint8_t *) mmpdu + len - beacon->ies);
   5301 }
   5302 
   5303 static void print_frame_type(unsigned int level, const char *label,
   5304 					const void *data, uint16_t size)
   5305 {
   5306 	uint16_t frame_type = *((uint16_t *) data);
   5307 	uint8_t type = frame_type & 0x000c;
   5308 	uint8_t subtype = (frame_type & 0x00f0) >> 4;
   5309 	const struct mmpdu_header *mpdu = NULL;
   5310 	const char *str;
   5311 
   5312 	print_attr(level, "%s: 0x%04x", label, frame_type);
   5313 
   5314 	switch (type) {
   5315 	case 0x00:
   5316 		str = "Management";
   5317 		mpdu = mpdu_validate(data, size);
   5318 		break;
   5319 	default:
   5320 		str = "Reserved";
   5321 		break;
   5322 	}
   5323 
   5324 	print_attr(level + 1, "Type: %s (%u)", str, type);
   5325 
   5326 	str = NULL;
   5327 
   5328 	switch (subtype) {
   5329 	case 0x00:
   5330 		if (mpdu)
   5331 			str = "Association request";
   5332 		else
   5333 			str = "Association request (invalid MPDU)";
   5334 		break;
   5335 	case 0x01:
   5336 		if (mpdu)
   5337 			print_association_mgmt_frame(level + 1, mpdu, size);
   5338 		else
   5339 			str = "Association response";
   5340 		break;
   5341 	case 0x02:
   5342 		if (mpdu)
   5343 			str = "Reassociation request";
   5344 		else
   5345 			str = "Reassociation request (invalid MPDU)";
   5346 		break;
   5347 	case 0x03:
   5348 		if (mpdu)
   5349 			str = "Reassociation response";
   5350 		else
   5351 			str = "Reassociation response (invalid MPDU)";
   5352 		break;
   5353 	case 0x04:
   5354 		if (mpdu)
   5355 			print_probe_request(level + 1, mpdu, size);
   5356 		else
   5357 			str = "Probe request (invalid MPDU)";
   5358 		break;
   5359 	case 0x05:
   5360 		if (mpdu)
   5361 			print_probe_response(level + 1, mpdu, size);
   5362 		else
   5363 			str = "Probe response (invalid MPDU)";
   5364 		break;
   5365 	case 0x06:
   5366 		str = "Timing Advertisement";
   5367 		break;
   5368 	case 0x08:
   5369 		if (mpdu)
   5370 			print_beacon(level + 1, mpdu, size);
   5371 		else
   5372 			str = "Beacon (invalid MPDU)";
   5373 		break;
   5374 	case 0x09:
   5375 		str = "ATIM";
   5376 		break;
   5377 	case 0x0a:
   5378 		if (mpdu)
   5379 			str = "Disassociation";
   5380 		else
   5381 			str = "Disassociation (invalid MPDU)";
   5382 		break;
   5383 	case 0x0b:
   5384 		if (mpdu)
   5385 			print_authentication_mgmt_frame(level + 1, mpdu, size);
   5386 		else
   5387 			str = "Authentication (invalid MPDU)";
   5388 		break;
   5389 	case 0x0c:
   5390 		if (mpdu)
   5391 			print_deauthentication_mgmt_frame(level + 1, mpdu);
   5392 		else
   5393 			str = "Deauthentication (invalid MPDU)";
   5394 		break;
   5395 	case 0x0d:
   5396 	case 0x0e:
   5397 		if (mpdu)
   5398 			print_action_mgmt_frame(level + 1, mpdu, size,
   5399 						subtype == 0x0e);
   5400 		else if (subtype == 0x0d)
   5401 			str = "Action";
   5402 		else
   5403 			str = "Action No Ack";
   5404 		break;
   5405 	default:
   5406 		str = "Reserved";
   5407 		break;
   5408 	}
   5409 
   5410 	if (str)
   5411 		print_attr(level + 1, "Subtype: %s (%u)", str, subtype);
   5412 }
   5413 
   5414 static void print_frame(unsigned int level, const char *label,
   5415 					const void *data, uint16_t size)
   5416 {
   5417 	print_attr(level, "%s: len %u", label, size);
   5418 	print_frame_type(level + 1, "Frame Type", data, size);
   5419 	print_hexdump(level + 1, data, size);
   5420 }
   5421 
   5422 static void print_cipher_suite(unsigned int level, const char *label,
   5423 					const void *data, uint16_t size)
   5424 {
   5425 	uint32_t cipher = *((uint32_t *) data);
   5426 
   5427 	if (size != 4)
   5428 		return;
   5429 
   5430 	print_ie_cipher_suite(level, label, cipher, rsn_cipher_selectors);
   5431 }
   5432 
   5433 static void print_cipher_suites(unsigned int level, const char *label,
   5434 					const void *data, uint16_t size)
   5435 {
   5436 	print_attr(level, "%s:", label);
   5437 
   5438 	while (size >= 4) {
   5439 		print_cipher_suite(level + 1, NULL, data, 4);
   5440 		data += 4;
   5441 		size -= 4;
   5442 	}
   5443 }
   5444 
   5445 static void print_akm_suites(unsigned int level, const char *label,
   5446 					const void *data, uint16_t size)
   5447 {
   5448 	print_attr(level, "%s:", label);
   5449 
   5450 	while (size >= 4) {
   5451 		uint32_t akm = *((uint32_t *) data);
   5452 
   5453 		print_ie_cipher_suite(level + 1, NULL, akm, rsn_akm_selectors);
   5454 		data += 4;
   5455 		size -= 4;
   5456 	}
   5457 }
   5458 
   5459 static const struct attr_entry iftype_table[] = {
   5460 	{ NL80211_IFTYPE_ADHOC,		"Ad-hoc",	ATTR_FLAG },
   5461 	{ NL80211_IFTYPE_STATION,	"Station",	ATTR_FLAG },
   5462 	{ NL80211_IFTYPE_AP,		"AP",		ATTR_FLAG },
   5463 	{ NL80211_IFTYPE_AP_VLAN,	"AP-VLAN",	ATTR_FLAG },
   5464 	{ NL80211_IFTYPE_WDS,		"WDS",		ATTR_FLAG },
   5465 	{ NL80211_IFTYPE_MONITOR,	"Monitor",	ATTR_FLAG },
   5466 	{ NL80211_IFTYPE_MESH_POINT,	"Mesh-point",	ATTR_FLAG },
   5467 	{ NL80211_IFTYPE_P2P_CLIENT,	"P2P-Client",	ATTR_FLAG },
   5468 	{ NL80211_IFTYPE_P2P_GO,	"P2P-GO",	ATTR_FLAG },
   5469 	{ NL80211_IFTYPE_P2P_DEVICE,	"P2P-Device",	ATTR_FLAG },
   5470 	{ }
   5471 };
   5472 
   5473 static const struct attr_entry bss_param_table[] = {
   5474 	{ NL80211_STA_BSS_PARAM_CTS_PROT,	"CTS protection",  ATTR_FLAG },
   5475 	{ NL80211_STA_BSS_PARAM_SHORT_PREAMBLE,	"Short Preamble",  ATTR_FLAG },
   5476 	{ NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME,"Short Slot Time", ATTR_FLAG },
   5477 	{ NL80211_STA_BSS_PARAM_DTIM_PERIOD,	"DTIM Period",     ATTR_U8   },
   5478 	{ NL80211_STA_BSS_PARAM_BEACON_INTERVAL,"Beacon Interval", ATTR_U16  },
   5479 	{ }
   5480 };
   5481 
   5482 static const struct attr_entry sta_flag_table[] = {
   5483 	{ NL80211_STA_FLAG_AUTHORIZED,		"Authorized",	ATTR_FLAG },
   5484 	{ NL80211_STA_FLAG_SHORT_PREAMBLE,	"ShortPreamble",ATTR_FLAG },
   5485 	{ NL80211_STA_FLAG_WME,			"WME",		ATTR_FLAG },
   5486 	{ NL80211_STA_FLAG_MFP,			"MFP",		ATTR_FLAG },
   5487 	{ NL80211_STA_FLAG_AUTHENTICATED,	"Authenticated",ATTR_FLAG },
   5488 	{ NL80211_STA_FLAG_TDLS_PEER,		"TDLS-Peer",	ATTR_FLAG },
   5489 	{ NL80211_STA_FLAG_ASSOCIATED,		"Associated",	ATTR_FLAG },
   5490 	{ }
   5491 };
   5492 
   5493 static void print_sta_flag_update(unsigned int level, const char *label,
   5494 					const void *data, uint16_t size)
   5495 {
   5496 	const struct nl80211_sta_flag_update *flags = data;
   5497 	unsigned int i;
   5498 
   5499 	print_attr(level, "%s: len %u", label, size);
   5500 
   5501 	print_attr(level + 1, "Mask: 0x%08x", flags->mask);
   5502 	for (i = 0; sta_flag_table[i].str; i++) {
   5503 		if (flags->mask & (1 << sta_flag_table[i].attr))
   5504 			print_attr(level + 2, "%s", sta_flag_table[i].str);
   5505 	}
   5506 
   5507 	print_attr(level + 1, "Set: 0x%08x", flags->set);
   5508 	for (i = 0; sta_flag_table[i].str; i++) {
   5509 		if (flags->set & (1 << sta_flag_table[i].attr))
   5510 			print_attr(level + 2, "%s", sta_flag_table[i].str);
   5511 	}
   5512 }
   5513 
   5514 static const struct attr_entry rate_info_table[] = {
   5515 	{ NL80211_RATE_INFO_BITRATE,	"Bit Rate (Legacy)",	ATTR_U16 },
   5516 	{ NL80211_RATE_INFO_MCS,	"MCS Index",		ATTR_U8 },
   5517 	{ NL80211_RATE_INFO_40_MHZ_WIDTH, "40 Mhz Width",	ATTR_FLAG },
   5518 	{ NL80211_RATE_INFO_SHORT_GI,	"Short GI",		ATTR_FLAG },
   5519 	{ NL80211_RATE_INFO_BITRATE32,	"Bit Rate",		ATTR_U32 },
   5520 	{ NL80211_RATE_INFO_VHT_MCS,	"VHT MCS Index",	ATTR_U8 },
   5521 	{ NL80211_RATE_INFO_VHT_NSS,	"# VHT Streams",	ATTR_U8 },
   5522 	{ NL80211_RATE_INFO_80_MHZ_WIDTH, "80 Mhz Width",	ATTR_FLAG },
   5523 	{ NL80211_RATE_INFO_80P80_MHZ_WIDTH, "80P80 Mhz Width", ATTR_FLAG },
   5524 	{ NL80211_RATE_INFO_160_MHZ_WIDTH, "160 Mhz Width",	ATTR_FLAG },
   5525 	{ }
   5526 };
   5527 
   5528 static const struct attr_entry sta_info_table[] = {
   5529 	{ NL80211_STA_INFO_INACTIVE_TIME,
   5530 					"Inactivity time",	ATTR_U32 },
   5531 	{ NL80211_STA_INFO_RX_BYTES,	"Total RX bytes",	ATTR_U32 },
   5532 	{ NL80211_STA_INFO_TX_BYTES,	"Total TX bytes",	ATTR_U32 },
   5533 	{ NL80211_STA_INFO_RX_BYTES64,	"Total RX bytes",	ATTR_U64 },
   5534 	{ NL80211_STA_INFO_TX_BYTES64,	"Total TX bytes",	ATTR_U64 },
   5535 	{ NL80211_STA_INFO_SIGNAL,	"Signal strength",	ATTR_S8  },
   5536 	{ NL80211_STA_INFO_TX_BITRATE,	"TX bitrate",
   5537 					ATTR_NESTED, { rate_info_table } },
   5538 	{ NL80211_STA_INFO_RX_PACKETS,	"RX packets",		ATTR_U32 },
   5539 	{ NL80211_STA_INFO_TX_PACKETS,	"TX packets",		ATTR_U32 },
   5540 	{ NL80211_STA_INFO_TX_RETRIES,	"TX retries",		ATTR_U32 },
   5541 	{ NL80211_STA_INFO_TX_FAILED,	"TX failed",		ATTR_U32 },
   5542 	{ NL80211_STA_INFO_SIGNAL_AVG,	"Signal strength average",
   5543 								ATTR_S8  },
   5544 	{ NL80211_STA_INFO_LLID,	"Mesh LLID",		ATTR_U16 },
   5545 	{ NL80211_STA_INFO_PLID,	"Mesh PLID",		ATTR_U16 },
   5546 	{ NL80211_STA_INFO_PLINK_STATE, "P-Link state" },
   5547 	{ NL80211_STA_INFO_RX_BITRATE,	"RX bitrate",
   5548 					ATTR_NESTED, { rate_info_table } },
   5549 	{ NL80211_STA_INFO_BSS_PARAM,	"BSS parameters",
   5550 					ATTR_NESTED, { bss_param_table } },
   5551 	{ NL80211_STA_INFO_CONNECTED_TIME,
   5552 					"Connected time",	ATTR_U32 },
   5553 	{ NL80211_STA_INFO_STA_FLAGS,	"Station flags",
   5554 			ATTR_CUSTOM, { .function = print_sta_flag_update } },
   5555 	{ NL80211_STA_INFO_BEACON_LOSS,	"Beacon loss",		ATTR_U32 },
   5556 	{ NL80211_STA_INFO_T_OFFSET,	"Timing offset",	ATTR_S64 },
   5557 	{ NL80211_STA_INFO_LOCAL_PM,	"Local mesh PM",	ATTR_U32 },
   5558 	{ NL80211_STA_INFO_PEER_PM,	"Peer mesh PM",		ATTR_U32 },
   5559 	{ NL80211_STA_INFO_NONPEER_PM,	"Neighbor mesh PM",	ATTR_U32 },
   5560 	{ NL80211_STA_INFO_CHAIN_SIGNAL,
   5561 					"Per-chain signal strength" },
   5562 	{ NL80211_STA_INFO_CHAIN_SIGNAL_AVG,
   5563 					"Per-chain signal strength average" },
   5564 	{ NL80211_STA_INFO_EXPECTED_THROUGHPUT,
   5565 					"Expected Throughput",	ATTR_U32 },
   5566 	{ }
   5567 };
   5568 
   5569 static void print_bss_capability(unsigned int level, const char *label,
   5570 					const void *data, uint16_t size)
   5571 {
   5572 	uint16_t cap = *(uint16_t *) data;
   5573 
   5574 	print_attr(level, "%s: %"PRIu16" (0x%04"PRIx16")", label, cap, cap);
   5575 
   5576 	if (cap & BSS_CAPABILITY_ESS)
   5577 		print_attr(level + 1, "ESS");
   5578 	if (cap & BSS_CAPABILITY_IBSS)
   5579 		print_attr(level + 1, "IBSS");
   5580 	if (cap & BSS_CAPABILITY_PRIVACY)
   5581 		print_attr(level + 1, "Privacy");
   5582 	if (cap & BSS_CAPABILITY_SHORT_PREAMBLE)
   5583 		print_attr(level + 1, "ShortPreamble");
   5584 	if (cap & BSS_CAPABILITY_PBCC)
   5585 		print_attr(level + 1, "PBCC");
   5586 	if (cap & BSS_CAPABILITY_CHANNEL_AGILITY)
   5587 		print_attr(level + 1, "ChannelAgility");
   5588 	if (cap & BSS_CAPABILITY_SPECTRUM_MGMT)
   5589 		print_attr(level + 1, "SpectrumMgmt");
   5590 	if (cap & BSS_CAPABILITY_QOS)
   5591 		print_attr(level + 1, "QoS");
   5592 	if (cap & BSS_CAPABILITY_SHORT_SLOT_TIME)
   5593 		print_attr(level + 1, "ShortSlotTime");
   5594 	if (cap & BSS_CAPABILITY_APSD)
   5595 		print_attr(level + 1, "APSD");
   5596 	if (cap & BSS_CAPABILITY_DSSS_OFDM)
   5597 		print_attr(level + 1, "DSSS-OFDM");
   5598 }
   5599 
   5600 static const struct attr_entry bss_table[] = {
   5601 	{ NL80211_BSS_BSSID,		"BSSID",	ATTR_ADDRESS	},
   5602 	{ NL80211_BSS_FREQUENCY,	"Frequency",	ATTR_U32	},
   5603 	{ NL80211_BSS_TSF,		"TSF",		ATTR_U64	},
   5604 	{ NL80211_BSS_BEACON_INTERVAL,	"Beacon Interval",
   5605 							ATTR_U16	},
   5606 	{ NL80211_BSS_CAPABILITY,	"Capability",  ATTR_CUSTOM,
   5607 				{ .function = print_bss_capability }	},
   5608 	{ NL80211_BSS_INFORMATION_ELEMENTS, "IEs",
   5609 		ATTR_CUSTOM, { .function = print_management_ies }	},
   5610 	{ NL80211_BSS_SIGNAL_MBM,	"Signal mBm",	ATTR_S32	},
   5611 	{ NL80211_BSS_SIGNAL_UNSPEC,	"Signal Unspec",ATTR_U8		},
   5612 	{ NL80211_BSS_STATUS,		"Status",	ATTR_U32	},
   5613 	{ NL80211_BSS_SEEN_MS_AGO,	"Seen ms ago",	ATTR_U32	},
   5614 	{ NL80211_BSS_BEACON_IES, "Beacon IEs",
   5615 		ATTR_CUSTOM, { .function = print_management_ies }	},
   5616 	{ NL80211_BSS_CHAN_WIDTH,	"Chan Width",	ATTR_U32	},
   5617 	{ NL80211_BSS_BEACON_TSF,	"Beacon TSF",	ATTR_U64	},
   5618 	{ NL80211_BSS_PRESP_DATA,	"Probe Response", ATTR_FLAG	},
   5619 	{ NL80211_BSS_PARENT_TSF,	"Parent TSF",	ATTR_U64	},
   5620 	{ NL80211_BSS_PARENT_BSSID,	"Parent BSSID", ATTR_ADDRESS	},
   5621 	{ NL80211_BSS_LAST_SEEN_BOOTTIME, "Timestamp",	ATTR_U64	},
   5622 	{ NL80211_BSS_FREQUENCY_OFFSET,	"Freq Offset",	ATTR_U32	},
   5623 	{ }
   5624 };
   5625 
   5626 static const struct attr_entry frame_types_field_table[] = {
   5627 	{ NL80211_ATTR_FRAME_TYPE,
   5628 			"Frame Type", ATTR_CUSTOM,
   5629 					{ .function = print_frame_type } },
   5630 	{ }
   5631 };
   5632 
   5633 static const struct attr_entry frame_types_table[] = {
   5634 	{ NL80211_IFTYPE_ADHOC, "Ad-hoc", ATTR_NESTED,
   5635 						{ frame_types_field_table } },
   5636 	{ NL80211_IFTYPE_STATION, "Station", ATTR_NESTED,
   5637 						{ frame_types_field_table } },
   5638 	{ NL80211_IFTYPE_AP, "AP", ATTR_NESTED,
   5639 						{ frame_types_field_table } },
   5640 	{ NL80211_IFTYPE_AP_VLAN, "AP-VLAN", ATTR_NESTED,
   5641 						{ frame_types_field_table } },
   5642 	{ NL80211_IFTYPE_WDS, "WDS", ATTR_NESTED,
   5643 						{ frame_types_field_table } },
   5644 	{ NL80211_IFTYPE_MONITOR, "Monitor", ATTR_NESTED,
   5645 						{ frame_types_field_table } },
   5646 	{ NL80211_IFTYPE_MESH_POINT, "Mesh-point", ATTR_NESTED,
   5647 						{ frame_types_field_table } },
   5648 	{ NL80211_IFTYPE_P2P_CLIENT, "P2P-Client", ATTR_NESTED,
   5649 						{ frame_types_field_table } },
   5650 	{ NL80211_IFTYPE_P2P_GO, "P2P-GO", ATTR_NESTED,
   5651 						{ frame_types_field_table } },
   5652 	{ NL80211_IFTYPE_P2P_DEVICE, "P2P-Device", ATTR_NESTED,
   5653 						{ frame_types_field_table } },
   5654 	{ }
   5655 };
   5656 
   5657 static const struct attr_entry survey_info_table[] = {
   5658 	{ NL80211_SURVEY_INFO_FREQUENCY, "Frequency", ATTR_U32 },
   5659 	{ NL80211_SURVEY_INFO_NOISE, "Noise dBm", ATTR_S8 },
   5660 	{ NL80211_SURVEY_INFO_IN_USE, "Channel Currently In Use", ATTR_FLAG },
   5661 	{ NL80211_SURVEY_INFO_TIME, "Survey Time", ATTR_U64 },
   5662 	{ NL80211_SURVEY_INFO_TIME_BUSY, "Busy Time", ATTR_U64 },
   5663 	{ NL80211_SURVEY_INFO_TIME_EXT_BUSY, "Busy Time Extension Channel", ATTR_U64 },
   5664 	{ NL80211_SURVEY_INFO_TIME_RX, "RX Time", ATTR_U64 },
   5665 	{ NL80211_SURVEY_INFO_TIME_TX, "TX Time", ATTR_U64 },
   5666 	{ NL80211_SURVEY_INFO_TIME_SCAN, "Scan Time", ATTR_U64 },
   5667 	{ }
   5668 };
   5669 
   5670 static void print_cqm_event(unsigned int level, const char *label,
   5671 					const void *data, uint16_t size)
   5672 {
   5673 	const uint32_t *event = data;
   5674 
   5675 	switch (*event) {
   5676 	case NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW:
   5677 		print_attr(level, "%s: %s", label, "Low");
   5678 		break;
   5679 	case NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH:
   5680 		print_attr(level, "%s: %s", label, "High");
   5681 		break;
   5682 	case NL80211_CQM_RSSI_BEACON_LOSS_EVENT:
   5683 		print_attr(level, "%s: %s", label, "Beacon Loss (unused)");
   5684 		break;
   5685 	default:
   5686 		print_attr(level, "%s: %s", label, "Unknown");
   5687 		break;
   5688 	}
   5689 }
   5690 
   5691 static void print_cqm_thresholds(unsigned int level, const char *label,
   5692 					const void *data, uint16_t size)
   5693 {
   5694 	const int32_t *thresholds = data;
   5695 	unsigned int i;
   5696 
   5697 	if (size % 4) {
   5698 		printf("malformed packet");
   5699 		return;
   5700 	}
   5701 
   5702 	print_attr(level, "%s:", label);
   5703 
   5704 	for (i = 0; i < size / 4; i++)
   5705 		print_attr(level + 1, "Threshold: %d", thresholds[i]);
   5706 
   5707 }
   5708 static const struct attr_entry cqm_table[] = {
   5709 	{ NL80211_ATTR_CQM_RSSI_THOLD,	"RSSI thresholds",	ATTR_CUSTOM,
   5710 					{ .function = print_cqm_thresholds } },
   5711 	{ NL80211_ATTR_CQM_RSSI_HYST,	"RSSI hysteresis",	ATTR_U32 },
   5712 	{ NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
   5713 					"RSSI threshold event",	ATTR_CUSTOM,
   5714 					{ .function = print_cqm_event } },
   5715 	{ NL80211_ATTR_CQM_PKT_LOSS_EVENT,
   5716 					"Packet loss event",	ATTR_U32 },
   5717 	{ NL80211_ATTR_CQM_TXE_RATE,	"TX error rate",	ATTR_U32 },
   5718 	{ NL80211_ATTR_CQM_TXE_PKTS,	"TX error packets",	ATTR_U32 },
   5719 	{ NL80211_ATTR_CQM_TXE_INTVL,	"TX error interval",	ATTR_U32 },
   5720 	{ NL80211_ATTR_CQM_BEACON_LOSS_EVENT, "Beacon Loss Event", ATTR_FLAG },
   5721 	{ NL80211_ATTR_CQM_RSSI_LEVEL,	"CQM RSSI Level", 	ATTR_S32 },
   5722 	{ }
   5723 };
   5724 
   5725 static const struct attr_entry key_default_type_table[] = {
   5726 	{ NL80211_KEY_DEFAULT_TYPE_UNICAST,	"Unicast",	ATTR_FLAG },
   5727 	{ NL80211_KEY_DEFAULT_TYPE_MULTICAST,	"Multicast",	ATTR_FLAG },
   5728 	{ }
   5729 };
   5730 
   5731 static void print_rekey_kek(unsigned int level, const char *label,
   5732 					const void *data, uint16_t size)
   5733 {
   5734 	print_attr(level, "%s: len %u", label, size);
   5735 
   5736 	if (size != NL80211_KEK_LEN)
   5737 		printf("malformed packet");
   5738 
   5739 	print_hexdump(level + 1, data, size);
   5740 }
   5741 
   5742 static void print_rekey_kck(unsigned int level, const char *label,
   5743 					const void *data, uint16_t size)
   5744 {
   5745 	print_attr(level, "%s: len %u", label, size);
   5746 
   5747 	if (size != NL80211_KCK_LEN)
   5748 		printf("malformed packet");
   5749 
   5750 	print_hexdump(level + 1, data, size);
   5751 }
   5752 
   5753 static void print_rekey_replay_ctr(unsigned int level, const char *label,
   5754 						const void *data, uint16_t size)
   5755 {
   5756 	print_attr(level, "%s: len %u", label, size);
   5757 
   5758 	if (size != NL80211_REPLAY_CTR_LEN)
   5759 		printf("malformed packet");
   5760 
   5761 	print_hexdump(level + 1, data, size);
   5762 }
   5763 
   5764 static const struct attr_entry rekey_table[] = {
   5765 	{ NL80211_REKEY_DATA_KEK, "KEK", ATTR_CUSTOM,
   5766 				{ .function = print_rekey_kek } },
   5767 	{ NL80211_REKEY_DATA_KCK, "KCK", ATTR_CUSTOM,
   5768 				{ .function = print_rekey_kck } },
   5769 	{ NL80211_REKEY_DATA_REPLAY_CTR, "Replay CTR", ATTR_CUSTOM,
   5770 				{ .function = print_rekey_replay_ctr } },
   5771 	{ }
   5772 };
   5773 
   5774 #define NLA_OK(nla,len)         ((len) >= (int) sizeof(struct nlattr) && \
   5775 				(nla)->nla_len >= sizeof(struct nlattr) && \
   5776 				(nla)->nla_len <= (len))
   5777 #define NLA_NEXT(nla,attrlen)	((attrlen) -= NLA_ALIGN((nla)->nla_len), \
   5778 				(struct nlattr*)(((char*)(nla)) + \
   5779 				NLA_ALIGN((nla)->nla_len)))
   5780 
   5781 #define NLA_LENGTH(len)		(NLA_ALIGN(sizeof(struct nlattr)) + (len))
   5782 #define NLA_DATA(nla)		((void*)(((char*)(nla)) + NLA_LENGTH(0)))
   5783 #define NLA_PAYLOAD(nla)	((int)((nla)->nla_len - NLA_LENGTH(0)))
   5784 
   5785 static void print_supported_commands(unsigned int level, const char *label,
   5786 					const void *data, uint16_t size)
   5787 {
   5788 	const struct nlattr *nla;
   5789 
   5790 	print_attr(level, "%s:", label);
   5791 
   5792 	for (nla = data; NLA_OK(nla, size); nla = NLA_NEXT(nla, size)) {
   5793 		uint32_t cmd = *((uint32_t *) NLA_DATA(nla));
   5794 
   5795 		print_attr(level + 1, "%s [%d]",
   5796 					nl80211cmd_to_string(cmd), cmd);
   5797 	}
   5798 }
   5799 
   5800 static const struct attr_entry frequency_attr_table[] = {
   5801 	{ NL80211_FREQUENCY_ATTR_FREQ, "Frequency", ATTR_U32 },
   5802 	{ NL80211_FREQUENCY_ATTR_DISABLED, "Disabled", ATTR_FLAG },
   5803 	{ NL80211_FREQUENCY_ATTR_NO_IR, "No IR", ATTR_FLAG },
   5804 	{ __NL80211_FREQUENCY_ATTR_NO_IBSS, "No IBSS", ATTR_FLAG },
   5805 	{ NL80211_FREQUENCY_ATTR_RADAR, "Radar Detection", ATTR_FLAG },
   5806 	{ NL80211_FREQUENCY_ATTR_MAX_TX_POWER, "Max TX Power", ATTR_U32 },
   5807 	{ NL80211_FREQUENCY_ATTR_DFS_STATE, "DFS State", ATTR_U32 },
   5808 	{ NL80211_FREQUENCY_ATTR_DFS_TIME, "DFS Time", ATTR_U32 },
   5809 	{ NL80211_FREQUENCY_ATTR_NO_HT40_MINUS, "No HT40-", ATTR_FLAG },
   5810 	{ NL80211_FREQUENCY_ATTR_NO_HT40_PLUS, "No HT40+", ATTR_FLAG },
   5811 	{ NL80211_FREQUENCY_ATTR_NO_80MHZ, "No 80 Mhz", ATTR_FLAG },
   5812 	{ NL80211_FREQUENCY_ATTR_NO_160MHZ, "No 160 Mhz", ATTR_FLAG },
   5813 	{ NL80211_FREQUENCY_ATTR_DFS_CAC_TIME, "DFS CAC Time", ATTR_U32 },
   5814 	{ NL80211_FREQUENCY_ATTR_INDOOR_ONLY, "Indoor only", ATTR_FLAG },
   5815 	{ NL80211_FREQUENCY_ATTR_GO_CONCURRENT, "Go Concurrent", ATTR_FLAG },
   5816 	{ NL80211_FREQUENCY_ATTR_NO_20MHZ, "No 20 Mhz", ATTR_FLAG },
   5817 	{ NL80211_FREQUENCY_ATTR_NO_10MHZ, "No 10 Mhz", ATTR_FLAG },
   5818 	{ }
   5819 };
   5820 
   5821 static void print_band_frequencies(unsigned int level, const char *label,
   5822 					const void *data, uint16_t size)
   5823 {
   5824 	const struct nlattr *nla;
   5825 	uint16_t nla_type;
   5826 
   5827 	print_attr(level, "%s: len %u", label, size);
   5828 
   5829 	for (nla = data; NLA_OK(nla, size); nla = NLA_NEXT(nla, size)) {
   5830 		nla_type = nla->nla_type & NLA_TYPE_MASK;
   5831 		print_attr(level + 1, "Frequency %u: len %u", nla_type,
   5832 							NLA_PAYLOAD(nla));
   5833 
   5834 		print_attributes(level + 2, frequency_attr_table,
   5835 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   5836 	}
   5837 }
   5838 
   5839 static const struct attr_entry bitrate_attr_table[] = {
   5840 	{ NL80211_BITRATE_ATTR_RATE, "Bitrate (100kbps multiple)", ATTR_U32 },
   5841 	{ NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE,
   5842 				"2GHZ Short Preamble", ATTR_FLAG },
   5843 	{ }
   5844 };
   5845 static void print_band_rates(unsigned int level, const char *label,
   5846 				const void *data, uint16_t size)
   5847 {
   5848 	const struct nlattr *nla;
   5849 	uint16_t nla_type;
   5850 
   5851 	print_attr(level, "%s: len %u", label, size);
   5852 
   5853 	for (nla = data; NLA_OK(nla, size); nla = NLA_NEXT(nla, size)) {
   5854 		nla_type = nla->nla_type & NLA_TYPE_MASK;
   5855 		print_attr(level + 1, "Bitrate %u: len %u", nla_type,
   5856 							NLA_PAYLOAD(nla));
   5857 
   5858 		print_attributes(level + 2, bitrate_attr_table,
   5859 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   5860 	}
   5861 }
   5862 
   5863 static const struct attr_entry iftype_data_table[] = {
   5864 	{ NL80211_BAND_IFTYPE_ATTR_IFTYPES, "Interface Types", ATTR_NESTED,
   5865 			{ iftype_table } },
   5866 	{ NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC, "HE MAC Capabilities",
   5867 			ATTR_BINARY },
   5868 	{ NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY, "HE PHY Capabilities",
   5869 			ATTR_BINARY },
   5870 	{ NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET, "HE NSS/MCS Set",
   5871 			ATTR_BINARY },
   5872 	{ NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE, "HE PPE Thresholds",
   5873 			ATTR_BINARY },
   5874 	{ NL80211_BAND_IFTYPE_ATTR_MAX, "Highest band HE capability attribute",
   5875 			ATTR_BINARY },
   5876 	{ NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA, "HE 6GHz band capabilities",
   5877 			ATTR_BINARY },
   5878 	{ }
   5879 };
   5880 
   5881 static const struct attr_entry wiphy_bands_table[] = {
   5882 	{ NL80211_BAND_ATTR_FREQS, "Frequencies",
   5883 			ATTR_CUSTOM, { .function = print_band_frequencies } },
   5884 	{ NL80211_BAND_ATTR_RATES, "Rates",
   5885 			ATTR_CUSTOM, { .function = print_band_rates } },
   5886 	{ NL80211_BAND_ATTR_HT_MCS_SET, "HT MCS Set",
   5887 			ATTR_CUSTOM, { .function = print_ht_mcs_set } },
   5888 	{ NL80211_BAND_ATTR_HT_CAPA, "HT Capabilities",
   5889 			ATTR_CUSTOM, { .function = print_ht_capabilities } },
   5890 	{ NL80211_BAND_ATTR_HT_AMPDU_FACTOR, "AMPDU Factor" },
   5891 	{ NL80211_BAND_ATTR_HT_AMPDU_DENSITY, "AMPDU Density" },
   5892 	{ NL80211_BAND_ATTR_VHT_MCS_SET, "VHT MCS Set" },
   5893 	{ NL80211_BAND_ATTR_VHT_CAPA, "VHT Capabilities" },
   5894 	{ NL80211_BAND_ATTR_IFTYPE_DATA, "Interface Type Data",
   5895 			ATTR_ARRAY, { iftype_data_table, ATTR_NESTED }  },
   5896 	{ }
   5897 };
   5898 
   5899 static void print_wiphy_bands(unsigned int level, const char *label,
   5900 					const void *data, uint16_t size)
   5901 {
   5902 	const struct nlattr *nla;
   5903 	uint16_t nla_type;
   5904 
   5905 	print_attr(level, "%s: len %u", label, size);
   5906 
   5907 	for (nla = data; NLA_OK(nla, size); nla = NLA_NEXT(nla, size)) {
   5908 		nla_type = nla->nla_type & NLA_TYPE_MASK;
   5909 		print_attr(level + 1, "Band %u: len %u", nla_type,
   5910 							NLA_PAYLOAD(nla));
   5911 
   5912 		print_attributes(level + 2, wiphy_bands_table,
   5913 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   5914 	}
   5915 }
   5916 
   5917 static void print_reg_rule_flags(unsigned int level, const char *label,
   5918 					const void *data, uint16_t size)
   5919 {
   5920 	uint32_t flag;
   5921 
   5922 	flag = l_get_u32(data);
   5923 
   5924 	if (!flag)
   5925 		return;
   5926 
   5927 	print_attr(level, "%s: len %u", label, size);
   5928 
   5929 	if (flag & NL80211_RRF_NO_OFDM)
   5930 		print_attr(level + 1, "No ODFM");
   5931 	if (flag & NL80211_RRF_NO_CCK)
   5932 		print_attr(level + 1, "No CCK");
   5933 	if (flag & NL80211_RRF_NO_INDOOR)
   5934 		print_attr(level + 1, "No Indoor");
   5935 	if (flag & NL80211_RRF_NO_OUTDOOR)
   5936 		print_attr(level + 1, "No Outdoor");
   5937 	if (flag & NL80211_RRF_DFS)
   5938 		print_attr(level + 1, "DFS");
   5939 	if (flag & NL80211_RRF_PTP_ONLY)
   5940 		print_attr(level + 1, "PTP Only");
   5941 	if (flag & NL80211_RRF_PTMP_ONLY)
   5942 		print_attr(level + 1, "PTMP Only");
   5943 	if (flag & NL80211_RRF_NO_IR)
   5944 		print_attr(level + 1, "No IR");
   5945 	if (flag & __NL80211_RRF_NO_IBSS)
   5946 		print_attr(level + 1, "No IBSS");
   5947 	if (flag & NL80211_RRF_AUTO_BW)
   5948 		print_attr(level + 1, "Auto BW");
   5949 	if (flag & NL80211_RRF_IR_CONCURRENT)
   5950 		print_attr(level + 1, "IR Concurrent");
   5951 	if (flag & NL80211_RRF_NO_HT40MINUS)
   5952 		print_attr(level + 1, "No HT40-");
   5953 	if (flag & NL80211_RRF_NO_HT40PLUS)
   5954 		print_attr(level + 1, "No HT40+");
   5955 	if (flag & NL80211_RRF_NO_80MHZ)
   5956 		print_attr(level + 1, "No 80MHz");
   5957 	if (flag & NL80211_RRF_NO_160MHZ)
   5958 		print_attr(level + 1, "No 160MHz");
   5959 	if (flag & NL80211_RRF_NO_HE)
   5960 		print_attr(level + 1, "No HE");
   5961 }
   5962 
   5963 static void print_u32_div_1000(unsigned int level, const char *label,
   5964 					const void *data, uint16_t size)
   5965 {
   5966 	uint32_t val = l_get_u32(data) / 1000;
   5967 
   5968 	print_attr(level, "%s: %u MHz", label, val);
   5969 }
   5970 
   5971 static const struct attr_entry reg_rule_table[] = {
   5972 	{ NL80211_ATTR_REG_RULE_FLAGS, "Flags", ATTR_CUSTOM,
   5973 					{ .function = print_reg_rule_flags } },
   5974 	{ NL80211_ATTR_FREQ_RANGE_START, "Frequency Start", ATTR_CUSTOM,
   5975 					{ .function = print_u32_div_1000 } },
   5976 	{ NL80211_ATTR_FREQ_RANGE_END, "Frequency End", ATTR_CUSTOM,
   5977 					{ .function = print_u32_div_1000 } },
   5978 	{ NL80211_ATTR_FREQ_RANGE_MAX_BW, "Frequency Range Max BW", ATTR_CUSTOM,
   5979 					{ .function = print_u32_div_1000 } },
   5980 	{ NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, "Max Antenna Gain", ATTR_U32 },
   5981 	{ NL80211_ATTR_POWER_RULE_MAX_EIRP, "Max EIRP", ATTR_U32 },
   5982 	{ NL80211_ATTR_DFS_CAC_TIME, "DFS CAC Time", ATTR_U32 },
   5983 	{ }
   5984 };
   5985 
   5986 static void print_reg_rules(unsigned int level, const char *label,
   5987 					const void *data, uint16_t size)
   5988 {
   5989 	const struct nlattr *nla;
   5990 	uint16_t nla_type;
   5991 
   5992 	print_attr(level, "%s: len %u", label, size);
   5993 
   5994 	for (nla = data; NLA_OK(nla, size); nla = NLA_NEXT(nla, size)) {
   5995 		nla_type = nla->nla_type & NLA_TYPE_MASK;
   5996 		print_attr(level + 1, "Rule %u: len %u", nla_type,
   5997 							NLA_PAYLOAD(nla));
   5998 
   5999 		print_attributes(level + 2, reg_rule_table,
   6000 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   6001 	}
   6002 }
   6003 
   6004 static void print_eapol_key(unsigned int level, const void *data, uint32_t size)
   6005 {
   6006 	const struct eapol_key *ek = (struct eapol_key *)data;
   6007 	size_t mic_lengths[] = { 16, 24, 32 };
   6008 	size_t mic_len = 0;
   6009 	int i;
   6010 
   6011 	/*
   6012 	 * The MIC length is not encoded anywhere in the frame, and should be
   6013 	 * determined by AKM. To even further complicate things, some non
   6014 	 * 802.11 AKMs define their own MIC lengths. But since the only valid
   6015 	 * lengths are 16, 24 and 32 its trivial to try each until we find a
   6016 	 * matching length.
   6017 	 */
   6018 	for (i = 0; i < 3; i++) {
   6019 		size_t mlen = mic_lengths[i];
   6020 
   6021 		if (size < EAPOL_FRAME_LEN(mlen))
   6022 			break;
   6023 
   6024 		if (size == EAPOL_FRAME_LEN(mlen) +
   6025 				EAPOL_KEY_DATA_LEN(ek, mlen)) {
   6026 			mic_len = mlen;
   6027 			break;
   6028 		}
   6029 	}
   6030 
   6031 	/* could not determine MIC length, malformed packet? */
   6032 	if (!mic_len)
   6033 		return;
   6034 
   6035 	ek = eapol_key_validate(data, size, mic_len);
   6036 	if (!ek)
   6037 		return;
   6038 
   6039 	print_attr(level, "Descriptor Type: %u", ek->descriptor_type);
   6040 	print_attr(level, "Key MIC: %s", ek->key_mic ? "true" : "false");
   6041 	print_attr(level, "Secure: %s", ek->secure ? "true" : "false");
   6042 	print_attr(level, "Error: %s", ek->error ? "true" : "false");
   6043 	print_attr(level, "Request: %s", ek->request ? "true" : "false");
   6044 	print_attr(level, "Encrypted Key Data: %s",
   6045 				ek->encrypted_key_data ? "true" : "false");
   6046 	print_attr(level, "SMK Message: %s",
   6047 				ek->smk_message ? "true" : "false");
   6048 	print_attr(level, "Key Descriptor Version: %d (%02x)",
   6049 						ek->key_descriptor_version,
   6050 						ek->key_descriptor_version);
   6051 	print_attr(level, "Key Type: %s", ek->key_type ? "true" : "false");
   6052 
   6053 	if (ek->descriptor_type == EAPOL_DESCRIPTOR_TYPE_WPA)
   6054 		print_attr(level, "Key Id: %u", ek->wpa_key_id);
   6055 
   6056 	print_attr(level, "Install: %s", ek->install ? "true" : "false");
   6057 	print_attr(level, "Key ACK: %s", ek->key_ack ? "true" : "false");
   6058 	print_attr(level, "Key Length: %d", L_BE16_TO_CPU(ek->key_length));
   6059 	print_attr(level, "Key Replay Counter: %" PRIu64,
   6060 					L_BE64_TO_CPU(ek->key_replay_counter));
   6061 	print_attr(level, "Key NONCE");
   6062 	print_hexdump(level + 1, ek->key_nonce, 32);
   6063 	print_attr(level, "Key IV");
   6064 	print_hexdump(level + 1, ek->eapol_key_iv, 16);
   6065 	print_attr(level, "Key RSC ");
   6066 	print_hexdump(level + 1, ek->key_rsc, 8);
   6067 
   6068 	print_attr(level, "Key MIC Data");
   6069 
   6070 	print_hexdump(level + 1, EAPOL_KEY_MIC(ek), mic_len);
   6071 
   6072 	if (ek->encrypted_key_data) {
   6073 		print_attr(level, "Key Data: len %d",
   6074 					EAPOL_KEY_DATA_LEN(ek, mic_len));
   6075 		print_hexdump(level + 1, EAPOL_KEY_DATA(ek, mic_len),
   6076 				EAPOL_KEY_DATA_LEN(ek, mic_len));
   6077 		return;
   6078 	}
   6079 
   6080 	print_ie(level, "Key Data", EAPOL_KEY_DATA(ek, mic_len),
   6081 			EAPOL_KEY_DATA_LEN(ek, mic_len));
   6082 }
   6083 
   6084 static void print_eap_wsc(unsigned int level,
   6085 				const uint8_t *eap_wsc, uint32_t size)
   6086 {
   6087 	const char *str;
   6088 
   6089 	if (size < 2)
   6090 		return;
   6091 
   6092 	switch (eap_wsc[0]) {
   6093 	case 0x01:
   6094 		str = "WSC-Start";
   6095 		break;
   6096 	case 0x02:
   6097 		str = "WSC-Ack";
   6098 		break;
   6099 	case 0x03:
   6100 		str = "WSC-Nack";
   6101 		break;
   6102 	case 0x04:
   6103 		str = "WSC-Msg";
   6104 		break;
   6105 	case 0x05:
   6106 		str = "WSC-Done";
   6107 		break;
   6108 	case 0x06:
   6109 		str = "WSC-Frag-Ack";
   6110 		break;
   6111 	default:
   6112 		str = "Reserved";
   6113 		break;
   6114 	}
   6115 
   6116 	print_attr(level, "Op-Code: %u (%s)", eap_wsc[0], str);
   6117 	print_attr(level, "Flags: %02x", eap_wsc[1]);
   6118 
   6119 	if (eap_wsc[1] == 0)
   6120 		print_wsc_attributes(level + 1, "EAP-WSC Payload",
   6121 						eap_wsc + 2, size - 2);
   6122 }
   6123 
   6124 static void print_eap(unsigned int level, const void *data, uint32_t size)
   6125 {
   6126 	static const uint8_t wfa_smi[3] = { 0x00, 0x37, 0x2a };
   6127 	const uint8_t *eap = data;
   6128 	const char *str;
   6129 
   6130 	if (size < 5)
   6131 		return;
   6132 
   6133 	switch (eap[0]) {
   6134 	case 0x01:
   6135 		str = "Request";
   6136 		break;
   6137 	case 0x02:
   6138 		str = "Response";
   6139 		break;
   6140 	case 0x03:
   6141 		str = "Success";
   6142 		break;
   6143 	case 0x04:
   6144 		str = "Failure";
   6145 		break;
   6146 	default:
   6147 		str = "Reserved";
   6148 		break;
   6149 	}
   6150 
   6151 	print_attr(level, "Code: %u (%s)", eap[0], str);
   6152 	print_attr(level, "Identifier: %u", eap[1]);
   6153 	print_attr(level, "Length: %u", l_get_be16(eap + 2));
   6154 
   6155 	switch (eap[4]) {
   6156 	case 1:
   6157 		str = "Identity";
   6158 		break;
   6159 	case 2:
   6160 		str = "Notification";
   6161 		break;
   6162 	case 3:
   6163 		str = "Nak";
   6164 		break;
   6165 	case 4:
   6166 		str = "MD5 Challenge";
   6167 		break;
   6168 	case 13:
   6169 		str = "TLS EAP";
   6170 		break;
   6171 	case 21:
   6172 		str = "TTLS";
   6173 		break;
   6174 	case 254:
   6175 		str = "Expanded";
   6176 		break;
   6177 	default:
   6178 		str = "Reserved";
   6179 		break;
   6180 	}
   6181 
   6182 	print_attr(level, "Type: %u (%s)", eap[4], str);
   6183 
   6184 	if (eap[4] == 254) {
   6185 		if (size < 12)
   6186 			return;
   6187 
   6188 		if (memcmp(eap + 5, wfa_smi, 3))
   6189 			return;
   6190 
   6191 		if (l_get_be32(eap + 8) == 1)
   6192 			print_eap_wsc(level, eap + 12, size - 12);
   6193 	}
   6194 }
   6195 
   6196 static void print_eapol(unsigned int level, const char *label,
   6197 					const void *data, uint16_t size)
   6198 {
   6199 	const struct eapol_header *eh;
   6200 	const char *str;
   6201 
   6202 	print_attr(level, "%s: len %u", label, size);
   6203 
   6204 	if (size < 4)
   6205 		return;
   6206 
   6207 	eh = data;
   6208 
   6209 	switch (eh->protocol_version) {
   6210 	case 0x01:
   6211 		str = "802.1X-2001";
   6212 		break;
   6213 	case 0x02:
   6214 		str = "802.1X-2004";
   6215 		break;
   6216 	case 0x03:
   6217 		str = "802.1X-2010";
   6218 		break;
   6219 	default:
   6220 		str = "Reserved";
   6221 		break;
   6222 	}
   6223 
   6224 	print_attr(level + 1, "Protocol Version: %u (%s)",
   6225 					eh->protocol_version, str);
   6226 
   6227 	switch (eh->packet_type) {
   6228 	case 0x00:
   6229 		str = "EAP";
   6230 		break;
   6231 	case 0x01:
   6232 		str = "Start";
   6233 		break;
   6234 	case 0x02:
   6235 		str = "Logoff";
   6236 		break;
   6237 	case 0x03:
   6238 		str = "Key";
   6239 		break;
   6240 	case 0x04:
   6241 		str = "Encapsulated-ASF-Alert";
   6242 		break;
   6243 	case 0x05:
   6244 		str = "MKA";
   6245 		break;
   6246 	case 0x06:
   6247 		str = "Announcement (Generic)";
   6248 		break;
   6249 	case 0x07:
   6250 		str = "Announcement (Specific)";
   6251 		break;
   6252 	case 0x08:
   6253 		str = "Announcement-Req";
   6254 		break;
   6255 	default:
   6256 		str = "Reserved";
   6257 		break;
   6258 	}
   6259 
   6260 	print_attr(level + 1, "Type: %u (%s)", eh->packet_type, str);
   6261 	print_attr(level + 1, "Length: %d", L_BE16_TO_CPU(eh->packet_len));
   6262 
   6263 	switch (eh->packet_type) {
   6264 	case 0x03:
   6265 		print_eapol_key(level + 1, data, size);
   6266 		break;
   6267 	case 0x00:
   6268 		print_eap(level + 1, data + 4, size - 4);
   6269 	}
   6270 
   6271 	print_hexdump(level + 1, data, size);
   6272 }
   6273 
   6274 #define EXT_ENTRY(a) #a
   6275 
   6276 static const char *ext_feature_map[] = {
   6277 	EXT_ENTRY(NL80211_EXT_FEATURE_VHT_IBSS),
   6278 	EXT_ENTRY(NL80211_EXT_FEATURE_RRM),
   6279 	EXT_ENTRY(NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER),
   6280 	EXT_ENTRY(NL80211_EXT_FEATURE_SCAN_START_TIME),
   6281 	EXT_ENTRY(NL80211_EXT_FEATURE_BSS_PARENT_TSF),
   6282 	EXT_ENTRY(NL80211_EXT_FEATURE_SET_SCAN_DWELL),
   6283 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_RATE_LEGACY),
   6284 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_RATE_HT),
   6285 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_RATE_VHT),
   6286 	EXT_ENTRY(NL80211_EXT_FEATURE_FILS_STA),
   6287 	EXT_ENTRY(NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA),
   6288 	EXT_ENTRY(NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED),
   6289 	EXT_ENTRY(NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI),
   6290 	EXT_ENTRY(NL80211_EXT_FEATURE_CQM_RSSI_LIST),
   6291 	EXT_ENTRY(NL80211_EXT_FEATURE_FILS_SK_OFFLOAD),
   6292 	EXT_ENTRY(NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK),
   6293 	EXT_ENTRY(NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X),
   6294 	EXT_ENTRY(NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME),
   6295 	EXT_ENTRY(NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP),
   6296 	EXT_ENTRY(NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE),
   6297 	EXT_ENTRY(NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION),
   6298 	EXT_ENTRY(NL80211_EXT_FEATURE_MFP_OPTIONAL),
   6299 	EXT_ENTRY(NL80211_EXT_FEATURE_LOW_SPAN_SCAN),
   6300 	EXT_ENTRY(NL80211_EXT_FEATURE_LOW_POWER_SCAN),
   6301 	EXT_ENTRY(NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN),
   6302 	EXT_ENTRY(NL80211_EXT_FEATURE_DFS_OFFLOAD),
   6303 	EXT_ENTRY(NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211),
   6304 	EXT_ENTRY(NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT),
   6305 	EXT_ENTRY(NL80211_EXT_FEATURE_TXQS),
   6306 	EXT_ENTRY(NL80211_EXT_FEATURE_SCAN_RANDOM_SN),
   6307 	EXT_ENTRY(NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT),
   6308 	EXT_ENTRY(NL80211_EXT_FEATURE_CAN_REPLACE_PTK0),
   6309 	EXT_ENTRY(NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER),
   6310 	EXT_ENTRY(NL80211_EXT_FEATURE_AIRTIME_FAIRNESS),
   6311 	EXT_ENTRY(NL80211_EXT_FEATURE_AP_PMKSA_CACHING),
   6312 	EXT_ENTRY(NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD),
   6313 	EXT_ENTRY(NL80211_EXT_FEATURE_EXT_KEY_ID),
   6314 	EXT_ENTRY(NL80211_EXT_FEATURE_STA_TX_PWR),
   6315 	EXT_ENTRY(NL80211_EXT_FEATURE_SAE_OFFLOAD),
   6316 	EXT_ENTRY(NL80211_EXT_FEATURE_VLAN_OFFLOAD),
   6317 	EXT_ENTRY(NL80211_EXT_FEATURE_AQL),
   6318 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_PROTECTION),
   6319 	EXT_ENTRY(NL80211_EXT_FEATURE_CONTROL_PORT_NO_PREAUTH),
   6320 	EXT_ENTRY(NL80211_EXT_FEATURE_PROTECTED_TWT),
   6321 	EXT_ENTRY(NL80211_EXT_FEATURE_DEL_IBSS_STA),
   6322 	EXT_ENTRY(NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS),
   6323 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT),
   6324 	EXT_ENTRY(NL80211_EXT_FEATURE_SCAN_FREQ_KHZ),
   6325 	EXT_ENTRY(NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211_TX_STATUS),
   6326 	EXT_ENTRY(NL80211_EXT_FEATURE_OPERATING_CHANNEL_VALIDATION),
   6327 	EXT_ENTRY(NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK),
   6328 	EXT_ENTRY(NL80211_EXT_FEATURE_SAE_OFFLOAD_AP),
   6329 	EXT_ENTRY(NL80211_EXT_FEATURE_FILS_DISCOVERY),
   6330 	EXT_ENTRY(NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP),
   6331 	EXT_ENTRY(NL80211_EXT_FEATURE_BEACON_RATE_HE),
   6332 };
   6333 
   6334 static void print_ext_features(unsigned int level, const char *label,
   6335 					const void *data, uint16_t size)
   6336 {
   6337 	int i;
   6338 	uint16_t bits;
   6339 
   6340 	if (L_ARRAY_SIZE(ext_feature_map) < size * 8)
   6341 		bits = L_ARRAY_SIZE(ext_feature_map);
   6342 	else
   6343 		bits = size * 8;
   6344 
   6345 	print_attr(level, "Extended Features:");
   6346 
   6347 	for (i = 0; i < bits; i++) {
   6348 		if (test_bit(data, i))
   6349 			print_attr(level + 1, "%s", ext_feature_map[i]);
   6350 	}
   6351 }
   6352 
   6353 /*
   6354  * Control Port sends a EAPoL frame inside ATTR_FRAME and not a management
   6355  * frame.  So a separate table with all the possible attributes is provided
   6356  */
   6357 static const struct attr_entry control_port_attr_table[] = {
   6358 	{ NL80211_ATTR_CONTROL_PORT_ETHERTYPE,
   6359 			"Control Port Ethertype", ATTR_FLAG_OR_U16 },
   6360 	{ NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT,
   6361 			"Control Port No Encrypt", ATTR_FLAG },
   6362 	{ NL80211_ATTR_MAC,
   6363 			"MAC Address", ATTR_ADDRESS },
   6364 	{ NL80211_ATTR_FRAME,
   6365 			"Frame", ATTR_CUSTOM, { .function = print_eapol } },
   6366 	{ NL80211_ATTR_WDEV,
   6367 			"Wireless Device", ATTR_U64 },
   6368 	{ NL80211_ATTR_IFINDEX,
   6369 			"Interface Index", ATTR_U32 },
   6370 	{ NL80211_ATTR_WIPHY,
   6371 			"Wiphy", ATTR_U32 },
   6372 	{ }
   6373 };
   6374 
   6375 static const struct attr_entry scan_flag_table[] = {
   6376 	{ NL80211_SCAN_FLAG_LOW_PRIORITY,	"LowPriority",	ATTR_FLAG },
   6377 	{ NL80211_SCAN_FLAG_FLUSH,		"Flush",	ATTR_FLAG },
   6378 	{ NL80211_SCAN_FLAG_AP,			"AP",		ATTR_FLAG },
   6379 	{ NL80211_SCAN_FLAG_RANDOM_ADDR,	"RandomMAC",	ATTR_FLAG },
   6380 	{ NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
   6381 						"FILSMaxTime",	ATTR_FLAG },
   6382 	{ NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
   6383 						"BcastPrespOK", ATTR_FLAG },
   6384 	{ NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
   6385 						"OCEHighTX",	ATTR_FLAG },
   6386 	{ NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
   6387 						"OCEDeferSupp",	ATTR_FLAG },
   6388 	{ NL80211_SCAN_FLAG_LOW_SPAN,		"LowSpan",	ATTR_FLAG },
   6389 	{ NL80211_SCAN_FLAG_LOW_POWER,		"LowPower",	ATTR_FLAG },
   6390 	{ NL80211_SCAN_FLAG_HIGH_ACCURACY,	"HighAccuracy", ATTR_FLAG },
   6391 	{ NL80211_SCAN_FLAG_RANDOM_SN,		"RandomSN",	ATTR_FLAG },
   6392 	{ NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,	"MinPreqCont",	ATTR_FLAG },
   6393 	{ NL80211_SCAN_FLAG_FREQ_KHZ,		"FreqKHz",	ATTR_FLAG },
   6394 	{ NL80211_SCAN_FLAG_COLOCATED_6GHZ,	"Colocated6G",	ATTR_FLAG },
   6395 	{ }
   6396 };
   6397 
   6398 static void print_scan_flags(unsigned int level, const char *label,
   6399 					const void *data, uint16_t size)
   6400 {
   6401 	const uint32_t *flags = data;
   6402 	unsigned int i;
   6403 
   6404 	print_attr(level, "%s: Mask: 0x%08x len %u", label, *flags, size);
   6405 
   6406 	for (i = 0; scan_flag_table[i].str; i++) {
   6407 		if (*flags & scan_flag_table[i].attr)
   6408 			print_attr(level + 2, "%s", scan_flag_table[i].str);
   6409 	}
   6410 }
   6411 
   6412 static void print_key_type(unsigned int level, const char *label,
   6413 					const void *data, uint16_t size)
   6414 {
   6415 	const uint8_t *ptr = data;
   6416 	const char *str;
   6417 
   6418 	switch (*ptr) {
   6419 	case NL80211_KEYTYPE_GROUP:
   6420 		str = "Group";
   6421 		break;
   6422 	case NL80211_KEYTYPE_PAIRWISE:
   6423 		str = "Pairwise";
   6424 		break;
   6425 	case NL80211_KEYTYPE_PEERKEY:
   6426 		str = "Peerkey";
   6427 		break;
   6428 	default:
   6429 		str = "Unknown";
   6430 	}
   6431 
   6432 	print_attr(level, "%s: %s", label, str);
   6433 }
   6434 
   6435 static void print_key_mode(unsigned int level, const char *label,
   6436 					const void *data, uint16_t size)
   6437 {
   6438 	const uint8_t *ptr = data;
   6439 	const char *str;
   6440 
   6441 	switch (*ptr) {
   6442 	case NL80211_KEY_RX_TX:
   6443 		str = "RX/TX";
   6444 		break;
   6445 	case NL80211_KEY_NO_TX:
   6446 		str = "RX Only";
   6447 		break;
   6448 	case NL80211_KEY_SET_TX:
   6449 		str = "Set TX";
   6450 		break;
   6451 	default:
   6452 		str = "Unknown";
   6453 	}
   6454 
   6455 	print_attr(level, "%s: %s", label, str);
   6456 }
   6457 
   6458 static const struct attr_entry default_key_type_table[] = {
   6459 	{ NL80211_KEY_DEFAULT_TYPE_UNICAST, "Unicast", ATTR_FLAG },
   6460 	{ NL80211_KEY_DEFAULT_TYPE_MULTICAST, "Multicast", ATTR_FLAG },
   6461 	{ }
   6462 };
   6463 
   6464 static const struct attr_entry key_table[] = {
   6465 	{ NL80211_KEY_DATA,		"Key Data",		ATTR_BINARY },
   6466 	{ NL80211_KEY_IDX,		"Key Index",		ATTR_U8 },
   6467 	{ NL80211_KEY_CIPHER,		"Key Cipher",		ATTR_CUSTOM,
   6468 					{ .function = print_cipher_suite } },
   6469 	{ NL80211_KEY_SEQ,		"Key Sequence",		ATTR_BINARY },
   6470 	{ NL80211_KEY_DEFAULT,		"Default",		ATTR_FLAG },
   6471 	{ NL80211_KEY_DEFAULT_MGMT,	"Default Management",	ATTR_FLAG },
   6472 	{ NL80211_KEY_TYPE,		"Key Type",		ATTR_CUSTOM,
   6473 					{ .function = print_key_type} },
   6474 	{ NL80211_KEY_DEFAULT_TYPES,	"Default Key Types",	ATTR_NESTED,
   6475 					{ default_key_type_table } },
   6476 	{ NL80211_KEY_MODE,		"Key Mode",		ATTR_CUSTOM,
   6477 					{ .function = print_key_mode } },
   6478 	{ NL80211_KEY_DEFAULT_BEACON,	"Default Beacon",	ATTR_FLAG },
   6479 	{ }
   6480 };
   6481 
   6482 static const struct attr_entry attr_table[] = {
   6483 	{ NL80211_ATTR_WIPHY,
   6484 			"Wiphy", ATTR_U32 },
   6485 	{ NL80211_ATTR_WIPHY_NAME,
   6486 			"Wiphy Name", ATTR_STRING },
   6487 	{ NL80211_ATTR_IFINDEX,
   6488 			"Interface Index", ATTR_U32 },
   6489 	{ NL80211_ATTR_IFNAME,
   6490 			"Interface Name", ATTR_STRING },
   6491 	{ NL80211_ATTR_IFTYPE,
   6492 			"Interface Type", ATTR_U32 },
   6493 	{ NL80211_ATTR_MAC,
   6494 			"MAC Address", ATTR_ADDRESS },
   6495 	{ NL80211_ATTR_KEY_DATA,
   6496 			"Key Data", ATTR_BINARY },
   6497 	{ NL80211_ATTR_KEY_IDX,
   6498 			"Key Index", ATTR_U8 },
   6499 	{ NL80211_ATTR_KEY_CIPHER,
   6500 			"Key Cipher", ATTR_CUSTOM,
   6501 					{ .function = print_cipher_suite } },
   6502 	{ NL80211_ATTR_KEY_SEQ,
   6503 			"Key Sequence", ATTR_BINARY },
   6504 	{ NL80211_ATTR_KEY_DEFAULT,
   6505 			"Key Default", ATTR_FLAG },
   6506 	{ NL80211_ATTR_BEACON_INTERVAL,
   6507 			"Beacon Interval", ATTR_U32 },
   6508 	{ NL80211_ATTR_DTIM_PERIOD,
   6509 			"DTIM Period", ATTR_U32 },
   6510 	{ NL80211_ATTR_BEACON_HEAD,
   6511 			"Beacon Head",  ATTR_CUSTOM, { .function = print_frame } },
   6512 	{ NL80211_ATTR_BEACON_TAIL,
   6513 			"Beacon Tail", ATTR_CUSTOM,
   6514 				{ .function = print_management_ies } },
   6515 	{ NL80211_ATTR_STA_AID,
   6516 			"Station AID", ATTR_U16 },
   6517 	{ NL80211_ATTR_STA_FLAGS,
   6518 			"Station Flags", ATTR_NESTED, { sta_flag_table } },
   6519 	{ NL80211_ATTR_STA_LISTEN_INTERVAL,
   6520 			"Station Listen Interval", ATTR_U16 },
   6521 	{ NL80211_ATTR_STA_SUPPORTED_RATES,
   6522 			"Station Supported Rates", ATTR_BINARY },
   6523 	{ NL80211_ATTR_STA_VLAN,
   6524 			"Station VLAN", ATTR_U32 },
   6525 	{ NL80211_ATTR_STA_INFO,
   6526 			"Station Info", ATTR_NESTED, { sta_info_table } },
   6527 	{ NL80211_ATTR_WIPHY_BANDS,
   6528 			"Wiphy Bands", ATTR_CUSTOM,
   6529 				{ .function = print_wiphy_bands } },
   6530 	{ NL80211_ATTR_MNTR_FLAGS,
   6531 			"MNTR Flags" },
   6532 	{ NL80211_ATTR_MESH_ID,
   6533 			"Mesh ID", ATTR_BINARY },
   6534 	{ NL80211_ATTR_STA_PLINK_ACTION,
   6535 			"Station P-Link Action", ATTR_U8 },
   6536 	{ NL80211_ATTR_MPATH_NEXT_HOP,
   6537 			"Mesh Path Next Hop", ATTR_U32 },
   6538 	{ NL80211_ATTR_MPATH_INFO,
   6539 			"Mesh Path Info" },
   6540 	{ NL80211_ATTR_BSS_CTS_PROT,
   6541 			"BSS CTS Protection", ATTR_U8 },
   6542 	{ NL80211_ATTR_BSS_SHORT_PREAMBLE,
   6543 			"BSS Short Preamble", ATTR_U8 },
   6544 	{ NL80211_ATTR_BSS_SHORT_SLOT_TIME,
   6545 			"BSS Short Slot Time", ATTR_U8 },
   6546 	{ NL80211_ATTR_HT_CAPABILITY, "HT Capability",
   6547 			ATTR_CUSTOM, { .function = print_ie_ht_capabilities } },
   6548 	{ NL80211_ATTR_SUPPORTED_IFTYPES,
   6549 			"Supported Interface Types", ATTR_NESTED,
   6550 							{ iftype_table } },
   6551 	{ NL80211_ATTR_REG_ALPHA2,
   6552 			"Regulatory Alpha2", ATTR_STRING },
   6553 	{ NL80211_ATTR_REG_RULES,
   6554 			"Regulatory Rules", ATTR_CUSTOM,
   6555 				{ .function = print_reg_rules } },
   6556 	{ NL80211_ATTR_MESH_CONFIG,
   6557 			"Mesh Configuration" },
   6558 	{ NL80211_ATTR_BSS_BASIC_RATES,
   6559 			"BSS Basic Rates", ATTR_BINARY },
   6560 	{ NL80211_ATTR_WIPHY_TXQ_PARAMS,
   6561 			"Wiphy TXQ Parameters" },
   6562 	{ NL80211_ATTR_WIPHY_FREQ,
   6563 			"Wiphy Frequency", ATTR_U32 },
   6564 	{ NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   6565 			"Wiphy Channel Type", ATTR_U32 },
   6566 	{ NL80211_ATTR_KEY_DEFAULT_MGMT,
   6567 			"Key Default Management", ATTR_FLAG },
   6568 	{ NL80211_ATTR_MGMT_SUBTYPE,
   6569 			"Management Subtype", ATTR_U8 },
   6570 	{ NL80211_ATTR_IE,
   6571 			"Information Elements", ATTR_CUSTOM,
   6572 					{ .function = print_management_ies } },
   6573 	{ NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
   6574 			"Max Number Scan SSIDs", ATTR_U8 },
   6575 	{ NL80211_ATTR_SCAN_FREQUENCIES,
   6576 			"Scan Frequencies", ATTR_ARRAY,
   6577 						{ .array_type = ATTR_U32 } },
   6578 	{ NL80211_ATTR_SCAN_SSIDS,
   6579 			"Scan SSIDs", ATTR_ARRAY,
   6580 					{ .array_type = ATTR_BINARY } },
   6581 	{ NL80211_ATTR_GENERATION,
   6582 			"Generation", ATTR_U32 },
   6583 	{ NL80211_ATTR_BSS,
   6584 			"BSS", ATTR_NESTED, { bss_table } },
   6585 	{ NL80211_ATTR_REG_INITIATOR,
   6586 			"Regulatory Initiator" },
   6587 	{ NL80211_ATTR_REG_TYPE,
   6588 			"Regulatory Type" },
   6589 	{ NL80211_ATTR_SUPPORTED_COMMANDS,
   6590 			"Supported Commands", ATTR_CUSTOM,
   6591 				{ .function = print_supported_commands } },
   6592 	{ NL80211_ATTR_FRAME,
   6593 			"Frame", ATTR_CUSTOM, { .function = print_frame } },
   6594 	{ NL80211_ATTR_SSID,
   6595 			"SSID", ATTR_BINARY },
   6596 	{ NL80211_ATTR_AUTH_TYPE,
   6597 			"Auth Type", ATTR_U32 },
   6598 	{ NL80211_ATTR_REASON_CODE,
   6599 			"Reason Code", ATTR_CUSTOM,
   6600 				{ .function = print_reason_code } },
   6601 	{ NL80211_ATTR_KEY_TYPE,
   6602 			"Key Type", ATTR_U32 },
   6603 	{ NL80211_ATTR_MAX_SCAN_IE_LEN,
   6604 			"Max Scan IE Length", ATTR_U16 },
   6605 	{ NL80211_ATTR_CIPHER_SUITES,
   6606 			"Cipher Suites", ATTR_CUSTOM,
   6607 					{ .function = print_cipher_suites } },
   6608 	{ NL80211_ATTR_FREQ_BEFORE,
   6609 			"Frequency Before" },
   6610 	{ NL80211_ATTR_FREQ_AFTER,
   6611 			"Frequency After" },
   6612 	{ NL80211_ATTR_FREQ_FIXED,
   6613 			"Frequency Fixed", ATTR_FLAG },
   6614 	{ NL80211_ATTR_WIPHY_RETRY_SHORT,
   6615 			"Wiphy Retry Short", ATTR_U8 },
   6616 	{ NL80211_ATTR_WIPHY_RETRY_LONG,
   6617 			"Wiphy Retry Long", ATTR_U8 },
   6618 	{ NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
   6619 			"Wiphy Frag Threshold", ATTR_U32 },
   6620 	{ NL80211_ATTR_WIPHY_RTS_THRESHOLD,
   6621 			"Wiphy RTS Threshold", ATTR_U32 },
   6622 	{ NL80211_ATTR_TIMED_OUT,
   6623 			"Timed Out", ATTR_FLAG },
   6624 	{ NL80211_ATTR_USE_MFP,
   6625 			"Use MFP", ATTR_U32 },
   6626 	{ NL80211_ATTR_STA_FLAGS2,
   6627 			"Station Flags 2", ATTR_CUSTOM,
   6628 				{ .function = print_sta_flag_update } },
   6629 	{ NL80211_ATTR_CONTROL_PORT,
   6630 			"Control Port", ATTR_FLAG },
   6631 	{ NL80211_ATTR_TESTDATA,
   6632 			"Test Data" },
   6633 	{ NL80211_ATTR_PRIVACY,
   6634 			"Privacy", ATTR_FLAG },
   6635 	{ NL80211_ATTR_DISCONNECTED_BY_AP,
   6636 			"Disconnect by AP", ATTR_FLAG },
   6637 	{ NL80211_ATTR_STATUS_CODE,
   6638 			"Status Code", ATTR_U16 },
   6639 	{ NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
   6640 			"Cipher Suites Pairwise", ATTR_CUSTOM,
   6641 					{ .function = print_cipher_suites } },
   6642 	{ NL80211_ATTR_CIPHER_SUITE_GROUP,
   6643 			"Cipher Suite Group", ATTR_CUSTOM,
   6644 					{ .function = print_cipher_suite } },
   6645 	{ NL80211_ATTR_WPA_VERSIONS,
   6646 			"WPA Versions", ATTR_U32 },
   6647 	{ NL80211_ATTR_AKM_SUITES,
   6648 			"AKM Suites", ATTR_CUSTOM,
   6649 					{ .function = print_akm_suites } },
   6650 	{ NL80211_ATTR_REQ_IE,
   6651 			"Request IE", ATTR_CUSTOM,
   6652 					{ .function = print_management_ies } },
   6653 	{ NL80211_ATTR_RESP_IE,
   6654 			"Response IE", ATTR_CUSTOM,
   6655 					{.function = print_management_ies } },
   6656 	{ NL80211_ATTR_PREV_BSSID,
   6657 			"Previous BSSID", ATTR_ADDRESS },
   6658 	{ NL80211_ATTR_KEY,
   6659 			"Key", ATTR_NESTED, { key_table } },
   6660 	{ NL80211_ATTR_KEYS,
   6661 			"Keys" },
   6662 	{ NL80211_ATTR_PID,
   6663 			"PID", ATTR_U32 },
   6664 	{ NL80211_ATTR_4ADDR,
   6665 			"4-Address", ATTR_U8 },
   6666 	{ NL80211_ATTR_SURVEY_INFO,
   6667 			"Survey Info", ATTR_NESTED, { survey_info_table } },
   6668 	{ NL80211_ATTR_PMKID,
   6669 			"PMKID", ATTR_BINARY },
   6670 	{ NL80211_ATTR_MAX_NUM_PMKIDS,
   6671 			"Max Number PMKIDs", ATTR_U8 },
   6672 	{ NL80211_ATTR_DURATION,
   6673 			"Duration", ATTR_U32 },
   6674 	{ NL80211_ATTR_COOKIE,
   6675 			"Cookie", ATTR_U64 },
   6676 	{ NL80211_ATTR_WIPHY_COVERAGE_CLASS,
   6677 			"Wiphy Coverage Class", ATTR_U8	},
   6678 	{ NL80211_ATTR_TX_RATES,
   6679 			"TX Rates" },
   6680 	{ NL80211_ATTR_FRAME_MATCH,
   6681 			"Frame Match", ATTR_BINARY },
   6682 	{ NL80211_ATTR_ACK,
   6683 			"ACK", ATTR_FLAG },
   6684 	{ NL80211_ATTR_PS_STATE,
   6685 			"PS State", ATTR_U32 },
   6686 	{ NL80211_ATTR_CQM,
   6687 			"CQM", ATTR_NESTED, { cqm_table } },
   6688 	{ NL80211_ATTR_LOCAL_STATE_CHANGE,
   6689 			"Local State Change", ATTR_FLAG },
   6690 	{ NL80211_ATTR_AP_ISOLATE,
   6691 			"AP Isolate", ATTR_U8 },
   6692 	{ NL80211_ATTR_WIPHY_TX_POWER_SETTING,
   6693 			"Wiphy TX Power Setting", ATTR_U32 },
   6694 	{ NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
   6695 			"Wiphy TX Power Level", ATTR_U32 },
   6696 	{ NL80211_ATTR_TX_FRAME_TYPES,
   6697 			"TX Frame Types", ATTR_NESTED,
   6698 						{ frame_types_table } },
   6699 	{ NL80211_ATTR_RX_FRAME_TYPES,
   6700 			"RX Frame Types", ATTR_NESTED,
   6701 						{ frame_types_table } },
   6702 	{ NL80211_ATTR_FRAME_TYPE,
   6703 			"Frame Type", ATTR_CUSTOM,
   6704 					{ .function = print_frame_type } },
   6705 	{ NL80211_ATTR_SUPPORT_IBSS_RSN,
   6706 			"Support IBSS RSN", ATTR_FLAG },
   6707 	{ NL80211_ATTR_WIPHY_ANTENNA_TX,
   6708 			"Wiphy Antenna TX", ATTR_U32 },
   6709 	{ NL80211_ATTR_WIPHY_ANTENNA_RX,
   6710 			"Wiphy Antenna RX", ATTR_U32 },
   6711 	{ NL80211_ATTR_MCAST_RATE,
   6712 			"Multicast Rate", ATTR_U32 },
   6713 	{ NL80211_ATTR_OFFCHANNEL_TX_OK,
   6714 			"Offchannel TX OK", ATTR_FLAG },
   6715 	{ NL80211_ATTR_BSS_HT_OPMODE,
   6716 			"BSS HT Operation Mode", ATTR_U16 },
   6717 	{ NL80211_ATTR_KEY_DEFAULT_TYPES,
   6718 			"Key Default Types", ATTR_NESTED,
   6719 						{ key_default_type_table } },
   6720 	{ NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
   6721 			"Max Remain on Channel Duration ", ATTR_U32 },
   6722 	{ NL80211_ATTR_MESH_SETUP,
   6723 			"Mesh Setup" },
   6724 	{ NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
   6725 			"Wiphy Antenna Avail TX" },
   6726 	{ NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
   6727 			"Wiphy Antenna Avail RX" },
   6728 	{ NL80211_ATTR_SUPPORT_MESH_AUTH,
   6729 			"Support Mesh Auth", ATTR_FLAG },
   6730 	{ NL80211_ATTR_STA_PLINK_STATE,
   6731 			"Station P-Link State" },
   6732 	{ NL80211_ATTR_WOWLAN_TRIGGERS,
   6733 			"WoWLAN Triggers" },
   6734 	{ NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED,
   6735 			"WoWLAN Triggers Supported" },
   6736 	{ NL80211_ATTR_SCHED_SCAN_INTERVAL,
   6737 			"Scheduled Scan Interval", ATTR_U32 },
   6738 	{ NL80211_ATTR_INTERFACE_COMBINATIONS,
   6739 			"Interface Combinations" },
   6740 	{ NL80211_ATTR_SOFTWARE_IFTYPES,
   6741 			"Software Interface Types", ATTR_NESTED,
   6742 							{ iftype_table } },
   6743 	{ NL80211_ATTR_REKEY_DATA,
   6744 			"Rekey Data", ATTR_NESTED, { rekey_table } },
   6745 	{ NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
   6746 			"Max Num Sched Scan SSIDs", ATTR_U8 },
   6747 	{ NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
   6748 			"Max Sched Scan IE Len", ATTR_U16 },
   6749 	{ NL80211_ATTR_SCAN_SUPP_RATES,
   6750 			"Scan Supported Rates" },
   6751 	{ NL80211_ATTR_HIDDEN_SSID,
   6752 			"Hidden SSID", ATTR_U32 },
   6753 	{ NL80211_ATTR_IE_PROBE_RESP,
   6754 			"IE Probe Response", ATTR_CUSTOM,
   6755 					{ .function = print_management_ies } },
   6756 	{ NL80211_ATTR_IE_ASSOC_RESP,
   6757 			"IE Assoc Response", ATTR_CUSTOM,
   6758 					{ .function = print_management_ies } },
   6759 	{ NL80211_ATTR_STA_WME,
   6760 			"Station WME" },
   6761 	{ NL80211_ATTR_SUPPORT_AP_UAPSD,
   6762 			"Support AP UAPSD", ATTR_FLAG },
   6763 	{ NL80211_ATTR_ROAM_SUPPORT,
   6764 			"Roaming Support", ATTR_FLAG },
   6765 	{ NL80211_ATTR_SCHED_SCAN_MATCH,
   6766 			"Scheduled Scan Match" },
   6767 	{ NL80211_ATTR_MAX_MATCH_SETS,
   6768 			"Max Match Sets", ATTR_U8 },
   6769 	{ NL80211_ATTR_PMKSA_CANDIDATE,
   6770 			"PMKSA Candidate" },
   6771 	{ NL80211_ATTR_TX_NO_CCK_RATE,
   6772 			"TX No CCK Rate", ATTR_FLAG },
   6773 	{ NL80211_ATTR_TDLS_ACTION,
   6774 			"TDLS Action", ATTR_U8 },
   6775 	{ NL80211_ATTR_TDLS_DIALOG_TOKEN,
   6776 			"TDLS Dialog Token", ATTR_U8 },
   6777 	{ NL80211_ATTR_TDLS_OPERATION,
   6778 			"TDLS Operation", ATTR_U8 },
   6779 	{ NL80211_ATTR_TDLS_SUPPORT,
   6780 			"TDLS Support", ATTR_FLAG },
   6781 	{ NL80211_ATTR_TDLS_EXTERNAL_SETUP,
   6782 			"TDLS External Setup", ATTR_FLAG },
   6783 	{ NL80211_ATTR_DEVICE_AP_SME,
   6784 			"Device AP SME" },
   6785 	{ NL80211_ATTR_DONT_WAIT_FOR_ACK,
   6786 			"Don't Wait for Ack", ATTR_FLAG },
   6787 	{ NL80211_ATTR_FEATURE_FLAGS,
   6788 			"Feature Flags" },
   6789 	{ NL80211_ATTR_PROBE_RESP_OFFLOAD,
   6790 			"Probe Response Offload" },
   6791 	{ NL80211_ATTR_PROBE_RESP,
   6792 			"Probe Response", ATTR_CUSTOM,
   6793 					{ .function = print_frame} },
   6794 	{ NL80211_ATTR_DFS_REGION,
   6795 			"DFS Region", ATTR_U8 },
   6796 	{ NL80211_ATTR_DISABLE_HT,
   6797 			"Disable HT", ATTR_FLAG },
   6798 	{ NL80211_ATTR_HT_CAPABILITY_MASK,
   6799 			"HT Capability Mask" },
   6800 	{ NL80211_ATTR_NOACK_MAP,
   6801 			"No-Ack Map", ATTR_U16 },
   6802 	{ NL80211_ATTR_INACTIVITY_TIMEOUT,
   6803 			"Inactivity Timeout", ATTR_U16 },
   6804 	{ NL80211_ATTR_RX_SIGNAL_DBM,
   6805 			"RX Signal dBm", ATTR_S32 },
   6806 	{ NL80211_ATTR_BG_SCAN_PERIOD,
   6807 			"Background Scan Period", ATTR_U16 },
   6808 	{ NL80211_ATTR_WDEV,
   6809 			"Wireless Device", ATTR_U64 },
   6810 	{ NL80211_ATTR_USER_REG_HINT_TYPE,
   6811 			"User Regulatroy Hint Type", ATTR_U32 },
   6812 	{ NL80211_ATTR_CONN_FAILED_REASON,
   6813 			"Connection Failed Reason" },
   6814 	{ NL80211_ATTR_AUTH_DATA,
   6815 			"Auth Data" },
   6816 	{ NL80211_ATTR_VHT_CAPABILITY,
   6817 			"VHT Capability" },
   6818 	{ NL80211_ATTR_SCAN_FLAGS, "Scan Flags", ATTR_CUSTOM,
   6819 					{ .function = print_scan_flags } },
   6820 	{ NL80211_ATTR_CHANNEL_WIDTH,
   6821 			"Channel Width", ATTR_U32 },
   6822 	{ NL80211_ATTR_CENTER_FREQ1,
   6823 			"Center Frequency 1", ATTR_U32 },
   6824 	{ NL80211_ATTR_CENTER_FREQ2,
   6825 			"Center Frequency 2", ATTR_U32 },
   6826 	{ NL80211_ATTR_P2P_CTWINDOW,
   6827 			"P2P CT Window", ATTR_U8 },
   6828 	{ NL80211_ATTR_P2P_OPPPS,
   6829 			"P2P OP PPS", ATTR_U8 },
   6830 	{ NL80211_ATTR_LOCAL_MESH_POWER_MODE,
   6831 			"Local Mesh Power Mode" },
   6832 	{ NL80211_ATTR_ACL_POLICY,
   6833 			"ACL Policy", ATTR_U32 },
   6834 	{ NL80211_ATTR_MAC_ADDRS,
   6835 			"MAC Addresses", ATTR_ARRAY,
   6836 					{ .array_type = ATTR_ADDRESS } },
   6837 	{ NL80211_ATTR_MAC_ACL_MAX,
   6838 			"MAC ACL Max" },
   6839 	{ NL80211_ATTR_RADAR_EVENT,
   6840 			"Radar Event" },
   6841 	{ NL80211_ATTR_EXT_CAPA,
   6842 			"Extended Capabilities", ATTR_CUSTOM,
   6843 			{ .function = print_ie_extended_capabilities } },
   6844 	{ NL80211_ATTR_EXT_CAPA_MASK,
   6845 			"Extended Capabilities Mask" },
   6846 	{ NL80211_ATTR_STA_CAPABILITY,
   6847 			"Station Capability", ATTR_U16 },
   6848 	{ NL80211_ATTR_STA_EXT_CAPABILITY,
   6849 			"Station Extended Capability" },
   6850 	{ NL80211_ATTR_PROTOCOL_FEATURES,
   6851 			"Protocol Features", ATTR_U32 },
   6852 	{ NL80211_ATTR_SPLIT_WIPHY_DUMP,
   6853 			"Split Wiphy Dump", ATTR_FLAG},
   6854 	{ NL80211_ATTR_DISABLE_VHT,
   6855 			"Disable VHT" },
   6856 	{ NL80211_ATTR_VHT_CAPABILITY_MASK,
   6857 			"VHT Capability Mask" },
   6858 	{ NL80211_ATTR_MDID,
   6859 			"MDID", ATTR_U16 },
   6860 	{ NL80211_ATTR_IE_RIC,
   6861 			"IE RIC", ATTR_BINARY },
   6862 	{ NL80211_ATTR_CRIT_PROT_ID,
   6863 			"Critical Protocol ID" },
   6864 	{ NL80211_ATTR_MAX_CRIT_PROT_DURATION,
   6865 			"Max Critical Protocol Duration" },
   6866 	{ NL80211_ATTR_PEER_AID,
   6867 			"Peer AID", ATTR_U16 },
   6868 	{ NL80211_ATTR_COALESCE_RULE,
   6869 			"Coalesce Rule" },
   6870 	{ NL80211_ATTR_CH_SWITCH_COUNT,
   6871 			"Channel Switch Count", ATTR_U32 },
   6872 	{ NL80211_ATTR_CH_SWITCH_BLOCK_TX,
   6873 			"Channel Switch Block TX", ATTR_FLAG },
   6874 	{ NL80211_ATTR_CSA_IES,
   6875 			"CSA IEs" },
   6876 	{ NL80211_ATTR_CSA_C_OFF_BEACON,
   6877 			"CSA C Off Beacon" },
   6878 	{ NL80211_ATTR_CSA_C_OFF_PRESP,
   6879 			"CSA C Off Response" },
   6880 	{ NL80211_ATTR_RXMGMT_FLAGS,
   6881 			"RX Management Flags", ATTR_U32 },
   6882 	{ NL80211_ATTR_STA_SUPPORTED_CHANNELS,
   6883 			"Station Supported Channels" },
   6884 	{ NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
   6885 			"Station Supported Operation Classes" },
   6886 	{ NL80211_ATTR_HANDLE_DFS,
   6887 			"Handle DFS", ATTR_FLAG },
   6888 	{ NL80211_ATTR_SUPPORT_5_MHZ,
   6889 			"Support 5 MHz" },
   6890 	{ NL80211_ATTR_SUPPORT_10_MHZ,
   6891 			"Support 10 MHz" },
   6892 	{ NL80211_ATTR_OPMODE_NOTIF,
   6893 			"Operation Mode Notification", ATTR_U8 },
   6894 	{ NL80211_ATTR_VENDOR_ID,
   6895 			"Vendor ID", ATTR_U32 },
   6896 	{ NL80211_ATTR_VENDOR_SUBCMD,
   6897 			"Vendor Subcommand", ATTR_U32 },
   6898 	{ NL80211_ATTR_VENDOR_DATA,
   6899 			"Vendor Data", ATTR_BINARY },
   6900 	{ NL80211_ATTR_VENDOR_EVENTS,
   6901 			"Vendor Events" },
   6902 	{ NL80211_ATTR_QOS_MAP,
   6903 			"QoS Map", ATTR_BINARY },
   6904 	{ NL80211_ATTR_MAC_HINT,
   6905 			"MAC Hint", ATTR_ADDRESS },
   6906 	{ NL80211_ATTR_WIPHY_FREQ_HINT,
   6907 			"Wiphy Frequency Hint", ATTR_U32 },
   6908 	{ NL80211_ATTR_MAX_AP_ASSOC_STA,
   6909 			"Max AP Assoc Station" },
   6910 	{ NL80211_ATTR_TDLS_PEER_CAPABILITY,
   6911 			"TDLS Peer Capability", ATTR_U32 },
   6912 	{ NL80211_ATTR_SOCKET_OWNER,
   6913 			"Socket Owns Interface/Connection", ATTR_FLAG },
   6914 	{ NL80211_ATTR_CSA_C_OFFSETS_TX,
   6915 			"CSA C Offsets TX" },
   6916 	{ NL80211_ATTR_MAX_CSA_COUNTERS,
   6917 			"Max CSA Counters" },
   6918 	{ NL80211_ATTR_TDLS_INITIATOR,
   6919 			"TDLS Initiator" },
   6920 	{ NL80211_ATTR_USE_RRM,
   6921 			"Use RRM", ATTR_FLAG },
   6922 	{ NL80211_ATTR_EXT_FEATURES,
   6923 			"Extended Features", ATTR_CUSTOM,
   6924 			{ .function = print_ext_features} },
   6925 	{ NL80211_ATTR_FILS_KEK,
   6926 			"FILS KEK" },
   6927 	{ NL80211_ATTR_FILS_NONCES,
   6928 			"FILS Nonces" },
   6929 	{ NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED,
   6930 			"Multicast to Unicast Enabled", ATTR_FLAG },
   6931 	{ NL80211_ATTR_BSSID,
   6932 			"BSSID", ATTR_ADDRESS },
   6933 	{ NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
   6934 			"Scheduled Scan Relative RSSI" },
   6935 	{ NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
   6936 			"Scheduled Scan RSSI Adjust" },
   6937 	{ NL80211_ATTR_TIMEOUT_REASON,
   6938 			"Timeout Reason" },
   6939 	{ NL80211_ATTR_FILS_ERP_USERNAME,
   6940 			"FILS ERP Username" },
   6941 	{ NL80211_ATTR_FILS_ERP_REALM,
   6942 			"FILS ERP Realm" },
   6943 	{ NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
   6944 			"FILS ERP Next Sequence Number" },
   6945 	{ NL80211_ATTR_FILS_ERP_RRK,
   6946 			"FILS ERP RRK" },
   6947 	{ NL80211_ATTR_FILS_CACHE_ID,
   6948 			"FILS Cache ID" },
   6949 	{ NL80211_ATTR_PMK,
   6950 			"PMK" },
   6951 	{ NL80211_ATTR_SCHED_SCAN_MULTI,
   6952 			"Scheduled Scan Multi" },
   6953 	{ NL80211_ATTR_SCHED_SCAN_MAX_REQS,
   6954 			"Scheduled Scan Maximum Requests" },
   6955 	{ NL80211_ATTR_WANT_1X_4WAY_HS,
   6956 			"Want 1X 4Way Handshake" },
   6957 	{ NL80211_ATTR_PMKR0_NAME,
   6958 			"PMKR0 Name" },
   6959 	{ NL80211_ATTR_PORT_AUTHORIZED,
   6960 			"Port Authorized" },
   6961 	{ NL80211_ATTR_EXTERNAL_AUTH_ACTION,
   6962 			"External Auth Action" },
   6963 	{ NL80211_ATTR_EXTERNAL_AUTH_SUPPORT,
   6964 			"External Auth Support" },
   6965 	{ NL80211_ATTR_NSS,
   6966 			"NSS" },
   6967 	{ NL80211_ATTR_ACK_SIGNAL,
   6968 			"Ack Signal" },
   6969 	{ NL80211_ATTR_CONTROL_PORT_OVER_NL80211,
   6970 			"Control Port over NL80211", ATTR_FLAG },
   6971 	{ NL80211_ATTR_TXQ_STATS,
   6972 			"TXQ Stats" },
   6973 	{ NL80211_ATTR_TXQ_LIMIT,
   6974 			"TXQ Limit" },
   6975 	{ NL80211_ATTR_TXQ_MEMORY_LIMIT,
   6976 			"TXQ Memory Limit" },
   6977 	{ NL80211_ATTR_TXQ_QUANTUM,
   6978 			"TXQ Quantum" },
   6979 	{ NL80211_ATTR_HE_CAPABILITY,
   6980 			"HE Capability" },
   6981 	{ NL80211_ATTR_FTM_RESPONDER,
   6982 			"FTM Responder" },
   6983 	{ NL80211_ATTR_FTM_RESPONDER_STATS,
   6984 			"FTM Responder Stats" },
   6985 	{ NL80211_ATTR_SCAN_START_TIME_TSF,
   6986 			"Scan Start Time", ATTR_U64 },
   6987 	{ NL80211_ATTR_SCAN_START_TIME_TSF_BSSID,
   6988 			"Scan Start Time BSSID", ATTR_ADDRESS },
   6989 	{ NL80211_ATTR_MEASUREMENT_DURATION,
   6990 			"Scan Duration", ATTR_U16 },
   6991 	{ NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY,
   6992 			"Scan Duration Mandatory", ATTR_FLAG },
   6993 	{ }
   6994 };
   6995 
   6996 static void print_value(int indent, const char *label, enum attr_type type,
   6997 						const void *buf, uint32_t len)
   6998 {
   6999 	uint16_t val_u16;
   7000 	uint32_t val_u32;
   7001 
   7002 	switch (type) {
   7003 	case ATTR_U16:
   7004 		val_u16 = *((uint16_t *) buf);
   7005 		print_attr(indent, "%s: %u (0x%04x)", label, val_u16, val_u16);
   7006 		if (len != 2)
   7007 			printf("malformed packet\n");
   7008 		break;
   7009 	case ATTR_U32:
   7010 		val_u32 = *((uint32_t *) buf);
   7011 		print_attr(indent, "%s: %u (0x%08x)", label, val_u32, val_u32);
   7012 		if (len != 4)
   7013 			printf("malformed packet\n");
   7014 		break;
   7015 	case ATTR_ADDRESS:
   7016 		print_address(indent, label, buf);
   7017 		break;
   7018 	case ATTR_UNSPEC:
   7019 	case ATTR_FLAG:
   7020 	case ATTR_U8:
   7021 	case ATTR_U64:
   7022 	case ATTR_S8:
   7023 	case ATTR_S32:
   7024 	case ATTR_S64:
   7025 	case ATTR_STRING:
   7026 	case ATTR_BINARY:
   7027 	case ATTR_NESTED:
   7028 	case ATTR_ARRAY:
   7029 	case ATTR_FLAG_OR_U16:
   7030 	case ATTR_CUSTOM:
   7031 		print_attr(indent, "%s: len %u", label, len);
   7032 		print_hexdump(indent  + 1, buf, len);
   7033 		break;
   7034 	}
   7035 }
   7036 
   7037 static void print_array(int indent, enum attr_type type,
   7038 						const struct attr_entry *entry,
   7039 						const void *buf, uint32_t len)
   7040 {
   7041 	const struct nlattr *nla;
   7042 
   7043 	for (nla = buf ; NLA_OK(nla, len); nla = NLA_NEXT(nla, len)) {
   7044 		uint16_t nla_type = nla->nla_type & NLA_TYPE_MASK;
   7045 		char str[8];
   7046 
   7047 		snprintf(str, sizeof(str), "%u", nla_type);
   7048 
   7049 		switch (type) {
   7050 		case ATTR_NESTED:
   7051 			if (entry->nested)
   7052 				print_attributes(indent + 1, entry->nested,
   7053 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   7054 			else
   7055 				printf("missing nested table\n");
   7056 			break;
   7057 		default:
   7058 			print_value(indent, str, type,
   7059 				NLA_DATA(nla), NLA_PAYLOAD(nla));
   7060 			break;
   7061 		}
   7062 	}
   7063 }
   7064 
   7065 static void print_attributes(int indent, const struct attr_entry *table,
   7066 						const void *buf, uint32_t len)
   7067 {
   7068 	const struct nlattr *nla;
   7069 	const char *str;
   7070 	int i;
   7071 
   7072 	for (nla = buf ; NLA_OK(nla, len); nla = NLA_NEXT(nla, len)) {
   7073 		uint16_t nla_type = nla->nla_type & NLA_TYPE_MASK;
   7074 		enum attr_type type;
   7075 		enum attr_type array_type;
   7076 		const struct attr_entry *nested;
   7077 		attr_func_t function;
   7078 		uint64_t val64;
   7079 		uint32_t val32;
   7080 		uint16_t val16;
   7081 		uint8_t val8;
   7082 		int8_t val_s8;
   7083 		int32_t val_s32;
   7084 		int64_t val_s64;
   7085 		uint8_t *ptr;
   7086 
   7087 		str = "Unknown";
   7088 		type = ATTR_UNSPEC;
   7089 		array_type = ATTR_UNSPEC;
   7090 		nested = NULL;
   7091 
   7092 		if (table) {
   7093 			for (i = 0; table[i].str; i++) {
   7094 				if (nla_type == table[i].attr) {
   7095 					str = table[i].str;
   7096 					type = table[i].type;
   7097 					nested = table[i].nested;
   7098 					array_type = table[i].array_type;
   7099 					function = table[i].function;
   7100 					break;
   7101 				}
   7102 			}
   7103 		}
   7104 
   7105 		switch (type) {
   7106 		case ATTR_UNSPEC:
   7107 			print_attr(indent, "%s: %u len %u", str, nla_type,
   7108 						NLA_PAYLOAD(nla));
   7109 			print_hexdump(indent + 1,
   7110 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   7111 			break;
   7112 		case ATTR_FLAG:
   7113 			print_attr(indent, "%s: true", str);
   7114 			if (NLA_PAYLOAD(nla) != 0)
   7115 				printf("malformed packet\n");
   7116 			break;
   7117 		case ATTR_U8:
   7118 			val8 = *((uint8_t *) NLA_DATA(nla));
   7119 			print_attr(indent, "%s: %"PRIu8" (0x%02"PRIx8")",
   7120 							str, val8, val8);
   7121 			if (NLA_PAYLOAD(nla) != 1)
   7122 				printf("malformed packet\n");
   7123 			break;
   7124 		case ATTR_U16:
   7125 			val16 = *((uint16_t *) NLA_DATA(nla));
   7126 			print_attr(indent, "%s: %"PRIu16" (0x%04"PRIx16")",
   7127 							str, val16, val16);
   7128 			if (NLA_PAYLOAD(nla) != 2)
   7129 				printf("malformed packet\n");
   7130 			break;
   7131 		case ATTR_U32:
   7132 			val32 = *((uint32_t *) NLA_DATA(nla));
   7133 			print_attr(indent, "%s: %"PRIu32" (0x%08"PRIx32")",
   7134 							str, val32, val32);
   7135 			if (NLA_PAYLOAD(nla) != 4)
   7136 				printf("malformed packet\n");
   7137 			break;
   7138 		case ATTR_U64:
   7139 			val64 = *((uint64_t *) NLA_DATA(nla));
   7140 			print_attr(indent, "%s: %"PRIu64" (0x%016"PRIx64")",
   7141 							str, val64, val64);
   7142 			if (NLA_PAYLOAD(nla) != 8)
   7143 				printf("malformed packet\n");
   7144 			break;
   7145 		case ATTR_S8:
   7146 			val_s8 = *((int8_t *) NLA_DATA(nla));
   7147 			print_attr(indent, "%s: %"PRId8, str, val_s8);
   7148 			if (NLA_PAYLOAD(nla) != 1)
   7149 				printf("malformed packet\n");
   7150 			break;
   7151 		case ATTR_S32:
   7152 			val_s32 = *((int32_t *) NLA_DATA(nla));
   7153 			print_attr(indent, "%s: %"PRId32, str, val_s32);
   7154 			if (NLA_PAYLOAD(nla) != 4)
   7155 				printf("malformed packet\n");
   7156 			break;
   7157 		case ATTR_S64:
   7158 			val_s64 = *((int64_t *) NLA_DATA(nla));
   7159 			print_attr(indent, "%s: %"PRId64, str, val_s64);
   7160 			if (NLA_PAYLOAD(nla) != 4)
   7161 				printf("malformed packet\n");
   7162 			break;
   7163 		case ATTR_STRING:
   7164 			print_attr(indent, "%s: %s", str,
   7165 						(char *) NLA_DATA(nla));
   7166 			break;
   7167 		case ATTR_ADDRESS:
   7168 			ptr = NLA_DATA(nla);
   7169 			print_address(indent, str, ptr);
   7170 			if (NLA_PAYLOAD(nla) != 6)
   7171 				printf("malformed packet\n");
   7172 			break;
   7173 		case ATTR_BINARY:
   7174 			print_attr(indent, "%s: len %u", str,
   7175 						NLA_PAYLOAD(nla));
   7176 			print_hexdump(indent + 1,
   7177 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   7178 			break;
   7179 		case ATTR_NESTED:
   7180 			print_attr(indent, "%s: len %u", str,
   7181 						NLA_PAYLOAD(nla));
   7182 			if (!nested)
   7183 				printf("missing table\n");
   7184 			print_attributes(indent + 1, nested,
   7185 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   7186 			break;
   7187 		case ATTR_ARRAY:
   7188 			print_attr(indent, "%s: len %u", str,
   7189 						NLA_PAYLOAD(nla));
   7190 			if (array_type == ATTR_UNSPEC)
   7191 				printf("missing type\n");
   7192 			print_array(indent + 1, array_type, &table[i],
   7193 					NLA_DATA(nla), NLA_PAYLOAD(nla));
   7194 			break;
   7195 		case ATTR_FLAG_OR_U16:
   7196 			if (NLA_PAYLOAD(nla) == 0)
   7197 				print_attr(indent, "%s: true", str);
   7198 			else if (NLA_PAYLOAD(nla) == 2) {
   7199 				val16 = *((uint16_t *) NLA_DATA(nla));
   7200 				print_attr(indent,
   7201 					"%s: %"PRIu16" (0x%04"PRIx16")",
   7202 					str, val16, val16);
   7203 			} else
   7204 				printf("malformed packet\n");
   7205 			break;
   7206 		case ATTR_CUSTOM:
   7207 			if (function)
   7208 				function(indent, str, NLA_DATA(nla),
   7209 							NLA_PAYLOAD(nla));
   7210 			else
   7211 				printf("missing function\n");
   7212 			break;
   7213 		}
   7214 	}
   7215 }
   7216 
   7217 static void netlink_str(char *str, size_t size,
   7218 				uint16_t type, uint16_t flags, uint32_t len)
   7219 {
   7220 	int pos;
   7221 	bool get_req = false, new_req = false;
   7222 
   7223 	pos = sprintf(str, "(0x%02x) len %u", type, len);
   7224 
   7225 	switch (type) {
   7226 	case RTM_GETLINK:
   7227 	case RTM_GETADDR:
   7228 	case RTM_GETROUTE:
   7229 		get_req = true;
   7230 		break;
   7231 
   7232 	case RTM_NEWLINK:
   7233 	case RTM_NEWADDR:
   7234 	case RTM_NEWROUTE:
   7235 		new_req = true;
   7236 		break;
   7237 	}
   7238 
   7239 	if (flags) {
   7240 		pos += sprintf(str + pos, " [");
   7241 
   7242 		if (flags & NLM_F_REQUEST) {
   7243 			flags &= ~NLM_F_REQUEST;
   7244 			pos += sprintf(str + pos, "request%c",
   7245 							flags ? ',' : ']');
   7246 		}
   7247 
   7248 		if (flags & NLM_F_MULTI) {
   7249 			flags &= ~NLM_F_MULTI;
   7250 			pos += sprintf(str + pos, "multi%c", flags ? ',' : ']');
   7251 		}
   7252 
   7253 		if (flags & NLM_F_ACK) {
   7254 			flags &= ~NLM_F_ACK;
   7255 			pos += sprintf(str + pos, "ack%c", flags ? ',' : ']');
   7256 		}
   7257 
   7258 		if (flags & NLM_F_ECHO) {
   7259 			flags &= ~NLM_F_ECHO;
   7260 			pos += sprintf(str + pos, "echo%c", flags ? ',' : ']');
   7261 		}
   7262 
   7263 		if (get_req && (flags & NLM_F_DUMP) == NLM_F_DUMP) {
   7264 			flags &= ~NLM_F_DUMP;
   7265 			pos += sprintf(str + pos, "dump%c", flags ? ',' : ']');
   7266 		}
   7267 
   7268 		if (get_req && flags & NLM_F_ROOT) {
   7269 			flags &= ~NLM_F_ROOT;
   7270 			pos += sprintf(str + pos, "root%c", flags ? ',' : ']');
   7271 		}
   7272 
   7273 		if (get_req && flags & NLM_F_MATCH) {
   7274 			flags &= ~NLM_F_MATCH;
   7275 			pos += sprintf(str + pos, "match%c", flags ? ',' : ']');
   7276 		}
   7277 
   7278 		if (get_req && flags & NLM_F_ATOMIC) {
   7279 			flags &= ~NLM_F_ATOMIC;
   7280 			pos += sprintf(str + pos, "atomic%c",
   7281 							flags ? ',' : ']');
   7282 		}
   7283 
   7284 		if (new_req && flags & NLM_F_REPLACE) {
   7285 			flags &= ~NLM_F_REPLACE;
   7286 			pos += sprintf(str + pos, "replace%c",
   7287 							flags ? ',' : ']');
   7288 		}
   7289 
   7290 		if (new_req && flags & NLM_F_EXCL) {
   7291 			flags &= ~NLM_F_EXCL;
   7292 			pos += sprintf(str + pos, "excl%c", flags ? ',' : ']');
   7293 		}
   7294 
   7295 		if (new_req && flags & NLM_F_CREATE) {
   7296 			flags &= ~NLM_F_CREATE;
   7297 			pos += sprintf(str + pos, "create%c",
   7298 							flags ? ',' : ']');
   7299 		}
   7300 
   7301 		if (new_req && flags & NLM_F_APPEND) {
   7302 			flags &= ~NLM_F_APPEND;
   7303 			pos += sprintf(str + pos, "append%c",
   7304 							flags ? ',' : ']');
   7305 		}
   7306 
   7307 		if (flags)
   7308 			pos += sprintf(str + pos, "0x%x]", flags);
   7309 	}
   7310 }
   7311 
   7312 static void print_message(struct nlmon *nlmon, const struct timeval *tv,
   7313 						enum msg_type type,
   7314 						uint16_t flags, int status,
   7315 						uint8_t cmd, uint8_t version,
   7316 						const void *data, uint32_t len)
   7317 {
   7318 	char extra_str[64];
   7319 	const char *label = "";
   7320 	const char *color = COLOR_OFF;
   7321 	const char *cmd_str;
   7322 	bool out = false;
   7323 
   7324 	if (nlmon->nowiphy && (cmd == NL80211_CMD_NEW_WIPHY))
   7325 		return;
   7326 
   7327 	if (nlmon->noscan && L_IN_SET(cmd, NL80211_CMD_NEW_SCAN_RESULTS,
   7328 					NL80211_CMD_NEW_SURVEY_RESULTS,
   7329 					NL80211_CMD_TRIGGER_SCAN,
   7330 					NL80211_CMD_GET_SURVEY))
   7331 		return;
   7332 
   7333 	switch (type) {
   7334 	case MSG_REQUEST:
   7335 		label = "Request";
   7336 		color = COLOR_REQUEST;
   7337 		out = true;
   7338 		break;
   7339 	case MSG_RESPONSE:
   7340 		label = "Response";
   7341 		color = COLOR_RESPONSE;
   7342 		break;
   7343 	case MSG_COMPLETE:
   7344 		label = "Complete";
   7345 		color = COLOR_COMPLETE;
   7346 		break;
   7347 	case MSG_RESULT:
   7348 		label = "Result";
   7349 		color = COLOR_RESULT;
   7350 		break;
   7351 	case MSG_EVENT:
   7352 		label = "Event";
   7353 		color = COLOR_EVENT;
   7354 		break;
   7355 	}
   7356 
   7357 	cmd_str = nl80211cmd_to_string(cmd);
   7358 
   7359 	netlink_str(extra_str, sizeof(extra_str), cmd, flags, len);
   7360 
   7361 	print_packet(tv, out ? '<' : '>', color, label, cmd_str, extra_str);
   7362 
   7363 	switch (type) {
   7364 	case MSG_REQUEST:
   7365 	case MSG_RESULT:
   7366 	case MSG_EVENT:
   7367 		cur_nlmon = nlmon;
   7368 
   7369 		switch (cmd) {
   7370 		case NL80211_CMD_CONTROL_PORT_FRAME:
   7371 			print_attributes(0, control_port_attr_table, data, len);
   7372 			break;
   7373 		default:
   7374 			print_attributes(0, attr_table, data, len);
   7375 		}
   7376 
   7377 		cur_nlmon = NULL;
   7378 		break;
   7379 	case MSG_RESPONSE:
   7380 		print_field("Status: %s (%d)", strerror(status), status);
   7381 		break;
   7382 	case MSG_COMPLETE:
   7383 		if (status < 0)
   7384 			print_field("Status: %s (%d)",
   7385 					strerror(-status), -status);
   7386 		else
   7387 			print_field("Status: %d", status);
   7388 		break;
   7389 	}
   7390 }
   7391 
   7392 struct nlmon_req_match {
   7393 	uint32_t seq;
   7394 	uint32_t pid;
   7395 };
   7396 
   7397 static bool nlmon_req_match(const void *a, const void *b)
   7398 {
   7399 	const struct nlmon_req *req = a;
   7400 	const struct nlmon_req_match *match = b;
   7401 
   7402 	return (req->seq == match->seq && req->pid == match->pid);
   7403 }
   7404 
   7405 /*
   7406  * Ensures that PCAP names are zero padded when needed. This makes the files
   7407  * sort correctly.
   7408  */
   7409 static void next_pcap_name(char *buf, size_t size, const char *prefix,
   7410 				unsigned int idx, unsigned int max)
   7411 {
   7412 	unsigned int ndigits = 1;
   7413 
   7414 	while (max > 9) {
   7415 		max /= 10;
   7416 		ndigits++;
   7417 	}
   7418 
   7419 	snprintf(buf, size, "%s%.*u", prefix, ndigits, idx);
   7420 }
   7421 
   7422 static bool check_pcap(struct nlmon *nlmon, size_t next_size)
   7423 {
   7424 	char path[PATH_MAX];
   7425 
   7426 	if (!nlmon->pcap)
   7427 		return false;
   7428 
   7429 	if (!nlmon->max_size)
   7430 		return true;
   7431 
   7432 	if (pcap_get_size(nlmon->pcap) + next_size <= nlmon->max_size)
   7433 		return true;
   7434 
   7435 	pcap_close(nlmon->pcap);
   7436 
   7437 	/* Exhausted the single PCAP file */
   7438 	if (nlmon->max_files < 2) {
   7439 		printf("Reached maximum size of PCAP, exiting\n");
   7440 		nlmon->pcap = NULL;
   7441 		l_main_quit();
   7442 		return false;
   7443 	}
   7444 
   7445 	next_pcap_name(path, sizeof(path), nlmon->file_prefix,
   7446 			++nlmon->file_idx, nlmon->max_files);
   7447 
   7448 	nlmon->pcap = pcap_create(path);
   7449 
   7450 	if (nlmon->max_files > nlmon->file_idx)
   7451 		return true;
   7452 
   7453 	/* Remove oldest PCAP file */
   7454 	next_pcap_name(path, sizeof(path), nlmon->file_prefix,
   7455 		nlmon->file_idx - nlmon->max_files, nlmon->max_files);
   7456 
   7457 	if (remove(path) < 0)
   7458 		printf("Failed to remove old PCAP file %s\n", path);
   7459 
   7460 	return true;
   7461 }
   7462 
   7463 static void store_packet(struct nlmon *nlmon, const struct timeval *tv,
   7464 					uint16_t pkt_type,
   7465 					uint16_t arphrd_type,
   7466 					uint16_t proto_type,
   7467 					const void *data, uint32_t size)
   7468 {
   7469 	uint8_t sll_hdr[16], *buf = sll_hdr;
   7470 
   7471 	if (!check_pcap(nlmon, sizeof(sll_hdr) + size))
   7472 		return;
   7473 
   7474 	memset(sll_hdr, 0, sizeof(sll_hdr));
   7475 
   7476 	l_put_be16(pkt_type, buf);
   7477 	l_put_be16(arphrd_type, buf + 2);
   7478 	l_put_be16(proto_type, buf + 14);
   7479 
   7480 	pcap_write(nlmon->pcap, tv, &sll_hdr, sizeof(sll_hdr), data, size);
   7481 }
   7482 
   7483 static void store_netlink(struct nlmon *nlmon, const struct timeval *tv,
   7484 					uint16_t proto_type,
   7485 					const struct nlmsghdr *nlmsg)
   7486 {
   7487 	store_packet(nlmon, tv, PACKET_HOST, ARPHRD_NETLINK, proto_type,
   7488 						nlmsg, nlmsg->nlmsg_len);
   7489 }
   7490 
   7491 static void store_message(struct nlmon *nlmon, const struct timeval *tv,
   7492 					const struct nlmsghdr *nlmsg)
   7493 {
   7494 	store_netlink(nlmon, tv, NETLINK_GENERIC, nlmsg);
   7495 }
   7496 
   7497 static void nlmon_message(struct nlmon *nlmon, const struct timeval *tv,
   7498 					const struct nlmsghdr *nlmsg)
   7499 {
   7500 	struct nlmon_req *req;
   7501 
   7502 	if (nlmsg->nlmsg_type < NLMSG_MIN_TYPE) {
   7503 		struct nlmon_req_match match = {
   7504 			.seq = nlmsg->nlmsg_seq,
   7505 			.pid = nlmsg->nlmsg_pid
   7506 		};
   7507 
   7508 		req = l_queue_remove_if(nlmon->req_list,
   7509 						nlmon_req_match, &match);
   7510 		if (req) {
   7511 			enum msg_type type;
   7512 			struct nlmsgerr *err;
   7513 			int status;
   7514 
   7515 			switch (nlmsg->nlmsg_type) {
   7516 			case NLMSG_ERROR:
   7517 				type = MSG_RESPONSE;
   7518 				err = NLMSG_DATA(nlmsg);
   7519 				status = -err->error;
   7520 				break;
   7521 			case NLMSG_DONE:
   7522 				type = MSG_COMPLETE;
   7523 				status = *((int *) NLMSG_DATA(nlmsg));
   7524 				break;
   7525 			default:
   7526 				return;
   7527 			}
   7528 
   7529 			store_message(nlmon, tv, nlmsg);
   7530 			print_message(nlmon, tv, type, nlmsg->nlmsg_flags, status,
   7531 						req->cmd, req->version,
   7532 						NULL, sizeof(status));
   7533 			nlmon_req_free(req);
   7534 		}
   7535 		return;
   7536 	}
   7537 
   7538 	if (nlmsg->nlmsg_flags & NLM_F_REQUEST) {
   7539 		const struct genlmsghdr *genlmsg = NLMSG_DATA(nlmsg);
   7540 		uint32_t flags = nlmsg->nlmsg_flags & ~NLM_F_REQUEST;
   7541 
   7542 		req = l_new(struct nlmon_req, 1);
   7543 
   7544 		req->seq = nlmsg->nlmsg_seq;
   7545 		req->pid = nlmsg->nlmsg_pid;
   7546 		req->flags = nlmsg->nlmsg_flags;
   7547 		req->cmd = genlmsg->cmd;
   7548 		req->version = genlmsg->version;
   7549 
   7550 		l_queue_push_tail(nlmon->req_list, req);
   7551 
   7552 		store_message(nlmon, tv, nlmsg);
   7553 		print_message(nlmon, tv, MSG_REQUEST, flags, 0,
   7554 					req->cmd, req->version,
   7555 					NLMSG_DATA(nlmsg) + GENL_HDRLEN,
   7556 					NLMSG_PAYLOAD(nlmsg, GENL_HDRLEN));
   7557 	} else {
   7558 		const struct genlmsghdr *genlmsg = NLMSG_DATA(nlmsg);
   7559 		enum msg_type type = MSG_EVENT;
   7560 
   7561 		struct nlmon_req_match match = {
   7562 			.seq = nlmsg->nlmsg_seq,
   7563 			.pid = nlmsg->nlmsg_pid
   7564 		};
   7565 
   7566 		req = l_queue_find(nlmon->req_list, nlmon_req_match, &match);
   7567 		if (req) {
   7568 			if (!(req->flags & NLM_F_ACK)) {
   7569 				l_queue_remove(nlmon->req_list, req);
   7570 				nlmon_req_free(req);
   7571 			}
   7572 			type = MSG_RESULT;
   7573 		}
   7574 
   7575 		store_message(nlmon, tv, nlmsg);
   7576 		print_message(nlmon, tv, type, nlmsg->nlmsg_flags, 0,
   7577 					genlmsg->cmd, genlmsg->version,
   7578 					NLMSG_DATA(nlmsg) + GENL_HDRLEN,
   7579 					NLMSG_PAYLOAD(nlmsg, GENL_HDRLEN));
   7580 	}
   7581 }
   7582 
   7583 struct nlmon *nlmon_create(uint16_t id, const struct nlmon_config *config)
   7584 {
   7585 	struct nlmon *nlmon;
   7586 
   7587 	nlmon = l_new(struct nlmon, 1);
   7588 
   7589 	nlmon->id = id;
   7590 	nlmon->req_list = l_queue_new();
   7591 	nlmon->nortnl = config->nortnl;
   7592 	nlmon->nowiphy = config->nowiphy;
   7593 	nlmon->noscan = config->noscan;
   7594 	nlmon->noies = config->noies;
   7595 	nlmon->read = config->read_only;
   7596 	nlmon->time_format = config->time_format;
   7597 	nlmon->max_files = config->pcap_file_count;
   7598 	/* Command line expects MB, but use bytes internally */
   7599 	nlmon->max_size = config->pcap_file_size * BYTES_PER_MB;
   7600 
   7601 	return nlmon;
   7602 }
   7603 
   7604 void nlmon_destroy(struct nlmon *nlmon)
   7605 {
   7606 	if (!nlmon)
   7607 		return;
   7608 
   7609 	l_queue_destroy(nlmon->req_list, nlmon_req_free);
   7610 
   7611 	l_free(nlmon);
   7612 }
   7613 
   7614 static const char *scope_to_string(uint8_t scope)
   7615 {
   7616 	switch (scope) {
   7617 	case RT_SCOPE_UNIVERSE:
   7618 		return "global";
   7619 	case RT_SCOPE_SITE:
   7620 		return "site";
   7621 	case RT_SCOPE_LINK:
   7622 		return "link";
   7623 	case RT_SCOPE_HOST:
   7624 		return "host";
   7625 	case RT_SCOPE_NOWHERE:
   7626 		return "nowhere";
   7627 	default:
   7628 		return "unknown";
   7629 	}
   7630 }
   7631 
   7632 static void print_ifi_addr(unsigned int indent, const char *str,
   7633 						const void *buf, uint16_t size)
   7634 {
   7635 	struct ether_addr eth;
   7636 
   7637 	if (size != ETH_ALEN) {
   7638 		printf("malformed packet\n");
   7639 		return;
   7640 	}
   7641 
   7642 	memcpy(&eth, buf, ETH_ALEN);
   7643 	print_attr(indent, "%s: %s", str, ether_ntoa(&eth));
   7644 }
   7645 
   7646 static const char *oper_state_to_ascii(const uint8_t state)
   7647 {
   7648 	switch(state) {
   7649 	case IF_OPER_UNKNOWN:
   7650 		return "unknown";
   7651 	case IF_OPER_NOTPRESENT:
   7652 		return "not present";
   7653 	case IF_OPER_DOWN:
   7654 		return "down";
   7655 	case IF_OPER_LOWERLAYERDOWN:
   7656 		return "lower layer down";
   7657 	case IF_OPER_TESTING:
   7658 		return "testing";
   7659 	case IF_OPER_DORMANT:
   7660 		return "dormant";
   7661 	case IF_OPER_UP:
   7662 		return "up";
   7663 	}
   7664 
   7665 	return NULL;
   7666 }
   7667 
   7668 static void print_oper_state(unsigned int indent, const char *str,
   7669 						const void *buf, uint16_t size)
   7670 {
   7671 	uint8_t oper_state;
   7672 
   7673 	if (size != 1) {
   7674 		printf("malformed packet\n");
   7675 		return;
   7676 	}
   7677 
   7678 	oper_state = ((uint8_t *)buf)[0];
   7679 
   7680 	print_attr(indent, "%s: %s (%d)", str,
   7681 		oper_state_to_ascii(oper_state), oper_state);
   7682 }
   7683 
   7684 static const char *link_mode_to_ascii(const uint8_t mode)
   7685 {
   7686 	switch(mode) {
   7687 	case 0:
   7688 		return "kernel controlled";
   7689 	case 1:
   7690 		return "userspace controlled";
   7691 	}
   7692 
   7693 	return NULL;
   7694 }
   7695 
   7696 static void print_link_mode(unsigned int indent, const char *str,
   7697 						const void *buf, uint16_t size)
   7698 {
   7699 	uint8_t link_mode;
   7700 
   7701 	if (size != 1) {
   7702 		printf("malformed packet\n");
   7703 		return;
   7704 	}
   7705 
   7706 	link_mode = ((uint8_t *)buf)[0];
   7707 
   7708 	print_attr(indent, "%s: %s (%d)", str,
   7709 		link_mode_to_ascii(link_mode), link_mode);
   7710 }
   7711 
   7712 static void flags_str(const struct flag_names *table,
   7713 				char *str, size_t size, uint16_t flags)
   7714 {
   7715 	int pos, i;
   7716 
   7717 	pos = sprintf(str, "(0x%02x)", flags);
   7718 	if (!flags)
   7719 		return;
   7720 
   7721 	pos += sprintf(str + pos, " [");
   7722 
   7723 	for (i = 0; table[i].name; i++) {
   7724 		if (flags & table[i].flag) {
   7725 			flags &= ~table[i].flag;
   7726 			pos += sprintf(str + pos, "%s%s", table[i].name,
   7727 							flags ? "," : "");
   7728 		}
   7729 	}
   7730 
   7731 	pos += sprintf(str + pos, "]");
   7732 }
   7733 
   7734 static void print_rmnet_flags(unsigned int indent,
   7735 					const char *label, uint32_t flags)
   7736 {
   7737 	if (flags & RMNET_FLAGS_INGRESS_DEAGGREGATION)
   7738 		print_attr(indent, "%s: %s", label, "deaggregation");
   7739 	if (flags & RMNET_FLAGS_INGRESS_MAP_COMMANDS)
   7740 		print_attr(indent, "%s: %s", label, "map commands");
   7741 	if (flags & RMNET_FLAGS_INGRESS_MAP_CKSUMV4)
   7742 		print_attr(indent, "%s: %s", label, "ingress_mapv4");
   7743 	if (flags & RMNET_FLAGS_EGRESS_MAP_CKSUMV4)
   7744 		print_attr(indent, "%s: %s", label, "egress_mapv4");
   7745 	if (flags & RMNET_FLAGS_INGRESS_MAP_CKSUMV5)
   7746 		print_attr(indent, "%s: %s", label, "ingress_mapv5");
   7747 	if (flags & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
   7748 		print_attr(indent, "%s: %s", label, "egress_mapv5");
   7749 }
   7750 
   7751 static void print_ifla_rmnet_flags(unsigned int indent, const char *str,
   7752 						const void *buf, uint16_t size)
   7753 {
   7754 	struct ifla_rmnet_flags flags;
   7755 
   7756 	if (size != 8) {
   7757 		printf("malformed packet\n");
   7758 		return;
   7759 	}
   7760 
   7761 	memcpy(&flags, buf, size);
   7762 
   7763 	print_attr(indent, "%s:", str);
   7764 	print_rmnet_flags(indent + 1, "Flags", flags.flags);
   7765 	print_rmnet_flags(indent + 1, "Mask", flags.mask);
   7766 }
   7767 
   7768 static struct attr_entry link_info_data_entry[] = {
   7769 	{ IFLA_RMNET_MUX_ID,	"RMNet Mux Id",		ATTR_U16 },
   7770 	{ IFLA_RMNET_FLAGS,	"RMNet Flags",		ATTR_CUSTOM,
   7771 					{ .function = print_ifla_rmnet_flags } },
   7772 	{ },
   7773 };
   7774 
   7775 static struct attr_entry link_info_entry[] = {
   7776 	{ IFLA_INFO_KIND,	"Kind",		ATTR_STRING },
   7777 	{ IFLA_INFO_DATA,	"Info Data",
   7778 				ATTR_NESTED, { link_info_data_entry } },
   7779 	{ },
   7780 };
   7781 
   7782 static struct attr_entry info_entry[] = {
   7783 	{ IFLA_ADDRESS,		"Interface Address", ATTR_CUSTOM,
   7784 					{ .function = print_ifi_addr } },
   7785 	{ IFLA_BROADCAST,	"Broadcast Address", ATTR_CUSTOM,
   7786 					{ .function = print_ifi_addr } },
   7787 	{ IFLA_IFNAME,		"IfName",	ATTR_STRING },
   7788 	{ IFLA_MASTER,		"Master",	ATTR_U32 },
   7789 	{ IFLA_MTU,		"MTU",		ATTR_U32 },
   7790 	{ IFLA_TXQLEN,		"Txqlen",	ATTR_U32 },
   7791 	{ IFLA_OPERSTATE,	"OperState",	ATTR_CUSTOM,
   7792 					{ .function = print_oper_state } },
   7793 	{ IFLA_LINKMODE,	"LinkMode",	ATTR_CUSTOM,
   7794 					{ .function = print_link_mode } },
   7795 	{ IFLA_LINK,		"Link",		ATTR_S32 },
   7796 	{ IFLA_QDISC,		"Qdisc",	ATTR_STRING },
   7797 	{ IFLA_STATS,		"Stats",	ATTR_BINARY },
   7798 	{ IFLA_MAP,		"Map",		ATTR_BINARY },
   7799 	{ IFLA_WIRELESS,	"Wireless",	ATTR_BINARY },
   7800 	{ IFLA_COST,		"Cost",		ATTR_BINARY },
   7801 	{ IFLA_PRIORITY,	"Priority",	ATTR_BINARY },
   7802 	{ IFLA_PROTINFO,	"ProtInfo",	ATTR_BINARY },
   7803 	{ IFLA_WEIGHT,		"Weight",	ATTR_BINARY },
   7804 	{ IFLA_NET_NS_PID,	"NetNSPid",	ATTR_BINARY },
   7805 	{ IFLA_IFALIAS,		"IFAlias",	ATTR_BINARY },
   7806 	{ IFLA_LINKINFO,	"LinkInfo",
   7807 					ATTR_NESTED, { link_info_entry } },
   7808 	{ },
   7809 };
   7810 
   7811 static struct flag_names ifa_flags[] = {
   7812 	{ IFA_F_SECONDARY, "temporary" },
   7813 	{ IFA_F_NODAD, "nodad" },
   7814 	{ IFA_F_OPTIMISTIC, "optimistic" },
   7815 	{ IFA_F_DADFAILED, "dadfailed" },
   7816 	{ IFA_F_HOMEADDRESS, "homeaddress" },
   7817 	{ IFA_F_DEPRECATED, "deprecated" },
   7818 	{ IFA_F_TENTATIVE, "tentative" },
   7819 	{ IFA_F_PERMANENT, "permanent" },
   7820 	{ IFA_F_MANAGETEMPADDR, "managetempaddr" },
   7821 	{ IFA_F_NOPREFIXROUTE, "noprefixroute" },
   7822 	{ IFA_F_MCAUTOJOIN, "mcautojoin" },
   7823 	{ IFA_F_STABLE_PRIVACY, "stableprivacy" },
   7824 	{ },
   7825 };
   7826 
   7827 static void print_ifa_flags(unsigned int indent, const char *str,
   7828 					const void *buf, uint16_t size)
   7829 {
   7830 	uint32_t flags;
   7831 	char str_flags[1024];
   7832 
   7833 	if (size != 4)
   7834 		return;
   7835 
   7836 	flags = l_get_u32(buf);
   7837 	flags_str(ifa_flags, str_flags, sizeof(str_flags), flags);
   7838 	print_attr(indent, "%s: %s", str, str_flags);
   7839 }
   7840 
   7841 static void print_inet_addr(unsigned int indent, const char *str,
   7842 						const void *buf, uint16_t size)
   7843 {
   7844 	int family;
   7845 	char ip[INET6_ADDRSTRLEN];
   7846 
   7847 	if (size == sizeof(struct in_addr))
   7848 		family = AF_INET;
   7849 	else if (size == sizeof(struct in6_addr))
   7850 		family = AF_INET6;
   7851 	else
   7852 		return;
   7853 
   7854 	inet_ntop(family, buf, ip, sizeof(ip));
   7855 	print_attr(indent, "%s: %s", str, ip);
   7856 }
   7857 
   7858 static void print_cacheinfo(unsigned int indent, const char *str,
   7859 						const void *buf, uint16_t size)
   7860 {
   7861 	const struct ifa_cacheinfo *cinfo = buf;
   7862 
   7863 	print_attr(indent, "%s:", str);
   7864 
   7865 	if (size != sizeof(struct ifa_cacheinfo)) {
   7866 		printf("malformed packet\n");
   7867 		return;
   7868 	}
   7869 
   7870 	if (cinfo->ifa_prefered == 0xffffffffu)
   7871 		print_attr(indent + 1, "ifa_prefered: infinite");
   7872 	else
   7873 		print_attr(indent + 1, "ifa_prefered: %u", cinfo->ifa_prefered);
   7874 
   7875 	if (cinfo->ifa_valid == 0xffffffffu)
   7876 		print_attr(indent + 1, "ifa_valid: infinite");
   7877 	else
   7878 		print_attr(indent + 1, "ifa_valid: %u", cinfo->ifa_valid);
   7879 
   7880 	print_attr(indent + 1, "tstamp: %u, cstamp: %u",
   7881 				cinfo->tstamp, cinfo->cstamp);
   7882 }
   7883 
   7884 static struct attr_entry addr_entry[] = {
   7885 	{ IFA_ADDRESS,		"Interface Address", ATTR_CUSTOM,
   7886 					{ .function = print_inet_addr } },
   7887 	{ IFA_LOCAL,		"Local Address", ATTR_CUSTOM,
   7888 					{ .function = print_inet_addr } },
   7889 	{ IFA_BROADCAST,	"Broadcast Address", ATTR_CUSTOM,
   7890 					{ .function = print_inet_addr } },
   7891 	{ IFA_ANYCAST,		"Anycast Address", ATTR_CUSTOM,
   7892 					{ .function = print_inet_addr } },
   7893 	{ IFA_LABEL,		"Label",	ATTR_STRING },
   7894 	{ IFA_CACHEINFO,	"CacheInfo",	ATTR_CUSTOM,
   7895 					{ .function = print_cacheinfo } },
   7896 	{ IFA_FLAGS,		"Flags", ATTR_CUSTOM,
   7897 					{ .function = print_ifa_flags } },
   7898 	{ },
   7899 };
   7900 
   7901 static const struct attr_entry metrics_table[] = {
   7902 	{ RTAX_MTU,	"MTU",		ATTR_U32 },
   7903 	{ }
   7904 };
   7905 
   7906 static struct attr_entry route_entry[] = {
   7907 	{ RTA_DST,		"Destination Address", ATTR_CUSTOM,
   7908 					{ .function = print_inet_addr } },
   7909 	{ RTA_SRC,		"Source Address", ATTR_CUSTOM,
   7910 					{ .function = print_inet_addr } },
   7911 	{ RTA_IIF,		"Input Interface Index", ATTR_S32 },
   7912 	{ RTA_OIF,		"Output Interface Index", ATTR_S32 },
   7913 	{ RTA_GATEWAY,		"Gateway", ATTR_CUSTOM,
   7914 					{ .function = print_inet_addr } },
   7915 	{ RTA_PRIORITY,		"Priority of the route", ATTR_S32 },
   7916 	{ RTA_TABLE,		"Routing Table", ATTR_U32 },
   7917 	{ RTA_EXPIRES,		"Route lifetime", ATTR_U32 },
   7918 	{ RTA_PREF,		"Route Preference", ATTR_U8 },
   7919 	{ RTA_PREFSRC,		"Preferred Source", ATTR_CUSTOM,
   7920 					{ .function = print_inet_addr } },
   7921 	{ RTA_METRICS,		"Metrics",
   7922 				ATTR_NESTED, { metrics_table } },
   7923 	{ },
   7924 };
   7925 
   7926 static void print_rtnl_attributes(int indent, const struct attr_entry *table,
   7927 						struct rtattr *rt_attr, int len)
   7928 {
   7929 	struct rtattr *attr;
   7930 
   7931 	if (!table || !rt_attr)
   7932 		return;
   7933 
   7934 	for (attr = rt_attr; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
   7935 		uint16_t rta_type = attr->rta_type & NLA_TYPE_MASK;
   7936 		enum attr_type type = ATTR_UNSPEC;
   7937 		attr_func_t function;
   7938 		const struct attr_entry *nested;
   7939 		uint64_t val64;
   7940 		uint32_t val32;
   7941 		uint16_t val16;
   7942 		uint8_t val8;
   7943 		int8_t val_s8;
   7944 		int32_t val_s32;
   7945 		int64_t val_s64;
   7946 		const char *str;
   7947 		int i, payload;
   7948 
   7949 		str = "Reserved";
   7950 
   7951 		for (i = 0; table[i].str; i++) {
   7952 			if (rta_type == table[i].attr) {
   7953 				str = table[i].str;
   7954 				type = table[i].type;
   7955 				function = table[i].function;
   7956 				nested = table[i].nested;
   7957 				break;
   7958 			}
   7959 		}
   7960 
   7961 		payload = RTA_PAYLOAD(attr);
   7962 
   7963 		switch (type) {
   7964 		case ATTR_CUSTOM:
   7965 			if (function)
   7966 				function(indent, str, RTA_DATA(attr), payload);
   7967 			else
   7968 				printf("missing function\n");
   7969 			break;
   7970 		case ATTR_STRING:
   7971 			print_attr(indent, "%s (len:%d): %s", str, payload,
   7972 						(char *) RTA_DATA(attr));
   7973 			break;
   7974 		case ATTR_U8:
   7975 			val8 = *((uint8_t *) RTA_DATA(attr));
   7976 			print_attr(indent, "%s: %"PRIu8" (0x%02"PRIx8")", str,
   7977 								val8, val8);
   7978 			if (payload != 1)
   7979 				printf("malformed packet\n");
   7980 			break;
   7981 		case ATTR_U16:
   7982 			val16 = *((uint16_t *) RTA_DATA(attr));
   7983 			print_attr(indent, "%s: %"PRIu16" (0x%04"PRIx16")", str,
   7984 								val16, val16);
   7985 			if (payload != 2)
   7986 				printf("malformed packet\n");
   7987 			break;
   7988 		case ATTR_U32:
   7989 			val32 = *((uint32_t *) RTA_DATA(attr));
   7990 			print_attr(indent, "%s: %"PRIu32" (0x%08"PRIx32")", str,
   7991 								val32, val32);
   7992 			if (payload != 4)
   7993 				printf("malformed packet\n");
   7994 			break;
   7995 		case ATTR_U64:
   7996 			val64 = *((uint64_t *) RTA_DATA(attr));
   7997 			print_attr(indent, "%s: %"PRIu64" (0x%016"PRIx64")",
   7998 							str, val64, val64);
   7999 			if (payload != 8)
   8000 				printf("malformed packet\n");
   8001 			break;
   8002 		case ATTR_S8:
   8003 			val_s8 = *((int8_t *) RTA_DATA(attr));
   8004 			print_attr(indent, "%s: %"PRId8, str, val_s8);
   8005 			if (payload != 1)
   8006 				printf("malformed packet\n");
   8007 			break;
   8008 		case ATTR_S32:
   8009 			val_s32 = *((int32_t *) RTA_DATA(attr));
   8010 			print_attr(indent, "%s: %"PRId32, str, val_s32);
   8011 			if (payload != 4)
   8012 				printf("malformed packet\n");
   8013 			break;
   8014 		case ATTR_S64:
   8015 			val_s64 = *((int64_t *) RTA_DATA(attr));
   8016 			print_attr(indent, "%s: %"PRId64, str, val_s64);
   8017 			if (payload != 8)
   8018 				printf("malformed packet\n");
   8019 			break;
   8020 		case ATTR_FLAG:
   8021 			print_attr(indent, "%s: true", str);
   8022 			if (payload != 0)
   8023 				printf("malformed packet\n");
   8024 			break;
   8025 		case ATTR_FLAG_OR_U16:
   8026 			if (payload == 0)
   8027 				print_attr(indent, "%s: true", str);
   8028 			else if (payload == 2) {
   8029 				val16 = *((uint16_t *) RTA_DATA(attr));
   8030 				print_attr(indent,
   8031 						"%s: %"PRIu16" (0x%04"PRIx16")",
   8032 							str, val16, val16);
   8033 			} else
   8034 				printf("malformed packet\n");
   8035 			break;
   8036 		case ATTR_NESTED:
   8037 			print_attr(indent, "%s: len %u", str, payload);
   8038 			if (!nested)
   8039 				printf("missing table\n");
   8040 			print_rtnl_attributes(indent + 1, nested,
   8041 						RTA_DATA(attr), payload);
   8042 			break;
   8043 		case ATTR_BINARY:
   8044 			print_attr(indent, "%s: len %d", str, payload);
   8045 			print_hexdump(indent + 1, RTA_DATA(attr), payload);
   8046 			break;
   8047 		case ATTR_ADDRESS:
   8048 		case ATTR_ARRAY:
   8049 		case ATTR_UNSPEC:
   8050 			print_attr(indent, "%s: len %d", str, payload);
   8051 			break;
   8052 		}
   8053 	}
   8054 }
   8055 
   8056 static const char *family_to_string(uint8_t family)
   8057 {
   8058 	switch (family) {
   8059 	case AF_INET:
   8060 		return "AF_INET";
   8061 	case AF_INET6:
   8062 		return "AF_INET6";
   8063 	default:
   8064 		return "Unknown";
   8065 	}
   8066 }
   8067 
   8068 static const char *protocol_to_string(uint8_t proto)
   8069 {
   8070 	switch (proto) {
   8071 	case RTPROT_DHCP:
   8072 		return "dhcp";
   8073 	case RTPROT_RA:
   8074 		return "ra";
   8075 	case RTPROT_KERNEL:
   8076 		return "kernel";
   8077 	case RTPROT_BOOT:
   8078 		return "boot";
   8079 	case RTPROT_STATIC:
   8080 		return "static";
   8081 	default:
   8082 		return "Unknown";
   8083 	}
   8084 }
   8085 
   8086 static void print_ifinfomsg(const struct ifinfomsg *info)
   8087 {
   8088 	static struct flag_names iff_flags[] = {
   8089 		{ IFF_UP, "up" },
   8090 		{ IFF_BROADCAST, "broadcast" },
   8091 		{ IFF_DEBUG, "debug" },
   8092 		{ IFF_LOOPBACK, "loopback" },
   8093 		{ IFF_POINTOPOINT, "pointopoint"},
   8094 		{ IFF_NOTRAILERS, "notrailers" },
   8095 		{ IFF_RUNNING, "running" },
   8096 		{ IFF_NOARP, "noarp" },
   8097 		{ IFF_PROMISC, "promisc" },
   8098 		{ IFF_ALLMULTI, "allmulti" },
   8099 		{ IFF_MASTER, "master" },
   8100 		{ IFF_SLAVE, "slave" },
   8101 		{ IFF_MULTICAST, "multicast" },
   8102 		{ IFF_PORTSEL, "portsel" },
   8103 		{ IFF_AUTOMEDIA, "automedia" },
   8104 		{ IFF_DYNAMIC, "dynamic" },
   8105 		{ },
   8106 	};
   8107 
   8108 	char str[256];
   8109 
   8110 	if (!info)
   8111 		return;
   8112 
   8113 	print_field("IFLA Family: %s", family_to_string(info->ifi_family));
   8114 	print_field("IFLA Type: %u", info->ifi_type);
   8115 	print_field("IFLA Index: %d", info->ifi_index);
   8116 	print_field("IFLA ChangeMask: %u", info->ifi_change);
   8117 	flags_str(iff_flags, str, sizeof(str), info->ifi_flags);
   8118 	print_field("IFLA Flags: %s", str);
   8119 }
   8120 
   8121 static void print_ifaddrmsg(const struct ifaddrmsg *addr)
   8122 {
   8123 	char str[1024];
   8124 
   8125 	if (!addr)
   8126 		return;
   8127 
   8128 	print_field("IFA Family: %s", family_to_string(addr->ifa_family));
   8129 	print_field("IFA Prefixlen: %u", addr->ifa_prefixlen);
   8130 	print_field("IFA Index: %d", addr->ifa_index);
   8131 	print_field("IFA Scope: %s", scope_to_string(addr->ifa_scope));
   8132 	flags_str(ifa_flags, str, sizeof(str), addr->ifa_flags);
   8133 	print_field("IFA Flags: %s", str);
   8134 }
   8135 
   8136 static void print_rtmsg(const struct rtmsg *msg)
   8137 {
   8138 	static struct flag_names rtm_flags[] = {
   8139 		{ RTM_F_NOTIFY, "notify" },
   8140 		{ RTM_F_CLONED, "cloned" },
   8141 		{ RTM_F_EQUALIZE, "multipath-equalizer" },
   8142 		{ },
   8143 	};
   8144 	char str[256];
   8145 
   8146 	print_field("RTM Family: %s", family_to_string(msg->rtm_family));
   8147 	print_field("RTM Destination Len: %hhu", msg->rtm_dst_len);
   8148 	print_field("RTM Source Len: %hhu", msg->rtm_src_len);
   8149 	print_field("RTM TOS Field: %hhu", msg->rtm_tos);
   8150 	print_field("RTM Table: %hhu", msg->rtm_table);
   8151 	print_field("RTM Protocol: %s",
   8152 			protocol_to_string(msg->rtm_protocol));
   8153 	print_field("RTM Scope: %s", scope_to_string(msg->rtm_scope));
   8154 	print_field("RTM Type: %hhu", msg->rtm_type);
   8155 	flags_str(rtm_flags, str, sizeof(str), msg->rtm_flags);
   8156 	print_field("RTM Flags: %s", str);
   8157 }
   8158 
   8159 static void read_uevent(const char *ifname, int index)
   8160 {
   8161 	char filename[64], line[128];
   8162 	FILE *f;
   8163 
   8164 	snprintf(filename, sizeof(filename), "/sys/class/net/%s/uevent",
   8165 								ifname);
   8166 	f = fopen(filename, "re");
   8167 	if (!f) {
   8168 		printf("%s do not exist\n", filename);
   8169 		return;
   8170 	}
   8171 
   8172 	while (fgets(line, sizeof(line), f)) {
   8173 		char *pos;
   8174 
   8175 		pos = strchr(line, '\n');
   8176 		if (!pos)
   8177 			continue;
   8178 		pos[0] = '\0';
   8179 
   8180 		if (strncmp(line, "DEVTYPE=", 8) != 0)
   8181 			continue;
   8182 
   8183 		if (strcmp(line + 8, "wlan") == 0) {
   8184 			struct wlan_iface *iface;
   8185 
   8186 			iface = l_new(struct wlan_iface, 1);
   8187 			iface->index = index;
   8188 
   8189 			if (!l_hashmap_insert(wlan_iface_list,
   8190 					L_INT_TO_PTR(index), iface))
   8191 				l_free(iface);
   8192 		}
   8193 	}
   8194 
   8195 	fclose(f);
   8196 }
   8197 
   8198 static char *rtnl_get_ifname(const struct ifinfomsg *ifi, int len)
   8199 {
   8200 	struct rtattr *attr;
   8201 	char *ifname = NULL;
   8202 
   8203 	if (!ifi)
   8204 		return NULL;
   8205 
   8206 	for (attr = IFLA_RTA(ifi); RTA_OK(attr, len);
   8207 					attr = RTA_NEXT(attr, len))
   8208 		if (attr->rta_type == IFLA_IFNAME)
   8209 			ifname = (char *) RTA_DATA(attr);
   8210 
   8211 	return ifname;
   8212 }
   8213 
   8214 static void print_rtm_link(uint16_t type, const struct ifinfomsg *info, int len)
   8215 {
   8216 	struct wlan_iface *iface;
   8217 	char *ifname;
   8218 
   8219 	if (!info || len <= 0)
   8220 		return;
   8221 
   8222 	if (type == RTM_NEWLINK) {
   8223 		ifname = rtnl_get_ifname(info, len);
   8224 		if (!ifname)
   8225 			return;
   8226 
   8227 		read_uevent(ifname, info->ifi_index);
   8228 	}
   8229 
   8230 	iface = l_hashmap_lookup(wlan_iface_list,
   8231 						L_INT_TO_PTR(info->ifi_index));
   8232 	if (!iface)
   8233 		return;
   8234 
   8235 	print_ifinfomsg(info);
   8236 	print_rtnl_attributes(1, info_entry, IFLA_RTA(info), len);
   8237 
   8238 	if (type == RTM_DELLINK) {
   8239 		iface = l_hashmap_remove(wlan_iface_list,
   8240 						L_INT_TO_PTR(info->ifi_index));
   8241 		if (!iface)
   8242 			return;
   8243 
   8244 		l_free(iface);
   8245 	}
   8246 }
   8247 
   8248 static void print_rtm_route(uint16_t type, const struct rtmsg *msg, size_t len)
   8249 {
   8250 	if (!msg || len < sizeof(struct rtmsg))
   8251 		return;
   8252 
   8253 	print_rtmsg(msg);
   8254 	print_rtnl_attributes(1, route_entry, RTM_RTA(msg), len);
   8255 }
   8256 
   8257 static void print_rtm_addr(uint16_t type, const struct ifaddrmsg *msg,
   8258 								size_t len)
   8259 {
   8260 	if (!msg || len < sizeof(struct ifaddrmsg))
   8261 		return;
   8262 
   8263 	print_ifaddrmsg(msg);
   8264 	print_rtnl_attributes(1, addr_entry, IFA_RTA(msg), len);
   8265 }
   8266 
   8267 static const char *nlmsg_type_to_str(uint32_t msg_type)
   8268 {
   8269 	const char *str = NULL;
   8270 
   8271 	switch (msg_type) {
   8272 	case NLMSG_NOOP:
   8273 		str = "Noop";
   8274 		break;
   8275 	case NLMSG_ERROR:
   8276 		str = "Error";
   8277 		break;
   8278 	case NLMSG_DONE:
   8279 		str = "Done";
   8280 		break;
   8281 	case NLMSG_OVERRUN:
   8282 		str = "Overrun";
   8283 		break;
   8284 	case RTM_NEWLINK:
   8285 		str = "New Link";
   8286 		break;
   8287 	case RTM_DELLINK:
   8288 		str = "Delete Link";
   8289 		break;
   8290 	case RTM_GETLINK:
   8291 		str = "Get Link";
   8292 		break;
   8293 	case RTM_SETLINK:
   8294 		str = "Set Link";
   8295 		break;
   8296 	case RTM_NEWADDR:
   8297 		str = "New Address";
   8298 		break;
   8299 	case RTM_DELADDR:
   8300 		str = "Delete Address";
   8301 		break;
   8302 	case RTM_GETADDR:
   8303 		str = "Get Address";
   8304 		break;
   8305 	case RTM_NEWROUTE:
   8306 		str = "New Route";
   8307 		break;
   8308 	case RTM_DELROUTE:
   8309 		str = "Delete Route";
   8310 		break;
   8311 	case RTM_GETROUTE:
   8312 		str = "Get Route";
   8313 		break;
   8314 	default:
   8315 		str = "Reserved";
   8316 		break;
   8317 	}
   8318 
   8319 	return str;
   8320 }
   8321 
   8322 static void print_nlmsghdr(const struct timeval *tv,
   8323 						const struct nlmsghdr *nlmsg)
   8324 {
   8325 	char extra_str[256];
   8326 	const char *str;
   8327 	bool out;
   8328 
   8329 	str = nlmsg_type_to_str(nlmsg->nlmsg_type);
   8330 	out = !!(nlmsg->nlmsg_flags & NLM_F_REQUEST);
   8331 
   8332 	netlink_str(extra_str, sizeof(extra_str), nlmsg->nlmsg_type,
   8333 				nlmsg->nlmsg_flags, NLMSG_PAYLOAD(nlmsg, 0));
   8334 
   8335 	print_packet(tv, out ? '<' : '>', COLOR_YELLOW, "RTNL", str, extra_str);
   8336 
   8337 	print_field("Flags: %hu (0x%03x)", nlmsg->nlmsg_flags,
   8338 							nlmsg->nlmsg_flags);
   8339 	print_field("Sequence number: %u (0x%08x)",
   8340 					nlmsg->nlmsg_seq, nlmsg->nlmsg_seq);
   8341 	print_field("Port ID: %u", nlmsg->nlmsg_pid);
   8342 }
   8343 
   8344 static void print_nlmsg(const struct timeval *tv, const struct nlmsghdr *nlmsg)
   8345 {
   8346 	struct nlmsgerr *err;
   8347 	int status;
   8348 
   8349 	print_nlmsghdr(tv, nlmsg);
   8350 
   8351 	switch (nlmsg->nlmsg_type) {
   8352 	case NLMSG_ERROR:
   8353 		err = NLMSG_DATA(nlmsg);
   8354 		status = err->error;
   8355 		if (status < 0)
   8356 			print_field("Error: %d (%s)",
   8357 						status, strerror(-status));
   8358 		else
   8359 			print_field("ACK: %d", status);
   8360 		break;
   8361 
   8362 	case NLMSG_DONE:
   8363 		status = *((int *) NLMSG_DATA(nlmsg));
   8364 		print_field("Status: %d", status);
   8365 		break;
   8366 
   8367 	case NLMSG_NOOP:
   8368 	case NLMSG_OVERRUN:
   8369 		break;
   8370 	}
   8371 }
   8372 
   8373 static void print_rtnl_msg(const struct timeval *tv,
   8374 						const struct nlmsghdr *nlmsg)
   8375 {
   8376 	struct ifinfomsg *info;
   8377 	struct ifaddrmsg *addr;
   8378 	struct rtmsg *rtm;
   8379 	struct wlan_iface *iface;
   8380 	int len;
   8381 
   8382 	switch (nlmsg->nlmsg_type) {
   8383 	case RTM_NEWLINK:
   8384 	case RTM_DELLINK:
   8385 	case RTM_SETLINK:
   8386 	case RTM_GETLINK:
   8387 		info = (struct ifinfomsg *) NLMSG_DATA(nlmsg);
   8388 		len = IFLA_PAYLOAD(nlmsg);
   8389 		print_nlmsghdr(tv, nlmsg);
   8390 		print_rtm_link(nlmsg->nlmsg_type, info, len);
   8391 		break;
   8392 
   8393 	case RTM_NEWROUTE:
   8394 	case RTM_GETROUTE:
   8395 	case RTM_DELROUTE:
   8396 		rtm = (struct rtmsg *) NLMSG_DATA(nlmsg);
   8397 		len = RTM_PAYLOAD(nlmsg);
   8398 
   8399 		if (!rtm || len <= (int) sizeof(struct rtmsg))
   8400 			return;
   8401 
   8402 		/* Skip 'kernel' and 'default' tables */
   8403 		if (rtm->rtm_table == RT_TABLE_LOCAL ||
   8404 				rtm->rtm_table == RT_TABLE_DEFAULT)
   8405 			return;
   8406 
   8407 		print_nlmsghdr(tv, nlmsg);
   8408 		print_rtm_route(nlmsg->nlmsg_type, rtm, len);
   8409 		break;
   8410 
   8411 	case RTM_NEWADDR:
   8412 	case RTM_DELADDR:
   8413 	case RTM_GETADDR:
   8414 		addr = (struct ifaddrmsg *) NLMSG_DATA(nlmsg);
   8415 		len = IFA_PAYLOAD(nlmsg);
   8416 		if (!addr || len <= 0)
   8417 			return;
   8418 
   8419 		iface = l_hashmap_lookup(wlan_iface_list,
   8420 						L_INT_TO_PTR(addr->ifa_index));
   8421 		if (!iface)
   8422 			return;
   8423 
   8424 		print_nlmsghdr(tv, nlmsg);
   8425 		print_rtm_addr(nlmsg->nlmsg_type, addr, len);
   8426 		break;
   8427 	}
   8428 }
   8429 
   8430 void nlmon_print_rtnl(struct nlmon *nlmon, const struct timeval *tv,
   8431 					const void *data, uint32_t size)
   8432 {
   8433 	int64_t aligned_size = NLMSG_ALIGN(size);
   8434 	const struct nlmsghdr *nlmsg;
   8435 
   8436 	if (nlmon->nortnl)
   8437 		return;
   8438 
   8439 	update_time_offset(tv, nlmon->time_format);
   8440 
   8441 	for (nlmsg = data; NLMSG_OK(nlmsg, aligned_size);
   8442 				nlmsg = NLMSG_NEXT(nlmsg, aligned_size)) {
   8443 		store_netlink(nlmon, tv, NETLINK_ROUTE, nlmsg);
   8444 
   8445 		if (nlmon->nortnl)
   8446 			continue;
   8447 
   8448 		switch (nlmsg->nlmsg_type) {
   8449 		case NLMSG_NOOP:
   8450 		case NLMSG_OVERRUN:
   8451 		case NLMSG_ERROR:
   8452 		case NLMSG_DONE:
   8453 			print_nlmsg(tv, nlmsg);
   8454 			break;
   8455 
   8456 		case RTM_NEWLINK:
   8457 		case RTM_DELLINK:
   8458 		case RTM_SETLINK:
   8459 		case RTM_GETLINK:
   8460 		case RTM_NEWADDR:
   8461 		case RTM_DELADDR:
   8462 		case RTM_GETADDR:
   8463 		case RTM_NEWROUTE:
   8464 		case RTM_GETROUTE:
   8465 		case RTM_DELROUTE:
   8466 			print_rtnl_msg(tv, nlmsg);
   8467 			break;
   8468 		}
   8469 	}
   8470 }
   8471 
   8472 void nlmon_print_genl(struct nlmon *nlmon, const struct timeval *tv,
   8473 					const void *data, int64_t size)
   8474 {
   8475 	const struct nlmsghdr *nlmsg;
   8476 
   8477 	update_time_offset(tv, nlmon->time_format);
   8478 
   8479 	for (nlmsg = data; NLMSG_OK(nlmsg, size);
   8480 				nlmsg = NLMSG_NEXT(nlmsg, size)) {
   8481 		if (nlmsg->nlmsg_type == GENL_ID_CTRL) {
   8482 			store_message(nlmon, tv, nlmsg);
   8483 			continue;
   8484 		}
   8485 
   8486 		if (nlmsg->nlmsg_type >= NLMSG_MIN_TYPE && !nlmon->read &&
   8487 				nlmsg->nlmsg_type != nlmon->id)
   8488 			continue;
   8489 
   8490 		nlmon_message(nlmon, tv, nlmsg);
   8491 	}
   8492 }
   8493 
   8494 void nlmon_print_pae(struct nlmon *nlmon, const struct timeval *tv,
   8495 					uint8_t type, int index,
   8496 					const void *data, uint32_t size)
   8497 {
   8498 	char extra_str[16];
   8499 
   8500 	update_time_offset(tv, nlmon->time_format);
   8501 
   8502 	sprintf(extra_str, "len %u", size);
   8503 
   8504 	print_packet(tv, (type == PACKET_HOST) ? '>' : '<',
   8505 					COLOR_YELLOW, "PAE", extra_str, "");
   8506 	if (index >= 0)
   8507 		print_attr(0, "Interface Index: %u", index);
   8508 
   8509 	print_eapol(0, "EAPoL", data, size);
   8510 }
   8511 
   8512 static bool pae_receive(struct l_io *io, void *user_data)
   8513 {
   8514 	struct nlmon *nlmon = user_data;
   8515 	struct msghdr msg;
   8516 	struct sockaddr_ll sll;
   8517 	struct iovec iov;
   8518 	struct cmsghdr *cmsg;
   8519 	struct timeval copy_tv;
   8520 	const struct timeval *tv = NULL;
   8521 	unsigned char buf[8192];
   8522 	unsigned char control[32];
   8523 	ssize_t bytes_read;
   8524 	int fd;
   8525 
   8526 	fd = l_io_get_fd(io);
   8527 	if (fd < 0)
   8528 		return false;
   8529 
   8530 	memset(&sll, 0, sizeof(sll));
   8531 
   8532 	memset(&iov, 0, sizeof(iov));
   8533 	iov.iov_base = buf;
   8534 	iov.iov_len = sizeof(buf);
   8535 
   8536 	memset(&msg, 0, sizeof(msg));
   8537 	msg.msg_name = &sll;
   8538 	msg.msg_namelen = sizeof(sll);
   8539 	msg.msg_iov = &iov;
   8540 	msg.msg_iovlen = 1;
   8541 	msg.msg_control = control;
   8542 	msg.msg_controllen = sizeof(control);
   8543 
   8544 	bytes_read = recvmsg(fd, &msg, 0);
   8545 	if (bytes_read < 0) {
   8546 		if (errno != EAGAIN && errno != EINTR)
   8547 			return false;
   8548 
   8549 		return true;
   8550 	}
   8551 
   8552 	if (sll.sll_hatype != ARPHRD_ETHER)
   8553 		return true;
   8554 
   8555 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
   8556 				cmsg = CMSG_NXTHDR(&msg, cmsg)) {
   8557 		if (cmsg->cmsg_level == SOL_SOCKET &&
   8558 					cmsg->cmsg_type == SCM_TIMESTAMP) {
   8559 			memcpy(&copy_tv, CMSG_DATA(cmsg), sizeof(copy_tv));
   8560 			tv = &copy_tv;
   8561 		}
   8562 	}
   8563 
   8564 	store_packet(nlmon, tv, sll.sll_pkttype, ARPHRD_ETHER,
   8565 				ntohs(sll.sll_protocol), buf, bytes_read);
   8566 
   8567 	nlmon_print_pae(nlmon, tv, sll.sll_pkttype, sll.sll_ifindex,
   8568 							buf, bytes_read);
   8569 
   8570 	return true;
   8571 }
   8572 
   8573 /*
   8574  * BPF filter to match skb->dev->type == 1 (ARPHRD_ETHER) and
   8575  * match skb->protocol == 0x888e (PAE) or 0x88c7 (preauthentication).
   8576  */
   8577 static struct sock_filter pae_filter[] = {
   8578 	{ 0x28,  0,  0, 0xfffff01c },	/* ldh #hatype		*/
   8579 	{ 0x15,  0,  4, 0x00000001 },	/* jne #1, drop		*/
   8580 	{ 0x28,  0,  0, 0xfffff000 },	/* ldh #proto		*/
   8581 	{ 0x15,  1,  0, 0x0000888e },	/* je  #0x888e, keep	*/
   8582 	{ 0x15,  0,  1, 0x000088c7 },	/* jne #0x88c7, drop	*/
   8583 	{ 0x06,  0,  0, 0xffffffff },	/* keep: ret #-1	*/
   8584 	{ 0x06,  0,  0, 0000000000 },	/* drop: ret #0		*/
   8585 };
   8586 
   8587 static const struct sock_fprog pae_fprog = { .len = 7, .filter = pae_filter };
   8588 
   8589 static struct l_io *open_pae(void)
   8590 {
   8591 	struct l_io *io;
   8592 	int fd, opt = 1;
   8593 
   8594 	fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
   8595 							htons(ETH_P_ALL));
   8596 	if (fd < 0) {
   8597 		perror("Failed to create authentication socket");
   8598 		return NULL;
   8599 	}
   8600 
   8601 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER,
   8602 					&pae_fprog, sizeof(pae_fprog)) < 0) {
   8603 		perror("Failed to enable authentication filter");
   8604 		close(fd);
   8605 		return NULL;
   8606 	}
   8607 
   8608 	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
   8609 		perror("Failed to enable authentication timestamps");
   8610 		close(fd);
   8611 		return NULL;
   8612 	}
   8613 
   8614 	io = l_io_new(fd);
   8615 
   8616 	l_io_set_close_on_destroy(io, true);
   8617 
   8618 	return io;
   8619 }
   8620 
   8621 struct nlmon *nlmon_open(uint16_t id, const char *pathname,
   8622 				const struct nlmon_config *config)
   8623 {
   8624 	struct nlmon *nlmon;
   8625 	struct l_io *pae_io;
   8626 	struct pcap *pcap;
   8627 	char path[PATH_MAX];
   8628 
   8629 	pae_io = open_pae();
   8630 	if (!pae_io)
   8631 		return NULL;
   8632 
   8633 	if (pathname) {
   8634 		if (config->pcap_file_count > 1)
   8635 			next_pcap_name(path, sizeof(path), pathname,
   8636 					0, config->pcap_file_count);
   8637 		else
   8638 			snprintf(path, sizeof(path), "%s", pathname);
   8639 
   8640 		pcap = pcap_create(path);
   8641 		if (!pcap) {
   8642 			l_io_destroy(pae_io);
   8643 			return NULL;
   8644 		}
   8645 	} else
   8646 		pcap = NULL;
   8647 
   8648 
   8649 	nlmon = nlmon_create(id, config);
   8650 
   8651 	nlmon->pae_io = pae_io;
   8652 	nlmon->pcap = pcap;
   8653 	nlmon->file_prefix = l_strdup(pathname);
   8654 
   8655 	l_io_set_read_handler(nlmon->pae_io, pae_receive, nlmon, NULL);
   8656 
   8657 	wlan_iface_list = l_hashmap_new();
   8658 
   8659 	return nlmon;
   8660 }
   8661 
   8662 void nlmon_close(struct nlmon *nlmon)
   8663 {
   8664 	if (!nlmon)
   8665 		return;
   8666 
   8667 	l_io_destroy(nlmon->pae_io);
   8668 	l_queue_destroy(nlmon->req_list, nlmon_req_free);
   8669 
   8670 	l_hashmap_destroy(wlan_iface_list, wlan_iface_list_free);
   8671 	wlan_iface_list = NULL;
   8672 
   8673 	if (nlmon->pcap)
   8674 		pcap_close(nlmon->pcap);
   8675 
   8676 	l_free(nlmon->file_prefix);
   8677 
   8678 	l_free(nlmon);
   8679 }