Skip to content

Commit fca5fdf

Browse files
borkmanndavem330
authored andcommitted
ip_tunnels, bpf: define IP_TUNNEL_OPTS_MAX and use it
eBPF defines this as BPF_TUNLEN_MAX and OVS just uses the hard-coded value inside struct sw_flow_key. Thus, add and use IP_TUNNEL_OPTS_MAX for this, which makes the code a bit more generic and allows to remove BPF_TUNLEN_MAX from eBPF code. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 808c1b6 commit fca5fdf

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

include/net/ip_tunnels.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <linux/socket.h>
88
#include <linux/types.h>
99
#include <linux/u64_stats_sync.h>
10+
#include <linux/bitops.h>
11+
1012
#include <net/dsfield.h>
1113
#include <net/gro_cells.h>
1214
#include <net/inet_ecn.h>
@@ -57,6 +59,11 @@ struct ip_tunnel_key {
5759
#define IP_TUNNEL_INFO_TX 0x01 /* represents tx tunnel parameters */
5860
#define IP_TUNNEL_INFO_IPV6 0x02 /* key contains IPv6 addresses */
5961

62+
/* Maximum tunnel options length. */
63+
#define IP_TUNNEL_OPTS_MAX \
64+
GENMASK((FIELD_SIZEOF(struct ip_tunnel_info, \
65+
options_len) * BITS_PER_BYTE) - 1, 0)
66+
6067
struct ip_tunnel_info {
6168
struct ip_tunnel_key key;
6269
#ifdef CONFIG_DST_CACHE

net/core/filter.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,8 +1904,6 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
19041904
.arg4_type = ARG_ANYTHING,
19051905
};
19061906

1907-
#define BPF_TUNLEN_MAX 255
1908-
19091907
static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
19101908
{
19111909
struct sk_buff *skb = (struct sk_buff *) (long) r1;
@@ -1915,7 +1913,7 @@ static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
19151913

19161914
if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
19171915
return -EINVAL;
1918-
if (unlikely(size > BPF_TUNLEN_MAX))
1916+
if (unlikely(size > IP_TUNNEL_OPTS_MAX))
19191917
return -ENOMEM;
19201918

19211919
ip_tunnel_info_opts_set(info, from, size);
@@ -1936,13 +1934,10 @@ static const struct bpf_func_proto *
19361934
bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
19371935
{
19381936
if (!md_dst) {
1939-
BUILD_BUG_ON(FIELD_SIZEOF(struct ip_tunnel_info,
1940-
options_len) != 1);
1941-
19421937
/* Race is not possible, since it's called from verifier
19431938
* that is holding verifier mutex.
19441939
*/
1945-
md_dst = metadata_dst_alloc_percpu(BPF_TUNLEN_MAX,
1940+
md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
19461941
GFP_KERNEL);
19471942
if (!md_dst)
19481943
return NULL;

net/ipv4/ip_tunnel_core.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
398398

399399
void __init ip_tunnel_core_init(void)
400400
{
401+
/* If you land here, make sure whether increasing ip_tunnel_info's
402+
* options_len is a reasonable choice with its usage in front ends
403+
* (f.e., it's part of flow keys, etc).
404+
*/
405+
BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
406+
401407
lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
402408
lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
403409
}

net/openvswitch/flow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct ovs_tunnel_info {
5555
FIELD_SIZEOF(struct sw_flow_key, recirc_id))
5656

5757
struct sw_flow_key {
58-
u8 tun_opts[255];
58+
u8 tun_opts[IP_TUNNEL_OPTS_MAX];
5959
u8 tun_opts_len;
6060
struct ip_tunnel_key tun_key; /* Encapsulating tunnel key. */
6161
struct {

0 commit comments

Comments
 (0)