blob: 088944824e135f53c769477aa201888cd343e398 [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>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010031#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080032#include <linux/netfilter/nf_conntrack_proto_gre.h>
33#include <linux/netfilter/nf_conntrack_pptp.h>
34
35#define NF_CT_PPTP_VERSION "3.1"
36
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
39MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
40MODULE_ALIAS("ip_conntrack_pptp");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010041MODULE_ALIAS_NFCT_HELPER("pptp");
Patrick McHardyf09943f2006-12-02 22:09:41 -080042
43static DEFINE_SPINLOCK(nf_pptp_lock);
44
45int
Herbert Xu3db05fe2007-10-15 00:53:15 -070046(*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080047 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
48 struct PptpControlHeader *ctlh,
49 union pptp_ctrl_union *pptpReq) __read_mostly;
50EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
51
52int
Herbert Xu3db05fe2007-10-15 00:53:15 -070053(*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -080054 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
55 struct PptpControlHeader *ctlh,
56 union pptp_ctrl_union *pptpReq) __read_mostly;
57EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
58
59void
60(*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
61 struct nf_conntrack_expect *expect_reply)
62 __read_mostly;
63EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
64
65void
66(*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
67 struct nf_conntrack_expect *exp) __read_mostly;
68EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
69
Jason Barone9d376f2009-02-05 11:51:38 -050070#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
Patrick McHardyf09943f2006-12-02 22:09:41 -080071/* PptpControlMessageType names */
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080072const char *const pptp_msg_name[] = {
Patrick McHardyf09943f2006-12-02 22:09:41 -080073 "UNKNOWN_MESSAGE",
74 "START_SESSION_REQUEST",
75 "START_SESSION_REPLY",
76 "STOP_SESSION_REQUEST",
77 "STOP_SESSION_REPLY",
78 "ECHO_REQUEST",
79 "ECHO_REPLY",
80 "OUT_CALL_REQUEST",
81 "OUT_CALL_REPLY",
82 "IN_CALL_REQUEST",
83 "IN_CALL_REPLY",
84 "IN_CALL_CONNECT",
85 "CALL_CLEAR_REQUEST",
86 "CALL_DISCONNECT_NOTIFY",
87 "WAN_ERROR_NOTIFY",
88 "SET_LINK_INFO"
89};
90EXPORT_SYMBOL(pptp_msg_name);
Patrick McHardyf09943f2006-12-02 22:09:41 -080091#endif
92
93#define SECS *HZ
94#define MINS * 60 SECS
95#define HOURS * 60 MINS
96
97#define PPTP_GRE_TIMEOUT (10 MINS)
98#define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
99
100static void pptp_expectfn(struct nf_conn *ct,
101 struct nf_conntrack_expect *exp)
102{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200103 struct net *net = nf_ct_net(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800104 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
Patrick McHardy0d537782007-07-07 22:39:38 -0700105 pr_debug("increasing timeouts\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800106
107 /* increase timeout of GRE data channel conntrack entry */
108 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
109 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
110
111 /* Can you see how rusty this code is, compared with the pre-2.6.11
112 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
113
114 rcu_read_lock();
115 nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
Patrick McHardy73990722007-01-26 01:07:59 -0800116 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800117 nf_nat_pptp_expectfn(ct, exp);
118 else {
119 struct nf_conntrack_tuple inv_t;
120 struct nf_conntrack_expect *exp_other;
121
122 /* obviously this tuple inversion only works until you do NAT */
123 nf_ct_invert_tuplepr(&inv_t, &exp->tuple);
Patrick McHardy0d537782007-07-07 22:39:38 -0700124 pr_debug("trying to unexpect other dir: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200125 nf_ct_dump_tuple(&inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800126
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100127 exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800128 if (exp_other) {
129 /* delete other expectation. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700130 pr_debug("found\n");
Patrick McHardy68236452007-07-07 22:30:49 -0700131 nf_ct_unexpect_related(exp_other);
132 nf_ct_expect_put(exp_other);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800133 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700134 pr_debug("not found\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800135 }
136 }
137 rcu_read_unlock();
138}
139
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100140static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200141 const struct nf_conntrack_tuple *t)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800142{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800143 const struct nf_conntrack_tuple_hash *h;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800144 struct nf_conntrack_expect *exp;
145 struct nf_conn *sibling;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100146 u16 zone = nf_ct_zone(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800147
Patrick McHardy0d537782007-07-07 22:39:38 -0700148 pr_debug("trying to timeout ct or exp for tuple ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200149 nf_ct_dump_tuple(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800150
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100151 h = nf_conntrack_find_get(net, zone, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800152 if (h) {
153 sibling = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy0d537782007-07-07 22:39:38 -0700154 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800155 sibling->proto.gre.timeout = 0;
156 sibling->proto.gre.stream_timeout = 0;
157 if (del_timer(&sibling->timeout))
158 sibling->timeout.function((unsigned long)sibling);
159 nf_ct_put(sibling);
160 return 1;
161 } else {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100162 exp = nf_ct_expect_find_get(net, zone, t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800163 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700164 pr_debug("unexpect_related of expect %p\n", exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700165 nf_ct_unexpect_related(exp);
166 nf_ct_expect_put(exp);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800167 return 1;
168 }
169 }
170 return 0;
171}
172
173/* timeout GRE data connections */
174static void pptp_destroy_siblings(struct nf_conn *ct)
175{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200176 struct net *net = nf_ct_net(ct);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800177 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800178 struct nf_conntrack_tuple t;
179
180 nf_ct_gre_keymap_destroy(ct);
181
182 /* try original (pns->pac) tuple */
183 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
184 t.dst.protonum = IPPROTO_GRE;
185 t.src.u.gre.key = help->help.ct_pptp_info.pns_call_id;
186 t.dst.u.gre.key = help->help.ct_pptp_info.pac_call_id;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100187 if (!destroy_sibling_or_exp(net, ct, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700188 pr_debug("failed to timeout original pns->pac ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800189
190 /* try reply (pac->pns) tuple */
191 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
192 t.dst.protonum = IPPROTO_GRE;
193 t.src.u.gre.key = help->help.ct_pptp_info.pac_call_id;
194 t.dst.u.gre.key = help->help.ct_pptp_info.pns_call_id;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100195 if (!destroy_sibling_or_exp(net, ct, &t))
Patrick McHardy0d537782007-07-07 22:39:38 -0700196 pr_debug("failed to timeout reply pac->pns ct/exp\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800197}
198
199/* expect GRE connections (PNS->PAC and PAC->PNS direction) */
200static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
201{
202 struct nf_conntrack_expect *exp_orig, *exp_reply;
203 enum ip_conntrack_dir dir;
204 int ret = 1;
205 typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
206
Patrick McHardy68236452007-07-07 22:30:49 -0700207 exp_orig = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800208 if (exp_orig == NULL)
209 goto out;
210
Patrick McHardy68236452007-07-07 22:30:49 -0700211 exp_reply = nf_ct_expect_alloc(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800212 if (exp_reply == NULL)
213 goto out_put_orig;
214
215 /* original direction, PNS->PAC */
216 dir = IP_CT_DIR_ORIGINAL;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700217 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200218 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700219 &ct->tuplehash[dir].tuple.src.u3,
220 &ct->tuplehash[dir].tuple.dst.u3,
221 IPPROTO_GRE, &peer_callid, &callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800222 exp_orig->expectfn = pptp_expectfn;
223
224 /* reply direction, PAC->PNS */
225 dir = IP_CT_DIR_REPLY;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700226 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200227 nf_ct_l3num(ct),
Patrick McHardy68236452007-07-07 22:30:49 -0700228 &ct->tuplehash[dir].tuple.src.u3,
229 &ct->tuplehash[dir].tuple.dst.u3,
230 IPPROTO_GRE, &callid, &peer_callid);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800231 exp_reply->expectfn = pptp_expectfn;
232
233 nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
234 if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
235 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
Patrick McHardy68236452007-07-07 22:30:49 -0700236 if (nf_ct_expect_related(exp_orig) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800237 goto out_put_both;
Patrick McHardy68236452007-07-07 22:30:49 -0700238 if (nf_ct_expect_related(exp_reply) != 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800239 goto out_unexpect_orig;
240
241 /* Add GRE keymap entries */
242 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
243 goto out_unexpect_both;
244 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
245 nf_ct_gre_keymap_destroy(ct);
246 goto out_unexpect_both;
247 }
248 ret = 0;
249
250out_put_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700251 nf_ct_expect_put(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800252out_put_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700253 nf_ct_expect_put(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800254out:
255 return ret;
256
257out_unexpect_both:
Patrick McHardy68236452007-07-07 22:30:49 -0700258 nf_ct_unexpect_related(exp_reply);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800259out_unexpect_orig:
Patrick McHardy68236452007-07-07 22:30:49 -0700260 nf_ct_unexpect_related(exp_orig);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800261 goto out_put_both;
262}
263
264static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700265pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800266 struct PptpControlHeader *ctlh,
267 union pptp_ctrl_union *pptpReq,
268 unsigned int reqlen,
269 struct nf_conn *ct,
270 enum ip_conntrack_info ctinfo)
271{
272 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
273 u_int16_t msg;
274 __be16 cid = 0, pcid = 0;
275 typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
276
277 msg = ntohs(ctlh->messageType);
Patrick McHardy0d537782007-07-07 22:39:38 -0700278 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800279
280 switch (msg) {
281 case PPTP_START_SESSION_REPLY:
282 /* server confirms new control session */
283 if (info->sstate < PPTP_SESSION_REQUESTED)
284 goto invalid;
285 if (pptpReq->srep.resultCode == PPTP_START_OK)
286 info->sstate = PPTP_SESSION_CONFIRMED;
287 else
288 info->sstate = PPTP_SESSION_ERROR;
289 break;
290
291 case PPTP_STOP_SESSION_REPLY:
292 /* server confirms end of control session */
293 if (info->sstate > PPTP_SESSION_STOPREQ)
294 goto invalid;
295 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
296 info->sstate = PPTP_SESSION_NONE;
297 else
298 info->sstate = PPTP_SESSION_ERROR;
299 break;
300
301 case PPTP_OUT_CALL_REPLY:
302 /* server accepted call, we now expect GRE frames */
303 if (info->sstate != PPTP_SESSION_CONFIRMED)
304 goto invalid;
305 if (info->cstate != PPTP_CALL_OUT_REQ &&
306 info->cstate != PPTP_CALL_OUT_CONF)
307 goto invalid;
308
309 cid = pptpReq->ocack.callID;
310 pcid = pptpReq->ocack.peersCallID;
311 if (info->pns_call_id != pcid)
312 goto invalid;
Patrick McHardy0d537782007-07-07 22:39:38 -0700313 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
314 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800315
316 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
317 info->cstate = PPTP_CALL_OUT_CONF;
318 info->pac_call_id = cid;
319 exp_gre(ct, cid, pcid);
320 } else
321 info->cstate = PPTP_CALL_NONE;
322 break;
323
324 case PPTP_IN_CALL_REQUEST:
325 /* server tells us about incoming call request */
326 if (info->sstate != PPTP_SESSION_CONFIRMED)
327 goto invalid;
328
329 cid = pptpReq->icreq.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700330 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800331 info->cstate = PPTP_CALL_IN_REQ;
332 info->pac_call_id = cid;
333 break;
334
335 case PPTP_IN_CALL_CONNECT:
336 /* server tells us about incoming call established */
337 if (info->sstate != PPTP_SESSION_CONFIRMED)
338 goto invalid;
339 if (info->cstate != PPTP_CALL_IN_REP &&
340 info->cstate != PPTP_CALL_IN_CONF)
341 goto invalid;
342
343 pcid = pptpReq->iccon.peersCallID;
344 cid = info->pac_call_id;
345
346 if (info->pns_call_id != pcid)
347 goto invalid;
348
Patrick McHardy0d537782007-07-07 22:39:38 -0700349 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800350 info->cstate = PPTP_CALL_IN_CONF;
351
352 /* we expect a GRE connection from PAC to PNS */
353 exp_gre(ct, cid, pcid);
354 break;
355
356 case PPTP_CALL_DISCONNECT_NOTIFY:
357 /* server confirms disconnect */
358 cid = pptpReq->disc.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700359 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800360 info->cstate = PPTP_CALL_NONE;
361
362 /* untrack this call id, unexpect GRE packets */
363 pptp_destroy_siblings(ct);
364 break;
365
366 case PPTP_WAN_ERROR_NOTIFY:
367 case PPTP_ECHO_REQUEST:
368 case PPTP_ECHO_REPLY:
369 /* I don't have to explain these ;) */
370 break;
371
372 default:
373 goto invalid;
374 }
375
376 nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
377 if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700378 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800379 return NF_ACCEPT;
380
381invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700382 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
383 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
384 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
385 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
386 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800387 return NF_ACCEPT;
388}
389
390static inline int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700391pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800392 struct PptpControlHeader *ctlh,
393 union pptp_ctrl_union *pptpReq,
394 unsigned int reqlen,
395 struct nf_conn *ct,
396 enum ip_conntrack_info ctinfo)
397{
398 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
399 u_int16_t msg;
400 __be16 cid = 0, pcid = 0;
401 typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
402
403 msg = ntohs(ctlh->messageType);
Patrick McHardy0d537782007-07-07 22:39:38 -0700404 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800405
406 switch (msg) {
407 case PPTP_START_SESSION_REQUEST:
408 /* client requests for new control session */
409 if (info->sstate != PPTP_SESSION_NONE)
410 goto invalid;
411 info->sstate = PPTP_SESSION_REQUESTED;
412 break;
413
414 case PPTP_STOP_SESSION_REQUEST:
415 /* client requests end of control session */
416 info->sstate = PPTP_SESSION_STOPREQ;
417 break;
418
419 case PPTP_OUT_CALL_REQUEST:
420 /* client initiating connection to server */
421 if (info->sstate != PPTP_SESSION_CONFIRMED)
422 goto invalid;
423 info->cstate = PPTP_CALL_OUT_REQ;
424 /* track PNS call id */
425 cid = pptpReq->ocreq.callID;
Patrick McHardy0d537782007-07-07 22:39:38 -0700426 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800427 info->pns_call_id = cid;
428 break;
429
430 case PPTP_IN_CALL_REPLY:
431 /* client answers incoming call */
432 if (info->cstate != PPTP_CALL_IN_REQ &&
433 info->cstate != PPTP_CALL_IN_REP)
434 goto invalid;
435
436 cid = pptpReq->icack.callID;
437 pcid = pptpReq->icack.peersCallID;
438 if (info->pac_call_id != pcid)
439 goto invalid;
Patrick McHardy0d537782007-07-07 22:39:38 -0700440 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
441 ntohs(cid), ntohs(pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800442
443 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
444 /* part two of the three-way handshake */
445 info->cstate = PPTP_CALL_IN_REP;
446 info->pns_call_id = cid;
447 } else
448 info->cstate = PPTP_CALL_NONE;
449 break;
450
451 case PPTP_CALL_CLEAR_REQUEST:
452 /* client requests hangup of call */
453 if (info->sstate != PPTP_SESSION_CONFIRMED)
454 goto invalid;
455 /* FUTURE: iterate over all calls and check if
456 * call ID is valid. We don't do this without newnat,
457 * because we only know about last call */
458 info->cstate = PPTP_CALL_CLEAR_REQ;
459 break;
460
461 case PPTP_SET_LINK_INFO:
462 case PPTP_ECHO_REQUEST:
463 case PPTP_ECHO_REPLY:
464 /* I don't have to explain these ;) */
465 break;
466
467 default:
468 goto invalid;
469 }
470
471 nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
472 if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700473 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800474 return NF_ACCEPT;
475
476invalid:
Patrick McHardy0d537782007-07-07 22:39:38 -0700477 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
478 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
479 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
480 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
481 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800482 return NF_ACCEPT;
483}
484
485static const unsigned int pptp_msg_size[] = {
486 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
487 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
488 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
489 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
490 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
491 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
492 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
493 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
494 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
495 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
496 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
497 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
498 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
499};
500
501/* track caller id inside control connection, call expect_related */
502static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700503conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800504 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
505
506{
507 int dir = CTINFO2DIR(ctinfo);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800508 const struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
509 const struct tcphdr *tcph;
510 struct tcphdr _tcph;
511 const struct pptp_pkt_hdr *pptph;
512 struct pptp_pkt_hdr _pptph;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800513 struct PptpControlHeader _ctlh, *ctlh;
514 union pptp_ctrl_union _pptpReq, *pptpReq;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700515 unsigned int tcplen = skb->len - protoff;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800516 unsigned int datalen, reqlen, nexthdr_off;
517 int oldsstate, oldcstate;
518 int ret;
519 u_int16_t msg;
520
521 /* don't do any tracking before tcp handshake complete */
522 if (ctinfo != IP_CT_ESTABLISHED &&
523 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
524 return NF_ACCEPT;
525
526 nexthdr_off = protoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700527 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800528 BUG_ON(!tcph);
529 nexthdr_off += tcph->doff * 4;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800530 datalen = tcplen - tcph->doff * 4;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800531
Herbert Xu3db05fe2007-10-15 00:53:15 -0700532 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800533 if (!pptph) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700534 pr_debug("no full PPTP header, can't track\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800535 return NF_ACCEPT;
536 }
537 nexthdr_off += sizeof(_pptph);
538 datalen -= sizeof(_pptph);
539
540 /* if it's not a control message we can't do anything with it */
541 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
542 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700543 pr_debug("not a control packet\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800544 return NF_ACCEPT;
545 }
546
Herbert Xu3db05fe2007-10-15 00:53:15 -0700547 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800548 if (!ctlh)
549 return NF_ACCEPT;
550 nexthdr_off += sizeof(_ctlh);
551 datalen -= sizeof(_ctlh);
552
553 reqlen = datalen;
554 msg = ntohs(ctlh->messageType);
555 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
556 return NF_ACCEPT;
557 if (reqlen > sizeof(*pptpReq))
558 reqlen = sizeof(*pptpReq);
559
Herbert Xu3db05fe2007-10-15 00:53:15 -0700560 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800561 if (!pptpReq)
562 return NF_ACCEPT;
563
564 oldsstate = info->sstate;
565 oldcstate = info->cstate;
566
567 spin_lock_bh(&nf_pptp_lock);
568
569 /* FIXME: We just blindly assume that the control connection is always
570 * established from PNS->PAC. However, RFC makes no guarantee */
571 if (dir == IP_CT_DIR_ORIGINAL)
572 /* client -> server (PNS -> PAC) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700573 ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800574 ctinfo);
575 else
576 /* server -> client (PAC -> PNS) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700577 ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800578 ctinfo);
Patrick McHardy0d537782007-07-07 22:39:38 -0700579 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
580 oldsstate, info->sstate, oldcstate, info->cstate);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800581 spin_unlock_bh(&nf_pptp_lock);
582
583 return ret;
584}
585
Patrick McHardy6002f2662008-03-25 20:09:15 -0700586static const struct nf_conntrack_expect_policy pptp_exp_policy = {
587 .max_expected = 2,
588 .timeout = 5 * 60,
589};
590
Patrick McHardyf09943f2006-12-02 22:09:41 -0800591/* control protocol helper */
592static struct nf_conntrack_helper pptp __read_mostly = {
593 .name = "pptp",
594 .me = THIS_MODULE,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800595 .tuple.src.l3num = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -0800596 .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT),
Patrick McHardyf09943f2006-12-02 22:09:41 -0800597 .tuple.dst.protonum = IPPROTO_TCP,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800598 .help = conntrack_pptp_help,
599 .destroy = pptp_destroy_siblings,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700600 .expect_policy = &pptp_exp_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800601};
602
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200603static void nf_conntrack_pptp_net_exit(struct net *net)
604{
605 nf_ct_gre_keymap_flush(net);
606}
607
608static struct pernet_operations nf_conntrack_pptp_net_ops = {
609 .exit = nf_conntrack_pptp_net_exit,
610};
611
Patrick McHardyf09943f2006-12-02 22:09:41 -0800612static int __init nf_conntrack_pptp_init(void)
613{
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200614 int rv;
615
616 rv = nf_conntrack_helper_register(&pptp);
617 if (rv < 0)
618 return rv;
619 rv = register_pernet_subsys(&nf_conntrack_pptp_net_ops);
620 if (rv < 0)
621 nf_conntrack_helper_unregister(&pptp);
622 return rv;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800623}
624
625static void __exit nf_conntrack_pptp_fini(void)
626{
627 nf_conntrack_helper_unregister(&pptp);
Alexey Dobriyan0e6e75a2008-10-08 11:35:10 +0200628 unregister_pernet_subsys(&nf_conntrack_pptp_net_ops);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800629}
630
631module_init(nf_conntrack_pptp_init);
632module_exit(nf_conntrack_pptp_fini);