]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_xt.c
tc: m_xt: Simplify argc adjusting in parse_ipt()
[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 rtattr *tail;
124
125 int c;
126 char **argv = *argv_p;
127 int argc;
128 char k[16];
129 int size = 0;
130 int iok = 0, ok = 0;
131 __u32 hook = 0, index = 0;
132 struct option *opts = NULL;
133
134 /* copy tcipt_globals because .opts will be modified by iptables */
135 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
136 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
137 set_lib_dir();
138
139 /* parse only up until the next action */
140 for (argc = 0; argc < *argc_p; argc++) {
141 if (!argv[argc] || !strcmp(argv[argc], "action"))
142 break;
143 }
144
145 if (argc <= 2) {
146 fprintf(stderr, "too few arguments for xt, need at least '-j <target>'\n");
147 return -1;
148 }
149
150 while (1) {
151 c = getopt_long(argc, argv, "j:", tmp_tcipt_globals.opts, NULL);
152 if (c == -1)
153 break;
154 switch (c) {
155 case 'j':
156 m = xtables_find_target(optarg, XTF_TRY_LOAD);
157 if (!m) {
158 fprintf(stderr, " failed to find target %s\n\n", optarg);
159 return -1;
160 }
161
162 if (build_st(m, NULL) < 0) {
163 printf(" %s error\n", m->name);
164 return -1;
165 }
166 #if (XTABLES_VERSION_CODE >= 6)
167 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
168 tmp_tcipt_globals.opts,
169 m->x6_options,
170 &m->option_offset);
171 #else
172 opts = xtables_merge_options(tmp_tcipt_globals.opts,
173 m->extra_opts,
174 &m->option_offset);
175 #endif
176 if (opts == NULL) {
177 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
178 return -1;
179 } else
180 tmp_tcipt_globals.opts = opts;
181 ok++;
182 break;
183
184 default:
185 #if (XTABLES_VERSION_CODE >= 6)
186 if (m != NULL && m->x6_parse != NULL) {
187 xtables_option_tpcall(c, argv, 0, m, NULL);
188 #else
189 if (m != NULL && m->parse != NULL) {
190 m->parse(c - m->option_offset, argv, 0,
191 &m->tflags, NULL, &m->t);
192 #endif
193 } else {
194 fprintf(stderr, "failed to find target %s\n\n", optarg);
195 return -1;
196
197 }
198 ok++;
199 break;
200 }
201 }
202
203 if (argc > optind) {
204 if (matches(argv[optind], "index") == 0) {
205 if (get_u32(&index, argv[optind + 1], 10)) {
206 fprintf(stderr, "Illegal \"index\"\n");
207 xtables_free_opts(1);
208 return -1;
209 }
210 iok++;
211
212 optind += 2;
213 }
214 }
215
216 if (!ok && !iok) {
217 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
218 return -1;
219 }
220
221 /* check that we passed the correct parameters to the target */
222 #if (XTABLES_VERSION_CODE >= 6)
223 if (m)
224 xtables_option_tfcall(m);
225 #else
226 if (m && m->final_check)
227 m->final_check(m->tflags);
228 #endif
229
230 {
231 struct tcmsg *t = NLMSG_DATA(n);
232
233 if (t->tcm_parent != TC_H_ROOT
234 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
235 hook = NF_IP_PRE_ROUTING;
236 } else {
237 hook = NF_IP_POST_ROUTING;
238 }
239 }
240
241 tail = NLMSG_TAIL(n);
242 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
243 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
244 fprintf(stdout, "\ttarget: ");
245
246 if (m) {
247 if (m->print)
248 m->print(NULL, m->t, 0);
249 else
250 printf("%s ", m->name);
251 }
252 fprintf(stdout, " index %d\n", index);
253
254 if (strlen(tname) > 16) {
255 size = 16;
256 k[15] = 0;
257 } else {
258 size = 1 + strlen(tname);
259 }
260 strncpy(k, tname, size);
261
262 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
263 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
264 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
265 if (m)
266 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
267 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
268
269 argv += optind;
270 *argc_p -= argc;
271 *argv_p = argv;
272
273 optind = 0;
274 xtables_free_opts(1);
275
276 if (m) {
277 /* Clear flags if target will be used again */
278 m->tflags = 0;
279 m->used = 0;
280 /* Free allocated memory */
281 if (m->t)
282 free(m->t);
283 }
284
285 return 0;
286
287 }
288
289 static int
290 print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
291 {
292 struct xtables_target *m;
293 struct rtattr *tb[TCA_IPT_MAX + 1];
294 struct xt_entry_target *t = NULL;
295 struct option *opts = NULL;
296
297 if (arg == NULL)
298 return -1;
299
300 /* copy tcipt_globals because .opts will be modified by iptables */
301 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
302
303 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
304 set_lib_dir();
305
306 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
307
308 if (tb[TCA_IPT_TABLE] == NULL) {
309 fprintf(f, "[NULL ipt table name ] assuming mangle ");
310 } else {
311 fprintf(f, "tablename: %s ",
312 rta_getattr_str(tb[TCA_IPT_TABLE]));
313 }
314
315 if (tb[TCA_IPT_HOOK] == NULL) {
316 fprintf(f, "[NULL ipt hook name ]\n ");
317 return -1;
318 } else {
319 __u32 hook;
320
321 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
322 fprintf(f, " hook: %s\n", ipthooks[hook]);
323 }
324
325 if (tb[TCA_IPT_TARG] == NULL) {
326 fprintf(f, "\t[NULL ipt target parameters ]\n");
327 return -1;
328 }
329
330 t = RTA_DATA(tb[TCA_IPT_TARG]);
331 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
332 if (!m) {
333 fprintf(stderr, " failed to find target %s\n\n",
334 t->u.user.name);
335 return -1;
336 }
337 if (build_st(m, t) < 0) {
338 fprintf(stderr, " %s error\n", m->name);
339 return -1;
340 }
341
342 #if (XTABLES_VERSION_CODE >= 6)
343 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
344 tmp_tcipt_globals.opts,
345 m->x6_options,
346 &m->option_offset);
347 #else
348 opts = xtables_merge_options(tmp_tcipt_globals.opts,
349 m->extra_opts,
350 &m->option_offset);
351 #endif
352 if (opts == NULL) {
353 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
354 return -1;
355 } else
356 tmp_tcipt_globals.opts = opts;
357 fprintf(f, "\ttarget ");
358 m->print(NULL, m->t, 0);
359 if (tb[TCA_IPT_INDEX] == NULL) {
360 fprintf(f, " [NULL ipt target index ]\n");
361 } else {
362 __u32 index;
363
364 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
365 fprintf(f, "\n\tindex %d", index);
366 }
367
368 if (tb[TCA_IPT_CNT]) {
369 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
370
371 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
372 }
373 if (show_stats) {
374 if (tb[TCA_IPT_TM]) {
375 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
376
377 print_tm(f, tm);
378 }
379 }
380 fprintf(f, "\n");
381
382 xtables_free_opts(1);
383
384 return 0;
385 }
386
387 struct action_util xt_action_util = {
388 .id = "xt",
389 .parse_aopt = parse_ipt,
390 .print_aopt = print_ipt,
391 };