blob: 385f435571234396a1e9371f0f14a070455a623c [file] [log] [blame]
Saurabh73579332012-06-27 15:01:57 +00001/*
2 * link_vti.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 */
12
13#include <string.h>
14#include <net/if.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <arpa/inet.h>
18
19#include <linux/ip.h>
20#include <linux/if_tunnel.h>
21#include "rt_names.h"
22#include "utils.h"
23#include "ip_common.h"
24#include "tunnel.h"
25
26
27static void usage(void) __attribute__((noreturn));
28static void usage(void)
29{
30 fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n");
31 fprintf(stderr, " type { vti } [ remote ADDR ] [ local ADDR ]\n");
32 fprintf(stderr, " [ [i|o]key KEY ]\n");
33 fprintf(stderr, " [ dev PHYS_DEV ]\n");
34 fprintf(stderr, "\n");
35 fprintf(stderr, "Where: NAME := STRING\n");
36 fprintf(stderr, " ADDR := { IP_ADDRESS }\n");
37 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
38 exit(-1);
39}
40
41static int vti_parse_opt(struct link_util *lu, int argc, char **argv,
42 struct nlmsghdr *n)
43{
44 struct {
45 struct nlmsghdr n;
46 struct ifinfomsg i;
47 char buf[1024];
48 } req;
49 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
50 struct rtattr *tb[IFLA_MAX + 1];
51 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
52 struct rtattr *vtiinfo[IFLA_VTI_MAX + 1];
53 unsigned ikey = 0;
54 unsigned okey = 0;
55 unsigned saddr = 0;
56 unsigned daddr = 0;
57 unsigned link = 0;
58 int len;
59
60 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
61 memset(&req, 0, sizeof(req));
62
63 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
64 req.n.nlmsg_flags = NLM_F_REQUEST;
65 req.n.nlmsg_type = RTM_GETLINK;
66 req.i.ifi_family = preferred_family;
67 req.i.ifi_index = ifi->ifi_index;
68
69 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) {
70get_failed:
71 fprintf(stderr,
72 "Failed to get existing tunnel info.\n");
73 return -1;
74 }
75
76 len = req.n.nlmsg_len;
77 len -= NLMSG_LENGTH(sizeof(*ifi));
78 if (len < 0)
79 goto get_failed;
80
81 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
82
83 if (!tb[IFLA_LINKINFO])
84 goto get_failed;
85
86 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
87
88 if (!linkinfo[IFLA_INFO_DATA])
89 goto get_failed;
90
91 parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX,
92 linkinfo[IFLA_INFO_DATA]);
93
94 if (vtiinfo[IFLA_VTI_IKEY])
95 ikey = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_IKEY]);
96
97 if (vtiinfo[IFLA_VTI_OKEY])
98 okey = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_OKEY]);
99
100 if (vtiinfo[IFLA_VTI_LOCAL])
101 saddr = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_LOCAL]);
102
103 if (vtiinfo[IFLA_VTI_REMOTE])
104 daddr = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_REMOTE]);
105
106 if (vtiinfo[IFLA_VTI_LINK])
107 link = *(__u8 *)RTA_DATA(vtiinfo[IFLA_VTI_LINK]);
108 }
109
110 while (argc > 0) {
111 if (!matches(*argv, "key")) {
112 unsigned uval;
113
114 NEXT_ARG();
115 if (strchr(*argv, '.'))
116 uval = get_addr32(*argv);
117 else {
118 if (get_unsigned(&uval, *argv, 0) < 0) {
119 fprintf(stderr,
120 "Invalid value for \"key\"\n");
121 exit(-1);
122 }
123 uval = htonl(uval);
124 }
125
126 ikey = okey = uval;
127 } else if (!matches(*argv, "ikey")) {
128 unsigned uval;
129
130 NEXT_ARG();
131 if (strchr(*argv, '.'))
132 uval = get_addr32(*argv);
133 else {
134 if (get_unsigned(&uval, *argv, 0) < 0) {
135 fprintf(stderr, "invalid value of \"ikey\"\n");
136 exit(-1);
137 }
138 uval = htonl(uval);
139 }
140 ikey = uval;
141 } else if (!matches(*argv, "okey")) {
142 unsigned uval;
143
144 NEXT_ARG();
145 if (strchr(*argv, '.'))
146 uval = get_addr32(*argv);
147 else {
148 if (get_unsigned(&uval, *argv, 0) < 0) {
149 fprintf(stderr, "invalid value of \"okey\"\n");
150 exit(-1);
151 }
152 uval = htonl(uval);
153 }
154 okey = uval;
155 } else if (!matches(*argv, "remote")) {
156 NEXT_ARG();
157 if (!strcmp(*argv, "any")) {
158 fprintf(stderr, "invalid value of \"remote\"\n");
159 exit(-1);
160 } else {
161 daddr = get_addr32(*argv);
162 }
163 } else if (!matches(*argv, "local")) {
164 NEXT_ARG();
165 if (!strcmp(*argv, "any")) {
166 fprintf(stderr, "invalid value of \"local\"\n");
167 exit(-1);
168 } else {
169 saddr = get_addr32(*argv);
170 }
171 } else if (!matches(*argv, "dev")) {
172 NEXT_ARG();
173 link = if_nametoindex(*argv);
174 if (link == 0)
175 exit(-1);
176 } else
177 usage();
178 argc--; argv++;
179 }
180
181 addattr32(n, 1024, IFLA_VTI_IKEY, ikey);
182 addattr32(n, 1024, IFLA_VTI_OKEY, okey);
183 addattr_l(n, 1024, IFLA_VTI_LOCAL, &saddr, 4);
184 addattr_l(n, 1024, IFLA_VTI_REMOTE, &daddr, 4);
185 if (link)
186 addattr32(n, 1024, IFLA_VTI_LINK, link);
187
188 return 0;
189}
190
191static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
192{
193 char s1[1024];
194 char s2[64];
195 const char *local = "any";
196 const char *remote = "any";
197
198 if (!tb)
199 return;
200
201 if (tb[IFLA_VTI_REMOTE]) {
202 unsigned addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_REMOTE]);
203
204 if (addr)
205 remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
206 }
207
208 fprintf(f, "remote %s ", remote);
209
210 if (tb[IFLA_VTI_LOCAL]) {
211 unsigned addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LOCAL]);
212
213 if (addr)
214 local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
215 }
216
217 fprintf(f, "local %s ", local);
218
219 if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) {
220 unsigned link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]);
221 const char *n = if_indextoname(link, s2);
222
223 if (n)
224 fprintf(f, "dev %s ", n);
225 else
226 fprintf(f, "dev %u ", link);
227 }
228
229 if (tb[IFLA_VTI_IKEY]) {
230 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2));
231 fprintf(f, "ikey %s ", s2);
232 }
233
234 if (tb[IFLA_VTI_OKEY]) {
235 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2));
236 fprintf(f, "okey %s ", s2);
237 }
238}
239
240struct link_util vti_link_util = {
241 .id = "vti",
242 .maxattr = IFLA_VTI_MAX,
243 .parse_opt = vti_parse_opt,
244 .print_opt = vti_print_opt,
245};