]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_action.c
libnetlink: add size argument to rtnl_talk
[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 *
14*/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <syslog.h>
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
68d5ba54
SH
31static struct action_util * action_list;
32#ifdef CONFIG_GACT
33int gact_ld = 0 ; //fuckin backward compatibility
34#endif
68d5ba54
SH
35int tab_flush = 0;
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 *
302d3fb7 45 */
f1e4f042 46 fprintf (stderr, "usage: tc actions <ACTSPECOP>*\n");
47 fprintf(stderr,
48 "Where: \tACTSPECOP := ACR | GD | FL\n"
302d3fb7
SH
49 "\tACR := add | change | replace <ACTSPEC>* \n"
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] ",
bb6a21a4 68 (unsigned) RTA_PAYLOAD(opt));
68d5ba54
SH
69 return 0;
70}
71
72static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
73{
74 int argc = *argc_p;
75 char **argv = *argv_p;
76
77 if (argc) {
78 fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
79 } else {
80 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
81 }
82 return -1;
83}
84
d1f28cf1 85static struct action_util *get_action_kind(char *str)
68d5ba54 86{
cda4026c 87 static void *aBODY;
68d5ba54
SH
88 void *dlh;
89 char buf[256];
90 struct action_util *a;
91#ifdef CONFIG_GACT
92 int looked4gact = 0;
93restart_s:
94#endif
95 for (a = action_list; a; a = a->next) {
96 if (strcmp(a->id, str) == 0)
97 return a;
98 }
99
ab814d63 100 snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str);
02833d1b 101 dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
68d5ba54
SH
102 if (dlh == NULL) {
103 dlh = aBODY;
104 if (dlh == NULL) {
105 dlh = aBODY = dlopen(NULL, RTLD_LAZY);
106 if (dlh == NULL)
107 goto noexist;
108 }
109 }
110
95812b56 111 snprintf(buf, sizeof(buf), "%s_action_util", str);
68d5ba54
SH
112 a = dlsym(dlh, buf);
113 if (a == NULL)
114 goto noexist;
115
116reg:
117 a->next = action_list;
118 action_list = a;
119 return a;
120
121noexist:
122#ifdef CONFIG_GACT
123 if (!looked4gact) {
124 looked4gact = 1;
125 strcpy(str,"gact");
126 goto restart_s;
127 }
128#endif
129 a = malloc(sizeof(*a));
130 if (a) {
131 memset(a, 0, sizeof(*a));
132 strncpy(a->id, "noact", 15);
133 a->parse_aopt = parse_noaopt;
134 a->print_aopt = print_noaopt;
135 goto reg;
136 }
137 return a;
138}
139
d1f28cf1 140static int
ae665a52 141new_cmd(char **argv)
68d5ba54
SH
142{
143 if ((matches(*argv, "change") == 0) ||
144 (matches(*argv, "replace") == 0)||
145 (matches(*argv, "delete") == 0)||
b159a7f1 146 (matches(*argv, "get") == 0)||
68d5ba54
SH
147 (matches(*argv, "add") == 0))
148 return 1;
149
150 return 0;
151
152}
153
154int
155parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
156{
157 int argc = *argc_p;
158 char **argv = *argv_p;
159 struct rtattr *tail, *tail2;
160 char k[16];
161 int ok = 0;
162 int eap = 0; /* expect action parameters */
163
164 int ret = 0;
165 int prio = 0;
166
167 if (argc <= 0)
168 return -1;
169
4a86fe19 170 tail = tail2 = NLMSG_TAIL(n);
68d5ba54
SH
171
172 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
173
174 while (argc > 0) {
175
176 memset(k, 0, sizeof (k));
177
178 if (strcmp(*argv, "action") == 0 ) {
179 argc--;
180 argv++;
181 eap = 1;
182#ifdef CONFIG_GACT
183 if (!gact_ld) {
184 get_action_kind("gact");
185 }
186#endif
187 continue;
9282d08d
JHS
188 } else if (strcmp(*argv, "flowid") == 0) {
189 break;
190 } else if (strcmp(*argv, "classid") == 0) {
191 break;
68d5ba54
SH
192 } else if (strcmp(*argv, "help") == 0) {
193 return -1;
194 } else if (new_cmd(argv)) {
195 goto done0;
196 } else {
197 struct action_util *a = NULL;
198 strncpy(k, *argv, sizeof (k) - 1);
199 eap = 0;
200 if (argc > 0 ) {
201 a = get_action_kind(k);
202 } else {
203done0:
204 if (ok)
205 break;
206 else
207 goto done;
208 }
209
ae665a52 210 if (NULL == a) {
68d5ba54
SH
211 goto bad_val;
212 }
213
4a86fe19 214 tail = NLMSG_TAIL(n);
68d5ba54
SH
215 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
216 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
217
218 ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
219
220 if (ret < 0) {
221 fprintf(stderr,"bad action parsing\n");
222 goto bad_val;
223 }
4a86fe19 224 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
68d5ba54
SH
225 ok++;
226 }
227
228 }
229
230 if (eap > 0) {
231 fprintf(stderr,"bad action empty %d\n",eap);
232 goto bad_val;
233 }
234
4a86fe19 235 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
68d5ba54
SH
236
237done:
238 *argc_p = argc;
239 *argv_p = argv;
240 return 0;
241bad_val:
ae665a52 242 /* no need to undo things, returning from here should
68d5ba54
SH
243 * cause enough pain */
244 fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
245 return -1;
246}
247
d1f28cf1 248static int
68d5ba54
SH
249tc_print_one_action(FILE * f, struct rtattr *arg)
250{
251
954de6c7 252 struct rtattr *tb[TCA_ACT_MAX + 1];
68d5ba54
SH
253 int err = 0;
254 struct action_util *a = NULL;
255
256 if (arg == NULL)
257 return -1;
258
954de6c7
JHS
259 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
260
261 if (tb[TCA_ACT_KIND] == NULL) {
68d5ba54
SH
262 fprintf(stderr, "NULL Action!\n");
263 return -1;
264 }
265
266
954de6c7 267 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
68d5ba54
SH
268 if (NULL == a)
269 return err;
270
954de6c7 271 err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]);
68d5ba54
SH
272
273 if (0 > err)
274 return err;
275
954de6c7 276 if (show_stats && tb[TCA_ACT_STATS]) {
e5879dc6 277 fprintf(f, "\tAction statistics:\n");
278 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
3d327bf5 279 fprintf(f, "\n");
68d5ba54
SH
280 }
281
282 return 0;
283}
284
954de6c7
JHS
285static int
286tc_print_action_flush(FILE *f, const struct rtattr *arg)
287{
288
289 struct rtattr *tb[TCA_MAX + 1];
290 int err = 0;
291 struct action_util *a = NULL;
292 __u32 *delete_count = 0;
293
294 parse_rtattr_nested(tb, TCA_MAX, arg);
295
296 if (tb[TCA_KIND] == NULL) {
297 fprintf(stderr, "NULL Action!\n");
298 return -1;
299 }
300
301 a = get_action_kind(RTA_DATA(tb[TCA_KIND]));
302 if (NULL == a)
303 return err;
304
305 delete_count = RTA_DATA(tb[TCA_FCNT]);
306 fprintf(f," %s (%d entries)\n", a->id, *delete_count);
307 tab_flush = 0;
308 return 0;
309}
310
68d5ba54 311int
954de6c7 312tc_print_action(FILE *f, const struct rtattr *arg)
68d5ba54
SH
313{
314
315 int i;
316 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
317
318 if (arg == NULL)
319 return 0;
320
ac2fc2df 321 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
68d5ba54 322
954de6c7
JHS
323 if (tab_flush && NULL != tb[0] && NULL == tb[1])
324 return tc_print_action_flush(f, tb[0]);
68d5ba54
SH
325
326 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
327 if (tb[i]) {
f24a7e72 328 fprintf(f, "\n\taction order %d: ", i);
68d5ba54
SH
329 if (0 > tc_print_one_action(f, tb[i])) {
330 fprintf(f, "Error printing action\n");
331 }
332 }
333
334 }
335
68d5ba54
SH
336 return 0;
337}
338
5bec3484 339int print_action(const struct sockaddr_nl *who,
50772dc5 340 struct nlmsghdr *n,
3d327bf5 341 void *arg)
68d5ba54
SH
342{
343 FILE *fp = (FILE*)arg;
344 struct tcamsg *t = NLMSG_DATA(n);
345 int len = n->nlmsg_len;
346 struct rtattr * tb[TCAA_MAX+1];
347
348 len -= NLMSG_LENGTH(sizeof(*t));
349
350 if (len < 0) {
351 fprintf(stderr, "Wrong len %d\n", len);
352 return -1;
353 }
354
68d5ba54
SH
355 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
356
357 if (NULL == tb[TCA_ACT_TAB]) {
358 if (n->nlmsg_type != RTM_GETACTION)
5bec3484 359 fprintf(stderr, "print_action: NULL kind\n");
68d5ba54 360 return -1;
ae665a52 361 }
68d5ba54
SH
362
363 if (n->nlmsg_type == RTM_DELACTION) {
364 if (n->nlmsg_flags & NLM_F_ROOT) {
ae665a52 365 fprintf(fp, "Flushed table ");
68d5ba54
SH
366 tab_flush = 1;
367 } else {
368 fprintf(fp, "deleted action ");
369 }
370 }
371
372 if (n->nlmsg_type == RTM_NEWACTION)
373 fprintf(fp, "Added action ");
374 tc_print_action(fp, tb[TCA_ACT_TAB]);
375
376 return 0;
377}
378
d1f28cf1 379static int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
68d5ba54
SH
380{
381 char k[16];
382 struct action_util *a = NULL;
383 int argc = *argc_p;
384 char **argv = *argv_p;
385 int prio = 0;
386 int ret = 0;
387 __u32 i;
68d5ba54
SH
388 struct sockaddr_nl nladdr;
389 struct rtattr *tail;
390 struct rtattr *tail2;
391 struct nlmsghdr *ans = NULL;
392
393 struct {
394 struct nlmsghdr n;
395 struct tcamsg t;
396 char buf[MAX_MSG];
397 } req;
398
399 req.t.tca_family = AF_UNSPEC;
400
401 memset(&req, 0, sizeof(req));
402
403 memset(&nladdr, 0, sizeof(nladdr));
404 nladdr.nl_family = AF_NETLINK;
405
406 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
407 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
408 req.n.nlmsg_type = cmd;
409 argc -=1;
410 argv +=1;
411
412
4a86fe19 413 tail = NLMSG_TAIL(&req.n);
68d5ba54
SH
414 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
415
416 while (argc > 0) {
417 if (strcmp(*argv, "action") == 0 ) {
418 argc--;
419 argv++;
420 continue;
421 } else if (strcmp(*argv, "help") == 0) {
422 return -1;
423 }
424
425 strncpy(k, *argv, sizeof (k) - 1);
426 a = get_action_kind(k);
ae665a52 427 if (NULL == a) {
68d5ba54
SH
428 fprintf(stderr, "Error: non existent action: %s\n",k);
429 ret = -1;
430 goto bad_val;
431 }
432 if (strcmp(a->id, k) != 0) {
433 fprintf(stderr, "Error: non existent action: %s\n",k);
434 ret = -1;
435 goto bad_val;
436 }
437
438 argc -=1;
439 argv +=1;
440 if (argc <= 0) {
441 fprintf(stderr, "Error: no index specified action: %s\n",k);
442 ret = -1;
443 goto bad_val;
444 }
445
446 if (matches(*argv, "index") == 0) {
447 NEXT_ARG();
448 if (get_u32(&i, *argv, 10)) {
449 fprintf(stderr, "Illegal \"index\"\n");
450 ret = -1;
451 goto bad_val;
452 }
453 argc -=1;
454 argv +=1;
455 } else {
456 fprintf(stderr, "Error: no index specified action: %s\n",k);
457 ret = -1;
458 goto bad_val;
459 }
460
4a86fe19 461 tail2 = NLMSG_TAIL(&req.n);
462 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
463 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
464 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
465 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
68d5ba54
SH
466
467 }
468
4a86fe19 469 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
68d5ba54 470
68d5ba54
SH
471 req.n.nlmsg_seq = rth.dump = ++rth.seq;
472 if (cmd == RTM_GETACTION)
473 ans = &req.n;
c1027a75 474
c079e121 475 if (rtnl_talk(&rth, &req.n, ans, MAX_MSG) < 0) {
68d5ba54 476 fprintf(stderr, "We have an error talking to the kernel\n");
52f337c1 477 return 1;
68d5ba54
SH
478 }
479
5bec3484 480 if (ans && print_action(NULL, &req.n, (void*)stdout) < 0) {
68d5ba54 481 fprintf(stderr, "Dump terminated\n");
52f337c1 482 return 1;
68d5ba54
SH
483 }
484
485 *argc_p = argc;
486 *argv_p = argv;
68d5ba54
SH
487bad_val:
488 return ret;
489}
490
d1f28cf1 491static int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
68d5ba54
SH
492{
493 int argc = *argc_p;
494 char **argv = *argv_p;
495 int ret = 0;
496
68d5ba54
SH
497 struct rtattr *tail;
498 struct {
499 struct nlmsghdr n;
500 struct tcamsg t;
501 char buf[MAX_MSG];
502 } req;
503
504 req.t.tca_family = AF_UNSPEC;
505
506 memset(&req, 0, sizeof(req));
507
508 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
509 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
510 req.n.nlmsg_type = cmd;
4a86fe19 511 tail = NLMSG_TAIL(&req.n);
68d5ba54
SH
512 argc -=1;
513 argv +=1;
514 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
515 fprintf(stderr, "Illegal \"action\"\n");
516 return -1;
517 }
4a86fe19 518 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
68d5ba54 519
c079e121 520 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
68d5ba54
SH
521 fprintf(stderr, "We have an error talking to the kernel\n");
522 ret = -1;
523 }
524
525 *argc_p = argc;
526 *argv_p = argv;
c1027a75 527
68d5ba54
SH
528 return ret;
529}
530
d1f28cf1 531static int tc_act_list_or_flush(int argc, char **argv, int event)
68d5ba54
SH
532{
533 int ret = 0, prio = 0, msg_size = 0;
534 char k[16];
68d5ba54
SH
535 struct rtattr *tail,*tail2;
536 struct action_util *a = NULL;
537 struct {
538 struct nlmsghdr n;
539 struct tcamsg t;
540 char buf[MAX_MSG];
541 } req;
542
543 req.t.tca_family = AF_UNSPEC;
544
545 memset(&req, 0, sizeof(req));
546
547 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
548
4a86fe19 549 tail = NLMSG_TAIL(&req.n);
68d5ba54 550 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
4a86fe19 551 tail2 = NLMSG_TAIL(&req.n);
68d5ba54
SH
552
553 strncpy(k, *argv, sizeof (k) - 1);
554#ifdef CONFIG_GACT
555 if (!gact_ld) {
556 get_action_kind("gact");
557 }
558#endif
559 a = get_action_kind(k);
ae665a52 560 if (NULL == a) {
68d5ba54
SH
561 fprintf(stderr,"bad action %s\n",k);
562 goto bad_val;
563 }
564 if (strcmp(a->id, k) != 0) {
565 fprintf(stderr,"bad action %s\n",k);
566 goto bad_val;
567 }
568 strncpy(k, *argv, sizeof (k) - 1);
569
570 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
571 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
4a86fe19 572 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
573 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
68d5ba54 574
68d5ba54
SH
575 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
576
ae665a52 577 if (event == RTM_GETACTION) {
68d5ba54
SH
578 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
579 perror("Cannot send dump request");
52f337c1 580 return 1;
68d5ba54 581 }
cd70f3f5 582 ret = rtnl_dump_filter(&rth, print_action, stdout);
68d5ba54
SH
583 }
584
ae665a52 585 if (event == RTM_DELACTION) {
68d5ba54
SH
586 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
587 req.n.nlmsg_type = RTM_DELACTION;
588 req.n.nlmsg_flags |= NLM_F_ROOT;
589 req.n.nlmsg_flags |= NLM_F_REQUEST;
c079e121 590 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
68d5ba54 591 fprintf(stderr, "We have an error flushing\n");
52f337c1 592 return 1;
68d5ba54
SH
593 }
594
595 }
596
597bad_val:
598
68d5ba54
SH
599 return ret;
600}
601
602int do_action(int argc, char **argv)
603{
604
605 int ret = 0;
606
607 while (argc > 0) {
608
609 if (matches(*argv, "add") == 0) {
610 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
611 } else if (matches(*argv, "change") == 0 ||
612 matches(*argv, "replace") == 0) {
613 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
614 } else if (matches(*argv, "delete") == 0) {
615 argc -=1;
616 argv +=1;
617 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
618 } else if (matches(*argv, "get") == 0) {
619 argc -=1;
620 argv +=1;
621 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
622 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
623 || matches(*argv, "lst") == 0) {
624 if (argc <= 2) {
625 act_usage();
626 return -1;
627 }
628 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
629 } else if (matches(*argv, "flush") == 0) {
630 if (argc <= 2) {
631 act_usage();
632 return -1;
633 }
634 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
635 } else if (matches(*argv, "help") == 0) {
636 act_usage();
637 return -1;
638 } else {
639
640 ret = -1;
641 }
642
643 if (ret < 0) {
f1e4f042 644 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
68d5ba54
SH
645 return -1;
646 }
647 }
648
649 return 0;
650}
651