nf_conntrack_pptp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
  4. * PPTP is a protocol for creating virtual private networks.
  5. * It is a specification defined by Microsoft and some vendors
  6. * working with Microsoft. PPTP is built on top of a modified
  7. * version of the Internet Generic Routing Encapsulation Protocol.
  8. * GRE is defined in RFC 1701 and RFC 1702. Documentation of
  9. * PPTP can be found in RFC 2637
  10. *
  11. * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
  12. *
  13. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  14. *
  15. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  16. *
  17. * Limitations:
  18. * - We blindly assume that control connections are always
  19. * established in PNS->PAC direction. This is a violation
  20. * of RFC 2637
  21. * - We can only support one single call within each session
  22. * TODO:
  23. * - testing of incoming PPTP calls
  24. */
  25. #include <linux/module.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/in.h>
  28. #include <linux/tcp.h>
  29. #include <net/netfilter/nf_conntrack.h>
  30. #include <net/netfilter/nf_conntrack_core.h>
  31. #include <net/netfilter/nf_conntrack_helper.h>
  32. #include <net/netfilter/nf_conntrack_zones.h>
  33. #include <linux/netfilter/nf_conntrack_proto_gre.h>
  34. #include <linux/netfilter/nf_conntrack_pptp.h>
  35. #define NF_CT_PPTP_VERSION "3.1"
  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");
  40. MODULE_ALIAS_NFCT_HELPER("pptp");
  41. static DEFINE_SPINLOCK(nf_pptp_lock);
  42. int
  43. (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
  44. struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  45. unsigned int protoff, struct PptpControlHeader *ctlh,
  46. union pptp_ctrl_union *pptpReq) __read_mostly;
  47. EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
  48. int
  49. (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
  50. struct nf_conn *ct, enum ip_conntrack_info ctinfo,
  51. unsigned int protoff, struct PptpControlHeader *ctlh,
  52. union pptp_ctrl_union *pptpReq) __read_mostly;
  53. EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
  54. void
  55. (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
  56. struct nf_conntrack_expect *expect_reply)
  57. __read_mostly;
  58. EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
  59. void
  60. (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
  61. struct nf_conntrack_expect *exp) __read_mostly;
  62. EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
  63. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  64. /* PptpControlMessageType names */
  65. static const char *const pptp_msg_name_array[PPTP_MSG_MAX + 1] = {
  66. [0] = "UNKNOWN_MESSAGE",
  67. [PPTP_START_SESSION_REQUEST] = "START_SESSION_REQUEST",
  68. [PPTP_START_SESSION_REPLY] = "START_SESSION_REPLY",
  69. [PPTP_STOP_SESSION_REQUEST] = "STOP_SESSION_REQUEST",
  70. [PPTP_STOP_SESSION_REPLY] = "STOP_SESSION_REPLY",
  71. [PPTP_ECHO_REQUEST] = "ECHO_REQUEST",
  72. [PPTP_ECHO_REPLY] = "ECHO_REPLY",
  73. [PPTP_OUT_CALL_REQUEST] = "OUT_CALL_REQUEST",
  74. [PPTP_OUT_CALL_REPLY] = "OUT_CALL_REPLY",
  75. [PPTP_IN_CALL_REQUEST] = "IN_CALL_REQUEST",
  76. [PPTP_IN_CALL_REPLY] = "IN_CALL_REPLY",
  77. [PPTP_IN_CALL_CONNECT] = "IN_CALL_CONNECT",
  78. [PPTP_CALL_CLEAR_REQUEST] = "CALL_CLEAR_REQUEST",
  79. [PPTP_CALL_DISCONNECT_NOTIFY] = "CALL_DISCONNECT_NOTIFY",
  80. [PPTP_WAN_ERROR_NOTIFY] = "WAN_ERROR_NOTIFY",
  81. [PPTP_SET_LINK_INFO] = "SET_LINK_INFO"
  82. };
  83. const char *pptp_msg_name(u_int16_t msg)
  84. {
  85. if (msg > PPTP_MSG_MAX)
  86. return pptp_msg_name_array[0];
  87. return pptp_msg_name_array[msg];
  88. }
  89. EXPORT_SYMBOL(pptp_msg_name);
  90. #endif
  91. #define SECS *HZ
  92. #define MINS * 60 SECS
  93. #define HOURS * 60 MINS
  94. #define PPTP_GRE_TIMEOUT (10 MINS)
  95. #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
  96. static void pptp_expectfn(struct nf_conn *ct,
  97. struct nf_conntrack_expect *exp)
  98. {
  99. struct net *net = nf_ct_net(ct);
  100. typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
  101. pr_debug("increasing timeouts\n");
  102. /* increase timeout of GRE data channel conntrack entry */
  103. ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
  104. ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
  105. /* Can you see how rusty this code is, compared with the pre-2.6.11
  106. * one? That's what happened to my shiny newnat of 2002 ;( -HW */
  107. nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
  108. if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
  109. nf_nat_pptp_expectfn(ct, exp);
  110. else {
  111. struct nf_conntrack_tuple inv_t;
  112. struct nf_conntrack_expect *exp_other;
  113. /* obviously this tuple inversion only works until you do NAT */
  114. nf_ct_invert_tuple(&inv_t, &exp->tuple);
  115. pr_debug("trying to unexpect other dir: ");
  116. nf_ct_dump_tuple(&inv_t);
  117. exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
  118. if (exp_other) {
  119. /* delete other expectation. */
  120. pr_debug("found\n");
  121. nf_ct_unexpect_related(exp_other);
  122. nf_ct_expect_put(exp_other);
  123. } else {
  124. pr_debug("not found\n");
  125. }
  126. }
  127. }
  128. static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
  129. const struct nf_conntrack_tuple *t)
  130. {
  131. const struct nf_conntrack_tuple_hash *h;
  132. const struct nf_conntrack_zone *zone;
  133. struct nf_conntrack_expect *exp;
  134. struct nf_conn *sibling;
  135. pr_debug("trying to timeout ct or exp for tuple ");
  136. nf_ct_dump_tuple(t);
  137. zone = nf_ct_zone(ct);
  138. h = nf_conntrack_find_get(net, zone, t);
  139. if (h) {
  140. sibling = nf_ct_tuplehash_to_ctrack(h);
  141. pr_debug("setting timeout of conntrack %p to 0\n", sibling);
  142. sibling->proto.gre.timeout = 0;
  143. sibling->proto.gre.stream_timeout = 0;
  144. nf_ct_kill(sibling);
  145. nf_ct_put(sibling);
  146. return 1;
  147. } else {
  148. exp = nf_ct_expect_find_get(net, zone, t);
  149. if (exp) {
  150. pr_debug("unexpect_related of expect %p\n", exp);
  151. nf_ct_unexpect_related(exp);
  152. nf_ct_expect_put(exp);
  153. return 1;
  154. }
  155. }
  156. return 0;
  157. }
  158. /* timeout GRE data connections */
  159. static void pptp_destroy_siblings(struct nf_conn *ct)
  160. {
  161. struct net *net = nf_ct_net(ct);
  162. const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
  163. struct nf_conntrack_tuple t;
  164. nf_ct_gre_keymap_destroy(ct);
  165. /* try original (pns->pac) tuple */
  166. memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
  167. t.dst.protonum = IPPROTO_GRE;
  168. t.src.u.gre.key = ct_pptp_info->pns_call_id;
  169. t.dst.u.gre.key = ct_pptp_info->pac_call_id;
  170. if (!destroy_sibling_or_exp(net, ct, &t))
  171. pr_debug("failed to timeout original pns->pac ct/exp\n");
  172. /* try reply (pac->pns) tuple */
  173. memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
  174. t.dst.protonum = IPPROTO_GRE;
  175. t.src.u.gre.key = ct_pptp_info->pac_call_id;
  176. t.dst.u.gre.key = ct_pptp_info->pns_call_id;
  177. if (!destroy_sibling_or_exp(net, ct, &t))
  178. pr_debug("failed to timeout reply pac->pns ct/exp\n");
  179. }
  180. /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
  181. static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
  182. {
  183. struct nf_conntrack_expect *exp_orig, *exp_reply;
  184. enum ip_conntrack_dir dir;
  185. int ret = 1;
  186. typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
  187. exp_orig = nf_ct_expect_alloc(ct);
  188. if (exp_orig == NULL)
  189. goto out;
  190. exp_reply = nf_ct_expect_alloc(ct);
  191. if (exp_reply == NULL)
  192. goto out_put_orig;
  193. /* original direction, PNS->PAC */
  194. dir = IP_CT_DIR_ORIGINAL;
  195. nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
  196. nf_ct_l3num(ct),
  197. &ct->tuplehash[dir].tuple.src.u3,
  198. &ct->tuplehash[dir].tuple.dst.u3,
  199. IPPROTO_GRE, &peer_callid, &callid);
  200. exp_orig->expectfn = pptp_expectfn;
  201. /* reply direction, PAC->PNS */
  202. dir = IP_CT_DIR_REPLY;
  203. nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
  204. nf_ct_l3num(ct),
  205. &ct->tuplehash[dir].tuple.src.u3,
  206. &ct->tuplehash[dir].tuple.dst.u3,
  207. IPPROTO_GRE, &callid, &peer_callid);
  208. exp_reply->expectfn = pptp_expectfn;
  209. nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
  210. if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
  211. nf_nat_pptp_exp_gre(exp_orig, exp_reply);
  212. if (nf_ct_expect_related(exp_orig, 0) != 0)
  213. goto out_put_both;
  214. if (nf_ct_expect_related(exp_reply, 0) != 0)
  215. goto out_unexpect_orig;
  216. /* Add GRE keymap entries */
  217. if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
  218. goto out_unexpect_both;
  219. if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
  220. nf_ct_gre_keymap_destroy(ct);
  221. goto out_unexpect_both;
  222. }
  223. ret = 0;
  224. out_put_both:
  225. nf_ct_expect_put(exp_reply);
  226. out_put_orig:
  227. nf_ct_expect_put(exp_orig);
  228. out:
  229. return ret;
  230. out_unexpect_both:
  231. nf_ct_unexpect_related(exp_reply);
  232. out_unexpect_orig:
  233. nf_ct_unexpect_related(exp_orig);
  234. goto out_put_both;
  235. }
  236. static int
  237. pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
  238. struct PptpControlHeader *ctlh,
  239. union pptp_ctrl_union *pptpReq,
  240. unsigned int reqlen,
  241. struct nf_conn *ct,
  242. enum ip_conntrack_info ctinfo)
  243. {
  244. struct nf_ct_pptp_master *info = nfct_help_data(ct);
  245. u_int16_t msg;
  246. __be16 cid = 0, pcid = 0;
  247. typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
  248. msg = ntohs(ctlh->messageType);
  249. pr_debug("inbound control message %s\n", pptp_msg_name(msg));
  250. switch (msg) {
  251. case PPTP_START_SESSION_REPLY:
  252. /* server confirms new control session */
  253. if (info->sstate < PPTP_SESSION_REQUESTED)
  254. goto invalid;
  255. if (pptpReq->srep.resultCode == PPTP_START_OK)
  256. info->sstate = PPTP_SESSION_CONFIRMED;
  257. else
  258. info->sstate = PPTP_SESSION_ERROR;
  259. break;
  260. case PPTP_STOP_SESSION_REPLY:
  261. /* server confirms end of control session */
  262. if (info->sstate > PPTP_SESSION_STOPREQ)
  263. goto invalid;
  264. if (pptpReq->strep.resultCode == PPTP_STOP_OK)
  265. info->sstate = PPTP_SESSION_NONE;
  266. else
  267. info->sstate = PPTP_SESSION_ERROR;
  268. break;
  269. case PPTP_OUT_CALL_REPLY:
  270. /* server accepted call, we now expect GRE frames */
  271. if (info->sstate != PPTP_SESSION_CONFIRMED)
  272. goto invalid;
  273. if (info->cstate != PPTP_CALL_OUT_REQ &&
  274. info->cstate != PPTP_CALL_OUT_CONF)
  275. goto invalid;
  276. cid = pptpReq->ocack.callID;
  277. pcid = pptpReq->ocack.peersCallID;
  278. if (info->pns_call_id != pcid)
  279. goto invalid;
  280. pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name(msg),
  281. ntohs(cid), ntohs(pcid));
  282. if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
  283. info->cstate = PPTP_CALL_OUT_CONF;
  284. info->pac_call_id = cid;
  285. exp_gre(ct, cid, pcid);
  286. } else
  287. info->cstate = PPTP_CALL_NONE;
  288. break;
  289. case PPTP_IN_CALL_REQUEST:
  290. /* server tells us about incoming call request */
  291. if (info->sstate != PPTP_SESSION_CONFIRMED)
  292. goto invalid;
  293. cid = pptpReq->icreq.callID;
  294. pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
  295. info->cstate = PPTP_CALL_IN_REQ;
  296. info->pac_call_id = cid;
  297. break;
  298. case PPTP_IN_CALL_CONNECT:
  299. /* server tells us about incoming call established */
  300. if (info->sstate != PPTP_SESSION_CONFIRMED)
  301. goto invalid;
  302. if (info->cstate != PPTP_CALL_IN_REP &&
  303. info->cstate != PPTP_CALL_IN_CONF)
  304. goto invalid;
  305. pcid = pptpReq->iccon.peersCallID;
  306. cid = info->pac_call_id;
  307. if (info->pns_call_id != pcid)
  308. goto invalid;
  309. pr_debug("%s, PCID=%X\n", pptp_msg_name(msg), ntohs(pcid));
  310. info->cstate = PPTP_CALL_IN_CONF;
  311. /* we expect a GRE connection from PAC to PNS */
  312. exp_gre(ct, cid, pcid);
  313. break;
  314. case PPTP_CALL_DISCONNECT_NOTIFY:
  315. /* server confirms disconnect */
  316. cid = pptpReq->disc.callID;
  317. pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
  318. info->cstate = PPTP_CALL_NONE;
  319. /* untrack this call id, unexpect GRE packets */
  320. pptp_destroy_siblings(ct);
  321. break;
  322. case PPTP_WAN_ERROR_NOTIFY:
  323. case PPTP_SET_LINK_INFO:
  324. case PPTP_ECHO_REQUEST:
  325. case PPTP_ECHO_REPLY:
  326. /* I don't have to explain these ;) */
  327. break;
  328. default:
  329. goto invalid;
  330. }
  331. nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
  332. if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
  333. return nf_nat_pptp_inbound(skb, ct, ctinfo,
  334. protoff, ctlh, pptpReq);
  335. return NF_ACCEPT;
  336. invalid:
  337. pr_debug("invalid %s: type=%d cid=%u pcid=%u "
  338. "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
  339. pptp_msg_name(msg),
  340. msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
  341. ntohs(info->pns_call_id), ntohs(info->pac_call_id));
  342. return NF_ACCEPT;
  343. }
  344. static int
  345. pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
  346. struct PptpControlHeader *ctlh,
  347. union pptp_ctrl_union *pptpReq,
  348. unsigned int reqlen,
  349. struct nf_conn *ct,
  350. enum ip_conntrack_info ctinfo)
  351. {
  352. struct nf_ct_pptp_master *info = nfct_help_data(ct);
  353. u_int16_t msg;
  354. __be16 cid = 0, pcid = 0;
  355. typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
  356. msg = ntohs(ctlh->messageType);
  357. pr_debug("outbound control message %s\n", pptp_msg_name(msg));
  358. switch (msg) {
  359. case PPTP_START_SESSION_REQUEST:
  360. /* client requests for new control session */
  361. if (info->sstate != PPTP_SESSION_NONE)
  362. goto invalid;
  363. info->sstate = PPTP_SESSION_REQUESTED;
  364. break;
  365. case PPTP_STOP_SESSION_REQUEST:
  366. /* client requests end of control session */
  367. info->sstate = PPTP_SESSION_STOPREQ;
  368. break;
  369. case PPTP_OUT_CALL_REQUEST:
  370. /* client initiating connection to server */
  371. if (info->sstate != PPTP_SESSION_CONFIRMED)
  372. goto invalid;
  373. info->cstate = PPTP_CALL_OUT_REQ;
  374. /* track PNS call id */
  375. cid = pptpReq->ocreq.callID;
  376. pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
  377. info->pns_call_id = cid;
  378. break;
  379. case PPTP_IN_CALL_REPLY:
  380. /* client answers incoming call */
  381. if (info->cstate != PPTP_CALL_IN_REQ &&
  382. info->cstate != PPTP_CALL_IN_REP)
  383. goto invalid;
  384. cid = pptpReq->icack.callID;
  385. pcid = pptpReq->icack.peersCallID;
  386. if (info->pac_call_id != pcid)
  387. goto invalid;
  388. pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name(msg),
  389. ntohs(cid), ntohs(pcid));
  390. if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
  391. /* part two of the three-way handshake */
  392. info->cstate = PPTP_CALL_IN_REP;
  393. info->pns_call_id = cid;
  394. } else
  395. info->cstate = PPTP_CALL_NONE;
  396. break;
  397. case PPTP_CALL_CLEAR_REQUEST:
  398. /* client requests hangup of call */
  399. if (info->sstate != PPTP_SESSION_CONFIRMED)
  400. goto invalid;
  401. /* FUTURE: iterate over all calls and check if
  402. * call ID is valid. We don't do this without newnat,
  403. * because we only know about last call */
  404. info->cstate = PPTP_CALL_CLEAR_REQ;
  405. break;
  406. case PPTP_SET_LINK_INFO:
  407. case PPTP_ECHO_REQUEST:
  408. case PPTP_ECHO_REPLY:
  409. /* I don't have to explain these ;) */
  410. break;
  411. default:
  412. goto invalid;
  413. }
  414. nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
  415. if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
  416. return nf_nat_pptp_outbound(skb, ct, ctinfo,
  417. protoff, ctlh, pptpReq);
  418. return NF_ACCEPT;
  419. invalid:
  420. pr_debug("invalid %s: type=%d cid=%u pcid=%u "
  421. "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
  422. pptp_msg_name(msg),
  423. msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
  424. ntohs(info->pns_call_id), ntohs(info->pac_call_id));
  425. return NF_ACCEPT;
  426. }
  427. static const unsigned int pptp_msg_size[] = {
  428. [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
  429. [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
  430. [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
  431. [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
  432. [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
  433. [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
  434. [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
  435. [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
  436. [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
  437. [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
  438. [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
  439. [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
  440. [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
  441. };
  442. /* track caller id inside control connection, call expect_related */
  443. static int
  444. conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
  445. struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  446. {
  447. int dir = CTINFO2DIR(ctinfo);
  448. const struct nf_ct_pptp_master *info = nfct_help_data(ct);
  449. const struct tcphdr *tcph;
  450. struct tcphdr _tcph;
  451. const struct pptp_pkt_hdr *pptph;
  452. struct pptp_pkt_hdr _pptph;
  453. struct PptpControlHeader _ctlh, *ctlh;
  454. union pptp_ctrl_union _pptpReq, *pptpReq;
  455. unsigned int tcplen = skb->len - protoff;
  456. unsigned int datalen, reqlen, nexthdr_off;
  457. int oldsstate, oldcstate;
  458. int ret;
  459. u_int16_t msg;
  460. #if IS_ENABLED(CONFIG_NF_NAT)
  461. if (!nf_ct_is_confirmed(ct) && (ct->status & IPS_NAT_MASK)) {
  462. struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
  463. if (!nat && !nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC))
  464. return NF_DROP;
  465. }
  466. #endif
  467. /* don't do any tracking before tcp handshake complete */
  468. if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
  469. return NF_ACCEPT;
  470. nexthdr_off = protoff;
  471. tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
  472. BUG_ON(!tcph);
  473. nexthdr_off += tcph->doff * 4;
  474. datalen = tcplen - tcph->doff * 4;
  475. pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
  476. if (!pptph) {
  477. pr_debug("no full PPTP header, can't track\n");
  478. return NF_ACCEPT;
  479. }
  480. nexthdr_off += sizeof(_pptph);
  481. datalen -= sizeof(_pptph);
  482. /* if it's not a control message we can't do anything with it */
  483. if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
  484. ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
  485. pr_debug("not a control packet\n");
  486. return NF_ACCEPT;
  487. }
  488. ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
  489. if (!ctlh)
  490. return NF_ACCEPT;
  491. nexthdr_off += sizeof(_ctlh);
  492. datalen -= sizeof(_ctlh);
  493. reqlen = datalen;
  494. msg = ntohs(ctlh->messageType);
  495. if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
  496. return NF_ACCEPT;
  497. if (reqlen > sizeof(*pptpReq))
  498. reqlen = sizeof(*pptpReq);
  499. pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
  500. if (!pptpReq)
  501. return NF_ACCEPT;
  502. oldsstate = info->sstate;
  503. oldcstate = info->cstate;
  504. spin_lock_bh(&nf_pptp_lock);
  505. /* FIXME: We just blindly assume that the control connection is always
  506. * established from PNS->PAC. However, RFC makes no guarantee */
  507. if (dir == IP_CT_DIR_ORIGINAL)
  508. /* client -> server (PNS -> PAC) */
  509. ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
  510. ctinfo);
  511. else
  512. /* server -> client (PAC -> PNS) */
  513. ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
  514. ctinfo);
  515. pr_debug("sstate: %d->%d, cstate: %d->%d\n",
  516. oldsstate, info->sstate, oldcstate, info->cstate);
  517. spin_unlock_bh(&nf_pptp_lock);
  518. return ret;
  519. }
  520. static const struct nf_conntrack_expect_policy pptp_exp_policy = {
  521. .max_expected = 2,
  522. .timeout = 5 * 60,
  523. };
  524. /* control protocol helper */
  525. static struct nf_conntrack_helper pptp __read_mostly = {
  526. .name = "pptp",
  527. .me = THIS_MODULE,
  528. .tuple.src.l3num = AF_INET,
  529. .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT),
  530. .tuple.dst.protonum = IPPROTO_TCP,
  531. .help = conntrack_pptp_help,
  532. .destroy = pptp_destroy_siblings,
  533. .expect_policy = &pptp_exp_policy,
  534. };
  535. static int __init nf_conntrack_pptp_init(void)
  536. {
  537. NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_pptp_master));
  538. return nf_conntrack_helper_register(&pptp);
  539. }
  540. static void __exit nf_conntrack_pptp_fini(void)
  541. {
  542. nf_conntrack_helper_unregister(&pptp);
  543. }
  544. module_init(nf_conntrack_pptp_init);
  545. module_exit(nf_conntrack_pptp_fini);