summaryrefslogtreecommitdiffstats
path: root/src/libnetfilter_cttimeout.c
blob: 7844a1f9c19adc9a0d291a88172c6716a4a4a7e7 (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
/*
 * (C) 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 General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */
#include "internal.h"

#include <time.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>

#include <libmnl/libmnl.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_cttimeout.h>

#include <libnetfilter_cttimeout/libnetfilter_cttimeout.h>

static const char *const tcp_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_TCP_SYN_SENT]	= "SYN_SENT",
	[NFCT_TIMEOUT_ATTR_TCP_SYN_RECV]	= "SYN_RECV",
	[NFCT_TIMEOUT_ATTR_TCP_ESTABLISHED]	= "ESTABLISHED",
	[NFCT_TIMEOUT_ATTR_TCP_FIN_WAIT]	= "FIN_WAIT",
	[NFCT_TIMEOUT_ATTR_TCP_CLOSE_WAIT]	= "CLOSE_WAIT",
	[NFCT_TIMEOUT_ATTR_TCP_LAST_ACK]	= "LAST_ACK",
	[NFCT_TIMEOUT_ATTR_TCP_TIME_WAIT]	= "TIME_WAIT",
	[NFCT_TIMEOUT_ATTR_TCP_CLOSE]		= "CLOSE",
	[NFCT_TIMEOUT_ATTR_TCP_SYN_SENT2]	= "SYN_SENT2",
	[NFCT_TIMEOUT_ATTR_TCP_RETRANS]		= "RETRANS",
	[NFCT_TIMEOUT_ATTR_TCP_UNACK]		= "UNACKNOWLEDGED",
};

static const char *const generic_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_GENERIC]		= "TIMEOUT",
};

static const char *const udp_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_UDP_UNREPLIED]	= "UNREPLIED",
	[NFCT_TIMEOUT_ATTR_UDP_REPLIED]		= "REPLIED",
};

static const char *const sctp_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_SCTP_CLOSED]			= "CLOSED",
	[NFCT_TIMEOUT_ATTR_SCTP_COOKIE_WAIT]		= "COOKIE_WAIT",
	[NFCT_TIMEOUT_ATTR_SCTP_COOKIE_ECHOED]		= "COOKIE_ECHOED",
	[NFCT_TIMEOUT_ATTR_SCTP_ESTABLISHED]		= "ESTABLISHED",
	[NFCT_TIMEOUT_ATTR_SCTP_SHUTDOWN_SENT]		= "SHUTDOWN_SENT",
	[NFCT_TIMEOUT_ATTR_SCTP_SHUTDOWN_RECD]		= "SHUTDOWN_RECD",
	[NFCT_TIMEOUT_ATTR_SCTP_SHUTDOWN_ACK_SENT]	= "SHUTDOWN_ACK_SENT",
};

static const char *const dccp_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_DCCP_REQUEST]	= "REQUEST",
	[NFCT_TIMEOUT_ATTR_DCCP_RESPOND]	= "RESPOND",
	[NFCT_TIMEOUT_ATTR_DCCP_PARTOPEN]	= "PARTOPEN",
	[NFCT_TIMEOUT_ATTR_DCCP_OPEN]		= "OPEN",
	[NFCT_TIMEOUT_ATTR_DCCP_CLOSEREQ]	= "CLOSEREQ",
	[NFCT_TIMEOUT_ATTR_DCCP_CLOSING]	= "CLOSING",
	[NFCT_TIMEOUT_ATTR_DCCP_TIMEWAIT]	= "TIMEWAIT",
};

static const char *const icmp_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_ICMP]		= "TIMEOUT",
};

static const char *const icmpv6_state_to_name[] = {
	[NFCT_TIMEOUT_ATTR_ICMPV6]		= "TIMEOUT",
};

