nft_reject_ipv4.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  4. * Copyright (c) 2013 Eric Leblond <eric@regit.org>
  5. *
  6. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/netlink.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables.h>
  15. #include <net/netfilter/ipv4/nf_reject.h>
  16. #include <net/netfilter/nft_reject.h>
  17. static void nft_reject_ipv4_eval(const struct nft_expr *expr,
  18. struct nft_regs *regs,
  19. const struct nft_pktinfo *pkt)
  20. {
  21. struct nft_reject *priv = nft_expr_priv(expr);
  22. switch (priv->type) {
  23. case NFT_REJECT_ICMP_UNREACH:
  24. nf_send_unreach(pkt->skb, priv->icmp_code, nft_hook(pkt));
  25. break;
  26. case NFT_REJECT_TCP_RST:
  27. nf_send_reset(nft_net(pkt), pkt->skb, nft_hook(pkt));
  28. break;
  29. default:
  30. break;
  31. }
  32. regs->verdict.code = NF_DROP;
  33. }
  34. static struct nft_expr_type nft_reject_ipv4_type;
  35. static const struct nft_expr_ops nft_reject_ipv4_ops = {
  36. .type = &nft_reject_ipv4_type,
  37. .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
  38. .eval = nft_reject_ipv4_eval,
  39. .init = nft_reject_init,
  40. .dump = nft_reject_dump,
  41. .validate = nft_reject_validate,
  42. };
  43. static struct nft_expr_type nft_reject_ipv4_type __read_mostly = {
  44. .family = NFPROTO_IPV4,
  45. .name = "reject",
  46. .ops = &nft_reject_ipv4_ops,
  47. .policy = nft_reject_policy,
  48. .maxattr = NFTA_REJECT_MAX,
  49. .owner = THIS_MODULE,
  50. };
  51. static int __init nft_reject_ipv4_module_init(void)
  52. {
  53. return nft_register_expr(&nft_reject_ipv4_type);
  54. }
  55. static void __exit nft_reject_ipv4_module_exit(void)
  56. {
  57. nft_unregister_expr(&nft_reject_ipv4_type);
  58. }
  59. module_init(nft_reject_ipv4_module_init);
  60. module_exit(nft_reject_ipv4_module_exit);
  61. MODULE_LICENSE("GPL");
  62. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  63. MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "reject");
  64. MODULE_DESCRIPTION("IPv4 packet rejection for nftables");