]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
Use NLMSG_TAIL
[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_util.h"
29
30 static struct action_util * action_list;
31 #ifdef CONFIG_GACT
32 int gact_ld = 0 ; //fuckin backward compatibility
33 #endif
34 int batch_c = 0;
35 int tab_flush = 0;
36
37 void act_usage(void)
38 {
39 fprintf (stderr, "action usage improper\n");
40 }
41
42 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
43 {
44 if (opt && RTA_PAYLOAD(opt))
45 fprintf(f, "[Unknown action, optlen=%u] ",
46 (unsigned) RTA_PAYLOAD(opt));
47 return 0;
48 }
49
50 static 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
63 struct action_util *get_action_kind(char *str)
64 {
65 static void *aBODY;
66 void *dlh;
67 char buf[256];
68 struct action_util *a;
69 #ifdef CONFIG_GACT
70 int looked4gact = 0;
71 restart_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
89 snprintf(buf, sizeof(buf), "%s_action_util", str);
90 a = dlsym(dlh, buf);
91 if (a == NULL)
92 goto noexist;
93
94 reg:
95 a->next = action_list;
96 action_list = a;
97 return a;
98
99 noexist:
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
118 int
119 new_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
131 int
132 parse_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
147 tail = tail2 = NLMSG_TAIL(n);
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 {
176 done0:
177 if (ok)
178 break;
179 else
180 goto done;
181 }
182
183 if (NULL == a) {
184 goto bad_val;
185 }
186
187 tail = NLMSG_TAIL(n);
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 }
197 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
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
208 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
209
210 done:
211 *argc_p = argc;
212 *argv_p = argv;
213 return 0;
214 bad_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
221 int
222 tc_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
232 memset(tb, 0, sizeof (tb));
233 parse_rtattr(tb, TCA_ACT_MAX, RTA_DATA(arg), RTA_PAYLOAD(arg));
234 if (tb[TCA_ACT_KIND] == NULL) {
235 fprintf(stderr, "NULL Action!\n");
236 return -1;
237 }
238
239
240 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
241 if (NULL == a)
242 return err;
243
244 if (tab_flush) {
245 fprintf(f," %s \n", a->id);
246 tab_flush = 0;
247 return 0;
248 }
249
250 err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
251
252
253 if (0 > err)
254 return err;
255
256 if (show_stats && tb[TCA_ACT_STATS]) {
257 fprintf(f, "\tAction statistics:\n");
258 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
259 fprintf(f, "\n");
260 }
261
262 return 0;
263 }
264
265 int
266 tc_print_action(FILE * f, const struct rtattr *arg)
267 {
268
269 int i;
270 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
271
272 if (arg == NULL)
273 return 0;
274
275 memset(tb, 0, sizeof (tb));
276 parse_rtattr(tb, TCA_ACT_MAX_PRIO, RTA_DATA(arg), RTA_PAYLOAD(arg));
277
278 if (tab_flush && NULL != tb[0] && NULL == tb[1]) {
279 int ret = tc_print_one_action(f, tb[0]);
280 return ret;
281 }
282
283 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
284 if (tb[i]) {
285 fprintf(f, "\n\taction order %d: ", i + batch_c);
286 if (0 > tc_print_one_action(f, tb[i])) {
287 fprintf(f, "Error printing action\n");
288 }
289 }
290
291 }
292
293 batch_c+=TCA_ACT_MAX_PRIO ;
294 return 0;
295 }
296
297 static int do_print_action(const struct sockaddr_nl *who,
298 struct nlmsghdr *n,
299 void *arg)
300 {
301 FILE *fp = (FILE*)arg;
302 struct tcamsg *t = NLMSG_DATA(n);
303 int len = n->nlmsg_len;
304 struct rtattr * tb[TCAA_MAX+1];
305
306 len -= NLMSG_LENGTH(sizeof(*t));
307
308 if (len < 0) {
309 fprintf(stderr, "Wrong len %d\n", len);
310 return -1;
311 }
312
313 memset(tb, 0, sizeof(tb));
314 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
315
316 if (NULL == tb[TCA_ACT_TAB]) {
317 if (n->nlmsg_type != RTM_GETACTION)
318 fprintf(stderr, "do_print_action: NULL kind\n");
319 return -1;
320 }
321
322 if (n->nlmsg_type == RTM_DELACTION) {
323 if (n->nlmsg_flags & NLM_F_ROOT) {
324 fprintf(fp, "Flushed table ");
325 tab_flush = 1;
326 } else {
327 fprintf(fp, "deleted action ");
328 }
329 }
330
331 if (n->nlmsg_type == RTM_NEWACTION)
332 fprintf(fp, "Added action ");
333 tc_print_action(fp, tb[TCA_ACT_TAB]);
334
335 return 0;
336 }
337
338 int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
339 {
340 char k[16];
341 struct action_util *a = NULL;
342 int argc = *argc_p;
343 char **argv = *argv_p;
344 int prio = 0;
345 int ret = 0;
346 __u32 i;
347 struct rtnl_handle rth;
348 struct sockaddr_nl nladdr;
349 struct rtattr *tail;
350 struct rtattr *tail2;
351 struct nlmsghdr *ans = NULL;
352
353 struct {
354 struct nlmsghdr n;
355 struct tcamsg t;
356 char buf[MAX_MSG];
357 } req;
358
359 req.t.tca_family = AF_UNSPEC;
360
361 memset(&req, 0, sizeof(req));
362
363 memset(&nladdr, 0, sizeof(nladdr));
364 nladdr.nl_family = AF_NETLINK;
365
366 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
367 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
368 req.n.nlmsg_type = cmd;
369 argc -=1;
370 argv +=1;
371
372
373 tail = NLMSG_TAIL(&req.n);
374 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
375
376 while (argc > 0) {
377 if (strcmp(*argv, "action") == 0 ) {
378 argc--;
379 argv++;
380 continue;
381 } else if (strcmp(*argv, "help") == 0) {
382 return -1;
383 }
384
385 strncpy(k, *argv, sizeof (k) - 1);
386 a = get_action_kind(k);
387 if (NULL == a) {
388 fprintf(stderr, "Error: non existent action: %s\n",k);
389 ret = -1;
390 goto bad_val;
391 }
392 if (strcmp(a->id, k) != 0) {
393 fprintf(stderr, "Error: non existent action: %s\n",k);
394 ret = -1;
395 goto bad_val;
396 }
397
398 argc -=1;
399 argv +=1;
400 if (argc <= 0) {
401 fprintf(stderr, "Error: no index specified action: %s\n",k);
402 ret = -1;
403 goto bad_val;
404 }
405
406 if (matches(*argv, "index") == 0) {
407 NEXT_ARG();
408 if (get_u32(&i, *argv, 10)) {
409 fprintf(stderr, "Illegal \"index\"\n");
410 ret = -1;
411 goto bad_val;
412 }
413 argc -=1;
414 argv +=1;
415 } else {
416 fprintf(stderr, "Error: no index specified action: %s\n",k);
417 ret = -1;
418 goto bad_val;
419 }
420
421 tail2 = NLMSG_TAIL(&req.n);
422 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
423 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
424 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
425 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
426
427 }
428
429 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
430
431 if (rtnl_open(&rth, 0) < 0) {
432 fprintf(stderr, "Cannot open rtnetlink\n");
433 exit(1);
434 }
435
436 req.n.nlmsg_seq = rth.dump = ++rth.seq;
437 if (cmd == RTM_GETACTION)
438 ans = &req.n;
439 if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
440 fprintf(stderr, "We have an error talking to the kernel\n");
441 rtnl_close(&rth);
442 exit (1);
443 }
444
445 if (ans && do_print_action(NULL, &req.n, (void*)stdout) < 0) {
446 fprintf(stderr, "Dump terminated\n");
447 rtnl_close(&rth);
448 exit(1);
449 }
450
451 *argc_p = argc;
452 *argv_p = argv;
453 rtnl_close(&rth);
454 bad_val:
455 return ret;
456 }
457
458 int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
459 {
460 int argc = *argc_p;
461 char **argv = *argv_p;
462 int ret = 0;
463
464 struct rtnl_handle rth;
465 struct rtattr *tail;
466 struct {
467 struct nlmsghdr n;
468 struct tcamsg t;
469 char buf[MAX_MSG];
470 } req;
471
472 req.t.tca_family = AF_UNSPEC;
473
474 memset(&req, 0, sizeof(req));
475
476 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
477 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
478 req.n.nlmsg_type = cmd;
479 tail = NLMSG_TAIL(&req.n);
480 argc -=1;
481 argv +=1;
482 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
483 fprintf(stderr, "Illegal \"action\"\n");
484 return -1;
485 }
486 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
487
488 if (rtnl_open(&rth, 0) < 0) {
489 fprintf(stderr, "Cannot open rtnetlink\n");
490 exit(1);
491 }
492
493
494 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
495 fprintf(stderr, "We have an error talking to the kernel\n");
496 ret = -1;
497 }
498
499 *argc_p = argc;
500 *argv_p = argv;
501 rtnl_close(&rth);
502 return ret;
503 }
504
505 int tc_act_list_or_flush(int argc, char **argv, int event)
506 {
507 int ret = 0, prio = 0, msg_size = 0;
508 char k[16];
509 struct rtnl_handle rth;
510 struct rtattr *tail,*tail2;
511 struct action_util *a = NULL;
512 struct {
513 struct nlmsghdr n;
514 struct tcamsg t;
515 char buf[MAX_MSG];
516 } req;
517
518 req.t.tca_family = AF_UNSPEC;
519
520 memset(&req, 0, sizeof(req));
521
522 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
523
524 tail = NLMSG_TAIL(&req.n);
525 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
526 tail2 = NLMSG_TAIL(&req.n);
527
528 strncpy(k, *argv, sizeof (k) - 1);
529 #ifdef CONFIG_GACT
530 if (!gact_ld) {
531 get_action_kind("gact");
532 }
533 #endif
534 a = get_action_kind(k);
535 if (NULL == a) {
536 fprintf(stderr,"bad action %s\n",k);
537 goto bad_val;
538 }
539 if (strcmp(a->id, k) != 0) {
540 fprintf(stderr,"bad action %s\n",k);
541 goto bad_val;
542 }
543 strncpy(k, *argv, sizeof (k) - 1);
544
545 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
546 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
547 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
548 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
549
550 if (rtnl_open(&rth, 0) < 0) {
551 fprintf(stderr, "Cannot open rtnetlink\n");
552 exit(1);
553 }
554
555 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
556
557 if (event == RTM_GETACTION) {
558 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
559 perror("Cannot send dump request");
560 exit(1);
561 }
562 ret = rtnl_dump_filter(&rth, do_print_action, stdout, NULL, NULL);
563 }
564
565 if (event == RTM_DELACTION) {
566 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
567 req.n.nlmsg_type = RTM_DELACTION;
568 req.n.nlmsg_flags |= NLM_F_ROOT;
569 req.n.nlmsg_flags |= NLM_F_REQUEST;
570 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
571 fprintf(stderr, "We have an error flushing\n");
572 rtnl_close(&rth);
573 exit (1);
574 }
575
576 }
577
578 bad_val:
579
580 rtnl_close(&rth);
581 return ret;
582 }
583
584 int do_action(int argc, char **argv)
585 {
586
587 int ret = 0;
588
589 while (argc > 0) {
590
591 if (matches(*argv, "add") == 0) {
592 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
593 } else if (matches(*argv, "change") == 0 ||
594 matches(*argv, "replace") == 0) {
595 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
596 } else if (matches(*argv, "delete") == 0) {
597 argc -=1;
598 argv +=1;
599 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
600 } else if (matches(*argv, "get") == 0) {
601 argc -=1;
602 argv +=1;
603 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
604 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
605 || matches(*argv, "lst") == 0) {
606 if (argc <= 2) {
607 act_usage();
608 return -1;
609 }
610 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
611 } else if (matches(*argv, "flush") == 0) {
612 if (argc <= 2) {
613 act_usage();
614 return -1;
615 }
616 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
617 } else if (matches(*argv, "help") == 0) {
618 act_usage();
619 return -1;
620 } else {
621
622 ret = -1;
623 }
624
625 if (ret < 0) {
626 fprintf(stderr, "Command \"%s\" is unknown, try \"tc action help\".\n", *argv);
627 return -1;
628 }
629 }
630
631 return 0;
632 }
633