act_tunnel_key.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <net/dst.h>
  18. #include <net/dst_metadata.h>
  19. #include <linux/tc_act/tc_tunnel_key.h>
  20. #include <net/tc_act/tc_tunnel_key.h>
  21. #define TUNNEL_KEY_TAB_MASK 15
  22. static int tunnel_key_net_id;
  23. static struct tc_action_ops act_tunnel_key_ops;
  24. static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
  25. struct tcf_result *res)
  26. {
  27. struct tcf_tunnel_key *t = to_tunnel_key(a);
  28. struct tcf_tunnel_key_params *params;
  29. int action;
  30. rcu_read_lock();
  31. params = rcu_dereference(t->params);
  32. tcf_lastuse_update(&t->tcf_tm);
  33. bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
  34. action = params->action;
  35. switch (params->tcft_action) {
  36. case TCA_TUNNEL_KEY_ACT_RELEASE:
  37. skb_dst_drop(skb);
  38. break;
  39. case TCA_TUNNEL_KEY_ACT_SET:
  40. skb_dst_drop(skb);
  41. skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
  42. break;
  43. default:
  44. WARN_ONCE(1, "Bad tunnel_key action %d.\n",
  45. params->tcft_action);
  46. break;
  47. }
  48. rcu_read_unlock();
  49. return action;
  50. }
  51. static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
  52. [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
  53. [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  54. [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  55. [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  56. [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  57. [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  58. };
  59. static int tunnel_key_init(struct net *net, struct nlattr *nla,
  60. struct nlattr *est, struct tc_action **a,
  61. int ovr, int bind)
  62. {
  63. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  64. struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
  65. struct tcf_tunnel_key_params *params_old;
  66. struct tcf_tunnel_key_params *params_new;
  67. struct metadata_dst *metadata = NULL;
  68. struct tc_tunnel_key *parm;
  69. struct tcf_tunnel_key *t;
  70. bool exists = false;
  71. __be64 key_id;
  72. int ret = 0;
  73. int err;
  74. if (!nla)
  75. return -EINVAL;
  76. err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy);
  77. if (err < 0)
  78. return err;
  79. if (!tb[TCA_TUNNEL_KEY_PARMS])
  80. return -EINVAL;
  81. parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
  82. exists = tcf_hash_check(tn, parm->index, a, bind);
  83. if (exists && bind)
  84. return 0;
  85. switch (parm->t_action) {
  86. case TCA_TUNNEL_KEY_ACT_RELEASE:
  87. break;
  88. case TCA_TUNNEL_KEY_ACT_SET:
  89. if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
  90. ret = -EINVAL;
  91. goto err_out;
  92. }
  93. key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
  94. if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
  95. tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
  96. __be32 saddr;
  97. __be32 daddr;
  98. saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
  99. daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
  100. metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
  101. TUNNEL_KEY, key_id, 0);
  102. } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
  103. tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
  104. struct in6_addr saddr;
  105. struct in6_addr daddr;
  106. saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
  107. daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
  108. metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, 0,
  109. TUNNEL_KEY, key_id, 0);
  110. }
  111. if (!metadata) {
  112. ret = -EINVAL;
  113. goto err_out;
  114. }
  115. metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
  116. break;
  117. default:
  118. ret = -EINVAL;
  119. goto err_out;
  120. }
  121. if (!exists) {
  122. ret = tcf_hash_create(tn, parm->index, est, a,
  123. &act_tunnel_key_ops, bind, true);
  124. if (ret)
  125. return ret;
  126. ret = ACT_P_CREATED;
  127. } else {
  128. tcf_hash_release(*a, bind);
  129. if (!ovr)
  130. return -EEXIST;
  131. }
  132. t = to_tunnel_key(*a);
  133. ASSERT_RTNL();
  134. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  135. if (unlikely(!params_new)) {
  136. if (ret == ACT_P_CREATED)
  137. tcf_hash_release(*a, bind);
  138. return -ENOMEM;
  139. }
  140. params_old = rtnl_dereference(t->params);
  141. params_new->action = parm->action;
  142. params_new->tcft_action = parm->t_action;
  143. params_new->tcft_enc_metadata = metadata;
  144. rcu_assign_pointer(t->params, params_new);
  145. if (params_old)
  146. kfree_rcu(params_old, rcu);
  147. if (ret == ACT_P_CREATED)
  148. tcf_hash_insert(tn, *a);
  149. return ret;
  150. err_out:
  151. if (exists)
  152. tcf_hash_release(*a, bind);
  153. return ret;
  154. }
  155. static void tunnel_key_release(struct tc_action *a, int bind)
  156. {
  157. struct tcf_tunnel_key *t = to_tunnel_key(a);
  158. struct tcf_tunnel_key_params *params;
  159. params = rcu_dereference_protected(t->params, 1);
  160. if (params) {
  161. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
  162. dst_release(&params->tcft_enc_metadata->dst);
  163. kfree_rcu(params, rcu);
  164. }
  165. }
  166. static int tunnel_key_dump_addresses(struct sk_buff *skb,
  167. const struct ip_tunnel_info *info)
  168. {
  169. unsigned short family = ip_tunnel_info_af(info);
  170. if (family == AF_INET) {
  171. __be32 saddr = info->key.u.ipv4.src;
  172. __be32 daddr = info->key.u.ipv4.dst;
  173. if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
  174. !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
  175. return 0;
  176. }
  177. if (family == AF_INET6) {
  178. const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
  179. const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
  180. if (!nla_put_in6_addr(skb,
  181. TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
  182. !nla_put_in6_addr(skb,
  183. TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
  184. return 0;
  185. }
  186. return -EINVAL;
  187. }
  188. static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
  189. int bind, int ref)
  190. {
  191. unsigned char *b = skb_tail_pointer(skb);
  192. struct tcf_tunnel_key *t = to_tunnel_key(a);
  193. struct tcf_tunnel_key_params *params;
  194. struct tc_tunnel_key opt = {
  195. .index = t->tcf_index,
  196. .refcnt = t->tcf_refcnt - ref,
  197. .bindcnt = t->tcf_bindcnt - bind,
  198. };
  199. struct tcf_t tm;
  200. params = rtnl_dereference(t->params);
  201. opt.t_action = params->tcft_action;
  202. opt.action = params->action;
  203. if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
  204. goto nla_put_failure;
  205. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
  206. struct ip_tunnel_key *key =
  207. &params->tcft_enc_metadata->u.tun_info.key;
  208. __be32 key_id = tunnel_id_to_key32(key->tun_id);
  209. if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
  210. tunnel_key_dump_addresses(skb,
  211. &params->tcft_enc_metadata->u.tun_info))
  212. goto nla_put_failure;
  213. }
  214. tcf_tm_dump(&tm, &t->tcf_tm);
  215. if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
  216. &tm, TCA_TUNNEL_KEY_PAD))
  217. goto nla_put_failure;
  218. return skb->len;
  219. nla_put_failure:
  220. nlmsg_trim(skb, b);
  221. return -1;
  222. }
  223. static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
  224. struct netlink_callback *cb, int type,
  225. const struct tc_action_ops *ops)
  226. {
  227. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  228. return tcf_generic_walker(tn, skb, cb, type, ops);
  229. }
  230. static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
  231. {
  232. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  233. return tcf_hash_search(tn, a, index);
  234. }
  235. static struct tc_action_ops act_tunnel_key_ops = {
  236. .kind = "tunnel_key",
  237. .type = TCA_ACT_TUNNEL_KEY,
  238. .owner = THIS_MODULE,
  239. .act = tunnel_key_act,
  240. .dump = tunnel_key_dump,
  241. .init = tunnel_key_init,
  242. .cleanup = tunnel_key_release,
  243. .walk = tunnel_key_walker,
  244. .lookup = tunnel_key_search,
  245. .size = sizeof(struct tcf_tunnel_key),
  246. };
  247. static __net_init int tunnel_key_init_net(struct net *net)
  248. {
  249. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  250. return tc_action_net_init(tn, &act_tunnel_key_ops, TUNNEL_KEY_TAB_MASK);
  251. }
  252. static void __net_exit tunnel_key_exit_net(struct net *net)
  253. {
  254. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  255. tc_action_net_exit(tn);
  256. }
  257. static struct pernet_operations tunnel_key_net_ops = {
  258. .init = tunnel_key_init_net,
  259. .exit = tunnel_key_exit_net,
  260. .id = &tunnel_key_net_id,
  261. .size = sizeof(struct tc_action_net),
  262. };
  263. static int __init tunnel_key_init_module(void)
  264. {
  265. return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  266. }
  267. static void __exit tunnel_key_cleanup_module(void)
  268. {
  269. tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  270. }
  271. module_init(tunnel_key_init_module);
  272. module_exit(tunnel_key_cleanup_module);
  273. MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
  274. MODULE_DESCRIPTION("ip tunnel manipulation actions");
  275. MODULE_LICENSE("GPL v2");