summaryrefslogtreecommitdiff
path: root/lib/route/link/ip6gre.c
blob: 51d4609cd6bcdfa666060c008d0ef5d40706be0e (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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/* SPDX-License-Identifier: LGPL-2.1-only */

/**
 * @ingroup link
 * @defgroup ip6gre IP6GRE
 * ip6gre link module
 *
 * @details
 * \b Link Type Name: "ip6gre"
 *
 * @route_doc{link_ip6gre, IP6GRE Documentation}
 *
 * @{
 */

#include "nl-default.h"

#include <linux/if_tunnel.h>

#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/utils.h>
#include <netlink/object.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/link/ip6gre.h>

#include "nl-route.h"
#include "link-api.h"

#define IP6GRE_ATTR_LINK          (1 << 0)
#define IP6GRE_ATTR_IFLAGS        (1 << 1)
#define IP6GRE_ATTR_OFLAGS        (1 << 2)
#define IP6GRE_ATTR_IKEY          (1 << 3)
#define IP6GRE_ATTR_OKEY          (1 << 4)
#define IP6GRE_ATTR_LOCAL         (1 << 5)
#define IP6GRE_ATTR_REMOTE        (1 << 6)
#define IP6GRE_ATTR_TTL           (1 << 7)
#define IP6GRE_ATTR_ENCAPLIMIT    (1 << 8)
#define IP6GRE_ATTR_FLOWINFO      (1 << 9)
#define IP6GRE_ATTR_FLAGS         (1 << 10)
#define IP6GRE_ATTR_FWMARK        (1 << 11)

struct ip6gre_info
{
	uint8_t             ttl;
	uint8_t             encaplimit;
	uint16_t            iflags;
	uint16_t            oflags;
	uint32_t            ikey;
	uint32_t            okey;
	uint32_t            link;
	uint32_t            flowinfo;
	uint32_t            flags;
	struct in6_addr     local;
	struct in6_addr     remote;
	uint32_t            fwmark;
	uint32_t            ip6gre_mask;
};

static  struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
	[IFLA_GRE_LINK]         = { .type = NLA_U32 },
	[IFLA_GRE_IFLAGS]       = { .type = NLA_U16 },
	[IFLA_GRE_OFLAGS]       = { .type = NLA_U16 },
	[IFLA_GRE_IKEY]         = { .type = NLA_U32 },
	[IFLA_GRE_OKEY]         = { .type = NLA_U32 },
	[IFLA_GRE_LOCAL]        = { .minlen = sizeof(struct in6_addr) },
	[IFLA_GRE_REMOTE]       = { .minlen = sizeof(struct in6_addr) },
	[IFLA_GRE_TTL]          = { .type = NLA_U8 },
	[IFLA_GRE_ENCAP_LIMIT]  = { .type = NLA_U8 },
	[IFLA_GRE_FLOWINFO]     = { .type = NLA_U32 },
	[IFLA_GRE_FLAGS]        = { .type = NLA_U32 },
	[IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
};

static int ip6gre_alloc(struct rtnl_link *link)
{
	struct ip6gre_info *ip6gre;

	if (link->l_info)
		memset(link->l_info, 0, sizeof(*ip6gre));
	else {
		ip6gre = calloc(1, sizeof(*ip6gre));
		if (!ip6gre)
			return -NLE_NOMEM;

		link->l_info = ip6gre;
	}

	return 0;
}

static int ip6gre_parse(struct rtnl_link *link, struct nlattr *data,
		        struct nlattr *xstats)
{
	struct nlattr *tb[IFLA_GRE_MAX + 1];
	struct ip6gre_info *ip6gre;
	int err;

	NL_DBG(3, "Parsing IP6GRE link info\n");

	err = nla_parse_nested(tb, IFLA_GRE_MAX, data, ip6gre_policy);
	if (err < 0)
		goto errout;

	err = ip6gre_alloc(link);
	if (err < 0)
		goto errout;

	ip6gre = link->l_info;

	if (tb[IFLA_GRE_LINK]) {
		ip6gre->link = nla_get_u32(tb[IFLA_GRE_LINK]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_LINK;
	}

	if (tb[IFLA_GRE_IFLAGS]) {
		ip6gre->iflags = nla_get_u16(tb[IFLA_GRE_IFLAGS]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_IFLAGS;
	}

	if (tb[IFLA_GRE_OFLAGS]) {
		ip6gre->oflags = nla_get_u16(tb[IFLA_GRE_OFLAGS]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_OFLAGS;
	}

	if (tb[IFLA_GRE_IKEY]) {
		ip6gre->ikey = nla_get_u32(tb[IFLA_GRE_IKEY]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_IKEY;
	}

	if (tb[IFLA_GRE_OKEY]) {
		ip6gre->okey = nla_get_u32(tb[IFLA_GRE_OKEY]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_OKEY;
	}

	if (tb[IFLA_GRE_LOCAL]) {
		nla_memcpy(&ip6gre->local, tb[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_LOCAL;
	}

	if (tb[IFLA_GRE_REMOTE]) {
		nla_memcpy(&ip6gre->remote, tb[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_REMOTE;
	}

	if (tb[IFLA_GRE_TTL]) {
		ip6gre->ttl = nla_get_u8(tb[IFLA_GRE_TTL]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_TTL;
	}

	if (tb[IFLA_GRE_ENCAP_LIMIT]) {
		ip6gre->encaplimit = nla_get_u8(tb[IFLA_GRE_ENCAP_LIMIT]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_ENCAPLIMIT;
	}

	if (tb[IFLA_GRE_FLOWINFO]) {
		ip6gre->flowinfo = nla_get_u32(tb[IFLA_GRE_FLOWINFO]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLOWINFO;
	}

	if (tb[IFLA_GRE_FLAGS]) {
		ip6gre->flags = nla_get_u32(tb[IFLA_GRE_FLAGS]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLAGS;
	}

	if (tb[IFLA_GRE_FWMARK]) {
		ip6gre->fwmark = nla_get_u32(tb[IFLA_GRE_FWMARK]);
		ip6gre->ip6gre_mask |= IP6GRE_ATTR_FWMARK;
	}

	err = 0;

 errout:
	return err;
}

static int ip6gre_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
{
	struct ip6gre_info *ip6gre = link->l_info;
	struct nlattr *data;

	data = nla_nest_start(msg, IFLA_INFO_DATA);
	if (!data)
		return -NLE_MSGSIZE;

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LINK)
		NLA_PUT_U32(msg, IFLA_GRE_LINK, ip6gre->link);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IFLAGS)
		NLA_PUT_U16(msg, IFLA_GRE_IFLAGS, ip6gre->iflags);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OFLAGS)
		NLA_PUT_U16(msg, IFLA_GRE_OFLAGS, ip6gre->oflags);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IKEY)
		NLA_PUT_U32(msg, IFLA_GRE_IKEY, ip6gre->ikey);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OKEY)
		NLA_PUT_U32(msg, IFLA_GRE_OKEY, ip6gre->okey);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LOCAL)
		NLA_PUT(msg, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &ip6gre->local);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_REMOTE)
		NLA_PUT(msg, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &ip6gre->remote);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_TTL)
		NLA_PUT_U8(msg, IFLA_GRE_TTL, ip6gre->ttl);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_ENCAPLIMIT)
		NLA_PUT_U8(msg, IFLA_GRE_ENCAP_LIMIT, ip6gre->encaplimit);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLOWINFO)
		NLA_PUT_U32(msg, IFLA_GRE_FLOWINFO, ip6gre->flowinfo);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLAGS)
		NLA_PUT_U32(msg, IFLA_GRE_FLAGS, ip6gre->flags);

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FWMARK)
		NLA_PUT_U32(msg, IFLA_GRE_FWMARK, ip6gre->fwmark);

	nla_nest_end(msg, data);

 nla_put_failure:

	return 0;
}

static void ip6gre_free(struct rtnl_link *link)
{
	struct ip6gre_info *ip6gre = link->l_info;

	free(ip6gre);
	link->l_info = NULL;
}

static void ip6gre_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
{
	nl_dump(p, "ip6gre : %s", link->l_name);
}

static void ip6gre_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
{
	struct ip6gre_info *ip6gre = link->l_info;
	char *name;
	char addr[INET6_ADDRSTRLEN];

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LINK) {
		nl_dump(p, "      link ");
		name = rtnl_link_get_name(link);
		if (name)
			nl_dump_line(p, "%s\n", name);
		else
			nl_dump_line(p, "%u\n", ip6gre->link);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IFLAGS) {
		nl_dump(p, "      iflags ");
		nl_dump_line(p, "%x\n", ip6gre->iflags);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OFLAGS) {
		nl_dump(p, "      oflags ");
		nl_dump_line(p, "%x\n", ip6gre->oflags);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IKEY) {
		nl_dump(p, "    ikey   ");
		nl_dump_line(p, "%x\n",ip6gre->ikey);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OKEY) {
		nl_dump(p, "      okey ");
		nl_dump_line(p, "%x\n", ip6gre->okey);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LOCAL) {
		nl_dump(p, "      local ");
		nl_dump_line(p, "%s\n",
			     _nl_inet_ntop(AF_INET6, &ip6gre->local, addr));
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_REMOTE) {
		nl_dump(p, "      remote ");
		nl_dump_line(p, "%s\n",
			     _nl_inet_ntop(AF_INET6, &ip6gre->remote, addr));
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_TTL) {
		nl_dump(p, "      ttl ");
		nl_dump_line(p, "%u\n", ip6gre->ttl);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_ENCAPLIMIT) {
		nl_dump(p, "      encaplimit ");
		nl_dump_line(p, "%u\n", ip6gre->encaplimit);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLOWINFO) {
		nl_dump(p, "      flowinfo ");
		nl_dump_line(p, "%x\n", ip6gre->flowinfo);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLAGS) {
		nl_dump(p, "      flags ");
		nl_dump_line(p, "%x\n", ip6gre->flags);
	}

	if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FWMARK) {
		nl_dump(p, "    fwmark   ");
		nl_dump_line(p, "%x\n", ip6gre->fwmark);
	}
}

