[NET] IPV4: Fix whitespace errors.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / ip_conntrack_helper_pptp.c
CommitLineData
926b50f9
HW
1/*
2 * ip_conntrack_pptp.c - Version 3.0
3 *
4 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
5 * PPTP is a a protocol for creating virtual private networks.
6 * It is a specification defined by Microsoft and some vendors
7 * working with Microsoft. PPTP is built on top of a modified
8 * version of the Internet Generic Routing Encapsulation Protocol.
9 * GRE is defined in RFC 1701 and RFC 1702. Documentation of
10 * PPTP can be found in RFC 2637
11 *
12 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
13 *
14 * Development of this code funded by Astaro AG (http://www.astaro.com/)
15 *
16 * Limitations:
17 * - We blindly assume that control connections are always
18 * established in PNS->PAC direction. This is a violation
19 * of RFFC2673
20 * - We can only support one single call within each session
21 *
22 * TODO:
edd5a329 23 * - testing of incoming PPTP calls
926b50f9 24 *
edd5a329 25 * Changes:
926b50f9 26 * 2002-02-05 - Version 1.3
edd5a329 27 * - Call ip_conntrack_unexpect_related() from
926b50f9
HW
28 * pptp_destroy_siblings() to destroy expectations in case
29 * CALL_DISCONNECT_NOTIFY or tcp fin packet was seen
30 * (Philip Craig <philipc@snapgear.com>)
31 * - Add Version information at module loadtime
32 * 2002-02-10 - Version 1.6
33 * - move to C99 style initializers
34 * - remove second expectation if first arrives
35 * 2004-10-22 - Version 2.0
36 * - merge Mandrake's 2.6.x port with recent 2.6.x API changes
37 * - fix lots of linear skb assumptions from Mandrake's port
38 * 2005-06-10 - Version 2.1
39 * - use ip_conntrack_expect_free() instead of kfree() on the
40 * expect's (which are from the slab for quite some time)
41 * 2005-06-10 - Version 3.0
42 * - port helper to post-2.6.11 API changes,
43 * funded by Oxcoda NetBox Blue (http://www.netboxblue.com/)
44 * 2005-07-30 - Version 3.1
45 * - port helper to 2.6.13 API changes
46 *
47 */
48
926b50f9
HW
49#include <linux/module.h>
50#include <linux/netfilter.h>
51#include <linux/ip.h>
52#include <net/checksum.h>
53#include <net/tcp.h>
54
55#include <linux/netfilter_ipv4/ip_conntrack.h>
56#include <linux/netfilter_ipv4/ip_conntrack_core.h>
57#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
58#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
59#include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
60
61#define IP_CT_PPTP_VERSION "3.1"
62
63MODULE_LICENSE("GPL");
64MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
65MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
66
67static DEFINE_SPINLOCK(ip_pptp_lock);
68
69int
70(*ip_nat_pptp_hook_outbound)(struct sk_buff **pskb,
71 struct ip_conntrack *ct,
72 enum ip_conntrack_info ctinfo,
73 struct PptpControlHeader *ctlh,
74 union pptp_ctrl_union *pptpReq);
75
76int
77(*ip_nat_pptp_hook_inbound)(struct sk_buff **pskb,
78 struct ip_conntrack *ct,
79 enum ip_conntrack_info ctinfo,
80 struct PptpControlHeader *ctlh,
81 union pptp_ctrl_union *pptpReq);
82
cf9f8152 83void
926b50f9
HW
84(*ip_nat_pptp_hook_exp_gre)(struct ip_conntrack_expect *expect_orig,
85 struct ip_conntrack_expect *expect_reply);
86
87void
88(*ip_nat_pptp_hook_expectfn)(struct ip_conntrack *ct,
89 struct ip_conntrack_expect *exp);
90
91#if 0
92/* PptpControlMessageType names */
93const char *pptp_msg_name[] = {
94 "UNKNOWN_MESSAGE",
95 "START_SESSION_REQUEST",
96 "START_SESSION_REPLY",
97 "STOP_SESSION_REQUEST",
98 "STOP_SESSION_REPLY",
99 "ECHO_REQUEST",
100 "ECHO_REPLY",
101 "OUT_CALL_REQUEST",
102 "OUT_CALL_REPLY",
103 "IN_CALL_REQUEST",
104 "IN_CALL_REPLY",
105 "IN_CALL_CONNECT",
106 "CALL_CLEAR_REQUEST",
107 "CALL_DISCONNECT_NOTIFY",
108 "WAN_ERROR_NOTIFY",
109 "SET_LINK_INFO"
110};
111EXPORT_SYMBOL(pptp_msg_name);
112#define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args)
113#else
114#define DEBUGP(format, args...)
115#endif
116
117#define SECS *HZ
118#define MINS * 60 SECS
119#define HOURS * 60 MINS
120
121#define PPTP_GRE_TIMEOUT (10 MINS)
122#define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
123
124static void pptp_expectfn(struct ip_conntrack *ct,
125 struct ip_conntrack_expect *exp)
126{
337fbc41
PM
127 typeof(ip_nat_pptp_hook_expectfn) ip_nat_pptp_expectfn;
128
926b50f9
HW
129 DEBUGP("increasing timeouts\n");
130
131 /* increase timeout of GRE data channel conntrack entry */
132 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
133 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
134
135 /* Can you see how rusty this code is, compared with the pre-2.6.11
136 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
137
337fbc41
PM
138 rcu_read_lock();
139 ip_nat_pptp_expectfn = rcu_dereference(ip_nat_pptp_hook_expectfn);
140 if (!ip_nat_pptp_expectfn) {
926b50f9
HW
141 struct ip_conntrack_tuple inv_t;
142 struct ip_conntrack_expect *exp_other;
143
144 /* obviously this tuple inversion only works until you do NAT */
145 invert_tuplepr(&inv_t, &exp->tuple);
146 DEBUGP("trying to unexpect other dir: ");
147 DUMP_TUPLE(&inv_t);
edd5a329 148
468ec44b 149 exp_other = ip_conntrack_expect_find_get(&inv_t);
926b50f9
HW
150 if (exp_other) {
151 /* delete other expectation. */
152 DEBUGP("found\n");
153 ip_conntrack_unexpect_related(exp_other);
154 ip_conntrack_expect_put(exp_other);
155 } else {
156 DEBUGP("not found\n");
157 }
158 } else {
159 /* we need more than simple inversion */
337fbc41 160 ip_nat_pptp_expectfn(ct, exp);
926b50f9 161 }
337fbc41 162 rcu_read_unlock();
926b50f9
HW
163}
164
165static int destroy_sibling_or_exp(const struct ip_conntrack_tuple *t)
166{
167 struct ip_conntrack_tuple_hash *h;
168 struct ip_conntrack_expect *exp;
169
170 DEBUGP("trying to timeout ct or exp for tuple ");
171 DUMP_TUPLE(t);
172
173 h = ip_conntrack_find_get(t, NULL);
174 if (h) {
175 struct ip_conntrack *sibling = tuplehash_to_ctrack(h);
176 DEBUGP("setting timeout of conntrack %p to 0\n", sibling);
177 sibling->proto.gre.timeout = 0;
178 sibling->proto.gre.stream_timeout = 0;
926b50f9
HW
179 if (del_timer(&sibling->timeout))
180 sibling->timeout.function((unsigned long)sibling);
181 ip_conntrack_put(sibling);
182 return 1;
183 } else {
468ec44b 184 exp = ip_conntrack_expect_find_get(t);
926b50f9
HW
185 if (exp) {
186 DEBUGP("unexpect_related of expect %p\n", exp);
187 ip_conntrack_unexpect_related(exp);
188 ip_conntrack_expect_put(exp);
189 return 1;
190 }
191 }
192
193 return 0;
194}
195
196
197/* timeout GRE data connections */
198static void pptp_destroy_siblings(struct ip_conntrack *ct)
199{
200 struct ip_conntrack_tuple t;
201
fd5e3bef 202 ip_ct_gre_keymap_destroy(ct);
edd5a329 203 /* Since ct->sibling_list has literally rusted away in 2.6.11,
926b50f9
HW
204 * we now need another way to find out about our sibling
205 * contrack and expects... -HW */
206
207 /* try original (pns->pac) tuple */
208 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
209 t.dst.protonum = IPPROTO_GRE;
955b9442
PM
210 t.src.u.gre.key = ct->help.ct_pptp_info.pns_call_id;
211 t.dst.u.gre.key = ct->help.ct_pptp_info.pac_call_id;
926b50f9
HW
212
213 if (!destroy_sibling_or_exp(&t))
214 DEBUGP("failed to timeout original pns->pac ct/exp\n");
215
216 /* try reply (pac->pns) tuple */
217 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
218 t.dst.protonum = IPPROTO_GRE;
955b9442
PM
219 t.src.u.gre.key = ct->help.ct_pptp_info.pac_call_id;
220 t.dst.u.gre.key = ct->help.ct_pptp_info.pns_call_id;
926b50f9
HW
221
222 if (!destroy_sibling_or_exp(&t))
223 DEBUGP("failed to timeout reply pac->pns ct/exp\n");
224}
225
226/* expect GRE connections (PNS->PAC and PAC->PNS direction) */
227static inline int
cf9f8152 228exp_gre(struct ip_conntrack *ct,
67497205
AD
229 __be16 callid,
230 __be16 peer_callid)
926b50f9 231{
926b50f9
HW
232 struct ip_conntrack_expect *exp_orig, *exp_reply;
233 int ret = 1;
337fbc41 234 typeof(ip_nat_pptp_hook_exp_gre) ip_nat_pptp_exp_gre;
926b50f9 235
cf9f8152 236 exp_orig = ip_conntrack_expect_alloc(ct);
926b50f9
HW
237 if (exp_orig == NULL)
238 goto out;
239
cf9f8152 240 exp_reply = ip_conntrack_expect_alloc(ct);
926b50f9
HW
241 if (exp_reply == NULL)
242 goto out_put_orig;
243
cf9f8152
PM
244 /* original direction, PNS->PAC */
245 exp_orig->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
246 exp_orig->tuple.src.u.gre.key = peer_callid;
247 exp_orig->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
248 exp_orig->tuple.dst.u.gre.key = callid;
249 exp_orig->tuple.dst.protonum = IPPROTO_GRE;
926b50f9 250
cdcb71bf 251 exp_orig->mask.src.ip = htonl(0xffffffff);
926b50f9 252 exp_orig->mask.src.u.all = 0;
67497205 253 exp_orig->mask.dst.u.gre.key = htons(0xffff);
cdcb71bf 254 exp_orig->mask.dst.ip = htonl(0xffffffff);
926b50f9 255 exp_orig->mask.dst.protonum = 0xff;
edd5a329 256
cf9f8152 257 exp_orig->master = ct;
926b50f9
HW
258 exp_orig->expectfn = pptp_expectfn;
259 exp_orig->flags = 0;
260
926b50f9
HW
261 /* both expectations are identical apart from tuple */
262 memcpy(exp_reply, exp_orig, sizeof(*exp_reply));
926b50f9 263
cf9f8152
PM
264 /* reply direction, PAC->PNS */
265 exp_reply->tuple.src.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
266 exp_reply->tuple.src.u.gre.key = callid;
267 exp_reply->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
268 exp_reply->tuple.dst.u.gre.key = peer_callid;
269 exp_reply->tuple.dst.protonum = IPPROTO_GRE;
926b50f9 270
337fbc41
PM
271 ip_nat_pptp_exp_gre = rcu_dereference(ip_nat_pptp_hook_exp_gre);
272 if (ip_nat_pptp_exp_gre)
273 ip_nat_pptp_exp_gre(exp_orig, exp_reply);
cf9f8152
PM
274 if (ip_conntrack_expect_related(exp_orig) != 0)
275 goto out_put_both;
276 if (ip_conntrack_expect_related(exp_reply) != 0)
277 goto out_unexpect_orig;
278
279 /* Add GRE keymap entries */
280 if (ip_ct_gre_keymap_add(ct, &exp_orig->tuple, 0) != 0)
281 goto out_unexpect_both;
282 if (ip_ct_gre_keymap_add(ct, &exp_reply->tuple, 1) != 0) {
283 ip_ct_gre_keymap_destroy(ct);
284 goto out_unexpect_both;
926b50f9 285 }
cf9f8152 286 ret = 0;
926b50f9
HW
287
288out_put_both:
289 ip_conntrack_expect_put(exp_reply);
290out_put_orig:
291 ip_conntrack_expect_put(exp_orig);
292out:
293 return ret;
294
295out_unexpect_both:
296 ip_conntrack_unexpect_related(exp_reply);
297out_unexpect_orig:
298 ip_conntrack_unexpect_related(exp_orig);
299 goto out_put_both;
300}
301
edd5a329 302static inline int
926b50f9 303pptp_inbound_pkt(struct sk_buff **pskb,
4c651756
PM
304 struct PptpControlHeader *ctlh,
305 union pptp_ctrl_union *pptpReq,
306 unsigned int reqlen,
926b50f9
HW
307 struct ip_conntrack *ct,
308 enum ip_conntrack_info ctinfo)
309{
926b50f9 310 struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
67497205 311 u_int16_t msg;
87a0117a 312 __be16 cid = 0, pcid = 0;
337fbc41 313 typeof(ip_nat_pptp_hook_inbound) ip_nat_pptp_inbound;
926b50f9 314
926b50f9
HW
315 msg = ntohs(ctlh->messageType);
316 DEBUGP("inbound control message %s\n", pptp_msg_name[msg]);
317
318 switch (msg) {
319 case PPTP_START_SESSION_REPLY:
926b50f9 320 /* server confirms new control session */
87a0117a
PM
321 if (info->sstate < PPTP_SESSION_REQUESTED)
322 goto invalid;
926b50f9
HW
323 if (pptpReq->srep.resultCode == PPTP_START_OK)
324 info->sstate = PPTP_SESSION_CONFIRMED;
edd5a329 325 else
926b50f9
HW
326 info->sstate = PPTP_SESSION_ERROR;
327 break;
328
329 case PPTP_STOP_SESSION_REPLY:
926b50f9 330 /* server confirms end of control session */
87a0117a
PM
331 if (info->sstate > PPTP_SESSION_STOPREQ)
332 goto invalid;
926b50f9
HW
333 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
334 info->sstate = PPTP_SESSION_NONE;
335 else
336 info->sstate = PPTP_SESSION_ERROR;
337 break;
338
339 case PPTP_OUT_CALL_REPLY:
926b50f9 340 /* server accepted call, we now expect GRE frames */
87a0117a
PM
341 if (info->sstate != PPTP_SESSION_CONFIRMED)
342 goto invalid;
926b50f9 343 if (info->cstate != PPTP_CALL_OUT_REQ &&
87a0117a
PM
344 info->cstate != PPTP_CALL_OUT_CONF)
345 goto invalid;
346
857c06da
PM
347 cid = pptpReq->ocack.callID;
348 pcid = pptpReq->ocack.peersCallID;
87a0117a
PM
349 if (info->pns_call_id != pcid)
350 goto invalid;
edd5a329 351 DEBUGP("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
857c06da 352 ntohs(cid), ntohs(pcid));
edd5a329 353
750a5842
PM
354 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
355 info->cstate = PPTP_CALL_OUT_CONF;
356 info->pac_call_id = cid;
357 exp_gre(ct, cid, pcid);
358 } else
359 info->cstate = PPTP_CALL_NONE;
926b50f9
HW
360 break;
361
362 case PPTP_IN_CALL_REQUEST:
926b50f9 363 /* server tells us about incoming call request */
87a0117a
PM
364 if (info->sstate != PPTP_SESSION_CONFIRMED)
365 goto invalid;
366
62fbe9c8
PM
367 cid = pptpReq->icreq.callID;
368 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
926b50f9 369 info->cstate = PPTP_CALL_IN_REQ;
62fbe9c8 370 info->pac_call_id = cid;
926b50f9
HW
371 break;
372
373 case PPTP_IN_CALL_CONNECT:
926b50f9 374 /* server tells us about incoming call established */
87a0117a
PM
375 if (info->sstate != PPTP_SESSION_CONFIRMED)
376 goto invalid;
377 if (info->cstate != PPTP_CALL_IN_REP &&
378 info->cstate != PPTP_CALL_IN_CONF)
379 goto invalid;
926b50f9 380
857c06da
PM
381 pcid = pptpReq->iccon.peersCallID;
382 cid = info->pac_call_id;
926b50f9 383
87a0117a
PM
384 if (info->pns_call_id != pcid)
385 goto invalid;
926b50f9 386
857c06da 387 DEBUGP("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
926b50f9
HW
388 info->cstate = PPTP_CALL_IN_CONF;
389
390 /* we expect a GRE connection from PAC to PNS */
857c06da 391 exp_gre(ct, cid, pcid);
926b50f9
HW
392 break;
393
394 case PPTP_CALL_DISCONNECT_NOTIFY:
926b50f9 395 /* server confirms disconnect */
857c06da
PM
396 cid = pptpReq->disc.callID;
397 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
926b50f9
HW
398 info->cstate = PPTP_CALL_NONE;
399
400 /* untrack this call id, unexpect GRE packets */
401 pptp_destroy_siblings(ct);
402 break;
403
404 case PPTP_WAN_ERROR_NOTIFY:
926b50f9
HW
405 case PPTP_ECHO_REQUEST:
406 case PPTP_ECHO_REPLY:
407 /* I don't have to explain these ;) */
408 break;
409 default:
87a0117a 410 goto invalid;
926b50f9
HW
411 }
412
337fbc41
PM
413 ip_nat_pptp_inbound = rcu_dereference(ip_nat_pptp_hook_inbound);
414 if (ip_nat_pptp_inbound)
415 return ip_nat_pptp_inbound(pskb, ct, ctinfo, ctlh, pptpReq);
926b50f9
HW
416 return NF_ACCEPT;
417
87a0117a
PM
418invalid:
419 DEBUGP("invalid %s: type=%d cid=%u pcid=%u "
420 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
421 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
422 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
423 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
424 return NF_ACCEPT;
926b50f9
HW
425}
426
427static inline int
428pptp_outbound_pkt(struct sk_buff **pskb,
4c651756
PM
429 struct PptpControlHeader *ctlh,
430 union pptp_ctrl_union *pptpReq,
431 unsigned int reqlen,
926b50f9
HW
432 struct ip_conntrack *ct,
433 enum ip_conntrack_info ctinfo)
434{
926b50f9 435 struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
67497205 436 u_int16_t msg;
87a0117a 437 __be16 cid = 0, pcid = 0;
337fbc41 438 typeof(ip_nat_pptp_hook_outbound) ip_nat_pptp_outbound;
926b50f9 439
926b50f9
HW
440 msg = ntohs(ctlh->messageType);
441 DEBUGP("outbound control message %s\n", pptp_msg_name[msg]);
442
443 switch (msg) {
444 case PPTP_START_SESSION_REQUEST:
445 /* client requests for new control session */
87a0117a
PM
446 if (info->sstate != PPTP_SESSION_NONE)
447 goto invalid;
926b50f9
HW
448 info->sstate = PPTP_SESSION_REQUESTED;
449 break;
450 case PPTP_STOP_SESSION_REQUEST:
451 /* client requests end of control session */
452 info->sstate = PPTP_SESSION_STOPREQ;
453 break;
454
455 case PPTP_OUT_CALL_REQUEST:
926b50f9 456 /* client initiating connection to server */
87a0117a
PM
457 if (info->sstate != PPTP_SESSION_CONFIRMED)
458 goto invalid;
926b50f9
HW
459 info->cstate = PPTP_CALL_OUT_REQ;
460 /* track PNS call id */
857c06da
PM
461 cid = pptpReq->ocreq.callID;
462 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
463 info->pns_call_id = cid;
926b50f9
HW
464 break;
465 case PPTP_IN_CALL_REPLY:
926b50f9 466 /* client answers incoming call */
87a0117a
PM
467 if (info->cstate != PPTP_CALL_IN_REQ &&
468 info->cstate != PPTP_CALL_IN_REP)
469 goto invalid;
470
62fbe9c8 471 cid = pptpReq->icack.callID;
857c06da 472 pcid = pptpReq->icack.peersCallID;
87a0117a
PM
473 if (info->pac_call_id != pcid)
474 goto invalid;
62fbe9c8
PM
475 DEBUGP("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
476 ntohs(cid), ntohs(pcid));
750a5842
PM
477
478 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
479 /* part two of the three-way handshake */
480 info->cstate = PPTP_CALL_IN_REP;
62fbe9c8 481 info->pns_call_id = cid;
750a5842
PM
482 } else
483 info->cstate = PPTP_CALL_NONE;
926b50f9
HW
484 break;
485
486 case PPTP_CALL_CLEAR_REQUEST:
487 /* client requests hangup of call */
87a0117a
PM
488 if (info->sstate != PPTP_SESSION_CONFIRMED)
489 goto invalid;
926b50f9
HW
490 /* FUTURE: iterate over all calls and check if
491 * call ID is valid. We don't do this without newnat,
492 * because we only know about last call */
493 info->cstate = PPTP_CALL_CLEAR_REQ;
494 break;
495 case PPTP_SET_LINK_INFO:
926b50f9
HW
496 case PPTP_ECHO_REQUEST:
497 case PPTP_ECHO_REPLY:
498 /* I don't have to explain these ;) */
499 break;
500 default:
87a0117a 501 goto invalid;
926b50f9 502 }
edd5a329 503
337fbc41
PM
504 ip_nat_pptp_outbound = rcu_dereference(ip_nat_pptp_hook_outbound);
505 if (ip_nat_pptp_outbound)
506 return ip_nat_pptp_outbound(pskb, ct, ctinfo, ctlh, pptpReq);
87a0117a 507 return NF_ACCEPT;
926b50f9 508
87a0117a
PM
509invalid:
510 DEBUGP("invalid %s: type=%d cid=%u pcid=%u "
511 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
512 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
513 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
514 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
926b50f9
HW
515 return NF_ACCEPT;
516}
517
4c651756
PM
518static const unsigned int pptp_msg_size[] = {
519 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
520 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
521 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
522 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
523 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
524 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
525 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
526 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
527 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
528 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
529 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
530 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
531 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
532};
926b50f9
HW
533
534/* track caller id inside control connection, call expect_related */
edd5a329 535static int
926b50f9
HW
536conntrack_pptp_help(struct sk_buff **pskb,
537 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
538
539{
926b50f9
HW
540 int dir = CTINFO2DIR(ctinfo);
541 struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
4c651756
PM
542 struct tcphdr _tcph, *tcph;
543 struct pptp_pkt_hdr _pptph, *pptph;
544 struct PptpControlHeader _ctlh, *ctlh;
545 union pptp_ctrl_union _pptpReq, *pptpReq;
546 unsigned int tcplen = (*pskb)->len - (*pskb)->nh.iph->ihl * 4;
547 unsigned int datalen, reqlen, nexthdr_off;
926b50f9
HW
548 int oldsstate, oldcstate;
549 int ret;
4c651756 550 u_int16_t msg;
926b50f9
HW
551
552 /* don't do any tracking before tcp handshake complete */
edd5a329 553 if (ctinfo != IP_CT_ESTABLISHED
926b50f9
HW
554 && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
555 DEBUGP("ctinfo = %u, skipping\n", ctinfo);
556 return NF_ACCEPT;
557 }
edd5a329 558
926b50f9
HW
559 nexthdr_off = (*pskb)->nh.iph->ihl*4;
560 tcph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_tcph), &_tcph);
561 BUG_ON(!tcph);
562 nexthdr_off += tcph->doff * 4;
e905a9ed 563 datalen = tcplen - tcph->doff * 4;
926b50f9 564
926b50f9
HW
565 pptph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_pptph), &_pptph);
566 if (!pptph) {
567 DEBUGP("no full PPTP header, can't track\n");
568 return NF_ACCEPT;
569 }
570 nexthdr_off += sizeof(_pptph);
571 datalen -= sizeof(_pptph);
572
573 /* if it's not a control message we can't do anything with it */
574 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
575 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
576 DEBUGP("not a control packet\n");
577 return NF_ACCEPT;
578 }
579
4c651756
PM
580 ctlh = skb_header_pointer(*pskb, nexthdr_off, sizeof(_ctlh), &_ctlh);
581 if (!ctlh)
582 return NF_ACCEPT;
583 nexthdr_off += sizeof(_ctlh);
584 datalen -= sizeof(_ctlh);
585
586 reqlen = datalen;
587 msg = ntohs(ctlh->messageType);
588 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
589 return NF_ACCEPT;
590 if (reqlen > sizeof(*pptpReq))
591 reqlen = sizeof(*pptpReq);
592
593 pptpReq = skb_header_pointer(*pskb, nexthdr_off, reqlen, &_pptpReq);
594 if (!pptpReq)
595 return NF_ACCEPT;
596
926b50f9
HW
597 oldsstate = info->sstate;
598 oldcstate = info->cstate;
599
600 spin_lock_bh(&ip_pptp_lock);
601
602 /* FIXME: We just blindly assume that the control connection is always
603 * established from PNS->PAC. However, RFC makes no guarantee */
604 if (dir == IP_CT_DIR_ORIGINAL)
605 /* client -> server (PNS -> PAC) */
4c651756 606 ret = pptp_outbound_pkt(pskb, ctlh, pptpReq, reqlen, ct,
926b50f9
HW
607 ctinfo);
608 else
609 /* server -> client (PAC -> PNS) */
4c651756 610 ret = pptp_inbound_pkt(pskb, ctlh, pptpReq, reqlen, ct,
926b50f9
HW
611 ctinfo);
612 DEBUGP("sstate: %d->%d, cstate: %d->%d\n",
613 oldsstate, info->sstate, oldcstate, info->cstate);
614 spin_unlock_bh(&ip_pptp_lock);
615
616 return ret;
617}
618
619/* control protocol helper */
edd5a329 620static struct ip_conntrack_helper pptp = {
926b50f9 621 .list = { NULL, NULL },
edd5a329 622 .name = "pptp",
926b50f9
HW
623 .me = THIS_MODULE,
624 .max_expected = 2,
625 .timeout = 5 * 60,
edd5a329 626 .tuple = { .src = { .ip = 0,
e905a9ed 627 .u = { .tcp = { .port =
edd5a329
PM
628 __constant_htons(PPTP_CONTROL_PORT) } }
629 },
630 .dst = { .ip = 0,
926b50f9
HW
631 .u = { .all = 0 },
632 .protonum = IPPROTO_TCP
edd5a329 633 }
926b50f9 634 },
edd5a329
PM
635 .mask = { .src = { .ip = 0,
636 .u = { .tcp = { .port = __constant_htons(0xffff) } }
637 },
638 .dst = { .ip = 0,
926b50f9 639 .u = { .all = 0 },
edd5a329 640 .protonum = 0xff
e905a9ed 641 }
926b50f9 642 },
4c5de695
PM
643 .help = conntrack_pptp_help,
644 .destroy = pptp_destroy_siblings,
926b50f9
HW
645};
646
a7768097 647extern void ip_ct_proto_gre_fini(void);
926b50f9
HW
648extern int __init ip_ct_proto_gre_init(void);
649
650/* ip_conntrack_pptp initialization */
65b4b4e8 651static int __init ip_conntrack_helper_pptp_init(void)
926b50f9
HW
652{
653 int retcode;
edd5a329 654
926b50f9
HW
655 retcode = ip_ct_proto_gre_init();
656 if (retcode < 0)
657 return retcode;
658
659 DEBUGP(" registering helper\n");
660 if ((retcode = ip_conntrack_helper_register(&pptp))) {
661 printk(KERN_ERR "Unable to register conntrack application "
662 "helper for pptp: %d\n", retcode);
663 ip_ct_proto_gre_fini();
664 return retcode;
665 }
666
667 printk("ip_conntrack_pptp version %s loaded\n", IP_CT_PPTP_VERSION);
668 return 0;
669}
670
65b4b4e8 671static void __exit ip_conntrack_helper_pptp_fini(void)
926b50f9
HW
672{
673 ip_conntrack_helper_unregister(&pptp);
674 ip_ct_proto_gre_fini();
675 printk("ip_conntrack_pptp version %s unloaded\n", IP_CT_PPTP_VERSION);
676}
677
65b4b4e8
AM
678module_init(ip_conntrack_helper_pptp_init);
679module_exit(ip_conntrack_helper_pptp_fini);
926b50f9
HW
680
681EXPORT_SYMBOL(ip_nat_pptp_hook_outbound);
682EXPORT_SYMBOL(ip_nat_pptp_hook_inbound);
683EXPORT_SYMBOL(ip_nat_pptp_hook_exp_gre);
684EXPORT_SYMBOL(ip_nat_pptp_hook_expectfn);