ip6t_rpfilter.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/route.h>
  13. #include <net/ip6_fib.h>
  14. #include <net/ip6_route.h>
  15. #include <linux/netfilter/xt_rpfilter.h>
  16. #include <linux/netfilter/x_tables.h>
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  19. MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
  20. static bool rpfilter_addr_unicast(const struct in6_addr *addr)
  21. {
  22. int addr_type = ipv6_addr_type(addr);
  23. return addr_type & IPV6_ADDR_UNICAST;
  24. }
  25. static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
  26. const struct net_device *dev, u8 flags)
  27. {
  28. struct rt6_info *rt;
  29. struct ipv6hdr *iph = ipv6_hdr(skb);
  30. bool ret = false;
  31. struct flowi6 fl6 = {
  32. .flowi6_iif = LOOPBACK_IFINDEX,
  33. .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
  34. .flowi6_proto = iph->nexthdr,
  35. .daddr = iph->saddr,
  36. };
  37. int lookup_flags;
  38. if (rpfilter_addr_unicast(&iph->daddr)) {
  39. memcpy(&fl6.saddr, &iph->daddr, sizeof(struct in6_addr));
  40. lookup_flags = RT6_LOOKUP_F_HAS_SADDR;
  41. } else {
  42. lookup_flags = 0;
  43. }
  44. fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
  45. if ((flags & XT_RPFILTER_LOOSE) == 0) {
  46. fl6.flowi6_oif = dev->ifindex;
  47. lookup_flags |= RT6_LOOKUP_F_IFACE;
  48. }
  49. rt = (void *) ip6_route_lookup(net, &fl6, lookup_flags);
  50. if (rt->dst.error)
  51. goto out;
  52. if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
  53. goto out;
  54. if (rt->rt6i_flags & RTF_LOCAL) {
  55. ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
  56. goto out;
  57. }
  58. if (rt->rt6i_idev->dev == dev || (flags & XT_RPFILTER_LOOSE))
  59. ret = true;
  60. out:
  61. ip6_rt_put(rt);
  62. return ret;
  63. }
  64. static bool rpfilter_is_local(const struct sk_buff *skb)
  65. {
  66. const struct rt6_info *rt = (const void *) skb_dst(skb);
  67. return rt && (rt->rt6i_flags & RTF_LOCAL);
  68. }
  69. static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
  70. {
  71. const struct xt_rpfilter_info *info = par->matchinfo;
  72. int saddrtype;
  73. struct ipv6hdr *iph;
  74. bool invert = info->flags & XT_RPFILTER_INVERT;
  75. if (rpfilter_is_local(skb))
  76. return true ^ invert;
  77. iph = ipv6_hdr(skb);
  78. saddrtype = ipv6_addr_type(&iph->saddr);
  79. if (unlikely(saddrtype == IPV6_ADDR_ANY))
  80. return true ^ invert; /* not routable: forward path will drop it */
  81. return rpfilter_lookup_reverse6(par->net, skb, par->in, info->flags) ^ invert;
  82. }
  83. static int rpfilter_check(const struct xt_mtchk_param *par)
  84. {
  85. const struct xt_rpfilter_info *info = par->matchinfo;
  86. unsigned int options = ~XT_RPFILTER_OPTION_MASK;
  87. if (info->flags & options) {
  88. pr_info("unknown options encountered");
  89. return -EINVAL;
  90. }
  91. if (strcmp(par->table, "mangle") != 0 &&
  92. strcmp(par->table, "raw") != 0) {
  93. pr_info("match only valid in the \'raw\' "
  94. "or \'mangle\' tables, not \'%s\'.\n", par->table);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. static struct xt_match rpfilter_mt_reg __read_mostly = {
  100. .name = "rpfilter",
  101. .family = NFPROTO_IPV6,
  102. .checkentry = rpfilter_check,
  103. .match = rpfilter_mt,
  104. .matchsize = sizeof(struct xt_rpfilter_info),
  105. .hooks = (1 << NF_INET_PRE_ROUTING),
  106. .me = THIS_MODULE
  107. };
  108. static int __init rpfilter_mt_init(void)
  109. {
  110. return xt_register_match(&rpfilter_mt_reg);
  111. }
  112. static void __exit rpfilter_mt_exit(void)
  113. {
  114. xt_unregister_match(&rpfilter_mt_reg);
  115. }
  116. module_init(rpfilter_mt_init);
  117. module_exit(rpfilter_mt_exit);