summaryrefslogtreecommitdiff
path: root/src/attr.c
blob: c551d0bd844391b8344b403d78e2baf5fadbbb62 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
 * (C) 2008-2012 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 Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */
#include <limits.h>	/* for INT_MAX */
#include <libmnl/libmnl.h>
#include <string.h>
#include <errno.h>
#include "internal.h"

/**
 * \defgroup attr Netlink attribute helpers
 *
 * Netlink Type-Length-Value (TLV) attribute:
 * \verbatim
	|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
	-------------------------------------------------
	|     length    |      type     |      value     |
	-------------------------------------------------
	|<--------- header ------------>|<-- payload --->|
\endverbatim
 * The payload of the Netlink message contains sequences of attributes that are
 * expressed in TLV format.
 *
 * @{
 */

/**
 * mnl_attr_get_type - get type of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute type.
 */
uint16_t mnl_attr_get_type(const struct nlattr *attr)
{
	return attr->nla_type & NLA_TYPE_MASK;
}
EXPORT_SYMBOL(mnl_attr_get_type);

/**
 * mnl_attr_get_len - get length of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute length that is the attribute header
 * plus the attribute payload.
 */
uint16_t mnl_attr_get_len(const struct nlattr *attr)
{
	return attr->nla_len;
}
EXPORT_SYMBOL(mnl_attr_get_len);

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

/**
 * mnl_attr_get_payload - get pointer to the attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function return a pointer to the attribute payload.
 */
void *mnl_attr_get_payload(const struct nlattr *attr)
{
	return (void *)attr + MNL_ATTR_HDRLEN;
}
EXPORT_SYMBOL(mnl_attr_get_payload);

/**
 * mnl_attr_ok - check if there is room for an attribute in a buffer
 * \param attr attribute that we want to check if there is room for
 * \param len remaining bytes in a buffer that contains the attribute
 *
 * This function is used to check that a buffer, which is supposed to contain
 * an attribute, has enough room for the attribute that it stores, i.e. this
 * function can be used to verify that an attribute is neither malformed nor
 * truncated.
 *
 * This function does not set errno in case of error since it is intended
 * for iterations. Thus, it returns true on success and false on error.
 *
 * The len parameter may be negative in the case of malformed messages during
 * attribute iteration, that is why we use a signed integer.
 */
bool mnl_attr_ok(const struct nlattr *attr, int len)
{
	return len >= (int)sizeof(struct nlattr) &&
	       attr->nla_len >= sizeof(struct nlattr) &&
	       (int)attr->nla_len <= len;
}
EXPORT_SYMBOL(mnl_attr_ok);

/**
 * mnl_attr_next - get the next attribute in the payload of a netlink message
 * \param attr pointer to the current attribute
 *
 * This function returns a pointer to the next attribute after the one passed
 * as parameter. You have to use mnl_attr_ok() to ensure that the next
 * attribute is valid.
 */
struct nlattr *mnl_attr_next(const struct nlattr *attr)
{
	return (struct nlattr *)((void *)attr + MNL_ALIGN(attr->nla_len));
}
EXPORT_SYMBOL(mnl_attr_next);

/**
 * mnl_attr_type_valid - check if the attribute type is valid
 * \param attr pointer to attribute to be checked
 * \param max maximum attribute type
 *
 * This function allows to check if the attribute type is higher than the
 * maximum supported type. If the attribute type is invalid, this function
 * returns -1 and errno is explicitly set. On success, this function returns 1.
 *
 * Strict attribute checking in user-space is not a good idea since you may
 * run an old application with a newer kernel that supports new attributes.
 * This leads to backward compatibility breakages in user-space. Better check
 * if you support an attribute, if not, skip it.
 */
int mnl_attr_type_valid(const struct nlattr *attr, uint16_t max)
{
	if (mnl_attr_get_type(attr) > max) {
		errno = EOPNOTSUPP;
		return -1;
	}
	return 1;
}
EXPORT_SYMBOL(mnl_attr_type_valid);

