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