Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Connection tracking support for PPTP (Point to Point Tunneling Protocol). |
| 3 | * PPTP is a a protocol for creating virtual private networks. |
| 4 | * It is a specification defined by Microsoft and some vendors |
| 5 | * working with Microsoft. PPTP is built on top of a modified |
| 6 | * version of the Internet Generic Routing Encapsulation Protocol. |
| 7 | * GRE is defined in RFC 1701 and RFC 1702. Documentation of |
| 8 | * PPTP can be found in RFC 2637 |
| 9 | * |
| 10 | * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org> |
| 11 | * |
| 12 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 13 | * |
| 14 | * Limitations: |
| 15 | * - We blindly assume that control connections are always |
| 16 | * established in PNS->PAC direction. This is a violation |
| 17 | * of RFFC2673 |
| 18 | * - We can only support one single call within each session |
| 19 | * TODO: |
| 20 | * - testing of incoming PPTP calls |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/skbuff.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/tcp.h> |
| 27 | |
| 28 | #include <net/netfilter/nf_conntrack.h> |
| 29 | #include <net/netfilter/nf_conntrack_core.h> |
| 30 | #include <net/netfilter/nf_conntrack_helper.h> |
| 31 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
| 32 | #include <linux/netfilter/nf_conntrack_pptp.h> |
| 33 | |
| 34 | #define NF_CT_PPTP_VERSION "3.1" |
| 35 | |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
| 38 | MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP"); |
| 39 | MODULE_ALIAS("ip_conntrack_pptp"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 40 | MODULE_ALIAS_NFCT_HELPER("pptp"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 41 | |
| 42 | static DEFINE_SPINLOCK(nf_pptp_lock); |
| 43 | |
| 44 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 45 | (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 46 | struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
| 47 | struct PptpControlHeader *ctlh, |
| 48 | union pptp_ctrl_union *pptpReq) __read_mostly; |
| 49 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound); |
| 50 | |
| 51 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 52 | (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 53 | struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
| 54 | struct PptpControlHeader *ctlh, |
| 55 | union pptp_ctrl_union *pptpReq) __read_mostly; |
| 56 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound); |
| 57 | |
| 58 | void |
| 59 | (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig, |
| 60 | struct nf_conntrack_expect *expect_reply) |
| 61 | __read_mostly; |
| 62 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre); |
| 63 | |
| 64 | void |
| 65 | (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct, |
| 66 | struct nf_conntrack_expect *exp) __read_mostly; |
| 67 | EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn); |
| 68 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 69 | #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 70 | /* PptpControlMessageType names */ |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 71 | const char *const pptp_msg_name[] = { |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 72 | "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 | }; |
| 89 | EXPORT_SYMBOL(pptp_msg_name); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 90 | #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 | |
| 99 | static void pptp_expectfn(struct nf_conn *ct, |
| 100 | struct nf_conntrack_expect *exp) |
| 101 | { |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 102 | struct net *net = nf_ct_net(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 103 | typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 104 | pr_debug("increasing timeouts\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 105 | |
| 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 McHardy | 7399072 | 2007-01-26 01:07:59 -0800 | [diff] [blame] | 115 | if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 116 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 123 | pr_debug("trying to unexpect other dir: "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 124 | nf_ct_dump_tuple(&inv_t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 125 | |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 126 | exp_other = nf_ct_expect_find_get(net, &inv_t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 127 | if (exp_other) { |
| 128 | /* delete other expectation. */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 129 | pr_debug("found\n"); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 130 | nf_ct_unexpect_related(exp_other); |
| 131 | nf_ct_expect_put(exp_other); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 132 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 133 | pr_debug("not found\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | rcu_read_unlock(); |
| 137 | } |
| 138 | |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 139 | static int destroy_sibling_or_exp(struct net *net, |
| 140 | const struct nf_conntrack_tuple *t) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 141 | { |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 142 | const struct nf_conntrack_tuple_hash *h; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 143 | struct nf_conntrack_expect *exp; |
| 144 | struct nf_conn *sibling; |
| 145 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 146 | pr_debug("trying to timeout ct or exp for tuple "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 147 | nf_ct_dump_tuple(t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 148 | |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 149 | h = nf_conntrack_find_get(net, t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 150 | if (h) { |
| 151 | sibling = nf_ct_tuplehash_to_ctrack(h); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 152 | pr_debug("setting timeout of conntrack %p to 0\n", sibling); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 153 | 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 Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 160 | exp = nf_ct_expect_find_get(net, t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 161 | if (exp) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 162 | pr_debug("unexpect_related of expect %p\n", exp); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 163 | nf_ct_unexpect_related(exp); |
| 164 | nf_ct_expect_put(exp); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* timeout GRE data connections */ |
| 172 | static void pptp_destroy_siblings(struct nf_conn *ct) |
| 173 | { |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 174 | struct net *net = nf_ct_net(ct); |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 175 | const struct nf_conn_help *help = nfct_help(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 176 | 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 Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 185 | if (!destroy_sibling_or_exp(net, &t)) |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 186 | pr_debug("failed to timeout original pns->pac ct/exp\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 187 | |
| 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 Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 193 | if (!destroy_sibling_or_exp(net, &t)) |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 194 | pr_debug("failed to timeout reply pac->pns ct/exp\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* expect GRE connections (PNS->PAC and PAC->PNS direction) */ |
| 198 | static 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 McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 205 | exp_orig = nf_ct_expect_alloc(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 206 | if (exp_orig == NULL) |
| 207 | goto out; |
| 208 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 209 | exp_reply = nf_ct_expect_alloc(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 210 | if (exp_reply == NULL) |
| 211 | goto out_put_orig; |
| 212 | |
| 213 | /* original direction, PNS->PAC */ |
| 214 | dir = IP_CT_DIR_ORIGINAL; |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 215 | nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT, |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 216 | nf_ct_l3num(ct), |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 217 | &ct->tuplehash[dir].tuple.src.u3, |
| 218 | &ct->tuplehash[dir].tuple.dst.u3, |
| 219 | IPPROTO_GRE, &peer_callid, &callid); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 220 | exp_orig->expectfn = pptp_expectfn; |
| 221 | |
| 222 | /* reply direction, PAC->PNS */ |
| 223 | dir = IP_CT_DIR_REPLY; |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 224 | nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT, |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 225 | nf_ct_l3num(ct), |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 226 | &ct->tuplehash[dir].tuple.src.u3, |
| 227 | &ct->tuplehash[dir].tuple.dst.u3, |
| 228 | IPPROTO_GRE, &callid, &peer_callid); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 229 | 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 McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 234 | if (nf_ct_expect_related(exp_orig) != 0) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 235 | goto out_put_both; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 236 | if (nf_ct_expect_related(exp_reply) != 0) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 237 | 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 | |
| 248 | out_put_both: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 249 | nf_ct_expect_put(exp_reply); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 250 | out_put_orig: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 251 | nf_ct_expect_put(exp_orig); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 252 | out: |
| 253 | return ret; |
| 254 | |
| 255 | out_unexpect_both: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 256 | nf_ct_unexpect_related(exp_reply); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 257 | out_unexpect_orig: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 258 | nf_ct_unexpect_related(exp_orig); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 259 | goto out_put_both; |
| 260 | } |
| 261 | |
| 262 | static inline int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 263 | pptp_inbound_pkt(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 264 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 276 | pr_debug("inbound control message %s\n", pptp_msg_name[msg]); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 277 | |
| 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 311 | pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg], |
| 312 | ntohs(cid), ntohs(pcid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 313 | |
| 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 328 | pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 329 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 347 | pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 348 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 357 | pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 358 | 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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 376 | return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 377 | return NF_ACCEPT; |
| 378 | |
| 379 | invalid: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 380 | 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 McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 385 | return NF_ACCEPT; |
| 386 | } |
| 387 | |
| 388 | static inline int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 389 | pptp_outbound_pkt(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 390 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 402 | pr_debug("outbound control message %s\n", pptp_msg_name[msg]); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 403 | |
| 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 424 | pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 425 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 438 | pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg], |
| 439 | ntohs(cid), ntohs(pcid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 440 | |
| 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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 471 | return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 472 | return NF_ACCEPT; |
| 473 | |
| 474 | invalid: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 475 | 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 McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 480 | return NF_ACCEPT; |
| 481 | } |
| 482 | |
| 483 | static 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 */ |
| 500 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 501 | conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 502 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
| 503 | |
| 504 | { |
| 505 | int dir = CTINFO2DIR(ctinfo); |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 506 | 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 McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 511 | struct PptpControlHeader _ctlh, *ctlh; |
| 512 | union pptp_ctrl_union _pptpReq, *pptpReq; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 513 | unsigned int tcplen = skb->len - protoff; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 514 | 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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 525 | tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 526 | BUG_ON(!tcph); |
| 527 | nexthdr_off += tcph->doff * 4; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 528 | datalen = tcplen - tcph->doff * 4; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 529 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 530 | pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 531 | if (!pptph) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 532 | pr_debug("no full PPTP header, can't track\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 533 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 541 | pr_debug("not a control packet\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 542 | return NF_ACCEPT; |
| 543 | } |
| 544 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 545 | ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 546 | 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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 558 | pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 559 | 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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 571 | ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 572 | ctinfo); |
| 573 | else |
| 574 | /* server -> client (PAC -> PNS) */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 575 | ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 576 | ctinfo); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 577 | pr_debug("sstate: %d->%d, cstate: %d->%d\n", |
| 578 | oldsstate, info->sstate, oldcstate, info->cstate); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 579 | spin_unlock_bh(&nf_pptp_lock); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 584 | static const struct nf_conntrack_expect_policy pptp_exp_policy = { |
| 585 | .max_expected = 2, |
| 586 | .timeout = 5 * 60, |
| 587 | }; |
| 588 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 589 | /* control protocol helper */ |
| 590 | static struct nf_conntrack_helper pptp __read_mostly = { |
| 591 | .name = "pptp", |
| 592 | .me = THIS_MODULE, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 593 | .tuple.src.l3num = AF_INET, |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 594 | .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT), |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 595 | .tuple.dst.protonum = IPPROTO_TCP, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 596 | .help = conntrack_pptp_help, |
| 597 | .destroy = pptp_destroy_siblings, |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 598 | .expect_policy = &pptp_exp_policy, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 599 | }; |
| 600 | |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 601 | static void nf_conntrack_pptp_net_exit(struct net *net) |
| 602 | { |
| 603 | nf_ct_gre_keymap_flush(net); |
| 604 | } |
| 605 | |
| 606 | static struct pernet_operations nf_conntrack_pptp_net_ops = { |
| 607 | .exit = nf_conntrack_pptp_net_exit, |
| 608 | }; |
| 609 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 610 | static int __init nf_conntrack_pptp_init(void) |
| 611 | { |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 612 | 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 McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | static void __exit nf_conntrack_pptp_fini(void) |
| 624 | { |
| 625 | nf_conntrack_helper_unregister(&pptp); |
Alexey Dobriyan | 0e6e75a | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 626 | unregister_pernet_subsys(&nf_conntrack_pptp_net_ops); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | module_init(nf_conntrack_pptp_init); |
| 630 | module_exit(nf_conntrack_pptp_fini); |