act_tunnel_key.c 9.1 KB

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