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