blob: 8fd83470d1b31aae6d8e552a664f95913d0aaa01 [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
2 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
3 * PPTP is a a protocol for creating virtual private networks.
4 * It is a specification defined by Microsoft and some vendors
5 * working with Microsoft. PPTP is built on top of a modified
6 * version of the Internet Generic Routing Encapsulation Protocol.
7 * GRE is defined in RFC 1701 and RFC 1702. Documentation of
8 * PPTP can be found in RFC 2637
9 *
10 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
11 *
12 * Development of this code funded by Astaro AG (http://www.astaro.com/)
13 *
14 * Limitations:
15 * - We blindly assume that control connections are always
16 * established in PNS->PAC direction. This is a violation
17 * of RFFC2673
18 * - We can only support one single call within each session
19 * TODO:
20 * - testing of incoming PPTP calls
21 */
22
23#include <linux/module.h>
24#include <linux/skbuff.h>
25#include <linux/in.h>
26#include <linux/tcp.h>
27
28#include <net/netfilter/nf_conntrack.h>
29#include <net/netfilter/nf_conntrack_core.h>
30#include <net/netfilter/nf_conntrack_helper.h>
31#include <linux/netfilter/nf_conntrack_proto_gre.h>
32#include <linux/netfilter/nf_conntrack_pptp.h>
33
34#define NF_CT_PPTP_VERSION "3.1"
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
38MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
39MODULE_ALIAS("ip_conntrack_pptp");
40
41static DEFINE_SPINLOCK(nf_pptp_lock);
42
43int
Herbert Xu3db05fe2007-10-15 00:53:15 -070044(*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080045 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
46 struct PptpControlHeader *ctlh,
47 union pptp_ctrl_union *pptpReq) __read_mostly;
48EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
49
50int
Herbert Xu3db05fe2007-10-15 00:53:15 -070051(*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080052 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
53 struct PptpControlHeader *ctlh,
54 union pptp_ctrl_union *pptpReq) __read_mostly;
55EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
56
57void
58(*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
59 struct nf_conntrack_expect *expect_reply)
60 __read_mostly;
61EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
62
63void
64(*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
65 struct nf_conntrack_expect *exp) __read_mostly;
66EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
67
Patrick McHardy0d537782007-07-07 22:39:38 -070068#ifdef DEBUG
Patrick McHardyf09943f2006-12-02 22:09:41 -080069/* PptpControlMessageType names */
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080070const char *const pptp_msg_name[] = {
Patrick McHardyf09943f2006-12-02 22:09:41 -080071 "UNKNOWN_MESSAGE",
72 "START_SESSION_REQUEST",
73 "START_SESSION_REPLY",
74 "STOP_SESSION_REQUEST",
75 "STOP_SESSION_REPLY",
76 "ECHO_REQUEST",
77 "ECHO_REPLY",
78 "OUT_CALL_REQUEST",
79 "OUT_CALL_REPLY",
80 "IN_CALL_REQUEST",
81 "IN_CALL_REPLY",
82 "IN_CALL_CONNECT",
83 "CALL_CLEAR_REQUEST",
84 "CALL_DISCONNECT_NOTIFY",
85 "WAN_ERROR_NOTIFY",
86 "SET_LINK_INFO"
87};
88EXPORT_SYMBOL(pptp_msg_name);
Patrick McHardyf09943f2006-12-02 22:09:41 -080089#endif
90
91#define SECS *HZ
92#define MINS * 60 SECS
93#define HOURS * 60 MINS
94
95#define PPTP_GRE_TIMEOUT (10 MINS)
96#define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
97
98static void pptp_expectfn(struct nf_conn *ct,
99 struct nf_conntrack_expect *exp)
100{
101 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
Patrick McHardy0d537782007-07-07 22:39:38 -0700102 pr_debug("increasing timeouts\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800103
104 /* increase timeout of GRE data channel conntrack entry */
105 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
106 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
107
108 /* Can you see how rusty this code is, compared with the pre-2.6.11
109 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
110
111 rcu_read_lock();
112 nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
Patrick McHardy73990722007-01-26 01:07:59 -0800113 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800114 nf_nat_pptp_expectfn(ct, exp);
115 else {
116 struct nf_conntrack_tuple inv_t;
117 struct nf_conntrack_expect *exp_other;
118
119 /* obviously this tuple inversion only works until you do NAT */
120 nf_ct_invert_tuplepr(&inv_t, &exp->tuple);
Patrick McHardy0d537782007-07-07 22:39:38 -0700121 pr_debug("trying to unexpect other dir: ");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800122 NF_CT_DUMP_TUPLE(&inv_t);
123
Patrick McHardy68236452007-07-07 22:30:49 -0700124 exp_other = nf_ct_expect_find_get(&inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800125 if (exp_other) {
126 /* delete other expectation. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700127 pr_debug("found\n");
Patrick McHardy68236452007-07-07 22:30:49 -0700128 nf_ct_unexpect_related(exp_other);
129 nf_ct_expect_put(exp_other);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800130 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700131 pr_debug("not found\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800132 }
133 }
134 rcu_read_unlock();
135}
136
137static int destroy_sibling_or_exp(const struct nf_conntrack_tuple *t)
138{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800139 const struct nf_conntrack_tuple_hash *h;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800140 struct nf_conntrack_expect *exp;
141 struct nf_conn *sibling;
142
Patrick McHardy0d537782007-07-07 22:39:38 -0700143 pr_debug("trying to timeout ct or exp for tuple ");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800144 NF_CT_DUMP_TUPLE(t);
145
Patrick McHardy330f7db2007-07-07 22:28:42 -0700146 h = nf_conntrack_find_get(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800147 if (h) {
148 sibling = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy0d537782007-07-07 22:39:38 -0700149 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800150 sibling->proto.gre.timeout = 0;
151 sibling->proto.gre.stream_timeout = 0;
152 if (del_timer(&sibling->timeout))
153 sibling->timeout.function((unsigned long)sibling);
154 nf_ct_put(sibling);
155 return 1;
156 } else {
Patrick McHardy68236452007-07-07 22:30:49 -0700157 exp = nf_ct_expect_find_get(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800158 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700159 pr_debug("unexpect_related of expect %p\n", exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700160 nf_ct_unexpect_related(exp);
161 nf_ct_expect_put(exp);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800162 return 1;
163 }
164 }
165 return 0;
166}
167
168/* timeout GRE data connections */
169static void pptp_destroy_siblings(struct nf_conn *ct)
170{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800171 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800172 struct nf_conntrack_tuple t;
173
174 nf_ct_gre_keymap_destroy(ct);
175
176 /* try original (pns->pac) tuple */
177 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
178 t.dst.protonum = IPPROTO_GRE;
179 t.src.u.gre.key = help->help.ct_pptp_info.pns_call_id;
180 t.dst.u.gre.key = help->help.ct_pptp_info.pac_call_id;
181 if (!destroy_sibling_or_exp(&t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700182 pr_debug("failed to timeout original pns->pac ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800183
184 /* try reply (pac->pns) tuple */
185 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
186 t.dst.protonum = IPPROTO_GRE;
187 t.src.u.gre.key = help->help.ct_pptp_info.pac_call_id;
188 t.dst.u.gre.key = help->help.ct_pptp_info.pns_call_id;
189 if (!destroy_sibling_or_exp(&t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700190 pr_debug("failed to timeout reply pac->pns ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800191}
192
193/* expect GRE connections (PNS->PAC and PAC->PNS direction) */
194static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
195{
196 struct nf_conntrack_expect *exp_orig, *exp_reply;
197 enum ip_conntrack_dir dir;
198 int ret = 1;
199 typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
200
Patrick McHardy68236452007-07-07 22:30:49 -0700201 exp_orig = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800202 if (exp_orig == NULL)
203 goto out;
204
Patrick McHardy68236452007-07-07 22:30:49 -0700205 exp_reply = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800206 if (exp_reply == NULL)
207 goto out_put_orig;
208
209 /* original direction, PNS->PAC */
210 dir = IP_CT_DIR_ORIGINAL;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700211 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
212 ct->tuplehash[dir].tuple.src.l3num,
Patrick McHardy68236452007-07-07 22:30:49 -0700213 &ct->tuplehash[dir].tuple.src.u3,
214 &ct->tuplehash[dir].tuple.dst.u3,
215 IPPROTO_GRE, &peer_callid, &callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800216 exp_orig->expectfn = pptp_expectfn;
217
218 /* reply direction, PAC->PNS */
219 dir = IP_CT_DIR_REPLY;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700220 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
221 ct->tuplehash[dir].tuple.src.l3num,
Patrick McHardy68236452007-07-07 22:30:49 -0700222 &ct->tuplehash[dir].tuple.src.u3,
223 &ct->tuplehash[dir].tuple.dst.u3,
224 IPPROTO_GRE, &callid, &peer_callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800225 exp_reply->expectfn = pptp_expectfn;
226
227 nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
228 if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
229 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
Patrick McHardy68236452007-07-07 22:30:49 -0700230 if (nf_ct_expect_related(exp_orig) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800231 goto out_put_both;
Patrick McHardy68236452007-07-07 22:30:49 -0700232 if (nf_ct_expect_related(exp_reply) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800233 goto out_unexpect_orig;
234
235 /* Add GRE keymap entries */
236 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
237 goto out_unexpect_both;
238 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
239 nf_ct_gre_keymap_destroy(ct);
240 goto out_unexpect_both;
241 }
242 ret = 0;
243
244out_put_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700245 nf_ct_expect_put(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800246out_put_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700247 nf_ct_expect_put(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800248out:
249 return ret;
250
251out_unexpect_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700252 nf_ct_unexpect_related(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800253out_unexpect_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700254 nf_ct_unexpect_related(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800255 goto out_put_both;
256}
257
258static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700259pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800260 struct PptpControlHeader *ctlh,
261 union pptp_ctrl_union *pptpReq,
262 unsigned int reqlen,
263 struct nf_conn *ct,
264 enum ip_conntrack_info ctinfo)
265{
266 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
267 u_int16_t msg;
268 __be16 cid = 0, pcid = 0;
269 typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
270
271 msg = ntohs(ctlh->messageType);
Patrick McHardy0d537782007-07-07 22:39:38 -0700272 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800273
274 switch (msg) {
275 case PPTP_START_SESSION_REPLY:
276 /* server confirms new control session */
277 if (info->sstate < PPTP_SESSION_REQUESTED)
278 goto invalid;
279 if (pptpReq->srep.resultCode == PPTP_START_OK)
280 info->sstate = PPTP_SESSION_CONFIRMED;
281 else
282 info->sstate = PPTP_SESSION_ERROR;
283 break;
284
285 case PPTP_STOP_SESSION_REPLY:
286 /* server confirms end of control session */
287 if (info->sstate > PPTP_SESSION_STOPREQ)
288 goto invalid;
289 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
290 info->sstate = PPTP_SESSION_NONE;
291 else
292 info->sstate = PPTP_SESSION_ERROR;
293 break;
294
295 case PPTP_OUT_CALL_REPLY:
296 /* server accepted call, we now expect GRE frames */
297 if (info->sstate != PPTP_SESSION_CONFIRMED)
298 goto invalid;
299 if (info->cstate != PPTP_CALL_OUT_REQ &&
300 info->cstate != PPTP_CALL_OUT_CONF)
301 goto invalid;
302
303 cid = pptpReq->ocack.callID;
304 pcid = pptpReq->ocack.peersCallID;
305 if (info->pns_call_id != pcid)
306 goto invalid;
Patrick McHardy0d537782007-07-07 22:39:38 -0700307 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
308 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800309
310 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
311 info->cstate = PPTP_CALL_OUT_CONF;
312 info->pac_call_id = cid;
313 exp_gre(ct, cid, pcid);
314 } else
315 info->cstate = PPTP_CALL_NONE;
316 break;
317
318 case PPTP_IN_CALL_REQUEST:
319 /* server tells us about incoming call request */
320 if (info->sstate != PPTP_SESSION_CONFIRMED)
321 goto invalid;
322
323 cid = pptpReq->icreq.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700324 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800325 info->cstate = PPTP_CALL_IN_REQ;
326 info->pac_call_id = cid;
327 break;
328
329 case PPTP_IN_CALL_CONNECT:
330 /* server tells us about incoming call established */
331 if (info->sstate != PPTP_SESSION_CONFIRMED)
332 goto invalid;
333 if (info->cstate != PPTP_CALL_IN_REP &&
334 info->cstate != PPTP_CALL_IN_CONF)
335 goto invalid;
336
337 pcid = pptpReq->iccon.peersCallID;
338 cid = info->pac_call_id;
339
340 if (info->pns_call_id != pcid)
341 goto invalid;
342
Patrick McHardy0d537782007-07-07 22:39:38 -0700343 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800344 info->cstate = PPTP_CALL_IN_CONF;
345
346 /* we expect a GRE connection from PAC to PNS */
347 exp_gre(ct, cid, pcid);
348 break;
349
350 case PPTP_CALL_DISCONNECT_NOTIFY:
351 /* server confirms disconnect */
352 cid = pptpReq->disc.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700353 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800354 info->cstate = PPTP_CALL_NONE;
355
356 /* untrack this call id, unexpect GRE packets */
357 pptp_destroy_siblings(ct);
358 break;
359
360 case PPTP_WAN_ERROR_NOTIFY:
361 case PPTP_ECHO_REQUEST:
362 case PPTP_ECHO_REPLY:
363 /* I don't have to explain these ;) */
364 break;
365
366 default:
367 goto invalid;
368 }
369
370 nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
371 if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700372 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800373 return NF_ACCEPT;
374
375invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700376 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
377 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
378 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
379 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
380 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800381 return NF_ACCEPT;
382}
383
384static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700385pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800386 struct PptpControlHeader *ctlh,
387 union pptp_ctrl_union *pptpReq,
388 unsigned int reqlen,
389 struct nf_conn *ct,
390 enum ip_conntrack_info ctinfo)
391{
392 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
393 u_int16_t msg;
394 __be16 cid = 0, pcid = 0;
395 typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
396
397 msg = ntohs(ctlh->messageType);
Patrick McHardy0d537782007-07-07 22:39:38 -0700398 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800399
400 switch (msg) {
401 case PPTP_START_SESSION_REQUEST:
402 /* client requests for new control session */
403 if (info->sstate != PPTP_SESSION_NONE)
404 goto invalid;
405 info->sstate = PPTP_SESSION_REQUESTED;
406 break;
407
408 case PPTP_STOP_SESSION_REQUEST:
409 /* client requests end of control session */
410 info->sstate = PPTP_SESSION_STOPREQ;
411 break;
412
413 case PPTP_OUT_CALL_REQUEST:
414 /* client initiating connection to server */
415 if (info->sstate != PPTP_SESSION_CONFIRMED)
416 goto invalid;
417 info->cstate = PPTP_CALL_OUT_REQ;
418 /* track PNS call id */
419 cid = pptpReq->ocreq.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700420 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800421 info->pns_call_id = cid;
422 break;
423
424 case PPTP_IN_CALL_REPLY:
425 /* client answers incoming call */
426 if (info->cstate != PPTP_CALL_IN_REQ &&
427 info->cstate != PPTP_CALL_IN_REP)
428 goto invalid;
429
430 cid = pptpReq->icack.callID;
431 pcid = pptpReq->icack.peersCallID;
432 if (info->pac_call_id != pcid)
433 goto invalid;
Patrick McHardy0d537782007-07-07 22:39:38 -0700434 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
435 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800436
437 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
438 /* part two of the three-way handshake */
439 info->cstate = PPTP_CALL_IN_REP;
440 info->pns_call_id = cid;
441 } else
442 info->cstate = PPTP_CALL_NONE;
443 break;
444
445 case PPTP_CALL_CLEAR_REQUEST:
446 /* client requests hangup of call */
447 if (info->sstate != PPTP_SESSION_CONFIRMED)
448 goto invalid;
449 /* FUTURE: iterate over all calls and check if
450 * call ID is valid. We don't do this without newnat,
451 * because we only know about last call */
452 info->cstate = PPTP_CALL_CLEAR_REQ;
453 break;
454
455 case PPTP_SET_LINK_INFO:
456 case PPTP_ECHO_REQUEST:
457 case PPTP_ECHO_REPLY:
458 /* I don't have to explain these ;) */
459 break;
460
461 default:
462 goto invalid;
463 }
464
465 nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
466 if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700467 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800468 return NF_ACCEPT;
469
470invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700471 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
472 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
473 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
474 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
475 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800476 return NF_ACCEPT;
477}
478
479static const unsigned int pptp_msg_size[] = {
480 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
481 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
482 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
483 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
484 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
485 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
486 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
487 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
488 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
489 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
490 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
491 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
492 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
493};
494
495/* track caller id inside control connection, call expect_related */
496static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700497conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800498 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
499
500{
501 int dir = CTINFO2DIR(ctinfo);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800502 const struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
503 const struct tcphdr *tcph;
504 struct tcphdr _tcph;
505 const struct pptp_pkt_hdr *pptph;
506 struct pptp_pkt_hdr _pptph;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800507 struct PptpControlHeader _ctlh, *ctlh;
508 union pptp_ctrl_union _pptpReq, *pptpReq;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700509 unsigned int tcplen = skb->len - protoff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800510 unsigned int datalen, reqlen, nexthdr_off;
511 int oldsstate, oldcstate;
512 int ret;
513 u_int16_t msg;
514
515 /* don't do any tracking before tcp handshake complete */
516 if (ctinfo != IP_CT_ESTABLISHED &&
517 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
518 return NF_ACCEPT;
519
520 nexthdr_off = protoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700521 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800522 BUG_ON(!tcph);
523 nexthdr_off += tcph->doff * 4;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800524 datalen = tcplen - tcph->doff * 4;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800525
Herbert Xu3db05fe2007-10-15 00:53:15 -0700526 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800527 if (!pptph) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700528 pr_debug("no full PPTP header, can't track\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800529 return NF_ACCEPT;
530 }
531 nexthdr_off += sizeof(_pptph);
532 datalen -= sizeof(_pptph);
533
534 /* if it's not a control message we can't do anything with it */
535 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
536 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700537 pr_debug("not a control packet\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800538 return NF_ACCEPT;
539 }
540
Herbert Xu3db05fe2007-10-15 00:53:15 -0700541 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800542 if (!ctlh)
543 return NF_ACCEPT;
544 nexthdr_off += sizeof(_ctlh);
545 datalen -= sizeof(_ctlh);
546
547 reqlen = datalen;
548 msg = ntohs(ctlh->messageType);
549 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
550 return NF_ACCEPT;
551 if (reqlen > sizeof(*pptpReq))
552 reqlen = sizeof(*pptpReq);
553
Herbert Xu3db05fe2007-10-15 00:53:15 -0700554 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800555 if (!pptpReq)
556 return NF_ACCEPT;
557
558 oldsstate = info->sstate;
559 oldcstate = info->cstate;
560
561 spin_lock_bh(&nf_pptp_lock);
562
563 /* FIXME: We just blindly assume that the control connection is always
564 * established from PNS->PAC. However, RFC makes no guarantee */
565 if (dir == IP_CT_DIR_ORIGINAL)
566 /* client -> server (PNS -> PAC) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700567 ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800568 ctinfo);
569 else
570 /* server -> client (PAC -> PNS) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700571 ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800572 ctinfo);
Patrick McHardy0d537782007-07-07 22:39:38 -0700573 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
574 oldsstate, info->sstate, oldcstate, info->cstate);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800575 spin_unlock_bh(&nf_pptp_lock);
576
577 return ret;
578}
579
Patrick McHardy6002f2662008-03-25 20:09:15 -0700580static const struct nf_conntrack_expect_policy pptp_exp_policy = {
581 .max_expected = 2,
582 .timeout = 5 * 60,
583};
584
Patrick McHardyf09943f2006-12-02 22:09:41 -0800585/* control protocol helper */
586static struct nf_conntrack_helper pptp __read_mostly = {
587 .name = "pptp",
588 .me = THIS_MODULE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800589 .tuple.src.l3num = AF_INET,
590 .tuple.src.u.tcp.port = __constant_htons(PPTP_CONTROL_PORT),
591 .tuple.dst.protonum = IPPROTO_TCP,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800592 .help = conntrack_pptp_help,
593 .destroy = pptp_destroy_siblings,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700594 .expect_policy = &pptp_exp_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800595};
596
597static int __init nf_conntrack_pptp_init(void)
598{
599 return nf_conntrack_helper_register(&pptp);
600}
601
602static void __exit nf_conntrack_pptp_fini(void)
603{
604 nf_conntrack_helper_unregister(&pptp);
605 nf_ct_gre_keymap_flush();
606}
607
608module_init(nf_conntrack_pptp_init);
609module_exit(nf_conntrack_pptp_fini);