PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv4/netfilter/ipt_SAME.c

https://bitbucket.org/abioy/linux
C | 206 lines | 153 code | 27 blank | 26 comment | 16 complexity | 800463f78038db0848d753f7ab8795e6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* Same. Just like SNAT, only try to make the connections
  2. * between client A and server B always have the same source ip.
  3. *
  4. * (C) 2000 Rusty Russell. GPL.
  5. *
  6. * 010320 Martin Josefsson <gandalf@wlug.westbo.se>
  7. * * copied ipt_BALANCE.c to ipt_SAME.c and changed a few things.
  8. * 010728 Martin Josefsson <gandalf@wlug.westbo.se>
  9. * * added --nodst to not include destination-ip in new source
  10. * calculations.
  11. * * added some more sanity-checks.
  12. * 010729 Martin Josefsson <gandalf@wlug.westbo.se>
  13. * * fixed a buggy if-statement in same_check(), should have
  14. * used ntohl() but didn't.
  15. * * added support for multiple ranges. IPT_SAME_MAX_RANGE is
  16. * defined in linux/include/linux/netfilter_ipv4/ipt_SAME.h
  17. * and is currently set to 10.
  18. * * added support for 1-address range, nice to have now that
  19. * we have multiple ranges.
  20. */
  21. #include <linux/types.h>
  22. #include <linux/ip.h>
  23. #include <linux/timer.h>
  24. #include <linux/module.h>
  25. #include <linux/netfilter.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/if.h>
  28. #include <linux/inetdevice.h>
  29. #include <net/protocol.h>
  30. #include <net/checksum.h>
  31. #include <linux/netfilter_ipv4.h>
  32. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  33. #include <linux/netfilter_ipv4/ipt_SAME.h>
  34. MODULE_LICENSE("GPL");
  35. MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
  36. MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");
  37. #if 0
  38. #define DEBUGP printk
  39. #else
  40. #define DEBUGP(format, args...)
  41. #endif
  42. static int
  43. same_check(const char *tablename,
  44. const struct ipt_entry *e,
  45. void *targinfo,
  46. unsigned int targinfosize,
  47. unsigned int hook_mask)
  48. {
  49. unsigned int count, countess, rangeip, index = 0;
  50. struct ipt_same_info *mr = targinfo;
  51. mr->ipnum = 0;
  52. if (strcmp(tablename, "nat") != 0) {
  53. DEBUGP("same_check: bad table `%s'.\n", tablename);
  54. return 0;
  55. }
  56. if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
  57. DEBUGP("same_check: size %u.\n", targinfosize);
  58. return 0;
  59. }
  60. if (hook_mask & ~(1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING)) {
  61. DEBUGP("same_check: bad hooks %x.\n", hook_mask);
  62. return 0;
  63. }
  64. if (mr->rangesize < 1) {
  65. DEBUGP("same_check: need at least one dest range.\n");
  66. return 0;
  67. }
  68. if (mr->rangesize > IPT_SAME_MAX_RANGE) {
  69. DEBUGP("same_check: too many ranges specified, maximum "
  70. "is %u ranges\n",
  71. IPT_SAME_MAX_RANGE);
  72. return 0;
  73. }
  74. for (count = 0; count < mr->rangesize; count++) {
  75. if (ntohl(mr->range[count].min_ip) >
  76. ntohl(mr->range[count].max_ip)) {
  77. DEBUGP("same_check: min_ip is larger than max_ip in "
  78. "range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
  79. NIPQUAD(mr->range[count].min_ip),
  80. NIPQUAD(mr->range[count].max_ip));
  81. return 0;
  82. }
  83. if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
  84. DEBUGP("same_check: bad MAP_IPS.\n");
  85. return 0;
  86. }
  87. rangeip = (ntohl(mr->range[count].max_ip) -
  88. ntohl(mr->range[count].min_ip) + 1);
  89. mr->ipnum += rangeip;
  90. DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
  91. }
  92. DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
  93. mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
  94. if (!mr->iparray) {
  95. DEBUGP("same_check: Couldn't allocate %u bytes "
  96. "for %u ipaddresses!\n",
  97. (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
  98. return 0;
  99. }
  100. DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
  101. (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
  102. for (count = 0; count < mr->rangesize; count++) {
  103. for (countess = ntohl(mr->range[count].min_ip);
  104. countess <= ntohl(mr->range[count].max_ip);
  105. countess++) {
  106. mr->iparray[index] = countess;
  107. DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
  108. "in index %u.\n",
  109. HIPQUAD(countess), index);
  110. index++;
  111. }
  112. }
  113. return 1;
  114. }
  115. static void
  116. same_destroy(void *targinfo,
  117. unsigned int targinfosize)
  118. {
  119. struct ipt_same_info *mr = targinfo;
  120. kfree(mr->iparray);
  121. DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
  122. (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
  123. }
  124. static unsigned int
  125. same_target(struct sk_buff **pskb,
  126. const struct net_device *in,
  127. const struct net_device *out,
  128. unsigned int hooknum,
  129. const void *targinfo,
  130. void *userinfo)
  131. {
  132. struct ip_conntrack *ct;
  133. enum ip_conntrack_info ctinfo;
  134. u_int32_t tmpip, aindex, new_ip;
  135. const struct ipt_same_info *mr = targinfo;
  136. struct ip_nat_multi_range newrange;
  137. const struct ip_conntrack_tuple *t;
  138. IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
  139. hooknum == NF_IP_POST_ROUTING);
  140. ct = ip_conntrack_get(*pskb, &ctinfo);
  141. t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  142. /* Base new source on real src ip and optionally dst ip,
  143. giving some hope for consistency across reboots.
  144. Here we calculate the index in mr->iparray which
  145. holds the ipaddress we should use */
  146. tmpip = ntohl(t->src.ip);
  147. if (!(mr->info & IPT_SAME_NODST))
  148. tmpip += ntohl(t->dst.ip);
  149. aindex = tmpip % mr->ipnum;
  150. new_ip = htonl(mr->iparray[aindex]);
  151. DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
  152. "new src=%u.%u.%u.%u\n",
  153. NIPQUAD(t->src.ip), NIPQUAD(t->dst.ip),
  154. NIPQUAD(new_ip));
  155. /* Transfer from original range. */
  156. newrange = ((struct ip_nat_multi_range)
  157. { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
  158. new_ip, new_ip,
  159. mr->range[0].min, mr->range[0].max } } });
  160. /* Hand modified range to generic setup. */
  161. return ip_nat_setup_info(ct, &newrange, hooknum);
  162. }
  163. static struct ipt_target same_reg = {
  164. .name = "SAME",
  165. .target = same_target,
  166. .checkentry = same_check,
  167. .destroy = same_destroy,
  168. .me = THIS_MODULE,
  169. };
  170. static int __init init(void)
  171. {
  172. return ipt_register_target(&same_reg);
  173. }
  174. static void __exit fini(void)
  175. {
  176. ipt_unregister_target(&same_reg);
  177. }
  178. module_init(init);
  179. module_exit(fini);