ematch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * net/sched/ematch.c Extended Match API
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. *
  11. * ==========================================================================
  12. *
  13. * An extended match (ematch) is a small classification tool not worth
  14. * writing a full classifier for. Ematches can be interconnected to form
  15. * a logic expression and get attached to classifiers to extend their
  16. * functionatlity.
  17. *
  18. * The userspace part transforms the logic expressions into an array
  19. * consisting of multiple sequences of interconnected ematches separated
  20. * by markers. Precedence is implemented by a special ematch kind
  21. * referencing a sequence beyond the marker of the current sequence
  22. * causing the current position in the sequence to be pushed onto a stack
  23. * to allow the current position to be overwritten by the position referenced
  24. * in the special ematch. Matching continues in the new sequence until a
  25. * marker is reached causing the position to be restored from the stack.
  26. *
  27. * Example:
  28. * A AND (B1 OR B2) AND C AND D
  29. *
  30. * ------->-PUSH-------
  31. * -->-- / -->-- \ -->--
  32. * / \ / / \ \ / \
  33. * +-------+-------+-------+-------+-------+--------+
  34. * | A AND | B AND | C AND | D END | B1 OR | B2 END |
  35. * +-------+-------+-------+-------+-------+--------+
  36. * \ /
  37. * --------<-POP---------
  38. *
  39. * where B is a virtual ematch referencing to sequence starting with B1.
  40. *
  41. * ==========================================================================
  42. *
  43. * How to write an ematch in 60 seconds
  44. * ------------------------------------
  45. *
  46. * 1) Provide a matcher function:
  47. * static int my_match(struct sk_buff *skb, struct tcf_ematch *m,
  48. * struct tcf_pkt_info *info)
  49. * {
  50. * struct mydata *d = (struct mydata *) m->data;
  51. *
  52. * if (...matching goes here...)
  53. * return 1;
  54. * else
  55. * return 0;
  56. * }
  57. *
  58. * 2) Fill out a struct tcf_ematch_ops:
  59. * static struct tcf_ematch_ops my_ops = {
  60. * .kind = unique id,
  61. * .datalen = sizeof(struct mydata),
  62. * .match = my_match,
  63. * .owner = THIS_MODULE,
  64. * };
  65. *
  66. * 3) Register/Unregister your ematch:
  67. * static int __init init_my_ematch(void)
  68. * {
  69. * return tcf_em_register(&my_ops);
  70. * }
  71. *
  72. * static void __exit exit_my_ematch(void)
  73. * {
  74. * tcf_em_unregister(&my_ops);
  75. * }
  76. *
  77. * module_init(init_my_ematch);
  78. * module_exit(exit_my_ematch);
  79. *
  80. * 4) By now you should have two more seconds left, barely enough to
  81. * open up a beer to watch the compilation going.
  82. */
  83. #include <linux/module.h>
  84. #include <linux/slab.h>
  85. #include <linux/types.h>
  86. #include <linux/kernel.h>
  87. #include <linux/errno.h>
  88. #include <linux/rtnetlink.h>
  89. #include <linux/skbuff.h>
  90. #include <net/pkt_cls.h>
  91. static LIST_HEAD(ematch_ops);
  92. static DEFINE_RWLOCK(ematch_mod_lock);
  93. static struct tcf_ematch_ops *tcf_em_lookup(u16 kind)
  94. {
  95. struct tcf_ematch_ops *e = NULL;
  96. read_lock(&ematch_mod_lock);
  97. list_for_each_entry(e, &ematch_ops, link) {
  98. if (kind == e->kind) {
  99. if (!try_module_get(e->owner))
  100. e = NULL;
  101. read_unlock(&ematch_mod_lock);
  102. return e;
  103. }
  104. }
  105. read_unlock(&ematch_mod_lock);
  106. return NULL;
  107. }
  108. /**
  109. * tcf_em_register - register an extended match
  110. *
  111. * @ops: ematch operations lookup table
  112. *
  113. * This function must be called by ematches to announce their presence.
  114. * The given @ops must have kind set to a unique identifier and the
  115. * callback match() must be implemented. All other callbacks are optional
  116. * and a fallback implementation is used instead.
  117. *
  118. * Returns -EEXISTS if an ematch of the same kind has already registered.
  119. */
  120. int tcf_em_register(struct tcf_ematch_ops *ops)
  121. {
  122. int err = -EEXIST;
  123. struct tcf_ematch_ops *e;
  124. if (ops->match == NULL)
  125. return -EINVAL;
  126. write_lock(&ematch_mod_lock);
  127. list_for_each_entry(e, &ematch_ops, link)
  128. if (ops->kind == e->kind)
  129. goto errout;
  130. list_add_tail(&ops->link, &ematch_ops);
  131. err = 0;
  132. errout:
  133. write_unlock(&ematch_mod_lock);
  134. return err;
  135. }
  136. EXPORT_SYMBOL(tcf_em_register);
  137. /**
  138. * tcf_em_unregister - unregster and extended match
  139. *
  140. * @ops: ematch operations lookup table
  141. *
  142. * This function must be called by ematches to announce their disappearance
  143. * for examples when the module gets unloaded. The @ops parameter must be
  144. * the same as the one used for registration.
  145. *
  146. * Returns -ENOENT if no matching ematch was found.
  147. */
  148. void tcf_em_unregister(struct tcf_ematch_ops *ops)
  149. {
  150. write_lock(&ematch_mod_lock);
  151. list_del(&ops->link);
  152. write_unlock(&ematch_mod_lock);
  153. }
  154. EXPORT_SYMBOL(tcf_em_unregister);
  155. static inline struct tcf_ematch *tcf_em_get_match(struct tcf_ematch_tree *tree,
  156. int index)
  157. {
  158. return &tree->matches[index];
  159. }
  160. static int tcf_em_validate(struct tcf_proto *tp,
  161. struct tcf_ematch_tree_hdr *tree_hdr,
  162. struct tcf_ematch *em, struct nlattr *nla, int idx)
  163. {
  164. int err = -EINVAL;
  165. struct tcf_ematch_hdr *em_hdr = nla_data(nla);
  166. int data_len = nla_len(nla) - sizeof(*em_hdr);
  167. void *data = (void *) em_hdr + sizeof(*em_hdr);
  168. struct net *net = dev_net(qdisc_dev(tp->q));
  169. if (!TCF_EM_REL_VALID(em_hdr->flags))
  170. goto errout;
  171. if (em_hdr->kind == TCF_EM_CONTAINER) {
  172. /* Special ematch called "container", carries an index
  173. * referencing an external ematch sequence.
  174. */
  175. u32 ref;
  176. if (data_len < sizeof(ref))
  177. goto errout;
  178. ref = *(u32 *) data;
  179. if (ref >= tree_hdr->nmatches)
  180. goto errout;
  181. /* We do not allow backward jumps to avoid loops and jumps
  182. * to our own position are of course illegal.
  183. */
  184. if (ref <= idx)
  185. goto errout;
  186. em->data = ref;
  187. } else {
  188. /* Note: This lookup will increase the module refcnt
  189. * of the ematch module referenced. In case of a failure,
  190. * a destroy function is called by the underlying layer
  191. * which automatically releases the reference again, therefore
  192. * the module MUST not be given back under any circumstances
  193. * here. Be aware, the destroy function assumes that the
  194. * module is held if the ops field is non zero.
  195. */
  196. em->ops = tcf_em_lookup(em_hdr->kind);
  197. if (em->ops == NULL) {
  198. err = -ENOENT;
  199. #ifdef CONFIG_MODULES
  200. __rtnl_unlock();
  201. request_module("ematch-kind-%u", em_hdr->kind);
  202. rtnl_lock();
  203. em->ops = tcf_em_lookup(em_hdr->kind);
  204. if (em->ops) {
  205. /* We dropped the RTNL mutex in order to
  206. * perform the module load. Tell the caller
  207. * to replay the request.
  208. */
  209. module_put(em->ops->owner);
  210. em->ops = NULL;
  211. err = -EAGAIN;
  212. }
  213. #endif
  214. goto errout;
  215. }
  216. /* ematch module provides expected length of data, so we
  217. * can do a basic sanity check.
  218. */
  219. if (em->ops->datalen && data_len < em->ops->datalen)
  220. goto errout;
  221. if (em->ops->change) {
  222. err = -EINVAL;
  223. if (em_hdr->flags & TCF_EM_SIMPLE)
  224. goto errout;
  225. err = em->ops->change(net, data, data_len, em);
  226. if (err < 0)
  227. goto errout;
  228. } else if (data_len > 0) {
  229. /* ematch module doesn't provide an own change
  230. * procedure and expects us to allocate and copy
  231. * the ematch data.
  232. *
  233. * TCF_EM_SIMPLE may be specified stating that the
  234. * data only consists of a u32 integer and the module
  235. * does not expected a memory reference but rather
  236. * the value carried.
  237. */
  238. if (em_hdr->flags & TCF_EM_SIMPLE) {
  239. if (data_len < sizeof(u32))
  240. goto errout;
  241. em->data = *(u32 *) data;
  242. } else {
  243. void *v = kmemdup(data, data_len, GFP_KERNEL);
  244. if (v == NULL) {
  245. err = -ENOBUFS;
  246. goto errout;
  247. }
  248. em->data = (unsigned long) v;
  249. }
  250. em->datalen = data_len;
  251. }
  252. }
  253. em->matchid = em_hdr->matchid;
  254. em->flags = em_hdr->flags;
  255. em->net = net;
  256. err = 0;
  257. errout:
  258. return err;
  259. }
  260. static const struct nla_policy em_policy[TCA_EMATCH_TREE_MAX + 1] = {
  261. [TCA_EMATCH_TREE_HDR] = { .len = sizeof(struct tcf_ematch_tree_hdr) },
  262. [TCA_EMATCH_TREE_LIST] = { .type = NLA_NESTED },
  263. };
  264. /**
  265. * tcf_em_tree_validate - validate ematch config TLV and build ematch tree
  266. *
  267. * @tp: classifier kind handle
  268. * @nla: ematch tree configuration TLV
  269. * @tree: destination ematch tree variable to store the resulting
  270. * ematch tree.
  271. *
  272. * This function validates the given configuration TLV @nla and builds an
  273. * ematch tree in @tree. The resulting tree must later be copied into
  274. * the private classifier data using tcf_em_tree_change(). You MUST NOT
  275. * provide the ematch tree variable of the private classifier data directly,
  276. * the changes would not be locked properly.
  277. *
  278. * Returns a negative error code if the configuration TLV contains errors.
  279. */
  280. int tcf_em_tree_validate(struct tcf_proto *tp, struct nlattr *nla,
  281. struct tcf_ematch_tree *tree)
  282. {
  283. int idx, list_len, matches_len, err;
  284. struct nlattr *tb[TCA_EMATCH_TREE_MAX + 1];
  285. struct nlattr *rt_match, *rt_hdr, *rt_list;
  286. struct tcf_ematch_tree_hdr *tree_hdr;
  287. struct tcf_ematch *em;
  288. memset(tree, 0, sizeof(*tree));
  289. if (!nla)
  290. return 0;
  291. err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, nla, em_policy, NULL);
  292. if (err < 0)
  293. goto errout;
  294. err = -EINVAL;
  295. rt_hdr = tb[TCA_EMATCH_TREE_HDR];
  296. rt_list = tb[TCA_EMATCH_TREE_LIST];
  297. if (rt_hdr == NULL || rt_list == NULL)
  298. goto errout;
  299. tree_hdr = nla_data(rt_hdr);
  300. memcpy(&tree->hdr, tree_hdr, sizeof(*tree_hdr));
  301. rt_match = nla_data(rt_list);
  302. list_len = nla_len(rt_list);
  303. matches_len = tree_hdr->nmatches * sizeof(*em);
  304. tree->matches = kzalloc(matches_len, GFP_KERNEL);
  305. if (tree->matches == NULL)
  306. goto errout;
  307. /* We do not use nla_parse_nested here because the maximum
  308. * number of attributes is unknown. This saves us the allocation
  309. * for a tb buffer which would serve no purpose at all.
  310. *
  311. * The array of rt attributes is parsed in the order as they are
  312. * provided, their type must be incremental from 1 to n. Even
  313. * if it does not serve any real purpose, a failure of sticking
  314. * to this policy will result in parsing failure.
  315. */
  316. for (idx = 0; nla_ok(rt_match, list_len); idx++) {
  317. err = -EINVAL;
  318. if (rt_match->nla_type != (idx + 1))
  319. goto errout_abort;
  320. if (idx >= tree_hdr->nmatches)
  321. goto errout_abort;
  322. if (nla_len(rt_match) < sizeof(struct tcf_ematch_hdr))
  323. goto errout_abort;
  324. em = tcf_em_get_match(tree, idx);
  325. err = tcf_em_validate(tp, tree_hdr, em, rt_match, idx);
  326. if (err < 0)
  327. goto errout_abort;
  328. rt_match = nla_next(rt_match, &list_len);
  329. }
  330. /* Check if the number of matches provided by userspace actually
  331. * complies with the array of matches. The number was used for
  332. * the validation of references and a mismatch could lead to
  333. * undefined references during the matching process.
  334. */
  335. if (idx != tree_hdr->nmatches) {
  336. err = -EINVAL;
  337. goto errout_abort;
  338. }
  339. err = 0;
  340. errout:
  341. return err;
  342. errout_abort:
  343. tcf_em_tree_destroy(tree);
  344. return err;
  345. }
  346. EXPORT_SYMBOL(tcf_em_tree_validate);
  347. /**
  348. * tcf_em_tree_destroy - destroy an ematch tree
  349. *
  350. * @tp: classifier kind handle
  351. * @tree: ematch tree to be deleted
  352. *
  353. * This functions destroys an ematch tree previously created by
  354. * tcf_em_tree_validate()/tcf_em_tree_change(). You must ensure that
  355. * the ematch tree is not in use before calling this function.
  356. */
  357. void tcf_em_tree_destroy(struct tcf_ematch_tree *tree)
  358. {
  359. int i;
  360. if (tree->matches == NULL)
  361. return;
  362. for (i = 0; i < tree->hdr.nmatches; i++) {
  363. struct tcf_ematch *em = tcf_em_get_match(tree, i);
  364. if (em->ops) {
  365. if (em->ops->destroy)
  366. em->ops->destroy(em);
  367. else if (!tcf_em_is_simple(em))
  368. kfree((void *) em->data);
  369. module_put(em->ops->owner);
  370. }
  371. }
  372. tree->hdr.nmatches = 0;
  373. kfree(tree->matches);
  374. tree->matches = NULL;
  375. }
  376. EXPORT_SYMBOL(tcf_em_tree_destroy);
  377. /**
  378. * tcf_em_tree_dump - dump ematch tree into a rtnl message
  379. *
  380. * @skb: skb holding the rtnl message
  381. * @t: ematch tree to be dumped
  382. * @tlv: TLV type to be used to encapsulate the tree
  383. *
  384. * This function dumps a ematch tree into a rtnl message. It is valid to
  385. * call this function while the ematch tree is in use.
  386. *
  387. * Returns -1 if the skb tailroom is insufficient.
  388. */
  389. int tcf_em_tree_dump(struct sk_buff *skb, struct tcf_ematch_tree *tree, int tlv)
  390. {
  391. int i;
  392. u8 *tail;
  393. struct nlattr *top_start;
  394. struct nlattr *list_start;
  395. top_start = nla_nest_start(skb, tlv);
  396. if (top_start == NULL)
  397. goto nla_put_failure;
  398. if (nla_put(skb, TCA_EMATCH_TREE_HDR, sizeof(tree->hdr), &tree->hdr))
  399. goto nla_put_failure;
  400. list_start = nla_nest_start(skb, TCA_EMATCH_TREE_LIST);
  401. if (list_start == NULL)
  402. goto nla_put_failure;
  403. tail = skb_tail_pointer(skb);
  404. for (i = 0; i < tree->hdr.nmatches; i++) {
  405. struct nlattr *match_start = (struct nlattr *)tail;
  406. struct tcf_ematch *em = tcf_em_get_match(tree, i);
  407. struct tcf_ematch_hdr em_hdr = {
  408. .kind = em->ops ? em->ops->kind : TCF_EM_CONTAINER,
  409. .matchid = em->matchid,
  410. .flags = em->flags
  411. };
  412. if (nla_put(skb, i + 1, sizeof(em_hdr), &em_hdr))
  413. goto nla_put_failure;
  414. if (em->ops && em->ops->dump) {
  415. if (em->ops->dump(skb, em) < 0)
  416. goto nla_put_failure;
  417. } else if (tcf_em_is_container(em) || tcf_em_is_simple(em)) {
  418. u32 u = em->data;
  419. nla_put_nohdr(skb, sizeof(u), &u);
  420. } else if (em->datalen > 0)
  421. nla_put_nohdr(skb, em->datalen, (void *) em->data);
  422. tail = skb_tail_pointer(skb);
  423. match_start->nla_len = tail - (u8 *)match_start;
  424. }
  425. nla_nest_end(skb, list_start);
  426. nla_nest_end(skb, top_start);
  427. return 0;
  428. nla_put_failure:
  429. return -1;
  430. }
  431. EXPORT_SYMBOL(tcf_em_tree_dump);
  432. static inline int tcf_em_match(struct sk_buff *skb, struct tcf_ematch *em,
  433. struct tcf_pkt_info *info)
  434. {
  435. int r = em->ops->match(skb, em, info);
  436. return tcf_em_is_inverted(em) ? !r : r;
  437. }
  438. /* Do not use this function directly, use tcf_em_tree_match instead */
  439. int __tcf_em_tree_match(struct sk_buff *skb, struct tcf_ematch_tree *tree,
  440. struct tcf_pkt_info *info)
  441. {
  442. int stackp = 0, match_idx = 0, res = 0;
  443. struct tcf_ematch *cur_match;
  444. int stack[CONFIG_NET_EMATCH_STACK];
  445. proceed:
  446. while (match_idx < tree->hdr.nmatches) {
  447. cur_match = tcf_em_get_match(tree, match_idx);
  448. if (tcf_em_is_container(cur_match)) {
  449. if (unlikely(stackp >= CONFIG_NET_EMATCH_STACK))
  450. goto stack_overflow;
  451. stack[stackp++] = match_idx;
  452. match_idx = cur_match->data;
  453. goto proceed;
  454. }
  455. res = tcf_em_match(skb, cur_match, info);
  456. if (tcf_em_early_end(cur_match, res))
  457. break;
  458. match_idx++;
  459. }
  460. pop_stack:
  461. if (stackp > 0) {
  462. match_idx = stack[--stackp];
  463. cur_match = tcf_em_get_match(tree, match_idx);
  464. if (tcf_em_is_inverted(cur_match))
  465. res = !res;
  466. if (tcf_em_early_end(cur_match, res)) {
  467. goto pop_stack;
  468. } else {
  469. match_idx++;
  470. goto proceed;
  471. }
  472. }
  473. return res;
  474. stack_overflow:
  475. net_warn_ratelimited("tc ematch: local stack overflow, increase NET_EMATCH_STACK\n");
  476. return -1;
  477. }
  478. EXPORT_SYMBOL(__tcf_em_tree_match);