static int ip6gre_clone(struct rtnl_link *dst, struct rtnl_link *src)
{
	struct ip6gre_info *ip6gre_dst, *ip6gre_src = src->l_info;
	int err;

	dst->l_info = NULL;

	err = rtnl_link_set_type(dst, "ip6gre");
	if (err < 0)
		return err;

	ip6gre_dst = dst->l_info;

	if (!ip6gre_dst || !ip6gre_src)
		BUG();

	memcpy(ip6gre_dst, ip6gre_src, sizeof(struct ip6gre_info));

	return 0;
}

static struct rtnl_link_info_ops ip6gre_info_ops = {
	.io_name                = "ip6gre",
	.io_alloc               = ip6gre_alloc,
	.io_parse               = ip6gre_parse,
	.io_dump = {
		[NL_DUMP_LINE]  = ip6gre_dump_line,
		[NL_DUMP_DETAILS] = ip6gre_dump_details,
	},
	.io_clone               = ip6gre_clone,
	.io_put_attrs           = ip6gre_put_attrs,
	.io_free                = ip6gre_free,
};

#define IS_IP6GRE_LINK_ASSERT(link)                                             \
        if ((link)->l_info_ops != &ip6gre_info_ops) {                           \
                APPBUG("Link is not a ip6gre link. set type \"ip6gre\" first.");\
                return -NLE_OPNOTSUPP;                                          \
        }

