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 <netinet/ether.h>
13 #include <xtables.h>
14 #include <linux/netfilter_bridge/ebt_nat.h>
15 #include "iptables/nft.h"
16 #include "iptables/nft-bridge.h"
17
18 enum {
19 O_DST,
20 O_TARGET,
21 };
22
23 static const struct xt_option_entry brdnat_opts[] =
24 {
25 { .name = "to-destination", .id = O_DST, .type = XTTYPE_ETHERMAC,
26 .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_nat_info, mac) },
27 { .name = "to-dst" , .id = O_DST, .type = XTTYPE_ETHERMAC,
28 .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_nat_info, mac) },
29 { .name = "dnat-target" , .id = O_TARGET, .type = XTTYPE_STRING },
30 XTOPT_TABLEEND,
31 };
32
brdnat_print_help(void)33 static void brdnat_print_help(void)
34 {
35 printf(
36 "dnat options:\n"
37 " --to-dst address : MAC address to map destination to\n"
38 " --dnat-target target : ACCEPT, DROP, RETURN or CONTINUE\n"
39 " (standard target is ACCEPT)\n");
40 }
41
brdnat_init(struct xt_entry_target * target)42 static void brdnat_init(struct xt_entry_target *target)
43 {
44 struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
45
46 natinfo->target = EBT_ACCEPT;
47 }
48
brdnat_parse(struct xt_option_call * cb)49 static void brdnat_parse(struct xt_option_call *cb)
50 {
51 struct ebt_nat_info *natinfo = cb->data;
52
53 xtables_option_parse(cb);
54 if (cb->entry->id == O_TARGET &&
55 ebt_fill_target(cb->arg, (unsigned int *)&natinfo->target))
56 xtables_error(PARAMETER_PROBLEM,
57 "Illegal --dnat-target target");
58 }
59
brdnat_final_check(struct xt_fcheck_call * fc)60 static void brdnat_final_check(struct xt_fcheck_call *fc)
61 {
62 if (!fc->xflags)
63 xtables_error(PARAMETER_PROBLEM,
64 "You must specify proper arguments");
65 }
66
brdnat_print(const void * ip,const struct xt_entry_target * target,int numeric)67 static void brdnat_print(const void *ip, const struct xt_entry_target *target, int numeric)
68 {
69 struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
70
71 printf("--to-dst ");
72 xtables_print_mac(natinfo->mac);
73 printf(" --dnat-target %s", ebt_target_name(natinfo->target));
74 }
75
brdnat_verdict(int verdict)76 static const char* brdnat_verdict(int verdict)
77 {
78 switch (verdict) {
79 case EBT_ACCEPT: return "accept";
80 case EBT_DROP: return "drop";
81 case EBT_CONTINUE: return "continue";
82 case EBT_RETURN: return "return";
83 }
84
85 return "";
86 }
87
brdnat_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)88 static int brdnat_xlate(struct xt_xlate *xl,
89 const struct xt_xlate_tg_params *params)
90 {
91 const struct ebt_nat_info *natinfo = (const void*)params->target->data;
92
93 xt_xlate_add(xl, "ether daddr set %s %s ",
94 ether_ntoa((struct ether_addr *)natinfo->mac),
95 brdnat_verdict(natinfo->target));
96
97 return 1;
98 }
99
100 static struct xtables_target brdnat_target =
101 {
102 .name = "dnat",
103 .version = XTABLES_VERSION,
104 .family = NFPROTO_BRIDGE,
105 .size = XT_ALIGN(sizeof(struct ebt_nat_info)),
106 .userspacesize = XT_ALIGN(sizeof(struct ebt_nat_info)),
107 .help = brdnat_print_help,
108 .init = brdnat_init,
109 .x6_parse = brdnat_parse,
110 .x6_fcheck = brdnat_final_check,
111 .print = brdnat_print,
112 .xlate = brdnat_xlate,
113 .x6_options = brdnat_opts,
114 };
115
_init(void)116 void _init(void)
117 {
118 xtables_register_target(&brdnat_target);
119 }
120