blob: 82c22307d05713c542cd9fc7f704cda66650beb7 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Shared library add-on to iptables to add masquerade support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <xtables.h>
8#include <limits.h> /* INT_MAX in ip_tables.h */
9#include <linux/netfilter_ipv4/ip_tables.h>
10#include <net/netfilter/nf_nat.h>
11
12static void MASQUERADE_help(void)
13{
14 printf(
15"MASQUERADE target options:\n"
16" --to-ports <port>[-<port>]\n"
17" Port (range) to map to.\n"
18" --random\n"
19" Randomize source port.\n");
20}
21
22static const struct option MASQUERADE_opts[] = {
23 { "to-ports", 1, NULL, '1' },
24 { "random", 0, NULL, '2' },
25 { .name = NULL }
26};
27
28static void MASQUERADE_init(struct xt_entry_target *t)
29{
30 struct nf_nat_multi_range *mr = (struct nf_nat_multi_range *)t->data;
31
32 /* Actually, it's 0, but it's ignored at the moment. */
33 mr->rangesize = 1;
34
35}
36
37/* Parses ports */
38static void
39parse_ports(const char *arg, struct nf_nat_multi_range *mr)
40{
41 const char *dash;
42 int port;
43
44 mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
45
46 port = atoi(arg);
47 if (port <= 0 || port > 65535)
48 xtables_error(PARAMETER_PROBLEM, "Port \"%s\" not valid\n", arg);
49
50 dash = strchr(arg, '-');
51 if (!dash) {
52 mr->range[0].min.tcp.port
53 = mr->range[0].max.tcp.port
54 = htons(port);
55 } else {
56 int maxport;
57
58 maxport = atoi(dash + 1);
59 if (maxport == 0 || maxport > 65535)
60 xtables_error(PARAMETER_PROBLEM,
61 "Port `%s' not valid\n", dash+1);
62 if (maxport < port)
63 /* People are stupid. Present reader excepted. */
64 xtables_error(PARAMETER_PROBLEM,
65 "Port range `%s' funky\n", arg);
66 mr->range[0].min.tcp.port = htons(port);
67 mr->range[0].max.tcp.port = htons(maxport);
68 }
69}
70
71static int MASQUERADE_parse(int c, char **argv, int invert, unsigned int *flags,
72 const void *e, struct xt_entry_target **target)
73{
74 const struct ipt_entry *entry = e;
75 int portok;
76 struct nf_nat_multi_range *mr
77 = (struct nf_nat_multi_range *)(*target)->data;
78
79 if (entry->ip.proto == IPPROTO_TCP
80 || entry->ip.proto == IPPROTO_UDP
81 || entry->ip.proto == IPPROTO_SCTP
82 || entry->ip.proto == IPPROTO_DCCP
83 || entry->ip.proto == IPPROTO_ICMP)
84 portok = 1;
85 else
86 portok = 0;
87
88 switch (c) {
89 case '1':
90 if (!portok)
91 xtables_error(PARAMETER_PROBLEM,
92 "Need TCP, UDP, SCTP or DCCP with port specification");
93
94 if (xtables_check_inverse(optarg, &invert, NULL, 0))
95 xtables_error(PARAMETER_PROBLEM,
96 "Unexpected `!' after --to-ports");
97
98 parse_ports(optarg, mr);
99 return 1;
100
101 case '2':
102 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
103 return 1;
104
105 default:
106 return 0;
107 }
108}
109
110static void
111MASQUERADE_print(const void *ip, const struct xt_entry_target *target,
112 int numeric)
113{
114 struct nf_nat_multi_range *mr
115 = (struct nf_nat_multi_range *)target->data;
116 struct nf_nat_range *r = &mr->range[0];
117
118 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
119 printf("masq ports: ");
120 printf("%hu", ntohs(r->min.tcp.port));
121 if (r->max.tcp.port != r->min.tcp.port)
122 printf("-%hu", ntohs(r->max.tcp.port));
123 printf(" ");
124 }
125
126 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
127 printf("random ");
128}
129
130static void
131MASQUERADE_save(const void *ip, const struct xt_entry_target *target)
132{
133 struct nf_nat_multi_range *mr
134 = (struct nf_nat_multi_range *)target->data;
135 struct nf_nat_range *r = &mr->range[0];
136
137 if (r->flags & IP_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 printf(" ");
142 }
143
144 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
145 printf("--random ");
146}
147
148static struct xtables_target masquerade_tg_reg = {
149 .name = "MASQUERADE",
150 .version = XTABLES_VERSION,
151 .family = NFPROTO_IPV4,
152 .size = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
153 .userspacesize = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
154 .help = MASQUERADE_help,
155 .init = MASQUERADE_init,
156 .parse = MASQUERADE_parse,
157 .print = MASQUERADE_print,
158 .save = MASQUERADE_save,
159 .extra_opts = MASQUERADE_opts,
160};
161
162void _init(void)
163{
164 xtables_register_target(&masquerade_tg_reg);
165}