ip_nat_proto_udp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/udp.h>
  14. #include <linux/if.h>
  15. #include <linux/netfilter_ipv4/ip_nat.h>
  16. #include <linux/netfilter_ipv4/ip_nat_core.h>
  17. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  18. #include <linux/netfilter_ipv4/ip_nat_protocol.h>
  19. static int
  20. udp_in_range(const struct ip_conntrack_tuple *tuple,
  21. enum ip_nat_manip_type maniptype,
  22. const union ip_conntrack_manip_proto *min,
  23. const union ip_conntrack_manip_proto *max)
  24. {
  25. __be16 port;
  26. if (maniptype == IP_NAT_MANIP_SRC)
  27. port = tuple->src.u.udp.port;
  28. else
  29. port = tuple->dst.u.udp.port;
  30. return ntohs(port) >= ntohs(min->udp.port)
  31. && ntohs(port) <= ntohs(max->udp.port);
  32. }
  33. static int
  34. udp_unique_tuple(struct ip_conntrack_tuple *tuple,
  35. const struct ip_nat_range *range,
  36. enum ip_nat_manip_type maniptype,
  37. const struct ip_conntrack *conntrack)
  38. {
  39. static u_int16_t port;
  40. __be16 *portptr;
  41. unsigned int range_size, min, i;
  42. if (maniptype == IP_NAT_MANIP_SRC)
  43. portptr = &tuple->src.u.udp.port;
  44. else
  45. portptr = &tuple->dst.u.udp.port;
  46. /* If no range specified... */
  47. if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
  48. /* If it's dst rewrite, can't change port */
  49. if (maniptype == IP_NAT_MANIP_DST)
  50. return 0;
  51. if (ntohs(*portptr) < 1024) {
  52. /* Loose convention: >> 512 is credential passing */
  53. if (ntohs(*portptr)<512) {
  54. min = 1;
  55. range_size = 511 - min + 1;
  56. } else {
  57. min = 600;
  58. range_size = 1023 - min + 1;
  59. }
  60. } else {
  61. min = 1024;
  62. range_size = 65535 - 1024 + 1;
  63. }
  64. } else {
  65. min = ntohs(range->min.udp.port);
  66. range_size = ntohs(range->max.udp.port) - min + 1;
  67. }
  68. /* Start from random port to avoid prediction */
  69. if (range->flags & IP_NAT_RANGE_PROTO_RANDOM)
  70. port = net_random();
  71. for (i = 0; i < range_size; i++, port++) {
  72. *portptr = htons(min + port % range_size);
  73. if (!ip_nat_used_tuple(tuple, conntrack))
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static int
  79. udp_manip_pkt(struct sk_buff **pskb,
  80. unsigned int iphdroff,
  81. const struct ip_conntrack_tuple *tuple,
  82. enum ip_nat_manip_type maniptype)
  83. {
  84. struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
  85. struct udphdr *hdr;
  86. unsigned int hdroff = iphdroff + iph->ihl*4;
  87. __be32 oldip, newip;
  88. __be16 *portptr, newport;
  89. if (!skb_make_writable(pskb, hdroff + sizeof(*hdr)))
  90. return 0;
  91. iph = (struct iphdr *)((*pskb)->data + iphdroff);
  92. hdr = (struct udphdr *)((*pskb)->data + hdroff);
  93. if (maniptype == IP_NAT_MANIP_SRC) {
  94. /* Get rid of src ip and src pt */
  95. oldip = iph->saddr;
  96. newip = tuple->src.ip;
  97. newport = tuple->src.u.udp.port;
  98. portptr = &hdr->source;
  99. } else {
  100. /* Get rid of dst ip and dst pt */
  101. oldip = iph->daddr;
  102. newip = tuple->dst.ip;
  103. newport = tuple->dst.u.udp.port;
  104. portptr = &hdr->dest;
  105. }
  106. if (hdr->check || (*pskb)->ip_summed == CHECKSUM_PARTIAL) {
  107. nf_proto_csum_replace4(&hdr->check, *pskb, oldip, newip, 1);
  108. nf_proto_csum_replace2(&hdr->check, *pskb, *portptr, newport, 0);
  109. if (!hdr->check)
  110. hdr->check = CSUM_MANGLED_0;
  111. }
  112. *portptr = newport;
  113. return 1;
  114. }
  115. struct ip_nat_protocol ip_nat_protocol_udp = {
  116. .name = "UDP",
  117. .protonum = IPPROTO_UDP,
  118. .me = THIS_MODULE,
  119. .manip_pkt = udp_manip_pkt,
  120. .in_range = udp_in_range,
  121. .unique_tuple = udp_unique_tuple,
  122. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  123. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  124. .range_to_nfattr = ip_nat_port_range_to_nfattr,
  125. .nfattr_to_range = ip_nat_port_nfattr_to_range,
  126. #endif
  127. };