m_gact.c revision f2f99e2eefdbd9cb6a750b19a7b3036db351b983
1/*
2 * m_gact.c		generic actions module
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 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22
23#include "utils.h"
24#include "tc_util.h"
25#include <linux/tc_act/tc_gact.h>
26
27/* define to turn on probablity stuff */
28
29#ifdef CONFIG_GACT_PROB
30static const char *prob_n2a(int p)
31{
32	if (p == PGACT_NONE)
33		return "none";
34	if (p == PGACT_NETRAND)
35		return "netrand";
36	if (p == PGACT_DETERM)
37		return "determ";
38	return "none";
39}
40#endif
41
42static void
43explain(void)
44{
45#ifdef CONFIG_GACT_PROB
46	fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
47	fprintf(stderr,
48		"Where: ACTION := reclassify | drop | continue | pass "
49		        "RAND := random <RANDTYPE> <ACTION> <VAL>"
50		        "RANDTYPE := netrand | determ"
51			"VAL : = value not exceeding 10000"
52			"INDEX := index value used"
53			"\n");
54#else
55	fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
56	fprintf(stderr,
57		"Where: ACTION := reclassify | drop | continue | pass "
58		"INDEX := index value used"
59			"\n");
60#endif
61}
62
63#define usage() return(-1)
64
65int
66get_act(char ***argv_p)
67{
68	char **argv = *argv_p;
69
70	if (matches(*argv, "reclassify") == 0) {
71		return TC_ACT_RECLASSIFY;
72	} else if (matches(*argv, "drop") == 0 || matches(*argv, "shot") == 0) {
73		return TC_ACT_SHOT;
74	} else if (matches(*argv, "continue") == 0) {
75		return TC_ACT_UNSPEC;
76	} else if (matches(*argv, "pipe") == 0) {
77		return TC_ACT_PIPE;
78	} else if (matches(*argv, "pass") == 0 || matches(*argv, "ok") == 0)  {
79		return TC_ACT_OK;
80	} else {
81		fprintf(stderr,"bad action type %s\n",*argv);
82		return -10;
83	}
84}
85
86int
87parse_gact(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
88{
89	int argc = *argc_p;
90	char **argv = *argv_p;
91	int ok = 0;
92	int action = TC_POLICE_RECLASSIFY;
93	struct tc_gact p;
94#ifdef CONFIG_GACT_PROB
95	int rd = 0;
96	struct tc_gact_p pp;
97#endif
98	struct rtattr *tail;
99
100	memset(&p, 0, sizeof (p));
101	p.action = TC_POLICE_RECLASSIFY;
102
103	if (argc < 0)
104		return -1;
105
106
107	if (matches(*argv, "gact") == 0) {
108		ok++;
109	} else {
110		action = get_act(&argv);
111		if (action != -10) {
112			p.action = action;
113			ok++;
114		} else {
115			explain();
116			return action;
117		}
118	}
119
120	if (ok) {
121		argc--;
122		argv++;
123	}
124
125#ifdef CONFIG_GACT_PROB
126	if (ok && argc > 0) {
127		if (matches(*argv, "random") == 0) {
128			rd = 1;
129			NEXT_ARG();
130			if (matches(*argv, "netrand") == 0) {
131				NEXT_ARG();
132				pp.ptype = PGACT_NETRAND;
133			} else if  (matches(*argv, "determ") == 0) {
134				NEXT_ARG();
135				pp.ptype = PGACT_DETERM;
136			} else {
137				fprintf(stderr, "Illegal \"random type\"\n");
138				return -1;
139			}
140
141			action = get_act(&argv);
142			if (action != -10) { /* FIXME */
143				pp.paction = action;
144			} else {
145				explain();
146				return -1;
147			}
148			argc--;
149			argv++;
150			if (get_u16(&pp.pval, *argv, 10)) {
151				fprintf(stderr, "Illegal probability val 0x%x\n",pp.pval);
152				return -1;
153			}
154			if (pp.pval > 10000) {
155				fprintf(stderr, "Illegal probability val  0x%x\n",pp.pval);
156				return -1;
157			}
158			argc--;
159			argv++;
160		}
161	}
162#endif
163
164	if (argc > 0) {
165		if (matches(*argv, "index") == 0) {
166			NEXT_ARG();
167			if (get_u32(&p.index, *argv, 10)) {
168				fprintf(stderr, "Illegal \"index\"\n");
169				return -1;
170			}
171			argc--;
172			argv++;
173			ok++;
174		}
175	}
176
177	if (!ok)
178		return -1;
179
180	tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len));
181	addattr_l(n, MAX_MSG, tca_id, NULL, 0);
182	addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof (p));
183#ifdef CONFIG_GACT_PROB
184	if (rd) {
185		addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof (pp));
186	}
187#endif
188	tail->rta_len =
189	    (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)) - (void *) tail;
190
191	*argc_p = argc;
192	*argv_p = argv;
193	return 0;
194}
195
196int
197print_gact(struct action_util *au,FILE * f, struct rtattr *arg)
198{
199	SPRINT_BUF(b1);
200#ifdef CONFIG_GACT_PROB
201	SPRINT_BUF(b2);
202	struct tc_gact_p *pp = NULL;
203	struct tc_gact_p pp_dummy;
204#endif
205	struct tc_gact *p = NULL;
206	struct rtattr *tb[TCA_GACT_MAX + 1];
207
208	if (arg == NULL)
209		return -1;
210
211	memset(tb, 0, sizeof (tb));
212	parse_rtattr(tb, TCA_GACT_MAX, RTA_DATA(arg), RTA_PAYLOAD(arg));
213
214	if (tb[TCA_GACT_PARMS] == NULL) {
215		fprintf(f, "[NULL gact parameters]");
216		return -1;
217	}
218	p = RTA_DATA(tb[TCA_GACT_PARMS]);
219
220	fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof (b1)));
221#ifdef CONFIG_GACT_PROB
222	if (NULL != tb[TCA_GACT_PROB]) {
223		pp = RTA_DATA(tb[TCA_GACT_PROB]);
224	} else {
225		/* need to keep consistent output */
226		memset(&pp_dummy, 0, sizeof (pp_dummy));
227		pp = &pp_dummy;
228	}
229	fprintf(f, "\n\t random type %s %s val %d",prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval);
230#endif
231	fprintf(f, "\n\t index %d ref %d bind %d",p->index, p->refcnt, p->bindcnt);
232	if (show_stats) {
233		if (tb[TCA_GACT_TM]) {
234			struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
235			print_tm(f,tm);
236		}
237	}
238	fprintf(f, "\n ");
239	return 0;
240}
241
242struct action_util gact_util = {
243	.id = "gact",
244	.parse_aopt = parse_gact,
245	.print_aopt = print_gact,
246};
247