Merge to iptables-1.3.5
[iptables.git] / extensions / libipt_REDIRECT.c
1 /* Shared library add-on to iptables to add redirect support. */
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <getopt.h>
7 #include <iptables.h>
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.h>
10
11 /* Function which prints out usage message. */
12 static void
13 help(void)
14 {
15         printf(
16 "REDIRECT v%s options:\n"
17 " --to-ports <port>[-<port>]\n"
18 "                               Port (range) to map to.\n\n",
19 IPTABLES_VERSION);
20 }
21
22 static struct option opts[] = {
23         { "to-ports", 1, 0, '1' },
24         { 0 }
25 };
26
27 /* Initialize the target. */
28 static void
29 init(struct ipt_entry_target *t, unsigned int *nfcache)
30 {
31         struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
32
33         /* Actually, it's 0, but it's ignored at the moment. */
34         mr->rangesize = 1;
35
36 }
37
38 /* Parses ports */
39 static void
40 parse_ports(const char *arg, struct ip_nat_multi_range *mr)
41 {
42         const char *dash;
43         int port;
44
45         mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
46
47         port = atoi(arg);
48         if (port == 0 || port > 65535)
49                 exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg);
50
51         dash = strchr(arg, '-');
52         if (!dash) {
53                 mr->range[0].min.tcp.port
54                         = mr->range[0].max.tcp.port
55                         = htons(port);
56         } else {
57                 int maxport;
58
59                 maxport = atoi(dash + 1);
60                 if (maxport == 0 || maxport > 65535)
61                         exit_error(PARAMETER_PROBLEM,
62                                    "Port `%s' not valid\n", dash+1);
63                 if (maxport < port)
64                         /* People are stupid. */
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);
69         }
70 }
71
72 /* Function which parses command options; returns true if it
73    ate an option */
74 static int
75 parse(int c, char **argv, int invert, unsigned int *flags,
76       const struct ipt_entry *entry,
77       struct ipt_entry_target **target)
78 {
79         struct ip_nat_multi_range *mr
80                 = (struct ip_nat_multi_range *)(*target)->data;
81         int portok;
82
83         if (entry->ip.proto == IPPROTO_TCP
84             || entry->ip.proto == IPPROTO_UDP
85             || entry->ip.proto == IPPROTO_ICMP)
86                 portok = 1;
87         else
88                 portok = 0;
89
90         switch (c) {
91         case '1':
92                 if (!portok)
93                         exit_error(PARAMETER_PROBLEM,
94                                    "Need TCP or UDP with port specification");
95
96                 if (check_inverse(optarg, &invert, NULL, 0))
97                         exit_error(PARAMETER_PROBLEM,
98                                    "Unexpected `!' after --to-ports");
99
100                 parse_ports(optarg, mr);
101                 return 1;
102
103         default:
104                 return 0;
105         }
106 }
107
108 /* Final check; don't care. */
109 static void final_check(unsigned int flags)
110 {
111 }
112
113 /* Prints out the targinfo. */
114 static void
115 print(const struct ipt_ip *ip,
116       const struct ipt_entry_target *target,
117       int numeric)
118 {
119         struct ip_nat_multi_range *mr
120                 = (struct ip_nat_multi_range *)target->data;
121         struct ip_nat_range *r = &mr->range[0];
122
123         if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
124                 printf("redir 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));
128                 printf(" ");
129         }
130 }
131
132 /* Saves the union ipt_targinfo in parsable form to stdout. */
133 static void
134 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
135 {
136         struct ip_nat_multi_range *mr
137                 = (struct ip_nat_multi_range *)target->data;
138         struct ip_nat_range *r = &mr->range[0];
139
140         if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
141                 printf("--to-ports ");
142                 printf("%hu", ntohs(r->min.tcp.port));
143                 if (r->max.tcp.port != r->min.tcp.port)
144                         printf("-%hu", ntohs(r->max.tcp.port));
145                 printf(" ");
146         }
147 }
148
149 static struct iptables_target redir = { 
150         .next           = NULL,
151         .name           = "REDIRECT",
152         .version        = IPTABLES_VERSION,
153         .size           = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
154         .userspacesize  = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
155         .help           = &help,
156         .init           = &init,
157         .parse          = &parse,
158         .final_check    = &final_check,
159         .print          = &print,
160         .save           = &save,
161         .extra_opts     = opts
162 };
163
164 void _init(void)
165 {
166         register_target(&redir);
167 }