fw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * lib/route/cls/fw.c fw 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) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
  11. * Copyright (c) 2006 Siemens AG Oesterreich
  12. */
  13. /**
  14. * @ingroup cls_api
  15. * @defgroup fw Firewall Classifier
  16. *
  17. * @{
  18. */
  19. #include <netlink-local.h>
  20. #include <netlink-tc.h>
  21. #include <netlink/netlink.h>
  22. #include <netlink/route/classifier.h>
  23. #include <netlink/route/classifier-modules.h>
  24. #include <netlink/route/cls/fw.h>
  25. /** @cond SKIP */
  26. #define FW_ATTR_CLASSID 0x001
  27. #define FW_ATTR_ACTION 0x002
  28. #define FW_ATTR_POLICE 0x004
  29. #define FW_ATTR_INDEV 0x008
  30. /** @endcond */
  31. static inline struct rtnl_fw *fw_cls(struct rtnl_cls *cls)
  32. {
  33. return (struct rtnl_fw *) cls->c_subdata;
  34. }
  35. static inline struct rtnl_fw *fw_alloc(struct rtnl_cls *cls)
  36. {
  37. if (!cls->c_subdata)
  38. cls->c_subdata = calloc(1, sizeof(struct rtnl_fw));
  39. return fw_cls(cls);
  40. }
  41. static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
  42. [TCA_FW_CLASSID] = { .type = NLA_U32 },
  43. [TCA_FW_INDEV] = { .type = NLA_STRING,
  44. .maxlen = IFNAMSIZ },
  45. };
  46. static int fw_msg_parser(struct rtnl_cls *cls)
  47. {
  48. int err;
  49. struct nlattr *tb[TCA_FW_MAX + 1];
  50. struct rtnl_fw *f;
  51. err = tca_parse(tb, TCA_FW_MAX, (struct rtnl_tca *) cls, fw_policy);
  52. if (err < 0)
  53. return err;
  54. f = fw_alloc(cls);
  55. if (!f)
  56. goto errout_nomem;
  57. if (tb[TCA_FW_CLASSID]) {
  58. f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
  59. f->cf_mask |= FW_ATTR_CLASSID;
  60. }
  61. if (tb[TCA_FW_ACT]) {
  62. f->cf_act = nla_get_data(tb[TCA_FW_ACT]);
  63. if (!f->cf_act)
  64. goto errout_nomem;
  65. f->cf_mask |= FW_ATTR_ACTION;
  66. }
  67. if (tb[TCA_FW_POLICE]) {
  68. f->cf_police = nla_get_data(tb[TCA_FW_POLICE]);
  69. if (!f->cf_police)
  70. goto errout_nomem;
  71. f->cf_mask |= FW_ATTR_POLICE;
  72. }
  73. if (tb[TCA_FW_INDEV]) {
  74. nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
  75. f->cf_mask |= FW_ATTR_INDEV;
  76. }
  77. return 0;
  78. errout_nomem:
  79. err = nl_errno(ENOMEM);
  80. return err;
  81. }
  82. static void fw_free_data(struct rtnl_cls *cls)
  83. {
  84. struct rtnl_fw *f = fw_cls(cls);
  85. if (!f)
  86. return;
  87. nl_data_free(f->cf_act);
  88. nl_data_free(f->cf_police);
  89. free(cls->c_subdata);
  90. }
  91. static int fw_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
  92. {
  93. struct rtnl_fw *dst, *src = fw_cls(_src);
  94. if (!src)
  95. return 0;
  96. dst = fw_alloc(_dst);
  97. if (!dst)
  98. return nl_errno(ENOMEM);
  99. if (src->cf_act)
  100. if (!(dst->cf_act = nl_data_clone(src->cf_act)))
  101. goto errout;
  102. if (src->cf_police)
  103. if (!(dst->cf_police = nl_data_clone(src->cf_police)))
  104. goto errout;
  105. return 0;
  106. errout:
  107. return nl_get_errno();
  108. }
  109. static int fw_dump_brief(struct rtnl_cls *cls, struct nl_dump_params *p,
  110. int line)
  111. {
  112. struct rtnl_fw *f = fw_cls(cls);
  113. char buf[32];
  114. if (!f)
  115. goto ignore;
  116. if (f->cf_mask & FW_ATTR_CLASSID)
  117. dp_dump(p, " target %s",
  118. rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
  119. ignore:
  120. return line;
  121. }
  122. static int fw_dump_full(struct rtnl_cls *cls, struct nl_dump_params *p,
  123. int line)
  124. {
  125. struct rtnl_fw *f = fw_cls(cls);
  126. if (!f)
  127. goto ignore;
  128. if (f->cf_mask & FW_ATTR_INDEV)
  129. dp_dump(p, "indev %s ", f->cf_indev);
  130. ignore:
  131. return line;
  132. }
  133. static int fw_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p,
  134. int line)
  135. {
  136. struct rtnl_fw *f = fw_cls(cls);
  137. if (!f)
  138. goto ignore;
  139. ignore:
  140. return line;
  141. }
  142. static struct nl_msg *fw_get_opts(struct rtnl_cls *cls)
  143. {
  144. struct rtnl_fw *f;
  145. struct nl_msg *msg;
  146. f = fw_cls(cls);
  147. if (!f)
  148. return NULL;
  149. msg = nlmsg_alloc();
  150. if (!msg)
  151. return NULL;
  152. if (f->cf_mask & FW_ATTR_CLASSID)
  153. nla_put_u32(msg, TCA_FW_CLASSID, f->cf_classid);
  154. if (f->cf_mask & FW_ATTR_ACTION)
  155. nla_put_data(msg, TCA_FW_ACT, f->cf_act);
  156. if (f->cf_mask & FW_ATTR_POLICE)
  157. nla_put_data(msg, TCA_FW_POLICE, f->cf_police);
  158. if (f->cf_mask & FW_ATTR_INDEV)
  159. nla_put_string(msg, TCA_FW_INDEV, f->cf_indev);
  160. return msg;
  161. }
  162. /**
  163. * @name Attribute Modifications
  164. * @{
  165. */
  166. int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
  167. {
  168. struct rtnl_fw *f;
  169. f = fw_alloc(cls);
  170. if (!f)
  171. return nl_errno(ENOMEM);
  172. f->cf_classid = classid;
  173. f->cf_mask |= FW_ATTR_CLASSID;
  174. return 0;
  175. }
  176. /** @} */
  177. static struct rtnl_cls_ops fw_ops = {
  178. .co_kind = "fw",
  179. .co_msg_parser = fw_msg_parser,
  180. .co_free_data = fw_free_data,
  181. .co_clone = fw_clone,
  182. .co_get_opts = fw_get_opts,
  183. .co_dump[NL_DUMP_BRIEF] = fw_dump_brief,
  184. .co_dump[NL_DUMP_FULL] = fw_dump_full,
  185. .co_dump[NL_DUMP_STATS] = fw_dump_stats,
  186. };
  187. static void __init fw_init(void)
  188. {
  189. rtnl_cls_register(&fw_ops);
  190. }
  191. static void __exit fw_exit(void)
  192. {
  193. rtnl_cls_unregister(&fw_ops);
  194. }
  195. /** @} */