19#include "nl-default.h"
21#include <linux/if_tunnel.h>
23#include <netlink/netlink.h>
24#include <netlink/attr.h>
25#include <netlink/utils.h>
26#include <netlink/object.h>
27#include <netlink/route/rtnl.h>
28#include <netlink/route/link/ip6tnl.h>
33#define IP6_TNL_ATTR_LINK (1 << 0)
34#define IP6_TNL_ATTR_LOCAL (1 << 1)
35#define IP6_TNL_ATTR_REMOTE (1 << 2)
36#define IP6_TNL_ATTR_TTL (1 << 3)
37#define IP6_TNL_ATTR_TOS (1 << 4)
38#define IP6_TNL_ATTR_ENCAPLIMIT (1 << 5)
39#define IP6_TNL_ATTR_FLAGS (1 << 6)
40#define IP6_TNL_ATTR_PROTO (1 << 7)
41#define IP6_TNL_ATTR_FLOWINFO (1 << 8)
42#define IP6_TNL_ATTR_FWMARK (1 << 9)
53 struct in6_addr local;
54 struct in6_addr remote;
56 uint32_t ip6_tnl_mask;
62 [IFLA_IPTUN_REMOTE] = { .minlen =
sizeof(
struct in6_addr) },
63 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
64 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
65 [IFLA_IPTUN_ENCAP_LIMIT] = { .type =
NLA_U8 },
66 [IFLA_IPTUN_FLOWINFO] = { .type =
NLA_U32 },
67 [IFLA_IPTUN_FLAGS] = { .type =
NLA_U32 },
68 [IFLA_IPTUN_PROTO] = { .type =
NLA_U8 },
69 [IFLA_IPTUN_FWMARK] = { .type =
NLA_U32 },
72static int ip6_tnl_alloc(
struct rtnl_link *link)
77 memset(link->l_info, 0,
sizeof(*ip6_tnl));
79 ip6_tnl = calloc(1,
sizeof(*ip6_tnl));
83 link->l_info = ip6_tnl;
89static int ip6_tnl_parse(
struct rtnl_link *link,
struct nlattr *data,
90 struct nlattr *xstats)
92 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
96 NL_DBG(3,
"Parsing IP6_TNL link info\n");
102 err = ip6_tnl_alloc(link);
106 ip6_tnl = link->l_info;
108 if (tb[IFLA_IPTUN_LINK]) {
110 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
113 if (tb[IFLA_IPTUN_LOCAL]) {
114 nla_memcpy(&ip6_tnl->local, tb[IFLA_IPTUN_LOCAL],
sizeof(
struct in6_addr));
115 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
118 if (tb[IFLA_IPTUN_REMOTE]) {
119 nla_memcpy(&ip6_tnl->remote, tb[IFLA_IPTUN_REMOTE],
sizeof(
struct in6_addr));
120 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
123 if (tb[IFLA_IPTUN_TTL]) {
124 ip6_tnl->ttl =
nla_get_u8(tb[IFLA_IPTUN_TTL]);
125 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
128 if (tb[IFLA_IPTUN_TOS]) {
129 ip6_tnl->tos =
nla_get_u8(tb[IFLA_IPTUN_TOS]);
130 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
133 if (tb[IFLA_IPTUN_ENCAP_LIMIT]) {
134 ip6_tnl->encap_limit =
nla_get_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]);
135 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
138 if (tb[IFLA_IPTUN_FLAGS]) {
139 ip6_tnl->flags =
nla_get_u32(tb[IFLA_IPTUN_FLAGS]);
140 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
143 if (tb[IFLA_IPTUN_FLOWINFO]) {
144 ip6_tnl->flowinfo =
nla_get_u32(tb[IFLA_IPTUN_FLOWINFO]);
145 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
148 if (tb[IFLA_IPTUN_PROTO]) {
149 ip6_tnl->proto =
nla_get_u8(tb[IFLA_IPTUN_PROTO]);
150 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
153 if (tb[IFLA_IPTUN_FWMARK]) {
154 ip6_tnl->fwmark =
nla_get_u32(tb[IFLA_IPTUN_FWMARK]);
155 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FWMARK;
164static int ip6_tnl_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
173 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK)
176 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL)
177 NLA_PUT(msg, IFLA_IPTUN_LOCAL,
sizeof(
struct in6_addr), &ip6_tnl->local);
179 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE)
180 NLA_PUT(msg, IFLA_IPTUN_REMOTE,
sizeof(
struct in6_addr), &ip6_tnl->remote);
182 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL)
183 NLA_PUT_U8(msg, IFLA_IPTUN_TTL, ip6_tnl->ttl);
185 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS)
186 NLA_PUT_U8(msg, IFLA_IPTUN_TOS, ip6_tnl->tos);
188 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT)
189 NLA_PUT_U8(msg, IFLA_IPTUN_ENCAP_LIMIT, ip6_tnl->encap_limit);
191 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS)
192 NLA_PUT_U32(msg, IFLA_IPTUN_FLAGS, ip6_tnl->flags);
194 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO)
195 NLA_PUT_U32(msg, IFLA_IPTUN_FLOWINFO, ip6_tnl->flowinfo);
198 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO)
199 NLA_PUT_U8(msg, IFLA_IPTUN_PROTO, ip6_tnl->proto);
203 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK)
204 NLA_PUT_U32(msg, IFLA_IPTUN_FWMARK, ip6_tnl->fwmark);
212static void ip6_tnl_free(
struct rtnl_link *link)
222 nl_dump(p,
"ip6_tnl : %s", link->l_name);
228 char *name, addr[INET6_ADDRSTRLEN];
231 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK) {
235 parent = link_lookup(link->ce_cache, ip6_tnl->link);
240 nl_dump_line(p,
"%s\n", name);
242 nl_dump_line(p,
"%u\n", ip6_tnl->link);
245 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL) {
247 nl_dump_line(p,
"%s\n",
248 _nl_inet_ntop(AF_INET6, &ip6_tnl->local, addr));
251 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE) {
253 nl_dump_line(p,
"%s\n",
254 _nl_inet_ntop(AF_INET6, &ip6_tnl->remote, addr));
257 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL) {
259 nl_dump_line(p,
"%d\n", ip6_tnl->ttl);
262 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS) {
264 nl_dump_line(p,
"%d\n", ip6_tnl->tos);
267 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT) {
269 nl_dump_line(p,
"%d\n", ip6_tnl->encap_limit);
272 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS) {
274 nl_dump_line(p,
" (%x)\n", ip6_tnl->flags);
277 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO) {
279 nl_dump_line(p,
" (%x)\n", ip6_tnl->flowinfo);
282 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO) {
284 nl_dump_line(p,
" (%x)\n", ip6_tnl->proto);
287 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK) {
289 nl_dump_line(p,
"%x\n", ip6_tnl->fwmark);
295 struct ip6_tnl_info *ip6_tnl_dst, *ip6_tnl_src = src->l_info;
304 ip6_tnl_dst = dst->l_info;
306 if (!ip6_tnl_dst || !ip6_tnl_src)
309 memcpy(ip6_tnl_dst, ip6_tnl_src,
sizeof(
struct ip6_tnl_info));
316 .io_alloc = ip6_tnl_alloc,
317 .io_parse = ip6_tnl_parse,
322 .io_clone = ip6_tnl_clone,
323 .io_put_attrs = ip6_tnl_put_attrs,
324 .io_free = ip6_tnl_free,
327#define IS_IP6_TNL_LINK_ASSERT(link)\
328 if ((link)->l_info_ops != &ip6_tnl_info_ops) {\
329 APPBUG("Link is not a ip6_tnl link. set type \"ip6tnl\" first.");\
330 return -NLE_OPNOTSUPP;\
333struct rtnl_link *rtnl_link_ip6_tnl_alloc(
void)
359 return link->l_info_ops && !strcmp(link->l_info_ops->
io_name,
"ip6tnl");
375 link = rtnl_link_ip6_tnl_alloc();
399 IS_IP6_TNL_LINK_ASSERT(link);
401 ip6_tnl->link = index;
402 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
417 IS_IP6_TNL_LINK_ASSERT(link);
419 return ip6_tnl->link;
433 IS_IP6_TNL_LINK_ASSERT(link);
435 memcpy(&ip6_tnl->local, addr,
sizeof(
struct in6_addr));
436 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
451 IS_IP6_TNL_LINK_ASSERT(link);
453 memcpy(addr, &ip6_tnl->local,
sizeof(
struct in6_addr));
469 IS_IP6_TNL_LINK_ASSERT(link);
471 memcpy(&ip6_tnl->remote, addr,
sizeof(
struct in6_addr));
472 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
487 IS_IP6_TNL_LINK_ASSERT(link);
489 memcpy(addr, &ip6_tnl->remote,
sizeof(
struct in6_addr));
505 IS_IP6_TNL_LINK_ASSERT(link);
508 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
523 IS_IP6_TNL_LINK_ASSERT(link);
539 IS_IP6_TNL_LINK_ASSERT(link);
542 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
557 IS_IP6_TNL_LINK_ASSERT(link);
573 IS_IP6_TNL_LINK_ASSERT(link);
575 ip6_tnl->encap_limit = encap_limit;
576 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
591 IS_IP6_TNL_LINK_ASSERT(link);
593 return ip6_tnl->encap_limit;
607 IS_IP6_TNL_LINK_ASSERT(link);
609 ip6_tnl->flowinfo = flowinfo;
610 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
625 IS_IP6_TNL_LINK_ASSERT(link);
627 return ip6_tnl->flowinfo;
641 IS_IP6_TNL_LINK_ASSERT(link);
643 ip6_tnl->flags = flags;
644 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
659 IS_IP6_TNL_LINK_ASSERT(link);
661 return ip6_tnl->flags;
675 IS_IP6_TNL_LINK_ASSERT(link);
677 ip6_tnl->proto = proto;
678 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
693 IS_IP6_TNL_LINK_ASSERT(link);
695 return ip6_tnl->proto;
709 IS_IP6_TNL_LINK_ASSERT(link);
711 ip6_tnl->fwmark = fwmark;
712 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FWMARK;
728 IS_IP6_TNL_LINK_ASSERT(link);
730 if (!(ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK))
733 *fwmark = ip6_tnl->fwmark;
738static void _nl_init ip6_tnl_init(
void)
743static void _nl_exit ip6_tnl_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#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_ip6_tnl_set_remote(struct rtnl_link *link, struct in6_addr *addr)
Set IP6_TNL tunnel remote address.
uint8_t rtnl_link_ip6_tnl_get_proto(struct rtnl_link *link)
Get IP6_TNL proto.
uint32_t rtnl_link_ip6_tnl_get_flowinfo(struct rtnl_link *link)
Get IP6_TNL flowinfo.
int rtnl_link_is_ip6_tnl(struct rtnl_link *link)
Check if link is a IP6_TNL link.
int rtnl_link_ip6_tnl_set_link(struct rtnl_link *link, uint32_t index)
Set IP6_TNL tunnel interface index.
int rtnl_link_ip6_tnl_add(struct nl_sock *sk, const char *name)
Create a new ip6_tnl tunnel device.
uint8_t rtnl_link_ip6_tnl_get_ttl(struct rtnl_link *link)
Get IP6_TNL tunnel ttl.
int rtnl_link_ip6_tnl_get_remote(struct rtnl_link *link, struct in6_addr *addr)
Get IP6_TNL tunnel remote address.
uint32_t rtnl_link_ip6_tnl_get_flags(struct rtnl_link *link)
Get IP6_TNL path flags.
int rtnl_link_ip6_tnl_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
Set IP6_TNL tunnel fwmark.
uint8_t rtnl_link_ip6_tnl_get_encaplimit(struct rtnl_link *link)
Get IP6_TNL encaplimit.
int rtnl_link_ip6_tnl_set_flags(struct rtnl_link *link, uint32_t flags)
Set IP6_TNL tunnel flags.
int rtnl_link_ip6_tnl_set_local(struct rtnl_link *link, struct in6_addr *addr)
Set IP6_TNL tunnel local address.
int rtnl_link_ip6_tnl_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set IP6_TNL tunnel ttl.
int rtnl_link_ip6_tnl_set_flowinfo(struct rtnl_link *link, uint32_t flowinfo)
Set IP6_TNL tunnel flowinfo.
int rtnl_link_ip6_tnl_get_local(struct rtnl_link *link, struct in6_addr *addr)
Get IP6_TNL tunnel local address.
int rtnl_link_ip6_tnl_set_tos(struct rtnl_link *link, uint8_t tos)
Set IP6_TNL tunnel tos.
int rtnl_link_ip6_tnl_set_proto(struct rtnl_link *link, uint8_t proto)
Set IP6_TNL tunnel proto.
int rtnl_link_ip6_tnl_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
Get IP6_TNL tunnel fwmark.
int rtnl_link_ip6_tnl_set_encaplimit(struct rtnl_link *link, uint8_t encap_limit)
Set IP6_TNL tunnel encap limit.
uint32_t rtnl_link_ip6_tnl_get_link(struct rtnl_link *link)
Get IP6_TNL tunnel interface index.
uint8_t rtnl_link_ip6_tnl_get_tos(struct rtnl_link *link)
Get IP6_TNL tunnel tos.
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.
uint16_t type
Type of attribute or NLA_UNSPEC.
Available operations to modules implementing a link info type.
char * io_name
Name of link info type, must match name on kernel side.