libipt_MASQUERADE.c revision 830132ac9c0d270bf9dcfe85c2464e3fe8c73fb9
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 const struct option opts[] = { 27 { "to-ports", 1, NULL, '1' }, 28 { "random", 0, NULL, '2' }, 29 { } 30}; 31 32/* Initialize the target. */ 33static void 34init(struct xt_entry_target *t) 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/* Prints out the targinfo. */ 119static void 120print(const void *ip, 121 const struct xt_entry_target *target, 122 int numeric) 123{ 124 struct ip_nat_multi_range *mr 125 = (struct ip_nat_multi_range *)target->data; 126 struct ip_nat_range *r = &mr->range[0]; 127 128 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { 129 printf("masq ports: "); 130 printf("%hu", ntohs(r->min.tcp.port)); 131 if (r->max.tcp.port != r->min.tcp.port) 132 printf("-%hu", ntohs(r->max.tcp.port)); 133 printf(" "); 134 } 135 136 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 137 printf("random "); 138} 139 140/* Saves the union ipt_targinfo in parsable form to stdout. */ 141static void 142save(const void *ip, const struct xt_entry_target *target) 143{ 144 struct ip_nat_multi_range *mr 145 = (struct ip_nat_multi_range *)target->data; 146 struct ip_nat_range *r = &mr->range[0]; 147 148 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { 149 printf("--to-ports %hu", ntohs(r->min.tcp.port)); 150 if (r->max.tcp.port != r->min.tcp.port) 151 printf("-%hu", ntohs(r->max.tcp.port)); 152 printf(" "); 153 } 154 155 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM) 156 printf("--random "); 157} 158 159static struct iptables_target masq = { 160 .name = "MASQUERADE", 161 .version = IPTABLES_VERSION, 162 .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), 163 .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), 164 .help = &help, 165 .init = &init, 166 .parse = &parse, 167 .print = &print, 168 .save = &save, 169 .extra_opts = opts 170}; 171 172void _init(void) 173{ 174 register_target(&masq); 175} 176