static int __mnl_attr_validate(const struct nlattr *attr,
			       enum mnl_attr_data_type type, size_t exp_len)
{
	uint16_t attr_len = mnl_attr_get_payload_len(attr);
	const char *attr_data = mnl_attr_get_payload(attr);

	if (attr_len < exp_len) {
		errno = ERANGE;
		return -1;
	}
	switch(type) {
	case MNL_TYPE_FLAG:
		if (attr_len > 0) {
			errno = ERANGE;
			return -1;
		}
		break;
	case MNL_TYPE_NUL_STRING:
		if (attr_len == 0) {
			errno = ERANGE;
			return -1;
		}
		if (attr_data[attr_len-1] != '\0') {
			errno = EINVAL;
			return -1;
		}
		break;
	case MNL_TYPE_STRING:
		if (attr_len == 0) {
			errno = ERANGE;
			return -1;
		}
		break;
	case MNL_TYPE_NESTED:
		/* empty nested attributes are OK. */
		if (attr_len == 0)
			break;
		/* if not empty, they must contain one header, eg. flag */
		if (attr_len < MNL_ATTR_HDRLEN) {
			errno = ERANGE;
			return -1;
		}
		break;
	default:
		/* make gcc happy. */
		break;
	}
	if (exp_len && attr_len > exp_len) {
		errno = ERANGE;
		return -1;
	}
	return 0;
}

static const size_t mnl_attr_data_type_len[MNL_TYPE_MAX] = {
	[MNL_TYPE_U8]		= sizeof(uint8_t),
	[MNL_TYPE_U16]		= sizeof(uint16_t),
	[MNL_TYPE_U32]		= sizeof(uint32_t),
	[MNL_TYPE_U64]		= sizeof(uint64_t),
	[MNL_TYPE_MSECS]	= sizeof(uint64_t),
};

/**
 * mnl_attr_validate - validate netlink attribute (simplified version)
 * \param attr pointer to netlink attribute that we want to validate
 * \param type data type (see enum mnl_attr_data_type)
 *
 * The validation is based on the data type. Specifically, it checks that
 * integers (u8, u16, u32 and u64) have enough room for them. This function
 * returns -1 in case of error, and errno is explicitly set.
 */
int mnl_attr_validate(const struct nlattr *attr, enum mnl_attr_data_type type)
{
	int exp_len;

	if (type >= MNL_TYPE_MAX) {
		errno = EINVAL;
		return -1;
	}
	exp_len = mnl_attr_data_type_len[type];
	return __mnl_attr_validate(attr, type, exp_len);
}
EXPORT_SYMBOL(mnl_attr_validate);

/**
 * mnl_attr_validate2 - validate netlink attribute (extended version)
 * \param attr pointer to netlink attribute that we want to validate
 * \param type attribute type (see enum mnl_attr_data_type)
 * \param exp_len expected attribute data size
 *
 * This function allows to perform a more accurate validation for attributes
 * whose size is variable. If the size of the attribute is not what we expect,
 * this functions returns -1 and errno is explicitly set.
 */
int
mnl_attr_validate2(const struct nlattr *attr, enum mnl_attr_data_type type,
		   size_t exp_len)
{
	if (type >= MNL_TYPE_MAX) {
		errno = EINVAL;
		return -1;
	}
	return __mnl_attr_validate(attr, type, exp_len);
}
EXPORT_SYMBOL(mnl_attr_validate2);

/**
 * mnl_attr_parse - parse attributes
 * \param nlh pointer to netlink message
 * \param offset offset to start parsing from (if payload is after any header)
 * \param cb callback function that is called for each attribute
 * \param data pointer to data that is passed to the callback function
 *
 * This function allows to iterate over the sequence of attributes that compose
 * the Netlink message. You can then put the attribute in an array as it
 * usually happens at this stage or you can use any other data structure (such
 * as lists or trees).
 *
 * This function propagates the return value of the callback, which can be
 * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.
 */
int
mnl_attr_parse(const struct nlmsghdr *nlh, unsigned int offset,
	       mnl_attr_cb_t cb, void *data)
{
	int ret = MNL_CB_OK;
	const struct nlattr *attr;

	mnl_attr_for_each(attr, nlh, offset)
		if ((ret = cb(attr, data)) <= MNL_CB_STOP)
			return ret;
	return ret;
}
EXPORT_SYMBOL(mnl_attr_parse);

