nf_nat_pptp.c 10.0 KB

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