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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* iplink_geneve.c GENEVE device support
*
* Authors: John W. Linville <linville@tuxdriver.com>
*/
#include <stdio.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"
#define GENEVE_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
static void print_explain(FILE *f)
{
fprintf(f,
"Usage: ... geneve id VNI\n"
" remote ADDR\n"
" [ ttl TTL ]\n"
" [ tos TOS ]\n"
" [ df DF ]\n"
" [ flowlabel LABEL ]\n"
" [ dstport PORT ]\n"
" [ [no]external ]\n"
" [ [no]udpcsum ]\n"
" [ [no]udp6zerocsumtx ]\n"
" [ [no]udp6zerocsumrx ]\n"
" [ innerprotoinherit ]\n"
"\n"
"Where: VNI := 0-16777215\n"
" ADDR := IP_ADDRESS\n"
" TOS := { NUMBER | inherit }\n"
" TTL := { 1..255 | auto | inherit }\n"
" DF := { unset | set | inherit }\n"
" LABEL := 0-1048575\n"
);
}
static void explain(void)
{
print_explain(stderr);
}
static void check_duparg(__u64 *attrs, int type, const char *key,
const char *argv)
{
if (!GENEVE_ATTRSET(*attrs, type)) {
*attrs |= (1L << type);
return;
}
duparg2(key, argv);
}
static int geneve_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n)
{
inet_prefix daddr;
__u32 vni = 0;
__u32 label = 0;
__u8 ttl = 0;
__u8 tos = 0;
__u16 dstport = 0;
bool metadata = 0;
__u8 udpcsum = 0;
__u8 udp6zerocsumtx = 0;
__u8 udp6zerocsumrx = 0;
__u64 attrs = 0;
bool set_op = (n->nlmsg_type == RTM_NEWLINK &&
!(n->nlmsg_flags & NLM_F_CREATE));
bool inner_proto_inherit = false;
inet_prefix_reset(&daddr);
while (argc > 0) {
if (!matches(*argv, "id") ||
!matches(*argv, "vni")) {
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_ID, "id", *argv);
if (get_u32(&vni, *argv, 0) ||
vni >= 1u << 24)
invarg("invalid id", *argv);
} else if (!matches(*argv, "remote")) {
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_REMOTE, "remote",
*argv);
get_addr(&daddr, *argv, AF_UNSPEC);
if (!is_addrtype_inet_not_multi(&daddr))
invarg("invalid remote address", *argv);
} else if (!matches(*argv, "ttl") ||
!matches(*argv, "hoplimit")) {
unsigned int uval;
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_TTL, "ttl", *argv);
if (strcmp(*argv, "inherit") == 0) {
addattr8(n, 1024, IFLA_GENEVE_TTL_INHERIT, 1);
} else if (strcmp(*argv, "auto") != 0) {
if (get_unsigned(&uval, *argv, 0))
invarg("invalid TTL", *argv);
if (uval > 255)
invarg("TTL must be <= 255", *argv);
ttl = uval;
}
} else if (!matches(*argv, "tos") ||
!matches(*argv, "dsfield")) {
__u32 uval;
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_TOS, "tos", *argv);
if (strcmp(*argv, "inherit") != 0) {
if (rtnl_dsfield_a2n(&uval, *argv))
invarg("bad TOS value", *argv);
tos = uval;
} else
tos = 1;
} else if (!matches(*argv, "df")) {
enum ifla_geneve_df df;
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_DF, "df", *argv);
if (strcmp(*argv, "unset") == 0)
df = GENEVE_DF_UNSET;
else if (strcmp(*argv, "set") == 0)
df = GENEVE_DF_SET;
else if (strcmp(*argv, "inherit") == 0)
df = GENEVE_DF_INHERIT;
else
invarg("DF must be 'unset', 'set' or 'inherit'",
*argv);
addattr8(n, 1024, IFLA_GENEVE_DF, df);
} else if (!matches(*argv, "label") ||
!matches(*argv, "flowlabel")) {
__u32 uval;
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_LABEL, "flowlabel",
*argv);
if (get_u32(&uval, *argv, 0) ||
(uval & ~LABEL_MAX_MASK))
invarg("invalid flowlabel", *argv);
label = htonl(uval);
} else if (!matches(*argv, "dstport")) {
NEXT_ARG();
check_duparg(&attrs, IFLA_GENEVE_PORT, "dstport",
*argv);
if (get_u16(&dstport, *argv, 0))
invarg("dstport", *argv);
} else if (!matches(*argv, "external")) {
check_duparg(&attrs, IFLA_GENEVE_COLLECT_METADATA,
*argv, *argv);
metadata = true;
} else if (!matches(*argv, "noexternal")) {
check_duparg(&attrs, IFLA_GENEVE_COLLECT_METADATA,
*argv, *argv);
metadata = false;
} else if (!matches(*argv, "udpcsum")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_CSUM, *argv,
*argv);
udpcsum = 1;
} else if (!matches(*argv, "noudpcsum")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_CSUM, *argv,
*argv);
udpcsum = 0;
} else if (!matches(*argv, "udp6zerocsumtx")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
*argv, *argv);
udp6zerocsumtx = 1;
} else if (!matches(*argv, "noudp6zerocsumtx")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
*argv, *argv);
udp6zerocsumtx = 0;
} else if (!matches(*argv, "udp6zerocsumrx")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
*argv, *argv);
udp6zerocsumrx = 1;
} else if (!matches(*argv, "noudp6zerocsumrx")) {
check_duparg(&attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
*argv, *argv);
udp6zerocsumrx = 0;
} else if (!strcmp(*argv, "innerprotoinherit")) {
check_duparg(&attrs, IFLA_GENEVE_INNER_PROTO_INHERIT,
*argv, *argv);
inner_proto_inherit = true;
} else if (matches(*argv, "help") == 0) {
explain();
return -1;
} else {
fprintf(stderr, "geneve: unknown command \"%s\"?\n", *argv);
explain();
return -1;
}
argc--, argv++;
}
if (metadata && GENEVE_ATTRSET(attrs, IFLA_GENEVE_ID)) {
fprintf(stderr, "geneve: both 'external' and vni cannot be specified\n");
return -1;
}
if (!metadata) {
/* parameter checking make sense only for full geneve tunnels */
if (!GENEVE_ATTRSET(attrs, IFLA_GENEVE_ID)) {
fprintf(stderr, "geneve: missing virtual network identifier\n");
return -1;
}
/* If we are modifying the geneve device, then we only need the
* ID (VNI) to identify the geneve device, and we do not need
* the remote IP.
*/
if (!set_op && !is_addrtype_inet(&daddr)) {
fprintf(stderr, "geneve: remote link partner not specified\n");
return -1;
}
}
addattr32(n, 1024, IFLA_GENEVE_ID, vni);
if (is_addrtype_inet(&daddr)) {
int type = (daddr.family == AF_INET) ? IFLA_GENEVE_REMOTE :
IFLA_GENEVE_REMOTE6;
addattr_l(n, 1024, type, daddr.data, daddr.bytelen);
}
if (!set_op || GENEVE_ATTRSET(attrs, IFLA_GENEVE_LABEL))
addattr32(n, 1024, IFLA_GENEVE_LABEL, label);
if (!set_op || GENEVE_ATTRSET(attrs, IFLA_GENEVE_TTL))
addattr8(n, 1024, IFLA_GENEVE_TTL, ttl);
if (!set_op || GENEVE_ATTRSET(attrs, IFLA_GENEVE_TOS))
addattr8(n, 1024, IFLA_GENEVE_TOS, tos);
if (dstport)
addattr16(n, 1024, IFLA_GENEVE_PORT, htons(dstport));
if (metadata)
addattr(n, 1024, IFLA_GENEVE_COLLECT_METADATA);
if (inner_proto_inherit)
addattr(n, 1024, IFLA_GENEVE_INNER_PROTO_INHERIT);
if (GENEVE_ATTRSET(attrs, IFLA_GENEVE_UDP_CSUM))
addattr8(n, 1024, IFLA_GENEVE_UDP_CSUM, udpcsum);
if (GENEVE_ATTRSET(attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_TX))
addattr8(n, 1024, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, udp6zerocsumtx);
if (GENEVE_ATTRSET(attrs, IFLA_GENEVE_UDP_ZERO_CSUM6_RX))
addattr8(n, 1024, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, udp6zerocsumrx);
return 0;
}
static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
__u8 ttl = 0;
__u8 tos = 0;
if (!tb)
return;
if (tb[IFLA_GENEVE_COLLECT_METADATA]) {
print_bool(PRINT_ANY, "external", "external ", true);
}
if (tb[IFLA_GENEVE_ID] &&
RTA_PAYLOAD(tb[IFLA_GENEVE_ID]) >= sizeof(__u32)) {
print_uint(PRINT_ANY, "id", "id %u ", rta_getattr_u32(tb[IFLA_GENEVE_ID]));
}
if (tb[IFLA_GENEVE_REMOTE]) {
__be32 addr = rta_getattr_u32(tb[IFLA_GENEVE_REMOTE]);
if (addr)
print_string(PRINT_ANY,
"remote",
"remote %s ",
format_host(AF_INET, 4, &addr));
} else if (tb[IFLA_GENEVE_REMOTE6]) {
struct in6_addr addr;
memcpy(&addr, RTA_DATA(tb[IFLA_GENEVE_REMOTE6]), sizeof(struct in6_addr));
if (!IN6_IS_ADDR_UNSPECIFIED(&addr)) {
if (!IN6_IS_ADDR_MULTICAST(&addr))
print_string(PRINT_ANY,
"remote6",
"remote %s ",
format_host(AF_INET6,
sizeof(struct in6_addr),
&addr));
}
}
if (tb[IFLA_GENEVE_TTL_INHERIT] &&
rta_getattr_u8(tb[IFLA_GENEVE_TTL_INHERIT])) {
print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
} else if (tb[IFLA_GENEVE_TTL]) {
ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]);
if (is_json_context() || ttl)
print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
else
print_string(PRINT_FP, NULL, "ttl %s ", "auto");
}
if (tb[IFLA_GENEVE_TOS])
tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]);
if (tos) {
if (is_json_context() || tos != 1)
print_0xhex(PRINT_ANY, "tos", "tos %#llx ", tos);
else
print_string(PRINT_FP, NULL, "tos %s ", "inherit");
}
if (tb[IFLA_GENEVE_DF]) {
enum ifla_geneve_df df = rta_getattr_u8(tb[IFLA_GENEVE_DF]);
if (df == GENEVE_DF_UNSET)
print_string(PRINT_JSON, "df", "df %s ", "unset");
else if (df == GENEVE_DF_SET)
print_string(PRINT_ANY, "df", "df %s ", "set");
else if (df == GENEVE_DF_INHERIT)
print_string(PRINT_ANY, "df", "df %s ", "inherit");
}
if (tb[IFLA_GENEVE_LABEL]) {
__u32 label = rta_getattr_u32(tb[IFLA_GENEVE_LABEL]);
if (label)
print_0xhex(PRINT_ANY,
"label", "flowlabel %#llx ",
ntohl(label));
}
if (tb[IFLA_GENEVE_PORT])
print_uint(PRINT_ANY,
"port",
"dstport %u ",
rta_getattr_be16(tb[IFLA_GENEVE_PORT]));
if (tb[IFLA_GENEVE_UDP_CSUM]) {
if (is_json_context()) {
print_bool(PRINT_JSON,
"udp_csum",
NULL,
rta_getattr_u8(tb[IFLA_GENEVE_UDP_CSUM]));
} else {
if (!rta_getattr_u8(tb[IFLA_GENEVE_UDP_CSUM]))
fputs("no", f);
fputs("udpcsum ", f);
}
}
if (tb[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]) {
if (is_json_context()) {
print_bool(PRINT_JSON,
"udp_zero_csum6_tx",
NULL,
rta_getattr_u8(tb[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]));
} else {
if (!rta_getattr_u8(tb[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
fputs("no", f);
fputs("udp6zerocsumtx ", f);
}
}
if (tb[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]) {
if (is_json_context()) {
print_bool(PRINT_JSON,
"udp_zero_csum6_rx",
NULL,
rta_getattr_u8(tb[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]));
} else {
if (!rta_getattr_u8(tb[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
fputs("no", f);
fputs("udp6zerocsumrx ", f);
}
}
if (tb[IFLA_GENEVE_INNER_PROTO_INHERIT]) {
print_bool(PRINT_ANY, "inner_proto_inherit",
"innerprotoinherit ", true);
}
}
static void geneve_print_help(struct link_util *lu, int argc, char **argv,
FILE *f)
{
print_explain(f);
}
struct link_util geneve_link_util = {
.id = "geneve",
.maxattr = IFLA_GENEVE_MAX,
.parse_opt = geneve_parse_opt,
.print_opt = geneve_print_opt,
.print_help = geneve_print_help,
};
|