/**
 * mnl_attr_parse_nested - parse attributes inside a nest
 * \param nested pointer to netlink attribute that contains a nest
 * \param cb callback function that is called for each attribute in the nest
 * \param data pointer to data passed to the callback function
 *
 * This function allows to iterate over the sequence of attributes that compose
 * the Netlink message. You can then put the attribute in an array as it
 * usually happens at this stage or you can use any other data structure (such
 * as lists or trees).
 *
 * This function propagates the return value of the callback, which can be
 * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.
 */
int
mnl_attr_parse_nested(const struct nlattr *nested, mnl_attr_cb_t cb,
		      void *data)
{
	int ret = MNL_CB_OK;
	const struct nlattr *attr;

	mnl_attr_for_each_nested(attr, nested)
		if ((ret = cb(attr, data)) <= MNL_CB_STOP)
			return ret;
	return ret;
}
EXPORT_SYMBOL(mnl_attr_parse_nested);

/**
 * mnl_attr_parse_payload - parse attributes in payload of Netlink message
 * \param payload pointer to payload of the Netlink message
 * \param payload_len payload length that contains the attributes
 * \param cb callback function that is called for each attribute
 * \param data pointer to data that is passed to the callback function
 *
 * This function takes a pointer to the area that contains the attributes,
 * commonly known as the payload of the Netlink message. Thus, you have to
 * pass a pointer to the Netlink message payload, instead of the entire
 * message.
 *
 * This function allows you to iterate over the sequence of attributes that are
 * located at some payload offset. You can then put the attributes in one array
 * as usual, or you can use any other data structure (such as lists or trees).
 *
 * This function propagates the return value of the callback, which can be
 * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.
 */
int
mnl_attr_parse_payload(const void *payload, size_t payload_len,
		       mnl_attr_cb_t cb, void *data)
{
	int ret = MNL_CB_OK;
	const struct nlattr *attr;

	mnl_attr_for_each_payload(payload, payload_len)
		if ((ret = cb(attr, data)) <= MNL_CB_STOP)
			return ret;
	return ret;
}
EXPORT_SYMBOL(mnl_attr_parse_payload);

/**
 * mnl_attr_get_u8 - returns 8-bit unsigned integer attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function returns the 8-bit value of the attribute payload.
 */
uint8_t mnl_attr_get_u8(const struct nlattr *attr)
{
	return *((uint8_t *)mnl_attr_get_payload(attr));
}
EXPORT_SYMBOL(mnl_attr_get_u8);

/**
 * mnl_attr_get_u16 - returns 16-bit unsigned integer attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function returns the 16-bit value of the attribute payload.
 */
uint16_t mnl_attr_get_u16(const struct nlattr *attr)
{
	return *((uint16_t *)mnl_attr_get_payload(attr));
}
EXPORT_SYMBOL(mnl_attr_get_u16);

/**
 * mnl_attr_get_u32 - returns 32-bit unsigned integer attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function returns the 32-bit value of the attribute payload.
 */
uint32_t mnl_attr_get_u32(const struct nlattr *attr)
{
	return *((uint32_t *)mnl_attr_get_payload(attr));
}
EXPORT_SYMBOL(mnl_attr_get_u32);

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

