1 #include <stdio.h>
2 #include <netdb.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <xtables.h>
7 #include <limits.h> /* INT_MAX in ip_tables.h */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter/nf_nat.h>
10
11 enum {
12 O_TO_PORTS = 0,
13 O_RANDOM,
14 O_RANDOM_FULLY,
15 };
16
MASQUERADE_help(void)17 static void MASQUERADE_help(void)
18 {
19 printf(
20 "MASQUERADE target options:\n"
21 " --to-ports <port>[-<port>]\n"
22 " Port (range) to map to.\n"
23 " --random\n"
24 " Randomize source port.\n"
25 " --random-fully\n"
26 " Fully randomize source port.\n");
27 }
28
29 static const struct xt_option_entry MASQUERADE_opts[] = {
30 {.name = "to-ports", .id = O_TO_PORTS, .type = XTTYPE_STRING},
31 {.name = "random", .id = O_RANDOM, .type = XTTYPE_NONE},
32 {.name = "random-fully", .id = O_RANDOM_FULLY, .type = XTTYPE_NONE},
33 XTOPT_TABLEEND,
34 };
35
MASQUERADE_init(struct xt_entry_target * t)36 static void MASQUERADE_init(struct xt_entry_target *t)
37 {
38 struct nf_nat_ipv4_multi_range_compat *mr = (struct nf_nat_ipv4_multi_range_compat *)t->data;
39
40 /* Actually, it's 0, but it's ignored at the moment. */
41 mr->rangesize = 1;
42 }
43
44 /* Parses ports */
45 static void
parse_ports(const char * arg,struct nf_nat_ipv4_multi_range_compat * mr)46 parse_ports(const char *arg, struct nf_nat_ipv4_multi_range_compat *mr)
47 {
48 char *end;
49 unsigned int port, maxport;
50
51 mr->range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
52
53 if (!xtables_strtoui(arg, &end, &port, 0, UINT16_MAX))
54 xtables_param_act(XTF_BAD_VALUE, "MASQUERADE", "--to-ports", arg);
55
56 switch (*end) {
57 case '\0':
58 mr->range[0].min.tcp.port
59 = mr->range[0].max.tcp.port
60 = htons(port);
61 return;
62 case '-':
63 if (!xtables_strtoui(end + 1, NULL, &maxport, 0, UINT16_MAX))
64 break;
65
66 if (maxport < port)
67 break;
68
69 mr->range[0].min.tcp.port = htons(port);
70 mr->range[0].max.tcp.port = htons(maxport);
71 return;
72 default:
73 break;
74 }
75 xtables_param_act(XTF_BAD_VALUE, "MASQUERADE", "--to-ports", arg);
76 }
77
MASQUERADE_parse(struct xt_option_call * cb)78 static void MASQUERADE_parse(struct xt_option_call *cb)
79 {
80 const struct ipt_entry *entry = cb->xt_entry;
81 int portok;
82 struct nf_nat_ipv4_multi_range_compat *mr = cb->data;
83
84 if (entry->ip.proto == IPPROTO_TCP
85 || entry->ip.proto == IPPROTO_UDP
86 || entry->ip.proto == IPPROTO_SCTP
87 || entry->ip.proto == IPPROTO_DCCP
88 || entry->ip.proto == IPPROTO_ICMP)
89 portok = 1;
90 else
91 portok = 0;
92
93 xtables_option_parse(cb);
94 switch (cb->entry->id) {
95 case O_TO_PORTS:
96 if (!portok)
97 xtables_error(PARAMETER_PROBLEM,
98 "Need TCP, UDP, SCTP or DCCP with port specification");
99 parse_ports(cb->arg, mr);
100 break;
101 case O_RANDOM:
102 mr->range[0].flags |= NF_NAT_RANGE_PROTO_RANDOM;
103 break;
104 case O_RANDOM_FULLY:
105 mr->range[0].flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
106 break;
107 }
108 }
109
110 static void
MASQUERADE_print(const void * ip,const struct xt_entry_target * target,int numeric)111 MASQUERADE_print(const void *ip, const struct xt_entry_target *target,
112 int numeric)
113 {
114 const struct nf_nat_ipv4_multi_range_compat *mr = (const void *)target->data;
115 const struct nf_nat_ipv4_range *r = &mr->range[0];
116
117 if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
118 printf(" masq ports: ");
119 printf("%hu", ntohs(r->min.tcp.port));
120 if (r->max.tcp.port != r->min.tcp.port)
121 printf("-%hu", ntohs(r->max.tcp.port));
122 }
123
124 if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
125 printf(" random");
126
127 if (r->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY)
128 printf(" random-fully");
129 }
130
131 static void
MASQUERADE_save(const void * ip,const struct xt_entry_target * target)132 MASQUERADE_save(const void *ip, const struct xt_entry_target *target)
133 {
134 const struct nf_nat_ipv4_multi_range_compat *mr = (const void *)target->data;
135 const struct nf_nat_ipv4_range *r = &mr->range[0];
136
137 if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
138 printf(" --to-ports %hu", ntohs(r->min.tcp.port));
139 if (r->max.tcp.port != r->min.tcp.port)
140 printf("-%hu", ntohs(r->max.tcp.port));
141 }
142
143 if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
144 printf(" --random");
145
146 if (r->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY)
147 printf(" --random-fully");
148 }
149
MASQUERADE_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)150 static int MASQUERADE_xlate(struct xt_xlate *xl,
151 const struct xt_xlate_tg_params *params)
152 {
153 const struct nf_nat_ipv4_multi_range_compat *mr =
154 (const void *)params->target->data;
155 const struct nf_nat_ipv4_range *r = &mr->range[0];
156
157 xt_xlate_add(xl, "masquerade");
158
159 if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
160 xt_xlate_add(xl, " to :%hu", ntohs(r->min.tcp.port));
161 if (r->max.tcp.port != r->min.tcp.port)
162 xt_xlate_add(xl, "-%hu", ntohs(r->max.tcp.port));
163 }
164
165 xt_xlate_add(xl, " ");
166 if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
167 xt_xlate_add(xl, "random ");
168
169 return 1;
170 }
171
172 static struct xtables_target masquerade_tg_reg = {
173 .name = "MASQUERADE",
174 .version = XTABLES_VERSION,
175 .family = NFPROTO_IPV4,
176 .size = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
177 .userspacesize = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
178 .help = MASQUERADE_help,
179 .init = MASQUERADE_init,
180 .x6_parse = MASQUERADE_parse,
181 .print = MASQUERADE_print,
182 .save = MASQUERADE_save,
183 .x6_options = MASQUERADE_opts,
184 .xlate = MASQUERADE_xlate,
185 };
186
_init(void)187 void _init(void)
188 {
189 xtables_register_target(&masquerade_tg_reg);
190 }
191