libipt_SAME.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
1/* Shared library add-on to iptables to add simple non load-balancing SNAT support. */ 2#include <stdbool.h> 3#include <stdio.h> 4#include <netdb.h> 5#include <string.h> 6#include <stdlib.h> 7#include <getopt.h> 8#include <xtables.h> 9#include <net/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 {.name = "to", .has_arg = true, .val = '1'}, 30 {.name = "nodst", .has_arg = false, .val = '2'}, 31 {.name = "random", .has_arg = false, .val = '3'}, 32 XT_GETOPT_TABLEEND, 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 nf_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 = xtables_numeric_to_ipaddr(arg); 60 if (!ip) 61 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n", 62 arg); 63 range->min_ip = ip->s_addr; 64 65 if (dash) { 66 ip = xtables_numeric_to_ipaddr(dash+1); 67 if (!ip) 68 xtables_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 xtables_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 xtables_error(PARAMETER_PROBLEM, 93 "Too many ranges specified, maximum " 94 "is %i ranges.\n", 95 IPT_SAME_MAX_RANGE); 96 if (xtables_check_inverse(optarg, &invert, NULL, 0, argv)) 97 xtables_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 xtables_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 125 return 1; 126} 127 128static void SAME_check(unsigned int flags) 129{ 130 if (!(flags & IPT_SAME_OPT_TO)) 131 xtables_error(PARAMETER_PROBLEM, 132 "SAME needs --to"); 133} 134 135static void SAME_print(const void *ip, const struct xt_entry_target *target, 136 int numeric) 137{ 138 unsigned int count; 139 const struct ipt_same_info *mr = (const void *)target->data; 140 int random_selection = 0; 141 142 printf(" same:"); 143 144 for (count = 0; count < mr->rangesize; count++) { 145 const struct nf_nat_range *r = &mr->range[count]; 146 struct in_addr a; 147 148 a.s_addr = r->min_ip; 149 150 printf("%s", xtables_ipaddr_to_numeric(&a)); 151 a.s_addr = r->max_ip; 152 153 if (r->min_ip != r->max_ip) 154 printf("-%s", xtables_ipaddr_to_numeric(&a)); 155 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 156 random_selection = 1; 157 } 158 159 if (mr->info & IPT_SAME_NODST) 160 printf(" nodst"); 161 162 if (random_selection) 163 printf(" random"); 164} 165 166static void SAME_save(const void *ip, const struct xt_entry_target *target) 167{ 168 unsigned int count; 169 const struct ipt_same_info *mr = (const void *)target->data; 170 int random_selection = 0; 171 172 for (count = 0; count < mr->rangesize; count++) { 173 const struct nf_nat_range *r = &mr->range[count]; 174 struct in_addr a; 175 176 a.s_addr = r->min_ip; 177 printf(" --to %s", xtables_ipaddr_to_numeric(&a)); 178 a.s_addr = r->max_ip; 179 180 if (r->min_ip != r->max_ip) 181 printf("-%s", xtables_ipaddr_to_numeric(&a)); 182 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 183 random_selection = 1; 184 } 185 186 if (mr->info & IPT_SAME_NODST) 187 printf(" --nodst"); 188 189 if (random_selection) 190 printf(" --random"); 191} 192 193static struct xtables_target same_tg_reg = { 194 .name = "SAME", 195 .version = XTABLES_VERSION, 196 .family = NFPROTO_IPV4, 197 .size = XT_ALIGN(sizeof(struct ipt_same_info)), 198 .userspacesize = XT_ALIGN(sizeof(struct ipt_same_info)), 199 .help = SAME_help, 200 .init = SAME_init, 201 .parse = SAME_parse, 202 .final_check = SAME_check, 203 .print = SAME_print, 204 .save = SAME_save, 205 .extra_opts = SAME_opts, 206}; 207 208void _init(void) 209{ 210 xtables_register_target(&same_tg_reg); 211} 212