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/ipvti.h>
26#include <netlink-private/route/link/api.h>
27#include <linux/if_tunnel.h>
29#define IPVTI_ATTR_LINK (1 << 0)
30#define IPVTI_ATTR_IKEY (1 << 1)
31#define IPVTI_ATTR_OKEY (1 << 2)
32#define IPVTI_ATTR_LOCAL (1 << 3)
33#define IPVTI_ATTR_REMOTE (1 << 4)
34#define IPVTI_ATTR_FWMARK (1 << 5)
47static struct nla_policy ipvti_policy[IFLA_VTI_MAX + 1] = {
49 [IFLA_VTI_IKEY] = { .type =
NLA_U32 },
50 [IFLA_VTI_OKEY] = { .type =
NLA_U32 },
51 [IFLA_VTI_LOCAL] = { .type =
NLA_U32 },
52 [IFLA_VTI_REMOTE] = { .type =
NLA_U32 },
53 [IFLA_VTI_FWMARK] = { .type =
NLA_U32 },
56static int ipvti_alloc(
struct rtnl_link *link)
61 memset(link->l_info, 0,
sizeof(*ipvti));
63 ipvti = calloc(1,
sizeof(*ipvti));
73static int ipvti_parse(
struct rtnl_link *link,
struct nlattr *data,
74 struct nlattr *xstats)
76 struct nlattr *tb[IFLA_VTI_MAX + 1];
80 NL_DBG(3,
"Parsing IPVTI link info\n");
86 err = ipvti_alloc(link);
92 if (tb[IFLA_VTI_LINK]) {
94 ipvti->ipvti_mask |= IPVTI_ATTR_LINK;
97 if (tb[IFLA_VTI_IKEY]) {
99 ipvti->ipvti_mask |= IPVTI_ATTR_IKEY;
102 if (tb[IFLA_VTI_OKEY]) {
104 ipvti->ipvti_mask |= IPVTI_ATTR_OKEY;
107 if (tb[IFLA_VTI_LOCAL]) {
109 ipvti->ipvti_mask |= IPVTI_ATTR_LOCAL;
112 if (tb[IFLA_VTI_REMOTE]) {
114 ipvti->ipvti_mask |= IPVTI_ATTR_REMOTE;
117 if (tb[IFLA_VTI_FWMARK]) {
119 ipvti->ipvti_mask |= IPVTI_ATTR_FWMARK;
128static int ipvti_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
137 if (ipvti->ipvti_mask & IPVTI_ATTR_LINK)
140 if (ipvti->ipvti_mask & IPVTI_ATTR_IKEY)
143 if (ipvti->ipvti_mask & IFLA_VTI_IKEY)
146 if (ipvti->ipvti_mask & IPVTI_ATTR_LOCAL)
149 if (ipvti->ipvti_mask & IPVTI_ATTR_REMOTE)
152 if (ipvti->ipvti_mask & IPVTI_ATTR_FWMARK)
162static void ipvti_free(
struct rtnl_link *link)
172 nl_dump(p,
"ipvti : %s", link->l_name);
178 char *name, addr[INET_ADDRSTRLEN];
181 if (ipvti->ipvti_mask & IPVTI_ATTR_LINK) {
185 parent = link_lookup(link->ce_cache, ipvti->link);
190 nl_dump_line(p,
"%s\n", name);
192 nl_dump_line(p,
"%u\n", ipvti->link);
195 if (ipvti->ipvti_mask & IPVTI_ATTR_IKEY) {
197 nl_dump_line(p,
"%x\n",ipvti->ikey);
200 if (ipvti->ipvti_mask & IPVTI_ATTR_OKEY) {
202 nl_dump_line(p,
"%x\n", ipvti->okey);
205 if (ipvti->ipvti_mask & IPVTI_ATTR_LOCAL) {
207 if(inet_ntop(AF_INET, &ipvti->local, addr,
sizeof(addr)))
208 nl_dump_line(p,
"%s\n", addr);
210 nl_dump_line(p,
"%#x\n", ntohs(ipvti->local));
213 if (ipvti->ipvti_mask & IPVTI_ATTR_REMOTE) {
215 if(inet_ntop(AF_INET, &ipvti->remote, addr,
sizeof(addr)))
216 nl_dump_line(p,
"%s\n", addr);
218 nl_dump_line(p,
"%#x\n", ntohs(ipvti->remote));
221 if (ipvti->ipvti_mask & IPVTI_ATTR_FWMARK) {
223 nl_dump_line(p,
"%x\n", ipvti->fwmark);
229 struct ipvti_info *ipvti_dst, *ipvti_src = src->l_info;
238 ipvti_dst = dst->l_info;
240 if (!ipvti_dst || !ipvti_src)
243 memcpy(ipvti_dst, ipvti_src,
sizeof(
struct ipvti_info));
248static struct rtnl_link_info_ops ipvti_info_ops = {
250 .io_alloc = ipvti_alloc,
251 .io_parse = ipvti_parse,
256 .io_clone = ipvti_clone,
257 .io_put_attrs = ipvti_put_attrs,
258 .io_free = ipvti_free,
261#define IS_IPVTI_LINK_ASSERT(link) \
262 if ((link)->l_info_ops != &ipvti_info_ops) { \
263 APPBUG("Link is not a ipvti link. set type \vti\" first."); \
264 return -NLE_OPNOTSUPP; \
267struct rtnl_link *rtnl_link_ipvti_alloc(
void)
293 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"vti");
308 link = rtnl_link_ipvti_alloc();
331 IS_IPVTI_LINK_ASSERT(link);
334 ipvti->ipvti_mask |= IPVTI_ATTR_LINK;
349 IS_IPVTI_LINK_ASSERT(link);
365 IS_IPVTI_LINK_ASSERT(link);
368 ipvti->ipvti_mask |= IPVTI_ATTR_IKEY;
383 IS_IPVTI_LINK_ASSERT(link);
399 IS_IPVTI_LINK_ASSERT(link);
402 ipvti->ipvti_mask |= IPVTI_ATTR_OKEY;
417 IS_IPVTI_LINK_ASSERT(link);
433 IS_IPVTI_LINK_ASSERT(link);
436 ipvti->ipvti_mask |= IPVTI_ATTR_LOCAL;
451 IS_IPVTI_LINK_ASSERT(link);
467 IS_IPVTI_LINK_ASSERT(link);
469 ipvti->remote = remote;
470 ipvti->ipvti_mask |= IPVTI_ATTR_REMOTE;
485 IS_IPVTI_LINK_ASSERT(link);
487 return ipvti->remote;
501 IS_IPVTI_LINK_ASSERT(link);
503 ipvti->fwmark = fwmark;
504 ipvti->ipvti_mask |= IPVTI_ATTR_FWMARK;
520 IS_IPVTI_LINK_ASSERT(link);
522 if (!(ipvti->ipvti_mask & IPVTI_ATTR_FWMARK))
525 *fwmark = ipvti->fwmark;
530static void __init ipvti_init(
void)
535static void __exit ipvti_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
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_ipvti_set_ikey(struct rtnl_link *link, uint32_t ikey)
Set IPVTI tunnel set ikey.
uint32_t rtnl_link_ipvti_get_ikey(struct rtnl_link *link)
Get IPVTI tunnel ikey.
int rtnl_link_ipvti_set_local(struct rtnl_link *link, uint32_t addr)
Set IPVTI tunnel local address.
int rtnl_link_ipvti_set_remote(struct rtnl_link *link, uint32_t remote)
Set IPVTI tunnel remote address.
int rtnl_link_ipvti_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
Get IPVTI tunnel fwmark.
uint32_t rtnl_link_ipvti_get_local(struct rtnl_link *link)
Get IPVTI tunnel local address.
int rtnl_link_ipvti_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
Set IPVTI tunnel fwmark.
int rtnl_link_ipvti_set_okey(struct rtnl_link *link, uint32_t okey)
Set IPVTI tunnel set okey.
int rtnl_link_is_ipvti(struct rtnl_link *link)
Check if link is a IPVTI link.
uint32_t rtnl_link_ipvti_get_link(struct rtnl_link *link)
Get IPVTI tunnel interface index.
uint32_t rtnl_link_ipvti_get_remote(struct rtnl_link *link)
Get IPVTI tunnel remote address.
int rtnl_link_ipvti_set_link(struct rtnl_link *link, uint32_t index)
Set IPVTI tunnel interface index.
uint32_t rtnl_link_ipvti_get_okey(struct rtnl_link *link)
Get IPVTI tunnel okey.
int rtnl_link_ipvti_add(struct nl_sock *sk, const char *name)
Create a new ipvti tunnel device.
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.