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