19#include <netlink-private/netlink.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/ip6tnl.h>
26#include <netlink-private/route/link/api.h>
27#include <linux/if_tunnel.h>
28#include <netinet/in.h>
30#define IP6_TNL_ATTR_LINK (1 << 0)
31#define IP6_TNL_ATTR_LOCAL (1 << 1)
32#define IP6_TNL_ATTR_REMOTE (1 << 2)
33#define IP6_TNL_ATTR_TTL (1 << 3)
34#define IP6_TNL_ATTR_TOS (1 << 4)
35#define IP6_TNL_ATTR_ENCAPLIMIT (1 << 5)
36#define IP6_TNL_ATTR_FLAGS (1 << 6)
37#define IP6_TNL_ATTR_PROTO (1 << 7)
38#define IP6_TNL_ATTR_FLOWINFO (1 << 8)
39#define IP6_TNL_ATTR_FWMARK (1 << 9)
50 struct in6_addr local;
51 struct in6_addr remote;
53 uint32_t ip6_tnl_mask;
56static struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
58 [IFLA_IPTUN_LOCAL] = { .minlen =
sizeof(
struct in6_addr) },
59 [IFLA_IPTUN_REMOTE] = { .minlen =
sizeof(
struct in6_addr) },
60 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
61 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
62 [IFLA_IPTUN_ENCAP_LIMIT] = { .type =
NLA_U8 },
63 [IFLA_IPTUN_FLOWINFO] = { .type =
NLA_U32 },
64 [IFLA_IPTUN_FLAGS] = { .type =
NLA_U32 },
65 [IFLA_IPTUN_PROTO] = { .type =
NLA_U8 },
66 [IFLA_IPTUN_FWMARK] = { .type =
NLA_U32 },
69static int ip6_tnl_alloc(
struct rtnl_link *link)
74 memset(link->l_info, 0,
sizeof(*ip6_tnl));
76 ip6_tnl = calloc(1,
sizeof(*ip6_tnl));
80 link->l_info = ip6_tnl;
86static int ip6_tnl_parse(
struct rtnl_link *link,
struct nlattr *data,
87 struct nlattr *xstats)
89 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
93 NL_DBG(3,
"Parsing IP6_TNL link info\n");
99 err = ip6_tnl_alloc(link);
103 ip6_tnl = link->l_info;
105 if (tb[IFLA_IPTUN_LINK]) {
107 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
110 if (tb[IFLA_IPTUN_LOCAL]) {
111 nla_memcpy(&ip6_tnl->local, tb[IFLA_IPTUN_LOCAL],
sizeof(
struct in6_addr));
112 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
115 if (tb[IFLA_IPTUN_REMOTE]) {
116 nla_memcpy(&ip6_tnl->remote, tb[IFLA_IPTUN_REMOTE],
sizeof(
struct in6_addr));
117 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
120 if (tb[IFLA_IPTUN_TTL]) {
121 ip6_tnl->ttl =
nla_get_u8(tb[IFLA_IPTUN_TTL]);
122 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
125 if (tb[IFLA_IPTUN_TOS]) {
126 ip6_tnl->tos =
nla_get_u8(tb[IFLA_IPTUN_TOS]);
127 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
130 if (tb[IFLA_IPTUN_ENCAP_LIMIT]) {
131 ip6_tnl->encap_limit =
nla_get_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]);
132 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
135 if (tb[IFLA_IPTUN_FLAGS]) {
136 ip6_tnl->flags =
nla_get_u32(tb[IFLA_IPTUN_FLAGS]);
137 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
140 if (tb[IFLA_IPTUN_FLOWINFO]) {
141 ip6_tnl->flowinfo =
nla_get_u32(tb[IFLA_IPTUN_FLOWINFO]);
142 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
145 if (tb[IFLA_IPTUN_PROTO]) {
146 ip6_tnl->proto =
nla_get_u8(tb[IFLA_IPTUN_PROTO]);
147 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
150 if (tb[IFLA_IPTUN_FWMARK]) {
151 ip6_tnl->fwmark =
nla_get_u32(tb[IFLA_IPTUN_FWMARK]);
152 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FWMARK;
161static int ip6_tnl_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
170 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK)
173 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL)
174 NLA_PUT(msg, IFLA_IPTUN_LOCAL,
sizeof(
struct in6_addr), &ip6_tnl->local);
176 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE)
177 NLA_PUT(msg, IFLA_IPTUN_REMOTE,
sizeof(
struct in6_addr), &ip6_tnl->remote);
179 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL)
180 NLA_PUT_U8(msg, IFLA_IPTUN_TTL, ip6_tnl->ttl);
182 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS)
183 NLA_PUT_U8(msg, IFLA_IPTUN_TOS, ip6_tnl->tos);
185 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT)
186 NLA_PUT_U8(msg, IFLA_IPTUN_ENCAP_LIMIT, ip6_tnl->encap_limit);
188 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS)
189 NLA_PUT_U32(msg, IFLA_IPTUN_FLAGS, ip6_tnl->flags);
191 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO)
192 NLA_PUT_U32(msg, IFLA_IPTUN_FLOWINFO, ip6_tnl->flowinfo);
195 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO)
196 NLA_PUT_U8(msg, IFLA_IPTUN_PROTO, ip6_tnl->proto);
200 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK)
201 NLA_PUT_U32(msg, IFLA_IPTUN_FWMARK, ip6_tnl->fwmark);
209static void ip6_tnl_free(
struct rtnl_link *link)
219 nl_dump(p,
"ip6_tnl : %s", link->l_name);
225 char *name, addr[INET6_ADDRSTRLEN];
228 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK) {
232 parent = link_lookup(link->ce_cache, ip6_tnl->link);
237 nl_dump_line(p,
"%s\n", name);
239 nl_dump_line(p,
"%u\n", ip6_tnl->link);
242 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL) {
244 nl_dump_line(p,
"%s\n",
245 _nl_inet_ntop(AF_INET6, &ip6_tnl->local, addr));
248 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE) {
250 nl_dump_line(p,
"%s\n",
251 _nl_inet_ntop(AF_INET6, &ip6_tnl->remote, addr));
254 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL) {
256 nl_dump_line(p,
"%d\n", ip6_tnl->ttl);
259 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS) {
261 nl_dump_line(p,
"%d\n", ip6_tnl->tos);
264 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT) {
266 nl_dump_line(p,
"%d\n", ip6_tnl->encap_limit);
269 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS) {
271 nl_dump_line(p,
" (%x)\n", ip6_tnl->flags);
274 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO) {
276 nl_dump_line(p,
" (%x)\n", ip6_tnl->flowinfo);
279 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO) {
281 nl_dump_line(p,
" (%x)\n", ip6_tnl->proto);
284 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK) {
286 nl_dump_line(p,
"%x\n", ip6_tnl->fwmark);
292 struct ip6_tnl_info *ip6_tnl_dst, *ip6_tnl_src = src->l_info;
301 ip6_tnl_dst = dst->l_info;
303 if (!ip6_tnl_dst || !ip6_tnl_src)
306 memcpy(ip6_tnl_dst, ip6_tnl_src,
sizeof(
struct ip6_tnl_info));
311static struct rtnl_link_info_ops ip6_tnl_info_ops = {
313 .io_alloc = ip6_tnl_alloc,
314 .io_parse = ip6_tnl_parse,
319 .io_clone = ip6_tnl_clone,
320 .io_put_attrs = ip6_tnl_put_attrs,
321 .io_free = ip6_tnl_free,
324#define IS_IP6_TNL_LINK_ASSERT(link)\
325 if ((link)->l_info_ops != &ip6_tnl_info_ops) {\
326 APPBUG("Link is not a ip6_tnl link. set type \"ip6tnl\" first.");\
327 return -NLE_OPNOTSUPP;\
330struct rtnl_link *rtnl_link_ip6_tnl_alloc(
void)
356 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ip6tnl");
372 link = rtnl_link_ip6_tnl_alloc();
396 IS_IP6_TNL_LINK_ASSERT(link);
398 ip6_tnl->link = index;
399 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
414 IS_IP6_TNL_LINK_ASSERT(link);
416 return ip6_tnl->link;
430 IS_IP6_TNL_LINK_ASSERT(link);
432 memcpy(&ip6_tnl->local, addr,
sizeof(
struct in6_addr));
433 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
448 IS_IP6_TNL_LINK_ASSERT(link);
450 memcpy(addr, &ip6_tnl->local,
sizeof(
struct in6_addr));
466 IS_IP6_TNL_LINK_ASSERT(link);
468 memcpy(&ip6_tnl->remote, addr,
sizeof(
struct in6_addr));
469 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
484 IS_IP6_TNL_LINK_ASSERT(link);
486 memcpy(addr, &ip6_tnl->remote,
sizeof(
struct in6_addr));
502 IS_IP6_TNL_LINK_ASSERT(link);
505 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
520 IS_IP6_TNL_LINK_ASSERT(link);
536 IS_IP6_TNL_LINK_ASSERT(link);
539 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
554 IS_IP6_TNL_LINK_ASSERT(link);
570 IS_IP6_TNL_LINK_ASSERT(link);
572 ip6_tnl->encap_limit = encap_limit;
573 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
588 IS_IP6_TNL_LINK_ASSERT(link);
590 return ip6_tnl->encap_limit;
604 IS_IP6_TNL_LINK_ASSERT(link);
606 ip6_tnl->flowinfo = flowinfo;
607 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
622 IS_IP6_TNL_LINK_ASSERT(link);
624 return ip6_tnl->flowinfo;
638 IS_IP6_TNL_LINK_ASSERT(link);
640 ip6_tnl->flags = flags;
641 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
656 IS_IP6_TNL_LINK_ASSERT(link);
658 return ip6_tnl->flags;
672 IS_IP6_TNL_LINK_ASSERT(link);
674 ip6_tnl->proto = proto;
675 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
690 IS_IP6_TNL_LINK_ASSERT(link);
692 return ip6_tnl->proto;
706 IS_IP6_TNL_LINK_ASSERT(link);
708 ip6_tnl->fwmark = fwmark;
709 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FWMARK;
725 IS_IP6_TNL_LINK_ASSERT(link);
727 if (!(ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK))
730 *fwmark = ip6_tnl->fwmark;
735static void __init ip6_tnl_init(
void)
740static void __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)
Return 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.