blob: e3161d81bededa6706b23d2e00ef4bafb3c6370b [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 *
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090017#include <sys/types.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000018#include <sys/socket.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000019#include <arpa/inet.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000020#include <sys/ioctl.h>
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080021#include <net/if.h>
22#include <net/if_arp.h>
osdl.org!shemminger7272ddc2004-06-02 20:22:08 +000023#include <linux/ip.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024#include <linux/if_tunnel.h>
25
26#include "rt_names.h"
27#include "utils.h"
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090028#include "ip_common.h"
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090029#include "tunnel.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030
31static void usage(void) __attribute__((noreturn));
32
33static void usage(void)
34{
Alexandre Cassenb88215c2009-12-16 02:38:29 +000035 fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n");
Saurabh Mohaneec47602012-08-01 16:11:25 -070036 fprintf(stderr, " [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000037 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
Sascha Hlusiaka07e9912009-05-04 01:44:47 +020038 fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n");
Alexandre Cassenb88215c2009-12-16 02:38:29 +000039 fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
41 fprintf(stderr, "\n");
42 fprintf(stderr, "Where: NAME := STRING\n");
43 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
David Warde59fd3d2013-01-27 13:04:59 +000044 fprintf(stderr, " TOS := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
46 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
47 exit(-1);
48}
49
Phil Sutter25205982015-11-13 18:09:01 +010050static void set_tunnel_proto(struct ip_tunnel_parm *p, int proto)
51{
52 if (p->iph.protocol && p->iph.protocol != proto) {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -080053 fprintf(stderr,
54 "You managed to ask for more than one tunnel mode.\n");
Phil Sutter25205982015-11-13 18:09:01 +010055 exit(-1);
56 }
57 p->iph.protocol = proto;
58}
59
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000060static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
61{
62 int count = 0;
63 char medium[IFNAMSIZ];
Templin, Fred L0bd17922007-11-26 09:46:06 -080064 int isatap = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000065
66 memset(p, 0, sizeof(*p));
67 memset(&medium, 0, sizeof(medium));
68
69 p->iph.version = 4;
70 p->iph.ihl = 5;
71#ifndef IP_DF
72#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
73#endif
74 p->iph.frag_off = htons(IP_DF);
75
76 while (argc > 0) {
77 if (strcmp(*argv, "mode") == 0) {
78 NEXT_ARG();
79 if (strcmp(*argv, "ipip") == 0 ||
80 strcmp(*argv, "ip/ip") == 0) {
Phil Sutter25205982015-11-13 18:09:01 +010081 set_tunnel_proto(p, IPPROTO_IPIP);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082 } else if (strcmp(*argv, "gre") == 0 ||
83 strcmp(*argv, "gre/ip") == 0) {
Phil Sutter25205982015-11-13 18:09:01 +010084 set_tunnel_proto(p, IPPROTO_GRE);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000085 } else if (strcmp(*argv, "sit") == 0 ||
86 strcmp(*argv, "ipv6/ip") == 0) {
Phil Sutter25205982015-11-13 18:09:01 +010087 set_tunnel_proto(p, IPPROTO_IPV6);
Templin, Fred L0bd17922007-11-26 09:46:06 -080088 } else if (strcmp(*argv, "isatap") == 0) {
Phil Sutter25205982015-11-13 18:09:01 +010089 set_tunnel_proto(p, IPPROTO_IPV6);
Templin, Fred L0bd17922007-11-26 09:46:06 -080090 isatap++;
Saurabh Mohaneec47602012-08-01 16:11:25 -070091 } else if (strcmp(*argv, "vti") == 0) {
Phil Sutter25205982015-11-13 18:09:01 +010092 set_tunnel_proto(p, IPPROTO_IPIP);
Saurabh Mohaneec47602012-08-01 16:11:25 -070093 p->i_flags |= VTI_ISVTI;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000094 } else {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -080095 fprintf(stderr,
96 "Unknown tunnel mode \"%s\"\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000097 exit(-1);
98 }
99 } else if (strcmp(*argv, "key") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 NEXT_ARG();
101 p->i_flags |= GRE_KEY;
102 p->o_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100103 p->i_key = p->o_key = tnl_parse_key("key", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104 } else if (strcmp(*argv, "ikey") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000105 NEXT_ARG();
106 p->i_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100107 p->i_key = tnl_parse_key("ikey", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 } else if (strcmp(*argv, "okey") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000109 NEXT_ARG();
110 p->o_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100111 p->o_key = tnl_parse_key("okey", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000112 } else if (strcmp(*argv, "seq") == 0) {
113 p->i_flags |= GRE_SEQ;
114 p->o_flags |= GRE_SEQ;
115 } else if (strcmp(*argv, "iseq") == 0) {
116 p->i_flags |= GRE_SEQ;
117 } else if (strcmp(*argv, "oseq") == 0) {
118 p->o_flags |= GRE_SEQ;
119 } else if (strcmp(*argv, "csum") == 0) {
120 p->i_flags |= GRE_CSUM;
121 p->o_flags |= GRE_CSUM;
122 } else if (strcmp(*argv, "icsum") == 0) {
123 p->i_flags |= GRE_CSUM;
124 } else if (strcmp(*argv, "ocsum") == 0) {
125 p->o_flags |= GRE_CSUM;
126 } else if (strcmp(*argv, "nopmtudisc") == 0) {
127 p->iph.frag_off = 0;
128 } else if (strcmp(*argv, "pmtudisc") == 0) {
129 p->iph.frag_off = htons(IP_DF);
130 } else if (strcmp(*argv, "remote") == 0) {
131 NEXT_ARG();
132 if (strcmp(*argv, "any"))
133 p->iph.daddr = get_addr32(*argv);
Thadeu Lima de Souza Cascardo4e4b7832015-06-04 09:01:18 -0300134 else
135 p->iph.daddr = htonl(INADDR_ANY);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 } else if (strcmp(*argv, "local") == 0) {
137 NEXT_ARG();
138 if (strcmp(*argv, "any"))
139 p->iph.saddr = get_addr32(*argv);
Thadeu Lima de Souza Cascardo4e4b7832015-06-04 09:01:18 -0300140 else
141 p->iph.saddr = htonl(INADDR_ANY);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142 } else if (strcmp(*argv, "dev") == 0) {
143 NEXT_ARG();
Phil Sutter04ce8d32015-11-13 18:09:05 +0100144 strncpy(medium, *argv, IFNAMSIZ - 1);
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400145 } else if (strcmp(*argv, "ttl") == 0 ||
Phil Sutter0dd4d2b2015-11-13 18:09:02 +0100146 strcmp(*argv, "hoplimit") == 0 ||
147 strcmp(*argv, "hlim") == 0) {
148 __u8 uval;
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800149
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150 NEXT_ARG();
151 if (strcmp(*argv, "inherit") != 0) {
Phil Sutter0dd4d2b2015-11-13 18:09:02 +0100152 if (get_u8(&uval, *argv, 0))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153 invarg("invalid TTL\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000154 p->iph.ttl = uval;
155 }
156 } else if (strcmp(*argv, "tos") == 0 ||
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400157 strcmp(*argv, "tclass") == 0 ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158 matches(*argv, "dsfield") == 0) {
David Warde59fd3d2013-01-27 13:04:59 +0000159 char *dsfield;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160 __u32 uval;
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800161
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000162 NEXT_ARG();
David Warde59fd3d2013-01-27 13:04:59 +0000163 dsfield = *argv;
164 strsep(&dsfield, "/");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000165 if (strcmp(*argv, "inherit") != 0) {
David Warde59fd3d2013-01-27 13:04:59 +0000166 dsfield = *argv;
167 p->iph.tos = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168 } else
169 p->iph.tos = 1;
David Warde59fd3d2013-01-27 13:04:59 +0000170 if (dsfield) {
171 if (rtnl_dsfield_a2n(&uval, dsfield))
172 invarg("bad TOS value", *argv);
173 p->iph.tos |= uval;
174 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175 } else {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800176 if (strcmp(*argv, "name") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000177 NEXT_ARG();
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800178 else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000179 usage();
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800180
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000181 if (p->name[0])
182 duparg2("name", *argv);
Phil Sutterf53ecee2015-11-13 18:09:04 +0100183 strncpy(p->name, *argv, IFNAMSIZ - 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000184 if (cmd == SIOCCHGTUNNEL && count == 0) {
185 struct ip_tunnel_parm old_p;
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800186
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000187 memset(&old_p, 0, sizeof(old_p));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900188 if (tnl_get_ioctl(*argv, &old_p))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000189 return -1;
190 *p = old_p;
191 }
192 }
193 count++;
194 argc--; argv++;
195 }
196
197
198 if (p->iph.protocol == 0) {
199 if (memcmp(p->name, "gre", 3) == 0)
200 p->iph.protocol = IPPROTO_GRE;
201 else if (memcmp(p->name, "ipip", 4) == 0)
202 p->iph.protocol = IPPROTO_IPIP;
203 else if (memcmp(p->name, "sit", 3) == 0)
204 p->iph.protocol = IPPROTO_IPV6;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800205 else if (memcmp(p->name, "isatap", 6) == 0) {
206 p->iph.protocol = IPPROTO_IPV6;
207 isatap++;
Saurabh Mohaneec47602012-08-01 16:11:25 -0700208 } else if (memcmp(p->name, "vti", 3) == 0) {
209 p->iph.protocol = IPPROTO_IPIP;
210 p->i_flags |= VTI_ISVTI;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800211 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000212 }
213
Hangbin Liu1c28bd52013-12-06 00:19:01 +0800214 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
215 if (!(p->i_flags & VTI_ISVTI) &&
216 (p->iph.protocol != IPPROTO_GRE)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000217 fprintf(stderr, "Keys are not allowed with ipip and sit tunnels\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218 return -1;
219 }
220 }
221
222 if (medium[0]) {
Phil Sutter4b3cb962015-11-13 18:08:57 +0100223 p->link = ll_name_to_index(medium);
Cong Wang0cb6bb52014-06-03 16:06:45 -0700224 if (p->link == 0) {
Phil Sutter6ddb1e82015-11-13 18:08:56 +0100225 fprintf(stderr, "Cannot find device \"%s\"\n", medium);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226 return -1;
Cong Wang0cb6bb52014-06-03 16:06:45 -0700227 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228 }
229
230 if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
231 p->i_key = p->iph.daddr;
232 p->i_flags |= GRE_KEY;
233 }
234 if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
235 p->o_key = p->iph.daddr;
236 p->o_flags |= GRE_KEY;
237 }
238 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000239 fprintf(stderr, "A broadcast tunnel requires a source address\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000240 return -1;
241 }
Sascha Hlusiakeeef12c2009-03-27 11:14:00 -0700242 if (isatap)
Templin, Fred L0bd17922007-11-26 09:46:06 -0800243 p->i_flags |= SIT_ISATAP;
Templin, Fred L0bd17922007-11-26 09:46:06 -0800244
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000245 return 0;
246}
247
Phil Sutterc9578212015-11-13 18:09:03 +0100248static const char *tnl_defname(const struct ip_tunnel_parm *p)
249{
250 switch (p->iph.protocol) {
251 case IPPROTO_IPIP:
252 if (p->i_flags & VTI_ISVTI)
253 return "ip_vti0";
254 else
255 return "tunl0";
256 case IPPROTO_GRE:
257 return "gre0";
258 case IPPROTO_IPV6:
259 return "sit0";
260 }
261 return NULL;
262}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263
264static int do_add(int cmd, int argc, char **argv)
265{
266 struct ip_tunnel_parm p;
Phil Sutterc9578212015-11-13 18:09:03 +0100267 const char *basedev;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268
269 if (parse_args(argc, argv, cmd, &p) < 0)
270 return -1;
271
272 if (p.iph.ttl && p.iph.frag_off == 0) {
Richard Godbee30d07e92013-08-25 22:40:18 -0400273 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000274 return -1;
275 }
276
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800277 basedev = tnl_defname(&p);
278 if (!basedev) {
279 fprintf(stderr,
280 "cannot determine tunnel mode (ipip, gre, vti or sit)\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281 return -1;
282 }
Phil Sutterc9578212015-11-13 18:09:03 +0100283
284 return tnl_add_ioctl(cmd, basedev, p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285}
286
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900287static int do_del(int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000288{
289 struct ip_tunnel_parm p;
290
291 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
292 return -1;
293
Phil Sutterc9578212015-11-13 18:09:03 +0100294 return tnl_del_ioctl(tnl_defname(&p) ? : p.name, p.name, &p);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000295}
296
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900297static void print_tunnel(struct ip_tunnel_parm *p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298{
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000299 struct ip_tunnel_6rd ip6rd;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 char s1[1024];
301 char s2[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000302
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000303 memset(&ip6rd, 0, sizeof(ip6rd));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304
305 /* Do not use format_host() for local addr,
306 * symbolic name will not be useful.
307 */
Phil Sutter8de592d2015-11-13 18:08:54 +0100308 printf("%s: %s/ip remote %s local %s",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309 p->name,
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900310 tnl_strproto(p->iph.protocol),
Phil Suttera418e452016-03-22 19:35:15 +0100311 p->iph.daddr ? format_host_r(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
Phil Sutter2e96d2c2016-03-22 19:35:16 +0100312 p->iph.saddr ? rt_addr_n2a_r(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000313
Amerigo Wang86c00fa2013-07-02 17:39:28 +0800314 if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200315 struct ip_tunnel_prl prl[16];
316 int i;
Stephen Hemminger06125192014-02-17 10:55:31 -0800317
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200318 memset(prl, 0, sizeof(prl));
319 prl[0].datalen = sizeof(prl) - sizeof(prl[0]);
320 prl[0].addr = htonl(INADDR_ANY);
Stephen Hemminger06125192014-02-17 10:55:31 -0800321
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200322 if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl))
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800323 for (i = 1; i < ARRAY_SIZE(prl); i++) {
324 if (prl[i].addr != htonl(INADDR_ANY)) {
325 printf(" %s %s ",
326 (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
Phil Suttera418e452016-03-22 19:35:15 +0100327 format_host(AF_INET, 4, &prl[i].addr));
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800328 }
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200329 }
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200330 }
331
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000332 if (p->link) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800333 const char *n = ll_index_to_name(p->link);
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800334
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000335 if (n)
Phil Sutter8de592d2015-11-13 18:08:54 +0100336 printf(" dev %s", n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000337 }
338
339 if (p->iph.ttl)
Phil Sutter8de592d2015-11-13 18:08:54 +0100340 printf(" ttl %d", p->iph.ttl);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341 else
Phil Sutter8de592d2015-11-13 18:08:54 +0100342 printf(" ttl inherit");
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800343
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000344 if (p->iph.tos) {
345 SPRINT_BUF(b1);
346 printf(" tos");
Phil Sutter04ce8d32015-11-13 18:09:05 +0100347 if (p->iph.tos & 1)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000348 printf(" inherit");
Phil Sutter04ce8d32015-11-13 18:09:05 +0100349 if (p->iph.tos & ~1)
350 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
351 rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000352 }
353
Phil Sutter04ce8d32015-11-13 18:09:05 +0100354 if (!(p->iph.frag_off & htons(IP_DF)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000355 printf(" nopmtudisc");
356
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200357 if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) {
Phil Sutter8de592d2015-11-13 18:08:54 +0100358 printf(" 6rd-prefix %s/%u",
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000359 inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)),
360 ip6rd.prefixlen);
361 if (ip6rd.relay_prefix) {
Phil Sutter8de592d2015-11-13 18:08:54 +0100362 printf(" 6rd-relay_prefix %s/%u",
Phil Suttera418e452016-03-22 19:35:15 +0100363 format_host(AF_INET, 4, &ip6rd.relay_prefix),
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000364 ip6rd.relay_prefixlen);
365 }
366 }
367
Phil Sutter04ce8d32015-11-13 18:09:05 +0100368 if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
Timo Teräs6f4f7c42010-11-23 22:18:57 +0000369 printf(" key %u", ntohl(p->i_key));
Phil Sutter04ce8d32015-11-13 18:09:05 +0100370 else if ((p->i_flags | p->o_flags) & GRE_KEY) {
371 if (p->i_flags & GRE_KEY)
Phil Sutter8de592d2015-11-13 18:08:54 +0100372 printf(" ikey %u", ntohl(p->i_key));
Phil Sutter04ce8d32015-11-13 18:09:05 +0100373 if (p->o_flags & GRE_KEY)
Phil Sutter8de592d2015-11-13 18:08:54 +0100374 printf(" okey %u", ntohl(p->o_key));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000375 }
376
Phil Sutter04ce8d32015-11-13 18:09:05 +0100377 if (p->i_flags & GRE_SEQ)
Dmitry Popov4cb8d032014-10-04 19:00:25 +0400378 printf("%s Drop packets out of sequence.", _SL_);
Phil Sutter04ce8d32015-11-13 18:09:05 +0100379 if (p->i_flags & GRE_CSUM)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000380 printf("%s Checksum in received packet is required.", _SL_);
Phil Sutter04ce8d32015-11-13 18:09:05 +0100381 if (p->o_flags & GRE_SEQ)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000382 printf("%s Sequence packets on output.", _SL_);
Phil Sutter04ce8d32015-11-13 18:09:05 +0100383 if (p->o_flags & GRE_CSUM)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000384 printf("%s Checksum output packets.", _SL_);
385}
386
387static int do_tunnels_list(struct ip_tunnel_parm *p)
388{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000389 char buf[512];
Phil Sutterc4527d72015-11-13 18:08:58 +0100390 int err = -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000391 FILE *fp = fopen("/proc/net/dev", "r");
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800392
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000393 if (fp == NULL) {
394 perror("fopen");
395 return -1;
396 }
397
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800398 /* skip header lines */
399 if (!fgets(buf, sizeof(buf), fp) ||
400 !fgets(buf, sizeof(buf), fp)) {
401 fprintf(stderr, "/proc/net/dev read error\n");
Phil Sutterc4527d72015-11-13 18:08:58 +0100402 goto end;
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800403 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000404
405 while (fgets(buf, sizeof(buf), fp) != NULL) {
Phil Sutterc4527d72015-11-13 18:08:58 +0100406 char name[IFNAMSIZ];
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800407 int index, type;
Phil Sutterc4527d72015-11-13 18:08:58 +0100408 struct ip_tunnel_parm p1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000409 char *ptr;
Phil Sutterc4527d72015-11-13 18:08:58 +0100410
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000411 buf[sizeof(buf) - 1] = 0;
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800412 ptr = strchr(buf, ':');
413 if (ptr == NULL ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000414 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000415 fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
Phil Sutterc4527d72015-11-13 18:08:58 +0100416 goto end;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000418 if (p->name[0] && strcmp(p->name, name))
419 continue;
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800420 index = ll_name_to_index(name);
421 if (index == 0)
422 continue;
423 type = ll_index_to_type(index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000424 if (type == -1) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000425 fprintf(stderr, "Failed to get type of \"%s\"\n", name);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000426 continue;
427 }
428 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
429 continue;
430 memset(&p1, 0, sizeof(p1));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900431 if (tnl_get_ioctl(name, &p1))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000432 continue;
433 if ((p->link && p1.link != p->link) ||
434 (p->name[0] && strcmp(p1.name, p->name)) ||
435 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
436 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
437 (p->i_key && p1.i_key != p->i_key))
438 continue;
439 print_tunnel(&p1);
Phil Sutter7d6aadc2015-12-18 11:58:06 +0100440 if (show_stats)
441 tnl_print_stats(ptr);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000442 printf("\n");
443 }
Phil Sutterc4527d72015-11-13 18:08:58 +0100444 err = 0;
445 end:
Thomas Jarosch297452a2011-10-03 05:24:32 +0000446 fclose(fp);
Phil Sutterc4527d72015-11-13 18:08:58 +0100447 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000448}
449
450static int do_show(int argc, char **argv)
451{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000452 struct ip_tunnel_parm p;
Phil Sutterc9578212015-11-13 18:09:03 +0100453 const char *basedev;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000454
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800455 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000456 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
457 return -1;
458
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800459 basedev = tnl_defname(&p);
460 if (!basedev)
Phil Sutterc9578212015-11-13 18:09:03 +0100461 return do_tunnels_list(&p);
462
463 if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000464 return -1;
465
466 print_tunnel(&p);
467 printf("\n");
468 return 0;
469}
470
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200471static int do_prl(int argc, char **argv)
472{
473 struct ip_tunnel_prl p;
474 int count = 0;
475 int devname = 0;
476 int cmd = 0;
477 char medium[IFNAMSIZ];
478
479 memset(&p, 0, sizeof(p));
480 memset(&medium, 0, sizeof(medium));
481
482 while (argc > 0) {
483 if (strcmp(*argv, "prl-default") == 0) {
484 NEXT_ARG();
485 cmd = SIOCADDPRL;
486 p.addr = get_addr32(*argv);
487 p.flags |= PRL_DEFAULT;
488 count++;
489 } else if (strcmp(*argv, "prl-nodefault") == 0) {
490 NEXT_ARG();
491 cmd = SIOCADDPRL;
492 p.addr = get_addr32(*argv);
493 count++;
494 } else if (strcmp(*argv, "prl-delete") == 0) {
495 NEXT_ARG();
496 cmd = SIOCDELPRL;
497 p.addr = get_addr32(*argv);
498 count++;
499 } else if (strcmp(*argv, "dev") == 0) {
500 NEXT_ARG();
501 strncpy(medium, *argv, IFNAMSIZ-1);
502 devname++;
503 } else {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800504 fprintf(stderr,
505 "Invalid PRL parameter \"%s\"\n", *argv);
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200506 exit(-1);
507 }
508 if (count > 1) {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800509 fprintf(stderr,
510 "One PRL entry at a time\n");
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200511 exit(-1);
512 }
513 argc--; argv++;
514 }
515 if (devname == 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000516 fprintf(stderr, "Must specify device\n");
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200517 exit(-1);
518 }
519
520 return tnl_prl_ioctl(cmd, medium, &p);
521}
522
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000523static int do_6rd(int argc, char **argv)
524{
525 struct ip_tunnel_6rd ip6rd;
526 int devname = 0;
527 int cmd = 0;
528 char medium[IFNAMSIZ];
529 inet_prefix prefix;
530
531 memset(&ip6rd, 0, sizeof(ip6rd));
532 memset(&medium, 0, sizeof(medium));
533
534 while (argc > 0) {
535 if (strcmp(*argv, "6rd-prefix") == 0) {
536 NEXT_ARG();
537 if (get_prefix(&prefix, *argv, AF_INET6))
538 invarg("invalid 6rd_prefix\n", *argv);
539 cmd = SIOCADD6RD;
540 memcpy(&ip6rd.prefix, prefix.data, 16);
541 ip6rd.prefixlen = prefix.bitlen;
542 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
543 NEXT_ARG();
544 if (get_prefix(&prefix, *argv, AF_INET))
545 invarg("invalid 6rd-relay_prefix\n", *argv);
546 cmd = SIOCADD6RD;
547 memcpy(&ip6rd.relay_prefix, prefix.data, 4);
548 ip6rd.relay_prefixlen = prefix.bitlen;
549 } else if (strcmp(*argv, "6rd-reset") == 0) {
550 cmd = SIOCDEL6RD;
551 } else if (strcmp(*argv, "dev") == 0) {
552 NEXT_ARG();
553 strncpy(medium, *argv, IFNAMSIZ-1);
554 devname++;
555 } else {
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800556 fprintf(stderr,
557 "Invalid 6RD parameter \"%s\"\n", *argv);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000558 exit(-1);
559 }
560 argc--; argv++;
561 }
562 if (devname == 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000563 fprintf(stderr, "Must specify device\n");
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000564 exit(-1);
565 }
566
567 return tnl_6rd_ioctl(cmd, medium, &ip6rd);
568}
569
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800570static int tunnel_mode_is_ipv6(char *tunnel_mode)
571{
572 static const char * const ipv6_modes[] = {
Konstantin Shemyakcc9c1df2015-11-26 18:22:05 +0200573 "ipv6/ipv6", "ip6ip6",
574 "vti6",
575 "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
576 "ip6gre", "gre/ipv6",
577 "any/ipv6", "any"
578 };
579 int i;
580
Stephen Hemmingera96a5d92015-11-29 12:05:39 -0800581 for (i = 0; i < ARRAY_SIZE(ipv6_modes); i++) {
Konstantin Shemyakcc9c1df2015-11-26 18:22:05 +0200582 if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
583 return 1;
584 }
585 return 0;
586}
587
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000588int do_iptunnel(int argc, char **argv)
589{
Konstantin Shemyakcc9c1df2015-11-26 18:22:05 +0200590 int i;
591
592 for (i = 0; i < argc - 1; i++) {
593 if (strcmp(argv[i], "mode") == 0) {
594 if (tunnel_mode_is_ipv6(argv[i + 1]))
595 preferred_family = AF_INET6;
596 break;
597 }
598 }
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900599 switch (preferred_family) {
600 case AF_UNSPEC:
601 preferred_family = AF_INET;
602 break;
603 case AF_INET:
604 break;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900605 /*
606 * This is silly enough but we have no easy way to make it
607 * protocol-independent because of unarranged structure between
608 * IPv4 and IPv6.
609 */
610 case AF_INET6:
611 return do_ip6tunnel(argc, argv);
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900612 default:
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000613 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900614 exit(-1);
615 }
616
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000617 if (argc > 0) {
618 if (matches(*argv, "add") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100619 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000620 if (matches(*argv, "change") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100621 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
Andreas Henriksson6e304612012-05-19 16:08:21 +0200622 if (matches(*argv, "delete") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100623 return do_del(argc - 1, argv + 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000624 if (matches(*argv, "show") == 0 ||
625 matches(*argv, "lst") == 0 ||
626 matches(*argv, "list") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100627 return do_show(argc - 1, argv + 1);
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200628 if (matches(*argv, "prl") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100629 return do_prl(argc - 1, argv + 1);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000630 if (matches(*argv, "6rd") == 0)
Phil Sutter04ce8d32015-11-13 18:09:05 +0100631 return do_6rd(argc - 1, argv + 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000632 if (matches(*argv, "help") == 0)
633 usage();
634 } else
635 return do_show(0, NULL);
636
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000637 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\"\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000638 exit(-1);
639}