basic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * lib/route/cls/basic.c Basic Classifier
  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 cls_basic Basic Classifier
  14. *
  15. * @par Introduction
  16. * The basic classifier is the simplest form of a classifier. It does
  17. * not have any special classification capabilities, instead it can be
  18. * used to classify exclusively based on extended matches or to
  19. * create a "catch-all" filter.
  20. *
  21. * @{
  22. */
  23. #include <netlink-private/netlink.h>
  24. #include <netlink-private/tc.h>
  25. #include <netlink/netlink.h>
  26. #include <netlink-private/route/tc-api.h>
  27. #include <netlink/route/classifier.h>
  28. #include <netlink/route/action.h>
  29. #include <netlink/route/cls/basic.h>
  30. #include <netlink/route/cls/ematch.h>
  31. struct rtnl_basic
  32. {
  33. uint32_t b_target;
  34. struct rtnl_ematch_tree * b_ematch;
  35. int b_mask;
  36. struct rtnl_act * b_act;
  37. };
  38. /** @cond SKIP */
  39. #define BASIC_ATTR_TARGET 0x001
  40. #define BASIC_ATTR_EMATCH 0x002
  41. #define BASIC_ATTR_ACTION 0x004
  42. /** @endcond */
  43. static struct nla_policy basic_policy[TCA_BASIC_MAX+1] = {
  44. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  45. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  46. };
  47. static int basic_clone(void *_dst, void *_src)
  48. {
  49. return -NLE_OPNOTSUPP;
  50. }
  51. static void basic_free_data(struct rtnl_tc *tc, void *data)
  52. {
  53. struct rtnl_basic *b = data;
  54. if (!b)
  55. return;
  56. if (b->b_act)
  57. rtnl_act_put_all(&b->b_act);
  58. rtnl_ematch_tree_free(b->b_ematch);
  59. }
  60. static int basic_msg_parser(struct rtnl_tc *tc, void *data)
  61. {
  62. struct nlattr *tb[TCA_BASIC_MAX + 1];
  63. struct rtnl_basic *b = data;
  64. int err;
  65. err = tca_parse(tb, TCA_BASIC_MAX, tc, basic_policy);
  66. if (err < 0)
  67. return err;
  68. if (tb[TCA_BASIC_CLASSID]) {
  69. b->b_target = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  70. b->b_mask |= BASIC_ATTR_TARGET;
  71. }
  72. if (tb[TCA_BASIC_EMATCHES]) {
  73. if ((err = rtnl_ematch_parse_attr(tb[TCA_BASIC_EMATCHES],
  74. &b->b_ematch)) < 0)
  75. return err;
  76. if (b->b_ematch)
  77. b->b_mask |= BASIC_ATTR_EMATCH;
  78. }
  79. if (tb[TCA_BASIC_ACT]) {
  80. b->b_mask |= BASIC_ATTR_ACTION;
  81. err = rtnl_act_parse(&b->b_act, tb[TCA_BASIC_ACT]);
  82. if (err)
  83. return err;
  84. }
  85. return 0;
  86. }
  87. static void basic_dump_line(struct rtnl_tc *tc, void *data,
  88. struct nl_dump_params *p)
  89. {
  90. struct rtnl_basic *b = data;
  91. char buf[32];
  92. if (!b)
  93. return;
  94. if (b->b_mask & BASIC_ATTR_EMATCH)
  95. nl_dump(p, " ematch");
  96. else
  97. nl_dump(p, " match-all");
  98. if (b->b_mask & BASIC_ATTR_TARGET)
  99. nl_dump(p, " target %s",
  100. rtnl_tc_handle2str(b->b_target, buf, sizeof(buf)));
  101. }
  102. static void basic_dump_details(struct rtnl_tc *tc, void *data,
  103. struct nl_dump_params *p)
  104. {
  105. struct rtnl_basic *b = data;
  106. if (!b)
  107. return;
  108. if (b->b_mask & BASIC_ATTR_EMATCH) {
  109. nl_dump_line(p, " ematch ");
  110. rtnl_ematch_tree_dump(b->b_ematch, p);
  111. } else
  112. nl_dump(p, "no options.\n");
  113. }
  114. static int basic_msg_fill(struct rtnl_tc *tc, void *data,
  115. struct nl_msg *msg)
  116. {
  117. struct rtnl_basic *b = data;
  118. if (!b)
  119. return 0;
  120. if (b->b_mask & BASIC_ATTR_TARGET)
  121. NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_target);
  122. if (b->b_mask & BASIC_ATTR_EMATCH &&
  123. rtnl_ematch_fill_attr(msg, TCA_BASIC_EMATCHES, b->b_ematch) < 0)
  124. goto nla_put_failure;
  125. if (b->b_mask & BASIC_ATTR_ACTION) {
  126. int err;
  127. err = rtnl_act_fill(msg, TCA_BASIC_ACT, b->b_act);
  128. if (err)
  129. return err;
  130. }
  131. return 0;
  132. nla_put_failure:
  133. return -NLE_NOMEM;
  134. }
  135. /**
  136. * @name Attribute Modifications
  137. * @{
  138. */
  139. void rtnl_basic_set_target(struct rtnl_cls *cls, uint32_t target)
  140. {
  141. struct rtnl_basic *b;
  142. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  143. return;
  144. b->b_target = target;
  145. b->b_mask |= BASIC_ATTR_TARGET;
  146. }
  147. uint32_t rtnl_basic_get_target(struct rtnl_cls *cls)
  148. {
  149. struct rtnl_basic *b;
  150. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  151. return 0;
  152. return b->b_target;
  153. }
  154. void rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
  155. {
  156. struct rtnl_basic *b;
  157. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  158. return;
  159. if (b->b_ematch) {
  160. rtnl_ematch_tree_free(b->b_ematch);
  161. b->b_mask &= ~BASIC_ATTR_EMATCH;
  162. }
  163. b->b_ematch = tree;
  164. if (tree)
  165. b->b_mask |= BASIC_ATTR_EMATCH;
  166. }
  167. struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
  168. {
  169. struct rtnl_basic *b;
  170. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  171. return NULL;
  172. return b->b_ematch;
  173. }
  174. int rtnl_basic_add_action(struct rtnl_cls *cls, struct rtnl_act *act)
  175. {
  176. struct rtnl_basic *b;
  177. if (!act)
  178. return 0;
  179. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  180. return -NLE_NOMEM;
  181. b->b_mask |= BASIC_ATTR_ACTION;
  182. /* In case user frees it */
  183. rtnl_act_get(act);
  184. return rtnl_act_append(&b->b_act, act);
  185. }
  186. int rtnl_basic_del_action(struct rtnl_cls *cls, struct rtnl_act *act)
  187. {
  188. struct rtnl_basic *b;
  189. int ret;
  190. if (!act)
  191. return 0;
  192. if (!(b = rtnl_tc_data(TC_CAST(cls))))
  193. return -NLE_NOMEM;
  194. if (!(b->b_mask & BASIC_ATTR_ACTION))
  195. return -NLE_INVAL;
  196. ret = rtnl_act_remove(&b->b_act, act);
  197. if (ret)
  198. return ret;
  199. if (!b->b_act)
  200. b->b_mask &= ~BASIC_ATTR_ACTION;
  201. rtnl_act_put(act);
  202. return 0;
  203. }
  204. /** @} */
  205. static struct rtnl_tc_ops basic_ops = {
  206. .to_kind = "basic",
  207. .to_type = RTNL_TC_TYPE_CLS,
  208. .to_size = sizeof(struct rtnl_basic),
  209. .to_msg_parser = basic_msg_parser,
  210. .to_clone = basic_clone,
  211. .to_free_data = basic_free_data,
  212. .to_msg_fill = basic_msg_fill,
  213. .to_dump = {
  214. [NL_DUMP_LINE] = basic_dump_line,
  215. [NL_DUMP_DETAILS] = basic_dump_details,
  216. },
  217. };
  218. static void __init basic_init(void)
  219. {
  220. rtnl_tc_register(&basic_ops);
  221. }
  222. static void __exit basic_exit(void)
  223. {
  224. rtnl_tc_unregister(&basic_ops);
  225. }
  226. /** @} */