blob: 47fff91c9ae67b756f6388acd02c3201507c41ed [file] [log] [blame]
Patrick McHardy937e0df2008-03-20 15:15:47 +01001/* (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/ip.h>
13
14#include <linux/netfilter.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070016#include <net/secure_seq.h>
Patrick McHardy937e0df2008-03-20 15:15:47 +010017#include <net/netfilter/nf_nat.h>
18#include <net/netfilter/nf_nat_core.h>
19#include <net/netfilter/nf_nat_rule.h>
20#include <net/netfilter/nf_nat_protocol.h>
21
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020022bool nf_nat_proto_in_range(const struct nf_conntrack_tuple *tuple,
23 enum nf_nat_manip_type maniptype,
24 const union nf_conntrack_man_proto *min,
25 const union nf_conntrack_man_proto *max)
Patrick McHardy937e0df2008-03-20 15:15:47 +010026{
27 __be16 port;
28
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010029 if (maniptype == NF_NAT_MANIP_SRC)
Patrick McHardy937e0df2008-03-20 15:15:47 +010030 port = tuple->src.u.all;
31 else
32 port = tuple->dst.u.all;
33
34 return ntohs(port) >= ntohs(min->all) &&
35 ntohs(port) <= ntohs(max->all);
36}
37EXPORT_SYMBOL_GPL(nf_nat_proto_in_range);
38
Changli Gaof43dc982010-08-02 17:20:54 +020039void nf_nat_proto_unique_tuple(struct nf_conntrack_tuple *tuple,
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010040 const struct nf_nat_ipv4_range *range,
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020041 enum nf_nat_manip_type maniptype,
42 const struct nf_conn *ct,
43 u_int16_t *rover)
Patrick McHardy937e0df2008-03-20 15:15:47 +010044{
45 unsigned int range_size, min, i;
46 __be16 *portptr;
Patrick McHardy5abd3632008-04-14 11:15:46 +020047 u_int16_t off;
Patrick McHardy937e0df2008-03-20 15:15:47 +010048
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010049 if (maniptype == NF_NAT_MANIP_SRC)
Patrick McHardy937e0df2008-03-20 15:15:47 +010050 portptr = &tuple->src.u.all;
51 else
52 portptr = &tuple->dst.u.all;
53
54 /* If no range specified... */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010055 if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
Patrick McHardy937e0df2008-03-20 15:15:47 +010056 /* If it's dst rewrite, can't change port */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010057 if (maniptype == NF_NAT_MANIP_DST)
Changli Gaof43dc982010-08-02 17:20:54 +020058 return;
Patrick McHardy937e0df2008-03-20 15:15:47 +010059
60 if (ntohs(*portptr) < 1024) {
61 /* Loose convention: >> 512 is credential passing */
62 if (ntohs(*portptr) < 512) {
63 min = 1;
64 range_size = 511 - min + 1;
65 } else {
66 min = 600;
67 range_size = 1023 - min + 1;
68 }
69 } else {
70 min = 1024;
71 range_size = 65535 - 1024 + 1;
72 }
73 } else {
74 min = ntohs(range->min.all);
75 range_size = ntohs(range->max.all) - min + 1;
76 }
77
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010078 if (range->flags & NF_NAT_RANGE_PROTO_RANDOM)
Stephen Hemminger9f593652008-08-18 21:32:32 -070079 off = secure_ipv4_port_ephemeral(tuple->src.u3.ip, tuple->dst.u3.ip,
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010080 maniptype == NF_NAT_MANIP_SRC
Stephen Hemminger9f593652008-08-18 21:32:32 -070081 ? tuple->dst.u.all
82 : tuple->src.u.all);
83 else
84 off = *rover;
Patrick McHardy937e0df2008-03-20 15:15:47 +010085
Changli Gao2452a992010-08-02 17:35:49 +020086 for (i = 0; ; ++off) {
Patrick McHardy5abd3632008-04-14 11:15:46 +020087 *portptr = htons(min + off % range_size);
Changli Gao2452a992010-08-02 17:35:49 +020088 if (++i != range_size && nf_nat_used_tuple(tuple, ct))
Patrick McHardy5abd3632008-04-14 11:15:46 +020089 continue;
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010090 if (!(range->flags & NF_NAT_RANGE_PROTO_RANDOM))
Patrick McHardy5abd3632008-04-14 11:15:46 +020091 *rover = off;
Changli Gaof43dc982010-08-02 17:20:54 +020092 return;
Patrick McHardy937e0df2008-03-20 15:15:47 +010093 }
Changli Gaof43dc982010-08-02 17:20:54 +020094 return;
Patrick McHardy937e0df2008-03-20 15:15:47 +010095}
96EXPORT_SYMBOL_GPL(nf_nat_proto_unique_tuple);
Patrick McHardy535b57c2008-04-14 11:15:47 +020097
98#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
99int nf_nat_proto_range_to_nlattr(struct sk_buff *skb,
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100100 const struct nf_nat_ipv4_range *range)
Patrick McHardy535b57c2008-04-14 11:15:47 +0200101{
102 NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MIN, range->min.all);
103 NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MAX, range->max.all);
104 return 0;
105
106nla_put_failure:
107 return -1;
108}
109EXPORT_SYMBOL_GPL(nf_nat_proto_nlattr_to_range);
110
111int nf_nat_proto_nlattr_to_range(struct nlattr *tb[],
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100112 struct nf_nat_ipv4_range *range)
Patrick McHardy535b57c2008-04-14 11:15:47 +0200113{
Patrick McHardy535b57c2008-04-14 11:15:47 +0200114 if (tb[CTA_PROTONAT_PORT_MIN]) {
Patrick McHardy535b57c2008-04-14 11:15:47 +0200115 range->min.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
Patrick McHardyca6a5072008-04-14 11:15:47 +0200116 range->max.all = range->min.tcp.port;
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100117 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardy535b57c2008-04-14 11:15:47 +0200118 }
Patrick McHardyca6a5072008-04-14 11:15:47 +0200119 if (tb[CTA_PROTONAT_PORT_MAX]) {
Patrick McHardy535b57c2008-04-14 11:15:47 +0200120 range->max.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100121 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardy535b57c2008-04-14 11:15:47 +0200122 }
Patrick McHardyca6a5072008-04-14 11:15:47 +0200123 return 0;
Patrick McHardy535b57c2008-04-14 11:15:47 +0200124}
125EXPORT_SYMBOL_GPL(nf_nat_proto_range_to_nlattr);
126#endif