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