act_ife.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. static unsigned int ife_net_id;
  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_info *ife, u32 metaid, void *metaval,
  248. int len, bool atomic, bool exists)
  249. {
  250. struct tcf_meta_info *mi = NULL;
  251. int ret = 0;
  252. mi = kzalloc(sizeof(*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. if (exists)
  265. spin_lock_bh(&ife->tcf_lock);
  266. list_add_tail(&mi->metalist, &ife->metalist);
  267. if (exists)
  268. spin_unlock_bh(&ife->tcf_lock);
  269. return ret;
  270. }
  271. static int add_metainfo_and_get_ops(const struct tcf_meta_ops *ops,
  272. struct tcf_ife_info *ife, u32 metaid,
  273. bool exists)
  274. {
  275. int ret;
  276. if (!try_module_get(ops->owner))
  277. return -ENOENT;
  278. ret = __add_metainfo(ops, ife, metaid, NULL, 0, true, exists);
  279. if (ret)
  280. module_put(ops->owner);
  281. return ret;
  282. }
  283. static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
  284. int len, bool exists)
  285. {
  286. const struct tcf_meta_ops *ops = find_ife_oplist(metaid);
  287. int ret;
  288. if (!ops)
  289. return -ENOENT;
  290. ret = __add_metainfo(ops, ife, metaid, metaval, len, false, exists);
  291. if (ret)
  292. /*put back what find_ife_oplist took */
  293. module_put(ops->owner);
  294. return ret;
  295. }
  296. static int use_all_metadata(struct tcf_ife_info *ife, bool exists)
  297. {
  298. struct tcf_meta_ops *o;
  299. int rc = 0;
  300. int installed = 0;
  301. read_lock(&ife_mod_lock);
  302. list_for_each_entry(o, &ifeoplist, list) {
  303. rc = add_metainfo_and_get_ops(o, ife, o->metaid, exists);
  304. if (rc == 0)
  305. installed += 1;
  306. }
  307. read_unlock(&ife_mod_lock);
  308. if (installed)
  309. return 0;
  310. else
  311. return -EINVAL;
  312. }
  313. static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
  314. {
  315. struct tcf_meta_info *e;
  316. struct nlattr *nest;
  317. unsigned char *b = skb_tail_pointer(skb);
  318. int total_encoded = 0;
  319. /*can only happen on decode */
  320. if (list_empty(&ife->metalist))
  321. return 0;
  322. nest = nla_nest_start_noflag(skb, TCA_IFE_METALST);
  323. if (!nest)
  324. goto out_nlmsg_trim;
  325. list_for_each_entry(e, &ife->metalist, metalist) {
  326. if (!e->ops->get(skb, e))
  327. total_encoded += 1;
  328. }
  329. if (!total_encoded)
  330. goto out_nlmsg_trim;
  331. nla_nest_end(skb, nest);
  332. return 0;
  333. out_nlmsg_trim:
  334. nlmsg_trim(skb, b);
  335. return -1;
  336. }
  337. /* under ife->tcf_lock */
  338. static void _tcf_ife_cleanup(struct tc_action *a)
  339. {
  340. struct tcf_ife_info *ife = to_ife(a);
  341. struct tcf_meta_info *e, *n;
  342. list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
  343. list_del(&e->metalist);
  344. if (e->metaval) {
  345. if (e->ops->release)
  346. e->ops->release(e);
  347. else
  348. kfree(e->metaval);
  349. }
  350. module_put(e->ops->owner);
  351. kfree(e);
  352. }
  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. spin_lock_bh(&ife->tcf_lock);
  359. _tcf_ife_cleanup(a);
  360. spin_unlock_bh(&ife->tcf_lock);
  361. p = rcu_dereference_protected(ife->params, 1);
  362. if (p)
  363. kfree_rcu(p, rcu);
  364. }
  365. static int load_metalist(struct nlattr **tb, bool rtnl_held)
  366. {
  367. int i;
  368. for (i = 1; i < max_metacnt; i++) {
  369. if (tb[i]) {
  370. void *val = nla_data(tb[i]);
  371. int len = nla_len(tb[i]);
  372. int rc;
  373. rc = load_metaops_and_vet(i, val, len, rtnl_held);
  374. if (rc != 0)
  375. return rc;
  376. }
  377. }
  378. return 0;
  379. }
  380. static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
  381. bool exists, bool rtnl_held)
  382. {
  383. int len = 0;
  384. int rc = 0;
  385. int i = 0;
  386. void *val;
  387. for (i = 1; i < max_metacnt; i++) {
  388. if (tb[i]) {
  389. val = nla_data(tb[i]);
  390. len = nla_len(tb[i]);
  391. rc = add_metainfo(ife, i, val, len, exists);
  392. if (rc)
  393. return rc;
  394. }
  395. }
  396. return rc;
  397. }
  398. static int tcf_ife_init(struct net *net, struct nlattr *nla,
  399. struct nlattr *est, struct tc_action **a,
  400. int ovr, int bind, bool rtnl_held,
  401. struct tcf_proto *tp, u32 flags,
  402. struct netlink_ext_ack *extack)
  403. {
  404. struct tc_action_net *tn = net_generic(net, ife_net_id);
  405. struct nlattr *tb[TCA_IFE_MAX + 1];
  406. struct nlattr *tb2[IFE_META_MAX + 1];
  407. struct tcf_chain *goto_ch = NULL;
  408. struct tcf_ife_params *p;
  409. struct tcf_ife_info *ife;
  410. u16 ife_type = ETH_P_IFE;
  411. struct tc_ife *parm;
  412. u8 *daddr = NULL;
  413. u8 *saddr = NULL;
  414. bool exists = false;
  415. int ret = 0;
  416. u32 index;
  417. int err;
  418. if (!nla) {
  419. NL_SET_ERR_MSG_MOD(extack, "IFE requires attributes to be passed");
  420. return -EINVAL;
  421. }
  422. err = nla_parse_nested_deprecated(tb, TCA_IFE_MAX, nla, ife_policy,
  423. NULL);
  424. if (err < 0)
  425. return err;
  426. if (!tb[TCA_IFE_PARMS])
  427. return -EINVAL;
  428. parm = nla_data(tb[TCA_IFE_PARMS]);
  429. /* IFE_DECODE is 0 and indicates the opposite of IFE_ENCODE because
  430. * they cannot run as the same time. Check on all other values which
  431. * are not supported right now.
  432. */
  433. if (parm->flags & ~IFE_ENCODE)
  434. return -EINVAL;
  435. p = kzalloc(sizeof(*p), GFP_KERNEL);
  436. if (!p)
  437. return -ENOMEM;
  438. if (tb[TCA_IFE_METALST]) {
  439. err = nla_parse_nested_deprecated(tb2, IFE_META_MAX,
  440. tb[TCA_IFE_METALST], NULL,
  441. NULL);
  442. if (err) {
  443. kfree(p);
  444. return err;
  445. }
  446. err = load_metalist(tb2, rtnl_held);
  447. if (err) {
  448. kfree(p);
  449. return err;
  450. }
  451. }
  452. index = parm->index;
  453. err = tcf_idr_check_alloc(tn, &index, a, bind);
  454. if (err < 0) {
  455. kfree(p);
  456. return err;
  457. }
  458. exists = err;
  459. if (exists && bind) {
  460. kfree(p);
  461. return 0;
  462. }
  463. if (!exists) {
  464. ret = tcf_idr_create(tn, index, est, a, &act_ife_ops,
  465. bind, true, 0);
  466. if (ret) {
  467. tcf_idr_cleanup(tn, index);
  468. kfree(p);
  469. return ret;
  470. }
  471. ret = ACT_P_CREATED;
  472. } else if (!ovr) {
  473. tcf_idr_release(*a, bind);
  474. kfree(p);
  475. return -EEXIST;
  476. }
  477. ife = to_ife(*a);
  478. if (ret == ACT_P_CREATED)
  479. INIT_LIST_HEAD(&ife->metalist);
  480. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  481. if (err < 0)
  482. goto release_idr;
  483. p->flags = parm->flags;
  484. if (parm->flags & IFE_ENCODE) {
  485. if (tb[TCA_IFE_TYPE])
  486. ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
  487. if (tb[TCA_IFE_DMAC])
  488. daddr = nla_data(tb[TCA_IFE_DMAC]);
  489. if (tb[TCA_IFE_SMAC])
  490. saddr = nla_data(tb[TCA_IFE_SMAC]);
  491. }
  492. if (parm->flags & IFE_ENCODE) {
  493. if (daddr)
  494. ether_addr_copy(p->eth_dst, daddr);
  495. else
  496. eth_zero_addr(p->eth_dst);
  497. if (saddr)
  498. ether_addr_copy(p->eth_src, saddr);
  499. else
  500. eth_zero_addr(p->eth_src);
  501. p->eth_type = ife_type;
  502. }
  503. if (tb[TCA_IFE_METALST]) {
  504. err = populate_metalist(ife, tb2, exists, rtnl_held);
  505. if (err)
  506. goto metadata_parse_err;
  507. } else {
  508. /* if no passed metadata allow list or passed allow-all
  509. * then here we process by adding as many supported metadatum
  510. * as we can. You better have at least one else we are
  511. * going to bail out
  512. */
  513. err = use_all_metadata(ife, exists);
  514. if (err)
  515. goto metadata_parse_err;
  516. }
  517. if (exists)
  518. spin_lock_bh(&ife->tcf_lock);
  519. /* protected by tcf_lock when modifying existing action */
  520. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  521. p = rcu_replace_pointer(ife->params, p, 1);
  522. if (exists)
  523. spin_unlock_bh(&ife->tcf_lock);
  524. if (goto_ch)
  525. tcf_chain_put_by_act(goto_ch);
  526. if (p)
  527. kfree_rcu(p, rcu);
  528. return ret;
  529. metadata_parse_err:
  530. if (goto_ch)
  531. tcf_chain_put_by_act(goto_ch);
  532. release_idr:
  533. kfree(p);
  534. tcf_idr_release(*a, bind);
  535. return err;
  536. }
  537. static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  538. int ref)
  539. {
  540. unsigned char *b = skb_tail_pointer(skb);
  541. struct tcf_ife_info *ife = to_ife(a);
  542. struct tcf_ife_params *p;
  543. struct tc_ife opt = {
  544. .index = ife->tcf_index,
  545. .refcnt = refcount_read(&ife->tcf_refcnt) - ref,
  546. .bindcnt = atomic_read(&ife->tcf_bindcnt) - bind,
  547. };
  548. struct tcf_t t;
  549. spin_lock_bh(&ife->tcf_lock);
  550. opt.action = ife->tcf_action;
  551. p = rcu_dereference_protected(ife->params,
  552. lockdep_is_held(&ife->tcf_lock));
  553. opt.flags = p->flags;
  554. if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
  555. goto nla_put_failure;
  556. tcf_tm_dump(&t, &ife->tcf_tm);
  557. if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
  558. goto nla_put_failure;
  559. if (!is_zero_ether_addr(p->eth_dst)) {
  560. if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst))
  561. goto nla_put_failure;
  562. }
  563. if (!is_zero_ether_addr(p->eth_src)) {
  564. if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src))
  565. goto nla_put_failure;
  566. }
  567. if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type))
  568. goto nla_put_failure;
  569. if (dump_metalist(skb, ife)) {
  570. /*ignore failure to dump metalist */
  571. pr_info("Failed to dump metalist\n");
  572. }
  573. spin_unlock_bh(&ife->tcf_lock);
  574. return skb->len;
  575. nla_put_failure:
  576. spin_unlock_bh(&ife->tcf_lock);
  577. nlmsg_trim(skb, b);
  578. return -1;
  579. }
  580. static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
  581. u16 metaid, u16 mlen, void *mdata)
  582. {
  583. struct tcf_meta_info *e;
  584. /* XXX: use hash to speed up */
  585. list_for_each_entry(e, &ife->metalist, metalist) {
  586. if (metaid == e->metaid) {
  587. if (e->ops) {
  588. /* We check for decode presence already */
  589. return e->ops->decode(skb, mdata, mlen);
  590. }
  591. }
  592. }
  593. return -ENOENT;
  594. }
  595. static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
  596. struct tcf_result *res)
  597. {
  598. struct tcf_ife_info *ife = to_ife(a);
  599. int action = ife->tcf_action;
  600. u8 *ifehdr_end;
  601. u8 *tlv_data;
  602. u16 metalen;
  603. bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
  604. tcf_lastuse_update(&ife->tcf_tm);
  605. if (skb_at_tc_ingress(skb))
  606. skb_push(skb, skb->dev->hard_header_len);
  607. tlv_data = ife_decode(skb, &metalen);
  608. if (unlikely(!tlv_data)) {
  609. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  610. return TC_ACT_SHOT;
  611. }
  612. ifehdr_end = tlv_data + metalen;
  613. for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
  614. u8 *curr_data;
  615. u16 mtype;
  616. u16 dlen;
  617. curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype,
  618. &dlen, NULL);
  619. if (!curr_data) {
  620. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  621. return TC_ACT_SHOT;
  622. }
  623. if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) {
  624. /* abuse overlimits to count when we receive metadata
  625. * but dont have an ops for it
  626. */
  627. pr_info_ratelimited("Unknown metaid %d dlen %d\n",
  628. mtype, dlen);
  629. qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
  630. }
  631. }
  632. if (WARN_ON(tlv_data != ifehdr_end)) {
  633. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  634. return TC_ACT_SHOT;
  635. }
  636. skb->protocol = eth_type_trans(skb, skb->dev);
  637. skb_reset_network_header(skb);
  638. return action;
  639. }
  640. /*XXX: check if we can do this at install time instead of current
  641. * send data path
  642. **/
  643. static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
  644. {
  645. struct tcf_meta_info *e, *n;
  646. int tot_run_sz = 0, run_sz = 0;
  647. list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
  648. if (e->ops->check_presence) {
  649. run_sz = e->ops->check_presence(skb, e);
  650. tot_run_sz += run_sz;
  651. }
  652. }
  653. return tot_run_sz;
  654. }
  655. static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
  656. struct tcf_result *res, struct tcf_ife_params *p)
  657. {
  658. struct tcf_ife_info *ife = to_ife(a);
  659. int action = ife->tcf_action;
  660. struct ethhdr *oethh; /* outer ether header */
  661. struct tcf_meta_info *e;
  662. /*
  663. OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
  664. where ORIGDATA = original ethernet header ...
  665. */
  666. u16 metalen = ife_get_sz(skb, ife);
  667. int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
  668. unsigned int skboff = 0;
  669. int new_len = skb->len + hdrm;
  670. bool exceed_mtu = false;
  671. void *ife_meta;
  672. int err = 0;
  673. if (!skb_at_tc_ingress(skb)) {
  674. if (new_len > skb->dev->mtu)
  675. exceed_mtu = true;
  676. }
  677. bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
  678. tcf_lastuse_update(&ife->tcf_tm);
  679. if (!metalen) { /* no metadata to send */
  680. /* abuse overlimits to count when we allow packet
  681. * with no metadata
  682. */
  683. qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
  684. return action;
  685. }
  686. /* could be stupid policy setup or mtu config
  687. * so lets be conservative.. */
  688. if ((action == TC_ACT_SHOT) || exceed_mtu) {
  689. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  690. return TC_ACT_SHOT;
  691. }
  692. if (skb_at_tc_ingress(skb))
  693. skb_push(skb, skb->dev->hard_header_len);
  694. ife_meta = ife_encode(skb, metalen);
  695. spin_lock(&ife->tcf_lock);
  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(e, &ife->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. spin_unlock(&ife->tcf_lock);
  708. qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
  709. return TC_ACT_SHOT;
  710. }
  711. skboff += err;
  712. }
  713. spin_unlock(&ife->tcf_lock);
  714. oethh = (struct ethhdr *)skb->data;
  715. if (!is_zero_ether_addr(p->eth_src))
  716. ether_addr_copy(oethh->h_source, p->eth_src);
  717. if (!is_zero_ether_addr(p->eth_dst))
  718. ether_addr_copy(oethh->h_dest, p->eth_dst);
  719. oethh->h_proto = htons(p->eth_type);
  720. if (skb_at_tc_ingress(skb))
  721. skb_pull(skb, skb->dev->hard_header_len);
  722. return action;
  723. }
  724. static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
  725. struct tcf_result *res)
  726. {
  727. struct tcf_ife_info *ife = to_ife(a);
  728. struct tcf_ife_params *p;
  729. int ret;
  730. p = rcu_dereference_bh(ife->params);
  731. if (p->flags & IFE_ENCODE) {
  732. ret = tcf_ife_encode(skb, a, res, p);
  733. return ret;
  734. }
  735. return tcf_ife_decode(skb, a, res);
  736. }
  737. static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
  738. struct netlink_callback *cb, int type,
  739. const struct tc_action_ops *ops,
  740. struct netlink_ext_ack *extack)
  741. {
  742. struct tc_action_net *tn = net_generic(net, ife_net_id);
  743. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  744. }
  745. static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
  746. {
  747. struct tc_action_net *tn = net_generic(net, ife_net_id);
  748. return tcf_idr_search(tn, a, index);
  749. }
  750. static struct tc_action_ops act_ife_ops = {
  751. .kind = "ife",
  752. .id = TCA_ID_IFE,
  753. .owner = THIS_MODULE,
  754. .act = tcf_ife_act,
  755. .dump = tcf_ife_dump,
  756. .cleanup = tcf_ife_cleanup,
  757. .init = tcf_ife_init,
  758. .walk = tcf_ife_walker,
  759. .lookup = tcf_ife_search,
  760. .size = sizeof(struct tcf_ife_info),
  761. };
  762. static __net_init int ife_init_net(struct net *net)
  763. {
  764. struct tc_action_net *tn = net_generic(net, ife_net_id);
  765. return tc_action_net_init(net, tn, &act_ife_ops);
  766. }
  767. static void __net_exit ife_exit_net(struct list_head *net_list)
  768. {
  769. tc_action_net_exit(net_list, ife_net_id);
  770. }
  771. static struct pernet_operations ife_net_ops = {
  772. .init = ife_init_net,
  773. .exit_batch = ife_exit_net,
  774. .id = &ife_net_id,
  775. .size = sizeof(struct tc_action_net),
  776. };
  777. static int __init ife_init_module(void)
  778. {
  779. return tcf_register_action(&act_ife_ops, &ife_net_ops);
  780. }
  781. static void __exit ife_cleanup_module(void)
  782. {
  783. tcf_unregister_action(&act_ife_ops, &ife_net_ops);
  784. }
  785. module_init(ife_init_module);
  786. module_exit(ife_cleanup_module);
  787. MODULE_AUTHOR("Jamal Hadi Salim(2015)");
  788. MODULE_DESCRIPTION("Inter-FE LFB action");
  789. MODULE_LICENSE("GPL");