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