]>
Commit | Line | Data |
---|---|---|
9447a0d3 MN |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
ae665a52 | 3 | * |
9447a0d3 MN |
4 | * 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. | |
ae665a52 | 8 | * |
9447a0d3 MN |
9 | * 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. | |
ae665a52 | 13 | * |
9447a0d3 | 14 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
9447a0d3 | 16 | */ |
9447a0d3 MN |
17 | /* |
18 | * Author: | |
19 | * Masahide NAKAMURA @USAGI | |
20 | */ | |
ae665a52 | 21 | |
9447a0d3 MN |
22 | #include <stdio.h> |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
9447a0d3 | 25 | #include <unistd.h> |
288384f2 MN |
26 | #include <sys/types.h> |
27 | #include <sys/socket.h> | |
9447a0d3 | 28 | #include <arpa/inet.h> |
288384f2 | 29 | #include <sys/ioctl.h> |
9447a0d3 | 30 | #include <linux/ip.h> |
288384f2 MN |
31 | #include <linux/if.h> |
32 | #include <linux/if_arp.h> | |
9447a0d3 | 33 | #include <linux/if_tunnel.h> |
288384f2 | 34 | #include <linux/ip6_tunnel.h> |
9447a0d3 | 35 | |
288384f2 MN |
36 | #include "utils.h" |
37 | #include "tunnel.h" | |
ea71beac | 38 | #include "ip_common.h" |
9447a0d3 | 39 | |
288384f2 MN |
40 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) |
41 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) | |
9447a0d3 | 42 | |
288384f2 MN |
43 | #define DEFAULT_TNL_HOP_LIMIT (64) |
44 | ||
45 | static void usage(void) __attribute__((noreturn)); | |
9447a0d3 MN |
46 | |
47 | static void usage(void) | |
48 | { | |
288384f2 | 49 | fprintf(stderr, "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n"); |
f36d1140 | 50 | fprintf(stderr, " [ mode { ip6ip6 | ipip6 | ip6gre | vti6 | any } ]\n"); |
288384f2 MN |
51 | fprintf(stderr, " [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n"); |
52 | fprintf(stderr, " [ encaplimit ELIM ]\n"); | |
eddde110 | 53 | fprintf(stderr ," [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"); |
288384f2 | 54 | fprintf(stderr, " [ dscp inherit ]\n"); |
af89576d | 55 | fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); |
288384f2 | 56 | fprintf(stderr, "\n"); |
eddde110 YH |
57 | fprintf(stderr, "Where: NAME := STRING\n"); |
58 | fprintf(stderr, " ADDR := IPV6_ADDRESS\n"); | |
59 | fprintf(stderr, " ELIM := { none | 0..255 }(default=%d)\n", | |
288384f2 | 60 | IPV6_DEFAULT_TNL_ENCAP_LIMIT); |
eddde110 | 61 | fprintf(stderr, " TTL := 0..255 (default=%d)\n", |
288384f2 | 62 | DEFAULT_TNL_HOP_LIMIT); |
d0c8420c | 63 | fprintf(stderr, " TCLASS := { 0x0..0xff | inherit }\n"); |
eddde110 | 64 | fprintf(stderr, " FLOWLABEL := { 0x0..0xfffff | inherit }\n"); |
af89576d | 65 | fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
9447a0d3 MN |
66 | exit(-1); |
67 | } | |
68 | ||
af89576d | 69 | static void print_tunnel(struct ip6_tnl_parm2 *p) |
9447a0d3 MN |
70 | { |
71 | char remote[64]; | |
72 | char local[64]; | |
ae665a52 | 73 | |
9447a0d3 MN |
74 | inet_ntop(AF_INET6, &p->raddr, remote, sizeof(remote)); |
75 | inet_ntop(AF_INET6, &p->laddr, local, sizeof(local)); | |
76 | ||
288384f2 MN |
77 | printf("%s: %s/ipv6 remote %s local %s", |
78 | p->name, tnl_strproto(p->proto), remote, local); | |
9447a0d3 | 79 | if (p->link) { |
ea71beac | 80 | const char *n = ll_index_to_name(p->link); |
9447a0d3 MN |
81 | if (n) |
82 | printf(" dev %s", n); | |
83 | } | |
9447a0d3 | 84 | |
288384f2 MN |
85 | if (p->flags & IP6_TNL_F_IGN_ENCAP_LIMIT) |
86 | printf(" encaplimit none"); | |
87 | else | |
88 | printf(" encaplimit %u", p->encap_limit); | |
9447a0d3 | 89 | |
288384f2 | 90 | printf(" hoplimit %u", p->hop_limit); |
9447a0d3 | 91 | |
288384f2 | 92 | if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS) |
eddde110 | 93 | printf(" tclass inherit"); |
288384f2 MN |
94 | else { |
95 | __u32 val = ntohl(p->flowinfo & IP6_FLOWINFO_TCLASS); | |
eddde110 | 96 | printf(" tclass 0x%02x", (__u8)(val >> 20)); |
9447a0d3 | 97 | } |
9447a0d3 | 98 | |
288384f2 | 99 | if (p->flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
eddde110 | 100 | printf(" flowlabel inherit"); |
288384f2 | 101 | else |
eddde110 | 102 | printf(" flowlabel 0x%05x", ntohl(p->flowinfo & IP6_FLOWINFO_FLOWLABEL)); |
9447a0d3 | 103 | |
288384f2 | 104 | printf(" (flowinfo 0x%08x)", ntohl(p->flowinfo)); |
9447a0d3 | 105 | |
288384f2 MN |
106 | if (p->flags & IP6_TNL_F_RCV_DSCP_COPY) |
107 | printf(" dscp inherit"); | |
af89576d | 108 | |
109 | if (p->proto == IPPROTO_GRE) { | |
110 | if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key) | |
111 | printf(" key %u", ntohl(p->i_key)); | |
112 | else if ((p->i_flags|p->o_flags)&GRE_KEY) { | |
113 | if (p->i_flags&GRE_KEY) | |
114 | printf(" ikey %u ", ntohl(p->i_key)); | |
115 | if (p->o_flags&GRE_KEY) | |
116 | printf(" okey %u ", ntohl(p->o_key)); | |
117 | } | |
118 | ||
119 | if (p->i_flags&GRE_SEQ) | |
4cb8d030 | 120 | printf("%s Drop packets out of sequence.", _SL_); |
af89576d | 121 | if (p->i_flags&GRE_CSUM) |
122 | printf("%s Checksum in received packet is required.", _SL_); | |
123 | if (p->o_flags&GRE_SEQ) | |
124 | printf("%s Sequence packets on output.", _SL_); | |
125 | if (p->o_flags&GRE_CSUM) | |
126 | printf("%s Checksum output packets.", _SL_); | |
127 | } | |
9447a0d3 MN |
128 | } |
129 | ||
af89576d | 130 | static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p) |
9447a0d3 | 131 | { |
21a5a6b3 | 132 | int count = 0; |
9447a0d3 MN |
133 | char medium[IFNAMSIZ]; |
134 | ||
288384f2 | 135 | memset(medium, 0, sizeof(medium)); |
9447a0d3 MN |
136 | |
137 | while (argc > 0) { | |
0b959b0f YH |
138 | if (strcmp(*argv, "mode") == 0) { |
139 | NEXT_ARG(); | |
140 | if (strcmp(*argv, "ipv6/ipv6") == 0 || | |
141 | strcmp(*argv, "ip6ip6") == 0) | |
142 | p->proto = IPPROTO_IPV6; | |
f36d1140 SK |
143 | else if (strcmp(*argv, "vti6") == 0) { |
144 | p->proto = IPPROTO_IPV6; | |
145 | p->i_flags |= VTI_ISVTI; | |
146 | } else if (strcmp(*argv, "ip/ipv6") == 0 || | |
0b959b0f YH |
147 | strcmp(*argv, "ipv4/ipv6") == 0 || |
148 | strcmp(*argv, "ipip6") == 0 || | |
149 | strcmp(*argv, "ip4ip6") == 0) | |
150 | p->proto = IPPROTO_IPIP; | |
af89576d | 151 | else if (strcmp(*argv, "ip6gre") == 0 || |
152 | strcmp(*argv, "gre/ipv6") == 0) | |
153 | p->proto = IPPROTO_GRE; | |
0b959b0f YH |
154 | else if (strcmp(*argv, "any/ipv6") == 0 || |
155 | strcmp(*argv, "any") == 0) | |
156 | p->proto = 0; | |
157 | else { | |
4b726cb1 SH |
158 | fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv); |
159 | exit(-1); | |
160 | } | |
161 | } else if (strcmp(*argv, "remote") == 0) { | |
288384f2 MN |
162 | inet_prefix raddr; |
163 | NEXT_ARG(); | |
164 | get_prefix(&raddr, *argv, preferred_family); | |
165 | if (raddr.family == AF_UNSPEC) | |
166 | invarg("\"remote\" address family is AF_UNSPEC", *argv); | |
167 | memcpy(&p->raddr, &raddr.data, sizeof(p->raddr)); | |
168 | } else if (strcmp(*argv, "local") == 0) { | |
169 | inet_prefix laddr; | |
170 | NEXT_ARG(); | |
171 | get_prefix(&laddr, *argv, preferred_family); | |
172 | if (laddr.family == AF_UNSPEC) | |
173 | invarg("\"local\" address family is AF_UNSPEC", *argv); | |
174 | memcpy(&p->laddr, &laddr.data, sizeof(p->laddr)); | |
175 | } else if (strcmp(*argv, "dev") == 0) { | |
176 | NEXT_ARG(); | |
9447a0d3 | 177 | strncpy(medium, *argv, IFNAMSIZ - 1); |
288384f2 MN |
178 | } else if (strcmp(*argv, "encaplimit") == 0) { |
179 | NEXT_ARG(); | |
180 | if (strcmp(*argv, "none") == 0) { | |
181 | p->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; | |
9447a0d3 MN |
182 | } else { |
183 | __u8 uval; | |
288384f2 MN |
184 | if (get_u8(&uval, *argv, 0) < -1) |
185 | invarg("invalid ELIM", *argv); | |
9447a0d3 | 186 | p->encap_limit = uval; |
3f83dce5 | 187 | p->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; |
9447a0d3 | 188 | } |
eddde110 YH |
189 | } else if (strcmp(*argv, "hoplimit") == 0 || |
190 | strcmp(*argv, "ttl") == 0 || | |
191 | strcmp(*argv, "hlim") == 0) { | |
288384f2 MN |
192 | __u8 uval; |
193 | NEXT_ARG(); | |
194 | if (get_u8(&uval, *argv, 0)) | |
eddde110 | 195 | invarg("invalid TTL", *argv); |
288384f2 | 196 | p->hop_limit = uval; |
eddde110 YH |
197 | } else if (strcmp(*argv, "tclass") == 0 || |
198 | strcmp(*argv, "tc") == 0 || | |
199 | strcmp(*argv, "tos") == 0 || | |
200 | matches(*argv, "dsfield") == 0) { | |
288384f2 MN |
201 | __u8 uval; |
202 | NEXT_ARG(); | |
df5574d0 | 203 | p->flowinfo &= ~IP6_FLOWINFO_TCLASS; |
288384f2 MN |
204 | if (strcmp(*argv, "inherit") == 0) |
205 | p->flags |= IP6_TNL_F_USE_ORIG_TCLASS; | |
206 | else { | |
207 | if (get_u8(&uval, *argv, 16)) | |
eddde110 | 208 | invarg("invalid TClass", *argv); |
288384f2 MN |
209 | p->flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; |
210 | p->flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; | |
211 | } | |
eddde110 YH |
212 | } else if (strcmp(*argv, "flowlabel") == 0 || |
213 | strcmp(*argv, "fl") == 0) { | |
288384f2 MN |
214 | __u32 uval; |
215 | NEXT_ARG(); | |
df5574d0 | 216 | p->flowinfo &= ~IP6_FLOWINFO_FLOWLABEL; |
288384f2 MN |
217 | if (strcmp(*argv, "inherit") == 0) |
218 | p->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
219 | else { | |
220 | if (get_u32(&uval, *argv, 16)) | |
eddde110 | 221 | invarg("invalid Flowlabel", *argv); |
288384f2 | 222 | if (uval > 0xFFFFF) |
eddde110 | 223 | invarg("invalid Flowlabel", *argv); |
288384f2 MN |
224 | p->flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; |
225 | p->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
226 | } | |
227 | } else if (strcmp(*argv, "dscp") == 0) { | |
228 | NEXT_ARG(); | |
229 | if (strcmp(*argv, "inherit") != 0) | |
230 | invarg("not inherit", *argv); | |
231 | p->flags |= IP6_TNL_F_RCV_DSCP_COPY; | |
af89576d | 232 | } else if (strcmp(*argv, "key") == 0) { |
233 | unsigned uval; | |
234 | NEXT_ARG(); | |
235 | p->i_flags |= GRE_KEY; | |
236 | p->o_flags |= GRE_KEY; | |
237 | if (strchr(*argv, '.')) | |
238 | p->i_key = p->o_key = get_addr32(*argv); | |
239 | else { | |
4b726cb1 | 240 | if (get_unsigned(&uval, *argv, 0) < 0) { |
af89576d | 241 | fprintf(stderr, "invalid value of \"key\"\n"); |
242 | exit(-1); | |
243 | } | |
244 | p->i_key = p->o_key = htonl(uval); | |
245 | } | |
246 | } else if (strcmp(*argv, "ikey") == 0) { | |
247 | unsigned uval; | |
248 | NEXT_ARG(); | |
249 | p->i_flags |= GRE_KEY; | |
250 | if (strchr(*argv, '.')) | |
251 | p->i_key = get_addr32(*argv); | |
252 | else { | |
253 | if (get_unsigned(&uval, *argv, 0)<0) { | |
254 | fprintf(stderr, "invalid value of \"ikey\"\n"); | |
255 | exit(-1); | |
256 | } | |
257 | p->i_key = htonl(uval); | |
258 | } | |
259 | } else if (strcmp(*argv, "okey") == 0) { | |
260 | unsigned uval; | |
261 | NEXT_ARG(); | |
262 | p->o_flags |= GRE_KEY; | |
263 | if (strchr(*argv, '.')) | |
264 | p->o_key = get_addr32(*argv); | |
265 | else { | |
266 | if (get_unsigned(&uval, *argv, 0)<0) { | |
267 | fprintf(stderr, "invalid value of \"okey\"\n"); | |
268 | exit(-1); | |
269 | } | |
270 | p->o_key = htonl(uval); | |
271 | } | |
272 | } else if (strcmp(*argv, "seq") == 0) { | |
273 | p->i_flags |= GRE_SEQ; | |
274 | p->o_flags |= GRE_SEQ; | |
275 | } else if (strcmp(*argv, "iseq") == 0) { | |
276 | p->i_flags |= GRE_SEQ; | |
277 | } else if (strcmp(*argv, "oseq") == 0) { | |
278 | p->o_flags |= GRE_SEQ; | |
279 | } else if (strcmp(*argv, "csum") == 0) { | |
280 | p->i_flags |= GRE_CSUM; | |
281 | p->o_flags |= GRE_CSUM; | |
282 | } else if (strcmp(*argv, "icsum") == 0) { | |
283 | p->i_flags |= GRE_CSUM; | |
284 | } else if (strcmp(*argv, "ocsum") == 0) { | |
285 | p->o_flags |= GRE_CSUM; | |
9447a0d3 | 286 | } else { |
288384f2 MN |
287 | if (strcmp(*argv, "name") == 0) { |
288 | NEXT_ARG(); | |
289 | } | |
290 | if (matches(*argv, "help") == 0) | |
9447a0d3 | 291 | usage(); |
288384f2 MN |
292 | if (p->name[0]) |
293 | duparg2("name", *argv); | |
9447a0d3 | 294 | strncpy(p->name, *argv, IFNAMSIZ - 1); |
21a5a6b3 | 295 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
af89576d | 296 | struct ip6_tnl_parm2 old_p; |
21a5a6b3 JB |
297 | memset(&old_p, 0, sizeof(old_p)); |
298 | if (tnl_get_ioctl(*argv, &old_p)) | |
299 | return -1; | |
300 | *p = old_p; | |
301 | } | |
9447a0d3 | 302 | } |
21a5a6b3 | 303 | count++; |
9447a0d3 MN |
304 | argc--; argv++; |
305 | } | |
306 | if (medium[0]) { | |
ea71beac | 307 | p->link = ll_name_to_index(medium); |
9447a0d3 MN |
308 | if (p->link == 0) |
309 | return -1; | |
310 | } | |
311 | return 0; | |
312 | } | |
313 | ||
af89576d | 314 | static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default) |
288384f2 MN |
315 | { |
316 | memset(p, 0, sizeof(*p)); | |
317 | p->proto = IPPROTO_IPV6; | |
318 | if (apply_default) { | |
319 | p->hop_limit = DEFAULT_TNL_HOP_LIMIT; | |
320 | p->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; | |
321 | } | |
322 | } | |
323 | ||
324 | /* | |
325 | * @p1: user specified parameter | |
326 | * @p2: database entry | |
327 | */ | |
af89576d | 328 | static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 *p1, |
329 | const struct ip6_tnl_parm2 *p2) | |
288384f2 MN |
330 | { |
331 | return ((!p1->link || p1->link == p2->link) && | |
332 | (!p1->name[0] || strcmp(p1->name, p2->name) == 0) && | |
333 | (memcmp(&p1->laddr, &in6addr_any, sizeof(p1->laddr)) == 0 || | |
334 | memcmp(&p1->laddr, &p2->laddr, sizeof(p1->laddr)) == 0) && | |
335 | (memcmp(&p1->raddr, &in6addr_any, sizeof(p1->raddr)) == 0 || | |
336 | memcmp(&p1->raddr, &p2->raddr, sizeof(p1->raddr)) == 0) && | |
337 | (!p1->proto || !p2->proto || p1->proto == p2->proto) && | |
338 | (!p1->encap_limit || p1->encap_limit == p2->encap_limit) && | |
339 | (!p1->hop_limit || p1->hop_limit == p2->hop_limit) && | |
340 | (!(p1->flowinfo & IP6_FLOWINFO_TCLASS) || | |
341 | !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_TCLASS)) && | |
342 | (!(p1->flowinfo & IP6_FLOWINFO_FLOWLABEL) || | |
343 | !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_FLOWLABEL)) && | |
344 | (!p1->flags || (p1->flags & p2->flags))); | |
345 | } | |
346 | ||
af89576d | 347 | static int do_tunnels_list(struct ip6_tnl_parm2 *p) |
288384f2 MN |
348 | { |
349 | char buf[512]; | |
350 | int err = -1; | |
351 | FILE *fp = fopen("/proc/net/dev", "r"); | |
352 | if (fp == NULL) { | |
353 | perror("fopen"); | |
354 | goto end; | |
355 | } | |
356 | ||
357 | /* skip two lines at the begenning of the file */ | |
38c867d2 SH |
358 | if (!fgets(buf, sizeof(buf), fp) || |
359 | !fgets(buf, sizeof(buf), fp)) { | |
360 | fprintf(stderr, "/proc/net/dev read error\n"); | |
361 | return -1; | |
362 | } | |
288384f2 MN |
363 | |
364 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
365 | char name[IFNAMSIZ]; | |
ea71beac | 366 | int index, type; |
288384f2 MN |
367 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
368 | rx_fifo, rx_frame, | |
369 | tx_bytes, tx_packets, tx_errs, tx_drops, | |
370 | tx_fifo, tx_colls, tx_carrier, rx_multi; | |
af89576d | 371 | struct ip6_tnl_parm2 p1; |
288384f2 MN |
372 | char *ptr; |
373 | ||
374 | buf[sizeof(buf) - 1] = '\0'; | |
375 | if ((ptr = strchr(buf, ':')) == NULL || | |
376 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { | |
14645ec2 | 377 | fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); |
288384f2 MN |
378 | goto end; |
379 | } | |
380 | if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", | |
381 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, | |
382 | &rx_fifo, &rx_frame, &rx_multi, | |
383 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, | |
384 | &tx_fifo, &tx_colls, &tx_carrier) != 14) | |
385 | continue; | |
386 | if (p->name[0] && strcmp(p->name, name)) | |
387 | continue; | |
ea71beac SH |
388 | index = ll_name_to_index(name); |
389 | if (index == 0) | |
390 | continue; | |
391 | type = ll_index_to_type(index); | |
288384f2 | 392 | if (type == -1) { |
14645ec2 | 393 | fprintf(stderr, "Failed to get type of \"%s\"\n", name); |
288384f2 MN |
394 | continue; |
395 | } | |
af89576d | 396 | if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE) |
288384f2 MN |
397 | continue; |
398 | memset(&p1, 0, sizeof(p1)); | |
399 | ip6_tnl_parm_init(&p1, 0); | |
af89576d | 400 | if (type == ARPHRD_IP6GRE) |
401 | p1.proto = IPPROTO_GRE; | |
288384f2 | 402 | strcpy(p1.name, name); |
ea71beac | 403 | p1.link = ll_name_to_index(p1.name); |
288384f2 MN |
404 | if (p1.link == 0) |
405 | continue; | |
406 | if (tnl_get_ioctl(p1.name, &p1)) | |
407 | continue; | |
408 | if (!ip6_tnl_parm_match(p, &p1)) | |
409 | continue; | |
410 | print_tunnel(&p1); | |
411 | if (show_stats) { | |
412 | printf("%s", _SL_); | |
413 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); | |
414 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", | |
415 | rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); | |
416 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); | |
417 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", | |
418 | tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); | |
419 | } | |
420 | printf("\n"); | |
421 | } | |
422 | err = 0; | |
423 | ||
424 | end: | |
425 | if (fp) | |
426 | fclose(fp); | |
427 | return err; | |
428 | } | |
429 | ||
9447a0d3 MN |
430 | static int do_show(int argc, char **argv) |
431 | { | |
af89576d | 432 | struct ip6_tnl_parm2 p; |
288384f2 | 433 | |
ea71beac | 434 | ll_init_map(&rth); |
288384f2 | 435 | ip6_tnl_parm_init(&p, 0); |
c3651bf4 | 436 | p.proto = 0; /* default to any */ |
9447a0d3 | 437 | |
21a5a6b3 | 438 | if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) |
9447a0d3 MN |
439 | return -1; |
440 | ||
288384f2 MN |
441 | if (!p.name[0] || show_stats) |
442 | do_tunnels_list(&p); | |
443 | else { | |
444 | if (tnl_get_ioctl(p.name, &p)) | |
445 | return -1; | |
446 | print_tunnel(&p); | |
447 | printf("\n"); | |
448 | } | |
9447a0d3 | 449 | |
9447a0d3 MN |
450 | return 0; |
451 | } | |
452 | ||
9447a0d3 MN |
453 | static int do_add(int cmd, int argc, char **argv) |
454 | { | |
af89576d | 455 | struct ip6_tnl_parm2 p; |
a0638e18 | 456 | const char *basedev = "ip6tnl0"; |
288384f2 MN |
457 | |
458 | ip6_tnl_parm_init(&p, 1); | |
9447a0d3 | 459 | |
21a5a6b3 | 460 | if (parse_args(argc, argv, cmd, &p) < 0) |
9447a0d3 | 461 | return -1; |
288384f2 | 462 | |
a0638e18 AA |
463 | if (p.proto == IPPROTO_GRE) |
464 | basedev = "ip6gre0"; | |
465 | else if (p.i_flags & VTI_ISVTI) | |
466 | basedev = "ip6_vti0"; | |
467 | ||
468 | return tnl_add_ioctl(cmd, basedev, p.name, &p); | |
9447a0d3 MN |
469 | } |
470 | ||
288384f2 | 471 | static int do_del(int argc, char **argv) |
9447a0d3 | 472 | { |
af89576d | 473 | struct ip6_tnl_parm2 p; |
a0638e18 | 474 | const char *basedev = "ip6tnl0"; |
288384f2 MN |
475 | |
476 | ip6_tnl_parm_init(&p, 1); | |
9447a0d3 | 477 | |
21a5a6b3 | 478 | if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0) |
9447a0d3 MN |
479 | return -1; |
480 | ||
a0638e18 AA |
481 | if (p.proto == IPPROTO_GRE) |
482 | basedev = "ip6gre0"; | |
483 | else if (p.i_flags & VTI_ISVTI) | |
484 | basedev = "ip6_vti0"; | |
485 | ||
486 | return tnl_del_ioctl(basedev, p.name, &p); | |
9447a0d3 MN |
487 | } |
488 | ||
ae665a52 SH |
489 | int do_ip6tunnel(int argc, char **argv) |
490 | { | |
288384f2 MN |
491 | switch (preferred_family) { |
492 | case AF_UNSPEC: | |
493 | preferred_family = AF_INET6; | |
494 | break; | |
495 | case AF_INET6: | |
496 | break; | |
497 | default: | |
14645ec2 | 498 | fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family); |
288384f2 MN |
499 | exit(-1); |
500 | } | |
501 | ||
9447a0d3 | 502 | if (argc > 0) { |
288384f2 | 503 | if (matches(*argv, "add") == 0) |
9447a0d3 | 504 | return do_add(SIOCADDTUNNEL, argc - 1, argv + 1); |
288384f2 | 505 | if (matches(*argv, "change") == 0) |
9447a0d3 | 506 | return do_add(SIOCCHGTUNNEL, argc - 1, argv + 1); |
6e30461e | 507 | if (matches(*argv, "delete") == 0) |
9447a0d3 | 508 | return do_del(argc - 1, argv + 1); |
288384f2 MN |
509 | if (matches(*argv, "show") == 0 || |
510 | matches(*argv, "lst") == 0 || | |
511 | matches(*argv, "list") == 0) | |
9447a0d3 | 512 | return do_show(argc - 1, argv + 1); |
288384f2 | 513 | if (matches(*argv, "help") == 0) |
9447a0d3 | 514 | usage(); |
9447a0d3 MN |
515 | } else |
516 | return do_show(0, NULL); | |
517 | ||
288384f2 MN |
518 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv); |
519 | exit(-1); | |
9447a0d3 | 520 | } |