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/ipip.h>
26#include <netlink-private/route/link/api.h>
27#include <linux/if_tunnel.h>
29#define IPIP_ATTR_LINK (1 << 0)
30#define IPIP_ATTR_LOCAL (1 << 1)
31#define IPIP_ATTR_REMOTE (1 << 2)
32#define IPIP_ATTR_TTL (1 << 3)
33#define IPIP_ATTR_TOS (1 << 4)
34#define IPIP_ATTR_PMTUDISC (1 << 5)
35#define IPIP_ATTR_FWMARK (1 << 6)
49static struct nla_policy ipip_policy[IFLA_IPTUN_MAX + 1] = {
51 [IFLA_IPTUN_LOCAL] = { .type =
NLA_U32 },
52 [IFLA_IPTUN_REMOTE] = { .type =
NLA_U32 },
53 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
54 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
55 [IFLA_IPTUN_PMTUDISC] = { .type =
NLA_U8 },
56 [IFLA_IPTUN_FWMARK] = { .type =
NLA_U32 },
59static int ipip_alloc(
struct rtnl_link *link)
64 memset(link->l_info, 0,
sizeof(*ipip));
66 ipip = calloc(1,
sizeof(*ipip));
76static int ipip_parse(
struct rtnl_link *link,
struct nlattr *data,
77 struct nlattr *xstats)
79 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
83 NL_DBG(3,
"Parsing IPIP link info\n");
89 err = ipip_alloc(link);
95 if (tb[IFLA_IPTUN_LINK]) {
97 ipip->ipip_mask |= IPIP_ATTR_LINK;
100 if (tb[IFLA_IPTUN_LOCAL]) {
102 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
105 if (tb[IFLA_IPTUN_REMOTE]) {
107 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
110 if (tb[IFLA_IPTUN_TTL]) {
112 ipip->ipip_mask |= IPIP_ATTR_TTL;
115 if (tb[IFLA_IPTUN_TOS]) {
117 ipip->ipip_mask |= IPIP_ATTR_TOS;
120 if (tb[IFLA_IPTUN_PMTUDISC]) {
121 ipip->pmtudisc =
nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
122 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
125 if (tb[IFLA_IPTUN_FWMARK]) {
127 ipip->ipip_mask |= IPIP_ATTR_FWMARK;
136static int ipip_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
145 if (ipip->ipip_mask & IPIP_ATTR_LINK)
148 if (ipip->ipip_mask & IPIP_ATTR_LOCAL)
151 if (ipip->ipip_mask & IPIP_ATTR_REMOTE)
154 if (ipip->ipip_mask & IPIP_ATTR_TTL)
157 if (ipip->ipip_mask & IPIP_ATTR_TOS)
160 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC)
161 NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, ipip->pmtudisc);
163 if (ipip->ipip_mask & IPIP_ATTR_FWMARK)
172static void ipip_free(
struct rtnl_link *link)
182 nl_dump(p,
"ipip : %s", link->l_name);
188 char *name, addr[INET_ADDRSTRLEN];
191 if (ipip->ipip_mask & IPIP_ATTR_LINK) {
195 parent = link_lookup(link->ce_cache, ipip->link);
200 nl_dump_line(p,
"%s\n", name);
202 nl_dump_line(p,
"%u\n", ipip->link);
205 if (ipip->ipip_mask & IPIP_ATTR_LOCAL) {
207 if(inet_ntop(AF_INET, &ipip->local, addr,
sizeof(addr)))
208 nl_dump_line(p,
"%s\n", addr);
210 nl_dump_line(p,
"%#x\n", ntohs(ipip->local));
213 if (ipip->ipip_mask & IPIP_ATTR_REMOTE) {
215 if(inet_ntop(AF_INET, &ipip->remote, addr,
sizeof(addr)))
216 nl_dump_line(p,
"%s\n", addr);
218 nl_dump_line(p,
"%#x\n", ntohs(ipip->remote));
221 if (ipip->ipip_mask & IPIP_ATTR_TTL) {
223 nl_dump_line(p,
"%u\n", ipip->ttl);
226 if (ipip->ipip_mask & IPIP_ATTR_TOS) {
228 nl_dump_line(p,
"%u\n", ipip->tos);
231 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC) {
233 nl_dump_line(p,
"enabled (%#x)\n", ipip->pmtudisc);
236 if (ipip->ipip_mask & IPIP_ATTR_FWMARK) {
238 nl_dump_line(p,
"%x\n", ipip->fwmark);
244 struct ipip_info *ipip_dst, *ipip_src = src->l_info;
253 ipip_dst = dst->l_info;
255 if (!ipip_dst || !ipip_src)
258 memcpy(ipip_dst, ipip_src,
sizeof(
struct ipip_info));
263static struct rtnl_link_info_ops ipip_info_ops = {
265 .io_alloc = ipip_alloc,
266 .io_parse = ipip_parse,
271 .io_clone = ipip_clone,
272 .io_put_attrs = ipip_put_attrs,
273 .io_free = ipip_free,
276#define IS_IPIP_LINK_ASSERT(link) \
277 if ((link)->l_info_ops != &ipip_info_ops) { \
278 APPBUG("Link is not a ipip link. set type \"ipip\" first."); \
279 return -NLE_OPNOTSUPP; \
282struct rtnl_link *rtnl_link_ipip_alloc(
void)
308 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ipip");
324 link = rtnl_link_ipip_alloc();
348 IS_IPIP_LINK_ASSERT(link);
351 ipip->ipip_mask |= IPIP_ATTR_LINK;
366 IS_IPIP_LINK_ASSERT(link);
382 IS_IPIP_LINK_ASSERT(link);
385 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
400 IS_IPIP_LINK_ASSERT(link);
416 IS_IPIP_LINK_ASSERT(link);
419 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
434 IS_IPIP_LINK_ASSERT(link);
450 IS_IPIP_LINK_ASSERT(link);
453 ipip->ipip_mask |= IPIP_ATTR_TTL;
468 IS_IPIP_LINK_ASSERT(link);
484 IS_IPIP_LINK_ASSERT(link);
487 ipip->ipip_mask |= IPIP_ATTR_TOS;
502 IS_IPIP_LINK_ASSERT(link);
518 IS_IPIP_LINK_ASSERT(link);
520 ipip->pmtudisc = pmtudisc;
521 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
536 IS_IPIP_LINK_ASSERT(link);
538 return ipip->pmtudisc;
552 IS_IPIP_LINK_ASSERT(link);
554 ipip->fwmark = fwmark;
555 ipip->ipip_mask |= IPIP_ATTR_FWMARK;
571 IS_IPIP_LINK_ASSERT(link);
573 if (!(ipip->ipip_mask & IPIP_ATTR_FWMARK))
576 *fwmark = ipip->fwmark;
581static void __init ipip_init(
void)
586static void __exit ipip_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_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.
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_ipip_set_tos(struct rtnl_link *link, uint8_t tos)
Set IPIP tunnel tos.
int rtnl_link_ipip_set_local(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel local address.
int rtnl_link_ipip_set_remote(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel remote address.
uint32_t rtnl_link_ipip_get_remote(struct rtnl_link *link)
Get IPIP tunnel remote address.
uint32_t rtnl_link_ipip_get_local(struct rtnl_link *link)
Get IPIP tunnel local address.
int rtnl_link_ipip_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set IPIP tunnel ttl.
int rtnl_link_ipip_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
Set IPIP tunnel fwmark.
int rtnl_link_ipip_add(struct nl_sock *sk, const char *name)
Create a new ipip tunnel device.
int rtnl_link_ipip_set_link(struct rtnl_link *link, uint32_t index)
Set IPIP tunnel interface index.
int rtnl_link_is_ipip(struct rtnl_link *link)
Check if link is a IPIP link.
uint8_t rtnl_link_ipip_get_ttl(struct rtnl_link *link)
Get IPIP tunnel ttl.
int rtnl_link_ipip_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
Set IPIP tunnel path MTU discovery.
uint8_t rtnl_link_ipip_get_tos(struct rtnl_link *link)
Get IPIP tunnel tos.
uint32_t rtnl_link_ipip_get_link(struct rtnl_link *link)
Get IPIP tunnel interface index.
int rtnl_link_ipip_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
Get IPIP tunnel fwmark.
uint8_t rtnl_link_ipip_get_pmtudisc(struct rtnl_link *link)
Get IPIP path MTU discovery.
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.