Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Amir Vadai <amir@vadai.me> |
| 3 | * Copyright (c) 2016, Mellanox Technologies. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/rtnetlink.h> |
| 16 | #include <net/netlink.h> |
| 17 | #include <net/pkt_sched.h> |
| 18 | #include <net/dst.h> |
| 19 | #include <net/dst_metadata.h> |
| 20 | |
| 21 | #include <linux/tc_act/tc_tunnel_key.h> |
| 22 | #include <net/tc_act/tc_tunnel_key.h> |
| 23 | |
| 24 | #define TUNNEL_KEY_TAB_MASK 15 |
| 25 | |
| 26 | static int tunnel_key_net_id; |
| 27 | static struct tc_action_ops act_tunnel_key_ops; |
| 28 | |
| 29 | static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a, |
| 30 | struct tcf_result *res) |
| 31 | { |
| 32 | struct tcf_tunnel_key *t = to_tunnel_key(a); |
| 33 | struct tcf_tunnel_key_params *params; |
| 34 | int action; |
| 35 | |
| 36 | rcu_read_lock(); |
| 37 | |
| 38 | params = rcu_dereference(t->params); |
| 39 | |
| 40 | tcf_lastuse_update(&t->tcf_tm); |
| 41 | bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb); |
Davide Caratti | 7754ed7 | 2018-07-06 21:01:06 +0200 | [diff] [blame] | 42 | action = READ_ONCE(t->tcf_action); |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 43 | |
| 44 | switch (params->tcft_action) { |
| 45 | case TCA_TUNNEL_KEY_ACT_RELEASE: |
| 46 | skb_dst_drop(skb); |
| 47 | break; |
| 48 | case TCA_TUNNEL_KEY_ACT_SET: |
| 49 | skb_dst_drop(skb); |
| 50 | skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst)); |
| 51 | break; |
| 52 | default: |
| 53 | WARN_ONCE(1, "Bad tunnel_key action %d.\n", |
| 54 | params->tcft_action); |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | rcu_read_unlock(); |
| 59 | |
| 60 | return action; |
| 61 | } |
| 62 | |
| 63 | static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = { |
| 64 | [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) }, |
| 65 | [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, |
| 66 | [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, |
| 67 | [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 68 | [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 69 | [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
| 70 | }; |
| 71 | |
| 72 | static int tunnel_key_init(struct net *net, struct nlattr *nla, |
| 73 | struct nlattr *est, struct tc_action **a, |
| 74 | int ovr, int bind) |
| 75 | { |
| 76 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); |
| 77 | struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1]; |
| 78 | struct tcf_tunnel_key_params *params_old; |
| 79 | struct tcf_tunnel_key_params *params_new; |
| 80 | struct metadata_dst *metadata = NULL; |
| 81 | struct tc_tunnel_key *parm; |
| 82 | struct tcf_tunnel_key *t; |
| 83 | bool exists = false; |
| 84 | __be64 key_id; |
| 85 | int ret = 0; |
| 86 | int err; |
| 87 | |
| 88 | if (!nla) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy); |
| 92 | if (err < 0) |
| 93 | return err; |
| 94 | |
| 95 | if (!tb[TCA_TUNNEL_KEY_PARMS]) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]); |
| 99 | exists = tcf_hash_check(tn, parm->index, a, bind); |
| 100 | if (exists && bind) |
| 101 | return 0; |
| 102 | |
| 103 | switch (parm->t_action) { |
| 104 | case TCA_TUNNEL_KEY_ACT_RELEASE: |
| 105 | break; |
| 106 | case TCA_TUNNEL_KEY_ACT_SET: |
| 107 | if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) { |
| 108 | ret = -EINVAL; |
| 109 | goto err_out; |
| 110 | } |
| 111 | |
| 112 | key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID])); |
| 113 | |
| 114 | if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] && |
| 115 | tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) { |
| 116 | __be32 saddr; |
| 117 | __be32 daddr; |
| 118 | |
| 119 | saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]); |
| 120 | daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]); |
| 121 | |
| 122 | metadata = __ip_tun_set_dst(saddr, daddr, 0, 0, |
| 123 | TUNNEL_KEY, key_id, 0); |
| 124 | } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] && |
| 125 | tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) { |
| 126 | struct in6_addr saddr; |
| 127 | struct in6_addr daddr; |
| 128 | |
| 129 | saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]); |
| 130 | daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]); |
| 131 | |
| 132 | metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, 0, |
| 133 | TUNNEL_KEY, key_id, 0); |
| 134 | } |
| 135 | |
| 136 | if (!metadata) { |
| 137 | ret = -EINVAL; |
| 138 | goto err_out; |
| 139 | } |
| 140 | |
| 141 | metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX; |
| 142 | break; |
| 143 | default: |
Roman Mashak | 4f2f7a0 | 2018-03-12 16:20:58 -0400 | [diff] [blame] | 144 | ret = -EINVAL; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 145 | goto err_out; |
| 146 | } |
| 147 | |
| 148 | if (!exists) { |
| 149 | ret = tcf_hash_create(tn, parm->index, est, a, |
| 150 | &act_tunnel_key_ops, bind, true); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | ret = ACT_P_CREATED; |
| 155 | } else { |
| 156 | tcf_hash_release(*a, bind); |
| 157 | if (!ovr) |
| 158 | return -EEXIST; |
| 159 | } |
| 160 | |
| 161 | t = to_tunnel_key(*a); |
| 162 | |
| 163 | ASSERT_RTNL(); |
| 164 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
| 165 | if (unlikely(!params_new)) { |
| 166 | if (ret == ACT_P_CREATED) |
| 167 | tcf_hash_release(*a, bind); |
| 168 | return -ENOMEM; |
| 169 | } |
| 170 | |
| 171 | params_old = rtnl_dereference(t->params); |
| 172 | |
Davide Caratti | 7754ed7 | 2018-07-06 21:01:06 +0200 | [diff] [blame] | 173 | t->tcf_action = parm->action; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 174 | params_new->tcft_action = parm->t_action; |
| 175 | params_new->tcft_enc_metadata = metadata; |
| 176 | |
| 177 | rcu_assign_pointer(t->params, params_new); |
| 178 | |
| 179 | if (params_old) |
| 180 | kfree_rcu(params_old, rcu); |
| 181 | |
| 182 | if (ret == ACT_P_CREATED) |
| 183 | tcf_hash_insert(tn, *a); |
| 184 | |
| 185 | return ret; |
| 186 | |
| 187 | err_out: |
| 188 | if (exists) |
| 189 | tcf_hash_release(*a, bind); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static void tunnel_key_release(struct tc_action *a, int bind) |
| 194 | { |
| 195 | struct tcf_tunnel_key *t = to_tunnel_key(a); |
| 196 | struct tcf_tunnel_key_params *params; |
| 197 | |
Hadar Hen Zion | 07c0f09 | 2016-09-12 15:19:21 +0300 | [diff] [blame] | 198 | params = rcu_dereference_protected(t->params, 1); |
Davide Caratti | 9d1ff26 | 2018-03-16 00:00:55 +0100 | [diff] [blame] | 199 | if (params) { |
| 200 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) |
| 201 | dst_release(¶ms->tcft_enc_metadata->dst); |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 202 | |
Davide Caratti | 9d1ff26 | 2018-03-16 00:00:55 +0100 | [diff] [blame] | 203 | kfree_rcu(params, rcu); |
| 204 | } |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int tunnel_key_dump_addresses(struct sk_buff *skb, |
| 208 | const struct ip_tunnel_info *info) |
| 209 | { |
| 210 | unsigned short family = ip_tunnel_info_af(info); |
| 211 | |
| 212 | if (family == AF_INET) { |
| 213 | __be32 saddr = info->key.u.ipv4.src; |
| 214 | __be32 daddr = info->key.u.ipv4.dst; |
| 215 | |
| 216 | if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) && |
| 217 | !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr)) |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | if (family == AF_INET6) { |
| 222 | const struct in6_addr *saddr6 = &info->key.u.ipv6.src; |
| 223 | const struct in6_addr *daddr6 = &info->key.u.ipv6.dst; |
| 224 | |
| 225 | if (!nla_put_in6_addr(skb, |
| 226 | TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) && |
| 227 | !nla_put_in6_addr(skb, |
| 228 | TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6)) |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a, |
| 236 | int bind, int ref) |
| 237 | { |
| 238 | unsigned char *b = skb_tail_pointer(skb); |
| 239 | struct tcf_tunnel_key *t = to_tunnel_key(a); |
| 240 | struct tcf_tunnel_key_params *params; |
| 241 | struct tc_tunnel_key opt = { |
| 242 | .index = t->tcf_index, |
| 243 | .refcnt = t->tcf_refcnt - ref, |
| 244 | .bindcnt = t->tcf_bindcnt - bind, |
Davide Caratti | 7754ed7 | 2018-07-06 21:01:06 +0200 | [diff] [blame] | 245 | .action = t->tcf_action, |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 246 | }; |
| 247 | struct tcf_t tm; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 248 | |
Hadar Hen Zion | 07c0f09 | 2016-09-12 15:19:21 +0300 | [diff] [blame] | 249 | params = rtnl_dereference(t->params); |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 250 | |
| 251 | opt.t_action = params->tcft_action; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 252 | |
| 253 | if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt)) |
| 254 | goto nla_put_failure; |
| 255 | |
| 256 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) { |
| 257 | struct ip_tunnel_key *key = |
| 258 | ¶ms->tcft_enc_metadata->u.tun_info.key; |
| 259 | __be32 key_id = tunnel_id_to_key32(key->tun_id); |
| 260 | |
| 261 | if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) || |
| 262 | tunnel_key_dump_addresses(skb, |
| 263 | ¶ms->tcft_enc_metadata->u.tun_info)) |
| 264 | goto nla_put_failure; |
| 265 | } |
| 266 | |
| 267 | tcf_tm_dump(&tm, &t->tcf_tm); |
| 268 | if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm), |
| 269 | &tm, TCA_TUNNEL_KEY_PAD)) |
| 270 | goto nla_put_failure; |
| 271 | |
Hadar Hen Zion | 07c0f09 | 2016-09-12 15:19:21 +0300 | [diff] [blame] | 272 | return skb->len; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 273 | |
| 274 | nla_put_failure: |
| 275 | nlmsg_trim(skb, b); |
Hadar Hen Zion | 07c0f09 | 2016-09-12 15:19:21 +0300 | [diff] [blame] | 276 | return -1; |
Amir Vadai | d0f6dd8 | 2016-09-08 16:23:48 +0300 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int tunnel_key_walker(struct net *net, struct sk_buff *skb, |
| 280 | struct netlink_callback *cb, int type, |
| 281 | const struct tc_action_ops *ops) |
| 282 | { |
| 283 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); |
| 284 | |
| 285 | return tcf_generic_walker(tn, skb, cb, type, ops); |
| 286 | } |
| 287 | |
| 288 | static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index) |
| 289 | { |
| 290 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); |
| 291 | |
| 292 | return tcf_hash_search(tn, a, index); |
| 293 | } |
| 294 | |
| 295 | static struct tc_action_ops act_tunnel_key_ops = { |
| 296 | .kind = "tunnel_key", |
| 297 | .type = TCA_ACT_TUNNEL_KEY, |
| 298 | .owner = THIS_MODULE, |
| 299 | .act = tunnel_key_act, |
| 300 | .dump = tunnel_key_dump, |
| 301 | .init = tunnel_key_init, |
| 302 | .cleanup = tunnel_key_release, |
| 303 | .walk = tunnel_key_walker, |
| 304 | .lookup = tunnel_key_search, |
| 305 | .size = sizeof(struct tcf_tunnel_key), |
| 306 | }; |
| 307 | |
| 308 | static __net_init int tunnel_key_init_net(struct net *net) |
| 309 | { |
| 310 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); |
| 311 | |
| 312 | return tc_action_net_init(tn, &act_tunnel_key_ops, TUNNEL_KEY_TAB_MASK); |
| 313 | } |
| 314 | |
| 315 | static void __net_exit tunnel_key_exit_net(struct net *net) |
| 316 | { |
| 317 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); |
| 318 | |
| 319 | tc_action_net_exit(tn); |
| 320 | } |
| 321 | |
| 322 | static struct pernet_operations tunnel_key_net_ops = { |
| 323 | .init = tunnel_key_init_net, |
| 324 | .exit = tunnel_key_exit_net, |
| 325 | .id = &tunnel_key_net_id, |
| 326 | .size = sizeof(struct tc_action_net), |
| 327 | }; |
| 328 | |
| 329 | static int __init tunnel_key_init_module(void) |
| 330 | { |
| 331 | return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops); |
| 332 | } |
| 333 | |
| 334 | static void __exit tunnel_key_cleanup_module(void) |
| 335 | { |
| 336 | tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops); |
| 337 | } |
| 338 | |
| 339 | module_init(tunnel_key_init_module); |
| 340 | module_exit(tunnel_key_cleanup_module); |
| 341 | |
| 342 | MODULE_AUTHOR("Amir Vadai <amir@vadai.me>"); |
| 343 | MODULE_DESCRIPTION("ip tunnel manipulation actions"); |
| 344 | MODULE_LICENSE("GPL v2"); |