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