nf_conntrack_tftp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 as
  5. * published by the Free Software Foundation.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/in.h>
  10. #include <linux/udp.h>
  11. #include <linux/netfilter.h>
  12. #include <net/netfilter/nf_conntrack.h>
  13. #include <net/netfilter/nf_conntrack_tuple.h>
  14. #include <net/netfilter/nf_conntrack_expect.h>
  15. #include <net/netfilter/nf_conntrack_ecache.h>
  16. #include <net/netfilter/nf_conntrack_helper.h>
  17. #include <linux/netfilter/nf_conntrack_tftp.h>
  18. MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
  19. MODULE_DESCRIPTION("TFTP connection tracking helper");
  20. MODULE_LICENSE("GPL");
  21. MODULE_ALIAS("ip_conntrack_tftp");
  22. #define MAX_PORTS 8
  23. static unsigned short ports[MAX_PORTS];
  24. static int ports_c;
  25. module_param_array(ports, ushort, &ports_c, 0400);
  26. MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
  27. #if 0
  28. #define DEBUGP(format, args...) printk("%s:%s:" format, \
  29. __FILE__, __FUNCTION__ , ## args)
  30. #else
  31. #define DEBUGP(format, args...)
  32. #endif
  33. unsigned int (*nf_nat_tftp_hook)(struct sk_buff **pskb,
  34. enum ip_conntrack_info ctinfo,
  35. struct nf_conntrack_expect *exp) __read_mostly;
  36. EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
  37. static int tftp_help(struct sk_buff **pskb,
  38. unsigned int protoff,
  39. struct nf_conn *ct,
  40. enum ip_conntrack_info ctinfo)
  41. {
  42. struct tftphdr _tftph, *tfh;
  43. struct nf_conntrack_expect *exp;
  44. struct nf_conntrack_tuple *tuple;
  45. unsigned int ret = NF_ACCEPT;
  46. int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
  47. typeof(nf_nat_tftp_hook) nf_nat_tftp;
  48. tfh = skb_header_pointer(*pskb, protoff + sizeof(struct udphdr),
  49. sizeof(_tftph), &_tftph);
  50. if (tfh == NULL)
  51. return NF_ACCEPT;
  52. switch (ntohs(tfh->opcode)) {
  53. case TFTP_OPCODE_READ:
  54. case TFTP_OPCODE_WRITE:
  55. /* RRQ and WRQ works the same way */
  56. DEBUGP("");
  57. NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  58. NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
  59. exp = nf_conntrack_expect_alloc(ct);
  60. if (exp == NULL)
  61. return NF_DROP;
  62. tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
  63. nf_conntrack_expect_init(exp, family,
  64. &tuple->src.u3, &tuple->dst.u3,
  65. IPPROTO_UDP,
  66. NULL, &tuple->dst.u.udp.port);
  67. DEBUGP("expect: ");
  68. NF_CT_DUMP_TUPLE(&exp->tuple);
  69. NF_CT_DUMP_TUPLE(&exp->mask);
  70. nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
  71. if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
  72. ret = nf_nat_tftp(pskb, ctinfo, exp);
  73. else if (nf_conntrack_expect_related(exp) != 0)
  74. ret = NF_DROP;
  75. nf_conntrack_expect_put(exp);
  76. break;
  77. case TFTP_OPCODE_DATA:
  78. case TFTP_OPCODE_ACK:
  79. DEBUGP("Data/ACK opcode\n");
  80. break;
  81. case TFTP_OPCODE_ERROR:
  82. DEBUGP("Error opcode\n");
  83. break;
  84. default:
  85. DEBUGP("Unknown opcode\n");
  86. }
  87. return ret;
  88. }
  89. static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly;
  90. static char tftp_names[MAX_PORTS][2][sizeof("tftp-65535")] __read_mostly;
  91. static void nf_conntrack_tftp_fini(void)
  92. {
  93. int i, j;
  94. for (i = 0; i < ports_c; i++) {
  95. for (j = 0; j < 2; j++)
  96. nf_conntrack_helper_unregister(&tftp[i][j]);
  97. }
  98. }
  99. static int __init nf_conntrack_tftp_init(void)
  100. {
  101. int i, j, ret;
  102. char *tmpname;
  103. if (ports_c == 0)
  104. ports[ports_c++] = TFTP_PORT;
  105. for (i = 0; i < ports_c; i++) {
  106. memset(&tftp[i], 0, sizeof(tftp[i]));
  107. tftp[i][0].tuple.src.l3num = AF_INET;
  108. tftp[i][1].tuple.src.l3num = AF_INET6;
  109. for (j = 0; j < 2; j++) {
  110. tftp[i][j].tuple.dst.protonum = IPPROTO_UDP;
  111. tftp[i][j].tuple.src.u.udp.port = htons(ports[i]);
  112. tftp[i][j].mask.src.l3num = 0xFFFF;
  113. tftp[i][j].mask.dst.protonum = 0xFF;
  114. tftp[i][j].mask.src.u.udp.port = htons(0xFFFF);
  115. tftp[i][j].max_expected = 1;
  116. tftp[i][j].timeout = 5 * 60; /* 5 minutes */
  117. tftp[i][j].me = THIS_MODULE;
  118. tftp[i][j].help = tftp_help;
  119. tmpname = &tftp_names[i][j][0];
  120. if (ports[i] == TFTP_PORT)
  121. sprintf(tmpname, "tftp");
  122. else
  123. sprintf(tmpname, "tftp-%u", i);
  124. tftp[i][j].name = tmpname;
  125. ret = nf_conntrack_helper_register(&tftp[i][j]);
  126. if (ret) {
  127. printk("nf_ct_tftp: failed to register helper "
  128. "for pf: %u port: %u\n",
  129. tftp[i][j].tuple.src.l3num, ports[i]);
  130. nf_conntrack_tftp_fini();
  131. return ret;
  132. }
  133. }
  134. }
  135. return 0;
  136. }
  137. module_init(nf_conntrack_tftp_init);
  138. module_exit(nf_conntrack_tftp_fini);