Skip to content

Commit 8cfd23e

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nft_payload: work around vlan header stripping
make payload expression aware of the fact that VLAN offload may have removed a vlan header. When we encounter tagged skb, transparently insert the tag into the register so that vlan header matching can work without userspace being aware of offload features. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 5e8018f commit 8cfd23e

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

net/netfilter/nft_payload.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include <linux/kernel.h>
12+
#include <linux/if_vlan.h>
1213
#include <linux/init.h>
1314
#include <linux/module.h>
1415
#include <linux/netlink.h>
@@ -17,6 +18,53 @@
1718
#include <net/netfilter/nf_tables_core.h>
1819
#include <net/netfilter/nf_tables.h>
1920

21+
/* add vlan header into the user buffer for if tag was removed by offloads */
22+
static bool
23+
nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len)
24+
{
25+
int mac_off = skb_mac_header(skb) - skb->data;
26+
u8 vlan_len, *vlanh, *dst_u8 = (u8 *) d;
27+
struct vlan_ethhdr veth;
28+
29+
vlanh = (u8 *) &veth;
30+
if (offset < ETH_HLEN) {
31+
u8 ethlen = min_t(u8, len, ETH_HLEN - offset);
32+
33+
if (skb_copy_bits(skb, mac_off, &veth, ETH_HLEN))
34+
return false;
35+
36+
veth.h_vlan_proto = skb->vlan_proto;
37+
38+
memcpy(dst_u8, vlanh + offset, ethlen);
39+
40+
len -= ethlen;
41+
if (len == 0)
42+
return true;
43+
44+
dst_u8 += ethlen;
45+
offset = ETH_HLEN;
46+
} else if (offset >= VLAN_ETH_HLEN) {
47+
offset -= VLAN_HLEN;
48+
goto skip;
49+
}
50+
51+
veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
52+
veth.h_vlan_encapsulated_proto = skb->protocol;
53+
54+
vlanh += offset;
55+
56+
vlan_len = min_t(u8, len, VLAN_ETH_HLEN - offset);
57+
memcpy(dst_u8, vlanh, vlan_len);
58+
59+
len -= vlan_len;
60+
if (!len)
61+
return true;
62+
63+
dst_u8 += vlan_len;
64+
skip:
65+
return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0;
66+
}
67+
2068
static void nft_payload_eval(const struct nft_expr *expr,
2169
struct nft_regs *regs,
2270
const struct nft_pktinfo *pkt)
@@ -26,10 +74,18 @@ static void nft_payload_eval(const struct nft_expr *expr,
2674
u32 *dest = &regs->data[priv->dreg];
2775
int offset;
2876

77+
dest[priv->len / NFT_REG32_SIZE] = 0;
2978
switch (priv->base) {
3079
case NFT_PAYLOAD_LL_HEADER:
3180
if (!skb_mac_header_was_set(skb))
3281
goto err;
82+
83+
if (skb_vlan_tag_present(skb)) {
84+
if (!nft_payload_copy_vlan(dest, skb,
85+
priv->offset, priv->len))
86+
goto err;
87+
return;
88+
}
3389
offset = skb_mac_header(skb) - skb->data;
3490
break;
3591
case NFT_PAYLOAD_NETWORK_HEADER:
@@ -43,7 +99,6 @@ static void nft_payload_eval(const struct nft_expr *expr,
4399
}
44100
offset += priv->offset;
45101

46-
dest[priv->len / NFT_REG32_SIZE] = 0;
47102
if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
48103
goto err;
49104
return;

0 commit comments

Comments
 (0)