nf_nat_pptp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  17. *
  18. * TODO: - NAT to a unique tuple, not to TCP source port
  19. * (needs netfilter tuple reservation)
  20. */
  21. #include <linux/module.h>
  22. #include <linux/tcp.h>
  23. #include <net/netfilter/nf_nat.h>
  24. #include <net/netfilter/nf_nat_helper.h>
  25. #include <net/netfilter/nf_conntrack_helper.h>
  26. #include <net/netfilter/nf_conntrack_expect.h>
  27. #include <net/netfilter/nf_conntrack_zones.h>
  28. #include <linux/netfilter/nf_conntrack_proto_gre.h>
  29. #include <linux/netfilter/nf_conntrack_pptp.h>
  30. #define NF_NAT_PPTP_VERSION "3.0"
  31. #define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off)))
  32. MODULE_LICENSE("GPL");
  33. MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
  34. MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
  35. MODULE_ALIAS("ip_nat_pptp");
  36. static void pptp_nat_expected(struct nf_conn *ct,
  37. struct nf_conntrack_expect *exp)
  38. {
  39. struct net *net = nf_ct_net(ct);
  40. const struct nf_conn *master = ct->master;
  41. struct nf_conntrack_expect *other_exp;
  42. struct nf_conntrack_tuple t = {};
  43. const struct nf_ct_pptp_master *ct_pptp_info;
  44. const struct nf_nat_pptp *nat_pptp_info;
  45. struct nf_nat_range range;
  46. ct_pptp_info = nfct_help_data(master);
  47. nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info;
  48. /* And here goes the grand finale of corrosion... */
  49. if (exp->dir == IP_CT_DIR_ORIGINAL) {
  50. pr_debug("we are PNS->PAC\n");
  51. /* therefore, build tuple for PAC->PNS */
  52. t.src.l3num = AF_INET;
  53. t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
  54. t.src.u.gre.key = ct_pptp_info->pac_call_id;
  55. t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
  56. t.dst.u.gre.key = ct_pptp_info->pns_call_id;
  57. t.dst.protonum = IPPROTO_GRE;
  58. } else {
  59. pr_debug("we are PAC->PNS\n");
  60. /* build tuple for PNS->PAC */
  61. t.src.l3num = AF_INET;
  62. t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
  63. t.src.u.gre.key = nat_pptp_info->pns_call_id;
  64. t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
  65. t.dst.u.gre.key = nat_pptp_info->pac_call_id;
  66. t.dst.protonum = IPPROTO_GRE;
  67. }
  68. pr_debug("trying to unexpect other dir: ");
  69. nf_ct_dump_tuple_ip(&t);
  70. other_exp = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
  71. if (other_exp) {
  72. nf_ct_unexpect_related(other_exp);
  73. nf_ct_expect_put(other_exp);
  74. pr_debug("success\n");
  75. } else {
  76. pr_debug("not found!\n");
  77. }
  78. /* This must be a fresh one. */
  79. BUG_ON(ct->status & IPS_NAT_DONE_MASK);
  80. /* Change src to where master sends to */
  81. range.flags = NF_NAT_RANGE_MAP_IPS;
  82. range.min_addr = range.max_addr
  83. = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
  84. if (exp->dir == IP_CT_DIR_ORIGINAL) {
  85. range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  86. range.min_proto = range.max_proto = exp->saved_proto;
  87. }
  88. nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
  89. /* For DST manip, map port here to where it's expected. */
  90. range.flags = NF_NAT_RANGE_MAP_IPS;
  91. range.min_addr = range.max_addr
  92. = ct->master->tuplehash[!exp->dir].tuple.src.u3;
  93. if (exp->dir == IP_CT_DIR_REPLY) {
  94. range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  95. range.min_proto = range.max_proto = exp->saved_proto;
  96. }
  97. nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
  98. }
  99. /* outbound packets == from PNS to PAC */
  100. static int
  101. pptp_outbound_pkt(struct sk_buff *skb,
  102. struct nf_conn *ct,
  103. enum ip_conntrack_info ctinfo,
  104. unsigned int protoff,
  105. struct PptpControlHeader *ctlh,
  106. union pptp_ctrl_union *pptpReq)
  107. {
  108. struct nf_ct_pptp_master *ct_pptp_info;
  109. struct nf_nat_pptp *nat_pptp_info;
  110. u_int16_t msg;
  111. __be16 new_callid;
  112. unsigned int cid_off;
  113. ct_pptp_info = nfct_help_data(ct);
  114. nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
  115. new_callid = ct_pptp_info->pns_call_id;
  116. switch (msg = ntohs(ctlh->messageType)) {
  117. case PPTP_OUT_CALL_REQUEST:
  118. cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
  119. /* FIXME: ideally we would want to reserve a call ID
  120. * here. current netfilter NAT core is not able to do
  121. * this :( For now we use TCP source port. This breaks
  122. * multiple calls within one control session */
  123. /* save original call ID in nat_info */
  124. nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
  125. /* don't use tcph->source since we are at a DSTmanip
  126. * hook (e.g. PREROUTING) and pkt is not mangled yet */
  127. new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
  128. /* save new call ID in ct info */
  129. ct_pptp_info->pns_call_id = new_callid;
  130. break;
  131. case PPTP_IN_CALL_REPLY:
  132. cid_off = offsetof(union pptp_ctrl_union, icack.callID);
  133. break;
  134. case PPTP_CALL_CLEAR_REQUEST:
  135. cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
  136. break;
  137. default:
  138. pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
  139. msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
  140. pptp_msg_name[0]);
  141. /* fall through */
  142. case PPTP_SET_LINK_INFO:
  143. /* only need to NAT in case PAC is behind NAT box */
  144. case PPTP_START_SESSION_REQUEST:
  145. case PPTP_START_SESSION_REPLY:
  146. case PPTP_STOP_SESSION_REQUEST:
  147. case PPTP_STOP_SESSION_REPLY:
  148. case PPTP_ECHO_REQUEST:
  149. case PPTP_ECHO_REPLY:
  150. /* no need to alter packet */
  151. return NF_ACCEPT;
  152. }
  153. /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
  154. * down to here */
  155. pr_debug("altering call id from 0x%04x to 0x%04x\n",
  156. ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
  157. /* mangle packet */
  158. if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  159. cid_off + sizeof(struct pptp_pkt_hdr) +
  160. sizeof(struct PptpControlHeader),
  161. sizeof(new_callid), (char *)&new_callid,
  162. sizeof(new_callid)) == 0)
  163. return NF_DROP;
  164. return NF_ACCEPT;
  165. }
  166. static void
  167. pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
  168. struct nf_conntrack_expect *expect_reply)
  169. {
  170. const struct nf_conn *ct = expect_orig->master;
  171. struct nf_ct_pptp_master *ct_pptp_info;
  172. struct nf_nat_pptp *nat_pptp_info;
  173. ct_pptp_info = nfct_help_data(ct);
  174. nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
  175. /* save original PAC call ID in nat_info */
  176. nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
  177. /* alter expectation for PNS->PAC direction */
  178. expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
  179. expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
  180. expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
  181. expect_orig->dir = IP_CT_DIR_ORIGINAL;
  182. /* alter expectation for PAC->PNS direction */
  183. expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
  184. expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
  185. expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
  186. expect_reply->dir = IP_CT_DIR_REPLY;
  187. }
  188. /* inbound packets == from PAC to PNS */
  189. static int
  190. pptp_inbound_pkt(struct sk_buff *skb,
  191. struct nf_conn *ct,
  192. enum ip_conntrack_info ctinfo,
  193. unsigned int protoff,
  194. struct PptpControlHeader *ctlh,
  195. union pptp_ctrl_union *pptpReq)
  196. {
  197. const struct nf_nat_pptp *nat_pptp_info;
  198. u_int16_t msg;
  199. __be16 new_pcid;
  200. unsigned int pcid_off;
  201. nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
  202. new_pcid = nat_pptp_info->pns_call_id;
  203. switch (msg = ntohs(ctlh->messageType)) {
  204. case PPTP_OUT_CALL_REPLY:
  205. pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
  206. break;
  207. case PPTP_IN_CALL_CONNECT:
  208. pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
  209. break;
  210. case PPTP_IN_CALL_REQUEST:
  211. /* only need to nat in case PAC is behind NAT box */
  212. return NF_ACCEPT;
  213. case PPTP_WAN_ERROR_NOTIFY:
  214. pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
  215. break;
  216. case PPTP_CALL_DISCONNECT_NOTIFY:
  217. pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
  218. break;
  219. case PPTP_SET_LINK_INFO:
  220. pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
  221. break;
  222. default:
  223. pr_debug("unknown inbound packet %s\n",
  224. msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
  225. pptp_msg_name[0]);
  226. /* fall through */
  227. case PPTP_START_SESSION_REQUEST:
  228. case PPTP_START_SESSION_REPLY:
  229. case PPTP_STOP_SESSION_REQUEST:
  230. case PPTP_STOP_SESSION_REPLY:
  231. case PPTP_ECHO_REQUEST:
  232. case PPTP_ECHO_REPLY:
  233. /* no need to alter packet */
  234. return NF_ACCEPT;
  235. }
  236. /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
  237. * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
  238. /* mangle packet */
  239. pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
  240. ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
  241. if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  242. pcid_off + sizeof(struct pptp_pkt_hdr) +
  243. sizeof(struct PptpControlHeader),
  244. sizeof(new_pcid), (char *)&new_pcid,
  245. sizeof(new_pcid)) == 0)
  246. return NF_DROP;
  247. return NF_ACCEPT;
  248. }
  249. static int __init nf_nat_helper_pptp_init(void)
  250. {
  251. nf_nat_need_gre();
  252. BUG_ON(nf_nat_pptp_hook_outbound != NULL);
  253. RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
  254. BUG_ON(nf_nat_pptp_hook_inbound != NULL);
  255. RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
  256. BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
  257. RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
  258. BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
  259. RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
  260. return 0;
  261. }
  262. static void __exit nf_nat_helper_pptp_fini(void)
  263. {
  264. RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
  265. RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
  266. RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
  267. RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
  268. synchronize_rcu();
  269. }
  270. module_init(nf_nat_helper_pptp_init);
  271. module_exit(nf_nat_helper_pptp_fini);