act_tunnel_key.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. goto err_out;
  119. }
  120. if (!exists) {
  121. ret = tcf_hash_create(tn, parm->index, est, a,
  122. &act_tunnel_key_ops, bind, true);
  123. if (ret)
  124. return ret;
  125. ret = ACT_P_CREATED;
  126. } else {
  127. tcf_hash_release(*a, bind);
  128. if (!ovr)
  129. return -EEXIST;
  130. }
  131. t = to_tunnel_key(*a);
  132. ASSERT_RTNL();
  133. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  134. if (unlikely(!params_new)) {
  135. if (ret == ACT_P_CREATED)
  136. tcf_hash_release(*a, bind);
  137. return -ENOMEM;
  138. }
  139. params_old = rtnl_dereference(t->params);
  140. params_new->action = parm->action;
  141. params_new->tcft_action = parm->t_action;
  142. params_new->tcft_enc_metadata = metadata;
  143. rcu_assign_pointer(t->params, params_new);
  144. if (params_old)
  145. kfree_rcu(params_old, rcu);
  146. if (ret == ACT_P_CREATED)
  147. tcf_hash_insert(tn, *a);
  148. return ret;
  149. err_out:
  150. if (exists)
  151. tcf_hash_release(*a, bind);
  152. return ret;
  153. }
  154. static void tunnel_key_release(struct tc_action *a, int bind)
  155. {
  156. struct tcf_tunnel_key *t = to_tunnel_key(a);
  157. struct tcf_tunnel_key_params *params;
  158. params = rcu_dereference_protected(t->params, 1);
  159. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
  160. dst_release(&params->tcft_enc_metadata->dst);
  161. kfree_rcu(params, rcu);
  162. }
  163. static int tunnel_key_dump_addresses(struct sk_buff *skb,
  164. const struct ip_tunnel_info *info)
  165. {
  166. unsigned short family = ip_tunnel_info_af(info);
  167. if (family == AF_INET) {
  168. __be32 saddr = info->key.u.ipv4.src;
  169. __be32 daddr = info->key.u.ipv4.dst;
  170. if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
  171. !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
  172. return 0;
  173. }
  174. if (family == AF_INET6) {
  175. const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
  176. const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
  177. if (!nla_put_in6_addr(skb,
  178. TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
  179. !nla_put_in6_addr(skb,
  180. TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
  181. return 0;
  182. }
  183. return -EINVAL;
  184. }
  185. static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
  186. int bind, int ref)
  187. {
  188. unsigned char *b = skb_tail_pointer(skb);
  189. struct tcf_tunnel_key *t = to_tunnel_key(a);
  190. struct tcf_tunnel_key_params *params;
  191. struct tc_tunnel_key opt = {
  192. .index = t->tcf_index,
  193. .refcnt = t->tcf_refcnt - ref,
  194. .bindcnt = t->tcf_bindcnt - bind,
  195. };
  196. struct tcf_t tm;
  197. params = rtnl_dereference(t->params);
  198. opt.t_action = params->tcft_action;
  199. opt.action = params->action;
  200. if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
  201. goto nla_put_failure;
  202. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
  203. struct ip_tunnel_key *key =
  204. &params->tcft_enc_metadata->u.tun_info.key;
  205. __be32 key_id = tunnel_id_to_key32(key->tun_id);
  206. if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
  207. tunnel_key_dump_addresses(skb,
  208. &params->tcft_enc_metadata->u.tun_info))
  209. goto nla_put_failure;
  210. }
  211. tcf_tm_dump(&tm, &t->tcf_tm);
  212. if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
  213. &tm, TCA_TUNNEL_KEY_PAD))
  214. goto nla_put_failure;
  215. return skb->len;
  216. nla_put_failure:
  217. nlmsg_trim(skb, b);
  218. return -1;
  219. }
  220. static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
  221. struct netlink_callback *cb, int type,
  222. const struct tc_action_ops *ops)
  223. {
  224. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  225. return tcf_generic_walker(tn, skb, cb, type, ops);
  226. }
  227. static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
  228. {
  229. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  230. return tcf_hash_search(tn, a, index);
  231. }
  232. static struct tc_action_ops act_tunnel_key_ops = {
  233. .kind = "tunnel_key",
  234. .type = TCA_ACT_TUNNEL_KEY,
  235. .owner = THIS_MODULE,
  236. .act = tunnel_key_act,
  237. .dump = tunnel_key_dump,
  238. .init = tunnel_key_init,
  239. .cleanup = tunnel_key_release,
  240. .walk = tunnel_key_walker,
  241. .lookup = tunnel_key_search,
  242. .size = sizeof(struct tcf_tunnel_key),
  243. };
  244. static __net_init int tunnel_key_init_net(struct net *net)
  245. {
  246. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  247. return tc_action_net_init(tn, &act_tunnel_key_ops, TUNNEL_KEY_TAB_MASK);
  248. }
  249. static void __net_exit tunnel_key_exit_net(struct net *net)
  250. {
  251. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  252. tc_action_net_exit(tn);
  253. }
  254. static struct pernet_operations tunnel_key_net_ops = {
  255. .init = tunnel_key_init_net,
  256. .exit = tunnel_key_exit_net,
  257. .id = &tunnel_key_net_id,
  258. .size = sizeof(struct tc_action_net),
  259. };
  260. static int __init tunnel_key_init_module(void)
  261. {
  262. return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  263. }
  264. static void __exit tunnel_key_cleanup_module(void)
  265. {
  266. tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  267. }
  268. module_init(tunnel_key_init_module);
  269. module_exit(tunnel_key_cleanup_module);
  270. MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
  271. MODULE_DESCRIPTION("ip tunnel manipulation actions");
  272. MODULE_LICENSE("GPL v2");