/**
 * mnl_attr_get_str - returns pointer to string attribute.
 * \param 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 mnl_attr_get_payload(attr);
}
EXPORT_SYMBOL(mnl_attr_get_str);

/**
 * mnl_attr_put - add an attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type that you want to add
 * \param len netlink attribute payload length
 * \param data pointer to the data that will be stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void
mnl_attr_put(struct nlmsghdr *nlh, uint16_t type, size_t len, const void *data)
{
	struct nlattr *attr = mnl_nlmsg_get_payload_tail(nlh);
	uint16_t payload_len = MNL_ALIGN(sizeof(struct nlattr)) + len;

	attr->nla_type = type;
	attr->nla_len = payload_len;
	memcpy(mnl_attr_get_payload(attr), data, len);
	nlh->nlmsg_len += MNL_ALIGN(payload_len);
}
EXPORT_SYMBOL(mnl_attr_put);

/**
 * mnl_attr_put_u8 - add 8-bit unsigned integer attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 * \param data 8-bit unsigned integer data that is stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_u8(struct nlmsghdr *nlh, uint16_t type, uint8_t data)
{
	mnl_attr_put(nlh, type, sizeof(uint8_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u8);

/**
 * mnl_attr_put_u16 - add 16-bit unsigned integer attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 * \param data 16-bit unsigned integer data that is stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_u16(struct nlmsghdr *nlh, uint16_t type, uint16_t data)
{
	mnl_attr_put(nlh, type, sizeof(uint16_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u16);

/**
 * mnl_attr_put_u32 - add 32-bit unsigned integer attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 * \param data 32-bit unsigned integer data that is stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_u32(struct nlmsghdr *nlh, uint16_t type, uint32_t data)
{
	mnl_attr_put(nlh, type, sizeof(uint32_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u32);

/**
 * mnl_attr_put_u64 - add 64-bit unsigned integer attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 * \param data 64-bit unsigned integer data that is stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_u64(struct nlmsghdr *nlh, uint16_t type, uint64_t data)
{
	mnl_attr_put(nlh, type, sizeof(uint64_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u64);

/**
 * mnl_attr_put_str - add string attribute to netlink message
 * \param nlh  pointer to the netlink message
 * \param type netlink attribute type
 * \param data pointer to string data that is stored by the new attribute
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_str(struct nlmsghdr *nlh, uint16_t type, const char *data)
{
	mnl_attr_put(nlh, type, strlen(data), data);
}
EXPORT_SYMBOL(mnl_attr_put_str);

/**
 * mnl_attr_put_strz - add string attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 * \param 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
 * NUL/zero ('\0') terminator at the end of the string.
 *
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
void mnl_attr_put_strz(struct nlmsghdr *nlh, uint16_t type, const char *data)
{
	mnl_attr_put(nlh, type, strlen(data)+1, data);
}
EXPORT_SYMBOL(mnl_attr_put_strz);

/**
 * mnl_attr_nest_start - start an attribute nest
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 *
 * This function adds the attribute header that identifies the beginning of
 * an attribute nest. This function always returns a valid pointer to the
 * beginning of the nest.
 */
struct nlattr *mnl_attr_nest_start(struct nlmsghdr *nlh, uint16_t type)
{
	struct nlattr *start = mnl_nlmsg_get_payload_tail(nlh);

	/* set start->nla_len in mnl_attr_nest_end() */
	start->nla_type = NLA_F_NESTED | type;
	nlh->nlmsg_len += MNL_ALIGN(sizeof(struct nlattr));

	return start;
}
EXPORT_SYMBOL(mnl_attr_nest_start);

/**
 * mnl_attr_put_check - add an attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type that you want to add
 * \param len netlink attribute payload length
 * \param data pointer to the data that will be stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 */
bool
mnl_attr_put_check(struct nlmsghdr *nlh, size_t buflen,
		   uint16_t type, size_t len, const void *data)
{
	if (nlh->nlmsg_len + MNL_ATTR_HDRLEN + MNL_ALIGN(len) > buflen)
		return false;
	mnl_attr_put(nlh, type, len, data);
	return true;
}
EXPORT_SYMBOL(mnl_attr_put_check);

/**
 * mnl_attr_put_u8_check - add 8-bit unsigned int attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param data 8-bit unsigned integer data that is stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 */
