Skip to content

Commit c9e1673

Browse files
kaberummakynes
authored andcommitted
netfilter: ipv4: propagate routing errors from ip_route_me_harder()
Propagate routing errors from ip_route_me_harder() when dropping a packet using NF_DROP_ERR(). This makes userspace get the proper error instead of EPERM for everything. Example: # ip r a unreachable default table 100 # ip ru add fwmark 0x1 lookup 100 # iptables -t mangle -A OUTPUT -d 8.8.8.8 -j MARK --set-mark 0x1 Current behaviour: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. ping: sendmsg: Operation not permitted ping: sendmsg: Operation not permitted ping: sendmsg: Operation not permitted New behaviour: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. ping: sendmsg: Network is unreachable ping: sendmsg: Network is unreachable ping: sendmsg: Network is unreachable ping: sendmsg: Network is unreachable Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 6b0ee8c commit c9e1673

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎net/ipv4/netfilter.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned int addr_type)
4040
fl4.flowi4_flags = flags;
4141
rt = ip_route_output_key(net, &fl4);
4242
if (IS_ERR(rt))
43-
return -1;
43+
return PTR_ERR(rt);
4444

4545
/* Drop old route. */
4646
skb_dst_drop(skb);
4747
skb_dst_set(skb, &rt->dst);
4848

4949
if (skb_dst(skb)->error)
50-
return -1;
50+
return skb_dst(skb)->error;
5151

5252
#ifdef CONFIG_XFRM
5353
if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
@@ -56,7 +56,7 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned int addr_type)
5656
skb_dst_set(skb, NULL);
5757
dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), skb->sk, 0);
5858
if (IS_ERR(dst))
59-
return -1;
59+
return PTR_ERR(dst);;
6060
skb_dst_set(skb, dst);
6161
}
6262
#endif
@@ -66,7 +66,7 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned int addr_type)
6666
if (skb_headroom(skb) < hh_len &&
6767
pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
6868
0, GFP_ATOMIC))
69-
return -1;
69+
return -ENOMEM;
7070

7171
return 0;
7272
}

‎net/ipv4/netfilter/iptable_mangle.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
4444
u_int8_t tos;
4545
__be32 saddr, daddr;
4646
u_int32_t mark;
47+
int err;
4748

4849
/* root is playing with raw sockets. */
4950
if (skb->len < sizeof(struct iphdr) ||
@@ -66,9 +67,11 @@ ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
6667
if (iph->saddr != saddr ||
6768
iph->daddr != daddr ||
6869
skb->mark != mark ||
69-
iph->tos != tos)
70-
if (ip_route_me_harder(skb, RTN_UNSPEC))
71-
ret = NF_DROP;
70+
iph->tos != tos) {
71+
err = ip_route_me_harder(skb, RTN_UNSPEC);
72+
if (err < 0)
73+
ret = NF_DROP_ERR(err);
74+
}
7275
}
7376

7477
return ret;

‎net/ipv4/netfilter/iptable_nat.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ nf_nat_ipv4_local_fn(unsigned int hooknum,
213213
const struct nf_conn *ct;
214214
enum ip_conntrack_info ctinfo;
215215
unsigned int ret;
216+
int err;
216217

217218
/* root is playing with raw sockets. */
218219
if (skb->len < sizeof(struct iphdr) ||
@@ -226,8 +227,9 @@ nf_nat_ipv4_local_fn(unsigned int hooknum,
226227

227228
if (ct->tuplehash[dir].tuple.dst.u3.ip !=
228229
ct->tuplehash[!dir].tuple.src.u3.ip) {
229-
if (ip_route_me_harder(skb, RTN_UNSPEC))
230-
ret = NF_DROP;
230+
err = ip_route_me_harder(skb, RTN_UNSPEC);
231+
if (err < 0)
232+
ret = NF_DROP_ERR(err);
231233
}
232234
#ifdef CONFIG_XFRM
233235
else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&

0 commit comments

Comments
 (0)