-
Notifications
You must be signed in to change notification settings - Fork 5
/
ethiptunnel.c
301 lines (260 loc) · 6.44 KB
/
ethiptunnel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_tunnel.h>
#define IPPROTO_ETHERIP 97
enum {
ACTION_NONE = 0,
ACTION_ADD = 1,
ACTION_DEL = 2,
ACTION_CHG = 3,
ACTION_LIST = 4 };
void __init_tunnel_params(struct ip_tunnel_parm *p, char *devname, __u32 daddr, __u32 saddr, int set_saddr, int ttl)
{
if (devname)
strncpy(p->name, devname, IFNAMSIZ);
else p->name[0] = 0;
p->name[IFNAMSIZ-1] = 0;
p->iph.version = 4;
p->iph.protocol = IPPROTO_ETHERIP;
p->iph.ihl = 5;
if (daddr != 0)
p->iph.daddr = daddr;
if (set_saddr)
p->iph.saddr = saddr;
if ((ttl >= 0) && (ttl < 256))
p->iph.ttl = ttl;
}
int __do_tunnel_action(int cmd, struct ifreq *ifr)
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int err;
if (fd == -1) {
perror("socket");
return 1;
}
err = ioctl(fd, cmd, ifr);
if (err < 0)
perror("ioctl");
return (err < 0);
}
int do_tunnel_add(char *devname, __u32 daddr, __u32 saddr, int ttl)
{
struct ifreq ifr;
struct ip_tunnel_parm p;
strncpy(ifr.ifr_name, "ethip0", IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void *)&p;
p.iph.ttl = 0;
__init_tunnel_params(&p, devname, daddr, saddr, 1, ttl);
return __do_tunnel_action(SIOCADDTUNNEL, &ifr);
}
int do_tunnel_change (char *devname, __u32 daddr, __u32 saddr, int set_saddr, int ttl)
{
struct ifreq ifr;
struct ip_tunnel_parm p;
int fd;
strncpy(ifr.ifr_name, devname, IFNAMSIZ);
p.name[IFNAMSIZ-1] = 0;
ifr.ifr_ifru.ifru_data = (void *)&p;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
perror("socket");
return 1;
}
if (ioctl(fd, SIOCGETTUNNEL, &ifr) != 0) {
perror("ioctl");
close(fd);
return 1;
}
close(fd);
__init_tunnel_params(&p, devname, daddr, saddr, set_saddr, ttl);
return __do_tunnel_action(SIOCCHGTUNNEL, &ifr);
}
int do_tunnel_del(char *devname)
{
struct ifreq ifr;
strncpy(ifr.ifr_name, devname, IFNAMSIZ);
return __do_tunnel_action(SIOCDELTUNNEL, &ifr);
}
int do_tunnel_list()
{
int sd;
FILE *fd = fopen("/proc/net/dev", "r");
char line[2048];
char dev[IFNAMSIZ];
int i,j,k,found = 0;
struct ifreq ifr;
struct ip_tunnel_parm p;
struct in_addr daddr_s;
char ttl_str[8], saddr_str[16];
char *result;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == -1) {
perror("socket");
return 1;
}
if (fd == NULL) {
perror("fopen");
close(sd);
return 1;
}
/* skip first 2 lines */
result = fgets(line, 2048, fd);
result = fgets(line, 2048, fd);
while (fgets(line, 2048, fd)) {
for (i=0;line[i] == ' ';++i);
for (j=0;(line[i+j] != ':') && (line[i+j] != 0);++j) {
if (j > IFNAMSIZ-1) break;
dev[j] = line[i+j];
}
dev[j] = 0;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void *)&p;
if (ioctl(sd, SIOCGETTUNNEL, &ifr) != 0) continue;
if (p.iph.protocol != IPPROTO_ETHERIP) continue;
for (k=0;k<IFNAMSIZ-1;++k) if (p.name[k] == 0) break;
for ( ;k<IFNAMSIZ-1;++k) p.name[k] = ' ';
p.name[k] = 0;
daddr_s.s_addr = p.iph.daddr;
if (p.iph.saddr != 0)
inet_ntop(AF_INET, (void*)&p.iph.saddr, saddr_str, 16);
else
strcpy(saddr_str, "any ");
if (p.iph.ttl == 0)
strcpy(ttl_str, "default");
else
snprintf(ttl_str, 7, "%d", p.iph.ttl);
if (found == 0)
printf("Device\t\tDestination\tSource\t\tTTL\n");
printf("%s\t%s\t%s\t%s\n", p.name,
inet_ntoa(daddr_s), saddr_str, ttl_str);
++found;
}
if (found == 0)
printf("No etherip devices configured\n");
close(sd);
fclose(fd);
return 0;
}
void usage(const char* prog)
{
printf("EtherIP Tunnel Setup Tool 0.1\n");
printf("Usage: %s [-a|-r|-c|-l] [-d dest] [-n devname] [-s <source>] [-t ttl]\n", prog);
printf("-a add a new device\n");
printf("-r remove a device\n");
printf("-c change the destination address of a device\n");
printf("-l list devices (other arguments are ignored)\n");
printf("-d <addr> specify tunnel destination (use with -a)\n");
printf("-s <addr> specify tunnel source address\n");
printf("-n <devname> specify the name of the tunnel device\n");
printf("-t <ttl> specify ttl for tunnel packets (0 means system default)\n");
}
int action_error()
{
printf("Only one action can be specified\n");
return 1;
}
int main(int argc, char **argv)
{
int opt;
char *devname = NULL;
int action = ACTION_NONE;
struct hostent *host;
__u32 daddr = 0;
__u32 saddr = 0;
int set_saddr = 0;
int ttl = -1;
do {
opt = getopt(argc, argv, "hard:n:lct:s:");
switch (opt) {
case 'h':
usage(argv[0]);
return 0;
case 'a':
if (action != ACTION_NONE)
return action_error();
action = ACTION_ADD;
break;
case 'r':
if (action != ACTION_NONE)
return action_error();
action = ACTION_DEL;
break;
case 'c':
if (action != ACTION_NONE)
return action_error();
action = ACTION_CHG;
break;
case 'l':
if (action != ACTION_NONE)
return action_error();
action = ACTION_LIST;
break;
case 'd':
host = gethostbyname(optarg);
if (host == NULL) {
printf("Destination host not "
"found: %s\n", optarg);
return 1;
}
daddr = ((struct in_addr*)host->h_addr)->s_addr;
break;
case 's':
host = gethostbyname(optarg);
if (host == NULL) {
printf("Source host not "
"found: %s\n", optarg);
return 1;
}
set_saddr = 1;
saddr = ((struct in_addr*)host->h_addr)->s_addr;
break;
case 'n':
devname = optarg;
break;
case 't':
ttl = atoi(optarg);
if ((ttl < 0) || (ttl > 255)) {
printf("TTL value must be between 0 and 255\n");
usage(argv[0]);
return 1;
}
case -1:
break;
default:
usage(argv[0]);
return 1;
}
} while (opt != -1);
if (action == ACTION_ADD) {
if (daddr == 0) {
printf("A valid tunnel destination must be specified\n");
return 1;
} else return do_tunnel_add(devname, daddr, saddr, ttl);
}
if (action == ACTION_DEL) {
if (devname == 0) {
printf("Please specify the devicename to delete\n");
return 1;
} else return do_tunnel_del(devname);
}
if (action == ACTION_CHG) {
if (devname == 0) {
printf("A valid devicename must be specified\n");
return 1;
} else return do_tunnel_change(devname, daddr, saddr, set_saddr, ttl);
}
if (action == ACTION_LIST)
return do_tunnel_list();
printf("Please specify an action\n");
return 1;
}