nft_reject_inet.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2014 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  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/nft_reject.h>
  16. #include <net/netfilter/ipv4/nf_reject.h>
  17. #include <net/netfilter/ipv6/nf_reject.h>
  18. static void nft_reject_inet_eval(const struct nft_expr *expr,
  19. struct nft_regs *regs,
  20. const struct nft_pktinfo *pkt)
  21. {
  22. struct nft_reject *priv = nft_expr_priv(expr);
  23. switch (pkt->pf) {
  24. case NFPROTO_IPV4:
  25. switch (priv->type) {
  26. case NFT_REJECT_ICMP_UNREACH:
  27. nf_send_unreach(pkt->skb, priv->icmp_code,
  28. pkt->hook);
  29. break;
  30. case NFT_REJECT_TCP_RST:
  31. nf_send_reset(pkt->net, pkt->skb, pkt->hook);
  32. break;
  33. case NFT_REJECT_ICMPX_UNREACH:
  34. nf_send_unreach(pkt->skb,
  35. nft_reject_icmp_code(priv->icmp_code),
  36. pkt->hook);
  37. break;
  38. }
  39. break;
  40. case NFPROTO_IPV6:
  41. switch (priv->type) {
  42. case NFT_REJECT_ICMP_UNREACH:
  43. nf_send_unreach6(pkt->net, pkt->skb, priv->icmp_code,
  44. pkt->hook);
  45. break;
  46. case NFT_REJECT_TCP_RST:
  47. nf_send_reset6(pkt->net, pkt->skb, pkt->hook);
  48. break;
  49. case NFT_REJECT_ICMPX_UNREACH:
  50. nf_send_unreach6(pkt->net, pkt->skb,
  51. nft_reject_icmpv6_code(priv->icmp_code),
  52. pkt->hook);
  53. break;
  54. }
  55. break;
  56. }
  57. regs->verdict.code = NF_DROP;
  58. }
  59. static int nft_reject_inet_init(const struct nft_ctx *ctx,
  60. const struct nft_expr *expr,
  61. const struct nlattr * const tb[])
  62. {
  63. struct nft_reject *priv = nft_expr_priv(expr);
  64. int icmp_code;
  65. if (tb[NFTA_REJECT_TYPE] == NULL)
  66. return -EINVAL;
  67. priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
  68. switch (priv->type) {
  69. case NFT_REJECT_ICMP_UNREACH:
  70. case NFT_REJECT_ICMPX_UNREACH:
  71. if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
  72. return -EINVAL;
  73. icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
  74. if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
  75. icmp_code > NFT_REJECT_ICMPX_MAX)
  76. return -EINVAL;
  77. priv->icmp_code = icmp_code;
  78. break;
  79. case NFT_REJECT_TCP_RST:
  80. break;
  81. default:
  82. return -EINVAL;
  83. }
  84. return 0;
  85. }
  86. static int nft_reject_inet_dump(struct sk_buff *skb,
  87. const struct nft_expr *expr)
  88. {
  89. const struct nft_reject *priv = nft_expr_priv(expr);
  90. if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
  91. goto nla_put_failure;
  92. switch (priv->type) {
  93. case NFT_REJECT_ICMP_UNREACH:
  94. case NFT_REJECT_ICMPX_UNREACH:
  95. if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
  96. goto nla_put_failure;
  97. break;
  98. default:
  99. break;
  100. }
  101. return 0;
  102. nla_put_failure:
  103. return -1;
  104. }
  105. static struct nft_expr_type nft_reject_inet_type;
  106. static const struct nft_expr_ops nft_reject_inet_ops = {
  107. .type = &nft_reject_inet_type,
  108. .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
  109. .eval = nft_reject_inet_eval,
  110. .init = nft_reject_inet_init,
  111. .dump = nft_reject_inet_dump,
  112. };
  113. static struct nft_expr_type nft_reject_inet_type __read_mostly = {
  114. .family = NFPROTO_INET,
  115. .name = "reject",
  116. .ops = &nft_reject_inet_ops,
  117. .policy = nft_reject_policy,
  118. .maxattr = NFTA_REJECT_MAX,
  119. .owner = THIS_MODULE,
  120. };
  121. static int __init nft_reject_inet_module_init(void)
  122. {
  123. return nft_register_expr(&nft_reject_inet_type);
  124. }
  125. static void __exit nft_reject_inet_module_exit(void)
  126. {
  127. nft_unregister_expr(&nft_reject_inet_type);
  128. }
  129. module_init(nft_reject_inet_module_init);
  130. module_exit(nft_reject_inet_module_exit);
  131. MODULE_LICENSE("GPL");
  132. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  133. MODULE_ALIAS_NFT_AF_EXPR(1, "reject");