]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
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 | ||
33 | int show_stats = 0; | |
34 | int show_details = 0; | |
35 | int show_raw = 0; | |
36 | int resolve_hosts = 0; | |
3ea2bf45 | 37 | int use_iec = 0; |
7901660a | 38 | struct rtnl_handle rth; |
aba5acdf | 39 | |
b7a45150 | 40 | static void *BODY; /* cached handle dlopen(NULL) */ |
aba5acdf SH |
41 | static struct qdisc_util * qdisc_list; |
42 | static struct filter_util * filter_list; | |
43 | ||
1798c9d5 SH |
44 | static int print_noqopt(struct qdisc_util *qu, FILE *f, |
45 | struct rtattr *opt) | |
aba5acdf SH |
46 | { |
47 | if (opt && RTA_PAYLOAD(opt)) | |
bb6a21a4 SH |
48 | fprintf(f, "[Unknown qdisc, optlen=%u] ", |
49 | (unsigned) RTA_PAYLOAD(opt)); | |
aba5acdf SH |
50 | return 0; |
51 | } | |
52 | ||
53 | static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) | |
54 | { | |
55 | if (argc) { | |
56 | fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
57 | return -1; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
62 | static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle) | |
63 | { | |
64 | if (opt && RTA_PAYLOAD(opt)) | |
bb6a21a4 SH |
65 | fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", |
66 | fhandle, (unsigned) RTA_PAYLOAD(opt)); | |
aba5acdf SH |
67 | else if (fhandle) |
68 | fprintf(f, "fh %08x ", fhandle); | |
69 | return 0; | |
70 | } | |
71 | ||
72 | static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n) | |
73 | { | |
74 | __u32 handle; | |
75 | ||
76 | if (argc) { | |
77 | fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
78 | return -1; | |
79 | } | |
80 | if (fhandle) { | |
81 | struct tcmsg *t = NLMSG_DATA(n); | |
82 | if (get_u32(&handle, fhandle, 16)) { | |
83 | fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle); | |
84 | return -1; | |
85 | } | |
86 | t->tcm_handle = handle; | |
87 | } | |
88 | return 0; | |
89 | } | |
90 | ||
4094db72 | 91 | struct qdisc_util *get_qdisc_kind(const char *str) |
aba5acdf SH |
92 | { |
93 | void *dlh; | |
94 | char buf[256]; | |
95 | struct qdisc_util *q; | |
96 | ||
97 | for (q = qdisc_list; q; q = q->next) | |
98 | if (strcmp(q->id, str) == 0) | |
99 | return q; | |
100 | ||
b7a45150 | 101 | snprintf(buf, sizeof(buf), "/usr/lib/tc/q_%s.so", str); |
aba5acdf | 102 | dlh = dlopen(buf, RTLD_LAZY); |
b7a45150 SH |
103 | if (!dlh) { |
104 | /* look in current binary, only open once */ | |
aba5acdf SH |
105 | dlh = BODY; |
106 | if (dlh == NULL) { | |
107 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
108 | if (dlh == NULL) | |
109 | goto noexist; | |
110 | } | |
111 | } | |
112 | ||
95812b56 | 113 | snprintf(buf, sizeof(buf), "%s_qdisc_util", str); |
aba5acdf SH |
114 | q = dlsym(dlh, buf); |
115 | if (q == NULL) | |
116 | goto noexist; | |
117 | ||
118 | reg: | |
119 | q->next = qdisc_list; | |
120 | qdisc_list = q; | |
121 | return q; | |
122 | ||
123 | noexist: | |
124 | q = malloc(sizeof(*q)); | |
125 | if (q) { | |
1798c9d5 | 126 | |
aba5acdf | 127 | memset(q, 0, sizeof(*q)); |
1798c9d5 | 128 | q->id = strcpy(malloc(strlen(str)+1), str); |
aba5acdf SH |
129 | q->parse_qopt = parse_noqopt; |
130 | q->print_qopt = print_noqopt; | |
131 | goto reg; | |
132 | } | |
133 | return q; | |
134 | } | |
135 | ||
136 | ||
4094db72 | 137 | struct filter_util *get_filter_kind(const char *str) |
aba5acdf SH |
138 | { |
139 | void *dlh; | |
140 | char buf[256]; | |
141 | struct filter_util *q; | |
142 | ||
143 | for (q = filter_list; q; q = q->next) | |
144 | if (strcmp(q->id, str) == 0) | |
145 | return q; | |
146 | ||
b7a45150 | 147 | snprintf(buf, sizeof(buf), "/usr/lib/tc/f_%s.so", str); |
aba5acdf SH |
148 | dlh = dlopen(buf, RTLD_LAZY); |
149 | if (dlh == NULL) { | |
150 | dlh = BODY; | |
151 | if (dlh == NULL) { | |
152 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
153 | if (dlh == NULL) | |
154 | goto noexist; | |
155 | } | |
156 | } | |
157 | ||
95812b56 | 158 | snprintf(buf, sizeof(buf), "%s_filter_util", str); |
aba5acdf SH |
159 | q = dlsym(dlh, buf); |
160 | if (q == NULL) | |
161 | goto noexist; | |
162 | ||
163 | reg: | |
164 | q->next = filter_list; | |
165 | filter_list = q; | |
166 | return q; | |
aba5acdf SH |
167 | noexist: |
168 | q = malloc(sizeof(*q)); | |
169 | if (q) { | |
170 | memset(q, 0, sizeof(*q)); | |
171 | strncpy(q->id, str, 15); | |
172 | q->parse_fopt = parse_nofopt; | |
173 | q->print_fopt = print_nofopt; | |
174 | goto reg; | |
175 | } | |
176 | return q; | |
177 | } | |
178 | ||
c2f3a0f9 | 179 | static void usage(void) |
aba5acdf SH |
180 | { |
181 | fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
2373fde9 | 182 | "where OBJECT := { qdisc | class | filter | action }\n" |
aba5acdf | 183 | " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n"); |
c2f3a0f9 | 184 | } |
185 | ||
186 | static int do_cmd(int argc, char **argv) | |
187 | { | |
188 | if (matches(*argv, "qdisc") == 0) | |
189 | return do_qdisc(argc-1, argv+1); | |
190 | ||
191 | if (matches(*argv, "class") == 0) | |
192 | return do_class(argc-1, argv+1); | |
193 | ||
194 | if (matches(*argv, "filter") == 0) | |
195 | return do_filter(argc-1, argv+1); | |
196 | ||
197 | if (matches(*argv, "actions") == 0) | |
198 | return do_action(argc-1, argv+1); | |
199 | ||
200 | if (matches(*argv, "help") == 0) { | |
201 | usage(); | |
202 | return 0; | |
203 | } | |
204 | ||
205 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", | |
206 | *argv); | |
044ebf35 | 207 | return -1; |
aba5acdf SH |
208 | } |
209 | ||
c2f3a0f9 | 210 | static int makeargs(char *line, char *argv[], int maxargs) |
211 | { | |
212 | static const char ws[] = " \t\r\n"; | |
213 | char *cp; | |
214 | int argc = 0; | |
aba5acdf | 215 | |
c2f3a0f9 | 216 | for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) { |
217 | if (argc >= maxargs) { | |
218 | fprintf(stderr, "Too many arguments to command\n"); | |
219 | exit(1); | |
220 | } | |
221 | argv[argc++] = cp; | |
222 | } | |
223 | argv[argc] = NULL; | |
aba5acdf | 224 | |
c2f3a0f9 | 225 | return argc; |
226 | } | |
227 | ||
228 | static int batch(const char *name) | |
aba5acdf | 229 | { |
c2f3a0f9 | 230 | char *line = NULL; |
231 | size_t len = 0; | |
232 | ssize_t cc; | |
233 | int lineno = 0; | |
234 | char *largv[100]; | |
235 | int largc, ret = 0; | |
236 | ||
237 | if (strcmp(name, "-") != 0) { | |
238 | if (freopen(name, "r", stdin) == NULL) { | |
239 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", | |
240 | name, strerror(errno)); | |
044ebf35 | 241 | return -1; |
aba5acdf | 242 | } |
c2f3a0f9 | 243 | } |
244 | ||
245 | tc_core_init(); | |
246 | ||
247 | if (rtnl_open(&rth, 0) < 0) { | |
248 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
249 | return -1; | |
250 | } | |
251 | ||
252 | while ((cc = getline(&line, &len, stdin)) != -1) { | |
253 | ++lineno; | |
254 | ||
255 | /* ignore blank lines and comments */ | |
256 | if (*line == '\n' || *line == '#') | |
257 | continue; | |
258 | ||
259 | /* handle continuation lines */ | |
260 | while (cc >= 2 && strcmp(line+cc-2, "\\\n") == 0) { | |
261 | char *line1 = NULL; | |
262 | ssize_t len1 = 0; | |
263 | int cc1; | |
264 | cc1 = getline(&line1, &len1, stdin); | |
265 | ||
266 | if (cc1 < 0) { | |
267 | fprintf(stderr, "Missing continuation line\n"); | |
044ebf35 | 268 | return -1; |
aba5acdf | 269 | } |
c2f3a0f9 | 270 | ++lineno; |
271 | line = realloc(line, cc + cc1); | |
272 | if (!line) { | |
273 | fprintf(stderr, "Out of memory\n"); | |
044ebf35 | 274 | return -1; |
aba5acdf | 275 | } |
aba5acdf | 276 | |
c2f3a0f9 | 277 | strcpy(line+cc-2, line1); |
278 | cc += cc1 - 2; | |
279 | free(line1); | |
7901660a | 280 | } |
aba5acdf | 281 | |
c2f3a0f9 | 282 | largc = makeargs(line, largv, 100); |
aba5acdf | 283 | |
c2f3a0f9 | 284 | ret = do_cmd(largc, largv); |
285 | if (ret) { | |
286 | fprintf(stderr, "Command failed %s:%d\n", name, lineno); | |
287 | break; | |
aba5acdf | 288 | } |
c2f3a0f9 | 289 | } |
7901660a | 290 | |
c2f3a0f9 | 291 | rtnl_close(&rth); |
292 | return ret; | |
293 | } | |
7901660a | 294 | |
c2f3a0f9 | 295 | |
296 | int main(int argc, char **argv) | |
297 | { | |
298 | int ret; | |
aba5acdf SH |
299 | |
300 | while (argc > 1) { | |
301 | if (argv[1][0] != '-') | |
302 | break; | |
303 | if (matches(argv[1], "-stats") == 0 || | |
3ea2bf45 | 304 | matches(argv[1], "-statistics") == 0) { |
aba5acdf SH |
305 | ++show_stats; |
306 | } else if (matches(argv[1], "-details") == 0) { | |
307 | ++show_details; | |
308 | } else if (matches(argv[1], "-raw") == 0) { | |
309 | ++show_raw; | |
310 | } else if (matches(argv[1], "-Version") == 0) { | |
311 | printf("tc utility, iproute2-ss%s\n", SNAPSHOT); | |
044ebf35 | 312 | return 0; |
3ea2bf45 SH |
313 | } else if (matches(argv[1], "-iec") == 0) { |
314 | ++use_iec; | |
aba5acdf SH |
315 | } else if (matches(argv[1], "-help") == 0) { |
316 | usage(); | |
c2f3a0f9 | 317 | return 0; |
318 | } else if (matches(argv[1], "-batch") == 0) { | |
319 | if (argc < 3) { | |
320 | fprintf(stderr, "Wrong number of arguments in batch mode\n"); | |
321 | return -1; | |
322 | } | |
323 | ||
324 | return batch(argv[2]); | |
aba5acdf SH |
325 | } else { |
326 | fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]); | |
044ebf35 | 327 | return -1; |
aba5acdf SH |
328 | } |
329 | argc--; argv++; | |
330 | } | |
331 | ||
c2f3a0f9 | 332 | if (argc <= 1) { |
333 | usage(); | |
334 | return 0; | |
335 | } | |
336 | ||
aba5acdf | 337 | tc_core_init(); |
7901660a SH |
338 | if (rtnl_open(&rth, 0) < 0) { |
339 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
340 | exit(1); | |
341 | } | |
aba5acdf | 342 | |
c2f3a0f9 | 343 | ret = do_cmd(argc-1, argv+1); |
7901660a | 344 | rtnl_close(&rth); |
c2f3a0f9 | 345 | |
346 | return ret; | |
aba5acdf | 347 | } |