ipt_rpfilter.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * based on fib_frontend.c; Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/ip.h>
  15. #include <net/ip.h>
  16. #include <net/ip_fib.h>
  17. #include <net/route.h>
  18. #include <linux/netfilter/xt_rpfilter.h>
  19. #include <linux/netfilter/x_tables.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  22. MODULE_DESCRIPTION("iptables: ipv4 reverse path filter match");
  23. /* don't try to find route from mcast/bcast/zeronet */
  24. static __be32 rpfilter_get_saddr(__be32 addr)
  25. {
  26. if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
  27. ipv4_is_zeronet(addr))
  28. return 0;
  29. return addr;
  30. }
  31. static bool rpfilter_lookup_reverse(struct net *net, struct flowi4 *fl4,
  32. const struct net_device *dev, u8 flags)
  33. {
  34. struct fib_result res;
  35. bool dev_match;
  36. int ret __maybe_unused;
  37. if (fib_lookup(net, fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
  38. return false;
  39. if (res.type != RTN_UNICAST) {
  40. if (res.type != RTN_LOCAL || !(flags & XT_RPFILTER_ACCEPT_LOCAL))
  41. return false;
  42. }
  43. dev_match = false;
  44. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  45. for (ret = 0; ret < res.fi->fib_nhs; ret++) {
  46. struct fib_nh *nh = &res.fi->fib_nh[ret];
  47. if (nh->nh_dev == dev) {
  48. dev_match = true;
  49. break;
  50. }
  51. }
  52. #else
  53. if (FIB_RES_DEV(res) == dev)
  54. dev_match = true;
  55. #endif
  56. return dev_match || flags & XT_RPFILTER_LOOSE;
  57. }
  58. static bool rpfilter_is_local(const struct sk_buff *skb)
  59. {
  60. const struct rtable *rt = skb_rtable(skb);
  61. return rt && (rt->rt_flags & RTCF_LOCAL);
  62. }
  63. static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
  64. {
  65. const struct xt_rpfilter_info *info;
  66. const struct iphdr *iph;
  67. struct flowi4 flow;
  68. bool invert;
  69. info = par->matchinfo;
  70. invert = info->flags & XT_RPFILTER_INVERT;
  71. if (rpfilter_is_local(skb))
  72. return true ^ invert;
  73. iph = ip_hdr(skb);
  74. if (ipv4_is_multicast(iph->daddr)) {
  75. if (ipv4_is_zeronet(iph->saddr))
  76. return ipv4_is_local_multicast(iph->daddr) ^ invert;
  77. }
  78. flow.flowi4_iif = LOOPBACK_IFINDEX;
  79. flow.daddr = iph->saddr;
  80. flow.saddr = rpfilter_get_saddr(iph->daddr);
  81. flow.flowi4_oif = 0;
  82. flow.flowi4_mark = info->flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
  83. flow.flowi4_tos = RT_TOS(iph->tos);
  84. flow.flowi4_scope = RT_SCOPE_UNIVERSE;
  85. return rpfilter_lookup_reverse(par->net, &flow, par->in, info->flags) ^ invert;
  86. }
  87. static int rpfilter_check(const struct xt_mtchk_param *par)
  88. {
  89. const struct xt_rpfilter_info *info = par->matchinfo;
  90. unsigned int options = ~XT_RPFILTER_OPTION_MASK;
  91. if (info->flags & options) {
  92. pr_info("unknown options encountered");
  93. return -EINVAL;
  94. }
  95. if (strcmp(par->table, "mangle") != 0 &&
  96. strcmp(par->table, "raw") != 0) {
  97. pr_info("match only valid in the \'raw\' "
  98. "or \'mangle\' tables, not \'%s\'.\n", par->table);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static struct xt_match rpfilter_mt_reg __read_mostly = {
  104. .name = "rpfilter",
  105. .family = NFPROTO_IPV4,
  106. .checkentry = rpfilter_check,
  107. .match = rpfilter_mt,
  108. .matchsize = sizeof(struct xt_rpfilter_info),
  109. .hooks = (1 << NF_INET_PRE_ROUTING),
  110. .me = THIS_MODULE
  111. };
  112. static int __init rpfilter_mt_init(void)
  113. {
  114. return xt_register_match(&rpfilter_mt_reg);
  115. }
  116. static void __exit rpfilter_mt_exit(void)
  117. {
  118. xt_unregister_match(&rpfilter_mt_reg);
  119. }
  120. module_init(rpfilter_mt_init);
  121. module_exit(rpfilter_mt_exit);