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