act_ife.c 20 KB

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