ematch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. * return 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/config.h>
  84. #include <linux/module.h>
  85. #include <linux/types.h>
  86. #include <linux/kernel.h>
  87. #include <linux/sched.h>
  88. #include <linux/mm.h>
  89. #include <linux/errno.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/rtnetlink.h>
  92. #include <linux/skbuff.h>
  93. #include <net/pkt_cls.h>
  94. #include <config/net/ematch/stack.h>
  95. static LIST_HEAD(ematch_ops);
  96. static DEFINE_RWLOCK(ematch_mod_lock);
  97. static inline struct tcf_ematch_ops * tcf_em_lookup(u16 kind)
  98. {
  99. struct tcf_ematch_ops *e = NULL;
  100. read_lock(&ematch_mod_lock);
  101. list_for_each_entry(e, &ematch_ops, link) {
  102. if (kind == e->kind) {
  103. if (!try_module_get(e->owner))
  104. e = NULL;
  105. read_unlock(&ematch_mod_lock);
  106. return e;
  107. }
  108. }
  109. read_unlock(&ematch_mod_lock);
  110. return NULL;
  111. }
  112. /**
  113. * tcf_em_register - register an extended match
  114. *
  115. * @ops: ematch operations lookup table
  116. *
  117. * This function must be called by ematches to announce their presence.
  118. * The given @ops must have kind set to a unique identifier and the
  119. * callback match() must be implemented. All other callbacks are optional
  120. * and a fallback implementation is used instead.
  121. *
  122. * Returns -EEXISTS if an ematch of the same kind has already registered.
  123. */
  124. int tcf_em_register(struct tcf_ematch_ops *ops)
  125. {
  126. int err = -EEXIST;
  127. struct tcf_ematch_ops *e;
  128. if (ops->match == NULL)
  129. return -EINVAL;
  130. write_lock(&ematch_mod_lock);
  131. list_for_each_entry(e, &ematch_ops, link)
  132. if (ops->kind == e->kind)
  133. goto errout;
  134. list_add_tail(&ops->link, &ematch_ops);
  135. err = 0;
  136. errout:
  137. write_unlock(&ematch_mod_lock);
  138. return err;
  139. }
  140. /**
  141. * tcf_em_unregister - unregster and extended match
  142. *
  143. * @ops: ematch operations lookup table
  144. *
  145. * This function must be called by ematches to announce their disappearance
  146. * for examples when the module gets unloaded. The @ops parameter must be
  147. * the same as the one used for registration.
  148. *
  149. * Returns -ENOENT if no matching ematch was found.
  150. */
  151. int tcf_em_unregister(struct tcf_ematch_ops *ops)
  152. {
  153. int err = 0;
  154. struct tcf_ematch_ops *e;
  155. write_lock(&ematch_mod_lock);
  156. list_for_each_entry(e, &ematch_ops, link) {
  157. if (e == ops) {
  158. list_del(&e->link);
  159. goto out;
  160. }
  161. }
  162. err = -ENOENT;
  163. out:
  164. write_unlock(&ematch_mod_lock);
  165. return err;
  166. }
  167. static inline struct tcf_ematch * tcf_em_get_match(struct tcf_ematch_tree *tree,
  168. int index)
  169. {
  170. return &tree->matches[index];
  171. }
  172. static int tcf_em_validate(struct tcf_proto *tp,
  173. struct tcf_ematch_tree_hdr *tree_hdr,
  174. struct tcf_ematch *em, struct rtattr *rta, int idx)
  175. {
  176. int err = -EINVAL;
  177. struct tcf_ematch_hdr *em_hdr = RTA_DATA(rta);
  178. int data_len = RTA_PAYLOAD(rta) - sizeof(*em_hdr);
  179. void *data = (void *) em_hdr + sizeof(*em_hdr);
  180. if (!TCF_EM_REL_VALID(em_hdr->flags))
  181. goto errout;
  182. if (em_hdr->kind == TCF_EM_CONTAINER) {
  183. /* Special ematch called "container", carries an index
  184. * referencing an external ematch sequence. */
  185. u32 ref;
  186. if (data_len < sizeof(ref))
  187. goto errout;
  188. ref = *(u32 *) data;
  189. if (ref >= tree_hdr->nmatches)
  190. goto errout;
  191. /* We do not allow backward jumps to avoid loops and jumps
  192. * to our own position are of course illegal. */
  193. if (ref <= idx)
  194. goto errout;
  195. em->data = ref;
  196. } else {
  197. /* Note: This lookup will increase the module refcnt
  198. * of the ematch module referenced. In case of a failure,
  199. * a destroy function is called by the underlying layer
  200. * which automatically releases the reference again, therefore
  201. * the module MUST not be given back under any circumstances
  202. * here. Be aware, the destroy function assumes that the
  203. * module is held if the ops field is non zero. */
  204. em->ops = tcf_em_lookup(em_hdr->kind);
  205. if (em->ops == NULL) {
  206. err = -ENOENT;
  207. goto errout;
  208. }
  209. /* ematch module provides expected length of data, so we
  210. * can do a basic sanity check. */
  211. if (em->ops->datalen && data_len < em->ops->datalen)
  212. goto errout;
  213. if (em->ops->change) {
  214. err = em->ops->change(tp, data, data_len, em);
  215. if (err < 0)
  216. goto errout;
  217. } else if (data_len > 0) {
  218. /* ematch module doesn't provide an own change
  219. * procedure and expects us to allocate and copy
  220. * the ematch data.
  221. *
  222. * TCF_EM_SIMPLE may be specified stating that the
  223. * data only consists of a u32 integer and the module
  224. * does not expected a memory reference but rather
  225. * the value carried. */
  226. if (em_hdr->flags & TCF_EM_SIMPLE) {
  227. if (data_len < sizeof(u32))
  228. goto errout;
  229. em->data = *(u32 *) data;
  230. } else {
  231. void *v = kmalloc(data_len, GFP_KERNEL);
  232. if (v == NULL) {
  233. err = -ENOBUFS;
  234. goto errout;
  235. }
  236. memcpy(v, data, data_len);
  237. em->data = (unsigned long) v;
  238. }
  239. }
  240. }
  241. em->matchid = em_hdr->matchid;
  242. em->flags = em_hdr->flags;
  243. em->datalen = data_len;
  244. err = 0;
  245. errout:
  246. return err;
  247. }
  248. /**
  249. * tcf_em_tree_validate - validate ematch config TLV and build ematch tree
  250. *
  251. * @tp: classifier kind handle
  252. * @rta: ematch tree configuration TLV
  253. * @tree: destination ematch tree variable to store the resulting
  254. * ematch tree.
  255. *
  256. * This function validates the given configuration TLV @rta and builds an
  257. * ematch tree in @tree. The resulting tree must later be copied into
  258. * the private classifier data using tcf_em_tree_change(). You MUST NOT
  259. * provide the ematch tree variable of the private classifier data directly,
  260. * the changes would not be locked properly.
  261. *
  262. * Returns a negative error code if the configuration TLV contains errors.
  263. */
  264. int tcf_em_tree_validate(struct tcf_proto *tp, struct rtattr *rta,
  265. struct tcf_ematch_tree *tree)
  266. {
  267. int idx, list_len, matches_len, err = -EINVAL;
  268. struct rtattr *tb[TCA_EMATCH_TREE_MAX];
  269. struct rtattr *rt_match, *rt_hdr, *rt_list;
  270. struct tcf_ematch_tree_hdr *tree_hdr;
  271. struct tcf_ematch *em;
  272. if (rtattr_parse_nested(tb, TCA_EMATCH_TREE_MAX, rta) < 0)
  273. goto errout;
  274. rt_hdr = tb[TCA_EMATCH_TREE_HDR-1];
  275. rt_list = tb[TCA_EMATCH_TREE_LIST-1];
  276. if (rt_hdr == NULL || rt_list == NULL)
  277. goto errout;
  278. if (RTA_PAYLOAD(rt_hdr) < sizeof(*tree_hdr) ||
  279. RTA_PAYLOAD(rt_list) < sizeof(*rt_match))
  280. goto errout;
  281. tree_hdr = RTA_DATA(rt_hdr);
  282. memcpy(&tree->hdr, tree_hdr, sizeof(*tree_hdr));
  283. rt_match = RTA_DATA(rt_list);
  284. list_len = RTA_PAYLOAD(rt_list);
  285. matches_len = tree_hdr->nmatches * sizeof(*em);
  286. tree->matches = kmalloc(matches_len, GFP_KERNEL);
  287. if (tree->matches == NULL)
  288. goto errout;
  289. memset(tree->matches, 0, matches_len);
  290. /* We do not use rtattr_parse_nested here because the maximum
  291. * number of attributes is unknown. This saves us the allocation
  292. * for a tb buffer which would serve no purpose at all.
  293. *
  294. * The array of rt attributes is parsed in the order as they are
  295. * provided, their type must be incremental from 1 to n. Even
  296. * if it does not serve any real purpose, a failure of sticking
  297. * to this policy will result in parsing failure. */
  298. for (idx = 0; RTA_OK(rt_match, list_len); idx++) {
  299. err = -EINVAL;
  300. if (rt_match->rta_type != (idx + 1))
  301. goto errout_abort;
  302. if (idx >= tree_hdr->nmatches)
  303. goto errout_abort;
  304. if (RTA_PAYLOAD(rt_match) < sizeof(struct tcf_ematch_hdr))
  305. goto errout_abort;
  306. em = tcf_em_get_match(tree, idx);
  307. err = tcf_em_validate(tp, tree_hdr, em, rt_match, idx);
  308. if (err < 0)
  309. goto errout_abort;
  310. rt_match = RTA_NEXT(rt_match, list_len);
  311. }
  312. /* Check if the number of matches provided by userspace actually
  313. * complies with the array of matches. The number was used for
  314. * the validation of references and a mismatch could lead to
  315. * undefined references during the matching process. */
  316. if (idx != tree_hdr->nmatches) {
  317. err = -EINVAL;
  318. goto errout_abort;
  319. }
  320. err = 0;
  321. errout:
  322. return err;
  323. errout_abort:
  324. tcf_em_tree_destroy(tp, tree);
  325. return err;
  326. }
  327. /**
  328. * tcf_em_tree_destroy - destroy an ematch tree
  329. *
  330. * @tp: classifier kind handle
  331. * @tree: ematch tree to be deleted
  332. *
  333. * This functions destroys an ematch tree previously created by
  334. * tcf_em_tree_validate()/tcf_em_tree_change(). You must ensure that
  335. * the ematch tree is not in use before calling this function.
  336. */
  337. void tcf_em_tree_destroy(struct tcf_proto *tp, struct tcf_ematch_tree *tree)
  338. {
  339. int i;
  340. if (tree->matches == NULL)
  341. return;
  342. for (i = 0; i < tree->hdr.nmatches; i++) {
  343. struct tcf_ematch *em = tcf_em_get_match(tree, i);
  344. if (em->ops) {
  345. if (em->ops->destroy)
  346. em->ops->destroy(tp, em);
  347. else if (!tcf_em_is_simple(em) && em->data)
  348. kfree((void *) em->data);
  349. module_put(em->ops->owner);
  350. }
  351. }
  352. tree->hdr.nmatches = 0;
  353. kfree(tree->matches);
  354. }
  355. /**
  356. * tcf_em_tree_dump - dump ematch tree into a rtnl message
  357. *
  358. * @skb: skb holding the rtnl message
  359. * @t: ematch tree to be dumped
  360. * @tlv: TLV type to be used to encapsulate the tree
  361. *
  362. * This function dumps a ematch tree into a rtnl message. It is valid to
  363. * call this function while the ematch tree is in use.
  364. *
  365. * Returns -1 if the skb tailroom is insufficient.
  366. */
  367. int tcf_em_tree_dump(struct sk_buff *skb, struct tcf_ematch_tree *tree, int tlv)
  368. {
  369. int i;
  370. struct rtattr * top_start = (struct rtattr*) skb->tail;
  371. struct rtattr * list_start;
  372. RTA_PUT(skb, tlv, 0, NULL);
  373. RTA_PUT(skb, TCA_EMATCH_TREE_HDR, sizeof(tree->hdr), &tree->hdr);
  374. list_start = (struct rtattr *) skb->tail;
  375. RTA_PUT(skb, TCA_EMATCH_TREE_LIST, 0, NULL);
  376. for (i = 0; i < tree->hdr.nmatches; i++) {
  377. struct rtattr *match_start = (struct rtattr*) skb->tail;
  378. struct tcf_ematch *em = tcf_em_get_match(tree, i);
  379. struct tcf_ematch_hdr em_hdr = {
  380. .kind = em->ops ? em->ops->kind : TCF_EM_CONTAINER,
  381. .matchid = em->matchid,
  382. .flags = em->flags
  383. };
  384. RTA_PUT(skb, i+1, sizeof(em_hdr), &em_hdr);
  385. if (em->ops && em->ops->dump) {
  386. if (em->ops->dump(skb, em) < 0)
  387. goto rtattr_failure;
  388. } else if (tcf_em_is_container(em) || tcf_em_is_simple(em)) {
  389. u32 u = em->data;
  390. RTA_PUT_NOHDR(skb, sizeof(u), &u);
  391. } else if (em->datalen > 0)
  392. RTA_PUT_NOHDR(skb, em->datalen, (void *) em->data);
  393. match_start->rta_len = skb->tail - (u8*) match_start;
  394. }
  395. list_start->rta_len = skb->tail - (u8 *) list_start;
  396. top_start->rta_len = skb->tail - (u8 *) top_start;
  397. return 0;
  398. rtattr_failure:
  399. return -1;
  400. }
  401. static inline int tcf_em_match(struct sk_buff *skb, struct tcf_ematch *em,
  402. struct tcf_pkt_info *info)
  403. {
  404. int r = em->ops->match(skb, em, info);
  405. return tcf_em_is_inverted(em) ? !r : r;
  406. }
  407. /* Do not use this function directly, use tcf_em_tree_match instead */
  408. int __tcf_em_tree_match(struct sk_buff *skb, struct tcf_ematch_tree *tree,
  409. struct tcf_pkt_info *info)
  410. {
  411. int stackp = 0, match_idx = 0, res = 0;
  412. struct tcf_ematch *cur_match;
  413. int stack[CONFIG_NET_EMATCH_STACK];
  414. proceed:
  415. while (match_idx < tree->hdr.nmatches) {
  416. cur_match = tcf_em_get_match(tree, match_idx);
  417. if (tcf_em_is_container(cur_match)) {
  418. if (unlikely(stackp >= CONFIG_NET_EMATCH_STACK))
  419. goto stack_overflow;
  420. stack[stackp++] = match_idx;
  421. match_idx = cur_match->data;
  422. goto proceed;
  423. }
  424. res = tcf_em_match(skb, cur_match, info);
  425. if (tcf_em_early_end(cur_match, res))
  426. break;
  427. match_idx++;
  428. }
  429. pop_stack:
  430. if (stackp > 0) {
  431. match_idx = stack[--stackp];
  432. cur_match = tcf_em_get_match(tree, match_idx);
  433. if (tcf_em_early_end(cur_match, res))
  434. goto pop_stack;
  435. else {
  436. match_idx++;
  437. goto proceed;
  438. }
  439. }
  440. return res;
  441. stack_overflow:
  442. if (net_ratelimit())
  443. printk("Local stack overflow, increase NET_EMATCH_STACK\n");
  444. return -1;
  445. }
  446. EXPORT_SYMBOL(tcf_em_register);
  447. EXPORT_SYMBOL(tcf_em_unregister);
  448. EXPORT_SYMBOL(tcf_em_tree_validate);
  449. EXPORT_SYMBOL(tcf_em_tree_destroy);
  450. EXPORT_SYMBOL(tcf_em_tree_dump);
  451. EXPORT_SYMBOL(__tcf_em_tree_match);