]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_tunnel_key.c
lib: introduce print_nl
[mirror_iproute2.git] / tc / m_tunnel_key.c
CommitLineData
d57639a4
AV
1/*
2 * m_tunnel_key.c ip tunnel manipulation module
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Amir Vadai <amir@vadai.me>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <string.h>
16#include <linux/if_ether.h>
17#include "utils.h"
18#include "rt_names.h"
19#include "tc_util.h"
20#include <linux/tc_act/tc_tunnel_key.h>
21
22static void explain(void)
23{
24 fprintf(stderr, "Usage: tunnel_key unset\n");
50907a82
JB
25 fprintf(stderr, " tunnel_key set <TUNNEL_KEY>\n");
26 fprintf(stderr,
27 "Where TUNNEL_KEY is a combination of:\n"
28 "id <TUNNELID> (mandatory)\n"
29 "src_ip <IP> (mandatory)\n"
30 "dst_ip <IP> (mandatory)\n"
59eb271d 31 "dst_port <UDP_PORT>\n"
6217917a 32 "geneve_opts <OPTIONS>\n"
59eb271d 33 "csum | nocsum (default is \"csum\")\n");
d57639a4
AV
34}
35
36static void usage(void)
37{
38 explain();
39 exit(-1);
40}
41
42static int tunnel_key_parse_ip_addr(const char *str, int addr4_type,
43 int addr6_type, struct nlmsghdr *n)
44{
45 inet_prefix addr;
46 int ret;
47
48 ret = get_addr(&addr, str, AF_UNSPEC);
49 if (ret)
50 return ret;
51
52 addattr_l(n, MAX_MSG, addr.family == AF_INET ? addr4_type : addr6_type,
53 addr.data, addr.bytelen);
54
55 return 0;
56}
57
58static int tunnel_key_parse_key_id(const char *str, int type,
59 struct nlmsghdr *n)
60{
61 __be32 key_id;
62 int ret;
63
64 ret = get_be32(&key_id, str, 10);
65 if (!ret)
66 addattr32(n, MAX_MSG, type, key_id);
67
68 return ret;
69}
70
449c709c
HHZ
71static int tunnel_key_parse_dst_port(char *str, int type, struct nlmsghdr *n)
72{
73 int ret;
74 __be16 dst_port;
75
76 ret = get_be16(&dst_port, str, 10);
77 if (ret)
78 return -1;
79
80 addattr16(n, MAX_MSG, type, dst_port);
81
82 return 0;
83}
84
6217917a
SH
85static int tunnel_key_parse_be16(char *str, int base, int type,
86 struct nlmsghdr *n)
87{
88 int ret;
89 __be16 value;
90
91 ret = get_be16(&value, str, base);
92 if (ret)
93 return ret;
94
95 addattr16(n, MAX_MSG, type, value);
96
97 return 0;
98}
99
100static int tunnel_key_parse_u8(char *str, int base, int type,
101 struct nlmsghdr *n)
102{
103 int ret;
104 __u8 value;
105
106 ret = get_u8(&value, str, base);
107 if (ret)
108 return ret;
109
110 addattr8(n, MAX_MSG, type, value);
111
112 return 0;
113}
114
115static int tunnel_key_parse_geneve_opt(char *str, struct nlmsghdr *n)
116{
117 char *token, *saveptr = NULL;
118 struct rtattr *nest;
119 int i, ret;
120
121 nest = addattr_nest(n, MAX_MSG, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
122
123 token = strtok_r(str, ":", &saveptr);
124 i = 1;
125 while (token) {
126 switch (i) {
127 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS:
128 {
129 ret = tunnel_key_parse_be16(token, 16, i, n);
130 if (ret)
131 return ret;
132 break;
133 }
134 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE:
135 {
136 ret = tunnel_key_parse_u8(token, 16, i, n);
137 if (ret)
138 return ret;
139 break;
140 }
141 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA:
142 {
143 size_t token_len = strlen(token);
144 uint8_t *opts;
145
146 opts = malloc(token_len / 2);
147 if (!opts)
148 return -1;
149 if (hex2mem(token, opts, token_len / 2) < 0) {
150 free(opts);
151 return -1;
152 }
153 addattr_l(n, MAX_MSG, i, opts, token_len / 2);
154 free(opts);
155
156 break;
157 }
158 default:
159 return -1;
160 }
161
162 token = strtok_r(NULL, ":", &saveptr);
163 i++;
164 }
165
166 addattr_nest_end(n, nest);
167
168 return 0;
169}
170
171static int tunnel_key_parse_geneve_opts(char *str, struct nlmsghdr *n)
172{
173 char *token, *saveptr = NULL;
174 struct rtattr *nest;
175 int ret;
176
177 nest = addattr_nest(n, MAX_MSG, TCA_TUNNEL_KEY_ENC_OPTS);
178
179 token = strtok_r(str, ",", &saveptr);
180 while (token) {
181 ret = tunnel_key_parse_geneve_opt(token, n);
182 if (ret)
183 return ret;
184
185 token = strtok_r(NULL, ",", &saveptr);
186 }
187
188 addattr_nest_end(n, nest);
189
190 return 0;
191}
192
9f89b0cc
OG
193static int tunnel_key_parse_tos_ttl(char *str, int type, struct nlmsghdr *n)
194{
195 int ret;
196 __u8 val;
197
198 ret = get_u8(&val, str, 10);
199 if (ret)
200 ret = get_u8(&val, str, 16);
201 if (ret)
202 return -1;
203
204 addattr8(n, MAX_MSG, type, val);
205
206 return 0;
207}
208
d57639a4
AV
209static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
210 int tca_id, struct nlmsghdr *n)
211{
e67aba55 212 struct tc_tunnel_key parm = {};
d57639a4
AV
213 char **argv = *argv_p;
214 int argc = *argc_p;
215 struct rtattr *tail;
216 int action = 0;
217 int ret;
218 int has_src_ip = 0;
219 int has_dst_ip = 0;
220 int has_key_id = 0;
59eb271d 221 int csum = 1;
d57639a4
AV
222
223 if (matches(*argv, "tunnel_key") != 0)
224 return -1;
225
c14f9d92 226 tail = addattr_nest(n, MAX_MSG, tca_id);
d57639a4
AV
227
228 NEXT_ARG();
229
230 while (argc > 0) {
231 if (matches(*argv, "unset") == 0) {
232 if (action) {
233 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
234 *argv);
235 explain();
236 return -1;
237 }
238 action = TCA_TUNNEL_KEY_ACT_RELEASE;
239 } else if (matches(*argv, "set") == 0) {
240 if (action) {
241 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
242 *argv);
243 explain();
244 return -1;
245 }
246 action = TCA_TUNNEL_KEY_ACT_SET;
247 } else if (matches(*argv, "src_ip") == 0) {
248 NEXT_ARG();
249 ret = tunnel_key_parse_ip_addr(*argv,
250 TCA_TUNNEL_KEY_ENC_IPV4_SRC,
251 TCA_TUNNEL_KEY_ENC_IPV6_SRC,
252 n);
253 if (ret < 0) {
254 fprintf(stderr, "Illegal \"src_ip\"\n");
255 return -1;
256 }
257 has_src_ip = 1;
258 } else if (matches(*argv, "dst_ip") == 0) {
259 NEXT_ARG();
260 ret = tunnel_key_parse_ip_addr(*argv,
261 TCA_TUNNEL_KEY_ENC_IPV4_DST,
262 TCA_TUNNEL_KEY_ENC_IPV6_DST,
263 n);
264 if (ret < 0) {
265 fprintf(stderr, "Illegal \"dst_ip\"\n");
266 return -1;
267 }
268 has_dst_ip = 1;
269 } else if (matches(*argv, "id") == 0) {
270 NEXT_ARG();
271 ret = tunnel_key_parse_key_id(*argv, TCA_TUNNEL_KEY_ENC_KEY_ID, n);
272 if (ret < 0) {
273 fprintf(stderr, "Illegal \"id\"\n");
274 return -1;
275 }
276 has_key_id = 1;
449c709c
HHZ
277 } else if (matches(*argv, "dst_port") == 0) {
278 NEXT_ARG();
279 ret = tunnel_key_parse_dst_port(*argv,
280 TCA_TUNNEL_KEY_ENC_DST_PORT, n);
281 if (ret < 0) {
282 fprintf(stderr, "Illegal \"dst port\"\n");
283 return -1;
284 }
6217917a
SH
285 } else if (matches(*argv, "geneve_opts") == 0) {
286 NEXT_ARG();
287
288 if (tunnel_key_parse_geneve_opts(*argv, n)) {
289 fprintf(stderr, "Illegal \"geneve_opts\"\n");
290 return -1;
291 }
9f89b0cc
OG
292 } else if (matches(*argv, "tos") == 0) {
293 NEXT_ARG();
294 ret = tunnel_key_parse_tos_ttl(*argv,
295 TCA_TUNNEL_KEY_ENC_TOS, n);
296 if (ret < 0) {
297 fprintf(stderr, "Illegal \"tos\"\n");
298 return -1;
299 }
300 } else if (matches(*argv, "ttl") == 0) {
301 NEXT_ARG();
302 ret = tunnel_key_parse_tos_ttl(*argv,
303 TCA_TUNNEL_KEY_ENC_TTL, n);
304 if (ret < 0) {
305 fprintf(stderr, "Illegal \"ttl\"\n");
306 return -1;
307 }
59eb271d
JB
308 } else if (matches(*argv, "csum") == 0) {
309 csum = 1;
310 } else if (matches(*argv, "nocsum") == 0) {
311 csum = 0;
d57639a4
AV
312 } else if (matches(*argv, "help") == 0) {
313 usage();
314 } else {
315 break;
316 }
317 NEXT_ARG_FWD();
318 }
319
59eb271d
JB
320 addattr8(n, MAX_MSG, TCA_TUNNEL_KEY_NO_CSUM, !csum);
321
e67aba55
JP
322 parse_action_control_dflt(&argc, &argv, &parm.action,
323 false, TC_ACT_PIPE);
d57639a4
AV
324
325 if (argc) {
326 if (matches(*argv, "index") == 0) {
327 NEXT_ARG();
328 if (get_u32(&parm.index, *argv, 10)) {
329 fprintf(stderr, "tunnel_key: Illegal \"index\"\n");
330 return -1;
331 }
332
333 NEXT_ARG_FWD();
334 }
335 }
336
337 if (action == TCA_TUNNEL_KEY_ACT_SET &&
338 (!has_src_ip || !has_dst_ip || !has_key_id)) {
339 fprintf(stderr, "set needs tunnel_key parameters\n");
340 explain();
341 return -1;
342 }
343
344 parm.t_action = action;
345 addattr_l(n, MAX_MSG, TCA_TUNNEL_KEY_PARMS, &parm, sizeof(parm));
c14f9d92 346 addattr_nest_end(n, tail);
d57639a4
AV
347
348 *argc_p = argc;
349 *argv_p = argv;
350
351 return 0;
352}
353
354static void tunnel_key_print_ip_addr(FILE *f, const char *name,
355 struct rtattr *attr)
356{
357 int family;
358 size_t len;
359
360 if (!attr)
361 return;
362
363 len = RTA_PAYLOAD(attr);
364
365 if (len == 4)
366 family = AF_INET;
367 else if (len == 16)
368 family = AF_INET6;
369 else
370 return;
371
8feb516b
RM
372 print_string(PRINT_FP, NULL, "%s", _SL_);
373 if (matches(name, "src_ip") == 0)
374 print_string(PRINT_ANY, "src_ip", "\tsrc_ip %s",
375 rt_addr_n2a_rta(family, attr));
376 else if (matches(name, "dst_ip") == 0)
377 print_string(PRINT_ANY, "dst_ip", "\tdst_ip %s",
378 rt_addr_n2a_rta(family, attr));
d57639a4
AV
379}
380
381static void tunnel_key_print_key_id(FILE *f, const char *name,
382 struct rtattr *attr)
383{
384 if (!attr)
385 return;
8feb516b
RM
386 print_string(PRINT_FP, NULL, "%s", _SL_);
387 print_uint(PRINT_ANY, "key_id", "\tkey_id %u", rta_getattr_be32(attr));
d57639a4
AV
388}
389
449c709c
HHZ
390static void tunnel_key_print_dst_port(FILE *f, char *name,
391 struct rtattr *attr)
392{
393 if (!attr)
394 return;
8feb516b
RM
395 print_string(PRINT_FP, NULL, "%s", _SL_);
396 print_uint(PRINT_ANY, "dst_port", "\tdst_port %u",
397 rta_getattr_be16(attr));
449c709c
HHZ
398}
399
59eb271d
JB
400static void tunnel_key_print_flag(FILE *f, const char *name_on,
401 const char *name_off,
402 struct rtattr *attr)
403{
404 if (!attr)
405 return;
8feb516b
RM
406 print_string(PRINT_FP, NULL, "%s", _SL_);
407 print_string(PRINT_ANY, "flag", "\t%s",
408 rta_getattr_u8(attr) ? name_on : name_off);
59eb271d
JB
409}
410
6217917a
SH
411static void tunnel_key_print_geneve_options(const char *name,
412 struct rtattr *attr)
413{
414 struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
415 struct rtattr *i = RTA_DATA(attr);
416 int ii, data_len = 0, offset = 0;
417 int rem = RTA_PAYLOAD(attr);
418 char strbuf[rem * 2 + 1];
419 char data[rem * 2 + 1];
420 uint8_t data_r[rem];
421 uint16_t clss;
422 uint8_t type;
423
424 open_json_array(PRINT_JSON, name);
425 print_string(PRINT_FP, name, "\n\t%s ", "geneve_opt");
426
427 while (rem) {
428 parse_rtattr(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX, i, rem);
429 clss = rta_getattr_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
430 type = rta_getattr_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
431 data_len = RTA_PAYLOAD(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
432 hexstring_n2a(RTA_DATA(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]),
433 data_len, data, sizeof(data));
434 hex2mem(data, data_r, data_len);
435 offset += data_len + 20;
436 rem -= data_len + 20;
437 i = RTA_DATA(attr) + offset;
438
439 open_json_object(NULL);
440 print_uint(PRINT_JSON, "class", NULL, clss);
441 print_uint(PRINT_JSON, "type", NULL, type);
442 open_json_array(PRINT_JSON, "data");
443 for (ii = 0; ii < data_len; ii++)
444 print_uint(PRINT_JSON, NULL, NULL, data_r[ii]);
445 close_json_array(PRINT_JSON, "data");
446 close_json_object();
447
448 sprintf(strbuf, "%04x:%02x:%s", clss, type, data);
449 if (rem)
450 print_string(PRINT_FP, NULL, "%s,", strbuf);
451 else
452 print_string(PRINT_FP, NULL, "%s", strbuf);
453 }
454
455 close_json_array(PRINT_JSON, name);
456}
457
458static void tunnel_key_print_key_opt(const char *name, struct rtattr *attr)
459{
460 struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1];
461
462 if (!attr)
463 return;
464
465 parse_rtattr_nested(tb, TCA_TUNNEL_KEY_ENC_OPTS_MAX, attr);
466 tunnel_key_print_geneve_options(name,
467 tb[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]);
468}
469
9f89b0cc
OG
470static void tunnel_key_print_tos_ttl(FILE *f, char *name,
471 struct rtattr *attr)
472{
473 if (!attr)
474 return;
475
476 if (matches(name, "tos") == 0 && rta_getattr_u8(attr) != 0) {
477 print_string(PRINT_FP, NULL, "%s", _SL_);
478 print_uint(PRINT_ANY, "tos", "\ttos 0x%x",
479 rta_getattr_u8(attr));
480 } else if (matches(name, "ttl") == 0 && rta_getattr_u8(attr) != 0) {
481 print_string(PRINT_FP, NULL, "%s", _SL_);
482 print_uint(PRINT_ANY, "ttl", "\tttl %u",
483 rta_getattr_u8(attr));
484 }
485}
486
d57639a4
AV
487static int print_tunnel_key(struct action_util *au, FILE *f, struct rtattr *arg)
488{
489 struct rtattr *tb[TCA_TUNNEL_KEY_MAX + 1];
490 struct tc_tunnel_key *parm;
491
492 if (!arg)
493 return -1;
494
495 parse_rtattr_nested(tb, TCA_TUNNEL_KEY_MAX, arg);
496
497 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
8feb516b
RM
498 print_string(PRINT_FP, NULL, "%s",
499 "[NULL tunnel_key parameters]");
d57639a4
AV
500 return -1;
501 }
502 parm = RTA_DATA(tb[TCA_TUNNEL_KEY_PARMS]);
503
8feb516b 504 print_string(PRINT_ANY, "kind", "%s ", "tunnel_key");
d57639a4
AV
505
506 switch (parm->t_action) {
507 case TCA_TUNNEL_KEY_ACT_RELEASE:
8feb516b 508 print_string(PRINT_ANY, "mode", " %s", "unset");
d57639a4
AV
509 break;
510 case TCA_TUNNEL_KEY_ACT_SET:
8feb516b 511 print_string(PRINT_ANY, "mode", " %s", "set");
d57639a4
AV
512 tunnel_key_print_ip_addr(f, "src_ip",
513 tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
514 tunnel_key_print_ip_addr(f, "dst_ip",
515 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
516 tunnel_key_print_ip_addr(f, "src_ip",
517 tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
518 tunnel_key_print_ip_addr(f, "dst_ip",
519 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
520 tunnel_key_print_key_id(f, "key_id",
521 tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
449c709c
HHZ
522 tunnel_key_print_dst_port(f, "dst_port",
523 tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
6217917a
SH
524 tunnel_key_print_key_opt("geneve_opts",
525 tb[TCA_TUNNEL_KEY_ENC_OPTS]);
59eb271d
JB
526 tunnel_key_print_flag(f, "nocsum", "csum",
527 tb[TCA_TUNNEL_KEY_NO_CSUM]);
9f89b0cc
OG
528 tunnel_key_print_tos_ttl(f, "tos",
529 tb[TCA_TUNNEL_KEY_ENC_TOS]);
530 tunnel_key_print_tos_ttl(f, "ttl",
531 tb[TCA_TUNNEL_KEY_ENC_TTL]);
d57639a4
AV
532 break;
533 }
e67aba55 534 print_action_control(f, " ", parm->action, "");
d57639a4 535
8feb516b
RM
536 print_string(PRINT_FP, NULL, "%s", _SL_);
537 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
538 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
539 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
d57639a4
AV
540
541 if (show_stats) {
542 if (tb[TCA_TUNNEL_KEY_TM]) {
543 struct tcf_t *tm = RTA_DATA(tb[TCA_TUNNEL_KEY_TM]);
544
545 print_tm(f, tm);
546 }
547 }
548
8feb516b 549 print_string(PRINT_FP, NULL, "%s", _SL_);
d57639a4
AV
550
551 return 0;
552}
553
554struct action_util tunnel_key_action_util = {
555 .id = "tunnel_key",
556 .parse_aopt = parse_tunnel_key,
557 .print_aopt = print_tunnel_key,
558};