static struct {
	uint32_t nlattr_max;
	uint32_t attr_max;
	const char *const *state_to_name;
} timeout_protocol[IPPROTO_MAX] = {
	[IPPROTO_ICMP]	= {
		.nlattr_max	= __CTA_TIMEOUT_ICMP_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_ICMP_MAX,
		.state_to_name	= icmp_state_to_name,
	},
	[IPPROTO_TCP]	= {
		.nlattr_max	= __CTA_TIMEOUT_TCP_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_TCP_MAX,
		.state_to_name	= tcp_state_to_name,
	},
	[IPPROTO_UDP]	= {
		.nlattr_max	= __CTA_TIMEOUT_UDP_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_UDP_MAX,
		.state_to_name	= udp_state_to_name,
	},
	[IPPROTO_GRE]	= {
		.nlattr_max	= __CTA_TIMEOUT_GRE_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_GRE_MAX,
		.state_to_name	= udp_state_to_name,
	},
	[IPPROTO_SCTP]	= {
		.nlattr_max	= __CTA_TIMEOUT_SCTP_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_SCTP_MAX,
		.state_to_name	= sctp_state_to_name,
	},
	[IPPROTO_DCCP]	= {
		.nlattr_max	= __CTA_TIMEOUT_DCCP_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_DCCP_MAX,
		.state_to_name	= dccp_state_to_name,
	},
	[IPPROTO_UDPLITE]	= {
		.nlattr_max	= __CTA_TIMEOUT_UDPLITE_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_UDPLITE_MAX,
		.state_to_name	= udp_state_to_name,
	},
	[IPPROTO_ICMPV6]	= {
		.nlattr_max	= __CTA_TIMEOUT_ICMPV6_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_ICMPV6_MAX,
		.state_to_name	= icmpv6_state_to_name,
	},
	/* add your new supported protocol tracker here. */
	[IPPROTO_RAW]	= {
		.nlattr_max	= __CTA_TIMEOUT_GENERIC_MAX,
		.attr_max	= NFCT_TIMEOUT_ATTR_GENERIC_MAX,
		.state_to_name	= generic_state_to_name,
	},
};


struct nfct_timeout {
	char				name[32];	/* object name. */
	uint16_t			l3num;		/* AF_INET, ... */
	uint8_t				l4num;		/* UDP, TCP, ... */
	uint16_t			attrset;

	uint32_t			*timeout;	/* array of timeout. */
	uint16_t			polset;
};

/**
 * \mainpage
 *
 * libnetfilter_cttimeout is the userspace library that provides a programming
 * interface (API) to the in-kernel cttimeout infrastructure. This
 * infrastructure allows you to define fine-grain connection tracking timeout
 * policies that can be attached to traffic flows via iptables CT target.
 * Before the existence of this infrastructure, you could only set global
 * timeout policies per protocol. This library is currently used by the nfct
 * utility that is part of the conntrack-tools.
 *
 * libnetfilter_cttimeout homepage is:
 *      http://netfilter.org/projects/libnetfilter_cttimeout/
 *
 * \section Dependencies
 * libnetfilter_cttimeout requires libmnl and a kernel that includes the
 * nfnetlink_cttimeout subsystem (i.e. 3.4.0 or later).
 *
 * \section Main Features
 *  - listing/retrieving entries from the timeout policy table.
 *  - inserting/modifying/deleting entries from the timeout policy table.
 *
 * \section Git Tree
 * The current development version of libnetfilter_cttimeout can be accessed at
 * https://git.netfilter.org/cgi-bin/gitweb.cgi?p=libnetfilter_cttimeout.git
 *
 * \section Privileges
 * You need the CAP_NET_ADMIN capability in order to allow your application
 * to receive events from and to send commands to kernel-space, excepting
 * the timeout policy table dumping operation.
 *
 * \section Authors
 * libnetfilter_conntrack has been written by Pablo Neira Ayuso.
 */

/**
 * \defgroup nfcttimeout Timeout policy object handling
 * @{
 */

