]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_filter.c
tc: m_action: Improve conversion to C99 style initializers
[mirror_iproute2.git] / tc / tc_filter.c
CommitLineData
aba5acdf
SH
1/*
2 * tc_filter.c "tc filter".
3 *
4 * This program is free software; you can redistribute 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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
aba5acdf
SH
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <linux/if_ether.h>
23
24#include "rt_names.h"
25#include "utils.h"
26#include "tc_util.h"
27#include "tc_common.h"
28
aba5acdf
SH
29static void usage(void)
30{
e5d179d8 31 fprintf(stderr, "Usage: tc filter [ add | del | change | replace | show ] dev STRING\n");
de33a430 32 fprintf(stderr, " [ pref PRIO ] protocol PROTO\n");
aba5acdf 33 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
8f9afdd5
DB
34 fprintf(stderr, " [ root | ingress | egress | parent CLASSID ]\n");
35 fprintf(stderr, " [ handle FILTERID ] [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n");
aba5acdf 36 fprintf(stderr, "\n");
8f9afdd5 37 fprintf(stderr, " tc filter show [ dev STRING ] [ root | ingress | egress | parent CLASSID ]\n");
aba5acdf 38 fprintf(stderr, "Where:\n");
4bd62446 39 fprintf(stderr, "FILTER_TYPE := { rsvp | u32 | bpf | fw | route | etc. }\n");
aba5acdf
SH
40 fprintf(stderr, "FILTERID := ... format depends on classifier, see there\n");
41 fprintf(stderr, "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
aba5acdf
SH
42}
43
32a121cb 44static int tc_filter_modify(int cmd, unsigned int flags, int argc, char **argv)
aba5acdf 45{
aba5acdf 46 struct {
32a121cb
SH
47 struct nlmsghdr n;
48 struct tcmsg t;
49 char buf[MAX_MSG];
aba5acdf
SH
50 } req;
51 struct filter_util *q = NULL;
52 __u32 prio = 0;
ae761068 53 __u32 protocol = 0;
083a5f00 54 int protocol_set = 0;
aba5acdf
SH
55 char *fhandle = NULL;
56 char d[16];
57 char k[16];
58 struct tc_estimator est;
59
60 memset(&req, 0, sizeof(req));
61 memset(&est, 0, sizeof(est));
62 memset(d, 0, sizeof(d));
63 memset(k, 0, sizeof(k));
64 memset(&req, 0, sizeof(req));
65
66 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
67 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
68 req.n.nlmsg_type = cmd;
69 req.t.tcm_family = AF_UNSPEC;
70
ae761068 71 if (cmd == RTM_NEWTFILTER && flags & NLM_F_CREATE)
05fb9184 72 protocol = htons(ETH_P_ALL);
ae761068 73
aba5acdf
SH
74 while (argc > 0) {
75 if (strcmp(*argv, "dev") == 0) {
76 NEXT_ARG();
77 if (d[0])
78 duparg("dev", *argv);
79 strncpy(d, *argv, sizeof(d)-1);
80 } else if (strcmp(*argv, "root") == 0) {
81 if (req.t.tcm_parent) {
82 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
024481bb 83 return -1;
aba5acdf
SH
84 }
85 req.t.tcm_parent = TC_H_ROOT;
8f9afdd5
DB
86 } else if (strcmp(*argv, "ingress") == 0) {
87 if (req.t.tcm_parent) {
88 fprintf(stderr, "Error: \"ingress\" is duplicate parent ID\n");
89 return -1;
90 }
91 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
92 TC_H_MIN_INGRESS);
93 } else if (strcmp(*argv, "egress") == 0) {
94 if (req.t.tcm_parent) {
95 fprintf(stderr, "Error: \"egress\" is duplicate parent ID\n");
96 return -1;
97 }
98 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
99 TC_H_MIN_EGRESS);
aba5acdf
SH
100 } else if (strcmp(*argv, "parent") == 0) {
101 __u32 handle;
32a121cb 102
aba5acdf
SH
103 NEXT_ARG();
104 if (req.t.tcm_parent)
105 duparg("parent", *argv);
106 if (get_tc_classid(&handle, *argv))
f1675d61 107 invarg("Invalid parent ID", *argv);
aba5acdf
SH
108 req.t.tcm_parent = handle;
109 } else if (strcmp(*argv, "handle") == 0) {
110 NEXT_ARG();
111 if (fhandle)
112 duparg("handle", *argv);
113 fhandle = *argv;
114 } else if (matches(*argv, "preference") == 0 ||
115 matches(*argv, "priority") == 0) {
116 NEXT_ARG();
117 if (prio)
118 duparg("priority", *argv);
424adc19 119 if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
f1675d61 120 invarg("invalid priority value", *argv);
aba5acdf
SH
121 } else if (matches(*argv, "protocol") == 0) {
122 __u16 id;
32a121cb 123
aba5acdf 124 NEXT_ARG();
083a5f00 125 if (protocol_set)
aba5acdf
SH
126 duparg("protocol", *argv);
127 if (ll_proto_a2n(&id, *argv))
f1675d61 128 invarg("invalid protocol", *argv);
aba5acdf 129 protocol = id;
083a5f00 130 protocol_set = 1;
aba5acdf
SH
131 } else if (matches(*argv, "estimator") == 0) {
132 if (parse_estimator(&argc, &argv, &est) < 0)
133 return -1;
134 } else if (matches(*argv, "help") == 0) {
135 usage();
3a99df70 136 return 0;
aba5acdf
SH
137 } else {
138 strncpy(k, *argv, sizeof(k)-1);
139
140 q = get_filter_kind(k);
141 argc--; argv++;
142 break;
143 }
144
145 argc--; argv++;
146 }
147
148 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
149
150 if (k[0])
151 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
152
153 if (q) {
154 if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
024481bb 155 return 1;
aba5acdf
SH
156 } else {
157 if (fhandle) {
32a121cb 158 fprintf(stderr, "Must specify filter type when using \"handle\"\n");
024481bb 159 return -1;
aba5acdf
SH
160 }
161 if (argc) {
162 if (matches(*argv, "help") == 0)
163 usage();
164 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv);
024481bb 165 return -1;
aba5acdf
SH
166 }
167 }
168 if (est.ewma_log)
169 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
170
aba5acdf
SH
171
172 if (d[0]) {
3d0b7439 173 ll_init_map(&rth);
aba5acdf
SH
174
175 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
176 fprintf(stderr, "Cannot find device \"%s\"\n", d);
024481bb 177 return 1;
aba5acdf
SH
178 }
179 }
180
c079e121 181 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
2373fde9 182 fprintf(stderr, "We have an error talking to the kernel\n");
024481bb 183 return 2;
2373fde9 184 }
aba5acdf 185
aba5acdf
SH
186 return 0;
187}
188
189static __u32 filter_parent;
190static int filter_ifindex;
191static __u32 filter_prio;
192static __u32 filter_protocol;
32a121cb 193__u16 f_proto;
aba5acdf 194
5bec3484 195int print_filter(const struct sockaddr_nl *who,
ae665a52 196 struct nlmsghdr *n,
de481780 197 void *arg)
aba5acdf 198{
32a121cb 199 FILE *fp = (FILE *)arg;
aba5acdf
SH
200 struct tcmsg *t = NLMSG_DATA(n);
201 int len = n->nlmsg_len;
32a121cb 202 struct rtattr *tb[TCA_MAX+1];
aba5acdf
SH
203 struct filter_util *q;
204 char abuf[256];
205
206 if (n->nlmsg_type != RTM_NEWTFILTER && n->nlmsg_type != RTM_DELTFILTER) {
207 fprintf(stderr, "Not a filter\n");
208 return 0;
209 }
210 len -= NLMSG_LENGTH(sizeof(*t));
211 if (len < 0) {
212 fprintf(stderr, "Wrong len %d\n", len);
213 return -1;
214 }
215
216 memset(tb, 0, sizeof(tb));
217 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
218
219 if (tb[TCA_KIND] == NULL) {
2373fde9 220 fprintf(stderr, "print_filter: NULL kind\n");
aba5acdf
SH
221 return -1;
222 }
223
224 if (n->nlmsg_type == RTM_DELTFILTER)
225 fprintf(fp, "deleted ");
226
227 fprintf(fp, "filter ");
228 if (!filter_ifindex || filter_ifindex != t->tcm_ifindex)
229 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
230
231 if (!filter_parent || filter_parent != t->tcm_parent) {
232 if (t->tcm_parent == TC_H_ROOT)
233 fprintf(fp, "root ");
8f9afdd5
DB
234 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS))
235 fprintf(fp, "ingress ");
236 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS))
237 fprintf(fp, "egress ");
aba5acdf
SH
238 else {
239 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
240 fprintf(fp, "parent %s ", abuf);
241 }
242 }
8f9afdd5 243
aba5acdf 244 if (t->tcm_info) {
eefcbc72 245 f_proto = TC_H_MIN(t->tcm_info);
aba5acdf 246 __u32 prio = TC_H_MAJ(t->tcm_info)>>16;
32a121cb 247
eefcbc72 248 if (!filter_protocol || filter_protocol != f_proto) {
249 if (f_proto) {
aba5acdf
SH
250 SPRINT_BUF(b1);
251 fprintf(fp, "protocol %s ",
eefcbc72 252 ll_proto_n2a(f_proto, b1, sizeof(b1)));
aba5acdf
SH
253 }
254 }
255 if (!filter_prio || filter_prio != prio) {
256 if (prio)
257 fprintf(fp, "pref %u ", prio);
258 }
259 }
ff24746c 260 fprintf(fp, "%s ", rta_getattr_str(tb[TCA_KIND]));
aba5acdf
SH
261 q = get_filter_kind(RTA_DATA(tb[TCA_KIND]));
262 if (tb[TCA_OPTIONS]) {
263 if (q)
264 q->print_fopt(q, fp, tb[TCA_OPTIONS], t->tcm_handle);
265 else
266 fprintf(fp, "[cannot parse parameters]");
267 }
268 fprintf(fp, "\n");
269
e5879dc6 270 if (show_stats && (tb[TCA_STATS] || tb[TCA_STATS2])) {
271 print_tcstats_attr(fp, tb, " ", NULL);
de481780 272 fprintf(fp, "\n");
aba5acdf 273 }
de481780 274
aba5acdf
SH
275 fflush(fp);
276 return 0;
277}
278
d1f28cf1 279static int tc_filter_list(int argc, char **argv)
aba5acdf
SH
280{
281 struct tcmsg t;
aba5acdf
SH
282 char d[16];
283 __u32 prio = 0;
284 __u32 protocol = 0;
285 char *fhandle = NULL;
286
287 memset(&t, 0, sizeof(t));
288 t.tcm_family = AF_UNSPEC;
289 memset(d, 0, sizeof(d));
290
291 while (argc > 0) {
292 if (strcmp(*argv, "dev") == 0) {
293 NEXT_ARG();
294 if (d[0])
295 duparg("dev", *argv);
296 strncpy(d, *argv, sizeof(d)-1);
297 } else if (strcmp(*argv, "root") == 0) {
298 if (t.tcm_parent) {
299 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
024481bb 300 return -1;
aba5acdf
SH
301 }
302 filter_parent = t.tcm_parent = TC_H_ROOT;
8f9afdd5
DB
303 } else if (strcmp(*argv, "ingress") == 0) {
304 if (t.tcm_parent) {
305 fprintf(stderr, "Error: \"ingress\" is duplicate parent ID\n");
306 return -1;
307 }
308 filter_parent = TC_H_MAKE(TC_H_CLSACT,
309 TC_H_MIN_INGRESS);
310 t.tcm_parent = filter_parent;
311 } else if (strcmp(*argv, "egress") == 0) {
312 if (t.tcm_parent) {
313 fprintf(stderr, "Error: \"egress\" is duplicate parent ID\n");
314 return -1;
315 }
316 filter_parent = TC_H_MAKE(TC_H_CLSACT,
317 TC_H_MIN_EGRESS);
318 t.tcm_parent = filter_parent;
aba5acdf
SH
319 } else if (strcmp(*argv, "parent") == 0) {
320 __u32 handle;
32a121cb 321
aba5acdf
SH
322 NEXT_ARG();
323 if (t.tcm_parent)
324 duparg("parent", *argv);
325 if (get_tc_classid(&handle, *argv))
f1675d61 326 invarg("invalid parent ID", *argv);
aba5acdf
SH
327 filter_parent = t.tcm_parent = handle;
328 } else if (strcmp(*argv, "handle") == 0) {
329 NEXT_ARG();
330 if (fhandle)
331 duparg("handle", *argv);
332 fhandle = *argv;
333 } else if (matches(*argv, "preference") == 0 ||
334 matches(*argv, "priority") == 0) {
335 NEXT_ARG();
336 if (prio)
337 duparg("priority", *argv);
338 if (get_u32(&prio, *argv, 0))
f1675d61 339 invarg("invalid preference", *argv);
aba5acdf
SH
340 filter_prio = prio;
341 } else if (matches(*argv, "protocol") == 0) {
342 __u16 res;
32a121cb 343
aba5acdf
SH
344 NEXT_ARG();
345 if (protocol)
346 duparg("protocol", *argv);
347 if (ll_proto_a2n(&res, *argv))
f1675d61 348 invarg("invalid protocol", *argv);
aba5acdf
SH
349 protocol = res;
350 filter_protocol = protocol;
351 } else if (matches(*argv, "help") == 0) {
352 usage();
353 } else {
354 fprintf(stderr, " What is \"%s\"? Try \"tc filter help\"\n", *argv);
024481bb 355 return -1;
aba5acdf
SH
356 }
357
358 argc--; argv++;
359 }
360
361 t.tcm_info = TC_H_MAKE(prio<<16, protocol);
362
3d0b7439 363 ll_init_map(&rth);
aba5acdf
SH
364
365 if (d[0]) {
366 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
367 fprintf(stderr, "Cannot find device \"%s\"\n", d);
024481bb 368 return 1;
aba5acdf
SH
369 }
370 filter_ifindex = t.tcm_ifindex;
371 }
372
3d0b7439 373 if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
aba5acdf 374 perror("Cannot send dump request");
024481bb 375 return 1;
aba5acdf
SH
376 }
377
3d0b7439 378 if (rtnl_dump_filter(&rth, print_filter, stdout) < 0) {
aba5acdf 379 fprintf(stderr, "Dump terminated\n");
024481bb 380 return 1;
aba5acdf
SH
381 }
382
aba5acdf
SH
383 return 0;
384}
385
386int do_filter(int argc, char **argv)
387{
388 if (argc < 1)
389 return tc_filter_list(0, NULL);
390 if (matches(*argv, "add") == 0)
391 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
392 if (matches(*argv, "change") == 0)
393 return tc_filter_modify(RTM_NEWTFILTER, 0, argc-1, argv+1);
394 if (matches(*argv, "replace") == 0)
395 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_CREATE, argc-1, argv+1);
396 if (matches(*argv, "delete") == 0)
397 return tc_filter_modify(RTM_DELTFILTER, 0, argc-1, argv+1);
398#if 0
399 if (matches(*argv, "get") == 0)
400 return tc_filter_get(RTM_GETTFILTER, 0, argc-1, argv+1);
401#endif
402 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
403 || matches(*argv, "lst") == 0)
404 return tc_filter_list(argc-1, argv+1);
e5d179d8 405 if (matches(*argv, "help") == 0) {
aba5acdf 406 usage();
e5d179d8 407 return 0;
32a121cb 408 }
aba5acdf 409 fprintf(stderr, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv);
024481bb 410 return -1;
aba5acdf 411}