lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C)2006 USAGI/WIDE Project |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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 | /* |
| 19 | * split from ip_tunnel.c |
| 20 | */ |
| 21 | /* |
| 22 | * Author: |
| 23 | * Masahide NAKAMURA @USAGI |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <linux/if.h> |
| 35 | #include <linux/ip.h> |
| 36 | #include <linux/if_tunnel.h> |
| 37 | |
| 38 | #include "utils.h" |
| 39 | #include "tunnel.h" |
| 40 | |
| 41 | const char *tnl_strproto(__u8 proto) |
| 42 | { |
| 43 | static char buf[16]; |
| 44 | |
| 45 | switch (proto) { |
| 46 | case IPPROTO_IPIP: |
| 47 | strcpy(buf, "ip"); |
| 48 | break; |
| 49 | case IPPROTO_GRE: |
| 50 | strcpy(buf, "gre"); |
| 51 | break; |
| 52 | case IPPROTO_IPV6: |
| 53 | strcpy(buf, "ipv6"); |
| 54 | break; |
| 55 | case 0: |
| 56 | strcpy(buf, "any"); |
| 57 | break; |
| 58 | default: |
| 59 | strcpy(buf, "unknown"); |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | return buf; |
| 64 | } |
| 65 | |
| 66 | int tnl_get_ioctl(const char *basedev, void *p) |
| 67 | { |
| 68 | struct ifreq ifr; |
| 69 | int fd; |
| 70 | int err; |
| 71 | |
| 72 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 73 | ifr.ifr_ifru.ifru_data = (void*)p; |
| 74 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 75 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
| 76 | if (err) |
| 77 | fprintf(stderr, "get tunnel %s failed: %s\n", basedev, |
| 78 | strerror(errno)); |
| 79 | |
| 80 | close(fd); |
| 81 | return err; |
| 82 | } |
| 83 | |
| 84 | int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) |
| 85 | { |
| 86 | struct ifreq ifr; |
| 87 | int fd; |
| 88 | int err; |
| 89 | |
| 90 | if (cmd == SIOCCHGTUNNEL && name[0]) |
| 91 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 92 | else |
| 93 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 94 | ifr.ifr_ifru.ifru_data = p; |
| 95 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 96 | err = ioctl(fd, cmd, &ifr); |
| 97 | if (err) |
| 98 | fprintf(stderr, "add tunnel %s failed: %s\n", ifr.ifr_name, |
| 99 | strerror(errno)); |
| 100 | close(fd); |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | int tnl_del_ioctl(const char *basedev, const char *name, void *p) |
| 105 | { |
| 106 | struct ifreq ifr; |
| 107 | int fd; |
| 108 | int err; |
| 109 | |
| 110 | if (name[0]) |
| 111 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 112 | else |
| 113 | strncpy(ifr.ifr_name, basedev, IFNAMSIZ); |
| 114 | |
| 115 | ifr.ifr_ifru.ifru_data = p; |
| 116 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 117 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
| 118 | if (err) |
| 119 | fprintf(stderr, "delete tunnel %s failed: %s\n", |
| 120 | ifr.ifr_name, strerror(errno)); |
| 121 | close(fd); |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | static int tnl_gen_ioctl(int cmd, const char *name, |
| 126 | void *p, int skiperr) |
| 127 | { |
| 128 | struct ifreq ifr; |
| 129 | int fd; |
| 130 | int err; |
| 131 | |
| 132 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 133 | ifr.ifr_ifru.ifru_data = p; |
| 134 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
| 135 | err = ioctl(fd, cmd, &ifr); |
| 136 | if (err && errno != skiperr) |
| 137 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
| 138 | cmd, strerror(errno)); |
| 139 | close(fd); |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | int tnl_prl_ioctl(int cmd, const char *name, void *p) |
| 144 | { |
| 145 | return tnl_gen_ioctl(cmd, name, p, -1); |
| 146 | } |
| 147 | |
| 148 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) |
| 149 | { |
| 150 | return tnl_gen_ioctl(cmd, name, p, -1); |
| 151 | } |
| 152 | |
| 153 | int tnl_ioctl_get_6rd(const char *name, void *p) |
| 154 | { |
| 155 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
| 156 | } |