Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / net / ipv4 / netfilter / ip_nat_proto_gre.c
1 /*
2  * ip_nat_proto_gre.c - Version 2.0
3  *
4  * NAT protocol helper module for GRE.
5  *
6  * GRE is a generic encapsulation protocol, which is generally not very
7  * suited for NAT, as it has no protocol-specific part as port numbers.
8  *
9  * It has an optional key field, which may help us distinguishing two
10  * connections between the same two hosts.
11  *
12  * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
13  *
14  * PPTP is built on top of a modified version of GRE, and has a mandatory
15  * field called "CallID", which serves us for the same purpose as the key
16  * field in plain GRE.
17  *
18  * Documentation about PPTP can be found in RFC 2637
19  *
20  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
21  *
22  * Development of this code funded by Astaro AG (http://www.astaro.com/)
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/ip.h>
28 #include <linux/netfilter_ipv4/ip_nat.h>
29 #include <linux/netfilter_ipv4/ip_nat_rule.h>
30 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
35 MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
36
37 #if 0
38 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \
39                                        __FUNCTION__, ## args)
40 #else
41 #define DEBUGP(x, args...)
42 #endif
43
44 /* is key in given range between min and max */
45 static int
46 gre_in_range(const struct ip_conntrack_tuple *tuple,
47              enum ip_nat_manip_type maniptype,
48              const union ip_conntrack_manip_proto *min,
49              const union ip_conntrack_manip_proto *max)
50 {
51         __be16 key;
52
53         if (maniptype == IP_NAT_MANIP_SRC)
54                 key = tuple->src.u.gre.key;
55         else
56                 key = tuple->dst.u.gre.key;
57
58         return ntohs(key) >= ntohs(min->gre.key)
59                 && ntohs(key) <= ntohs(max->gre.key);
60 }
61
62 /* generate unique tuple ... */
63 static int
64 gre_unique_tuple(struct ip_conntrack_tuple *tuple,
65                  const struct ip_nat_range *range,
66                  enum ip_nat_manip_type maniptype,
67                  const struct ip_conntrack *conntrack)
68 {
69         static u_int16_t key;
70         __be16 *keyptr;
71         unsigned int min, i, range_size;
72
73         if (maniptype == IP_NAT_MANIP_SRC)
74                 keyptr = &tuple->src.u.gre.key;
75         else
76                 keyptr = &tuple->dst.u.gre.key;
77
78         if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
79                 DEBUGP("%p: NATing GRE PPTP\n", conntrack);
80                 min = 1;
81                 range_size = 0xffff;
82         } else {
83                 min = ntohs(range->min.gre.key);
84                 range_size = ntohs(range->max.gre.key) - min + 1;
85         }
86
87         DEBUGP("min = %u, range_size = %u\n", min, range_size);
88
89         for (i = 0; i < range_size; i++, key++) {
90                 *keyptr = htons(min + key % range_size);
91                 if (!ip_nat_used_tuple(tuple, conntrack))
92                         return 1;
93         }
94
95         DEBUGP("%p: no NAT mapping\n", conntrack);
96
97         return 0;
98 }
99
100 /* manipulate a GRE packet according to maniptype */
101 static int
102 gre_manip_pkt(struct sk_buff **pskb,
103               unsigned int iphdroff,
104               const struct ip_conntrack_tuple *tuple,
105               enum ip_nat_manip_type maniptype)
106 {
107         struct gre_hdr *greh;
108         struct gre_hdr_pptp *pgreh;
109         struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
110         unsigned int hdroff = iphdroff + iph->ihl*4;
111
112         /* pgreh includes two optional 32bit fields which are not required
113          * to be there.  That's where the magic '8' comes from */
114         if (!skb_make_writable(pskb, hdroff + sizeof(*pgreh)-8))
115                 return 0;
116
117         greh = (void *)(*pskb)->data + hdroff;
118         pgreh = (struct gre_hdr_pptp *) greh;
119
120         /* we only have destination manip of a packet, since 'source key'
121          * is not present in the packet itself */
122         if (maniptype == IP_NAT_MANIP_DST) {
123                 /* key manipulation is always dest */
124                 switch (greh->version) {
125                 case 0:
126                         if (!greh->key) {
127                                 DEBUGP("can't nat GRE w/o key\n");
128                                 break;
129                         }
130                         if (greh->csum) {
131                                 /* FIXME: Never tested this code... */
132                                 *(gre_csum(greh)) =
133                                         nf_proto_csum_update(*pskb,
134                                                         ~*(gre_key(greh)),
135                                                         tuple->dst.u.gre.key,
136                                                         *(gre_csum(greh)), 0);
137                         }
138                         *(gre_key(greh)) = tuple->dst.u.gre.key;
139                         break;
140                 case GRE_VERSION_PPTP:
141                         DEBUGP("call_id -> 0x%04x\n",
142                                 ntohs(tuple->dst.u.gre.key));
143                         pgreh->call_id = tuple->dst.u.gre.key;
144                         break;
145                 default:
146                         DEBUGP("can't nat unknown GRE version\n");
147                         return 0;
148                         break;
149                 }
150         }
151         return 1;
152 }
153
154 /* nat helper struct */
155 static struct ip_nat_protocol gre = {
156         .name           = "GRE",
157         .protonum       = IPPROTO_GRE,
158         .manip_pkt      = gre_manip_pkt,
159         .in_range       = gre_in_range,
160         .unique_tuple   = gre_unique_tuple,
161 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
162     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
163         .range_to_nfattr        = ip_nat_port_range_to_nfattr,
164         .nfattr_to_range        = ip_nat_port_nfattr_to_range,
165 #endif
166 };
167
168 int __init ip_nat_proto_gre_init(void)
169 {
170         return ip_nat_protocol_register(&gre);
171 }
172
173 void __exit ip_nat_proto_gre_fini(void)
174 {
175         ip_nat_protocol_unregister(&gre);
176 }