^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/geneve.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/vxlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/erspan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/dst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/tc_act/tc_tunnel_key.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/tc_act/tc_tunnel_key.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static unsigned int tunnel_key_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct tc_action_ops act_tunnel_key_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct tcf_tunnel_key *t = to_tunnel_key(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct tcf_tunnel_key_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) params = rcu_dereference_bh(t->params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) tcf_lastuse_update(&t->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) tcf_action_update_bstats(&t->common, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) action = READ_ONCE(t->tcf_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) switch (params->tcft_action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case TCA_TUNNEL_KEY_ACT_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case TCA_TUNNEL_KEY_ACT_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) WARN_ONCE(1, "Bad tunnel_key action %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) params->tcft_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) [TCA_TUNNEL_KEY_ENC_OPTS_UNSPEC] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .strict_start_type = TCA_TUNNEL_KEY_ENC_OPTS_VXLAN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) [TCA_TUNNEL_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) [TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .len = 128 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) vxlan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) [TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) erspan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int err, data_len, opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) err = nla_parse_nested_deprecated(tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) nla, geneve_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (data_len < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (data_len % 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) opt_len = sizeof(struct geneve_opt) + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct geneve_opt *opt = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) WARN_ON(dst_len < opt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) opt->opt_class =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) opt->length = data_len / 4; /* length is in units of 4 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) opt->r1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) opt->r2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) opt->r3 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) memcpy(opt + 1, data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) tunnel_key_copy_vxlan_opt(const struct nlattr *nla, void *dst, int dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) vxlan_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct vxlan_metadata *md = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) md->gbp = nla_get_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) md->gbp &= VXLAN_GBP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return sizeof(struct vxlan_metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) tunnel_key_copy_erspan_opt(const struct nlattr *nla, void *dst, int dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) u8 ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) erspan_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ver = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ver == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } else if (ver == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) !tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct erspan_metadata *md = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) md->version = ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (ver == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) md->u.index = nla_get_be32(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) md->u.md2.dir = nla_get_u8(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) set_hwid(&md->u.md2, nla_get_u8(nla));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return sizeof(struct erspan_metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) int dst_len, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int err, rem, opt_len, len = nla_len(nla), opts_len = 0, type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const struct nlattr *attr, *head = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) enc_opts_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) nla_for_each_attr(attr, head, len, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) switch (nla_type(attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (type && type != TUNNEL_GENEVE_OPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) opt_len = tunnel_key_copy_geneve_opt(attr, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) dst_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (opt_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) opts_len += opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (opts_len > IP_TUNNEL_OPTS_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dst_len -= opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dst += opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) type = TUNNEL_GENEVE_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) opt_len = tunnel_key_copy_vxlan_opt(attr, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) dst_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (opt_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) opts_len += opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) type = TUNNEL_VXLAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) opt_len = tunnel_key_copy_erspan_opt(attr, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dst_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (opt_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) opts_len += opt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) type = TUNNEL_ERSPAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!opts_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (rem > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return opts_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int tunnel_key_get_opts_len(struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return tunnel_key_copy_opts(nla, NULL, 0, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int opts_len, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) info->options_len = opts_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) switch (nla_type(nla_data(nla))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) #if IS_ENABLED(CONFIG_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) info->key.tun_flags |= TUNNEL_GENEVE_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) opts_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #if IS_ENABLED(CONFIG_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) info->key.tun_flags |= TUNNEL_VXLAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) opts_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) #if IS_ENABLED(CONFIG_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) opts_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) dst_release(&p->tcft_enc_metadata->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) kfree_rcu(p, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int tunnel_key_init(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct nlattr *est, struct tc_action **a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int ovr, int bind, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct tcf_proto *tp, u32 act_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct tcf_tunnel_key_params *params_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct metadata_dst *metadata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct tcf_chain *goto_ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct tc_tunnel_key *parm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct tcf_tunnel_key *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) bool exists = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) __be16 dst_port = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) __be64 key_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int opts_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) __be16 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) u8 tos, ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!nla) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) tunnel_key_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (!tb[TCA_TUNNEL_KEY_PARMS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) index = parm->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) err = tcf_idr_check_alloc(tn, &index, a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) exists = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (exists && bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) switch (parm->t_action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) case TCA_TUNNEL_KEY_ACT_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) case TCA_TUNNEL_KEY_ACT_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) __be32 key32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) key_id = key32_to_tunnel_id(key32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) flags = TUNNEL_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) flags |= TUNNEL_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) flags &= ~TUNNEL_CSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (opts_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) ret = opts_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (tb[TCA_TUNNEL_KEY_ENC_TOS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) ttl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (tb[TCA_TUNNEL_KEY_ENC_TTL])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) __be32 saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) __be32 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) dst_port, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) key_id, opts_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct in6_addr saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct in6_addr daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 0, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) key_id, opts_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (!metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #ifdef CONFIG_DST_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) goto release_tun_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (opts_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) &metadata->u.tun_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) opts_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto release_tun_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!exists) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ret = tcf_idr_create_from_flags(tn, index, est, a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) &act_tunnel_key_ops, bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) act_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto release_tun_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) ret = ACT_P_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) } else if (!ovr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) NL_SET_ERR_MSG(extack, "TC IDR already exists");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) goto release_tun_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) exists = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) goto release_tun_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) t = to_tunnel_key(*a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (unlikely(!params_new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) exists = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) params_new->tcft_action = parm->t_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) params_new->tcft_enc_metadata = metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) spin_lock_bh(&t->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) params_new = rcu_replace_pointer(t->params, params_new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) lockdep_is_held(&t->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) spin_unlock_bh(&t->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) tunnel_key_release_params(params_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) put_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) release_tun_meta:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) dst_release(&metadata->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static void tunnel_key_release(struct tc_action *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct tcf_tunnel_key *t = to_tunnel_key(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct tcf_tunnel_key_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) params = rcu_dereference_protected(t->params, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) tunnel_key_release_params(params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int len = info->options_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) u8 *src = (u8 *)(info + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) struct nlattr *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct geneve_opt *opt = (struct geneve_opt *)src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) opt->opt_class) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) opt->type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) opt->length * 4, opt + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) nla_nest_cancel(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) len -= sizeof(struct geneve_opt) + opt->length * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) src += sizeof(struct geneve_opt) + opt->length * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) nla_nest_end(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct nlattr *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) nla_nest_cancel(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) nla_nest_end(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static int tunnel_key_erspan_opts_dump(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) struct erspan_metadata *md = (struct erspan_metadata *)(info + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct nlattr *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER, md->version))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (md->version == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (md->version == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) md->u.md2.dir) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) get_hwid(&md->u.md2))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) nla_nest_end(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) nla_nest_cancel(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static int tunnel_key_opts_dump(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct nlattr *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (!info->options_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) err = tunnel_key_geneve_opts_dump(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) err = tunnel_key_vxlan_opts_dump(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) err = tunnel_key_erspan_opts_dump(skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) nla_nest_cancel(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) nla_nest_end(skb, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static int tunnel_key_dump_addresses(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) const struct ip_tunnel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) unsigned short family = ip_tunnel_info_af(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) __be32 saddr = info->key.u.ipv4.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) __be32 daddr = info->key.u.ipv4.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (!nla_put_in6_addr(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) !nla_put_in6_addr(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) int bind, int ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) unsigned char *b = skb_tail_pointer(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct tcf_tunnel_key *t = to_tunnel_key(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct tcf_tunnel_key_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct tc_tunnel_key opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) .index = t->tcf_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) .refcnt = refcount_read(&t->tcf_refcnt) - ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct tcf_t tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) spin_lock_bh(&t->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) params = rcu_dereference_protected(t->params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) lockdep_is_held(&t->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) opt.action = t->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) opt.t_action = params->tcft_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct ip_tunnel_info *info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) ¶ms->tcft_enc_metadata->u.tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct ip_tunnel_key *key = &info->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) __be32 key_id = tunnel_id_to_key32(key->tun_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (((key->tun_flags & TUNNEL_KEY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) tunnel_key_dump_addresses(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) ¶ms->tcft_enc_metadata->u.tun_info) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) (key->tp_dst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) key->tp_dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) !(key->tun_flags & TUNNEL_CSUM)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) tunnel_key_opts_dump(skb, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) tcf_tm_dump(&tm, &t->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) &tm, TCA_TUNNEL_KEY_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) spin_unlock_bh(&t->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) spin_unlock_bh(&t->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) nlmsg_trim(skb, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct netlink_callback *cb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) const struct tc_action_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return tcf_generic_walker(tn, skb, cb, type, ops, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return tcf_idr_search(tn, a, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static struct tc_action_ops act_tunnel_key_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) .kind = "tunnel_key",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) .id = TCA_ID_TUNNEL_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) .act = tunnel_key_act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) .dump = tunnel_key_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) .init = tunnel_key_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) .cleanup = tunnel_key_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) .walk = tunnel_key_walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) .lookup = tunnel_key_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) .size = sizeof(struct tcf_tunnel_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static __net_init int tunnel_key_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) return tc_action_net_init(net, tn, &act_tunnel_key_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) tc_action_net_exit(net_list, tunnel_key_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) static struct pernet_operations tunnel_key_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) .init = tunnel_key_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) .exit_batch = tunnel_key_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) .id = &tunnel_key_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) .size = sizeof(struct tc_action_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static int __init tunnel_key_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static void __exit tunnel_key_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) module_init(tunnel_key_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) module_exit(tunnel_key_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) MODULE_DESCRIPTION("ip tunnel manipulation actions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) MODULE_LICENSE("GPL v2");