ematch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * lib/route/cls/ematch.c Extended Matches
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2008-2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup cls
  13. * @defgroup ematch Extended Match
  14. *
  15. * @{
  16. */
  17. #include <netlink-private/netlink.h>
  18. #include <netlink-private/tc.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/route/classifier.h>
  21. #include <netlink/route/cls/ematch.h>
  22. #include <netlink/route/cls/ematch/cmp.h>
  23. #include "ematch_syntax.h"
  24. #include "ematch_grammar.h"
  25. /**
  26. * @name Module API
  27. * @{
  28. */
  29. static NL_LIST_HEAD(ematch_ops_list);
  30. /**
  31. * Register ematch module
  32. * @arg ops Module operations.
  33. *
  34. * This function must be called by each ematch module at initialization
  35. * time. It registers the calling module as available module.
  36. *
  37. * @return 0 on success or a negative error code.
  38. */
  39. int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
  40. {
  41. if (rtnl_ematch_lookup_ops(ops->eo_kind))
  42. return -NLE_EXIST;
  43. NL_DBG(1, "ematch module \"%s\" registered\n", ops->eo_name);
  44. nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
  45. return 0;
  46. }
  47. /**
  48. * Lookup ematch module by identification number.
  49. * @arg kind Module kind.
  50. *
  51. * Searches the list of registered ematch modules for match and returns it.
  52. *
  53. * @return Module operations or NULL if not found.
  54. */
  55. struct rtnl_ematch_ops *rtnl_ematch_lookup_ops(int kind)
  56. {
  57. struct rtnl_ematch_ops *ops;
  58. nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
  59. if (ops->eo_kind == kind)
  60. return ops;
  61. return NULL;
  62. }
  63. /**
  64. * Lookup ematch module by name
  65. * @arg name Name of ematch module.
  66. *
  67. * Searches the list of registered ematch modules for a match and returns it.
  68. *
  69. * @return Module operations or NULL if not fuond.
  70. */
  71. struct rtnl_ematch_ops *rtnl_ematch_lookup_ops_by_name(const char *name)
  72. {
  73. struct rtnl_ematch_ops *ops;
  74. nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
  75. if (!strcasecmp(ops->eo_name, name))
  76. return ops;
  77. return NULL;
  78. }
  79. /** @} */
  80. /**
  81. * @name Match
  82. */
  83. /**
  84. * Allocate ematch object.
  85. *
  86. * Allocates and initializes an ematch object.
  87. *
  88. * @return New ematch object or NULL.
  89. */
  90. struct rtnl_ematch *rtnl_ematch_alloc(void)
  91. {
  92. struct rtnl_ematch *e;
  93. if (!(e = calloc(1, sizeof(*e))))
  94. return NULL;
  95. NL_DBG(2, "allocated ematch %p\n", e);
  96. NL_INIT_LIST_HEAD(&e->e_list);
  97. NL_INIT_LIST_HEAD(&e->e_childs);
  98. return e;
  99. }
  100. /**
  101. * Add ematch to the end of the parent's list of children.
  102. * @arg parent parent ematch object
  103. * @arg child ematch object to be added to parent
  104. *
  105. * The parent must be a container ematch.
  106. */
  107. int rtnl_ematch_add_child(struct rtnl_ematch *parent,
  108. struct rtnl_ematch *child)
  109. {
  110. if (parent->e_kind != TCF_EM_CONTAINER)
  111. return -NLE_OPNOTSUPP;
  112. NL_DBG(2, "added ematch %p \"%s\" to container %p\n",
  113. child, child->e_ops->eo_name, parent);
  114. nl_list_add_tail(&child->e_list, &parent->e_childs);
  115. return 0;
  116. }
  117. /**
  118. * Remove ematch from the list of ematches it is linked to.
  119. * @arg ematch ematch object
  120. */
  121. void rtnl_ematch_unlink(struct rtnl_ematch *ematch)
  122. {
  123. NL_DBG(2, "unlinked ematch %p from any lists\n", ematch);
  124. if (!nl_list_empty(&ematch->e_childs))
  125. NL_DBG(1, "warning: ematch %p with childs was unlinked\n",
  126. ematch);
  127. nl_list_del(&ematch->e_list);
  128. nl_init_list_head(&ematch->e_list);
  129. }
  130. void rtnl_ematch_free(struct rtnl_ematch *ematch)
  131. {
  132. NL_DBG(2, "freed ematch %p\n", ematch);
  133. rtnl_ematch_unlink(ematch);
  134. free(ematch->e_data);
  135. free(ematch);
  136. }
  137. int rtnl_ematch_set_ops(struct rtnl_ematch *ematch, struct rtnl_ematch_ops *ops)
  138. {
  139. if (ematch->e_ops)
  140. return -NLE_EXIST;
  141. ematch->e_ops = ops;
  142. ematch->e_kind = ops->eo_kind;
  143. if (ops->eo_datalen) {
  144. ematch->e_data = calloc(1, ops->eo_datalen);
  145. if (!ematch->e_data)
  146. return -NLE_NOMEM;
  147. ematch->e_datalen = ops->eo_datalen;
  148. }
  149. return 0;
  150. }
  151. int rtnl_ematch_set_kind(struct rtnl_ematch *ematch, uint16_t kind)
  152. {
  153. struct rtnl_ematch_ops *ops;
  154. if (ematch->e_kind)
  155. return -NLE_EXIST;
  156. ematch->e_kind = kind;
  157. if ((ops = rtnl_ematch_lookup_ops(kind)))
  158. rtnl_ematch_set_ops(ematch, ops);
  159. return 0;
  160. }
  161. int rtnl_ematch_set_name(struct rtnl_ematch *ematch, const char *name)
  162. {
  163. struct rtnl_ematch_ops *ops;
  164. if (ematch->e_kind)
  165. return -NLE_EXIST;
  166. if (!(ops = rtnl_ematch_lookup_ops_by_name(name)))
  167. return -NLE_OPNOTSUPP;
  168. rtnl_ematch_set_ops(ematch, ops);
  169. return 0;
  170. }
  171. void rtnl_ematch_set_flags(struct rtnl_ematch *ematch, uint16_t flags)
  172. {
  173. ematch->e_flags |= flags;
  174. }
  175. void rtnl_ematch_unset_flags(struct rtnl_ematch *ematch, uint16_t flags)
  176. {
  177. ematch->e_flags &= ~flags;
  178. }
  179. uint16_t rtnl_ematch_get_flags(struct rtnl_ematch *ematch)
  180. {
  181. return ematch->e_flags;
  182. }
  183. void *rtnl_ematch_data(struct rtnl_ematch *ematch)
  184. {
  185. return ematch->e_data;
  186. }
  187. /** @} */
  188. /**
  189. * @name Tree
  190. */
  191. /**
  192. * Allocate ematch tree object
  193. * @arg progid program id
  194. */
  195. struct rtnl_ematch_tree *rtnl_ematch_tree_alloc(uint16_t progid)
  196. {
  197. struct rtnl_ematch_tree *tree;
  198. if (!(tree = calloc(1, sizeof(*tree))))
  199. return NULL;
  200. NL_INIT_LIST_HEAD(&tree->et_list);
  201. tree->et_progid = progid;
  202. NL_DBG(2, "allocated new ematch tree %p, progid=%u\n", tree, progid);
  203. return tree;
  204. }
  205. static void free_ematch_list(struct nl_list_head *head)
  206. {
  207. struct rtnl_ematch *pos, *next;
  208. nl_list_for_each_entry_safe(pos, next, head, e_list) {
  209. if (!nl_list_empty(&pos->e_childs))
  210. free_ematch_list(&pos->e_childs);
  211. rtnl_ematch_free(pos);
  212. }
  213. }
  214. /**
  215. * Free ematch tree object
  216. * @arg tree ematch tree object
  217. *
  218. * This function frees the ematch tree and all ematches attached to it.
  219. */
  220. void rtnl_ematch_tree_free(struct rtnl_ematch_tree *tree)
  221. {
  222. if (!tree)
  223. return;
  224. free_ematch_list(&tree->et_list);
  225. NL_DBG(2, "Freed ematch tree %p\n", tree);
  226. free(tree);
  227. }
  228. /**
  229. * Add ematch object to the end of the ematch tree
  230. * @arg tree ematch tree object
  231. * @arg ematch ematch object to add
  232. */
  233. void rtnl_ematch_tree_add(struct rtnl_ematch_tree *tree,
  234. struct rtnl_ematch *ematch)
  235. {
  236. nl_list_add_tail(&ematch->e_list, &tree->et_list);
  237. }
  238. static inline uint32_t container_ref(struct rtnl_ematch *ematch)
  239. {
  240. return *((uint32_t *) rtnl_ematch_data(ematch));
  241. }
  242. static int link_tree(struct rtnl_ematch *index[], int nmatches, int pos,
  243. struct nl_list_head *root)
  244. {
  245. struct rtnl_ematch *ematch;
  246. int i;
  247. for (i = pos; i < nmatches; i++) {
  248. ematch = index[i];
  249. nl_list_add_tail(&ematch->e_list, root);
  250. if (ematch->e_kind == TCF_EM_CONTAINER)
  251. link_tree(index, nmatches, container_ref(ematch),
  252. &ematch->e_childs);
  253. if (!(ematch->e_flags & TCF_EM_REL_MASK))
  254. return 0;
  255. }
  256. /* Last entry in chain can't possibly have no relation */
  257. return -NLE_INVAL;
  258. }
  259. static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
  260. [TCA_EMATCH_TREE_HDR] = { .minlen=sizeof(struct tcf_ematch_tree_hdr) },
  261. [TCA_EMATCH_TREE_LIST] = { .type = NLA_NESTED },
  262. };
  263. /**
  264. * Parse ematch netlink attributes
  265. *
  266. * @return 0 on success or a negative error code.
  267. */
  268. int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result)
  269. {
  270. struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
  271. struct tcf_ematch_tree_hdr *thdr;
  272. struct rtnl_ematch_tree *tree;
  273. struct rtnl_ematch **index;
  274. int nmatches = 0, err, remaining;
  275. NL_DBG(2, "Parsing attribute %p as ematch tree\n", attr);
  276. err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, attr, tree_policy);
  277. if (err < 0)
  278. return err;
  279. if (!tb[TCA_EMATCH_TREE_HDR])
  280. return -NLE_MISSING_ATTR;
  281. thdr = nla_data(tb[TCA_EMATCH_TREE_HDR]);
  282. /* Ignore empty trees */
  283. if (thdr->nmatches == 0) {
  284. NL_DBG(2, "Ignoring empty ematch configuration\n");
  285. return 0;
  286. }
  287. if (!tb[TCA_EMATCH_TREE_LIST])
  288. return -NLE_MISSING_ATTR;
  289. NL_DBG(2, "ematch tree found with nmatches=%u, progid=%u\n",
  290. thdr->nmatches, thdr->progid);
  291. /*
  292. * Do some basic sanity checking since we will allocate
  293. * index[thdr->nmatches]. Calculate how many ematch headers fit into
  294. * the provided data and make sure nmatches does not exceed it.
  295. */
  296. if (thdr->nmatches > (nla_len(tb[TCA_EMATCH_TREE_LIST]) /
  297. nla_total_size(sizeof(struct tcf_ematch_hdr))))
  298. return -NLE_INVAL;
  299. if (!(index = calloc(thdr->nmatches, sizeof(struct rtnl_ematch *))))
  300. return -NLE_NOMEM;
  301. if (!(tree = rtnl_ematch_tree_alloc(thdr->progid))) {
  302. err = -NLE_NOMEM;
  303. goto errout;
  304. }
  305. nla_for_each_nested(a, tb[TCA_EMATCH_TREE_LIST], remaining) {
  306. struct rtnl_ematch_ops *ops;
  307. struct tcf_ematch_hdr *hdr;
  308. struct rtnl_ematch *ematch;
  309. void *data;
  310. size_t len;
  311. NL_DBG(3, "parsing ematch attribute %d, len=%u\n",
  312. nmatches+1, nla_len(a));
  313. if (nla_len(a) < sizeof(*hdr)) {
  314. err = -NLE_INVAL;
  315. goto errout;
  316. }
  317. /* Quit as soon as we've parsed more matches than expected */
  318. if (nmatches >= thdr->nmatches) {
  319. err = -NLE_RANGE;
  320. goto errout;
  321. }
  322. hdr = nla_data(a);
  323. data = nla_data(a) + NLA_ALIGN(sizeof(*hdr));
  324. len = nla_len(a) - NLA_ALIGN(sizeof(*hdr));
  325. NL_DBG(3, "ematch attribute matchid=%u, kind=%u, flags=%u\n",
  326. hdr->matchid, hdr->kind, hdr->flags);
  327. /*
  328. * Container matches contain a reference to another sequence
  329. * of matches. Ensure that the reference is within boundries.
  330. */
  331. if (hdr->kind == TCF_EM_CONTAINER &&
  332. *((uint32_t *) data) >= thdr->nmatches) {
  333. err = -NLE_INVAL;
  334. goto errout;
  335. }
  336. if (!(ematch = rtnl_ematch_alloc())) {
  337. err = -NLE_NOMEM;
  338. goto errout;
  339. }
  340. ematch->e_id = hdr->matchid;
  341. ematch->e_kind = hdr->kind;
  342. ematch->e_flags = hdr->flags;
  343. if ((ops = rtnl_ematch_lookup_ops(hdr->kind))) {
  344. if (ops->eo_minlen && len < ops->eo_minlen) {
  345. rtnl_ematch_free(ematch);
  346. err = -NLE_INVAL;
  347. goto errout;
  348. }
  349. rtnl_ematch_set_ops(ematch, ops);
  350. if (ops->eo_parse &&
  351. (err = ops->eo_parse(ematch, data, len)) < 0) {
  352. rtnl_ematch_free(ematch);
  353. goto errout;
  354. }
  355. }
  356. NL_DBG(3, "index[%d] = %p\n", nmatches, ematch);
  357. index[nmatches++] = ematch;
  358. }
  359. if (nmatches != thdr->nmatches) {
  360. err = -NLE_INVAL;
  361. goto errout;
  362. }
  363. err = link_tree(index, nmatches, 0, &tree->et_list);
  364. if (err < 0)
  365. goto errout;
  366. free(index);
  367. *result = tree;
  368. return 0;
  369. errout:
  370. rtnl_ematch_tree_free(tree);
  371. free(index);
  372. return err;
  373. }
  374. static void dump_ematch_sequence(struct nl_list_head *head,
  375. struct nl_dump_params *p)
  376. {
  377. struct rtnl_ematch *match;
  378. nl_list_for_each_entry(match, head, e_list) {
  379. if (match->e_flags & TCF_EM_INVERT)
  380. nl_dump(p, "!");
  381. if (match->e_kind == TCF_EM_CONTAINER) {
  382. nl_dump(p, "(");
  383. dump_ematch_sequence(&match->e_childs, p);
  384. nl_dump(p, ")");
  385. } else if (!match->e_ops) {
  386. nl_dump(p, "[unknown ematch %d]", match->e_kind);
  387. } else {
  388. if (match->e_ops->eo_dump)
  389. match->e_ops->eo_dump(match, p);
  390. else
  391. nl_dump(p, "[data]");
  392. }
  393. switch (match->e_flags & TCF_EM_REL_MASK) {
  394. case TCF_EM_REL_AND:
  395. nl_dump(p, " AND ");
  396. break;
  397. case TCF_EM_REL_OR:
  398. nl_dump(p, " OR ");
  399. break;
  400. default:
  401. /* end of first level ematch sequence */
  402. return;
  403. }
  404. }
  405. }
  406. void rtnl_ematch_tree_dump(struct rtnl_ematch_tree *tree,
  407. struct nl_dump_params *p)
  408. {
  409. if (!tree)
  410. BUG();
  411. dump_ematch_sequence(&tree->et_list, p);
  412. nl_dump(p, "\n");
  413. }
  414. static int update_container_index(struct nl_list_head *list, int *index)
  415. {
  416. struct rtnl_ematch *e;
  417. nl_list_for_each_entry(e, list, e_list)
  418. e->e_index = (*index)++;
  419. nl_list_for_each_entry(e, list, e_list) {
  420. if (e->e_kind == TCF_EM_CONTAINER) {
  421. int err;
  422. if (nl_list_empty(&e->e_childs))
  423. return -NLE_OBJ_NOTFOUND;
  424. *((uint32_t *) e->e_data) = *index;
  425. err = update_container_index(&e->e_childs, index);
  426. if (err < 0)
  427. return err;
  428. }
  429. }
  430. return 0;
  431. }
  432. static int fill_ematch_sequence(struct nl_msg *msg, struct nl_list_head *list)
  433. {
  434. struct rtnl_ematch *e;
  435. nl_list_for_each_entry(e, list, e_list) {
  436. struct tcf_ematch_hdr match = {
  437. .matchid = e->e_id,
  438. .kind = e->e_kind,
  439. .flags = e->e_flags,
  440. };
  441. struct nlattr *attr;
  442. int err = 0;
  443. if (!(attr = nla_nest_start(msg, e->e_index + 1)))
  444. return -NLE_NOMEM;
  445. if (nlmsg_append(msg, &match, sizeof(match), 0) < 0)
  446. return -NLE_NOMEM;
  447. if (e->e_ops->eo_fill)
  448. err = e->e_ops->eo_fill(e, msg);
  449. else if (e->e_flags & TCF_EM_SIMPLE)
  450. err = nlmsg_append(msg, e->e_data, 4, 0);
  451. else if (e->e_datalen > 0)
  452. err = nlmsg_append(msg, e->e_data, e->e_datalen, 0);
  453. NL_DBG(3, "msg %p: added ematch [%d] id=%d kind=%d flags=%d\n",
  454. msg, e->e_index, match.matchid, match.kind, match.flags);
  455. if (err < 0)
  456. return -NLE_NOMEM;
  457. nla_nest_end(msg, attr);
  458. }
  459. nl_list_for_each_entry(e, list, e_list) {
  460. if (e->e_kind == TCF_EM_CONTAINER &&
  461. fill_ematch_sequence(msg, &e->e_childs) < 0)
  462. return -NLE_NOMEM;
  463. }
  464. return 0;
  465. }
  466. int rtnl_ematch_fill_attr(struct nl_msg *msg, int attrid,
  467. struct rtnl_ematch_tree *tree)
  468. {
  469. struct tcf_ematch_tree_hdr thdr = {
  470. .progid = tree->et_progid,
  471. };
  472. struct nlattr *list, *topattr;
  473. int err, index = 0;
  474. /* Assign index number to each ematch to allow for references
  475. * to be made while constructing the sequence of matches. */
  476. err = update_container_index(&tree->et_list, &index);
  477. if (err < 0)
  478. return err;
  479. if (!(topattr = nla_nest_start(msg, attrid)))
  480. goto nla_put_failure;
  481. thdr.nmatches = index;
  482. NLA_PUT(msg, TCA_EMATCH_TREE_HDR, sizeof(thdr), &thdr);
  483. if (!(list = nla_nest_start(msg, TCA_EMATCH_TREE_LIST)))
  484. goto nla_put_failure;
  485. if (fill_ematch_sequence(msg, &tree->et_list) < 0)
  486. goto nla_put_failure;
  487. nla_nest_end(msg, list);
  488. nla_nest_end(msg, topattr);
  489. return 0;
  490. nla_put_failure:
  491. return -NLE_NOMEM;
  492. }
  493. /** @} */
  494. extern int ematch_parse(void *, char **, struct nl_list_head *);
  495. int rtnl_ematch_parse_expr(const char *expr, char **errp,
  496. struct rtnl_ematch_tree **result)
  497. {
  498. struct rtnl_ematch_tree *tree;
  499. YY_BUFFER_STATE buf = NULL;
  500. yyscan_t scanner = NULL;
  501. int err;
  502. NL_DBG(2, "Parsing ematch expression \"%s\"\n", expr);
  503. if (!(tree = rtnl_ematch_tree_alloc(RTNL_EMATCH_PROGID)))
  504. return -NLE_FAILURE;
  505. if ((err = ematch_lex_init(&scanner)) < 0) {
  506. err = -NLE_FAILURE;
  507. goto errout;
  508. }
  509. buf = ematch__scan_string(expr, scanner);
  510. if ((err = ematch_parse(scanner, errp, &tree->et_list)) != 0) {
  511. ematch__delete_buffer(buf, scanner);
  512. err = -NLE_PARSE_ERR;
  513. goto errout;
  514. }
  515. ematch_lex_destroy(scanner);
  516. *result = tree;
  517. return 0;
  518. errout:
  519. if (scanner)
  520. ematch_lex_destroy(scanner);
  521. rtnl_ematch_tree_free(tree);
  522. return err;
  523. }
  524. static const char *layer_txt[] = {
  525. [TCF_LAYER_LINK] = "eth",
  526. [TCF_LAYER_NETWORK] = "ip",
  527. [TCF_LAYER_TRANSPORT] = "tcp",
  528. };
  529. char *rtnl_ematch_offset2txt(uint8_t layer, uint16_t offset, char *buf, size_t len)
  530. {
  531. snprintf(buf, len, "%s+%u",
  532. (layer <= TCF_LAYER_MAX) ? layer_txt[layer] : "?",
  533. offset);
  534. return buf;
  535. }
  536. static const char *operand_txt[] = {
  537. [TCF_EM_OPND_EQ] = "=",
  538. [TCF_EM_OPND_LT] = "<",
  539. [TCF_EM_OPND_GT] = ">",
  540. };
  541. char *rtnl_ematch_opnd2txt(uint8_t opnd, char *buf, size_t len)
  542. {
  543. snprintf(buf, len, "%s",
  544. opnd < ARRAY_SIZE(operand_txt) ? operand_txt[opnd] : "?");
  545. return buf;
  546. }
  547. /** @} */