]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_tunnel_key.c
vdpa: add .gitignore
[mirror_iproute2.git] / tc / m_tunnel_key.c
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
22 static void explain(void)
23 {
24 fprintf(stderr, "Usage: tunnel_key unset\n");
25 fprintf(stderr, " tunnel_key set <TUNNEL_KEY>\n");
26 fprintf(stderr,
27 "Where TUNNEL_KEY is a combination of:\n"
28 "id <TUNNELID>\n"
29 "src_ip <IP> (mandatory)\n"
30 "dst_ip <IP> (mandatory)\n"
31 "dst_port <UDP_PORT>\n"
32 "geneve_opts <OPTIONS>\n"
33 "csum | nocsum (default is \"csum\")\n");
34 }
35
36 static void usage(void)
37 {
38 explain();
39 exit(-1);
40 }
41
42 static 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
58 static 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
71 static 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
85 static 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
100 static 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
115 static 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
171 static 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
193 static 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
209 static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
210 int tca_id, struct nlmsghdr *n)
211 {
212 struct tc_tunnel_key parm = {};
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 csum = 1;
221
222 if (matches(*argv, "tunnel_key") != 0)
223 return -1;
224
225 tail = addattr_nest(n, MAX_MSG, tca_id);
226
227 NEXT_ARG();
228
229 while (argc > 0) {
230 if (matches(*argv, "unset") == 0) {
231 if (action) {
232 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
233 *argv);
234 explain();
235 return -1;
236 }
237 action = TCA_TUNNEL_KEY_ACT_RELEASE;
238 } else if (matches(*argv, "set") == 0) {
239 if (action) {
240 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
241 *argv);
242 explain();
243 return -1;
244 }
245 action = TCA_TUNNEL_KEY_ACT_SET;
246 } else if (matches(*argv, "src_ip") == 0) {
247 NEXT_ARG();
248 ret = tunnel_key_parse_ip_addr(*argv,
249 TCA_TUNNEL_KEY_ENC_IPV4_SRC,
250 TCA_TUNNEL_KEY_ENC_IPV6_SRC,
251 n);
252 if (ret < 0) {
253 fprintf(stderr, "Illegal \"src_ip\"\n");
254 return -1;
255 }
256 has_src_ip = 1;
257 } else if (matches(*argv, "dst_ip") == 0) {
258 NEXT_ARG();
259 ret = tunnel_key_parse_ip_addr(*argv,
260 TCA_TUNNEL_KEY_ENC_IPV4_DST,
261 TCA_TUNNEL_KEY_ENC_IPV6_DST,
262 n);
263 if (ret < 0) {
264 fprintf(stderr, "Illegal \"dst_ip\"\n");
265 return -1;
266 }
267 has_dst_ip = 1;
268 } else if (matches(*argv, "id") == 0) {
269 NEXT_ARG();
270 ret = tunnel_key_parse_key_id(*argv, TCA_TUNNEL_KEY_ENC_KEY_ID, n);
271 if (ret < 0) {
272 fprintf(stderr, "Illegal \"id\"\n");
273 return -1;
274 }
275 } else if (matches(*argv, "dst_port") == 0) {
276 NEXT_ARG();
277 ret = tunnel_key_parse_dst_port(*argv,
278 TCA_TUNNEL_KEY_ENC_DST_PORT, n);
279 if (ret < 0) {
280 fprintf(stderr, "Illegal \"dst port\"\n");
281 return -1;
282 }
283 } else if (matches(*argv, "geneve_opts") == 0) {
284 NEXT_ARG();
285
286 if (tunnel_key_parse_geneve_opts(*argv, n)) {
287 fprintf(stderr, "Illegal \"geneve_opts\"\n");
288 return -1;
289 }
290 } else if (matches(*argv, "tos") == 0) {
291 NEXT_ARG();
292 ret = tunnel_key_parse_tos_ttl(*argv,
293 TCA_TUNNEL_KEY_ENC_TOS, n);
294 if (ret < 0) {
295 fprintf(stderr, "Illegal \"tos\"\n");
296 return -1;
297 }
298 } else if (matches(*argv, "ttl") == 0) {
299 NEXT_ARG();
300 ret = tunnel_key_parse_tos_ttl(*argv,
301 TCA_TUNNEL_KEY_ENC_TTL, n);
302 if (ret < 0) {
303 fprintf(stderr, "Illegal \"ttl\"\n");
304 return -1;
305 }
306 } else if (matches(*argv, "csum") == 0) {
307 csum = 1;
308 } else if (matches(*argv, "nocsum") == 0) {
309 csum = 0;
310 } else if (matches(*argv, "help") == 0) {
311 usage();
312 } else {
313 break;
314 }
315 NEXT_ARG_FWD();
316 }
317
318 addattr8(n, MAX_MSG, TCA_TUNNEL_KEY_NO_CSUM, !csum);
319
320 parse_action_control_dflt(&argc, &argv, &parm.action,
321 false, TC_ACT_PIPE);
322
323 if (argc) {
324 if (matches(*argv, "index") == 0) {
325 NEXT_ARG();
326 if (get_u32(&parm.index, *argv, 10)) {
327 fprintf(stderr, "tunnel_key: Illegal \"index\"\n");
328 return -1;
329 }
330
331 NEXT_ARG_FWD();
332 }
333 }
334
335 if (action == TCA_TUNNEL_KEY_ACT_SET &&
336 (!has_src_ip || !has_dst_ip)) {
337 fprintf(stderr, "set needs tunnel_key parameters\n");
338 explain();
339 return -1;
340 }
341
342 parm.t_action = action;
343 addattr_l(n, MAX_MSG, TCA_TUNNEL_KEY_PARMS, &parm, sizeof(parm));
344 addattr_nest_end(n, tail);
345
346 *argc_p = argc;
347 *argv_p = argv;
348
349 return 0;
350 }
351
352 static void tunnel_key_print_ip_addr(FILE *f, const char *name,
353 struct rtattr *attr)
354 {
355 int family;
356 size_t len;
357
358 if (!attr)
359 return;
360
361 len = RTA_PAYLOAD(attr);
362
363 if (len == 4)
364 family = AF_INET;
365 else if (len == 16)
366 family = AF_INET6;
367 else
368 return;
369
370 print_string(PRINT_FP, NULL, "%s", _SL_);
371 if (matches(name, "src_ip") == 0)
372 print_string(PRINT_ANY, "src_ip", "\tsrc_ip %s",
373 rt_addr_n2a_rta(family, attr));
374 else if (matches(name, "dst_ip") == 0)
375 print_string(PRINT_ANY, "dst_ip", "\tdst_ip %s",
376 rt_addr_n2a_rta(family, attr));
377 }
378
379 static void tunnel_key_print_key_id(FILE *f, const char *name,
380 struct rtattr *attr)
381 {
382 if (!attr)
383 return;
384 print_string(PRINT_FP, NULL, "%s", _SL_);
385 print_uint(PRINT_ANY, "key_id", "\tkey_id %u", rta_getattr_be32(attr));
386 }
387
388 static void tunnel_key_print_dst_port(FILE *f, char *name,
389 struct rtattr *attr)
390 {
391 if (!attr)
392 return;
393 print_string(PRINT_FP, NULL, "%s", _SL_);
394 print_uint(PRINT_ANY, "dst_port", "\tdst_port %u",
395 rta_getattr_be16(attr));
396 }
397
398 static void tunnel_key_print_flag(FILE *f, const char *name_on,
399 const char *name_off,
400 struct rtattr *attr)
401 {
402 if (!attr)
403 return;
404 print_string(PRINT_FP, NULL, "%s", _SL_);
405 print_string(PRINT_ANY, "flag", "\t%s",
406 rta_getattr_u8(attr) ? name_on : name_off);
407 }
408
409 static void tunnel_key_print_geneve_options(const char *name,
410 struct rtattr *attr)
411 {
412 struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
413 struct rtattr *i = RTA_DATA(attr);
414 int ii, data_len = 0, offset = 0;
415 int rem = RTA_PAYLOAD(attr);
416 char strbuf[rem * 2 + 1];
417 char data[rem * 2 + 1];
418 uint8_t data_r[rem];
419 uint16_t clss;
420 uint8_t type;
421
422 open_json_array(PRINT_JSON, name);
423 print_string(PRINT_FP, name, "\n\t%s ", "geneve_opt");
424
425 while (rem) {
426 parse_rtattr(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX, i, rem);
427 clss = rta_getattr_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
428 type = rta_getattr_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
429 data_len = RTA_PAYLOAD(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
430 hexstring_n2a(RTA_DATA(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]),
431 data_len, data, sizeof(data));
432 hex2mem(data, data_r, data_len);
433 offset += data_len + 20;
434 rem -= data_len + 20;
435 i = RTA_DATA(attr) + offset;
436
437 open_json_object(NULL);
438 print_uint(PRINT_JSON, "class", NULL, clss);
439 print_uint(PRINT_JSON, "type", NULL, type);
440 open_json_array(PRINT_JSON, "data");
441 for (ii = 0; ii < data_len; ii++)
442 print_uint(PRINT_JSON, NULL, NULL, data_r[ii]);
443 close_json_array(PRINT_JSON, "data");
444 close_json_object();
445
446 sprintf(strbuf, "%04x:%02x:%s", clss, type, data);
447 if (rem)
448 print_string(PRINT_FP, NULL, "%s,", strbuf);
449 else
450 print_string(PRINT_FP, NULL, "%s", strbuf);
451 }
452
453 close_json_array(PRINT_JSON, name);
454 }
455
456 static void tunnel_key_print_key_opt(const char *name, struct rtattr *attr)
457 {
458 struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1];
459
460 if (!attr)
461 return;
462
463 parse_rtattr_nested(tb, TCA_TUNNEL_KEY_ENC_OPTS_MAX, attr);
464 tunnel_key_print_geneve_options(name,
465 tb[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]);
466 }
467
468 static void tunnel_key_print_tos_ttl(FILE *f, char *name,
469 struct rtattr *attr)
470 {
471 if (!attr)
472 return;
473
474 if (matches(name, "tos") == 0 && rta_getattr_u8(attr) != 0) {
475 print_string(PRINT_FP, NULL, "%s", _SL_);
476 print_uint(PRINT_ANY, "tos", "\ttos 0x%x",
477 rta_getattr_u8(attr));
478 } else if (matches(name, "ttl") == 0 && rta_getattr_u8(attr) != 0) {
479 print_string(PRINT_FP, NULL, "%s", _SL_);
480 print_uint(PRINT_ANY, "ttl", "\tttl %u",
481 rta_getattr_u8(attr));
482 }
483 }
484
485 static int print_tunnel_key(struct action_util *au, FILE *f, struct rtattr *arg)
486 {
487 struct rtattr *tb[TCA_TUNNEL_KEY_MAX + 1];
488 struct tc_tunnel_key *parm;
489
490 if (!arg)
491 return -1;
492
493 parse_rtattr_nested(tb, TCA_TUNNEL_KEY_MAX, arg);
494
495 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
496 print_string(PRINT_FP, NULL, "%s",
497 "[NULL tunnel_key parameters]");
498 return -1;
499 }
500 parm = RTA_DATA(tb[TCA_TUNNEL_KEY_PARMS]);
501
502 print_string(PRINT_ANY, "kind", "%s ", "tunnel_key");
503
504 switch (parm->t_action) {
505 case TCA_TUNNEL_KEY_ACT_RELEASE:
506 print_string(PRINT_ANY, "mode", " %s", "unset");
507 break;
508 case TCA_TUNNEL_KEY_ACT_SET:
509 print_string(PRINT_ANY, "mode", " %s", "set");
510 tunnel_key_print_ip_addr(f, "src_ip",
511 tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
512 tunnel_key_print_ip_addr(f, "dst_ip",
513 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
514 tunnel_key_print_ip_addr(f, "src_ip",
515 tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
516 tunnel_key_print_ip_addr(f, "dst_ip",
517 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
518 tunnel_key_print_key_id(f, "key_id",
519 tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
520 tunnel_key_print_dst_port(f, "dst_port",
521 tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
522 tunnel_key_print_key_opt("geneve_opts",
523 tb[TCA_TUNNEL_KEY_ENC_OPTS]);
524 tunnel_key_print_flag(f, "nocsum", "csum",
525 tb[TCA_TUNNEL_KEY_NO_CSUM]);
526 tunnel_key_print_tos_ttl(f, "tos",
527 tb[TCA_TUNNEL_KEY_ENC_TOS]);
528 tunnel_key_print_tos_ttl(f, "ttl",
529 tb[TCA_TUNNEL_KEY_ENC_TTL]);
530 break;
531 }
532 print_action_control(f, " ", parm->action, "");
533
534 print_string(PRINT_FP, NULL, "%s", _SL_);
535 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
536 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
537 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
538
539 if (show_stats) {
540 if (tb[TCA_TUNNEL_KEY_TM]) {
541 struct tcf_t *tm = RTA_DATA(tb[TCA_TUNNEL_KEY_TM]);
542
543 print_tm(f, tm);
544 }
545 }
546
547 print_string(PRINT_FP, NULL, "%s", _SL_);
548
549 return 0;
550 }
551
552 struct action_util tunnel_key_action_util = {
553 .id = "tunnel_key",
554 .parse_aopt = parse_tunnel_key,
555 .print_aopt = print_tunnel_key,
556 };