25 #include <netlink-private/netlink.h> 26 #include <netlink/netlink.h> 27 #include <netlink/attr.h> 28 #include <netlink/utils.h> 29 #include <netlink/object.h> 30 #include <netlink/route/rtnl.h> 31 #include <netlink/route/link/ip6tnl.h> 32 #include <netlink-private/route/link/api.h> 33 #include <linux/if_tunnel.h> 34 #include <netinet/in.h> 36 #define IP6_TNL_ATTR_LINK (1 << 0) 37 #define IP6_TNL_ATTR_LOCAL (1 << 1) 38 #define IP6_TNL_ATTR_REMOTE (1 << 2) 39 #define IP6_TNL_ATTR_TTL (1 << 3) 40 #define IP6_TNL_ATTR_TOS (1 << 4) 41 #define IP6_TNL_ATTR_ENCAPLIMIT (1 << 5) 42 #define IP6_TNL_ATTR_FLAGS (1 << 6) 43 #define IP6_TNL_ATTR_PROTO (1 << 7) 44 #define IP6_TNL_ATTR_FLOWINFO (1 << 8) 55 struct in6_addr local;
56 struct in6_addr remote;
57 uint32_t ip6_tnl_mask;
60 static struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
62 [IFLA_IPTUN_LOCAL] = { .minlen =
sizeof(
struct in6_addr) },
63 [IFLA_IPTUN_REMOTE] = { .minlen =
sizeof(
struct in6_addr) },
64 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
65 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
66 [IFLA_IPTUN_ENCAP_LIMIT] = { .type =
NLA_U8 },
67 [IFLA_IPTUN_FLOWINFO] = { .type =
NLA_U32 },
68 [IFLA_IPTUN_FLAGS] = { .type =
NLA_U32 },
69 [IFLA_IPTUN_PROTO] = { .type =
NLA_U8 },
72 static 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;
89 static 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;
159 static int ip6_tnl_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
168 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK)
171 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL)
172 NLA_PUT(msg, IFLA_IPTUN_LOCAL,
sizeof(
struct in6_addr), &ip6_tnl->local);
174 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE)
175 NLA_PUT(msg, IFLA_IPTUN_REMOTE,
sizeof(
struct in6_addr), &ip6_tnl->remote);
177 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL)
178 NLA_PUT_U8(msg, IFLA_IPTUN_TTL, ip6_tnl->ttl);
180 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS)
181 NLA_PUT_U8(msg, IFLA_IPTUN_TOS, ip6_tnl->tos);
183 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT)
184 NLA_PUT_U8(msg, IFLA_IPTUN_ENCAP_LIMIT, ip6_tnl->encap_limit);
186 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS)
187 NLA_PUT_U32(msg, IFLA_IPTUN_FLAGS, ip6_tnl->flags);
189 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO)
190 NLA_PUT_U32(msg, IFLA_IPTUN_FLOWINFO, ip6_tnl->flowinfo);
193 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO)
194 NLA_PUT_U8(msg, IFLA_IPTUN_PROTO, ip6_tnl->proto);
204 static void ip6_tnl_free(
struct rtnl_link *link)
214 nl_dump(p,
"ip6_tnl : %s", link->l_name);
220 char *name, addr[INET6_ADDRSTRLEN];
223 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LINK) {
227 parent = link_lookup(link->ce_cache, ip6_tnl->link);
232 nl_dump_line(p,
"%s\n", name);
234 nl_dump_line(p,
"%u\n", ip6_tnl->link);
237 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_LOCAL) {
240 if(inet_ntop(AF_INET6, &ip6_tnl->local, addr, INET6_ADDRSTRLEN))
241 nl_dump_line(p,
"%s\n", addr);
243 nl_dump_line(p,
"%#x\n", ip6_tnl->local);
246 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_REMOTE) {
249 if(inet_ntop(AF_INET6, &ip6_tnl->remote, addr, INET6_ADDRSTRLEN))
250 nl_dump_line(p,
"%s\n", addr);
252 nl_dump_line(p,
"%#x\n", ip6_tnl->remote);
255 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TTL) {
257 nl_dump_line(p,
"%d\n", ip6_tnl->ttl);
260 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_TOS) {
262 nl_dump_line(p,
"%d\n", ip6_tnl->tos);
265 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_ENCAPLIMIT) {
267 nl_dump_line(p,
"%d\n", ip6_tnl->encap_limit);
270 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLAGS) {
272 nl_dump_line(p,
" (%x)\n", ip6_tnl->flags);
275 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_FLOWINFO) {
277 nl_dump_line(p,
" (%x)\n", ip6_tnl->flowinfo);
280 if (ip6_tnl->ip6_tnl_mask & IP6_TNL_ATTR_PROTO) {
282 nl_dump_line(p,
" (%x)\n", ip6_tnl->proto);
288 struct ip6_tnl_info *ip6_tnl_dst, *ip6_tnl_src = src->l_info;
297 ip6_tnl_dst = dst->l_info;
299 if (!ip6_tnl_dst || !ip6_tnl_src)
302 memcpy(ip6_tnl_dst, ip6_tnl_src,
sizeof(
struct ip6_tnl_info));
307 static struct rtnl_link_info_ops ip6_tnl_info_ops = {
309 .io_alloc = ip6_tnl_alloc,
310 .io_parse = ip6_tnl_parse,
315 .io_clone = ip6_tnl_clone,
316 .io_put_attrs = ip6_tnl_put_attrs,
317 .io_free = ip6_tnl_free,
320 #define IS_IP6_TNL_LINK_ASSERT(link)\ 321 if ((link)->l_info_ops != &ip6_tnl_info_ops) {\ 322 APPBUG("Link is not a ip6_tnl link. set type \"ip6tnl\" first.");\ 323 return -NLE_OPNOTSUPP;\ 326 struct rtnl_link *rtnl_link_ip6_tnl_alloc(
void)
352 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ip6tnl");
368 link = rtnl_link_ip6_tnl_alloc();
392 IS_IP6_TNL_LINK_ASSERT(link);
394 ip6_tnl->link = index;
395 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LINK;
410 IS_IP6_TNL_LINK_ASSERT(link);
412 return ip6_tnl->link;
426 IS_IP6_TNL_LINK_ASSERT(link);
428 memcpy(&ip6_tnl->local, addr,
sizeof(
struct in6_addr));
429 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_LOCAL;
444 IS_IP6_TNL_LINK_ASSERT(link);
446 memcpy(addr, &ip6_tnl->local,
sizeof(
struct in6_addr));
462 IS_IP6_TNL_LINK_ASSERT(link);
464 memcpy(&ip6_tnl->remote, addr,
sizeof(
struct in6_addr));
465 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_REMOTE;
480 IS_IP6_TNL_LINK_ASSERT(link);
482 memcpy(addr, &ip6_tnl->remote,
sizeof(
struct in6_addr));
498 IS_IP6_TNL_LINK_ASSERT(link);
501 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TTL;
516 IS_IP6_TNL_LINK_ASSERT(link);
532 IS_IP6_TNL_LINK_ASSERT(link);
535 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_TOS;
550 IS_IP6_TNL_LINK_ASSERT(link);
566 IS_IP6_TNL_LINK_ASSERT(link);
568 ip6_tnl->encap_limit = encap_limit;
569 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_ENCAPLIMIT;
584 IS_IP6_TNL_LINK_ASSERT(link);
586 return ip6_tnl->encap_limit;
600 IS_IP6_TNL_LINK_ASSERT(link);
602 ip6_tnl->flowinfo = flowinfo;
603 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLOWINFO;
618 IS_IP6_TNL_LINK_ASSERT(link);
620 return ip6_tnl->flowinfo;
634 IS_IP6_TNL_LINK_ASSERT(link);
636 ip6_tnl->flags = flags;
637 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_FLAGS;
652 IS_IP6_TNL_LINK_ASSERT(link);
654 return ip6_tnl->flags;
668 IS_IP6_TNL_LINK_ASSERT(link);
670 ip6_tnl->proto = proto;
671 ip6_tnl->ip6_tnl_mask |= IP6_TNL_ATTR_PROTO;
686 IS_IP6_TNL_LINK_ASSERT(link);
688 return ip6_tnl->proto;
691 static void __init ip6_tnl_init(
void)
696 static void __exit ip6_tnl_exit(
void)
Dump object briefly on one line.
uint8_t rtnl_link_ip6_tnl_get_ttl(struct rtnl_link *link)
Get IP6_TNL tunnel ttl.
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.
uint32_t rtnl_link_ip6_tnl_get_flowinfo(struct rtnl_link *link)
Get IP6_TNL flowinfo.
int rtnl_link_ip6_tnl_add(struct nl_sock *sk, const char *name)
Create a new ip6_tnl tunnel device.
Attribute validation policy.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
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_proto(struct rtnl_link *link, uint8_t proto)
Set IP6_TNL tunnel proto.
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_remote(struct rtnl_link *link, struct in6_addr *addr)
Set IP6_TNL tunnel remote address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
int rtnl_link_ip6_tnl_set_tos(struct rtnl_link *link, uint8_t tos)
Set IP6_TNL tunnel tos.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Dump all attributes but no statistics.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
int rtnl_link_ip6_tnl_set_local(struct rtnl_link *link, struct in6_addr *addr)
Set IP6_TNL tunnel local address.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
uint8_t rtnl_link_ip6_tnl_get_encaplimit(struct rtnl_link *link)
Get IP6_TNL encaplimit.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
int rtnl_link_ip6_tnl_set_flowinfo(struct rtnl_link *link, uint32_t flowinfo)
Set IP6_TNL tunnel flowinfo.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t rtnl_link_ip6_tnl_get_proto(struct rtnl_link *link)
Get IP6_TNL proto.
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_flags(struct rtnl_link *link)
Get IP6_TNL path flags.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_ip6_tnl_get_local(struct rtnl_link *link, struct in6_addr *addr)
Get IP6_TNL tunnel local address.
uint16_t type
Type of attribute or NLA_UNSPEC.
int rtnl_link_is_ip6_tnl(struct rtnl_link *link)
Check if link is a IP6_TNL link.
uint32_t rtnl_link_ip6_tnl_get_link(struct rtnl_link *link)
Get IP6_TNL tunnel interface index.
int rtnl_link_ip6_tnl_set_link(struct rtnl_link *link, uint32_t index)
Set IP6_TNL tunnel interface index.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
int rtnl_link_ip6_tnl_get_remote(struct rtnl_link *link, struct in6_addr *addr)
Get IP6_TNL tunnel remote address.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.