summaryrefslogtreecommitdiffstats
path: root/netlink/bitset.c
blob: 10ce8e9def9a0bd3c8965d8e72a8ffaff2804e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * bitset.h - netlink bitset helpers
 *
 * Functions for easier handling of ethtool netlink bitset attributes.
 */

#include <stdio.h>
#include <errno.h>

#include "../common.h"
#include "netlink.h"
#include "bitset.h"

uint32_t bitset_get_count(const struct nlattr *bitset, int *retptr)
{
	const struct nlattr *attr;

	mnl_attr_for_each_nested(attr, bitset) {
		if (mnl_attr_get_type(attr) != ETHTOOL_A_BITSET_SIZE)
			continue;
		*retptr = 0;
		return mnl_attr_get_u32(attr);
	}

	*retptr = -EFAULT;
	return 0;
}

bool bitset_is_compact(const struct nlattr *bitset)
{
	const struct nlattr *attr;

	mnl_attr_for_each_nested(attr, bitset) {
		switch(mnl_attr_get_type(attr)) {
		case ETHTOOL_A_BITSET_BITS:
			return 0;
		case ETHTOOL_A_BITSET_VALUE:
		case ETHTOOL_A_BITSET_MASK:
			return 1;
		}
	}

	return false;
}

bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
		    int *retptr)
{
	const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(bitset_tb);
	const struct nlattr *bits;
	const struct nlattr *bit;
	bool nomask;
	int ret;

	*retptr = 0;
	ret = mnl_attr_parse_nested(bitset, attr_cb, &bitset_tb_info);
	if (ret < 0)
		goto err;

	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
	if (mask && nomask) {
		/* Trying to determine if a bit is set in the mask of a "no
		 * mask" bitset doesn't make sense.
		 */
		ret = -EFAULT;
		goto err;
	}

	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
	if (bits) {
		const uint32_t *bitmap =
			(const uint32_t *)mnl_attr_get_payload(bits);

		if (idx >= 8 * mnl_attr_get_payload_len(bits))
			return false;
		return bitmap[idx / 32] & (1U << (idx % 32));
	}

	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
	if (!bits)
		goto err;
	mnl_attr_for_each_nested(bit, bits) {
		const struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1] = {};
		DECLARE_ATTR_TB_INFO(tb);
		unsigned int my_idx;

		if (mnl_attr_get_type(bit) != ETHTOOL_A_BITSET_BITS_BIT)
			continue;
		ret = mnl_attr_parse_nested(bit, attr_cb, &tb_info);
		if (ret < 0)
			goto err;
		ret = -EFAULT;
		if (!tb[ETHTOOL_A_BITSET_BIT_INDEX])
			goto err;

		my_idx = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
		if (my_idx == idx)
			return mask || nomask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
	}

	return false;
err:
	fprintf(stderr, "malformed netlink message (bitset)\n");
	*retptr = ret;
	return false;
}

bool bitset_is_empty(const struct nlattr *bitset, bool mask, int *retptr)
{
	const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(bitset_tb);
	const struct nlattr *bits;
	const struct nlattr *bit;
	int ret;

	*retptr = 0;
	ret = mnl_attr_parse_nested(bitset, attr_cb, &bitset_tb_info);
	if (ret < 0)
		goto err;

	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
	if (bits) {
		const uint32_t *bitmap =
			(const uint32_t *)mnl_attr_get_payload(bits);
		unsigned int n = mnl_attr_get_payload_len(bits);
		unsigned int i;

		ret = -EFAULT;
		if (n % 4)
			goto err;
		for (i = 0; i < n / 4; i++)
			if (bitmap[i])
				return false;
		return true;
	}

	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
	if (!bits)
		goto err;
	mnl_attr_for_each_nested(bit, bits) {
		const struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1] = {};
		DECLARE_ATTR_TB_INFO(tb);

		if (mnl_attr_get_type(bit) != ETHTOOL_A_BITSET_BITS_BIT)
			continue;
		if (mask || bitset_tb[ETHTOOL_A_BITSET_NOMASK])
			return false;

		ret = mnl_attr_parse_nested(bit, attr_cb, &tb_info);
		if (ret < 0)
			goto err;
		if (tb[ETHTOOL_A_BITSET_BIT_VALUE])
			return false;
	}

	return true;
