ip_nat_proto_tcp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/init.h>
  10. #include <linux/random.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/ip.h>
  13. #include <linux/tcp.h>
  14. #include <linux/if.h>
  15. #include <linux/netfilter/nfnetlink_conntrack.h>
  16. #include <linux/netfilter_ipv4/ip_nat.h>
  17. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  18. #include <linux/netfilter_ipv4/ip_nat_protocol.h>
  19. #include <linux/netfilter_ipv4/ip_nat_core.h>
  20. static int
  21. tcp_in_range(const struct ip_conntrack_tuple *tuple,
  22. enum ip_nat_manip_type maniptype,
  23. const union ip_conntrack_manip_proto *min,
  24. const union ip_conntrack_manip_proto *max)
  25. {
  26. __be16 port;
  27. if (maniptype == IP_NAT_MANIP_SRC)
  28. port = tuple->src.u.tcp.port;
  29. else
  30. port = tuple->dst.u.tcp.port;
  31. return ntohs(port) >= ntohs(min->tcp.port)
  32. && ntohs(port) <= ntohs(max->tcp.port);
  33. }
  34. static int
  35. tcp_unique_tuple(struct ip_conntrack_tuple *tuple,
  36. const struct ip_nat_range *range,
  37. enum ip_nat_manip_type maniptype,
  38. const struct ip_conntrack *conntrack)
  39. {
  40. static u_int16_t port;
  41. __be16 *portptr;
  42. unsigned int range_size, min, i;
  43. if (maniptype == IP_NAT_MANIP_SRC)
  44. portptr = &tuple->src.u.tcp.port;
  45. else
  46. portptr = &tuple->dst.u.tcp.port;
  47. /* If no range specified... */
  48. if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
  49. /* If it's dst rewrite, can't change port */
  50. if (maniptype == IP_NAT_MANIP_DST)
  51. return 0;
  52. /* Map privileged onto privileged. */
  53. if (ntohs(*portptr) < 1024) {
  54. /* Loose convention: >> 512 is credential passing */
  55. if (ntohs(*portptr)<512) {
  56. min = 1;
  57. range_size = 511 - min + 1;
  58. } else {
  59. min = 600;
  60. range_size = 1023 - min + 1;
  61. }
  62. } else {
  63. min = 1024;
  64. range_size = 65535 - 1024 + 1;
  65. }
  66. } else {
  67. min = ntohs(range->min.tcp.port);
  68. range_size = ntohs(range->max.tcp.port) - min + 1;
  69. }
  70. /* Start from random port to avoid prediction */
  71. if (range->flags & IP_NAT_RANGE_PROTO_RANDOM)
  72. port = net_random();
  73. for (i = 0; i < range_size; i++, port++) {
  74. *portptr = htons(min + port % range_size);
  75. if (!ip_nat_used_tuple(tuple, conntrack)) {
  76. return 1;
  77. }
  78. }
  79. return 0;
  80. }
  81. static int
  82. tcp_manip_pkt(struct sk_buff **pskb,
  83. unsigned int iphdroff,
  84. const struct ip_conntrack_tuple *tuple,
  85. enum ip_nat_manip_type maniptype)
  86. {
  87. struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
  88. struct tcphdr *hdr;
  89. unsigned int hdroff = iphdroff + iph->ihl*4;
  90. __be32 oldip, newip;
  91. __be16 *portptr, newport, oldport;
  92. int hdrsize = 8; /* TCP connection tracking guarantees this much */
  93. /* this could be a inner header returned in icmp packet; in such
  94. cases we cannot update the checksum field since it is outside of
  95. the 8 bytes of transport layer headers we are guaranteed */
  96. if ((*pskb)->len >= hdroff + sizeof(struct tcphdr))
  97. hdrsize = sizeof(struct tcphdr);
  98. if (!skb_make_writable(pskb, hdroff + hdrsize))
  99. return 0;
  100. iph = (struct iphdr *)((*pskb)->data + iphdroff);
  101. hdr = (struct tcphdr *)((*pskb)->data + hdroff);
  102. if (maniptype == IP_NAT_MANIP_SRC) {
  103. /* Get rid of src ip and src pt */
  104. oldip = iph->saddr;
  105. newip = tuple->src.ip;
  106. newport = tuple->src.u.tcp.port;
  107. portptr = &hdr->source;
  108. } else {
  109. /* Get rid of dst ip and dst pt */
  110. oldip = iph->daddr;
  111. newip = tuple->dst.ip;
  112. newport = tuple->dst.u.tcp.port;
  113. portptr = &hdr->dest;
  114. }
  115. oldport = *portptr;
  116. *portptr = newport;
  117. if (hdrsize < sizeof(*hdr))
  118. return 1;
  119. nf_proto_csum_replace4(&hdr->check, *pskb, oldip, newip, 1);
  120. nf_proto_csum_replace2(&hdr->check, *pskb, oldport, newport, 0);
  121. return 1;
  122. }
  123. struct ip_nat_protocol ip_nat_protocol_tcp = {
  124. .name = "TCP",
  125. .protonum = IPPROTO_TCP,
  126. .me = THIS_MODULE,
  127. .manip_pkt = tcp_manip_pkt,
  128. .in_range = tcp_in_range,
  129. .unique_tuple = tcp_unique_tuple,
  130. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  131. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  132. .range_to_nfattr = ip_nat_port_range_to_nfattr,
  133. .nfattr_to_range = ip_nat_port_nfattr_to_range,
  134. #endif
  135. };