Commit 6443ebc3 authored by Liping Zhang's avatar Liping Zhang Committed by Pablo Neira Ayuso

netfilter: rpfilter: fix incorrect loopback packet judgment

Currently, we check the existing rtable in PREROUTING hook, if RTCF_LOCAL
is set, we assume that the packet is loopback.

But this assumption is incorrect, for example, a packet encapsulated
in ipsec transport mode was received and routed to local, after
decapsulation, it would be delivered to local again, and the rtable
was not dropped, so RTCF_LOCAL check would trigger. But actually, the
packet was not loopback.

So for these normal loopback packets, we can check whether the in device
is IFF_LOOPBACK or not. For these locally generated broadcast/multicast,
we can check whether the skb->pkt_type is PACKET_LOOPBACK or not.

Finally, there's a subtle difference between nft fib expr and xtables
rpfilter extension, user can add the following nft rule to do strict
rpfilter check:
  # nft add rule x y meta iif eth0 fib saddr . iif oif != eth0 drop

So when the packet is loopback, it's better to store the in device
instead of the LOOPBACK_IFINDEX, otherwise, after adding the above
nft rule, locally generated broad/multicast packets will be dropped
incorrectly.

Fixes: f83a7ea2 ("netfilter: xt_rpfilter: skip locally generated broadcast/multicast, too")
Fixes: f6d0cbcf ("netfilter: nf_tables: add fib expression")
Signed-off-by: default avatarLiping Zhang <zlpnobody@gmail.com>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent d7f5762c
...@@ -9,6 +9,12 @@ struct nft_fib { ...@@ -9,6 +9,12 @@ struct nft_fib {
extern const struct nla_policy nft_fib_policy[]; extern const struct nla_policy nft_fib_policy[];
static inline bool
nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in)
{
return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
}
int nft_fib_dump(struct sk_buff *skb, const struct nft_expr *expr); int nft_fib_dump(struct sk_buff *skb, const struct nft_expr *expr);
int nft_fib_init(const struct nft_ctx *ctx, const struct nft_expr *expr, int nft_fib_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
const struct nlattr * const tb[]); const struct nlattr * const tb[]);
......
...@@ -63,10 +63,10 @@ static bool rpfilter_lookup_reverse(struct net *net, struct flowi4 *fl4, ...@@ -63,10 +63,10 @@ static bool rpfilter_lookup_reverse(struct net *net, struct flowi4 *fl4,
return dev_match || flags & XT_RPFILTER_LOOSE; return dev_match || flags & XT_RPFILTER_LOOSE;
} }
static bool rpfilter_is_local(const struct sk_buff *skb) static bool
rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
{ {
const struct rtable *rt = skb_rtable(skb); return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
return rt && (rt->rt_flags & RTCF_LOCAL);
} }
static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par) static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
...@@ -79,7 +79,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par) ...@@ -79,7 +79,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
info = par->matchinfo; info = par->matchinfo;
invert = info->flags & XT_RPFILTER_INVERT; invert = info->flags & XT_RPFILTER_INVERT;
if (rpfilter_is_local(skb)) if (rpfilter_is_loopback(skb, xt_in(par)))
return true ^ invert; return true ^ invert;
iph = ip_hdr(skb); iph = ip_hdr(skb);
......
...@@ -26,13 +26,6 @@ static __be32 get_saddr(__be32 addr) ...@@ -26,13 +26,6 @@ static __be32 get_saddr(__be32 addr)
return addr; return addr;
} }
static bool fib4_is_local(const struct sk_buff *skb)
{
const struct rtable *rt = skb_rtable(skb);
return rt && (rt->rt_flags & RTCF_LOCAL);
}
#define DSCP_BITS 0xfc #define DSCP_BITS 0xfc
void nft_fib4_eval_type(const struct nft_expr *expr, struct nft_regs *regs, void nft_fib4_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
...@@ -95,8 +88,10 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, ...@@ -95,8 +88,10 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
else else
oif = NULL; oif = NULL;
if (nft_hook(pkt) == NF_INET_PRE_ROUTING && fib4_is_local(pkt->skb)) { if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX); nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
nft_fib_store_result(dest, priv->result, pkt,
nft_in(pkt)->ifindex);
return; return;
} }
...@@ -131,7 +126,7 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, ...@@ -131,7 +126,7 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
switch (res.type) { switch (res.type) {
case RTN_UNICAST: case RTN_UNICAST:
break; break;
case RTN_LOCAL: /* should not appear here, see fib4_is_local() above */ case RTN_LOCAL: /* Should not see RTN_LOCAL here */
return; return;
default: default:
break; break;
......
...@@ -72,10 +72,10 @@ static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb, ...@@ -72,10 +72,10 @@ static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
return ret; return ret;
} }
static bool rpfilter_is_local(const struct sk_buff *skb) static bool
rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
{ {
const struct rt6_info *rt = (const void *) skb_dst(skb); return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
return rt && (rt->rt6i_flags & RTF_LOCAL);
} }
static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par) static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
...@@ -85,7 +85,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par) ...@@ -85,7 +85,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
struct ipv6hdr *iph; struct ipv6hdr *iph;
bool invert = info->flags & XT_RPFILTER_INVERT; bool invert = info->flags & XT_RPFILTER_INVERT;
if (rpfilter_is_local(skb)) if (rpfilter_is_loopback(skb, xt_in(par)))
return true ^ invert; return true ^ invert;
iph = ipv6_hdr(skb); iph = ipv6_hdr(skb);
......
...@@ -18,13 +18,6 @@ ...@@ -18,13 +18,6 @@
#include <net/ip6_fib.h> #include <net/ip6_fib.h>
#include <net/ip6_route.h> #include <net/ip6_route.h>
static bool fib6_is_local(const struct sk_buff *skb)
{
const struct rt6_info *rt = (const void *)skb_dst(skb);
return rt && (rt->rt6i_flags & RTF_LOCAL);
}
static int get_ifindex(const struct net_device *dev) static int get_ifindex(const struct net_device *dev)
{ {
return dev ? dev->ifindex : 0; return dev ? dev->ifindex : 0;
...@@ -164,8 +157,10 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs, ...@@ -164,8 +157,10 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif); lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif);
if (nft_hook(pkt) == NF_INET_PRE_ROUTING && fib6_is_local(pkt->skb)) { if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX); nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
nft_fib_store_result(dest, priv->result, pkt,
nft_in(pkt)->ifindex);
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment