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) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
43 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
47 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
50 static const char *tname
= "mangle";
54 static const char *ipthooks
[] = {
62 static struct option original_opts
[] = {
71 static struct xtables_globals tcipt_globals
= {
73 .program_name
= "tc-ipt",
74 .program_version
= "0.2",
75 .orig_opts
= original_opts
,
76 .opts
= original_opts
,
81 * we may need to check for version mismatch
84 build_st(struct xtables_target
*target
, struct xt_entry_target
*t
)
88 XT_ALIGN(sizeof(struct xt_entry_target
)) + target
->size
;
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
;
96 if (target
->init
!= NULL
)
97 target
->init(target
->t
);
105 static void set_lib_dir(void)
108 lib_dir
= getenv("XTABLES_LIBDIR");
110 lib_dir
= getenv("IPTABLES_LIB_DIR");
112 fprintf(stderr
, "using deprecated IPTABLES_LIB_DIR\n");
115 lib_dir
= XT_LIB_DIR
;
119 static int parse_ipt(struct action_util
*a
, int *argc_p
,
120 char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
122 struct xtables_target
*m
= NULL
;
128 char **argv
= *argv_p
;
129 int argc
= 0, iargc
= 0;
133 __u32 hook
= 0, index
= 0;
134 struct option
*opts
= NULL
;
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
);
144 for (i
= 0; i
< rargc
; i
++) {
145 if (NULL
== argv
[i
] || 0 == strcmp(argv
[i
], "action")) {
153 fprintf(stderr
, "bad arguments to ipt %d vs %d\n", argc
, rargc
);
158 c
= getopt_long(argc
, argv
, "j:", tmp_tcipt_globals
.opts
, NULL
);
163 m
= xtables_find_target(optarg
, XTF_TRY_LOAD
);
165 fprintf(stderr
, " failed to find target %s\n\n", optarg
);
169 if (build_st(m
, NULL
) < 0) {
170 printf(" %s error\n", m
->name
);
173 #if (XTABLES_VERSION_CODE >= 6)
174 opts
= xtables_options_xfrm(tmp_tcipt_globals
.orig_opts
,
175 tmp_tcipt_globals
.opts
,
179 opts
= xtables_merge_options(tmp_tcipt_globals
.opts
,
184 fprintf(stderr
, " failed to find additional options for target %s\n\n", optarg
);
187 tmp_tcipt_globals
.opts
= opts
;
192 memset(&fw
, 0, sizeof(fw
));
193 #if (XTABLES_VERSION_CODE >= 6)
194 if (m
!= NULL
&& m
->x6_parse
!= NULL
) {
195 xtables_option_tpcall(c
, argv
, 0, m
, NULL
);
197 if (m
!= NULL
&& m
->parse
!= NULL
) {
198 m
->parse(c
- m
->option_offset
, argv
, 0,
199 &m
->tflags
, NULL
, &m
->t
);
202 fprintf(stderr
, "failed to find target %s\n\n", optarg
);
211 if (iargc
> optind
) {
212 if (matches(argv
[optind
], "index") == 0) {
213 if (get_u32(&index
, argv
[optind
+ 1], 10)) {
214 fprintf(stderr
, "Illegal \"index\"\n");
215 xtables_free_opts(1);
225 fprintf(stderr
, " ipt Parser BAD!! (%s)\n", *argv
);
229 /* check that we passed the correct parameters to the target */
230 #if (XTABLES_VERSION_CODE >= 6)
232 xtables_option_tfcall(m
);
234 if (m
&& m
->final_check
)
235 m
->final_check(m
->tflags
);
239 struct tcmsg
*t
= NLMSG_DATA(n
);
241 if (t
->tcm_parent
!= TC_H_ROOT
242 && t
->tcm_parent
== TC_H_MAJ(TC_H_INGRESS
)) {
243 hook
= NF_IP_PRE_ROUTING
;
245 hook
= NF_IP_POST_ROUTING
;
249 tail
= NLMSG_TAIL(n
);
250 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
251 fprintf(stdout
, "tablename: %s hook: %s\n ", tname
, ipthooks
[hook
]);
252 fprintf(stdout
, "\ttarget: ");
256 m
->print(NULL
, m
->t
, 0);
258 printf("%s ", m
->name
);
260 fprintf(stdout
, " index %d\n", index
);
262 if (strlen(tname
) > 16) {
266 size
= 1 + strlen(tname
);
268 strncpy(k
, tname
, size
);
270 addattr_l(n
, MAX_MSG
, TCA_IPT_TABLE
, k
, size
);
271 addattr_l(n
, MAX_MSG
, TCA_IPT_HOOK
, &hook
, 4);
272 addattr_l(n
, MAX_MSG
, TCA_IPT_INDEX
, &index
, 4);
274 addattr_l(n
, MAX_MSG
, TCA_IPT_TARG
, m
->t
, m
->t
->u
.target_size
);
275 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
279 *argc_p
= rargc
- iargc
;
283 xtables_free_opts(1);
286 /* Clear flags if target will be used again */
289 /* Free allocated memory */
299 print_ipt(struct action_util
*au
, FILE * f
, struct rtattr
*arg
)
301 struct rtattr
*tb
[TCA_IPT_MAX
+ 1];
302 struct xt_entry_target
*t
= NULL
;
303 struct option
*opts
= NULL
;
308 /* copy tcipt_globals because .opts will be modified by iptables */
309 struct xtables_globals tmp_tcipt_globals
= tcipt_globals
;
311 xtables_init_all(&tmp_tcipt_globals
, NFPROTO_IPV4
);
314 parse_rtattr_nested(tb
, TCA_IPT_MAX
, arg
);
316 if (tb
[TCA_IPT_TABLE
] == NULL
) {
317 fprintf(f
, "[NULL ipt table name ] assuming mangle ");
319 fprintf(f
, "tablename: %s ",
320 rta_getattr_str(tb
[TCA_IPT_TABLE
]));
323 if (tb
[TCA_IPT_HOOK
] == NULL
) {
324 fprintf(f
, "[NULL ipt hook name ]\n ");
329 hook
= rta_getattr_u32(tb
[TCA_IPT_HOOK
]);
330 fprintf(f
, " hook: %s\n", ipthooks
[hook
]);
333 if (tb
[TCA_IPT_TARG
] == NULL
) {
334 fprintf(f
, "\t[NULL ipt target parameters ]\n");
337 struct xtables_target
*m
= NULL
;
339 t
= RTA_DATA(tb
[TCA_IPT_TARG
]);
340 m
= xtables_find_target(t
->u
.user
.name
, XTF_TRY_LOAD
);
342 fprintf(stderr
, " failed to find target %s\n\n",
346 if (build_st(m
, t
) < 0) {
347 fprintf(stderr
, " %s error\n", m
->name
);
351 #if (XTABLES_VERSION_CODE >= 6)
352 opts
= xtables_options_xfrm(tmp_tcipt_globals
.orig_opts
,
353 tmp_tcipt_globals
.opts
,
357 opts
= xtables_merge_options(tmp_tcipt_globals
.opts
,
362 fprintf(stderr
, " failed to find additional options for target %s\n\n", optarg
);
365 tmp_tcipt_globals
.opts
= opts
;
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");
373 index
= rta_getattr_u32(tb
[TCA_IPT_INDEX
]);
374 fprintf(f
, "\n\tindex %d", index
);
377 if (tb
[TCA_IPT_CNT
]) {
378 struct tc_cnt
*c
= RTA_DATA(tb
[TCA_IPT_CNT
]);
380 fprintf(f
, " ref %d bind %d", c
->refcnt
, c
->bindcnt
);
383 if (tb
[TCA_IPT_TM
]) {
384 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IPT_TM
]);
392 xtables_free_opts(1);
397 struct action_util xt_action_util
= {
399 .parse_aopt
= parse_ipt
,
400 .print_aopt
= print_ipt
,