]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/link_vti6.c
tc: m_action: Improve conversion to C99 style initializers
[mirror_iproute2.git] / ip / link_vti6.c
CommitLineData
2f7fbec2
SK
1/*
2 * link_vti6.c VTI driver module
3 *
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.
8 *
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
12 */
13
14#include <string.h>
15#include <net/if.h>
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <arpa/inet.h>
19
20#include <linux/ip.h>
21#include <linux/if_tunnel.h>
22#include "rt_names.h"
23#include "utils.h"
24#include "ip_common.h"
25#include "tunnel.h"
26
27
28static void usage(void) __attribute__((noreturn));
29static void usage(void)
30{
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");
39 exit(-1);
40}
41
42static int vti6_parse_opt(struct link_util *lu, int argc, char **argv,
43 struct nlmsghdr *n)
44{
45 struct {
46 struct nlmsghdr n;
47 struct ifinfomsg i;
48 char buf[1024];
49 } req;
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;
56f5daac
SH
56 unsigned int ikey = 0;
57 unsigned int okey = 0;
58 unsigned int link = 0;
2f7fbec2
SK
59 int len;
60
61 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
62 memset(&req, 0, sizeof(req));
63
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;
69
c079e121 70 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
2f7fbec2
SK
71get_failed:
72 fprintf(stderr,
73 "Failed to get existing tunnel info.\n");
74 return -1;
75 }
76
77 len = req.n.nlmsg_len;
78 len -= NLMSG_LENGTH(sizeof(*ifi));
79 if (len < 0)
80 goto get_failed;
81
82 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
83
84 if (!tb[IFLA_LINKINFO])
85 goto get_failed;
86
87 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
88
89 if (!linkinfo[IFLA_INFO_DATA])
90 goto get_failed;
91
92 parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX,
93 linkinfo[IFLA_INFO_DATA]);
94
95 if (vtiinfo[IFLA_VTI_IKEY])
96 ikey = rta_getattr_u32(vtiinfo[IFLA_VTI_IKEY]);
97
98 if (vtiinfo[IFLA_VTI_OKEY])
99 okey = rta_getattr_u32(vtiinfo[IFLA_VTI_OKEY]);
100
101 if (vtiinfo[IFLA_VTI_LOCAL])
102 memcpy(&saddr, RTA_DATA(vtiinfo[IFLA_VTI_LOCAL]), sizeof(saddr));
103
104 if (vtiinfo[IFLA_VTI_REMOTE])
105 memcpy(&daddr, RTA_DATA(vtiinfo[IFLA_VTI_REMOTE]), sizeof(daddr));
106
107 if (vtiinfo[IFLA_VTI_LINK])
108 link = rta_getattr_u8(vtiinfo[IFLA_VTI_LINK]);
109 }
110
111 while (argc > 0) {
112 if (!matches(*argv, "key")) {
56f5daac 113 unsigned int uval;
2f7fbec2
SK
114
115 NEXT_ARG();
116 if (strchr(*argv, '.'))
117 uval = get_addr32(*argv);
118 else {
119 if (get_unsigned(&uval, *argv, 0) < 0) {
120 fprintf(stderr,
121 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
122 exit(-1);
123 }
124 uval = htonl(uval);
125 }
126
127 ikey = okey = uval;
128 } else if (!matches(*argv, "ikey")) {
56f5daac 129 unsigned int uval;
2f7fbec2
SK
130
131 NEXT_ARG();
132 if (strchr(*argv, '.'))
133 uval = get_addr32(*argv);
134 else {
135 if (get_unsigned(&uval, *argv, 0) < 0) {
136 fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
137 exit(-1);
138 }
139 uval = htonl(uval);
140 }
141 ikey = uval;
142 } else if (!matches(*argv, "okey")) {
56f5daac 143 unsigned int uval;
2f7fbec2
SK
144
145 NEXT_ARG();
146 if (strchr(*argv, '.'))
147 uval = get_addr32(*argv);
148 else {
149 if (get_unsigned(&uval, *argv, 0) < 0) {
150 fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
151 exit(-1);
152 }
153 uval = htonl(uval);
154 }
155 okey = uval;
156 } else if (!matches(*argv, "remote")) {
157 NEXT_ARG();
158 if (!strcmp(*argv, "any")) {
159 fprintf(stderr, "invalid value for \"remote\": \"%s\"\n", *argv);
160 exit(-1);
161 } else {
162 inet_prefix addr;
56f5daac 163
2f7fbec2
SK
164 get_prefix(&addr, *argv, AF_INET6);
165 memcpy(&daddr, addr.data, addr.bytelen);
166 }
167 } else if (!matches(*argv, "local")) {
168 NEXT_ARG();
169 if (!strcmp(*argv, "any")) {
170 fprintf(stderr, "invalid value for \"local\": \"%s\"\n", *argv);
171 exit(-1);
172 } else {
173 inet_prefix addr;
56f5daac 174
2f7fbec2
SK
175 get_prefix(&addr, *argv, AF_INET6);
176 memcpy(&saddr, addr.data, addr.bytelen);
177 }
178 } else if (!matches(*argv, "dev")) {
179 NEXT_ARG();
180 link = if_nametoindex(*argv);
181 if (link == 0)
182 exit(-1);
183 } else
184 usage();
185 argc--; argv++;
186 }
187
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));
192 if (link)
193 addattr32(n, 1024, IFLA_VTI_LINK, link);
194
195 return 0;
196}
197
198static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
199{
2f7fbec2
SK
200 char s2[64];
201 const char *local = "any";
202 const char *remote = "any";
203 struct in6_addr saddr;
204 struct in6_addr daddr;
205
206 if (!tb)
207 return;
208
209 if (tb[IFLA_VTI_REMOTE]) {
210 memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr));
211
a418e451 212 remote = format_host(AF_INET6, 16, &daddr);
2f7fbec2
SK
213 }
214
215 fprintf(f, "remote %s ", remote);
216
217 if (tb[IFLA_VTI_LOCAL]) {
218 memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr));
219
a418e451 220 local = format_host(AF_INET6, 16, &saddr);
2f7fbec2
SK
221 }
222
223 fprintf(f, "local %s ", local);
224
225 if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) {
56f5daac 226 unsigned int link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]);
2f7fbec2
SK
227 const char *n = if_indextoname(link, s2);
228
229 if (n)
230 fprintf(f, "dev %s ", n);
231 else
232 fprintf(f, "dev %u ", link);
233 }
234
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);
238 }
239
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);
243 }
244}
245
246struct link_util vti6_link_util = {
247 .id = "vti6",
248 .maxattr = IFLA_VTI_MAX,
249 .parse_opt = vti6_parse_opt,
250 .print_opt = vti6_print_opt,
251};