blob: 6274c836cb944b9c47aad6804b0395d8b08e190c [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,
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000120 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
Saurabh73579332012-06-27 15:01:57 +0000121 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) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000135 fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
Saurabh73579332012-06-27 15:01:57 +0000136 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) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000149 fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
Saurabh73579332012-06-27 15:01:57 +0000150 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")) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000158 fprintf(stderr, "invalid value for \"remote\": \"%s\"\n", *argv);
Saurabh73579332012-06-27 15:01:57 +0000159 exit(-1);
160 } else {
161 daddr = get_addr32(*argv);
162 }
163 } else if (!matches(*argv, "local")) {
164 NEXT_ARG();
165 if (!strcmp(*argv, "any")) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000166 fprintf(stderr, "invalid value for \"local\": \"%s\"\n", *argv);
Saurabh73579332012-06-27 15:01:57 +0000167 exit(-1);
168 } else {
169 saddr = get_addr32(*argv);
170 }
171 } else if (!matches(*argv, "dev")) {
172 NEXT_ARG();
173 link = if_nametoindex(*argv);
Cong Wang0cb6bb52014-06-03 16:06:45 -0700174 if (link == 0) {
175 fprintf(stderr, "Cannot find device \"%s\"\n",
176 *argv);
Saurabh73579332012-06-27 15:01:57 +0000177 exit(-1);
Cong Wang0cb6bb52014-06-03 16:06:45 -0700178 }
Saurabh73579332012-06-27 15:01:57 +0000179 } else
180 usage();
181 argc--; argv++;
182 }
183
184 addattr32(n, 1024, IFLA_VTI_IKEY, ikey);
185 addattr32(n, 1024, IFLA_VTI_OKEY, okey);
186 addattr_l(n, 1024, IFLA_VTI_LOCAL, &saddr, 4);
187 addattr_l(n, 1024, IFLA_VTI_REMOTE, &daddr, 4);
188 if (link)
189 addattr32(n, 1024, IFLA_VTI_LINK, link);
190
191 return 0;
192}
193
194static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
195{
196 char s1[1024];
197 char s2[64];
198 const char *local = "any";
199 const char *remote = "any";
200
201 if (!tb)
202 return;
203
204 if (tb[IFLA_VTI_REMOTE]) {
205 unsigned addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_REMOTE]);
206
207 if (addr)
208 remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
209 }
210
211 fprintf(f, "remote %s ", remote);
212
213 if (tb[IFLA_VTI_LOCAL]) {
214 unsigned addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LOCAL]);
215
216 if (addr)
217 local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
218 }
219
220 fprintf(f, "local %s ", local);
221
222 if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) {
223 unsigned link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]);
224 const char *n = if_indextoname(link, s2);
225
226 if (n)
227 fprintf(f, "dev %s ", n);
228 else
229 fprintf(f, "dev %u ", link);
230 }
231
232 if (tb[IFLA_VTI_IKEY]) {
233 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2));
234 fprintf(f, "ikey %s ", s2);
235 }
236
237 if (tb[IFLA_VTI_OKEY]) {
238 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2));
239 fprintf(f, "okey %s ", s2);
240 }
241}
242
243struct link_util vti_link_util = {
244 .id = "vti",
245 .maxattr = IFLA_VTI_MAX,
246 .parse_opt = vti_parse_opt,
247 .print_opt = vti_print_opt,
248};