fw.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2013 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
  15. * @defgroup cls_fw Firewall Classifier
  16. *
  17. * @{
  18. */
  19. #include <netlink-private/netlink.h>
  20. #include <netlink-private/tc.h>
  21. #include <netlink/netlink.h>
  22. #include <netlink-private/route/tc-api.h>
  23. #include <netlink/route/classifier.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. #define FW_ATTR_MASK 0x010
  31. /** @endcond */
  32. static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
  33. [TCA_FW_CLASSID] = { .type = NLA_U32 },
  34. [TCA_FW_INDEV] = { .type = NLA_STRING,
  35. .maxlen = IFNAMSIZ },
  36. [TCA_FW_MASK] = { .type = NLA_U32 },
  37. };
  38. static int fw_msg_parser(struct rtnl_tc *tc, void *data)
  39. {
  40. struct nlattr *tb[TCA_FW_MAX + 1];
  41. struct rtnl_fw *f = data;
  42. int err;
  43. err = tca_parse(tb, TCA_FW_MAX, tc, fw_policy);
  44. if (err < 0)
  45. return err;
  46. if (tb[TCA_FW_CLASSID]) {
  47. f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
  48. f->cf_mask |= FW_ATTR_CLASSID;
  49. }
  50. if (tb[TCA_FW_ACT]) {
  51. f->cf_act = nl_data_alloc_attr(tb[TCA_FW_ACT]);
  52. if (!f->cf_act)
  53. return -NLE_NOMEM;
  54. f->cf_mask |= FW_ATTR_ACTION;
  55. }
  56. if (tb[TCA_FW_POLICE]) {
  57. f->cf_police = nl_data_alloc_attr(tb[TCA_FW_POLICE]);
  58. if (!f->cf_police)
  59. return -NLE_NOMEM;
  60. f->cf_mask |= FW_ATTR_POLICE;
  61. }
  62. if (tb[TCA_FW_INDEV]) {
  63. nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
  64. f->cf_mask |= FW_ATTR_INDEV;
  65. }
  66. if (tb[TCA_FW_MASK]) {
  67. f->cf_fwmask = nla_get_u32(tb[TCA_FW_MASK]);
  68. f->cf_mask |= FW_ATTR_MASK;
  69. }
  70. return 0;
  71. }
  72. static void fw_free_data(struct rtnl_tc *tc, void *data)
  73. {
  74. struct rtnl_fw *f = data;
  75. nl_data_free(f->cf_act);
  76. nl_data_free(f->cf_police);
  77. }
  78. static int fw_clone(void *_dst, void *_src)
  79. {
  80. struct rtnl_fw *dst = _dst, *src = _src;
  81. if (src->cf_act && !(dst->cf_act = nl_data_clone(src->cf_act)))
  82. return -NLE_NOMEM;
  83. if (src->cf_police && !(dst->cf_police = nl_data_clone(src->cf_police)))
  84. return -NLE_NOMEM;
  85. return 0;
  86. }
  87. static void fw_dump_line(struct rtnl_tc *tc, void *data,
  88. struct nl_dump_params *p)
  89. {
  90. struct rtnl_fw *f = data;
  91. if (!f)
  92. return;
  93. if (f->cf_mask & FW_ATTR_CLASSID) {
  94. char buf[32];
  95. nl_dump(p, " target %s",
  96. rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
  97. }
  98. if (f->cf_mask & FW_ATTR_MASK)
  99. nl_dump(p, " mask 0x%x", f->cf_fwmask);
  100. }
  101. static void fw_dump_details(struct rtnl_tc *tc, void *data,
  102. struct nl_dump_params *p)
  103. {
  104. struct rtnl_fw *f = data;
  105. if (f && f->cf_mask & FW_ATTR_INDEV)
  106. nl_dump(p, "indev %s ", f->cf_indev);
  107. }
  108. static int fw_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
  109. {
  110. struct rtnl_fw *f = data;
  111. if (!f)
  112. return 0;
  113. if (f->cf_mask & FW_ATTR_CLASSID)
  114. NLA_PUT_U32(msg, TCA_FW_CLASSID, f->cf_classid);
  115. if (f->cf_mask & FW_ATTR_ACTION)
  116. NLA_PUT_DATA(msg, TCA_FW_ACT, f->cf_act);
  117. if (f->cf_mask & FW_ATTR_POLICE)
  118. NLA_PUT_DATA(msg, TCA_FW_POLICE, f->cf_police);
  119. if (f->cf_mask & FW_ATTR_INDEV)
  120. NLA_PUT_STRING(msg, TCA_FW_INDEV, f->cf_indev);
  121. if (f->cf_mask & FW_ATTR_MASK)
  122. NLA_PUT_U32(msg, TCA_FW_MASK, f->cf_fwmask);
  123. return 0;
  124. nla_put_failure:
  125. return -NLE_MSGSIZE;
  126. }
  127. /**
  128. * @name Attribute Modifications
  129. * @{
  130. */
  131. int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
  132. {
  133. struct rtnl_fw *f;
  134. if (!(f = rtnl_tc_data(TC_CAST(cls))))
  135. return -NLE_NOMEM;
  136. f->cf_classid = classid;
  137. f->cf_mask |= FW_ATTR_CLASSID;
  138. return 0;
  139. }
  140. int rtnl_fw_set_mask(struct rtnl_cls *cls, uint32_t mask)
  141. {
  142. struct rtnl_fw *f;
  143. if (!(f = rtnl_tc_data(TC_CAST(cls))))
  144. return -NLE_NOMEM;
  145. f->cf_fwmask = mask;
  146. f->cf_mask |= FW_ATTR_MASK;
  147. return 0;
  148. }
  149. /** @} */
  150. static struct rtnl_tc_ops fw_ops = {
  151. .to_kind = "fw",
  152. .to_type = RTNL_TC_TYPE_CLS,
  153. .to_size = sizeof(struct rtnl_fw),
  154. .to_msg_parser = fw_msg_parser,
  155. .to_msg_fill = fw_msg_fill,
  156. .to_free_data = fw_free_data,
  157. .to_clone = fw_clone,
  158. .to_dump = {
  159. [NL_DUMP_LINE] = fw_dump_line,
  160. [NL_DUMP_DETAILS] = fw_dump_details,
  161. },
  162. };
  163. static void __init fw_init(void)
  164. {
  165. rtnl_tc_register(&fw_ops);
  166. }
  167. static void __exit fw_exit(void)
  168. {
  169. rtnl_tc_unregister(&fw_ops);
  170. }
  171. /** @} */