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;
56 static 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 },
69 static 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;
86 static 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;
161 static 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);
209 static 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) {
245 if(inet_ntop(AF_INET6, &ip6_tnl->local, addr, INET6_ADDRSTRLEN))
246 nl_dump_line(p,
"%s\n", addr);
248 nl_dump_line(p,
"%#x\n", ip6_tnl->local);
251 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE) {
254 if(inet_ntop(AF_INET6, &ip6_tnl->remote, addr, INET6_ADDRSTRLEN))
255 nl_dump_line(p,
"%s\n", addr);
257 nl_dump_line(p,
"%#x\n", ip6_tnl->remote);
260 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL) {
262 nl_dump_line(p,
"%d\n", ip6_tnl->ttl);
265 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS) {
267 nl_dump_line(p,
"%d\n", ip6_tnl->tos);
270 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT) {
272 nl_dump_line(p,
"%d\n", ip6_tnl->encap_limit);
275 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS) {
277 nl_dump_line(p,
" (%x)\n", ip6_tnl->flags);
280 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO) {
282 nl_dump_line(p,
" (%x)\n", ip6_tnl->flowinfo);
285 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO) {
287 nl_dump_line(p,
" (%x)\n", ip6_tnl->proto);
290 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK) {
292 nl_dump_line(p,
"%x\n", ip6_tnl->fwmark);
298 struct ip6_tnl_info *ip6_tnl_dst, *ip6_tnl_src = src->l_info;
307 ip6_tnl_dst = dst->l_info;
309 if (!ip6_tnl_dst || !ip6_tnl_src)
312 memcpy(ip6_tnl_dst, ip6_tnl_src,
sizeof(
struct ip6_tnl_info));
317 static struct rtnl_link_info_ops ip6_tnl_info_ops = {
319 .io_alloc = ip6_tnl_alloc,
320 .io_parse = ip6_tnl_parse,
325 .io_clone = ip6_tnl_clone,
326 .io_put_attrs = ip6_tnl_put_attrs,
327 .io_free = ip6_tnl_free,
330 #define IS_IP6_TNL_LINK_ASSERT(link)\
331 if ((link)->l_info_ops != &ip6_tnl_info_ops) {\
332 APPBUG("Link is not a ip6_tnl link. set type \"ip6tnl\" first.");\
333 return -NLE_OPNOTSUPP;\
336 struct rtnl_link *rtnl_link_ip6_tnl_alloc(
void)
362 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ip6tnl");
378 link = rtnl_link_ip6_tnl_alloc();
402 IS_IP6_TNL_LINK_ASSERT(link);
404 ip6_tnl->link = index;
405 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
420 IS_IP6_TNL_LINK_ASSERT(link);
422 return ip6_tnl->link;
436 IS_IP6_TNL_LINK_ASSERT(link);
438 memcpy(&ip6_tnl->local, addr,
sizeof(
struct in6_addr));
439 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
454 IS_IP6_TNL_LINK_ASSERT(link);
456 memcpy(addr, &ip6_tnl->local,
sizeof(
struct in6_addr));
472 IS_IP6_TNL_LINK_ASSERT(link);
474 memcpy(&ip6_tnl->remote, addr,
sizeof(
struct in6_addr));
475 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
490 IS_IP6_TNL_LINK_ASSERT(link);
492 memcpy(addr, &ip6_tnl->remote,
sizeof(
struct in6_addr));
508 IS_IP6_TNL_LINK_ASSERT(link);
511 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
526 IS_IP6_TNL_LINK_ASSERT(link);
542 IS_IP6_TNL_LINK_ASSERT(link);
545 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
560 IS_IP6_TNL_LINK_ASSERT(link);
576 IS_IP6_TNL_LINK_ASSERT(link);
578 ip6_tnl->encap_limit = encap_limit;
579 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
594 IS_IP6_TNL_LINK_ASSERT(link);
596 return ip6_tnl->encap_limit;
610 IS_IP6_TNL_LINK_ASSERT(link);
612 ip6_tnl->flowinfo = flowinfo;
613 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
628 IS_IP6_TNL_LINK_ASSERT(link);
630 return ip6_tnl->flowinfo;
644 IS_IP6_TNL_LINK_ASSERT(link);
646 ip6_tnl->flags = flags;
647 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
662 IS_IP6_TNL_LINK_ASSERT(link);
664 return ip6_tnl->flags;
678 IS_IP6_TNL_LINK_ASSERT(link);
680 ip6_tnl->proto = proto;
681 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
696 IS_IP6_TNL_LINK_ASSERT(link);
698 return ip6_tnl->proto;
712 IS_IP6_TNL_LINK_ASSERT(link);
714 ip6_tnl->fwmark = fwmark;
715 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FWMARK;
731 IS_IP6_TNL_LINK_ASSERT(link);
733 if (!(ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FWMARK))
736 *fwmark = ip6_tnl->fwmark;
741 static void __init ip6_tnl_init(
void)
746 static 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.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set 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.
struct rtnl_link * rtnl_link_alloc(void)
Allocate 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.