~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_nat_proto_common.c

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 10 #include <linux/types.h>
 11 #include <linux/random.h>
 12 #include <linux/netfilter.h>
 13 #include <linux/export.h>
 14 
 15 #include <net/netfilter/nf_nat.h>
 16 #include <net/netfilter/nf_nat_core.h>
 17 #include <net/netfilter/nf_nat_l3proto.h>
 18 #include <net/netfilter/nf_nat_l4proto.h>
 19 
 20 bool nf_nat_l4proto_in_range(const struct nf_conntrack_tuple *tuple,
 21                              enum nf_nat_manip_type maniptype,
 22                              const union nf_conntrack_man_proto *min,
 23                              const union nf_conntrack_man_proto *max)
 24 {
 25         __be16 port;
 26 
 27         if (maniptype == NF_NAT_MANIP_SRC)
 28                 port = tuple->src.u.all;
 29         else
 30                 port = tuple->dst.u.all;
 31 
 32         return ntohs(port) >= ntohs(min->all) &&
 33                ntohs(port) <= ntohs(max->all);
 34 }
 35 EXPORT_SYMBOL_GPL(nf_nat_l4proto_in_range);
 36 
 37 void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto,
 38                                  struct nf_conntrack_tuple *tuple,
 39                                  const struct nf_nat_range2 *range,
 40                                  enum nf_nat_manip_type maniptype,
 41                                  const struct nf_conn *ct,
 42                                  u16 *rover)
 43 {
 44         unsigned int range_size, min, max, i;
 45         __be16 *portptr;
 46         u_int16_t off;
 47 
 48         if (maniptype == NF_NAT_MANIP_SRC)
 49                 portptr = &tuple->src.u.all;
 50         else
 51                 portptr = &tuple->dst.u.all;
 52 
 53         /* If no range specified... */
 54         if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
 55                 /* If it's dst rewrite, can't change port */
 56                 if (maniptype == NF_NAT_MANIP_DST)
 57                         return;
 58 
 59                 if (ntohs(*portptr) < 1024) {
 60                         /* Loose convention: >> 512 is credential passing */
 61                         if (ntohs(*portptr) < 512) {
 62                                 min = 1;
 63                                 range_size = 511 - min + 1;
 64                         } else {
 65                                 min = 600;
 66                                 range_size = 1023 - min + 1;
 67                         }
 68                 } else {
 69                         min = 1024;
 70                         range_size = 65535 - 1024 + 1;
 71                 }
 72         } else {
 73                 min = ntohs(range->min_proto.all);
 74                 max = ntohs(range->max_proto.all);
 75                 if (unlikely(max < min))
 76                         swap(max, min);
 77                 range_size = max - min + 1;
 78         }
 79 
 80         if (range->flags & NF_NAT_RANGE_PROTO_RANDOM) {
 81                 off = l3proto->secure_port(tuple, maniptype == NF_NAT_MANIP_SRC
 82                                                   ? tuple->dst.u.all
 83                                                   : tuple->src.u.all);
 84         } else if (range->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) {
 85                 off = prandom_u32();
 86         } else if (range->flags & NF_NAT_RANGE_PROTO_OFFSET) {
 87                 off = (ntohs(*portptr) - ntohs(range->base_proto.all));
 88         } else {
 89                 off = *rover;
 90         }
 91 
 92         for (i = 0; ; ++off) {
 93                 *portptr = htons(min + off % range_size);
 94                 if (++i != range_size && nf_nat_used_tuple(tuple, ct))
 95                         continue;
 96                 if (!(range->flags & (NF_NAT_RANGE_PROTO_RANDOM_ALL|
 97                                         NF_NAT_RANGE_PROTO_OFFSET)))
 98                         *rover = off;
 99                 return;
100         }
101 }
102 EXPORT_SYMBOL_GPL(nf_nat_l4proto_unique_tuple);
103 
104 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
105 int nf_nat_l4proto_nlattr_to_range(struct nlattr *tb[],
106                                    struct nf_nat_range2 *range)
107 {
108         if (tb[CTA_PROTONAT_PORT_MIN]) {
109                 range->min_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
110                 range->max_proto.all = range->min_proto.all;
111                 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
112         }
113         if (tb[CTA_PROTONAT_PORT_MAX]) {
114                 range->max_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
115                 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
116         }
117         return 0;
118 }
119 EXPORT_SYMBOL_GPL(nf_nat_l4proto_nlattr_to_range);
120 #endif
121 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp