act_ife.c 20 KB

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