blob: 1bc3001d182773e33872081d4b40fc7bf6f4e95d [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
2 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
3 * PPTP is a a protocol for creating virtual private networks.
4 * It is a specification defined by Microsoft and some vendors
5 * working with Microsoft. PPTP is built on top of a modified
6 * version of the Internet Generic Routing Encapsulation Protocol.
7 * GRE is defined in RFC 1701 and RFC 1702. Documentation of
8 * PPTP can be found in RFC 2637
9 *
10 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
11 *
12 * Development of this code funded by Astaro AG (http://www.astaro.com/)
13 *
14 * Limitations:
15 * - We blindly assume that control connections are always
16 * established in PNS->PAC direction. This is a violation
17 * of RFFC2673
18 * - We can only support one single call within each session
19 * TODO:
20 * - testing of incoming PPTP calls
21 */
22
23#include <linux/module.h>
24#include <linux/skbuff.h>
25#include <linux/in.h>
26#include <linux/tcp.h>
27
28#include <net/netfilter/nf_conntrack.h>
29#include <net/netfilter/nf_conntrack_core.h>
30#include <net/netfilter/nf_conntrack_helper.h>
31#include <linux/netfilter/nf_conntrack_proto_gre.h>
32#include <linux/netfilter/nf_conntrack_pptp.h>
33
34#define NF_CT_PPTP_VERSION "3.1"
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
38MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
39MODULE_ALIAS("ip_conntrack_pptp");
40
41static DEFINE_SPINLOCK(nf_pptp_lock);
42
43int
Herbert Xu3db05fe2007-10-15 00:53:15 -070044(*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080045 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
46 struct PptpControlHeader *ctlh,
47 union pptp_ctrl_union *pptpReq) __read_mostly;
48EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
49
50int
Herbert Xu3db05fe2007-10-15 00:53:15 -070051(*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080052 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
53 struct PptpControlHeader *ctlh,
54 union pptp_ctrl_union *pptpReq) __read_mostly;
55EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
56
57void
58(*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
59 struct nf_conntrack_expect *expect_reply)
60 __read_mostly;
61EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
62
63void
64(*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
65 struct nf_conntrack_expect *exp) __read_mostly;
66EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
67
Jason Baron346e15b2008-08-12 16:46:19 -040068#if defined(DEBUG) || defined(CONFIG_DYNAMIC_PRINTK_DEBUG)
Patrick McHardyf09943f2006-12-02 22:09:41 -080069/* PptpControlMessageType names */
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080070const char *const pptp_msg_name[] = {
Patrick McHardyf09943f2006-12-02 22:09:41 -080071 "UNKNOWN_MESSAGE",
72 "START_SESSION_REQUEST",
73 "START_SESSION_REPLY",
74 "STOP_SESSION_REQUEST",
75 "STOP_SESSION_REPLY",
76 "ECHO_REQUEST",
77 "ECHO_REPLY",
78 "OUT_CALL_REQUEST",
79 "OUT_CALL_REPLY",
80 "IN_CALL_REQUEST",
81 "IN_CALL_REPLY",
82 "IN_CALL_CONNECT",
83 "CALL_CLEAR_REQUEST",
84 "CALL_DISCONNECT_NOTIFY",
85 "WAN_ERROR_NOTIFY",
86 "SET_LINK_INFO"
87};
88EXPORT_SYMBOL(pptp_msg_name);
Patrick McHardyf09943f2006-12-02 22:09:41 -080089#endif
90
91#define SECS *HZ
92#define MINS * 60 SECS
93#define HOURS * 60 MINS
94
95#define PPTP_GRE_TIMEOUT (10 MINS)
96#define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
97
98static void pptp_expectfn(struct nf_conn *ct,
99 struct nf_conntrack_expect *exp)
100{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200101 struct net *net = nf_ct_net(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800102 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
Patrick McHardy0d537782007-07-07 22:39:38 -0700103 pr_debug("increasing timeouts\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800104
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);
Patrick McHardy73990722007-01-26 01:07:59 -0800114 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800115 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700122 pr_debug("trying to unexpect other dir: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200123 nf_ct_dump_tuple(&inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800124
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200125 exp_other = nf_ct_expect_find_get(net, &inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800126 if (exp_other) {
127 /* delete other expectation. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700128 pr_debug("found\n");
Patrick McHardy68236452007-07-07 22:30:49 -0700129 nf_ct_unexpect_related(exp_other);
130 nf_ct_expect_put(exp_other);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800131 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700132 pr_debug("not found\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800133 }
134 }
135 rcu_read_unlock();
136}
137
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200138static int destroy_sibling_or_exp(struct net *net,
139 const struct nf_conntrack_tuple *t)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800140{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800141 const struct nf_conntrack_tuple_hash *h;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800142 struct nf_conntrack_expect *exp;
143 struct nf_conn *sibling;
144
Patrick McHardy0d537782007-07-07 22:39:38 -0700145 pr_debug("trying to timeout ct or exp for tuple ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200146 nf_ct_dump_tuple(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800147
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200148 h = nf_conntrack_find_get(net, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800149 if (h) {
150 sibling = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy0d537782007-07-07 22:39:38 -0700151 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800152 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 {
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200159 exp = nf_ct_expect_find_get(net, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800160 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700161 pr_debug("unexpect_related of expect %p\n", exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700162 nf_ct_unexpect_related(exp);
163 nf_ct_expect_put(exp);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800164 return 1;
165 }
166 }
167 return 0;
168}
169
170/* timeout GRE data connections */
171static void pptp_destroy_siblings(struct nf_conn *ct)
172{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200173 struct net *net = nf_ct_net(ct);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800174 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800175 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;
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200184 if (!destroy_sibling_or_exp(net, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700185 pr_debug("failed to timeout original pns->pac ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800186
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;
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200192 if (!destroy_sibling_or_exp(net, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700193 pr_debug("failed to timeout reply pac->pns ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800194}
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
Patrick McHardy68236452007-07-07 22:30:49 -0700204 exp_orig = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800205 if (exp_orig == NULL)
206 goto out;
207
Patrick McHardy68236452007-07-07 22:30:49 -0700208 exp_reply = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800209 if (exp_reply == NULL)
210 goto out_put_orig;
211
212 /* original direction, PNS->PAC */
213 dir = IP_CT_DIR_ORIGINAL;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700214 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200215 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700216 &ct->tuplehash[dir].tuple.src.u3,
217 &ct->tuplehash[dir].tuple.dst.u3,
218 IPPROTO_GRE, &peer_callid, &callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800219 exp_orig->expectfn = pptp_expectfn;
220
221 /* reply direction, PAC->PNS */
222 dir = IP_CT_DIR_REPLY;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700223 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200224 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700225 &ct->tuplehash[dir].tuple.src.u3,
226 &ct->tuplehash[dir].tuple.dst.u3,
227 IPPROTO_GRE, &callid, &peer_callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800228 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);
Patrick McHardy68236452007-07-07 22:30:49 -0700233 if (nf_ct_expect_related(exp_orig) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800234 goto out_put_both;
Patrick McHardy68236452007-07-07 22:30:49 -0700235 if (nf_ct_expect_related(exp_reply) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800236 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:
Patrick McHardy68236452007-07-07 22:30:49 -0700248 nf_ct_expect_put(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800249out_put_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700250 nf_ct_expect_put(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800251out:
252 return ret;
253
254out_unexpect_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700255 nf_ct_unexpect_related(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800256out_unexpect_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700257 nf_ct_unexpect_related(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800258 goto out_put_both;
259}
260
261static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700262pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800263 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700275 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800276
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700310 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
311 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800312
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700327 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800328 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
Patrick McHardy0d537782007-07-07 22:39:38 -0700346 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800347 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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700356 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800357 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)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700375 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800376 return NF_ACCEPT;
377
378invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700379 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));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800384 return NF_ACCEPT;
385}
386
387static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700388pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800389 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);
Patrick McHardy0d537782007-07-07 22:39:38 -0700401 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800402
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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700423 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800424 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;
Patrick McHardy0d537782007-07-07 22:39:38 -0700437 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
438 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800439
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)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700470 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800471 return NF_ACCEPT;
472
473invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700474 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));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800479 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
Herbert Xu3db05fe2007-10-15 00:53:15 -0700500conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800501 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
502
503{
504 int dir = CTINFO2DIR(ctinfo);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800505 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;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800510 struct PptpControlHeader _ctlh, *ctlh;
511 union pptp_ctrl_union _pptpReq, *pptpReq;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700512 unsigned int tcplen = skb->len - protoff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800513 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;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700524 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800525 BUG_ON(!tcph);
526 nexthdr_off += tcph->doff * 4;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800527 datalen = tcplen - tcph->doff * 4;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800528
Herbert Xu3db05fe2007-10-15 00:53:15 -0700529 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800530 if (!pptph) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700531 pr_debug("no full PPTP header, can't track\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800532 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) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700540 pr_debug("not a control packet\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800541 return NF_ACCEPT;
542 }
543
Herbert Xu3db05fe2007-10-15 00:53:15 -0700544 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800545 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
Herbert Xu3db05fe2007-10-15 00:53:15 -0700557 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800558 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) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700570 ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800571 ctinfo);
572 else
573 /* server -> client (PAC -> PNS) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700574 ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800575 ctinfo);
Patrick McHardy0d537782007-07-07 22:39:38 -0700576 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
577 oldsstate, info->sstate, oldcstate, info->cstate);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800578 spin_unlock_bh(&nf_pptp_lock);
579
580 return ret;
581}
582
Patrick McHardy6002f2662008-03-25 20:09:15 -0700583static const struct nf_conntrack_expect_policy pptp_exp_policy = {
584 .max_expected = 2,
585 .timeout = 5 * 60,
586};
587
Patrick McHardyf09943f2006-12-02 22:09:41 -0800588/* control protocol helper */
589static struct nf_conntrack_helper pptp __read_mostly = {
590 .name = "pptp",
591 .me = THIS_MODULE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800592 .tuple.src.l3num = AF_INET,
593 .tuple.src.u.tcp.port = __constant_htons(PPTP_CONTROL_PORT),
594 .tuple.dst.protonum = IPPROTO_TCP,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800595 .help = conntrack_pptp_help,
596 .destroy = pptp_destroy_siblings,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700597 .expect_policy = &pptp_exp_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800598};
599
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200600static 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
Patrick McHardyf09943f2006-12-02 22:09:41 -0800609static int __init nf_conntrack_pptp_init(void)
610{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200611 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;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800620}
621
622static void __exit nf_conntrack_pptp_fini(void)
623{
624 nf_conntrack_helper_unregister(&pptp);
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200625 unregister_pernet_subsys(&nf_conntrack_pptp_net_ops);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800626}
627
628module_init(nf_conntrack_pptp_init);
629module_exit(nf_conntrack_pptp_fini);