Commit | Line | Data |
---|---|---|
f09943fe PM |
1 | /* |
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> | |
c7232c99 | 31 | #include <net/netfilter/nf_nat_l4proto.h> |
f09943fe PM |
32 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
33 | ||
34 | MODULE_LICENSE("GPL"); | |
35 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); | |
36 | MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE"); | |
37 | ||
f09943fe | 38 | /* generate unique tuple ... */ |
f43dc98b | 39 | static void |
c7232c99 PM |
40 | gre_unique_tuple(const struct nf_nat_l3proto *l3proto, |
41 | struct nf_conntrack_tuple *tuple, | |
42 | const struct nf_nat_range *range, | |
f09943fe | 43 | enum nf_nat_manip_type maniptype, |
c88130bc | 44 | const struct nf_conn *ct) |
f09943fe PM |
45 | { |
46 | static u_int16_t key; | |
47 | __be16 *keyptr; | |
48 | unsigned int min, i, range_size; | |
49 | ||
c2a1910b JB |
50 | /* If there is no master conntrack we are not PPTP, |
51 | do not change tuples */ | |
c88130bc | 52 | if (!ct->master) |
f43dc98b | 53 | return; |
c2a1910b | 54 | |
cbc9f2f4 | 55 | if (maniptype == NF_NAT_MANIP_SRC) |
f09943fe PM |
56 | keyptr = &tuple->src.u.gre.key; |
57 | else | |
58 | keyptr = &tuple->dst.u.gre.key; | |
59 | ||
cbc9f2f4 | 60 | if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) { |
c88130bc | 61 | pr_debug("%p: NATing GRE PPTP\n", ct); |
f09943fe PM |
62 | min = 1; |
63 | range_size = 0xffff; | |
64 | } else { | |
c7232c99 PM |
65 | min = ntohs(range->min_proto.gre.key); |
66 | range_size = ntohs(range->max_proto.gre.key) - min + 1; | |
f09943fe PM |
67 | } |
68 | ||
0d53778e | 69 | pr_debug("min = %u, range_size = %u\n", min, range_size); |
f09943fe | 70 | |
2452a99d | 71 | for (i = 0; ; ++key) { |
f09943fe | 72 | *keyptr = htons(min + key % range_size); |
2452a99d | 73 | if (++i == range_size || !nf_nat_used_tuple(tuple, ct)) |
f43dc98b | 74 | return; |
f09943fe PM |
75 | } |
76 | ||
c88130bc | 77 | pr_debug("%p: no NAT mapping\n", ct); |
f43dc98b | 78 | return; |
f09943fe PM |
79 | } |
80 | ||
81 | /* manipulate a GRE packet according to maniptype */ | |
f2ea825f | 82 | static bool |
c7232c99 PM |
83 | gre_manip_pkt(struct sk_buff *skb, |
84 | const struct nf_nat_l3proto *l3proto, | |
85 | unsigned int iphdroff, unsigned int hdroff, | |
f09943fe PM |
86 | const struct nf_conntrack_tuple *tuple, |
87 | enum nf_nat_manip_type maniptype) | |
88 | { | |
12c33aa2 | 89 | const struct gre_hdr *greh; |
f09943fe | 90 | struct gre_hdr_pptp *pgreh; |
f09943fe PM |
91 | |
92 | /* pgreh includes two optional 32bit fields which are not required | |
93 | * to be there. That's where the magic '8' comes from */ | |
3db05fea | 94 | if (!skb_make_writable(skb, hdroff + sizeof(*pgreh) - 8)) |
f2ea825f | 95 | return false; |
f09943fe | 96 | |
3db05fea | 97 | greh = (void *)skb->data + hdroff; |
f09943fe PM |
98 | pgreh = (struct gre_hdr_pptp *)greh; |
99 | ||
100 | /* we only have destination manip of a packet, since 'source key' | |
101 | * is not present in the packet itself */ | |
cbc9f2f4 | 102 | if (maniptype != NF_NAT_MANIP_DST) |
f2ea825f | 103 | return true; |
f09943fe | 104 | switch (greh->version) { |
c2a1910b JB |
105 | case GRE_VERSION_1701: |
106 | /* We do not currently NAT any GREv0 packets. | |
107 | * Try to behave like "nf_nat_proto_unknown" */ | |
f09943fe PM |
108 | break; |
109 | case GRE_VERSION_PPTP: | |
0d53778e | 110 | pr_debug("call_id -> 0x%04x\n", ntohs(tuple->dst.u.gre.key)); |
f09943fe PM |
111 | pgreh->call_id = tuple->dst.u.gre.key; |
112 | break; | |
113 | default: | |
0d53778e | 114 | pr_debug("can't nat unknown GRE version\n"); |
f2ea825f | 115 | return false; |
f09943fe | 116 | } |
f2ea825f | 117 | return true; |
f09943fe PM |
118 | } |
119 | ||
c7232c99 PM |
120 | static const struct nf_nat_l4proto gre = { |
121 | .l4proto = IPPROTO_GRE, | |
f09943fe | 122 | .manip_pkt = gre_manip_pkt, |
c7232c99 | 123 | .in_range = nf_nat_l4proto_in_range, |
f09943fe | 124 | .unique_tuple = gre_unique_tuple, |
e281db5c | 125 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c7232c99 | 126 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
f09943fe PM |
127 | #endif |
128 | }; | |
129 | ||
f4f6fb71 | 130 | static int __init nf_nat_proto_gre_init(void) |
f09943fe | 131 | { |
c7232c99 | 132 | return nf_nat_l4proto_register(NFPROTO_IPV4, &gre); |
f09943fe PM |
133 | } |
134 | ||
f4f6fb71 | 135 | static void __exit nf_nat_proto_gre_fini(void) |
f09943fe | 136 | { |
c7232c99 | 137 | nf_nat_l4proto_unregister(NFPROTO_IPV4, &gre); |
f09943fe PM |
138 | } |
139 | ||
140 | module_init(nf_nat_proto_gre_init); | |
141 | module_exit(nf_nat_proto_gre_fini); | |
142 | ||
143 | void nf_nat_need_gre(void) | |
144 | { | |
145 | return; | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(nf_nat_need_gre); |