/**
 * nfct_timeout_alloc - allocate a new conntrack timeout object
 * \param protonum layer 4 protocol number (use IPPROTO_* constants)
 *
 * You can use IPPROTO_MAX to set the timeout for the generic protocol tracker.
 *
 * In case of success, this function returns a valid pointer, otherwise NULL
 * s returned and errno is appropriately set.
 */
struct nfct_timeout *nfct_timeout_alloc(void)
{
	struct nfct_timeout *t;

	t = calloc(1, sizeof(struct nfct_timeout));
	if (t == NULL)
		return NULL;

	return t;
}
EXPORT_SYMBOL(nfct_timeout_alloc);

/**
 * nfct_timeout_free - release one conntrack timeout object
 * \param t pointer to the conntrack timeout object
 */
void nfct_timeout_free(struct nfct_timeout *t)
{
	if (t->timeout)
		free(t->timeout);
	free(t);
}
EXPORT_SYMBOL(nfct_timeout_free);

/**
 * nfct_timeout_attr_set - set one attribute of the conntrack timeout object
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 * \param data pointer to data that will be used to set this attribute
 */
int
nfct_timeout_attr_set(struct nfct_timeout *t, uint32_t type, const void *data)
{
	switch(type) {
	case NFCT_TIMEOUT_ATTR_NAME:
		strncpy(t->name, data, sizeof(t->name));
		t->name[sizeof(t->name)-1] = '\0';
		break;
	case NFCT_TIMEOUT_ATTR_L3PROTO:
		t->l3num = *((uint16_t *) data);
		break;
	case NFCT_TIMEOUT_ATTR_L4PROTO:
		t->l4num = *((uint8_t *) data);
		break;
	/* NFCT_TIMEOUT_ATTR_POLICY is set by nfct_timeout_policy_attr_set. */
	}
	t->attrset |= (1 << type);
	return 0;
}
EXPORT_SYMBOL(nfct_timeout_attr_set);

/**
 * nfct_timeout_attr_set_u8 - set one attribute of the conntrack timeout object
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 * \param data pointer to data that will be used to set this attribute
 */
int
nfct_timeout_attr_set_u8(struct nfct_timeout *t, uint32_t type, uint8_t data)
{
	return nfct_timeout_attr_set(t, type, &data);
}
EXPORT_SYMBOL(nfct_timeout_attr_set_u8);

/**
 * nfct_timeout_attr_set_u16 - set one attribute of the conntrack timeout object
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 * \param data pointer to data that will be used to set this attribute
 */
int
nfct_timeout_attr_set_u16(struct nfct_timeout *t, uint32_t type, uint16_t data)
{
	return nfct_timeout_attr_set(t, type, &data);
}
EXPORT_SYMBOL(nfct_timeout_attr_set_u16);

/**
 * nfct_timeout_attr_unset - unset one attribute of the conntrack timeout object
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 */
void nfct_timeout_attr_unset(struct nfct_timeout *t, uint32_t type)
{
	t->attrset &= ~(1 << type);
}
EXPORT_SYMBOL(nfct_timeout_attr_unset);

/**
 * nfct_timeout_policy_attr_set_u32 - set one attribute of the policy
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 * \param data data that will be used to set this attribute
 */
int
nfct_timeout_policy_attr_set_u32(struct nfct_timeout *t,
				 uint32_t type, uint32_t data)
{
	size_t timeout_array_size;

	/* Layer 4 protocol needs to be already set. */
	if (!(t->attrset & (1 << NFCT_TIMEOUT_ATTR_L4PROTO)))
		return -1;

	if (t->timeout == NULL) {
		/* if not supported, default to generic protocol tracker. */
		if (timeout_protocol[t->l4num].attr_max != 0) {
			timeout_array_size =
				sizeof(uint32_t) *
					timeout_protocol[t->l4num].attr_max;
		} else {
			timeout_array_size =
				sizeof(uint32_t) *
					timeout_protocol[IPPROTO_RAW].attr_max;
		}
		t->timeout = calloc(1, timeout_array_size);
		if (t->timeout == NULL)
			return -1;
	}

	/* this state does not exists in this protocol tracker. */
	if (type > timeout_protocol[t->l4num].attr_max)
		return -1;

	t->timeout[type] = data;
	t->polset |= (1 << type);

	if (!(t->attrset & (1 << NFCT_TIMEOUT_ATTR_POLICY)))
		t->attrset |= (1 << NFCT_TIMEOUT_ATTR_POLICY);

	return 0;
}
EXPORT_SYMBOL(nfct_timeout_policy_attr_set_u32);

