1 /* Shared library add-on to iptables to add masquerade support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.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\n",
22 static struct option opts[] = {
23 { "to-ports", 1, 0, '1' },
27 /* Initialize the target. */
29 init(struct ipt_entry_target *t, unsigned int *nfcache)
31 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
33 /* Actually, it's 0, but it's ignored at the moment. */
40 parse_ports(const char *arg, struct ip_nat_multi_range *mr)
45 mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
48 if (port <= 0 || port > 65535)
49 exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg);
51 dash = strchr(arg, '-');
53 mr->range[0].min.tcp.port
54 = mr->range[0].max.tcp.port
59 maxport = atoi(dash + 1);
60 if (maxport == 0 || maxport > 65535)
61 exit_error(PARAMETER_PROBLEM,
62 "Port `%s' not valid\n", dash+1);
64 /* People are stupid. Present reader excepted. */
65 exit_error(PARAMETER_PROBLEM,
66 "Port range `%s' funky\n", arg);
67 mr->range[0].min.tcp.port = htons(port);
68 mr->range[0].max.tcp.port = htons(maxport);
72 /* Function which parses command options; returns true if it
75 parse(int c, char **argv, int invert, unsigned int *flags,
76 const struct ipt_entry *entry,
77 struct ipt_entry_target **target)
80 struct ip_nat_multi_range *mr
81 = (struct ip_nat_multi_range *)(*target)->data;
83 if (entry->ip.proto == IPPROTO_TCP
84 || entry->ip.proto == IPPROTO_UDP
85 || entry->ip.proto == IPPROTO_ICMP)
93 exit_error(PARAMETER_PROBLEM,
94 "Need TCP or UDP with port specification");
96 if (check_inverse(optarg, &invert, NULL, 0))
97 exit_error(PARAMETER_PROBLEM,
98 "Unexpected `!' after --to-ports");
100 parse_ports(optarg, mr);
108 /* Final check; don't care. */
109 static void final_check(unsigned int flags)
113 /* Prints out the targinfo. */
115 print(const struct ipt_ip *ip,
116 const struct ipt_entry_target *target,
119 struct ip_nat_multi_range *mr
120 = (struct ip_nat_multi_range *)target->data;
121 struct ip_nat_range *r = &mr->range[0];
123 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
124 printf("masq ports: ");
125 printf("%hu", ntohs(r->min.tcp.port));
126 if (r->max.tcp.port != r->min.tcp.port)
127 printf("-%hu", ntohs(r->max.tcp.port));
132 /* Saves the union ipt_targinfo in parsable form to stdout. */
134 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
136 struct ip_nat_multi_range *mr
137 = (struct ip_nat_multi_range *)target->data;
138 struct ip_nat_range *r = &mr->range[0];
140 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
141 printf("--to-ports %hu", ntohs(r->min.tcp.port));
142 if (r->max.tcp.port != r->min.tcp.port)
143 printf("-%hu", ntohs(r->max.tcp.port));
148 static struct iptables_target masq = { NULL,
149 .name = "MASQUERADE",
150 .version = IPTABLES_VERSION,
151 .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
152 .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
156 .final_check = &final_check,
164 register_target(&masq);