Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2006 USAGI/WIDE Project |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 3 | * |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 8 | * |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 13 | * |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | /* |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 19 | * Author: |
| 20 | * Masahide NAKAMURA @USAGI |
| 21 | */ |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 22 | |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 26 | #include <unistd.h> |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/socket.h> |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 29 | #include <arpa/inet.h> |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 31 | #include <linux/ip.h> |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 32 | #include <linux/if.h> |
| 33 | #include <linux/if_arp.h> |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 34 | #include <linux/if_tunnel.h> |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 35 | #include <linux/ip6_tunnel.h> |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 36 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | #include "tunnel.h" |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 39 | #include "ip_common.h" |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 40 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 41 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) |
| 42 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 43 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 44 | #define DEFAULT_TNL_HOP_LIMIT (64) |
| 45 | |
| 46 | static void usage(void) __attribute__((noreturn)); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 47 | |
| 48 | static void usage(void) |
| 49 | { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 50 | fprintf(stderr, "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n"); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 51 | fprintf(stderr, " [ mode { ip6ip6 | ipip6 | ip6gre | any } ]\n"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 52 | fprintf(stderr, " [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n"); |
| 53 | fprintf(stderr, " [ encaplimit ELIM ]\n"); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 54 | fprintf(stderr ," [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 55 | fprintf(stderr, " [ dscp inherit ]\n"); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 56 | fprintf(stderr, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 57 | fprintf(stderr, "\n"); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 58 | fprintf(stderr, "Where: NAME := STRING\n"); |
| 59 | fprintf(stderr, " ADDR := IPV6_ADDRESS\n"); |
| 60 | fprintf(stderr, " ELIM := { none | 0..255 }(default=%d)\n", |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 61 | IPV6_DEFAULT_TNL_ENCAP_LIMIT); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 62 | fprintf(stderr, " TTL := 0..255 (default=%d)\n", |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 63 | DEFAULT_TNL_HOP_LIMIT); |
Nicolas Dichtel | d0c8420 | 2012-11-14 16:29:24 +0100 | [diff] [blame] | 64 | fprintf(stderr, " TCLASS := { 0x0..0xff | inherit }\n"); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 65 | fprintf(stderr, " FLOWLABEL := { 0x0..0xfffff | inherit }\n"); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 66 | fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 67 | exit(-1); |
| 68 | } |
| 69 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 70 | static void print_tunnel(struct ip6_tnl_parm2 *p) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 71 | { |
| 72 | char remote[64]; |
| 73 | char local[64]; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 74 | |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 75 | inet_ntop(AF_INET6, &p->raddr, remote, sizeof(remote)); |
| 76 | inet_ntop(AF_INET6, &p->laddr, local, sizeof(local)); |
| 77 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 78 | printf("%s: %s/ipv6 remote %s local %s", |
| 79 | p->name, tnl_strproto(p->proto), remote, local); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 80 | if (p->link) { |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 81 | const char *n = ll_index_to_name(p->link); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 82 | if (n) |
| 83 | printf(" dev %s", n); |
| 84 | } |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 85 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 86 | if (p->flags & IP6_TNL_F_IGN_ENCAP_LIMIT) |
| 87 | printf(" encaplimit none"); |
| 88 | else |
| 89 | printf(" encaplimit %u", p->encap_limit); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 90 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 91 | printf(" hoplimit %u", p->hop_limit); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 92 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 93 | if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 94 | printf(" tclass inherit"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 95 | else { |
| 96 | __u32 val = ntohl(p->flowinfo & IP6_FLOWINFO_TCLASS); |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 97 | printf(" tclass 0x%02x", (__u8)(val >> 20)); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 98 | } |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 99 | |
| 100 | if (p->flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 101 | printf(" flowlabel inherit"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 102 | else |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 103 | printf(" flowlabel 0x%05x", ntohl(p->flowinfo & IP6_FLOWINFO_FLOWLABEL)); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 104 | |
| 105 | printf(" (flowinfo 0x%08x)", ntohl(p->flowinfo)); |
| 106 | |
| 107 | if (p->flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 108 | printf(" dscp inherit"); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 109 | |
| 110 | if (p->proto == IPPROTO_GRE) { |
| 111 | if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key) |
| 112 | printf(" key %u", ntohl(p->i_key)); |
| 113 | else if ((p->i_flags|p->o_flags)&GRE_KEY) { |
| 114 | if (p->i_flags&GRE_KEY) |
| 115 | printf(" ikey %u ", ntohl(p->i_key)); |
| 116 | if (p->o_flags&GRE_KEY) |
| 117 | printf(" okey %u ", ntohl(p->o_key)); |
| 118 | } |
| 119 | |
| 120 | if (p->i_flags&GRE_SEQ) |
| 121 | printf("%s Drop packets out of sequence.\n", _SL_); |
| 122 | if (p->i_flags&GRE_CSUM) |
| 123 | printf("%s Checksum in received packet is required.", _SL_); |
| 124 | if (p->o_flags&GRE_SEQ) |
| 125 | printf("%s Sequence packets on output.", _SL_); |
| 126 | if (p->o_flags&GRE_CSUM) |
| 127 | printf("%s Checksum output packets.", _SL_); |
| 128 | } |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 129 | } |
| 130 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 131 | static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 132 | { |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 133 | int count = 0; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 134 | char medium[IFNAMSIZ]; |
| 135 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 136 | memset(medium, 0, sizeof(medium)); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 137 | |
| 138 | while (argc > 0) { |
YOSHIFUJI Hideaki / 吉藤英明 | 0b959b0 | 2007-10-12 16:51:22 +0900 | [diff] [blame] | 139 | if (strcmp(*argv, "mode") == 0) { |
| 140 | NEXT_ARG(); |
| 141 | if (strcmp(*argv, "ipv6/ipv6") == 0 || |
| 142 | strcmp(*argv, "ip6ip6") == 0) |
| 143 | p->proto = IPPROTO_IPV6; |
| 144 | else if (strcmp(*argv, "ip/ipv6") == 0 || |
| 145 | strcmp(*argv, "ipv4/ipv6") == 0 || |
| 146 | strcmp(*argv, "ipip6") == 0 || |
| 147 | strcmp(*argv, "ip4ip6") == 0) |
| 148 | p->proto = IPPROTO_IPIP; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 149 | else if (strcmp(*argv, "ip6gre") == 0 || |
| 150 | strcmp(*argv, "gre/ipv6") == 0) |
| 151 | p->proto = IPPROTO_GRE; |
YOSHIFUJI Hideaki / 吉藤英明 | 0b959b0 | 2007-10-12 16:51:22 +0900 | [diff] [blame] | 152 | else if (strcmp(*argv, "any/ipv6") == 0 || |
| 153 | strcmp(*argv, "any") == 0) |
| 154 | p->proto = 0; |
| 155 | else { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 156 | fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv); |
YOSHIFUJI Hideaki / 吉藤英明 | 0b959b0 | 2007-10-12 16:51:22 +0900 | [diff] [blame] | 157 | exit(-1); |
| 158 | } |
| 159 | } else if (strcmp(*argv, "remote") == 0) { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 160 | inet_prefix raddr; |
| 161 | NEXT_ARG(); |
| 162 | get_prefix(&raddr, *argv, preferred_family); |
| 163 | if (raddr.family == AF_UNSPEC) |
| 164 | invarg("\"remote\" address family is AF_UNSPEC", *argv); |
| 165 | memcpy(&p->raddr, &raddr.data, sizeof(p->raddr)); |
| 166 | } else if (strcmp(*argv, "local") == 0) { |
| 167 | inet_prefix laddr; |
| 168 | NEXT_ARG(); |
| 169 | get_prefix(&laddr, *argv, preferred_family); |
| 170 | if (laddr.family == AF_UNSPEC) |
| 171 | invarg("\"local\" address family is AF_UNSPEC", *argv); |
| 172 | memcpy(&p->laddr, &laddr.data, sizeof(p->laddr)); |
| 173 | } else if (strcmp(*argv, "dev") == 0) { |
| 174 | NEXT_ARG(); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 175 | strncpy(medium, *argv, IFNAMSIZ - 1); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 176 | } else if (strcmp(*argv, "encaplimit") == 0) { |
| 177 | NEXT_ARG(); |
| 178 | if (strcmp(*argv, "none") == 0) { |
| 179 | p->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 180 | } else { |
| 181 | __u8 uval; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 182 | if (get_u8(&uval, *argv, 0) < -1) |
| 183 | invarg("invalid ELIM", *argv); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 184 | p->encap_limit = uval; |
Nicolas Dichtel | 3f83dce | 2012-11-14 16:29:25 +0100 | [diff] [blame] | 185 | p->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 186 | } |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 187 | } else if (strcmp(*argv, "hoplimit") == 0 || |
| 188 | strcmp(*argv, "ttl") == 0 || |
| 189 | strcmp(*argv, "hlim") == 0) { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 190 | __u8 uval; |
| 191 | NEXT_ARG(); |
| 192 | if (get_u8(&uval, *argv, 0)) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 193 | invarg("invalid TTL", *argv); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 194 | p->hop_limit = uval; |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 195 | } else if (strcmp(*argv, "tclass") == 0 || |
| 196 | strcmp(*argv, "tc") == 0 || |
| 197 | strcmp(*argv, "tos") == 0 || |
| 198 | matches(*argv, "dsfield") == 0) { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 199 | __u8 uval; |
| 200 | NEXT_ARG(); |
Nicolas Dichtel | df5574d | 2012-11-14 16:29:26 +0100 | [diff] [blame] | 201 | p->flowinfo &= ~IP6_FLOWINFO_TCLASS; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 202 | if (strcmp(*argv, "inherit") == 0) |
| 203 | p->flags |= IP6_TNL_F_USE_ORIG_TCLASS; |
| 204 | else { |
| 205 | if (get_u8(&uval, *argv, 16)) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 206 | invarg("invalid TClass", *argv); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 207 | p->flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; |
| 208 | p->flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; |
| 209 | } |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 210 | } else if (strcmp(*argv, "flowlabel") == 0 || |
| 211 | strcmp(*argv, "fl") == 0) { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 212 | __u32 uval; |
| 213 | NEXT_ARG(); |
Nicolas Dichtel | df5574d | 2012-11-14 16:29:26 +0100 | [diff] [blame] | 214 | p->flowinfo &= ~IP6_FLOWINFO_FLOWLABEL; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 215 | if (strcmp(*argv, "inherit") == 0) |
| 216 | p->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 217 | else { |
| 218 | if (get_u32(&uval, *argv, 16)) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 219 | invarg("invalid Flowlabel", *argv); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 220 | if (uval > 0xFFFFF) |
YOSHIFUJI Hideaki / 吉藤英明 | eddde11 | 2008-03-13 11:17:54 -0400 | [diff] [blame] | 221 | invarg("invalid Flowlabel", *argv); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 222 | p->flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; |
| 223 | p->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 224 | } |
| 225 | } else if (strcmp(*argv, "dscp") == 0) { |
| 226 | NEXT_ARG(); |
| 227 | if (strcmp(*argv, "inherit") != 0) |
| 228 | invarg("not inherit", *argv); |
| 229 | p->flags |= IP6_TNL_F_RCV_DSCP_COPY; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 230 | } else if (strcmp(*argv, "key") == 0) { |
| 231 | unsigned uval; |
| 232 | NEXT_ARG(); |
| 233 | p->i_flags |= GRE_KEY; |
| 234 | p->o_flags |= GRE_KEY; |
| 235 | if (strchr(*argv, '.')) |
| 236 | p->i_key = p->o_key = get_addr32(*argv); |
| 237 | else { |
| 238 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 239 | fprintf(stderr, "invalid value of \"key\"\n"); |
| 240 | exit(-1); |
| 241 | } |
| 242 | p->i_key = p->o_key = htonl(uval); |
| 243 | } |
| 244 | } else if (strcmp(*argv, "ikey") == 0) { |
| 245 | unsigned uval; |
| 246 | NEXT_ARG(); |
| 247 | p->i_flags |= GRE_KEY; |
| 248 | if (strchr(*argv, '.')) |
| 249 | p->i_key = get_addr32(*argv); |
| 250 | else { |
| 251 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 252 | fprintf(stderr, "invalid value of \"ikey\"\n"); |
| 253 | exit(-1); |
| 254 | } |
| 255 | p->i_key = htonl(uval); |
| 256 | } |
| 257 | } else if (strcmp(*argv, "okey") == 0) { |
| 258 | unsigned uval; |
| 259 | NEXT_ARG(); |
| 260 | p->o_flags |= GRE_KEY; |
| 261 | if (strchr(*argv, '.')) |
| 262 | p->o_key = get_addr32(*argv); |
| 263 | else { |
| 264 | if (get_unsigned(&uval, *argv, 0)<0) { |
| 265 | fprintf(stderr, "invalid value of \"okey\"\n"); |
| 266 | exit(-1); |
| 267 | } |
| 268 | p->o_key = htonl(uval); |
| 269 | } |
| 270 | } else if (strcmp(*argv, "seq") == 0) { |
| 271 | p->i_flags |= GRE_SEQ; |
| 272 | p->o_flags |= GRE_SEQ; |
| 273 | } else if (strcmp(*argv, "iseq") == 0) { |
| 274 | p->i_flags |= GRE_SEQ; |
| 275 | } else if (strcmp(*argv, "oseq") == 0) { |
| 276 | p->o_flags |= GRE_SEQ; |
| 277 | } else if (strcmp(*argv, "csum") == 0) { |
| 278 | p->i_flags |= GRE_CSUM; |
| 279 | p->o_flags |= GRE_CSUM; |
| 280 | } else if (strcmp(*argv, "icsum") == 0) { |
| 281 | p->i_flags |= GRE_CSUM; |
| 282 | } else if (strcmp(*argv, "ocsum") == 0) { |
| 283 | p->o_flags |= GRE_CSUM; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 284 | } else { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 285 | if (strcmp(*argv, "name") == 0) { |
| 286 | NEXT_ARG(); |
| 287 | } |
| 288 | if (matches(*argv, "help") == 0) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 289 | usage(); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 290 | if (p->name[0]) |
| 291 | duparg2("name", *argv); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 292 | strncpy(p->name, *argv, IFNAMSIZ - 1); |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 293 | if (cmd == SIOCCHGTUNNEL && count == 0) { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 294 | struct ip6_tnl_parm2 old_p; |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 295 | memset(&old_p, 0, sizeof(old_p)); |
| 296 | if (tnl_get_ioctl(*argv, &old_p)) |
| 297 | return -1; |
| 298 | *p = old_p; |
| 299 | } |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 300 | } |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 301 | count++; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 302 | argc--; argv++; |
| 303 | } |
| 304 | if (medium[0]) { |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 305 | p->link = ll_name_to_index(medium); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 306 | if (p->link == 0) |
| 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 312 | static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default) |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 313 | { |
| 314 | memset(p, 0, sizeof(*p)); |
| 315 | p->proto = IPPROTO_IPV6; |
| 316 | if (apply_default) { |
| 317 | p->hop_limit = DEFAULT_TNL_HOP_LIMIT; |
| 318 | p->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * @p1: user specified parameter |
| 324 | * @p2: database entry |
| 325 | */ |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 326 | static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 *p1, |
| 327 | const struct ip6_tnl_parm2 *p2) |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 328 | { |
| 329 | return ((!p1->link || p1->link == p2->link) && |
| 330 | (!p1->name[0] || strcmp(p1->name, p2->name) == 0) && |
| 331 | (memcmp(&p1->laddr, &in6addr_any, sizeof(p1->laddr)) == 0 || |
| 332 | memcmp(&p1->laddr, &p2->laddr, sizeof(p1->laddr)) == 0) && |
| 333 | (memcmp(&p1->raddr, &in6addr_any, sizeof(p1->raddr)) == 0 || |
| 334 | memcmp(&p1->raddr, &p2->raddr, sizeof(p1->raddr)) == 0) && |
| 335 | (!p1->proto || !p2->proto || p1->proto == p2->proto) && |
| 336 | (!p1->encap_limit || p1->encap_limit == p2->encap_limit) && |
| 337 | (!p1->hop_limit || p1->hop_limit == p2->hop_limit) && |
| 338 | (!(p1->flowinfo & IP6_FLOWINFO_TCLASS) || |
| 339 | !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_TCLASS)) && |
| 340 | (!(p1->flowinfo & IP6_FLOWINFO_FLOWLABEL) || |
| 341 | !((p1->flowinfo ^ p2->flowinfo) & IP6_FLOWINFO_FLOWLABEL)) && |
| 342 | (!p1->flags || (p1->flags & p2->flags))); |
| 343 | } |
| 344 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 345 | static int do_tunnels_list(struct ip6_tnl_parm2 *p) |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 346 | { |
| 347 | char buf[512]; |
| 348 | int err = -1; |
| 349 | FILE *fp = fopen("/proc/net/dev", "r"); |
| 350 | if (fp == NULL) { |
| 351 | perror("fopen"); |
| 352 | goto end; |
| 353 | } |
| 354 | |
| 355 | /* skip two lines at the begenning of the file */ |
Stephen Hemminger | 38c867d | 2011-03-09 10:41:09 -0800 | [diff] [blame] | 356 | if (!fgets(buf, sizeof(buf), fp) || |
| 357 | !fgets(buf, sizeof(buf), fp)) { |
| 358 | fprintf(stderr, "/proc/net/dev read error\n"); |
| 359 | return -1; |
| 360 | } |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 361 | |
| 362 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 363 | char name[IFNAMSIZ]; |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 364 | int index, type; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 365 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 366 | rx_fifo, rx_frame, |
| 367 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 368 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 369 | struct ip6_tnl_parm2 p1; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 370 | char *ptr; |
| 371 | |
| 372 | buf[sizeof(buf) - 1] = '\0'; |
| 373 | if ((ptr = strchr(buf, ':')) == NULL || |
| 374 | (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 375 | fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 376 | goto end; |
| 377 | } |
| 378 | if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", |
| 379 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 380 | &rx_fifo, &rx_frame, &rx_multi, |
| 381 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 382 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 383 | continue; |
| 384 | if (p->name[0] && strcmp(p->name, name)) |
| 385 | continue; |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 386 | index = ll_name_to_index(name); |
| 387 | if (index == 0) |
| 388 | continue; |
| 389 | type = ll_index_to_type(index); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 390 | if (type == -1) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 391 | fprintf(stderr, "Failed to get type of \"%s\"\n", name); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 392 | continue; |
| 393 | } |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 394 | if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE) |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 395 | continue; |
| 396 | memset(&p1, 0, sizeof(p1)); |
| 397 | ip6_tnl_parm_init(&p1, 0); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 398 | if (type == ARPHRD_IP6GRE) |
| 399 | p1.proto = IPPROTO_GRE; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 400 | strcpy(p1.name, name); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 401 | p1.link = ll_name_to_index(p1.name); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 402 | if (p1.link == 0) |
| 403 | continue; |
| 404 | if (tnl_get_ioctl(p1.name, &p1)) |
| 405 | continue; |
| 406 | if (!ip6_tnl_parm_match(p, &p1)) |
| 407 | continue; |
| 408 | print_tunnel(&p1); |
| 409 | if (show_stats) { |
| 410 | printf("%s", _SL_); |
| 411 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); |
| 412 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", |
| 413 | rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); |
| 414 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); |
| 415 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", |
| 416 | tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); |
| 417 | } |
| 418 | printf("\n"); |
| 419 | } |
| 420 | err = 0; |
| 421 | |
| 422 | end: |
| 423 | if (fp) |
| 424 | fclose(fp); |
| 425 | return err; |
| 426 | } |
| 427 | |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 428 | static int do_show(int argc, char **argv) |
| 429 | { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 430 | struct ip6_tnl_parm2 p; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 431 | |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 432 | ll_init_map(&rth); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 433 | ip6_tnl_parm_init(&p, 0); |
Srivats P | c3651bf | 2009-03-27 11:17:26 -0700 | [diff] [blame] | 434 | p.proto = 0; /* default to any */ |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 435 | |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 436 | if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 437 | return -1; |
| 438 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 439 | if (!p.name[0] || show_stats) |
| 440 | do_tunnels_list(&p); |
| 441 | else { |
| 442 | if (tnl_get_ioctl(p.name, &p)) |
| 443 | return -1; |
| 444 | print_tunnel(&p); |
| 445 | printf("\n"); |
| 446 | } |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 447 | |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 451 | static int do_add(int cmd, int argc, char **argv) |
| 452 | { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 453 | struct ip6_tnl_parm2 p; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 454 | |
| 455 | ip6_tnl_parm_init(&p, 1); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 456 | |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 457 | if (parse_args(argc, argv, cmd, &p) < 0) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 458 | return -1; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 459 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 460 | switch (p.proto) { |
| 461 | case IPPROTO_IPIP: |
| 462 | case IPPROTO_IPV6: |
| 463 | return tnl_add_ioctl(cmd, "ip6tnl0", p.name, &p); |
| 464 | case IPPROTO_GRE: |
| 465 | return tnl_add_ioctl(cmd, "ip6gre0", p.name, &p); |
| 466 | default: |
| 467 | fprintf(stderr, "cannot determine tunnel mode (ip6ip6, ipip6 or gre)\n"); |
| 468 | } |
| 469 | return -1; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 470 | } |
| 471 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 472 | static int do_del(int argc, char **argv) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 473 | { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 474 | struct ip6_tnl_parm2 p; |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 475 | |
| 476 | ip6_tnl_parm_init(&p, 1); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 477 | |
Jiri Benc | 21a5a6b | 2011-09-19 05:14:10 +0000 | [diff] [blame] | 478 | if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0) |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 479 | return -1; |
| 480 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame^] | 481 | switch (p.proto) { |
| 482 | case IPPROTO_IPIP: |
| 483 | case IPPROTO_IPV6: |
| 484 | return tnl_del_ioctl("ip6tnl0", p.name, &p); |
| 485 | case IPPROTO_GRE: |
| 486 | return tnl_del_ioctl("ip6gre0", p.name, &p); |
| 487 | default: |
| 488 | return tnl_del_ioctl(p.name, p.name, &p); |
| 489 | } |
| 490 | return -1; |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 491 | } |
| 492 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 493 | int do_ip6tunnel(int argc, char **argv) |
| 494 | { |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 495 | switch (preferred_family) { |
| 496 | case AF_UNSPEC: |
| 497 | preferred_family = AF_INET6; |
| 498 | break; |
| 499 | case AF_INET6: |
| 500 | break; |
| 501 | default: |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 502 | fprintf(stderr, "Unsupported protocol family: %d\n", preferred_family); |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 503 | exit(-1); |
| 504 | } |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 505 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 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); |
Andreas Henriksson | 6e30461 | 2012-05-19 16:08:21 +0200 | [diff] [blame] | 511 | if (matches(*argv, "delete") == 0) |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 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 | if (matches(*argv, "help") == 0) |
| 518 | usage(); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 519 | } else |
| 520 | return do_show(0, NULL); |
| 521 | |
Masahide NAKAMURA | 288384f | 2006-11-24 12:27:09 +0900 | [diff] [blame] | 522 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip -f inet6 tunnel help\".\n", *argv); |
| 523 | exit(-1); |
Masahide NAKAMURA | 9447a0d | 2006-11-24 12:27:06 +0900 | [diff] [blame] | 524 | } |