libipt_SAME.c revision 5d9678ad3eabc34ac40dfe055d7f6a8e44445a5a
1/* Shared library add-on to iptables to add simple non load-balancing SNAT support. */ 2#include <stdio.h> 3#include <netdb.h> 4#include <string.h> 5#include <stdlib.h> 6#include <getopt.h> 7#include <xtables.h> 8#include <linux/netfilter_ipv4/ip_tables.h> 9#include <linux/netfilter/nf_nat.h> 10/* For 64bit kernel / 32bit userspace */ 11#include <linux/netfilter_ipv4/ipt_SAME.h> 12 13static void SAME_help(void) 14{ 15 printf( 16"SAME target options:\n" 17" --to <ipaddr>-<ipaddr>\n" 18" Addresses to map source to.\n" 19" May be specified more than\n" 20" once for multiple ranges.\n" 21" --nodst\n" 22" Don't use destination-ip in\n" 23" source selection\n" 24" --random\n" 25" Randomize source port\n"); 26} 27 28static const struct option SAME_opts[] = { 29 { "to", 1, NULL, '1' }, 30 { "nodst", 0, NULL, '2'}, 31 { "random", 0, NULL, '3' }, 32 { .name = NULL } 33}; 34 35static void SAME_init(struct xt_entry_target *t) 36{ 37 struct ipt_same_info *mr = (struct ipt_same_info *)t->data; 38 39 /* Set default to 0 */ 40 mr->rangesize = 0; 41 mr->info = 0; 42 mr->ipnum = 0; 43 44} 45 46/* Parses range of IPs */ 47static void 48parse_to(char *arg, struct ip_nat_range *range) 49{ 50 char *dash; 51 const struct in_addr *ip; 52 53 range->flags |= IP_NAT_RANGE_MAP_IPS; 54 dash = strchr(arg, '-'); 55 56 if (dash) 57 *dash = '\0'; 58 59 ip = numeric_to_ipaddr(arg); 60 if (!ip) 61 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 62 arg); 63 range->min_ip = ip->s_addr; 64 65 if (dash) { 66 ip = numeric_to_ipaddr(dash+1); 67 if (!ip) 68 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 69 dash+1); 70 } 71 range->max_ip = ip->s_addr; 72 if (dash) 73 if (range->min_ip > range->max_ip) 74 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n", 75 arg, dash+1); 76} 77 78#define IPT_SAME_OPT_TO 0x01 79#define IPT_SAME_OPT_NODST 0x02 80#define IPT_SAME_OPT_RANDOM 0x04 81 82static int SAME_parse(int c, char **argv, int invert, unsigned int *flags, 83 const void *entry, struct xt_entry_target **target) 84{ 85 struct ipt_same_info *mr 86 = (struct ipt_same_info *)(*target)->data; 87 unsigned int count; 88 89 switch (c) { 90 case '1': 91 if (mr->rangesize == IPT_SAME_MAX_RANGE) 92 exit_error(PARAMETER_PROBLEM, 93 "Too many ranges specified, maximum " 94 "is %i ranges.\n", 95 IPT_SAME_MAX_RANGE); 96 if (check_inverse(optarg, &invert, NULL, 0)) 97 exit_error(PARAMETER_PROBLEM, 98 "Unexpected `!' after --to"); 99 100 parse_to(optarg, &mr->range[mr->rangesize]); 101 /* WTF do we need this for? */ 102 if (*flags & IPT_SAME_OPT_RANDOM) 103 mr->range[mr->rangesize].flags 104 |= IP_NAT_RANGE_PROTO_RANDOM; 105 mr->rangesize++; 106 *flags |= IPT_SAME_OPT_TO; 107 break; 108 109 case '2': 110 if (*flags & IPT_SAME_OPT_NODST) 111 exit_error(PARAMETER_PROBLEM, 112 "Can't specify --nodst twice"); 113 114 mr->info |= IPT_SAME_NODST; 115 *flags |= IPT_SAME_OPT_NODST; 116 break; 117 118 case '3': 119 *flags |= IPT_SAME_OPT_RANDOM; 120 for (count=0; count < mr->rangesize; count++) 121 mr->range[count].flags |= IP_NAT_RANGE_PROTO_RANDOM; 122 break; 123 124 default: 125 return 0; 126 } 127 128 return 1; 129} 130 131static void SAME_check(unsigned int flags) 132{ 133 if (!(flags & IPT_SAME_OPT_TO)) 134 exit_error(PARAMETER_PROBLEM, 135 "SAME needs --to"); 136} 137 138static void SAME_print(const void *ip, const struct xt_entry_target *target, 139 int numeric) 140{ 141 unsigned int count; 142 struct ipt_same_info *mr 143 = (struct ipt_same_info *)target->data; 144 int random_selection = 0; 145 146 printf("same:"); 147 148 for (count = 0; count < mr->rangesize; count++) { 149 struct ip_nat_range *r = &mr->range[count]; 150 struct in_addr a; 151 152 a.s_addr = r->min_ip; 153 154 printf("%s", ipaddr_to_numeric(&a)); 155 a.s_addr = r->max_ip; 156 157 if (r->min_ip == r->max_ip) 158 printf(" "); 159 else 160 printf("-%s ", ipaddr_to_numeric(&a)); 161 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 162 random_selection = 1; 163 } 164 165 if (mr->info & IPT_SAME_NODST) 166 printf("nodst "); 167 168 if (random_selection) 169 printf("random "); 170} 171 172static void SAME_save(const void *ip, const struct xt_entry_target *target) 173{ 174 unsigned int count; 175 struct ipt_same_info *mr 176 = (struct ipt_same_info *)target->data; 177 int random_selection = 0; 178 179 for (count = 0; count < mr->rangesize; count++) { 180 struct ip_nat_range *r = &mr->range[count]; 181 struct in_addr a; 182 183 a.s_addr = r->min_ip; 184 printf("--to %s", ipaddr_to_numeric(&a)); 185 a.s_addr = r->max_ip; 186 187 if (r->min_ip == r->max_ip) 188 printf(" "); 189 else 190 printf("-%s ", ipaddr_to_numeric(&a)); 191 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 192 random_selection = 1; 193 } 194 195 if (mr->info & IPT_SAME_NODST) 196 printf("--nodst "); 197 198 if (random_selection) 199 printf("--random "); 200} 201 202static struct xtables_target same_tg_reg = { 203 .name = "SAME", 204 .version = XTABLES_VERSION, 205 .family = NFPROTO_IPV4, 206 .size = XT_ALIGN(sizeof(struct ipt_same_info)), 207 .userspacesize = XT_ALIGN(sizeof(struct ipt_same_info)), 208 .help = SAME_help, 209 .init = SAME_init, 210 .parse = SAME_parse, 211 .final_check = SAME_check, 212 .print = SAME_print, 213 .save = SAME_save, 214 .extra_opts = SAME_opts, 215}; 216 217void _init(void) 218{ 219 xtables_register_target(&same_tg_reg); 220} 221