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-private/route/link/api.h>
32 #include <linux/if_tunnel.h>
34 #define IPIP_ATTR_LINK (1 << 0)
35 #define IPIP_ATTR_LOCAL (1 << 1)
36 #define IPIP_ATTR_REMOTE (1 << 2)
37 #define IPIP_ATTR_TTL (1 << 3)
38 #define IPIP_ATTR_TOS (1 << 4)
39 #define IPIP_ATTR_PMTUDISC (1 << 5)
52 static struct nla_policy ipip_policy[IFLA_IPTUN_MAX + 1] = {
54 [IFLA_IPTUN_LOCAL] = { .type =
NLA_U32 },
55 [IFLA_IPTUN_REMOTE] = { .type =
NLA_U32 },
56 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
57 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
58 [IFLA_IPTUN_PMTUDISC] = { .type =
NLA_U8 },
61 static int ipip_alloc(
struct rtnl_link *link)
66 memset(link->l_info, 0,
sizeof(*ipip));
68 ipip = calloc(1,
sizeof(*ipip));
78 static int ipip_parse(
struct rtnl_link *link,
struct nlattr *data,
79 struct nlattr *xstats)
81 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
85 NL_DBG(3,
"Parsing IPIP link info\n");
91 err = ipip_alloc(link);
97 if (tb[IFLA_IPTUN_LINK]) {
99 ipip->ipip_mask |= IPIP_ATTR_LINK;
102 if (tb[IFLA_IPTUN_LOCAL]) {
104 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
107 if (tb[IFLA_IPTUN_REMOTE]) {
109 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
112 if (tb[IFLA_IPTUN_TTL]) {
114 ipip->ipip_mask |= IPIP_ATTR_TTL;
117 if (tb[IFLA_IPTUN_TOS]) {
119 ipip->ipip_mask |= IPIP_ATTR_TOS;
122 if (tb[IFLA_IPTUN_PMTUDISC]) {
123 ipip->pmtudisc =
nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
124 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
133 static int ipip_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
142 if (ipip->ipip_mask & IPIP_ATTR_LINK)
145 if (ipip->ipip_mask & IPIP_ATTR_LOCAL)
148 if (ipip->ipip_mask & IPIP_ATTR_REMOTE)
151 if (ipip->ipip_mask & IPIP_ATTR_TTL)
154 if (ipip->ipip_mask & IPIP_ATTR_TOS)
157 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC)
158 NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, ipip->pmtudisc);
166 static void ipip_free(
struct rtnl_link *link)
176 nl_dump(p,
"ipip : %s", link->l_name);
182 char *name, addr[INET_ADDRSTRLEN];
184 if (ipip->ipip_mask & IPIP_ATTR_LINK) {
188 nl_dump_line(p,
"%s\n", name);
190 nl_dump_line(p,
"%u\n", ipip->link);
193 if (ipip->ipip_mask & IPIP_ATTR_LOCAL) {
195 if(inet_ntop(AF_INET, &ipip->local, addr,
sizeof(addr)))
196 nl_dump_line(p,
"%s\n", addr);
198 nl_dump_line(p,
"%#x\n", ntohs(ipip->local));
201 if (ipip->ipip_mask & IPIP_ATTR_REMOTE) {
203 if(inet_ntop(AF_INET, &ipip->remote, addr,
sizeof(addr)))
204 nl_dump_line(p,
"%s\n", addr);
206 nl_dump_line(p,
"%#x\n", ntohs(ipip->remote));
209 if (ipip->ipip_mask & IPIP_ATTR_TTL) {
211 nl_dump_line(p,
"%u\n", ipip->ttl);
214 if (ipip->ipip_mask & IPIP_ATTR_TOS) {
216 nl_dump_line(p,
"%u\n", ipip->tos);
219 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC) {
221 nl_dump_line(p,
"enabled (%#x)\n", ipip->pmtudisc);
227 struct ipip_info *ipip_dst, *ipip_src = src->l_info;
236 ipip_dst = dst->l_info;
238 if (!ipip_dst || !ipip_src)
241 memcpy(ipip_dst, ipip_src,
sizeof(
struct ipip_info));
246 static struct rtnl_link_info_ops ipip_info_ops = {
248 .io_alloc = ipip_alloc,
249 .io_parse = ipip_parse,
254 .io_clone = ipip_clone,
255 .io_put_attrs = ipip_put_attrs,
256 .io_free = ipip_free,
259 #define IS_IPIP_LINK_ASSERT(link) \
260 if ((link)->l_info_ops != &ipip_info_ops) { \
261 APPBUG("Link is not a ipip link. set type \"ipip\" first."); \
262 return -NLE_OPNOTSUPP; \
265 struct rtnl_link *rtnl_link_ipip_alloc(
void)
291 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ipip");
307 link = rtnl_link_ipip_alloc();
331 IS_IPIP_LINK_ASSERT(link);
334 ipip->ipip_mask |= IPIP_ATTR_LINK;
349 IS_IPIP_LINK_ASSERT(link);
365 IS_IPIP_LINK_ASSERT(link);
368 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
383 IS_IPIP_LINK_ASSERT(link);
399 IS_IPIP_LINK_ASSERT(link);
402 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
417 IS_IPIP_LINK_ASSERT(link);
433 IS_IPIP_LINK_ASSERT(link);
436 ipip->ipip_mask |= IPIP_ATTR_TTL;
451 IS_IPIP_LINK_ASSERT(link);
467 IS_IPIP_LINK_ASSERT(link);
470 ipip->ipip_mask |= IPIP_ATTR_TOS;
485 IS_IPIP_LINK_ASSERT(link);
501 IS_IPIP_LINK_ASSERT(link);
503 ipip->pmtudisc = pmtudisc;
504 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
519 IS_IPIP_LINK_ASSERT(link);
521 return ipip->pmtudisc;
524 static void __init ipip_init(
void)
529 static void __exit ipip_exit(
void)
Dump object briefly on one line.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_ipip_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
Set IPIP tunnel path MTU discovery.
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_is_ipip(struct rtnl_link *link)
Check if link is a IPIP link.
int rtnl_link_ipip_set_remote(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel remote address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
int rtnl_link_ipip_add(struct nl_sock *sk, const char *name)
Create a new ipip tunnel device.
int rtnl_link_ipip_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set IPIP tunnel ttl.
#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_ipip_set_local(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel local address.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
uint32_t rtnl_link_ipip_get_local(struct rtnl_link *link)
Get IPIP tunnel local address.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
uint8_t rtnl_link_ipip_get_pmtudisc(struct rtnl_link *link)
Get IPIP path MTU discovery.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
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.
int rtnl_link_ipip_set_link(struct rtnl_link *link, uint32_t index)
Set IPIP tunnel interface index.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
uint32_t rtnl_link_ipip_get_remote(struct rtnl_link *link)
Get IPIP tunnel remote address.
uint16_t type
Type of attribute or NLA_UNSPEC.
int rtnl_link_ipip_set_tos(struct rtnl_link *link, uint8_t tos)
Set IPIP tunnel tos.
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.
uint32_t rtnl_link_ipip_get_link(struct rtnl_link *link)
Get IPIP tunnel interface index.
uint8_t rtnl_link_ipip_get_tos(struct rtnl_link *link)
Get IPIP tunnel tos.
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.
uint8_t rtnl_link_ipip_get_ttl(struct rtnl_link *link)
Get IPIP tunnel ttl.