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