nf_nat_tftp.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
  3. */
  4. #include <linux/module.h>
  5. #include <linux/udp.h>
  6. #include <net/netfilter/nf_conntrack_helper.h>
  7. #include <net/netfilter/nf_conntrack_expect.h>
  8. #include <net/netfilter/nf_nat_helper.h>
  9. #include <linux/netfilter/nf_conntrack_tftp.h>
  10. #define NAT_HELPER_NAME "tftp"
  11. MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
  12. MODULE_DESCRIPTION("TFTP NAT helper");
  13. MODULE_LICENSE("GPL");
  14. MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
  15. static struct nf_conntrack_nat_helper nat_helper_tftp =
  16. NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
  17. static unsigned int help(struct sk_buff *skb,
  18. enum ip_conntrack_info ctinfo,
  19. struct nf_conntrack_expect *exp)
  20. {
  21. const struct nf_conn *ct = exp->master;
  22. exp->saved_proto.udp.port
  23. = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
  24. exp->dir = IP_CT_DIR_REPLY;
  25. exp->expectfn = nf_nat_follow_master;
  26. if (nf_ct_expect_related(exp, 0) != 0) {
  27. nf_ct_helper_log(skb, exp->master, "cannot add expectation");
  28. return NF_DROP;
  29. }
  30. return NF_ACCEPT;
  31. }
  32. static void __exit nf_nat_tftp_fini(void)
  33. {
  34. nf_nat_helper_unregister(&nat_helper_tftp);
  35. RCU_INIT_POINTER(nf_nat_tftp_hook, NULL);
  36. synchronize_rcu();
  37. }
  38. static int __init nf_nat_tftp_init(void)
  39. {
  40. BUG_ON(nf_nat_tftp_hook != NULL);
  41. nf_nat_helper_register(&nat_helper_tftp);
  42. RCU_INIT_POINTER(nf_nat_tftp_hook, help);
  43. return 0;
  44. }
  45. module_init(nf_nat_tftp_init);
  46. module_exit(nf_nat_tftp_fini);