m_action.c revision ae665a522bd46bea44c5ea84c89c8b1731954170
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 batch_c = 0;
36int tab_flush = 0;
37
38void 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	exit(-1);
63}
64
65static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
66{
67	if (opt && RTA_PAYLOAD(opt))
68		fprintf(f, "[Unknown action, optlen=%u] ",
69			(unsigned) RTA_PAYLOAD(opt));
70	return 0;
71}
72
73static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
74{
75	int argc = *argc_p;
76	char **argv = *argv_p;
77
78	if (argc) {
79		fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
80	} else {
81		fprintf(stderr, "Unknown action \"%s\"\n", au->id);
82	}
83	return -1;
84}
85
86struct action_util *get_action_kind(char *str)
87{
88	static void *aBODY;
89	void *dlh;
90	char buf[256];
91	struct action_util *a;
92#ifdef CONFIG_GACT
93	int looked4gact = 0;
94restart_s:
95#endif
96	for (a = action_list; a; a = a->next) {
97		if (strcmp(a->id, str) == 0)
98			return a;
99	}
100
101	snprintf(buf, sizeof(buf), "m_%s.so", str);
102	dlh = dlopen(buf, RTLD_LAZY);
103	if (dlh == NULL) {
104		dlh = aBODY;
105		if (dlh == NULL) {
106			dlh = aBODY = dlopen(NULL, RTLD_LAZY);
107			if (dlh == NULL)
108				goto noexist;
109		}
110	}
111
112	snprintf(buf, sizeof(buf), "%s_action_util", str);
113	a = dlsym(dlh, buf);
114	if (a == NULL)
115		goto noexist;
116
117reg:
118	a->next = action_list;
119	action_list = a;
120	return a;
121
122noexist:
123#ifdef CONFIG_GACT
124	if (!looked4gact) {
125		looked4gact = 1;
126		strcpy(str,"gact");
127		goto restart_s;
128	}
129#endif
130	a = malloc(sizeof(*a));
131	if (a) {
132		memset(a, 0, sizeof(*a));
133		strncpy(a->id, "noact", 15);
134		a->parse_aopt = parse_noaopt;
135		a->print_aopt = print_noaopt;
136		goto reg;
137	}
138	return a;
139}
140
141int
142new_cmd(char **argv)
143{
144	if ((matches(*argv, "change") == 0) ||
145		(matches(*argv, "replace") == 0)||
146		(matches(*argv, "delete") == 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, "help") == 0) {
189			return -1;
190		} else if (new_cmd(argv)) {
191			goto done0;
192		} else {
193			struct action_util *a = NULL;
194			strncpy(k, *argv, sizeof (k) - 1);
195			eap = 0;
196			if (argc > 0 ) {
197				a = get_action_kind(k);
198			} else {
199done0:
200				if (ok)
201					break;
202				else
203					goto done;
204			}
205
206			if (NULL == a) {
207				goto bad_val;
208			}
209
210			tail = NLMSG_TAIL(n);
211			addattr_l(n, MAX_MSG, ++prio, NULL, 0);
212			addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
213
214			ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
215
216			if (ret < 0) {
217				fprintf(stderr,"bad action parsing\n");
218				goto bad_val;
219			}
220			tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
221			ok++;
222		}
223
224	}
225
226	if (eap > 0) {
227		fprintf(stderr,"bad action empty %d\n",eap);
228		goto bad_val;
229	}
230
231	tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
232
233done:
234	*argc_p = argc;
235	*argv_p = argv;
236	return 0;
237bad_val:
238	/* no need to undo things, returning from here should
239	 * cause enough pain */
240	fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
241	return -1;
242}
243
244int
245tc_print_one_action(FILE * f, struct rtattr *arg)
246{
247
248	struct rtattr *tb[TCA_ACT_MAX + 1];
249	int err = 0;
250	struct action_util *a = NULL;
251
252	if (arg == NULL)
253		return -1;
254
255	parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
256	if (tb[TCA_ACT_KIND] == NULL) {
257		fprintf(stderr, "NULL Action!\n");
258		return -1;
259	}
260
261
262	a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
263	if (NULL == a)
264		return err;
265
266	if (tab_flush) {
267		fprintf(f," %s \n", a->id);
268		tab_flush = 0;
269		return 0;
270	}
271
272	err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
273
274
275	if (0 > err)
276		return err;
277
278	if (show_stats && tb[TCA_ACT_STATS]) {
279		fprintf(f, "\tAction statistics:\n");
280		print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
281		fprintf(f, "\n");
282	}
283
284	return 0;
285}
286
287int
288tc_print_action(FILE * f, const struct rtattr *arg)
289{
290
291	int i;
292	struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
293
294	if (arg == NULL)
295		return 0;
296
297	parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
298
299	if (tab_flush && NULL != tb[0]  && NULL == tb[1]) {
300		int ret = tc_print_one_action(f, tb[0]);
301		return ret;
302	}
303
304	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
305		if (tb[i]) {
306			fprintf(f, "\n\taction order %d: ", i + batch_c);
307			if (0 > tc_print_one_action(f, tb[i])) {
308				fprintf(f, "Error printing action\n");
309			}
310		}
311
312	}
313
314	batch_c+=TCA_ACT_MAX_PRIO ;
315	return 0;
316}
317
318int print_action(const struct sockaddr_nl *who,
319			   struct nlmsghdr *n,
320			   void *arg)
321{
322	FILE *fp = (FILE*)arg;
323	struct tcamsg *t = NLMSG_DATA(n);
324	int len = n->nlmsg_len;
325	struct rtattr * tb[TCAA_MAX+1];
326
327	len -= NLMSG_LENGTH(sizeof(*t));
328
329	if (len < 0) {
330		fprintf(stderr, "Wrong len %d\n", len);
331		return -1;
332	}
333
334	parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
335
336	if (NULL == tb[TCA_ACT_TAB]) {
337		if (n->nlmsg_type != RTM_GETACTION)
338			fprintf(stderr, "print_action: NULL kind\n");
339		return -1;
340	}
341
342	if (n->nlmsg_type == RTM_DELACTION) {
343		if (n->nlmsg_flags & NLM_F_ROOT) {
344			fprintf(fp, "Flushed table ");
345			tab_flush = 1;
346		} else {
347			fprintf(fp, "deleted action ");
348		}
349	}
350
351	if (n->nlmsg_type == RTM_NEWACTION)
352		fprintf(fp, "Added action ");
353	tc_print_action(fp, tb[TCA_ACT_TAB]);
354
355	return 0;
356}
357
358int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
359{
360	char k[16];
361	struct action_util *a = NULL;
362	int argc = *argc_p;
363	char **argv = *argv_p;
364	int prio = 0;
365	int ret = 0;
366	__u32 i;
367	struct sockaddr_nl nladdr;
368	struct rtattr *tail;
369	struct rtattr *tail2;
370	struct nlmsghdr *ans = NULL;
371
372	struct {
373		struct nlmsghdr         n;
374		struct tcamsg           t;
375		char                    buf[MAX_MSG];
376	} req;
377
378	req.t.tca_family = AF_UNSPEC;
379
380	memset(&req, 0, sizeof(req));
381
382	memset(&nladdr, 0, sizeof(nladdr));
383	nladdr.nl_family = AF_NETLINK;
384
385	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
386	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
387	req.n.nlmsg_type = cmd;
388	argc -=1;
389	argv +=1;
390
391
392	tail = NLMSG_TAIL(&req.n);
393	addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
394
395	while (argc > 0) {
396		if (strcmp(*argv, "action") == 0 ) {
397			argc--;
398			argv++;
399			continue;
400		} else if (strcmp(*argv, "help") == 0) {
401			return -1;
402		}
403
404		strncpy(k, *argv, sizeof (k) - 1);
405		a = get_action_kind(k);
406		if (NULL == a) {
407			fprintf(stderr, "Error: non existent action: %s\n",k);
408			ret = -1;
409			goto bad_val;
410		}
411		if (strcmp(a->id, k) != 0) {
412			fprintf(stderr, "Error: non existent action: %s\n",k);
413			ret = -1;
414			goto bad_val;
415		}
416
417		argc -=1;
418		argv +=1;
419		if (argc <= 0) {
420			fprintf(stderr, "Error: no index specified action: %s\n",k);
421			ret = -1;
422			goto bad_val;
423		}
424
425		if (matches(*argv, "index") == 0) {
426			NEXT_ARG();
427			if (get_u32(&i, *argv, 10)) {
428				fprintf(stderr, "Illegal \"index\"\n");
429				ret = -1;
430				goto bad_val;
431			}
432			argc -=1;
433			argv +=1;
434		} else {
435			fprintf(stderr, "Error: no index specified action: %s\n",k);
436			ret = -1;
437			goto bad_val;
438		}
439
440		tail2 = NLMSG_TAIL(&req.n);
441		addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
442		addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
443		addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
444		tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
445
446	}
447
448	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
449
450	req.n.nlmsg_seq = rth.dump = ++rth.seq;
451	if (cmd == RTM_GETACTION)
452		ans = &req.n;
453
454	if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
455		fprintf(stderr, "We have an error talking to the kernel\n");
456		return 1;
457	}
458
459	if (ans && print_action(NULL, &req.n, (void*)stdout) < 0) {
460		fprintf(stderr, "Dump terminated\n");
461		return 1;
462	}
463
464	*argc_p = argc;
465	*argv_p = argv;
466bad_val:
467	return ret;
468}
469
470int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
471{
472	int argc = *argc_p;
473	char **argv = *argv_p;
474	int ret = 0;
475
476	struct rtattr *tail;
477	struct {
478		struct nlmsghdr         n;
479		struct tcamsg           t;
480		char                    buf[MAX_MSG];
481	} req;
482
483	req.t.tca_family = AF_UNSPEC;
484
485	memset(&req, 0, sizeof(req));
486
487	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
488	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
489	req.n.nlmsg_type = cmd;
490	tail = NLMSG_TAIL(&req.n);
491	argc -=1;
492	argv +=1;
493	if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
494		fprintf(stderr, "Illegal \"action\"\n");
495		return -1;
496	}
497	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
498
499	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
500		fprintf(stderr, "We have an error talking to the kernel\n");
501		ret = -1;
502	}
503
504	*argc_p = argc;
505	*argv_p = argv;
506
507	return ret;
508}
509
510int tc_act_list_or_flush(int argc, char **argv, int event)
511{
512	int ret = 0, prio = 0, msg_size = 0;
513	char k[16];
514	struct rtattr *tail,*tail2;
515	struct action_util *a = NULL;
516	struct {
517		struct nlmsghdr         n;
518		struct tcamsg           t;
519		char                    buf[MAX_MSG];
520	} req;
521
522	req.t.tca_family = AF_UNSPEC;
523
524	memset(&req, 0, sizeof(req));
525
526	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
527
528	tail = NLMSG_TAIL(&req.n);
529	addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
530	tail2 = NLMSG_TAIL(&req.n);
531
532	strncpy(k, *argv, sizeof (k) - 1);
533#ifdef CONFIG_GACT
534	if (!gact_ld) {
535		get_action_kind("gact");
536	}
537#endif
538	a = get_action_kind(k);
539	if (NULL == a) {
540		fprintf(stderr,"bad action %s\n",k);
541		goto bad_val;
542	}
543	if (strcmp(a->id, k) != 0) {
544		fprintf(stderr,"bad action %s\n",k);
545		goto bad_val;
546	}
547	strncpy(k, *argv, sizeof (k) - 1);
548
549	addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
550	addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
551	tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
552	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
553
554	msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
555
556	if (event == RTM_GETACTION) {
557		if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
558			perror("Cannot send dump request");
559			return 1;
560		}
561		ret = rtnl_dump_filter(&rth, print_action, stdout, NULL, NULL);
562	}
563
564	if (event == RTM_DELACTION) {
565		req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
566		req.n.nlmsg_type = RTM_DELACTION;
567		req.n.nlmsg_flags |= NLM_F_ROOT;
568		req.n.nlmsg_flags |= NLM_F_REQUEST;
569		if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
570			fprintf(stderr, "We have an error flushing\n");
571			return 1;
572		}
573
574	}
575
576bad_val:
577
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 actions help\".\n", *argv);
624			return -1;
625		}
626	}
627
628	return 0;
629}
630
631