/**
 * nfct_timeout_policy_attr_unset - unset one attribute of the policy
 * \param t pointer to the conntrack timeout object
 * \param type attribute type you want to set
 */
void nfct_timeout_policy_attr_unset(struct nfct_timeout *t, uint32_t type)
{
	t->attrset &= ~(1 << type);
}
EXPORT_SYMBOL(nfct_timeout_policy_attr_unset);

/**
 * nfct_timeout_policy_attr_to_name - get state name from protocol state number
 * \param l4proto protocol, ie. IPPROTO_*
 * \param state state number that you want to get the state name
 *
 * This function returns NULL if unsupported protocol or state number is passed.
 * Otherwise, a pointer to valid string is returned.
 */
const char *nfct_timeout_policy_attr_to_name(uint8_t l4proto, uint32_t state)
{
	if (timeout_protocol[l4proto].state_to_name == NULL) {
		printf("no array state name\n");
		return NULL;
	}

	if (timeout_protocol[l4proto].state_to_name[state] == NULL) {
		printf("state %d does not exists\n", state);
		return NULL;
	}

	return timeout_protocol[l4proto].state_to_name[state];
}
EXPORT_SYMBOL(nfct_timeout_policy_attr_to_name);

/**
 * @}
 */

/**
 * \defgroup nfcttimeout_output Timeout policy object output
 * @{
 */

static int
nfct_timeout_snprintf_default(char *buf, size_t size,
			      const struct nfct_timeout *t,
			      unsigned int flags)
{
	int ret = 0;
	unsigned int offset = 0;

	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_NAME)) {
		ret = snprintf(buf+offset, size, ".%s = {\n", t->name);
		offset += ret;
		size -= ret;
	}
	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_L3PROTO)) {
		ret = snprintf(buf+offset, size, "\t.l3proto = %u,\n",
				t->l3num);
		offset += ret;
		size -= ret;
	}
	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_L4PROTO)) {
		ret = snprintf(buf+offset, size, "\t.l4proto = %u,\n",
				t->l4num);
		offset += ret;
		size -= ret;
	}
	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_POLICY)) {
		uint8_t l4num = t->l4num;
		int i;

		/* default to generic protocol tracker. */
		if (timeout_protocol[t->l4num].attr_max == 0)
			l4num = IPPROTO_RAW;

		ret = snprintf(buf+offset, size, "\t.policy = {\n");
		offset += ret;
		size -= ret;

		for (i=0; i<timeout_protocol[l4num].attr_max; i++) {
			const char *state_name =
				timeout_protocol[l4num].state_to_name[i][0] ?
				timeout_protocol[l4num].state_to_name[i] :
				"UNKNOWN";

			ret = snprintf(buf+offset, size,
				"\t\t.%s = %u,\n", state_name, t->timeout[i]);
			offset += ret;
			size -= ret;
		}

		ret = snprintf(buf+offset, size, "\t},\n");
		offset += ret;
		size -= ret;
	}
	ret = snprintf(buf+offset, size, "};");
	offset += ret;
	size -= ret;

	buf[offset]='\0';

	return ret;
}

