libipt_SAME.c revision cf655eb194951a93e4e1371747273c12466c1952
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_ipv4/ip_nat_rule.h> 10#include <linux/netfilter_ipv4/ipt_SAME.h> 11 12#define BREAKUP_IP(x) (x) & 0xFF, ((x)>>8) & 0xFF, ((x)>>16) & 0xFF, (x)>>24 13 14/* Function which prints out usage message. */ 15static void 16help(void) 17{ 18 printf( 19"SAME v%s options:\n" 20" --to <ipaddr>-<ipaddr>\n" 21" Addresses to map source to.\n" 22" --nodst\n" 23" Don't use destination-ip in\n" 24" source selection\n", 25NETFILTER_VERSION); 26} 27 28static struct option opts[] = { 29 { "to", 1, 0, '1' }, 30 { "nodst", 0, 0, '2'}, 31 { 0 } 32}; 33 34/* Initialize the target. */ 35static void 36init(struct ipt_entry_target *t, unsigned int *nfcache) 37{ 38 struct ipt_same_info *mr = (struct ipt_same_info *)t->data; 39 40 /* Actually, it's 0, but it's ignored at the moment. */ 41 mr->rangesize = 1; 42 43 /* Set default info to 0 */ 44 mr->info = 0; 45 46 /* Can't cache this */ 47 *nfcache |= NFC_UNKNOWN; 48} 49 50/* Parses range of IPs */ 51static void 52parse_to(char *arg, struct ip_nat_range *range) 53{ 54 char *dash; 55 struct in_addr *ip; 56 57 range->flags |= IP_NAT_RANGE_MAP_IPS; 58 dash = strchr(arg, '-'); 59 if (dash) 60 *dash = '\0'; 61 else 62 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s'\n", arg); 63 64 ip = dotted_to_addr(arg); 65 if (!ip) 66 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 67 arg); 68 range->min_ip = ip->s_addr; 69 ip = dotted_to_addr(dash+1); 70 if (!ip) 71 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", 72 dash+1); 73 range->max_ip = ip->s_addr; 74 if (range->min_ip >= range->max_ip) 75 exit_error(PARAMETER_PROBLEM, "Bad IP range `%u.%u.%u.%u-%u.%u.%u.%u'\n", BREAKUP_IP(range->min_ip), BREAKUP_IP(range->max_ip)); 76} 77 78#define IPT_SAME_OPT_TO 0x01 79#define IPT_SAME_OPT_NODST 0x02 80 81/* Function which parses command options; returns true if it 82 ate an option */ 83static int 84parse(int c, char **argv, int invert, unsigned int *flags, 85 const struct ipt_entry *entry, 86 struct ipt_entry_target **target) 87{ 88 struct ipt_same_info *mr 89 = (struct ipt_same_info *)(*target)->data; 90 91 switch (c) { 92 case '1': 93 if (*flags & IPT_SAME_OPT_TO) 94 exit_error(PARAMETER_PROBLEM, 95 "Can't specify --to twice"); 96 97 if (check_inverse(optarg, &invert)) 98 exit_error(PARAMETER_PROBLEM, 99 "Unexpected `!' after --to"); 100 101 parse_to(optarg, &mr->range[0]); 102 *flags |= IPT_SAME_OPT_TO; 103 break; 104 105 case '2': 106 if (*flags & IPT_SAME_OPT_NODST) 107 exit_error(PARAMETER_PROBLEM, 108 "Can't specify --nodst twice"); 109 110 mr->info |= IPT_SAME_NODST; 111 *flags |= IPT_SAME_OPT_NODST; 112 break; 113 114 default: 115 return 0; 116 } 117 118 return 1; 119} 120 121/* Final check; need --to. */ 122static void final_check(unsigned int flags) 123{ 124 if (!(flags & IPT_SAME_OPT_TO)) 125 exit_error(PARAMETER_PROBLEM, 126 "SAME needs --to"); 127} 128 129/* Prints out the targinfo. */ 130static void 131print(const struct ipt_ip *ip, 132 const struct ipt_entry_target *target, 133 int numeric) 134{ 135 struct ipt_same_info *mr 136 = (struct ipt_same_info *)target->data; 137 struct ip_nat_range *r = &mr->range[0]; 138 struct in_addr a; 139 140 a.s_addr = r->min_ip; 141 142 printf("same %s", addr_to_dotted(&a)); 143 a.s_addr = r->max_ip; 144 printf("-%s ", addr_to_dotted(&a)); 145 146 if (mr->info & IPT_SAME_NODST) 147 printf("nodst "); 148} 149 150/* Saves the union ipt_targinfo in parsable form to stdout. */ 151static void 152save(const struct ipt_ip *ip, const struct ipt_entry_target *target) 153{ 154 struct ipt_same_info *mr 155 = (struct ipt_same_info *)target->data; 156 struct ip_nat_range *r = &mr->range[0]; 157 struct in_addr a; 158 159 a.s_addr = r->min_ip; 160 printf("--to %s", addr_to_dotted(&a)); 161 a.s_addr = r->max_ip; 162 printf("-%s ", addr_to_dotted(&a)); 163 164 if (mr->info & IPT_SAME_NODST) 165 printf("--nodst "); 166} 167 168struct iptables_target same 169= { NULL, 170 "SAME", 171 NETFILTER_VERSION, 172 IPT_ALIGN(sizeof(struct ipt_same_info)), 173 IPT_ALIGN(sizeof(struct ipt_same_info)), 174 &help, 175 &init, 176 &parse, 177 &final_check, 178 &print, 179 &save, 180 opts 181}; 182 183void _init(void) 184{ 185 register_target(&same); 186} 187