act_tunnel_key.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016, Amir Vadai <[email protected]>
  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 <linux/tc_act/tc_tunnel_key.h>
  19. #include <net/tc_act/tc_tunnel_key.h>
  20. static struct tc_action_ops act_tunnel_key_ops;
  21. static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
  22. struct tcf_result *res)
  23. {
  24. struct tcf_tunnel_key *t = to_tunnel_key(a);
  25. struct tcf_tunnel_key_params *params;
  26. int action;
  27. params = rcu_dereference_bh(t->params);
  28. tcf_lastuse_update(&t->tcf_tm);
  29. tcf_action_update_bstats(&t->common, skb);
  30. action = READ_ONCE(t->tcf_action);
  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 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 = 128 },
  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 != TUNNEL_GENEVE_OPT) {
  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 = TUNNEL_GENEVE_OPT;
  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 = TUNNEL_VXLAN_OPT;
  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 = TUNNEL_ERSPAN_OPT;
  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. info->key.tun_flags |= TUNNEL_GENEVE_OPT;
  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. info->key.tun_flags |= TUNNEL_VXLAN_OPT;
  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. info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
  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. struct metadata_dst *metadata = NULL;
  319. struct tcf_chain *goto_ch = NULL;
  320. struct tc_tunnel_key *parm;
  321. struct tcf_tunnel_key *t;
  322. bool exists = false;
  323. __be16 dst_port = 0;
  324. __be64 key_id = 0;
  325. int opts_len = 0;
  326. __be16 flags = 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 0;
  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. flags = TUNNEL_KEY;
  362. }
  363. flags |= TUNNEL_CSUM;
  364. if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
  365. nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
  366. flags &= ~TUNNEL_CSUM;
  367. if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
  368. dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
  369. if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
  370. opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  371. extack);
  372. if (opts_len < 0) {
  373. ret = opts_len;
  374. goto err_out;
  375. }
  376. }
  377. tos = 0;
  378. if (tb[TCA_TUNNEL_KEY_ENC_TOS])
  379. tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
  380. ttl = 0;
  381. if (tb[TCA_TUNNEL_KEY_ENC_TTL])
  382. ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
  383. if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
  384. tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
  385. __be32 saddr;
  386. __be32 daddr;
  387. saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
  388. daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
  389. metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
  390. dst_port, flags,
  391. key_id, opts_len);
  392. } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
  393. tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
  394. struct in6_addr saddr;
  395. struct in6_addr daddr;
  396. saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
  397. daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
  398. metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
  399. 0, flags,
  400. key_id, opts_len);
  401. } else {
  402. NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
  403. ret = -EINVAL;
  404. goto err_out;
  405. }
  406. if (!metadata) {
  407. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
  408. ret = -ENOMEM;
  409. goto err_out;
  410. }
  411. #ifdef CONFIG_DST_CACHE
  412. ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
  413. if (ret)
  414. goto release_tun_meta;
  415. #endif
  416. if (opts_len) {
  417. ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  418. &metadata->u.tun_info,
  419. opts_len, extack);
  420. if (ret < 0)
  421. goto release_tun_meta;
  422. }
  423. metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
  424. break;
  425. default:
  426. NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
  427. ret = -EINVAL;
  428. goto err_out;
  429. }
  430. if (!exists) {
  431. ret = tcf_idr_create_from_flags(tn, index, est, a,
  432. &act_tunnel_key_ops, bind,
  433. act_flags);
  434. if (ret) {
  435. NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
  436. goto release_tun_meta;
  437. }
  438. ret = ACT_P_CREATED;
  439. } else if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) {
  440. NL_SET_ERR_MSG(extack, "TC IDR already exists");
  441. ret = -EEXIST;
  442. goto release_tun_meta;
  443. }
  444. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  445. if (err < 0) {
  446. ret = err;
  447. exists = true;
  448. goto release_tun_meta;
  449. }
  450. t = to_tunnel_key(*a);
  451. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  452. if (unlikely(!params_new)) {
  453. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
  454. ret = -ENOMEM;
  455. exists = true;
  456. goto put_chain;
  457. }
  458. params_new->tcft_action = parm->t_action;
  459. params_new->tcft_enc_metadata = metadata;
  460. spin_lock_bh(&t->tcf_lock);
  461. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  462. params_new = rcu_replace_pointer(t->params, params_new,
  463. lockdep_is_held(&t->tcf_lock));
  464. spin_unlock_bh(&t->tcf_lock);
  465. tunnel_key_release_params(params_new);
  466. if (goto_ch)
  467. tcf_chain_put_by_act(goto_ch);
  468. return ret;
  469. put_chain:
  470. if (goto_ch)
  471. tcf_chain_put_by_act(goto_ch);
  472. release_tun_meta:
  473. if (metadata)
  474. dst_release(&metadata->dst);
  475. err_out:
  476. if (exists)
  477. tcf_idr_release(*a, bind);
  478. else
  479. tcf_idr_cleanup(tn, index);
  480. return ret;
  481. }
  482. static void tunnel_key_release(struct tc_action *a)
  483. {
  484. struct tcf_tunnel_key *t = to_tunnel_key(a);
  485. struct tcf_tunnel_key_params *params;
  486. params = rcu_dereference_protected(t->params, 1);
  487. tunnel_key_release_params(params);
  488. }
  489. static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
  490. const struct ip_tunnel_info *info)
  491. {
  492. int len = info->options_len;
  493. u8 *src = (u8 *)(info + 1);
  494. struct nlattr *start;
  495. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
  496. if (!start)
  497. return -EMSGSIZE;
  498. while (len > 0) {
  499. struct geneve_opt *opt = (struct geneve_opt *)src;
  500. if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
  501. opt->opt_class) ||
  502. nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
  503. opt->type) ||
  504. nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
  505. opt->length * 4, opt + 1)) {
  506. nla_nest_cancel(skb, start);
  507. return -EMSGSIZE;
  508. }
  509. len -= sizeof(struct geneve_opt) + opt->length * 4;
  510. src += sizeof(struct geneve_opt) + opt->length * 4;
  511. }
  512. nla_nest_end(skb, start);
  513. return 0;
  514. }
  515. static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb,
  516. const struct ip_tunnel_info *info)
  517. {
  518. struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1);
  519. struct nlattr *start;
  520. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN);
  521. if (!start)
  522. return -EMSGSIZE;
  523. if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) {
  524. nla_nest_cancel(skb, start);
  525. return -EMSGSIZE;
  526. }
  527. nla_nest_end(skb, start);
  528. return 0;
  529. }
  530. static int tunnel_key_erspan_opts_dump(struct sk_buff *skb,
  531. const struct ip_tunnel_info *info)
  532. {
  533. struct erspan_metadata *md = (struct erspan_metadata *)(info + 1);
  534. struct nlattr *start;
  535. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN);
  536. if (!start)
  537. return -EMSGSIZE;
  538. if (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER, md->version))
  539. goto err;
  540. if (md->version == 1 &&
  541. nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
  542. goto err;
  543. if (md->version == 2 &&
  544. (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR,
  545. md->u.md2.dir) ||
  546. nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID,
  547. get_hwid(&md->u.md2))))
  548. goto err;
  549. nla_nest_end(skb, start);
  550. return 0;
  551. err:
  552. nla_nest_cancel(skb, start);
  553. return -EMSGSIZE;
  554. }
  555. static int tunnel_key_opts_dump(struct sk_buff *skb,
  556. const struct ip_tunnel_info *info)
  557. {
  558. struct nlattr *start;
  559. int err = -EINVAL;
  560. if (!info->options_len)
  561. return 0;
  562. start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
  563. if (!start)
  564. return -EMSGSIZE;
  565. if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
  566. err = tunnel_key_geneve_opts_dump(skb, info);
  567. if (err)
  568. goto err_out;
  569. } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
  570. err = tunnel_key_vxlan_opts_dump(skb, info);
  571. if (err)
  572. goto err_out;
  573. } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
  574. err = tunnel_key_erspan_opts_dump(skb, info);
  575. if (err)
  576. goto err_out;
  577. } else {
  578. err_out:
  579. nla_nest_cancel(skb, start);
  580. return err;
  581. }
  582. nla_nest_end(skb, start);
  583. return 0;
  584. }
  585. static int tunnel_key_dump_addresses(struct sk_buff *skb,
  586. const struct ip_tunnel_info *info)
  587. {
  588. unsigned short family = ip_tunnel_info_af(info);
  589. if (family == AF_INET) {
  590. __be32 saddr = info->key.u.ipv4.src;
  591. __be32 daddr = info->key.u.ipv4.dst;
  592. if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
  593. !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
  594. return 0;
  595. }
  596. if (family == AF_INET6) {
  597. const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
  598. const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
  599. if (!nla_put_in6_addr(skb,
  600. TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
  601. !nla_put_in6_addr(skb,
  602. TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
  603. return 0;
  604. }
  605. return -EINVAL;
  606. }
  607. static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
  608. int bind, int ref)
  609. {
  610. unsigned char *b = skb_tail_pointer(skb);
  611. struct tcf_tunnel_key *t = to_tunnel_key(a);
  612. struct tcf_tunnel_key_params *params;
  613. struct tc_tunnel_key opt = {
  614. .index = t->tcf_index,
  615. .refcnt = refcount_read(&t->tcf_refcnt) - ref,
  616. .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
  617. };
  618. struct tcf_t tm;
  619. spin_lock_bh(&t->tcf_lock);
  620. params = rcu_dereference_protected(t->params,
  621. lockdep_is_held(&t->tcf_lock));
  622. opt.action = t->tcf_action;
  623. opt.t_action = params->tcft_action;
  624. if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
  625. goto nla_put_failure;
  626. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
  627. struct ip_tunnel_info *info =
  628. &params->tcft_enc_metadata->u.tun_info;
  629. struct ip_tunnel_key *key = &info->key;
  630. __be32 key_id = tunnel_id_to_key32(key->tun_id);
  631. if (((key->tun_flags & TUNNEL_KEY) &&
  632. nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
  633. tunnel_key_dump_addresses(skb,
  634. &params->tcft_enc_metadata->u.tun_info) ||
  635. (key->tp_dst &&
  636. nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
  637. key->tp_dst)) ||
  638. nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
  639. !(key->tun_flags & TUNNEL_CSUM)) ||
  640. tunnel_key_opts_dump(skb, info))
  641. goto nla_put_failure;
  642. if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
  643. goto nla_put_failure;
  644. if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
  645. goto nla_put_failure;
  646. }
  647. tcf_tm_dump(&tm, &t->tcf_tm);
  648. if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
  649. &tm, TCA_TUNNEL_KEY_PAD))
  650. goto nla_put_failure;
  651. spin_unlock_bh(&t->tcf_lock);
  652. return skb->len;
  653. nla_put_failure:
  654. spin_unlock_bh(&t->tcf_lock);
  655. nlmsg_trim(skb, b);
  656. return -1;
  657. }
  658. static void tcf_tunnel_encap_put_tunnel(void *priv)
  659. {
  660. struct ip_tunnel_info *tunnel = priv;
  661. kfree(tunnel);
  662. }
  663. static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
  664. const struct tc_action *act)
  665. {
  666. entry->tunnel = tcf_tunnel_info_copy(act);
  667. if (!entry->tunnel)
  668. return -ENOMEM;
  669. entry->destructor = tcf_tunnel_encap_put_tunnel;
  670. entry->destructor_priv = entry->tunnel;
  671. return 0;
  672. }
  673. static int tcf_tunnel_key_offload_act_setup(struct tc_action *act,
  674. void *entry_data,
  675. u32 *index_inc,
  676. bool bind,
  677. struct netlink_ext_ack *extack)
  678. {
  679. int err;
  680. if (bind) {
  681. struct flow_action_entry *entry = entry_data;
  682. if (is_tcf_tunnel_set(act)) {
  683. entry->id = FLOW_ACTION_TUNNEL_ENCAP;
  684. err = tcf_tunnel_encap_get_tunnel(entry, act);
  685. if (err)
  686. return err;
  687. } else if (is_tcf_tunnel_release(act)) {
  688. entry->id = FLOW_ACTION_TUNNEL_DECAP;
  689. } else {
  690. NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel key mode offload");
  691. return -EOPNOTSUPP;
  692. }
  693. *index_inc = 1;
  694. } else {
  695. struct flow_offload_action *fl_action = entry_data;
  696. if (is_tcf_tunnel_set(act))
  697. fl_action->id = FLOW_ACTION_TUNNEL_ENCAP;
  698. else if (is_tcf_tunnel_release(act))
  699. fl_action->id = FLOW_ACTION_TUNNEL_DECAP;
  700. else
  701. return -EOPNOTSUPP;
  702. }
  703. return 0;
  704. }
  705. static struct tc_action_ops act_tunnel_key_ops = {
  706. .kind = "tunnel_key",
  707. .id = TCA_ID_TUNNEL_KEY,
  708. .owner = THIS_MODULE,
  709. .act = tunnel_key_act,
  710. .dump = tunnel_key_dump,
  711. .init = tunnel_key_init,
  712. .cleanup = tunnel_key_release,
  713. .offload_act_setup = tcf_tunnel_key_offload_act_setup,
  714. .size = sizeof(struct tcf_tunnel_key),
  715. };
  716. static __net_init int tunnel_key_init_net(struct net *net)
  717. {
  718. struct tc_action_net *tn = net_generic(net, act_tunnel_key_ops.net_id);
  719. return tc_action_net_init(net, tn, &act_tunnel_key_ops);
  720. }
  721. static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
  722. {
  723. tc_action_net_exit(net_list, act_tunnel_key_ops.net_id);
  724. }
  725. static struct pernet_operations tunnel_key_net_ops = {
  726. .init = tunnel_key_init_net,
  727. .exit_batch = tunnel_key_exit_net,
  728. .id = &act_tunnel_key_ops.net_id,
  729. .size = sizeof(struct tc_action_net),
  730. };
  731. static int __init tunnel_key_init_module(void)
  732. {
  733. return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  734. }
  735. static void __exit tunnel_key_cleanup_module(void)
  736. {
  737. tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  738. }
  739. module_init(tunnel_key_init_module);
  740. module_exit(tunnel_key_cleanup_module);
  741. MODULE_AUTHOR("Amir Vadai <[email protected]>");
  742. MODULE_DESCRIPTION("ip tunnel manipulation actions");
  743. MODULE_LICENSE("GPL v2");