bool
mnl_attr_put_u8_check(struct nlmsghdr *nlh, size_t buflen,
		      uint16_t type, uint8_t data)
{
	return mnl_attr_put_check(nlh, buflen, type, sizeof(uint8_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u8_check);

/**
 * mnl_attr_put_u16_check - add 16-bit unsigned int attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param data 16-bit unsigned integer data that is stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
bool
mnl_attr_put_u16_check(struct nlmsghdr *nlh, size_t buflen,
		       uint16_t type, uint16_t data)
{
	return mnl_attr_put_check(nlh, buflen, type, sizeof(uint16_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u16_check);

/**
 * mnl_attr_put_u32_check - add 32-bit unsigned int attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param data 32-bit unsigned integer data that is stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
bool
mnl_attr_put_u32_check(struct nlmsghdr *nlh, size_t buflen,
		       uint16_t type, uint32_t data)
{
	return mnl_attr_put_check(nlh, buflen, type, sizeof(uint32_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u32_check);

/**
 * mnl_attr_put_u64_check - add 64-bit unsigned int attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param data 64-bit unsigned integer data that is stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
bool
mnl_attr_put_u64_check(struct nlmsghdr *nlh, size_t buflen,
		       uint16_t type, uint64_t data)
{
	return mnl_attr_put_check(nlh, buflen, type, sizeof(uint64_t), &data);
}
EXPORT_SYMBOL(mnl_attr_put_u64_check);

/**
 * mnl_attr_put_str_check - add string attribute to netlink message
 * \param nlh  pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param data pointer to string data that is stored by the new attribute
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 * This function updates the length field of the Netlink message (nlmsg_len)
 * by adding the size (header + payload) of the new attribute.
 */
bool
mnl_attr_put_str_check(struct nlmsghdr *nlh, size_t buflen,
		       uint16_t type, const char *data)
{
	return mnl_attr_put_check(nlh, buflen, type, strlen(data), data);
}
EXPORT_SYMBOL(mnl_attr_put_str_check);

/**
 * mnl_attr_put_strz_check - add string attribute to netlink message
 * \param nlh pointer to the netlink message
 * \param buflen size of buffer which stores the message
 * \param type netlink attribute type
 * \param 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
 * NUL/zero ('\0') terminator at the end of the string.
 *
 * This function first checks that the data can be added to the message
 * (fits into the buffer) and then updates the length field of the Netlink
 * message (nlmsg_len) by adding the size (header + payload) of the new
 * attribute. The function returns true if the attribute could be added
 * to the message, otherwise false is returned.
 */
bool
mnl_attr_put_strz_check(struct nlmsghdr *nlh, size_t buflen,
			uint16_t type, const char *data)
{
	return mnl_attr_put_check(nlh, buflen, type, strlen(data)+1, data);
}
EXPORT_SYMBOL(mnl_attr_put_strz_check);

/**
 * mnl_attr_nest_start_check - start an attribute nest
 * \param buflen size of buffer which stores the message
 * \param nlh pointer to the netlink message
 * \param type netlink attribute type
 *
 * This function adds the attribute header that identifies the beginning of
 * an attribute nest. If the nested attribute cannot be added then NULL,
 * otherwise valid pointer to the beginning of the nest is returned.
 */
struct nlattr *
mnl_attr_nest_start_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type)
{
	if (nlh->nlmsg_len + MNL_ATTR_HDRLEN > buflen)
		return NULL;
	return mnl_attr_nest_start(nlh, type);
}
EXPORT_SYMBOL(mnl_attr_nest_start_check);

/**
 * mnl_attr_nest_end - end an attribute nest
 * \param nlh pointer to the netlink message
 * \param start pointer to the attribute nest returned by mnl_attr_nest_start()
 *
 * This function updates the attribute header that identifies the nest.
 */
void
mnl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *start)
{
	start->nla_len = mnl_nlmsg_get_payload_tail(nlh) - (void *)start;
}
EXPORT_SYMBOL(mnl_attr_nest_end);

/**
 * mnl_attr_nest_cancel - cancel an attribute nest
 * \param nlh pointer to the netlink message
 * \param start pointer to the attribute nest returned by mnl_attr_nest_start()
 *
 * This function updates the attribute header that identifies the nest.
 */
void
mnl_attr_nest_cancel(struct nlmsghdr *nlh, struct nlattr *start)
{
	nlh->nlmsg_len -= mnl_nlmsg_get_payload_tail(nlh) - (void *)start;
}
EXPORT_SYMBOL(mnl_attr_nest_cancel);

/**
 * @}
 */