]>
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>
19 #include <net/if_arp.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
24 #include <linux/if_ether.h>
29 #include "tc_common.h"
31 static void usage(void) __attribute__((noreturn
));
33 static void usage(void)
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");
50 int tc_filter_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
52 struct rtnl_handle rth
;
58 struct filter_util
*q
= NULL
;
64 struct tc_estimator est
;
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
));
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
;
78 if (strcmp(*argv
, "dev") == 0) {
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");
88 req
.t
.tcm_parent
= TC_H_ROOT
;
89 } else if (strcmp(*argv
, "parent") == 0) {
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) {
100 duparg("handle", *argv
);
102 } else if (matches(*argv
, "preference") == 0 ||
103 matches(*argv
, "priority") == 0) {
106 duparg("priority", *argv
);
107 if (get_u32(&prio
, *argv
, 0))
108 invarg(*argv
, "invalid prpriority value");
109 } else if (matches(*argv
, "protocol") == 0) {
113 duparg("protocol", *argv
);
114 if (ll_proto_a2n(&id
, *argv
))
115 invarg(*argv
, "invalid protocol");
117 } else if (matches(*argv
, "estimator") == 0) {
118 if (parse_estimator(&argc
, &argv
, &est
) < 0)
120 } else if (matches(*argv
, "help") == 0) {
123 strncpy(k
, *argv
, sizeof(k
)-1);
125 q
= get_filter_kind(k
);
133 req
.t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
136 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
139 if (q
->parse_fopt(q
, fhandle
, argc
, argv
, &req
.n
))
143 fprintf(stderr
, "Must specify filter type when using "
148 if (matches(*argv
, "help") == 0)
150 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n", *argv
);
155 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
158 if (rtnl_open(&rth
, 0) < 0) {
159 fprintf(stderr
, "Cannot open rtnetlink\n");
166 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
167 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
173 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0) {
174 fprintf(stderr
, "We have an error talking to the kernel\n");
183 static __u32 filter_parent
;
184 static int filter_ifindex
;
185 static __u32 filter_prio
;
186 static __u32 filter_protocol
;
188 static int print_filter(const struct sockaddr_nl
*who
,
192 FILE *fp
= (FILE*)arg
;
193 struct tcmsg
*t
= NLMSG_DATA(n
);
194 int len
= n
->nlmsg_len
;
195 struct rtattr
* tb
[TCA_MAX
+1];
196 struct filter_util
*q
;
199 if (n
->nlmsg_type
!= RTM_NEWTFILTER
&& n
->nlmsg_type
!= RTM_DELTFILTER
) {
200 fprintf(stderr
, "Not a filter\n");
203 len
-= NLMSG_LENGTH(sizeof(*t
));
205 fprintf(stderr
, "Wrong len %d\n", len
);
209 memset(tb
, 0, sizeof(tb
));
210 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
212 if (tb
[TCA_KIND
] == NULL
) {
213 fprintf(stderr
, "print_filter: NULL kind\n");
217 if (n
->nlmsg_type
== RTM_DELTFILTER
)
218 fprintf(fp
, "deleted ");
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
));
224 if (!filter_parent
|| filter_parent
!= t
->tcm_parent
) {
225 if (t
->tcm_parent
== TC_H_ROOT
)
226 fprintf(fp
, "root ");
228 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
229 fprintf(fp
, "parent %s ", abuf
);
233 __u32 protocol
= TC_H_MIN(t
->tcm_info
);
234 __u32 prio
= TC_H_MAJ(t
->tcm_info
)>>16;
235 if (!filter_protocol
|| filter_protocol
!= protocol
) {
238 fprintf(fp
, "protocol %s ",
239 ll_proto_n2a(protocol
, b1
, sizeof(b1
)));
242 if (!filter_prio
|| filter_prio
!= prio
) {
244 fprintf(fp
, "pref %u ", prio
);
247 fprintf(fp
, "%s ", (char*)RTA_DATA(tb
[TCA_KIND
]));
248 q
= get_filter_kind(RTA_DATA(tb
[TCA_KIND
]));
249 if (tb
[TCA_OPTIONS
]) {
251 q
->print_fopt(q
, fp
, tb
[TCA_OPTIONS
], t
->tcm_handle
);
253 fprintf(fp
, "[cannot parse parameters]");
257 if (show_stats
&& (tb
[TCA_STATS
] || tb
[TCA_STATS2
])) {
258 print_tcstats_attr(fp
, tb
, " ", NULL
);
267 int tc_filter_list(int argc
, char **argv
)
270 struct rtnl_handle rth
;
274 char *fhandle
= NULL
;
276 memset(&t
, 0, sizeof(t
));
277 t
.tcm_family
= AF_UNSPEC
;
278 memset(d
, 0, sizeof(d
));
281 if (strcmp(*argv
, "dev") == 0) {
284 duparg("dev", *argv
);
285 strncpy(d
, *argv
, sizeof(d
)-1);
286 } else if (strcmp(*argv
, "root") == 0) {
288 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
291 filter_parent
= t
.tcm_parent
= TC_H_ROOT
;
292 } else if (strcmp(*argv
, "parent") == 0) {
296 duparg("parent", *argv
);
297 if (get_tc_classid(&handle
, *argv
))
298 invarg(*argv
, "invalid parent ID");
299 filter_parent
= t
.tcm_parent
= handle
;
300 } else if (strcmp(*argv
, "handle") == 0) {
303 duparg("handle", *argv
);
305 } else if (matches(*argv
, "preference") == 0 ||
306 matches(*argv
, "priority") == 0) {
309 duparg("priority", *argv
);
310 if (get_u32(&prio
, *argv
, 0))
311 invarg(*argv
, "invalid preference");
313 } else if (matches(*argv
, "protocol") == 0) {
317 duparg("protocol", *argv
);
318 if (ll_proto_a2n(&res
, *argv
))
319 invarg(*argv
, "invalid protocol");
321 filter_protocol
= protocol
;
322 } else if (matches(*argv
, "help") == 0) {
325 fprintf(stderr
, " What is \"%s\"? Try \"tc filter help\"\n", *argv
);
332 t
.tcm_info
= TC_H_MAKE(prio
<<16, protocol
);
334 if (rtnl_open(&rth
, 0) < 0) {
335 fprintf(stderr
, "Cannot open rtnetlink\n");
342 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
343 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
347 filter_ifindex
= t
.tcm_ifindex
;
350 if (rtnl_dump_request(&rth
, RTM_GETTFILTER
, &t
, sizeof(t
)) < 0) {
351 perror("Cannot send dump request");
356 if (rtnl_dump_filter(&rth
, print_filter
, stdout
, NULL
, NULL
) < 0) {
357 fprintf(stderr
, "Dump terminated\n");
365 int do_filter(int argc
, char **argv
)
368 return tc_filter_list(0, NULL
);
369 if (matches(*argv
, "add") == 0)
370 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
371 if (matches(*argv
, "change") == 0)
372 return tc_filter_modify(RTM_NEWTFILTER
, 0, argc
-1, argv
+1);
373 if (matches(*argv
, "replace") == 0)
374 return tc_filter_modify(RTM_NEWTFILTER
, NLM_F_CREATE
, argc
-1, argv
+1);
375 if (matches(*argv
, "delete") == 0)
376 return tc_filter_modify(RTM_DELTFILTER
, 0, argc
-1, argv
+1);
378 if (matches(*argv
, "get") == 0)
379 return tc_filter_get(RTM_GETTFILTER
, 0, argc
-1, argv
+1);
381 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
382 || matches(*argv
, "lst") == 0)
383 return tc_filter_list(argc
-1, argv
+1);
384 if (matches(*argv
, "help") == 0)
386 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc filter help\".\n", *argv
);