2009-12-02 23:11:50 +08:00
|
|
|
/*
|
|
|
|
* m_xt.c xtables based targets
|
2014-12-21 07:47:17 +08:00
|
|
|
* utilities mostly ripped from iptables <duh, its the linux way>
|
2009-12-02 23:11:50 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can distribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: J Hadi Salim (hadi@cyberus.ca)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
|
|
#include <xtables.h>
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tc_util.h"
|
|
|
|
#include <linux/tc_act/tc_ipt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#ifndef XT_LIB_DIR
|
|
|
|
# define XT_LIB_DIR "/lib/xtables"
|
|
|
|
#endif
|
|
|
|
|
2013-04-29 13:50:13 +08:00
|
|
|
#ifndef __ALIGN_KERNEL
|
2016-06-15 05:40:53 +08:00
|
|
|
#define __ALIGN_KERNEL(x, a) \
|
|
|
|
__ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
|
|
|
|
#define __ALIGN_KERNEL_MASK(x, mask) \
|
|
|
|
(((x) + (mask)) & ~(mask))
|
2013-04-29 13:50:13 +08:00
|
|
|
#endif
|
|
|
|
|
2010-03-30 06:17:48 +08:00
|
|
|
#ifndef ALIGN
|
2016-03-22 02:48:36 +08:00
|
|
|
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
|
2010-03-30 06:17:48 +08:00
|
|
|
#endif
|
|
|
|
|
2009-12-02 23:11:50 +08:00
|
|
|
static const char *tname = "mangle";
|
|
|
|
|
|
|
|
char *lib_dir;
|
|
|
|
|
2016-06-15 05:40:53 +08:00
|
|
|
static const char * const ipthooks[] = {
|
2009-12-02 23:11:50 +08:00
|
|
|
"NF_IP_PRE_ROUTING",
|
|
|
|
"NF_IP_LOCAL_IN",
|
|
|
|
"NF_IP_FORWARD",
|
|
|
|
"NF_IP_LOCAL_OUT",
|
|
|
|
"NF_IP_POST_ROUTING",
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct option original_opts[] = {
|
|
|
|
{
|
|
|
|
.name = "jump",
|
|
|
|
.has_arg = 1,
|
|
|
|
.val = 'j'
|
|
|
|
},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct xtables_globals tcipt_globals = {
|
|
|
|
.option_offset = 0,
|
|
|
|
.program_name = "tc-ipt",
|
|
|
|
.program_version = "0.2",
|
|
|
|
.orig_opts = original_opts,
|
|
|
|
.opts = original_opts,
|
|
|
|
.exit_err = NULL,
|
2017-01-13 20:06:19 +08:00
|
|
|
#if XTABLES_VERSION_CODE >= 11
|
2017-01-12 22:22:49 +08:00
|
|
|
.compat_rev = xtables_compatible_revision,
|
|
|
|
#endif
|
2009-12-02 23:11:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we may need to check for version mismatch
|
|
|
|
*/
|
2013-02-13 03:09:03 +08:00
|
|
|
static int
|
2009-12-02 23:11:50 +08:00
|
|
|
build_st(struct xtables_target *target, struct xt_entry_target *t)
|
|
|
|
{
|
|
|
|
|
|
|
|
size_t size =
|
2016-03-22 02:48:36 +08:00
|
|
|
XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
|
2009-12-02 23:11:50 +08:00
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
if (t == NULL) {
|
2009-12-02 23:11:50 +08:00
|
|
|
target->t = xtables_calloc(1, size);
|
|
|
|
target->t->u.target_size = size;
|
|
|
|
strcpy(target->t->u.user.name, target->name);
|
2010-11-22 05:16:54 +08:00
|
|
|
target->t->u.user.revision = target->revision;
|
2009-12-02 23:11:50 +08:00
|
|
|
|
|
|
|
if (target->init != NULL)
|
|
|
|
target->init(target->t);
|
|
|
|
} else {
|
|
|
|
target->t = t;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
static void set_lib_dir(void)
|
2009-12-02 23:11:50 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
lib_dir = getenv("XTABLES_LIBDIR");
|
|
|
|
if (!lib_dir) {
|
|
|
|
lib_dir = getenv("IPTABLES_LIB_DIR");
|
|
|
|
if (lib_dir)
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
|
2009-12-02 23:11:50 +08:00
|
|
|
}
|
|
|
|
if (lib_dir == NULL)
|
|
|
|
lib_dir = XT_LIB_DIR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-10 19:42:08 +08:00
|
|
|
static int get_xtables_target_opts(struct xtables_globals *globals,
|
|
|
|
struct xtables_target *m)
|
|
|
|
{
|
|
|
|
struct option *opts;
|
|
|
|
|
2017-01-13 20:06:19 +08:00
|
|
|
#if XTABLES_VERSION_CODE >= 6
|
2016-06-10 19:42:08 +08:00
|
|
|
opts = xtables_options_xfrm(globals->orig_opts,
|
|
|
|
globals->opts,
|
|
|
|
m->x6_options,
|
|
|
|
&m->option_offset);
|
|
|
|
#else
|
|
|
|
opts = xtables_merge_options(globals->opts,
|
|
|
|
m->extra_opts,
|
|
|
|
&m->option_offset);
|
|
|
|
#endif
|
|
|
|
if (!opts)
|
|
|
|
return -1;
|
|
|
|
globals->opts = opts;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
static int parse_ipt(struct action_util *a, int *argc_p,
|
2009-12-02 23:11:50 +08:00
|
|
|
char ***argv_p, int tca_id, struct nlmsghdr *n)
|
|
|
|
{
|
|
|
|
struct xtables_target *m = NULL;
|
2017-05-23 21:40:57 +08:00
|
|
|
#if XTABLES_VERSION_CODE >= 6
|
|
|
|
struct ipt_entry fw = {};
|
|
|
|
#endif
|
2009-12-02 23:11:50 +08:00
|
|
|
struct rtattr *tail;
|
2012-12-16 18:41:39 +08:00
|
|
|
|
2009-12-02 23:11:50 +08:00
|
|
|
int c;
|
|
|
|
char **argv = *argv_p;
|
2016-06-10 19:42:07 +08:00
|
|
|
int argc;
|
2009-12-02 23:11:50 +08:00
|
|
|
char k[16];
|
|
|
|
int size = 0;
|
|
|
|
int iok = 0, ok = 0;
|
|
|
|
__u32 hook = 0, index = 0;
|
|
|
|
|
2016-06-10 19:42:01 +08:00
|
|
|
/* copy tcipt_globals because .opts will be modified by iptables */
|
|
|
|
struct xtables_globals tmp_tcipt_globals = tcipt_globals;
|
2016-06-15 05:40:53 +08:00
|
|
|
|
2016-06-10 19:42:01 +08:00
|
|
|
xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
|
2009-12-02 23:11:50 +08:00
|
|
|
set_lib_dir();
|
|
|
|
|
2016-06-10 19:42:07 +08:00
|
|
|
/* parse only up until the next action */
|
|
|
|
for (argc = 0; argc < *argc_p; argc++) {
|
|
|
|
if (!argv[argc] || !strcmp(argv[argc], "action"))
|
|
|
|
break;
|
2009-12-02 23:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc <= 2) {
|
2016-06-15 05:40:53 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"too few arguments for xt, need at least '-j <target>'\n");
|
2009-12-02 23:11:50 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2016-06-10 19:42:01 +08:00
|
|
|
c = getopt_long(argc, argv, "j:", tmp_tcipt_globals.opts, NULL);
|
2009-12-02 23:11:50 +08:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'j':
|
|
|
|
m = xtables_find_target(optarg, XTF_TRY_LOAD);
|
2016-06-10 19:42:02 +08:00
|
|
|
if (!m) {
|
2016-06-15 05:40:53 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" failed to find target %s\n\n",
|
|
|
|
optarg);
|
2016-06-10 19:42:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
|
2016-06-10 19:42:02 +08:00
|
|
|
if (build_st(m, NULL) < 0) {
|
|
|
|
printf(" %s error\n", m->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-10 19:42:08 +08:00
|
|
|
|
2016-06-15 05:40:53 +08:00
|
|
|
if (get_xtables_target_opts(&tmp_tcipt_globals,
|
|
|
|
m) < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
" failed to find additional options for target %s\n\n",
|
|
|
|
optarg);
|
2012-12-16 18:41:39 +08:00
|
|
|
return -1;
|
2016-06-10 19:42:08 +08:00
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
ok++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-01-13 20:06:19 +08:00
|
|
|
#if XTABLES_VERSION_CODE >= 6
|
2016-06-10 19:42:02 +08:00
|
|
|
if (m != NULL && m->x6_parse != NULL) {
|
2017-05-23 21:40:57 +08:00
|
|
|
xtables_option_tpcall(c, argv, 0, m, &fw);
|
2012-12-16 18:41:39 +08:00
|
|
|
#else
|
2016-06-10 19:42:02 +08:00
|
|
|
if (m != NULL && m->parse != NULL) {
|
|
|
|
m->parse(c - m->option_offset, argv, 0,
|
2016-06-15 05:40:53 +08:00
|
|
|
&m->tflags, NULL, &m->t);
|
2012-12-16 18:41:39 +08:00
|
|
|
#endif
|
2009-12-02 23:11:50 +08:00
|
|
|
} else {
|
2016-06-15 05:40:53 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"failed to find target %s\n\n", optarg);
|
2009-12-02 23:11:50 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 19:42:06 +08:00
|
|
|
if (argc > optind) {
|
2009-12-02 23:11:50 +08:00
|
|
|
if (matches(argv[optind], "index") == 0) {
|
|
|
|
if (get_u32(&index, argv[optind + 1], 10)) {
|
|
|
|
fprintf(stderr, "Illegal \"index\"\n");
|
|
|
|
xtables_free_opts(1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
iok++;
|
|
|
|
|
|
|
|
optind += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok && !iok) {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
|
2009-12-02 23:11:50 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that we passed the correct parameters to the target */
|
2017-01-13 20:06:19 +08:00
|
|
|
#if XTABLES_VERSION_CODE >= 6
|
2012-12-16 18:41:39 +08:00
|
|
|
if (m)
|
|
|
|
xtables_option_tfcall(m);
|
|
|
|
#else
|
2009-12-02 23:11:50 +08:00
|
|
|
if (m && m->final_check)
|
|
|
|
m->final_check(m->tflags);
|
2012-12-16 18:41:39 +08:00
|
|
|
#endif
|
2009-12-02 23:11:50 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
struct tcmsg *t = NLMSG_DATA(n);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2009-12-02 23:11:50 +08:00
|
|
|
if (t->tcm_parent != TC_H_ROOT
|
|
|
|
&& t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
|
|
|
|
hook = NF_IP_PRE_ROUTING;
|
|
|
|
} else {
|
|
|
|
hook = NF_IP_POST_ROUTING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tail = NLMSG_TAIL(n);
|
|
|
|
addattr_l(n, MAX_MSG, tca_id, NULL, 0);
|
|
|
|
fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
|
|
|
|
fprintf(stdout, "\ttarget: ");
|
|
|
|
|
2016-06-10 19:42:00 +08:00
|
|
|
if (m) {
|
|
|
|
if (m->print)
|
|
|
|
m->print(NULL, m->t, 0);
|
|
|
|
else
|
|
|
|
printf("%s ", m->name);
|
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
fprintf(stdout, " index %d\n", index);
|
|
|
|
|
|
|
|
if (strlen(tname) > 16) {
|
|
|
|
size = 16;
|
|
|
|
k[15] = 0;
|
|
|
|
} else {
|
|
|
|
size = 1 + strlen(tname);
|
|
|
|
}
|
|
|
|
strncpy(k, tname, size);
|
|
|
|
|
|
|
|
addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
|
|
|
|
addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
|
|
|
|
addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
|
|
|
|
if (m)
|
|
|
|
addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
|
|
|
|
tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
|
|
|
|
|
|
|
|
argv += optind;
|
2016-06-10 19:42:06 +08:00
|
|
|
*argc_p -= argc;
|
2009-12-02 23:11:50 +08:00
|
|
|
*argv_p = argv;
|
|
|
|
|
|
|
|
optind = 0;
|
|
|
|
xtables_free_opts(1);
|
|
|
|
|
2011-09-01 03:18:49 +08:00
|
|
|
if (m) {
|
|
|
|
/* Clear flags if target will be used again */
|
|
|
|
m->tflags = 0;
|
|
|
|
m->used = 0;
|
|
|
|
/* Free allocated memory */
|
|
|
|
if (m->t)
|
|
|
|
free(m->t);
|
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-06-15 05:40:53 +08:00
|
|
|
print_ipt(struct action_util *au, FILE *f, struct rtattr *arg)
|
2009-12-02 23:11:50 +08:00
|
|
|
{
|
2016-06-10 19:42:03 +08:00
|
|
|
struct xtables_target *m;
|
2009-12-02 23:11:50 +08:00
|
|
|
struct rtattr *tb[TCA_IPT_MAX + 1];
|
|
|
|
struct xt_entry_target *t = NULL;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2014-05-10 17:19:18 +08:00
|
|
|
/* copy tcipt_globals because .opts will be modified by iptables */
|
|
|
|
struct xtables_globals tmp_tcipt_globals = tcipt_globals;
|
|
|
|
|
|
|
|
xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
|
2009-12-02 23:11:50 +08:00
|
|
|
set_lib_dir();
|
|
|
|
|
|
|
|
parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
|
|
|
|
|
|
|
|
if (tb[TCA_IPT_TABLE] == NULL) {
|
|
|
|
fprintf(f, "[NULL ipt table name ] assuming mangle ");
|
|
|
|
} else {
|
|
|
|
fprintf(f, "tablename: %s ",
|
2012-04-10 23:47:55 +08:00
|
|
|
rta_getattr_str(tb[TCA_IPT_TABLE]));
|
2009-12-02 23:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[TCA_IPT_HOOK] == NULL) {
|
|
|
|
fprintf(f, "[NULL ipt hook name ]\n ");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
__u32 hook;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2012-04-10 23:47:55 +08:00
|
|
|
hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(f, " hook: %s\n", ipthooks[hook]);
|
2009-12-02 23:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[TCA_IPT_TARG] == NULL) {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(f, "\t[NULL ipt target parameters ]\n");
|
2009-12-02 23:11:50 +08:00
|
|
|
return -1;
|
2016-06-10 19:42:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
t = RTA_DATA(tb[TCA_IPT_TARG]);
|
|
|
|
m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
|
|
|
|
if (!m) {
|
|
|
|
fprintf(stderr, " failed to find target %s\n\n",
|
|
|
|
t->u.user.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (build_st(m, t) < 0) {
|
|
|
|
fprintf(stderr, " %s error\n", m->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
|
2016-06-10 19:42:08 +08:00
|
|
|
if (get_xtables_target_opts(&tmp_tcipt_globals, m) < 0) {
|
|
|
|
fprintf(stderr,
|
2016-06-15 05:40:53 +08:00
|
|
|
" failed to find additional options for target %s\n\n",
|
2016-06-10 19:42:08 +08:00
|
|
|
t->u.user.name);
|
2016-06-10 19:42:03 +08:00
|
|
|
return -1;
|
2016-06-10 19:42:08 +08:00
|
|
|
}
|
2016-06-10 19:42:03 +08:00
|
|
|
fprintf(f, "\ttarget ");
|
|
|
|
m->print(NULL, m->t, 0);
|
|
|
|
if (tb[TCA_IPT_INDEX] == NULL) {
|
|
|
|
fprintf(f, " [NULL ipt target index ]\n");
|
|
|
|
} else {
|
|
|
|
__u32 index;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2016-06-10 19:42:03 +08:00
|
|
|
index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
|
2016-12-14 04:31:16 +08:00
|
|
|
fprintf(f, "\n\tindex %u", index);
|
2016-06-10 19:42:03 +08:00
|
|
|
}
|
2009-12-02 23:11:50 +08:00
|
|
|
|
2016-06-10 19:42:03 +08:00
|
|
|
if (tb[TCA_IPT_CNT]) {
|
|
|
|
struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2016-06-10 19:42:03 +08:00
|
|
|
fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
|
|
|
|
}
|
|
|
|
if (show_stats) {
|
|
|
|
if (tb[TCA_IPT_TM]) {
|
|
|
|
struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2016-06-10 19:42:03 +08:00
|
|
|
print_tm(f, tm);
|
2009-12-02 23:11:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-10 19:42:03 +08:00
|
|
|
fprintf(f, "\n");
|
|
|
|
|
2009-12-02 23:11:50 +08:00
|
|
|
xtables_free_opts(1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-01 06:52:07 +08:00
|
|
|
struct action_util xt_action_util = {
|
2016-03-22 02:48:36 +08:00
|
|
|
.id = "xt",
|
|
|
|
.parse_aopt = parse_ipt,
|
|
|
|
.print_aopt = print_ipt,
|
2009-12-02 23:11:50 +08:00
|
|
|
};
|