"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 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 17 | //#include <sys/socket.h> |
| 18 | //#include <sys/ioctl.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 19 | #include <netinet/ip.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 20 | #include <net/if.h> |
| 21 | #include <net/if_arp.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 22 | #include <asm/types.h> |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 23 | #ifndef __constant_htons |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 24 | #define __constant_htons htons |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 25 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 26 | #include <linux/if_tunnel.h> |
| 27 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 28 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 29 | #include "rt_names.h" |
| 30 | #include "utils.h" |
| 31 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 33 | /* Dies on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | static int do_ioctl_get_ifindex(char *dev) |
| 35 | { |
| 36 | struct ifreq ifr; |
| 37 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 39 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 40 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 41 | if (ioctl(fd, SIOCGIFINDEX, &ifr)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 42 | bb_perror_msg_and_die("SIOCGIFINDEX"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | } |
| 44 | close(fd); |
| 45 | return ifr.ifr_ifindex; |
| 46 | } |
| 47 | |
| 48 | static int do_ioctl_get_iftype(char *dev) |
| 49 | { |
| 50 | struct ifreq ifr; |
| 51 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 53 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 54 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 55 | if (ioctl(fd, SIOCGIFHWADDR, &ifr)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 56 | bb_perror_msg("SIOCGIFHWADDR"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | close(fd); |
| 60 | return ifr.ifr_addr.sa_family; |
| 61 | } |
| 62 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 63 | static char *do_ioctl_get_ifname(int idx) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 64 | { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 65 | struct ifreq ifr; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 67 | |
| 68 | ifr.ifr_ifindex = idx; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 69 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 70 | if (ioctl(fd, SIOCGIFNAME, &ifr)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 71 | bb_perror_msg("SIOCGIFNAME"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | close(fd); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 75 | return xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 78 | static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | { |
| 80 | struct ifreq ifr; |
| 81 | int fd; |
| 82 | int err; |
| 83 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 84 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 86 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 87 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 88 | if (err) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 89 | bb_perror_msg("SIOCGETTUNNEL"); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 90 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | close(fd); |
| 92 | return err; |
| 93 | } |
| 94 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 95 | /* Dies on error, otherwise returns 0 */ |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 96 | static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 97 | { |
| 98 | struct ifreq ifr; |
| 99 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 101 | if (cmd == SIOCCHGTUNNEL && p->name[0]) { |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 102 | strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 103 | } else { |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 104 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 105 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 106 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 107 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 108 | if (ioctl(fd, cmd, &ifr)) { |
| 109 | bb_perror_msg_and_die("ioctl"); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 110 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 111 | close(fd); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 112 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 115 | /* Dies on error, otherwise returns 0 */ |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 116 | static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 117 | { |
| 118 | struct ifreq ifr; |
| 119 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 120 | |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 121 | if (p->name[0]) { |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 122 | strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 123 | } else { |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 124 | strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 125 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | ifr.ifr_ifru.ifru_data = (void*)p; |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 127 | fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 128 | if (ioctl(fd, SIOCDELTUNNEL, &ifr)) { |
| 129 | bb_perror_msg_and_die("SIOCDELTUNNEL"); |
Glenn L McGrath | 8b60244 | 2002-11-28 12:19:51 +0000 | [diff] [blame] | 130 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 131 | close(fd); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 132 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 135 | /* Dies on error */ |
| 136 | static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 137 | { |
| 138 | int count = 0; |
| 139 | char medium[IFNAMSIZ]; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 140 | static const char * const keywords[] = { |
| 141 | "mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip", |
| 142 | "key", "ikey", "okey", "seq", "iseq", "oseq", |
| 143 | "csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc", |
| 144 | "remote", "any", "local", "dev", |
| 145 | "ttl", "inherit", "tos", "dsfield", |
| 146 | "name", NULL |
| 147 | }; |
| 148 | enum { |
| 149 | ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip, |
| 150 | ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq, |
| 151 | ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc, |
| 152 | ARG_remote, ARG_any, ARG_local, ARG_dev, |
| 153 | ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield, |
| 154 | ARG_name |
| 155 | }; |
| 156 | int key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 157 | memset(p, 0, sizeof(*p)); |
| 158 | memset(&medium, 0, sizeof(medium)); |
| 159 | |
| 160 | p->iph.version = 4; |
| 161 | p->iph.ihl = 5; |
| 162 | #ifndef IP_DF |
| 163 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
| 164 | #endif |
| 165 | p->iph.frag_off = htons(IP_DF); |
| 166 | |
| 167 | while (argc > 0) { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 168 | key = index_in_str_array(keywords, *argv); |
| 169 | if (key == ARG_mode) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 170 | NEXT_ARG(); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 171 | key = index_in_str_array(keywords, *argv); |
| 172 | if (key == ARG_ipip || |
| 173 | key == ARG_ip_ip) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 174 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 175 | bb_error_msg_and_die("you managed to ask for more than one tunnel mode"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 176 | } |
| 177 | p->iph.protocol = IPPROTO_IPIP; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 178 | } else if (key == ARG_gre || |
| 179 | key == ARG_gre_ip) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 181 | bb_error_msg_and_die("you managed to ask for more than one tunnel mode"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 182 | } |
| 183 | p->iph.protocol = IPPROTO_GRE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 184 | } else if (key == ARG_sit || |
| 185 | key == ARG_ip6_ip) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 187 | bb_error_msg_and_die("you managed to ask for more than one tunnel mode"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 188 | } |
| 189 | p->iph.protocol = IPPROTO_IPV6; |
| 190 | } else { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 191 | bb_error_msg_and_die("cannot guess tunnel mode"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 192 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 193 | } else if (key == ARG_key) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | unsigned uval; |
| 195 | NEXT_ARG(); |
| 196 | p->i_flags |= GRE_KEY; |
| 197 | p->o_flags |= GRE_KEY; |
| 198 | if (strchr(*argv, '.')) |
| 199 | p->i_key = p->o_key = get_addr32(*argv); |
| 200 | else { |
| 201 | if (get_unsigned(&uval, *argv, 0)<0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 202 | bb_error_msg_and_die("invalid value of \"key\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 203 | } |
| 204 | p->i_key = p->o_key = htonl(uval); |
| 205 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 206 | } else if (key == ARG_ikey) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 214 | bb_error_msg_and_die("invalid value of \"ikey\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | } |
| 216 | p->i_key = htonl(uval); |
| 217 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 218 | } else if (key == ARG_okey) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 226 | bb_error_msg_and_die("invalid value of \"okey\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 227 | } |
| 228 | p->o_key = htonl(uval); |
| 229 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 230 | } else if (key == ARG_seq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 231 | p->i_flags |= GRE_SEQ; |
| 232 | p->o_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 233 | } else if (key == ARG_iseq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 234 | p->i_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 235 | } else if (key == ARG_oseq) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 236 | p->o_flags |= GRE_SEQ; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 237 | } else if (key == ARG_csum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | p->i_flags |= GRE_CSUM; |
| 239 | p->o_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 240 | } else if (key == ARG_icsum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 241 | p->i_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 242 | } else if (key == ARG_ocsum) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | p->o_flags |= GRE_CSUM; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 244 | } else if (key == ARG_nopmtudisc) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | p->iph.frag_off = 0; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 246 | } else if (key == ARG_pmtudisc) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | p->iph.frag_off = htons(IP_DF); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 248 | } else if (key == ARG_remote) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 249 | NEXT_ARG(); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 250 | key = index_in_str_array(keywords, *argv); |
| 251 | if (key == ARG_any) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 252 | p->iph.daddr = get_addr32(*argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 253 | } else if (key == ARG_local) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 254 | NEXT_ARG(); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 255 | key = index_in_str_array(keywords, *argv); |
| 256 | if (key == ARG_any) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | p->iph.saddr = get_addr32(*argv); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 258 | } else if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 259 | NEXT_ARG(); |
| 260 | strncpy(medium, *argv, IFNAMSIZ-1); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 261 | } else if (key == ARG_ttl) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 262 | unsigned uval; |
| 263 | NEXT_ARG(); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 264 | key = index_in_str_array(keywords, *argv); |
| 265 | if (key != ARG_inherit) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 266 | if (get_unsigned(&uval, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 267 | invarg(*argv, "TTL"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 268 | if (uval > 255) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 269 | invarg(*argv, "TTL must be <=255"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | p->iph.ttl = uval; |
| 271 | } |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 272 | } else if (key == ARG_tos || |
| 273 | key == ARG_dsfield) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 274 | uint32_t uval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 275 | NEXT_ARG(); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 276 | key = index_in_str_array(keywords, *argv); |
| 277 | if (key != ARG_inherit) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | if (rtnl_dsfield_a2n(&uval, *argv)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 279 | invarg(*argv, "TOS"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 280 | p->iph.tos = uval; |
| 281 | } else |
| 282 | p->iph.tos = 1; |
| 283 | } else { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 284 | if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 285 | NEXT_ARG(); |
| 286 | } |
| 287 | if (p->name[0]) |
| 288 | duparg2("name", *argv); |
| 289 | strncpy(p->name, *argv, IFNAMSIZ); |
| 290 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
| 291 | struct ip_tunnel_parm old_p; |
| 292 | memset(&old_p, 0, sizeof(old_p)); |
| 293 | if (do_get_ioctl(*argv, &old_p)) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 294 | exit(1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 295 | *p = old_p; |
| 296 | } |
| 297 | } |
| 298 | count++; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 299 | argc--; |
| 300 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 303 | if (p->iph.protocol == 0) { |
| 304 | if (memcmp(p->name, "gre", 3) == 0) |
| 305 | p->iph.protocol = IPPROTO_GRE; |
| 306 | else if (memcmp(p->name, "ipip", 4) == 0) |
| 307 | p->iph.protocol = IPPROTO_IPIP; |
| 308 | else if (memcmp(p->name, "sit", 3) == 0) |
| 309 | p->iph.protocol = IPPROTO_IPV6; |
| 310 | } |
| 311 | |
| 312 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
| 313 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 314 | bb_error_msg_and_die("keys are not allowed with ipip and sit"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | if (medium[0]) { |
| 319 | p->link = do_ioctl_get_ifindex(medium); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 331 | bb_error_msg_and_die("broadcast tunnel requires a source address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 332 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 336 | /* Return value becomes exitcode. It's okay to not return at all */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 337 | static int do_add(int cmd, int argc, char **argv) |
| 338 | { |
| 339 | struct ip_tunnel_parm p; |
| 340 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 341 | parse_args(argc, argv, cmd, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 342 | |
| 343 | if (p.iph.ttl && p.iph.frag_off == 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 344 | bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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: |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 355 | bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 356 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 359 | /* Return value becomes exitcode. It's okay to not return at all */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 360 | static int do_del(int argc, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 361 | { |
| 362 | struct ip_tunnel_parm p; |
| 363 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 364 | parse_args(argc, argv, SIOCDELTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 365 | |
| 366 | switch (p.iph.protocol) { |
| 367 | case IPPROTO_IPIP: |
| 368 | return do_del_ioctl("tunl0", &p); |
| 369 | case IPPROTO_GRE: |
| 370 | return do_del_ioctl("gre0", &p); |
| 371 | case IPPROTO_IPV6: |
| 372 | return do_del_ioctl("sit0", &p); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 373 | default: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 374 | return do_del_ioctl(p.name, &p); |
| 375 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 378 | static void print_tunnel(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 379 | { |
| 380 | char s1[256]; |
| 381 | char s2[256]; |
| 382 | char s3[64]; |
| 383 | char s4[64]; |
| 384 | |
| 385 | format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)); |
| 386 | format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)); |
| 387 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); |
| 388 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); |
| 389 | |
| 390 | printf("%s: %s/ip remote %s local %s ", |
| 391 | p->name, |
| 392 | p->iph.protocol == IPPROTO_IPIP ? "ip" : |
| 393 | (p->iph.protocol == IPPROTO_GRE ? "gre" : |
| 394 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), |
| 395 | p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any"); |
| 396 | if (p->link) { |
| 397 | char *n = do_ioctl_get_ifname(p->link); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 398 | if (n) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 399 | printf(" dev %s ", n); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 400 | free(n); |
| 401 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 402 | } |
| 403 | if (p->iph.ttl) |
| 404 | printf(" ttl %d ", p->iph.ttl); |
| 405 | else |
| 406 | printf(" ttl inherit "); |
| 407 | if (p->iph.tos) { |
| 408 | SPRINT_BUF(b1); |
| 409 | printf(" tos"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 410 | if (p->iph.tos & 1) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 411 | printf(" inherit"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 412 | if (p->iph.tos & ~1) |
| 413 | printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', |
| 414 | rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1))); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 415 | } |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 416 | if (!(p->iph.frag_off & htons(IP_DF))) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 417 | printf(" nopmtudisc"); |
| 418 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 419 | if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 420 | printf(" key %s", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 421 | else if ((p->i_flags | p->o_flags) & GRE_KEY) { |
| 422 | if (p->i_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 423 | printf(" ikey %s ", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 424 | if (p->o_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 425 | printf(" okey %s ", s4); |
| 426 | } |
| 427 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 428 | if (p->i_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 429 | printf("%c Drop packets out of sequence.\n", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 430 | if (p->i_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 431 | printf("%c Checksum in received packet is required.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 432 | if (p->o_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 433 | printf("%c Sequence packets on output.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 434 | if (p->o_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 435 | printf("%c Checksum output packets.", _SL_); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 438 | static void do_tunnels_list(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 439 | { |
| 440 | char name[IFNAMSIZ]; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 441 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 442 | rx_fifo, rx_frame, |
| 443 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 444 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 445 | int type; |
| 446 | struct ip_tunnel_parm p1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 447 | char buf[512]; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 448 | FILE *fp = fopen_or_warn("/proc/net/dev", "r"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 449 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 450 | if (fp == NULL) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 451 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | fgets(buf, sizeof(buf), fp); |
| 455 | fgets(buf, sizeof(buf), fp); |
| 456 | |
| 457 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 458 | char *ptr; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 459 | |
| 460 | /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ |
| 461 | ptr = strchr(buf, ':'); |
| 462 | if (ptr == NULL || |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 463 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 464 | bb_error_msg("wrong format of /proc/net/dev"); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 465 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 477 | bb_error_msg("cannot 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); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 492 | puts(""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 493 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 496 | /* Return value becomes exitcode. It's okay to not return at all */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 497 | static int do_show(int argc, char **argv) |
| 498 | { |
| 499 | int err; |
| 500 | struct ip_tunnel_parm p; |
| 501 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 502 | parse_args(argc, argv, SIOCGETTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 503 | |
| 504 | switch (p.iph.protocol) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 505 | case IPPROTO_IPIP: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 506 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
| 507 | break; |
| 508 | case IPPROTO_GRE: |
| 509 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
| 510 | break; |
| 511 | case IPPROTO_IPV6: |
| 512 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
| 513 | break; |
| 514 | default: |
| 515 | do_tunnels_list(&p); |
| 516 | return 0; |
| 517 | } |
| 518 | if (err) |
| 519 | return -1; |
| 520 | |
| 521 | print_tunnel(&p); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 522 | puts(""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 526 | /* Return value becomes exitcode. It's okay to not return at all */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 527 | int do_iptunnel(int argc, char **argv) |
| 528 | { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 529 | static const char * const keywords[] = { |
| 530 | "add", "change", "delete", "show", "list", "lst", NULL |
| 531 | }; |
| 532 | enum {ARG_add = 1, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst}; |
| 533 | smalluint key = 4; /* show */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 534 | if (argc > 0) { |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 535 | key = index_in_substr_array(keywords, *argv) +1; |
| 536 | --argc; |
| 537 | ++argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 538 | } else |
| 539 | return do_show(0, NULL); |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 540 | if (key < ARG_add) |
| 541 | bail: |
| 542 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 543 | |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame^] | 544 | if (key == ARG_add) |
| 545 | return do_add(SIOCADDTUNNEL, argc, argv); |
| 546 | if (key == ARG_change) |
| 547 | return do_add(SIOCCHGTUNNEL, argc, argv); |
| 548 | if (key == ARG_del) |
| 549 | return do_del(argc, argv); |
| 550 | if (key == ARG_show || key == ARG_list || key == ARG_lst) |
| 551 | return do_show(argc, argv); |
| 552 | /* be gentle to gcc; avoid warning about non returning */ |
| 553 | goto bail; /* never reached */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 554 | } |