ematch.c 14 KB

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