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/ipip.h> 32 #include <netlink-private/route/link/api.h> 33 #include <linux/if_tunnel.h> 35 #define IPIP_ATTR_LINK (1 << 0) 36 #define IPIP_ATTR_LOCAL (1 << 1) 37 #define IPIP_ATTR_REMOTE (1 << 2) 38 #define IPIP_ATTR_TTL (1 << 3) 39 #define IPIP_ATTR_TOS (1 << 4) 40 #define IPIP_ATTR_PMTUDISC (1 << 5) 53 static struct nla_policy ipip_policy[IFLA_IPTUN_MAX + 1] = {
55 [IFLA_IPTUN_LOCAL] = { .type =
NLA_U32 },
56 [IFLA_IPTUN_REMOTE] = { .type =
NLA_U32 },
57 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
58 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
59 [IFLA_IPTUN_PMTUDISC] = { .type =
NLA_U8 },
62 static int ipip_alloc(
struct rtnl_link *link)
67 memset(link->l_info, 0,
sizeof(*ipip));
69 ipip = calloc(1,
sizeof(*ipip));
79 static int ipip_parse(
struct rtnl_link *link,
struct nlattr *data,
80 struct nlattr *xstats)
82 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
86 NL_DBG(3,
"Parsing IPIP link info\n");
92 err = ipip_alloc(link);
98 if (tb[IFLA_IPTUN_LINK]) {
100 ipip->ipip_mask |= IPIP_ATTR_LINK;
103 if (tb[IFLA_IPTUN_LOCAL]) {
105 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
108 if (tb[IFLA_IPTUN_REMOTE]) {
110 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
113 if (tb[IFLA_IPTUN_TTL]) {
115 ipip->ipip_mask |= IPIP_ATTR_TTL;
118 if (tb[IFLA_IPTUN_TOS]) {
120 ipip->ipip_mask |= IPIP_ATTR_TOS;
123 if (tb[IFLA_IPTUN_PMTUDISC]) {
124 ipip->pmtudisc =
nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
125 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
134 static int ipip_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
143 if (ipip->ipip_mask & IPIP_ATTR_LINK)
146 if (ipip->ipip_mask & IPIP_ATTR_LOCAL)
149 if (ipip->ipip_mask & IPIP_ATTR_REMOTE)
152 if (ipip->ipip_mask & IPIP_ATTR_TTL)
155 if (ipip->ipip_mask & IPIP_ATTR_TOS)
158 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC)
159 NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, ipip->pmtudisc);
167 static void ipip_free(
struct rtnl_link *link)
177 nl_dump(p,
"ipip : %s", link->l_name);
183 char *name, addr[INET_ADDRSTRLEN];
186 if (ipip->ipip_mask & IPIP_ATTR_LINK) {
190 parent = link_lookup(link->ce_cache, ipip->link);
195 nl_dump_line(p,
"%s\n", name);
197 nl_dump_line(p,
"%u\n", ipip->link);
200 if (ipip->ipip_mask & IPIP_ATTR_LOCAL) {
202 if(inet_ntop(AF_INET, &ipip->local, addr,
sizeof(addr)))
203 nl_dump_line(p,
"%s\n", addr);
205 nl_dump_line(p,
"%#x\n", ntohs(ipip->local));
208 if (ipip->ipip_mask & IPIP_ATTR_REMOTE) {
210 if(inet_ntop(AF_INET, &ipip->remote, addr,
sizeof(addr)))
211 nl_dump_line(p,
"%s\n", addr);
213 nl_dump_line(p,
"%#x\n", ntohs(ipip->remote));
216 if (ipip->ipip_mask & IPIP_ATTR_TTL) {
218 nl_dump_line(p,
"%u\n", ipip->ttl);
221 if (ipip->ipip_mask & IPIP_ATTR_TOS) {
223 nl_dump_line(p,
"%u\n", ipip->tos);
226 if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC) {
228 nl_dump_line(p,
"enabled (%#x)\n", ipip->pmtudisc);
234 struct ipip_info *ipip_dst, *ipip_src = src->l_info;
243 ipip_dst = dst->l_info;
245 if (!ipip_dst || !ipip_src)
248 memcpy(ipip_dst, ipip_src,
sizeof(
struct ipip_info));
253 static struct rtnl_link_info_ops ipip_info_ops = {
255 .io_alloc = ipip_alloc,
256 .io_parse = ipip_parse,
261 .io_clone = ipip_clone,
262 .io_put_attrs = ipip_put_attrs,
263 .io_free = ipip_free,
266 #define IS_IPIP_LINK_ASSERT(link) \ 267 if ((link)->l_info_ops != &ipip_info_ops) { \ 268 APPBUG("Link is not a ipip link. set type \"ipip\" first."); \ 269 return -NLE_OPNOTSUPP; \ 272 struct rtnl_link *rtnl_link_ipip_alloc(
void)
298 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"ipip");
314 link = rtnl_link_ipip_alloc();
338 IS_IPIP_LINK_ASSERT(link);
341 ipip->ipip_mask |= IPIP_ATTR_LINK;
356 IS_IPIP_LINK_ASSERT(link);
372 IS_IPIP_LINK_ASSERT(link);
375 ipip->ipip_mask |= IPIP_ATTR_LOCAL;
390 IS_IPIP_LINK_ASSERT(link);
406 IS_IPIP_LINK_ASSERT(link);
409 ipip->ipip_mask |= IPIP_ATTR_REMOTE;
424 IS_IPIP_LINK_ASSERT(link);
440 IS_IPIP_LINK_ASSERT(link);
443 ipip->ipip_mask |= IPIP_ATTR_TTL;
458 IS_IPIP_LINK_ASSERT(link);
474 IS_IPIP_LINK_ASSERT(link);
477 ipip->ipip_mask |= IPIP_ATTR_TOS;
492 IS_IPIP_LINK_ASSERT(link);
508 IS_IPIP_LINK_ASSERT(link);
510 ipip->pmtudisc = pmtudisc;
511 ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
526 IS_IPIP_LINK_ASSERT(link);
528 return ipip->pmtudisc;
531 static void __init ipip_init(
void)
536 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, const 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.