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>
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.
10 #include <linux/types.h>
11 #include <linux/random.h>
12 #include <linux/netfilter.h>
13 #include <linux/export.h>
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>
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)
27 if (maniptype == NF_NAT_MANIP_SRC)
28 port = tuple->src.u.all;
30 port = tuple->dst.u.all;
32 return ntohs(port) >= ntohs(min->all) &&
33 ntohs(port) <= ntohs(max->all);
35 EXPORT_SYMBOL_GPL(nf_nat_l4proto_in_range);
37 void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto,
38 struct nf_conntrack_tuple *tuple,
39 const struct nf_nat_range *range,
40 enum nf_nat_manip_type maniptype,
41 const struct nf_conn *ct)
43 unsigned int range_size, min, max, i, attempts;
46 static const unsigned int max_attempts = 128;
48 if (maniptype == NF_NAT_MANIP_SRC)
49 portptr = &tuple->src.u.all;
51 portptr = &tuple->dst.u.all;
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)
59 if (ntohs(*portptr) < 1024) {
60 /* Loose convention: >> 512 is credential passing */
61 if (ntohs(*portptr) < 512) {
63 range_size = 511 - min + 1;
66 range_size = 1023 - min + 1;
70 range_size = 65535 - 1024 + 1;
73 min = ntohs(range->min_proto.all);
74 max = ntohs(range->max_proto.all);
75 if (unlikely(max < min))
77 range_size = max - min + 1;
80 if (range->flags & NF_NAT_RANGE_PROTO_RANDOM) {
81 off = l3proto->secure_port(tuple, maniptype == NF_NAT_MANIP_SRC
84 } else if (range->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) {
90 attempts = range_size;
91 if (attempts > max_attempts)
92 attempts = max_attempts;
94 /* We are in softirq; doing a search of the entire range risks
95 * soft lockup when all tuples are already used.
97 * If we can't find any free port from first offset, pick a new
98 * one and try again, with ever smaller search window.
101 for (i = 0; i < attempts; i++, off++) {
102 *portptr = htons(min + off % range_size);
103 if (!nf_nat_used_tuple(tuple, ct))
107 if (attempts >= range_size || attempts < 16)
113 EXPORT_SYMBOL_GPL(nf_nat_l4proto_unique_tuple);
115 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
116 int nf_nat_l4proto_nlattr_to_range(struct nlattr *tb[],
117 struct nf_nat_range *range)
119 if (tb[CTA_PROTONAT_PORT_MIN]) {
120 range->min_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
121 range->max_proto.all = range->min_proto.all;
122 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
124 if (tb[CTA_PROTONAT_PORT_MAX]) {
125 range->max_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
126 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
130 EXPORT_SYMBOL_GPL(nf_nat_l4proto_nlattr_to_range);