summaryrefslogtreecommitdiffstats
path: root/src/attr.c
blob: d4a88f63ae8802d5729174beea74a7cd417c2cb5 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * (C) 2008-2010 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <libmnl/libmnl.h>
#include <string.h>

/*
 * Netlink Type-Length-Value (TLV) attribute:
 *
 *  |<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
 *  -------------------------------------------------
 *  |     length    |      type     |      value     |
 *  -------------------------------------------------
 *  |<--------- header ------------>|<-- payload --->|
 */

/**
 * mnl_attr_get_type - get the attribute type of a netlink message
 * @attr: pointer to netlink attribute
 *
 * This function returns the attribute type.
 */
u_int16_t mnl_attr_get_type(const struct nlattr *attr)
{
	return attr->nla_type & NLA_TYPE_MASK;
}

/**
 * mnl_attr_get_len - get the attribute length
 * @attr: pointer to netlink attribute
 *
 * This function returns the attribute length, including the attribute header.
 */
u_int16_t mnl_attr_get_len(const struct nlattr *attr)
{
	return attr->nla_len;
}

/**
 * mnl_attr_get_payload_len - get the attribute payload-value length
 * @attr: pointer to netlink attribute
 *
 * This function returns the attribute payload-value length.
 */
u_int16_t mnl_attr_get_payload_len(const struct nlattr *attr)
{
	return attr->nla_len - MNL_ATTR_HDRLEN;
}

/**
 * mnl_attr_get_data - get pointer to the attribute payload
 *
 * This function return a pointer to the attribute payload
 */
void *mnl_attr_get_data(const struct nlattr *attr)
{
	return (void *)attr + MNL_ATTR_HDRLEN;
}

/**
 * mnl_attr_ok - check a there is room for an attribute in a buffer
 * @nlh: attribute that we want to check
 * @len: remaining bytes in a buffer that contains the attribute
 *
 * This function is used to check that a buffer that contains an attribute
 * has enough room for the attribute that it stores, ie. this function can
 * be used to verify that an attribute is neither malformed nor truncated.
 */
int mnl_attr_ok(const struct nlattr *attr, int len)
{
	return len >= sizeof(struct nlattr) &&
	       attr->nla_len >= sizeof(struct nlattr) &&
	       attr->nla_len <= len;
}

/**
 * mnl_attr_next - get the next attribute in the payload of a netlink message
 * @attr: pointer to the current attribute
 * @len: pointer to the current remaining bytes in the buffer
 *
 * This function returns a pointer to the next attribute that is in the
 * payload of a netlink message.
 */
struct nlattr *mnl_attr_next(const struct nlattr *attr, int *len)
{
	*len -= mnl_align(attr->nla_len);
	return (struct nlattr *)((void *)attr + mnl_align(attr->nla_len));
}

/**
 * mnl_attr_parse_at_offset - returns an array of attributes from offset
 * @nlh: pointer to netlink message
 * @offset: offset to start parse from
 * @tb: array of pointers to the attribute found
 * @max: size of the attribute array
 *
 * This functions zeroes the array of pointers. Thus, you don't need to
 * initialize this array.
 *
 * This function returns an array of pointers to the attributes that has been
 * found in a netlink payload. This function return 0 on sucess, and >0 to
 * indicate the number of bytes the remaining bytes.
 */
int mnl_attr_parse_at_offset(const struct nlmsghdr *nlh, int offset,
			     struct nlattr *tb[], int max)
{
	struct nlattr *attr = mnl_nlmsg_get_data_offset(nlh, offset);
	int len = mnl_nlmsg_get_len(nlh);

	memset(tb, 0, sizeof(struct nlattr *) * (max + 1));

	while (mnl_attr_ok(attr, len)) {
		if (mnl_attr_get_type(attr) <= max)
			tb[mnl_attr_get_type(attr)] = attr;
		attr = mnl_attr_next(attr, &len);
	}
	return len;
}

/**
 * mnl_attr_parse - returns an array with the attributes in the netlink message
 * @nlh: pointer to netlink message header
 * @tb: array of pointers to the attribute found
 * @max: size of the attribute array
 *
 * This functions zeroes the array of pointers. Thus, you don't need to
 * initialize this array.
 *
 * This function returns an array of pointers to the attributes that has been
 * found in a netlink payload. This function return 0 on sucess, and >0 to
 * indicate the number of bytes the remaining bytes.
 */
int mnl_attr_parse(const struct nlmsghdr *nlh, struct nlattr *tb[], int max)
{
	return mnl_attr_parse_at_offset(nlh, 0, tb, max);
}

/**
 * mnl_attr_parse_nested - returns an array with the attributes from nested
 * @nested: pointer to netlink attribute that contains a nest
 * @tb: array of pointers to the attribute found
 * @max: size of the attribute array
 *
 * This functions zeroes the array of pointers. Thus, you don't need to
 * initialize this array.
 *
 * This function returns an array of pointers to the attributes that has been
 * found in a netlink payload. This function return 0 on sucess, and >0 to
 * indicate the number of bytes the remaining bytes.
 */
int mnl_attr_parse_nested(const struct nlattr *nested,
			  struct nlattr *tb[], int max)
{
	struct nlattr *attr = mnl_attr_get_data(nested);
	int len = mnl_attr_get_payload_len(nested);

	memset(tb, 0, sizeof(struct nlattr *) * (max + 1));

	while (mnl_attr_ok(attr, len)) {
		if (mnl_attr_get_type(attr) <= max)
			tb[mnl_attr_get_type(attr)] = attr;
		attr = mnl_attr_next(attr, &len);
	}
	return len;
}

/**
 * mnl_attr_get_u8 - returns 8-bit unsigned integer attribute.
 * @attr: pointer to netlink attribute
 *
 * This function returns the 8-bit value of a netlink attribute.
 */
u_int8_t mnl_attr_get_u8(const struct nlattr *attr)
{
	return *((u_int8_t *)mnl_attr_get_data(attr));
}

/**
 * mnl_attr_get_u16 - returns 16-bit unsigned integer attribute.
 * @attr: pointer to netlink attribute
 *
 * This function returns the 16-bit value of a netlink attribute.
 */
u_int16_t mnl_attr_get_u16(const struct nlattr *attr)
{
	return *((u_int16_t *)mnl_attr_get_data(attr));
}

/**
 * mnl_attr_get_u32 - returns 32-bit unsigned integer attribute.
 * @attr: pointer to netlink attribute
 *
 * This function returns the 32-bit value of a netlink attribute.
 */
u_int32_t mnl_attr_get_u32(const struct nlattr *attr)
{
	return *((u_int32_t *)mnl_attr_get_data(attr));
}

/**
 * mnl_attr_get_u64 - returns 64-bit unsigned integer attribute.
 * @attr: pointer to netlink attribute
 *
 * This function returns the payload of a 64-bit attribute. This function
 * is align-safe since accessing 64-bit Netlink attributes is a common
 * source of alignment issues.
 */
u_int64_t mnl_attr_get_u64(const struct nlattr *attr)
{
	u_int64_t tmp;
	memcpy(&tmp, mnl_attr_get_data(attr), sizeof(tmp));
	return tmp;
}

/**
 * mnl_attr_get_str - returns pointer to string attribute.
 * @attr: pointer to netlink attribute
 *
 * This function returns the payload of string attribute value.
 */
const char *mnl_attr_get_str(const struct nlattr *attr)
{
	return (const char *)mnl_attr_get_data(attr);
}

/**
 * mnl_attr_put - add an attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @len: netlink attribute payload size
 * @data: pointer to the data that is stored by the new attribute 
 */
void mnl_attr_put(struct nlmsghdr *nlh, int type, size_t len, const void *data)
{
	struct nlattr *attr = mnl_nlmsg_get_tail(nlh);
	int payload_len = mnl_align(sizeof(struct nlattr)) + len;

	attr->nla_type = type;
	attr->nla_len = payload_len;
	memcpy(mnl_attr_get_data(attr), data, len);
	nlh->nlmsg_len += mnl_align(payload_len);
}

/**
 * mnl_attr_put_u8 - add 8-bit unsigned integer attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @len: netlink attribute payload size
 * @data: 8-bit unsigned integer data that is stored by the new attribute
 */
void mnl_attr_put_u8(struct nlmsghdr *nlh, int type, u_int8_t data)
{
	mnl_attr_put(nlh, type, sizeof(u_int8_t), &data);
}

/**
 * mnl_attr_put_u16 - add 16-bit unsigned integer attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @data: 16-bit unsigned integer data that is stored by the new attribute
 */
void mnl_attr_put_u16(struct nlmsghdr *nlh, int type, u_int16_t data)
{
	mnl_attr_put(nlh, type, sizeof(u_int16_t), &data);
}

/**
 * mnl_attr_put_u32 - add 32-bit unsigned integer attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @data: 32-bit unsigned integer data that is stored by the new attribute
 */
void mnl_attr_put_u32(struct nlmsghdr *nlh, int type, u_int32_t data)
{
	mnl_attr_put(nlh, type, sizeof(u_int32_t), &data);
}

/**
 * mnl_attr_put_u64 - add 64-bit unsigned integer attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @data: 64-bit unsigned integer data that is stored by the new attribute
 */
void mnl_attr_put_u64(struct nlmsghdr *nlh, int type, u_int64_t data)
{
	mnl_attr_put(nlh, type, sizeof(u_int64_t), &data);
}

/**
 * mnl_attr_put_str - add string attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @data: pointer to string data that is stored by the new attribute
 */
void mnl_attr_put_str(struct nlmsghdr *nlh, int type, const void *data)
{
	mnl_attr_put(nlh, type, strlen(data), data);
}

/**
 * mnl_attr_put_str_null - add string attribute to netlink message
 * @nlh: pointer to the netlink message
 * @type: netlink attribute type
 * @data: pointer to string data that is stored by the new attribute
 *
 * This function is similar to mnl_attr_put_str but it includes the NULL
 * terminator at the end of the string.
 */
void mnl_attr_put_str_null(struct nlmsghdr *nlh, int type, const void *data)
{
	mnl_attr_put(nlh, type, strlen(data)+1, data);
}