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