#define HAS_IP6GRE_ATTR_ASSERT(link,attr)                                       \
        if (!((link)->ip6gre_mask & (attr)))                                    \
                return -NLE_NOATTR;

struct rtnl_link *rtnl_link_ip6gre_alloc(void)
{
	struct rtnl_link *link;
	int err;

	link = rtnl_link_alloc();
	if (!link)
		return NULL;

	err = rtnl_link_set_type(link, "ip6gre");
	if (err < 0) {
		rtnl_link_put(link);
		return NULL;
	}

	return link;
}

/**
 * Check if link is a IP6GRE link
 * @arg link            Link object
 *
 * @return True if link is a IP6GRE link, otherwise 0 is returned.
 */
int rtnl_link_is_ip6gre(struct rtnl_link *link)
{
	return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "ip6gre");
}

/**
 * Create a new IP6GRE tunnel device
 * @arg sock            netlink socket
 * @arg name            name of the tunnel deviceL
 *
 * Creates a new ip6gre tunnel device in the kernel
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_add(struct nl_sock *sk, const char *name)
{
	struct rtnl_link *link;
	int err;

	link = rtnl_link_ip6gre_alloc();
	if (!link)
		return -NLE_NOMEM;

	if(name)
		rtnl_link_set_name(link, name);

	err = rtnl_link_add(sk, link, NLM_F_CREATE);
	rtnl_link_put(link);

	return err;
}

/**
 * Set IP6GRE tunnel interface index
 * @arg link            Link object
 * @arg index           interface index
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_link(struct rtnl_link *link, uint32_t index)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->link = index;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_LINK;

	return 0;
}

/**
 * Get IP6GRE tunnel interface index
 * @arg link            Link object
 * @arg index           addr to fill in with the interface index
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_link(struct rtnl_link *link, uint32_t *index)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_LINK);

	*index = ip6gre->link;

	return 0;
}

/**
 * Set IP6GRE tunnel set iflags
 * @arg link            Link object
 * @arg iflags          ip6gre iflags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_iflags(struct rtnl_link *link, uint16_t iflags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->iflags = iflags;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_IFLAGS;

	return 0;
}

/**
 * Get IP6GRE tunnel iflags
 * @arg link            Link object
 * @arg iflags          addr to fill in with the iflags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_iflags(struct rtnl_link *link, uint16_t *iflags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_IFLAGS);

	*iflags = ip6gre->iflags;

	return 0;
}

/**
 * Set IP6GRE tunnel set oflags
 * @arg link            Link object
 * @arg oflags          ip6gre oflags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_oflags(struct rtnl_link *link, uint16_t oflags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->oflags = oflags;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_OFLAGS;

	return 0;
}

/**
 * Get IP6GRE tunnel oflags
 * @arg link            Link object
 * @arg oflags          addr to fill in with the oflags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_oflags(struct rtnl_link *link, uint16_t *oflags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_OFLAGS);

	*oflags = ip6gre->oflags;

	return 0;
}

/**
 * Set IP6GRE tunnel set ikey
 * @arg link            Link object
 * @arg ikey            ip6gre ikey
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_ikey(struct rtnl_link *link, uint32_t ikey)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->ikey = ikey;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_IKEY;

	return 0;
}

/**
 * Get IP6GRE tunnel ikey
 * @arg link            Link object
 * @arg ikey            addr to fill in with the ikey
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_ikey(struct rtnl_link *link, uint32_t *ikey)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_IKEY);

	*ikey = ip6gre->ikey;

	return 0;
}

/**
 * Set IP6GRE tunnel set okey
 * @arg link            Link object
 * @arg okey            ip6gre okey
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_okey(struct rtnl_link *link, uint32_t okey)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->okey = okey;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_OKEY;

	return 0;
}

/**
 * Get IP6GRE tunnel okey
 * @arg link            Link object
 * @arg okey            addr to fill in with the okey
 *
 * @return okey value
 */
