blob: 9414923f1e156939bbd68acf22ca76d2e7e34698 [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 *
Patrick McHardyf229f6c2013-04-06 15:24:29 +020024 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
25 *
Patrick McHardyf09943f2006-12-02 22:09:41 -080026 */
27
28#include <linux/module.h>
29#include <linux/skbuff.h>
30#include <linux/ip.h>
31
32#include <net/netfilter/nf_nat.h>
Patrick McHardyc7232c92012-08-26 19:14:06 +020033#include <net/netfilter/nf_nat_l4proto.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080034#include <linux/netfilter/nf_conntrack_proto_gre.h>
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
38MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
39
Patrick McHardyf09943f2006-12-02 22:09:41 -080040/* generate unique tuple ... */
Changli Gaof43dc982010-08-02 17:20:54 +020041static void
Patrick McHardyc7232c92012-08-26 19:14:06 +020042gre_unique_tuple(const struct nf_nat_l3proto *l3proto,
43 struct nf_conntrack_tuple *tuple,
44 const struct nf_nat_range *range,
Patrick McHardyf09943f2006-12-02 22:09:41 -080045 enum nf_nat_manip_type maniptype,
Patrick McHardyc88130b2008-01-31 04:42:11 -080046 const struct nf_conn *ct)
Patrick McHardyf09943f2006-12-02 22:09:41 -080047{
48 static u_int16_t key;
49 __be16 *keyptr;
50 unsigned int min, i, range_size;
51
Jorge Boncomptec2a19102007-05-03 03:34:42 -070052 /* If there is no master conntrack we are not PPTP,
53 do not change tuples */
Patrick McHardyc88130b2008-01-31 04:42:11 -080054 if (!ct->master)
Changli Gaof43dc982010-08-02 17:20:54 +020055 return;
Jorge Boncomptec2a19102007-05-03 03:34:42 -070056
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010057 if (maniptype == NF_NAT_MANIP_SRC)
Patrick McHardyf09943f2006-12-02 22:09:41 -080058 keyptr = &tuple->src.u.gre.key;
59 else
60 keyptr = &tuple->dst.u.gre.key;
61
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010062 if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -080063 pr_debug("%p: NATing GRE PPTP\n", ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -080064 min = 1;
65 range_size = 0xffff;
66 } else {
Patrick McHardyc7232c92012-08-26 19:14:06 +020067 min = ntohs(range->min_proto.gre.key);
68 range_size = ntohs(range->max_proto.gre.key) - min + 1;
Patrick McHardyf09943f2006-12-02 22:09:41 -080069 }
70
Patrick McHardy0d537782007-07-07 22:39:38 -070071 pr_debug("min = %u, range_size = %u\n", min, range_size);
Patrick McHardyf09943f2006-12-02 22:09:41 -080072
Changli Gao2452a992010-08-02 17:35:49 +020073 for (i = 0; ; ++key) {
Patrick McHardyf09943f2006-12-02 22:09:41 -080074 *keyptr = htons(min + key % range_size);
Changli Gao2452a992010-08-02 17:35:49 +020075 if (++i == range_size || !nf_nat_used_tuple(tuple, ct))
Changli Gaof43dc982010-08-02 17:20:54 +020076 return;
Patrick McHardyf09943f2006-12-02 22:09:41 -080077 }
78
Patrick McHardyc88130b2008-01-31 04:42:11 -080079 pr_debug("%p: no NAT mapping\n", ct);
Changli Gaof43dc982010-08-02 17:20:54 +020080 return;
Patrick McHardyf09943f2006-12-02 22:09:41 -080081}
82
83/* manipulate a GRE packet according to maniptype */
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020084static bool
Patrick McHardyc7232c92012-08-26 19:14:06 +020085gre_manip_pkt(struct sk_buff *skb,
86 const struct nf_nat_l3proto *l3proto,
87 unsigned int iphdroff, unsigned int hdroff,
Patrick McHardyf09943f2006-12-02 22:09:41 -080088 const struct nf_conntrack_tuple *tuple,
89 enum nf_nat_manip_type maniptype)
90{
Jan Engelhardt12c33aa2008-04-14 11:15:54 +020091 const struct gre_hdr *greh;
Patrick McHardyf09943f2006-12-02 22:09:41 -080092 struct gre_hdr_pptp *pgreh;
Patrick McHardyf09943f2006-12-02 22:09:41 -080093
94 /* pgreh includes two optional 32bit fields which are not required
95 * to be there. That's where the magic '8' comes from */
Herbert Xu3db05fe2007-10-15 00:53:15 -070096 if (!skb_make_writable(skb, hdroff + sizeof(*pgreh) - 8))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020097 return false;
Patrick McHardyf09943f2006-12-02 22:09:41 -080098
Herbert Xu3db05fe2007-10-15 00:53:15 -070099 greh = (void *)skb->data + hdroff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800100 pgreh = (struct gre_hdr_pptp *)greh;
101
102 /* we only have destination manip of a packet, since 'source key'
103 * is not present in the packet itself */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100104 if (maniptype != NF_NAT_MANIP_DST)
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200105 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800106 switch (greh->version) {
Jorge Boncomptec2a19102007-05-03 03:34:42 -0700107 case GRE_VERSION_1701:
108 /* We do not currently NAT any GREv0 packets.
109 * Try to behave like "nf_nat_proto_unknown" */
Patrick McHardyf09943f2006-12-02 22:09:41 -0800110 break;
111 case GRE_VERSION_PPTP:
Patrick McHardy0d537782007-07-07 22:39:38 -0700112 pr_debug("call_id -> 0x%04x\n", ntohs(tuple->dst.u.gre.key));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800113 pgreh->call_id = tuple->dst.u.gre.key;
114 break;
115 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700116 pr_debug("can't nat unknown GRE version\n");
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200117 return false;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800118 }
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200119 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800120}
121
Patrick McHardyc7232c92012-08-26 19:14:06 +0200122static const struct nf_nat_l4proto gre = {
123 .l4proto = IPPROTO_GRE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800124 .manip_pkt = gre_manip_pkt,
Patrick McHardyc7232c92012-08-26 19:14:06 +0200125 .in_range = nf_nat_l4proto_in_range,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800126 .unique_tuple = gre_unique_tuple,
Duan Jiong24de3d32014-06-30 09:19:32 +0800127#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyc7232c92012-08-26 19:14:06 +0200128 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800129#endif
130};
131
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800132static int __init nf_nat_proto_gre_init(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800133{
Patrick McHardyc7232c92012-08-26 19:14:06 +0200134 return nf_nat_l4proto_register(NFPROTO_IPV4, &gre);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800135}
136
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800137static void __exit nf_nat_proto_gre_fini(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800138{
Patrick McHardyc7232c92012-08-26 19:14:06 +0200139 nf_nat_l4proto_unregister(NFPROTO_IPV4, &gre);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800140}
141
142module_init(nf_nat_proto_gre_init);
143module_exit(nf_nat_proto_gre_fini);
144
145void nf_nat_need_gre(void)
146{
147 return;
148}
149EXPORT_SYMBOL_GPL(nf_nat_need_gre);