ip_nat_proto_tcp.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. #include <linux/types.h>
  2. #include <linux/init.h>
  3. #include <linux/netfilter.h>
  4. #include <linux/ip.h>
  5. #include <linux/tcp.h>
  6. #include <linux/if.h>
  7. #include <linux/netfilter_ipv4/ip_nat.h>
  8. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  9. #include <linux/netfilter_ipv4/ip_nat_protocol.h>
  10. static int
  11. tcp_in_range(const struct ip_conntrack_tuple *tuple,
  12.      enum ip_nat_manip_type maniptype,
  13.      const union ip_conntrack_manip_proto *min,
  14.      const union ip_conntrack_manip_proto *max)
  15. {
  16. u_int16_t port;
  17. if (maniptype == IP_NAT_MANIP_SRC)
  18. port = tuple->src.u.tcp.port;
  19. else
  20. port = tuple->dst.u.tcp.port;
  21. return ntohs(port) >= ntohs(min->tcp.port)
  22. && ntohs(port) <= ntohs(max->tcp.port);
  23. }
  24. static int
  25. tcp_unique_tuple(struct ip_conntrack_tuple *tuple,
  26.  const struct ip_nat_range *range,
  27.  enum ip_nat_manip_type maniptype,
  28.  const struct ip_conntrack *conntrack)
  29. {
  30. static u_int16_t port = 0, *portptr;
  31. unsigned int range_size, min, i;
  32. if (maniptype == IP_NAT_MANIP_SRC)
  33. portptr = &tuple->src.u.tcp.port;
  34. else
  35. portptr = &tuple->dst.u.tcp.port;
  36. /* If no range specified... */
  37. if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
  38. /* If it's dst rewrite, can't change port */
  39. if (maniptype == IP_NAT_MANIP_DST)
  40. return 0;
  41. /* Map privileged onto privileged. */
  42. if (ntohs(*portptr) < 1024) {
  43. /* Loose convention: >> 512 is credential passing */
  44. if (ntohs(*portptr)<512) {
  45. min = 1;
  46. range_size = 511 - min + 1;
  47. } else {
  48. min = 600;
  49. range_size = 1023 - min + 1;
  50. }
  51. } else {
  52. min = 1024;
  53. range_size = 65535 - 1024 + 1;
  54. }
  55. } else {
  56. min = ntohs(range->min.tcp.port);
  57. range_size = ntohs(range->max.tcp.port) - min + 1;
  58. }
  59. for (i = 0; i < range_size; i++, port++) {
  60. *portptr = htons(min + port % range_size);
  61. if (!ip_nat_used_tuple(tuple, conntrack)) {
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }
  67. static void
  68. tcp_manip_pkt(struct iphdr *iph, size_t len,
  69.       const struct ip_conntrack_manip *manip,
  70.       enum ip_nat_manip_type maniptype)
  71. {
  72. struct tcphdr *hdr = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
  73. u_int32_t oldip;
  74. u_int16_t *portptr;
  75. if (maniptype == IP_NAT_MANIP_SRC) {
  76. /* Get rid of src ip and src pt */
  77. oldip = iph->saddr;
  78. portptr = &hdr->source;
  79. } else {
  80. /* Get rid of dst ip and dst pt */
  81. oldip = iph->daddr;
  82. portptr = &hdr->dest;
  83. }
  84. /* this could be a inner header returned in icmp packet; in such
  85.    cases we cannot update the checksum field since it is outside of
  86.    the 8 bytes of transport layer headers we are guaranteed */
  87. if(((void *)&hdr->check + sizeof(hdr->check) - (void *)iph) <= len) {
  88. hdr->check = ip_nat_cheat_check(~oldip, manip->ip,
  89. ip_nat_cheat_check(*portptr ^ 0xFFFF,
  90.    manip->u.tcp.port,
  91.    hdr->check));
  92. }
  93. *portptr = manip->u.tcp.port;
  94. }
  95. static unsigned int
  96. tcp_print(char *buffer,
  97.   const struct ip_conntrack_tuple *match,
  98.   const struct ip_conntrack_tuple *mask)
  99. {
  100. unsigned int len = 0;
  101. if (mask->src.u.tcp.port)
  102. len += sprintf(buffer + len, "srcpt=%u ",
  103.        ntohs(match->src.u.tcp.port));
  104. if (mask->dst.u.tcp.port)
  105. len += sprintf(buffer + len, "dstpt=%u ",
  106.        ntohs(match->dst.u.tcp.port));
  107. return len;
  108. }
  109. static unsigned int
  110. tcp_print_range(char *buffer, const struct ip_nat_range *range)
  111. {
  112. if (range->min.tcp.port != 0 || range->max.tcp.port != 0xFFFF) {
  113. if (range->min.tcp.port == range->max.tcp.port)
  114. return sprintf(buffer, "port %u ",
  115.        ntohs(range->min.tcp.port));
  116. else
  117. return sprintf(buffer, "ports %u-%u ",
  118.        ntohs(range->min.tcp.port),
  119.        ntohs(range->max.tcp.port));
  120. }
  121. else return 0;
  122. }
  123. struct ip_nat_protocol ip_nat_protocol_tcp
  124. = { { NULL, NULL }, "TCP", IPPROTO_TCP,
  125.     tcp_manip_pkt,
  126.     tcp_in_range,
  127.     tcp_unique_tuple,
  128.     tcp_print,
  129.     tcp_print_range
  130. };