nft_reject.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_init(const struct nft_ctx *ctx,
  27. const struct nft_expr *expr,
  28. const struct nlattr * const tb[])
  29. {
  30. struct nft_reject *priv = nft_expr_priv(expr);
  31. if (tb[NFTA_REJECT_TYPE] == NULL)
  32. return -EINVAL;
  33. priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
  34. switch (priv->type) {
  35. case NFT_REJECT_ICMP_UNREACH:
  36. if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
  37. return -EINVAL;
  38. priv->icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
  39. case NFT_REJECT_TCP_RST:
  40. break;
  41. default:
  42. return -EINVAL;
  43. }
  44. return 0;
  45. }
  46. EXPORT_SYMBOL_GPL(nft_reject_init);
  47. int nft_reject_dump(struct sk_buff *skb, const struct nft_expr *expr)
  48. {
  49. const struct nft_reject *priv = nft_expr_priv(expr);
  50. if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
  51. goto nla_put_failure;
  52. switch (priv->type) {
  53. case NFT_REJECT_ICMP_UNREACH:
  54. if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
  55. goto nla_put_failure;
  56. break;
  57. default:
  58. break;
  59. }
  60. return 0;
  61. nla_put_failure:
  62. return -1;
  63. }
  64. EXPORT_SYMBOL_GPL(nft_reject_dump);
  65. static u8 icmp_code_v4[NFT_REJECT_ICMPX_MAX + 1] = {
  66. [NFT_REJECT_ICMPX_NO_ROUTE] = ICMP_NET_UNREACH,
  67. [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMP_PORT_UNREACH,
  68. [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMP_HOST_UNREACH,
  69. [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMP_PKT_FILTERED,
  70. };
  71. int nft_reject_icmp_code(u8 code)
  72. {
  73. BUG_ON(code > NFT_REJECT_ICMPX_MAX);
  74. return icmp_code_v4[code];
  75. }
  76. EXPORT_SYMBOL_GPL(nft_reject_icmp_code);
  77. static u8 icmp_code_v6[NFT_REJECT_ICMPX_MAX + 1] = {
  78. [NFT_REJECT_ICMPX_NO_ROUTE] = ICMPV6_NOROUTE,
  79. [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMPV6_PORT_UNREACH,
  80. [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMPV6_ADDR_UNREACH,
  81. [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMPV6_ADM_PROHIBITED,
  82. };
  83. int nft_reject_icmpv6_code(u8 code)
  84. {
  85. BUG_ON(code > NFT_REJECT_ICMPX_MAX);
  86. return icmp_code_v6[code];
  87. }
  88. EXPORT_SYMBOL_GPL(nft_reject_icmpv6_code);
  89. MODULE_LICENSE("GPL");
  90. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");