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