nf_nat_proto_common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/netfilter.h>
  12. #include <linux/export.h>
  13. #include <net/netfilter/nf_nat.h>
  14. #include <net/netfilter/nf_nat_core.h>
  15. #include <net/netfilter/nf_nat_l3proto.h>
  16. #include <net/netfilter/nf_nat_l4proto.h>
  17. bool nf_nat_l4proto_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 == NF_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_l4proto_in_range);
  31. void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto,
  32. struct nf_conntrack_tuple *tuple,
  33. const struct nf_nat_range *range,
  34. enum nf_nat_manip_type maniptype,
  35. const struct nf_conn *ct,
  36. u16 *rover)
  37. {
  38. unsigned int range_size, min, max, i;
  39. __be16 *portptr;
  40. u_int16_t off;
  41. if (maniptype == NF_NAT_MANIP_SRC)
  42. portptr = &tuple->src.u.all;
  43. else
  44. portptr = &tuple->dst.u.all;
  45. /* If no range specified... */
  46. if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
  47. /* If it's dst rewrite, can't change port */
  48. if (maniptype == NF_NAT_MANIP_DST)
  49. return;
  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_proto.all);
  65. max = ntohs(range->max_proto.all);
  66. if (unlikely(max < min))
  67. swap(max, min);
  68. range_size = max - min + 1;
  69. }
  70. if (range->flags & NF_NAT_RANGE_PROTO_RANDOM) {
  71. off = l3proto->secure_port(tuple, maniptype == NF_NAT_MANIP_SRC
  72. ? tuple->dst.u.all
  73. : tuple->src.u.all);
  74. } else if (range->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) {
  75. off = prandom_u32();
  76. } else {
  77. off = *rover;
  78. }
  79. for (i = 0; ; ++off) {
  80. *portptr = htons(min + off % range_size);
  81. if (++i != range_size && nf_nat_used_tuple(tuple, ct))
  82. continue;
  83. if (!(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL))
  84. *rover = off;
  85. return;
  86. }
  87. }
  88. EXPORT_SYMBOL_GPL(nf_nat_l4proto_unique_tuple);
  89. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  90. int nf_nat_l4proto_nlattr_to_range(struct nlattr *tb[],
  91. struct nf_nat_range *range)
  92. {
  93. if (tb[CTA_PROTONAT_PORT_MIN]) {
  94. range->min_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
  95. range->max_proto.all = range->min_proto.all;
  96. range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  97. }
  98. if (tb[CTA_PROTONAT_PORT_MAX]) {
  99. range->max_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
  100. range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL_GPL(nf_nat_l4proto_nlattr_to_range);
  105. #endif