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