act_tunnel_key.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
  4. * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/rtnetlink.h>
  11. #include <net/geneve.h>
  12. #include <net/vxlan.h>
  13. #include <net/erspan.h>
  14. #include <net/netlink.h>
  15. #include <net/pkt_sched.h>
  16. #include <net/dst.h>
  17. #include <net/pkt_cls.h>
  18. #include <net/tc_wrapper.h>
  19. #include <linux/tc_act/tc_tunnel_key.h>
  20. #include <net/tc_act/tc_tunnel_key.h>
  21. static struct tc_action_ops act_tunnel_key_ops;
  22. TC_INDIRECT_SCOPE int tunnel_key_act(struct sk_buff *skb,
  23. 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. params = rcu_dereference_bh(t->params);
  30. tcf_lastuse_update(&t->tcf_tm);
  31. tcf_action_update_bstats(&t->common, 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. return action;
  47. }
  48. static const struct nla_policy
  49. enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
  50. [TCA_TUNNEL_KEY_ENC_OPTS_UNSPEC] = {
  51. .strict_start_type = TCA_TUNNEL_KEY_ENC_OPTS_VXLAN },
  52. [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
  53. [TCA_TUNNEL_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
  54. [TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
  55. };
  56. static const struct nla_policy
  57. geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
  58. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
  59. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
  60. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
  61. .len = 127 },
  62. };
  63. static const struct nla_policy
  64. vxlan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1] = {
  65. [TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
  66. };
  67. static const struct nla_policy
  68. erspan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
  69. [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
  70. [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
  71. [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
  72. [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
  73. };
  74. static int
  75. tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
  76. struct netlink_ext_ack *extack)
  77. {
  78. struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
  79. int err, data_len, opt_len;
  80. u8 *data;
  81. err = nla_parse_nested_deprecated(tb,
  82. TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
  83. nla, geneve_opt_policy, extack);
  84. if (err < 0)
  85. return err;
  86. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
  87. !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
  88. !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
  89. NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
  90. return -EINVAL;
  91. }
  92. data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
  93. data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
  94. if (data_len < 4) {
  95. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
  96. return -ERANGE;
  97. }
  98. if (data_len % 4) {
  99. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
  100. return -ERANGE;
  101. }
  102. opt_len = sizeof(struct geneve_opt) + data_len;
  103. if (dst) {
  104. struct geneve_opt *opt = dst;
  105. WARN_ON(dst_len < opt_len);
  106. opt->opt_class =
  107. nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
  108. opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
  109. opt->length = data_len / 4; /* length is in units of 4 bytes */
  110. opt->r1 = 0;
  111. opt->r2 = 0;
  112. opt->r3 = 0;
  113. memcpy(opt + 1, data, data_len);
  114. }
  115. return opt_len;
  116. }
  117. static int
  118. tunnel_key_copy_vxlan_opt(const struct nlattr *nla, void *dst, int dst_len,
  119. struct netlink_ext_ack *extack)
  120. {
  121. struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1];
  122. int err;
  123. err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, nla,
  124. vxlan_opt_policy, extack);
  125. if (err < 0)
  126. return err;
  127. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]) {
  128. NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
  129. return -EINVAL;
  130. }
  131. if (dst) {
  132. struct vxlan_metadata *md = dst;
  133. md->gbp = nla_get_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]);
  134. md->gbp &= VXLAN_GBP_MASK;
  135. }
  136. return sizeof(struct vxlan_metadata);
  137. }
  138. static int
  139. tunnel_key_copy_erspan_opt(const struct nlattr *nla, void *dst, int dst_len,
  140. struct netlink_ext_ack *extack)
  141. {
  142. struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1];
  143. int err;
  144. u8 ver;
  145. err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX, nla,
  146. erspan_opt_policy, extack);
  147. if (err < 0)
  148. return err;
  149. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]) {
  150. NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
  151. return -EINVAL;
  152. }
  153. ver = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]);
  154. if (ver == 1) {
  155. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX]) {
  156. NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
  157. return -EINVAL;
  158. }
  159. } else if (ver == 2) {
  160. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] ||
  161. !tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID]) {
  162. NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
  163. return -EINVAL;
  164. }
  165. } else {
  166. NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
  167. return -EINVAL;
  168. }
  169. if (dst) {
  170. struct erspan_metadata *md = dst;
  171. md->version = ver;
  172. if (ver == 1) {
  173. nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX];
  174. md->u.index = nla_get_be32(nla);
  175. } else {
  176. nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR];
  177. md->u.md2.dir = nla_get_u8(nla);
  178. nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID];
  179. set_hwid(&md->u.md2, nla_get_u8(nla));
  180. }
  181. }
  182. return sizeof(struct erspan_metadata);
  183. }
  184. static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
  185. int dst_len, struct netlink_ext_ack *extack)
  186. {
  187. int err, rem, opt_len, len = nla_len(nla), opts_len = 0, type = 0;
  188. const struct nlattr *attr, *head = nla_data(nla);
  189. err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
  190. enc_opts_policy, extack);
  191. if (err)
  192. return err;
  193. nla_for_each_attr(attr, head, len, rem) {
  194. switch (nla_type(attr)) {
  195. case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
  196. if (type && type != IP_TUNNEL_GENEVE_OPT_BIT) {
  197. NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
  198. return -EINVAL;
  199. }
  200. opt_len = tunnel_key_copy_geneve_opt(attr, dst,
  201. dst_len, extack);
  202. if (opt_len < 0)
  203. return opt_len;
  204. opts_len += opt_len;
  205. if (opts_len > IP_TUNNEL_OPTS_MAX) {
  206. NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
  207. return -EINVAL;
  208. }
  209. if (dst) {
  210. dst_len -= opt_len;
  211. dst += opt_len;
  212. }
  213. type = IP_TUNNEL_GENEVE_OPT_BIT;
  214. break;
  215. case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
  216. if (type) {
  217. NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
  218. return -EINVAL;
  219. }
  220. opt_len = tunnel_key_copy_vxlan_opt(attr, dst,
  221. dst_len, extack);
  222. if (opt_len < 0)
  223. return opt_len;
  224. opts_len += opt_len;
  225. type = IP_TUNNEL_VXLAN_OPT_BIT;
  226. break;
  227. case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN:
  228. if (type) {
  229. NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
  230. return -EINVAL;
  231. }
  232. opt_len = tunnel_key_copy_erspan_opt(attr, dst,
  233. dst_len, extack);
  234. if (opt_len < 0)
  235. return opt_len;
  236. opts_len += opt_len;
  237. type = IP_TUNNEL_ERSPAN_OPT_BIT;
  238. break;
  239. }
  240. }
  241. if (!opts_len) {
  242. NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
  243. return -EINVAL;
  244. }
  245. if (rem > 0) {
  246. NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
  247. return -EINVAL;
  248. }
  249. return opts_len;
  250. }
  251. static int tunnel_key_get_opts_len(struct nlattr *nla,
  252. struct netlink_ext_ack *extack)
  253. {
  254. return tunnel_key_copy_opts(nla, NULL, 0, extack);
  255. }
  256. static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
  257. int opts_len, struct netlink_ext_ack *extack)
  258. {
  259. info->options_len = opts_len;
  260. switch (nla_type(nla_data(nla))) {
  261. case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
  262. #if IS_ENABLED(CONFIG_INET)
  263. __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags);
  264. return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
  265. opts_len, extack);
  266. #else
  267. return -EAFNOSUPPORT;
  268. #endif
  269. case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
  270. #if IS_ENABLED(CONFIG_INET)
  271. __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags);
  272. return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
  273. opts_len, extack);
  274. #else
  275. return -EAFNOSUPPORT;
  276. #endif
  277. case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN:
  278. #if IS_ENABLED(CONFIG_INET)
  279. __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags);
  280. return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
  281. opts_len, extack);
  282. #else
  283. return -EAFNOSUPPORT;
  284. #endif
  285. default:
  286. NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
  287. return -EINVAL;
  288. }
  289. }
  290. static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
  291. [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
  292. [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  293. [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  294. [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  295. [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  296. [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  297. [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
  298. [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
  299. [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED },
  300. [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 },
  301. [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 },
  302. };
  303. static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
  304. {
  305. if (!p)
  306. return;
  307. if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
  308. dst_release(&p->tcft_enc_metadata->dst);
  309. kfree_rcu(p, rcu);
  310. }
  311. static int tunnel_key_init(struct net *net, struct nlattr *nla,
  312. struct nlattr *est, struct tc_action **a,
  313. struct tcf_proto *tp, u32 act_flags,
  314. struct netlink_ext_ack *extack)
  315. {
  316. struct tc_action_net *tn = net_generic(net, act_tunnel_key_ops.net_id);
  317. bool bind = act_flags & TCA_ACT_FLAGS_BIND;
  318. struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
  319. struct tcf_tunnel_key_params *params_new;
  320. IP_TUNNEL_DECLARE_FLAGS(flags) = { };
  321. struct metadata_dst *metadata = NULL;
  322. struct tcf_chain *goto_ch = NULL;
  323. struct tc_tunnel_key *parm;
  324. struct tcf_tunnel_key *t;
  325. bool exists = false;
  326. __be16 dst_port = 0;
  327. __be64 key_id = 0;
  328. int opts_len = 0;
  329. u8 tos, ttl;
  330. int ret = 0;
  331. u32 index;
  332. int err;
  333. if (!nla) {
  334. NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
  335. return -EINVAL;
  336. }
  337. err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla,
  338. tunnel_key_policy, extack);
  339. if (err < 0) {
  340. NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
  341. return err;
  342. }
  343. if (!tb[TCA_TUNNEL_KEY_PARMS]) {
  344. NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
  345. return -EINVAL;
  346. }
  347. parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
  348. index = parm->index;
  349. err = tcf_idr_check_alloc(tn, &index, a, bind);
  350. if (err < 0)
  351. return err;
  352. exists = err;
  353. if (exists && bind)
  354. return ACT_P_BOUND;
  355. switch (parm->t_action) {
  356. case TCA_TUNNEL_KEY_ACT_RELEASE:
  357. break;
  358. case TCA_TUNNEL_KEY_ACT_SET:
  359. if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
  360. __be32 key32;
  361. key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
  362. key_id = key32_to_tunnel_id(key32);
  363. __set_bit(IP_TUNNEL_KEY_BIT, flags);
  364. }
  365. __set_bit(IP_TUNNEL_CSUM_BIT, flags);
  366. if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
  367. nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
  368. __clear_bit(IP_TUNNEL_CSUM_BIT, flags);
  369. if (nla_get_flag(tb[TCA_TUNNEL_KEY_NO_FRAG]))
  370. __set_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, flags);
  371. if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
  372. dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
  373. if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
  374. opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  375. extack);
  376. if (opts_len < 0) {
  377. ret = opts_len;
  378. goto err_out;
  379. }
  380. }
  381. tos = 0;
  382. if (tb[TCA_TUNNEL_KEY_ENC_TOS])
  383. tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
  384. ttl = 0;
  385. if (tb[TCA_TUNNEL_KEY_ENC_TTL])
  386. ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
  387. if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
  388. tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
  389. __be32 saddr;
  390. __be32 daddr;
  391. saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
  392. daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
  393. metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
  394. dst_port, flags,
  395. key_id, opts_len);
  396. } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
  397. tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
  398. struct in6_addr saddr;
  399. struct in6_addr daddr;
  400. saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
  401. daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
  402. metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
  403. 0, flags,
  404. key_id, opts_len);
  405. } else {
  406. NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
  407. ret = -EINVAL;
  408. goto err_out;
  409. }
  410. if (!metadata) {
  411. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
  412. ret = -ENOMEM;
  413. goto err_out;
  414. }
  415. #ifdef CONFIG_DST_CACHE
  416. ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
  417. if (ret)
  418. goto release_tun_meta;
  419. #endif
  420. if (opts_len) {
  421. ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  422. &metadata->u.tun_info,
  423. opts_len, extack);
  424. if (ret < 0)
  425. goto release_tun_meta;
  426. }
  427. metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
  428. break;
  429. default:
  430. NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
  431. ret = -EINVAL;
  432. goto err_out;
  433. }
  434. if (!exists) {
  435. ret = tcf_idr_create_from_flags(tn, index, est, a,
  436. &act_tunnel_key_ops, bind,
  437. act_flags);
  438. if (ret) {
  439. NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
  440. goto release_tun_meta;
  441. }
  442. ret = ACT_P_CREATED;
  443. } else if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) {
  444. NL_SET_ERR_MSG(extack, "TC IDR already exists");
  445. ret = -EEXIST;
  446. goto release_tun_meta;
  447. }
  448. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  449. if (err < 0) {
  450. ret = err;
  451. exists = true;
  452. goto release_tun_meta;
  453. }
  454. t = to_tunnel_key(*a);
  455. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  456. if (unlikely(!params_new)) {
  457. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
  458. ret = -ENOMEM;
  459. exists = true;
  460. goto put_chain;
  461. }
  462. params_new->tcft_action = parm->t_action;
  463. params_new->tcft_enc_metadata = metadata;
  464. spin_lock_bh(&t->tcf_lock);
  465. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  466. params_new = rcu_replace_pointer(t->params, params_new,
  467. lockdep_is_held(&t->tcf_lock));
  468. spin_unlock_bh(&t->tcf_lock);
  469. tunnel_key_release_params(params_new);
  470. if (goto_ch)
  471. tcf_chain_put_by_act(goto_ch);
  472. return ret;
  473. put_chain:
  474. if (goto_ch)
  475. tcf_chain_put_by_act(goto_ch);
  476. release_tun_meta:
  477. if (metadata)
  478. dst_release(&metadata->dst);
  479. err_out:
  480. if (exists)
  481. tcf_idr_release(*a, bind);
  482. else
  483. tcf_idr_cleanup(tn, index);
  484. return ret;
  485. }
  486. static void tunnel_key_release(struct tc_action *a)
  487. {
  488. struct tcf_tunnel_key *t = to_tunnel_key(a);
  489. struct tcf_tunnel_key_params *params;
  490. params = rcu_dereference_protected(t->params, 1);
  491. tunnel_key_release_params(params);
  492. }
  493. static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
  494. const struct ip_tunnel_info *info)
  495. {
  496. int len = info->options_len;
  497. u8 *src = (u8 *)(info + 1);
  498. struct nlattr *start;
  499. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
  500. if (!start)
  501. return -EMSGSIZE;
  502. while (len > 0) {
  503. struct geneve_opt *opt = (struct geneve_opt *)src;
  504. if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
  505. opt->opt_class) ||
  506. nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
  507. opt->type) ||
  508. nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
  509. opt->length * 4, opt + 1)) {
  510. nla_nest_cancel(skb, start);
  511. return -EMSGSIZE;
  512. }
  513. len -= sizeof(struct geneve_opt) + opt->length * 4;
  514. src += sizeof(struct geneve_opt) + opt->length * 4;
  515. }
  516. nla_nest_end(skb, start);
  517. return 0;
  518. }
  519. static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb,
  520. const struct ip_tunnel_info *info)
  521. {
  522. struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1);
  523. struct nlattr *start;
  524. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN);
  525. if (!start)
  526. return -EMSGSIZE;
  527. if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) {
  528. nla_nest_cancel(skb, start);
  529. return -EMSGSIZE;
  530. }
  531. nla_nest_end(skb, start);
  532. return 0;
  533. }
  534. static int tunnel_key_erspan_opts_dump(struct sk_buff *skb,
  535. const struct ip_tunnel_info *info)
  536. {
  537. struct erspan_metadata *md = (struct erspan_metadata *)(info + 1);
  538. struct nlattr *start;
  539. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN);
  540. if (!start)
  541. return -EMSGSIZE;
  542. if (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER, md->version))
  543. goto err;
  544. if (md->version == 1 &&
  545. nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
  546. goto err;
  547. if (md->version == 2 &&
  548. (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR,
  549. md->u.md2.dir) ||
  550. nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID,
  551. get_hwid(&md->u.md2))))
  552. goto err;
  553. nla_nest_end(skb, start);
  554. return 0;
  555. err:
  556. nla_nest_cancel(skb, start);
  557. return -EMSGSIZE;
  558. }
  559. static int tunnel_key_opts_dump(struct sk_buff *skb,
  560. const struct ip_tunnel_info *info)
  561. {
  562. struct nlattr *start;
  563. int err = -EINVAL;
  564. if (!info->options_len)
  565. return 0;
  566. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
  567. if (!start)
  568. return -EMSGSIZE;
  569. if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) {
  570. err = tunnel_key_geneve_opts_dump(skb, info);
  571. if (err)
  572. goto err_out;
  573. } else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) {
  574. err = tunnel_key_vxlan_opts_dump(skb, info);
  575. if (err)
  576. goto err_out;
  577. } else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags)) {
  578. err = tunnel_key_erspan_opts_dump(skb, info);
  579. if (err)
  580. goto err_out;
  581. } else {
  582. err_out:
  583. nla_nest_cancel(skb, start);
  584. return err;
  585. }
  586. nla_nest_end(skb, start);
  587. return 0;
  588. }
  589. static int tunnel_key_dump_addresses(struct sk_buff *skb,
  590. const struct ip_tunnel_info *info)
  591. {
  592. unsigned short family = ip_tunnel_info_af(info);
  593. if (family == AF_INET) {
  594. __be32 saddr = info->key.u.ipv4.src;
  595. __be32 daddr = info->key.u.ipv4.dst;
  596. if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
  597. !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
  598. return 0;
  599. }
  600. if (family == AF_INET6) {
  601. const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
  602. const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
  603. if (!nla_put_in6_addr(skb,
  604. TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
  605. !nla_put_in6_addr(skb,
  606. TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
  607. return 0;
  608. }
  609. return -EINVAL;
  610. }
  611. static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
  612. int bind, int ref)
  613. {
  614. unsigned char *b = skb_tail_pointer(skb);
  615. struct tcf_tunnel_key *t = to_tunnel_key(a);
  616. struct tcf_tunnel_key_params *params;
  617. struct tc_tunnel_key opt = {
  618. .index = t->tcf_index,
  619. .refcnt = refcount_read(&t->tcf_refcnt) - ref,
  620. .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
  621. };
  622. struct tcf_t tm;
  623. spin_lock_bh(&t->tcf_lock);
  624. params = rcu_dereference_protected(t->params,
  625. lockdep_is_held(&t->tcf_lock));
  626. opt.action = t->tcf_action;
  627. opt.t_action = params->tcft_action;
  628. if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
  629. goto nla_put_failure;
  630. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
  631. struct ip_tunnel_info *info =
  632. &params->tcft_enc_metadata->u.tun_info;
  633. struct ip_tunnel_key *key = &info->key;
  634. __be32 key_id = tunnel_id_to_key32(key->tun_id);
  635. if ((test_bit(IP_TUNNEL_KEY_BIT, key->tun_flags) &&
  636. nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
  637. tunnel_key_dump_addresses(skb,
  638. &params->tcft_enc_metadata->u.tun_info) ||
  639. (key->tp_dst &&
  640. nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
  641. key->tp_dst)) ||
  642. nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
  643. !test_bit(IP_TUNNEL_CSUM_BIT, key->tun_flags)) ||
  644. (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags) &&
  645. nla_put_flag(skb, TCA_TUNNEL_KEY_NO_FRAG)) ||
  646. tunnel_key_opts_dump(skb, info))
  647. goto nla_put_failure;
  648. if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
  649. goto nla_put_failure;
  650. if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
  651. goto nla_put_failure;
  652. }
  653. tcf_tm_dump(&tm, &t->tcf_tm);
  654. if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
  655. &tm, TCA_TUNNEL_KEY_PAD))
  656. goto nla_put_failure;
  657. spin_unlock_bh(&t->tcf_lock);
  658. return skb->len;
  659. nla_put_failure:
  660. spin_unlock_bh(&t->tcf_lock);
  661. nlmsg_trim(skb, b);
  662. return -1;
  663. }
  664. static void tcf_tunnel_encap_put_tunnel(void *priv)
  665. {
  666. struct ip_tunnel_info *tunnel = priv;
  667. kfree(tunnel);
  668. }
  669. static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
  670. const struct tc_action *act)
  671. {
  672. entry->tunnel = tcf_tunnel_info_copy(act);
  673. if (!entry->tunnel)
  674. return -ENOMEM;
  675. entry->destructor = tcf_tunnel_encap_put_tunnel;
  676. entry->destructor_priv = entry->tunnel;
  677. return 0;
  678. }
  679. static int tcf_tunnel_key_offload_act_setup(struct tc_action *act,
  680. void *entry_data,
  681. u32 *index_inc,
  682. bool bind,
  683. struct netlink_ext_ack *extack)
  684. {
  685. int err;
  686. if (bind) {
  687. struct flow_action_entry *entry = entry_data;
  688. if (is_tcf_tunnel_set(act)) {
  689. entry->id = FLOW_ACTION_TUNNEL_ENCAP;
  690. err = tcf_tunnel_encap_get_tunnel(entry, act);
  691. if (err)
  692. return err;
  693. } else if (is_tcf_tunnel_release(act)) {
  694. entry->id = FLOW_ACTION_TUNNEL_DECAP;
  695. } else {
  696. NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel key mode offload");
  697. return -EOPNOTSUPP;
  698. }
  699. *index_inc = 1;
  700. } else {
  701. struct flow_offload_action *fl_action = entry_data;
  702. if (is_tcf_tunnel_set(act))
  703. fl_action->id = FLOW_ACTION_TUNNEL_ENCAP;
  704. else if (is_tcf_tunnel_release(act))
  705. fl_action->id = FLOW_ACTION_TUNNEL_DECAP;
  706. else
  707. return -EOPNOTSUPP;
  708. }
  709. return 0;
  710. }
  711. static struct tc_action_ops act_tunnel_key_ops = {
  712. .kind = "tunnel_key",
  713. .id = TCA_ID_TUNNEL_KEY,
  714. .owner = THIS_MODULE,
  715. .act = tunnel_key_act,
  716. .dump = tunnel_key_dump,
  717. .init = tunnel_key_init,
  718. .cleanup = tunnel_key_release,
  719. .offload_act_setup = tcf_tunnel_key_offload_act_setup,
  720. .size = sizeof(struct tcf_tunnel_key),
  721. };
  722. MODULE_ALIAS_NET_ACT("tunnel_key");
  723. static __net_init int tunnel_key_init_net(struct net *net)
  724. {
  725. struct tc_action_net *tn = net_generic(net, act_tunnel_key_ops.net_id);
  726. return tc_action_net_init(net, tn, &act_tunnel_key_ops);
  727. }
  728. static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
  729. {
  730. tc_action_net_exit(net_list, act_tunnel_key_ops.net_id);
  731. }
  732. static struct pernet_operations tunnel_key_net_ops = {
  733. .init = tunnel_key_init_net,
  734. .exit_batch = tunnel_key_exit_net,
  735. .id = &act_tunnel_key_ops.net_id,
  736. .size = sizeof(struct tc_action_net),
  737. };
  738. static int __init tunnel_key_init_module(void)
  739. {
  740. return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  741. }
  742. static void __exit tunnel_key_cleanup_module(void)
  743. {
  744. tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  745. }
  746. module_init(tunnel_key_init_module);
  747. module_exit(tunnel_key_cleanup_module);
  748. MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
  749. MODULE_DESCRIPTION("ip tunnel manipulation actions");
  750. MODULE_LICENSE("GPL v2");