nf_conntrack_sane.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* SANE connection tracking helper
  3. * (SANE = Scanner Access Now Easy)
  4. * For documentation about the SANE network protocol see
  5. * http://www.sane-project.org/html/doc015.html
  6. */
  7. /* Copyright (C) 2007 Red Hat, Inc.
  8. * Author: Michal Schmidt <mschmidt@redhat.com>
  9. * Based on the FTP conntrack helper (net/netfilter/nf_conntrack_ftp.c):
  10. * (C) 1999-2001 Paul `Rusty' Russell
  11. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  12. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  13. * (C) 2003 Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/slab.h>
  20. #include <linux/in.h>
  21. #include <linux/tcp.h>
  22. #include <net/netfilter/nf_conntrack.h>
  23. #include <net/netfilter/nf_conntrack_helper.h>
  24. #include <net/netfilter/nf_conntrack_expect.h>
  25. #include <linux/netfilter/nf_conntrack_sane.h>
  26. #define HELPER_NAME "sane"
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
  29. MODULE_DESCRIPTION("SANE connection tracking helper");
  30. MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
  31. static char *sane_buffer;
  32. static DEFINE_SPINLOCK(nf_sane_lock);
  33. #define MAX_PORTS 8
  34. static u_int16_t ports[MAX_PORTS];
  35. static unsigned int ports_c;
  36. module_param_array(ports, ushort, &ports_c, 0400);
  37. struct sane_request {
  38. __be32 RPC_code;
  39. #define SANE_NET_START 7 /* RPC code */
  40. __be32 handle;
  41. };
  42. struct sane_reply_net_start {
  43. __be32 status;
  44. #define SANE_STATUS_SUCCESS 0
  45. __be16 zero;
  46. __be16 port;
  47. /* other fields aren't interesting for conntrack */
  48. };
  49. static int help(struct sk_buff *skb,
  50. unsigned int protoff,
  51. struct nf_conn *ct,
  52. enum ip_conntrack_info ctinfo)
  53. {
  54. unsigned int dataoff, datalen;
  55. const struct tcphdr *th;
  56. struct tcphdr _tcph;
  57. void *sb_ptr;
  58. int ret = NF_ACCEPT;
  59. int dir = CTINFO2DIR(ctinfo);
  60. struct nf_ct_sane_master *ct_sane_info = nfct_help_data(ct);
  61. struct nf_conntrack_expect *exp;
  62. struct nf_conntrack_tuple *tuple;
  63. struct sane_request *req;
  64. struct sane_reply_net_start *reply;
  65. /* Until there's been traffic both ways, don't look in packets. */
  66. if (ctinfo != IP_CT_ESTABLISHED &&
  67. ctinfo != IP_CT_ESTABLISHED_REPLY)
  68. return NF_ACCEPT;
  69. /* Not a full tcp header? */
  70. th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  71. if (th == NULL)
  72. return NF_ACCEPT;
  73. /* No data? */
  74. dataoff = protoff + th->doff * 4;
  75. if (dataoff >= skb->len)
  76. return NF_ACCEPT;
  77. datalen = skb->len - dataoff;
  78. spin_lock_bh(&nf_sane_lock);
  79. sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
  80. BUG_ON(sb_ptr == NULL);
  81. if (dir == IP_CT_DIR_ORIGINAL) {
  82. if (datalen != sizeof(struct sane_request))
  83. goto out;
  84. req = sb_ptr;
  85. if (req->RPC_code != htonl(SANE_NET_START)) {
  86. /* Not an interesting command */
  87. ct_sane_info->state = SANE_STATE_NORMAL;
  88. goto out;
  89. }
  90. /* We're interested in the next reply */
  91. ct_sane_info->state = SANE_STATE_START_REQUESTED;
  92. goto out;
  93. }
  94. /* Is it a reply to an uninteresting command? */
  95. if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
  96. goto out;
  97. /* It's a reply to SANE_NET_START. */
  98. ct_sane_info->state = SANE_STATE_NORMAL;
  99. if (datalen < sizeof(struct sane_reply_net_start)) {
  100. pr_debug("NET_START reply too short\n");
  101. goto out;
  102. }
  103. reply = sb_ptr;
  104. if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
  105. /* saned refused the command */
  106. pr_debug("unsuccessful SANE_STATUS = %u\n",
  107. ntohl(reply->status));
  108. goto out;
  109. }
  110. /* Invalid saned reply? Ignore it. */
  111. if (reply->zero != 0)
  112. goto out;
  113. exp = nf_ct_expect_alloc(ct);
  114. if (exp == NULL) {
  115. nf_ct_helper_log(skb, ct, "cannot alloc expectation");
  116. ret = NF_DROP;
  117. goto out;
  118. }
  119. tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  120. nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
  121. &tuple->src.u3, &tuple->dst.u3,
  122. IPPROTO_TCP, NULL, &reply->port);
  123. pr_debug("expect: ");
  124. nf_ct_dump_tuple(&exp->tuple);
  125. /* Can't expect this? Best to drop packet now. */
  126. if (nf_ct_expect_related(exp, 0) != 0) {
  127. nf_ct_helper_log(skb, ct, "cannot add expectation");
  128. ret = NF_DROP;
  129. }
  130. nf_ct_expect_put(exp);
  131. out:
  132. spin_unlock_bh(&nf_sane_lock);
  133. return ret;
  134. }
  135. static struct nf_conntrack_helper sane[MAX_PORTS * 2] __read_mostly;
  136. static const struct nf_conntrack_expect_policy sane_exp_policy = {
  137. .max_expected = 1,
  138. .timeout = 5 * 60,
  139. };
  140. static void __exit nf_conntrack_sane_fini(void)
  141. {
  142. nf_conntrack_helpers_unregister(sane, ports_c * 2);
  143. kfree(sane_buffer);
  144. }
  145. static int __init nf_conntrack_sane_init(void)
  146. {
  147. int i, ret = 0;
  148. NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sane_master));
  149. sane_buffer = kmalloc(65536, GFP_KERNEL);
  150. if (!sane_buffer)
  151. return -ENOMEM;
  152. if (ports_c == 0)
  153. ports[ports_c++] = SANE_PORT;
  154. /* FIXME should be configurable whether IPv4 and IPv6 connections
  155. are tracked or not - YK */
  156. for (i = 0; i < ports_c; i++) {
  157. nf_ct_helper_init(&sane[2 * i], AF_INET, IPPROTO_TCP,
  158. HELPER_NAME, SANE_PORT, ports[i], ports[i],
  159. &sane_exp_policy, 0, help, NULL,
  160. THIS_MODULE);
  161. nf_ct_helper_init(&sane[2 * i + 1], AF_INET6, IPPROTO_TCP,
  162. HELPER_NAME, SANE_PORT, ports[i], ports[i],
  163. &sane_exp_policy, 0, help, NULL,
  164. THIS_MODULE);
  165. }
  166. ret = nf_conntrack_helpers_register(sane, ports_c * 2);
  167. if (ret < 0) {
  168. pr_err("failed to register helpers\n");
  169. kfree(sane_buffer);
  170. return ret;
  171. }
  172. return 0;
  173. }
  174. module_init(nf_conntrack_sane_init);
  175. module_exit(nf_conntrack_sane_fini);