• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ebt_redirect
2  *
3  * Authors:
4  * Bart De Schuymer <bdschuym@pandora.be>
5  *
6  * April, 2002
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <getopt.h>
13 #include <xtables.h>
14 #include <linux/netfilter_bridge/ebt_redirect.h>
15 #include "iptables/nft.h"
16 #include "iptables/nft-bridge.h"
17 
18 #define REDIRECT_TARGET '1'
19 static const struct option brredir_opts[] =
20 {
21 	{ "redirect-target", required_argument, 0, REDIRECT_TARGET },
22 	{ 0 }
23 };
24 
brredir_print_help(void)25 static void brredir_print_help(void)
26 {
27 	printf(
28 	"redirect option:\n"
29 	" --redirect-target target   : ACCEPT, DROP, RETURN or CONTINUE\n");
30 }
31 
brredir_init(struct xt_entry_target * target)32 static void brredir_init(struct xt_entry_target *target)
33 {
34 	struct ebt_redirect_info *redirectinfo =
35 	   (struct ebt_redirect_info *)target->data;
36 
37 	redirectinfo->target = EBT_ACCEPT;
38 }
39 
40 #define OPT_REDIRECT_TARGET  0x01
brredir_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_target ** target)41 static int brredir_parse(int c, char **argv, int invert, unsigned int *flags,
42 			 const void *entry, struct xt_entry_target **target)
43 {
44 	struct ebt_redirect_info *redirectinfo =
45 	   (struct ebt_redirect_info *)(*target)->data;
46 
47 	switch (c) {
48 	case REDIRECT_TARGET:
49 		EBT_CHECK_OPTION(flags, OPT_REDIRECT_TARGET);
50 		if (ebt_fill_target(optarg, (unsigned int *)&redirectinfo->target))
51 			xtables_error(PARAMETER_PROBLEM, "Illegal --redirect-target target");
52 		break;
53 	default:
54 		return 0;
55 	}
56 	return 1;
57 }
58 
brredir_print(const void * ip,const struct xt_entry_target * target,int numeric)59 static void brredir_print(const void *ip, const struct xt_entry_target *target, int numeric)
60 {
61 	struct ebt_redirect_info *redirectinfo =
62 	   (struct ebt_redirect_info *)target->data;
63 
64 	if (redirectinfo->target == EBT_ACCEPT)
65 		return;
66 	printf("--redirect-target %s", ebt_target_name(redirectinfo->target));
67 }
68 
brredir_verdict(int verdict)69 static const char* brredir_verdict(int verdict)
70 {
71 	switch (verdict) {
72 	case EBT_ACCEPT: return "accept";
73 	case EBT_DROP: return "drop";
74 	case EBT_CONTINUE: return "continue";
75 	case EBT_RETURN: return "return";
76 	}
77 
78 	return "";
79 }
80 
brredir_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)81 static int brredir_xlate(struct xt_xlate *xl,
82 			 const struct xt_xlate_tg_params *params)
83 {
84 	const struct ebt_redirect_info *red = (const void*)params->target->data;
85 
86 	xt_xlate_add(xl, "meta set pkttype host");
87 	if (red->target != EBT_ACCEPT)
88 		xt_xlate_add(xl, " %s ", brredir_verdict(red->target));
89 	return 0;
90 }
91 
92 static struct xtables_target brredirect_target = {
93 	.name		= "redirect",
94 	.version	= XTABLES_VERSION,
95 	.family		= NFPROTO_BRIDGE,
96 	.size		= XT_ALIGN(sizeof(struct ebt_redirect_info)),
97 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_redirect_info)),
98 	.help		= brredir_print_help,
99 	.init		= brredir_init,
100 	.parse		= brredir_parse,
101 	.print		= brredir_print,
102 	.xlate		= brredir_xlate,
103 	.extra_opts	= brredir_opts,
104 };
105 
_init(void)106 void _init(void)
107 {
108 	xtables_register_target(&brredirect_target);
109 }
110