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)
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
24 #include <linux/tc_act/tc_ipt.h>
38 # define XT_LIB_DIR "/lib/xtables"
41 #ifndef __ALIGN_KERNEL
42 #define __ALIGN_KERNEL(x, a) \
43 __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
44 #define __ALIGN_KERNEL_MASK(x, mask) \
45 (((x) + (mask)) & ~(mask))
49 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
52 static const char *tname
= "mangle";
56 static const char * const ipthooks
[] = {
64 static struct option original_opts
[] = {
73 static struct xtables_globals tcipt_globals
= {
75 .program_name
= "tc-ipt",
76 .program_version
= "0.2",
77 .orig_opts
= original_opts
,
78 .opts
= original_opts
,
80 #if XTABLES_VERSION_CODE >= 11
81 .compat_rev
= xtables_compatible_revision
,
86 * we may need to check for version mismatch
89 build_st(struct xtables_target
*target
, struct xt_entry_target
*t
)
93 XT_ALIGN(sizeof(struct xt_entry_target
)) + target
->size
;
96 target
->t
= xtables_calloc(1, size
);
97 target
->t
->u
.target_size
= size
;
98 strcpy(target
->t
->u
.user
.name
, target
->name
);
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
;
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
= NLMSG_TAIL(n
);
268 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
269 fprintf(stdout
, "tablename: %s hook: %s\n ", tname
, ipthooks
[hook
]);
270 fprintf(stdout
, "\ttarget: ");
274 m
->print(NULL
, m
->t
, 0);
276 printf("%s ", m
->name
);
278 fprintf(stdout
, " index %d\n", index
);
280 if (strlen(tname
) > 16) {
284 size
= 1 + strlen(tname
);
286 strncpy(k
, tname
, size
);
288 addattr_l(n
, MAX_MSG
, TCA_IPT_TABLE
, k
, size
);
289 addattr_l(n
, MAX_MSG
, TCA_IPT_HOOK
, &hook
, 4);
290 addattr_l(n
, MAX_MSG
, TCA_IPT_INDEX
, &index
, 4);
292 addattr_l(n
, MAX_MSG
, TCA_IPT_TARG
, m
->t
, m
->t
->u
.target_size
);
293 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
300 xtables_free_opts(1);
303 /* Clear flags if target will be used again */
306 /* Free allocated memory */
316 print_ipt(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
318 struct xtables_target
*m
;
319 struct rtattr
*tb
[TCA_IPT_MAX
+ 1];
320 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(f
, "[NULL ipt table name ] assuming mangle ");
336 fprintf(f
, "tablename: %s ",
337 rta_getattr_str(tb
[TCA_IPT_TABLE
]));
340 if (tb
[TCA_IPT_HOOK
] == NULL
) {
341 fprintf(f
, "[NULL ipt hook name ]\n ");
346 hook
= rta_getattr_u32(tb
[TCA_IPT_HOOK
]);
347 fprintf(f
, " hook: %s\n", ipthooks
[hook
]);
350 if (tb
[TCA_IPT_TARG
] == NULL
) {
351 fprintf(f
, "\t[NULL ipt target parameters ]\n");
355 t
= RTA_DATA(tb
[TCA_IPT_TARG
]);
356 m
= xtables_find_target(t
->u
.user
.name
, XTF_TRY_LOAD
);
358 fprintf(stderr
, " failed to find target %s\n\n",
362 if (build_st(m
, t
) < 0) {
363 fprintf(stderr
, " %s error\n", m
->name
);
367 if (get_xtables_target_opts(&tmp_tcipt_globals
, m
) < 0) {
369 " failed to find additional options for target %s\n\n",
373 fprintf(f
, "\ttarget ");
374 m
->print(NULL
, m
->t
, 0);
375 if (tb
[TCA_IPT_INDEX
] == NULL
) {
376 fprintf(f
, " [NULL ipt target index ]\n");
380 index
= rta_getattr_u32(tb
[TCA_IPT_INDEX
]);
381 fprintf(f
, "\n\tindex %u", index
);
384 if (tb
[TCA_IPT_CNT
]) {
385 struct tc_cnt
*c
= RTA_DATA(tb
[TCA_IPT_CNT
]);
387 fprintf(f
, " ref %d bind %d", c
->refcnt
, c
->bindcnt
);
390 if (tb
[TCA_IPT_TM
]) {
391 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IPT_TM
]);
398 xtables_free_opts(1);
403 struct action_util xt_action_util
= {
405 .parse_aopt
= parse_ipt
,
406 .print_aopt
= print_ipt
,