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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C)2006 USAGI/WIDE Project
*
* split from ip_tunnel.c
*
* Author:
* Masahide NAKAMURA @USAGI
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/ip.h>
#include <linux/if_tunnel.h>
#include <linux/if_arp.h>
#include "utils.h"
#include "tunnel.h"
#include "json_print.h"
const char *tnl_strproto(__u8 proto)
{
switch (proto) {
case IPPROTO_IPIP:
return "ip";
case IPPROTO_GRE:
return "gre";
case IPPROTO_IPV6:
return "ipv6";
case IPPROTO_ESP:
return "esp";
case IPPROTO_MPLS:
return "mpls";
case 0:
return "any";
default:
return "unknown";
}
}
int tnl_get_ioctl(const char *basedev, void *p)
{
struct ifreq ifr;
int fd;
int err;
strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void *)p;
fd = socket(preferred_family, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "create socket failed: %s\n", strerror(errno));
return -1;
}
err = ioctl(fd, SIOCGETTUNNEL, &ifr);
if (err)
fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev,
strerror(errno));
close(fd);
return err;
}
int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
{
struct ifreq ifr;
int fd;
int err;
if (cmd == SIOCCHGTUNNEL && name[0])
strlcpy(ifr.ifr_name, name, IFNAMSIZ);
else
strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = p;
fd = socket(preferred_family, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "create socket failed: %s\n", strerror(errno));
return -1;
}
err = ioctl(fd, cmd, &ifr);
if (err)
fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name,
strerror(errno));
close(fd);
return err;
}
int tnl_del_ioctl(const char *basedev, const char *name, void *p)
{
struct ifreq ifr;
int fd;
int err;
if (name[0])
strlcpy(ifr.ifr_name, name, IFNAMSIZ);
else
strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = p;
fd = socket(preferred_family, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "create socket failed: %s\n", strerror(errno));
return -1;
}
err = ioctl(fd, SIOCDELTUNNEL, &ifr);
if (err)
fprintf(stderr, "delete tunnel \"%s\" failed: %s\n",
ifr.ifr_name, strerror(errno));
close(fd);
return err;
}
static int tnl_gen_ioctl(int cmd, const char *name,
void *p, int skiperr)
{
struct ifreq ifr;
int fd;
int err;
strlcpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = p;
fd = socket(preferred_family, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "create socket failed: %s\n", strerror(errno));
return -1;
}
err = ioctl(fd, cmd, &ifr);
if (err && errno != skiperr)
fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
cmd, strerror(errno));
close(fd);
return err;
}
int tnl_prl_ioctl(int cmd, const char *name, void *p)
{
return tnl_gen_ioctl(cmd, name, p, -1);
}
int tnl_6rd_ioctl(int cmd, const char *name, void *p)
{
return tnl_gen_ioctl(cmd, name, p, -1);
}
int tnl_ioctl_get_6rd(const char *name, void *p)
{
return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
}
__be32 tnl_parse_key(const char *name, const char *key)
{
unsigned int uval;
if (strchr(key, '.'))
return get_addr32(key);
if (get_unsigned(&uval, key, 0) < 0) {
fprintf(stderr,
"invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n",
name, key);
exit(-1);
}
return htonl(uval);
}
static const char *tnl_encap_str(const char *name, int enabled, int port)
{
static const char ne[][sizeof("no")] = {
[0] = "no",
[1] = "",
};
static char buf[32];
char b1[16];
const char *val;
if (!port) {
val = "auto ";
} else if (port < 0) {
val = "";
} else {
snprintf(b1, sizeof(b1), "%u ", port - 1);
val = b1;
}
snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val);
return buf;
}
void tnl_print_encap(struct rtattr *tb[],
int encap_type, int encap_flags,
int encap_sport, int encap_dport)
{
__u16 type, flags, sport, dport;
if (!tb[encap_type])
return;
type = rta_getattr_u16(tb[encap_type]);
if (type == TUNNEL_ENCAP_NONE)
return;
flags = rta_getattr_u16(tb[encap_flags]);
sport = rta_getattr_u16(tb[encap_sport]);
dport = rta_getattr_u16(tb[encap_dport]);
open_json_object("encap");
print_string(PRINT_FP, NULL, "encap ", NULL);
switch (type) {
case TUNNEL_ENCAP_FOU:
print_string(PRINT_ANY, "type", "%s ", "fou");
break;
case TUNNEL_ENCAP_GUE:
print_string(PRINT_ANY, "type", "%s ", "gue");
break;
default:
print_null(PRINT_ANY, "type", "%s ", "unknown");
break;
}
if (is_json_context()) {
print_uint(PRINT_JSON, "sport", NULL, ntohs(sport));
print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
print_bool(PRINT_JSON, "csum", NULL,
flags & TUNNEL_ENCAP_FLAG_CSUM);
print_bool(PRINT_JSON, "csum6", NULL,
flags & TUNNEL_ENCAP_FLAG_CSUM6);
print_bool(PRINT_JSON, "remcsum", NULL,
flags & TUNNEL_ENCAP_FLAG_REMCSUM);
close_json_object();
} else {
int t;
t = sport ? ntohs(sport) + 1 : 0;
print_string(PRINT_FP, NULL, "%s",
tnl_encap_str("sport", 1, t));
t = ntohs(dport) + 1;
print_string(PRINT_FP, NULL, "%s",
tnl_encap_str("dport", 1, t));
t = flags & TUNNEL_ENCAP_FLAG_CSUM;
print_string(PRINT_FP, NULL, "%s",
tnl_encap_str("csum", t, -1));
t = flags & TUNNEL_ENCAP_FLAG_CSUM6;
print_string(PRINT_FP, NULL, "%s",
tnl_encap_str("csum6", t, -1));
t = flags & TUNNEL_ENCAP_FLAG_REMCSUM;
print_string(PRINT_FP, NULL, "%s",
tnl_encap_str("remcsum", t, -1));
}
}
void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
{
const char *value;
inet_prefix dst;
if (!rta) {
value = "any";
} else if (get_addr_rta(&dst, rta, family)) {
value = "unknown";
} else if (dst.flags & ADDRTYPE_UNSPEC) {
value = "any";
} else {
value = format_host(family, dst.bytelen, dst.data);
if (!value)
value = "unknown";
}
print_string_name_value(name, value);
print_string(PRINT_FP, NULL, " ", NULL);
}
void tnl_print_gre_flags(__u8 proto,
__be16 i_flags, __be16 o_flags,
__be32 i_key, __be32 o_key)
{
if ((i_flags & GRE_KEY) && (o_flags & GRE_KEY) &&
o_key == i_key) {
print_uint(PRINT_ANY, "key", " key %u", ntohl(i_key));
} else {
if (i_flags & GRE_KEY)
print_uint(PRINT_ANY, "ikey", " ikey %u", ntohl(i_key));
if (o_flags & GRE_KEY)
print_uint(PRINT_ANY, "okey", " okey %u", ntohl(o_key));
}
if (proto != IPPROTO_GRE)
return;
open_json_array(PRINT_JSON, "flags");
if (i_flags & GRE_SEQ) {
if (is_json_context())
print_string(PRINT_JSON, NULL, "%s", "rx_drop_ooseq");
else
printf("%s Drop packets out of sequence.", _SL_);
}
if (i_flags & GRE_CSUM) {
if (is_json_context())
print_string(PRINT_JSON, NULL, "%s", "rx_csum");
else
printf("%s Checksum in received packet is required.", _SL_);
}
if (o_flags & GRE_SEQ) {
if (is_json_context())
print_string(PRINT_JSON, NULL, "%s", "tx_seq");
else
printf("%s Sequence packets on output.", _SL_);
}
if (o_flags & GRE_CSUM) {
if (is_json_context())
print_string(PRINT_JSON, NULL, "%s", "tx_csum");
else
printf("%s Checksum output packets.", _SL_);
}
close_json_array(PRINT_JSON, NULL);
}
static void tnl_print_stats(const struct rtnl_link_stats64 *s)
{
printf("%s", _SL_);
printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s",
s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors,
s->rx_fifo_errors, s->multicast, _SL_);
printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld",
s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions,
s->tx_carrier_errors, s->tx_dropped);
}
static int print_nlmsg_tunnel(struct nlmsghdr *n, void *arg)
{
struct tnl_print_nlmsg_info *info = arg;
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr *tb[IFLA_MAX+1];
const char *name, *n1;
if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
return 0;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
return -1;
if (preferred_family == AF_INET) {
switch (ifi->ifi_type) {
case ARPHRD_TUNNEL:
case ARPHRD_IPGRE:
case ARPHRD_SIT:
break;
default:
return 0;
}
} else {
switch (ifi->ifi_type) {
case ARPHRD_TUNNEL6:
case ARPHRD_IP6GRE:
break;
default:
return 0;
}
}
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
if (!tb[IFLA_IFNAME])
return 0;
name = rta_getattr_str(tb[IFLA_IFNAME]);
/* Assume p1->name[IFNAMSIZ] is first field of structure */
n1 = info->p1;
if (n1[0] && strcmp(n1, name))
return 0;
info->ifi = ifi;
info->init(info);
/* TODO: parse netlink attributes */
if (tnl_get_ioctl(name, info->p2))
return 0;
if (!info->match(info))
return 0;
info->print(info->p2);
if (show_stats) {
struct rtnl_link_stats64 s;
if (get_rtnl_link_stats_rta(&s, tb) <= 0)
return -1;
tnl_print_stats(&s);
}
fputc('\n', stdout);
return 0;
}
int do_tunnels_list(struct tnl_print_nlmsg_info *info)
{
new_json_obj(json);
if (rtnl_linkdump_req(&rth, preferred_family) < 0) {
perror("Cannot send dump request\n");
delete_json_obj();
return -1;
}
if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -1;
}
delete_json_obj();
return 0;
}
|