act_ife.c 19 KB

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