• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ebt_nat
2  *
3  * Authors:
4  * Bart De Schuymer <bdschuym@pandora.be>
5  *
6  * June, 2002
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <getopt.h>
13 #include <netinet/ether.h>
14 #include <xtables.h>
15 #include <linux/netfilter_bridge/ebt_nat.h>
16 #include "iptables/nft.h"
17 #include "iptables/nft-bridge.h"
18 
19 #define NAT_D '1'
20 #define NAT_D_TARGET '2'
21 static const struct option brdnat_opts[] =
22 {
23 	{ "to-destination", required_argument, 0, NAT_D },
24 	{ "to-dst"        , required_argument, 0, NAT_D },
25 	{ "dnat-target"   , required_argument, 0, NAT_D_TARGET },
26 	{ 0 }
27 };
28 
brdnat_print_help(void)29 static void brdnat_print_help(void)
30 {
31 	printf(
32 	"dnat options:\n"
33 	" --to-dst address       : MAC address to map destination to\n"
34 	" --dnat-target target   : ACCEPT, DROP, RETURN or CONTINUE\n");
35 }
36 
brdnat_init(struct xt_entry_target * target)37 static void brdnat_init(struct xt_entry_target *target)
38 {
39 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
40 
41 	natinfo->target = EBT_ACCEPT;
42 }
43 
44 #define OPT_DNAT        0x01
45 #define OPT_DNAT_TARGET 0x02
brdnat_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_target ** target)46 static int brdnat_parse(int c, char **argv, int invert, unsigned int *flags,
47 			 const void *entry, struct xt_entry_target **target)
48 {
49 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)(*target)->data;
50 	struct ether_addr *addr;
51 
52 	switch (c) {
53 	case NAT_D:
54 		EBT_CHECK_OPTION(flags, OPT_DNAT);
55 		if (!(addr = ether_aton(optarg)))
56 			xtables_error(PARAMETER_PROBLEM, "Problem with specified --to-destination mac");
57 		memcpy(natinfo->mac, addr, ETH_ALEN);
58 		break;
59 	case NAT_D_TARGET:
60 		EBT_CHECK_OPTION(flags, OPT_DNAT_TARGET);
61 		if (ebt_fill_target(optarg, (unsigned int *)&natinfo->target))
62 			xtables_error(PARAMETER_PROBLEM, "Illegal --dnat-target target");
63 		break;
64 	default:
65 		return 0;
66 	}
67 	return 1;
68 }
69 
brdnat_final_check(unsigned int flags)70 static void brdnat_final_check(unsigned int flags)
71 {
72 	if (!flags)
73 		xtables_error(PARAMETER_PROBLEM,
74 			      "You must specify proper arguments");
75 }
76 
brdnat_print(const void * ip,const struct xt_entry_target * target,int numeric)77 static void brdnat_print(const void *ip, const struct xt_entry_target *target, int numeric)
78 {
79 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
80 
81 	printf("--to-dst ");
82 	xtables_print_mac(natinfo->mac);
83 	printf(" --dnat-target %s", ebt_target_name(natinfo->target));
84 }
85 
brdnat_verdict(int verdict)86 static const char* brdnat_verdict(int verdict)
87 {
88 	switch (verdict) {
89 	case EBT_ACCEPT: return "accept";
90 	case EBT_DROP: return "drop";
91 	case EBT_CONTINUE: return "continue";
92 	case EBT_RETURN: return "return";
93 	}
94 
95 	return "";
96 }
97 
brdnat_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)98 static int brdnat_xlate(struct xt_xlate *xl,
99 			 const struct xt_xlate_tg_params *params)
100 {
101 	const struct ebt_nat_info *natinfo = (const void*)params->target->data;
102 
103 	xt_xlate_add(xl, "ether daddr set %s %s ",
104 		     ether_ntoa((struct ether_addr *)natinfo->mac),
105 		     brdnat_verdict(natinfo->target));
106 
107 	return 1;
108 }
109 
110 static struct xtables_target brdnat_target =
111 {
112 	.name		= "dnat",
113 	.version	= XTABLES_VERSION,
114 	.family		= NFPROTO_BRIDGE,
115 	.size           = XT_ALIGN(sizeof(struct ebt_nat_info)),
116 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_nat_info)),
117 	.help		= brdnat_print_help,
118 	.init		= brdnat_init,
119 	.parse		= brdnat_parse,
120 	.final_check	= brdnat_final_check,
121 	.print		= brdnat_print,
122 	.xlate		= brdnat_xlate,
123 	.extra_opts	= brdnat_opts,
124 };
125 
_init(void)126 void _init(void)
127 {
128 	xtables_register_target(&brdnat_target);
129 }
130