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