act_ife.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/ife.c Inter-FE action based on ForCES WG InterFE LFB
  4. *
  5. * Refer to:
  6. * draft-ietf-forces-interfelfb-03
  7. * and
  8. * netdev01 paper:
  9. * "Distributing Linux Traffic Control Classifier-Action
  10. * Subsystem"
  11. * Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
  12. *
  13. * copyright Jamal Hadi Salim (2015)
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <net/net_namespace.h>
  24. #include <net/netlink.h>
  25. #include <net/pkt_sched.h>
  26. #include <net/pkt_cls.h>
  27. #include <uapi/linux/tc_act/tc_ife.h>
  28. #include <net/tc_act/tc_ife.h>
  29. #include <linux/etherdevice.h>
  30. #include <net/ife.h>
  31. #include <net/tc_wrapper.h>
  32. static int max_metacnt = IFE_META_MAX + 1;
  33. static struct tc_action_ops act_ife_ops;
  34. static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
  35. [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
  36. [TCA_IFE_DMAC] = { .len = ETH_ALEN},
  37. [TCA_IFE_SMAC] = { .len = ETH_ALEN},
  38. [TCA_IFE_TYPE] = { .type = NLA_U16},
  39. };
  40. int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
  41. {
  42. u16 edata = 0;
  43. if (mi->metaval)
  44. edata = *(u16 *)mi->metaval;
  45. else if (metaval)
  46. edata = metaval;
  47. if (!edata) /* will not encode */
  48. return 0;
  49. edata = htons(edata);
  50. return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
  51. }
  52. EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
  53. int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
  54. {
  55. if (mi->metaval)
  56. return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
  57. else
  58. return nla_put(skb, mi->metaid, 0, NULL);
  59. }
  60. EXPORT_SYMBOL_GPL(ife_get_meta_u32);
  61. int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
  62. {
  63. if (metaval || mi->metaval)
  64. return 8; /* T+L+V == 2+2+4 */
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(ife_check_meta_u32);
  68. int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
  69. {
  70. if (metaval || mi->metaval)
  71. return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(ife_check_meta_u16);
  75. int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
  76. {
  77. u32 edata = metaval;
  78. if (mi->metaval)
  79. edata = *(u32 *)mi->metaval;
  80. else if (metaval)
  81. edata = metaval;
  82. if (!edata) /* will not encode */
  83. return 0;
  84. edata = htonl(edata);
  85. return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
  86. }
  87. EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
  88. int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
  89. {
  90. if (mi->metaval)
  91. return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
  92. else
  93. return nla_put(skb, mi->metaid, 0, NULL);
  94. }
  95. EXPORT_SYMBOL_GPL(ife_get_meta_u16);
  96. int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
  97. {
  98. mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
  99. if (!mi->metaval)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
  104. int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
  105. {
  106. mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
  107. if (!mi->metaval)
  108. return -ENOMEM;
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
  112. void ife_release_meta_gen(struct tcf_meta_info *mi)
  113. {
  114. kfree(mi->metaval);
  115. }
  116. EXPORT_SYMBOL_GPL(ife_release_meta_gen);
  117. int ife_validate_meta_u32(void *val, int len)
  118. {
  119. if (len == sizeof(u32))
  120. return 0;
  121. return -EINVAL;
  122. }
  123. EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
  124. int ife_validate_meta_u16(void *val, int len)
  125. {
  126. /* length will not include padding */
  127. if (len == sizeof(u16))
  128. return 0;
  129. return -EINVAL;
  130. }
  131. EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
  132. static LIST_HEAD(ifeoplist);
  133. static DEFINE_RWLOCK(ife_mod_lock);
  134. static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
  135. {
  136. struct tcf_meta_ops *o;
  137. read_lock(&ife_mod_lock);
  138. list_for_each_entry(o, &ifeoplist, list) {
  139. if (o->metaid == metaid) {
  140. if (!try_module_get(o->owner))
  141. o = NULL;
  142. read_unlock(&ife_mod_lock);
  143. return o;
  144. }
  145. }
  146. read_unlock(&ife_mod_lock);
  147. return NULL;
  148. }
  149. int register_ife_op(struct tcf_meta_ops *mops)
  150. {
  151. struct tcf_meta_ops *m;
  152. if (!mops->metaid || !mops->metatype || !mops->name ||
  153. !mops->check_presence || !mops->encode || !mops->decode ||
  154. !mops->get || !mops->alloc)
  155. return -EINVAL;
  156. write_lock(&ife_mod_lock);
  157. list_for_each_entry(m, &ifeoplist, list) {
  158. if (m->metaid == mops->metaid ||
  159. (strcmp(mops->name, m->name) == 0)) {
  160. write_unlock(&ife_mod_lock);
  161. return -EEXIST;
  162. }
  163. }
  164. if (!mops->release)
  165. mops->release = ife_release_meta_gen;
  166. list_add_tail(&mops->list, &ifeoplist);
  167. write_unlock(&ife_mod_lock);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(unregister_ife_op);
  171. int unregister_ife_op(struct tcf_meta_ops *mops)
  172. {
  173. struct tcf_meta_ops *m;
  174. int err = -ENOENT;
  175. write_lock(&ife_mod_lock);
  176. list_for_each_entry(m, &ifeoplist, list) {
  177. if (m->metaid == mops->metaid) {
  178. list_del(&mops->list);
  179. err = 0;
  180. break;
  181. }
  182. }
  183. write_unlock(&ife_mod_lock);
  184. return err;
  185. }
  186. EXPORT_SYMBOL_GPL(register_ife_op);
  187. static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
  188. {
  189. int ret = 0;
  190. /* XXX: unfortunately cant use nla_policy at this point
  191. * because a length of 0 is valid in the case of
  192. * "allow". "use" semantics do enforce for proper
  193. * length and i couldve use nla_policy but it makes it hard
  194. * to use it just for that..
  195. */
  196. if (ops->validate)
  197. return ops->validate(val, len);
  198. if (ops->metatype == NLA_U32)
  199. ret = ife_validate_meta_u32(val, len);
  200. else if (ops->metatype == NLA_U16)
  201. ret = ife_validate_meta_u16(val, len);
  202. return ret;
  203. }
  204. #ifdef CONFIG_MODULES
  205. static const char *ife_meta_id2name(u32 metaid)
  206. {
  207. switch (metaid) {
  208. case IFE_META_SKBMARK:
  209. return "skbmark";
  210. case IFE_META_PRIO:
  211. return "skbprio";
  212. case IFE_META_TCINDEX:
  213. return "tcindex";
  214. default:
  215. return "unknown";
  216. }
  217. }
  218. #endif
  219. /* called when adding new meta information
  220. */
  221. static int load_metaops_and_vet(u32 metaid, void *val, int len, bool rtnl_held)
  222. {
  223. struct tcf_meta_ops *ops = find_ife_oplist(metaid);
  224. int ret = 0;
  225. if (!ops) {
  226. ret = -ENOENT;
  227. #ifdef CONFIG_MODULES
  228. if (rtnl_held)
  229. rtnl_unlock();
  230. request_module("ife-meta-%s", ife_meta_id2name(metaid));
  231. if (rtnl_held)
  232. rtnl_lock();
  233. ops = find_ife_oplist(metaid);
  234. #endif
  235. }
  236. if (ops) {
  237. ret = 0;
  238. if (len)
  239. ret = ife_validate_metatype(ops, val, len);
  240. module_put(ops->owner);
  241. }
  242. return ret;
  243. }
  244. /* called when adding new meta information
  245. */
  246. static int __add_metainfo(const struct tcf_meta_ops *ops,
  247. struct tcf_ife_params *p, u32 metaid, void *metaval,
  248. int len, bool atomic)
  249. {
  250. struct tcf_meta_info *mi = NULL;
  251. int ret = 0;
  252. mi = kzalloc_obj(*mi, atomic ? GFP_ATOMIC : GFP_KERNEL);
  253. if (!mi)
  254. return -ENOMEM;
  255. mi->metaid = metaid;
  256. mi->ops = ops;
  257. if (len > 0) {
  258. ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
  259. if (ret != 0) {
  260. kfree(mi);
  261. return ret;
  262. }
  263. }
  264. list_add_tail(&mi->metalist, &p->metalist);
  265. return ret;
  266. }
  267. static int add_metainfo_and_get_ops(const struct tcf_meta_ops *ops,
  268. struct tcf_ife_params *p, u32 metaid)
  269. {
  270. int ret;
  271. if (!try_module_get(ops->owner))
  272. return -ENOENT;
  273. ret = __add_metainfo(ops, p, metaid, NULL, 0, true);
  274. if (ret)
  275. module_put(ops->owner);
  276. return ret;
  277. }
  278. static int add_metainfo(struct tcf_ife_params *p, u32 metaid, void *metaval,
  279. int len)
  280. {
  281. const struct tcf_meta_ops *ops = find_ife_oplist(metaid);
  282. int ret;
  283. if (!ops)
  284. return -ENOENT;
  285. ret = __add_metainfo(ops, p, metaid, metaval, len, false);
  286. if (ret)
  287. /*put back what find_ife_oplist took */
  288. module_put(ops->owner);
  289. return ret;
  290. }
  291. static int use_all_metadata(struct tcf_ife_params *p)
  292. {
  293. struct tcf_meta_ops *o;
  294. int rc = 0;
  295. int installed = 0;
  296. read_lock(&ife_mod_lock);
  297. list_for_each_entry(o, &ifeoplist, list) {
  298. rc = add_metainfo_and_get_ops(o, p, o->metaid);
  299. if (rc == 0)
  300. installed += 1;
  301. }
  302. read_unlock(&ife_mod_lock);
  303. if (installed)
  304. return 0;
  305. else
  306. return -EINVAL;
  307. }
  308. static int dump_metalist(struct sk_buff *skb, struct tcf_ife_params *p)
  309. {
  310. struct tcf_meta_info *e;
  311. struct nlattr *nest;
  312. unsigned char *b = skb_tail_pointer(skb);
  313. int total_encoded = 0;
  314. /*can only happen on decode */
  315. if (list_empty(&p->metalist))
  316. return 0;
  317. nest = nla_nest_start_noflag(skb, TCA_IFE_METALST);
  318. if (!nest)
  319. goto out_nlmsg_trim;
  320. list_for_each_entry(e, &p->metalist, metalist) {
  321. if (!e->ops->get(skb, e))
  322. total_encoded += 1;
  323. }
  324. if (!total_encoded)
  325. goto out_nlmsg_trim;
  326. nla_nest_end(skb, nest);
  327. return 0;
  328. out_nlmsg_trim:
  329. nlmsg_trim(skb, b);
  330. return -1;
  331. }
  332. static void __tcf_ife_cleanup(struct tcf_ife_params *p)
  333. {
  334. struct tcf_meta_info *e, *n;
  335. list_for_each_entry_safe(e, n, &p->metalist, metalist) {
  336. list_del(&e->metalist);
  337. if (e->metaval) {
  338. if (e->ops->release)
  339. e->ops->release(e);
  340. else
  341. kfree(e->metaval);
  342. }
  343. module_put(e->ops->owner);
  344. kfree(e);
  345. }
  346. }
  347. static void tcf_ife_cleanup_params(struct rcu_head *head)
  348. {
  349. struct tcf_ife_params *p = container_of(head, struct tcf_ife_params,
  350. rcu);
  351. __tcf_ife_cleanup(p);
  352. kfree(p);
  353. }
  354. static void tcf_ife_cleanup(struct tc_action *a)
  355. {
  356. struct tcf_ife_info *ife = to_ife(a);
  357. struct tcf_ife_params *p;
  358. p = rcu_dereference_protected(ife->params, 1);
  359. if (p)
  360. call_rcu(&p->rcu, tcf_ife_cleanup_params);
  361. }
  362. static int load_metalist(struct nlattr **tb, bool rtnl_held)
  363. {
  364. int i;
  365. for (i = 1; i < max_metacnt; i++) {
  366. if (tb[i]) {
  367. void *val = nla_data(tb[i]);
  368. int len = nla_len(tb[i]);
  369. int rc;
  370. rc = load_metaops_and_vet(i, val, len, rtnl_held);
  371. if (rc != 0)
  372. return rc;
  373. }
  374. }
  375. return 0;
  376. }
  377. static int populate_metalist(struct tcf_ife_params *p, struct nlattr **tb)
  378. {
  379. int len = 0;
  380. int rc = 0;
  381. int i = 0;
  382. void *val;
  383. for (i = 1; i < max_metacnt; i++) {
  384. if (tb[i]) {
  385. val = nla_data(tb[i]);
  386. len = nla_len(tb[i]);
  387. rc = add_metainfo(p, i, val, len);
  388. if (rc)
  389. return rc;
  390. }
  391. }
  392. return rc;
  393. }
  394. static int tcf_ife_init(struct net *net, struct nlattr *nla,
  395. struct nlattr *est, struct tc_action **a,
  396. struct tcf_proto *tp, u32 flags,
  397. struct netlink_ext_ack *extack)
  398. {
  399. struct tc_action_net *tn = net_generic(net, act_ife_ops.net_id);
  400. bool bind = flags & TCA_ACT_FLAGS_BIND;
  401. struct nlattr *tb[TCA_IFE_MAX + 1];
  402. struct nlattr *tb2[IFE_META_MAX + 1];
  403. struct tcf_chain *goto_ch = NULL;
  404. struct tcf_ife_params *p;
  405. struct tcf_ife_info *ife;
  406. u16 ife_type = ETH_P_IFE;
  407. struct tc_ife *parm;
  408. u8 *daddr = NULL;
  409. u8 *saddr = NULL;
  410. bool exists = false;
  411. int ret = 0;
  412. u32 index;
  413. int err;
  414. if (!nla) {
  415. NL_SET_ERR_MSG_MOD(extack, "IFE requires attributes to be passed");
  416. return -EINVAL;
  417. }
  418. err = nla_parse_nested_deprecated(tb, TCA_IFE_MAX, nla, ife_policy,
  419. NULL);
  420. if (err < 0)
  421. return err;
  422. if (!tb[TCA_IFE_PARMS])
  423. return -EINVAL;
  424. parm = nla_data(tb[TCA_IFE_PARMS]);
  425. /* IFE_DECODE is 0 and indicates the opposite of IFE_ENCODE because
  426. * they cannot run as the same time. Check on all other values which
  427. * are not supported right now.
  428. */
  429. if (parm->flags & ~IFE_ENCODE)
  430. return -EINVAL;
  431. p = kzalloc_obj(*p);
  432. if (!p)
  433. return -ENOMEM;
  434. INIT_LIST_HEAD(&p->metalist);
  435. if (tb[TCA_IFE_METALST]) {
  436. err = nla_parse_nested_deprecated(tb2, IFE_META_MAX,
  437. tb[TCA_IFE_METALST], NULL,
  438. NULL);
  439. if (err) {
  440. kfree(p);
  441. return err;
  442. }
  443. err = load_metalist(tb2, !(flags & TCA_ACT_FLAGS_NO_RTNL));
  444. if (err) {
  445. kfree(p);
  446. return err;
  447. }
  448. }
  449. index = parm->index;
  450. err = tcf_idr_check_alloc(tn, &index, a, bind);
  451. if (err < 0) {
  452. kfree(p);
  453. return err;
  454. }
  455. exists = err;
  456. if (exists && bind) {
  457. kfree(p);
  458. return ACT_P_BOUND;
  459. }
  460. if (!exists) {
  461. ret = tcf_idr_create(tn, index, est, a, &act_ife_ops,
  462. bind, true, flags);
  463. if (ret) {
  464. tcf_idr_cleanup(tn, index);
  465. kfree(p);
  466. return ret;
  467. }
  468. ret = ACT_P_CREATED;
  469. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  470. tcf_idr_release(*a, bind);
  471. kfree(p);
  472. return -EEXIST;
  473. }
  474. ife = to_ife(*a);
  475. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  476. if (err < 0)
  477. goto release_idr;
  478. p->flags = parm->flags;
  479. if (parm->flags & IFE_ENCODE) {
  480. if (tb[TCA_IFE_TYPE])
  481. ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
  482. if (tb[TCA_IFE_DMAC])
  483. daddr = nla_data(tb[TCA_IFE_DMAC]);
  484. if (tb[TCA_IFE_SMAC])
  485. saddr = nla_data(tb[TCA_IFE_SMAC]);
  486. }
  487. if (parm->flags & IFE_ENCODE) {
  488. if (daddr)
  489. ether_addr_copy(p->eth_dst, daddr);
  490. else
  491. eth_zero_addr(p->eth_dst);
  492. if (saddr)
  493. ether_addr_copy(p->eth_src, saddr);
  494. else
  495. eth_zero_addr(p->eth_src);
  496. p->eth_type = ife_type;
  497. }
  498. if (tb[TCA_IFE_METALST]) {
  499. err = populate_metalist(p, tb2);
  500. if (err)
  501. goto metadata_parse_err;
  502. } else {
  503. /* if no passed metadata allow list or passed allow-all
  504. * then here we process by adding as many supported metadatum
  505. * as we can. You better have at least one else we are
  506. * going to bail out
  507. */
  508. err = use_all_metadata(p);
  509. if (err)
  510. goto metadata_parse_err;
  511. }
  512. if (exists)
  513. spin_lock_bh(&ife->tcf_lock);
  514. /* protected by tcf_lock when modifying existing action */
  515. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  516. p = rcu_replace_pointer(ife->params, p, 1);
  517. if (exists)
  518. spin_unlock_bh(&ife->tcf_lock);
  519. if (goto_ch)
  520. tcf_chain_put_by_act(goto_ch);
  521. if (p)
  522. call_rcu(&p->rcu, tcf_ife_cleanup_params);
  523. return ret;
  524. metadata_parse_err:
  525. if (goto_ch)
  526. tcf_chain_put_by_act(goto_ch);
  527. release_idr:
  528. __tcf_ife_cleanup(p);
  529. kfree(p);
  530. tcf_idr_release(*a, bind);
  531. return err;
  532. }
  533. static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  534. int ref)
  535. {
  536. unsigned char *b = skb_tail_pointer(skb);
  537. struct tcf_ife_info *ife = to_ife(a);
  538. struct tcf_ife_params *p;
  539. struct tc_ife opt;
  540. struct tcf_t t;
  541. memset(&opt, 0, sizeof(opt));
  542. opt.index = ife->tcf_index;
  543. opt.refcnt = refcount_read(&ife->tcf_refcnt) - ref;
  544. opt.bindcnt = atomic_read(&ife->tcf_bindcnt) - bind;
  545. spin_lock_bh(&ife->tcf_lock);
  546. opt.action = ife->tcf_action;
  547. p = rcu_dereference_protected(ife->params,
  548. lockdep_is_held(&ife->tcf_lock));
  549. opt.flags = p->flags;
  550. if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
  551. goto nla_put_failure;
  552. tcf_tm_dump(&t, &ife->tcf_tm);
  553. if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
  554. goto nla_put_failure;
  555. if (!is_zero_ether_addr(p->eth_dst)) {
  556. if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst))
  557. goto nla_put_failure;
  558. }
  559. if (!is_zero_ether_addr(p->eth_src)) {
  560. if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src))
  561. goto nla_put_failure;
  562. }
  563. if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type))
  564. goto nla_put_failure;
  565. if (dump_metalist(skb, p)) {
  566. /*ignore failure to dump metalist */
  567. pr_info("Failed to dump metalist\n");
  568. }
  569. spin_unlock_bh(&ife->tcf_lock);
  570. return skb->len;
  571. nla_put_failure:
  572. spin_unlock_bh(&ife->tcf_lock);
  573. nlmsg_trim(skb, b);
  574. return -1;
  575. }
  576. static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_params *p,
  577. u16 metaid, u16 mlen, void *mdata)
  578. {
  579. struct tcf_meta_info *e;
  580. /* XXX: use hash to speed up */
  581. list_for_each_entry_rcu(e, &p->metalist, metalist) {
  582. if (metaid == e->metaid) {
  583. if (e->ops) {
  584. /* We check for decode presence already */
  585. return e->ops->decode(skb, mdata, mlen);
  586. }
  587. }
  588. }
  589. return -ENOENT;
  590. }
  591. static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
  592. struct tcf_result *res)
  593. {
  594. struct tcf_ife_info *ife = to_ife(a);
  595. int action = ife->tcf_action;
  596. struct tcf_ife_params *p;
  597. u8 *ifehdr_end;
  598. u8 *tlv_data;
  599. u16 metalen;
  600. p = rcu_dereference_bh(ife->params);
  601. bstats_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
  602. tcf_lastuse_update(&ife->tcf_tm);
  603. if (skb_at_tc_ingress(skb))
  604. skb_push(skb, skb->dev->hard_header_len);
  605. tlv_data = ife_decode(skb, &metalen);
  606. if (unlikely(!tlv_data)) {
  607. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  608. return TC_ACT_SHOT;
  609. }
  610. ifehdr_end = tlv_data + metalen;
  611. for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
  612. u8 *curr_data;
  613. u16 mtype;
  614. u16 dlen;
  615. curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype,
  616. &dlen, NULL);
  617. if (!curr_data) {
  618. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  619. return TC_ACT_SHOT;
  620. }
  621. if (find_decode_metaid(skb, p, mtype, dlen, curr_data)) {
  622. /* abuse overlimits to count when we receive metadata
  623. * but dont have an ops for it
  624. */
  625. pr_info_ratelimited("Unknown metaid %d dlen %d\n",
  626. mtype, dlen);
  627. qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
  628. }
  629. }
  630. if (WARN_ON(tlv_data != ifehdr_end)) {
  631. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  632. return TC_ACT_SHOT;
  633. }
  634. skb->protocol = eth_type_trans(skb, skb->dev);
  635. skb_reset_network_header(skb);
  636. return action;
  637. }
  638. /*XXX: check if we can do this at install time instead of current
  639. * send data path
  640. **/
  641. static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_params *p)
  642. {
  643. struct tcf_meta_info *e;
  644. int tot_run_sz = 0, run_sz = 0;
  645. list_for_each_entry_rcu(e, &p->metalist, metalist) {
  646. if (e->ops->check_presence) {
  647. run_sz = e->ops->check_presence(skb, e);
  648. tot_run_sz += run_sz;
  649. }
  650. }
  651. return tot_run_sz;
  652. }
  653. static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
  654. struct tcf_result *res, struct tcf_ife_params *p)
  655. {
  656. struct tcf_ife_info *ife = to_ife(a);
  657. int action = ife->tcf_action;
  658. struct ethhdr *oethh; /* outer ether header */
  659. struct tcf_meta_info *e;
  660. /*
  661. OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
  662. where ORIGDATA = original ethernet header ...
  663. */
  664. u16 metalen = ife_get_sz(skb, p);
  665. int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
  666. unsigned int skboff = 0;
  667. int new_len = skb->len + hdrm;
  668. bool exceed_mtu = false;
  669. void *ife_meta;
  670. int err = 0;
  671. if (!skb_at_tc_ingress(skb)) {
  672. if (new_len > skb->dev->mtu)
  673. exceed_mtu = true;
  674. }
  675. bstats_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
  676. tcf_lastuse_update(&ife->tcf_tm);
  677. if (!metalen) { /* no metadata to send */
  678. /* abuse overlimits to count when we allow packet
  679. * with no metadata
  680. */
  681. qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
  682. return action;
  683. }
  684. /* could be stupid policy setup or mtu config
  685. * so lets be conservative.. */
  686. if ((action == TC_ACT_SHOT) || exceed_mtu) {
  687. drop:
  688. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  689. return TC_ACT_SHOT;
  690. }
  691. if (skb_at_tc_ingress(skb))
  692. skb_push(skb, skb->dev->hard_header_len);
  693. ife_meta = ife_encode(skb, metalen);
  694. if (!ife_meta)
  695. goto drop;
  696. /* XXX: we dont have a clever way of telling encode to
  697. * not repeat some of the computations that are done by
  698. * ops->presence_check...
  699. */
  700. list_for_each_entry_rcu(e, &p->metalist, metalist) {
  701. if (e->ops->encode) {
  702. err = e->ops->encode(skb, (void *)(ife_meta + skboff),
  703. e);
  704. }
  705. if (err < 0) {
  706. /* too corrupt to keep around if overwritten */
  707. goto drop;
  708. }
  709. skboff += err;
  710. }
  711. oethh = (struct ethhdr *)skb->data;
  712. if (!is_zero_ether_addr(p->eth_src))
  713. ether_addr_copy(oethh->h_source, p->eth_src);
  714. if (!is_zero_ether_addr(p->eth_dst))
  715. ether_addr_copy(oethh->h_dest, p->eth_dst);
  716. oethh->h_proto = htons(p->eth_type);
  717. if (skb_at_tc_ingress(skb))
  718. skb_pull(skb, skb->dev->hard_header_len);
  719. return action;
  720. }
  721. TC_INDIRECT_SCOPE int tcf_ife_act(struct sk_buff *skb,
  722. const struct tc_action *a,
  723. struct tcf_result *res)
  724. {
  725. struct tcf_ife_info *ife = to_ife(a);
  726. struct tcf_ife_params *p;
  727. int ret;
  728. p = rcu_dereference_bh(ife->params);
  729. if (p->flags & IFE_ENCODE) {
  730. ret = tcf_ife_encode(skb, a, res, p);
  731. return ret;
  732. }
  733. return tcf_ife_decode(skb, a, res);
  734. }
  735. static struct tc_action_ops act_ife_ops = {
  736. .kind = "ife",
  737. .id = TCA_ID_IFE,
  738. .owner = THIS_MODULE,
  739. .act = tcf_ife_act,
  740. .dump = tcf_ife_dump,
  741. .cleanup = tcf_ife_cleanup,
  742. .init = tcf_ife_init,
  743. .size = sizeof(struct tcf_ife_info),
  744. };
  745. MODULE_ALIAS_NET_ACT("ife");
  746. static __net_init int ife_init_net(struct net *net)
  747. {
  748. struct tc_action_net *tn = net_generic(net, act_ife_ops.net_id);
  749. return tc_action_net_init(net, tn, &act_ife_ops);
  750. }
  751. static void __net_exit ife_exit_net(struct list_head *net_list)
  752. {
  753. tc_action_net_exit(net_list, act_ife_ops.net_id);
  754. }
  755. static struct pernet_operations ife_net_ops = {
  756. .init = ife_init_net,
  757. .exit_batch = ife_exit_net,
  758. .id = &act_ife_ops.net_id,
  759. .size = sizeof(struct tc_action_net),
  760. };
  761. static int __init ife_init_module(void)
  762. {
  763. return tcf_register_action(&act_ife_ops, &ife_net_ops);
  764. }
  765. static void __exit ife_cleanup_module(void)
  766. {
  767. tcf_unregister_action(&act_ife_ops, &ife_net_ops);
  768. }
  769. module_init(ife_init_module);
  770. module_exit(ife_cleanup_module);
  771. MODULE_AUTHOR("Jamal Hadi Salim(2015)");
  772. MODULE_DESCRIPTION("Inter-FE LFB action");
  773. MODULE_LICENSE("GPL");