int rtnl_link_ip6gre_get_okey(struct rtnl_link *link, uint32_t *okey)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_OKEY);

	*okey = ip6gre->okey;

	return 0;
}

/**
 * Set IP6GRE tunnel local address
 * @arg link            Link object
 * @arg local           local address
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_local(struct rtnl_link *link, struct in6_addr *local)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	memcpy(&ip6gre->local, local, sizeof(struct in6_addr));
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_LOCAL;

	return 0;
}

/**
 * Get IP6GRE tunnel local address
 * @arg link            Link object
 * @arg local           addr to fill in with local address
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_local(struct rtnl_link *link, struct in6_addr *local)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_LOCAL);

	memcpy(local, &ip6gre->local, sizeof(struct in6_addr));

	return 0;
}

/**
 * Set IP6GRE tunnel remote address
 * @arg link            Link object
 * @arg remote          remote address
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_remote(struct rtnl_link *link, struct in6_addr *remote)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	memcpy(&ip6gre->remote, remote, sizeof(struct in6_addr));
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_REMOTE;

	return 0;
}

/**
 * Get IP6GRE tunnel remote address
 * @arg link            Link object
 * @arg remote          addr to fill in with remote address
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_remote(struct rtnl_link *link, struct in6_addr *remote)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_REMOTE);

	memcpy(remote, &ip6gre->remote, sizeof(struct in6_addr));

	return 0;
}

/**
 * Set IP6GRE tunnel ttl
 * @arg link            Link object
 * @arg ttl             tunnel ttl
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_ttl(struct rtnl_link *link, uint8_t ttl)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->ttl = ttl;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_TTL;

	return 0;
}

/**
 * Set IP6GRE tunnel ttl
 * @arg link            Link object
 * @arg ttl             addr to fill in with the ttl
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_ttl(struct rtnl_link *link, uint8_t *ttl)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_TTL);

	*ttl = ip6gre->ttl;

	return 0;
}

/**
 * Set IP6GRE tunnel encap limit
 * @arg link            Link object
 * @arg encaplimit      tunnel encap limit value
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_encaplimit(struct rtnl_link *link, uint8_t encaplimit)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->encaplimit = encaplimit;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_ENCAPLIMIT;

	return 0;
}

/**
 * Get IP6GRE tunnel encap limit
 * @arg link            Link object
 * @arg encaplimit      addr to fill in with the encaplimit
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_encaplimit(struct rtnl_link *link, uint8_t *encaplimit)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_ENCAPLIMIT);

	*encaplimit = ip6gre->encaplimit;

	return 0;
}

/**
 * Set IP6GRE tunnel flowinfo
 * @arg link            Link object
 * @arg flowinfo        flowinfo value
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_flowinfo(struct rtnl_link *link, uint32_t flowinfo)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->flowinfo = flowinfo;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLOWINFO;

	return 0;
}

/**
 * Get IP6GRE flowinfo
 * @arg link            Link object
 * @arg flowinfo        addr to fill in with the flowinfo
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_flowinfo(struct rtnl_link *link, uint32_t *flowinfo)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FLOWINFO);

	*flowinfo = ip6gre->flowinfo;

	return 0;
}

/**
 * Set IP6GRE tunnel flags
 * @arg link            Link object
 * @arg flags           tunnel flags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_flags(struct rtnl_link *link, uint32_t flags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->flags = flags;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLAGS;

	return 0;
}

/**
 * Get IP6GRE flags
 * @arg link            Link object
 * @arg flags           addr to fill in with the tunnel flags
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_flags(struct rtnl_link *link, uint32_t *flags)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FLAGS);

	*flags = ip6gre->flags;

	return 0;
}

/**
 * Set IP6GRE tunnel fwmark
 * @arg link            Link object
 * @arg fwmark          fwmark
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	ip6gre->fwmark = fwmark;
	ip6gre->ip6gre_mask |= IP6GRE_ATTR_FWMARK;

	return 0;
}

/**
 * Get IP6GRE tunnel fwmark
 * @arg link            Link object
 * @arg fwmark          addr to fill in with the fwmark
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_ip6gre_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
{
	struct ip6gre_info *ip6gre = link->l_info;

	IS_IP6GRE_LINK_ASSERT(link);

	HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FWMARK);

	*fwmark = ip6gre->fwmark;

	return 0;
}

static void _nl_init ip6gre_init(void)
{
	rtnl_link_register_info(&ip6gre_info_ops);
}

static void _nl_exit ip6gre_exit(void)
{
	rtnl_link_unregister_info(&ip6gre_info_ops);
}