|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* link_vti.c VTI driver module
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
3880ab |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
3880ab |
* as published by the Free Software Foundation; either version
|
|
Packit Service |
3880ab |
* 2 of the License, or (at your option) any later version.
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* Authors: Herbert Xu <herbert@gondor.apana.org.au>
|
|
Packit Service |
3880ab |
* Saurabh Mohan <saurabh.mohan@vyatta.com> Modified link_gre.c for VTI
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#include <string.h>
|
|
Packit Service |
3880ab |
#include <net/if.h>
|
|
Packit Service |
3880ab |
#include <sys/types.h>
|
|
Packit Service |
3880ab |
#include <sys/socket.h>
|
|
Packit Service |
3880ab |
#include <arpa/inet.h>
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#include <linux/ip.h>
|
|
Packit Service |
3880ab |
#include <linux/if_tunnel.h>
|
|
Packit Service |
3880ab |
#include "rt_names.h"
|
|
Packit Service |
3880ab |
#include "utils.h"
|
|
Packit Service |
3880ab |
#include "ip_common.h"
|
|
Packit Service |
3880ab |
#include "tunnel.h"
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static void vti_print_help(struct link_util *lu, int argc, char **argv, FILE *f)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
fprintf(f,
|
|
Packit Service |
3880ab |
"Usage: ... %-4s [ remote ADDR ]\n"
|
|
Packit Service |
3880ab |
" [ local ADDR ]\n"
|
|
Packit Service |
3880ab |
" [ [i|o]key KEY ]\n"
|
|
Packit Service |
3880ab |
" [ dev PHYS_DEV ]\n"
|
|
Packit Service |
3880ab |
" [ fwmark MARK ]\n"
|
|
Packit Service |
3880ab |
"\n"
|
|
Packit Service |
3880ab |
"Where: ADDR := { IP_ADDRESS }\n"
|
|
Packit Service |
3880ab |
" KEY := { DOTTED_QUAD | NUMBER }\n"
|
|
Packit Service |
3880ab |
" MARK := { 0x0..0xffffffff }\n",
|
|
Packit Service |
3880ab |
lu->id);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static int vti_parse_opt(struct link_util *lu, int argc, char **argv,
|
|
Packit Service |
3880ab |
struct nlmsghdr *n)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
struct ifinfomsg *ifi = NLMSG_DATA(n);
|
|
Packit Service |
3880ab |
struct {
|
|
Packit Service |
3880ab |
struct nlmsghdr n;
|
|
Packit Service |
3880ab |
struct ifinfomsg i;
|
|
Packit Service |
3880ab |
} req = {
|
|
Packit Service |
3880ab |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
|
|
Packit Service |
3880ab |
.n.nlmsg_flags = NLM_F_REQUEST,
|
|
Packit Service |
3880ab |
.n.nlmsg_type = RTM_GETLINK,
|
|
Packit Service |
3880ab |
.i.ifi_family = preferred_family,
|
|
Packit Service |
3880ab |
.i.ifi_index = ifi->ifi_index,
|
|
Packit Service |
3880ab |
};
|
|
Packit Service |
3880ab |
struct nlmsghdr *answer;
|
|
Packit Service |
3880ab |
struct rtattr *tb[IFLA_MAX + 1];
|
|
Packit Service |
3880ab |
struct rtattr *linkinfo[IFLA_INFO_MAX+1];
|
|
Packit Service |
3880ab |
struct rtattr *vtiinfo[IFLA_VTI_MAX + 1];
|
|
Packit Service |
3880ab |
__be32 ikey = 0;
|
|
Packit Service |
3880ab |
__be32 okey = 0;
|
|
Packit Service |
3880ab |
inet_prefix saddr, daddr;
|
|
Packit Service |
3880ab |
unsigned int link = 0;
|
|
Packit Service |
3880ab |
__u32 fwmark = 0;
|
|
Packit Service |
3880ab |
int len;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
inet_prefix_reset(&saddr);
|
|
Packit Service |
3880ab |
inet_prefix_reset(&daddr);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (!(n->nlmsg_flags & NLM_F_CREATE)) {
|
|
Packit Service |
3880ab |
const struct rtattr *rta;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (rtnl_talk(&rth, &req.n, &answer) < 0) {
|
|
Packit Service |
3880ab |
get_failed:
|
|
Packit Service |
3880ab |
fprintf(stderr,
|
|
Packit Service |
3880ab |
"Failed to get existing tunnel info.\n");
|
|
Packit Service |
3880ab |
return -1;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
len = answer->nlmsg_len;
|
|
Packit Service |
3880ab |
len -= NLMSG_LENGTH(sizeof(*ifi));
|
|
Packit Service |
3880ab |
if (len < 0)
|
|
Packit Service |
3880ab |
goto get_failed;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (!tb[IFLA_LINKINFO])
|
|
Packit Service |
3880ab |
goto get_failed;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (!linkinfo[IFLA_INFO_DATA])
|
|
Packit Service |
3880ab |
goto get_failed;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX,
|
|
Packit Service |
3880ab |
linkinfo[IFLA_INFO_DATA]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
rta = vtiinfo[IFLA_VTI_LOCAL];
|
|
Packit Service |
3880ab |
if (rta && get_addr_rta(&saddr, rta, AF_INET))
|
|
Packit Service |
3880ab |
goto get_failed;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
rta = vtiinfo[IFLA_VTI_REMOTE];
|
|
Packit Service |
3880ab |
if (rta && get_addr_rta(&daddr, rta, AF_INET))
|
|
Packit Service |
3880ab |
goto get_failed;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (vtiinfo[IFLA_VTI_IKEY])
|
|
Packit Service |
3880ab |
ikey = rta_getattr_u32(vtiinfo[IFLA_VTI_IKEY]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (vtiinfo[IFLA_VTI_OKEY])
|
|
Packit Service |
3880ab |
okey = rta_getattr_u32(vtiinfo[IFLA_VTI_OKEY]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (vtiinfo[IFLA_VTI_LINK])
|
|
Packit Service |
3880ab |
link = rta_getattr_u8(vtiinfo[IFLA_VTI_LINK]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (vtiinfo[IFLA_VTI_FWMARK])
|
|
Packit Service |
3880ab |
fwmark = rta_getattr_u32(vtiinfo[IFLA_VTI_FWMARK]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
free(answer);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
while (argc > 0) {
|
|
Packit Service |
3880ab |
if (!matches(*argv, "key")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
ikey = okey = tnl_parse_key("key", *argv);
|
|
Packit Service |
3880ab |
} else if (!matches(*argv, "ikey")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
ikey = tnl_parse_key("ikey", *argv);
|
|
Packit Service |
3880ab |
} else if (!matches(*argv, "okey")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
okey = tnl_parse_key("okey", *argv);
|
|
Packit Service |
3880ab |
} else if (!matches(*argv, "remote")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
get_addr(&daddr, *argv, AF_INET);
|
|
Packit Service |
3880ab |
} else if (!matches(*argv, "local")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
get_addr(&saddr, *argv, AF_INET);
|
|
Packit Service |
3880ab |
} else if (!matches(*argv, "dev")) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
link = ll_name_to_index(*argv);
|
|
Packit Service |
3880ab |
if (!link)
|
|
Packit Service |
3880ab |
exit(nodev(*argv));
|
|
Packit Service |
3880ab |
} else if (strcmp(*argv, "fwmark") == 0) {
|
|
Packit Service |
3880ab |
NEXT_ARG();
|
|
Packit Service |
3880ab |
if (get_u32(&fwmark, *argv, 0))
|
|
Packit Service |
3880ab |
invarg("invalid fwmark\n", *argv);
|
|
Packit Service |
3880ab |
} else {
|
|
Packit Service |
3880ab |
vti_print_help(lu, argc, argv, stderr);
|
|
Packit Service |
3880ab |
return -1;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
argc--; argv++;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
addattr32(n, 1024, IFLA_VTI_IKEY, ikey);
|
|
Packit Service |
3880ab |
addattr32(n, 1024, IFLA_VTI_OKEY, okey);
|
|
Packit Service |
3880ab |
if (is_addrtype_inet_not_unspec(&saddr))
|
|
Packit Service |
3880ab |
addattr_l(n, 1024, IFLA_VTI_LOCAL, saddr.data, saddr.bytelen);
|
|
Packit Service |
3880ab |
if (is_addrtype_inet_not_unspec(&daddr))
|
|
Packit Service |
3880ab |
addattr_l(n, 1024, IFLA_VTI_REMOTE, daddr.data, daddr.bytelen);
|
|
Packit Service |
3880ab |
addattr32(n, 1024, IFLA_VTI_FWMARK, fwmark);
|
|
Packit Service |
3880ab |
if (link)
|
|
Packit Service |
3880ab |
addattr32(n, 1024, IFLA_VTI_LINK, link);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return 0;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
char s2[64];
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (!tb)
|
|
Packit Service |
3880ab |
return;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
tnl_print_endpoint("remote", tb[IFLA_VTI_REMOTE], AF_INET);
|
|
Packit Service |
3880ab |
tnl_print_endpoint("local", tb[IFLA_VTI_LOCAL], AF_INET);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (tb[IFLA_VTI_LINK]) {
|
|
Packit Service |
3880ab |
__u32 link = rta_getattr_u32(tb[IFLA_VTI_LINK]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (link) {
|
|
Packit Service |
3880ab |
print_string(PRINT_ANY, "link", "dev %s ",
|
|
Packit Service |
3880ab |
ll_index_to_name(link));
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (tb[IFLA_VTI_IKEY]) {
|
|
Packit Service |
3880ab |
struct rtattr *rta = tb[IFLA_VTI_IKEY];
|
|
Packit Service |
3880ab |
__u32 key = rta_getattr_u32(rta);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2)))
|
|
Packit Service |
3880ab |
print_string(PRINT_ANY, "ikey", "ikey %s ", s2);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (tb[IFLA_VTI_OKEY]) {
|
|
Packit Service |
3880ab |
struct rtattr *rta = tb[IFLA_VTI_OKEY];
|
|
Packit Service |
3880ab |
__u32 key = rta_getattr_u32(rta);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2)))
|
|
Packit Service |
3880ab |
print_string(PRINT_ANY, "okey", "okey %s ", s2);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (tb[IFLA_VTI_FWMARK]) {
|
|
Packit Service |
3880ab |
__u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (fwmark) {
|
|
Packit Service |
3880ab |
print_0xhex(PRINT_ANY,
|
|
Packit Service |
3880ab |
"fwmark", "fwmark %#llx ", fwmark);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
struct link_util vti_link_util = {
|
|
Packit Service |
3880ab |
.id = "vti",
|
|
Packit Service |
3880ab |
.maxattr = IFLA_VTI_MAX,
|
|
Packit Service |
3880ab |
.parse_opt = vti_parse_opt,
|
|
Packit Service |
3880ab |
.print_opt = vti_print_opt,
|
|
Packit Service |
3880ab |
.print_help = vti_print_help,
|
|
Packit Service |
3880ab |
};
|