libipt_MASQUERADE.c revision ac8b2718daf8a79a59b181f6e62495f307ae86b9
1/* Shared library add-on to iptables to add masquerade 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 11/* Function which prints out usage message. */ 12static void 13help(void) 14{ 15 printf( 16"MASQUERADE v%s options:\n" 17" --to-ports <port>[-<port>]\n" 18" Port (range) to map to.\n" 19" --random\n" 20" Randomize source port.\n" 21"\n" 22, 23IPTABLES_VERSION); 24} 25 26static struct option opts[] = { 27 { "to-ports", 1, 0, '1' }, 28 { "random", 0, 0, '2' }, 29 { 0 } 30}; 31 32/* Initialize the target. */ 33static void 34init(struct xt_entry_target *t, unsigned int *nfcache) 35{ 36 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data; 37 38 /* Actually, it's 0, but it's ignored at the moment. */ 39 mr->rangesize = 1; 40 41} 42 43/* Parses ports */ 44static void 45parse_ports(const char *arg, struct ip_nat_multi_range *mr) 46{ 47 const char *dash; 48 int port; 49 50 mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED; 51 52 port = atoi(arg); 53 if (port <= 0 || port > 65535) 54 exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg); 55 56 dash = strchr(arg, '-'); 57 if (!dash) { 58 mr->range[0].min.tcp.port 59 = mr->range[0].max.tcp.port 60 = htons(port); 61 } else { 62 int maxport; 63 64 maxport = atoi(dash + 1); 65 if (maxport == 0 || maxport > 65535) 66 exit_error(PARAMETER_PROBLEM, 67 "Port `%s' not valid\n", dash+1); 68 if (maxport < port) 69 /* People are stupid. Present reader excepted. */ 70 exit_error(PARAMETER_PROBLEM, 71 "Port range `%s' funky\n", arg); 72 mr->range[0].min.tcp.port = htons(port); 73 mr->range[0].max.tcp.port = htons(maxport); 74 } 75} 76 77/* Function which parses command options; returns true if it 78 ate an option */ 79static int 80parse(int c, char **argv, int invert, unsigned int *flags, 81 const void *e, 82 struct xt_entry_target **target) 83{ 84 const struct ipt_entry *entry = e; 85 int portok; 86 struct ip_nat_multi_range *mr 87 = (struct ip_nat_multi_range *)(*target)->data; 88 89 if (entry->ip.proto == IPPROTO_TCP 90 || entry->ip.proto == IPPROTO_UDP 91 || entry->ip.proto == IPPROTO_ICMP) 92 portok = 1; 93 else 94 portok = 0; 95 96 switch (c) { 97 case '1': 98 if (!portok) 99 exit_error(PARAMETER_PROBLEM, 100 "Need TCP or UDP with port specification"); 101 102 if (check_inverse(optarg, &invert, NULL, 0)) 103 exit_error(PARAMETER_PROBLEM, 104 "Unexpected `!' after --to-ports"); 105 106 parse_ports(optarg, mr); 107 return 1; 108 109 case '2': 110 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; 111 return 1; 112 113 default: 114 return 0; 115 } 116} 117 118/* Final check; don't care. */ 119static void final_check(unsigned int flags) 120{ 121} 122 123/* Prints out the targinfo. */ 124static void 125print(const void *ip, 126 const struct xt_entry_target *target, 127 int numeric) 128{ 129 struct ip_nat_multi_range *mr 130 = (struct ip_nat_multi_range *)target->data; 131 struct ip_nat_range *r = &mr->range[0]; 132 133 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { 134 printf("masq ports: "); 135 printf("%hu", ntohs(r->min.tcp.port)); 136 if (r->max.tcp.port != r->min.tcp.port) 137 printf("-%hu", ntohs(r->max.tcp.port)); 138 printf(" "); 139 } 140 141 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 142 printf("random "); 143} 144 145/* Saves the union ipt_targinfo in parsable form to stdout. */ 146static void 147save(const void *ip, const struct xt_entry_target *target) 148{ 149 struct ip_nat_multi_range *mr 150 = (struct ip_nat_multi_range *)target->data; 151 struct ip_nat_range *r = &mr->range[0]; 152 153 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { 154 printf("--to-ports %hu", ntohs(r->min.tcp.port)); 155 if (r->max.tcp.port != r->min.tcp.port) 156 printf("-%hu", ntohs(r->max.tcp.port)); 157 printf(" "); 158 } 159 160 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 161 printf("--random "); 162} 163 164static struct iptables_target masq = { NULL, 165 .name = "MASQUERADE", 166 .version = IPTABLES_VERSION, 167 .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), 168 .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), 169 .help = &help, 170 .init = &init, 171 .parse = &parse, 172 .final_check = &final_check, 173 .print = &print, 174 .save = &save, 175 .extra_opts = opts 176}; 177 178void _init(void) 179{ 180 register_target(&masq); 181} 182