nft_reject.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3. * Copyright (c) 2013 Eric Leblond <eric@regit.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables.h>
  18. #include <net/netfilter/nft_reject.h>
  19. #include <linux/icmp.h>
  20. #include <linux/icmpv6.h>
  21. const struct nla_policy nft_reject_policy[NFTA_REJECT_MAX + 1] = {
  22. [NFTA_REJECT_TYPE] = { .type = NLA_U32 },
  23. [NFTA_REJECT_ICMP_CODE] = { .type = NLA_U8 },
  24. };
  25. EXPORT_SYMBOL_GPL(nft_reject_policy);
  26. int nft_reject_validate(const struct nft_ctx *ctx,
  27. const struct nft_expr *expr,
  28. const struct nft_data **data)
  29. {
  30. return nft_chain_validate_hooks(ctx->chain,
  31. (1 << NF_INET_LOCAL_IN) |
  32. (1 << NF_INET_FORWARD) |
  33. (1 << NF_INET_LOCAL_OUT));
  34. }
  35. EXPORT_SYMBOL_GPL(nft_reject_validate);
  36. int nft_reject_init(const struct nft_ctx *ctx,
  37. const struct nft_expr *expr,
  38. const struct nlattr * const tb[])
  39. {
  40. struct nft_reject *priv = nft_expr_priv(expr);
  41. int err;
  42. err = nft_reject_validate(ctx, expr, NULL);
  43. if (err < 0)
  44. return err;
  45. if (tb[NFTA_REJECT_TYPE] == NULL)
  46. return -EINVAL;
  47. priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
  48. switch (priv->type) {
  49. case NFT_REJECT_ICMP_UNREACH:
  50. if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
  51. return -EINVAL;
  52. priv->icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
  53. case NFT_REJECT_TCP_RST:
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(nft_reject_init);
  61. int nft_reject_dump(struct sk_buff *skb, const struct nft_expr *expr)
  62. {
  63. const struct nft_reject *priv = nft_expr_priv(expr);
  64. if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
  65. goto nla_put_failure;
  66. switch (priv->type) {
  67. case NFT_REJECT_ICMP_UNREACH:
  68. if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
  69. goto nla_put_failure;
  70. break;
  71. default:
  72. break;
  73. }
  74. return 0;
  75. nla_put_failure:
  76. return -1;
  77. }
  78. EXPORT_SYMBOL_GPL(nft_reject_dump);
  79. static u8 icmp_code_v4[NFT_REJECT_ICMPX_MAX + 1] = {
  80. [NFT_REJECT_ICMPX_NO_ROUTE] = ICMP_NET_UNREACH,
  81. [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMP_PORT_UNREACH,
  82. [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMP_HOST_UNREACH,
  83. [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMP_PKT_FILTERED,
  84. };
  85. int nft_reject_icmp_code(u8 code)
  86. {
  87. BUG_ON(code > NFT_REJECT_ICMPX_MAX);
  88. return icmp_code_v4[code];
  89. }
  90. EXPORT_SYMBOL_GPL(nft_reject_icmp_code);
  91. static u8 icmp_code_v6[NFT_REJECT_ICMPX_MAX + 1] = {
  92. [NFT_REJECT_ICMPX_NO_ROUTE] = ICMPV6_NOROUTE,
  93. [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMPV6_PORT_UNREACH,
  94. [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMPV6_ADDR_UNREACH,
  95. [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMPV6_ADM_PROHIBITED,
  96. };
  97. int nft_reject_icmpv6_code(u8 code)
  98. {
  99. BUG_ON(code > NFT_REJECT_ICMPX_MAX);
  100. return icmp_code_v6[code];
  101. }
  102. EXPORT_SYMBOL_GPL(nft_reject_icmpv6_code);
  103. MODULE_LICENSE("GPL");
  104. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");