• 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_S '1'
20 #define NAT_S_TARGET '2'
21 #define NAT_S_ARP '3'
22 static const struct option brsnat_opts[] =
23 {
24 	{ "to-source"     , required_argument, 0, NAT_S },
25 	{ "to-src"        , required_argument, 0, NAT_S },
26 	{ "snat-target"   , required_argument, 0, NAT_S_TARGET },
27 	{ "snat-arp"      ,       no_argument, 0, NAT_S_ARP },
28 	{ 0 }
29 };
30 
brsnat_print_help(void)31 static void brsnat_print_help(void)
32 {
33 	printf(
34 	"snat options:\n"
35 	" --to-src address       : MAC address to map source to\n"
36 	" --snat-target target   : ACCEPT, DROP, RETURN or CONTINUE\n"
37 	" --snat-arp             : also change src address in arp msg\n");
38 }
39 
brsnat_init(struct xt_entry_target * target)40 static void brsnat_init(struct xt_entry_target *target)
41 {
42 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
43 
44 	natinfo->target = EBT_ACCEPT;
45 }
46 
47 #define OPT_SNAT         0x01
48 #define OPT_SNAT_TARGET  0x02
49 #define OPT_SNAT_ARP     0x04
brsnat_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_target ** target)50 static int brsnat_parse(int c, char **argv, int invert, unsigned int *flags,
51 			 const void *entry, struct xt_entry_target **target)
52 {
53 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)(*target)->data;
54 	struct ether_addr *addr;
55 
56 	switch (c) {
57 	case NAT_S:
58 		EBT_CHECK_OPTION(flags, OPT_SNAT);
59 		if (!(addr = ether_aton(optarg)))
60 			xtables_error(PARAMETER_PROBLEM, "Problem with specified --to-source mac");
61 		memcpy(natinfo->mac, addr, ETH_ALEN);
62 		break;
63 	case NAT_S_TARGET:
64 		{ unsigned int tmp;
65 		EBT_CHECK_OPTION(flags, OPT_SNAT_TARGET);
66 		if (ebt_fill_target(optarg, &tmp))
67 			xtables_error(PARAMETER_PROBLEM, "Illegal --snat-target target");
68 		natinfo->target = (natinfo->target & ~EBT_VERDICT_BITS) | (tmp & EBT_VERDICT_BITS);
69 		}
70 		break;
71 	case NAT_S_ARP:
72 		EBT_CHECK_OPTION(flags, OPT_SNAT_ARP);
73 		natinfo->target ^= NAT_ARP_BIT;
74 		break;
75 	default:
76 		return 0;
77 	}
78 	return 1;
79 }
80 
brsnat_final_check(unsigned int flags)81 static void brsnat_final_check(unsigned int flags)
82 {
83 	if (!flags)
84 		xtables_error(PARAMETER_PROBLEM,
85 			      "You must specify proper arguments");
86 }
87 
brsnat_print(const void * ip,const struct xt_entry_target * target,int numeric)88 static void brsnat_print(const void *ip, const struct xt_entry_target *target, int numeric)
89 {
90 	struct ebt_nat_info *natinfo = (struct ebt_nat_info *)target->data;
91 
92 	printf("--to-src ");
93 	xtables_print_mac(natinfo->mac);
94 	if (!(natinfo->target&NAT_ARP_BIT))
95 		printf(" --snat-arp");
96 	printf(" --snat-target %s", ebt_target_name((natinfo->target|~EBT_VERDICT_BITS)));
97 }
98 
brsnat_verdict(int verdict)99 static const char* brsnat_verdict(int verdict)
100 {
101 	switch (verdict) {
102 	case EBT_ACCEPT: return "accept";
103 	case EBT_DROP: return "drop";
104 	case EBT_CONTINUE: return "continue";
105 	case EBT_RETURN: return "return";
106 	}
107 
108 	return "";
109 }
110 
brsnat_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)111 static int brsnat_xlate(struct xt_xlate *xl,
112 			 const struct xt_xlate_tg_params *params)
113 {
114 	const struct ebt_nat_info *natinfo = (const void*)params->target->data;
115 
116 	xt_xlate_add(xl, "ether saddr set %s ",
117 		     ether_ntoa((struct ether_addr *)natinfo->mac));
118 
119 	/* NAT_ARP_BIT set -> no arp mangling, not set -> arp mangling (yes, its inverted) */
120 	if (!(natinfo->target&NAT_ARP_BIT))
121 		return 0;
122 
123 	xt_xlate_add(xl, "%s ", brsnat_verdict(natinfo->target | ~EBT_VERDICT_BITS));
124 	return 1;
125 }
126 
127 static struct xtables_target brsnat_target =
128 {
129 	.name		= "snat",
130 	.version	= XTABLES_VERSION,
131 	.family		= NFPROTO_BRIDGE,
132 	.size           = XT_ALIGN(sizeof(struct ebt_nat_info)),
133 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_nat_info)),
134 	.help		= brsnat_print_help,
135 	.init		= brsnat_init,
136 	.parse		= brsnat_parse,
137 	.final_check	= brsnat_final_check,
138 	.print		= brsnat_print,
139 	.xlate		= brsnat_xlate,
140 	.extra_opts	= brsnat_opts,
141 };
142 
_init(void)143 void _init(void)
144 {
145 	xtables_register_target(&brsnat_target);
146 }
147