]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_xt.c
tc: m_xt: Fix segfault when adding multiple actions at once
[mirror_iproute2.git] / tc / m_xt.c
1 /*
2 * m_xt.c xtables based targets
3 * utilities mostly ripped from iptables <duh, its the linux way>
4 *
5 * This program is free software; you can distribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: J Hadi Salim (hadi@cyberus.ca)
11 */
12
13 #include <syslog.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <net/if.h>
18 #include <limits.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <xtables.h>
22 #include "utils.h"
23 #include "tc_util.h"
24 #include <linux/tc_act/tc_ipt.h>
25 #include <stdio.h>
26 #include <dlfcn.h>
27 #include <getopt.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <netdb.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/wait.h>
37 #ifndef XT_LIB_DIR
38 # define XT_LIB_DIR "/lib/xtables"
39 #endif
40
41 #ifndef __ALIGN_KERNEL
42 #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
43 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
44 #endif
45
46 #ifndef ALIGN
47 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
48 #endif
49
50 static const char *tname = "mangle";
51
52 char *lib_dir;
53
54 static const char *ipthooks[] = {
55 "NF_IP_PRE_ROUTING",
56 "NF_IP_LOCAL_IN",
57 "NF_IP_FORWARD",
58 "NF_IP_LOCAL_OUT",
59 "NF_IP_POST_ROUTING",
60 };
61
62 static struct option original_opts[] = {
63 {
64 .name = "jump",
65 .has_arg = 1,
66 .val = 'j'
67 },
68 {0, 0, 0, 0}
69 };
70
71 static struct xtables_globals tcipt_globals = {
72 .option_offset = 0,
73 .program_name = "tc-ipt",
74 .program_version = "0.2",
75 .orig_opts = original_opts,
76 .opts = original_opts,
77 .exit_err = NULL,
78 };
79
80 /*
81 * we may need to check for version mismatch
82 */
83 static int
84 build_st(struct xtables_target *target, struct xt_entry_target *t)
85 {
86
87 size_t size =
88 XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
89
90 if (t == NULL) {
91 target->t = xtables_calloc(1, size);
92 target->t->u.target_size = size;
93 strcpy(target->t->u.user.name, target->name);
94 target->t->u.user.revision = target->revision;
95
96 if (target->init != NULL)
97 target->init(target->t);
98 } else {
99 target->t = t;
100 }
101 return 0;
102
103 }
104
105 static void set_lib_dir(void)
106 {
107
108 lib_dir = getenv("XTABLES_LIBDIR");
109 if (!lib_dir) {
110 lib_dir = getenv("IPTABLES_LIB_DIR");
111 if (lib_dir)
112 fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
113 }
114 if (lib_dir == NULL)
115 lib_dir = XT_LIB_DIR;
116
117 }
118
119 static int parse_ipt(struct action_util *a, int *argc_p,
120 char ***argv_p, int tca_id, struct nlmsghdr *n)
121 {
122 struct xtables_target *m = NULL;
123 struct ipt_entry fw;
124 struct rtattr *tail;
125
126 int c;
127 int rargc = *argc_p;
128 char **argv = *argv_p;
129 int argc = 0, iargc = 0;
130 char k[16];
131 int size = 0;
132 int iok = 0, ok = 0;
133 __u32 hook = 0, index = 0;
134 struct option *opts = NULL;
135
136 /* copy tcipt_globals because .opts will be modified by iptables */
137 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
138 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
139 set_lib_dir();
140
141 {
142 int i;
143
144 for (i = 0; i < rargc; i++) {
145 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
146 break;
147 }
148 }
149 iargc = argc = i;
150 }
151
152 if (argc <= 2) {
153 fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc);
154 return -1;
155 }
156
157 while (1) {
158 c = getopt_long(argc, argv, "j:", tmp_tcipt_globals.opts, NULL);
159 if (c == -1)
160 break;
161 switch (c) {
162 case 'j':
163 m = xtables_find_target(optarg, XTF_TRY_LOAD);
164 if (m != NULL) {
165
166 if (build_st(m, NULL) < 0) {
167 printf(" %s error\n", m->name);
168 return -1;
169 }
170 #if (XTABLES_VERSION_CODE >= 6)
171 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
172 tmp_tcipt_globals.opts,
173 m->x6_options,
174 &m->option_offset);
175 #else
176 opts = xtables_merge_options(tmp_tcipt_globals.opts,
177 m->extra_opts,
178 &m->option_offset);
179 #endif
180 if (opts == NULL) {
181 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
182 return -1;
183 } else
184 tmp_tcipt_globals.opts = opts;
185 } else {
186 fprintf(stderr, " failed to find target %s\n\n", optarg);
187 return -1;
188 }
189 ok++;
190 break;
191
192 default:
193 memset(&fw, 0, sizeof(fw));
194 #if (XTABLES_VERSION_CODE >= 6)
195 if (m != NULL && m->x6_parse != NULL) {
196 xtables_option_tpcall(c, argv, 0, m, NULL);
197 #else
198 if (m != NULL && m->parse != NULL) {
199 m->parse(c - m->option_offset, argv, 0, &m->tflags,
200 NULL, &m->t);
201 #endif
202 } else {
203 fprintf(stderr, "failed to find target %s\n\n", optarg);
204 return -1;
205
206 }
207 ok++;
208 break;
209 }
210 }
211
212 if (iargc > optind) {
213 if (matches(argv[optind], "index") == 0) {
214 if (get_u32(&index, argv[optind + 1], 10)) {
215 fprintf(stderr, "Illegal \"index\"\n");
216 xtables_free_opts(1);
217 return -1;
218 }
219 iok++;
220
221 optind += 2;
222 }
223 }
224
225 if (!ok && !iok) {
226 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
227 return -1;
228 }
229
230 /* check that we passed the correct parameters to the target */
231 #if (XTABLES_VERSION_CODE >= 6)
232 if (m)
233 xtables_option_tfcall(m);
234 #else
235 if (m && m->final_check)
236 m->final_check(m->tflags);
237 #endif
238
239 {
240 struct tcmsg *t = NLMSG_DATA(n);
241
242 if (t->tcm_parent != TC_H_ROOT
243 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
244 hook = NF_IP_PRE_ROUTING;
245 } else {
246 hook = NF_IP_POST_ROUTING;
247 }
248 }
249
250 tail = NLMSG_TAIL(n);
251 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
252 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
253 fprintf(stdout, "\ttarget: ");
254
255 if (m) {
256 if (m->print)
257 m->print(NULL, m->t, 0);
258 else
259 printf("%s ", m->name);
260 }
261 fprintf(stdout, " index %d\n", index);
262
263 if (strlen(tname) > 16) {
264 size = 16;
265 k[15] = 0;
266 } else {
267 size = 1 + strlen(tname);
268 }
269 strncpy(k, tname, size);
270
271 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
272 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
273 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
274 if (m)
275 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
276 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
277
278 argc -= optind;
279 argv += optind;
280 *argc_p = rargc - iargc;
281 *argv_p = argv;
282
283 optind = 0;
284 xtables_free_opts(1);
285
286 if (m) {
287 /* Clear flags if target will be used again */
288 m->tflags = 0;
289 m->used = 0;
290 /* Free allocated memory */
291 if (m->t)
292 free(m->t);
293 }
294
295 return 0;
296
297 }
298
299 static int
300 print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
301 {
302 struct rtattr *tb[TCA_IPT_MAX + 1];
303 struct xt_entry_target *t = NULL;
304 struct option *opts = NULL;
305
306 if (arg == NULL)
307 return -1;
308
309 /* copy tcipt_globals because .opts will be modified by iptables */
310 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
311
312 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
313 set_lib_dir();
314
315 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
316
317 if (tb[TCA_IPT_TABLE] == NULL) {
318 fprintf(f, "[NULL ipt table name ] assuming mangle ");
319 } else {
320 fprintf(f, "tablename: %s ",
321 rta_getattr_str(tb[TCA_IPT_TABLE]));
322 }
323
324 if (tb[TCA_IPT_HOOK] == NULL) {
325 fprintf(f, "[NULL ipt hook name ]\n ");
326 return -1;
327 } else {
328 __u32 hook;
329
330 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
331 fprintf(f, " hook: %s\n", ipthooks[hook]);
332 }
333
334 if (tb[TCA_IPT_TARG] == NULL) {
335 fprintf(f, "\t[NULL ipt target parameters ]\n");
336 return -1;
337 } else {
338 struct xtables_target *m = NULL;
339
340 t = RTA_DATA(tb[TCA_IPT_TARG]);
341 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
342 if (m != NULL) {
343 if (build_st(m, t) < 0) {
344 fprintf(stderr, " %s error\n", m->name);
345 return -1;
346 }
347
348 #if (XTABLES_VERSION_CODE >= 6)
349 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
350 tmp_tcipt_globals.opts,
351 m->x6_options,
352 &m->option_offset);
353 #else
354 opts = xtables_merge_options(tmp_tcipt_globals.opts,
355 m->extra_opts,
356 &m->option_offset);
357 #endif
358 if (opts == NULL) {
359 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
360 return -1;
361 } else
362 tmp_tcipt_globals.opts = opts;
363 } else {
364 fprintf(stderr, " failed to find target %s\n\n",
365 t->u.user.name);
366 return -1;
367 }
368 fprintf(f, "\ttarget ");
369 m->print(NULL, m->t, 0);
370 if (tb[TCA_IPT_INDEX] == NULL) {
371 fprintf(f, " [NULL ipt target index ]\n");
372 } else {
373 __u32 index;
374
375 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
376 fprintf(f, "\n\tindex %d", index);
377 }
378
379 if (tb[TCA_IPT_CNT]) {
380 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
381
382 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
383 }
384 if (show_stats) {
385 if (tb[TCA_IPT_TM]) {
386 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
387
388 print_tm(f, tm);
389 }
390 }
391 fprintf(f, "\n");
392
393 }
394 xtables_free_opts(1);
395
396 return 0;
397 }
398
399 struct action_util xt_action_util = {
400 .id = "xt",
401 .parse_aopt = parse_ipt,
402 .print_aopt = print_ipt,
403 };