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