act_ife.c 20 KB

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