1 /*
2 * tc.c "tc" utility frontend.
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 * Fixes:
12 *
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <fcntl.h>
21 #include <dlfcn.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "SNAPSHOT.h"
29 #include "utils.h"
30 #include "tc_util.h"
31 #include "tc_common.h"
32 #include "namespace.h"
33
34 int show_stats;
35 int show_details;
36 int show_raw;
37 int show_pretty;
38 int show_graph;
39 int timestamp;
40
41 int batch_mode;
42 int use_iec;
43 int force;
44 bool use_names;
45
46 static char *conf_file;
47
48 struct rtnl_handle rth;
49
50 static void *BODY; /* cached handle dlopen(NULL) */
51 static struct qdisc_util *qdisc_list;
52 static struct filter_util *filter_list;
53
print_noqopt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)54 static int print_noqopt(struct qdisc_util *qu, FILE *f,
55 struct rtattr *opt)
56 {
57 if (opt && RTA_PAYLOAD(opt))
58 fprintf(f, "[Unknown qdisc, optlen=%u] ",
59 (unsigned int) RTA_PAYLOAD(opt));
60 return 0;
61 }
62
parse_noqopt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)63 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
64 {
65 if (argc) {
66 fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
67 return -1;
68 }
69 return 0;
70 }
71
print_nofopt(struct filter_util * qu,FILE * f,struct rtattr * opt,__u32 fhandle)72 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
73 {
74 if (opt && RTA_PAYLOAD(opt))
75 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
76 fhandle, (unsigned int) RTA_PAYLOAD(opt));
77 else if (fhandle)
78 fprintf(f, "fh %08x ", fhandle);
79 return 0;
80 }
81
parse_nofopt(struct filter_util * qu,char * fhandle,int argc,char ** argv,struct nlmsghdr * n)82 static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
83 {
84 __u32 handle;
85
86 if (argc) {
87 fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
88 return -1;
89 }
90 if (fhandle) {
91 struct tcmsg *t = NLMSG_DATA(n);
92
93 if (get_u32(&handle, fhandle, 16)) {
94 fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
95 return -1;
96 }
97 t->tcm_handle = handle;
98 }
99 return 0;
100 }
101
get_qdisc_kind(const char * str)102 struct qdisc_util *get_qdisc_kind(const char *str)
103 {
104 void *dlh;
105 char buf[256];
106 struct qdisc_util *q;
107
108 for (q = qdisc_list; q; q = q->next)
109 if (strcmp(q->id, str) == 0)
110 return q;
111
112 snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
113 dlh = dlopen(buf, RTLD_LAZY);
114 if (!dlh) {
115 /* look in current binary, only open once */
116 dlh = BODY;
117 if (dlh == NULL) {
118 dlh = BODY = dlopen(NULL, RTLD_LAZY);
119 if (dlh == NULL)
120 goto noexist;
121 }
122 }
123
124 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
125 q = dlsym(dlh, buf);
126 if (q == NULL)
127 goto noexist;
128
129 reg:
130 q->next = qdisc_list;
131 qdisc_list = q;
132 return q;
133
134 noexist:
135 q = calloc(1, sizeof(*q));
136 if (q) {
137 q->id = strdup(str);
138 q->parse_qopt = parse_noqopt;
139 q->print_qopt = print_noqopt;
140 goto reg;
141 }
142 return q;
143 }
144
145
get_filter_kind(const char * str)146 struct filter_util *get_filter_kind(const char *str)
147 {
148 void *dlh;
149 char buf[256];
150 struct filter_util *q;
151
152 for (q = filter_list; q; q = q->next)
153 if (strcmp(q->id, str) == 0)
154 return q;
155
156 snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
157 dlh = dlopen(buf, RTLD_LAZY);
158 if (dlh == NULL) {
159 dlh = BODY;
160 if (dlh == NULL) {
161 dlh = BODY = dlopen(NULL, RTLD_LAZY);
162 if (dlh == NULL)
163 goto noexist;
164 }
165 }
166
167 snprintf(buf, sizeof(buf), "%s_filter_util", str);
168 q = dlsym(dlh, buf);
169 if (q == NULL)
170 goto noexist;
171
172 reg:
173 q->next = filter_list;
174 filter_list = q;
175 return q;
176 noexist:
177 q = calloc(1, sizeof(*q));
178 if (q) {
179 strncpy(q->id, str, 15);
180 q->parse_fopt = parse_nofopt;
181 q->print_fopt = print_nofopt;
182 goto reg;
183 }
184 return q;
185 }
186
usage(void)187 static void usage(void)
188 {
189 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
190 " tc [-force] -batch filename\n"
191 "where OBJECT := { qdisc | class | filter | action | monitor | exec }\n"
192 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n"
193 " -nm | -nam[es] | { -cf | -conf } path }\n");
194 }
195
do_cmd(int argc,char ** argv)196 static int do_cmd(int argc, char **argv)
197 {
198 if (matches(*argv, "qdisc") == 0)
199 return do_qdisc(argc-1, argv+1);
200 if (matches(*argv, "class") == 0)
201 return do_class(argc-1, argv+1);
202 if (matches(*argv, "filter") == 0)
203 return do_filter(argc-1, argv+1);
204 if (matches(*argv, "actions") == 0)
205 return do_action(argc-1, argv+1);
206 if (matches(*argv, "monitor") == 0)
207 return do_tcmonitor(argc-1, argv+1);
208 if (matches(*argv, "exec") == 0)
209 return do_exec(argc-1, argv+1);
210 if (matches(*argv, "help") == 0) {
211 usage();
212 return 0;
213 }
214
215 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
216 *argv);
217 return -1;
218 }
219
batch(const char * name)220 static int batch(const char *name)
221 {
222 char *line = NULL;
223 size_t len = 0;
224 int ret = 0;
225
226 batch_mode = 1;
227 if (name && strcmp(name, "-") != 0) {
228 if (freopen(name, "r", stdin) == NULL) {
229 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
230 name, strerror(errno));
231 return -1;
232 }
233 }
234
235 tc_core_init();
236
237 if (rtnl_open(&rth, 0) < 0) {
238 fprintf(stderr, "Cannot open rtnetlink\n");
239 return -1;
240 }
241
242 cmdlineno = 0;
243 while (getcmdline(&line, &len, stdin) != -1) {
244 char *largv[100];
245 int largc;
246
247 largc = makeargs(line, largv, 100);
248 if (largc == 0)
249 continue; /* blank line */
250
251 if (do_cmd(largc, largv)) {
252 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
253 ret = 1;
254 if (!force)
255 break;
256 }
257 }
258 if (line)
259 free(line);
260
261 rtnl_close(&rth);
262 return ret;
263 }
264
265
main(int argc,char ** argv)266 int main(int argc, char **argv)
267 {
268 int ret;
269 char *batch_file = NULL;
270
271 while (argc > 1) {
272 if (argv[1][0] != '-')
273 break;
274 if (matches(argv[1], "-stats") == 0 ||
275 matches(argv[1], "-statistics") == 0) {
276 ++show_stats;
277 } else if (matches(argv[1], "-details") == 0) {
278 ++show_details;
279 } else if (matches(argv[1], "-raw") == 0) {
280 ++show_raw;
281 } else if (matches(argv[1], "-pretty") == 0) {
282 ++show_pretty;
283 } else if (matches(argv[1], "-graph") == 0) {
284 show_graph = 1;
285 } else if (matches(argv[1], "-Version") == 0) {
286 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
287 return 0;
288 } else if (matches(argv[1], "-iec") == 0) {
289 ++use_iec;
290 } else if (matches(argv[1], "-help") == 0) {
291 usage();
292 return 0;
293 } else if (matches(argv[1], "-force") == 0) {
294 ++force;
295 } else if (matches(argv[1], "-batch") == 0) {
296 argc--; argv++;
297 if (argc <= 1)
298 usage();
299 batch_file = argv[1];
300 } else if (matches(argv[1], "-netns") == 0) {
301 NEXT_ARG();
302 if (netns_switch(argv[1]))
303 return -1;
304 } else if (matches(argv[1], "-names") == 0 ||
305 matches(argv[1], "-nm") == 0) {
306 use_names = true;
307 } else if (matches(argv[1], "-cf") == 0 ||
308 matches(argv[1], "-conf") == 0) {
309 NEXT_ARG();
310 conf_file = argv[1];
311 } else if (matches(argv[1], "-timestamp") == 0) {
312 timestamp++;
313 } else if (matches(argv[1], "-tshort") == 0) {
314 ++timestamp;
315 ++timestamp_short;
316 } else {
317 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
318 return -1;
319 }
320 argc--; argv++;
321 }
322
323 if (batch_file)
324 return batch(batch_file);
325
326 if (argc <= 1) {
327 usage();
328 return 0;
329 }
330
331 tc_core_init();
332 if (rtnl_open(&rth, 0) < 0) {
333 fprintf(stderr, "Cannot open rtnetlink\n");
334 exit(1);
335 }
336
337 if (use_names && cls_names_init(conf_file)) {
338 ret = -1;
339 goto Exit;
340 }
341
342 ret = do_cmd(argc-1, argv+1);
343 Exit:
344 rtnl_close(&rth);
345
346 if (use_names)
347 cls_names_uninit();
348
349 return ret;
350 }
351