]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_vti6.c
2 * link_vti6.c VTI driver module
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Herbert Xu <herbert@gondor.apana.org.au>
10 * Saurabh Mohan <saurabh.mohan@vyatta.com> Modified link_gre.c for VTI
11 * Steffen Klassert <steffen.klassert@secunet.com> Modified link_vti.c for IPv6
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
21 #include <linux/if_tunnel.h>
24 #include "ip_common.h"
28 static void usage(void) __attribute__((noreturn
));
29 static void usage(void)
31 fprintf(stderr
, "Usage: ip link { add | set | change | replace | del } NAME\n");
32 fprintf(stderr
, " type { vti6 } [ remote ADDR ] [ local ADDR ]\n");
33 fprintf(stderr
, " [ [i|o]key KEY ]\n");
34 fprintf(stderr
, " [ dev PHYS_DEV ]\n");
35 fprintf(stderr
, "\n");
36 fprintf(stderr
, "Where: NAME := STRING\n");
37 fprintf(stderr
, " ADDR := { IPV6_ADDRESS }\n");
38 fprintf(stderr
, " KEY := { DOTTED_QUAD | NUMBER }\n");
42 static int vti6_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
50 struct ifinfomsg
*ifi
= (struct ifinfomsg
*)(n
+ 1);
51 struct rtattr
*tb
[IFLA_MAX
+ 1];
52 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
53 struct rtattr
*vtiinfo
[IFLA_VTI_MAX
+ 1];
54 struct in6_addr saddr
;
55 struct in6_addr daddr
;
56 unsigned int ikey
= 0;
57 unsigned int okey
= 0;
58 unsigned int link
= 0;
61 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
62 memset(&req
, 0, sizeof(req
));
64 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(*ifi
));
65 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
66 req
.n
.nlmsg_type
= RTM_GETLINK
;
67 req
.i
.ifi_family
= preferred_family
;
68 req
.i
.ifi_index
= ifi
->ifi_index
;
70 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0) {
73 "Failed to get existing tunnel info.\n");
77 len
= req
.n
.nlmsg_len
;
78 len
-= NLMSG_LENGTH(sizeof(*ifi
));
82 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(&req
.i
), len
);
84 if (!tb
[IFLA_LINKINFO
])
87 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
89 if (!linkinfo
[IFLA_INFO_DATA
])
92 parse_rtattr_nested(vtiinfo
, IFLA_VTI_MAX
,
93 linkinfo
[IFLA_INFO_DATA
]);
95 if (vtiinfo
[IFLA_VTI_IKEY
])
96 ikey
= rta_getattr_u32(vtiinfo
[IFLA_VTI_IKEY
]);
98 if (vtiinfo
[IFLA_VTI_OKEY
])
99 okey
= rta_getattr_u32(vtiinfo
[IFLA_VTI_OKEY
]);
101 if (vtiinfo
[IFLA_VTI_LOCAL
])
102 memcpy(&saddr
, RTA_DATA(vtiinfo
[IFLA_VTI_LOCAL
]), sizeof(saddr
));
104 if (vtiinfo
[IFLA_VTI_REMOTE
])
105 memcpy(&daddr
, RTA_DATA(vtiinfo
[IFLA_VTI_REMOTE
]), sizeof(daddr
));
107 if (vtiinfo
[IFLA_VTI_LINK
])
108 link
= rta_getattr_u8(vtiinfo
[IFLA_VTI_LINK
]);
112 if (!matches(*argv
, "key")) {
116 if (strchr(*argv
, '.'))
117 uval
= get_addr32(*argv
);
119 if (get_unsigned(&uval
, *argv
, 0) < 0) {
121 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv
);
128 } else if (!matches(*argv
, "ikey")) {
132 if (strchr(*argv
, '.'))
133 uval
= get_addr32(*argv
);
135 if (get_unsigned(&uval
, *argv
, 0) < 0) {
136 fprintf(stderr
, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv
);
142 } else if (!matches(*argv
, "okey")) {
146 if (strchr(*argv
, '.'))
147 uval
= get_addr32(*argv
);
149 if (get_unsigned(&uval
, *argv
, 0) < 0) {
150 fprintf(stderr
, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv
);
156 } else if (!matches(*argv
, "remote")) {
158 if (!strcmp(*argv
, "any")) {
159 fprintf(stderr
, "invalid value for \"remote\": \"%s\"\n", *argv
);
164 get_prefix(&addr
, *argv
, AF_INET6
);
165 memcpy(&daddr
, addr
.data
, addr
.bytelen
);
167 } else if (!matches(*argv
, "local")) {
169 if (!strcmp(*argv
, "any")) {
170 fprintf(stderr
, "invalid value for \"local\": \"%s\"\n", *argv
);
175 get_prefix(&addr
, *argv
, AF_INET6
);
176 memcpy(&saddr
, addr
.data
, addr
.bytelen
);
178 } else if (!matches(*argv
, "dev")) {
180 link
= if_nametoindex(*argv
);
188 addattr32(n
, 1024, IFLA_VTI_IKEY
, ikey
);
189 addattr32(n
, 1024, IFLA_VTI_OKEY
, okey
);
190 addattr_l(n
, 1024, IFLA_VTI_LOCAL
, &saddr
, sizeof(saddr
));
191 addattr_l(n
, 1024, IFLA_VTI_REMOTE
, &daddr
, sizeof(daddr
));
193 addattr32(n
, 1024, IFLA_VTI_LINK
, link
);
198 static void vti6_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
201 const char *local
= "any";
202 const char *remote
= "any";
203 struct in6_addr saddr
;
204 struct in6_addr daddr
;
209 if (tb
[IFLA_VTI_REMOTE
]) {
210 memcpy(&daddr
, RTA_DATA(tb
[IFLA_VTI_REMOTE
]), sizeof(daddr
));
212 remote
= format_host(AF_INET6
, 16, &daddr
);
215 fprintf(f
, "remote %s ", remote
);
217 if (tb
[IFLA_VTI_LOCAL
]) {
218 memcpy(&saddr
, RTA_DATA(tb
[IFLA_VTI_LOCAL
]), sizeof(saddr
));
220 local
= format_host(AF_INET6
, 16, &saddr
);
223 fprintf(f
, "local %s ", local
);
225 if (tb
[IFLA_VTI_LINK
] && *(__u32
*)RTA_DATA(tb
[IFLA_VTI_LINK
])) {
226 unsigned int link
= *(__u32
*)RTA_DATA(tb
[IFLA_VTI_LINK
]);
227 const char *n
= if_indextoname(link
, s2
);
230 fprintf(f
, "dev %s ", n
);
232 fprintf(f
, "dev %u ", link
);
235 if (tb
[IFLA_VTI_IKEY
]) {
236 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_VTI_IKEY
]), s2
, sizeof(s2
));
237 fprintf(f
, "ikey %s ", s2
);
240 if (tb
[IFLA_VTI_OKEY
]) {
241 inet_ntop(AF_INET
, RTA_DATA(tb
[IFLA_VTI_OKEY
]), s2
, sizeof(s2
));
242 fprintf(f
, "okey %s ", s2
);
246 struct link_util vti6_link_util
= {
248 .maxattr
= IFLA_VTI_MAX
,
249 .parse_opt
= vti6_parse_opt
,
250 .print_opt
= vti6_print_opt
,