blob: 95810202d849f02502695c8ca96c4abda301a324 [file] [log] [blame]
Harald Welte926b50f2005-09-19 15:33:08 -07001/*
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 *
Patrick McHardyedd5a322006-09-20 12:07:39 -07009 * It has an optional key field, which may help us distinguishing two
Harald Welte926b50f2005-09-19 15:33:08 -070010 * connections between the same two hosts.
11 *
Patrick McHardyedd5a322006-09-20 12:07:39 -070012 * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
Harald Welte926b50f2005-09-19 15:33:08 -070013 *
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
Harald Welte926b50f2005-09-19 15:33:08 -070026#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
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
35MODULE_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 */
45static int
46gre_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{
Alexey Dobriyana4677042006-05-19 02:16:29 -070051 __be16 key;
Harald Welte926b50f2005-09-19 15:33:08 -070052
53 if (maniptype == IP_NAT_MANIP_SRC)
54 key = tuple->src.u.gre.key;
55 else
56 key = tuple->dst.u.gre.key;
57
Alexey Dobriyana4677042006-05-19 02:16:29 -070058 return ntohs(key) >= ntohs(min->gre.key)
59 && ntohs(key) <= ntohs(max->gre.key);
Harald Welte926b50f2005-09-19 15:33:08 -070060}
61
62/* generate unique tuple ... */
Patrick McHardyedd5a322006-09-20 12:07:39 -070063static int
Harald Welte926b50f2005-09-19 15:33:08 -070064gre_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;
Patrick McHardy955b9442006-09-20 12:08:03 -070070 __be16 *keyptr;
Harald Welte926b50f2005-09-19 15:33:08 -070071 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 {
Alexey Dobriyana4677042006-05-19 02:16:29 -070083 min = ntohs(range->min.gre.key);
84 range_size = ntohs(range->max.gre.key) - min + 1;
Harald Welte926b50f2005-09-19 15:33:08 -070085 }
86
Patrick McHardyedd5a322006-09-20 12:07:39 -070087 DEBUGP("min = %u, range_size = %u\n", min, range_size);
Harald Welte926b50f2005-09-19 15:33:08 -070088
89 for (i = 0; i < range_size; i++, key++) {
Alexey Dobriyana4677042006-05-19 02:16:29 -070090 *keyptr = htons(min + key % range_size);
Harald Welte926b50f2005-09-19 15:33:08 -070091 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 */
101static int
102gre_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
Patrick McHardyedd5a322006-09-20 12:07:39 -0700120 /* we only have destination manip of a packet, since 'source key'
Harald Welte926b50f2005-09-19 15:33:08 -0700121 * 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... */
Al Viro43bc0ca2006-11-14 21:43:23 -0800132 nf_proto_csum_replace4(gre_csum(greh), *pskb,
133 *(gre_key(greh)),
134 tuple->dst.u.gre.key, 0);
Harald Welte926b50f2005-09-19 15:33:08 -0700135 }
136 *(gre_key(greh)) = tuple->dst.u.gre.key;
137 break;
138 case GRE_VERSION_PPTP:
Patrick McHardyedd5a322006-09-20 12:07:39 -0700139 DEBUGP("call_id -> 0x%04x\n",
Harald Welted8115522005-11-03 13:05:20 +0100140 ntohs(tuple->dst.u.gre.key));
141 pgreh->call_id = tuple->dst.u.gre.key;
Harald Welte926b50f2005-09-19 15:33:08 -0700142 break;
143 default:
144 DEBUGP("can't nat unknown GRE version\n");
145 return 0;
146 break;
147 }
148 }
149 return 1;
150}
151
Harald Welte926b50f2005-09-19 15:33:08 -0700152/* nat helper struct */
Patrick McHardyedd5a322006-09-20 12:07:39 -0700153static struct ip_nat_protocol gre = {
154 .name = "GRE",
Harald Welte926b50f2005-09-19 15:33:08 -0700155 .protonum = IPPROTO_GRE,
156 .manip_pkt = gre_manip_pkt,
157 .in_range = gre_in_range,
158 .unique_tuple = gre_unique_tuple,
Harald Welte926b50f2005-09-19 15:33:08 -0700159#if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
160 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
161 .range_to_nfattr = ip_nat_port_range_to_nfattr,
162 .nfattr_to_range = ip_nat_port_nfattr_to_range,
163#endif
164};
Patrick McHardyedd5a322006-09-20 12:07:39 -0700165
Harald Welte926b50f2005-09-19 15:33:08 -0700166int __init ip_nat_proto_gre_init(void)
167{
168 return ip_nat_protocol_register(&gre);
169}
170
171void __exit ip_nat_proto_gre_fini(void)
172{
173 ip_nat_protocol_unregister(&gre);
174}