blob: e30c139248a06a16ba2ad013039ccb2b9ff7417b [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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>
Patrick McHardy40d54752007-04-18 07:00:36 +00009#include <linux/netfilter/nf_nat.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000010
Patrick McHardyef399a32007-05-29 11:24:45 +000011#define IPT_REDIRECT_OPT_DEST 0x01
12#define IPT_REDIRECT_OPT_RANDOM 0x02
13
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000014static void REDIRECT_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000015{
16 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020017"REDIRECT target options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000018" --to-ports <port>[-<port>]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020019" Port (range) to map to.\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000020}
21
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000022static const struct option REDIRECT_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000023 { "to-ports", 1, NULL, '1' },
Tom Eastep75b4b202007-11-15 13:06:58 +000024 { "random", 0, NULL, '2' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000025 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000026};
27
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000028static void REDIRECT_init(struct xt_entry_target *t)
Marc Bouchere6869a82000-03-20 06:03:29 +000029{
30 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
31
32 /* Actually, it's 0, but it's ignored at the moment. */
33 mr->rangesize = 1;
34
Marc Bouchere6869a82000-03-20 06:03:29 +000035}
36
37/* Parses ports */
38static void
39parse_ports(const char *arg, struct ip_nat_multi_range *mr)
40{
41 const char *dash;
42 int port;
43
44 mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
45
Phil Oester3836fcc2006-06-20 13:45:38 +000046 if (strchr(arg, '.'))
47 exit_error(PARAMETER_PROBLEM, "IP address not permitted\n");
48
Marc Bouchere6869a82000-03-20 06:03:29 +000049 port = atoi(arg);
Kristof Provostccecd382008-05-26 00:55:34 +020050 if (port == 0)
51 port = service_to_port(arg, NULL);
52
Marc Bouchere6869a82000-03-20 06:03:29 +000053 if (port == 0 || port > 65535)
54 exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg);
55
56 dash = strchr(arg, '-');
57 if (!dash) {
58 mr->range[0].min.tcp.port
59 = mr->range[0].max.tcp.port
Rusty Russellf9b2e662000-04-19 11:24:02 +000060 = htons(port);
Marc Bouchere6869a82000-03-20 06:03:29 +000061 } else {
62 int maxport;
63
64 maxport = atoi(dash + 1);
65 if (maxport == 0 || maxport > 65535)
66 exit_error(PARAMETER_PROBLEM,
67 "Port `%s' not valid\n", dash+1);
68 if (maxport < port)
69 /* People are stupid. */
70 exit_error(PARAMETER_PROBLEM,
71 "Port range `%s' funky\n", arg);
Rusty Russellf9b2e662000-04-19 11:24:02 +000072 mr->range[0].min.tcp.port = htons(port);
73 mr->range[0].max.tcp.port = htons(maxport);
Marc Bouchere6869a82000-03-20 06:03:29 +000074 }
75}
76
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000077static int REDIRECT_parse(int c, char **argv, int invert, unsigned int *flags,
78 const void *e, struct xt_entry_target **target)
Marc Bouchere6869a82000-03-20 06:03:29 +000079{
Yasuyuki KOZAKAIac8b2712007-07-24 06:06:59 +000080 const struct ipt_entry *entry = e;
Marc Bouchere6869a82000-03-20 06:03:29 +000081 struct ip_nat_multi_range *mr
82 = (struct ip_nat_multi_range *)(*target)->data;
83 int portok;
84
85 if (entry->ip.proto == IPPROTO_TCP
Patrick McHardy36d870c2005-07-22 06:39:45 +000086 || entry->ip.proto == IPPROTO_UDP
Patrick McHardy5a942f92008-11-04 13:22:40 +010087 || entry->ip.proto == IPPROTO_SCTP
88 || entry->ip.proto == IPPROTO_DCCP
Patrick McHardy36d870c2005-07-22 06:39:45 +000089 || entry->ip.proto == IPPROTO_ICMP)
Marc Bouchere6869a82000-03-20 06:03:29 +000090 portok = 1;
91 else
92 portok = 0;
93
94 switch (c) {
95 case '1':
96 if (!portok)
97 exit_error(PARAMETER_PROBLEM,
Patrick McHardy5a942f92008-11-04 13:22:40 +010098 "Need TCP, UDP, SCTP or DCCP with port specification");
Marc Bouchere6869a82000-03-20 06:03:29 +000099
Harald Welteb77f1da2002-03-14 11:35:58 +0000100 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000101 exit_error(PARAMETER_PROBLEM,
102 "Unexpected `!' after --to-ports");
103
104 parse_ports(optarg, mr);
Patrick McHardyef399a32007-05-29 11:24:45 +0000105 if (*flags & IPT_REDIRECT_OPT_RANDOM)
106 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
107 *flags |= IPT_REDIRECT_OPT_DEST;
108 return 1;
109
110 case '2':
111 if (*flags & IPT_REDIRECT_OPT_DEST) {
112 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
113 *flags |= IPT_REDIRECT_OPT_RANDOM;
114 } else
115 *flags |= IPT_REDIRECT_OPT_RANDOM;
Marc Bouchere6869a82000-03-20 06:03:29 +0000116 return 1;
117
118 default:
119 return 0;
120 }
121}
122
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000123static void REDIRECT_print(const void *ip, const struct xt_entry_target *target,
124 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000125{
126 struct ip_nat_multi_range *mr
127 = (struct ip_nat_multi_range *)target->data;
128 struct ip_nat_range *r = &mr->range[0];
129
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
Rusty Russell849779c2000-04-23 15:51:51 +0000131 printf("redir ports ");
Rusty Russellf9b2e662000-04-19 11:24:02 +0000132 printf("%hu", ntohs(r->min.tcp.port));
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 if (r->max.tcp.port != r->min.tcp.port)
Rusty Russellf9b2e662000-04-19 11:24:02 +0000134 printf("-%hu", ntohs(r->max.tcp.port));
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 printf(" ");
Patrick McHardyef399a32007-05-29 11:24:45 +0000136 if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM)
137 printf("random ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 }
139}
140
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000141static void REDIRECT_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000142{
143 struct ip_nat_multi_range *mr
144 = (struct ip_nat_multi_range *)target->data;
145 struct ip_nat_range *r = &mr->range[0];
146
147 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
Harald Welte963bdcc2001-03-16 20:37:10 +0000148 printf("--to-ports ");
Rusty Russellf9b2e662000-04-19 11:24:02 +0000149 printf("%hu", ntohs(r->min.tcp.port));
Marc Bouchere6869a82000-03-20 06:03:29 +0000150 if (r->max.tcp.port != r->min.tcp.port)
Rusty Russellf9b2e662000-04-19 11:24:02 +0000151 printf("-%hu", ntohs(r->max.tcp.port));
Marc Bouchere6869a82000-03-20 06:03:29 +0000152 printf(" ");
Patrick McHardyef399a32007-05-29 11:24:45 +0000153 if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM)
154 printf("--random ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000155 }
156}
157
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200158static struct xtables_target redirect_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000159 .name = "REDIRECT",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200160 .version = XTABLES_VERSION,
161 .family = PF_INET,
162 .size = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
163 .userspacesize = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000164 .help = REDIRECT_help,
165 .init = REDIRECT_init,
166 .parse = REDIRECT_parse,
167 .print = REDIRECT_print,
168 .save = REDIRECT_save,
169 .extra_opts = REDIRECT_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000170};
171
172void _init(void)
173{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200174 xtables_register_target(&redirect_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000175}