Skip to content

Commit c1ea5d6

Browse files
Jiri Bencdavem330
authored andcommitted
ip_tunnels: add IPv6 addresses to ip_tunnel_key
Add the IPv6 addresses as an union with IPv4 ones. When using IPv4, the newly introduced padding after the IPv4 addresses needs to be zeroed out. Signed-off-by: Jiri Benc <jbenc@redhat.com> Acked-by: Thomas Graf <tgraf@suug.ch> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 376534a commit c1ea5d6

10 files changed

Lines changed: 48 additions & 32 deletions

File tree

‎drivers/net/vxlan.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,8 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
12761276
goto drop;
12771277

12781278
info = &tun_dst->u.tun_info;
1279-
info->key.ipv4_src = iph->saddr;
1280-
info->key.ipv4_dst = iph->daddr;
1279+
info->key.u.ipv4.src = iph->saddr;
1280+
info->key.u.ipv4.dst = iph->daddr;
12811281
info->key.ipv4_tos = iph->tos;
12821282
info->key.ipv4_ttl = iph->ttl;
12831283
info->key.tp_src = udp_hdr(skb)->source;
@@ -1925,7 +1925,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
19251925
dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
19261926
vni = be64_to_cpu(info->key.tun_id);
19271927
remote_ip.sin.sin_family = AF_INET;
1928-
remote_ip.sin.sin_addr.s_addr = info->key.ipv4_dst;
1928+
remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
19291929
dst = &remote_ip;
19301930
}
19311931

‎include/net/ip_tunnels.h‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@
2525
/* Used to memset ip_tunnel padding. */
2626
#define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst)
2727

28+
/* Used to memset ipv4 address padding. */
29+
#define IP_TUNNEL_KEY_IPV4_PAD offsetofend(struct ip_tunnel_key, u.ipv4.dst)
30+
#define IP_TUNNEL_KEY_IPV4_PAD_LEN \
31+
(FIELD_SIZEOF(struct ip_tunnel_key, u) - \
32+
FIELD_SIZEOF(struct ip_tunnel_key, u.ipv4))
33+
2834
struct ip_tunnel_key {
2935
__be64 tun_id;
30-
__be32 ipv4_src;
31-
__be32 ipv4_dst;
36+
union {
37+
struct {
38+
__be32 src;
39+
__be32 dst;
40+
} ipv4;
41+
struct {
42+
struct in6_addr src;
43+
struct in6_addr dst;
44+
} ipv6;
45+
} u;
3246
__be16 tun_flags;
3347
u8 ipv4_tos;
3448
u8 ipv4_ttl;
@@ -177,8 +191,10 @@ static inline void __ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
177191
const void *opts, u8 opts_len)
178192
{
179193
tun_info->key.tun_id = tun_id;
180-
tun_info->key.ipv4_src = saddr;
181-
tun_info->key.ipv4_dst = daddr;
194+
tun_info->key.u.ipv4.src = saddr;
195+
tun_info->key.u.ipv4.dst = daddr;
196+
memset((unsigned char *)&tun_info->key + IP_TUNNEL_KEY_IPV4_PAD,
197+
0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
182198
tun_info->key.ipv4_tos = tos;
183199
tun_info->key.ipv4_ttl = ttl;
184200
tun_info->key.tun_flags = tun_flags;

‎net/core/filter.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
14951495
return -EINVAL;
14961496

14971497
to->tunnel_id = be64_to_cpu(info->key.tun_id);
1498-
to->remote_ipv4 = be32_to_cpu(info->key.ipv4_src);
1498+
to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
14991499

15001500
return 0;
15011501
}
@@ -1529,7 +1529,7 @@ static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
15291529
info = &md->u.tun_info;
15301530
info->mode = IP_TUNNEL_INFO_TX;
15311531
info->key.tun_id = cpu_to_be64(from->tunnel_id);
1532-
info->key.ipv4_dst = cpu_to_be32(from->remote_ipv4);
1532+
info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
15331533

15341534
return 0;
15351535
}

‎net/ipv4/ip_gre.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
407407
return PACKET_REJECT;
408408

409409
info = &tun_dst->u.tun_info;
410-
info->key.ipv4_src = iph->saddr;
411-
info->key.ipv4_dst = iph->daddr;
410+
info->key.u.ipv4.src = iph->saddr;
411+
info->key.u.ipv4.dst = iph->daddr;
412412
info->key.ipv4_tos = iph->tos;
413413
info->key.ipv4_ttl = iph->ttl;
414414

@@ -527,8 +527,8 @@ static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev)
527527

528528
key = &tun_info->key;
529529
memset(&fl, 0, sizeof(fl));
530-
fl.daddr = key->ipv4_dst;
531-
fl.saddr = key->ipv4_src;
530+
fl.daddr = key->u.ipv4.dst;
531+
fl.saddr = key->u.ipv4.src;
532532
fl.flowi4_tos = RT_TOS(key->ipv4_tos);
533533
fl.flowi4_mark = skb->mark;
534534
fl.flowi4_proto = IPPROTO_GRE;
@@ -564,7 +564,7 @@ static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev)
564564

565565
df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
566566
err = iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
567-
key->ipv4_dst, IPPROTO_GRE,
567+
key->u.ipv4.dst, IPPROTO_GRE,
568568
key->ipv4_tos, key->ipv4_ttl, df, false);
569569
iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
570570
return;

