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> | |
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 | ||
35 | MODULE_LICENSE("GPL"); | |
36 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); | |
37 | MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE"); | |
38 | ||
39 | #if 0 | |
40 | #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \ | |
41 | __FUNCTION__, ## args) | |
42 | #else | |
43 | #define DEBUGP(x, args...) | |
44 | #endif | |
45 | ||
46 | /* is key in given range between min and max */ | |
47 | static int | |
48 | gre_in_range(const struct nf_conntrack_tuple *tuple, | |
49 | enum nf_nat_manip_type maniptype, | |
50 | const union nf_conntrack_man_proto *min, | |
51 | const union nf_conntrack_man_proto *max) | |
52 | { | |
53 | __be16 key; | |
54 | ||
55 | if (maniptype == IP_NAT_MANIP_SRC) | |
56 | key = tuple->src.u.gre.key; | |
57 | else | |
58 | key = tuple->dst.u.gre.key; | |
59 | ||
60 | return ntohs(key) >= ntohs(min->gre.key) && | |
61 | ntohs(key) <= ntohs(max->gre.key); | |
62 | } | |
63 | ||
64 | /* generate unique tuple ... */ | |
65 | static int | |
66 | gre_unique_tuple(struct nf_conntrack_tuple *tuple, | |
67 | const struct nf_nat_range *range, | |
68 | enum nf_nat_manip_type maniptype, | |
69 | const struct nf_conn *conntrack) | |
70 | { | |
71 | static u_int16_t key; | |
72 | __be16 *keyptr; | |
73 | unsigned int min, i, range_size; | |
74 | ||
75 | if (maniptype == IP_NAT_MANIP_SRC) | |
76 | keyptr = &tuple->src.u.gre.key; | |
77 | else | |
78 | keyptr = &tuple->dst.u.gre.key; | |
79 | ||
80 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { | |
81 | DEBUGP("%p: NATing GRE PPTP\n", conntrack); | |
82 | min = 1; | |
83 | range_size = 0xffff; | |
84 | } else { | |
85 | min = ntohs(range->min.gre.key); | |
86 | range_size = ntohs(range->max.gre.key) - min + 1; | |
87 | } | |
88 | ||
89 | DEBUGP("min = %u, range_size = %u\n", min, range_size); | |
90 | ||
91 | for (i = 0; i < range_size; i++, key++) { | |
92 | *keyptr = htons(min + key % range_size); | |
93 | if (!nf_nat_used_tuple(tuple, conntrack)) | |
94 | return 1; | |
95 | } | |
96 | ||
97 | DEBUGP("%p: no NAT mapping\n", conntrack); | |
98 | return 0; | |
99 | } | |
100 | ||
101 | /* manipulate a GRE packet according to maniptype */ | |
102 | static int | |
103 | gre_manip_pkt(struct sk_buff **pskb, unsigned int iphdroff, | |
104 | const struct nf_conntrack_tuple *tuple, | |
105 | enum nf_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 | return 1; | |
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 | nf_proto_csum_replace4(gre_csum(greh), *pskb, | |
133 | *(gre_key(greh)), | |
134 | tuple->dst.u.gre.key, 0); | |
135 | } | |
136 | *(gre_key(greh)) = tuple->dst.u.gre.key; | |
137 | break; | |
138 | case GRE_VERSION_PPTP: | |
139 | DEBUGP("call_id -> 0x%04x\n", ntohs(tuple->dst.u.gre.key)); | |
140 | pgreh->call_id = tuple->dst.u.gre.key; | |
141 | break; | |
142 | default: | |
143 | DEBUGP("can't nat unknown GRE version\n"); | |
144 | return 0; | |
145 | } | |
146 | return 1; | |
147 | } | |
148 | ||
149 | static struct nf_nat_protocol gre __read_mostly = { | |
150 | .name = "GRE", | |
151 | .protonum = IPPROTO_GRE, | |
152 | .manip_pkt = gre_manip_pkt, | |
153 | .in_range = gre_in_range, | |
154 | .unique_tuple = gre_unique_tuple, | |
e281db5c | 155 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
f09943fe PM |
156 | .range_to_nfattr = nf_nat_port_range_to_nfattr, |
157 | .nfattr_to_range = nf_nat_port_nfattr_to_range, | |
158 | #endif | |
159 | }; | |
160 | ||
161 | int __init nf_nat_proto_gre_init(void) | |
162 | { | |
163 | return nf_nat_protocol_register(&gre); | |
164 | } | |
165 | ||
166 | void __exit nf_nat_proto_gre_fini(void) | |
167 | { | |
168 | nf_nat_protocol_unregister(&gre); | |
169 | } | |
170 | ||
171 | module_init(nf_nat_proto_gre_init); | |
172 | module_exit(nf_nat_proto_gre_fini); | |
173 | ||
174 | void nf_nat_need_gre(void) | |
175 | { | |
176 | return; | |
177 | } | |
178 | EXPORT_SYMBOL_GPL(nf_nat_need_gre); |