osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame^] | 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 | * |
| 11 | * |
| 12 | * Changes: |
| 13 | * |
| 14 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 15 | * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit |
| 16 | * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <syslog.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <linux/if.h> |
| 28 | #include <linux/if_arp.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <netinet/ip.h> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <linux/if_tunnel.h> |
| 33 | |
| 34 | #include "rt_names.h" |
| 35 | #include "utils.h" |
| 36 | |
| 37 | static void usage(void) __attribute__((noreturn)); |
| 38 | |
| 39 | static void usage(void) |
| 40 | { |
| 41 | fprintf(stderr, "Usage: ip tunnel { add | change | del | show } [ NAME ]\n"); |
| 42 | fprintf(stderr, " [ mode { ipip | gre | sit } ] [ remote ADDR ] [ local ADDR ]\n"); |
| 43 | fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); |
| 44 | fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n"); |
| 45 | fprintf(stderr, "\n"); |
| 46 | fprintf(stderr, "Where: NAME := STRING\n"); |
| 47 | fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n"); |
| 48 | fprintf(stderr, " TOS := { NUMBER | inherit }\n"); |
| 49 | fprintf(stderr, " TTL := { 1..255 | inherit }\n"); |
| 50 | fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
| 51 | exit(-1); |
| 52 | } |
| 53 | |
| 54 | static int do_ioctl_get_ifindex(char *dev) |
| 55 | { |
| 56 | struct ifreq ifr; |
| 57 | int fd; |
| 58 | int err; |
| 59 | |
| 60 | strcpy(ifr.ifr_name, dev); |
| 61 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 62 | err = ioctl(fd, SIOCGIFINDEX, &ifr); |
| 63 | if (err) { |
| 64 | perror("ioctl"); |
| 65 | return 0; |
| 66 | } |
| 67 | close(fd); |
| 68 | return ifr.ifr_ifindex; |
| 69 | } |
| 70 | |
| 71 | static int do_ioctl_get_iftype(char *dev) |
| 72 | { |
| 73 | struct ifreq ifr; |
| 74 | int fd; |
| 75 | int err; |
| 76 | |
| 77 | strcpy(ifr.ifr_name, dev); |
| 78 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 79 | err = ioctl(fd, SIOCGIFHWADDR, &ifr); |
| 80 | if (err) { |
| 81 | perror("ioctl"); |
| 82 | return -1; |
| 83 | } |
| 84 | close(fd); |
| 85 | return ifr.ifr_addr.sa_family; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static char * do_ioctl_get_ifname(int idx) |
| 90 | { |
| 91 | static struct ifreq ifr; |
| 92 | int fd; |
| 93 | int err; |
| 94 | |
| 95 | ifr.ifr_ifindex = idx; |
| 96 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 97 | err = ioctl(fd, SIOCGIFNAME, &ifr); |
| 98 | if (err) { |
| 99 | perror("ioctl"); |
| 100 | return NULL; |
| 101 | } |
| 102 | close(fd); |
| 103 | return ifr.ifr_name; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p) |
| 109 | { |
| 110 | struct ifreq ifr; |
| 111 | int fd; |
| 112 | int err; |
| 113 | |
| 114 | strcpy(ifr.ifr_name, basedev); |
| 115 | ifr.ifr_ifru.ifru_data = (void*)p; |
| 116 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 117 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
| 118 | if (err) |
| 119 | perror("ioctl"); |
| 120 | close(fd); |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p) |
| 125 | { |
| 126 | struct ifreq ifr; |
| 127 | int fd; |
| 128 | int err; |
| 129 | |
| 130 | if (cmd == SIOCCHGTUNNEL && p->name[0]) |
| 131 | strcpy(ifr.ifr_name, p->name); |
| 132 | else |
| 133 | strcpy(ifr.ifr_name, basedev); |
| 134 | ifr.ifr_ifru.ifru_data = (void*)p; |
| 135 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 136 | err = ioctl(fd, cmd, &ifr); |
| 137 | if (err) |
| 138 | perror("ioctl"); |
| 139 | close(fd); |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p) |
| 144 | { |
| 145 | struct ifreq ifr; |
| 146 | int fd; |
| 147 | int err; |
| 148 | |
| 149 | if (p->name[0]) |
| 150 | strcpy(ifr.ifr_name, p->name); |
| 151 | else |
| 152 | strcpy(ifr.ifr_name, basedev); |
| 153 | ifr.ifr_ifru.ifru_data = (void*)p; |
| 154 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 155 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
| 156 | if (err) |
| 157 | perror("ioctl"); |
| 158 | close(fd); |
| 159 | return err; |
| 160 | } |
| 161 | |
| 162 | static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) |
| 163 | { |
| 164 | int count = 0; |
| 165 | char medium[IFNAMSIZ]; |
| 166 | |
| 167 | memset(p, 0, sizeof(*p)); |
| 168 | memset(&medium, 0, sizeof(medium)); |
| 169 | |
| 170 | p->iph.version = 4; |
| 171 | p->iph.ihl = 5; |
| 172 | #ifndef IP_DF |
| 173 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
| 174 | #endif |
| 175 | p->iph.frag_off = htons(IP_DF); |
| 176 | |
| 177 | while (argc > 0) { |
| 178 | if (strcmp(*argv, "mode") == 0) { |
| 179 | NEXT_ARG(); |
| 180 | if (strcmp(*argv, "ipip") == 0 || |
| 181 | strcmp(*argv, "ip/ip") == 0) { |
| 182 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
| 183 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 184 | exit(-1); |
| 185 | } |
| 186 | p->iph.protocol = IPPROTO_IPIP; |
| 187 | } else if (strcmp(*argv, "gre") == 0 || |
| 188 | strcmp(*argv, "gre/ip") == 0) { |
| 189 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
| 190 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 191 | exit(-1); |
| 192 | } |
| 193 | p->iph.protocol = IPPROTO_GRE; |
| 194 | } else if (strcmp(*argv, "sit") == 0 || |
| 195 | strcmp(*argv, "ipv6/ip") == 0) { |
| 196 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
| 197 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 198 | exit(-1); |
| 199 | } |
| 200 | p->iph.protocol = IPPROTO_IPV6; |
| 201 | } else { |
| 202 | fprintf(stderr,"Cannot guess tunnel mode.\n"); |
| 203 | exit(-1); |
| 204 | } |
| 205 | } else if (strcmp(*argv, "key") == 0) { |
| 206 | unsigned uval; |
| 207 | NEXT_ARG(); |
| 208 | p->i_flags |= GRE_KEY; |
| 209 | p->o_flags |= GRE_KEY; |
| 210 | if (strchr(*argv, '.')) |
| 211 | p->i_key = p->o_key = get_addr32(*argv); |
| 212 | else { |
| 213 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 214 | fprintf(stderr, "invalid value of \"key\"\n"); |
| 215 | exit(-1); |
| 216 | } |
| 217 | p->i_key = p->o_key = htonl(uval); |
| 218 | } |
| 219 | } else if (strcmp(*argv, "ikey") == 0) { |
| 220 | unsigned uval; |
| 221 | NEXT_ARG(); |
| 222 | p->i_flags |= GRE_KEY; |
| 223 | if (strchr(*argv, '.')) |
| 224 | p->o_key = get_addr32(*argv); |
| 225 | else { |
| 226 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 227 | fprintf(stderr, "invalid value of \"ikey\"\n"); |
| 228 | exit(-1); |
| 229 | } |
| 230 | p->i_key = htonl(uval); |
| 231 | } |
| 232 | } else if (strcmp(*argv, "okey") == 0) { |
| 233 | unsigned uval; |
| 234 | NEXT_ARG(); |
| 235 | p->o_flags |= GRE_KEY; |
| 236 | if (strchr(*argv, '.')) |
| 237 | p->o_key = get_addr32(*argv); |
| 238 | else { |
| 239 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 240 | fprintf(stderr, "invalid value of \"okey\"\n"); |
| 241 | exit(-1); |
| 242 | } |
| 243 | p->o_key = htonl(uval); |
| 244 | } |
| 245 | } else if (strcmp(*argv, "seq") == 0) { |
| 246 | p->i_flags |= GRE_SEQ; |
| 247 | p->o_flags |= GRE_SEQ; |
| 248 | } else if (strcmp(*argv, "iseq") == 0) { |
| 249 | p->i_flags |= GRE_SEQ; |
| 250 | } else if (strcmp(*argv, "oseq") == 0) { |
| 251 | p->o_flags |= GRE_SEQ; |
| 252 | } else if (strcmp(*argv, "csum") == 0) { |
| 253 | p->i_flags |= GRE_CSUM; |
| 254 | p->o_flags |= GRE_CSUM; |
| 255 | } else if (strcmp(*argv, "icsum") == 0) { |
| 256 | p->i_flags |= GRE_CSUM; |
| 257 | } else if (strcmp(*argv, "ocsum") == 0) { |
| 258 | p->o_flags |= GRE_CSUM; |
| 259 | } else if (strcmp(*argv, "nopmtudisc") == 0) { |
| 260 | p->iph.frag_off = 0; |
| 261 | } else if (strcmp(*argv, "pmtudisc") == 0) { |
| 262 | p->iph.frag_off = htons(IP_DF); |
| 263 | } else if (strcmp(*argv, "remote") == 0) { |
| 264 | NEXT_ARG(); |
| 265 | if (strcmp(*argv, "any")) |
| 266 | p->iph.daddr = get_addr32(*argv); |
| 267 | } else if (strcmp(*argv, "local") == 0) { |
| 268 | NEXT_ARG(); |
| 269 | if (strcmp(*argv, "any")) |
| 270 | p->iph.saddr = get_addr32(*argv); |
| 271 | } else if (strcmp(*argv, "dev") == 0) { |
| 272 | NEXT_ARG(); |
| 273 | strncpy(medium, *argv, IFNAMSIZ-1); |
| 274 | } else if (strcmp(*argv, "ttl") == 0) { |
| 275 | unsigned uval; |
| 276 | NEXT_ARG(); |
| 277 | if (strcmp(*argv, "inherit") != 0) { |
| 278 | if (get_unsigned(&uval, *argv, 0)) |
| 279 | invarg("invalid TTL\n", *argv); |
| 280 | if (uval > 255) |
| 281 | invarg("TTL must be <=255\n", *argv); |
| 282 | p->iph.ttl = uval; |
| 283 | } |
| 284 | } else if (strcmp(*argv, "tos") == 0 || |
| 285 | matches(*argv, "dsfield") == 0) { |
| 286 | __u32 uval; |
| 287 | NEXT_ARG(); |
| 288 | if (strcmp(*argv, "inherit") != 0) { |
| 289 | if (rtnl_dsfield_a2n(&uval, *argv)) |
| 290 | invarg("bad TOS value", *argv); |
| 291 | p->iph.tos = uval; |
| 292 | } else |
| 293 | p->iph.tos = 1; |
| 294 | } else { |
| 295 | if (strcmp(*argv, "name") == 0) { |
| 296 | NEXT_ARG(); |
| 297 | } |
| 298 | if (matches(*argv, "help") == 0) |
| 299 | usage(); |
| 300 | if (p->name[0]) |
| 301 | duparg2("name", *argv); |
| 302 | strncpy(p->name, *argv, IFNAMSIZ); |
| 303 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
| 304 | struct ip_tunnel_parm old_p; |
| 305 | memset(&old_p, 0, sizeof(old_p)); |
| 306 | if (do_get_ioctl(*argv, &old_p)) |
| 307 | return -1; |
| 308 | *p = old_p; |
| 309 | } |
| 310 | } |
| 311 | count++; |
| 312 | argc--; argv++; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | if (p->iph.protocol == 0) { |
| 317 | if (memcmp(p->name, "gre", 3) == 0) |
| 318 | p->iph.protocol = IPPROTO_GRE; |
| 319 | else if (memcmp(p->name, "ipip", 4) == 0) |
| 320 | p->iph.protocol = IPPROTO_IPIP; |
| 321 | else if (memcmp(p->name, "sit", 3) == 0) |
| 322 | p->iph.protocol = IPPROTO_IPV6; |
| 323 | } |
| 324 | |
| 325 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
| 326 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
| 327 | fprintf(stderr, "Keys are not allowed with ipip and sit.\n"); |
| 328 | return -1; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (medium[0]) { |
| 333 | p->link = do_ioctl_get_ifindex(medium); |
| 334 | if (p->link == 0) |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 339 | p->i_key = p->iph.daddr; |
| 340 | p->i_flags |= GRE_KEY; |
| 341 | } |
| 342 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 343 | p->o_key = p->iph.daddr; |
| 344 | p->o_flags |= GRE_KEY; |
| 345 | } |
| 346 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { |
| 347 | fprintf(stderr, "Broadcast tunnel requires a source address.\n"); |
| 348 | return -1; |
| 349 | } |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static int do_add(int cmd, int argc, char **argv) |
| 355 | { |
| 356 | struct ip_tunnel_parm p; |
| 357 | |
| 358 | if (parse_args(argc, argv, cmd, &p) < 0) |
| 359 | return -1; |
| 360 | |
| 361 | if (p.iph.ttl && p.iph.frag_off == 0) { |
| 362 | fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n"); |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | switch (p.iph.protocol) { |
| 367 | case IPPROTO_IPIP: |
| 368 | return do_add_ioctl(cmd, "tunl0", &p); |
| 369 | case IPPROTO_GRE: |
| 370 | return do_add_ioctl(cmd, "gre0", &p); |
| 371 | case IPPROTO_IPV6: |
| 372 | return do_add_ioctl(cmd, "sit0", &p); |
| 373 | default: |
| 374 | fprintf(stderr, "cannot determine tunnel mode (ipip, gre or sit)\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | int do_del(int argc, char **argv) |
| 381 | { |
| 382 | struct ip_tunnel_parm p; |
| 383 | |
| 384 | if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0) |
| 385 | return -1; |
| 386 | |
| 387 | switch (p.iph.protocol) { |
| 388 | case IPPROTO_IPIP: |
| 389 | return do_del_ioctl("tunl0", &p); |
| 390 | case IPPROTO_GRE: |
| 391 | return do_del_ioctl("gre0", &p); |
| 392 | case IPPROTO_IPV6: |
| 393 | return do_del_ioctl("sit0", &p); |
| 394 | default: |
| 395 | return do_del_ioctl(p.name, &p); |
| 396 | } |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | void print_tunnel(struct ip_tunnel_parm *p) |
| 401 | { |
| 402 | char s1[1024]; |
| 403 | char s2[1024]; |
| 404 | char s3[64]; |
| 405 | char s4[64]; |
| 406 | |
| 407 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); |
| 408 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); |
| 409 | |
| 410 | /* Do not use format_host() for local addr, |
| 411 | * symbolic name will not be useful. |
| 412 | */ |
| 413 | printf("%s: %s/ip remote %s local %s ", |
| 414 | p->name, |
| 415 | p->iph.protocol == IPPROTO_IPIP ? "ip" : |
| 416 | (p->iph.protocol == IPPROTO_GRE ? "gre" : |
| 417 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), |
| 418 | p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any", |
| 419 | p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any"); |
| 420 | |
| 421 | if (p->link) { |
| 422 | char *n = do_ioctl_get_ifname(p->link); |
| 423 | if (n) |
| 424 | printf(" dev %s ", n); |
| 425 | } |
| 426 | |
| 427 | if (p->iph.ttl) |
| 428 | printf(" ttl %d ", p->iph.ttl); |
| 429 | else |
| 430 | printf(" ttl inherit "); |
| 431 | |
| 432 | if (p->iph.tos) { |
| 433 | SPRINT_BUF(b1); |
| 434 | printf(" tos"); |
| 435 | if (p->iph.tos&1) |
| 436 | printf(" inherit"); |
| 437 | if (p->iph.tos&~1) |
| 438 | printf("%c%s ", p->iph.tos&1 ? '/' : ' ', |
| 439 | rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1))); |
| 440 | } |
| 441 | |
| 442 | if (!(p->iph.frag_off&htons(IP_DF))) |
| 443 | printf(" nopmtudisc"); |
| 444 | |
| 445 | if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key) |
| 446 | printf(" key %s", s3); |
| 447 | else if ((p->i_flags|p->o_flags)&GRE_KEY) { |
| 448 | if (p->i_flags&GRE_KEY) |
| 449 | printf(" ikey %s ", s3); |
| 450 | if (p->o_flags&GRE_KEY) |
| 451 | printf(" okey %s ", s4); |
| 452 | } |
| 453 | |
| 454 | if (p->i_flags&GRE_SEQ) |
| 455 | printf("%s Drop packets out of sequence.\n", _SL_); |
| 456 | if (p->i_flags&GRE_CSUM) |
| 457 | printf("%s Checksum in received packet is required.", _SL_); |
| 458 | if (p->o_flags&GRE_SEQ) |
| 459 | printf("%s Sequence packets on output.", _SL_); |
| 460 | if (p->o_flags&GRE_CSUM) |
| 461 | printf("%s Checksum output packets.", _SL_); |
| 462 | } |
| 463 | |
| 464 | static int do_tunnels_list(struct ip_tunnel_parm *p) |
| 465 | { |
| 466 | char name[IFNAMSIZ]; |
| 467 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 468 | rx_fifo, rx_frame, |
| 469 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 470 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
| 471 | int type; |
| 472 | struct ip_tunnel_parm p1; |
| 473 | |
| 474 | char buf[512]; |
| 475 | FILE *fp = fopen("/proc/net/dev", "r"); |
| 476 | if (fp == NULL) { |
| 477 | perror("fopen"); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | fgets(buf, sizeof(buf), fp); |
| 482 | fgets(buf, sizeof(buf), fp); |
| 483 | |
| 484 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 485 | char *ptr; |
| 486 | buf[sizeof(buf) - 1] = 0; |
| 487 | if ((ptr = strchr(buf, ':')) == NULL || |
| 488 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
| 489 | fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n"); |
| 490 | return -1; |
| 491 | } |
| 492 | if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", |
| 493 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 494 | &rx_fifo, &rx_frame, &rx_multi, |
| 495 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 496 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 497 | continue; |
| 498 | if (p->name[0] && strcmp(p->name, name)) |
| 499 | continue; |
| 500 | type = do_ioctl_get_iftype(name); |
| 501 | if (type == -1) { |
| 502 | fprintf(stderr, "Failed to get type of [%s]\n", name); |
| 503 | continue; |
| 504 | } |
| 505 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) |
| 506 | continue; |
| 507 | memset(&p1, 0, sizeof(p1)); |
| 508 | if (do_get_ioctl(name, &p1)) |
| 509 | continue; |
| 510 | if ((p->link && p1.link != p->link) || |
| 511 | (p->name[0] && strcmp(p1.name, p->name)) || |
| 512 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || |
| 513 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || |
| 514 | (p->i_key && p1.i_key != p->i_key)) |
| 515 | continue; |
| 516 | print_tunnel(&p1); |
| 517 | if (show_stats) { |
| 518 | printf("%s", _SL_); |
| 519 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); |
| 520 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", |
| 521 | rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); |
| 522 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); |
| 523 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", |
| 524 | tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); |
| 525 | } |
| 526 | printf("\n"); |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int do_show(int argc, char **argv) |
| 532 | { |
| 533 | int err; |
| 534 | struct ip_tunnel_parm p; |
| 535 | |
| 536 | if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) |
| 537 | return -1; |
| 538 | |
| 539 | switch (p.iph.protocol) { |
| 540 | case IPPROTO_IPIP: |
| 541 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
| 542 | break; |
| 543 | case IPPROTO_GRE: |
| 544 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
| 545 | break; |
| 546 | case IPPROTO_IPV6: |
| 547 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
| 548 | break; |
| 549 | default: |
| 550 | do_tunnels_list(&p); |
| 551 | return 0; |
| 552 | } |
| 553 | if (err) |
| 554 | return -1; |
| 555 | |
| 556 | print_tunnel(&p); |
| 557 | printf("\n"); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int do_iptunnel(int argc, char **argv) |
| 562 | { |
| 563 | if (argc > 0) { |
| 564 | if (matches(*argv, "add") == 0) |
| 565 | return do_add(SIOCADDTUNNEL, argc-1, argv+1); |
| 566 | if (matches(*argv, "change") == 0) |
| 567 | return do_add(SIOCCHGTUNNEL, argc-1, argv+1); |
| 568 | if (matches(*argv, "del") == 0) |
| 569 | return do_del(argc-1, argv+1); |
| 570 | if (matches(*argv, "show") == 0 || |
| 571 | matches(*argv, "lst") == 0 || |
| 572 | matches(*argv, "list") == 0) |
| 573 | return do_show(argc-1, argv+1); |
| 574 | if (matches(*argv, "help") == 0) |
| 575 | usage(); |
| 576 | } else |
| 577 | return do_show(0, NULL); |
| 578 | |
| 579 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv); |
| 580 | exit(-1); |
| 581 | } |