2 * m_xt.c xtables based targets
3 * utilities mostly ripped from iptables <duh, its the linux way>
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.
10 * Authors: J Hadi Salim (hadi@cyberus.ca)
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <linux/tc_act/tc_ipt.h>
37 # define XT_LIB_DIR "/lib/xtables"
40 #ifndef __ALIGN_KERNEL
41 #define __ALIGN_KERNEL(x, a) \
42 __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
43 #define __ALIGN_KERNEL_MASK(x, mask) \
44 (((x) + (mask)) & ~(mask))
48 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
51 static const char *tname = "mangle";
55 static const char * const ipthooks[] = {
63 static struct option original_opts[] = {
72 static struct xtables_globals tcipt_globals = {
74 .program_name = "tc-ipt",
75 .program_version = "0.2",
76 .orig_opts = original_opts,
77 .opts = original_opts,
79 #if XTABLES_VERSION_CODE >= 11
80 .compat_rev = xtables_compatible_revision,
85 * we may need to check for version mismatch
88 build_st(struct xtables_target *target, struct xt_entry_target *t)
92 XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
95 target->t = xtables_calloc(1, size);
96 target->t->u.target_size = size;
97 strncpy(target->t->u.user.name, target->name,
98 sizeof(target->t->u.user.name) - 1);
99 target->t->u.user.revision = target->revision;
101 if (target->init != NULL)
102 target->init(target->t);
110 static void set_lib_dir(void)
113 lib_dir = getenv("XTABLES_LIBDIR");
115 lib_dir = getenv("IPTABLES_LIB_DIR");
117 fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
120 lib_dir = XT_LIB_DIR;
124 static int get_xtables_target_opts(struct xtables_globals *globals,
125 struct xtables_target *m)
129 #if XTABLES_VERSION_CODE >= 6
130 opts = xtables_options_xfrm(globals->orig_opts,
135 opts = xtables_merge_options(globals->opts,
141 globals->opts = opts;
145 static int parse_ipt(struct action_util *a, int *argc_p,
146 char ***argv_p, int tca_id, struct nlmsghdr *n)
148 struct xtables_target *m = NULL;
149 #if XTABLES_VERSION_CODE >= 6
150 struct ipt_entry fw = {};
155 char **argv = *argv_p;
157 char k[FILTER_NAMESZ];
160 __u32 hook = 0, index = 0;
162 /* copy tcipt_globals because .opts will be modified by iptables */
163 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
165 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
168 /* parse only up until the next action */
169 for (argc = 0; argc < *argc_p; argc++) {
170 if (!argv[argc] || !strcmp(argv[argc], "action"))
176 "too few arguments for xt, need at least '-j <target>'\n");
181 c = getopt_long(argc, argv, "j:", tmp_tcipt_globals.opts, NULL);
186 m = xtables_find_target(optarg, XTF_TRY_LOAD);
189 " failed to find target %s\n\n",
194 if (build_st(m, NULL) < 0) {
195 printf(" %s error\n", m->name);
199 if (get_xtables_target_opts(&tmp_tcipt_globals,
202 " failed to find additional options for target %s\n\n",
210 #if XTABLES_VERSION_CODE >= 6
211 if (m != NULL && m->x6_parse != NULL) {
212 xtables_option_tpcall(c, argv, 0, m, &fw);
214 if (m != NULL && m->parse != NULL) {
215 m->parse(c - m->option_offset, argv, 0,
216 &m->tflags, NULL, &m->t);
220 "failed to find target %s\n\n", optarg);
230 if (matches(argv[optind], "index") == 0) {
231 if (get_u32(&index, argv[optind + 1], 10)) {
232 fprintf(stderr, "Illegal \"index\"\n");
233 xtables_free_opts(1);
243 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
247 /* check that we passed the correct parameters to the target */
248 #if XTABLES_VERSION_CODE >= 6
250 xtables_option_tfcall(m);
252 if (m && m->final_check)
253 m->final_check(m->tflags);
257 struct tcmsg *t = NLMSG_DATA(n);
259 if (t->tcm_parent != TC_H_ROOT
260 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
261 hook = NF_IP_PRE_ROUTING;
263 hook = NF_IP_POST_ROUTING;
267 tail = addattr_nest(n, MAX_MSG, tca_id);
268 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
269 fprintf(stdout, "\ttarget: ");
273 m->print(NULL, m->t, 0);
275 printf("%s ", m->name);
277 fprintf(stdout, " index %d\n", index);
279 if (strlen(tname) >= 16) {
283 size = 1 + strlen(tname);
285 strncpy(k, tname, size);
287 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
288 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
289 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
291 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
292 addattr_nest_end(n, tail);
299 xtables_free_opts(1);
302 /* Clear flags if target will be used again */
305 /* Free allocated memory */
315 print_ipt(struct action_util *au, FILE *f, struct rtattr *arg)
317 struct xtables_target *m;
318 struct rtattr *tb[TCA_IPT_MAX + 1];
319 struct xt_entry_target *t = NULL;
325 /* copy tcipt_globals because .opts will be modified by iptables */
326 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
328 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
331 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
333 if (tb[TCA_IPT_TABLE] == NULL) {
334 fprintf(stderr, "Missing ipt table name, assuming mangle\n");
336 fprintf(f, "tablename: %s ",
337 rta_getattr_str(tb[TCA_IPT_TABLE]));
340 if (tb[TCA_IPT_HOOK] == NULL) {
341 fprintf(stderr, "Missing ipt hook name\n ");
345 if (tb[TCA_IPT_TARG] == NULL) {
346 fprintf(stderr, "Missing ipt target parameters\n");
350 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
351 fprintf(f, " hook: %s\n", ipthooks[hook]);
353 t = RTA_DATA(tb[TCA_IPT_TARG]);
354 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
356 fprintf(stderr, " failed to find target %s\n\n",
360 if (build_st(m, t) < 0) {
361 fprintf(stderr, " %s error\n", m->name);
365 if (get_xtables_target_opts(&tmp_tcipt_globals, m) < 0) {
367 " failed to find additional options for target %s\n\n",
371 fprintf(f, "\ttarget ");
372 m->print(NULL, m->t, 0);
373 if (tb[TCA_IPT_INDEX] == NULL) {
374 fprintf(f, " [NULL ipt target index ]\n");
378 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
379 fprintf(f, "\n\tindex %u", index);
382 if (tb[TCA_IPT_CNT]) {
383 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
385 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
388 if (tb[TCA_IPT_TM]) {
389 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
396 xtables_free_opts(1);
401 struct action_util xt_action_util = {
403 .parse_aopt = parse_ipt,
404 .print_aopt = print_ipt,