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