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