/**
 * nfct_timeout_snprintf - print conntrack timeout object into one buffer
 * \param buf: pointer to buffer that is used to print the object
 * \param size: size of the buffer (or remaining room in it).
 * \param t: pointer to a valid conntrack timeout object.
 * \param type: output type (see NFCT_TIMEOUT_O_*)
 * \param flags: output flags (always set this to zero).
 *
 * This function returns -1 in case that some mandatory attributes are
 * missing. On sucess, it returns 0.
 */
int nfct_timeout_snprintf(char *buf, size_t size, const struct nfct_timeout *t,
			  unsigned int type, unsigned int flags)
{
	int ret = 0;

	switch(type) {
	case NFCT_TIMEOUT_O_DEFAULT:
		ret = nfct_timeout_snprintf_default(buf, size, t, flags);
		break;
	/* add your new output here. */
	default:
		break;
	}

	return ret;
}
EXPORT_SYMBOL(nfct_timeout_snprintf);

/**
 * @}
 */

/**
 * \defgroup nlmsg Netlink message helper functions
 * @{
 */

/**
 * nfct_timeout_nlmsg_build_hdr - build netlink message header for ct timeout
 * \param buf: buffer where this function outputs the netlink message.
 * \param cmd: nfct_timeout nfnetlink command.
 * \param flags: netlink flags.
 * \param seq: sequence number for this message.
 *
 * Possible commands:
 * - CTNL_MSG_TIMEOUT_NEW: new conntrack timeout object.
 * - CTNL_MSG_TIMEOUT_GET: get conntrack timeout object.
 * - CTNL_MSG_TIMEOUT_DEL: delete conntrack timeout object.
 */
struct nlmsghdr *
nfct_timeout_nlmsg_build_hdr(char *buf, uint8_t cmd,
			     uint16_t flags, uint32_t seq)
{
	struct nlmsghdr *nlh;
	struct nfgenmsg *nfh;

	nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8) | cmd;
	nlh->nlmsg_flags = NLM_F_REQUEST | flags;
	nlh->nlmsg_seq = seq;

	nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg));
	nfh->nfgen_family = AF_UNSPEC;
	nfh->version = NFNETLINK_V0;
	nfh->res_id = 0;

	return nlh;
}
EXPORT_SYMBOL(nfct_timeout_nlmsg_build_hdr);

/**
 * nfct_timeout_nlmsg_build_payload - build payload from ct timeout object
 * \param nlh: netlink message that you want to use to add the payload.
 * \param t: pointer to a conntrack timeout object
 */
void
nfct_timeout_nlmsg_build_payload(struct nlmsghdr *nlh,
				 const struct nfct_timeout *t)
{
	int i;
	struct nlattr *nest;

	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_NAME))
		mnl_attr_put_strz(nlh, CTA_TIMEOUT_NAME, t->name);

	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_L3PROTO))
		mnl_attr_put_u16(nlh, CTA_TIMEOUT_L3PROTO, htons(t->l3num));

	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_L4PROTO))
		mnl_attr_put_u8(nlh, CTA_TIMEOUT_L4PROTO, t->l4num);

	if (t->attrset & (1 << NFCT_TIMEOUT_ATTR_POLICY) && t->polset) {
		nest = mnl_attr_nest_start(nlh, CTA_TIMEOUT_DATA);

		for (i=0; i<timeout_protocol[t->l4num].attr_max; i++) {
			if (t->polset & (1 << i)) {
				mnl_attr_put_u32(nlh, i+1,
						 htonl(t->timeout[i]));
			}
		}
		mnl_attr_nest_end(nlh, nest);
	}

}
EXPORT_SYMBOL(nfct_timeout_nlmsg_build_payload);

