ipt_REJECT.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This is a module which is used for rejecting packets.
  4. */
  5. /* (C) 1999-2001 Paul `Rusty' Russell
  6. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/slab.h>
  12. #include <linux/ip.h>
  13. #include <linux/udp.h>
  14. #include <linux/icmp.h>
  15. #include <net/icmp.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter_ipv4/ip_tables.h>
  18. #include <linux/netfilter_ipv4/ipt_REJECT.h>
  19. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  20. #include <linux/netfilter_bridge.h>
  21. #endif
  22. #include <net/netfilter/ipv4/nf_reject.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  25. MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
  26. static unsigned int
  27. reject_tg(struct sk_buff *skb, const struct xt_action_param *par)
  28. {
  29. const struct ipt_reject_info *reject = par->targinfo;
  30. int hook = xt_hooknum(par);
  31. switch (reject->with) {
  32. case IPT_ICMP_NET_UNREACHABLE:
  33. nf_send_unreach(skb, ICMP_NET_UNREACH, hook);
  34. break;
  35. case IPT_ICMP_HOST_UNREACHABLE:
  36. nf_send_unreach(skb, ICMP_HOST_UNREACH, hook);
  37. break;
  38. case IPT_ICMP_PROT_UNREACHABLE:
  39. nf_send_unreach(skb, ICMP_PROT_UNREACH, hook);
  40. break;
  41. case IPT_ICMP_PORT_UNREACHABLE:
  42. nf_send_unreach(skb, ICMP_PORT_UNREACH, hook);
  43. break;
  44. case IPT_ICMP_NET_PROHIBITED:
  45. nf_send_unreach(skb, ICMP_NET_ANO, hook);
  46. break;
  47. case IPT_ICMP_HOST_PROHIBITED:
  48. nf_send_unreach(skb, ICMP_HOST_ANO, hook);
  49. break;
  50. case IPT_ICMP_ADMIN_PROHIBITED:
  51. nf_send_unreach(skb, ICMP_PKT_FILTERED, hook);
  52. break;
  53. case IPT_TCP_RESET:
  54. nf_send_reset(xt_net(par), skb, hook);
  55. case IPT_ICMP_ECHOREPLY:
  56. /* Doesn't happen. */
  57. break;
  58. }
  59. return NF_DROP;
  60. }
  61. static int reject_tg_check(const struct xt_tgchk_param *par)
  62. {
  63. const struct ipt_reject_info *rejinfo = par->targinfo;
  64. const struct ipt_entry *e = par->entryinfo;
  65. if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
  66. pr_info_ratelimited("ECHOREPLY no longer supported.\n");
  67. return -EINVAL;
  68. } else if (rejinfo->with == IPT_TCP_RESET) {
  69. /* Must specify that it's a TCP packet */
  70. if (e->ip.proto != IPPROTO_TCP ||
  71. (e->ip.invflags & XT_INV_PROTO)) {
  72. pr_info_ratelimited("TCP_RESET invalid for non-tcp\n");
  73. return -EINVAL;
  74. }
  75. }
  76. return 0;
  77. }
  78. static struct xt_target reject_tg_reg __read_mostly = {
  79. .name = "REJECT",
  80. .family = NFPROTO_IPV4,
  81. .target = reject_tg,
  82. .targetsize = sizeof(struct ipt_reject_info),
  83. .table = "filter",
  84. .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
  85. (1 << NF_INET_LOCAL_OUT),
  86. .checkentry = reject_tg_check,
  87. .me = THIS_MODULE,
  88. };
  89. static int __init reject_tg_init(void)
  90. {
  91. return xt_register_target(&reject_tg_reg);
  92. }
  93. static void __exit reject_tg_exit(void)
  94. {
  95. xt_unregister_target(&reject_tg_reg);
  96. }
  97. module_init(reject_tg_init);
  98. module_exit(reject_tg_exit);