act_ife.c 20 KB

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