act_ife.c 19 KB

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