]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/iptunnel.c
ip: print "temporary" for IPv6 temp addresses
[mirror_iproute2.git] / ip / iptunnel.c
CommitLineData
aba5acdf
SH
1/*
2 * iptunnel.c "ip tunnel"
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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
aba5acdf
SH
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
d9bd1bd9 17#include <sys/types.h>
aba5acdf 18#include <sys/socket.h>
7272ddc7 19#include <arpa/inet.h>
aba5acdf
SH
20#include <sys/ioctl.h>
21#include <linux/if.h>
22#include <linux/if_arp.h>
7272ddc7 23#include <linux/ip.h>
aba5acdf
SH
24#include <linux/if_tunnel.h>
25
26#include "rt_names.h"
27#include "utils.h"
288384f2 28#include "ip_common.h"
d9bd1bd9 29#include "tunnel.h"
aba5acdf
SH
30
31static void usage(void) __attribute__((noreturn));
32
33static void usage(void)
34{
a07e9912 35 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl } [ NAME ]\n");
0bd17929 36 fprintf(stderr, " [ mode { ipip | gre | sit | isatap } ] [ remote ADDR ] [ local ADDR ]\n");
aba5acdf 37 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
a07e9912 38 fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
aba5acdf
SH
39 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
40 fprintf(stderr, "\n");
41 fprintf(stderr, "Where: NAME := STRING\n");
42 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
43 fprintf(stderr, " TOS := { NUMBER | inherit }\n");
44 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
45 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
46 exit(-1);
47}
48
aba5acdf
SH
49static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
50{
51 int count = 0;
52 char medium[IFNAMSIZ];
0bd17929 53 int isatap = 0;
aba5acdf
SH
54
55 memset(p, 0, sizeof(*p));
56 memset(&medium, 0, sizeof(medium));
57
58 p->iph.version = 4;
59 p->iph.ihl = 5;
60#ifndef IP_DF
61#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
62#endif
63 p->iph.frag_off = htons(IP_DF);
64
65 while (argc > 0) {
66 if (strcmp(*argv, "mode") == 0) {
67 NEXT_ARG();
68 if (strcmp(*argv, "ipip") == 0 ||
69 strcmp(*argv, "ip/ip") == 0) {
70 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
71 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
72 exit(-1);
73 }
74 p->iph.protocol = IPPROTO_IPIP;
75 } else if (strcmp(*argv, "gre") == 0 ||
76 strcmp(*argv, "gre/ip") == 0) {
77 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
78 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
79 exit(-1);
80 }
81 p->iph.protocol = IPPROTO_GRE;
82 } else if (strcmp(*argv, "sit") == 0 ||
83 strcmp(*argv, "ipv6/ip") == 0) {
84 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
85 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
86 exit(-1);
87 }
88 p->iph.protocol = IPPROTO_IPV6;
0bd17929
TF
89 } else if (strcmp(*argv, "isatap") == 0) {
90 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
91 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
92 exit(-1);
93 }
94 p->iph.protocol = IPPROTO_IPV6;
95 isatap++;
aba5acdf
SH
96 } else {
97 fprintf(stderr,"Cannot guess tunnel mode.\n");
98 exit(-1);
99 }
100 } else if (strcmp(*argv, "key") == 0) {
101 unsigned uval;
102 NEXT_ARG();
103 p->i_flags |= GRE_KEY;
104 p->o_flags |= GRE_KEY;
105 if (strchr(*argv, '.'))
106 p->i_key = p->o_key = get_addr32(*argv);
107 else {
108 if (get_unsigned(&uval, *argv, 0)<0) {
109 fprintf(stderr, "invalid value of \"key\"\n");
110 exit(-1);
111 }
112 p->i_key = p->o_key = htonl(uval);
113 }
114 } else if (strcmp(*argv, "ikey") == 0) {
115 unsigned uval;
116 NEXT_ARG();
117 p->i_flags |= GRE_KEY;
118 if (strchr(*argv, '.'))
4282c6c5 119 p->i_key = get_addr32(*argv);
aba5acdf
SH
120 else {
121 if (get_unsigned(&uval, *argv, 0)<0) {
122 fprintf(stderr, "invalid value of \"ikey\"\n");
123 exit(-1);
124 }
125 p->i_key = htonl(uval);
126 }
127 } else if (strcmp(*argv, "okey") == 0) {
128 unsigned uval;
129 NEXT_ARG();
130 p->o_flags |= GRE_KEY;
131 if (strchr(*argv, '.'))
132 p->o_key = get_addr32(*argv);
133 else {
134 if (get_unsigned(&uval, *argv, 0)<0) {
135 fprintf(stderr, "invalid value of \"okey\"\n");
136 exit(-1);
137 }
138 p->o_key = htonl(uval);
139 }
140 } else if (strcmp(*argv, "seq") == 0) {
141 p->i_flags |= GRE_SEQ;
142 p->o_flags |= GRE_SEQ;
143 } else if (strcmp(*argv, "iseq") == 0) {
144 p->i_flags |= GRE_SEQ;
145 } else if (strcmp(*argv, "oseq") == 0) {
146 p->o_flags |= GRE_SEQ;
147 } else if (strcmp(*argv, "csum") == 0) {
148 p->i_flags |= GRE_CSUM;
149 p->o_flags |= GRE_CSUM;
150 } else if (strcmp(*argv, "icsum") == 0) {
151 p->i_flags |= GRE_CSUM;
152 } else if (strcmp(*argv, "ocsum") == 0) {
153 p->o_flags |= GRE_CSUM;
154 } else if (strcmp(*argv, "nopmtudisc") == 0) {
155 p->iph.frag_off = 0;
156 } else if (strcmp(*argv, "pmtudisc") == 0) {
157 p->iph.frag_off = htons(IP_DF);
158 } else if (strcmp(*argv, "remote") == 0) {
159 NEXT_ARG();
160 if (strcmp(*argv, "any"))
161 p->iph.daddr = get_addr32(*argv);
162 } else if (strcmp(*argv, "local") == 0) {
163 NEXT_ARG();
164 if (strcmp(*argv, "any"))
165 p->iph.saddr = get_addr32(*argv);
166 } else if (strcmp(*argv, "dev") == 0) {
167 NEXT_ARG();
168 strncpy(medium, *argv, IFNAMSIZ-1);
eddde110
YH
169 } else if (strcmp(*argv, "ttl") == 0 ||
170 strcmp(*argv, "hoplimit") == 0) {
aba5acdf
SH
171 unsigned uval;
172 NEXT_ARG();
173 if (strcmp(*argv, "inherit") != 0) {
174 if (get_unsigned(&uval, *argv, 0))
175 invarg("invalid TTL\n", *argv);
176 if (uval > 255)
177 invarg("TTL must be <=255\n", *argv);
178 p->iph.ttl = uval;
179 }
180 } else if (strcmp(*argv, "tos") == 0 ||
eddde110 181 strcmp(*argv, "tclass") == 0 ||
aba5acdf
SH
182 matches(*argv, "dsfield") == 0) {
183 __u32 uval;
184 NEXT_ARG();
185 if (strcmp(*argv, "inherit") != 0) {
186 if (rtnl_dsfield_a2n(&uval, *argv))
187 invarg("bad TOS value", *argv);
188 p->iph.tos = uval;
189 } else
190 p->iph.tos = 1;
191 } else {
192 if (strcmp(*argv, "name") == 0) {
193 NEXT_ARG();
1f1ae524 194 } else if (matches(*argv, "help") == 0)
aba5acdf
SH
195 usage();
196 if (p->name[0])
197 duparg2("name", *argv);
198 strncpy(p->name, *argv, IFNAMSIZ);
199 if (cmd == SIOCCHGTUNNEL && count == 0) {
200 struct ip_tunnel_parm old_p;
201 memset(&old_p, 0, sizeof(old_p));
d9bd1bd9 202 if (tnl_get_ioctl(*argv, &old_p))
aba5acdf
SH
203 return -1;
204 *p = old_p;
205 }
206 }
207 count++;
208 argc--; argv++;
209 }
210
211
212 if (p->iph.protocol == 0) {
213 if (memcmp(p->name, "gre", 3) == 0)
214 p->iph.protocol = IPPROTO_GRE;
215 else if (memcmp(p->name, "ipip", 4) == 0)
216 p->iph.protocol = IPPROTO_IPIP;
217 else if (memcmp(p->name, "sit", 3) == 0)
218 p->iph.protocol = IPPROTO_IPV6;
0bd17929
TF
219 else if (memcmp(p->name, "isatap", 6) == 0) {
220 p->iph.protocol = IPPROTO_IPV6;
221 isatap++;
222 }
aba5acdf
SH
223 }
224
225 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
226 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
227 fprintf(stderr, "Keys are not allowed with ipip and sit.\n");
228 return -1;
229 }
230 }
231
232 if (medium[0]) {
d9bd1bd9 233 p->link = tnl_ioctl_get_ifindex(medium);
aba5acdf
SH
234 if (p->link == 0)
235 return -1;
236 }
237
238 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
239 p->i_key = p->iph.daddr;
240 p->i_flags |= GRE_KEY;
241 }
242 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
243 p->o_key = p->iph.daddr;
244 p->o_flags |= GRE_KEY;
245 }
246 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
247 fprintf(stderr, "Broadcast tunnel requires a source address.\n");
248 return -1;
249 }
eeef12c5 250 if (isatap)
0bd17929 251 p->i_flags |= SIT_ISATAP;
0bd17929 252
aba5acdf
SH
253 return 0;
254}
255
256
257static int do_add(int cmd, int argc, char **argv)
258{
259 struct ip_tunnel_parm p;
260
261 if (parse_args(argc, argv, cmd, &p) < 0)
262 return -1;
263
264 if (p.iph.ttl && p.iph.frag_off == 0) {
265 fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n");
266 return -1;
267 }
268
269 switch (p.iph.protocol) {
270 case IPPROTO_IPIP:
d9bd1bd9 271 return tnl_add_ioctl(cmd, "tunl0", p.name, &p);
aba5acdf 272 case IPPROTO_GRE:
d9bd1bd9 273 return tnl_add_ioctl(cmd, "gre0", p.name, &p);
aba5acdf 274 case IPPROTO_IPV6:
d9bd1bd9 275 return tnl_add_ioctl(cmd, "sit0", p.name, &p);
ae665a52 276 default:
aba5acdf
SH
277 fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n");
278 return -1;
279 }
280 return -1;
281}
282
d9bd1bd9 283static int do_del(int argc, char **argv)
aba5acdf
SH
284{
285 struct ip_tunnel_parm p;
286
287 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
288 return -1;
289
290 switch (p.iph.protocol) {
291 case IPPROTO_IPIP:
d9bd1bd9 292 return tnl_del_ioctl("tunl0", p.name, &p);
aba5acdf 293 case IPPROTO_GRE:
d9bd1bd9 294 return tnl_del_ioctl("gre0", p.name, &p);
aba5acdf 295 case IPPROTO_IPV6:
d9bd1bd9 296 return tnl_del_ioctl("sit0", p.name, &p);
ae665a52 297 default:
d9bd1bd9 298 return tnl_del_ioctl(p.name, p.name, &p);
aba5acdf
SH
299 }
300 return -1;
301}
302
d9bd1bd9 303static void print_tunnel(struct ip_tunnel_parm *p)
aba5acdf
SH
304{
305 char s1[1024];
306 char s2[1024];
307 char s3[64];
308 char s4[64];
309
310 inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
311 inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
312
313 /* Do not use format_host() for local addr,
314 * symbolic name will not be useful.
315 */
316 printf("%s: %s/ip remote %s local %s ",
317 p->name,
d9bd1bd9 318 tnl_strproto(p->iph.protocol),
aba5acdf
SH
319 p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
320 p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
321
a07e9912
SH
322 if (p->i_flags & SIT_ISATAP) {
323 struct ip_tunnel_prl prl[16];
324 int i;
325
326 memset(prl, 0, sizeof(prl));
327 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
328 prl[0].addr = htonl(INADDR_ANY);
329
330 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
331 for (i = 1; i < sizeof(prl) / sizeof(prl[0]); i++)
332 {
333 if (prl[i].addr != htonl(INADDR_ANY)) {
334 printf(" %s %s ",
335 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
336 format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1)));
337 }
338 }
339 }
340
aba5acdf 341 if (p->link) {
d9bd1bd9 342 char *n = tnl_ioctl_get_ifname(p->link);
aba5acdf
SH
343 if (n)
344 printf(" dev %s ", n);
345 }
346
347 if (p->iph.ttl)
348 printf(" ttl %d ", p->iph.ttl);
349 else
350 printf(" ttl inherit ");
ae665a52 351
aba5acdf
SH
352 if (p->iph.tos) {
353 SPRINT_BUF(b1);
354 printf(" tos");
355 if (p->iph.tos&1)
356 printf(" inherit");
357 if (p->iph.tos&~1)
358 printf("%c%s ", p->iph.tos&1 ? '/' : ' ',
359 rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1)));
360 }
361
362 if (!(p->iph.frag_off&htons(IP_DF)))
363 printf(" nopmtudisc");
364
365 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
366 printf(" key %s", s3);
367 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
368 if (p->i_flags&GRE_KEY)
369 printf(" ikey %s ", s3);
370 if (p->o_flags&GRE_KEY)
371 printf(" okey %s ", s4);
372 }
373
374 if (p->i_flags&GRE_SEQ)
375 printf("%s Drop packets out of sequence.\n", _SL_);
376 if (p->i_flags&GRE_CSUM)
377 printf("%s Checksum in received packet is required.", _SL_);
378 if (p->o_flags&GRE_SEQ)
379 printf("%s Sequence packets on output.", _SL_);
380 if (p->o_flags&GRE_CSUM)
381 printf("%s Checksum output packets.", _SL_);
382}
383
384static int do_tunnels_list(struct ip_tunnel_parm *p)
385{
386 char name[IFNAMSIZ];
387 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
388 rx_fifo, rx_frame,
389 tx_bytes, tx_packets, tx_errs, tx_drops,
390 tx_fifo, tx_colls, tx_carrier, rx_multi;
391 int type;
392 struct ip_tunnel_parm p1;
393
394 char buf[512];
395 FILE *fp = fopen("/proc/net/dev", "r");
396 if (fp == NULL) {
397 perror("fopen");
398 return -1;
399 }
400
401 fgets(buf, sizeof(buf), fp);
402 fgets(buf, sizeof(buf), fp);
403
404 while (fgets(buf, sizeof(buf), fp) != NULL) {
405 char *ptr;
406 buf[sizeof(buf) - 1] = 0;
407 if ((ptr = strchr(buf, ':')) == NULL ||
408 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
409 fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n");
410 return -1;
411 }
412 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
413 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
414 &rx_fifo, &rx_frame, &rx_multi,
415 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
416 &tx_fifo, &tx_colls, &tx_carrier) != 14)
417 continue;
418 if (p->name[0] && strcmp(p->name, name))
419 continue;
d9bd1bd9 420 type = tnl_ioctl_get_iftype(name);
aba5acdf
SH
421 if (type == -1) {
422 fprintf(stderr, "Failed to get type of [%s]\n", name);
423 continue;
424 }
425 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
426 continue;
427 memset(&p1, 0, sizeof(p1));
d9bd1bd9 428 if (tnl_get_ioctl(name, &p1))
aba5acdf
SH
429 continue;
430 if ((p->link && p1.link != p->link) ||
431 (p->name[0] && strcmp(p1.name, p->name)) ||
432 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
433 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
434 (p->i_key && p1.i_key != p->i_key))
435 continue;
436 print_tunnel(&p1);
437 if (show_stats) {
438 printf("%s", _SL_);
439 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
440 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
441 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
442 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
443 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
444 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
445 }
446 printf("\n");
447 }
448 return 0;
449}
450
451static int do_show(int argc, char **argv)
452{
453 int err;
454 struct ip_tunnel_parm p;
455
456 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
457 return -1;
458
459 switch (p.iph.protocol) {
ae665a52 460 case IPPROTO_IPIP:
d9bd1bd9 461 err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
aba5acdf
SH
462 break;
463 case IPPROTO_GRE:
d9bd1bd9 464 err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p);
aba5acdf
SH
465 break;
466 case IPPROTO_IPV6:
d9bd1bd9 467 err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p);
aba5acdf
SH
468 break;
469 default:
470 do_tunnels_list(&p);
471 return 0;
472 }
473 if (err)
474 return -1;
475
476 print_tunnel(&p);
477 printf("\n");
478 return 0;
479}
480
a07e9912
SH
481static int do_prl(int argc, char **argv)
482{
483 struct ip_tunnel_prl p;
484 int count = 0;
485 int devname = 0;
486 int cmd = 0;
487 char medium[IFNAMSIZ];
488
489 memset(&p, 0, sizeof(p));
490 memset(&medium, 0, sizeof(medium));
491
492 while (argc > 0) {
493 if (strcmp(*argv, "prl-default") == 0) {
494 NEXT_ARG();
495 cmd = SIOCADDPRL;
496 p.addr = get_addr32(*argv);
497 p.flags |= PRL_DEFAULT;
498 count++;
499 } else if (strcmp(*argv, "prl-nodefault") == 0) {
500 NEXT_ARG();
501 cmd = SIOCADDPRL;
502 p.addr = get_addr32(*argv);
503 count++;
504 } else if (strcmp(*argv, "prl-delete") == 0) {
505 NEXT_ARG();
506 cmd = SIOCDELPRL;
507 p.addr = get_addr32(*argv);
508 count++;
509 } else if (strcmp(*argv, "dev") == 0) {
510 NEXT_ARG();
511 strncpy(medium, *argv, IFNAMSIZ-1);
512 devname++;
513 } else {
514 fprintf(stderr,"%s: Invalid PRL parameter.\n", *argv);
515 exit(-1);
516 }
517 if (count > 1) {
518 fprintf(stderr,"One PRL entry at a time.\n");
519 exit(-1);
520 }
521 argc--; argv++;
522 }
523 if (devname == 0) {
524 fprintf(stderr, "Must specify dev.\n");
525 exit(-1);
526 }
527
528 return tnl_prl_ioctl(cmd, medium, &p);
529}
530
aba5acdf
SH
531int do_iptunnel(int argc, char **argv)
532{
d9bd1bd9
MN
533 switch (preferred_family) {
534 case AF_UNSPEC:
535 preferred_family = AF_INET;
536 break;
537 case AF_INET:
538 break;
288384f2
MN
539 /*
540 * This is silly enough but we have no easy way to make it
541 * protocol-independent because of unarranged structure between
542 * IPv4 and IPv6.
543 */
544 case AF_INET6:
545 return do_ip6tunnel(argc, argv);
d9bd1bd9
MN
546 default:
547 fprintf(stderr, "Unsupported family:%d\n", preferred_family);
548 exit(-1);
549 }
550
aba5acdf
SH
551 if (argc > 0) {
552 if (matches(*argv, "add") == 0)
553 return do_add(SIOCADDTUNNEL, argc-1, argv+1);
554 if (matches(*argv, "change") == 0)
555 return do_add(SIOCCHGTUNNEL, argc-1, argv+1);
556 if (matches(*argv, "del") == 0)
557 return do_del(argc-1, argv+1);
558 if (matches(*argv, "show") == 0 ||
559 matches(*argv, "lst") == 0 ||
560 matches(*argv, "list") == 0)
561 return do_show(argc-1, argv+1);
a07e9912
SH
562 if (matches(*argv, "prl") == 0)
563 return do_prl(argc-1, argv+1);
aba5acdf
SH
564 if (matches(*argv, "help") == 0)
565 usage();
566 } else
567 return do_show(0, NULL);
568
569 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv);
570 exit(-1);
571}