blob: 9e169ef2e85443eaff5c8224ccbeaad1d0f4a202 [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");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010040MODULE_ALIAS_NFCT_HELPER("pptp");
Patrick McHardyf09943f2006-12-02 22:09:41 -080041
42static DEFINE_SPINLOCK(nf_pptp_lock);
43
44int
Herbert Xu3db05fe2007-10-15 00:53:15 -070045(*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080046 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
Herbert Xu3db05fe2007-10-15 00:53:15 -070052(*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080053 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
Jason Baron346e15b2008-08-12 16:46:19 -040069#if defined(DEBUG) || defined(CONFIG_DYNAMIC_PRINTK_DEBUG)
Patrick McHardyf09943f2006-12-02 22:09:41 -080070/* PptpControlMessageType names */
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080071const char *const pptp_msg_name[] = {
Patrick McHardyf09943f2006-12-02 22:09:41 -080072 "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);
Patrick McHardyf09943f2006-12-02 22:09:41 -080090#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{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200102 struct net *net = nf_ct_net(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800103 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
Patrick McHardy0d537782007-07-07 22:39:38 -0700104 pr_debug("increasing timeouts\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800105
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);
Patrick McHardy73990722007-01-26 01:07:59 -0800115 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800116 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700123 pr_debug("trying to unexpect other dir: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200124 nf_ct_dump_tuple(&inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800125
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200126 exp_other = nf_ct_expect_find_get(net, &inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800127 if (exp_other) {
128 /* delete other expectation. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700129 pr_debug("found\n");
Patrick McHardy68236452007-07-07 22:30:49 -0700130 nf_ct_unexpect_related(exp_other);
131 nf_ct_expect_put(exp_other);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800132 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700133 pr_debug("not found\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800134 }
135 }
136 rcu_read_unlock();
137}
138
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200139static int destroy_sibling_or_exp(struct net *net,
140 const struct nf_conntrack_tuple *t)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800141{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800142 const struct nf_conntrack_tuple_hash *h;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800143 struct nf_conntrack_expect *exp;
144 struct nf_conn *sibling;
145
Patrick McHardy0d537782007-07-07 22:39:38 -0700146 pr_debug("trying to timeout ct or exp for tuple ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200147 nf_ct_dump_tuple(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800148
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200149 h = nf_conntrack_find_get(net, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800150 if (h) {
151 sibling = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy0d537782007-07-07 22:39:38 -0700152 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800153 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 {
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200160 exp = nf_ct_expect_find_get(net, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800161 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700162 pr_debug("unexpect_related of expect %p\n", exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700163 nf_ct_unexpect_related(exp);
164 nf_ct_expect_put(exp);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800165 return 1;
166 }
167 }
168 return 0;
169}
170
171/* timeout GRE data connections */
172static void pptp_destroy_siblings(struct nf_conn *ct)
173{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200174 struct net *net = nf_ct_net(ct);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800175 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800176 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;
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200185 if (!destroy_sibling_or_exp(net, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700186 pr_debug("failed to timeout original pns->pac ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800187
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;
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200193 if (!destroy_sibling_or_exp(net, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700194 pr_debug("failed to timeout reply pac->pns ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800195}
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
Patrick McHardy68236452007-07-07 22:30:49 -0700205 exp_orig = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800206 if (exp_orig == NULL)
207 goto out;
208
Patrick McHardy68236452007-07-07 22:30:49 -0700209 exp_reply = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800210 if (exp_reply == NULL)
211 goto out_put_orig;
212
213 /* original direction, PNS->PAC */
214 dir = IP_CT_DIR_ORIGINAL;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700215 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200216 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700217 &ct->tuplehash[dir].tuple.src.u3,
218 &ct->tuplehash[dir].tuple.dst.u3,
219 IPPROTO_GRE, &peer_callid, &callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800220 exp_orig->expectfn = pptp_expectfn;
221
222 /* reply direction, PAC->PNS */
223 dir = IP_CT_DIR_REPLY;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700224 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200225 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700226 &ct->tuplehash[dir].tuple.src.u3,
227 &ct->tuplehash[dir].tuple.dst.u3,
228 IPPROTO_GRE, &callid, &peer_callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800229 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);
Patrick McHardy68236452007-07-07 22:30:49 -0700234 if (nf_ct_expect_related(exp_orig) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800235 goto out_put_both;
Patrick McHardy68236452007-07-07 22:30:49 -0700236 if (nf_ct_expect_related(exp_reply) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800237 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:
Patrick McHardy68236452007-07-07 22:30:49 -0700249 nf_ct_expect_put(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800250out_put_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700251 nf_ct_expect_put(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800252out:
253 return ret;
254
255out_unexpect_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700256 nf_ct_unexpect_related(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800257out_unexpect_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700258 nf_ct_unexpect_related(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800259 goto out_put_both;
260}
261
262static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700263pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800264 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700276 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800277
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700311 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
312 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800313
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700328 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800329 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
Patrick McHardy0d537782007-07-07 22:39:38 -0700347 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800348 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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700357 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800358 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)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700376 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800377 return NF_ACCEPT;
378
379invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700380 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));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800385 return NF_ACCEPT;
386}
387
388static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700389pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800390 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700402 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800403
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700424 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800425 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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700438 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
439 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800440
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)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700471 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800472 return NF_ACCEPT;
473
474invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700475 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));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800480 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
Herbert Xu3db05fe2007-10-15 00:53:15 -0700501conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800502 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
503
504{
505 int dir = CTINFO2DIR(ctinfo);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800506 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;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800511 struct PptpControlHeader _ctlh, *ctlh;
512 union pptp_ctrl_union _pptpReq, *pptpReq;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700513 unsigned int tcplen = skb->len - protoff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800514 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;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700525 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800526 BUG_ON(!tcph);
527 nexthdr_off += tcph->doff * 4;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800528 datalen = tcplen - tcph->doff * 4;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800529
Herbert Xu3db05fe2007-10-15 00:53:15 -0700530 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800531 if (!pptph) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700532 pr_debug("no full PPTP header, can't track\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800533 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) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700541 pr_debug("not a control packet\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800542 return NF_ACCEPT;
543 }
544
Herbert Xu3db05fe2007-10-15 00:53:15 -0700545 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800546 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
Herbert Xu3db05fe2007-10-15 00:53:15 -0700558 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800559 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) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700571 ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800572 ctinfo);
573 else
574 /* server -> client (PAC -> PNS) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700575 ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800576 ctinfo);
Patrick McHardy0d537782007-07-07 22:39:38 -0700577 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
578 oldsstate, info->sstate, oldcstate, info->cstate);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800579 spin_unlock_bh(&nf_pptp_lock);
580
581 return ret;
582}
583
Patrick McHardy6002f2662008-03-25 20:09:15 -0700584static const struct nf_conntrack_expect_policy pptp_exp_policy = {
585 .max_expected = 2,
586 .timeout = 5 * 60,
587};
588
Patrick McHardyf09943f2006-12-02 22:09:41 -0800589/* control protocol helper */
590static struct nf_conntrack_helper pptp __read_mostly = {
591 .name = "pptp",
592 .me = THIS_MODULE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800593 .tuple.src.l3num = AF_INET,
594 .tuple.src.u.tcp.port = __constant_htons(PPTP_CONTROL_PORT),
595 .tuple.dst.protonum = IPPROTO_TCP,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800596 .help = conntrack_pptp_help,
597 .destroy = pptp_destroy_siblings,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700598 .expect_policy = &pptp_exp_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800599};
600
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200601static 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
Patrick McHardyf09943f2006-12-02 22:09:41 -0800610static int __init nf_conntrack_pptp_init(void)
611{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200612 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;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800621}
622
623static void __exit nf_conntrack_pptp_fini(void)
624{
625 nf_conntrack_helper_unregister(&pptp);
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200626 unregister_pernet_subsys(&nf_conntrack_pptp_net_ops);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800627}
628
629module_init(nf_conntrack_pptp_init);
630module_exit(nf_conntrack_pptp_fini);