]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
f09943fe PM |
2 | /* |
3 | * Connection tracking support for PPTP (Point to Point Tunneling Protocol). | |
4 | * PPTP is a a protocol for creating virtual private networks. | |
5 | * It is a specification defined by Microsoft and some vendors | |
6 | * working with Microsoft. PPTP is built on top of a modified | |
7 | * version of the Internet Generic Routing Encapsulation Protocol. | |
8 | * GRE is defined in RFC 1701 and RFC 1702. Documentation of | |
9 | * PPTP can be found in RFC 2637 | |
10 | * | |
11 | * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org> | |
12 | * | |
13 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
14 | * | |
f229f6ce PM |
15 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
16 | * | |
f09943fe PM |
17 | * Limitations: |
18 | * - We blindly assume that control connections are always | |
19 | * established in PNS->PAC direction. This is a violation | |
38124328 | 20 | * of RFC 2637 |
f09943fe PM |
21 | * - We can only support one single call within each session |
22 | * TODO: | |
23 | * - testing of incoming PPTP calls | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/skbuff.h> | |
28 | #include <linux/in.h> | |
29 | #include <linux/tcp.h> | |
30 | ||
31 | #include <net/netfilter/nf_conntrack.h> | |
32 | #include <net/netfilter/nf_conntrack_core.h> | |
33 | #include <net/netfilter/nf_conntrack_helper.h> | |
5d0aa2cc | 34 | #include <net/netfilter/nf_conntrack_zones.h> |
f09943fe PM |
35 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
36 | #include <linux/netfilter/nf_conntrack_pptp.h> | |
37 | ||
38 | #define NF_CT_PPTP_VERSION "3.1" | |
39 | ||
40 | MODULE_LICENSE("GPL"); | |
41 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); | |
42 | MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP"); | |
43 | MODULE_ALIAS("ip_conntrack_pptp"); | |
4dc06f96 | 44 | MODULE_ALIAS_NFCT_HELPER("pptp"); |
f09943fe PM |
45 | |
46 | static DEFINE_SPINLOCK(nf_pptp_lock); | |
47 | ||
48 | int | |
3db05fea | 49 | (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb, |
f09943fe | 50 | struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
051966c0 | 51 | unsigned int protoff, struct PptpControlHeader *ctlh, |
f09943fe PM |
52 | union pptp_ctrl_union *pptpReq) __read_mostly; |
53 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound); | |
54 | ||
55 | int | |
3db05fea | 56 | (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb, |
f09943fe | 57 | struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
051966c0 | 58 | unsigned int protoff, struct PptpControlHeader *ctlh, |
f09943fe PM |
59 | union pptp_ctrl_union *pptpReq) __read_mostly; |
60 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound); | |
61 | ||
62 | void | |
63 | (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig, | |
64 | struct nf_conntrack_expect *expect_reply) | |
65 | __read_mostly; | |
66 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre); | |
67 | ||
68 | void | |
69 | (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct, | |
70 | struct nf_conntrack_expect *exp) __read_mostly; | |
71 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn); | |
72 | ||
e9d376f0 | 73 | #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
f09943fe | 74 | /* PptpControlMessageType names */ |
4c559f15 PNA |
75 | static const char *const pptp_msg_name_array[PPTP_MSG_MAX + 1] = { |
76 | [0] = "UNKNOWN_MESSAGE", | |
77 | [PPTP_START_SESSION_REQUEST] = "START_SESSION_REQUEST", | |
78 | [PPTP_START_SESSION_REPLY] = "START_SESSION_REPLY", | |
79 | [PPTP_STOP_SESSION_REQUEST] = "STOP_SESSION_REQUEST", | |
80 | [PPTP_STOP_SESSION_REPLY] = "STOP_SESSION_REPLY", | |
81 | [PPTP_ECHO_REQUEST] = "ECHO_REQUEST", | |
82 | [PPTP_ECHO_REPLY] = "ECHO_REPLY", | |
83 | [PPTP_OUT_CALL_REQUEST] = "OUT_CALL_REQUEST", | |
84 | [PPTP_OUT_CALL_REPLY] = "OUT_CALL_REPLY", | |
85 | [PPTP_IN_CALL_REQUEST] = "IN_CALL_REQUEST", | |
86 | [PPTP_IN_CALL_REPLY] = "IN_CALL_REPLY", | |
87 | [PPTP_IN_CALL_CONNECT] = "IN_CALL_CONNECT", | |
88 | [PPTP_CALL_CLEAR_REQUEST] = "CALL_CLEAR_REQUEST", | |
89 | [PPTP_CALL_DISCONNECT_NOTIFY] = "CALL_DISCONNECT_NOTIFY", | |
90 | [PPTP_WAN_ERROR_NOTIFY] = "WAN_ERROR_NOTIFY", | |
91 | [PPTP_SET_LINK_INFO] = "SET_LINK_INFO" | |
f09943fe | 92 | }; |
4c559f15 | 93 | |
4946ea5c | 94 | const char *pptp_msg_name(u_int16_t msg) |
4c559f15 PNA |
95 | { |
96 | if (msg > PPTP_MSG_MAX) | |
97 | return pptp_msg_name_array[0]; | |
98 | ||
99 | return pptp_msg_name_array[msg]; | |
100 | } | |
f09943fe | 101 | EXPORT_SYMBOL(pptp_msg_name); |
f09943fe PM |
102 | #endif |
103 | ||
104 | #define SECS *HZ | |
105 | #define MINS * 60 SECS | |
106 | #define HOURS * 60 MINS | |
107 | ||
108 | #define PPTP_GRE_TIMEOUT (10 MINS) | |
109 | #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS) | |
110 | ||
111 | static void pptp_expectfn(struct nf_conn *ct, | |
112 | struct nf_conntrack_expect *exp) | |
113 | { | |
0e6e75af | 114 | struct net *net = nf_ct_net(ct); |
f09943fe | 115 | typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn; |
0d53778e | 116 | pr_debug("increasing timeouts\n"); |
f09943fe PM |
117 | |
118 | /* increase timeout of GRE data channel conntrack entry */ | |
119 | ct->proto.gre.timeout = PPTP_GRE_TIMEOUT; | |
120 | ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT; | |
121 | ||
122 | /* Can you see how rusty this code is, compared with the pre-2.6.11 | |
123 | * one? That's what happened to my shiny newnat of 2002 ;( -HW */ | |
124 | ||
f09943fe | 125 | nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn); |
7399072a | 126 | if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK) |
f09943fe PM |
127 | nf_nat_pptp_expectfn(ct, exp); |
128 | else { | |
129 | struct nf_conntrack_tuple inv_t; | |
130 | struct nf_conntrack_expect *exp_other; | |
131 | ||
132 | /* obviously this tuple inversion only works until you do NAT */ | |
303e0c55 | 133 | nf_ct_invert_tuple(&inv_t, &exp->tuple); |
0d53778e | 134 | pr_debug("trying to unexpect other dir: "); |
3c9fba65 | 135 | nf_ct_dump_tuple(&inv_t); |
f09943fe | 136 | |
5d0aa2cc | 137 | exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t); |
f09943fe PM |
138 | if (exp_other) { |
139 | /* delete other expectation. */ | |
0d53778e | 140 | pr_debug("found\n"); |
6823645d PM |
141 | nf_ct_unexpect_related(exp_other); |
142 | nf_ct_expect_put(exp_other); | |
f09943fe | 143 | } else { |
0d53778e | 144 | pr_debug("not found\n"); |
f09943fe PM |
145 | } |
146 | } | |
f09943fe PM |
147 | } |
148 | ||
5d0aa2cc | 149 | static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct, |
0e6e75af | 150 | const struct nf_conntrack_tuple *t) |
f09943fe | 151 | { |
9ddd0ed0 | 152 | const struct nf_conntrack_tuple_hash *h; |
308ac914 | 153 | const struct nf_conntrack_zone *zone; |
f09943fe PM |
154 | struct nf_conntrack_expect *exp; |
155 | struct nf_conn *sibling; | |
156 | ||
0d53778e | 157 | pr_debug("trying to timeout ct or exp for tuple "); |
3c9fba65 | 158 | nf_ct_dump_tuple(t); |
f09943fe | 159 | |
308ac914 | 160 | zone = nf_ct_zone(ct); |
5d0aa2cc | 161 | h = nf_conntrack_find_get(net, zone, t); |
f09943fe PM |
162 | if (h) { |
163 | sibling = nf_ct_tuplehash_to_ctrack(h); | |
0d53778e | 164 | pr_debug("setting timeout of conntrack %p to 0\n", sibling); |
f09943fe PM |
165 | sibling->proto.gre.timeout = 0; |
166 | sibling->proto.gre.stream_timeout = 0; | |
f330a7fd | 167 | nf_ct_kill(sibling); |
f09943fe PM |
168 | nf_ct_put(sibling); |
169 | return 1; | |
170 | } else { | |
5d0aa2cc | 171 | exp = nf_ct_expect_find_get(net, zone, t); |
f09943fe | 172 | if (exp) { |
0d53778e | 173 | pr_debug("unexpect_related of expect %p\n", exp); |
6823645d PM |
174 | nf_ct_unexpect_related(exp); |
175 | nf_ct_expect_put(exp); | |
f09943fe PM |
176 | return 1; |
177 | } | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | ||
182 | /* timeout GRE data connections */ | |
183 | static void pptp_destroy_siblings(struct nf_conn *ct) | |
184 | { | |
0e6e75af | 185 | struct net *net = nf_ct_net(ct); |
1afc5679 | 186 | const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
187 | struct nf_conntrack_tuple t; |
188 | ||
189 | nf_ct_gre_keymap_destroy(ct); | |
190 | ||
191 | /* try original (pns->pac) tuple */ | |
192 | memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t)); | |
193 | t.dst.protonum = IPPROTO_GRE; | |
1afc5679 PNA |
194 | t.src.u.gre.key = ct_pptp_info->pns_call_id; |
195 | t.dst.u.gre.key = ct_pptp_info->pac_call_id; | |
5d0aa2cc | 196 | if (!destroy_sibling_or_exp(net, ct, &t)) |
0d53778e | 197 | pr_debug("failed to timeout original pns->pac ct/exp\n"); |
f09943fe PM |
198 | |
199 | /* try reply (pac->pns) tuple */ | |
200 | memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t)); | |
201 | t.dst.protonum = IPPROTO_GRE; | |
1afc5679 PNA |
202 | t.src.u.gre.key = ct_pptp_info->pac_call_id; |
203 | t.dst.u.gre.key = ct_pptp_info->pns_call_id; | |
5d0aa2cc | 204 | if (!destroy_sibling_or_exp(net, ct, &t)) |
0d53778e | 205 | pr_debug("failed to timeout reply pac->pns ct/exp\n"); |
f09943fe PM |
206 | } |
207 | ||
208 | /* expect GRE connections (PNS->PAC and PAC->PNS direction) */ | |
209 | static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid) | |
210 | { | |
211 | struct nf_conntrack_expect *exp_orig, *exp_reply; | |
212 | enum ip_conntrack_dir dir; | |
213 | int ret = 1; | |
214 | typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre; | |
215 | ||
6823645d | 216 | exp_orig = nf_ct_expect_alloc(ct); |
f09943fe PM |
217 | if (exp_orig == NULL) |
218 | goto out; | |
219 | ||
6823645d | 220 | exp_reply = nf_ct_expect_alloc(ct); |
f09943fe PM |
221 | if (exp_reply == NULL) |
222 | goto out_put_orig; | |
223 | ||
224 | /* original direction, PNS->PAC */ | |
225 | dir = IP_CT_DIR_ORIGINAL; | |
6002f266 | 226 | nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT, |
5e8fbe2a | 227 | nf_ct_l3num(ct), |
6823645d PM |
228 | &ct->tuplehash[dir].tuple.src.u3, |
229 | &ct->tuplehash[dir].tuple.dst.u3, | |
230 | IPPROTO_GRE, &peer_callid, &callid); | |
f09943fe PM |
231 | exp_orig->expectfn = pptp_expectfn; |
232 | ||
233 | /* reply direction, PAC->PNS */ | |
234 | dir = IP_CT_DIR_REPLY; | |
6002f266 | 235 | nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT, |
5e8fbe2a | 236 | nf_ct_l3num(ct), |
6823645d PM |
237 | &ct->tuplehash[dir].tuple.src.u3, |
238 | &ct->tuplehash[dir].tuple.dst.u3, | |
239 | IPPROTO_GRE, &callid, &peer_callid); | |
f09943fe PM |
240 | exp_reply->expectfn = pptp_expectfn; |
241 | ||
242 | nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre); | |
243 | if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK) | |
244 | nf_nat_pptp_exp_gre(exp_orig, exp_reply); | |
3c00fb0b | 245 | if (nf_ct_expect_related(exp_orig, 0) != 0) |
f09943fe | 246 | goto out_put_both; |
3c00fb0b | 247 | if (nf_ct_expect_related(exp_reply, 0) != 0) |
f09943fe PM |
248 | goto out_unexpect_orig; |
249 | ||
250 | /* Add GRE keymap entries */ | |
251 | if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0) | |
252 | goto out_unexpect_both; | |
253 | if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) { | |
254 | nf_ct_gre_keymap_destroy(ct); | |
255 | goto out_unexpect_both; | |
256 | } | |
257 | ret = 0; | |
258 | ||
259 | out_put_both: | |
6823645d | 260 | nf_ct_expect_put(exp_reply); |
f09943fe | 261 | out_put_orig: |
6823645d | 262 | nf_ct_expect_put(exp_orig); |
f09943fe PM |
263 | out: |
264 | return ret; | |
265 | ||
266 | out_unexpect_both: | |
6823645d | 267 | nf_ct_unexpect_related(exp_reply); |
f09943fe | 268 | out_unexpect_orig: |
6823645d | 269 | nf_ct_unexpect_related(exp_orig); |
f09943fe PM |
270 | goto out_put_both; |
271 | } | |
272 | ||
2fe7c321 | 273 | static int |
051966c0 | 274 | pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff, |
f09943fe PM |
275 | struct PptpControlHeader *ctlh, |
276 | union pptp_ctrl_union *pptpReq, | |
277 | unsigned int reqlen, | |
278 | struct nf_conn *ct, | |
279 | enum ip_conntrack_info ctinfo) | |
280 | { | |
1afc5679 | 281 | struct nf_ct_pptp_master *info = nfct_help_data(ct); |
f09943fe PM |
282 | u_int16_t msg; |
283 | __be16 cid = 0, pcid = 0; | |
284 | typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound; | |
285 | ||
286 | msg = ntohs(ctlh->messageType); | |
4c559f15 | 287 | pr_debug("inbound control message %s\n", pptp_msg_name(msg)); |
f09943fe PM |
288 | |
289 | switch (msg) { | |
290 | case PPTP_START_SESSION_REPLY: | |
291 | /* server confirms new control session */ | |
292 | if (info->sstate < PPTP_SESSION_REQUESTED) | |
293 | goto invalid; | |
294 | if (pptpReq->srep.resultCode == PPTP_START_OK) | |
295 | info->sstate = PPTP_SESSION_CONFIRMED; | |
296 | else | |
297 | info->sstate = PPTP_SESSION_ERROR; | |
298 | break; | |
299 | ||
300 | case PPTP_STOP_SESSION_REPLY: | |
301 | /* server confirms end of control session */ | |
302 | if (info->sstate > PPTP_SESSION_STOPREQ) | |
303 | goto invalid; | |
304 | if (pptpReq->strep.resultCode == PPTP_STOP_OK) | |
305 | info->sstate = PPTP_SESSION_NONE; | |
306 | else | |
307 | info->sstate = PPTP_SESSION_ERROR; | |
308 | break; | |
309 | ||
310 | case PPTP_OUT_CALL_REPLY: | |
311 | /* server accepted call, we now expect GRE frames */ | |
312 | if (info->sstate != PPTP_SESSION_CONFIRMED) | |
313 | goto invalid; | |
314 | if (info->cstate != PPTP_CALL_OUT_REQ && | |
315 | info->cstate != PPTP_CALL_OUT_CONF) | |
316 | goto invalid; | |
317 | ||
318 | cid = pptpReq->ocack.callID; | |
319 | pcid = pptpReq->ocack.peersCallID; | |
320 | if (info->pns_call_id != pcid) | |
321 | goto invalid; | |
4c559f15 | 322 | pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name(msg), |
0d53778e | 323 | ntohs(cid), ntohs(pcid)); |
f09943fe PM |
324 | |
325 | if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) { | |
326 | info->cstate = PPTP_CALL_OUT_CONF; | |
327 | info->pac_call_id = cid; | |
328 | exp_gre(ct, cid, pcid); | |
329 | } else | |
330 | info->cstate = PPTP_CALL_NONE; | |
331 | break; | |
332 | ||
333 | case PPTP_IN_CALL_REQUEST: | |
334 | /* server tells us about incoming call request */ | |
335 | if (info->sstate != PPTP_SESSION_CONFIRMED) | |
336 | goto invalid; | |
337 | ||
338 | cid = pptpReq->icreq.callID; | |
4c559f15 | 339 | pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid)); |
f09943fe PM |
340 | info->cstate = PPTP_CALL_IN_REQ; |
341 | info->pac_call_id = cid; | |
342 | break; | |
343 | ||
344 | case PPTP_IN_CALL_CONNECT: | |
345 | /* server tells us about incoming call established */ | |
346 | if (info->sstate != PPTP_SESSION_CONFIRMED) | |
347 | goto invalid; | |
348 | if (info->cstate != PPTP_CALL_IN_REP && | |
349 | info->cstate != PPTP_CALL_IN_CONF) | |
350 | goto invalid; | |
351 | ||
352 | pcid = pptpReq->iccon.peersCallID; | |
353 | cid = info->pac_call_id; | |
354 | ||
355 | if (info->pns_call_id != pcid) | |
356 | goto invalid; | |
357 | ||
4c559f15 | 358 | pr_debug("%s, PCID=%X\n", pptp_msg_name(msg), ntohs(pcid)); |
f09943fe PM |
359 | info->cstate = PPTP_CALL_IN_CONF; |
360 | ||
361 | /* we expect a GRE connection from PAC to PNS */ | |
362 | exp_gre(ct, cid, pcid); | |
363 | break; | |
364 | ||
365 | case PPTP_CALL_DISCONNECT_NOTIFY: | |
366 | /* server confirms disconnect */ | |
367 | cid = pptpReq->disc.callID; | |
4c559f15 | 368 | pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid)); |
f09943fe PM |
369 | info->cstate = PPTP_CALL_NONE; |
370 | ||
371 | /* untrack this call id, unexpect GRE packets */ | |
372 | pptp_destroy_siblings(ct); | |
373 | break; | |
374 | ||
375 | case PPTP_WAN_ERROR_NOTIFY: | |
4c6e4209 | 376 | case PPTP_SET_LINK_INFO: |
f09943fe PM |
377 | case PPTP_ECHO_REQUEST: |
378 | case PPTP_ECHO_REPLY: | |
379 | /* I don't have to explain these ;) */ | |
380 | break; | |
381 | ||
382 | default: | |
383 | goto invalid; | |
384 | } | |
385 | ||
386 | nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound); | |
387 | if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK) | |
051966c0 PM |
388 | return nf_nat_pptp_inbound(skb, ct, ctinfo, |
389 | protoff, ctlh, pptpReq); | |
f09943fe PM |
390 | return NF_ACCEPT; |
391 | ||
392 | invalid: | |
0d53778e PM |
393 | pr_debug("invalid %s: type=%d cid=%u pcid=%u " |
394 | "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n", | |
4c559f15 | 395 | pptp_msg_name(msg), |
0d53778e PM |
396 | msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate, |
397 | ntohs(info->pns_call_id), ntohs(info->pac_call_id)); | |
f09943fe PM |
398 | return NF_ACCEPT; |
399 | } | |
400 | ||
2fe7c321 | 401 | static int |
051966c0 | 402 | pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff, |
f09943fe PM |
403 | struct PptpControlHeader *ctlh, |
404 | union pptp_ctrl_union *pptpReq, | |
405 | unsigned int reqlen, | |
406 | struct nf_conn *ct, | |
407 | enum ip_conntrack_info ctinfo) | |
408 | { | |
1afc5679 | 409 | struct nf_ct_pptp_master *info = nfct_help_data(ct); |
f09943fe PM |
410 | u_int16_t msg; |
411 | __be16 cid = 0, pcid = 0; | |
412 | typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound; | |
413 | ||
414 | msg = ntohs(ctlh->messageType); | |
4c559f15 | 415 | pr_debug("outbound control message %s\n", pptp_msg_name(msg)); |
f09943fe PM |
416 | |
417 | switch (msg) { | |
418 | case PPTP_START_SESSION_REQUEST: | |
419 | /* client requests for new control session */ | |
420 | if (info->sstate != PPTP_SESSION_NONE) | |
421 | goto invalid; | |
422 | info->sstate = PPTP_SESSION_REQUESTED; | |
423 | break; | |
424 | ||
425 | case PPTP_STOP_SESSION_REQUEST: | |
426 | /* client requests end of control session */ | |
427 | info->sstate = PPTP_SESSION_STOPREQ; | |
428 | break; | |
429 | ||
430 | case PPTP_OUT_CALL_REQUEST: | |
431 | /* client initiating connection to server */ | |
432 | if (info->sstate != PPTP_SESSION_CONFIRMED) | |
433 | goto invalid; | |
434 | info->cstate = PPTP_CALL_OUT_REQ; | |
435 | /* track PNS call id */ | |
436 | cid = pptpReq->ocreq.callID; | |
4c559f15 | 437 | pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid)); |
f09943fe PM |
438 | info->pns_call_id = cid; |
439 | break; | |
440 | ||
441 | case PPTP_IN_CALL_REPLY: | |
442 | /* client answers incoming call */ | |
443 | if (info->cstate != PPTP_CALL_IN_REQ && | |
444 | info->cstate != PPTP_CALL_IN_REP) | |
445 | goto invalid; | |
446 | ||
447 | cid = pptpReq->icack.callID; | |
448 | pcid = pptpReq->icack.peersCallID; | |
449 | if (info->pac_call_id != pcid) | |
450 | goto invalid; | |
4c559f15 | 451 | pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name(msg), |
0d53778e | 452 | ntohs(cid), ntohs(pcid)); |
f09943fe PM |
453 | |
454 | if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) { | |
455 | /* part two of the three-way handshake */ | |
456 | info->cstate = PPTP_CALL_IN_REP; | |
457 | info->pns_call_id = cid; | |
458 | } else | |
459 | info->cstate = PPTP_CALL_NONE; | |
460 | break; | |
461 | ||
462 | case PPTP_CALL_CLEAR_REQUEST: | |
463 | /* client requests hangup of call */ | |
464 | if (info->sstate != PPTP_SESSION_CONFIRMED) | |
465 | goto invalid; | |
466 | /* FUTURE: iterate over all calls and check if | |
467 | * call ID is valid. We don't do this without newnat, | |
468 | * because we only know about last call */ | |
469 | info->cstate = PPTP_CALL_CLEAR_REQ; | |
470 | break; | |
471 | ||
472 | case PPTP_SET_LINK_INFO: | |
473 | case PPTP_ECHO_REQUEST: | |
474 | case PPTP_ECHO_REPLY: | |
475 | /* I don't have to explain these ;) */ | |
476 | break; | |
477 | ||
478 | default: | |
479 | goto invalid; | |
480 | } | |
481 | ||
482 | nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound); | |
483 | if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK) | |
051966c0 PM |
484 | return nf_nat_pptp_outbound(skb, ct, ctinfo, |
485 | protoff, ctlh, pptpReq); | |
f09943fe PM |
486 | return NF_ACCEPT; |
487 | ||
488 | invalid: | |
0d53778e PM |
489 | pr_debug("invalid %s: type=%d cid=%u pcid=%u " |
490 | "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n", | |
4c559f15 | 491 | pptp_msg_name(msg), |
0d53778e PM |
492 | msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate, |
493 | ntohs(info->pns_call_id), ntohs(info->pac_call_id)); | |
f09943fe PM |
494 | return NF_ACCEPT; |
495 | } | |
496 | ||
497 | static const unsigned int pptp_msg_size[] = { | |
498 | [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest), | |
499 | [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply), | |
500 | [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest), | |
501 | [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply), | |
502 | [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest), | |
503 | [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply), | |
504 | [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest), | |
505 | [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply), | |
506 | [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected), | |
507 | [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest), | |
508 | [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify), | |
509 | [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify), | |
510 | [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo), | |
511 | }; | |
512 | ||
513 | /* track caller id inside control connection, call expect_related */ | |
514 | static int | |
3db05fea | 515 | conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff, |
f09943fe PM |
516 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
517 | ||
518 | { | |
519 | int dir = CTINFO2DIR(ctinfo); | |
1afc5679 | 520 | const struct nf_ct_pptp_master *info = nfct_help_data(ct); |
9ddd0ed0 JE |
521 | const struct tcphdr *tcph; |
522 | struct tcphdr _tcph; | |
523 | const struct pptp_pkt_hdr *pptph; | |
524 | struct pptp_pkt_hdr _pptph; | |
f09943fe PM |
525 | struct PptpControlHeader _ctlh, *ctlh; |
526 | union pptp_ctrl_union _pptpReq, *pptpReq; | |
3db05fea | 527 | unsigned int tcplen = skb->len - protoff; |
f09943fe PM |
528 | unsigned int datalen, reqlen, nexthdr_off; |
529 | int oldsstate, oldcstate; | |
530 | int ret; | |
531 | u_int16_t msg; | |
532 | ||
2fe7c321 FW |
533 | #if IS_ENABLED(CONFIG_NF_NAT) |
534 | if (!nf_ct_is_confirmed(ct) && (ct->status & IPS_NAT_MASK)) { | |
535 | struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT); | |
536 | ||
537 | if (!nat && !nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC)) | |
538 | return NF_DROP; | |
539 | } | |
540 | #endif | |
f09943fe | 541 | /* don't do any tracking before tcp handshake complete */ |
fb048833 | 542 | if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) |
f09943fe PM |
543 | return NF_ACCEPT; |
544 | ||
545 | nexthdr_off = protoff; | |
3db05fea | 546 | tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph); |
f09943fe PM |
547 | BUG_ON(!tcph); |
548 | nexthdr_off += tcph->doff * 4; | |
601e68e1 | 549 | datalen = tcplen - tcph->doff * 4; |
f09943fe | 550 | |
3db05fea | 551 | pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph); |
f09943fe | 552 | if (!pptph) { |
0d53778e | 553 | pr_debug("no full PPTP header, can't track\n"); |
f09943fe PM |
554 | return NF_ACCEPT; |
555 | } | |
556 | nexthdr_off += sizeof(_pptph); | |
557 | datalen -= sizeof(_pptph); | |
558 | ||
559 | /* if it's not a control message we can't do anything with it */ | |
560 | if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL || | |
561 | ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) { | |
0d53778e | 562 | pr_debug("not a control packet\n"); |
f09943fe PM |
563 | return NF_ACCEPT; |
564 | } | |
565 | ||
3db05fea | 566 | ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh); |
f09943fe PM |
567 | if (!ctlh) |
568 | return NF_ACCEPT; | |
569 | nexthdr_off += sizeof(_ctlh); | |
570 | datalen -= sizeof(_ctlh); | |
571 | ||
572 | reqlen = datalen; | |
573 | msg = ntohs(ctlh->messageType); | |
574 | if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg]) | |
575 | return NF_ACCEPT; | |
576 | if (reqlen > sizeof(*pptpReq)) | |
577 | reqlen = sizeof(*pptpReq); | |
578 | ||
3db05fea | 579 | pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq); |
f09943fe PM |
580 | if (!pptpReq) |
581 | return NF_ACCEPT; | |
582 | ||
583 | oldsstate = info->sstate; | |
584 | oldcstate = info->cstate; | |
585 | ||
586 | spin_lock_bh(&nf_pptp_lock); | |
587 | ||
588 | /* FIXME: We just blindly assume that the control connection is always | |
589 | * established from PNS->PAC. However, RFC makes no guarantee */ | |
590 | if (dir == IP_CT_DIR_ORIGINAL) | |
591 | /* client -> server (PNS -> PAC) */ | |
051966c0 | 592 | ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct, |
f09943fe PM |
593 | ctinfo); |
594 | else | |
595 | /* server -> client (PAC -> PNS) */ | |
051966c0 | 596 | ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct, |
f09943fe | 597 | ctinfo); |
0d53778e PM |
598 | pr_debug("sstate: %d->%d, cstate: %d->%d\n", |
599 | oldsstate, info->sstate, oldcstate, info->cstate); | |
f09943fe PM |
600 | spin_unlock_bh(&nf_pptp_lock); |
601 | ||
602 | return ret; | |
603 | } | |
604 | ||
6002f266 PM |
605 | static const struct nf_conntrack_expect_policy pptp_exp_policy = { |
606 | .max_expected = 2, | |
607 | .timeout = 5 * 60, | |
608 | }; | |
609 | ||
f09943fe PM |
610 | /* control protocol helper */ |
611 | static struct nf_conntrack_helper pptp __read_mostly = { | |
612 | .name = "pptp", | |
613 | .me = THIS_MODULE, | |
f09943fe | 614 | .tuple.src.l3num = AF_INET, |
09640e63 | 615 | .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT), |
f09943fe | 616 | .tuple.dst.protonum = IPPROTO_TCP, |
f09943fe PM |
617 | .help = conntrack_pptp_help, |
618 | .destroy = pptp_destroy_siblings, | |
6002f266 | 619 | .expect_policy = &pptp_exp_policy, |
f09943fe PM |
620 | }; |
621 | ||
622 | static int __init nf_conntrack_pptp_init(void) | |
623 | { | |
dcf67740 FW |
624 | NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_pptp_master)); |
625 | ||
8142b227 | 626 | return nf_conntrack_helper_register(&pptp); |
f09943fe PM |
627 | } |
628 | ||
629 | static void __exit nf_conntrack_pptp_fini(void) | |
630 | { | |
631 | nf_conntrack_helper_unregister(&pptp); | |
f09943fe PM |
632 | } |
633 | ||
634 | module_init(nf_conntrack_pptp_init); | |
635 | module_exit(nf_conntrack_pptp_fini); |