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