libipt_SAME.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
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 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 xtables_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 const struct ipt_same_info *mr = (const void *)target->data; 143 int random_selection = 0; 144 145 printf("same:"); 146 147 for (count = 0; count < mr->rangesize; count++) { 148 const struct nf_nat_range *r = &mr->range[count]; 149 struct in_addr a; 150 151 a.s_addr = r->min_ip; 152 153 printf("%s", xtables_ipaddr_to_numeric(&a)); 154 a.s_addr = r->max_ip; 155 156 if (r->min_ip == r->max_ip) 157 printf(" "); 158 else 159 printf("-%s ", xtables_ipaddr_to_numeric(&a)); 160 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 161 random_selection = 1; 162 } 163 164 if (mr->info & IPT_SAME_NODST) 165 printf("nodst "); 166 167 if (random_selection) 168 printf("random "); 169} 170 171static void SAME_save(const void *ip, const struct xt_entry_target *target) 172{ 173 unsigned int count; 174 const struct ipt_same_info *mr = (const void *)target->data; 175 int random_selection = 0; 176 177 for (count = 0; count < mr->rangesize; count++) { 178 const struct nf_nat_range *r = &mr->range[count]; 179 struct in_addr a; 180 181 a.s_addr = r->min_ip; 182 printf("--to %s", xtables_ipaddr_to_numeric(&a)); 183 a.s_addr = r->max_ip; 184 185 if (r->min_ip == r->max_ip) 186 printf(" "); 187 else 188 printf("-%s ", xtables_ipaddr_to_numeric(&a)); 189 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 190 random_selection = 1; 191 } 192 193 if (mr->info & IPT_SAME_NODST) 194 printf("--nodst "); 195 196 if (random_selection) 197 printf("--random "); 198} 199 200static struct xtables_target same_tg_reg = { 201 .name = "SAME", 202 .version = XTABLES_VERSION, 203 .family = NFPROTO_IPV4, 204 .size = XT_ALIGN(sizeof(struct ipt_same_info)), 205 .userspacesize = XT_ALIGN(sizeof(struct ipt_same_info)), 206 .help = SAME_help, 207 .init = SAME_init, 208 .parse = SAME_parse, 209 .final_check = SAME_check, 210 .print = SAME_print, 211 .save = SAME_save, 212 .extra_opts = SAME_opts, 213}; 214 215void _init(void) 216{ 217 xtables_register_target(&same_tg_reg); 218} 219