nf_nat_amanda.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Amanda extension for TCP NAT alteration.
  2. * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca>
  3. * based on a copy of HW's ip_nat_irc.c as well as other modules
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/udp.h>
  14. #include <net/netfilter/nf_nat_helper.h>
  15. #include <net/netfilter/nf_nat_rule.h>
  16. #include <net/netfilter/nf_conntrack_helper.h>
  17. #include <net/netfilter/nf_conntrack_expect.h>
  18. #include <linux/netfilter/nf_conntrack_amanda.h>
  19. MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
  20. MODULE_DESCRIPTION("Amanda NAT helper");
  21. MODULE_LICENSE("GPL");
  22. MODULE_ALIAS("ip_nat_amanda");
  23. static unsigned int help(struct sk_buff **pskb,
  24. enum ip_conntrack_info ctinfo,
  25. unsigned int matchoff,
  26. unsigned int matchlen,
  27. struct nf_conntrack_expect *exp)
  28. {
  29. char buffer[sizeof("65535")];
  30. u_int16_t port;
  31. unsigned int ret;
  32. /* Connection comes from client. */
  33. exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
  34. exp->dir = IP_CT_DIR_ORIGINAL;
  35. /* When you see the packet, we need to NAT it the same as the
  36. * this one (ie. same IP: it will be TCP and master is UDP). */
  37. exp->expectfn = nf_nat_follow_master;
  38. /* Try to get same port: if not, try to change it. */
  39. for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
  40. exp->tuple.dst.u.tcp.port = htons(port);
  41. if (nf_conntrack_expect_related(exp) == 0)
  42. break;
  43. }
  44. if (port == 0)
  45. return NF_DROP;
  46. sprintf(buffer, "%u", port);
  47. ret = nf_nat_mangle_udp_packet(pskb, exp->master, ctinfo,
  48. matchoff, matchlen,
  49. buffer, strlen(buffer));
  50. if (ret != NF_ACCEPT)
  51. nf_conntrack_unexpect_related(exp);
  52. return ret;
  53. }
  54. static void __exit nf_nat_amanda_fini(void)
  55. {
  56. rcu_assign_pointer(nf_nat_amanda_hook, NULL);
  57. synchronize_rcu();
  58. }
  59. static int __init nf_nat_amanda_init(void)
  60. {
  61. BUG_ON(rcu_dereference(nf_nat_amanda_hook));
  62. rcu_assign_pointer(nf_nat_amanda_hook, help);
  63. return 0;
  64. }
  65. module_init(nf_nat_amanda_init);
  66. module_exit(nf_nat_amanda_fini);