summaryrefslogtreecommitdiffstats
path: root/extensions/ebt_redirect.c
blob: 436158e67f795d6bcf361128f3456bc1a769dcf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "../include/ebtables_u.h"
#include <linux/netfilter_bridge/ebt_redirect.h>

#define REDIRECT_TARGET '1'
static struct option opts[] =
{
	{ "redirect-target", required_argument, 0, REDIRECT_TARGET },
	{ 0 }
};

static void print_help()
{
	printf(
	"redirect option:\n"
	" --redirect-target target   : ACCEPT, DROP, RETURN or CONTINUE\n");
}

static void init(struct ebt_entry_target *target)
{
	struct ebt_redirect_info *redirectinfo =
	   (struct ebt_redirect_info *)target->data;

	redirectinfo->target = EBT_ACCEPT;
	return;
}

#define OPT_REDIRECT_TARGET  0x01
static int parse(int c, char **argv, int argc,
   const struct ebt_u_entry *entry, unsigned int *flags,
   struct ebt_entry_target **target)
{
	struct ebt_redirect_info *redirectinfo =
	   (struct ebt_redirect_info *)(*target)->data;

	switch (c) {
	case REDIRECT_TARGET:
		check_option(flags, OPT_REDIRECT_TARGET);
		if (FILL_TARGET(optarg, redirectinfo->target))
			print_error("Illegal --redirect-target target");
		break;
	default:
		return 0;
	}
	return 1;
}

static void final_check(const struct ebt_u_entry *entry,
   const struct ebt_entry_target *target, const char *name,
   unsigned int hook_mask, unsigned int time)
{
	struct ebt_redirect_info *redirectinfo =
	   (struct ebt_redirect_info *)target->data;

	if (BASE_CHAIN && redirectinfo->target == EBT_RETURN)
		print_error("--redirect-target RETURN not allowed on base chain");
	CLEAR_BASE_CHAIN_BIT;
	if ( ((hook_mask & ~(1 << NF_BR_PRE_ROUTING)) || strcmp(name, "nat")) &&
	   ((hook_mask & ~(1 << NF_BR_BROUTING)) || strcmp(name, "broute")) )
		print_error("Wrong chain for redirect");
}

static void print(const struct ebt_u_entry *entry,
   const struct ebt_entry_target *target)
{
	struct ebt_redirect_info *redirectinfo =
	   (struct ebt_redirect_info *)target->data;

	if (redirectinfo->target == EBT_ACCEPT)
		return;
	printf(" --redirect-target %s", TARGET_NAME(redirectinfo->target));
}

static int compare(const struct ebt_entry_target *t1,
   const struct ebt_entry_target *t2)
{
	struct ebt_redirect_info *redirectinfo1 =
	   (struct ebt_redirect_info *)t1->data;
	struct ebt_redirect_info *redirectinfo2 =
	   (struct ebt_redirect_info *)t2->data;

	return redirectinfo1->target == redirectinfo2->target;
}

static struct ebt_u_target redirect_target =
{
	EBT_REDIRECT_TARGET,
	sizeof(struct ebt_redirect_info),
	print_help,
	init,
	parse,
	final_check,
	print,
	compare,
	opts,
};

static void _init(void) __attribute__ ((constructor));
static void _init(void)
{
	register_target(&redirect_target);
}