act_tunnel_key.c 8.8 KB

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