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