ipt_rpfilter.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
  4. *
  5. * based on fib_frontend.c; Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/ip.h>
  12. #include <net/ip.h>
  13. #include <net/ip_fib.h>
  14. #include <net/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("iptables: ipv4 reverse path filter match");
  20. /* don't try to find route from mcast/bcast/zeronet */
  21. static __be32 rpfilter_get_saddr(__be32 addr)
  22. {
  23. if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
  24. ipv4_is_zeronet(addr))
  25. return 0;
  26. return addr;
  27. }
  28. static bool rpfilter_lookup_reverse(struct net *net, struct flowi4 *fl4,
  29. const struct net_device *dev, u8 flags)
  30. {
  31. struct fib_result res;
  32. int ret __maybe_unused;
  33. if (fib_lookup(net, fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
  34. return false;
  35. if (res.type != RTN_UNICAST) {
  36. if (res.type != RTN_LOCAL || !(flags & XT_RPFILTER_ACCEPT_LOCAL))
  37. return false;
  38. }
  39. return fib_info_nh_uses_dev(res.fi, dev) || flags & XT_RPFILTER_LOOSE;
  40. }
  41. static bool
  42. rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
  43. {
  44. return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
  45. }
  46. static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
  47. {
  48. const struct xt_rpfilter_info *info;
  49. const struct iphdr *iph;
  50. struct flowi4 flow;
  51. bool invert;
  52. info = par->matchinfo;
  53. invert = info->flags & XT_RPFILTER_INVERT;
  54. if (rpfilter_is_loopback(skb, xt_in(par)))
  55. return true ^ invert;
  56. iph = ip_hdr(skb);
  57. if (ipv4_is_zeronet(iph->saddr)) {
  58. if (ipv4_is_lbcast(iph->daddr) ||
  59. ipv4_is_local_multicast(iph->daddr))
  60. return true ^ invert;
  61. }
  62. memset(&flow, 0, sizeof(flow));
  63. flow.flowi4_iif = LOOPBACK_IFINDEX;
  64. flow.daddr = iph->saddr;
  65. flow.saddr = rpfilter_get_saddr(iph->daddr);
  66. flow.flowi4_mark = info->flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
  67. flow.flowi4_tos = iph->tos & IPTOS_RT_MASK;
  68. flow.flowi4_scope = RT_SCOPE_UNIVERSE;
  69. flow.flowi4_oif = l3mdev_master_ifindex_rcu(xt_in(par));
  70. return rpfilter_lookup_reverse(xt_net(par), &flow, xt_in(par), info->flags) ^ invert;
  71. }
  72. static int rpfilter_check(const struct xt_mtchk_param *par)
  73. {
  74. const struct xt_rpfilter_info *info = par->matchinfo;
  75. unsigned int options = ~XT_RPFILTER_OPTION_MASK;
  76. if (info->flags & options) {
  77. pr_info_ratelimited("unknown options\n");
  78. return -EINVAL;
  79. }
  80. if (strcmp(par->table, "mangle") != 0 &&
  81. strcmp(par->table, "raw") != 0) {
  82. pr_info_ratelimited("only valid in \'raw\' or \'mangle\' table, not \'%s\'\n",
  83. par->table);
  84. return -EINVAL;
  85. }
  86. return 0;
  87. }
  88. static struct xt_match rpfilter_mt_reg __read_mostly = {
  89. .name = "rpfilter",
  90. .family = NFPROTO_IPV4,
  91. .checkentry = rpfilter_check,
  92. .match = rpfilter_mt,
  93. .matchsize = sizeof(struct xt_rpfilter_info),
  94. .hooks = (1 << NF_INET_PRE_ROUTING),
  95. .me = THIS_MODULE
  96. };
  97. static int __init rpfilter_mt_init(void)
  98. {
  99. return xt_register_match(&rpfilter_mt_reg);
  100. }
  101. static void __exit rpfilter_mt_exit(void)
  102. {
  103. xt_unregister_match(&rpfilter_mt_reg);
  104. }
  105. module_init(rpfilter_mt_init);
  106. module_exit(rpfilter_mt_exit);