act_ife.c 19 KB

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