nf_nat_proto_udp.c 3.6 KB

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