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 /*XXX: in the future (xtables 1.4.3?) get rid of everything tagged
14 * as TC_CONFIG_XT_H */
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv4/ip_tables.h>
26 #include <linux/tc_act/tc_ipt.h>
40 #include "xt-internal.h"
44 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
45 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
48 static const char *pname
= "tc-ipt";
49 static const char *tname
= "mangle";
50 static const char *pversion
= "0.2";
52 static const char *ipthooks
[] = {
60 static struct option original_opts
[] = {
65 static struct option
*opts
= original_opts
;
66 static unsigned int global_option_offset
;
68 const char *program_version
= XTABLES_VERSION
;
69 const char *program_name
= "tc-ipt";
70 struct afinfo afinfo
= {
72 .libprefix
= "libxt_",
73 .ipproto
= IPPROTO_IP
,
75 .so_rev_target
= IPT_SO_GET_REVISION_TARGET
,
79 #define OPTION_OFFSET 256
81 /*XXX: TC_CONFIG_XT_H */
82 static void free_opts(struct option
*local_opts
)
84 if (local_opts
!= original_opts
) {
87 global_option_offset
= 0;
91 /*XXX: TC_CONFIG_XT_H */
92 static struct option
*
93 merge_options(struct option
*oldopts
, const struct option
*newopts
,
94 unsigned int *option_offset
)
97 unsigned int num_old
, num_new
, i
;
99 for (num_old
= 0; oldopts
[num_old
].name
; num_old
++);
100 for (num_new
= 0; newopts
[num_new
].name
; num_new
++);
102 *option_offset
= global_option_offset
+ OPTION_OFFSET
;
104 merge
= malloc(sizeof(struct option
) * (num_new
+ num_old
+ 1));
105 memcpy(merge
, oldopts
, num_old
* sizeof(struct option
));
106 for (i
= 0; i
< num_new
; i
++) {
107 merge
[num_old
+ i
] = newopts
[i
];
108 merge
[num_old
+ i
].val
+= *option_offset
;
110 memset(merge
+ num_old
+ num_new
, 0, sizeof(struct option
));
116 /*XXX: TC_CONFIG_XT_H */
124 /*XXX: TC_CONFIG_XT_H */
126 check_inverse(const char option
[], int *invert
, int *my_optind
, int argc
)
128 if (option
&& strcmp(option
, "!") == 0) {
130 exit_error(PARAMETER_PROBLEM
,
131 "Multiple `!' flags not allowed");
133 if (my_optind
!= NULL
) {
135 if (argc
&& *my_optind
> argc
)
136 exit_error(PARAMETER_PROBLEM
,
137 "no argument following `!'");
145 /*XXX: TC_CONFIG_XT_H */
146 void exit_error(enum exittype status
, const char *msg
, ...)
151 fprintf(stderr
, "%s v%s: ", pname
, pversion
);
152 vfprintf(stderr
, msg
, args
);
154 fprintf(stderr
, "\n");
155 /* On error paths, make sure that we don't leak memory */
159 /*XXX: TC_CONFIG_XT_H */
160 static void set_revision(char *name
, u_int8_t revision
)
162 /* Old kernel sources don't have ".revision" field,
163 * but we stole a byte from name. */
164 name
[IPT_FUNCTION_MAXNAMELEN
- 2] = '\0';
165 name
[IPT_FUNCTION_MAXNAMELEN
- 1] = revision
;
169 * we may need to check for version mismatch
172 build_st(struct xtables_target
*target
, struct xt_entry_target
*t
)
176 XT_ALIGN(sizeof(struct xt_entry_target
)) + target
->size
;
179 target
->t
= fw_calloc(1, size
);
180 target
->t
->u
.target_size
= size
;
181 strcpy(target
->t
->u
.user
.name
, target
->name
);
182 set_revision(target
->t
->u
.user
.name
, target
->revision
);
184 if (target
->init
!= NULL
)
185 target
->init(target
->t
);
193 inline void set_lib_dir(void)
196 lib_dir
= getenv("XTABLES_LIBDIR");
198 lib_dir
= getenv("IPTABLES_LIB_DIR");
200 fprintf(stderr
, "using deprecated IPTABLES_LIB_DIR\n");
203 lib_dir
= XT_LIB_DIR
;
207 static int parse_ipt(struct action_util
*a
, int *argc_p
,
208 char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
210 struct xtables_target
*m
= NULL
;
215 char **argv
= *argv_p
;
216 int argc
= 0, iargc
= 0;
220 __u32 hook
= 0, index
= 0;
227 for (i
= 0; i
< rargc
; i
++) {
228 if (NULL
== argv
[i
] || 0 == strcmp(argv
[i
], "action")) {
236 fprintf(stderr
, "bad arguments to ipt %d vs %d\n", argc
, rargc
);
241 c
= getopt_long(argc
, argv
, "j:", opts
, NULL
);
246 m
= find_target(optarg
, TRY_LOAD
);
249 if (build_st(m
, NULL
) < 0) {
250 printf(" %s error\n", m
->name
);
254 merge_options(opts
, m
->extra_opts
,
257 fprintf(stderr
, " failed to find target %s\n\n", optarg
);
264 memset(&fw
, 0, sizeof(fw
));
266 m
->parse(c
- m
->option_offset
, argv
, 0,
267 &m
->tflags
, NULL
, &m
->t
);
269 fprintf(stderr
, " failed to find target %s\n\n", optarg
);
279 if (iargc
> optind
) {
280 if (matches(argv
[optind
], "index") == 0) {
281 if (get_u32(&index
, argv
[optind
+ 1], 10)) {
282 fprintf(stderr
, "Illegal \"index\"\n");
293 fprintf(stderr
, " ipt Parser BAD!! (%s)\n", *argv
);
297 /* check that we passed the correct parameters to the target */
299 m
->final_check(m
->tflags
);
302 struct tcmsg
*t
= NLMSG_DATA(n
);
304 if (t
->tcm_parent
!= TC_H_ROOT
305 && t
->tcm_parent
== TC_H_MAJ(TC_H_INGRESS
)) {
306 hook
= NF_IP_PRE_ROUTING
;
308 hook
= NF_IP_POST_ROUTING
;
312 tail
= NLMSG_TAIL(n
);
313 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
314 fprintf(stdout
, "tablename: %s hook: %s\n ", tname
, ipthooks
[hook
]);
315 fprintf(stdout
, "\ttarget: ");
318 m
->print(NULL
, m
->t
, 0);
319 fprintf(stdout
, " index %d\n", index
);
321 if (strlen(tname
) > 16) {
325 size
= 1 + strlen(tname
);
327 strncpy(k
, tname
, size
);
329 addattr_l(n
, MAX_MSG
, TCA_IPT_TABLE
, k
, size
);
330 addattr_l(n
, MAX_MSG
, TCA_IPT_HOOK
, &hook
, 4);
331 addattr_l(n
, MAX_MSG
, TCA_IPT_INDEX
, &index
, 4);
333 addattr_l(n
, MAX_MSG
, TCA_IPT_TARG
, m
->t
, m
->t
->u
.target_size
);
334 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
338 *argc_p
= rargc
- iargc
;
343 /* Clear flags if target will be used again */
346 /* Free allocated memory */
356 print_ipt(struct action_util
*au
, FILE * f
, struct rtattr
*arg
)
358 struct rtattr
*tb
[TCA_IPT_MAX
+ 1];
359 struct xt_entry_target
*t
= NULL
;
366 parse_rtattr_nested(tb
, TCA_IPT_MAX
, arg
);
368 if (tb
[TCA_IPT_TABLE
] == NULL
) {
369 fprintf(f
, "[NULL ipt table name ] assuming mangle ");
371 fprintf(f
, "tablename: %s ",
372 rta_getattr_str(tb
[TCA_IPT_TABLE
]));
375 if (tb
[TCA_IPT_HOOK
] == NULL
) {
376 fprintf(f
, "[NULL ipt hook name ]\n ");
381 hook
= rta_getattr_u32(tb
[TCA_IPT_HOOK
]);
382 fprintf(f
, " hook: %s\n", ipthooks
[hook
]);
385 if (tb
[TCA_IPT_TARG
] == NULL
) {
386 fprintf(f
, "\t[NULL ipt target parameters ]\n");
389 struct xtables_target
*m
= NULL
;
391 t
= RTA_DATA(tb
[TCA_IPT_TARG
]);
392 m
= find_target(t
->u
.user
.name
, TRY_LOAD
);
394 if (build_st(m
, t
) < 0) {
395 fprintf(stderr
, " %s error\n", m
->name
);
400 merge_options(opts
, m
->extra_opts
,
403 fprintf(stderr
, " failed to find target %s\n\n",
407 fprintf(f
, "\ttarget ");
408 m
->print(NULL
, m
->t
, 0);
409 if (tb
[TCA_IPT_INDEX
] == NULL
) {
410 fprintf(f
, " [NULL ipt target index ]\n");
414 index
= rta_getattr_u32(tb
[TCA_IPT_INDEX
]);
415 fprintf(f
, "\n\tindex %d", index
);
418 if (tb
[TCA_IPT_CNT
]) {
419 struct tc_cnt
*c
= RTA_DATA(tb
[TCA_IPT_CNT
]);
421 fprintf(f
, " ref %d bind %d", c
->refcnt
, c
->bindcnt
);
424 if (tb
[TCA_IPT_TM
]) {
425 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IPT_TM
]);
438 struct action_util ipt_action_util
= {
440 .parse_aopt
= parse_ipt
,
441 .print_aopt
= print_ipt
,