osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * iptunnel.c "ip tunnel" |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <unistd.h> |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 17 | #include <sys/types.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 18 | #include <sys/socket.h> |
osdl.org!shemminger | 7272ddc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 19 | #include <arpa/inet.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | #include <sys/ioctl.h> |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 21 | #include <net/if.h> |
| 22 | #include <net/if_arp.h> |
osdl.org!shemminger | 7272ddc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 23 | #include <linux/ip.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 24 | #include <linux/if_tunnel.h> |
| 25 | |
| 26 | #include "rt_names.h" |
| 27 | #include "utils.h" |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 28 | #include "ip_common.h" |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 29 | #include "tunnel.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 30 | |
| 31 | static void usage(void) __attribute__((noreturn)); |
| 32 | |
| 33 | static void usage(void) |
| 34 | { |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 35 | fprintf(stderr, "Usage: ip tunnel { add | change | del | show | prl | 6rd } [ NAME ]\n"); |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 36 | fprintf(stderr, " [ mode { ipip | gre | sit | isatap | vti } ] [ remote ADDR ] [ local ADDR ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 37 | fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 38 | fprintf(stderr, " [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]\n"); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 39 | fprintf(stderr, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 40 | fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n"); |
| 41 | fprintf(stderr, "\n"); |
| 42 | fprintf(stderr, "Where: NAME := STRING\n"); |
| 43 | fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n"); |
David Ward | e59fd3d | 2013-01-27 13:04:59 +0000 | [diff] [blame^] | 44 | fprintf(stderr, " TOS := { STRING | 00..ff | inherit | inherit/STRING | inherit/00..ff }\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | fprintf(stderr, " TTL := { 1..255 | inherit }\n"); |
| 46 | fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
| 47 | exit(-1); |
| 48 | } |
| 49 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) |
| 51 | { |
| 52 | int count = 0; |
| 53 | char medium[IFNAMSIZ]; |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 54 | int isatap = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | |
| 56 | memset(p, 0, sizeof(*p)); |
| 57 | memset(&medium, 0, sizeof(medium)); |
| 58 | |
| 59 | p->iph.version = 4; |
| 60 | p->iph.ihl = 5; |
| 61 | #ifndef IP_DF |
| 62 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ |
| 63 | #endif |
| 64 | p->iph.frag_off = htons(IP_DF); |
| 65 | |
| 66 | while (argc > 0) { |
| 67 | if (strcmp(*argv, "mode") == 0) { |
| 68 | NEXT_ARG(); |
| 69 | if (strcmp(*argv, "ipip") == 0 || |
| 70 | strcmp(*argv, "ip/ip") == 0) { |
| 71 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
| 72 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 73 | exit(-1); |
| 74 | } |
| 75 | p->iph.protocol = IPPROTO_IPIP; |
| 76 | } else if (strcmp(*argv, "gre") == 0 || |
| 77 | strcmp(*argv, "gre/ip") == 0) { |
| 78 | if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { |
| 79 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 80 | exit(-1); |
| 81 | } |
| 82 | p->iph.protocol = IPPROTO_GRE; |
| 83 | } else if (strcmp(*argv, "sit") == 0 || |
| 84 | strcmp(*argv, "ipv6/ip") == 0) { |
| 85 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
| 86 | fprintf(stderr,"You managed to ask for more than one tunnel mode.\n"); |
| 87 | exit(-1); |
| 88 | } |
| 89 | p->iph.protocol = IPPROTO_IPV6; |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 90 | } else if (strcmp(*argv, "isatap") == 0) { |
| 91 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { |
| 92 | fprintf(stderr, "You managed to ask for more than one tunnel mode.\n"); |
| 93 | exit(-1); |
| 94 | } |
| 95 | p->iph.protocol = IPPROTO_IPV6; |
| 96 | isatap++; |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 97 | } else if (strcmp(*argv, "vti") == 0) { |
| 98 | if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { |
| 99 | fprintf(stderr, "You managed to ask for more than one tunnel mode.\n"); |
| 100 | exit(-1); |
| 101 | } |
| 102 | p->iph.protocol = IPPROTO_IPIP; |
| 103 | p->i_flags |= VTI_ISVTI; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 104 | } else { |
| 105 | fprintf(stderr,"Cannot guess tunnel mode.\n"); |
| 106 | exit(-1); |
| 107 | } |
| 108 | } else if (strcmp(*argv, "key") == 0) { |
| 109 | unsigned uval; |
| 110 | NEXT_ARG(); |
| 111 | p->i_flags |= GRE_KEY; |
| 112 | p->o_flags |= GRE_KEY; |
| 113 | if (strchr(*argv, '.')) |
| 114 | p->i_key = p->o_key = get_addr32(*argv); |
| 115 | else { |
| 116 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 117 | fprintf(stderr, "invalid value of \"key\"\n"); |
| 118 | exit(-1); |
| 119 | } |
| 120 | p->i_key = p->o_key = htonl(uval); |
| 121 | } |
| 122 | } else if (strcmp(*argv, "ikey") == 0) { |
| 123 | unsigned uval; |
| 124 | NEXT_ARG(); |
| 125 | p->i_flags |= GRE_KEY; |
| 126 | if (strchr(*argv, '.')) |
Herbert Xu | 4282c6c | 2007-10-12 10:56:40 +0200 | [diff] [blame] | 127 | p->i_key = get_addr32(*argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 128 | else { |
| 129 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 130 | fprintf(stderr, "invalid value of \"ikey\"\n"); |
| 131 | exit(-1); |
| 132 | } |
| 133 | p->i_key = htonl(uval); |
| 134 | } |
| 135 | } else if (strcmp(*argv, "okey") == 0) { |
| 136 | unsigned uval; |
| 137 | NEXT_ARG(); |
| 138 | p->o_flags |= GRE_KEY; |
| 139 | if (strchr(*argv, '.')) |
| 140 | p->o_key = get_addr32(*argv); |
| 141 | else { |
| 142 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 143 | fprintf(stderr, "invalid value of \"okey\"\n"); |
| 144 | exit(-1); |
| 145 | } |
| 146 | p->o_key = htonl(uval); |
| 147 | } |
| 148 | } else if (strcmp(*argv, "seq") == 0) { |
| 149 | p->i_flags |= GRE_SEQ; |
| 150 | p->o_flags |= GRE_SEQ; |
| 151 | } else if (strcmp(*argv, "iseq") == 0) { |
| 152 | p->i_flags |= GRE_SEQ; |
| 153 | } else if (strcmp(*argv, "oseq") == 0) { |
| 154 | p->o_flags |= GRE_SEQ; |
| 155 | } else if (strcmp(*argv, "csum") == 0) { |
| 156 | p->i_flags |= GRE_CSUM; |
| 157 | p->o_flags |= GRE_CSUM; |
| 158 | } else if (strcmp(*argv, "icsum") == 0) { |
| 159 | p->i_flags |= GRE_CSUM; |
| 160 | } else if (strcmp(*argv, "ocsum") == 0) { |
| 161 | p->o_flags |= GRE_CSUM; |
| 162 | } else if (strcmp(*argv, "nopmtudisc") == 0) { |
| 163 | p->iph.frag_off = 0; |
| 164 | } else if (strcmp(*argv, "pmtudisc") == 0) { |
| 165 | p->iph.frag_off = htons(IP_DF); |
| 166 | } else if (strcmp(*argv, "remote") == 0) { |
| 167 | NEXT_ARG(); |
| 168 | if (strcmp(*argv, "any")) |
| 169 | p->iph.daddr = get_addr32(*argv); |
| 170 | } else if (strcmp(*argv, "local") == 0) { |
| 171 | NEXT_ARG(); |
| 172 | if (strcmp(*argv, "any")) |
| 173 | p->iph.saddr = get_addr32(*argv); |
| 174 | } else if (strcmp(*argv, "dev") == 0) { |
| 175 | NEXT_ARG(); |
| 176 | strncpy(medium, *argv, IFNAMSIZ-1); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 177 | } else if (strcmp(*argv, "ttl") == 0 || |
| 178 | strcmp(*argv, "hoplimit") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 179 | unsigned uval; |
| 180 | NEXT_ARG(); |
| 181 | if (strcmp(*argv, "inherit") != 0) { |
| 182 | if (get_unsigned(&uval, *argv, 0)) |
| 183 | invarg("invalid TTL\n", *argv); |
| 184 | if (uval > 255) |
| 185 | invarg("TTL must be <=255\n", *argv); |
| 186 | p->iph.ttl = uval; |
| 187 | } |
| 188 | } else if (strcmp(*argv, "tos") == 0 || |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 189 | strcmp(*argv, "tclass") == 0 || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 190 | matches(*argv, "dsfield") == 0) { |
David Ward | e59fd3d | 2013-01-27 13:04:59 +0000 | [diff] [blame^] | 191 | char *dsfield; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 192 | __u32 uval; |
| 193 | NEXT_ARG(); |
David Ward | e59fd3d | 2013-01-27 13:04:59 +0000 | [diff] [blame^] | 194 | dsfield = *argv; |
| 195 | strsep(&dsfield, "/"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 196 | if (strcmp(*argv, "inherit") != 0) { |
David Ward | e59fd3d | 2013-01-27 13:04:59 +0000 | [diff] [blame^] | 197 | dsfield = *argv; |
| 198 | p->iph.tos = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | } else |
| 200 | p->iph.tos = 1; |
David Ward | e59fd3d | 2013-01-27 13:04:59 +0000 | [diff] [blame^] | 201 | if (dsfield) { |
| 202 | if (rtnl_dsfield_a2n(&uval, dsfield)) |
| 203 | invarg("bad TOS value", *argv); |
| 204 | p->iph.tos |= uval; |
| 205 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | } else { |
| 207 | if (strcmp(*argv, "name") == 0) { |
| 208 | NEXT_ARG(); |
Andreas Henriksson | 1f1ae52 | 2008-09-18 10:55:03 -0700 | [diff] [blame] | 209 | } else if (matches(*argv, "help") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 210 | usage(); |
| 211 | if (p->name[0]) |
| 212 | duparg2("name", *argv); |
| 213 | strncpy(p->name, *argv, IFNAMSIZ); |
| 214 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
| 215 | struct ip_tunnel_parm old_p; |
| 216 | memset(&old_p, 0, sizeof(old_p)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 217 | if (tnl_get_ioctl(*argv, &old_p)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | return -1; |
| 219 | *p = old_p; |
| 220 | } |
| 221 | } |
| 222 | count++; |
| 223 | argc--; argv++; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | if (p->iph.protocol == 0) { |
| 228 | if (memcmp(p->name, "gre", 3) == 0) |
| 229 | p->iph.protocol = IPPROTO_GRE; |
| 230 | else if (memcmp(p->name, "ipip", 4) == 0) |
| 231 | p->iph.protocol = IPPROTO_IPIP; |
| 232 | else if (memcmp(p->name, "sit", 3) == 0) |
| 233 | p->iph.protocol = IPPROTO_IPV6; |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 234 | else if (memcmp(p->name, "isatap", 6) == 0) { |
| 235 | p->iph.protocol = IPPROTO_IPV6; |
| 236 | isatap++; |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 237 | } else if (memcmp(p->name, "vti", 3) == 0) { |
| 238 | p->iph.protocol = IPPROTO_IPIP; |
| 239 | p->i_flags |= VTI_ISVTI; |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 240 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { |
| 244 | if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { |
| 245 | fprintf(stderr, "Keys are not allowed with ipip and sit.\n"); |
| 246 | return -1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (medium[0]) { |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 251 | p->link = if_nametoindex(medium); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 252 | if (p->link == 0) |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 257 | p->i_key = p->iph.daddr; |
| 258 | p->i_flags |= GRE_KEY; |
| 259 | } |
| 260 | if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { |
| 261 | p->o_key = p->iph.daddr; |
| 262 | p->o_flags |= GRE_KEY; |
| 263 | } |
| 264 | if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { |
| 265 | fprintf(stderr, "Broadcast tunnel requires a source address.\n"); |
| 266 | return -1; |
| 267 | } |
Sascha Hlusiak | eeef12c | 2009-03-27 11:14:00 -0700 | [diff] [blame] | 268 | if (isatap) |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 269 | p->i_flags |= SIT_ISATAP; |
Templin, Fred L | 0bd1792 | 2007-11-26 09:46:06 -0800 | [diff] [blame] | 270 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static int do_add(int cmd, int argc, char **argv) |
| 276 | { |
| 277 | struct ip_tunnel_parm p; |
| 278 | |
| 279 | if (parse_args(argc, argv, cmd, &p) < 0) |
| 280 | return -1; |
| 281 | |
| 282 | if (p.iph.ttl && p.iph.frag_off == 0) { |
| 283 | fprintf(stderr, "ttl != 0 and noptmudisc are incompatible\n"); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | switch (p.iph.protocol) { |
| 288 | case IPPROTO_IPIP: |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 289 | if (p.i_flags != VTI_ISVTI) |
| 290 | return tnl_add_ioctl(cmd, "tunl0", p.name, &p); |
| 291 | else |
| 292 | return tnl_add_ioctl(cmd, "ip_vti0", p.name, &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 293 | case IPPROTO_GRE: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 294 | return tnl_add_ioctl(cmd, "gre0", p.name, &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 295 | case IPPROTO_IPV6: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 296 | return tnl_add_ioctl(cmd, "sit0", p.name, &p); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 297 | default: |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 298 | fprintf(stderr, "cannot determine tunnel mode (ipip, gre, vti or sit)\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 299 | return -1; |
| 300 | } |
| 301 | return -1; |
| 302 | } |
| 303 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 304 | static int do_del(int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 305 | { |
| 306 | struct ip_tunnel_parm p; |
| 307 | |
| 308 | if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0) |
| 309 | return -1; |
| 310 | |
| 311 | switch (p.iph.protocol) { |
| 312 | case IPPROTO_IPIP: |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 313 | if (p.i_flags != VTI_ISVTI) |
| 314 | return tnl_del_ioctl("tunl0", p.name, &p); |
| 315 | else |
| 316 | return tnl_del_ioctl("ip_vti0", p.name, &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 317 | case IPPROTO_GRE: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 318 | return tnl_del_ioctl("gre0", p.name, &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 319 | case IPPROTO_IPV6: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 320 | return tnl_del_ioctl("sit0", p.name, &p); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 321 | default: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 322 | return tnl_del_ioctl(p.name, p.name, &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 323 | } |
| 324 | return -1; |
| 325 | } |
| 326 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 327 | static void print_tunnel(struct ip_tunnel_parm *p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 328 | { |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 329 | struct ip_tunnel_6rd ip6rd; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 330 | char s1[1024]; |
| 331 | char s2[1024]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 332 | |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 333 | memset(&ip6rd, 0, sizeof(ip6rd)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 334 | |
| 335 | /* Do not use format_host() for local addr, |
| 336 | * symbolic name will not be useful. |
| 337 | */ |
| 338 | printf("%s: %s/ip remote %s local %s ", |
| 339 | p->name, |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 340 | tnl_strproto(p->iph.protocol), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 341 | p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any", |
| 342 | p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any"); |
| 343 | |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 344 | if (p->i_flags & SIT_ISATAP) { |
| 345 | struct ip_tunnel_prl prl[16]; |
| 346 | int i; |
| 347 | |
| 348 | memset(prl, 0, sizeof(prl)); |
| 349 | prl[0].datalen = sizeof(prl) - sizeof(prl[0]); |
| 350 | prl[0].addr = htonl(INADDR_ANY); |
| 351 | |
| 352 | if (!tnl_prl_ioctl(SIOCGETPRL, p->name, prl)) |
| 353 | for (i = 1; i < sizeof(prl) / sizeof(prl[0]); i++) |
| 354 | { |
| 355 | if (prl[i].addr != htonl(INADDR_ANY)) { |
| 356 | printf(" %s %s ", |
| 357 | (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr", |
| 358 | format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1))); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 363 | if (p->link) { |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 364 | const char *n = ll_index_to_name(p->link); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 365 | if (n) |
| 366 | printf(" dev %s ", n); |
| 367 | } |
| 368 | |
| 369 | if (p->iph.ttl) |
| 370 | printf(" ttl %d ", p->iph.ttl); |
| 371 | else |
| 372 | printf(" ttl inherit "); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 373 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 374 | if (p->iph.tos) { |
| 375 | SPRINT_BUF(b1); |
| 376 | printf(" tos"); |
| 377 | if (p->iph.tos&1) |
| 378 | printf(" inherit"); |
| 379 | if (p->iph.tos&~1) |
| 380 | printf("%c%s ", p->iph.tos&1 ? '/' : ' ', |
| 381 | rtnl_dsfield_n2a(p->iph.tos&~1, b1, sizeof(b1))); |
| 382 | } |
| 383 | |
| 384 | if (!(p->iph.frag_off&htons(IP_DF))) |
| 385 | printf(" nopmtudisc"); |
| 386 | |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 387 | if (p->iph.protocol == IPPROTO_IPV6 && !tnl_ioctl_get_6rd(p->name, &ip6rd) && ip6rd.prefixlen) { |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 388 | printf(" 6rd-prefix %s/%u ", |
| 389 | inet_ntop(AF_INET6, &ip6rd.prefix, s1, sizeof(s1)), |
| 390 | ip6rd.prefixlen); |
| 391 | if (ip6rd.relay_prefix) { |
| 392 | printf("6rd-relay_prefix %s/%u ", |
| 393 | format_host(AF_INET, 4, &ip6rd.relay_prefix, s1, sizeof(s1)), |
| 394 | ip6rd.relay_prefixlen); |
| 395 | } |
| 396 | } |
| 397 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 398 | if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key) |
Timo Teräs | 6f4f7c4 | 2010-11-23 22:18:57 +0000 | [diff] [blame] | 399 | printf(" key %u", ntohl(p->i_key)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 400 | else if ((p->i_flags|p->o_flags)&GRE_KEY) { |
| 401 | if (p->i_flags&GRE_KEY) |
Timo Teräs | 6f4f7c4 | 2010-11-23 22:18:57 +0000 | [diff] [blame] | 402 | printf(" ikey %u ", ntohl(p->i_key)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 403 | if (p->o_flags&GRE_KEY) |
Timo Teräs | 6f4f7c4 | 2010-11-23 22:18:57 +0000 | [diff] [blame] | 404 | printf(" okey %u ", ntohl(p->o_key)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (p->i_flags&GRE_SEQ) |
| 408 | printf("%s Drop packets out of sequence.\n", _SL_); |
| 409 | if (p->i_flags&GRE_CSUM) |
| 410 | printf("%s Checksum in received packet is required.", _SL_); |
| 411 | if (p->o_flags&GRE_SEQ) |
| 412 | printf("%s Sequence packets on output.", _SL_); |
| 413 | if (p->o_flags&GRE_CSUM) |
| 414 | printf("%s Checksum output packets.", _SL_); |
| 415 | } |
| 416 | |
| 417 | static int do_tunnels_list(struct ip_tunnel_parm *p) |
| 418 | { |
| 419 | char name[IFNAMSIZ]; |
| 420 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 421 | rx_fifo, rx_frame, |
| 422 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 423 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 424 | struct ip_tunnel_parm p1; |
| 425 | |
| 426 | char buf[512]; |
| 427 | FILE *fp = fopen("/proc/net/dev", "r"); |
| 428 | if (fp == NULL) { |
| 429 | perror("fopen"); |
| 430 | return -1; |
| 431 | } |
| 432 | |
Stephen Hemminger | 38c867d | 2011-03-09 10:41:09 -0800 | [diff] [blame] | 433 | /* skip header lines */ |
| 434 | if (!fgets(buf, sizeof(buf), fp) || |
| 435 | !fgets(buf, sizeof(buf), fp)) { |
| 436 | fprintf(stderr, "/proc/net/dev read error\n"); |
Thomas Jarosch | 297452a | 2011-10-03 05:24:32 +0000 | [diff] [blame] | 437 | fclose(fp); |
Stephen Hemminger | 38c867d | 2011-03-09 10:41:09 -0800 | [diff] [blame] | 438 | return -1; |
| 439 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 440 | |
| 441 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 442 | int index, type; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 443 | char *ptr; |
| 444 | buf[sizeof(buf) - 1] = 0; |
| 445 | if ((ptr = strchr(buf, ':')) == NULL || |
| 446 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
| 447 | fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n"); |
Thomas Jarosch | 297452a | 2011-10-03 05:24:32 +0000 | [diff] [blame] | 448 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 449 | return -1; |
| 450 | } |
| 451 | if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", |
| 452 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 453 | &rx_fifo, &rx_frame, &rx_multi, |
| 454 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 455 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 456 | continue; |
| 457 | if (p->name[0] && strcmp(p->name, name)) |
| 458 | continue; |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 459 | index = ll_name_to_index(name); |
| 460 | if (index == 0) |
| 461 | continue; |
| 462 | type = ll_index_to_type(index); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 463 | if (type == -1) { |
| 464 | fprintf(stderr, "Failed to get type of [%s]\n", name); |
| 465 | continue; |
| 466 | } |
| 467 | if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) |
| 468 | continue; |
| 469 | memset(&p1, 0, sizeof(p1)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 470 | if (tnl_get_ioctl(name, &p1)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 471 | continue; |
| 472 | if ((p->link && p1.link != p->link) || |
| 473 | (p->name[0] && strcmp(p1.name, p->name)) || |
| 474 | (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || |
| 475 | (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || |
| 476 | (p->i_key && p1.i_key != p->i_key)) |
| 477 | continue; |
| 478 | print_tunnel(&p1); |
| 479 | if (show_stats) { |
| 480 | printf("%s", _SL_); |
| 481 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); |
| 482 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", |
| 483 | rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); |
| 484 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); |
| 485 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", |
| 486 | tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); |
| 487 | } |
| 488 | printf("\n"); |
| 489 | } |
Thomas Jarosch | 297452a | 2011-10-03 05:24:32 +0000 | [diff] [blame] | 490 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int do_show(int argc, char **argv) |
| 495 | { |
| 496 | int err; |
| 497 | struct ip_tunnel_parm p; |
| 498 | |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 499 | ll_init_map(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 500 | if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) |
| 501 | return -1; |
| 502 | |
| 503 | switch (p.iph.protocol) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 504 | case IPPROTO_IPIP: |
Saurabh Mohan | eec4760 | 2012-08-01 16:11:25 -0700 | [diff] [blame] | 505 | if (p.i_flags != VTI_ISVTI) |
| 506 | err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p); |
| 507 | else |
| 508 | err = tnl_get_ioctl(p.name[0] ? p.name : "ip_vti0", &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 509 | break; |
| 510 | case IPPROTO_GRE: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 511 | err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 512 | break; |
| 513 | case IPPROTO_IPV6: |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 514 | err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | do_tunnels_list(&p); |
| 518 | return 0; |
| 519 | } |
| 520 | if (err) |
| 521 | return -1; |
| 522 | |
| 523 | print_tunnel(&p); |
| 524 | printf("\n"); |
| 525 | return 0; |
| 526 | } |
| 527 | |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 528 | static int do_prl(int argc, char **argv) |
| 529 | { |
| 530 | struct ip_tunnel_prl p; |
| 531 | int count = 0; |
| 532 | int devname = 0; |
| 533 | int cmd = 0; |
| 534 | char medium[IFNAMSIZ]; |
| 535 | |
| 536 | memset(&p, 0, sizeof(p)); |
| 537 | memset(&medium, 0, sizeof(medium)); |
| 538 | |
| 539 | while (argc > 0) { |
| 540 | if (strcmp(*argv, "prl-default") == 0) { |
| 541 | NEXT_ARG(); |
| 542 | cmd = SIOCADDPRL; |
| 543 | p.addr = get_addr32(*argv); |
| 544 | p.flags |= PRL_DEFAULT; |
| 545 | count++; |
| 546 | } else if (strcmp(*argv, "prl-nodefault") == 0) { |
| 547 | NEXT_ARG(); |
| 548 | cmd = SIOCADDPRL; |
| 549 | p.addr = get_addr32(*argv); |
| 550 | count++; |
| 551 | } else if (strcmp(*argv, "prl-delete") == 0) { |
| 552 | NEXT_ARG(); |
| 553 | cmd = SIOCDELPRL; |
| 554 | p.addr = get_addr32(*argv); |
| 555 | count++; |
| 556 | } else if (strcmp(*argv, "dev") == 0) { |
| 557 | NEXT_ARG(); |
| 558 | strncpy(medium, *argv, IFNAMSIZ-1); |
| 559 | devname++; |
| 560 | } else { |
| 561 | fprintf(stderr,"%s: Invalid PRL parameter.\n", *argv); |
| 562 | exit(-1); |
| 563 | } |
| 564 | if (count > 1) { |
| 565 | fprintf(stderr,"One PRL entry at a time.\n"); |
| 566 | exit(-1); |
| 567 | } |
| 568 | argc--; argv++; |
| 569 | } |
| 570 | if (devname == 0) { |
| 571 | fprintf(stderr, "Must specify dev.\n"); |
| 572 | exit(-1); |
| 573 | } |
| 574 | |
| 575 | return tnl_prl_ioctl(cmd, medium, &p); |
| 576 | } |
| 577 | |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 578 | static int do_6rd(int argc, char **argv) |
| 579 | { |
| 580 | struct ip_tunnel_6rd ip6rd; |
| 581 | int devname = 0; |
| 582 | int cmd = 0; |
| 583 | char medium[IFNAMSIZ]; |
| 584 | inet_prefix prefix; |
| 585 | |
| 586 | memset(&ip6rd, 0, sizeof(ip6rd)); |
| 587 | memset(&medium, 0, sizeof(medium)); |
| 588 | |
| 589 | while (argc > 0) { |
| 590 | if (strcmp(*argv, "6rd-prefix") == 0) { |
| 591 | NEXT_ARG(); |
| 592 | if (get_prefix(&prefix, *argv, AF_INET6)) |
| 593 | invarg("invalid 6rd_prefix\n", *argv); |
| 594 | cmd = SIOCADD6RD; |
| 595 | memcpy(&ip6rd.prefix, prefix.data, 16); |
| 596 | ip6rd.prefixlen = prefix.bitlen; |
| 597 | } else if (strcmp(*argv, "6rd-relay_prefix") == 0) { |
| 598 | NEXT_ARG(); |
| 599 | if (get_prefix(&prefix, *argv, AF_INET)) |
| 600 | invarg("invalid 6rd-relay_prefix\n", *argv); |
| 601 | cmd = SIOCADD6RD; |
| 602 | memcpy(&ip6rd.relay_prefix, prefix.data, 4); |
| 603 | ip6rd.relay_prefixlen = prefix.bitlen; |
| 604 | } else if (strcmp(*argv, "6rd-reset") == 0) { |
| 605 | cmd = SIOCDEL6RD; |
| 606 | } else if (strcmp(*argv, "dev") == 0) { |
| 607 | NEXT_ARG(); |
| 608 | strncpy(medium, *argv, IFNAMSIZ-1); |
| 609 | devname++; |
| 610 | } else { |
| 611 | fprintf(stderr,"%s: Invalid 6RD parameter.\n", *argv); |
| 612 | exit(-1); |
| 613 | } |
| 614 | argc--; argv++; |
| 615 | } |
| 616 | if (devname == 0) { |
| 617 | fprintf(stderr, "Must specify dev.\n"); |
| 618 | exit(-1); |
| 619 | } |
| 620 | |
| 621 | return tnl_6rd_ioctl(cmd, medium, &ip6rd); |
| 622 | } |
| 623 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 624 | int do_iptunnel(int argc, char **argv) |
| 625 | { |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 626 | switch (preferred_family) { |
| 627 | case AF_UNSPEC: |
| 628 | preferred_family = AF_INET; |
| 629 | break; |
| 630 | case AF_INET: |
| 631 | break; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 632 | /* |
| 633 | * This is silly enough but we have no easy way to make it |
| 634 | * protocol-independent because of unarranged structure between |
| 635 | * IPv4 and IPv6. |
| 636 | */ |
| 637 | case AF_INET6: |
| 638 | return do_ip6tunnel(argc, argv); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 639 | default: |
| 640 | fprintf(stderr, "Unsupported family:%d\n", preferred_family); |
| 641 | exit(-1); |
| 642 | } |
| 643 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 644 | if (argc > 0) { |
| 645 | if (matches(*argv, "add") == 0) |
| 646 | return do_add(SIOCADDTUNNEL, argc-1, argv+1); |
| 647 | if (matches(*argv, "change") == 0) |
| 648 | return do_add(SIOCCHGTUNNEL, argc-1, argv+1); |
Andreas Henriksson | 6e30461 | 2012-05-19 16:08:21 +0200 | [diff] [blame] | 649 | if (matches(*argv, "delete") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 650 | return do_del(argc-1, argv+1); |
| 651 | if (matches(*argv, "show") == 0 || |
| 652 | matches(*argv, "lst") == 0 || |
| 653 | matches(*argv, "list") == 0) |
| 654 | return do_show(argc-1, argv+1); |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 655 | if (matches(*argv, "prl") == 0) |
| 656 | return do_prl(argc-1, argv+1); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 657 | if (matches(*argv, "6rd") == 0) |
| 658 | return do_6rd(argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 659 | if (matches(*argv, "help") == 0) |
| 660 | usage(); |
| 661 | } else |
| 662 | return do_show(0, NULL); |
| 663 | |
| 664 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip tunnel help\".\n", *argv); |
| 665 | exit(-1); |
| 666 | } |