libipt_DNAT.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <stdio.h>
  2. #include <netdb.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <xtables.h>
  6. #include <iptables.h> /* get_kernel_version */
  7. #include <limits.h> /* INT_MAX in ip_tables.h */
  8. #include <linux/netfilter_ipv4/ip_tables.h>
  9. #include <linux/netfilter/nf_nat.h>
  10. enum {
  11. O_TO_DEST = 0,
  12. O_RANDOM,
  13. O_PERSISTENT,
  14. O_X_TO_DEST, /* hidden flag */
  15. F_TO_DEST = 1 << O_TO_DEST,
  16. F_RANDOM = 1 << O_RANDOM,
  17. F_X_TO_DEST = 1 << O_X_TO_DEST,
  18. };
  19. /* Dest NAT data consists of a multi-range, indicating where to map
  20. to. */
  21. struct ipt_natinfo
  22. {
  23. struct xt_entry_target t;
  24. struct nf_nat_ipv4_multi_range_compat mr;
  25. };
  26. static void DNAT_help(void)
  27. {
  28. printf(
  29. "DNAT target options:\n"
  30. " --to-destination [<ipaddr>[-<ipaddr>]][:port[-port]]\n"
  31. " Address to map destination to.\n"
  32. "[--random] [--persistent]\n");
  33. }
  34. static const struct xt_option_entry DNAT_opts[] = {
  35. {.name = "to-destination", .id = O_TO_DEST, .type = XTTYPE_STRING,
  36. .flags = XTOPT_MAND | XTOPT_MULTI},
  37. {.name = "random", .id = O_RANDOM, .type = XTTYPE_NONE},
  38. {.name = "persistent", .id = O_PERSISTENT, .type = XTTYPE_NONE},
  39. XTOPT_TABLEEND,
  40. };
  41. static struct ipt_natinfo *
  42. append_range(struct ipt_natinfo *info, const struct nf_nat_ipv4_range *range)
  43. {
  44. unsigned int size;
  45. /* One rangesize already in struct ipt_natinfo */
  46. size = XT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range));
  47. info = realloc(info, size);
  48. if (!info)
  49. xtables_error(OTHER_PROBLEM, "Out of memory\n");
  50. info->t.u.target_size = size;
  51. info->mr.range[info->mr.rangesize] = *range;
  52. info->mr.rangesize++;
  53. return info;
  54. }
  55. /* Ranges expected in network order. */
  56. static struct xt_entry_target *
  57. parse_to(const char *orig_arg, int portok, struct ipt_natinfo *info)
  58. {
  59. struct nf_nat_ipv4_range range;
  60. char *arg, *colon, *dash, *error;
  61. const struct in_addr *ip;
  62. arg = strdup(orig_arg);
  63. if (arg == NULL)
  64. xtables_error(RESOURCE_PROBLEM, "strdup");
  65. memset(&range, 0, sizeof(range));
  66. colon = strchr(arg, ':');
  67. if (colon) {
  68. int port;
  69. if (!portok)
  70. xtables_error(PARAMETER_PROBLEM,
  71. "Need TCP, UDP, SCTP or DCCP with port specification");
  72. range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  73. port = atoi(colon+1);
  74. if (port <= 0 || port > 65535)
  75. xtables_error(PARAMETER_PROBLEM,
  76. "Port `%s' not valid\n", colon+1);
  77. error = strchr(colon+1, ':');
  78. if (error)
  79. xtables_error(PARAMETER_PROBLEM,
  80. "Invalid port:port syntax - use dash\n");
  81. dash = strchr(colon, '-');
  82. if (!dash) {
  83. range.min.tcp.port
  84. = range.max.tcp.port
  85. = htons(port);
  86. } else {
  87. int maxport;
  88. maxport = atoi(dash + 1);
  89. if (maxport <= 0 || maxport > 65535)
  90. xtables_error(PARAMETER_PROBLEM,
  91. "Port `%s' not valid\n", dash+1);
  92. if (maxport < port)
  93. /* People are stupid. */
  94. xtables_error(PARAMETER_PROBLEM,
  95. "Port range `%s' funky\n", colon+1);
  96. range.min.tcp.port = htons(port);
  97. range.max.tcp.port = htons(maxport);
  98. }
  99. /* Starts with a colon? No IP info...*/
  100. if (colon == arg) {
  101. free(arg);
  102. return &(append_range(info, &range)->t);
  103. }
  104. *colon = '\0';
  105. }
  106. range.flags |= NF_NAT_RANGE_MAP_IPS;
  107. dash = strchr(arg, '-');
  108. if (colon && dash && dash > colon)
  109. dash = NULL;
  110. if (dash)
  111. *dash = '\0';
  112. ip = xtables_numeric_to_ipaddr(arg);
  113. if (!ip)
  114. xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
  115. arg);
  116. range.min_ip = ip->s_addr;
  117. if (dash) {
  118. ip = xtables_numeric_to_ipaddr(dash+1);
  119. if (!ip)
  120. xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
  121. dash+1);
  122. range.max_ip = ip->s_addr;
  123. } else
  124. range.max_ip = range.min_ip;
  125. free(arg);
  126. return &(append_range(info, &range)->t);
  127. }
  128. static void DNAT_parse(struct xt_option_call *cb)
  129. {
  130. const struct ipt_entry *entry = cb->xt_entry;
  131. struct ipt_natinfo *info = (void *)(*cb->target);
  132. int portok;
  133. if (entry->ip.proto == IPPROTO_TCP
  134. || entry->ip.proto == IPPROTO_UDP
  135. || entry->ip.proto == IPPROTO_SCTP
  136. || entry->ip.proto == IPPROTO_DCCP
  137. || entry->ip.proto == IPPROTO_ICMP)
  138. portok = 1;
  139. else
  140. portok = 0;
  141. xtables_option_parse(cb);
  142. switch (cb->entry->id) {
  143. case O_TO_DEST:
  144. if (cb->xflags & F_X_TO_DEST) {
  145. if (!kernel_version)
  146. get_kernel_version();
  147. if (kernel_version > LINUX_VERSION(2, 6, 10))
  148. xtables_error(PARAMETER_PROBLEM,
  149. "DNAT: Multiple --to-destination not supported");
  150. }
  151. *cb->target = parse_to(cb->arg, portok, info);
  152. cb->xflags |= F_X_TO_DEST;
  153. break;
  154. case O_PERSISTENT:
  155. info->mr.range[0].flags |= NF_NAT_RANGE_PERSISTENT;
  156. break;
  157. }
  158. }
  159. static void DNAT_fcheck(struct xt_fcheck_call *cb)
  160. {
  161. static const unsigned int f = F_TO_DEST | F_RANDOM;
  162. struct nf_nat_ipv4_multi_range_compat *mr = cb->data;
  163. if ((cb->xflags & f) == f)
  164. mr->range[0].flags |= NF_NAT_RANGE_PROTO_RANDOM;
  165. }
  166. static void print_range(const struct nf_nat_ipv4_range *r)
  167. {
  168. if (r->flags & NF_NAT_RANGE_MAP_IPS) {
  169. struct in_addr a;
  170. a.s_addr = r->min_ip;
  171. printf("%s", xtables_ipaddr_to_numeric(&a));
  172. if (r->max_ip != r->min_ip) {
  173. a.s_addr = r->max_ip;
  174. printf("-%s", xtables_ipaddr_to_numeric(&a));
  175. }
  176. }
  177. if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
  178. printf(":");
  179. printf("%hu", ntohs(r->min.tcp.port));
  180. if (r->max.tcp.port != r->min.tcp.port)
  181. printf("-%hu", ntohs(r->max.tcp.port));
  182. }
  183. }
  184. static void DNAT_print(const void *ip, const struct xt_entry_target *target,
  185. int numeric)
  186. {
  187. const struct ipt_natinfo *info = (const void *)target;
  188. unsigned int i = 0;
  189. printf(" to:");
  190. for (i = 0; i < info->mr.rangesize; i++) {
  191. print_range(&info->mr.range[i]);
  192. if (info->mr.range[i].flags & NF_NAT_RANGE_PROTO_RANDOM)
  193. printf(" random");
  194. if (info->mr.range[i].flags & NF_NAT_RANGE_PERSISTENT)
  195. printf(" persistent");
  196. }
  197. }
  198. static void DNAT_save(const void *ip, const struct xt_entry_target *target)
  199. {
  200. const struct ipt_natinfo *info = (const void *)target;
  201. unsigned int i = 0;
  202. for (i = 0; i < info->mr.rangesize; i++) {
  203. printf(" --to-destination ");
  204. print_range(&info->mr.range[i]);
  205. if (info->mr.range[i].flags & NF_NAT_RANGE_PROTO_RANDOM)
  206. printf(" --random");
  207. if (info->mr.range[i].flags & NF_NAT_RANGE_PERSISTENT)
  208. printf(" --persistent");
  209. }
  210. }
  211. static struct xtables_target dnat_tg_reg = {
  212. .name = "DNAT",
  213. .version = XTABLES_VERSION,
  214. .family = NFPROTO_IPV4,
  215. .size = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
  216. .userspacesize = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
  217. .help = DNAT_help,
  218. .x6_parse = DNAT_parse,
  219. .x6_fcheck = DNAT_fcheck,
  220. .print = DNAT_print,
  221. .save = DNAT_save,
  222. .x6_options = DNAT_opts,
  223. };
  224. void _init(void)
  225. {
  226. xtables_register_target(&dnat_tg_reg);
  227. }