]>
Commit | Line | Data |
---|---|---|
2265da08 SH |
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 | |
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 | |
43 | explain(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 | ||
65 | int | |
66 | get_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 | ||
86 | int | |
87 | parse_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 | ||
228569c3 | 180 | tail = NLMSG_TAIL(n); |
2265da08 SH |
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 | |
228569c3 | 188 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
2265da08 SH |
189 | |
190 | *argc_p = argc; | |
191 | *argv_p = argv; | |
192 | return 0; | |
193 | } | |
194 | ||
195 | int | |
196 | print_gact(struct action_util *au,FILE * f, struct rtattr *arg) | |
197 | { | |
198 | SPRINT_BUF(b1); | |
199 | #ifdef CONFIG_GACT_PROB | |
200 | SPRINT_BUF(b2); | |
201 | struct tc_gact_p *pp = NULL; | |
202 | struct tc_gact_p pp_dummy; | |
203 | #endif | |
204 | struct tc_gact *p = NULL; | |
205 | struct rtattr *tb[TCA_GACT_MAX + 1]; | |
206 | ||
207 | if (arg == NULL) | |
208 | return -1; | |
209 | ||
3b3ecd31 | 210 | parse_rtattr_nested(tb, TCA_GACT_MAX, arg); |
2265da08 SH |
211 | |
212 | if (tb[TCA_GACT_PARMS] == NULL) { | |
213 | fprintf(f, "[NULL gact parameters]"); | |
214 | return -1; | |
215 | } | |
216 | p = RTA_DATA(tb[TCA_GACT_PARMS]); | |
217 | ||
218 | fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof (b1))); | |
219 | #ifdef CONFIG_GACT_PROB | |
220 | if (NULL != tb[TCA_GACT_PROB]) { | |
221 | pp = RTA_DATA(tb[TCA_GACT_PROB]); | |
222 | } else { | |
223 | /* need to keep consistent output */ | |
224 | memset(&pp_dummy, 0, sizeof (pp_dummy)); | |
225 | pp = &pp_dummy; | |
226 | } | |
227 | fprintf(f, "\n\t random type %s %s val %d",prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval); | |
228 | #endif | |
229 | fprintf(f, "\n\t index %d ref %d bind %d",p->index, p->refcnt, p->bindcnt); | |
230 | if (show_stats) { | |
231 | if (tb[TCA_GACT_TM]) { | |
232 | struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]); | |
233 | print_tm(f,tm); | |
234 | } | |
235 | } | |
236 | fprintf(f, "\n "); | |
237 | return 0; | |
238 | } | |
239 | ||
95812b56 | 240 | struct action_util gact_action_util = { |
2265da08 SH |
241 | .id = "gact", |
242 | .parse_aopt = parse_gact, | |
243 | .print_aopt = print_gact, | |
2265da08 | 244 | }; |