act_tunnel_key.c 9.1 KB

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