]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_xt.c
tc: m_xt: Prevent segfault with standard targets
[mirror_iproute2.git] / tc / m_xt.c
CommitLineData
a36ceb85
AH
1/*
2 * m_xt.c xtables based targets
3d0b7439 3 * utilities mostly ripped from iptables <duh, its the linux way>
a36ceb85
AH
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>
a36ceb85
AH
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
cfa292de
AD
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
609ceb80 46#ifndef ALIGN
32a121cb 47#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
609ceb80
SH
48#endif
49
a36ceb85
AH
50static const char *tname = "mangle";
51
52char *lib_dir;
53
54static 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
62static struct option original_opts[] = {
63 {
64 .name = "jump",
65 .has_arg = 1,
66 .val = 'j'
67 },
68 {0, 0, 0, 0}
69};
70
71static 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*/
d1f28cf1 83static int
a36ceb85
AH
84build_st(struct xtables_target *target, struct xt_entry_target *t)
85{
86
87 size_t size =
32a121cb 88 XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
a36ceb85 89
32a121cb 90 if (t == NULL) {
a36ceb85
AH
91 target->t = xtables_calloc(1, size);
92 target->t->u.target_size = size;
93 strcpy(target->t->u.user.name, target->name);
be3c4d4f 94 target->t->u.user.revision = target->revision;
a36ceb85
AH
95
96 if (target->init != NULL)
97 target->init(target->t);
98 } else {
99 target->t = t;
100 }
101 return 0;
102
103}
104
d1f28cf1 105static void set_lib_dir(void)
a36ceb85
AH
106{
107
108 lib_dir = getenv("XTABLES_LIBDIR");
109 if (!lib_dir) {
110 lib_dir = getenv("IPTABLES_LIB_DIR");
111 if (lib_dir)
32a121cb 112 fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
a36ceb85
AH
113 }
114 if (lib_dir == NULL)
115 lib_dir = XT_LIB_DIR;
116
117}
118
32a121cb 119static int parse_ipt(struct action_util *a, int *argc_p,
a36ceb85
AH
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;
852d5122 125
a36ceb85
AH
126 int c;
127 int rargc = *argc_p;
128 char **argv = *argv_p;
129 int argc = 0, iargc = 0;
130 char k[16];
a36ceb85
AH
131 int size = 0;
132 int iok = 0, ok = 0;
133 __u32 hook = 0, index = 0;
852d5122 134 struct option *opts = NULL;
a36ceb85
AH
135
136 xtables_init_all(&tcipt_globals, NFPROTO_IPV4);
137 set_lib_dir();
138
139 {
140 int i;
32a121cb 141
a36ceb85
AH
142 for (i = 0; i < rargc; i++) {
143 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
144 break;
145 }
146 }
147 iargc = argc = i;
148 }
149
150 if (argc <= 2) {
32a121cb 151 fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc);
a36ceb85
AH
152 return -1;
153 }
154
155 while (1) {
156 c = getopt_long(argc, argv, "j:", tcipt_globals.opts, NULL);
157 if (c == -1)
158 break;
159 switch (c) {
160 case 'j':
161 m = xtables_find_target(optarg, XTF_TRY_LOAD);
32a121cb 162 if (m != NULL) {
a36ceb85 163
32a121cb
SH
164 if (build_st(m, NULL) < 0) {
165 printf(" %s error\n", m->name);
a36ceb85
AH
166 return -1;
167 }
73de5d96 168#if (XTABLES_VERSION_CODE >= 6)
852d5122
JHS
169 opts = xtables_options_xfrm(tcipt_globals.orig_opts,
170 tcipt_globals.opts,
171 m->x6_options,
172 &m->option_offset);
173#else
cfa292de 174 opts = xtables_merge_options(tcipt_globals.opts,
852d5122
JHS
175 m->extra_opts,
176 &m->option_offset);
73de5d96 177#endif
852d5122 178 if (opts == NULL) {
b2e116d6 179 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
852d5122
JHS
180 return -1;
181 } else
182 tcipt_globals.opts = opts;
a36ceb85 183 } else {
32a121cb 184 fprintf(stderr, " failed to find target %s\n\n", optarg);
a36ceb85
AH
185 return -1;
186 }
187 ok++;
188 break;
189
190 default:
32a121cb 191 memset(&fw, 0, sizeof(fw));
852d5122 192#if (XTABLES_VERSION_CODE >= 6)
32a121cb
SH
193 if (m != NULL && m->x6_parse != NULL) {
194 xtables_option_tpcall(c, argv, 0, m, NULL);
852d5122 195#else
32a121cb 196 if (m != NULL && m->parse != NULL) {
852d5122
JHS
197 m->parse(c - m->option_offset, argv, 0, &m->tflags,
198 NULL, &m->t);
199#endif
a36ceb85 200 } else {
32a121cb 201 fprintf(stderr, "failed to find target %s\n\n", optarg);
a36ceb85
AH
202 return -1;
203
204 }
205 ok++;
206 break;
a36ceb85
AH
207 }
208 }
209
210 if (iargc > optind) {
211 if (matches(argv[optind], "index") == 0) {
212 if (get_u32(&index, argv[optind + 1], 10)) {
213 fprintf(stderr, "Illegal \"index\"\n");
214 xtables_free_opts(1);
215 return -1;
216 }
217 iok++;
218
219 optind += 2;
220 }
221 }
222
223 if (!ok && !iok) {
32a121cb 224 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
a36ceb85
AH
225 return -1;
226 }
227
228 /* check that we passed the correct parameters to the target */
852d5122
JHS
229#if (XTABLES_VERSION_CODE >= 6)
230 if (m)
231 xtables_option_tfcall(m);
232#else
a36ceb85
AH
233 if (m && m->final_check)
234 m->final_check(m->tflags);
852d5122 235#endif
a36ceb85
AH
236
237 {
238 struct tcmsg *t = NLMSG_DATA(n);
32a121cb 239
a36ceb85
AH
240 if (t->tcm_parent != TC_H_ROOT
241 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
242 hook = NF_IP_PRE_ROUTING;
243 } else {
244 hook = NF_IP_POST_ROUTING;
245 }
246 }
247
248 tail = NLMSG_TAIL(n);
249 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
250 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
251 fprintf(stdout, "\ttarget: ");
252
44574522
PS
253 if (m) {
254 if (m->print)
255 m->print(NULL, m->t, 0);
256 else
257 printf("%s ", m->name);
258 }
a36ceb85
AH
259 fprintf(stdout, " index %d\n", index);
260
261 if (strlen(tname) > 16) {
262 size = 16;
263 k[15] = 0;
264 } else {
265 size = 1 + strlen(tname);
266 }
267 strncpy(k, tname, size);
268
269 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
270 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
271 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
272 if (m)
273 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
274 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
275
276 argc -= optind;
277 argv += optind;
278 *argc_p = rargc - iargc;
279 *argv_p = argv;
280
281 optind = 0;
282 xtables_free_opts(1);
a36ceb85 283
4f3626f9
DM
284 if (m) {
285 /* Clear flags if target will be used again */
286 m->tflags = 0;
287 m->used = 0;
288 /* Free allocated memory */
289 if (m->t)
290 free(m->t);
291 }
a36ceb85
AH
292
293 return 0;
294
295}
296
297static int
32a121cb 298print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
a36ceb85
AH
299{
300 struct rtattr *tb[TCA_IPT_MAX + 1];
301 struct xt_entry_target *t = NULL;
852d5122 302 struct option *opts = NULL;
a36ceb85
AH
303
304 if (arg == NULL)
305 return -1;
306
6e2e5ec2
AG
307 /* copy tcipt_globals because .opts will be modified by iptables */
308 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
309
310 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
a36ceb85
AH
311 set_lib_dir();
312
313 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
314
315 if (tb[TCA_IPT_TABLE] == NULL) {
316 fprintf(f, "[NULL ipt table name ] assuming mangle ");
317 } else {
318 fprintf(f, "tablename: %s ",
ff24746c 319 rta_getattr_str(tb[TCA_IPT_TABLE]));
a36ceb85
AH
320 }
321
322 if (tb[TCA_IPT_HOOK] == NULL) {
323 fprintf(f, "[NULL ipt hook name ]\n ");
324 return -1;
325 } else {
326 __u32 hook;
32a121cb 327
ff24746c 328 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
32a121cb 329 fprintf(f, " hook: %s\n", ipthooks[hook]);
a36ceb85
AH
330 }
331
332 if (tb[TCA_IPT_TARG] == NULL) {
32a121cb 333 fprintf(f, "\t[NULL ipt target parameters ]\n");
a36ceb85
AH
334 return -1;
335 } else {
336 struct xtables_target *m = NULL;
32a121cb 337
a36ceb85
AH
338 t = RTA_DATA(tb[TCA_IPT_TARG]);
339 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
32a121cb
SH
340 if (m != NULL) {
341 if (build_st(m, t) < 0) {
342 fprintf(stderr, " %s error\n", m->name);
a36ceb85
AH
343 return -1;
344 }
345
73de5d96 346#if (XTABLES_VERSION_CODE >= 6)
6e2e5ec2
AG
347 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
348 tmp_tcipt_globals.opts,
852d5122
JHS
349 m->x6_options,
350 &m->option_offset);
351#else
6e2e5ec2 352 opts = xtables_merge_options(tmp_tcipt_globals.opts,
852d5122
JHS
353 m->extra_opts,
354 &m->option_offset);
73de5d96 355#endif
852d5122 356 if (opts == NULL) {
b2e116d6 357 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
852d5122
JHS
358 return -1;
359 } else
6e2e5ec2 360 tmp_tcipt_globals.opts = opts;
a36ceb85
AH
361 } else {
362 fprintf(stderr, " failed to find target %s\n\n",
363 t->u.user.name);
364 return -1;
365 }
366 fprintf(f, "\ttarget ");
367 m->print(NULL, m->t, 0);
368 if (tb[TCA_IPT_INDEX] == NULL) {
369 fprintf(f, " [NULL ipt target index ]\n");
370 } else {
371 __u32 index;
32a121cb 372
ff24746c 373 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
32a121cb 374 fprintf(f, "\n\tindex %d", index);
a36ceb85
AH
375 }
376
377 if (tb[TCA_IPT_CNT]) {
32a121cb
SH
378 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
379
a36ceb85
AH
380 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
381 }
382 if (show_stats) {
383 if (tb[TCA_IPT_TM]) {
384 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
32a121cb
SH
385
386 print_tm(f, tm);
a36ceb85
AH
387 }
388 }
32a121cb 389 fprintf(f, "\n");
a36ceb85
AH
390
391 }
392 xtables_free_opts(1);
393
394 return 0;
395}
396
8e91a80d 397struct action_util xt_action_util = {
32a121cb
SH
398 .id = "xt",
399 .parse_aopt = parse_ipt,
400 .print_aopt = print_ipt,
a36ceb85 401};