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