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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* ipfou.c FOU (foo over UDP) support
*
* Authors: Tom Herbert <therbert@google.com>
*/
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <linux/fou.h>
#include <linux/genetlink.h>
#include <linux/ip.h>
#include <arpa/inet.h>
#include "libgenl.h"
#include "utils.h"
#include "ip_common.h"
#include "json_print.h"
static void usage(void)
{
fprintf(stderr,
"Usage: ip fou add port PORT { ipproto PROTO | gue }\n"
" [ local IFADDR ] [ peer IFADDR ]\n"
" [ peer_port PORT ] [ dev IFNAME ]\n"
" ip fou del port PORT [ local IFADDR ]\n"
" [ peer IFADDR ] [ peer_port PORT ]\n"
" [ dev IFNAME ]\n"
" ip fou show\n"
"\n"
"Where: PROTO { ipproto-name | 1..255 }\n"
" PORT { 1..65535 }\n"
" IFADDR { addr }\n");
exit(-1);
}
/* netlink socket */
static struct rtnl_handle genl_rth = { .fd = -1 };
static int genl_family = -1;
#define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \
GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
FOU_GENL_VERSION, _cmd, _flags)
static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n,
bool adding)
{
const char *local = NULL, *peer = NULL;
__u16 port, peer_port = 0;
__u8 family = preferred_family;
bool gue_set = false;
int ipproto_set = 0;
__u8 ipproto, type;
int port_set = 0;
int index = 0;
if (preferred_family == AF_UNSPEC) {
family = AF_INET;
}
while (argc > 0) {
if (!matches(*argv, "port")) {
NEXT_ARG();
if (get_be16(&port, *argv, 0) || port == 0)
invarg("invalid port", *argv);
port_set = 1;
} else if (!matches(*argv, "ipproto")) {
struct protoent *servptr;
NEXT_ARG();
servptr = getprotobyname(*argv);
if (servptr)
ipproto = servptr->p_proto;
else if (get_u8(&ipproto, *argv, 0) || ipproto == 0)
invarg("invalid ipproto", *argv);
ipproto_set = 1;
} else if (!matches(*argv, "gue")) {
gue_set = true;
} else if (!matches(*argv, "-6")) {
family = AF_INET6;
} else if (!matches(*argv, "local")) {
NEXT_ARG();
local = *argv;
} else if (!matches(*argv, "peer")) {
NEXT_ARG();
peer = *argv;
} else if (!matches(*argv, "peer_port")) {
NEXT_ARG();
if (get_be16(&peer_port, *argv, 0) || peer_port == 0)
invarg("invalid peer port", *argv);
} else if (!matches(*argv, "dev")) {
const char *ifname;
NEXT_ARG();
ifname = *argv;
if (check_ifname(ifname)) {
fprintf(stderr, "fou: invalid device name\n");
exit(EXIT_FAILURE);
}
index = ll_name_to_index(ifname);
if (!index) {
fprintf(stderr, "fou: unknown device name\n");
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr
, "fou: unknown command \"%s\"?\n", *argv);
usage();
return -1;
}
argc--, argv++;
}
if (!port_set) {
fprintf(stderr, "fou: missing port\n");
return -1;
}
if (!ipproto_set && !gue_set && adding) {
fprintf(stderr, "fou: must set ipproto or gue\n");
return -1;
}
if (ipproto_set && gue_set) {
fprintf(stderr, "fou: cannot set ipproto and gue\n");
return -1;
}
if ((peer_port && !peer) || (peer && !peer_port)) {
fprintf(stderr, "fou: both peer and peer port must be set\n");
return -1;
}
type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT;
addattr16(n, 1024, FOU_ATTR_PORT, port);
addattr8(n, 1024, FOU_ATTR_TYPE, type);
addattr8(n, 1024, FOU_ATTR_AF, family);
if (ipproto_set)
addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto);
if (local) {
inet_prefix local_addr;
__u8 attr_type = family == AF_INET ? FOU_ATTR_LOCAL_V4 :
FOU_ATTR_LOCAL_V6;
if (get_addr(&local_addr, local, family)) {
fprintf(stderr, "fou: parsing local address failed\n");
exit(EXIT_FAILURE);
}
addattr_l(n, 1024, attr_type, &local_addr.data,
local_addr.bytelen);
}
if (peer) {
inet_prefix peer_addr;
__u8 attr_type = family == AF_INET ? FOU_ATTR_PEER_V4 :
FOU_ATTR_PEER_V6;
if (get_addr(&peer_addr, peer, family)) {
fprintf(stderr, "fou: parsing peer address failed\n");
exit(EXIT_FAILURE);
}
addattr_l(n, 1024, attr_type, &peer_addr.data,
peer_addr.bytelen);
if (peer_port)
addattr16(n, 1024, FOU_ATTR_PEER_PORT, peer_port);
}
if (index)
addattr32(n, 1024, FOU_ATTR_IFINDEX, index);
return 0;
}
static int do_add(int argc, char **argv)
{
FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST);
fou_parse_opt(argc, argv, &req.n, true);
if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
return -2;
return 0;
}
static int do_del(int argc, char **argv)
{
FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST);
fou_parse_opt(argc, argv, &req.n, false);
if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
return -2;
return 0;
}
static int print_fou_mapping(struct nlmsghdr *n, void *arg)
{
__u8 family = AF_INET, local_attr_type, peer_attr_type, byte_len;
struct rtattr *tb[FOU_ATTR_MAX + 1];
__u8 empty_buf[16] = {0};
struct genlmsghdr *ghdr;
int len = n->nlmsg_len;
if (n->nlmsg_type != genl_family)
return 0;
len -= NLMSG_LENGTH(GENL_HDRLEN);
if (len < 0)
return -1;
ghdr = NLMSG_DATA(n);
parse_rtattr(tb, FOU_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
open_json_object(NULL);
if (tb[FOU_ATTR_PORT])
print_uint(PRINT_ANY, "port", "port %u",
ntohs(rta_getattr_u16(tb[FOU_ATTR_PORT])));
if (tb[FOU_ATTR_TYPE] &&
rta_getattr_u8(tb[FOU_ATTR_TYPE]) == FOU_ENCAP_GUE)
print_null(PRINT_ANY, "gue", " gue", NULL);
else if (tb[FOU_ATTR_IPPROTO])
print_uint(PRINT_ANY, "ipproto",
" ipproto %u", rta_getattr_u8(tb[FOU_ATTR_IPPROTO]));
if (tb[FOU_ATTR_AF]) {
family = rta_getattr_u8(tb[FOU_ATTR_AF]);
print_string(PRINT_JSON, "family", NULL,
family_name(family));
if (family == AF_INET6)
print_string(PRINT_FP, NULL,
" -6", NULL);
}
local_attr_type = family == AF_INET ? FOU_ATTR_LOCAL_V4 :
FOU_ATTR_LOCAL_V6;
peer_attr_type = family == AF_INET ? FOU_ATTR_PEER_V4 :
FOU_ATTR_PEER_V6;
byte_len = af_bit_len(family) / 8;
if (tb[local_attr_type] && memcmp(RTA_DATA(tb[local_attr_type]),
empty_buf, byte_len)) {
print_string(PRINT_ANY, "local", " local %s",
format_host_rta(family, tb[local_attr_type]));
}
if (tb[peer_attr_type] && memcmp(RTA_DATA(tb[peer_attr_type]),
empty_buf, byte_len)) {
print_string(PRINT_ANY, "peer", " peer %s",
format_host_rta(family, tb[peer_attr_type]));
}
if (tb[FOU_ATTR_PEER_PORT]) {
__u16 p_port = ntohs(rta_getattr_u16(tb[FOU_ATTR_PEER_PORT]));
if (p_port)
print_uint(PRINT_ANY, "peer_port", " peer_port %u",
p_port);
}
if (tb[FOU_ATTR_IFINDEX]) {
int index = rta_getattr_s32(tb[FOU_ATTR_IFINDEX]);
if (index) {
const char *ifname;
ifname = ll_index_to_name(index);
if (ifname)
print_string(PRINT_ANY, "dev", " dev %s",
ifname);
}
}
print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
return 0;
}
static int do_show(int argc, char **argv)
{
FOU_REQUEST(req, 4096, FOU_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP);
if (argc > 0) {
fprintf(stderr,
"\"ip fou show\" does not take any arguments.\n");
return -1;
}
if (rtnl_send(&genl_rth, &req.n, req.n.nlmsg_len) < 0) {
perror("Cannot send show request");
exit(1);
}
new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();
fflush(stdout);
return 0;
}
int do_ipfou(int argc, char **argv)
{
if (argc < 1)
usage();
if (matches(*argv, "help") == 0)
usage();
if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family))
exit(1);
if (matches(*argv, "add") == 0)
return do_add(argc-1, argv+1);
if (matches(*argv, "delete") == 0)
return do_del(argc-1, argv+1);
if (matches(*argv, "show") == 0)
return do_show(argc-1, argv+1);
fprintf(stderr,
"Command \"%s\" is unknown, try \"ip fou help\".\n", *argv);
exit(-1);
}
|