err:
	fprintf(stderr, "malformed netlink message (bitset)\n");
	*retptr = ret;
	return true;
}

static uint32_t *get_compact_bitset_attr(const struct nlattr *bitset,
					 uint16_t type)
{
	const struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	unsigned int count;
	int ret;

	ret = mnl_attr_parse_nested(bitset, attr_cb, &tb_info);
	if (ret < 0)
		return NULL;
	if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE] ||
	    !tb[type])
		return NULL;
	count = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
	if (8 * mnl_attr_get_payload_len(tb[type]) < count)
		return NULL;

	return mnl_attr_get_payload(tb[type]);
}

uint32_t *get_compact_bitset_value(const struct nlattr *bitset)
{
	return get_compact_bitset_attr(bitset, ETHTOOL_A_BITSET_VALUE);
}

uint32_t *get_compact_bitset_mask(const struct nlattr *bitset)
{
	return get_compact_bitset_attr(bitset, ETHTOOL_A_BITSET_MASK);
}

int walk_bitset(const struct nlattr *bitset, const struct stringset *labels,
		bitset_walk_callback cb, void *data)
{
	const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(bitset_tb);
	const struct nlattr *bits;
	const struct nlattr *bit;
	bool is_list;
	int ret;

	ret = mnl_attr_parse_nested(bitset, attr_cb, &bitset_tb_info);
	if (ret < 0)
		return ret;
	is_list = bitset_tb[ETHTOOL_A_BITSET_NOMASK];

	bits = bitset_tb[ETHTOOL_A_BITSET_VALUE];
	if (bits) {
		const struct nlattr *mask = bitset_tb[ETHTOOL_A_BITSET_MASK];
		unsigned int count, nwords, idx;
		uint32_t *val_bm;
		uint32_t *mask_bm;

		if (!bitset_tb[ETHTOOL_A_BITSET_SIZE])
			return -EFAULT;
		count = mnl_attr_get_u32(bitset_tb[ETHTOOL_A_BITSET_SIZE]);
		nwords = (count + 31) / 32;
		if ((mnl_attr_get_payload_len(bits) / 4 < nwords) ||
		    (mask && mnl_attr_get_payload_len(mask) / 4 < nwords))
			return -EFAULT;

		val_bm = mnl_attr_get_payload(bits);
		mask_bm = mask ? mnl_attr_get_payload(mask) : NULL;
		for (idx = 0; idx < count; idx++)
			if (!mask_bm || (mask_bm[idx / 32] & (1 << (idx % 32))))
				cb(idx, get_string(labels, idx),
				   val_bm[idx / 32] & (1 << (idx % 32)), data);
		return 0;
	}

	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
	if (!bits)
		return -EFAULT;
	mnl_attr_for_each_nested(bit, bits) {
		const struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1] = {};
		DECLARE_ATTR_TB_INFO(tb);
		const char *name;
		unsigned int idx;

		if (mnl_attr_get_type(bit) != ETHTOOL_A_BITSET_BITS_BIT)
			continue;

		ret = mnl_attr_parse_nested(bit, attr_cb, &tb_info);
		if (ret < 0 || !tb[ETHTOOL_A_BITSET_BIT_INDEX] ||
		    !tb[ETHTOOL_A_BITSET_BIT_NAME])
			return -EFAULT;

		idx = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
		name = mnl_attr_get_str(tb[ETHTOOL_A_BITSET_BIT_NAME]);
		cb(idx, name, is_list || tb[ETHTOOL_A_BITSET_BIT_VALUE], data);
	}

	return 0;
}