summaryrefslogtreecommitdiffstats
path: root/src/obj/tunnel.c
blob: 100aa099c6e970095b494801b067779873e20b92 (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
/*
 * (C) 2018 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 <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>

#include <linux/netfilter/nf_tables.h>

#include <libmnl/libmnl.h>
#include <libnftnl/object.h>

#include "internal.h"
#include "obj.h"

static int
nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type,
		     const void *data, uint32_t data_len)
{
	struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);

	switch (type) {
	case NFTNL_OBJ_TUNNEL_ID:
		memcpy(&tun->id, data, sizeof(tun->id));
		break;
	case NFTNL_OBJ_TUNNEL_IPV4_SRC:
		memcpy(&tun->src_v4, data, sizeof(tun->src_v4));
		break;
	case NFTNL_OBJ_TUNNEL_IPV4_DST:
		memcpy(&tun->dst_v4, data, sizeof(tun->dst_v4));
		break;
	case NFTNL_OBJ_TUNNEL_IPV6_SRC:
		memcpy(&tun->src_v6, data, sizeof(struct in6_addr));
		break;
	case NFTNL_OBJ_TUNNEL_IPV6_DST:
		memcpy(&tun->dst_v6, data, sizeof(struct in6_addr));
		break;
	case NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL:
		memcpy(&tun->flowlabel, data, sizeof(tun->flowlabel));
		break;
	case NFTNL_OBJ_TUNNEL_SPORT:
		memcpy(&tun->sport, data, sizeof(tun->sport));
		break;
	case NFTNL_OBJ_TUNNEL_DPORT:
		memcpy(&tun->dport, data, sizeof(tun->dport));
		break;
	case NFTNL_OBJ_TUNNEL_FLAGS:
		memcpy(&tun->tun_flags, data, sizeof(tun->tun_flags));
		break;
	case NFTNL_OBJ_TUNNEL_TOS:
		memcpy(&tun->tun_tos, data, sizeof(tun->tun_tos));
		break;
	case NFTNL_OBJ_TUNNEL_TTL:
		memcpy(&tun->tun_ttl, data, sizeof(tun->tun_ttl));
		break;
	case NFTNL_OBJ_TUNNEL_VXLAN_GBP:
		memcpy(&tun->u.tun_vxlan.gbp, data, sizeof(tun->u.tun_vxlan.gbp));
		break;
	case NFTNL_OBJ_TUNNEL_ERSPAN_VERSION:
		memcpy(&tun->u.tun_erspan.version, data, sizeof(tun->u.tun_erspan.version));
		break;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX:
		memcpy(&tun->u.tun_erspan.u.v1_index, data, sizeof(tun->u.tun_erspan.u.v1_index));
		break;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID:
		memcpy(&tun->u.tun_erspan.u.v2.hwid, data, sizeof(tun->u.tun_erspan.u.v2.hwid));
		break;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
		memcpy(&tun->u.tun_erspan.u.v2.dir, data, sizeof(tun->u.tun_erspan.u.v2.dir));
		break;
	default:
		return -1;
	}
	return 0;
}

static const void *
nftnl_obj_tunnel_get(const struct nftnl_obj *e, uint16_t type,
		     uint32_t *data_len)
{
	struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);

	switch (type) {
	case NFTNL_OBJ_TUNNEL_ID:
		*data_len = sizeof(tun->id);
		return &tun->id;
	case NFTNL_OBJ_TUNNEL_IPV4_SRC:
		*data_len = sizeof(tun->src_v4);
		return &tun->src_v4;
	case NFTNL_OBJ_TUNNEL_IPV4_DST:
		*data_len = sizeof(tun->dst_v4);
		return &tun->dst_v4;
	case NFTNL_OBJ_TUNNEL_IPV6_SRC:
		*data_len = sizeof(tun->src_v6);
		return &tun->src_v6;
	case NFTNL_OBJ_TUNNEL_IPV6_DST:
		*data_len = sizeof(tun->dst_v6);
		return &tun->dst_v6;
	case NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL:
		*data_len = sizeof(tun->flowlabel);
		return &tun->flowlabel;
	case NFTNL_OBJ_TUNNEL_SPORT:
		*data_len = sizeof(tun->sport);
		return &tun->sport;
	case NFTNL_OBJ_TUNNEL_DPORT:
		*data_len = sizeof(tun->dport);
		return &tun->dport;
	case NFTNL_OBJ_TUNNEL_FLAGS:
		*data_len = sizeof(tun->tun_flags);
		return &tun->tun_flags;
	case NFTNL_OBJ_TUNNEL_TOS:
		*data_len = sizeof(tun->tun_tos);
		return &tun->tun_tos;
	case NFTNL_OBJ_TUNNEL_TTL:
		*data_len = sizeof(tun->tun_ttl);
		return &tun->tun_ttl;
	case NFTNL_OBJ_TUNNEL_VXLAN_GBP:
		*data_len = sizeof(tun->u.tun_vxlan.gbp);
		return &tun->u.tun_vxlan.gbp;
	case NFTNL_OBJ_TUNNEL_ERSPAN_VERSION:
		*data_len = sizeof(tun->u.tun_erspan.version);
		return &tun->u.tun_erspan.version;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX:
		*data_len = sizeof(tun->u.tun_erspan.u.v1_index);
		return &tun->u.tun_erspan.u.v1_index;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID:
		*data_len = sizeof(tun->u.tun_erspan.u.v2.hwid);
		return &tun->u.tun_erspan.u.v2.hwid;
	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
		*data_len = sizeof(tun->u.tun_erspan.u.v2.dir);
		return &tun->u.tun_erspan.u.v2.dir;
	}
	return NULL;
}

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

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

	switch(type) {
	case NFTA_TUNNEL_KEY_ID:
	case NFTA_TUNNEL_KEY_FLAGS:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	case NFTA_TUNNEL_KEY_IP:
	case NFTA_TUNNEL_KEY_IP6:
	case NFTA_TUNNEL_KEY_OPTS:
		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
			abi_breakage();
		break;
	case NFTA_TUNNEL_KEY_SPORT:
	case NFTA_TUNNEL_KEY_DPORT:
		if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
			abi_breakage();
		break;
	case NFTA_TUNNEL_KEY_TOS:
	case NFTA_TUNNEL_KEY_TTL:
		if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static void
nftnl_obj_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
{
	struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
	struct nlattr *nest;

	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ID))
		mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ID, htonl(tun->id));
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC) ||
	    e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_DST)) {
		nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_IP);
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC))
			mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP_SRC, tun->src_v4);
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_DST))
			mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP_DST, tun->dst_v4);
		mnl_attr_nest_end(nlh, nest);
	}
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC) ||
	    e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_DST)) {
		nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_IP6);
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC))
			mnl_attr_put(nlh, NFTA_TUNNEL_KEY_IP6_SRC,
				     sizeof(tun->src_v6), &tun->src_v6);
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_DST))
			mnl_attr_put(nlh, NFTA_TUNNEL_KEY_IP6_DST,
				     sizeof(tun->dst_v6), &tun->dst_v6);
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL))
			mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP6_FLOWLABEL,
					 htonl(tun->flowlabel));
		mnl_attr_nest_end(nlh, nest);
	}
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_SPORT))
		mnl_attr_put_u16(nlh, NFTA_TUNNEL_KEY_SPORT, htons(tun->sport));
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_DPORT))
		mnl_attr_put_u16(nlh, NFTA_TUNNEL_KEY_DPORT, htons(tun->dport));
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_TOS))
		mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_TOS, tun->tun_tos);
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_TTL))
		mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_TTL, tun->tun_ttl);
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_FLAGS))
		mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_FLAGS, htonl(tun->tun_flags));
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_VXLAN_GBP)) {
		nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS);
		mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_VXLAN_GBP,
				 htonl(tun->u.tun_vxlan.gbp));
		mnl_attr_nest_end(nlh, nest);
	}
	if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_VERSION) &&
	    (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX) ||
	     (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID) &&
	      e->flags & (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR)))) {
		struct nlattr *nest_inner;

		nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS);
		nest_inner = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS_ERSPAN);
		mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ERSPAN_VERSION,
				 htonl(tun->u.tun_erspan.version));
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX))
			mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX,
					 htonl(tun->u.tun_erspan.u.v1_index));
		if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID))
			mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID,
					tun->u.tun_erspan.u.v2.hwid);
		if (e->flags & (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR))
			mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR,
					tun->u.tun_erspan.u.v2.dir);
		mnl_attr_nest_end(nlh, nest_inner);
		mnl_attr_nest_end(nlh, nest);
	}
}

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

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

	switch (type) {
	case NFTA_TUNNEL_KEY_IP_SRC:
	case NFTA_TUNNEL_KEY_IP_DST:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static int nftnl_obj_tunnel_parse_ip(struct nftnl_obj *e, struct nlattr *attr,
				     struct nftnl_obj_tunnel *tun)
{
	struct nlattr *tb[NFTA_TUNNEL_KEY_IP_MAX + 1] = {};

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_ip_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_IP_SRC]) {
		tun->src_v4 = mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP_SRC]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC);
	}
	if (tb[NFTA_TUNNEL_KEY_IP_DST]) {
		tun->dst_v4 = mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP_DST]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV4_DST);
	}

	return 0;
}

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

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

	switch(type) {
	case NFTA_TUNNEL_KEY_IP6_SRC:
	case NFTA_TUNNEL_KEY_IP6_DST:
		if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
			abi_breakage();
		break;
	case NFTA_TUNNEL_KEY_IP6_FLOWLABEL:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static int nftnl_obj_tunnel_parse_ip6(struct nftnl_obj *e, struct nlattr *attr,
				      struct nftnl_obj_tunnel *tun)
{
	struct nlattr *tb[NFTA_TUNNEL_KEY_IP6_MAX + 1] = {};

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_ip6_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_IP6_SRC]) {
		memcpy(&tun->src_v6,
		       mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_IP6_SRC]),
		       sizeof(struct in6_addr));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC);
	}
	if (tb[NFTA_TUNNEL_KEY_IP6_DST]) {
		memcpy(&tun->dst_v6,
		       mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_IP6_DST]),
		       sizeof(struct in6_addr));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_DST);
	}
	if (tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]) {
		tun->flowlabel =
			ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL);
	}

	return 0;
}

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

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

	switch (type) {
	case NFTA_TUNNEL_KEY_VXLAN_GBP:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static int
nftnl_obj_tunnel_parse_vxlan(struct nftnl_obj *e, struct nlattr *attr,
			     struct nftnl_obj_tunnel *tun)
{
	struct nlattr *tb[NFTA_TUNNEL_KEY_VXLAN_MAX + 1] = {};

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_vxlan_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_VXLAN_GBP]) {
		tun->u.tun_vxlan.gbp =
			ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_VXLAN_GBP]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_VXLAN_GBP);
	}

	return 0;
}

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

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

	switch (type) {
	case NFTA_TUNNEL_KEY_ERSPAN_VERSION:
	case NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	case NFTA_TUNNEL_KEY_ERSPAN_V2_HWID:
	case NFTA_TUNNEL_KEY_ERSPAN_V2_DIR:
		if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static int
nftnl_obj_tunnel_parse_erspan(struct nftnl_obj *e, struct nlattr *attr,
			      struct nftnl_obj_tunnel *tun)
{
	struct nlattr *tb[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1] = {};

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_erspan_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]) {
		tun->u.tun_erspan.version =
			ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_VERSION);
	}
	if (tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]) {
		tun->u.tun_erspan.u.v1_index =
			ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX);
	}
	if (tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]) {
		tun->u.tun_erspan.u.v2.hwid =
			mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID);
	}
	if (tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]) {
		tun->u.tun_erspan.u.v2.dir =
			mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]);
		e->flags |= (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR);
	}

	return 0;
}

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

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

	switch (type) {
	case NFTA_TUNNEL_KEY_OPTS_VXLAN:
	case NFTA_TUNNEL_KEY_OPTS_ERSPAN:
		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

static int
nftnl_obj_tunnel_parse_opts(struct nftnl_obj *e, struct nlattr *attr,
			    struct nftnl_obj_tunnel *tun)
{
	struct nlattr *tb[NFTA_TUNNEL_KEY_OPTS_MAX + 1] = {};
	int err = 0;

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_opts_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_OPTS_VXLAN]) {
		err = nftnl_obj_tunnel_parse_vxlan(e, tb[NFTA_TUNNEL_KEY_OPTS_VXLAN],
						   tun);
	} else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
		err = nftnl_obj_tunnel_parse_erspan(e, tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
						    tun);
	}

	return err;
}

static int
nftnl_obj_tunnel_parse(struct nftnl_obj *e, struct nlattr *attr)
{
	struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
	struct nlattr *tb[NFTA_TUNNEL_KEY_MAX + 1] = {};
	int err;

	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TUNNEL_KEY_ID]) {
		tun->id = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ID]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_ID);
	}
	if (tb[NFTA_TUNNEL_KEY_IP]) {
		err = nftnl_obj_tunnel_parse_ip(e, tb[NFTA_TUNNEL_KEY_IP], tun);
		if (err < 0)
			return err;
	} else if (tb[NFTA_TUNNEL_KEY_IP6]) {
		err = nftnl_obj_tunnel_parse_ip6(e, tb[NFTA_TUNNEL_KEY_IP6], tun);
		if (err < 0)
			return err;
	}

	if (tb[NFTA_TUNNEL_KEY_SPORT]) {
		tun->sport = ntohs(mnl_attr_get_u16(tb[NFTA_TUNNEL_KEY_SPORT]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_SPORT);
	}
	if (tb[NFTA_TUNNEL_KEY_DPORT]) {
		tun->dport = ntohs(mnl_attr_get_u16(tb[NFTA_TUNNEL_KEY_DPORT]));
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_DPORT);
	}
	if (tb[NFTA_TUNNEL_KEY_TOS]) {
		tun->tun_tos = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_TOS]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_TOS);
	}
	if (tb[NFTA_TUNNEL_KEY_TTL]) {
		tun->tun_ttl = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_TTL]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_TTL);
	}
	if (tb[NFTA_TUNNEL_KEY_FLAGS]) {
		tun->tun_flags = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_FLAGS]);
		e->flags |= (1 << NFTNL_OBJ_TUNNEL_FLAGS);
	}
	if (tb[NFTA_TUNNEL_KEY_OPTS]) {
		err = nftnl_obj_tunnel_parse_opts(e, tb[NFTA_TUNNEL_KEY_OPTS], tun);
		if (err < 0)
			return err;
	}

	return 0;
}

static int nftnl_obj_tunnel_snprintf_default(char *buf, size_t len,
					     const struct nftnl_obj *e)
{
	struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);

	return snprintf(buf, len, "id %u ", tun->id);
}

static int nftnl_obj_tunnel_snprintf(char *buf, size_t len, uint32_t type,
				     uint32_t flags, const struct nftnl_obj *e)
{
	if (len)
		buf[0] = '\0';

	switch (type) {
	case NFTNL_OUTPUT_DEFAULT:
		return nftnl_obj_tunnel_snprintf_default(buf, len, e);
	case NFTNL_OUTPUT_XML:
	case NFTNL_OUTPUT_JSON:
	default:
		break;
	}
	return -1;
}

struct obj_ops obj_ops_tunnel = {
	.name		= "tunnel",
	.type		= NFT_OBJECT_TUNNEL,
	.alloc_len	= sizeof(struct nftnl_obj_tunnel),
	.max_attr	= NFTA_TUNNEL_KEY_MAX,
	.set		= nftnl_obj_tunnel_set,
	.get		= nftnl_obj_tunnel_get,
	.parse		= nftnl_obj_tunnel_parse,
	.build		= nftnl_obj_tunnel_build,
	.snprintf	= nftnl_obj_tunnel_snprintf,
};