]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_filter.c
2 * tc_filter.c "tc filter".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include <linux/if_ether.h>
27 #include "tc_common.h"
29 static void usage(void);
31 static void usage(void)
33 fprintf(stderr
, "Usage: tc filter [ add | del | change | replace | show ] dev STRING\n");
34 fprintf(stderr
, " [ pref PRIO ] protocol PROTO\n");
35 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
36 fprintf(stderr
, " [ root | classid CLASSID ] [ handle FILTERID ]\n");
37 fprintf(stderr
, " [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n");
38 fprintf(stderr
, "\n");
39 fprintf(stderr
, " tc filter show [ dev STRING ] [ root | parent CLASSID ]\n");
40 fprintf(stderr
, "Where:\n");
41 fprintf(stderr
, "FILTER_TYPE := { rsvp | u32 | fw | route | etc. }\n");
42 fprintf(stderr
, "FILTERID := ... format depends on classifier, see there\n");
43 fprintf(stderr
, "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
48 int tc_filter_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
55 struct filter_util
*q
= NULL
;
62 struct tc_estimator est
;
64 memset(&req
, 0, sizeof(req
));
65 memset(&est
, 0, sizeof(est
));
66 memset(d
, 0, sizeof(d
));
67 memset(k
, 0, sizeof(k
));
68 memset(&req
, 0, sizeof(req
));
70 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
));
71 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
72 req
.n
.nlmsg_type
= cmd
;
73 req
.t
.tcm_family
= AF_UNSPEC
;
75 if (cmd
== RTM_NEWTFILTER
&& flags
& NLM_F_CREATE
)
79 if (strcmp(*argv
, "dev") == 0) {
83 strncpy(d
, *argv
, sizeof(d
)-1);
84 } else if (strcmp(*argv
, "root") == 0) {
85 if (req
.t
.tcm_parent
) {
86 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
89 req
.t
.tcm_parent
= TC_H_ROOT
;
90 } else if (strcmp(*argv
, "parent") == 0) {
94 duparg("parent", *argv
);
95 if (get_tc_classid(&handle
, *argv
))
96 invarg(*argv
, "Invalid parent ID");
97 req
.t
.tcm_parent
= handle
;
98 } else if (strcmp(*argv
, "handle") == 0) {
101 duparg("handle", *argv
);
103 } else if (matches(*argv
, "preference") == 0 ||
104 matches(*argv
, "priority") == 0) {
107 duparg("priority", *argv
);
108 if (get_u32(&prio
, *argv
, 0))
109 invarg(*argv
, "invalid priority value");
110 } else if (matches(*argv
, "protocol") == 0) {
114 duparg("protocol", *argv
);
115 if (ll_proto_a2n(&id
, *argv
))
116 invarg(*argv
, "invalid protocol");
119 } else if (matches(*argv
, "estimator") == 0) {
120 if (parse_estimator(&argc
, &argv
, &est
) < 0)
122 } else if (matches(*argv
, "help") == 0) {
125 strncpy(k
, *argv
, sizeof(k
)-1);
127 q
= get_filter_kind(k
);
135 req
.t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
138 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
141 if (q
->parse_fopt(q
, fhandle
, argc
, argv
, &req
.n
))
145 fprintf(stderr
, "Must specify filter type when using "
150 if (matches(*argv
, "help") == 0)
152 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv
);
157 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
163 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
164 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
169 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
170 fprintf(stderr
, "We have an error talking to the kernel\n");
177 static __u32 filter_parent
;
178 static int filter_ifindex
;
179 static __u32 filter_prio
;
180 static __u32 filter_protocol
;
183 int print_filter(const struct sockaddr_nl
*who
,
187 FILE *fp
= (FILE*)arg
;
188 struct tcmsg
*t
= NLMSG_DATA(n
);
189 int len
= n
->nlmsg_len
;
190 struct rtattr
* tb
[TCA_MAX
+1];
191 struct filter_util
*q
;
194 if (n
->nlmsg_type
!= RTM_NEWTFILTER
&& n
->nlmsg_type
!= RTM_DELTFILTER
) {
195 fprintf(stderr
, "Not a filter\n");
198 len
-= NLMSG_LENGTH(sizeof(*t
));
200 fprintf(stderr
, "Wrong len %d\n", len
);
204 memset(tb
, 0, sizeof(tb
));
205 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
207 if (tb
[TCA_KIND
] == NULL
) {
208 fprintf(stderr
, "print_filter: NULL kind\n");
212 if (n
->nlmsg_type
== RTM_DELTFILTER
)
213 fprintf(fp
, "deleted ");
215 fprintf(fp
, "filter ");
216 if (!filter_ifindex
|| filter_ifindex
!= t
->tcm_ifindex
)
217 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
219 if (!filter_parent
|| filter_parent
!= t
->tcm_parent
) {
220 if (t
->tcm_parent
== TC_H_ROOT
)
221 fprintf(fp
, "root ");
223 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
224 fprintf(fp
, "parent %s ", abuf
);
228 f_proto
= TC_H_MIN(t
->tcm_info
);
229 __u32 prio
= TC_H_MAJ(t
->tcm_info
)>>16;
230 if (!filter_protocol
|| filter_protocol
!= f_proto
) {
233 fprintf(fp
, "protocol %s ",
234 ll_proto_n2a(f_proto
, b1
, sizeof(b1
)));
237 if (!filter_prio
|| filter_prio
!= prio
) {
239 fprintf(fp
, "pref %u ", prio
);
242 fprintf(fp
, "%s ", (char*)RTA_DATA(tb
[TCA_KIND
]));
243 q
= get_filter_kind(RTA_DATA(tb
[TCA_KIND
]));
244 if (tb
[TCA_OPTIONS
]) {
246 q
->print_fopt(q
, fp
, tb
[TCA_OPTIONS
], t
->tcm_handle
);
248 fprintf(fp
, "[cannot parse parameters]");
252 if (show_stats
&& (tb
[TCA_STATS
] || tb
[TCA_STATS2
])) {
253 print_tcstats_attr(fp
, tb
, " ", NULL
);
262 int tc_filter_list(int argc
, char **argv
)
268 char *fhandle
= NULL
;
270 memset(&t
, 0, sizeof(t
));
271 t
.tcm_family
= AF_UNSPEC
;
272 memset(d
, 0, sizeof(d
));
275 if (strcmp(*argv
, "dev") == 0) {
278 duparg("dev", *argv
);
279 strncpy(d
, *argv
, sizeof(d
)-1);
280 } else if (strcmp(*argv
, "root") == 0) {
282 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
285 filter_parent
= t
.tcm_parent
= TC_H_ROOT
;
286 } else if (strcmp(*argv
, "parent") == 0) {
290 duparg("parent", *argv
);
291 if (get_tc_classid(&handle
, *argv
))
292 invarg(*argv
, "invalid parent ID");
293 filter_parent
= t
.tcm_parent
= handle
;
294 } else if (strcmp(*argv
, "handle") == 0) {
297 duparg("handle", *argv
);
299 } else if (matches(*argv
, "preference") == 0 ||
300 matches(*argv
, "priority") == 0) {
303 duparg("priority", *argv
);
304 if (get_u32(&prio
, *argv
, 0))
305 invarg(*argv
, "invalid preference");
307 } else if (matches(*argv
, "protocol") == 0) {
311 duparg("protocol", *argv
);
312 if (ll_proto_a2n(&res
, *argv
))
313 invarg(*argv
, "invalid protocol");
315 filter_protocol
= protocol
;
316 } else if (matches(*argv
, "help") == 0) {
319 fprintf(stderr
, " What is \"%s\"? Try \"tc filter help\"\n", *argv
);
326 t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
331 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
332 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
335 filter_ifindex
= t
.tcm_ifindex
;
338 if (rtnl_dump_request(&rth
, RTM_GETTFILTER
, &t
, sizeof(t
)) < 0) {
339 perror("Cannot send dump request");
343 if (rtnl_dump_filter(&rth
, print_filter
, stdout
, NULL
, NULL
) < 0) {
344 fprintf(stderr
, "Dump terminated\n");
351 int do_filter(int argc
, char **argv
)
354 return tc_filter_list(0, NULL
);
355 if (matches(*argv
, "add") == 0)
356 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
357 if (matches(*argv
, "change") == 0)
358 return tc_filter_modify(RTM_NEWTFILTER
, 0, argc
-1, argv
+1);
359 if (matches(*argv
, "replace") == 0)
360 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_CREATE
, argc
-1, argv
+1);
361 if (matches(*argv
, "delete") == 0)
362 return tc_filter_modify(RTM_DELTFILTER
, 0, argc
-1, argv
+1);
364 if (matches(*argv
, "get") == 0)
365 return tc_filter_get(RTM_GETTFILTER
, 0, argc
-1, argv
+1);
367 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
368 || matches(*argv
, "lst") == 0)
369 return tc_filter_list(argc
-1, argv
+1);
370 if (matches(*argv
, "help") == 0) {
374 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv
);