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; |
YOSHIFUJI Hideaki / 吉藤英明 | 0b959b0 | 2007-10-12 16:51:22 +0900 | [diff] [blame] | 54 | case 0: |
| 55 | strcpy(buf, "any"); |
| 56 | break; |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 57 | default: |
| 58 | strcpy(buf, "unknown"); |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | return buf; |
| 63 | } |
| 64 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 65 | int tnl_get_ioctl(const char *basedev, void *p) |
| 66 | { |
| 67 | struct ifreq ifr; |
| 68 | int fd; |
| 69 | int err; |
| 70 | |
| 71 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 72 | ifr.ifr_ifru.ifru_data = (void*)p; |
| 73 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 74 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
| 75 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 76 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 77 | strerror(errno)); |
| 78 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 79 | close(fd); |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) |
| 84 | { |
| 85 | struct ifreq ifr; |
| 86 | int fd; |
| 87 | int err; |
| 88 | |
| 89 | if (cmd == SIOCCHGTUNNEL && name[0]) |
| 90 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 91 | else |
| 92 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 93 | ifr.ifr_ifru.ifru_data = p; |
| 94 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 95 | err = ioctl(fd, cmd, &ifr); |
| 96 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 97 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 98 | strerror(errno)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 99 | close(fd); |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | int tnl_del_ioctl(const char *basedev, const char *name, void *p) |
| 104 | { |
| 105 | struct ifreq ifr; |
| 106 | int fd; |
| 107 | int err; |
| 108 | |
| 109 | if (name[0]) |
| 110 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 111 | else |
| 112 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 113 | |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 114 | ifr.ifr_ifru.ifru_data = p; |
| 115 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 116 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
| 117 | if (err) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 118 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 119 | ifr.ifr_name, strerror(errno)); |
Masahide NAKAMURA | d9bd1bd | 2006-11-24 12:27:03 +0900 | [diff] [blame] | 120 | close(fd); |
| 121 | return err; |
| 122 | } |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 123 | |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 124 | static int tnl_gen_ioctl(int cmd, const char *name, |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 125 | void *p, int skiperr) |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 126 | { |
| 127 | struct ifreq ifr; |
| 128 | int fd; |
| 129 | int err; |
| 130 | |
| 131 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 132 | ifr.ifr_ifru.ifru_data = p; |
| 133 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 134 | err = ioctl(fd, cmd, &ifr); |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 135 | if (err && errno != skiperr) |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 136 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
| 137 | cmd, strerror(errno)); |
Sascha Hlusiak | a07e991 | 2009-05-04 01:44:47 +0200 | [diff] [blame] | 138 | close(fd); |
| 139 | return err; |
| 140 | } |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 141 | |
| 142 | int tnl_prl_ioctl(int cmd, const char *name, void *p) |
| 143 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 144 | return tnl_gen_ioctl(cmd, name, p, -1); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) |
| 148 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 149 | return tnl_gen_ioctl(cmd, name, p, -1); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int tnl_ioctl_get_6rd(const char *name, void *p) |
| 153 | { |
Alexandre Cassen | 3979ef9 | 2010-04-04 22:40:14 +0200 | [diff] [blame] | 154 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
Alexandre Cassen | b88215c | 2009-12-16 02:38:29 +0000 | [diff] [blame] | 155 | } |