blob: 948711782cca8e44e712064ca291688ae01bc627 [file] [log] [blame]
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +01001/*
2 * link_iptnl.c ipip and sit 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: Nicolas Dichtel <nicolas.dichtel@6wind.com>
10 *
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
vadimk561e6502014-09-30 08:17:31 +030026static void print_usage(FILE *f, int sit)
27{
28 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
29 fprintf(f, " type { ipip | sit } [ remote ADDR ] [ local ADDR ]\n");
30 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
31 fprintf(f, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
Tom Herbertc1159152014-11-05 10:06:25 -080032 fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n");
33 fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n");
34 fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ]\n");
vadimk561e6502014-09-30 08:17:31 +030035 if (sit) {
36 fprintf(f, " [ mode { ip6ip | ipip | any } ]\n");
37 fprintf(f, " [ isatap ]\n");
38 }
39 fprintf(f, "\n");
40 fprintf(f, "Where: NAME := STRING\n");
41 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
42 fprintf(f, " TOS := { NUMBER | inherit }\n");
43 fprintf(f, " TTL := { 1..255 | inherit }\n");
44}
45
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +010046static void usage(int sit) __attribute__((noreturn));
47static void usage(int sit)
48{
vadimk561e6502014-09-30 08:17:31 +030049 print_usage(stderr, sit);
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +010050 exit(-1);
51}
52
53static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
54 struct nlmsghdr *n)
55{
56 struct {
57 struct nlmsghdr n;
58 struct ifinfomsg i;
59 char buf[2048];
60 } req;
61 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
62 struct rtattr *tb[IFLA_MAX + 1];
63 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
64 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
65 int len;
66 __u32 link = 0;
67 __u32 laddr = 0;
68 __u32 raddr = 0;
69 __u8 ttl = 0;
70 __u8 tos = 0;
71 __u8 pmtudisc = 1;
72 __u16 iflags = 0;
Nicolas Dichtel77620be2013-07-16 22:54:14 +020073 __u8 proto = 0;
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +010074 struct in6_addr ip6rdprefix;
75 __u16 ip6rdprefixlen = 0;
76 __u32 ip6rdrelayprefix = 0;
77 __u16 ip6rdrelayprefixlen = 0;
Tom Herbertc1159152014-11-05 10:06:25 -080078 __u16 encaptype = 0;
79 __u16 encapflags = 0;
80 __u16 encapsport = 0;
81 __u16 encapdport = 0;
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +010082
83 memset(&ip6rdprefix, 0, sizeof(ip6rdprefix));
84
85 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
86 memset(&req, 0, sizeof(req));
87
88 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
89 req.n.nlmsg_flags = NLM_F_REQUEST;
90 req.n.nlmsg_type = RTM_GETLINK;
91 req.i.ifi_family = preferred_family;
92 req.i.ifi_index = ifi->ifi_index;
93
94 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) {
95get_failed:
96 fprintf(stderr,
97 "Failed to get existing tunnel info.\n");
98 return -1;
99 }
100
101 len = req.n.nlmsg_len;
102 len -= NLMSG_LENGTH(sizeof(*ifi));
103 if (len < 0)
104 goto get_failed;
105
106 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
107
108 if (!tb[IFLA_LINKINFO])
109 goto get_failed;
110
111 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
112
113 if (!linkinfo[IFLA_INFO_DATA])
114 goto get_failed;
115
116 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
117 linkinfo[IFLA_INFO_DATA]);
118
119 if (iptuninfo[IFLA_IPTUN_LOCAL])
120 laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]);
121
122 if (iptuninfo[IFLA_IPTUN_REMOTE])
123 raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]);
124
125 if (iptuninfo[IFLA_IPTUN_TTL])
126 ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
127
128 if (iptuninfo[IFLA_IPTUN_TOS])
129 tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
130
131 if (iptuninfo[IFLA_IPTUN_PMTUDISC])
132 pmtudisc =
133 rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]);
134
135 if (iptuninfo[IFLA_IPTUN_FLAGS])
136 iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]);
137
138 if (iptuninfo[IFLA_IPTUN_LINK])
139 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
140
Nicolas Dichtel77620be2013-07-16 22:54:14 +0200141 if (iptuninfo[IFLA_IPTUN_PROTO])
142 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
143
Tom Herbertc1159152014-11-05 10:06:25 -0800144 if (iptuninfo[IFLA_IPTUN_ENCAP_TYPE])
145 encaptype = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_TYPE]);
146 if (iptuninfo[IFLA_IPTUN_ENCAP_FLAGS])
147 encapflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]);
148 if (iptuninfo[IFLA_IPTUN_ENCAP_SPORT])
149 encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]);
150 if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT])
151 encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]);
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100152 if (iptuninfo[IFLA_IPTUN_6RD_PREFIX])
153 memcpy(&ip6rdprefix,
154 RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]),
155 sizeof(laddr));
156
157 if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN])
158 ip6rdprefixlen =
159 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]);
160
161 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX])
162 ip6rdrelayprefix =
163 rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]);
164
165 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN])
166 ip6rdrelayprefixlen =
167 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
168 }
169
170 while (argc > 0) {
171 if (strcmp(*argv, "remote") == 0) {
172 NEXT_ARG();
173 if (strcmp(*argv, "any"))
174 raddr = get_addr32(*argv);
175 else
176 raddr = 0;
177 } else if (strcmp(*argv, "local") == 0) {
178 NEXT_ARG();
179 if (strcmp(*argv, "any"))
180 laddr = get_addr32(*argv);
181 else
182 laddr = 0;
183 } else if (matches(*argv, "dev") == 0) {
184 NEXT_ARG();
185 link = if_nametoindex(*argv);
186 if (link == 0)
187 invarg("\"dev\" is invalid", *argv);
188 } else if (strcmp(*argv, "ttl") == 0 ||
189 strcmp(*argv, "hoplimit") == 0) {
190 NEXT_ARG();
191 if (strcmp(*argv, "inherit") != 0) {
192 if (get_u8(&ttl, *argv, 0))
193 invarg("invalid TTL\n", *argv);
194 } else
195 ttl = 0;
196 } else if (strcmp(*argv, "tos") == 0 ||
197 strcmp(*argv, "tclass") == 0 ||
198 matches(*argv, "dsfield") == 0) {
199 __u32 uval;
200 NEXT_ARG();
201 if (strcmp(*argv, "inherit") != 0) {
202 if (rtnl_dsfield_a2n(&uval, *argv))
203 invarg("bad TOS value", *argv);
204 tos = uval;
205 } else
206 tos = 1;
207 } else if (strcmp(*argv, "nopmtudisc") == 0) {
208 pmtudisc = 0;
209 } else if (strcmp(*argv, "pmtudisc") == 0) {
210 pmtudisc = 1;
211 } else if (strcmp(lu->id, "sit") == 0 &&
212 strcmp(*argv, "isatap") == 0) {
213 iflags |= SIT_ISATAP;
Nicolas Dichtel77620be2013-07-16 22:54:14 +0200214 } else if (strcmp(lu->id, "sit") == 0 &&
215 strcmp(*argv, "mode") == 0) {
216 NEXT_ARG();
217 if (strcmp(*argv, "ipv6/ipv4") == 0 ||
218 strcmp(*argv, "ip6ip") == 0)
219 proto = IPPROTO_IPV6;
220 else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
221 strcmp(*argv, "ipip") == 0 ||
222 strcmp(*argv, "ip4ip4") == 0)
223 proto = IPPROTO_IPIP;
224 else if (strcmp(*argv, "any/ipv4") == 0 ||
225 strcmp(*argv, "any") == 0)
226 proto = 0;
227 else
228 invarg("Cannot guess tunnel mode.", *argv);
Tom Herbertc1159152014-11-05 10:06:25 -0800229 } else if (strcmp(*argv, "noencap") == 0) {
230 encaptype = TUNNEL_ENCAP_NONE;
231 } else if (strcmp(*argv, "encap") == 0) {
232 NEXT_ARG();
233 if (strcmp(*argv, "fou") == 0)
234 encaptype = TUNNEL_ENCAP_FOU;
235 else if (strcmp(*argv, "gue") == 0)
236 encaptype = TUNNEL_ENCAP_GUE;
237 else if (strcmp(*argv, "none") == 0)
238 encaptype = TUNNEL_ENCAP_NONE;
239 else
240 invarg("Invalid encap type.", *argv);
241 } else if (strcmp(*argv, "encap-sport") == 0) {
242 NEXT_ARG();
243 if (strcmp(*argv, "auto") == 0)
244 encapsport = 0;
245 else if (get_u16(&encapsport, *argv, 0))
246 invarg("Invalid source port.", *argv);
247 } else if (strcmp(*argv, "encap-dport") == 0) {
248 NEXT_ARG();
249 if (get_u16(&encapdport, *argv, 0))
250 invarg("Invalid destination port.", *argv);
251 } else if (strcmp(*argv, "encap-csum") == 0) {
252 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
253 } else if (strcmp(*argv, "noencap-csum") == 0) {
254 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
255 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
256 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
257 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
258 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100259 } else if (strcmp(*argv, "6rd-prefix") == 0) {
260 inet_prefix prefix;
261 NEXT_ARG();
262 if (get_prefix(&prefix, *argv, AF_INET6))
263 invarg("invalid 6rd_prefix\n", *argv);
264 memcpy(&ip6rdprefix, prefix.data, 16);
265 ip6rdprefixlen = prefix.bitlen;
266 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
267 inet_prefix prefix;
268 NEXT_ARG();
269 if (get_prefix(&prefix, *argv, AF_INET))
270 invarg("invalid 6rd-relay_prefix\n", *argv);
271 memcpy(&ip6rdrelayprefix, prefix.data, 4);
272 ip6rdrelayprefixlen = prefix.bitlen;
273 } else if (strcmp(*argv, "6rd-reset") == 0) {
274 inet_prefix prefix;
275 get_prefix(&prefix, "2002::", AF_INET6);
276 memcpy(&ip6rdprefix, prefix.data, 16);
277 ip6rdprefixlen = 16;
278 ip6rdrelayprefix = 0;
279 ip6rdrelayprefixlen = 0;
280 } else
281 usage(strcmp(lu->id, "sit") == 0);
282 argc--, argv++;
283 }
284
285 if (ttl && pmtudisc == 0) {
Richard Godbee30d07e92013-08-25 22:40:18 -0400286 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100287 exit(-1);
288 }
289
290 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
291 addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr);
292 addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr);
293 addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
294 addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
295 addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc);
Tom Herbertc1159152014-11-05 10:06:25 -0800296
297 addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
298 addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags);
299 addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
300 addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
301
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100302 if (strcmp(lu->id, "sit") == 0) {
303 addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags);
Nicolas Dichtel77620be2013-07-16 22:54:14 +0200304 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100305 if (ip6rdprefixlen) {
306 addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX,
307 &ip6rdprefix, sizeof(ip6rdprefix));
308 addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN,
309 ip6rdprefixlen);
310 addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX,
311 ip6rdrelayprefix);
312 addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
313 ip6rdrelayprefixlen);
314 }
315 }
316
317 return 0;
318}
319
320static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
321{
322 char s1[1024];
323 char s2[64];
324 const char *local = "any";
325 const char *remote = "any";
326
327 if (!tb)
328 return;
329
330 if (tb[IFLA_IPTUN_REMOTE]) {
331 unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
332
333 if (addr)
334 remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
335 }
336
337 fprintf(f, "remote %s ", remote);
338
339 if (tb[IFLA_IPTUN_LOCAL]) {
340 unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
341
342 if (addr)
343 local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
344 }
345
346 fprintf(f, "local %s ", local);
347
348 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
349 unsigned link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
350 const char *n = if_indextoname(link, s2);
351
352 if (n)
353 fprintf(f, "dev %s ", n);
354 else
355 fprintf(f, "dev %u ", link);
356 }
357
358 if (tb[IFLA_IPTUN_TTL] && rta_getattr_u8(tb[IFLA_IPTUN_TTL]))
359 fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_IPTUN_TTL]));
360 else
361 fprintf(f, "ttl inherit ");
362
363 if (tb[IFLA_IPTUN_TOS] && rta_getattr_u8(tb[IFLA_IPTUN_TOS])) {
364 int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);
365
366 fputs("tos ", f);
367 if (tos == 1)
368 fputs("inherit ", f);
369 else
370 fprintf(f, "0x%x ", tos);
371 }
372
373 if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC]))
374 fprintf(f, "pmtudisc ");
375 else
376 fprintf(f, "nopmtudisc ");
377
378 if (tb[IFLA_IPTUN_FLAGS]) {
Nicolas Dichtel195f0f62012-12-14 09:50:33 -0800379 __u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]);
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100380
Nicolas Dichtel195f0f62012-12-14 09:50:33 -0800381 if (iflags & SIT_ISATAP)
382 fprintf(f, "isatap ");
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100383 }
384
385 if (tb[IFLA_IPTUN_6RD_PREFIXLEN] &&
386 *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIXLEN])) {
387 __u16 prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]);
388 __u16 relayprefixlen =
389 rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
390 __u32 relayprefix =
391 rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
392
393 printf("6rd-prefix %s/%u ",
394 inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]),
Nicolas Dichtel195f0f62012-12-14 09:50:33 -0800395 s1, sizeof(s1)),
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100396 prefixlen);
397 if (relayprefix) {
398 printf("6rd-relay_prefix %s/%u ",
399 format_host(AF_INET, 4, &relayprefix, s1,
Nicolas Dichtel195f0f62012-12-14 09:50:33 -0800400 sizeof(s1)),
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100401 relayprefixlen);
402 }
403 }
Tom Herbertc1159152014-11-05 10:06:25 -0800404
405 if (tb[IFLA_IPTUN_ENCAP_TYPE] &&
406 *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
407 __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]);
408 __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]);
409 __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]);
410 __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]);
411
412 fputs("encap ", f);
413 switch (type) {
414 case TUNNEL_ENCAP_FOU:
415 fputs("fou ", f);
416 break;
417 case TUNNEL_ENCAP_GUE:
418 fputs("gue ", f);
419 break;
420 default:
421 fputs("unknown ", f);
422 break;
423 }
424
425 if (sport == 0)
426 fputs("encap-sport auto ", f);
427 else
428 fprintf(f, "encap-sport %u", ntohs(sport));
429
430 fprintf(f, "encap-dport %u ", ntohs(dport));
431
432 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
433 fputs("encap-csum ", f);
434 else
435 fputs("noencap-csum ", f);
436
437 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
438 fputs("encap-csum6 ", f);
439 else
440 fputs("noencap-csum6 ", f);
441 }
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100442}
443
vadimk561e6502014-09-30 08:17:31 +0300444static void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
445 FILE *f)
446{
447 print_usage(f, strcmp(lu->id, "sit") == 0);
448}
449
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100450struct link_util ipip_link_util = {
451 .id = "ipip",
452 .maxattr = IFLA_IPTUN_MAX,
453 .parse_opt = iptunnel_parse_opt,
454 .print_opt = iptunnel_print_opt,
vadimk561e6502014-09-30 08:17:31 +0300455 .print_help = iptunnel_print_help,
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100456};
457
458struct link_util sit_link_util = {
459 .id = "sit",
460 .maxattr = IFLA_IPTUN_MAX,
461 .parse_opt = iptunnel_parse_opt,
462 .print_opt = iptunnel_print_opt,
vadimk561e6502014-09-30 08:17:31 +0300463 .print_help = iptunnel_print_help,
Nicolas Dichtel1ce2de92012-12-12 10:51:47 +0100464};