• 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 <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_SRC,
20 	O_TARGET,
21 	O_ARP,
22 };
23 
24 static const struct xt_option_entry brsnat_opts[] =
25 {
26 	{ .name = "to-source", .id = O_SRC, .type = XTTYPE_ETHERMAC,
27 	  .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_nat_info, mac) },
28 	{ .name = "to-src",    .id = O_SRC, .type = XTTYPE_ETHERMAC,
29 	  .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_nat_info, mac) },
30 	{ .name = "snat-target", .id = O_TARGET, .type = XTTYPE_STRING },
31 	{ .name = "snat-arp", .id = O_ARP, .type = XTTYPE_NONE },
32 	XTOPT_TABLEEND,
33 };
34 
brsnat_print_help(void)35 static void brsnat_print_help(void)
36 {
37 	printf(
38 	"snat options:\n"
39 	" --to-src address       : MAC address to map source to\n"
40 	" --snat-target target   : ACCEPT, DROP, RETURN or CONTINUE\n"
41 	" --snat-arp             : also change src address in arp msg\n");
42 }
43 
brsnat_init(struct xt_entry_target * target)44 static void brsnat_init(struct xt_entry_target *target)
45 {
46 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
47 
48 	natinfo->target = EBT_ACCEPT;
49 }
50 
brsnat_parse(struct xt_option_call * cb)51 static void brsnat_parse(struct xt_option_call *cb)
52 {
53 	struct ebt_nat_info *natinfo = cb->data;
54 	unsigned int tmp;
55 
56 	xtables_option_parse(cb);
57 	switch (cb->entry->id) {
58 	case O_TARGET:
59 		if (ebt_fill_target(cb->arg, &tmp))
60 			xtables_error(PARAMETER_PROBLEM,
61 				      "Illegal --snat-target target");
62 		natinfo->target &= ~EBT_VERDICT_BITS;
63 		natinfo->target |= tmp & EBT_VERDICT_BITS;
64 		break;
65 	case O_ARP:
66 		natinfo->target ^= NAT_ARP_BIT;
67 		break;
68 	}
69 }
70 
brsnat_final_check(struct xt_fcheck_call * fc)71 static void brsnat_final_check(struct xt_fcheck_call *fc)
72 {
73 	if (!fc->xflags)
74 		xtables_error(PARAMETER_PROBLEM,
75 			      "You must specify proper arguments");
76 }
77 
brsnat_print(const void * ip,const struct xt_entry_target * target,int numeric)78 static void brsnat_print(const void *ip, const struct xt_entry_target *target, int numeric)
79 {
80 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
81 
82 	printf("--to-src ");
83 	xtables_print_mac(natinfo->mac);
84 	if (!(natinfo->target&NAT_ARP_BIT))
85 		printf(" --snat-arp");
86 	printf(" --snat-target %s", ebt_target_name((natinfo->target|~EBT_VERDICT_BITS)));
87 }
88 
brsnat_verdict(int verdict)89 static const char* brsnat_verdict(int verdict)
90 {
91 	switch (verdict) {
92 	case EBT_ACCEPT: return "accept";
93 	case EBT_DROP: return "drop";
94 	case EBT_CONTINUE: return "continue";
95 	case EBT_RETURN: return "return";
96 	}
97 
98 	return "";
99 }
100 
brsnat_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)101 static int brsnat_xlate(struct xt_xlate *xl,
102 			 const struct xt_xlate_tg_params *params)
103 {
104 	const struct ebt_nat_info *natinfo = (const void*)params->target->data;
105 
106 	xt_xlate_add(xl, "ether saddr set %s ",
107 		     ether_ntoa((struct ether_addr *)natinfo->mac));
108 
109 	/* NAT_ARP_BIT set -> no arp mangling, not set -> arp mangling (yes, its inverted) */
110 	if (!(natinfo->target&NAT_ARP_BIT))
111 		return 0;
112 
113 	xt_xlate_add(xl, "%s ", brsnat_verdict(natinfo->target | ~EBT_VERDICT_BITS));
114 	return 1;
115 }
116 
117 static struct xtables_target brsnat_target =
118 {
119 	.name		= "snat",
120 	.version	= XTABLES_VERSION,
121 	.family		= NFPROTO_BRIDGE,
122 	.size           = XT_ALIGN(sizeof(struct ebt_nat_info)),
123 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_nat_info)),
124 	.help		= brsnat_print_help,
125 	.init		= brsnat_init,
126 	.x6_parse	= brsnat_parse,
127 	.x6_fcheck	= brsnat_final_check,
128 	.print		= brsnat_print,
129 	.xlate		= brsnat_xlate,
130 	.x6_options	= brsnat_opts,
131 };
132 
_init(void)133 void _init(void)
134 {
135 	xtables_register_target(&brsnat_target);
136 }
137