libipt_DNAT.c revision ddac6c5bc636003d664d25c08ea3fe176565096c
1/* Shared library add-on to iptables to add destination-NAT 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/nf_nat.h> 10 11#define IPT_DNAT_OPT_DEST 0x1 12#define IPT_DNAT_OPT_RANDOM 0x2 13 14/* Dest NAT data consists of a multi-range, indicating where to map 15 to. */ 16struct ipt_natinfo 17{ 18 struct xt_entry_target t; 19 struct ip_nat_multi_range mr; 20}; 21 22static void DNAT_help(void) 23{ 24 printf( 25"DNAT target options:\n" 26" --to-destination <ipaddr>[-<ipaddr>][:port-port]\n" 27" Address to map destination to.\n" 28"[--random]\n"); 29} 30 31static const struct option DNAT_opts[] = { 32 { "to-destination", 1, NULL, '1' }, 33 { "random", 0, NULL, '2' }, 34 { .name = NULL } 35}; 36 37static struct ipt_natinfo * 38append_range(struct ipt_natinfo *info, const struct ip_nat_range *range) 39{ 40 unsigned int size; 41 42 /* One rangesize already in struct ipt_natinfo */ 43 size = XT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range)); 44 45 info = realloc(info, size); 46 if (!info) 47 exit_error(OTHER_PROBLEM, "Out of memory\n"); 48 49 info->t.u.target_size = size; 50 info->mr.range[info->mr.rangesize] = *range; 51 info->mr.rangesize++; 52 53 return info; 54} 55 56/* Ranges expected in network order. */ 57static struct xt_entry_target * 58parse_to(char *arg, int portok, struct ipt_natinfo *info) 59{ 60 struct ip_nat_range range; 61 char *colon, *dash, *error; 62 const struct in_addr *ip; 63 64 memset(&range, 0, sizeof(range)); 65 colon = strchr(arg, ':'); 66 67 if (colon) { 68 int port; 69 70 if (!portok) 71 exit_error(PARAMETER_PROBLEM, 72 "Need TCP or UDP with port specification"); 73 74 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED; 75 76 port = atoi(colon+1); 77 if (port <= 0 || port > 65535) 78 exit_error(PARAMETER_PROBLEM, 79 "Port `%s' not valid\n", colon+1); 80 81 error = strchr(colon+1, ':'); 82 if (error) 83 exit_error(PARAMETER_PROBLEM, 84 "Invalid port:port syntax - use dash\n"); 85 86 dash = strchr(colon, '-'); 87 if (!dash) { 88 range.min.tcp.port 89 = range.max.tcp.port 90 = htons(port); 91 } else { 92 int maxport; 93 94 maxport = atoi(dash + 1); 95 if (maxport <= 0 || maxport > 65535) 96 exit_error(PARAMETER_PROBLEM, 97 "Port `%s' not valid\n", dash+1); 98 if (maxport < port) 99 /* People are stupid. */ 100 exit_error(PARAMETER_PROBLEM, 101 "Port range `%s' funky\n", colon+1); 102 range.min.tcp.port = htons(port); 103 range.max.tcp.port = htons(maxport); 104 } 105 /* Starts with a colon? No IP info...*/ 106 if (colon == arg) 107 return &(append_range(info, &range)->t); 108 *colon = '\0'; 109 } 110 111 range.flags |= IP_NAT_RANGE_MAP_IPS; 112 dash = strchr(arg, '-'); 113 if (colon && dash && dash > colon) 114 dash = NULL; 115 116 if (dash) 117 *dash = '\0'; 118 119 ip = numeric_to_ipaddr(arg); 120 if (!ip) 121 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 122 arg); 123 range.min_ip = ip->s_addr; 124 if (dash) { 125 ip = numeric_to_ipaddr(dash+1); 126 if (!ip) 127 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 128 dash+1); 129 range.max_ip = ip->s_addr; 130 } else 131 range.max_ip = range.min_ip; 132 133 return &(append_range(info, &range)->t); 134} 135 136static int DNAT_parse(int c, char **argv, int invert, unsigned int *flags, 137 const void *e, struct xt_entry_target **target) 138{ 139 const struct ipt_entry *entry = e; 140 struct ipt_natinfo *info = (void *)*target; 141 int portok; 142 143 if (entry->ip.proto == IPPROTO_TCP 144 || entry->ip.proto == IPPROTO_UDP 145 || entry->ip.proto == IPPROTO_ICMP) 146 portok = 1; 147 else 148 portok = 0; 149 150 switch (c) { 151 case '1': 152 if (check_inverse(optarg, &invert, NULL, 0)) 153 exit_error(PARAMETER_PROBLEM, 154 "Unexpected `!' after --to-destination"); 155 156 if (*flags) { 157 if (!kernel_version) 158 get_kernel_version(); 159 if (kernel_version > LINUX_VERSION(2, 6, 10)) 160 exit_error(PARAMETER_PROBLEM, 161 "Multiple --to-destination not supported"); 162 } 163 *target = parse_to(optarg, portok, info); 164 /* WTF do we need this for?? */ 165 if (*flags & IPT_DNAT_OPT_RANDOM) 166 info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; 167 *flags |= IPT_DNAT_OPT_DEST; 168 return 1; 169 170 case '2': 171 if (*flags & IPT_DNAT_OPT_DEST) { 172 info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; 173 *flags |= IPT_DNAT_OPT_RANDOM; 174 } else 175 *flags |= IPT_DNAT_OPT_RANDOM; 176 return 1; 177 default: 178 return 0; 179 } 180} 181 182static void DNAT_check(unsigned int flags) 183{ 184 if (!flags) 185 exit_error(PARAMETER_PROBLEM, 186 "You must specify --to-destination"); 187} 188 189static void print_range(const struct ip_nat_range *r) 190{ 191 if (r->flags & IP_NAT_RANGE_MAP_IPS) { 192 struct in_addr a; 193 194 a.s_addr = r->min_ip; 195 printf("%s", ipaddr_to_numeric(&a)); 196 if (r->max_ip != r->min_ip) { 197 a.s_addr = r->max_ip; 198 printf("-%s", ipaddr_to_numeric(&a)); 199 } 200 } 201 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { 202 printf(":"); 203 printf("%hu", ntohs(r->min.tcp.port)); 204 if (r->max.tcp.port != r->min.tcp.port) 205 printf("-%hu", ntohs(r->max.tcp.port)); 206 } 207} 208 209static void DNAT_print(const void *ip, const struct xt_entry_target *target, 210 int numeric) 211{ 212 struct ipt_natinfo *info = (void *)target; 213 unsigned int i = 0; 214 215 printf("to:"); 216 for (i = 0; i < info->mr.rangesize; i++) { 217 print_range(&info->mr.range[i]); 218 printf(" "); 219 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM) 220 printf("random "); 221 } 222} 223 224static void DNAT_save(const void *ip, const struct xt_entry_target *target) 225{ 226 struct ipt_natinfo *info = (void *)target; 227 unsigned int i = 0; 228 229 for (i = 0; i < info->mr.rangesize; i++) { 230 printf("--to-destination "); 231 print_range(&info->mr.range[i]); 232 printf(" "); 233 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM) 234 printf("--random "); 235 } 236} 237 238static struct xtables_target dnat_tg_reg = { 239 .name = "DNAT", 240 .version = XTABLES_VERSION, 241 .family = PF_INET, 242 .size = XT_ALIGN(sizeof(struct ip_nat_multi_range)), 243 .userspacesize = XT_ALIGN(sizeof(struct ip_nat_multi_range)), 244 .help = DNAT_help, 245 .parse = DNAT_parse, 246 .final_check = DNAT_check, 247 .print = DNAT_print, 248 .save = DNAT_save, 249 .extra_opts = DNAT_opts, 250}; 251 252void _init(void) 253{ 254 xtables_register_target(&dnat_tg_reg); 255} 256