‎net/ipv4/ip_tunnel_core.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr,
227227
tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP_ID]);
228228

229229
if (tb[LWTUNNEL_IP_DST])
230-
tun_info->key.ipv4_dst = nla_get_be32(tb[LWTUNNEL_IP_DST]);
230+
tun_info->key.u.ipv4.dst = nla_get_be32(tb[LWTUNNEL_IP_DST]);
231231

232232
if (tb[LWTUNNEL_IP_SRC])
233-
tun_info->key.ipv4_src = nla_get_be32(tb[LWTUNNEL_IP_SRC]);
233+
tun_info->key.u.ipv4.src = nla_get_be32(tb[LWTUNNEL_IP_SRC]);
234234

235235
if (tb[LWTUNNEL_IP_TTL])
236236
tun_info->key.ipv4_ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
@@ -262,8 +262,8 @@ static int ip_tun_fill_encap_info(struct sk_buff *skb,
262262
struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
263263

264264
if (nla_put_u64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id) ||
265-
nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.ipv4_dst) ||
266-
nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.ipv4_src) ||
265+
nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
266+
nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
267267
nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.ipv4_tos) ||
268268
nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ipv4_ttl) ||
269269
nla_put_u16(skb, LWTUNNEL_IP_SPORT, tun_info->key.tp_src) ||

‎net/openvswitch/flow_netlink.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,11 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
534534
tun_flags |= TUNNEL_KEY;
535535
break;
536536
case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
537-
SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
537+
SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
538538
nla_get_in_addr(a), is_mask);
539539
break;
540540
case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
541-
SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
541+
SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
542542
nla_get_in_addr(a), is_mask);
543543
break;
544544
case OVS_TUNNEL_KEY_ATTR_TOS:
@@ -609,7 +609,7 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
609609
}
610610

611611
if (!is_mask) {
612-
if (!match->key->tun_key.ipv4_dst) {
612+
if (!match->key->tun_key.u.ipv4.dst) {
613613
OVS_NLERR(log, "IPv4 tunnel dst address is zero");
614614
return -EINVAL;
615615
}
@@ -647,13 +647,13 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
647647
if (output->tun_flags & TUNNEL_KEY &&
648648
nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
649649
return -EMSGSIZE;
650-
if (output->ipv4_src &&
650+
if (output->u.ipv4.src &&
651651
nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
652-
output->ipv4_src))
652+
output->u.ipv4.src))
653653
return -EMSGSIZE;
654-
if (output->ipv4_dst &&
654+
if (output->u.ipv4.dst &&
655655
nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
656-
output->ipv4_dst))
656+
output->u.ipv4.dst))
657657
return -EMSGSIZE;
658658
if (output->ipv4_tos &&
659659
nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
@@ -1116,7 +1116,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
11161116
/* The userspace does not send tunnel attributes that
11171117
* are 0, but we should not wildcard them nonetheless.
11181118
*/
1119-
if (match->key->tun_key.ipv4_dst)
1119+
if (match->key->tun_key.u.ipv4.dst)
11201120
SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
11211121
0xff, true);
11221122

@@ -1287,7 +1287,7 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
12871287
if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
12881288
goto nla_put_failure;
12891289

1290-
if ((swkey->tun_key.ipv4_dst || is_mask)) {
1290+
if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
12911291
const void *opts = NULL;
12921292

12931293
if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)

‎net/openvswitch/flow_table.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static u32 flow_hash(const struct sw_flow_key *key,
426426

427427
static int flow_key_start(const struct sw_flow_key *key)
428428
{
429-
if (key->tun_key.ipv4_dst)
429+
if (key->tun_key.u.ipv4.dst)
430430
return 0;
431431
else
432432
return rounddown(offsetof(struct sw_flow_key, phy),

‎net/openvswitch/vport-geneve.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
203203
}
204204

205205
err = geneve_xmit_skb(geneve_port->gs, rt, skb, fl.saddr,
206-
tun_key->ipv4_dst, tun_key->ipv4_tos,
206+
tun_key->u.ipv4.dst, tun_key->ipv4_tos,
207207
tun_key->ipv4_ttl, df, sport, dport,
208208
tun_key->tun_flags, vni, opts_len, opts,
209209
!!(tun_key->tun_flags & TUNNEL_CSUM), false);

‎net/openvswitch/vport.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ int ovs_tunnel_get_egress_info(struct ip_tunnel_info *egress_tun_info,
603603
* saddr, tp_src and tp_dst
604604
*/
605605
__ip_tunnel_info_init(egress_tun_info,
606-
fl.saddr, tun_key->ipv4_dst,
606+
fl.saddr, tun_key->u.ipv4.dst,
607607
tun_key->ipv4_tos,
608608
tun_key->ipv4_ttl,
609609
tp_src, tp_dst,

‎net/openvswitch/vport.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ static inline struct rtable *ovs_tunnel_route_lookup(struct net *net,
254254
struct rtable *rt;
255255

256256
memset(fl, 0, sizeof(*fl));
257-
fl->daddr = key->ipv4_dst;
258-
fl->saddr = key->ipv4_src;
257+
fl->daddr = key->u.ipv4.dst;
258+
fl->saddr = key->u.ipv4.src;
259259
fl->flowi4_tos = RT_TOS(key->ipv4_tos);
260260
fl->flowi4_mark = mark;
261261
fl->flowi4_proto = protocol;

0 commit comments

Comments
 (0)