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