/net/ipv4/netfilter/nf_nat_pptp.c

http://github.com/mirrors/linux · C · 327 lines · 234 code · 43 blank · 50 comment · 21 complexity · 538056a9fb2431d972289d457c756f7a MD5 · raw file

  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 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. msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
  148. pptp_msg_name[0]);
  149. fallthrough;
  150. case PPTP_SET_LINK_INFO:
  151. /* only need to NAT in case PAC is behind NAT box */
  152. case PPTP_START_SESSION_REQUEST:
  153. case PPTP_START_SESSION_REPLY:
  154. case PPTP_STOP_SESSION_REQUEST:
  155. case PPTP_STOP_SESSION_REPLY:
  156. case PPTP_ECHO_REQUEST:
  157. case PPTP_ECHO_REPLY:
  158. /* no need to alter packet */
  159. return NF_ACCEPT;
  160. }
  161. /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
  162. * down to here */
  163. pr_debug("altering call id from 0x%04x to 0x%04x\n",
  164. ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
  165. /* mangle packet */
  166. if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  167. cid_off + sizeof(struct pptp_pkt_hdr) +
  168. sizeof(struct PptpControlHeader),
  169. sizeof(new_callid), (char *)&new_callid,
  170. sizeof(new_callid)))
  171. return NF_DROP;
  172. return NF_ACCEPT;
  173. }
  174. static void
  175. pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
  176. struct nf_conntrack_expect *expect_reply)
  177. {
  178. const struct nf_conn *ct = expect_orig->master;
  179. struct nf_conn_nat *nat = nfct_nat(ct);
  180. struct nf_ct_pptp_master *ct_pptp_info;
  181. struct nf_nat_pptp *nat_pptp_info;
  182. if (WARN_ON_ONCE(!nat))
  183. return;
  184. nat_pptp_info = &nat->help.nat_pptp_info;
  185. ct_pptp_info = nfct_help_data(ct);
  186. /* save original PAC call ID in nat_info */
  187. nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
  188. /* alter expectation for PNS->PAC direction */
  189. expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
  190. expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
  191. expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
  192. expect_orig->dir = IP_CT_DIR_ORIGINAL;
  193. /* alter expectation for PAC->PNS direction */
  194. expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
  195. expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
  196. expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
  197. expect_reply->dir = IP_CT_DIR_REPLY;
  198. }
  199. /* inbound packets == from PAC to PNS */
  200. static int
  201. pptp_inbound_pkt(struct sk_buff *skb,
  202. struct nf_conn *ct,
  203. enum ip_conntrack_info ctinfo,
  204. unsigned int protoff,
  205. struct PptpControlHeader *ctlh,
  206. union pptp_ctrl_union *pptpReq)
  207. {
  208. const struct nf_nat_pptp *nat_pptp_info;
  209. struct nf_conn_nat *nat = nfct_nat(ct);
  210. u_int16_t msg;
  211. __be16 new_pcid;
  212. unsigned int pcid_off;
  213. if (WARN_ON_ONCE(!nat))
  214. return NF_DROP;
  215. nat_pptp_info = &nat->help.nat_pptp_info;
  216. new_pcid = nat_pptp_info->pns_call_id;
  217. switch (msg = ntohs(ctlh->messageType)) {
  218. case PPTP_OUT_CALL_REPLY:
  219. pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
  220. break;
  221. case PPTP_IN_CALL_CONNECT:
  222. pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
  223. break;
  224. case PPTP_IN_CALL_REQUEST:
  225. /* only need to nat in case PAC is behind NAT box */
  226. return NF_ACCEPT;
  227. case PPTP_WAN_ERROR_NOTIFY:
  228. pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
  229. break;
  230. case PPTP_CALL_DISCONNECT_NOTIFY:
  231. pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
  232. break;
  233. case PPTP_SET_LINK_INFO:
  234. pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
  235. break;
  236. default:
  237. pr_debug("unknown inbound packet %s\n",
  238. msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
  239. pptp_msg_name[0]);
  240. fallthrough;
  241. case PPTP_START_SESSION_REQUEST:
  242. case PPTP_START_SESSION_REPLY:
  243. case PPTP_STOP_SESSION_REQUEST:
  244. case PPTP_STOP_SESSION_REPLY:
  245. case PPTP_ECHO_REQUEST:
  246. case PPTP_ECHO_REPLY:
  247. /* no need to alter packet */
  248. return NF_ACCEPT;
  249. }
  250. /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
  251. * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
  252. /* mangle packet */
  253. pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
  254. ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
  255. if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
  256. pcid_off + sizeof(struct pptp_pkt_hdr) +
  257. sizeof(struct PptpControlHeader),
  258. sizeof(new_pcid), (char *)&new_pcid,
  259. sizeof(new_pcid)))
  260. return NF_DROP;
  261. return NF_ACCEPT;
  262. }
  263. static int __init nf_nat_helper_pptp_init(void)
  264. {
  265. BUG_ON(nf_nat_pptp_hook_outbound != NULL);
  266. RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
  267. BUG_ON(nf_nat_pptp_hook_inbound != NULL);
  268. RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
  269. BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
  270. RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
  271. BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
  272. RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
  273. return 0;
  274. }
  275. static void __exit nf_nat_helper_pptp_fini(void)
  276. {
  277. RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
  278. RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
  279. RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
  280. RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
  281. synchronize_rcu();
  282. }
  283. module_init(nf_nat_helper_pptp_init);
  284. module_exit(nf_nat_helper_pptp_fini);