Skip to content

Commit 37e82c2

Browse files
Alexei Starovoitovdavem330
authored andcommitted
bpf: allow BPF programs access skb->skb_iif and skb->dev->ifindex fields
classic BPF already exposes skb->dev->ifindex via SKF_AD_IFINDEX extension. Allow eBPF program to access it as well. Note that classic aborts execution of the program if 'skb->dev == NULL' (which is inconvenient for program writers), whereas eBPF returns zero in such case. Also expose the 'skb_iif' field, since programs triggered by redirected packet need to known the original interface index. Summary: __skb->ifindex -> skb->dev->ifindex __skb->ingress_ifindex -> skb->skb_iif Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e01ec21 commit 37e82c2

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ struct __sk_buff {
236236
__u32 vlan_tci;
237237
__u32 vlan_proto;
238238
__u32 priority;
239+
__u32 ingress_ifindex;
240+
__u32 ifindex;
239241
};
240242

241243
#endif /* _UAPI__LINUX_BPF_H__ */

net/core/filter.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,24 @@ static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
14991499
offsetof(struct sk_buff, priority));
15001500
break;
15011501

1502+
case offsetof(struct __sk_buff, ingress_ifindex):
1503+
BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1504+
1505+
*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1506+
offsetof(struct sk_buff, skb_iif));
1507+
break;
1508+
1509+
case offsetof(struct __sk_buff, ifindex):
1510+
BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1511+
1512+
*insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1513+
dst_reg, src_reg,
1514+
offsetof(struct sk_buff, dev));
1515+
*insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1516+
*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1517+
offsetof(struct net_device, ifindex));
1518+
break;
1519+
15021520
case offsetof(struct __sk_buff, mark):
15031521
return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
15041522

0 commit comments

Comments
 (0)