libip6t_MASQUERADE.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  3. *
  4. * Based on Rusty Russell's IPv4 MASQUERADE target. Development of IPv6 NAT
  5. * funded by Astaro.
  6. */
  7. #include <stdio.h>
  8. #include <netdb.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <getopt.h>
  12. #include <xtables.h>
  13. #include <limits.h> /* INT_MAX in ip_tables.h */
  14. #include <linux/netfilter_ipv6/ip6_tables.h>
  15. #include <linux/netfilter/nf_nat.h>
  16. enum {
  17. O_TO_PORTS = 0,
  18. O_RANDOM,
  19. };
  20. static void MASQUERADE_help(void)
  21. {
  22. printf(
  23. "MASQUERADE target options:\n"
  24. " --to-ports <port>[-<port>]\n"
  25. " Port (range) to map to.\n"
  26. " --random\n"
  27. " Randomize source port.\n");
  28. }
  29. static const struct xt_option_entry MASQUERADE_opts[] = {
  30. {.name = "to-ports", .id = O_TO_PORTS, .type = XTTYPE_STRING},
  31. {.name = "random", .id = O_RANDOM, .type = XTTYPE_NONE},
  32. XTOPT_TABLEEND,
  33. };
  34. /* Parses ports */
  35. static void
  36. parse_ports(const char *arg, struct nf_nat_range *r)
  37. {
  38. char *end;
  39. unsigned int port, maxport;
  40. r->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  41. if (!xtables_strtoui(arg, &end, &port, 0, UINT16_MAX))
  42. xtables_param_act(XTF_BAD_VALUE, "MASQUERADE", "--to-ports", arg);
  43. switch (*end) {
  44. case '\0':
  45. r->min_proto.tcp.port
  46. = r->max_proto.tcp.port
  47. = htons(port);
  48. return;
  49. case '-':
  50. if (!xtables_strtoui(end + 1, NULL, &maxport, 0, UINT16_MAX))
  51. break;
  52. if (maxport < port)
  53. break;
  54. r->min_proto.tcp.port = htons(port);
  55. r->max_proto.tcp.port = htons(maxport);
  56. return;
  57. default:
  58. break;
  59. }
  60. xtables_param_act(XTF_BAD_VALUE, "MASQUERADE", "--to-ports", arg);
  61. }
  62. static void MASQUERADE_parse(struct xt_option_call *cb)
  63. {
  64. const struct ip6t_entry *entry = cb->xt_entry;
  65. struct nf_nat_range *r = cb->data;
  66. int portok;
  67. if (entry->ipv6.proto == IPPROTO_TCP ||
  68. entry->ipv6.proto == IPPROTO_UDP ||
  69. entry->ipv6.proto == IPPROTO_SCTP ||
  70. entry->ipv6.proto == IPPROTO_DCCP ||
  71. entry->ipv6.proto == IPPROTO_ICMP)
  72. portok = 1;
  73. else
  74. portok = 0;
  75. xtables_option_parse(cb);
  76. switch (cb->entry->id) {
  77. case O_TO_PORTS:
  78. if (!portok)
  79. xtables_error(PARAMETER_PROBLEM,
  80. "Need TCP, UDP, SCTP or DCCP with port specification");
  81. parse_ports(cb->arg, r);
  82. break;
  83. case O_RANDOM:
  84. r->flags |= NF_NAT_RANGE_PROTO_RANDOM;
  85. break;
  86. }
  87. }
  88. static void
  89. MASQUERADE_print(const void *ip, const struct xt_entry_target *target,
  90. int numeric)
  91. {
  92. const struct nf_nat_range *r = (const void *)target->data;
  93. if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
  94. printf(" masq ports: ");
  95. printf("%hu", ntohs(r->min_proto.tcp.port));
  96. if (r->max_proto.tcp.port != r->min_proto.tcp.port)
  97. printf("-%hu", ntohs(r->max_proto.tcp.port));
  98. }
  99. if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
  100. printf(" random");
  101. }
  102. static void
  103. MASQUERADE_save(const void *ip, const struct xt_entry_target *target)
  104. {
  105. const struct nf_nat_range *r = (const void *)target->data;
  106. if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
  107. printf(" --to-ports %hu", ntohs(r->min_proto.tcp.port));
  108. if (r->max_proto.tcp.port != r->min_proto.tcp.port)
  109. printf("-%hu", ntohs(r->max_proto.tcp.port));
  110. }
  111. if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
  112. printf(" --random");
  113. }
  114. static struct xtables_target masquerade_tg_reg = {
  115. .name = "MASQUERADE",
  116. .version = XTABLES_VERSION,
  117. .family = NFPROTO_IPV6,
  118. .size = XT_ALIGN(sizeof(struct nf_nat_range)),
  119. .userspacesize = XT_ALIGN(sizeof(struct nf_nat_range)),
  120. .help = MASQUERADE_help,
  121. .x6_parse = MASQUERADE_parse,
  122. .print = MASQUERADE_print,
  123. .save = MASQUERADE_save,
  124. .x6_options = MASQUERADE_opts,
  125. };
  126. void _init(void)
  127. {
  128. xtables_register_target(&masquerade_tg_reg);
  129. }