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