]>
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; |
aba5acdf | 38 | |
b7a45150 | 39 | static void *BODY; /* cached handle dlopen(NULL) */ |
aba5acdf SH |
40 | static struct qdisc_util * qdisc_list; |
41 | static struct filter_util * filter_list; | |
42 | ||
1798c9d5 SH |
43 | static int print_noqopt(struct qdisc_util *qu, FILE *f, |
44 | struct rtattr *opt) | |
aba5acdf SH |
45 | { |
46 | if (opt && RTA_PAYLOAD(opt)) | |
47 | fprintf(f, "[Unknown qdisc, optlen=%u] ", RTA_PAYLOAD(opt)); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) | |
52 | { | |
53 | if (argc) { | |
54 | fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
55 | return -1; | |
56 | } | |
57 | return 0; | |
58 | } | |
59 | ||
60 | static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle) | |
61 | { | |
62 | if (opt && RTA_PAYLOAD(opt)) | |
63 | fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", fhandle, RTA_PAYLOAD(opt)); | |
64 | else if (fhandle) | |
65 | fprintf(f, "fh %08x ", fhandle); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n) | |
70 | { | |
71 | __u32 handle; | |
72 | ||
73 | if (argc) { | |
74 | fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); | |
75 | return -1; | |
76 | } | |
77 | if (fhandle) { | |
78 | struct tcmsg *t = NLMSG_DATA(n); | |
79 | if (get_u32(&handle, fhandle, 16)) { | |
80 | fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle); | |
81 | return -1; | |
82 | } | |
83 | t->tcm_handle = handle; | |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
4094db72 | 88 | struct qdisc_util *get_qdisc_kind(const char *str) |
aba5acdf SH |
89 | { |
90 | void *dlh; | |
91 | char buf[256]; | |
92 | struct qdisc_util *q; | |
93 | ||
94 | for (q = qdisc_list; q; q = q->next) | |
95 | if (strcmp(q->id, str) == 0) | |
96 | return q; | |
97 | ||
b7a45150 | 98 | snprintf(buf, sizeof(buf), "/usr/lib/tc/q_%s.so", str); |
aba5acdf | 99 | dlh = dlopen(buf, RTLD_LAZY); |
b7a45150 SH |
100 | if (!dlh) { |
101 | /* look in current binary, only open once */ | |
aba5acdf SH |
102 | dlh = BODY; |
103 | if (dlh == NULL) { | |
104 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
105 | if (dlh == NULL) | |
106 | goto noexist; | |
107 | } | |
108 | } | |
109 | ||
110 | snprintf(buf, sizeof(buf), "%s_util", str); | |
111 | q = dlsym(dlh, buf); | |
112 | if (q == NULL) | |
113 | goto noexist; | |
114 | ||
115 | reg: | |
116 | q->next = qdisc_list; | |
117 | qdisc_list = q; | |
118 | return q; | |
119 | ||
120 | noexist: | |
121 | q = malloc(sizeof(*q)); | |
122 | if (q) { | |
1798c9d5 | 123 | |
aba5acdf | 124 | memset(q, 0, sizeof(*q)); |
1798c9d5 | 125 | q->id = strcpy(malloc(strlen(str)+1), str); |
aba5acdf SH |
126 | q->parse_qopt = parse_noqopt; |
127 | q->print_qopt = print_noqopt; | |
128 | goto reg; | |
129 | } | |
130 | return q; | |
131 | } | |
132 | ||
133 | ||
4094db72 | 134 | struct filter_util *get_filter_kind(const char *str) |
aba5acdf SH |
135 | { |
136 | void *dlh; | |
137 | char buf[256]; | |
138 | struct filter_util *q; | |
139 | ||
140 | for (q = filter_list; q; q = q->next) | |
141 | if (strcmp(q->id, str) == 0) | |
142 | return q; | |
143 | ||
b7a45150 | 144 | snprintf(buf, sizeof(buf), "/usr/lib/tc/f_%s.so", str); |
aba5acdf SH |
145 | dlh = dlopen(buf, RTLD_LAZY); |
146 | if (dlh == NULL) { | |
147 | dlh = BODY; | |
148 | if (dlh == NULL) { | |
149 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
150 | if (dlh == NULL) | |
151 | goto noexist; | |
152 | } | |
153 | } | |
154 | ||
155 | snprintf(buf, sizeof(buf), "%s_util", str); | |
156 | q = dlsym(dlh, buf); | |
157 | if (q == NULL) | |
158 | goto noexist; | |
159 | ||
160 | reg: | |
161 | q->next = filter_list; | |
162 | filter_list = q; | |
163 | return q; | |
aba5acdf SH |
164 | noexist: |
165 | q = malloc(sizeof(*q)); | |
166 | if (q) { | |
167 | memset(q, 0, sizeof(*q)); | |
168 | strncpy(q->id, str, 15); | |
169 | q->parse_fopt = parse_nofopt; | |
170 | q->print_fopt = print_nofopt; | |
171 | goto reg; | |
172 | } | |
173 | return q; | |
174 | } | |
175 | ||
176 | static void usage(void) __attribute__((noreturn)); | |
177 | ||
178 | static void usage(void) | |
179 | { | |
180 | fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
2373fde9 | 181 | "where OBJECT := { qdisc | class | filter | action }\n" |
aba5acdf SH |
182 | " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n"); |
183 | exit(-1); | |
184 | } | |
185 | ||
186 | ||
187 | ||
188 | int main(int argc, char **argv) | |
189 | { | |
190 | char *basename; | |
191 | ||
192 | basename = strrchr(argv[0], '/'); | |
193 | if (basename == NULL) | |
194 | basename = argv[0]; | |
195 | else | |
196 | basename++; | |
197 | ||
198 | ||
199 | /* batch mode */ | |
200 | if (argc > 1 && matches(argv[1], "-batch") == 0) { | |
201 | FILE *batch; | |
202 | char line[400]; | |
203 | char *largv[100]; | |
204 | int largc, ret=0; | |
205 | #define BMAXARG (sizeof(largv)/sizeof(char *)-2) | |
206 | ||
207 | if (argc != 3) { | |
208 | fprintf(stderr, "Wrong number of arguments in batch mode\n"); | |
209 | exit(-1); | |
210 | } | |
211 | if (matches(argv[2], "-") != 0) { | |
212 | if ((batch = fopen(argv[2], "r")) == NULL) { | |
213 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", argv[2], strerror(errno)); | |
214 | exit(-1); | |
215 | } | |
216 | } else { | |
217 | if ((batch = fdopen(0, "r")) == NULL) { | |
218 | fprintf(stderr, "Cannot open stdin for reading: %s=n", strerror(errno)); | |
219 | exit(-1); | |
220 | } | |
221 | } | |
222 | ||
223 | tc_core_init(); | |
224 | ||
225 | while (fgets(line, sizeof(line)-1, batch)) { | |
226 | if (line[strlen(line)-1]=='\n') { | |
227 | line[strlen(line)-1] = '\0'; | |
228 | } else { | |
229 | fprintf(stderr, "No newline at the end of line, looks like to long (%d chars or more)\n", strlen(line)); | |
230 | exit(-1); | |
231 | } | |
232 | largc = 0; | |
233 | largv[largc]=strtok(line, " "); | |
234 | while ((largv[++largc]=strtok(NULL, " ")) != NULL) { | |
235 | if (largc > BMAXARG) { | |
236 | fprintf(stderr, "Over %d arguments in batch mode, enough!\n", BMAXARG); | |
237 | exit(-1); | |
238 | } | |
239 | } | |
240 | ||
241 | if (matches(largv[0], "qdisc") == 0) { | |
242 | ret += do_qdisc(largc-1, largv+1); | |
243 | } else if (matches(largv[0], "class") == 0) { | |
244 | ret += do_class(largc-1, largv+1); | |
245 | } else if (matches(largv[0], "filter") == 0) { | |
246 | ret += do_filter(largc-1, largv+1); | |
2373fde9 SH |
247 | } else if (matches(largv[0], "action") == 0) { |
248 | ret += do_action(largc-1, largv+1); | |
aba5acdf SH |
249 | } else if (matches(largv[0], "help") == 0) { |
250 | usage(); /* note that usage() doesn't return */ | |
251 | } else { | |
252 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", largv[1]); | |
253 | exit(-1); | |
254 | } | |
255 | } | |
256 | fclose(batch); | |
257 | exit(0); /* end of batch, that's all */ | |
258 | } | |
259 | ||
260 | while (argc > 1) { | |
261 | if (argv[1][0] != '-') | |
262 | break; | |
263 | if (matches(argv[1], "-stats") == 0 || | |
3ea2bf45 | 264 | matches(argv[1], "-statistics") == 0) { |
aba5acdf SH |
265 | ++show_stats; |
266 | } else if (matches(argv[1], "-details") == 0) { | |
267 | ++show_details; | |
268 | } else if (matches(argv[1], "-raw") == 0) { | |
269 | ++show_raw; | |
270 | } else if (matches(argv[1], "-Version") == 0) { | |
271 | printf("tc utility, iproute2-ss%s\n", SNAPSHOT); | |
272 | exit(0); | |
3ea2bf45 SH |
273 | } else if (matches(argv[1], "-iec") == 0) { |
274 | ++use_iec; | |
aba5acdf SH |
275 | } else if (matches(argv[1], "-help") == 0) { |
276 | usage(); | |
277 | } else { | |
278 | fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]); | |
279 | exit(-1); | |
280 | } | |
281 | argc--; argv++; | |
282 | } | |
283 | ||
284 | tc_core_init(); | |
285 | ||
286 | if (argc > 1) { | |
287 | if (matches(argv[1], "qdisc") == 0) | |
288 | return do_qdisc(argc-2, argv+2); | |
289 | if (matches(argv[1], "class") == 0) | |
290 | return do_class(argc-2, argv+2); | |
291 | if (matches(argv[1], "filter") == 0) | |
292 | return do_filter(argc-2, argv+2); | |
2373fde9 SH |
293 | if (matches(argv[1], "actions") == 0) |
294 | return do_action(argc-2, argv+2); | |
aba5acdf SH |
295 | if (matches(argv[1], "help") == 0) |
296 | usage(); | |
297 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", argv[1]); | |
298 | exit(-1); | |
299 | } | |
300 | ||
301 | usage(); | |
302 | } |