ip6t_REJECT.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * IP6 tables REJECT target module
  3. * Linux INET6 implementation
  4. *
  5. * Copyright (C)2003 USAGI/WIDE Project
  6. *
  7. * Authors:
  8. * Yasuyuki Kozakai <yasuyuki.kozakai@toshiba.co.jp>
  9. *
  10. * Copyright (c) 2005-2007 Patrick McHardy <kaber@trash.net>
  11. *
  12. * Based on net/ipv4/netfilter/ipt_REJECT.c
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/gfp.h>
  21. #include <linux/module.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/icmpv6.h>
  24. #include <linux/netdevice.h>
  25. #include <net/icmp.h>
  26. #include <net/flow.h>
  27. #include <linux/netfilter/x_tables.h>
  28. #include <linux/netfilter_ipv6/ip6_tables.h>
  29. #include <linux/netfilter_ipv6/ip6t_REJECT.h>
  30. #include <net/netfilter/ipv6/nf_reject.h>
  31. MODULE_AUTHOR("Yasuyuki KOZAKAI <yasuyuki.kozakai@toshiba.co.jp>");
  32. MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv6");
  33. MODULE_LICENSE("GPL");
  34. static unsigned int
  35. reject_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  36. {
  37. const struct ip6t_reject_info *reject = par->targinfo;
  38. struct net *net = par->net;
  39. switch (reject->with) {
  40. case IP6T_ICMP6_NO_ROUTE:
  41. nf_send_unreach6(net, skb, ICMPV6_NOROUTE, par->hooknum);
  42. break;
  43. case IP6T_ICMP6_ADM_PROHIBITED:
  44. nf_send_unreach6(net, skb, ICMPV6_ADM_PROHIBITED, par->hooknum);
  45. break;
  46. case IP6T_ICMP6_NOT_NEIGHBOUR:
  47. nf_send_unreach6(net, skb, ICMPV6_NOT_NEIGHBOUR, par->hooknum);
  48. break;
  49. case IP6T_ICMP6_ADDR_UNREACH:
  50. nf_send_unreach6(net, skb, ICMPV6_ADDR_UNREACH, par->hooknum);
  51. break;
  52. case IP6T_ICMP6_PORT_UNREACH:
  53. nf_send_unreach6(net, skb, ICMPV6_PORT_UNREACH, par->hooknum);
  54. break;
  55. case IP6T_ICMP6_ECHOREPLY:
  56. /* Do nothing */
  57. break;
  58. case IP6T_TCP_RESET:
  59. nf_send_reset6(net, skb, par->hooknum);
  60. break;
  61. case IP6T_ICMP6_POLICY_FAIL:
  62. nf_send_unreach6(net, skb, ICMPV6_POLICY_FAIL, par->hooknum);
  63. break;
  64. case IP6T_ICMP6_REJECT_ROUTE:
  65. nf_send_unreach6(net, skb, ICMPV6_REJECT_ROUTE, par->hooknum);
  66. break;
  67. }
  68. return NF_DROP;
  69. }
  70. static int reject_tg6_check(const struct xt_tgchk_param *par)
  71. {
  72. const struct ip6t_reject_info *rejinfo = par->targinfo;
  73. const struct ip6t_entry *e = par->entryinfo;
  74. if (rejinfo->with == IP6T_ICMP6_ECHOREPLY) {
  75. pr_info("ECHOREPLY is not supported.\n");
  76. return -EINVAL;
  77. } else if (rejinfo->with == IP6T_TCP_RESET) {
  78. /* Must specify that it's a TCP packet */
  79. if (!(e->ipv6.flags & IP6T_F_PROTO) ||
  80. e->ipv6.proto != IPPROTO_TCP ||
  81. (e->ipv6.invflags & XT_INV_PROTO)) {
  82. pr_info("TCP_RESET illegal for non-tcp\n");
  83. return -EINVAL;
  84. }
  85. }
  86. return 0;
  87. }
  88. static struct xt_target reject_tg6_reg __read_mostly = {
  89. .name = "REJECT",
  90. .family = NFPROTO_IPV6,
  91. .target = reject_tg6,
  92. .targetsize = sizeof(struct ip6t_reject_info),
  93. .table = "filter",
  94. .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
  95. (1 << NF_INET_LOCAL_OUT),
  96. .checkentry = reject_tg6_check,
  97. .me = THIS_MODULE
  98. };
  99. static int __init reject_tg6_init(void)
  100. {
  101. return xt_register_target(&reject_tg6_reg);
  102. }
  103. static void __exit reject_tg6_exit(void)
  104. {
  105. xt_unregister_target(&reject_tg6_reg);
  106. }
  107. module_init(reject_tg6_init);
  108. module_exit(reject_tg6_exit);