Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * nf_nat_pptp.c |
| 3 | * |
| 4 | * NAT support for PPTP (Point to Point Tunneling Protocol). |
| 5 | * PPTP is a a protocol for creating virtual private networks. |
| 6 | * It is a specification defined by Microsoft and some vendors |
| 7 | * working with Microsoft. PPTP is built on top of a modified |
| 8 | * version of the Internet Generic Routing Encapsulation Protocol. |
| 9 | * GRE is defined in RFC 1701 and RFC 1702. Documentation of |
| 10 | * PPTP can be found in RFC 2637 |
| 11 | * |
| 12 | * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org> |
| 13 | * |
| 14 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 15 | * |
| 16 | * TODO: - NAT to a unique tuple, not to TCP source port |
| 17 | * (needs netfilter tuple reservation) |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/tcp.h> |
| 22 | |
| 23 | #include <net/netfilter/nf_nat.h> |
| 24 | #include <net/netfilter/nf_nat_helper.h> |
| 25 | #include <net/netfilter/nf_nat_rule.h> |
| 26 | #include <net/netfilter/nf_conntrack_helper.h> |
| 27 | #include <net/netfilter/nf_conntrack_expect.h> |
| 28 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
| 29 | #include <linux/netfilter/nf_conntrack_pptp.h> |
| 30 | |
| 31 | #define NF_NAT_PPTP_VERSION "3.0" |
| 32 | |
| 33 | #define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off))) |
| 34 | |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
| 37 | MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP"); |
| 38 | MODULE_ALIAS("ip_nat_pptp"); |
| 39 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 40 | static void pptp_nat_expected(struct nf_conn *ct, |
| 41 | struct nf_conntrack_expect *exp) |
| 42 | { |
Alexey Dobriyan | cfd6e3d | 2008-10-08 11:35:11 +0200 | [diff] [blame^] | 43 | struct net *net = nf_ct_net(ct); |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 44 | const struct nf_conn *master = ct->master; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 45 | struct nf_conntrack_expect *other_exp; |
| 46 | struct nf_conntrack_tuple t; |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 47 | const struct nf_ct_pptp_master *ct_pptp_info; |
| 48 | const struct nf_nat_pptp *nat_pptp_info; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 49 | struct nf_nat_range range; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 50 | |
| 51 | ct_pptp_info = &nfct_help(master)->help.ct_pptp_info; |
| 52 | nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info; |
| 53 | |
| 54 | /* And here goes the grand finale of corrosion... */ |
| 55 | if (exp->dir == IP_CT_DIR_ORIGINAL) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 56 | pr_debug("we are PNS->PAC\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 57 | /* therefore, build tuple for PAC->PNS */ |
| 58 | t.src.l3num = AF_INET; |
| 59 | t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip; |
| 60 | t.src.u.gre.key = ct_pptp_info->pac_call_id; |
| 61 | t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip; |
| 62 | t.dst.u.gre.key = ct_pptp_info->pns_call_id; |
| 63 | t.dst.protonum = IPPROTO_GRE; |
| 64 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 65 | pr_debug("we are PAC->PNS\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 66 | /* build tuple for PNS->PAC */ |
| 67 | t.src.l3num = AF_INET; |
Patrick McHardy | a46bf7d | 2007-01-26 01:07:30 -0800 | [diff] [blame] | 68 | t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 69 | t.src.u.gre.key = nat_pptp_info->pns_call_id; |
Patrick McHardy | a46bf7d | 2007-01-26 01:07:30 -0800 | [diff] [blame] | 70 | t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 71 | t.dst.u.gre.key = nat_pptp_info->pac_call_id; |
| 72 | t.dst.protonum = IPPROTO_GRE; |
| 73 | } |
| 74 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 75 | pr_debug("trying to unexpect other dir: "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 76 | nf_ct_dump_tuple_ip(&t); |
Alexey Dobriyan | cfd6e3d | 2008-10-08 11:35:11 +0200 | [diff] [blame^] | 77 | other_exp = nf_ct_expect_find_get(net, &t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 78 | if (other_exp) { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 79 | nf_ct_unexpect_related(other_exp); |
| 80 | nf_ct_expect_put(other_exp); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 81 | pr_debug("success\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 82 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 83 | pr_debug("not found!\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* This must be a fresh one. */ |
| 87 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); |
| 88 | |
| 89 | /* Change src to where master sends to */ |
| 90 | range.flags = IP_NAT_RANGE_MAP_IPS; |
| 91 | range.min_ip = range.max_ip |
| 92 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip; |
| 93 | if (exp->dir == IP_CT_DIR_ORIGINAL) { |
| 94 | range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 95 | range.min = range.max = exp->saved_proto; |
| 96 | } |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 97 | nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 98 | |
| 99 | /* For DST manip, map port here to where it's expected. */ |
| 100 | range.flags = IP_NAT_RANGE_MAP_IPS; |
| 101 | range.min_ip = range.max_ip |
| 102 | = ct->master->tuplehash[!exp->dir].tuple.src.u3.ip; |
| 103 | if (exp->dir == IP_CT_DIR_REPLY) { |
| 104 | range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 105 | range.min = range.max = exp->saved_proto; |
| 106 | } |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 107 | nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* outbound packets == from PNS to PAC */ |
| 111 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 112 | pptp_outbound_pkt(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 113 | struct nf_conn *ct, |
| 114 | enum ip_conntrack_info ctinfo, |
| 115 | struct PptpControlHeader *ctlh, |
| 116 | union pptp_ctrl_union *pptpReq) |
| 117 | |
| 118 | { |
| 119 | struct nf_ct_pptp_master *ct_pptp_info; |
| 120 | struct nf_nat_pptp *nat_pptp_info; |
| 121 | u_int16_t msg; |
| 122 | __be16 new_callid; |
| 123 | unsigned int cid_off; |
| 124 | |
| 125 | ct_pptp_info = &nfct_help(ct)->help.ct_pptp_info; |
| 126 | nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info; |
| 127 | |
| 128 | new_callid = ct_pptp_info->pns_call_id; |
| 129 | |
| 130 | switch (msg = ntohs(ctlh->messageType)) { |
| 131 | case PPTP_OUT_CALL_REQUEST: |
| 132 | cid_off = offsetof(union pptp_ctrl_union, ocreq.callID); |
| 133 | /* FIXME: ideally we would want to reserve a call ID |
| 134 | * here. current netfilter NAT core is not able to do |
| 135 | * this :( For now we use TCP source port. This breaks |
| 136 | * multiple calls within one control session */ |
| 137 | |
| 138 | /* save original call ID in nat_info */ |
| 139 | nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id; |
| 140 | |
| 141 | /* don't use tcph->source since we are at a DSTmanip |
| 142 | * hook (e.g. PREROUTING) and pkt is not mangled yet */ |
| 143 | new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port; |
| 144 | |
| 145 | /* save new call ID in ct info */ |
| 146 | ct_pptp_info->pns_call_id = new_callid; |
| 147 | break; |
| 148 | case PPTP_IN_CALL_REPLY: |
| 149 | cid_off = offsetof(union pptp_ctrl_union, icack.callID); |
| 150 | break; |
| 151 | case PPTP_CALL_CLEAR_REQUEST: |
| 152 | cid_off = offsetof(union pptp_ctrl_union, clrreq.callID); |
| 153 | break; |
| 154 | default: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 155 | pr_debug("unknown outbound packet 0x%04x:%s\n", msg, |
| 156 | msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : |
| 157 | pptp_msg_name[0]); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 158 | /* fall through */ |
| 159 | case PPTP_SET_LINK_INFO: |
| 160 | /* only need to NAT in case PAC is behind NAT box */ |
| 161 | case PPTP_START_SESSION_REQUEST: |
| 162 | case PPTP_START_SESSION_REPLY: |
| 163 | case PPTP_STOP_SESSION_REQUEST: |
| 164 | case PPTP_STOP_SESSION_REPLY: |
| 165 | case PPTP_ECHO_REQUEST: |
| 166 | case PPTP_ECHO_REPLY: |
| 167 | /* no need to alter packet */ |
| 168 | return NF_ACCEPT; |
| 169 | } |
| 170 | |
| 171 | /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass |
| 172 | * down to here */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 173 | pr_debug("altering call id from 0x%04x to 0x%04x\n", |
| 174 | ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 175 | |
| 176 | /* mangle packet */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 177 | if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 178 | cid_off + sizeof(struct pptp_pkt_hdr) + |
| 179 | sizeof(struct PptpControlHeader), |
| 180 | sizeof(new_callid), (char *)&new_callid, |
| 181 | sizeof(new_callid)) == 0) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 182 | return NF_DROP; |
| 183 | return NF_ACCEPT; |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | pptp_exp_gre(struct nf_conntrack_expect *expect_orig, |
| 188 | struct nf_conntrack_expect *expect_reply) |
| 189 | { |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 190 | const struct nf_conn *ct = expect_orig->master; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 191 | struct nf_ct_pptp_master *ct_pptp_info; |
| 192 | struct nf_nat_pptp *nat_pptp_info; |
| 193 | |
| 194 | ct_pptp_info = &nfct_help(ct)->help.ct_pptp_info; |
| 195 | nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info; |
| 196 | |
| 197 | /* save original PAC call ID in nat_info */ |
| 198 | nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id; |
| 199 | |
| 200 | /* alter expectation for PNS->PAC direction */ |
| 201 | expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id; |
| 202 | expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id; |
| 203 | expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id; |
| 204 | expect_orig->dir = IP_CT_DIR_ORIGINAL; |
| 205 | |
| 206 | /* alter expectation for PAC->PNS direction */ |
| 207 | expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id; |
| 208 | expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id; |
| 209 | expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id; |
| 210 | expect_reply->dir = IP_CT_DIR_REPLY; |
| 211 | } |
| 212 | |
| 213 | /* inbound packets == from PAC to PNS */ |
| 214 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 215 | pptp_inbound_pkt(struct sk_buff *skb, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 216 | struct nf_conn *ct, |
| 217 | enum ip_conntrack_info ctinfo, |
| 218 | struct PptpControlHeader *ctlh, |
| 219 | union pptp_ctrl_union *pptpReq) |
| 220 | { |
Jan Engelhardt | 9ddd0ed | 2008-01-31 04:51:23 -0800 | [diff] [blame] | 221 | const struct nf_nat_pptp *nat_pptp_info; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 222 | u_int16_t msg; |
| 223 | __be16 new_pcid; |
| 224 | unsigned int pcid_off; |
| 225 | |
| 226 | nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info; |
| 227 | new_pcid = nat_pptp_info->pns_call_id; |
| 228 | |
| 229 | switch (msg = ntohs(ctlh->messageType)) { |
| 230 | case PPTP_OUT_CALL_REPLY: |
| 231 | pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID); |
| 232 | break; |
| 233 | case PPTP_IN_CALL_CONNECT: |
| 234 | pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID); |
| 235 | break; |
| 236 | case PPTP_IN_CALL_REQUEST: |
| 237 | /* only need to nat in case PAC is behind NAT box */ |
| 238 | return NF_ACCEPT; |
| 239 | case PPTP_WAN_ERROR_NOTIFY: |
| 240 | pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID); |
| 241 | break; |
| 242 | case PPTP_CALL_DISCONNECT_NOTIFY: |
| 243 | pcid_off = offsetof(union pptp_ctrl_union, disc.callID); |
| 244 | break; |
| 245 | case PPTP_SET_LINK_INFO: |
| 246 | pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID); |
| 247 | break; |
| 248 | default: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 249 | pr_debug("unknown inbound packet %s\n", |
| 250 | msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : |
| 251 | pptp_msg_name[0]); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 252 | /* fall through */ |
| 253 | case PPTP_START_SESSION_REQUEST: |
| 254 | case PPTP_START_SESSION_REPLY: |
| 255 | case PPTP_STOP_SESSION_REQUEST: |
| 256 | case PPTP_STOP_SESSION_REPLY: |
| 257 | case PPTP_ECHO_REQUEST: |
| 258 | case PPTP_ECHO_REPLY: |
| 259 | /* no need to alter packet */ |
| 260 | return NF_ACCEPT; |
| 261 | } |
| 262 | |
| 263 | /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST, |
| 264 | * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */ |
| 265 | |
| 266 | /* mangle packet */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 267 | pr_debug("altering peer call id from 0x%04x to 0x%04x\n", |
| 268 | ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid)); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 269 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 270 | if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 271 | pcid_off + sizeof(struct pptp_pkt_hdr) + |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 272 | sizeof(struct PptpControlHeader), |
| 273 | sizeof(new_pcid), (char *)&new_pcid, |
| 274 | sizeof(new_pcid)) == 0) |
| 275 | return NF_DROP; |
| 276 | return NF_ACCEPT; |
| 277 | } |
| 278 | |
| 279 | static int __init nf_nat_helper_pptp_init(void) |
| 280 | { |
| 281 | nf_nat_need_gre(); |
| 282 | |
Patrick McHardy | d1332e0 | 2007-11-05 20:43:30 -0800 | [diff] [blame] | 283 | BUG_ON(nf_nat_pptp_hook_outbound != NULL); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 284 | rcu_assign_pointer(nf_nat_pptp_hook_outbound, pptp_outbound_pkt); |
| 285 | |
Patrick McHardy | d1332e0 | 2007-11-05 20:43:30 -0800 | [diff] [blame] | 286 | BUG_ON(nf_nat_pptp_hook_inbound != NULL); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 287 | rcu_assign_pointer(nf_nat_pptp_hook_inbound, pptp_inbound_pkt); |
| 288 | |
Patrick McHardy | d1332e0 | 2007-11-05 20:43:30 -0800 | [diff] [blame] | 289 | BUG_ON(nf_nat_pptp_hook_exp_gre != NULL); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 290 | rcu_assign_pointer(nf_nat_pptp_hook_exp_gre, pptp_exp_gre); |
| 291 | |
Patrick McHardy | d1332e0 | 2007-11-05 20:43:30 -0800 | [diff] [blame] | 292 | BUG_ON(nf_nat_pptp_hook_expectfn != NULL); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 293 | rcu_assign_pointer(nf_nat_pptp_hook_expectfn, pptp_nat_expected); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void __exit nf_nat_helper_pptp_fini(void) |
| 298 | { |
| 299 | rcu_assign_pointer(nf_nat_pptp_hook_expectfn, NULL); |
| 300 | rcu_assign_pointer(nf_nat_pptp_hook_exp_gre, NULL); |
| 301 | rcu_assign_pointer(nf_nat_pptp_hook_inbound, NULL); |
| 302 | rcu_assign_pointer(nf_nat_pptp_hook_outbound, NULL); |
| 303 | synchronize_rcu(); |
| 304 | } |
| 305 | |
| 306 | module_init(nf_nat_helper_pptp_init); |
| 307 | module_exit(nf_nat_helper_pptp_fini); |