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/sit.h>
26#include <netlink-private/route/link/api.h>
27#include <linux/if_tunnel.h>
29#define SIT_ATTR_LINK (1 << 0)
30#define SIT_ATTR_LOCAL (1 << 1)
31#define SIT_ATTR_REMOTE (1 << 2)
32#define SIT_ATTR_TTL (1 << 3)
33#define SIT_ATTR_TOS (1 << 4)
34#define SIT_ATTR_PMTUDISC (1 << 5)
35#define SIT_ATTR_FLAGS (1 << 6)
36#define SIT_ATTR_PROTO (1 << 7)
37#define SIT_ATTR_6RD_PREFIX (1 << 8)
38#define SIT_ATTR_6RD_RELAY_PREFIX (1 << 9)
39#define SIT_ATTR_6RD_PREFIXLEN (1 << 10)
40#define SIT_ATTR_6RD_RELAY_PREFIXLEN (1 << 11)
41#define SIT_ATTR_FWMARK (1 << 12)
53 struct in6_addr ip6rd_prefix;
54 uint32_t ip6rd_relay_prefix;
55 uint16_t ip6rd_prefixlen;
56 uint16_t ip6rd_relay_prefixlen;
61static struct nla_policy sit_policy[IFLA_IPTUN_MAX + 1] = {
63 [IFLA_IPTUN_LOCAL] = { .type =
NLA_U32 },
64 [IFLA_IPTUN_REMOTE] = { .type =
NLA_U32 },
65 [IFLA_IPTUN_TTL] = { .type =
NLA_U8 },
66 [IFLA_IPTUN_TOS] = { .type =
NLA_U8 },
67 [IFLA_IPTUN_PMTUDISC] = { .type =
NLA_U8 },
68 [IFLA_IPTUN_FLAGS] = { .type =
NLA_U16 },
69 [IFLA_IPTUN_PROTO] = { .type =
NLA_U8 },
70 [IFLA_IPTUN_6RD_PREFIX] = { .minlen =
sizeof(
struct in6_addr) },
71 [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type =
NLA_U32 },
72 [IFLA_IPTUN_6RD_PREFIXLEN] = { .type =
NLA_U16 },
73 [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type =
NLA_U16 },
74 [IFLA_IPTUN_FWMARK] = { .type =
NLA_U32 },
77static int sit_alloc(
struct rtnl_link *link)
82 memset(link->l_info, 0,
sizeof(*sit));
84 sit = calloc(1,
sizeof(*sit));
94static int sit_parse(
struct rtnl_link *link,
struct nlattr *data,
95 struct nlattr *xstats)
97 struct nlattr *tb[IFLA_IPTUN_MAX + 1];
101 NL_DBG(3,
"Parsing SIT link info\n");
107 err = sit_alloc(link);
113 if (tb[IFLA_IPTUN_LINK]) {
115 sit->sit_mask |= SIT_ATTR_LINK;
118 if (tb[IFLA_IPTUN_LOCAL]) {
120 sit->sit_mask |= SIT_ATTR_LOCAL;
123 if (tb[IFLA_IPTUN_REMOTE]) {
125 sit->sit_mask |= SIT_ATTR_REMOTE;
128 if (tb[IFLA_IPTUN_TTL]) {
130 sit->sit_mask |= SIT_ATTR_TTL;
133 if (tb[IFLA_IPTUN_TOS]) {
135 sit->sit_mask |= SIT_ATTR_TOS;
138 if (tb[IFLA_IPTUN_PMTUDISC]) {
139 sit->pmtudisc =
nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
140 sit->sit_mask |= SIT_ATTR_PMTUDISC;
143 if (tb[IFLA_IPTUN_FLAGS]) {
145 sit->sit_mask |= SIT_ATTR_FLAGS;
148 if (tb[IFLA_IPTUN_PROTO]) {
149 sit->proto =
nla_get_u8(tb[IFLA_IPTUN_PROTO]);
150 sit->sit_mask |= SIT_ATTR_PROTO;
153 if (tb[IFLA_IPTUN_6RD_PREFIX]) {
154 nla_memcpy(&sit->ip6rd_prefix, tb[IFLA_IPTUN_6RD_PREFIX],
155 sizeof(
struct in6_addr));
156 sit->sit_mask |= SIT_ATTR_6RD_PREFIX;
159 if (tb[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
160 sit->ip6rd_relay_prefix =
nla_get_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
161 sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIX;
164 if (tb[IFLA_IPTUN_6RD_PREFIXLEN]) {
165 sit->ip6rd_prefixlen =
nla_get_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]);
166 sit->sit_mask |= SIT_ATTR_6RD_PREFIXLEN;
169 if (tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
170 sit->ip6rd_relay_prefixlen =
nla_get_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
171 sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIXLEN;
174 if (tb[IFLA_IPTUN_FWMARK]) {
176 sit->sit_mask |= SIT_ATTR_FWMARK;
185static int sit_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
187 struct sit_info *sit = link->l_info;
194 if (sit->sit_mask & SIT_ATTR_LINK)
197 if (sit->sit_mask & SIT_ATTR_LOCAL)
200 if (sit->sit_mask & SIT_ATTR_REMOTE)
203 if (sit->sit_mask & SIT_ATTR_TTL)
206 if (sit->sit_mask & SIT_ATTR_TOS)
209 if (sit->sit_mask & SIT_ATTR_PMTUDISC)
210 NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, sit->pmtudisc);
212 if (sit->sit_mask & SIT_ATTR_FLAGS)
215 if (sit->sit_mask & SIT_ATTR_PROTO)
216 NLA_PUT_U8(msg, IFLA_IPTUN_PROTO, sit->proto);
218 if (sit->sit_mask & SIT_ATTR_6RD_PREFIX)
219 NLA_PUT(msg, IFLA_IPTUN_6RD_PREFIX,
sizeof(
struct in6_addr), &sit->ip6rd_prefix);
221 if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX)
222 NLA_PUT_U32(msg, IFLA_IPTUN_6RD_RELAY_PREFIX, sit->ip6rd_relay_prefix);
224 if (sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN)
225 NLA_PUT_U16(msg, IFLA_IPTUN_6RD_PREFIXLEN, sit->ip6rd_prefixlen);
227 if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIXLEN)
228 NLA_PUT_U16(msg, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, sit->ip6rd_relay_prefixlen);
230 if (sit->sit_mask & SIT_ATTR_FWMARK)
240static void sit_free(
struct rtnl_link *link)
242 struct sit_info *sit = link->l_info;
250 nl_dump(p,
"sit : %s", link->l_name);
255 struct sit_info *sit = link->l_info;
256 char *name, addr[INET_ADDRSTRLEN], addr6[INET6_ADDRSTRLEN];
259 if (sit->sit_mask & SIT_ATTR_LINK) {
263 parent = link_lookup(link->ce_cache, sit->link);
268 nl_dump_line(p,
"%s\n", name);
270 nl_dump_line(p,
"%u\n", sit->link);
273 if (sit->sit_mask & SIT_ATTR_LOCAL) {
275 if(inet_ntop(AF_INET, &sit->local, addr,
sizeof(addr)))
276 nl_dump_line(p,
"%s\n", addr);
278 nl_dump_line(p,
"%#x\n", ntohs(sit->local));
281 if (sit->sit_mask & SIT_ATTR_REMOTE) {
283 if(inet_ntop(AF_INET, &sit->remote, addr,
sizeof(addr)))
284 nl_dump_line(p,
"%s\n", addr);
286 nl_dump_line(p,
"%#x\n", ntohs(sit->remote));
289 if (sit->sit_mask & SIT_ATTR_TTL) {
291 nl_dump_line(p,
"%u\n", sit->ttl);
294 if (sit->sit_mask & SIT_ATTR_TOS) {
296 nl_dump_line(p,
"%u\n", sit->tos);
299 if (sit->sit_mask & SIT_ATTR_FLAGS) {
301 nl_dump_line(p,
" (%x)\n", sit->flags);
304 if (sit->sit_mask & SIT_ATTR_PROTO) {
306 nl_dump_line(p,
" (%x)\n", sit->proto);
309 if (sit->sit_mask & SIT_ATTR_6RD_PREFIX) {
311 if(inet_ntop(AF_INET6, &sit->ip6rd_prefix, addr6, INET6_ADDRSTRLEN))
312 nl_dump_line(p,
"%s\n", addr6);
314 nl_dump_line(p,
"[unknown]\n");
317 if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX) {
318 nl_dump(p,
" 6rd_relay_prefix ");
319 if(inet_ntop(AF_INET, &sit->ip6rd_relay_prefix, addr,
sizeof(addr)))
320 nl_dump_line(p,
"%s\n", addr);
322 nl_dump_line(p,
"[unknown]\n");
325 if (sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN) {
327 nl_dump_line(p,
"%d\n", sit->ip6rd_prefixlen);
330 if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIXLEN) {
331 nl_dump(p,
" 6rd_relay_prefixlen ");
332 nl_dump_line(p,
"%d\n", sit->ip6rd_relay_prefixlen);
335 if (sit->sit_mask & SIT_ATTR_FWMARK) {
337 nl_dump_line(p,
"%x\n", sit->fwmark);
343 struct sit_info *sit_dst, *sit_src = src->l_info;
352 sit_dst = dst->l_info;
354 if (!sit_dst || !sit_src)
357 memcpy(sit_dst, sit_src,
sizeof(
struct sit_info));
362static struct rtnl_link_info_ops sit_info_ops = {
364 .io_alloc = sit_alloc,
365 .io_parse = sit_parse,
370 .io_clone = sit_clone,
371 .io_put_attrs = sit_put_attrs,
375#define IS_SIT_LINK_ASSERT(link, sit) \
376 struct sit_info *sit; \
378 const struct rtnl_link *_link = (link); \
379 if (!_link || _link->l_info_ops != &sit_info_ops) { \
380 APPBUG("Link is not a sit link. set type \"sit\" first."); \
381 return -NLE_OPNOTSUPP; \
383 (sit) = _link->l_info; \
386struct rtnl_link *rtnl_link_sit_alloc(
void)
412 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"sit");
428 link = rtnl_link_sit_alloc();
450 IS_SIT_LINK_ASSERT(link, sit);
453 sit->sit_mask |= SIT_ATTR_LINK;
466 IS_SIT_LINK_ASSERT(link, sit);
480 IS_SIT_LINK_ASSERT(link, sit);
483 sit->sit_mask |= SIT_ATTR_LOCAL;
496 IS_SIT_LINK_ASSERT(link, sit);
510 IS_SIT_LINK_ASSERT(link, sit);
513 sit->sit_mask |= SIT_ATTR_REMOTE;
526 IS_SIT_LINK_ASSERT(link, sit);
540 IS_SIT_LINK_ASSERT(link, sit);
543 sit->sit_mask |= SIT_ATTR_TTL;
556 IS_SIT_LINK_ASSERT(link, sit);
570 IS_SIT_LINK_ASSERT(link, sit);
573 sit->sit_mask |= SIT_ATTR_TOS;
586 IS_SIT_LINK_ASSERT(link, sit);
600 IS_SIT_LINK_ASSERT(link, sit);
602 sit->pmtudisc = pmtudisc;
603 sit->sit_mask |= SIT_ATTR_PMTUDISC;
616 IS_SIT_LINK_ASSERT(link, sit);
618 return sit->pmtudisc;
630 IS_SIT_LINK_ASSERT(link, sit);
633 sit->sit_mask |= SIT_ATTR_FLAGS;
646 IS_SIT_LINK_ASSERT(link, sit);
660 IS_SIT_LINK_ASSERT(link, sit);
663 sit->sit_mask |= SIT_ATTR_PROTO;
676 IS_SIT_LINK_ASSERT(link, sit);
690 IS_SIT_LINK_ASSERT(link, sit);
692 sit->ip6rd_prefix = *prefix;
693 sit->sit_mask |= SIT_ATTR_6RD_PREFIX;
707 IS_SIT_LINK_ASSERT(link, sit);
709 if (!(sit->sit_mask & SIT_ATTR_6RD_PREFIX))
713 *prefix = sit->ip6rd_prefix;
726 IS_SIT_LINK_ASSERT(link, sit);
728 sit->sit_mask |= SIT_ATTR_6RD_PREFIXLEN;
729 sit->ip6rd_prefixlen = prefixlen;
743 IS_SIT_LINK_ASSERT(link, sit);
745 if (!(sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN))
749 *prefixlen = sit->ip6rd_prefixlen;
762 IS_SIT_LINK_ASSERT(link, sit);
764 sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIX;
765 sit->ip6rd_relay_prefix = prefix;
779 IS_SIT_LINK_ASSERT(link, sit);
781 if (!(sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX))
785 *prefix = sit->ip6rd_relay_prefix;
798 IS_SIT_LINK_ASSERT(link, sit);
800 sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIXLEN;
801 sit->ip6rd_relay_prefixlen = prefixlen;
815 IS_SIT_LINK_ASSERT(link, sit);
817 if (!(sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX))
821 *prefixlen = sit->ip6rd_relay_prefixlen;
834 IS_SIT_LINK_ASSERT(link, sit);
836 sit->fwmark = fwmark;
837 sit->sit_mask |= SIT_ATTR_FWMARK;
851 IS_SIT_LINK_ASSERT(link, sit);
853 if (!(sit->sit_mask & SIT_ATTR_FWMARK))
856 *fwmark = sit->fwmark;
861static void __init sit_init(
void)
866static void __exit sit_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific 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.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
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_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.
uint8_t rtnl_link_sit_get_proto(struct rtnl_link *link)
Get SIT proto.
int rtnl_link_sit_set_local(struct rtnl_link *link, uint32_t addr)
Set SIT tunnel local address.
int rtnl_link_sit_set_ip6rd_relay_prefix(struct rtnl_link *link, uint32_t prefix)
Set ip6rd relay prefix.
int rtnl_link_sit_set_ip6rd_prefix(struct rtnl_link *link, const struct in6_addr *prefix)
Set ip6rd prefix.
int rtnl_link_sit_set_remote(struct rtnl_link *link, uint32_t addr)
Set SIT tunnel remote address.
int rtnl_link_sit_set_tos(struct rtnl_link *link, uint8_t tos)
Set SIT tunnel tos.
int rtnl_link_sit_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
Set SIT tunnel path MTU discovery.
int rtnl_link_sit_set_flags(struct rtnl_link *link, uint16_t flags)
Set SIT tunnel flags.
int rtnl_link_sit_get_ip6rd_relay_prefixlen(struct rtnl_link *link, uint16_t *prefixlen)
Get ip6rd relay prefix length.
int rtnl_link_is_sit(struct rtnl_link *link)
Check if link is a SIT link.
int rtnl_link_sit_get_ip6rd_prefixlen(struct rtnl_link *link, uint16_t *prefixlen)
Get ip6rd prefix length.
uint8_t rtnl_link_sit_get_tos(struct rtnl_link *link)
Get SIT tunnel tos.
uint16_t rtnl_link_sit_get_flags(struct rtnl_link *link)
Get SIT path flags.
int rtnl_link_sit_add(struct nl_sock *sk, const char *name)
Create a new sit tunnel device.
int rtnl_link_sit_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
Get SIT tunnel fwmark.
uint8_t rtnl_link_sit_get_ttl(struct rtnl_link *link)
Get SIT tunnel ttl.
int rtnl_link_sit_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
Set SIT tunnel fwmark.
int rtnl_link_sit_get_ip6rd_prefix(const struct rtnl_link *link, struct in6_addr *prefix)
Get ip6rd prefix.
int rtnl_link_sit_set_ip6rd_relay_prefixlen(struct rtnl_link *link, uint16_t prefixlen)
Set ip6rd relay prefix length.
int rtnl_link_sit_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set SIT tunnel ttl.
int rtnl_link_sit_set_link(struct rtnl_link *link, uint32_t index)
Set SIT tunnel interface index.
int rtnl_link_sit_set_ip6rd_prefixlen(struct rtnl_link *link, uint16_t prefixlen)
Set ip6rd prefix length.
uint32_t rtnl_link_sit_get_local(struct rtnl_link *link)
Get SIT tunnel local address.
int rtnl_link_sit_set_proto(struct rtnl_link *link, uint8_t proto)
Set SIT tunnel proto.
uint8_t rtnl_link_sit_get_pmtudisc(struct rtnl_link *link)
Get SIT path MTU discovery.
int rtnl_link_sit_get_ip6rd_relay_prefix(const struct rtnl_link *link, uint32_t *prefix)
Get ip6rd prefix length.
uint32_t rtnl_link_sit_get_remote(struct rtnl_link *link)
Get SIT tunnel remote address.
uint32_t rtnl_link_sit_get_link(struct rtnl_link *link)
Get SIT tunnel interface index.
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.