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