static int
timeout_nlmsg_parse_attr_cb(const struct nlattr *attr, void *data)
{
	const struct nlattr **tb = data;
	uint16_t type = mnl_attr_get_type(attr);

	if (mnl_attr_type_valid(attr, CTA_TIMEOUT_MAX) < 0)
		return MNL_CB_OK;

	switch(type) {
	case CTA_TIMEOUT_NAME:
		if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
			perror("mnl_attr_validate");
			return MNL_CB_ERROR;
		}
		break;
	case CTA_TIMEOUT_L3PROTO:
		if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
			perror("mnl_attr_validate");
			return MNL_CB_ERROR;
		}
		break;
	case CTA_TIMEOUT_L4PROTO:
		if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) {
			perror("mnl_attr_validate");
			return MNL_CB_ERROR;
		}
		break;
	case CTA_TIMEOUT_DATA:
		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
			perror("mnl_attr_validate");
			return MNL_CB_ERROR;
		}
		break;
	}
	tb[type] = attr;
	return MNL_CB_OK;
}

struct _container_policy_cb {
	unsigned int nlattr_max;
	void *tb;
};

static int
parse_timeout_attr_policy_cb(const struct nlattr *attr, void *data)
{
	struct _container_policy_cb *data_cb = data;
        const struct nlattr **tb = data_cb->tb;
	uint16_t type = mnl_attr_get_type(attr);

        if (mnl_attr_type_valid(attr, data_cb->nlattr_max) < 0)
                return MNL_CB_OK;

	if (type <= data_cb->nlattr_max) {
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
			perror("mnl_attr_validate");
			return MNL_CB_ERROR;
		}
		tb[type] = attr;
	}
	return MNL_CB_OK;
}

static void
timeout_parse_attr_data(struct nfct_timeout *t, const struct nlattr *nest)
{
	unsigned int nlattr_max = timeout_protocol[t->l4num].nlattr_max;
	struct nlattr *tb[nlattr_max];
	struct _container_policy_cb cnt = {
		.nlattr_max = nlattr_max,
		.tb = tb,
	};
	unsigned int i;

	memset(tb, 0, sizeof(struct nlattr *) * nlattr_max);

	mnl_attr_parse_nested(nest, parse_timeout_attr_policy_cb, &cnt);

	for (i=1; i<nlattr_max; i++) {
		if (tb[i]) {
			nfct_timeout_policy_attr_set_u32(t, i-1,
				ntohl(mnl_attr_get_u32(tb[i])));
		}
	}
}

/**
 * nfct_timeout_nlmsg_parse_payload - set timeout object attributes from message
 * \param nlh: netlink message that you want to use to add the payload.
 * \param t: pointer to a conntrack timeout object
 *
 * This function returns -1 in case that some mandatory attributes are
 * missing. On sucess, it returns 0.
 */
int
nfct_timeout_nlmsg_parse_payload(const struct nlmsghdr *nlh,
				 struct nfct_timeout *t)
{
	struct nlattr *tb[CTA_TIMEOUT_MAX+1] = {};
	struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);

	mnl_attr_parse(nlh, sizeof(*nfg), timeout_nlmsg_parse_attr_cb, tb);
	if (tb[CTA_TIMEOUT_NAME]) {
		nfct_timeout_attr_set(t, NFCT_TIMEOUT_ATTR_NAME,
			mnl_attr_get_str(tb[CTA_TIMEOUT_NAME]));
	}
	if (tb[CTA_TIMEOUT_L3PROTO]) {
		nfct_timeout_attr_set_u16(t, NFCT_TIMEOUT_ATTR_L3PROTO,
			ntohs(mnl_attr_get_u16(tb[CTA_TIMEOUT_L3PROTO])));
	}
	if (tb[CTA_TIMEOUT_L4PROTO]) {
		nfct_timeout_attr_set_u8(t, NFCT_TIMEOUT_ATTR_L4PROTO,
			mnl_attr_get_u8(tb[CTA_TIMEOUT_L4PROTO]));
	}
	if (tb[CTA_TIMEOUT_DATA]) {
		timeout_parse_attr_data(t, tb[CTA_TIMEOUT_DATA]);
	}
	return 0;
}
EXPORT_SYMBOL(nfct_timeout_nlmsg_parse_payload);

/**
 * @}
 */