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