-
Notifications
You must be signed in to change notification settings - Fork 31
/
eoipcr.c
346 lines (310 loc) · 8.48 KB
/
eoipcr.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <linux/if_tunnel.h>
#include "version.h"
#include "libnetlink.h"
int print_link(const struct sockaddr_nl *who __attribute((unused)), struct nlmsghdr *n, void *arg __attribute((unused))) {
struct ndmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr *ifattr[IFLA_MAX+1];
struct rtattr *ifinfo[IFLA_INFO_MAX+1];
struct rtattr *ifgreo[IFLA_GRE_MAX+1];
const char *ifname;
const char *kind;
struct in_addr sip,dip;
struct ifinfomsg *ifi;
sip.s_addr=dip.s_addr=htonl(0);
if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) {
fprintf(stderr, "Not RTM_xxxLINK: %08x %08x %08x\n", n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return 0;
}
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
ifi=(void *)r;
parse_rtattr(ifattr, IFLA_MAX, IFLA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
if (ifattr[IFLA_IFNAME])
ifname=rta_getattr_str(ifattr[IFLA_IFNAME]);
else
ifname="";
if (ifattr[IFLA_LINKINFO])
parse_rtattr(ifinfo, IFLA_INFO_MAX, (void *)rta_getattr_str(ifattr[IFLA_LINKINFO]), ifattr[IFLA_LINKINFO]->rta_len);
else
memset(ifinfo,0,sizeof ifinfo);
if (ifinfo[IFLA_INFO_KIND])
kind=rta_getattr_str(ifinfo[IFLA_INFO_KIND]);
else
kind="";
if (!strcmp(kind,"eoip") && ifinfo[IFLA_INFO_DATA]) {
char ts[IFNAMSIZ],td[IFNAMSIZ];
uint8_t ttl=0,tos=0;
int tunid=0;
int link=0;
parse_rtattr(ifgreo, IFLA_GRE_MAX, (void *)rta_getattr_str(ifinfo[IFLA_INFO_DATA]), ifinfo[IFLA_INFO_DATA]->rta_len);
if (ifgreo[IFLA_GRE_LINK])
link=rta_getattr_u32(ifgreo[IFLA_GRE_LINK]);
if (ifgreo[IFLA_GRE_TOS])
tos=rta_getattr_u8(ifgreo[IFLA_GRE_TOS]);
if (ifgreo[IFLA_GRE_TTL])
ttl=rta_getattr_u8(ifgreo[IFLA_GRE_TTL]);
if (ifgreo[IFLA_GRE_LOCAL])
sip.s_addr=rta_getattr_u32(ifgreo[IFLA_GRE_LOCAL]);
if (ifgreo[IFLA_GRE_REMOTE])
dip.s_addr=rta_getattr_u32(ifgreo[IFLA_GRE_REMOTE]);
if (ifgreo[IFLA_GRE_IKEY])
tunid=rta_getattr_u32(ifgreo[IFLA_GRE_IKEY]);
strcpy(ts,inet_ntoa(sip));
strcpy(td,inet_ntoa(dip));
printf("%d: %s@%d: link/%s %s remote %s tunnel-id %d ttl %d tos %d\n",ifi->ifi_index,ifname,link,kind,ts,td,tunid,ttl,tos);
}
return 0;
}
void list(void) {
struct rtnl_handle rth;
if (rtnl_open(&rth,0)) {
perror("Cannot open rtnetlink");
return;
}
if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
perror("Cannot send dump request");
return;
}
if (rtnl_dump_filter(&rth, print_link, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
return;
}
rtnl_close(&rth);
}
static int eoip_add(int excl,char *name,uint16_t tunnelid,uint32_t sip,uint32_t dip,uint32_t link,uint8_t ttl,uint8_t tos) {
struct {
struct nlmsghdr msg;
struct ifinfomsg ifi;
uint8_t buf[1024];
} req;
struct rtnl_handle rth = { .fd = -1 };
struct rtattr *lnfo;
struct rtattr *data;
memset(&req, 0, sizeof(req));
req.msg.nlmsg_type = RTM_NEWLINK;
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | (excl ? NLM_F_EXCL : 0);
req.ifi.ifi_family = AF_UNSPEC;
addattr_l(&req.msg, sizeof(req), IFLA_IFNAME, name, strlen(name));
lnfo = NLMSG_TAIL(&req.msg);
addattr_l(&req.msg, sizeof(req), IFLA_LINKINFO, NULL, 0);
addattr_l(&req.msg, sizeof(req), IFLA_INFO_KIND, "eoip", strlen("eoip"));
data = NLMSG_TAIL(&req.msg);
addattr_l(&req.msg, sizeof(req), IFLA_INFO_DATA, NULL, 0);
addattr32(&req.msg, sizeof(req), IFLA_GRE_IKEY, tunnelid);
addattr32(&req.msg, sizeof(req), IFLA_GRE_LOCAL, sip);
addattr32(&req.msg, sizeof(req), IFLA_GRE_REMOTE, dip);
addattr32(&req.msg, sizeof(req), IFLA_GRE_LINK, link);
addattr8(&req.msg, sizeof(req), IFLA_GRE_TTL, ttl);
addattr8(&req.msg, sizeof(req), IFLA_GRE_TOS, tos);
data->rta_len = (void *)NLMSG_TAIL(&req.msg) - (void *)data;
lnfo->rta_len = (void *)NLMSG_TAIL(&req.msg) - (void *)lnfo;
if (rtnl_open(&rth, 0)) {
fprintf(stderr, "cannot open netlink\n");
return -1;
}
if (rtnl_talk(&rth, &req.msg, 0, 0, NULL) < 0) {
fprintf(stderr, "failed to talk to netlink!\n");
return -1;
}
rtnl_close(&rth);
return 0;
}
typedef enum {
E_AMBIGUOUS = -1,
E_NONE = 0,
C_LIST,
C_ADD,
C_SET,
C_VER,
C_HELP,
P_LOCAL,
P_REMOTE,
P_TUNNELID,
P_TTL,
P_TOS,
P_LINK,
P_NAME,
} e_cmd;
typedef struct {
char *cmd;
e_cmd cid;
} s_cmd;
static s_cmd cmds[] = {
{ .cmd = "list", .cid = C_LIST, },
{ .cmd = "show", .cid = C_LIST, },
{ .cmd = "add", .cid = C_ADD, },
{ .cmd = "new", .cid = C_ADD, },
{ .cmd = "set", .cid = C_SET, },
{ .cmd = "change", .cid = C_SET, },
{ .cmd = "version", .cid = C_VER, },
{ .cmd = "--version", .cid = C_VER, },
{ .cmd = "-v", .cid = C_VER, },
{ .cmd = "help", .cid = C_HELP, },
{ .cmd = "--help", .cid = C_HELP, },
{ .cmd = "-h", .cid = C_HELP, },
{ .cmd = NULL, .cid = 0, },
};
static s_cmd prms[] = {
{ .cmd = "local", .cid = P_LOCAL, },
{ .cmd = "remote", .cid = P_REMOTE, },
{ .cmd = "tunnel-id", .cid = P_TUNNELID, },
{ .cmd = "tid", .cid = P_TUNNELID, },
{ .cmd = "ttl", .cid = P_TTL, },
{ .cmd = "tos", .cid = P_TOS, },
{ .cmd = "link", .cid = P_LINK, },
{ .cmd = "name", .cid = P_NAME, },
{ .cmd = NULL, .cid = 0, },
};
static e_cmd find_cmd(s_cmd *l, char *pcmd) {
int cnt=0;
int len=0;
e_cmd match=E_NONE;
while (l && pcmd && l->cmd) {
char *ha = l->cmd;
char *ne = pcmd;
int clen = 0;
while (*ha && *ne && *ha == *ne)
ha++, ne++, clen++;
if (!*ne) {
if (clen && clen > len) {
cnt = 0;
len = clen;
match = l->cid;
}
if (clen && clen == len)
cnt++;
}
l++;
}
if (cnt == 1)
return match;
return cnt ? E_AMBIGUOUS : E_NONE;
}
static void version(void) {
printf("eoip version %s\n",EOIP_VERSION);
}
static void usage(char *me) {
printf("usage:\n"
"\t%s add [name <name>] tunnel-id <id> [local <ip>] remote <ip> [ttl <ttl>] [tos <tos>] [link <ifindex|ifname>]\n"
"\t%s set name <name> tunnel-id <id> [local <ip>] remote <ip> [ttl <ttl>] [tos <tos>] [link <ifindex|ifname>]\n"
"\t%s list\n"
"\t%s version\n"
,me,me,me,me);
}
int main(int argc,char **argv) {
if (argc >= 1) {
e_cmd c = (argc == 1) ? C_LIST : find_cmd(cmds, argv[1]);
int excl = 1;
switch (c) {
dafeutl:
default:
case C_HELP:
usage(argv[0]);
return 0;
case C_VER:
version();
return 0;
case C_LIST:
if (argc > 2)
goto dafeutl;
list();
return 0;
case C_SET:
excl = 0;
// fall through
case C_ADD: {
char ifname[IFNAMSIZ + 1] = "";
uint32_t sip = htonl(0), dip = htonl(0);
uint32_t tid = ~0;
uint8_t ttl=0, tos=0;
uint32_t link = 0;
int i=2;
while (i < argc) {
e_cmd p = find_cmd(prms, argv[i]);
int noarg=!((i + 1) < argc);
struct in_addr iad;
switch (p) {
default:
break;
case E_NONE:
printf("unknown option: %s\n", argv[i]);
return 0;
case E_AMBIGUOUS:
printf("option is ambiguous: %s\n", argv[i]);
return 0;
}
if (noarg) {
printf("option: %s requires an argument\n", argv[i]);
return 0;
}
switch (p) {
default:
printf("BUG!\n");
return 0;
case P_NAME:
ifname[(sizeof ifname)-1] = 0;
strncpy(ifname,argv[i + 1], IFNAMSIZ);
break;
case P_TTL:
ttl=atoi(argv[i + 1]);
break;
case P_TOS:
tos=atoi(argv[i + 1]);
break;
case P_LINK:
// convert ifname to ifinidex, also support numeric arg
link=if_nametoindex(argv[i + 1]);
if (!link)
link=atoi(argv[i + 1]);
if (!link) {
printf("invalid interface name/index: %s\n", argv[i + 1]);
return 0;
}
break;
case P_TUNNELID:
tid=atoi(argv[i + 1]);
break;
case P_LOCAL:
if (!inet_aton(argv[i + 1], &iad)) {
printf("invalid ip address: %s\n", argv[i + 1]);
return 0;
}
sip = iad.s_addr;
break;
case P_REMOTE:
if (!inet_aton(argv[i + 1], &iad)) {
printf("invalid ip address: %s\n", argv[i + 1]);
return 0;
}
dip = iad.s_addr;
break;
}
i += 2;
}
if (tid > 0xffff) {
if (tid == ~0U)
printf("tunnel-id is mandatory parameter\n");
else
printf("invalid tunnel-id value: %u\n",tid);
return 0;
}
// tunnel id is in host byte order, addresses are in net byte order
eoip_add(excl,ifname,tid,sip,dip,link,ttl,tos);
return 0;
}
}
}
return 0;
}