blob: c9dbd550df39d935234e9d240cfce0b09b77db27 [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
2 * nf_nat_proto_gre.c
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/skbuff.h>
28#include <linux/ip.h>
29
30#include <net/netfilter/nf_nat.h>
31#include <net/netfilter/nf_nat_rule.h>
32#include <net/netfilter/nf_nat_protocol.h>
33#include <linux/netfilter/nf_conntrack_proto_gre.h>
34
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
37MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
38
Patrick McHardyf09943f2006-12-02 22:09:41 -080039/* is key in given range between min and max */
40static int
41gre_in_range(const struct nf_conntrack_tuple *tuple,
42 enum nf_nat_manip_type maniptype,
43 const union nf_conntrack_man_proto *min,
44 const union nf_conntrack_man_proto *max)
45{
46 __be16 key;
47
48 if (maniptype == IP_NAT_MANIP_SRC)
49 key = tuple->src.u.gre.key;
50 else
51 key = tuple->dst.u.gre.key;
52
53 return ntohs(key) >= ntohs(min->gre.key) &&
54 ntohs(key) <= ntohs(max->gre.key);
55}
56
57/* generate unique tuple ... */
58static int
59gre_unique_tuple(struct nf_conntrack_tuple *tuple,
60 const struct nf_nat_range *range,
61 enum nf_nat_manip_type maniptype,
62 const struct nf_conn *conntrack)
63{
64 static u_int16_t key;
65 __be16 *keyptr;
66 unsigned int min, i, range_size;
67
Jorge Boncomptec2a19102007-05-03 03:34:42 -070068 /* If there is no master conntrack we are not PPTP,
69 do not change tuples */
70 if (!conntrack->master)
71 return 0;
72
Patrick McHardyf09943f2006-12-02 22:09:41 -080073 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)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070079 pr_debug("%p: NATing GRE PPTP\n", conntrack);
Patrick McHardyf09943f2006-12-02 22:09:41 -080080 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
Patrick McHardy0d537782007-07-07 22:39:38 -070087 pr_debug("min = %u, range_size = %u\n", min, range_size);
Patrick McHardyf09943f2006-12-02 22:09:41 -080088
89 for (i = 0; i < range_size; i++, key++) {
90 *keyptr = htons(min + key % range_size);
91 if (!nf_nat_used_tuple(tuple, conntrack))
92 return 1;
93 }
94
Patrick McHardy0d537782007-07-07 22:39:38 -070095 pr_debug("%p: no NAT mapping\n", conntrack);
Patrick McHardyf09943f2006-12-02 22:09:41 -080096 return 0;
97}
98
99/* manipulate a GRE packet according to maniptype */
100static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700101gre_manip_pkt(struct sk_buff *skb, unsigned int iphdroff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800102 const struct nf_conntrack_tuple *tuple,
103 enum nf_nat_manip_type maniptype)
104{
105 struct gre_hdr *greh;
106 struct gre_hdr_pptp *pgreh;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700107 struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800108 unsigned int hdroff = iphdroff + iph->ihl * 4;
109
110 /* pgreh includes two optional 32bit fields which are not required
111 * to be there. That's where the magic '8' comes from */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700112 if (!skb_make_writable(skb, hdroff + sizeof(*pgreh) - 8))
Patrick McHardyf09943f2006-12-02 22:09:41 -0800113 return 0;
114
Herbert Xu3db05fe2007-10-15 00:53:15 -0700115 greh = (void *)skb->data + hdroff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800116 pgreh = (struct gre_hdr_pptp *)greh;
117
118 /* we only have destination manip of a packet, since 'source key'
119 * is not present in the packet itself */
120 if (maniptype != IP_NAT_MANIP_DST)
121 return 1;
122 switch (greh->version) {
Jorge Boncomptec2a19102007-05-03 03:34:42 -0700123 case GRE_VERSION_1701:
124 /* We do not currently NAT any GREv0 packets.
125 * Try to behave like "nf_nat_proto_unknown" */
Patrick McHardyf09943f2006-12-02 22:09:41 -0800126 break;
127 case GRE_VERSION_PPTP:
Patrick McHardy0d537782007-07-07 22:39:38 -0700128 pr_debug("call_id -> 0x%04x\n", ntohs(tuple->dst.u.gre.key));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800129 pgreh->call_id = tuple->dst.u.gre.key;
130 break;
131 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700132 pr_debug("can't nat unknown GRE version\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800133 return 0;
134 }
135 return 1;
136}
137
Patrick McHardy2b628a02007-12-17 22:37:36 -0800138static const struct nf_nat_protocol gre = {
Patrick McHardyf09943f2006-12-02 22:09:41 -0800139 .name = "GRE",
140 .protonum = IPPROTO_GRE,
Patrick McHardy3ee9e762007-12-17 22:37:20 -0800141 .me = THIS_MODULE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800142 .manip_pkt = gre_manip_pkt,
143 .in_range = gre_in_range,
144 .unique_tuple = gre_unique_tuple,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800145#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700146 .range_to_nlattr = nf_nat_port_range_to_nlattr,
147 .nlattr_to_range = nf_nat_port_nlattr_to_range,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800148#endif
149};
150
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800151static int __init nf_nat_proto_gre_init(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800152{
153 return nf_nat_protocol_register(&gre);
154}
155
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800156static void __exit nf_nat_proto_gre_fini(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800157{
158 nf_nat_protocol_unregister(&gre);
159}
160
161module_init(nf_nat_proto_gre_init);
162module_exit(nf_nat_proto_gre_fini);
163
164void nf_nat_need_gre(void)
165{
166 return;
167}
168EXPORT_SYMBOL_GPL(nf_nat_need_gre);