nf_conntrack_proto_gre.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * ip_conntrack_proto_gre.c - Version 3.0
  3. *
  4. * Connection tracking protocol helper module for GRE.
  5. *
  6. * GRE is a generic encapsulation protocol, which is generally not very
  7. * suited for NAT, as it has no protocol-specific part as port numbers.
  8. *
  9. * It has an optional key field, which may help us distinguishing two
  10. * connections between the same two hosts.
  11. *
  12. * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
  13. *
  14. * PPTP is built on top of a modified version of GRE, and has a mandatory
  15. * field called "CallID", which serves us for the same purpose as the key
  16. * field in plain GRE.
  17. *
  18. * Documentation about PPTP can be found in RFC 2637
  19. *
  20. * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
  21. *
  22. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  23. *
  24. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/timer.h>
  29. #include <linux/list.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/in.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/slab.h>
  35. #include <net/dst.h>
  36. #include <net/net_namespace.h>
  37. #include <net/netns/generic.h>
  38. #include <net/netfilter/nf_conntrack_l4proto.h>
  39. #include <net/netfilter/nf_conntrack_helper.h>
  40. #include <net/netfilter/nf_conntrack_core.h>
  41. #include <linux/netfilter/nf_conntrack_proto_gre.h>
  42. #include <linux/netfilter/nf_conntrack_pptp.h>
  43. enum grep_conntrack {
  44. GRE_CT_UNREPLIED,
  45. GRE_CT_REPLIED,
  46. GRE_CT_MAX
  47. };
  48. static unsigned int gre_timeouts[GRE_CT_MAX] = {
  49. [GRE_CT_UNREPLIED] = 30*HZ,
  50. [GRE_CT_REPLIED] = 180*HZ,
  51. };
  52. static int proto_gre_net_id __read_mostly;
  53. struct netns_proto_gre {
  54. struct nf_proto_net nf;
  55. rwlock_t keymap_lock;
  56. struct list_head keymap_list;
  57. unsigned int gre_timeouts[GRE_CT_MAX];
  58. };
  59. static inline struct netns_proto_gre *gre_pernet(struct net *net)
  60. {
  61. return net_generic(net, proto_gre_net_id);
  62. }
  63. static void nf_ct_gre_keymap_flush(struct net *net)
  64. {
  65. struct netns_proto_gre *net_gre = gre_pernet(net);
  66. struct nf_ct_gre_keymap *km, *tmp;
  67. write_lock_bh(&net_gre->keymap_lock);
  68. list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
  69. list_del(&km->list);
  70. kfree(km);
  71. }
  72. write_unlock_bh(&net_gre->keymap_lock);
  73. }
  74. static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
  75. const struct nf_conntrack_tuple *t)
  76. {
  77. return km->tuple.src.l3num == t->src.l3num &&
  78. !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
  79. !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
  80. km->tuple.dst.protonum == t->dst.protonum &&
  81. km->tuple.dst.u.all == t->dst.u.all;
  82. }
  83. /* look up the source key for a given tuple */
  84. static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
  85. {
  86. struct netns_proto_gre *net_gre = gre_pernet(net);
  87. struct nf_ct_gre_keymap *km;
  88. __be16 key = 0;
  89. read_lock_bh(&net_gre->keymap_lock);
  90. list_for_each_entry(km, &net_gre->keymap_list, list) {
  91. if (gre_key_cmpfn(km, t)) {
  92. key = km->tuple.src.u.gre.key;
  93. break;
  94. }
  95. }
  96. read_unlock_bh(&net_gre->keymap_lock);
  97. pr_debug("lookup src key 0x%x for ", key);
  98. nf_ct_dump_tuple(t);
  99. return key;
  100. }
  101. /* add a single keymap entry, associate with specified master ct */
  102. int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
  103. struct nf_conntrack_tuple *t)
  104. {
  105. struct net *net = nf_ct_net(ct);
  106. struct netns_proto_gre *net_gre = gre_pernet(net);
  107. struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
  108. struct nf_ct_gre_keymap **kmp, *km;
  109. kmp = &ct_pptp_info->keymap[dir];
  110. if (*kmp) {
  111. /* check whether it's a retransmission */
  112. read_lock_bh(&net_gre->keymap_lock);
  113. list_for_each_entry(km, &net_gre->keymap_list, list) {
  114. if (gre_key_cmpfn(km, t) && km == *kmp) {
  115. read_unlock_bh(&net_gre->keymap_lock);
  116. return 0;
  117. }
  118. }
  119. read_unlock_bh(&net_gre->keymap_lock);
  120. pr_debug("trying to override keymap_%s for ct %p\n",
  121. dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
  122. return -EEXIST;
  123. }
  124. km = kmalloc(sizeof(*km), GFP_ATOMIC);
  125. if (!km)
  126. return -ENOMEM;
  127. memcpy(&km->tuple, t, sizeof(*t));
  128. *kmp = km;
  129. pr_debug("adding new entry %p: ", km);
  130. nf_ct_dump_tuple(&km->tuple);
  131. write_lock_bh(&net_gre->keymap_lock);
  132. list_add_tail(&km->list, &net_gre->keymap_list);
  133. write_unlock_bh(&net_gre->keymap_lock);
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
  137. /* destroy the keymap entries associated with specified master ct */
  138. void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
  139. {
  140. struct net *net = nf_ct_net(ct);
  141. struct netns_proto_gre *net_gre = gre_pernet(net);
  142. struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
  143. enum ip_conntrack_dir dir;
  144. pr_debug("entering for ct %p\n", ct);
  145. write_lock_bh(&net_gre->keymap_lock);
  146. for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
  147. if (ct_pptp_info->keymap[dir]) {
  148. pr_debug("removing %p from list\n",
  149. ct_pptp_info->keymap[dir]);
  150. list_del(&ct_pptp_info->keymap[dir]->list);
  151. kfree(ct_pptp_info->keymap[dir]);
  152. ct_pptp_info->keymap[dir] = NULL;
  153. }
  154. }
  155. write_unlock_bh(&net_gre->keymap_lock);
  156. }
  157. EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
  158. /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
  159. /* invert gre part of tuple */
  160. static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple,
  161. const struct nf_conntrack_tuple *orig)
  162. {
  163. tuple->dst.u.gre.key = orig->src.u.gre.key;
  164. tuple->src.u.gre.key = orig->dst.u.gre.key;
  165. return true;
  166. }
  167. /* gre hdr info to tuple */
  168. static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
  169. struct net *net, struct nf_conntrack_tuple *tuple)
  170. {
  171. const struct gre_hdr_pptp *pgrehdr;
  172. struct gre_hdr_pptp _pgrehdr;
  173. __be16 srckey;
  174. const struct gre_hdr *grehdr;
  175. struct gre_hdr _grehdr;
  176. /* first only delinearize old RFC1701 GRE header */
  177. grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
  178. if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
  179. /* try to behave like "nf_conntrack_proto_generic" */
  180. tuple->src.u.all = 0;
  181. tuple->dst.u.all = 0;
  182. return true;
  183. }
  184. /* PPTP header is variable length, only need up to the call_id field */
  185. pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
  186. if (!pgrehdr)
  187. return true;
  188. if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
  189. pr_debug("GRE_VERSION_PPTP but unknown proto\n");
  190. return false;
  191. }
  192. tuple->dst.u.gre.key = pgrehdr->call_id;
  193. srckey = gre_keymap_lookup(net, tuple);
  194. tuple->src.u.gre.key = srckey;
  195. return true;
  196. }
  197. /* print gre part of tuple */
  198. static void gre_print_tuple(struct seq_file *s,
  199. const struct nf_conntrack_tuple *tuple)
  200. {
  201. seq_printf(s, "srckey=0x%x dstkey=0x%x ",
  202. ntohs(tuple->src.u.gre.key),
  203. ntohs(tuple->dst.u.gre.key));
  204. }
  205. /* print private data for conntrack */
  206. static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
  207. {
  208. seq_printf(s, "timeout=%u, stream_timeout=%u ",
  209. (ct->proto.gre.timeout / HZ),
  210. (ct->proto.gre.stream_timeout / HZ));
  211. }
  212. static unsigned int *gre_get_timeouts(struct net *net)
  213. {
  214. return gre_pernet(net)->gre_timeouts;
  215. }
  216. /* Returns verdict for packet, and may modify conntrack */
  217. static int gre_packet(struct nf_conn *ct,
  218. const struct sk_buff *skb,
  219. unsigned int dataoff,
  220. enum ip_conntrack_info ctinfo,
  221. u_int8_t pf,
  222. unsigned int hooknum,
  223. unsigned int *timeouts)
  224. {
  225. /* If we've seen traffic both ways, this is a GRE connection.
  226. * Extend timeout. */
  227. if (ct->status & IPS_SEEN_REPLY) {
  228. nf_ct_refresh_acct(ct, ctinfo, skb,
  229. ct->proto.gre.stream_timeout);
  230. /* Also, more likely to be important, and not a probe. */
  231. if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
  232. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  233. } else
  234. nf_ct_refresh_acct(ct, ctinfo, skb,
  235. ct->proto.gre.timeout);
  236. return NF_ACCEPT;
  237. }
  238. /* Called when a new connection for this protocol found. */
  239. static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
  240. unsigned int dataoff, unsigned int *timeouts)
  241. {
  242. pr_debug(": ");
  243. nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  244. /* initialize to sane value. Ideally a conntrack helper
  245. * (e.g. in case of pptp) is increasing them */
  246. ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
  247. ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
  248. return true;
  249. }
  250. /* Called when a conntrack entry has already been removed from the hashes
  251. * and is about to be deleted from memory */
  252. static void gre_destroy(struct nf_conn *ct)
  253. {
  254. struct nf_conn *master = ct->master;
  255. pr_debug(" entering\n");
  256. if (!master)
  257. pr_debug("no master !?!\n");
  258. else
  259. nf_ct_gre_keymap_destroy(master);
  260. }
  261. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  262. #include <linux/netfilter/nfnetlink.h>
  263. #include <linux/netfilter/nfnetlink_cttimeout.h>
  264. static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
  265. struct net *net, void *data)
  266. {
  267. unsigned int *timeouts = data;
  268. struct netns_proto_gre *net_gre = gre_pernet(net);
  269. /* set default timeouts for GRE. */
  270. timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED];
  271. timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED];
  272. if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
  273. timeouts[GRE_CT_UNREPLIED] =
  274. ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
  275. }
  276. if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
  277. timeouts[GRE_CT_REPLIED] =
  278. ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
  279. }
  280. return 0;
  281. }
  282. static int
  283. gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  284. {
  285. const unsigned int *timeouts = data;
  286. if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
  287. htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
  288. nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
  289. htonl(timeouts[GRE_CT_REPLIED] / HZ)))
  290. goto nla_put_failure;
  291. return 0;
  292. nla_put_failure:
  293. return -ENOSPC;
  294. }
  295. static const struct nla_policy
  296. gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
  297. [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 },
  298. [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 },
  299. };
  300. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  301. static int gre_init_net(struct net *net, u_int16_t proto)
  302. {
  303. struct netns_proto_gre *net_gre = gre_pernet(net);
  304. int i;
  305. rwlock_init(&net_gre->keymap_lock);
  306. INIT_LIST_HEAD(&net_gre->keymap_list);
  307. for (i = 0; i < GRE_CT_MAX; i++)
  308. net_gre->gre_timeouts[i] = gre_timeouts[i];
  309. return 0;
  310. }
  311. /* protocol helper struct */
  312. static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = {
  313. .l3proto = AF_INET,
  314. .l4proto = IPPROTO_GRE,
  315. .name = "gre",
  316. .pkt_to_tuple = gre_pkt_to_tuple,
  317. .invert_tuple = gre_invert_tuple,
  318. .print_tuple = gre_print_tuple,
  319. .print_conntrack = gre_print_conntrack,
  320. .get_timeouts = gre_get_timeouts,
  321. .packet = gre_packet,
  322. .new = gre_new,
  323. .destroy = gre_destroy,
  324. .me = THIS_MODULE,
  325. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  326. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  327. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  328. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  329. .nla_policy = nf_ct_port_nla_policy,
  330. #endif
  331. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  332. .ctnl_timeout = {
  333. .nlattr_to_obj = gre_timeout_nlattr_to_obj,
  334. .obj_to_nlattr = gre_timeout_obj_to_nlattr,
  335. .nlattr_max = CTA_TIMEOUT_GRE_MAX,
  336. .obj_size = sizeof(unsigned int) * GRE_CT_MAX,
  337. .nla_policy = gre_timeout_nla_policy,
  338. },
  339. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  340. .net_id = &proto_gre_net_id,
  341. .init_net = gre_init_net,
  342. };
  343. static int proto_gre_net_init(struct net *net)
  344. {
  345. int ret = 0;
  346. ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_gre4);
  347. if (ret < 0)
  348. pr_err("nf_conntrack_gre4: pernet registration failed.\n");
  349. return ret;
  350. }
  351. static void proto_gre_net_exit(struct net *net)
  352. {
  353. nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_gre4);
  354. nf_ct_gre_keymap_flush(net);
  355. }
  356. static struct pernet_operations proto_gre_net_ops = {
  357. .init = proto_gre_net_init,
  358. .exit = proto_gre_net_exit,
  359. .id = &proto_gre_net_id,
  360. .size = sizeof(struct netns_proto_gre),
  361. };
  362. static int __init nf_ct_proto_gre_init(void)
  363. {
  364. int ret;
  365. ret = register_pernet_subsys(&proto_gre_net_ops);
  366. if (ret < 0)
  367. goto out_pernet;
  368. ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_gre4);
  369. if (ret < 0)
  370. goto out_gre4;
  371. return 0;
  372. out_gre4:
  373. unregister_pernet_subsys(&proto_gre_net_ops);
  374. out_pernet:
  375. return ret;
  376. }
  377. static void __exit nf_ct_proto_gre_fini(void)
  378. {
  379. nf_ct_l4proto_unregister(&nf_conntrack_l4proto_gre4);
  380. unregister_pernet_subsys(&proto_gre_net_ops);
  381. }
  382. module_init(nf_ct_proto_gre_init);
  383. module_exit(nf_ct_proto_gre_fini);
  384. MODULE_LICENSE("GPL");