16#include "nl-default.h"
18#include <linux/if_tunnel.h>
20#include <netlink/netlink.h>
21#include <netlink/attr.h>
22#include <netlink/utils.h>
23#include <netlink/object.h>
24#include <netlink/route/rtnl.h>
25#include <netlink/route/link/ip6gre.h>
29#include "nl-aux-route/nl-route.h"
31#define IP6GRE_ATTR_LINK (1 << 0)
32#define IP6GRE_ATTR_IFLAGS (1 << 1)
33#define IP6GRE_ATTR_OFLAGS (1 << 2)
34#define IP6GRE_ATTR_IKEY (1 << 3)
35#define IP6GRE_ATTR_OKEY (1 << 4)
36#define IP6GRE_ATTR_LOCAL (1 << 5)
37#define IP6GRE_ATTR_REMOTE (1 << 6)
38#define IP6GRE_ATTR_TTL (1 << 7)
39#define IP6GRE_ATTR_ENCAPLIMIT (1 << 8)
40#define IP6GRE_ATTR_FLOWINFO (1 << 9)
41#define IP6GRE_ATTR_FLAGS (1 << 10)
42#define IP6GRE_ATTR_FWMARK (1 << 11)
55 struct in6_addr local;
56 struct in6_addr remote;
61static struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
62 [IFLA_GRE_LINK] = { .type =
NLA_U32 },
63 [IFLA_GRE_IFLAGS] = { .type =
NLA_U16 },
64 [IFLA_GRE_OFLAGS] = { .type =
NLA_U16 },
65 [IFLA_GRE_IKEY] = { .type =
NLA_U32 },
66 [IFLA_GRE_OKEY] = { .type =
NLA_U32 },
67 [IFLA_GRE_LOCAL] = { .minlen =
sizeof(
struct in6_addr) },
68 [IFLA_GRE_REMOTE] = { .minlen =
sizeof(
struct in6_addr) },
69 [IFLA_GRE_TTL] = { .type =
NLA_U8 },
70 [IFLA_GRE_ENCAP_LIMIT] = { .type =
NLA_U8 },
71 [IFLA_GRE_FLOWINFO] = { .type =
NLA_U32 },
72 [IFLA_GRE_FLAGS] = { .type =
NLA_U32 },
73 [IFLA_GRE_FWMARK] = { .type =
NLA_U32 },
76static int ip6gre_alloc(
struct rtnl_link *link)
81 memset(link->l_info, 0,
sizeof(*ip6gre));
83 ip6gre = calloc(1,
sizeof(*ip6gre));
87 link->l_info = ip6gre;
93static int ip6gre_parse(
struct rtnl_link *link,
struct nlattr *data,
94 struct nlattr *xstats)
96 struct nlattr *tb[IFLA_GRE_MAX + 1];
100 NL_DBG(3,
"Parsing IP6GRE link info\n");
106 err = ip6gre_alloc(link);
110 ip6gre = link->l_info;
112 if (tb[IFLA_GRE_LINK]) {
114 ip6gre->ip6gre_mask |= IP6GRE_ATTR_LINK;
117 if (tb[IFLA_GRE_IFLAGS]) {
119 ip6gre->ip6gre_mask |= IP6GRE_ATTR_IFLAGS;
122 if (tb[IFLA_GRE_OFLAGS]) {
124 ip6gre->ip6gre_mask |= IP6GRE_ATTR_OFLAGS;
127 if (tb[IFLA_GRE_IKEY]) {
129 ip6gre->ip6gre_mask |= IP6GRE_ATTR_IKEY;
132 if (tb[IFLA_GRE_OKEY]) {
134 ip6gre->ip6gre_mask |= IP6GRE_ATTR_OKEY;
137 if (tb[IFLA_GRE_LOCAL]) {
138 nla_memcpy(&ip6gre->local, tb[IFLA_GRE_LOCAL],
sizeof(
struct in6_addr));
139 ip6gre->ip6gre_mask |= IP6GRE_ATTR_LOCAL;
142 if (tb[IFLA_GRE_REMOTE]) {
143 nla_memcpy(&ip6gre->remote, tb[IFLA_GRE_REMOTE],
sizeof(
struct in6_addr));
144 ip6gre->ip6gre_mask |= IP6GRE_ATTR_REMOTE;
147 if (tb[IFLA_GRE_TTL]) {
149 ip6gre->ip6gre_mask |= IP6GRE_ATTR_TTL;
152 if (tb[IFLA_GRE_ENCAP_LIMIT]) {
153 ip6gre->encaplimit =
nla_get_u8(tb[IFLA_GRE_ENCAP_LIMIT]);
154 ip6gre->ip6gre_mask |= IP6GRE_ATTR_ENCAPLIMIT;
157 if (tb[IFLA_GRE_FLOWINFO]) {
158 ip6gre->flowinfo =
nla_get_u32(tb[IFLA_GRE_FLOWINFO]);
159 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLOWINFO;
162 if (tb[IFLA_GRE_FLAGS]) {
164 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLAGS;
167 if (tb[IFLA_GRE_FWMARK]) {
169 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FWMARK;
178static int ip6gre_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
187 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LINK)
190 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IFLAGS)
193 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OFLAGS)
196 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IKEY)
199 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OKEY)
202 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LOCAL)
203 NLA_PUT(msg, IFLA_GRE_LOCAL,
sizeof(
struct in6_addr), &ip6gre->local);
205 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_REMOTE)
206 NLA_PUT(msg, IFLA_GRE_REMOTE,
sizeof(
struct in6_addr), &ip6gre->remote);
208 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_TTL)
211 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_ENCAPLIMIT)
212 NLA_PUT_U8(msg, IFLA_GRE_ENCAP_LIMIT, ip6gre->encaplimit);
214 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLOWINFO)
215 NLA_PUT_U32(msg, IFLA_GRE_FLOWINFO, ip6gre->flowinfo);
217 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLAGS)
220 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FWMARK)
230static void ip6gre_free(
struct rtnl_link *link)
240 nl_dump(p,
"ip6gre : %s", link->l_name);
246 char addr[INET6_ADDRSTRLEN];
248 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LINK) {
249 _nl_auto_rtnl_link
struct rtnl_link *parent = NULL;
255 parent = link_lookup(link->ce_cache, ip6gre->link);
260 nl_dump_line(p,
"%s\n", name);
262 nl_dump_line(p,
"%u\n", ip6gre->link);
265 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IFLAGS) {
267 nl_dump_line(p,
"%x\n", ip6gre->iflags);
270 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OFLAGS) {
272 nl_dump_line(p,
"%x\n", ip6gre->oflags);
275 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_IKEY) {
277 nl_dump_line(p,
"%x\n",ip6gre->ikey);
280 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_OKEY) {
282 nl_dump_line(p,
"%x\n", ip6gre->okey);
285 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_LOCAL) {
287 nl_dump_line(p,
"%s\n",
288 _nl_inet_ntop(AF_INET6, &ip6gre->local, addr));
291 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_REMOTE) {
293 nl_dump_line(p,
"%s\n",
294 _nl_inet_ntop(AF_INET6, &ip6gre->remote, addr));
297 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_TTL) {
299 nl_dump_line(p,
"%u\n", ip6gre->ttl);
302 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_ENCAPLIMIT) {
304 nl_dump_line(p,
"%u\n", ip6gre->encaplimit);
307 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLOWINFO) {
309 nl_dump_line(p,
"%x\n", ip6gre->flowinfo);
312 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FLAGS) {
314 nl_dump_line(p,
"%x\n", ip6gre->flags);
317 if (ip6gre->ip6gre_mask & IP6GRE_ATTR_FWMARK) {
319 nl_dump_line(p,
"%x\n", ip6gre->fwmark);
325 struct ip6gre_info *ip6gre_dst, *ip6gre_src = src->l_info;
334 ip6gre_dst = dst->l_info;
336 if (!ip6gre_dst || !ip6gre_src)
339 memcpy(ip6gre_dst, ip6gre_src,
sizeof(
struct ip6gre_info));
346 .io_alloc = ip6gre_alloc,
347 .io_parse = ip6gre_parse,
352 .io_clone = ip6gre_clone,
353 .io_put_attrs = ip6gre_put_attrs,
354 .io_free = ip6gre_free,
357#define IS_IP6GRE_LINK_ASSERT(link) \
358 if ((link)->l_info_ops != &ip6gre_info_ops) { \
359 APPBUG("Link is not a ip6gre link. set type \"ip6gre\" first.");\
360 return -NLE_OPNOTSUPP; \
363#define HAS_IP6GRE_ATTR_ASSERT(link,attr) \
364 if (!((link)->ip6gre_mask & (attr))) \
367struct rtnl_link *rtnl_link_ip6gre_alloc(
void)
393 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"ip6gre");
409 link = rtnl_link_ip6gre_alloc();
433 IS_IP6GRE_LINK_ASSERT(link);
435 ip6gre->link = index;
436 ip6gre->ip6gre_mask |= IP6GRE_ATTR_LINK;
452 IS_IP6GRE_LINK_ASSERT(link);
454 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_LINK);
456 *index = ip6gre->link;
472 IS_IP6GRE_LINK_ASSERT(link);
474 ip6gre->iflags = iflags;
475 ip6gre->ip6gre_mask |= IP6GRE_ATTR_IFLAGS;
491 IS_IP6GRE_LINK_ASSERT(link);
493 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_IFLAGS);
495 *iflags = ip6gre->iflags;
511 IS_IP6GRE_LINK_ASSERT(link);
513 ip6gre->oflags = oflags;
514 ip6gre->ip6gre_mask |= IP6GRE_ATTR_OFLAGS;
530 IS_IP6GRE_LINK_ASSERT(link);
532 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_OFLAGS);
534 *oflags = ip6gre->oflags;
550 IS_IP6GRE_LINK_ASSERT(link);
553 ip6gre->ip6gre_mask |= IP6GRE_ATTR_IKEY;
569 IS_IP6GRE_LINK_ASSERT(link);
571 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_IKEY);
573 *ikey = ip6gre->ikey;
589 IS_IP6GRE_LINK_ASSERT(link);
592 ip6gre->ip6gre_mask |= IP6GRE_ATTR_OKEY;
608 IS_IP6GRE_LINK_ASSERT(link);
610 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_OKEY);
612 *okey = ip6gre->okey;
628 IS_IP6GRE_LINK_ASSERT(link);
630 memcpy(&ip6gre->local, local,
sizeof(
struct in6_addr));
631 ip6gre->ip6gre_mask |= IP6GRE_ATTR_LOCAL;
647 IS_IP6GRE_LINK_ASSERT(link);
649 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_LOCAL);
651 memcpy(local, &ip6gre->local,
sizeof(
struct in6_addr));
667 IS_IP6GRE_LINK_ASSERT(link);
669 memcpy(&ip6gre->remote, remote,
sizeof(
struct in6_addr));
670 ip6gre->ip6gre_mask |= IP6GRE_ATTR_REMOTE;
686 IS_IP6GRE_LINK_ASSERT(link);
688 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_REMOTE);
690 memcpy(remote, &ip6gre->remote,
sizeof(
struct in6_addr));
706 IS_IP6GRE_LINK_ASSERT(link);
709 ip6gre->ip6gre_mask |= IP6GRE_ATTR_TTL;
725 IS_IP6GRE_LINK_ASSERT(link);
727 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_TTL);
745 IS_IP6GRE_LINK_ASSERT(link);
747 ip6gre->encaplimit = encaplimit;
748 ip6gre->ip6gre_mask |= IP6GRE_ATTR_ENCAPLIMIT;
764 IS_IP6GRE_LINK_ASSERT(link);
766 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_ENCAPLIMIT);
768 *encaplimit = ip6gre->encaplimit;
784 IS_IP6GRE_LINK_ASSERT(link);
786 ip6gre->flowinfo = flowinfo;
787 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLOWINFO;
803 IS_IP6GRE_LINK_ASSERT(link);
805 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FLOWINFO);
807 *flowinfo = ip6gre->flowinfo;
823 IS_IP6GRE_LINK_ASSERT(link);
825 ip6gre->flags = flags;
826 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FLAGS;
842 IS_IP6GRE_LINK_ASSERT(link);
844 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FLAGS);
846 *flags = ip6gre->flags;
862 IS_IP6GRE_LINK_ASSERT(link);
864 ip6gre->fwmark = fwmark;
865 ip6gre->ip6gre_mask |= IP6GRE_ATTR_FWMARK;
881 IS_IP6GRE_LINK_ASSERT(link);
883 HAS_IP6GRE_ATTR_ASSERT(ip6gre, IP6GRE_ATTR_FWMARK);
885 *fwmark = ip6gre->fwmark;
890static void _nl_init ip6gre_init(
void)
895static void _nl_exit ip6gre_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int rtnl_link_ip6gre_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
Set IP6GRE tunnel fwmark.
int rtnl_link_ip6gre_set_flags(struct rtnl_link *link, uint32_t flags)
Set IP6GRE tunnel flags.
int rtnl_link_ip6gre_set_remote(struct rtnl_link *link, struct in6_addr *remote)
Set IP6GRE tunnel remote address.
int rtnl_link_ip6gre_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set IP6GRE tunnel ttl.
int rtnl_link_ip6gre_get_oflags(struct rtnl_link *link, uint16_t *oflags)
Get IP6GRE tunnel oflags.
int rtnl_link_ip6gre_set_okey(struct rtnl_link *link, uint32_t okey)
Set IP6GRE tunnel set okey.
int rtnl_link_ip6gre_get_ttl(struct rtnl_link *link, uint8_t *ttl)
Set IP6GRE tunnel ttl.
int rtnl_link_ip6gre_set_iflags(struct rtnl_link *link, uint16_t iflags)
Set IP6GRE tunnel set iflags.
int rtnl_link_ip6gre_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
Get IP6GRE tunnel fwmark.
int rtnl_link_ip6gre_set_link(struct rtnl_link *link, uint32_t index)
Set IP6GRE tunnel interface index.
int rtnl_link_ip6gre_get_ikey(struct rtnl_link *link, uint32_t *ikey)
Get IP6GRE tunnel ikey.
int rtnl_link_ip6gre_get_iflags(struct rtnl_link *link, uint16_t *iflags)
Get IP6GRE tunnel iflags.
int rtnl_link_ip6gre_get_flowinfo(struct rtnl_link *link, uint32_t *flowinfo)
Get IP6GRE flowinfo.
int rtnl_link_ip6gre_set_flowinfo(struct rtnl_link *link, uint32_t flowinfo)
Set IP6GRE tunnel flowinfo.
int rtnl_link_ip6gre_set_encaplimit(struct rtnl_link *link, uint8_t encaplimit)
Set IP6GRE tunnel encap limit.
int rtnl_link_ip6gre_get_flags(struct rtnl_link *link, uint32_t *flags)
Get IP6GRE flags.
int rtnl_link_ip6gre_set_ikey(struct rtnl_link *link, uint32_t ikey)
Set IP6GRE tunnel set ikey.
int rtnl_link_ip6gre_set_local(struct rtnl_link *link, struct in6_addr *local)
Set IP6GRE tunnel local address.
int rtnl_link_ip6gre_get_link(struct rtnl_link *link, uint32_t *index)
Get IP6GRE tunnel interface index.
int rtnl_link_ip6gre_add(struct nl_sock *sk, const char *name)
Create a new IP6GRE tunnel device.
int rtnl_link_ip6gre_get_local(struct rtnl_link *link, struct in6_addr *local)
Get IP6GRE tunnel local address.
int rtnl_link_ip6gre_get_okey(struct rtnl_link *link, uint32_t *okey)
Get IP6GRE tunnel okey.
int rtnl_link_ip6gre_set_oflags(struct rtnl_link *link, uint16_t oflags)
Set IP6GRE tunnel set oflags.
int rtnl_link_ip6gre_get_encaplimit(struct rtnl_link *link, uint8_t *encaplimit)
Get IP6GRE tunnel encap limit.
int rtnl_link_is_ip6gre(struct rtnl_link *link)
Check if link is a IP6GRE link.
int rtnl_link_ip6gre_get_remote(struct rtnl_link *link, struct in6_addr *remote)
Get IP6GRE tunnel remote address.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
Available operations to modules implementing a link info type.
char * io_name
Name of link info type, must match name on kernel side.