Skip to content

Commit 8d79266

Browse files
4astdavem330
authored andcommitted
ip6_tunnel: add collect_md mode to IPv6 tunnels
Similar to gre, vxlan, geneve tunnels allow IPIP6 and IP6IP6 tunnels to operate in 'collect metadata' mode. Unlike ipv4 code here it's possible to reuse ip6_tnl_xmit() function for both collect_md and traditional tunnels. bpf_skb_[gs]et_tunnel_key() helpers and ovs (in the future) are the users. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Thomas Graf <tgraf@suug.ch> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cfc7381 commit 8d79266

2 files changed

Lines changed: 134 additions & 45 deletions

File tree

‎include/net/ip6_tunnel.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct __ip6_tnl_parm {
2323
__u8 proto; /* tunnel protocol */
2424
__u8 encap_limit; /* encapsulation limit for tunnel */
2525
__u8 hop_limit; /* hop limit for tunnel */
26+
bool collect_md;
2627
__be32 flowinfo; /* traffic class and flowlabel for tunnel */
2728
__u32 flags; /* tunnel flags */
2829
struct in6_addr laddr; /* local tunnel end-point address */

‎net/ipv6/ip6_tunnel.c‎

Lines changed: 133 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <net/inet_ecn.h>
5858
#include <net/net_namespace.h>
5959
#include <net/netns/generic.h>
60+
#include <net/dst_metadata.h>
6061

6162
MODULE_AUTHOR("Ville Nuorvala");
6263
MODULE_DESCRIPTION("IPv6 tunneling device");
@@ -90,6 +91,7 @@ struct ip6_tnl_net {
9091
struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
9192
struct ip6_tnl __rcu *tnls_wc[1];
9293
struct ip6_tnl __rcu **tnls[2];
94+
struct ip6_tnl __rcu *collect_md_tun;
9395
};
9496

9597
static struct net_device_stats *ip6_get_stats(struct net_device *dev)
@@ -166,6 +168,10 @@ ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_
166168
return t;
167169
}
168170

171+
t = rcu_dereference(ip6n->collect_md_tun);
172+
if (t)
173+
return t;
174+
169175
t = rcu_dereference(ip6n->tnls_wc[0]);
170176
if (t && (t->dev->flags & IFF_UP))
171177
return t;
@@ -209,6 +215,8 @@ ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
209215
{
210216
struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
211217

218+
if (t->parms.collect_md)
219+
rcu_assign_pointer(ip6n->collect_md_tun, t);
212220
rcu_assign_pointer(t->next , rtnl_dereference(*tp));
213221
rcu_assign_pointer(*tp, t);
214222
}
@@ -224,6 +232,9 @@ ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
224232
struct ip6_tnl __rcu **tp;
225233
struct ip6_tnl *iter;
226234

235+
if (t->parms.collect_md)
236+
rcu_assign_pointer(ip6n->collect_md_tun, NULL);
237+
227238
for (tp = ip6_tnl_bucket(ip6n, &t->parms);
228239
(iter = rtnl_dereference(*tp)) != NULL;
229240
tp = &iter->next) {
@@ -829,6 +840,9 @@ static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
829840

830841
skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
831842

843+
if (tun_dst)
844+
skb_dst_set(skb, (struct dst_entry *)tun_dst);
845+
832846
gro_cells_receive(&tunnel->gro_cells, skb);
833847
return 0;
834848

@@ -865,6 +879,7 @@ static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
865879
{
866880
struct ip6_tnl *t;
867881
const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
882+
struct metadata_dst *tun_dst = NULL;
868883
int ret = -1;
869884

870885
rcu_read_lock();
@@ -881,7 +896,12 @@ static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
881896
goto drop;
882897
if (iptunnel_pull_header(skb, 0, tpi->proto, false))
883898
goto drop;
884-
ret = __ip6_tnl_rcv(t, skb, tpi, NULL, dscp_ecn_decapsulate,
899+
if (t->parms.collect_md) {
900+
tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
901+
if (!tun_dst)
902+
return 0;
903+
}
904+
ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
885905
log_ecn_error);
886906
}
887907

@@ -1012,8 +1032,16 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
10121032
int mtu;
10131033
unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
10141034
unsigned int max_headroom = psh_hlen;
1035+
u8 hop_limit;
10151036
int err = -1;
10161037

1038+
if (t->parms.collect_md) {
1039+
hop_limit = skb_tunnel_info(skb)->key.ttl;
1040+
goto route_lookup;
1041+
} else {
1042+
hop_limit = t->parms.hop_limit;
1043+
}
1044+
10171045
/* NBMA tunnel */
10181046
if (ipv6_addr_any(&t->parms.raddr)) {
10191047
struct in6_addr *addr6;
@@ -1043,6 +1071,7 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
10431071
goto tx_err_link_failure;
10441072

10451073
if (!dst) {
1074+
route_lookup:
10461075
dst = ip6_route_output(net, NULL, fl6);
10471076

10481077
if (dst->error)
@@ -1053,6 +1082,10 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
10531082
dst = NULL;
10541083
goto tx_err_link_failure;
10551084
}
1085+
if (t->parms.collect_md &&
1086+
ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
1087+
&fl6->daddr, 0, &fl6->saddr))
1088+
goto tx_err_link_failure;
10561089
ndst = dst;
10571090
}
10581091

@@ -1071,7 +1104,7 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
10711104
}
10721105
if (mtu < IPV6_MIN_MTU)
10731106
mtu = IPV6_MIN_MTU;
1074-
if (skb_dst(skb))
1107+
if (skb_dst(skb) && !t->parms.collect_md)
10751108
skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
10761109
if (skb->len > mtu && !skb_is_gso(skb)) {
10771110
*pmtu = mtu;
@@ -1111,8 +1144,13 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
11111144
skb = new_skb;
11121145
}
11131146

1114-
if (!fl6->flowi6_mark && ndst)
1115-
dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1147+
if (t->parms.collect_md) {
1148+
if (t->encap.type != TUNNEL_ENCAP_NONE)
1149+
goto tx_err_dst_release;
1150+
} else {
1151+
if (!fl6->flowi6_mark && ndst)
1152+
dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1153+
}
11161154
skb_dst_set(skb, dst);
11171155

11181156
if (encap_limit >= 0) {
@@ -1137,7 +1175,7 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
11371175
ipv6h = ipv6_hdr(skb);
11381176
ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
11391177
ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1140-
ipv6h->hop_limit = t->parms.hop_limit;
1178+
ipv6h->hop_limit = hop_limit;
11411179
ipv6h->nexthdr = proto;
11421180
ipv6h->saddr = fl6->saddr;
11431181
ipv6h->daddr = fl6->daddr;
@@ -1170,19 +1208,34 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
11701208
if (tproto != IPPROTO_IPIP && tproto != 0)
11711209
return -1;
11721210

1173-
if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1174-
encap_limit = t->parms.encap_limit;
1211+
dsfield = ipv4_get_dsfield(iph);
11751212

1176-
memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1177-
fl6.flowi6_proto = IPPROTO_IPIP;
1213+
if (t->parms.collect_md) {
1214+
struct ip_tunnel_info *tun_info;
1215+
const struct ip_tunnel_key *key;
11781216

1179-
dsfield = ipv4_get_dsfield(iph);
1217+
tun_info = skb_tunnel_info(skb);
1218+
if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1219+
ip_tunnel_info_af(tun_info) != AF_INET6))
1220+
return -1;
1221+
key = &tun_info->key;
1222+
memset(&fl6, 0, sizeof(fl6));
1223+
fl6.flowi6_proto = IPPROTO_IPIP;
1224+
fl6.daddr = key->u.ipv6.dst;
1225+
fl6.flowlabel = key->label;
1226+
} else {
1227+
if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1228+
encap_limit = t->parms.encap_limit;
11801229

1181-
if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1182-
fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1183-
& IPV6_TCLASS_MASK;
1184-
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1185-
fl6.flowi6_mark = skb->mark;
1230+
memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1231+
fl6.flowi6_proto = IPPROTO_IPIP;
1232+
1233+
if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1234+
fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1235+
& IPV6_TCLASS_MASK;
1236+
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1237+
fl6.flowi6_mark = skb->mark;
1238+
}
11861239

11871240
if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
11881241
return -1;
@@ -1220,29 +1273,47 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
12201273
ip6_tnl_addr_conflict(t, ipv6h))
12211274
return -1;
12221275

1223-
offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1224-
if (offset > 0) {
1225-
struct ipv6_tlv_tnl_enc_lim *tel;
1226-
tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1227-
if (tel->encap_limit == 0) {
1228-
icmpv6_send(skb, ICMPV6_PARAMPROB,
1229-
ICMPV6_HDR_FIELD, offset + 2);
1276+
dsfield = ipv6_get_dsfield(ipv6h);
1277+
1278+
if (t->parms.collect_md) {
1279+
struct ip_tunnel_info *tun_info;
1280+
const struct ip_tunnel_key *key;
1281+
1282+
tun_info = skb_tunnel_info(skb);
1283+
if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1284+
ip_tunnel_info_af(tun_info) != AF_INET6))
12301285
return -1;
1286+
key = &tun_info->key;
1287+
memset(&fl6, 0, sizeof(fl6));
1288+
fl6.flowi6_proto = IPPROTO_IPV6;
1289+
fl6.daddr = key->u.ipv6.dst;
1290+
fl6.flowlabel = key->label;
1291+
} else {
1292+
offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1293+
if (offset > 0) {
1294+
struct ipv6_tlv_tnl_enc_lim *tel;
1295+
1296+
tel = (void *)&skb_network_header(skb)[offset];
1297+
if (tel->encap_limit == 0) {
1298+
icmpv6_send(skb, ICMPV6_PARAMPROB,
1299+
ICMPV6_HDR_FIELD, offset + 2);
1300+
return -1;
1301+
}
1302+
encap_limit = tel->encap_limit - 1;
1303+
} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
1304+
encap_limit = t->parms.encap_limit;
12311305
}
1232-
encap_limit = tel->encap_limit - 1;
1233-
} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1234-
encap_limit = t->parms.encap_limit;
12351306

