blob: d8957f0edb789a10b7299419af14ce81765c8140 [file] [log] [blame]
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +09001/*
2 * Copyright (C)2006 USAGI/WIDE Project
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003 *
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +09004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Stephen Hemmingerae665a52006-12-05 10:10:22 -08008 *
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +09009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080013 *
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090014 * You should have received a copy of the GNU General Public License
Stephen Hemminger4d98ab02013-12-06 15:05:07 -080015 * along with this program; if not, see <http://www.gnu.org/licenses>.
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090016 */
17/*
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090018 * Author:
19 * Masahide NAKAMURA @USAGI
20 */
Stephen Hemmingerae665a52006-12-05 10:10:22 -080021
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090022#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090025#include <unistd.h>
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090026#include <sys/types.h>
27#include <sys/socket.h>
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090028#include <arpa/inet.h>
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090029#include <sys/ioctl.h>
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090030#include <linux/ip.h>
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090031#include <linux/if.h>
32#include <linux/if_arp.h>
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090033#include <linux/if_tunnel.h>
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090034#include <linux/ip6_tunnel.h>
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090035
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090036#include "utils.h"
37#include "tunnel.h"
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080038#include "ip_common.h"
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090039
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090040#define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
41#define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090042
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090043#define DEFAULT_TNL_HOP_LIMIT (64)
44
45static void usage(void) __attribute__((noreturn));
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090046
47static void usage(void)
48{
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090049 fprintf(stderr, "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n");
Steffen Klassertf36d1142014-09-26 09:10:56 +020050 fprintf(stderr, " [ mode { ip6ip6 | ipip6 | ip6gre | vti6 | any } ]\n");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090051 fprintf(stderr, " [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n");
52 fprintf(stderr, " [ encaplimit ELIM ]\n");
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040053 fprintf(stderr ," [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090054 fprintf(stderr, " [ dscp inherit ]\n");
xeb@mail.ruaf895762013-09-28 11:32:51 +040055 fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090056 fprintf(stderr, "\n");
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040057 fprintf(stderr, "Where: NAME := STRING\n");
58 fprintf(stderr, " ADDR := IPV6_ADDRESS\n");
59 fprintf(stderr, " ELIM := { none | 0..255 }(default=%d)\n",
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090060 IPV6_DEFAULT_TNL_ENCAP_LIMIT);
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040061 fprintf(stderr, " TTL := 0..255 (default=%d)\n",
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090062 DEFAULT_TNL_HOP_LIMIT);
Nicolas Dichteld0c84202012-11-14 16:29:24 +010063 fprintf(stderr, " TCLASS := { 0x0..0xff | inherit }\n");
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040064 fprintf(stderr, " FLOWLABEL := { 0x0..0xfffff | inherit }\n");
xeb@mail.ruaf895762013-09-28 11:32:51 +040065 fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n");
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090066 exit(-1);
67}
68
xeb@mail.ruaf895762013-09-28 11:32:51 +040069static void print_tunnel(struct ip6_tnl_parm2 *p)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090070{
Phil Sutter9af72f82015-11-13 18:08:59 +010071 char s1[1024];
72 char s2[1024];
Stephen Hemmingerae665a52006-12-05 10:10:22 -080073
Phil Sutter9af72f82015-11-13 18:08:59 +010074 /* Do not use format_host() for local addr,
75 * symbolic name will not be useful.
76 */
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090077 printf("%s: %s/ipv6 remote %s local %s",
Phil Sutter9af72f82015-11-13 18:08:59 +010078 p->name,
79 tnl_strproto(p->proto),
80 format_host(AF_INET6, 16, &p->raddr, s1, sizeof(s1)),
81 rt_addr_n2a(AF_INET6, 16, &p->laddr, s2, sizeof(s2)));
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090082 if (p->link) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080083 const char *n = ll_index_to_name(p->link);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090084 if (n)
85 printf(" dev %s", n);
86 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090087
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090088 if (p->flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
89 printf(" encaplimit none");
90 else
91 printf(" encaplimit %u", p->encap_limit);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090092
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090093 printf(" hoplimit %u", p->hop_limit);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +090094
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090095 if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS)
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040096 printf(" tclass inherit");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +090097 else {
98 __u32 val = ntohl(p->flowinfo & IP6_FLOWINFO_TCLASS);
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -040099 printf(" tclass 0x%02x", (__u8)(val >> 20));
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900100 }
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900101
102 if (p->flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400103 printf(" flowlabel inherit");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900104 else
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400105 printf(" flowlabel 0x%05x", ntohl(p->flowinfo & IP6_FLOWINFO_FLOWLABEL));
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900106
107 printf(" (flowinfo 0x%08x)", ntohl(p->flowinfo));
108
109 if (p->flags & IP6_TNL_F_RCV_DSCP_COPY)
110 printf(" dscp inherit");
xeb@mail.ruaf895762013-09-28 11:32:51 +0400111
112 if (p->proto == IPPROTO_GRE) {
113 if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
114 printf(" key %u", ntohl(p->i_key));
115 else if ((p->i_flags|p->o_flags)&GRE_KEY) {
116 if (p->i_flags&GRE_KEY)
Phil Sutter8de592d2015-11-13 18:08:54 +0100117 printf(" ikey %u", ntohl(p->i_key));
xeb@mail.ruaf895762013-09-28 11:32:51 +0400118 if (p->o_flags&GRE_KEY)
Phil Sutter8de592d2015-11-13 18:08:54 +0100119 printf(" okey %u", ntohl(p->o_key));
xeb@mail.ruaf895762013-09-28 11:32:51 +0400120 }
121
122 if (p->i_flags&GRE_SEQ)
Dmitry Popov4cb8d032014-10-04 19:00:25 +0400123 printf("%s Drop packets out of sequence.", _SL_);
xeb@mail.ruaf895762013-09-28 11:32:51 +0400124 if (p->i_flags&GRE_CSUM)
125 printf("%s Checksum in received packet is required.", _SL_);
126 if (p->o_flags&GRE_SEQ)
127 printf("%s Sequence packets on output.", _SL_);
128 if (p->o_flags&GRE_CSUM)
129 printf("%s Checksum output packets.", _SL_);
130 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900131}
132
xeb@mail.ruaf895762013-09-28 11:32:51 +0400133static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900134{
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000135 int count = 0;
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900136 char medium[IFNAMSIZ];
137
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900138 memset(medium, 0, sizeof(medium));
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900139
140 while (argc > 0) {
YOSHIFUJI Hideaki / 吉藤英明0b959b02007-10-12 16:51:22 +0900141 if (strcmp(*argv, "mode") == 0) {
142 NEXT_ARG();
143 if (strcmp(*argv, "ipv6/ipv6") == 0 ||
144 strcmp(*argv, "ip6ip6") == 0)
145 p->proto = IPPROTO_IPV6;
Steffen Klassertf36d1142014-09-26 09:10:56 +0200146 else if (strcmp(*argv, "vti6") == 0) {
147 p->proto = IPPROTO_IPV6;
148 p->i_flags |= VTI_ISVTI;
149 } else if (strcmp(*argv, "ip/ipv6") == 0 ||
YOSHIFUJI Hideaki / 吉藤英明0b959b02007-10-12 16:51:22 +0900150 strcmp(*argv, "ipv4/ipv6") == 0 ||
151 strcmp(*argv, "ipip6") == 0 ||
152 strcmp(*argv, "ip4ip6") == 0)
153 p->proto = IPPROTO_IPIP;
xeb@mail.ruaf895762013-09-28 11:32:51 +0400154 else if (strcmp(*argv, "ip6gre") == 0 ||
155 strcmp(*argv, "gre/ipv6") == 0)
156 p->proto = IPPROTO_GRE;
YOSHIFUJI Hideaki / 吉藤英明0b959b02007-10-12 16:51:22 +0900157 else if (strcmp(*argv, "any/ipv6") == 0 ||
158 strcmp(*argv, "any") == 0)
159 p->proto = 0;
160 else {
Stephen Hemminger4b726cb2014-05-09 12:36:46 -0700161 fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv);
162 exit(-1);
163 }
164 } else if (strcmp(*argv, "remote") == 0) {
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900165 inet_prefix raddr;
166 NEXT_ARG();
167 get_prefix(&raddr, *argv, preferred_family);
168 if (raddr.family == AF_UNSPEC)
169 invarg("\"remote\" address family is AF_UNSPEC", *argv);
170 memcpy(&p->raddr, &raddr.data, sizeof(p->raddr));
171 } else if (strcmp(*argv, "local") == 0) {
172 inet_prefix laddr;
173 NEXT_ARG();
174 get_prefix(&laddr, *argv, preferred_family);
175 if (laddr.family == AF_UNSPEC)
176 invarg("\"local\" address family is AF_UNSPEC", *argv);
177 memcpy(&p->laddr, &laddr.data, sizeof(p->laddr));
178 } else if (strcmp(*argv, "dev") == 0) {
179 NEXT_ARG();
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900180 strncpy(medium, *argv, IFNAMSIZ - 1);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900181 } else if (strcmp(*argv, "encaplimit") == 0) {
182 NEXT_ARG();
183 if (strcmp(*argv, "none") == 0) {
184 p->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT;
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900185 } else {
186 __u8 uval;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900187 if (get_u8(&uval, *argv, 0) < -1)
188 invarg("invalid ELIM", *argv);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900189 p->encap_limit = uval;
Nicolas Dichtel3f83dce2012-11-14 16:29:25 +0100190 p->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900191 }
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400192 } else if (strcmp(*argv, "hoplimit") == 0 ||
193 strcmp(*argv, "ttl") == 0 ||
194 strcmp(*argv, "hlim") == 0) {
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900195 __u8 uval;
196 NEXT_ARG();
197 if (get_u8(&uval, *argv, 0))
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400198 invarg("invalid TTL", *argv);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900199 p->hop_limit = uval;
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400200 } else if (strcmp(*argv, "tclass") == 0 ||
201 strcmp(*argv, "tc") == 0 ||
202 strcmp(*argv, "tos") == 0 ||
203 matches(*argv, "dsfield") == 0) {
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900204 __u8 uval;
205 NEXT_ARG();
Nicolas Dichteldf5574d2012-11-14 16:29:26 +0100206 p->flowinfo &= ~IP6_FLOWINFO_TCLASS;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900207 if (strcmp(*argv, "inherit") == 0)
208 p->flags |= IP6_TNL_F_USE_ORIG_TCLASS;
209 else {
210 if (get_u8(&uval, *argv, 16))
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400211 invarg("invalid TClass", *argv);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900212 p->flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
213 p->flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
214 }
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400215 } else if (strcmp(*argv, "flowlabel") == 0 ||
216 strcmp(*argv, "fl") == 0) {
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900217 __u32 uval;
218 NEXT_ARG();
Nicolas Dichteldf5574d2012-11-14 16:29:26 +0100219 p->flowinfo &= ~IP6_FLOWINFO_FLOWLABEL;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900220 if (strcmp(*argv, "inherit") == 0)
221 p->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
222 else {
223 if (get_u32(&uval, *argv, 16))
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400224 invarg("invalid Flowlabel", *argv);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900225 if (uval > 0xFFFFF)
YOSHIFUJI Hideaki / 吉藤英明eddde112008-03-13 11:17:54 -0400226 invarg("invalid Flowlabel", *argv);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900227 p->flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
228 p->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
229 }
230 } else if (strcmp(*argv, "dscp") == 0) {
231 NEXT_ARG();
232 if (strcmp(*argv, "inherit") != 0)
233 invarg("not inherit", *argv);
234 p->flags |= IP6_TNL_F_RCV_DSCP_COPY;
xeb@mail.ruaf895762013-09-28 11:32:51 +0400235 } else if (strcmp(*argv, "key") == 0) {
xeb@mail.ruaf895762013-09-28 11:32:51 +0400236 NEXT_ARG();
237 p->i_flags |= GRE_KEY;
238 p->o_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100239 p->i_key = p->o_key = tnl_parse_key("key", *argv);
xeb@mail.ruaf895762013-09-28 11:32:51 +0400240 } else if (strcmp(*argv, "ikey") == 0) {
xeb@mail.ruaf895762013-09-28 11:32:51 +0400241 NEXT_ARG();
242 p->i_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100243 p->i_key = tnl_parse_key("ikey", *argv);
xeb@mail.ruaf895762013-09-28 11:32:51 +0400244 } else if (strcmp(*argv, "okey") == 0) {
xeb@mail.ruaf895762013-09-28 11:32:51 +0400245 NEXT_ARG();
246 p->o_flags |= GRE_KEY;
Phil Suttera7ed1522015-11-13 18:08:55 +0100247 p->o_key = tnl_parse_key("okey", *argv);
xeb@mail.ruaf895762013-09-28 11:32:51 +0400248 } else if (strcmp(*argv, "seq") == 0) {
249 p->i_flags |= GRE_SEQ;
250 p->o_flags |= GRE_SEQ;
251 } else if (strcmp(*argv, "iseq") == 0) {
252 p->i_flags |= GRE_SEQ;
253 } else if (strcmp(*argv, "oseq") == 0) {
254 p->o_flags |= GRE_SEQ;
255 } else if (strcmp(*argv, "csum") == 0) {
256 p->i_flags |= GRE_CSUM;
257 p->o_flags |= GRE_CSUM;
258 } else if (strcmp(*argv, "icsum") == 0) {
259 p->i_flags |= GRE_CSUM;
260 } else if (strcmp(*argv, "ocsum") == 0) {
261 p->o_flags |= GRE_CSUM;
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900262 } else {
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900263 if (strcmp(*argv, "name") == 0) {
264 NEXT_ARG();
Phil Sutter7894ce72015-11-13 18:09:00 +0100265 } else if (matches(*argv, "help") == 0)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900266 usage();
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900267 if (p->name[0])
268 duparg2("name", *argv);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900269 strncpy(p->name, *argv, IFNAMSIZ - 1);
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000270 if (cmd == SIOCCHGTUNNEL && count == 0) {
xeb@mail.ruaf895762013-09-28 11:32:51 +0400271 struct ip6_tnl_parm2 old_p;
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000272 memset(&old_p, 0, sizeof(old_p));
273 if (tnl_get_ioctl(*argv, &old_p))
274 return -1;
275 *p = old_p;
276 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900277 }
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000278 count++;
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900279 argc--; argv++;
280 }
281 if (medium[0]) {
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800282 p->link = ll_name_to_index(medium);
Phil Sutter6ddb1e82015-11-13 18:08:56 +0100283 if (p->link == 0) {
284 fprintf(stderr, "Cannot find device \"%s\"\n", medium);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900285 return -1;
Phil Sutter6ddb1e82015-11-13 18:08:56 +0100286 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900287 }
288 return 0;
289}
290
xeb@mail.ruaf895762013-09-28 11:32:51 +0400291static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default)
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900292{
293 memset(p, 0, sizeof(*p));
294 p->proto = IPPROTO_IPV6;
295 if (apply_default) {
296 p->hop_limit = DEFAULT_TNL_HOP_LIMIT;
297 p->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
298 }
299}
300
301/*
302 * @p1: user specified parameter
303 * @p2: database entry
304 */
xeb@mail.ruaf895762013-09-28 11:32:51 +0400305static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 *p1,
306 const struct ip6_tnl_parm2 *p2)
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900307{
308 return ((!p1->link || p1->link == p2->link) &&
309 (!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
310 (memcmp(&p1->laddr, &in6addr_any, sizeof(p1->laddr)) == 0 ||
311 memcmp(&p1->laddr, &p2->laddr, sizeof(p1->laddr)) == 0) &&
312 (memcmp(&p1->raddr, &in6addr_any, sizeof(p1->raddr)) == 0 ||
313 memcmp(&p1->raddr, &p2->raddr, sizeof(p1->raddr)) == 0) &&
314 (!p1->proto || !p2->proto || p1->proto == p2->proto) &&
315 (!p1->encap_limit || p1->encap_limit == p2->encap_limit) &&
316 (!p1->hop_limit || p1->hop_limit == p2->hop_limit) &&
317 (!(p1->flowinfo & IP6_FLOWINFO_TCLASS) ||
318 !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_TCLASS)) &&
319 (!(p1->flowinfo & IP6_FLOWINFO_FLOWLABEL) ||
320 !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_FLOWLABEL)) &&
321 (!p1->flags || (p1->flags & p2->flags)));
322}
323
xeb@mail.ruaf895762013-09-28 11:32:51 +0400324static int do_tunnels_list(struct ip6_tnl_parm2 *p)
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900325{
326 char buf[512];
327 int err = -1;
328 FILE *fp = fopen("/proc/net/dev", "r");
329 if (fp == NULL) {
330 perror("fopen");
Phil Sutterc4527d72015-11-13 18:08:58 +0100331 return -1;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900332 }
333
334 /* skip two lines at the begenning of the file */
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800335 if (!fgets(buf, sizeof(buf), fp) ||
336 !fgets(buf, sizeof(buf), fp)) {
337 fprintf(stderr, "/proc/net/dev read error\n");
Phil Sutterc4527d72015-11-13 18:08:58 +0100338 goto end;
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800339 }
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900340
341 while (fgets(buf, sizeof(buf), fp) != NULL) {
342 char name[IFNAMSIZ];
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800343 int index, type;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900344 unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
345 rx_fifo, rx_frame,
346 tx_bytes, tx_packets, tx_errs, tx_drops,
347 tx_fifo, tx_colls, tx_carrier, rx_multi;
xeb@mail.ruaf895762013-09-28 11:32:51 +0400348 struct ip6_tnl_parm2 p1;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900349 char *ptr;
350
351 buf[sizeof(buf) - 1] = '\0';
352 if ((ptr = strchr(buf, ':')) == NULL ||
353 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000354 fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900355 goto end;
356 }
357 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
358 &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
359 &rx_fifo, &rx_frame, &rx_multi,
360 &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
361 &tx_fifo, &tx_colls, &tx_carrier) != 14)
362 continue;
363 if (p->name[0] && strcmp(p->name, name))
364 continue;
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800365 index = ll_name_to_index(name);
366 if (index == 0)
367 continue;
368 type = ll_index_to_type(index);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900369 if (type == -1) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000370 fprintf(stderr, "Failed to get type of \"%s\"\n", name);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900371 continue;
372 }
xeb@mail.ruaf895762013-09-28 11:32:51 +0400373 if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE)
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900374 continue;
375 memset(&p1, 0, sizeof(p1));
376 ip6_tnl_parm_init(&p1, 0);
xeb@mail.ruaf895762013-09-28 11:32:51 +0400377 if (type == ARPHRD_IP6GRE)
378 p1.proto = IPPROTO_GRE;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900379 strcpy(p1.name, name);
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800380 p1.link = ll_name_to_index(p1.name);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900381 if (p1.link == 0)
382 continue;
383 if (tnl_get_ioctl(p1.name, &p1))
384 continue;
385 if (!ip6_tnl_parm_match(p, &p1))
386 continue;
387 print_tunnel(&p1);
388 if (show_stats) {
389 printf("%s", _SL_);
390 printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
391 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
392 rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
393 printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
394 printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
395 tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
396 }
397 printf("\n");
398 }
399 err = 0;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900400 end:
Phil Sutterc4527d72015-11-13 18:08:58 +0100401 fclose(fp);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900402 return err;
403}
404
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900405static int do_show(int argc, char **argv)
406{
xeb@mail.ruaf895762013-09-28 11:32:51 +0400407 struct ip6_tnl_parm2 p;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900408
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800409 ll_init_map(&rth);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900410 ip6_tnl_parm_init(&p, 0);
Srivats Pc3651bf2009-03-27 11:17:26 -0700411 p.proto = 0; /* default to any */
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900412
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000413 if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900414 return -1;
415
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900416 if (!p.name[0] || show_stats)
417 do_tunnels_list(&p);
418 else {
419 if (tnl_get_ioctl(p.name, &p))
420 return -1;
421 print_tunnel(&p);
422 printf("\n");
423 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900424
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900425 return 0;
426}
427
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900428static int do_add(int cmd, int argc, char **argv)
429{
xeb@mail.ruaf895762013-09-28 11:32:51 +0400430 struct ip6_tnl_parm2 p;
Alexey Andriyanova0638e12014-10-29 09:19:30 +0300431 const char *basedev = "ip6tnl0";
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900432
433 ip6_tnl_parm_init(&p, 1);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900434
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000435 if (parse_args(argc, argv, cmd, &p) < 0)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900436 return -1;
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900437
Alexey Andriyanova0638e12014-10-29 09:19:30 +0300438 if (p.proto == IPPROTO_GRE)
439 basedev = "ip6gre0";
440 else if (p.i_flags & VTI_ISVTI)
441 basedev = "ip6_vti0";
442
443 return tnl_add_ioctl(cmd, basedev, p.name, &p);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900444}
445
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900446static int do_del(int argc, char **argv)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900447{
xeb@mail.ruaf895762013-09-28 11:32:51 +0400448 struct ip6_tnl_parm2 p;
Alexey Andriyanova0638e12014-10-29 09:19:30 +0300449 const char *basedev = "ip6tnl0";
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900450
451 ip6_tnl_parm_init(&p, 1);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900452
Jiri Benc21a5a6b2011-09-19 05:14:10 +0000453 if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900454 return -1;
455
Alexey Andriyanova0638e12014-10-29 09:19:30 +0300456 if (p.proto == IPPROTO_GRE)
457 basedev = "ip6gre0";
458 else if (p.i_flags & VTI_ISVTI)
459 basedev = "ip6_vti0";
460
461 return tnl_del_ioctl(basedev, p.name, &p);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900462}
463
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800464int do_ip6tunnel(int argc, char **argv)
465{
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900466 switch (preferred_family) {
467 case AF_UNSPEC:
468 preferred_family = AF_INET6;
469 break;
470 case AF_INET6:
471 break;
472 default:
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000473 fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family);
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900474 exit(-1);
475 }
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900476
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900477 if (argc > 0) {
478 if (matches(*argv, "add") == 0)
479 return do_add(SIOCADDTUNNEL, argc - 1, argv + 1);
480 if (matches(*argv, "change") == 0)
481 return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1);
Andreas Henriksson6e304612012-05-19 16:08:21 +0200482 if (matches(*argv, "delete") == 0)
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900483 return do_del(argc - 1, argv + 1);
484 if (matches(*argv, "show") == 0 ||
485 matches(*argv, "lst") == 0 ||
486 matches(*argv, "list") == 0)
487 return do_show(argc - 1, argv + 1);
488 if (matches(*argv, "help") == 0)
489 usage();
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900490 } else
491 return do_show(0, NULL);
492
Masahide NAKAMURA288384f2006-11-24 12:27:09 +0900493 fprintf(stderr, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv);
494 exit(-1);
Masahide NAKAMURA9447a0d2006-11-24 12:27:06 +0900495}