PageRenderTime 99ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/net/netfilter/nf_conntrack_sane.c

https://bitbucket.org/abioy/linux
C | 238 lines | 165 code | 42 blank | 31 comment | 34 complexity | c9a6fc39fd240d2e4dd0d4a80f33a8a0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* SANE connection tracking helper
  2. * (SANE = Scanner Access Now Easy)
  3. * For documentation about the SANE network protocol see
  4. * http://www.sane-project.org/html/doc015.html
  5. */
  6. /* Copyright (C) 2007 Red Hat, Inc.
  7. * Author: Michal Schmidt <mschmidt@redhat.com>
  8. * Based on the FTP conntrack helper (net/netfilter/nf_conntrack_ftp.c):
  9. * (C) 1999-2001 Paul `Rusty' Russell
  10. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  11. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  12. * (C) 2003 Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/netfilter.h>
  21. #include <linux/slab.h>
  22. #include <linux/in.h>
  23. #include <linux/tcp.h>
  24. #include <net/netfilter/nf_conntrack.h>
  25. #include <net/netfilter/nf_conntrack_helper.h>
  26. #include <net/netfilter/nf_conntrack_expect.h>
  27. #include <linux/netfilter/nf_conntrack_sane.h>
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
  30. MODULE_DESCRIPTION("SANE connection tracking helper");
  31. MODULE_ALIAS_NFCT_HELPER("sane");
  32. static char *sane_buffer;
  33. static DEFINE_SPINLOCK(nf_sane_lock);
  34. #define MAX_PORTS 8
  35. static u_int16_t ports[MAX_PORTS];
  36. static unsigned int ports_c;
  37. module_param_array(ports, ushort, &ports_c, 0400);
  38. struct sane_request {
  39. __be32 RPC_code;
  40. #define SANE_NET_START 7 /* RPC code */
  41. __be32 handle;
  42. };
  43. struct sane_reply_net_start {
  44. __be32 status;
  45. #define SANE_STATUS_SUCCESS 0
  46. __be16 zero;
  47. __be16 port;
  48. /* other fields aren't interesting for conntrack */
  49. };
  50. static int help(struct sk_buff *skb,
  51. unsigned int protoff,
  52. struct nf_conn *ct,
  53. enum ip_conntrack_info ctinfo)
  54. {
  55. unsigned int dataoff, datalen;
  56. const struct tcphdr *th;
  57. struct tcphdr _tcph;
  58. void *sb_ptr;
  59. int ret = NF_ACCEPT;
  60. int dir = CTINFO2DIR(ctinfo);
  61. struct nf_ct_sane_master *ct_sane_info;
  62. struct nf_conntrack_expect *exp;
  63. struct nf_conntrack_tuple *tuple;
  64. struct sane_request *req;
  65. struct sane_reply_net_start *reply;
  66. ct_sane_info = &nfct_help(ct)->help.ct_sane_info;
  67. /* Until there's been traffic both ways, don't look in packets. */
  68. if (ctinfo != IP_CT_ESTABLISHED &&
  69. ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY)
  70. return NF_ACCEPT;
  71. /* Not a full tcp header? */
  72. th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  73. if (th == NULL)
  74. return NF_ACCEPT;
  75. /* No data? */
  76. dataoff = protoff + th->doff * 4;
  77. if (dataoff >= skb->len)
  78. return NF_ACCEPT;
  79. datalen = skb->len - dataoff;
  80. spin_lock_bh(&nf_sane_lock);
  81. sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
  82. BUG_ON(sb_ptr == NULL);
  83. if (dir == IP_CT_DIR_ORIGINAL) {
  84. if (datalen != sizeof(struct sane_request))
  85. goto out;
  86. req = sb_ptr;
  87. if (req->RPC_code != htonl(SANE_NET_START)) {
  88. /* Not an interesting command */
  89. ct_sane_info->state = SANE_STATE_NORMAL;
  90. goto out;
  91. }
  92. /* We're interested in the next reply */
  93. ct_sane_info->state = SANE_STATE_START_REQUESTED;
  94. goto out;
  95. }
  96. /* Is it a reply to an uninteresting command? */
  97. if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
  98. goto out;
  99. /* It's a reply to SANE_NET_START. */
  100. ct_sane_info->state = SANE_STATE_NORMAL;
  101. if (datalen < sizeof(struct sane_reply_net_start)) {
  102. pr_debug("nf_ct_sane: NET_START reply too short\n");
  103. goto out;
  104. }
  105. reply = sb_ptr;
  106. if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
  107. /* saned refused the command */
  108. pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
  109. ntohl(reply->status));
  110. goto out;
  111. }
  112. /* Invalid saned reply? Ignore it. */
  113. if (reply->zero != 0)
  114. goto out;
  115. exp = nf_ct_expect_alloc(ct);
  116. if (exp == NULL) {
  117. ret = NF_DROP;
  118. goto out;
  119. }
  120. tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  121. nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
  122. &tuple->src.u3, &tuple->dst.u3,
  123. IPPROTO_TCP, NULL, &reply->port);
  124. pr_debug("nf_ct_sane: expect: ");
  125. nf_ct_dump_tuple(&exp->tuple);
  126. /* Can't expect this? Best to drop packet now. */
  127. if (nf_ct_expect_related(exp) != 0)
  128. ret = NF_DROP;
  129. nf_ct_expect_put(exp);
  130. out:
  131. spin_unlock_bh(&nf_sane_lock);
  132. return ret;
  133. }
  134. static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
  135. static char sane_names[MAX_PORTS][2][sizeof("sane-65535")] __read_mostly;
  136. static const struct nf_conntrack_expect_policy sane_exp_policy = {
  137. .max_expected = 1,
  138. .timeout = 5 * 60,
  139. };
  140. /* don't make this __exit, since it's called from __init ! */
  141. static void nf_conntrack_sane_fini(void)
  142. {
  143. int i, j;
  144. for (i = 0; i < ports_c; i++) {
  145. for (j = 0; j < 2; j++) {
  146. pr_debug("nf_ct_sane: unregistering helper for pf: %d "
  147. "port: %d\n",
  148. sane[i][j].tuple.src.l3num, ports[i]);
  149. nf_conntrack_helper_unregister(&sane[i][j]);
  150. }
  151. }
  152. kfree(sane_buffer);
  153. }
  154. static int __init nf_conntrack_sane_init(void)
  155. {
  156. int i, j = -1, ret = 0;
  157. char *tmpname;
  158. sane_buffer = kmalloc(65536, GFP_KERNEL);
  159. if (!sane_buffer)
  160. return -ENOMEM;
  161. if (ports_c == 0)
  162. ports[ports_c++] = SANE_PORT;
  163. /* FIXME should be configurable whether IPv4 and IPv6 connections
  164. are tracked or not - YK */
  165. for (i = 0; i < ports_c; i++) {
  166. sane[i][0].tuple.src.l3num = PF_INET;
  167. sane[i][1].tuple.src.l3num = PF_INET6;
  168. for (j = 0; j < 2; j++) {
  169. sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
  170. sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
  171. sane[i][j].expect_policy = &sane_exp_policy;
  172. sane[i][j].me = THIS_MODULE;
  173. sane[i][j].help = help;
  174. tmpname = &sane_names[i][j][0];
  175. if (ports[i] == SANE_PORT)
  176. sprintf(tmpname, "sane");
  177. else
  178. sprintf(tmpname, "sane-%d", ports[i]);
  179. sane[i][j].name = tmpname;
  180. pr_debug("nf_ct_sane: registering helper for pf: %d "
  181. "port: %d\n",
  182. sane[i][j].tuple.src.l3num, ports[i]);
  183. ret = nf_conntrack_helper_register(&sane[i][j]);
  184. if (ret) {
  185. printk(KERN_ERR "nf_ct_sane: failed to "
  186. "register helper for pf: %d port: %d\n",
  187. sane[i][j].tuple.src.l3num, ports[i]);
  188. nf_conntrack_sane_fini();
  189. return ret;
  190. }
  191. }
  192. }
  193. return 0;
  194. }
  195. module_init(nf_conntrack_sane_init);
  196. module_exit(nf_conntrack_sane_fini);