]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_action.c
devlink: Add support for resource/dpipe relation
[mirror_iproute2.git] / tc / m_action.c
CommitLineData
68d5ba54
SH
1/*
2 * m_action.c Action Management
3 *
4 * This program is free software; you can distribute 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 *
ae665a52
SH
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 *
68d5ba54
SH
11 * TODO:
12 * - parse to be passed a filedescriptor for logging purposes
13 *
ed485691 14 */
68d5ba54
SH
15
16#include <stdio.h>
17#include <stdlib.h>
ed485691 18#include <stdbool.h>
68d5ba54 19#include <unistd.h>
68d5ba54
SH
20#include <fcntl.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <string.h>
25#include <dlfcn.h>
26
27#include "utils.h"
c1027a75 28#include "tc_common.h"
68d5ba54
SH
29#include "tc_util.h"
30
32a121cb 31static struct action_util *action_list;
68d5ba54 32#ifdef CONFIG_GACT
32a121cb 33int gact_ld; /* f*ckin backward compatibility */
68d5ba54 34#endif
32a121cb 35int tab_flush;
68d5ba54 36
d1f28cf1 37static void act_usage(void)
68d5ba54 38{
f1e4f042 39 /*XXX: In the near future add a action->print_help to improve
40 * usability
41 * This would mean new tc will not be backward compatible
42 * with any action .so from the old days. But if someone really
43 * does that, they would know how to fix this ..
44 *
ed485691 45 */
32a121cb 46 fprintf(stderr, "usage: tc actions <ACTSPECOP>*\n");
f1e4f042 47 fprintf(stderr,
48 "Where: \tACTSPECOP := ACR | GD | FL\n"
32a121cb 49 "\tACR := add | change | replace <ACTSPEC>*\n"
302d3fb7
SH
50 "\tGD := get | delete | <ACTISPEC>*\n"
51 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
52 "\tACTNAMESPEC := action <ACTNAME>\n"
53 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
54 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
55 "\tINDEXSPEC := index <32 bit indexvalue>\n"
56 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
4bd62446 57 "\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
302d3fb7
SH
58 "\t\tEach action has its own parameters (ACTPARAMS)\n"
59 "\n");
60
ebf32083 61 exit(-1);
68d5ba54
SH
62}
63
64static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
65{
66 if (opt && RTA_PAYLOAD(opt))
ae665a52 67 fprintf(f, "[Unknown action, optlen=%u] ",
32a121cb 68 (unsigned int) RTA_PAYLOAD(opt));
68d5ba54
SH
69 return 0;
70}
71
ed485691
SH
72static int parse_noaopt(struct action_util *au, int *argc_p,
73 char ***argv_p, int code, struct nlmsghdr *n)
68d5ba54
SH
74{
75 int argc = *argc_p;
76 char **argv = *argv_p;
77
ed485691
SH
78 if (argc)
79 fprintf(stderr,
80 "Unknown action \"%s\", hence option \"%s\" is unparsable\n",
81 au->id, *argv);
82 else
68d5ba54 83 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
ed485691 84
68d5ba54
SH
85 return -1;
86}
87
d1f28cf1 88static struct action_util *get_action_kind(char *str)
68d5ba54 89{
cda4026c 90 static void *aBODY;
68d5ba54
SH
91 void *dlh;
92 char buf[256];
93 struct action_util *a;
94#ifdef CONFIG_GACT
95 int looked4gact = 0;
96restart_s:
97#endif
98 for (a = action_list; a; a = a->next) {
99 if (strcmp(a->id, str) == 0)
100 return a;
101 }
102
ab814d63 103 snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str);
02833d1b 104 dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
68d5ba54
SH
105 if (dlh == NULL) {
106 dlh = aBODY;
107 if (dlh == NULL) {
108 dlh = aBODY = dlopen(NULL, RTLD_LAZY);
109 if (dlh == NULL)
110 goto noexist;
111 }
112 }
113
95812b56 114 snprintf(buf, sizeof(buf), "%s_action_util", str);
68d5ba54
SH
115 a = dlsym(dlh, buf);
116 if (a == NULL)
117 goto noexist;
118
119reg:
120 a->next = action_list;
121 action_list = a;
122 return a;
123
124noexist:
125#ifdef CONFIG_GACT
126 if (!looked4gact) {
127 looked4gact = 1;
32a121cb 128 strcpy(str, "gact");
68d5ba54
SH
129 goto restart_s;
130 }
131#endif
f89bb021 132 a = calloc(1, sizeof(*a));
68d5ba54 133 if (a) {
68d5ba54
SH
134 strncpy(a->id, "noact", 15);
135 a->parse_aopt = parse_noaopt;
136 a->print_aopt = print_noaopt;
137 goto reg;
138 }
139 return a;
140}
141
ed485691 142static bool
ae665a52 143new_cmd(char **argv)
68d5ba54 144{
ed485691 145 return (matches(*argv, "change") == 0) ||
e9e9365b
SH
146 (matches(*argv, "replace") == 0) ||
147 (matches(*argv, "delete") == 0) ||
148 (matches(*argv, "get") == 0) ||
ed485691 149 (matches(*argv, "add") == 0);
68d5ba54
SH
150}
151
fd8b3d2c 152int parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
68d5ba54
SH
153{
154 int argc = *argc_p;
155 char **argv = *argv_p;
156 struct rtattr *tail, *tail2;
b317557f 157 char k[FILTER_NAMESZ];
fd8b3d2c 158 int act_ck_len = 0;
68d5ba54
SH
159 int ok = 0;
160 int eap = 0; /* expect action parameters */
161
162 int ret = 0;
163 int prio = 0;
fd8b3d2c 164 unsigned char act_ck[TC_COOKIE_MAX_SIZE];
68d5ba54
SH
165
166 if (argc <= 0)
167 return -1;
168
4a86fe19 169 tail = tail2 = NLMSG_TAIL(n);
68d5ba54
SH
170
171 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
172
173 while (argc > 0) {
174
32a121cb 175 memset(k, 0, sizeof(k));
68d5ba54 176
32a121cb 177 if (strcmp(*argv, "action") == 0) {
68d5ba54
SH
178 argc--;
179 argv++;
180 eap = 1;
181#ifdef CONFIG_GACT
ed485691 182 if (!gact_ld)
68d5ba54 183 get_action_kind("gact");
68d5ba54
SH
184#endif
185 continue;
9282d08d
JHS
186 } else if (strcmp(*argv, "flowid") == 0) {
187 break;
188 } else if (strcmp(*argv, "classid") == 0) {
189 break;
68d5ba54
SH
190 } else if (strcmp(*argv, "help") == 0) {
191 return -1;
192 } else if (new_cmd(argv)) {
193 goto done0;
194 } else {
195 struct action_util *a = NULL;
32a121cb 196
6f7df6b2
PS
197 if (!action_a2n(*argv, NULL, false))
198 strncpy(k, "gact", sizeof(k) - 1);
199 else
200 strncpy(k, *argv, sizeof(k) - 1);
68d5ba54 201 eap = 0;
32a121cb 202 if (argc > 0) {
68d5ba54
SH
203 a = get_action_kind(k);
204 } else {
205done0:
206 if (ok)
207 break;
208 else
209 goto done;
210 }
211
ed485691 212 if (a == NULL)
68d5ba54 213 goto bad_val;
ed485691 214
68d5ba54 215
4a86fe19 216 tail = NLMSG_TAIL(n);
68d5ba54
SH
217 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
218 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
219
fd8b3d2c
JHS
220 ret = a->parse_aopt(a, &argc, &argv, TCA_ACT_OPTIONS,
221 n);
68d5ba54
SH
222
223 if (ret < 0) {
32a121cb 224 fprintf(stderr, "bad action parsing\n");
68d5ba54
SH
225 goto bad_val;
226 }
fd8b3d2c
JHS
227
228 if (*argv && strcmp(*argv, "cookie") == 0) {
229 size_t slen;
230
231 NEXT_ARG();
232 slen = strlen(*argv);
233 if (slen > TC_COOKIE_MAX_SIZE * 2) {
234 char cookie_err_m[128];
235
236 snprintf(cookie_err_m, 128,
237 "%zd Max allowed size %d",
238 slen, TC_COOKIE_MAX_SIZE*2);
239 invarg(cookie_err_m, *argv);
240 }
241
242 if (hex2mem(*argv, act_ck, slen / 2) < 0)
243 invarg("cookie must be a hex string\n",
244 *argv);
245
b75e0f6f 246 act_ck_len = slen / 2;
fd8b3d2c
JHS
247 argc--;
248 argv++;
249 }
250
251 if (act_ck_len)
252 addattr_l(n, MAX_MSG, TCA_ACT_COOKIE,
253 &act_ck, act_ck_len);
254
4a86fe19 255 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
68d5ba54
SH
256 ok++;
257 }
68d5ba54
SH
258 }
259
260 if (eap > 0) {
32a121cb 261 fprintf(stderr, "bad action empty %d\n", eap);
68d5ba54
SH
262 goto bad_val;
263 }
264
4a86fe19 265 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
68d5ba54
SH
266
267done:
268 *argc_p = argc;
269 *argv_p = argv;
270 return 0;
271bad_val:
ae665a52 272 /* no need to undo things, returning from here should
ed485691
SH
273 * cause enough pain
274 */
32a121cb 275 fprintf(stderr, "parse_action: bad value (%d:%s)!\n", argc, *argv);
68d5ba54
SH
276 return -1;
277}
278
fd8b3d2c 279static int tc_print_one_action(FILE *f, struct rtattr *arg)
68d5ba54
SH
280{
281
954de6c7 282 struct rtattr *tb[TCA_ACT_MAX + 1];
68d5ba54
SH
283 int err = 0;
284 struct action_util *a = NULL;
285
286 if (arg == NULL)
287 return -1;
288
954de6c7
JHS
289 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
290
291 if (tb[TCA_ACT_KIND] == NULL) {
68d5ba54
SH
292 fprintf(stderr, "NULL Action!\n");
293 return -1;
294 }
295
296
954de6c7 297 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
32a121cb 298 if (a == NULL)
68d5ba54
SH
299 return err;
300
954de6c7 301 err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]);
68d5ba54 302
32a121cb 303 if (err < 0)
68d5ba54
SH
304 return err;
305
954de6c7 306 if (show_stats && tb[TCA_ACT_STATS]) {
2704bd62
JP
307 print_string(PRINT_FP, NULL, "\tAction statistics:\n", NULL);
308 open_json_object("stats");
e5879dc6 309 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
2704bd62
JP
310 close_json_object();
311 print_string(PRINT_FP, NULL, "\n", NULL);
68d5ba54 312 }
abff45b8
JP
313 if (tb[TCA_ACT_COOKIE]) {
314 int strsz = RTA_PAYLOAD(tb[TCA_ACT_COOKIE]);
315 char b1[strsz * 2 + 1];
316
2704bd62
JP
317 print_string(PRINT_ANY, "cookie", "\tcookie %s\n",
318 hexstring_n2a(RTA_DATA(tb[TCA_ACT_COOKIE]),
319 strsz, b1, sizeof(b1)));
abff45b8 320 }
68d5ba54
SH
321
322 return 0;
323}
324
954de6c7
JHS
325static int
326tc_print_action_flush(FILE *f, const struct rtattr *arg)
327{
328
329 struct rtattr *tb[TCA_MAX + 1];
330 int err = 0;
331 struct action_util *a = NULL;
332 __u32 *delete_count = 0;
333
334 parse_rtattr_nested(tb, TCA_MAX, arg);
335
336 if (tb[TCA_KIND] == NULL) {
337 fprintf(stderr, "NULL Action!\n");
338 return -1;
339 }
340
341 a = get_action_kind(RTA_DATA(tb[TCA_KIND]));
32a121cb 342 if (a == NULL)
954de6c7
JHS
343 return err;
344
345 delete_count = RTA_DATA(tb[TCA_FCNT]);
32a121cb 346 fprintf(f, " %s (%d entries)\n", a->id, *delete_count);
954de6c7
JHS
347 tab_flush = 0;
348 return 0;
349}
350
68d5ba54 351int
9e713525 352tc_print_action(FILE *f, const struct rtattr *arg, unsigned short tot_acts)
68d5ba54
SH
353{
354
355 int i;
68d5ba54
SH
356
357 if (arg == NULL)
358 return 0;
359
9e713525
JHS
360 if (!tot_acts)
361 tot_acts = TCA_ACT_MAX_PRIO;
362
363 struct rtattr *tb[tot_acts + 1];
364
365 parse_rtattr_nested(tb, tot_acts, arg);
68d5ba54 366
954de6c7
JHS
367 if (tab_flush && NULL != tb[0] && NULL == tb[1])
368 return tc_print_action_flush(f, tb[0]);
68d5ba54 369
2704bd62 370 open_json_array(PRINT_JSON, "actions");
2fb854d0 371 for (i = 0; i <= tot_acts; i++) {
68d5ba54 372 if (tb[i]) {
2704bd62
JP
373 open_json_object(NULL);
374 print_uint(PRINT_ANY, "order",
375 "\n\taction order %u: ", i);
32a121cb 376 if (tc_print_one_action(f, tb[i]) < 0) {
2704bd62
JP
377 print_string(PRINT_FP, NULL,
378 "Error printing action\n", NULL);
68d5ba54 379 }
2704bd62 380 close_json_object();
68d5ba54
SH
381 }
382
383 }
1876ab07 384 close_json_array(PRINT_JSON, NULL);
68d5ba54 385
68d5ba54
SH
386 return 0;
387}
388
5bec3484 389int print_action(const struct sockaddr_nl *who,
50772dc5 390 struct nlmsghdr *n,
3d327bf5 391 void *arg)
68d5ba54 392{
32a121cb 393 FILE *fp = (FILE *)arg;
68d5ba54
SH
394 struct tcamsg *t = NLMSG_DATA(n);
395 int len = n->nlmsg_len;
9e713525
JHS
396 __u32 *tot_acts = NULL;
397 struct rtattr *tb[TCA_ROOT_MAX+1];
68d5ba54
SH
398
399 len -= NLMSG_LENGTH(sizeof(*t));
400
401 if (len < 0) {
402 fprintf(stderr, "Wrong len %d\n", len);
403 return -1;
404 }
405
9e713525
JHS
406 parse_rtattr(tb, TCA_ROOT_MAX, TA_RTA(t), len);
407
408 if (tb[TCA_ROOT_COUNT])
409 tot_acts = RTA_DATA(tb[TCA_ROOT_COUNT]);
68d5ba54 410
9e713525 411 fprintf(fp, "total acts %d\n", tot_acts ? *tot_acts:0);
32a121cb 412 if (tb[TCA_ACT_TAB] == NULL) {
68d5ba54 413 if (n->nlmsg_type != RTM_GETACTION)
5bec3484 414 fprintf(stderr, "print_action: NULL kind\n");
68d5ba54 415 return -1;
ae665a52 416 }
68d5ba54
SH
417
418 if (n->nlmsg_type == RTM_DELACTION) {
419 if (n->nlmsg_flags & NLM_F_ROOT) {
ae665a52 420 fprintf(fp, "Flushed table ");
68d5ba54
SH
421 tab_flush = 1;
422 } else {
31951c47 423 fprintf(fp, "Deleted action ");
68d5ba54
SH
424 }
425 }
426
31951c47
RM
427 if (n->nlmsg_type == RTM_NEWACTION) {
428 if ((n->nlmsg_flags & NLM_F_CREATE) &&
429 !(n->nlmsg_flags & NLM_F_REPLACE)) {
430 fprintf(fp, "Added action ");
431 } else if (n->nlmsg_flags & NLM_F_REPLACE) {
432 fprintf(fp, "Replaced action ");
433 }
434 }
9e713525
JHS
435
436
437 tc_print_action(fp, tb[TCA_ACT_TAB], tot_acts ? *tot_acts:0);
68d5ba54
SH
438
439 return 0;
440}
441
ed485691
SH
442static int tc_action_gd(int cmd, unsigned int flags,
443 int *argc_p, char ***argv_p)
68d5ba54 444{
b317557f 445 char k[FILTER_NAMESZ];
68d5ba54
SH
446 struct action_util *a = NULL;
447 int argc = *argc_p;
448 char **argv = *argv_p;
449 int prio = 0;
450 int ret = 0;
9e713525 451 __u32 i = 0;
68d5ba54
SH
452 struct rtattr *tail;
453 struct rtattr *tail2;
454 struct nlmsghdr *ans = NULL;
455
456 struct {
457 struct nlmsghdr n;
458 struct tcamsg t;
459 char buf[MAX_MSG];
a0a73b29 460 } req = {
d892aaf7
PS
461 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
462 .n.nlmsg_flags = NLM_F_REQUEST | flags,
463 .n.nlmsg_type = cmd,
a0a73b29 464 .t.tca_family = AF_UNSPEC,
a0a73b29 465 };
68d5ba54 466
32a121cb
SH
467 argc -= 1;
468 argv += 1;
68d5ba54
SH
469
470
4a86fe19 471 tail = NLMSG_TAIL(&req.n);
68d5ba54
SH
472 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
473
474 while (argc > 0) {
32a121cb 475 if (strcmp(*argv, "action") == 0) {
68d5ba54
SH
476 argc--;
477 argv++;
478 continue;
479 } else if (strcmp(*argv, "help") == 0) {
480 return -1;
481 }
482
32a121cb 483 strncpy(k, *argv, sizeof(k) - 1);
68d5ba54 484 a = get_action_kind(k);
32a121cb
SH
485 if (a == NULL) {
486 fprintf(stderr, "Error: non existent action: %s\n", k);
68d5ba54
SH
487 ret = -1;
488 goto bad_val;
489 }
490 if (strcmp(a->id, k) != 0) {
32a121cb 491 fprintf(stderr, "Error: non existent action: %s\n", k);
68d5ba54
SH
492 ret = -1;
493 goto bad_val;
494 }
495
32a121cb
SH
496 argc -= 1;
497 argv += 1;
68d5ba54 498 if (argc <= 0) {
ed485691
SH
499 fprintf(stderr,
500 "Error: no index specified action: %s\n", k);
68d5ba54
SH
501 ret = -1;
502 goto bad_val;
503 }
504
505 if (matches(*argv, "index") == 0) {
506 NEXT_ARG();
507 if (get_u32(&i, *argv, 10)) {
508 fprintf(stderr, "Illegal \"index\"\n");
509 ret = -1;
510 goto bad_val;
511 }
32a121cb
SH
512 argc -= 1;
513 argv += 1;
68d5ba54 514 } else {
ed485691
SH
515 fprintf(stderr,
516 "Error: no index specified action: %s\n", k);
68d5ba54
SH
517 ret = -1;
518 goto bad_val;
519 }
520
4a86fe19 521 tail2 = NLMSG_TAIL(&req.n);
522 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
523 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
9e713525
JHS
524 if (i > 0)
525 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
4a86fe19 526 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
68d5ba54
SH
527
528 }
529
4a86fe19 530 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
68d5ba54 531
68d5ba54 532 req.n.nlmsg_seq = rth.dump = ++rth.seq;
c1027a75 533
83cf5bc7 534 if (rtnl_talk(&rth, &req.n, cmd == RTM_DELACTION ? NULL : &ans) < 0) {
68d5ba54 535 fprintf(stderr, "We have an error talking to the kernel\n");
52f337c1 536 return 1;
68d5ba54
SH
537 }
538
86bf43c7 539 if (cmd == RTM_GETACTION && print_action(NULL, ans, stdout) < 0) {
68d5ba54 540 fprintf(stderr, "Dump terminated\n");
86bf43c7 541 free(ans);
52f337c1 542 return 1;
68d5ba54 543 }
86bf43c7 544 free(ans);
68d5ba54
SH
545
546 *argc_p = argc;
547 *argv_p = argv;
68d5ba54
SH
548bad_val:
549 return ret;
550}
551
485d0c60
CM
552struct tc_action_req {
553 struct nlmsghdr n;
554 struct tcamsg t;
555 char buf[MAX_MSG];
556};
557
ed485691 558static int tc_action_modify(int cmd, unsigned int flags,
485d0c60
CM
559 int *argc_p, char ***argv_p,
560 void *buf, size_t buflen)
68d5ba54 561{
485d0c60 562 struct tc_action_req *req, action_req;
68d5ba54 563 char **argv = *argv_p;
485d0c60
CM
564 struct rtattr *tail;
565 int argc = *argc_p;
566 struct iovec iov;
68d5ba54 567 int ret = 0;
485d0c60
CM
568
569 if (buf) {
570 req = buf;
571 if (buflen < sizeof (struct tc_action_req)) {
572 fprintf(stderr, "buffer is too small: %zu\n", buflen);
573 return -1;
574 }
575 } else {
576 memset(&action_req, 0, sizeof (struct tc_action_req));
577 req = &action_req;
578 }
579
580 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
581 req->n.nlmsg_flags = NLM_F_REQUEST | flags;
582 req->n.nlmsg_type = cmd;
583 req->t.tca_family = AF_UNSPEC;
584 tail = NLMSG_TAIL(&req->n);
68d5ba54 585
32a121cb
SH
586 argc -= 1;
587 argv += 1;
485d0c60 588 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req->n)) {
68d5ba54
SH
589 fprintf(stderr, "Illegal \"action\"\n");
590 return -1;
591 }
485d0c60
CM
592 tail->rta_len = (void *) NLMSG_TAIL(&req->n) - (void *) tail;
593
594 *argc_p = argc;
595 *argv_p = argv;
596
597 if (buf)
598 return 0;
68d5ba54 599
485d0c60
CM
600 iov.iov_base = &req->n;
601 iov.iov_len = req->n.nlmsg_len;
602 if (rtnl_talk_iov(&rth, &iov, 1, NULL) < 0) {
68d5ba54
SH
603 fprintf(stderr, "We have an error talking to the kernel\n");
604 ret = -1;
605 }
606
68d5ba54
SH
607 return ret;
608}
609
9e713525 610static int tc_act_list_or_flush(int *argc_p, char ***argv_p, int event)
68d5ba54 611{
9e713525 612 struct rtattr *tail, *tail2, *tail3, *tail4;
68d5ba54 613 int ret = 0, prio = 0, msg_size = 0;
68d5ba54 614 struct action_util *a = NULL;
9e713525
JHS
615 struct nla_bitfield32 flag_select = { 0 };
616 char **argv = *argv_p;
617 __u32 msec_since = 0;
618 int argc = *argc_p;
b317557f 619 char k[FILTER_NAMESZ];
68d5ba54
SH
620 struct {
621 struct nlmsghdr n;
622 struct tcamsg t;
623 char buf[MAX_MSG];
a0a73b29
PS
624 } req = {
625 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
626 .t.tca_family = AF_UNSPEC,
a0a73b29 627 };
68d5ba54 628
4a86fe19 629 tail = NLMSG_TAIL(&req.n);
68d5ba54 630 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
4a86fe19 631 tail2 = NLMSG_TAIL(&req.n);
68d5ba54 632
32a121cb 633 strncpy(k, *argv, sizeof(k) - 1);
68d5ba54 634#ifdef CONFIG_GACT
ed485691 635 if (!gact_ld)
68d5ba54 636 get_action_kind("gact");
ed485691 637
68d5ba54
SH
638#endif
639 a = get_action_kind(k);
32a121cb
SH
640 if (a == NULL) {
641 fprintf(stderr, "bad action %s\n", k);
68d5ba54
SH
642 goto bad_val;
643 }
644 if (strcmp(a->id, k) != 0) {
32a121cb 645 fprintf(stderr, "bad action %s\n", k);
68d5ba54
SH
646 goto bad_val;
647 }
32a121cb 648 strncpy(k, *argv, sizeof(k) - 1);
68d5ba54 649
9e713525
JHS
650 argc -= 1;
651 argv += 1;
652
653 if (argc && (strcmp(*argv, "since") == 0)) {
654 NEXT_ARG();
655 if (get_u32(&msec_since, *argv, 0))
656 invarg("dump time \"since\" is invalid", *argv);
657 }
658
68d5ba54
SH
659 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
660 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
4a86fe19 661 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
662 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
68d5ba54 663
9e713525
JHS
664 tail3 = NLMSG_TAIL(&req.n);
665 flag_select.value |= TCA_FLAG_LARGE_DUMP_ON;
666 flag_select.selector |= TCA_FLAG_LARGE_DUMP_ON;
667 addattr_l(&req.n, MAX_MSG, TCA_ROOT_FLAGS, &flag_select,
668 sizeof(struct nla_bitfield32));
669 tail3->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail3;
670 if (msec_since) {
671 tail4 = NLMSG_TAIL(&req.n);
672 addattr32(&req.n, MAX_MSG, TCA_ROOT_TIME_DELTA, msec_since);
673 tail4->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail4;
674 }
ed485691
SH
675 msg_size = NLMSG_ALIGN(req.n.nlmsg_len)
676 - NLMSG_ALIGN(sizeof(struct nlmsghdr));
68d5ba54 677
ae665a52 678 if (event == RTM_GETACTION) {
ed485691
SH
679 if (rtnl_dump_request(&rth, event,
680 (void *)&req.t, msg_size) < 0) {
68d5ba54 681 perror("Cannot send dump request");
52f337c1 682 return 1;
68d5ba54 683 }
cd70f3f5 684 ret = rtnl_dump_filter(&rth, print_action, stdout);
68d5ba54
SH
685 }
686
ae665a52 687 if (event == RTM_DELACTION) {
68d5ba54
SH
688 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
689 req.n.nlmsg_type = RTM_DELACTION;
690 req.n.nlmsg_flags |= NLM_F_ROOT;
691 req.n.nlmsg_flags |= NLM_F_REQUEST;
86bf43c7 692 if (rtnl_talk(&rth, &req.n, NULL) < 0) {
68d5ba54 693 fprintf(stderr, "We have an error flushing\n");
52f337c1 694 return 1;
68d5ba54
SH
695 }
696
697 }
698
699bad_val:
700
9e713525
JHS
701 *argc_p = argc;
702 *argv_p = argv;
68d5ba54
SH
703 return ret;
704}
705
485d0c60 706int do_action(int argc, char **argv, void *buf, size_t buflen)
68d5ba54
SH
707{
708
709 int ret = 0;
710
711 while (argc > 0) {
712
713 if (matches(*argv, "add") == 0) {
ed485691
SH
714 ret = tc_action_modify(RTM_NEWACTION,
715 NLM_F_EXCL | NLM_F_CREATE,
485d0c60 716 &argc, &argv, buf, buflen);
68d5ba54
SH
717 } else if (matches(*argv, "change") == 0 ||
718 matches(*argv, "replace") == 0) {
ed485691
SH
719 ret = tc_action_modify(RTM_NEWACTION,
720 NLM_F_CREATE | NLM_F_REPLACE,
485d0c60 721 &argc, &argv, buf, buflen);
68d5ba54 722 } else if (matches(*argv, "delete") == 0) {
32a121cb
SH
723 argc -= 1;
724 argv += 1;
68d5ba54
SH
725 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
726 } else if (matches(*argv, "get") == 0) {
32a121cb
SH
727 argc -= 1;
728 argv += 1;
68d5ba54 729 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
ed485691
SH
730 } else if (matches(*argv, "list") == 0 ||
731 matches(*argv, "show") == 0 ||
732 matches(*argv, "lst") == 0) {
68d5ba54
SH
733 if (argc <= 2) {
734 act_usage();
735 return -1;
736 }
9e713525
JHS
737
738 argc -= 2;
739 argv += 2;
740 return tc_act_list_or_flush(&argc, &argv,
741 RTM_GETACTION);
68d5ba54
SH
742 } else if (matches(*argv, "flush") == 0) {
743 if (argc <= 2) {
744 act_usage();
745 return -1;
746 }
9e713525
JHS
747
748 argc -= 2;
749 argv += 2;
750 return tc_act_list_or_flush(&argc, &argv,
751 RTM_DELACTION);
68d5ba54
SH
752 } else if (matches(*argv, "help") == 0) {
753 act_usage();
754 return -1;
755 } else {
ed485691
SH
756 fprintf(stderr,
757 "Command \"%s\" is unknown, try \"tc actions help\".\n",
758 *argv);
68d5ba54
SH
759 return -1;
760 }
6fcf36c9
WC
761
762 if (ret < 0)
763 return -1;
68d5ba54
SH
764 }
765
766 return 0;
767}