1236-
memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1237-
fl6.flowi6_proto = IPPROTO_IPV6;
1307+
memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1308+
fl6.flowi6_proto = IPPROTO_IPV6;
12381309

1239-
dsfield = ipv6_get_dsfield(ipv6h);
1240-
if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1241-
fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1242-
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1243-
fl6.flowlabel |= ip6_flowlabel(ipv6h);
1244-
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1245-
fl6.flowi6_mark = skb->mark;
1310+
if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1311+
fl6.flowlabel |= (*(__be32 *)ipv6h & IPV6_TCLASS_MASK);
1312+
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1313+
fl6.flowlabel |= ip6_flowlabel(ipv6h);
1314+
if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1315+
fl6.flowi6_mark = skb->mark;
1316+
}
12461317

12471318
if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
12481319
return -1;
@@ -1741,6 +1812,10 @@ static int ip6_tnl_dev_init(struct net_device *dev)
17411812
if (err)
17421813
return err;
17431814
ip6_tnl_link_config(t);
1815+
if (t->parms.collect_md) {
1816+
dev->features |= NETIF_F_NETNS_LOCAL;
1817+
netif_keep_dst(dev);
1818+
}
17441819
return 0;
17451820
}
17461821

@@ -1811,6 +1886,9 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[],
18111886

18121887
if (data[IFLA_IPTUN_PROTO])
18131888
parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1889+
1890+
if (data[IFLA_IPTUN_COLLECT_METADATA])
1891+
parms->collect_md = true;
18141892
}
18151893

18161894
static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
@@ -1850,6 +1928,7 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
18501928
struct nlattr *tb[], struct nlattr *data[])
18511929
{
18521930
struct net *net = dev_net(dev);
1931+
struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
18531932
struct ip6_tnl *nt, *t;
18541933
struct ip_tunnel_encap ipencap;
18551934

@@ -1864,9 +1943,14 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
18641943

18651944
ip6_tnl_netlink_parms(data, &nt->parms);
18661945

1867-
t = ip6_tnl_locate(net, &nt->parms, 0);
1868-
if (!IS_ERR(t))
1869-
return -EEXIST;
1946+
if (nt->parms.collect_md) {
1947+
if (rtnl_dereference(ip6n->collect_md_tun))
1948+
return -EEXIST;
1949+
} else {
1950+
t = ip6_tnl_locate(net, &nt->parms, 0);
1951+
if (!IS_ERR(t))
1952+
return -EEXIST;
1953+
}
18701954

18711955
return ip6_tnl_create2(dev);
18721956
}
@@ -1890,6 +1974,8 @@ static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
18901974
return err;
18911975
}
18921976
ip6_tnl_netlink_parms(data, &p);
1977+
if (p.collect_md)
1978+
return -EINVAL;
18931979

18941980
t = ip6_tnl_locate(net, &p, 0);
18951981
if (!IS_ERR(t)) {
@@ -1937,6 +2023,8 @@ static size_t ip6_tnl_get_size(const struct net_device *dev)
19372023
nla_total_size(2) +
19382024
/* IFLA_IPTUN_ENCAP_DPORT */
19392025
nla_total_size(2) +
2026+
/* IFLA_IPTUN_COLLECT_METADATA */
2027+
nla_total_size(0) +
19402028
0;
19412029
}
19422030

@@ -1955,16 +2043,15 @@ static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
19552043
nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
19562044
goto nla_put_failure;
19572045

1958-
if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
1959-
tunnel->encap.type) ||
1960-
nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
1961-
tunnel->encap.sport) ||
1962-
nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
1963-
tunnel->encap.dport) ||
1964-
nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
1965-
tunnel->encap.flags))
2046+
if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
2047+
nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
2048+
nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
2049+
nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
19662050
goto nla_put_failure;
19672051

2052+
if (parm->collect_md)
2053+
if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
2054+
goto nla_put_failure;
19682055
return 0;
19692056

19702057
nla_put_failure:
@@ -1992,6 +2079,7 @@ static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
19922079
[IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
19932080
[IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
19942081
[IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
2082+
[IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
19952083
};
19962084

19972085
static struct rtnl_link_ops ip6_link_ops __read_mostly = {

0 commit comments

Comments
 (0)