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