PageRenderTime 92ms CodeModel.GetById 22ms RepoModel.GetById 19ms app.codeStats 7ms

/net/ipv4/netfilter/nf_nat_proto_common.c

https://bitbucket.org/abioy/linux
C | 124 lines | 99 code | 14 blank | 11 comment | 20 complexity | 7d5e1c2004834fd120150bc0c3a0e722 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  3. * (C) 2008 Patrick McHardy <kaber@trash.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/random.h>
  11. #include <linux/ip.h>
  12. #include <linux/netfilter.h>
  13. #include <net/netfilter/nf_nat.h>
  14. #include <net/netfilter/nf_nat_core.h>
  15. #include <net/netfilter/nf_nat_rule.h>
  16. #include <net/netfilter/nf_nat_protocol.h>
  17. bool nf_nat_proto_in_range(const struct nf_conntrack_tuple *tuple,
  18. enum nf_nat_manip_type maniptype,
  19. const union nf_conntrack_man_proto *min,
  20. const union nf_conntrack_man_proto *max)
  21. {
  22. __be16 port;
  23. if (maniptype == IP_NAT_MANIP_SRC)
  24. port = tuple->src.u.all;
  25. else
  26. port = tuple->dst.u.all;
  27. return ntohs(port) >= ntohs(min->all) &&
  28. ntohs(port) <= ntohs(max->all);
  29. }
  30. EXPORT_SYMBOL_GPL(nf_nat_proto_in_range);
  31. bool nf_nat_proto_unique_tuple(struct nf_conntrack_tuple *tuple,
  32. const struct nf_nat_range *range,
  33. enum nf_nat_manip_type maniptype,
  34. const struct nf_conn *ct,
  35. u_int16_t *rover)
  36. {
  37. unsigned int range_size, min, i;
  38. __be16 *portptr;
  39. u_int16_t off;
  40. if (maniptype == IP_NAT_MANIP_SRC)
  41. portptr = &tuple->src.u.all;
  42. else
  43. portptr = &tuple->dst.u.all;
  44. /* If no range specified... */
  45. if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
  46. /* If it's dst rewrite, can't change port */
  47. if (maniptype == IP_NAT_MANIP_DST)
  48. return false;
  49. if (ntohs(*portptr) < 1024) {
  50. /* Loose convention: >> 512 is credential passing */
  51. if (ntohs(*portptr) < 512) {
  52. min = 1;
  53. range_size = 511 - min + 1;
  54. } else {
  55. min = 600;
  56. range_size = 1023 - min + 1;
  57. }
  58. } else {
  59. min = 1024;
  60. range_size = 65535 - 1024 + 1;
  61. }
  62. } else {
  63. min = ntohs(range->min.all);
  64. range_size = ntohs(range->max.all) - min + 1;
  65. }
  66. if (range->flags & IP_NAT_RANGE_PROTO_RANDOM)
  67. off = secure_ipv4_port_ephemeral(tuple->src.u3.ip, tuple->dst.u3.ip,
  68. maniptype == IP_NAT_MANIP_SRC
  69. ? tuple->dst.u.all
  70. : tuple->src.u.all);
  71. else
  72. off = *rover;
  73. for (i = 0; i < range_size; i++, off++) {
  74. *portptr = htons(min + off % range_size);
  75. if (nf_nat_used_tuple(tuple, ct))
  76. continue;
  77. if (!(range->flags & IP_NAT_RANGE_PROTO_RANDOM))
  78. *rover = off;
  79. return true;
  80. }
  81. return false;
  82. }
  83. EXPORT_SYMBOL_GPL(nf_nat_proto_unique_tuple);
  84. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  85. int nf_nat_proto_range_to_nlattr(struct sk_buff *skb,
  86. const struct nf_nat_range *range)
  87. {
  88. NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MIN, range->min.all);
  89. NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MAX, range->max.all);
  90. return 0;
  91. nla_put_failure:
  92. return -1;
  93. }
  94. EXPORT_SYMBOL_GPL(nf_nat_proto_nlattr_to_range);
  95. int nf_nat_proto_nlattr_to_range(struct nlattr *tb[],
  96. struct nf_nat_range *range)
  97. {
  98. if (tb[CTA_PROTONAT_PORT_MIN]) {
  99. range->min.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
  100. range->max.all = range->min.tcp.port;
  101. range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
  102. }
  103. if (tb[CTA_PROTONAT_PORT_MAX]) {
  104. range->max.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
  105. range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
  106. }
  107. return 0;
  108. }
  109. EXPORT_SYMBOL_GPL(nf_nat_proto_range_to_nlattr);
  110. #endif