act_tunnel_key.c 23 KB

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