]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/netfilter/nf_nat_proto_common.c
a124213fb9da9d5245c8260f7171a4e639b6d971
[linux-2.6-omap-h63xx.git] / net / ipv4 / netfilter / nf_nat_proto_common.c
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/ip.h>
13
14 #include <linux/netfilter.h>
15 #include <net/netfilter/nf_nat.h>
16 #include <net/netfilter/nf_nat_core.h>
17 #include <net/netfilter/nf_nat_rule.h>
18 #include <net/netfilter/nf_nat_protocol.h>
19
20 int nf_nat_proto_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 == IP_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_proto_in_range);
36
37 int nf_nat_proto_unique_tuple(struct nf_conntrack_tuple *tuple,
38                               const struct nf_nat_range *range,
39                               enum nf_nat_manip_type maniptype,
40                               const struct nf_conn *ct,
41                               u_int16_t *rover)
42 {
43         unsigned int range_size, min, i;
44         __be16 *portptr;
45
46         if (maniptype == IP_NAT_MANIP_SRC)
47                 portptr = &tuple->src.u.all;
48         else
49                 portptr = &tuple->dst.u.all;
50
51         /* If no range specified... */
52         if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
53                 /* If it's dst rewrite, can't change port */
54                 if (maniptype == IP_NAT_MANIP_DST)
55                         return 0;
56
57                 if (ntohs(*portptr) < 1024) {
58                         /* Loose convention: >> 512 is credential passing */
59                         if (ntohs(*portptr) < 512) {
60                                 min = 1;
61                                 range_size = 511 - min + 1;
62                         } else {
63                                 min = 600;
64                                 range_size = 1023 - min + 1;
65                         }
66                 } else {
67                         min = 1024;
68                         range_size = 65535 - 1024 + 1;
69                 }
70         } else {
71                 min = ntohs(range->min.all);
72                 range_size = ntohs(range->max.all) - min + 1;
73         }
74
75         if (range->flags & IP_NAT_RANGE_PROTO_RANDOM)
76                 *rover = net_random();
77
78         for (i = 0; i < range_size; i++, (*rover)++) {
79                 *portptr = htons(min + *rover % range_size);
80                 if (!nf_nat_used_tuple(tuple, ct))
81                         return 1;
82         }
83         return 0;
84 }
85 EXPORT_SYMBOL_GPL(nf_nat_proto_unique_tuple);