"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]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | memset(p, 0, sizeof(*p)); |
| 141 | memset(&medium, 0, sizeof(medium)); |
| 142 | |
| 143 | p->iph.version = 4; |
| 144 | p->iph.ihl = 5; |
| 145 | #ifndef IP_DF |
| 146 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
| 147 | #endif |
| 148 | p->iph.frag_off = htons(IP_DF); |
| 149 | |
| 150 | while (argc > 0) { |
| 151 | if (strcmp(*argv, "mode") == 0) { |
| 152 | NEXT_ARG(); |
| 153 | if (strcmp(*argv, "ipip") == 0 || |
| 154 | strcmp(*argv, "ip/ip") == 0) { |
| 155 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 156 | 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] | 157 | } |
| 158 | p->iph.protocol = IPPROTO_IPIP; |
| 159 | } else if (strcmp(*argv, "gre") == 0 || |
| 160 | strcmp(*argv, "gre/ip") == 0) { |
| 161 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 162 | 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] | 163 | } |
| 164 | p->iph.protocol = IPPROTO_GRE; |
| 165 | } else if (strcmp(*argv, "sit") == 0 || |
| 166 | strcmp(*argv, "ipv6/ip") == 0) { |
| 167 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
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_IPV6; |
| 171 | } else { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 172 | bb_error_msg_and_die("cannot guess tunnel mode"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 173 | } |
| 174 | } else if (strcmp(*argv, "key") == 0) { |
| 175 | unsigned uval; |
| 176 | NEXT_ARG(); |
| 177 | p->i_flags |= GRE_KEY; |
| 178 | p->o_flags |= GRE_KEY; |
| 179 | if (strchr(*argv, '.')) |
| 180 | p->i_key = p->o_key = get_addr32(*argv); |
| 181 | else { |
| 182 | if (get_unsigned(&uval, *argv, 0)<0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 183 | bb_error_msg_and_die("invalid value of \"key\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | } |
| 185 | p->i_key = p->o_key = htonl(uval); |
| 186 | } |
| 187 | } else if (strcmp(*argv, "ikey") == 0) { |
| 188 | unsigned uval; |
| 189 | NEXT_ARG(); |
| 190 | p->i_flags |= GRE_KEY; |
| 191 | if (strchr(*argv, '.')) |
| 192 | p->o_key = get_addr32(*argv); |
| 193 | else { |
| 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 \"ikey\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 196 | } |
| 197 | p->i_key = htonl(uval); |
| 198 | } |
| 199 | } else if (strcmp(*argv, "okey") == 0) { |
| 200 | unsigned uval; |
| 201 | NEXT_ARG(); |
| 202 | p->o_flags |= GRE_KEY; |
| 203 | if (strchr(*argv, '.')) |
| 204 | p->o_key = get_addr32(*argv); |
| 205 | else { |
| 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 \"okey\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 208 | } |
| 209 | p->o_key = htonl(uval); |
| 210 | } |
| 211 | } else if (strcmp(*argv, "seq") == 0) { |
| 212 | p->i_flags |= GRE_SEQ; |
| 213 | p->o_flags |= GRE_SEQ; |
| 214 | } else if (strcmp(*argv, "iseq") == 0) { |
| 215 | p->i_flags |= GRE_SEQ; |
| 216 | } else if (strcmp(*argv, "oseq") == 0) { |
| 217 | p->o_flags |= GRE_SEQ; |
| 218 | } else if (strcmp(*argv, "csum") == 0) { |
| 219 | p->i_flags |= GRE_CSUM; |
| 220 | p->o_flags |= GRE_CSUM; |
| 221 | } else if (strcmp(*argv, "icsum") == 0) { |
| 222 | p->i_flags |= GRE_CSUM; |
| 223 | } else if (strcmp(*argv, "ocsum") == 0) { |
| 224 | p->o_flags |= GRE_CSUM; |
| 225 | } else if (strcmp(*argv, "nopmtudisc") == 0) { |
| 226 | p->iph.frag_off = 0; |
| 227 | } else if (strcmp(*argv, "pmtudisc") == 0) { |
| 228 | p->iph.frag_off = htons(IP_DF); |
| 229 | } else if (strcmp(*argv, "remote") == 0) { |
| 230 | NEXT_ARG(); |
| 231 | if (strcmp(*argv, "any")) |
| 232 | p->iph.daddr = get_addr32(*argv); |
| 233 | } else if (strcmp(*argv, "local") == 0) { |
| 234 | NEXT_ARG(); |
| 235 | if (strcmp(*argv, "any")) |
| 236 | p->iph.saddr = get_addr32(*argv); |
| 237 | } else if (strcmp(*argv, "dev") == 0) { |
| 238 | NEXT_ARG(); |
| 239 | strncpy(medium, *argv, IFNAMSIZ-1); |
| 240 | } else if (strcmp(*argv, "ttl") == 0) { |
| 241 | unsigned uval; |
| 242 | NEXT_ARG(); |
| 243 | if (strcmp(*argv, "inherit") != 0) { |
| 244 | if (get_unsigned(&uval, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 245 | invarg(*argv, "TTL"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 246 | if (uval > 255) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 247 | invarg(*argv, "TTL must be <=255"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 248 | p->iph.ttl = uval; |
| 249 | } |
| 250 | } else if (strcmp(*argv, "tos") == 0 || |
| 251 | matches(*argv, "dsfield") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 252 | uint32_t uval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 253 | NEXT_ARG(); |
| 254 | if (strcmp(*argv, "inherit") != 0) { |
| 255 | if (rtnl_dsfield_a2n(&uval, *argv)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 256 | invarg(*argv, "TOS"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | p->iph.tos = uval; |
| 258 | } else |
| 259 | p->iph.tos = 1; |
| 260 | } else { |
| 261 | if (strcmp(*argv, "name") == 0) { |
| 262 | NEXT_ARG(); |
| 263 | } |
| 264 | if (p->name[0]) |
| 265 | duparg2("name", *argv); |
| 266 | strncpy(p->name, *argv, IFNAMSIZ); |
| 267 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
| 268 | struct ip_tunnel_parm old_p; |
| 269 | memset(&old_p, 0, sizeof(old_p)); |
| 270 | if (do_get_ioctl(*argv, &old_p)) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 271 | exit(1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | *p = old_p; |
| 273 | } |
| 274 | } |
| 275 | count++; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 276 | argc--; |
| 277 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 280 | if (p->iph.protocol == 0) { |
| 281 | if (memcmp(p->name, "gre", 3) == 0) |
| 282 | p->iph.protocol = IPPROTO_GRE; |
| 283 | else if (memcmp(p->name, "ipip", 4) == 0) |
| 284 | p->iph.protocol = IPPROTO_IPIP; |
| 285 | else if (memcmp(p->name, "sit", 3) == 0) |
| 286 | p->iph.protocol = IPPROTO_IPV6; |
| 287 | } |
| 288 | |
| 289 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
| 290 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 291 | 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] | 292 | } |
| 293 | } |
| 294 | |
| 295 | if (medium[0]) { |
| 296 | p->link = do_ioctl_get_ifindex(medium); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 300 | p->i_key = p->iph.daddr; |
| 301 | p->i_flags |= GRE_KEY; |
| 302 | } |
| 303 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 304 | p->o_key = p->iph.daddr; |
| 305 | p->o_flags |= GRE_KEY; |
| 306 | } |
| 307 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 308 | bb_error_msg_and_die("broadcast tunnel requires a source address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 309 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 313 | /* 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] | 314 | static int do_add(int cmd, int argc, char **argv) |
| 315 | { |
| 316 | struct ip_tunnel_parm p; |
| 317 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 318 | parse_args(argc, argv, cmd, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 319 | |
| 320 | if (p.iph.ttl && p.iph.frag_off == 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 321 | bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | switch (p.iph.protocol) { |
| 325 | case IPPROTO_IPIP: |
| 326 | return do_add_ioctl(cmd, "tunl0", &p); |
| 327 | case IPPROTO_GRE: |
| 328 | return do_add_ioctl(cmd, "gre0", &p); |
| 329 | case IPPROTO_IPV6: |
| 330 | return do_add_ioctl(cmd, "sit0", &p); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 331 | default: |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 332 | 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] | 333 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 337 | static int do_del(int argc, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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, SIOCDELTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 342 | |
| 343 | switch (p.iph.protocol) { |
| 344 | case IPPROTO_IPIP: |
| 345 | return do_del_ioctl("tunl0", &p); |
| 346 | case IPPROTO_GRE: |
| 347 | return do_del_ioctl("gre0", &p); |
| 348 | case IPPROTO_IPV6: |
| 349 | return do_del_ioctl("sit0", &p); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 350 | default: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 351 | return do_del_ioctl(p.name, &p); |
| 352 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 355 | static void print_tunnel(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 356 | { |
| 357 | char s1[256]; |
| 358 | char s2[256]; |
| 359 | char s3[64]; |
| 360 | char s4[64]; |
| 361 | |
| 362 | format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)); |
| 363 | format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)); |
| 364 | inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3)); |
| 365 | inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4)); |
| 366 | |
| 367 | printf("%s: %s/ip remote %s local %s ", |
| 368 | p->name, |
| 369 | p->iph.protocol == IPPROTO_IPIP ? "ip" : |
| 370 | (p->iph.protocol == IPPROTO_GRE ? "gre" : |
| 371 | (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")), |
| 372 | p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any"); |
| 373 | if (p->link) { |
| 374 | char *n = do_ioctl_get_ifname(p->link); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 375 | if (n) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 376 | printf(" dev %s ", n); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 377 | free(n); |
| 378 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 379 | } |
| 380 | if (p->iph.ttl) |
| 381 | printf(" ttl %d ", p->iph.ttl); |
| 382 | else |
| 383 | printf(" ttl inherit "); |
| 384 | if (p->iph.tos) { |
| 385 | SPRINT_BUF(b1); |
| 386 | printf(" tos"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 387 | if (p->iph.tos & 1) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 388 | printf(" inherit"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 389 | if (p->iph.tos & ~1) |
| 390 | printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', |
| 391 | rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1))); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 392 | } |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 393 | if (!(p->iph.frag_off & htons(IP_DF))) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 394 | printf(" nopmtudisc"); |
| 395 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 396 | 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] | 397 | printf(" key %s", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 398 | else if ((p->i_flags | p->o_flags) & GRE_KEY) { |
| 399 | if (p->i_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 400 | printf(" ikey %s ", s3); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 401 | if (p->o_flags & GRE_KEY) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 402 | printf(" okey %s ", s4); |
| 403 | } |
| 404 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 405 | if (p->i_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 406 | printf("%c Drop packets out of sequence.\n", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 407 | if (p->i_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 408 | printf("%c Checksum in received packet is required.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 409 | if (p->o_flags & GRE_SEQ) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 410 | printf("%c Sequence packets on output.", _SL_); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 411 | if (p->o_flags & GRE_CSUM) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 412 | printf("%c Checksum output packets.", _SL_); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 415 | static void do_tunnels_list(struct ip_tunnel_parm *p) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 416 | { |
| 417 | char name[IFNAMSIZ]; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 418 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 419 | rx_fifo, rx_frame, |
| 420 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 421 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 422 | int type; |
| 423 | struct ip_tunnel_parm p1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 424 | char buf[512]; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 425 | FILE *fp = fopen_or_warn("/proc/net/dev", "r"); |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 426 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 427 | if (fp == NULL) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 428 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | fgets(buf, sizeof(buf), fp); |
| 432 | fgets(buf, sizeof(buf), fp); |
| 433 | |
| 434 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 435 | char *ptr; |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 436 | |
| 437 | /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ |
| 438 | ptr = strchr(buf, ':'); |
| 439 | if (ptr == NULL || |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 440 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 441 | bb_error_msg("wrong format of /proc/net/dev. Sorry"); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 442 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 443 | } |
Bernhard Reutner-Fischer | eedd1be | 2006-01-12 13:15:49 +0000 | [diff] [blame] | 444 | 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] | 445 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 446 | &rx_fifo, &rx_frame, &rx_multi, |
| 447 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 448 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 449 | continue; |
| 450 | if (p->name[0] && strcmp(p->name, name)) |
| 451 | continue; |
| 452 | type = do_ioctl_get_iftype(name); |
| 453 | if (type == -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 454 | bb_error_msg("cannot get type of [%s]", name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) |
| 458 | continue; |
| 459 | memset(&p1, 0, sizeof(p1)); |
| 460 | if (do_get_ioctl(name, &p1)) |
| 461 | continue; |
| 462 | if ((p->link && p1.link != p->link) || |
| 463 | (p->name[0] && strcmp(p1.name, p->name)) || |
| 464 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || |
| 465 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || |
| 466 | (p->i_key && p1.i_key != p->i_key)) |
| 467 | continue; |
| 468 | print_tunnel(&p1); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 469 | puts(""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 470 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 473 | /* 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] | 474 | static int do_show(int argc, char **argv) |
| 475 | { |
| 476 | int err; |
| 477 | struct ip_tunnel_parm p; |
| 478 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 479 | parse_args(argc, argv, SIOCGETTUNNEL, &p); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 480 | |
| 481 | switch (p.iph.protocol) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 482 | case IPPROTO_IPIP: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 483 | err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
| 484 | break; |
| 485 | case IPPROTO_GRE: |
| 486 | err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
| 487 | break; |
| 488 | case IPPROTO_IPV6: |
| 489 | err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
| 490 | break; |
| 491 | default: |
| 492 | do_tunnels_list(&p); |
| 493 | return 0; |
| 494 | } |
| 495 | if (err) |
| 496 | return -1; |
| 497 | |
| 498 | print_tunnel(&p); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 499 | puts(""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 503 | /* 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] | 504 | int do_iptunnel(int argc, char **argv) |
| 505 | { |
| 506 | if (argc > 0) { |
| 507 | if (matches(*argv, "add") == 0) |
| 508 | return do_add(SIOCADDTUNNEL, argc-1, argv+1); |
| 509 | if (matches(*argv, "change") == 0) |
| 510 | return do_add(SIOCCHGTUNNEL, argc-1, argv+1); |
| 511 | if (matches(*argv, "del") == 0) |
| 512 | return do_del(argc-1, argv+1); |
| 513 | if (matches(*argv, "show") == 0 || |
| 514 | matches(*argv, "lst") == 0 || |
| 515 | matches(*argv, "list") == 0) |
| 516 | return do_show(argc-1, argv+1); |
| 517 | } else |
| 518 | return do_show(0, NULL); |
| 519 | |
Denis Vlasenko | ace35ee | 2007-01-02 16:32:16 +0000 | [diff] [blame] | 520 | bb_error_msg_and_die("command \"%s\" is unknown", *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 521 | } |