Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +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 | d9bd1bd | 2006-11-24 12:27:03 +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 | d9bd1bd | 2006-11-24 12:27:03 +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 | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
Stephen Hemminger | 4d98ab0 | 2013-12-06 15:05:07 -0800 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 16 | */ |
| 17 | /* |
| 18 | * split from ip_tunnel.c |
| 19 | */ |
| 20 | /* |
| 21 | * Author: |
| 22 | * Masahide NAKAMURA @USAGI |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 28 | #include <errno.h> |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <netinet/in.h> |
| 33 | #include <linux/if.h> |
| 34 | #include <linux/ip.h> |
| 35 | #include <linux/if_tunnel.h> |
| 36 | |
| 37 | #include "utils.h" |
| 38 | #include "tunnel.h" |
| 39 | |
| 40 | const char *tnl_strproto(__u8 proto) |
| 41 | { |
| 42 | static char buf[16]; |
| 43 | |
| 44 | switch (proto) { |
| 45 | case IPPROTO_IPIP: |
| 46 | strcpy(buf, "ip"); |
| 47 | break; |
| 48 | case IPPROTO_GRE: |
| 49 | strcpy(buf, "gre"); |
| 50 | break; |
| 51 | case IPPROTO_IPV6: |
| 52 | strcpy(buf, "ipv6"); |
| 53 | break; |
Stephen Hemminger | 14e9767 | 2014-12-03 19:08:41 -0800 | [diff] [blame] | 54 | case IPPROTO_ESP: |
| 55 | strcpy(buf, "esp"); |
| 56 | break; |
YOSHIFUJI Hideaki / 吉藤英明 | 0b959b0 | 2007-10-12 16:51:22 +0900 | [diff] [blame] | 57 | case 0: |
| 58 | strcpy(buf, "any"); |
| 59 | break; |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 60 | default: |
| 61 | strcpy(buf, "unknown"); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | return buf; |
| 66 | } |
| 67 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 68 | int tnl_get_ioctl(const char *basedev, void *p) |
| 69 | { |
| 70 | struct ifreq ifr; |
| 71 | int fd; |
| 72 | int err; |
| 73 | |
| 74 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 75 | ifr.ifr_ifru.ifru_data = (void*)p; |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 76 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 77 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 78 | if (fd < 0) { |
| 79 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); |
| 80 | return -1; |
| 81 | } |
| 82 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 83 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
| 84 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 85 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 86 | strerror(errno)); |
| 87 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 88 | close(fd); |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) |
| 93 | { |
| 94 | struct ifreq ifr; |
| 95 | int fd; |
| 96 | int err; |
| 97 | |
| 98 | if (cmd == SIOCCHGTUNNEL && name[0]) |
| 99 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 100 | else |
| 101 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 102 | ifr.ifr_ifru.ifru_data = p; |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 103 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 104 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 105 | if (fd < 0) { |
| 106 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); |
| 107 | return -1; |
| 108 | } |
| 109 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 110 | err = ioctl(fd, cmd, &ifr); |
| 111 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 112 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 113 | strerror(errno)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 114 | close(fd); |
| 115 | return err; |
| 116 | } |
| 117 | |
| 118 | int tnl_del_ioctl(const char *basedev, const char *name, void *p) |
| 119 | { |
| 120 | struct ifreq ifr; |
| 121 | int fd; |
| 122 | int err; |
| 123 | |
| 124 | if (name[0]) |
| 125 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 126 | else |
| 127 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 128 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 129 | ifr.ifr_ifru.ifru_data = p; |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 130 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 131 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 132 | if (fd < 0) { |
| 133 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); |
| 134 | return -1; |
| 135 | } |
| 136 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 137 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
| 138 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 139 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 140 | ifr.ifr_name, strerror(errno)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 141 | close(fd); |
| 142 | return err; |
| 143 | } |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 144 | |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 145 | static int tnl_gen_ioctl(int cmd, const char *name, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 146 | void *p, int skiperr) |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 147 | { |
| 148 | struct ifreq ifr; |
| 149 | int fd; |
| 150 | int err; |
| 151 | |
| 152 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 153 | ifr.ifr_ifru.ifru_data = p; |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 154 | |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 155 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
Zhang Shengju | cb89c7c | 2015-07-24 12:11:32 -0400 | [diff] [blame] | 156 | if (fd < 0) { |
| 157 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); |
| 158 | return -1; |
| 159 | } |
| 160 | |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 161 | err = ioctl(fd, cmd, &ifr); |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 162 | if (err && errno != skiperr) |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 163 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
| 164 | cmd, strerror(errno)); |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 165 | close(fd); |
| 166 | return err; |
| 167 | } |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 168 | |
| 169 | int tnl_prl_ioctl(int cmd, const char *name, void *p) |
| 170 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 171 | return tnl_gen_ioctl(cmd, name, p, -1); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) |
| 175 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 176 | return tnl_gen_ioctl(cmd, name, p, -1); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int tnl_ioctl_get_6rd(const char *name, void *p) |
| 180 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 181 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 182 | } |
Phil Sutter | a7ed152 | 2015-11-13 18:08:55 +0100 | [diff] [blame] | 183 | |
| 184 | __be32 tnl_parse_key(const char *name, const char *key) |
| 185 | { |
| 186 | unsigned uval; |
| 187 | |
| 188 | if (strchr(key, '.')) |
| 189 | return get_addr32(key); |
| 190 | |
| 191 | if (get_unsigned(&uval, key, 0) < 0) { |
| 192 | fprintf(stderr, "invalid value for \"%s\": \"%s\";", name, key); |
| 193 | fprintf(stderr, " it should be an unsigned integer\n"); |
| 194 | exit(-1); |
| 195 | } |
| 196 | return htonl(uval); |
| 197 | } |
Phil Sutter | 7d6aadc | 2015-12-18 11:58:06 +0100 | [diff] [blame] | 198 | |
| 199 | /* tnl_print_stats - print tunnel statistics |
| 200 | * |
| 201 | * @buf - tunnel interface's line in /proc/net/dev, |
| 202 | * starting past the interface name and following colon |
| 203 | */ |
| 204 | void tnl_print_stats(const char *buf) |
| 205 | { |
| 206 | unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, |
| 207 | rx_fifo, rx_frame, |
| 208 | tx_bytes, tx_packets, tx_errs, tx_drops, |
| 209 | tx_fifo, tx_colls, tx_carrier, rx_multi; |
| 210 | |
Phil Sutter | f8fc1d1 | 2015-12-21 20:42:56 +0100 | [diff] [blame] | 211 | if (sscanf(buf, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", |
Phil Sutter | 7d6aadc | 2015-12-18 11:58:06 +0100 | [diff] [blame] | 212 | &rx_bytes, &rx_packets, &rx_errs, &rx_drops, |
| 213 | &rx_fifo, &rx_frame, &rx_multi, |
| 214 | &tx_bytes, &tx_packets, &tx_errs, &tx_drops, |
| 215 | &tx_fifo, &tx_colls, &tx_carrier) != 14) |
| 216 | return; |
| 217 | |
| 218 | printf("%s", _SL_); |
| 219 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); |
| 220 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", |
| 221 | rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); |
| 222 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); |
| 223 | printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", |
| 224 | tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); |
| 225 | } |