act_tunnel_key.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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/geneve.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/dst.h>
  19. #include <linux/tc_act/tc_tunnel_key.h>
  20. #include <net/tc_act/tc_tunnel_key.h>
  21. static unsigned int tunnel_key_net_id;
  22. static struct tc_action_ops act_tunnel_key_ops;
  23. static int tunnel_key_act(struct sk_buff *skb, 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. 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. 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_GENEVE] = { .type = NLA_NESTED },
  51. };
  52. static const struct nla_policy
  53. geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
  54. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
  55. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
  56. [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
  57. .len = 128 },
  58. };
  59. static int
  60. tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
  61. struct netlink_ext_ack *extack)
  62. {
  63. struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
  64. int err, data_len, opt_len;
  65. u8 *data;
  66. err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
  67. nla, geneve_opt_policy, extack);
  68. if (err < 0)
  69. return err;
  70. if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
  71. !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
  72. !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
  73. NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
  74. return -EINVAL;
  75. }
  76. data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
  77. data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
  78. if (data_len < 4) {
  79. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
  80. return -ERANGE;
  81. }
  82. if (data_len % 4) {
  83. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
  84. return -ERANGE;
  85. }
  86. opt_len = sizeof(struct geneve_opt) + data_len;
  87. if (dst) {
  88. struct geneve_opt *opt = dst;
  89. WARN_ON(dst_len < opt_len);
  90. opt->opt_class =
  91. nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
  92. opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
  93. opt->length = data_len / 4; /* length is in units of 4 bytes */
  94. opt->r1 = 0;
  95. opt->r2 = 0;
  96. opt->r3 = 0;
  97. memcpy(opt + 1, data, data_len);
  98. }
  99. return opt_len;
  100. }
  101. static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
  102. int dst_len, struct netlink_ext_ack *extack)
  103. {
  104. int err, rem, opt_len, len = nla_len(nla), opts_len = 0;
  105. const struct nlattr *attr, *head = nla_data(nla);
  106. err = nla_validate(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
  107. enc_opts_policy, extack);
  108. if (err)
  109. return err;
  110. nla_for_each_attr(attr, head, len, rem) {
  111. switch (nla_type(attr)) {
  112. case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
  113. opt_len = tunnel_key_copy_geneve_opt(attr, dst,
  114. dst_len, extack);
  115. if (opt_len < 0)
  116. return opt_len;
  117. opts_len += opt_len;
  118. if (opts_len > IP_TUNNEL_OPTS_MAX) {
  119. NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
  120. return -EINVAL;
  121. }
  122. if (dst) {
  123. dst_len -= opt_len;
  124. dst += opt_len;
  125. }
  126. break;
  127. }
  128. }
  129. if (!opts_len) {
  130. NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
  131. return -EINVAL;
  132. }
  133. if (rem > 0) {
  134. NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
  135. return -EINVAL;
  136. }
  137. return opts_len;
  138. }
  139. static int tunnel_key_get_opts_len(struct nlattr *nla,
  140. struct netlink_ext_ack *extack)
  141. {
  142. return tunnel_key_copy_opts(nla, NULL, 0, extack);
  143. }
  144. static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
  145. int opts_len, struct netlink_ext_ack *extack)
  146. {
  147. info->options_len = opts_len;
  148. switch (nla_type(nla_data(nla))) {
  149. case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
  150. #if IS_ENABLED(CONFIG_INET)
  151. info->key.tun_flags |= TUNNEL_GENEVE_OPT;
  152. return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
  153. opts_len, extack);
  154. #else
  155. return -EAFNOSUPPORT;
  156. #endif
  157. default:
  158. NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
  159. return -EINVAL;
  160. }
  161. }
  162. static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
  163. [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
  164. [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  165. [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  166. [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  167. [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  168. [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  169. [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
  170. [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
  171. [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED },
  172. [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 },
  173. [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 },
  174. };
  175. static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
  176. {
  177. if (!p)
  178. return;
  179. if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
  180. dst_release(&p->tcft_enc_metadata->dst);
  181. kfree_rcu(p, rcu);
  182. }
  183. static int tunnel_key_init(struct net *net, struct nlattr *nla,
  184. struct nlattr *est, struct tc_action **a,
  185. int ovr, int bind, bool rtnl_held,
  186. struct netlink_ext_ack *extack)
  187. {
  188. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  189. struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
  190. struct tcf_tunnel_key_params *params_new;
  191. struct metadata_dst *metadata = NULL;
  192. struct tc_tunnel_key *parm;
  193. struct tcf_tunnel_key *t;
  194. bool exists = false;
  195. __be16 dst_port = 0;
  196. int opts_len = 0;
  197. __be64 key_id;
  198. __be16 flags;
  199. u8 tos, ttl;
  200. int ret = 0;
  201. u32 index;
  202. int err;
  203. if (!nla) {
  204. NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
  205. return -EINVAL;
  206. }
  207. err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
  208. extack);
  209. if (err < 0) {
  210. NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
  211. return err;
  212. }
  213. if (!tb[TCA_TUNNEL_KEY_PARMS]) {
  214. NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
  215. return -EINVAL;
  216. }
  217. parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
  218. index = parm->index;
  219. err = tcf_idr_check_alloc(tn, &index, a, bind);
  220. if (err < 0)
  221. return err;
  222. exists = err;
  223. if (exists && bind)
  224. return 0;
  225. switch (parm->t_action) {
  226. case TCA_TUNNEL_KEY_ACT_RELEASE:
  227. break;
  228. case TCA_TUNNEL_KEY_ACT_SET:
  229. if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
  230. NL_SET_ERR_MSG(extack, "Missing tunnel key id");
  231. ret = -EINVAL;
  232. goto err_out;
  233. }
  234. key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
  235. flags = TUNNEL_KEY | TUNNEL_CSUM;
  236. if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
  237. nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
  238. flags &= ~TUNNEL_CSUM;
  239. if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
  240. dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
  241. if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
  242. opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  243. extack);
  244. if (opts_len < 0) {
  245. ret = opts_len;
  246. goto err_out;
  247. }
  248. }
  249. tos = 0;
  250. if (tb[TCA_TUNNEL_KEY_ENC_TOS])
  251. tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
  252. ttl = 0;
  253. if (tb[TCA_TUNNEL_KEY_ENC_TTL])
  254. ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
  255. if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
  256. tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
  257. __be32 saddr;
  258. __be32 daddr;
  259. saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
  260. daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
  261. metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
  262. dst_port, flags,
  263. key_id, opts_len);
  264. } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
  265. tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
  266. struct in6_addr saddr;
  267. struct in6_addr daddr;
  268. saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
  269. daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
  270. metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
  271. 0, flags,
  272. key_id, 0);
  273. } else {
  274. NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
  275. ret = -EINVAL;
  276. goto err_out;
  277. }
  278. if (!metadata) {
  279. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
  280. ret = -ENOMEM;
  281. goto err_out;
  282. }
  283. if (opts_len) {
  284. ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
  285. &metadata->u.tun_info,
  286. opts_len, extack);
  287. if (ret < 0)
  288. goto release_tun_meta;
  289. }
  290. metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
  291. break;
  292. default:
  293. NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
  294. ret = -EINVAL;
  295. goto err_out;
  296. }
  297. if (!exists) {
  298. ret = tcf_idr_create(tn, index, est, a,
  299. &act_tunnel_key_ops, bind, true);
  300. if (ret) {
  301. NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
  302. goto release_tun_meta;
  303. }
  304. ret = ACT_P_CREATED;
  305. } else if (!ovr) {
  306. NL_SET_ERR_MSG(extack, "TC IDR already exists");
  307. ret = -EEXIST;
  308. goto release_tun_meta;
  309. }
  310. t = to_tunnel_key(*a);
  311. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  312. if (unlikely(!params_new)) {
  313. NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
  314. ret = -ENOMEM;
  315. exists = true;
  316. goto release_tun_meta;
  317. }
  318. params_new->tcft_action = parm->t_action;
  319. params_new->tcft_enc_metadata = metadata;
  320. spin_lock_bh(&t->tcf_lock);
  321. t->tcf_action = parm->action;
  322. rcu_swap_protected(t->params, params_new,
  323. lockdep_is_held(&t->tcf_lock));
  324. spin_unlock_bh(&t->tcf_lock);
  325. tunnel_key_release_params(params_new);
  326. if (ret == ACT_P_CREATED)
  327. tcf_idr_insert(tn, *a);
  328. return ret;
  329. release_tun_meta:
  330. if (metadata)
  331. dst_release(&metadata->dst);
  332. err_out:
  333. if (exists)
  334. tcf_idr_release(*a, bind);
  335. else
  336. tcf_idr_cleanup(tn, index);
  337. return ret;
  338. }
  339. static void tunnel_key_release(struct tc_action *a)
  340. {
  341. struct tcf_tunnel_key *t = to_tunnel_key(a);
  342. struct tcf_tunnel_key_params *params;
  343. params = rcu_dereference_protected(t->params, 1);
  344. tunnel_key_release_params(params);
  345. }
  346. static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
  347. const struct ip_tunnel_info *info)
  348. {
  349. int len = info->options_len;
  350. u8 *src = (u8 *)(info + 1);
  351. struct nlattr *start;
  352. start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
  353. if (!start)
  354. return -EMSGSIZE;
  355. while (len > 0) {
  356. struct geneve_opt *opt = (struct geneve_opt *)src;
  357. if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
  358. opt->opt_class) ||
  359. nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
  360. opt->type) ||
  361. nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
  362. opt->length * 4, opt + 1)) {
  363. nla_nest_cancel(skb, start);
  364. return -EMSGSIZE;
  365. }
  366. len -= sizeof(struct geneve_opt) + opt->length * 4;
  367. src += sizeof(struct geneve_opt) + opt->length * 4;
  368. }
  369. nla_nest_end(skb, start);
  370. return 0;
  371. }
  372. static int tunnel_key_opts_dump(struct sk_buff *skb,
  373. const struct ip_tunnel_info *info)
  374. {
  375. struct nlattr *start;
  376. int err = -EINVAL;
  377. if (!info->options_len)
  378. return 0;
  379. start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS);
  380. if (!start)
  381. return -EMSGSIZE;
  382. if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
  383. err = tunnel_key_geneve_opts_dump(skb, info);
  384. if (err)
  385. goto err_out;
  386. } else {
  387. err_out:
  388. nla_nest_cancel(skb, start);
  389. return err;
  390. }
  391. nla_nest_end(skb, start);
  392. return 0;
  393. }
  394. static int tunnel_key_dump_addresses(struct sk_buff *skb,
  395. const struct ip_tunnel_info *info)
  396. {
  397. unsigned short family = ip_tunnel_info_af(info);
  398. if (family == AF_INET) {
  399. __be32 saddr = info->key.u.ipv4.src;
  400. __be32 daddr = info->key.u.ipv4.dst;
  401. if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
  402. !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
  403. return 0;
  404. }
  405. if (family == AF_INET6) {
  406. const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
  407. const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
  408. if (!nla_put_in6_addr(skb,
  409. TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
  410. !nla_put_in6_addr(skb,
  411. TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
  412. return 0;
  413. }
  414. return -EINVAL;
  415. }
  416. static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
  417. int bind, int ref)
  418. {
  419. unsigned char *b = skb_tail_pointer(skb);
  420. struct tcf_tunnel_key *t = to_tunnel_key(a);
  421. struct tcf_tunnel_key_params *params;
  422. struct tc_tunnel_key opt = {
  423. .index = t->tcf_index,
  424. .refcnt = refcount_read(&t->tcf_refcnt) - ref,
  425. .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
  426. };
  427. struct tcf_t tm;
  428. spin_lock_bh(&t->tcf_lock);
  429. params = rcu_dereference_protected(t->params,
  430. lockdep_is_held(&t->tcf_lock));
  431. opt.action = t->tcf_action;
  432. opt.t_action = params->tcft_action;
  433. if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
  434. goto nla_put_failure;
  435. if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
  436. struct ip_tunnel_info *info =
  437. &params->tcft_enc_metadata->u.tun_info;
  438. struct ip_tunnel_key *key = &info->key;
  439. __be32 key_id = tunnel_id_to_key32(key->tun_id);
  440. if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
  441. tunnel_key_dump_addresses(skb,
  442. &params->tcft_enc_metadata->u.tun_info) ||
  443. nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
  444. nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
  445. !(key->tun_flags & TUNNEL_CSUM)) ||
  446. tunnel_key_opts_dump(skb, info))
  447. goto nla_put_failure;
  448. if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
  449. goto nla_put_failure;
  450. if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
  451. goto nla_put_failure;
  452. }
  453. tcf_tm_dump(&tm, &t->tcf_tm);
  454. if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
  455. &tm, TCA_TUNNEL_KEY_PAD))
  456. goto nla_put_failure;
  457. spin_unlock_bh(&t->tcf_lock);
  458. return skb->len;
  459. nla_put_failure:
  460. spin_unlock_bh(&t->tcf_lock);
  461. nlmsg_trim(skb, b);
  462. return -1;
  463. }
  464. static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
  465. struct netlink_callback *cb, int type,
  466. const struct tc_action_ops *ops,
  467. struct netlink_ext_ack *extack)
  468. {
  469. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  470. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  471. }
  472. static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
  473. struct netlink_ext_ack *extack)
  474. {
  475. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  476. return tcf_idr_search(tn, a, index);
  477. }
  478. static struct tc_action_ops act_tunnel_key_ops = {
  479. .kind = "tunnel_key",
  480. .type = TCA_ACT_TUNNEL_KEY,
  481. .owner = THIS_MODULE,
  482. .act = tunnel_key_act,
  483. .dump = tunnel_key_dump,
  484. .init = tunnel_key_init,
  485. .cleanup = tunnel_key_release,
  486. .walk = tunnel_key_walker,
  487. .lookup = tunnel_key_search,
  488. .size = sizeof(struct tcf_tunnel_key),
  489. };
  490. static __net_init int tunnel_key_init_net(struct net *net)
  491. {
  492. struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
  493. return tc_action_net_init(net, tn, &act_tunnel_key_ops);
  494. }
  495. static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
  496. {
  497. tc_action_net_exit(net_list, tunnel_key_net_id);
  498. }
  499. static struct pernet_operations tunnel_key_net_ops = {
  500. .init = tunnel_key_init_net,
  501. .exit_batch = tunnel_key_exit_net,
  502. .id = &tunnel_key_net_id,
  503. .size = sizeof(struct tc_action_net),
  504. };
  505. static int __init tunnel_key_init_module(void)
  506. {
  507. return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  508. }
  509. static void __exit tunnel_key_cleanup_module(void)
  510. {
  511. tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
  512. }
  513. module_init(tunnel_key_init_module);
  514. module_exit(tunnel_key_cleanup_module);
  515. MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
  516. MODULE_DESCRIPTION("ip tunnel manipulation actions");
  517. MODULE_LICENSE("GPL v2");