1 /* Shared library add-on to iptables to add masquerade support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter/nf_nat.h>
11 /* Function which prints out usage message. */
16 "MASQUERADE v%s options:\n"
17 " --to-ports <port>[-<port>]\n"
18 " Port (range) to map to.\n"
20 " Randomize source port.\n"
26 static struct option opts[] = {
27 { "to-ports", 1, 0, '1' },
28 { "random", 0, 0, '2' },
32 /* Initialize the target. */
34 init(struct ipt_entry_target *t, unsigned int *nfcache)
36 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
38 /* Actually, it's 0, but it's ignored at the moment. */
45 parse_ports(const char *arg, struct ip_nat_multi_range *mr)
50 mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
53 if (port <= 0 || port > 65535)
54 exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg);
56 dash = strchr(arg, '-');
58 mr->range[0].min.tcp.port
59 = mr->range[0].max.tcp.port
64 maxport = atoi(dash + 1);
65 if (maxport == 0 || maxport > 65535)
66 exit_error(PARAMETER_PROBLEM,
67 "Port `%s' not valid\n", dash+1);
69 /* People are stupid. Present reader excepted. */
70 exit_error(PARAMETER_PROBLEM,
71 "Port range `%s' funky\n", arg);
72 mr->range[0].min.tcp.port = htons(port);
73 mr->range[0].max.tcp.port = htons(maxport);
77 /* Function which parses command options; returns true if it
80 parse(int c, char **argv, int invert, unsigned int *flags,
81 const struct ipt_entry *entry,
82 struct ipt_entry_target **target)
85 struct ip_nat_multi_range *mr
86 = (struct ip_nat_multi_range *)(*target)->data;
88 if (entry->ip.proto == IPPROTO_TCP
89 || entry->ip.proto == IPPROTO_UDP
90 || entry->ip.proto == IPPROTO_ICMP)
98 exit_error(PARAMETER_PROBLEM,
99 "Need TCP or UDP with port specification");
101 if (check_inverse(optarg, &invert, NULL, 0))
102 exit_error(PARAMETER_PROBLEM,
103 "Unexpected `!' after --to-ports");
105 parse_ports(optarg, mr);
109 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
117 /* Final check; don't care. */
118 static void final_check(unsigned int flags)
122 /* Prints out the targinfo. */
124 print(const struct ipt_ip *ip,
125 const struct ipt_entry_target *target,
128 struct ip_nat_multi_range *mr
129 = (struct ip_nat_multi_range *)target->data;
130 struct ip_nat_range *r = &mr->range[0];
132 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
133 printf("masq ports: ");
134 printf("%hu", ntohs(r->min.tcp.port));
135 if (r->max.tcp.port != r->min.tcp.port)
136 printf("-%hu", ntohs(r->max.tcp.port));
140 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
144 /* Saves the union ipt_targinfo in parsable form to stdout. */
146 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
148 struct ip_nat_multi_range *mr
149 = (struct ip_nat_multi_range *)target->data;
150 struct ip_nat_range *r = &mr->range[0];
152 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
153 printf("--to-ports %hu", ntohs(r->min.tcp.port));
154 if (r->max.tcp.port != r->min.tcp.port)
155 printf("-%hu", ntohs(r->max.tcp.port));
159 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
163 static struct iptables_target masq = { NULL,
164 .name = "MASQUERADE",
165 .version = IPTABLES_VERSION,
166 .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
167 .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
171 .final_check = &final_check,
179 register_target(&masq);