nf_nat_amanda.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/udp.h>
  15. #include <net/netfilter/nf_conntrack_helper.h>
  16. #include <net/netfilter/nf_conntrack_expect.h>
  17. #include <net/netfilter/nf_nat_helper.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 *skb,
  24. enum ip_conntrack_info ctinfo,
  25. unsigned int protoff,
  26. unsigned int matchoff,
  27. unsigned int matchlen,
  28. struct nf_conntrack_expect *exp)
  29. {
  30. char buffer[sizeof("65535")];
  31. u_int16_t port;
  32. unsigned int ret;
  33. /* Connection comes from client. */
  34. exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
  35. exp->dir = IP_CT_DIR_ORIGINAL;
  36. /* When you see the packet, we need to NAT it the same as the
  37. * this one (ie. same IP: it will be TCP and master is UDP). */
  38. exp->expectfn = nf_nat_follow_master;
  39. /* Try to get same port: if not, try to change it. */
  40. for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
  41. int res;
  42. exp->tuple.dst.u.tcp.port = htons(port);
  43. res = nf_ct_expect_related(exp);
  44. if (res == 0)
  45. break;
  46. else if (res != -EBUSY) {
  47. port = 0;
  48. break;
  49. }
  50. }
  51. if (port == 0) {
  52. nf_ct_helper_log(skb, exp->master, "all ports in use");
  53. return NF_DROP;
  54. }
  55. sprintf(buffer, "%u", port);
  56. ret = nf_nat_mangle_udp_packet(skb, exp->master, ctinfo,
  57. protoff, matchoff, matchlen,
  58. buffer, strlen(buffer));
  59. if (ret != NF_ACCEPT) {
  60. nf_ct_helper_log(skb, exp->master, "cannot mangle packet");
  61. nf_ct_unexpect_related(exp);
  62. }
  63. return ret;
  64. }
  65. static void __exit nf_nat_amanda_fini(void)
  66. {
  67. RCU_INIT_POINTER(nf_nat_amanda_hook, NULL);
  68. synchronize_rcu();
  69. }
  70. static int __init nf_nat_amanda_init(void)
  71. {
  72. BUG_ON(nf_nat_amanda_hook != NULL);
  73. RCU_INIT_POINTER(nf_nat_amanda_hook, help);
  74. return 0;
  75. }
  76. module_init(nf_nat_amanda_init);
  77. module_exit(nf_nat_amanda_fini);