]>
Commit | Line | Data |
---|---|---|
d9bd1bd9 MN |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
ae665a52 | 3 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 8 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 13 | * |
d9bd1bd9 | 14 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
d9bd1bd9 MN |
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> | |
3979ef91 | 28 | #include <errno.h> |
d9bd1bd9 MN |
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; | |
14e97673 SH |
54 | case IPPROTO_ESP: |
55 | strcpy(buf, "esp"); | |
56 | break; | |
0b959b0f YH |
57 | case 0: |
58 | strcpy(buf, "any"); | |
59 | break; | |
d9bd1bd9 MN |
60 | default: |
61 | strcpy(buf, "unknown"); | |
62 | break; | |
63 | } | |
64 | ||
65 | return buf; | |
66 | } | |
67 | ||
d9bd1bd9 MN |
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; | |
cb89c7c7 | 76 | |
d9bd1bd9 | 77 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
78 | if (fd < 0) { |
79 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
80 | return -1; | |
81 | } | |
82 | ||
d9bd1bd9 MN |
83 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
84 | if (err) | |
14645ec2 | 85 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, |
ea71beac SH |
86 | strerror(errno)); |
87 | ||
d9bd1bd9 MN |
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; | |
cb89c7c7 | 103 | |
d9bd1bd9 | 104 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
105 | if (fd < 0) { |
106 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
107 | return -1; | |
108 | } | |
109 | ||
d9bd1bd9 MN |
110 | err = ioctl(fd, cmd, &ifr); |
111 | if (err) | |
14645ec2 | 112 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, |
ea71beac | 113 | strerror(errno)); |
d9bd1bd9 MN |
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); | |
ea71beac | 128 | |
d9bd1bd9 | 129 | ifr.ifr_ifru.ifru_data = p; |
cb89c7c7 | 130 | |
d9bd1bd9 | 131 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
132 | if (fd < 0) { |
133 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
134 | return -1; | |
135 | } | |
136 | ||
d9bd1bd9 MN |
137 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
138 | if (err) | |
14645ec2 | 139 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", |
ea71beac | 140 | ifr.ifr_name, strerror(errno)); |
d9bd1bd9 MN |
141 | close(fd); |
142 | return err; | |
143 | } | |
a07e9912 | 144 | |
0612519e | 145 | static int tnl_gen_ioctl(int cmd, const char *name, |
ea71beac | 146 | void *p, int skiperr) |
a07e9912 SH |
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; | |
cb89c7c7 | 154 | |
a07e9912 | 155 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
156 | if (fd < 0) { |
157 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
158 | return -1; | |
159 | } | |
160 | ||
a07e9912 | 161 | err = ioctl(fd, cmd, &ifr); |
3979ef91 | 162 | if (err && errno != skiperr) |
ea71beac SH |
163 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
164 | cmd, strerror(errno)); | |
a07e9912 SH |
165 | close(fd); |
166 | return err; | |
167 | } | |
b88215c4 AC |
168 | |
169 | int tnl_prl_ioctl(int cmd, const char *name, void *p) | |
170 | { | |
3979ef91 | 171 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
172 | } |
173 | ||
174 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) | |
175 | { | |
3979ef91 | 176 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
177 | } |
178 | ||
179 | int tnl_ioctl_get_6rd(const char *name, void *p) | |
180 | { | |
3979ef91 | 181 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
b88215c4 | 182 | } |