act_ife.c 21 KB

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