cls_fw.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
  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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. *
  11. * Changes:
  12. * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
  13. * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
  14. * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
  15. *
  16. * JHS: We should remove the CONFIG_NET_CLS_IND from here
  17. * eventually when the meta match extension is made available
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/errno.h>
  26. #include <linux/skbuff.h>
  27. #include <net/netlink.h>
  28. #include <net/act_api.h>
  29. #include <net/pkt_cls.h>
  30. #define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
  31. struct fw_head {
  32. struct fw_filter *ht[HTSIZE];
  33. u32 mask;
  34. };
  35. struct fw_filter {
  36. struct fw_filter *next;
  37. u32 id;
  38. struct tcf_result res;
  39. #ifdef CONFIG_NET_CLS_IND
  40. char indev[IFNAMSIZ];
  41. #endif /* CONFIG_NET_CLS_IND */
  42. struct tcf_exts exts;
  43. };
  44. static const struct tcf_ext_map fw_ext_map = {
  45. .action = TCA_FW_ACT,
  46. .police = TCA_FW_POLICE
  47. };
  48. static inline int fw_hash(u32 handle)
  49. {
  50. if (HTSIZE == 4096)
  51. return ((handle >> 24) & 0xFFF) ^
  52. ((handle >> 12) & 0xFFF) ^
  53. (handle & 0xFFF);
  54. else if (HTSIZE == 2048)
  55. return ((handle >> 22) & 0x7FF) ^
  56. ((handle >> 11) & 0x7FF) ^
  57. (handle & 0x7FF);
  58. else if (HTSIZE == 1024)
  59. return ((handle >> 20) & 0x3FF) ^
  60. ((handle >> 10) & 0x3FF) ^
  61. (handle & 0x3FF);
  62. else if (HTSIZE == 512)
  63. return (handle >> 27) ^
  64. ((handle >> 18) & 0x1FF) ^
  65. ((handle >> 9) & 0x1FF) ^
  66. (handle & 0x1FF);
  67. else if (HTSIZE == 256) {
  68. u8 *t = (u8 *) &handle;
  69. return t[0] ^ t[1] ^ t[2] ^ t[3];
  70. } else
  71. return handle & (HTSIZE - 1);
  72. }
  73. static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
  74. struct tcf_result *res)
  75. {
  76. struct fw_head *head = (struct fw_head *)tp->root;
  77. struct fw_filter *f;
  78. int r;
  79. u32 id = skb->mark;
  80. if (head != NULL) {
  81. id &= head->mask;
  82. for (f = head->ht[fw_hash(id)]; f; f = f->next) {
  83. if (f->id == id) {
  84. *res = f->res;
  85. #ifdef CONFIG_NET_CLS_IND
  86. if (!tcf_match_indev(skb, f->indev))
  87. continue;
  88. #endif /* CONFIG_NET_CLS_IND */
  89. r = tcf_exts_exec(skb, &f->exts, res);
  90. if (r < 0)
  91. continue;
  92. return r;
  93. }
  94. }
  95. } else {
  96. /* old method */
  97. if (id && (TC_H_MAJ(id) == 0 ||
  98. !(TC_H_MAJ(id ^ tp->q->handle)))) {
  99. res->classid = id;
  100. res->class = 0;
  101. return 0;
  102. }
  103. }
  104. return -1;
  105. }
  106. static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
  107. {
  108. struct fw_head *head = (struct fw_head *)tp->root;
  109. struct fw_filter *f;
  110. if (head == NULL)
  111. return 0;
  112. for (f = head->ht[fw_hash(handle)]; f; f = f->next) {
  113. if (f->id == handle)
  114. return (unsigned long)f;
  115. }
  116. return 0;
  117. }
  118. static void fw_put(struct tcf_proto *tp, unsigned long f)
  119. {
  120. }
  121. static int fw_init(struct tcf_proto *tp)
  122. {
  123. return 0;
  124. }
  125. static void fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
  126. {
  127. tcf_unbind_filter(tp, &f->res);
  128. tcf_exts_destroy(tp, &f->exts);
  129. kfree(f);
  130. }
  131. static void fw_destroy(struct tcf_proto *tp)
  132. {
  133. struct fw_head *head = tp->root;
  134. struct fw_filter *f;
  135. int h;
  136. if (head == NULL)
  137. return;
  138. for (h = 0; h < HTSIZE; h++) {
  139. while ((f = head->ht[h]) != NULL) {
  140. head->ht[h] = f->next;
  141. fw_delete_filter(tp, f);
  142. }
  143. }
  144. kfree(head);
  145. }
  146. static int fw_delete(struct tcf_proto *tp, unsigned long arg)
  147. {
  148. struct fw_head *head = (struct fw_head *)tp->root;
  149. struct fw_filter *f = (struct fw_filter *)arg;
  150. struct fw_filter **fp;
  151. if (head == NULL || f == NULL)
  152. goto out;
  153. for (fp = &head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
  154. if (*fp == f) {
  155. tcf_tree_lock(tp);
  156. *fp = f->next;
  157. tcf_tree_unlock(tp);
  158. fw_delete_filter(tp, f);
  159. return 0;
  160. }
  161. }
  162. out:
  163. return -EINVAL;
  164. }
  165. static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
  166. [TCA_FW_CLASSID] = { .type = NLA_U32 },
  167. [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  168. [TCA_FW_MASK] = { .type = NLA_U32 },
  169. };
  170. static int
  171. fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
  172. struct nlattr **tb, struct nlattr **tca, unsigned long base)
  173. {
  174. struct fw_head *head = (struct fw_head *)tp->root;
  175. struct tcf_exts e;
  176. u32 mask;
  177. int err;
  178. err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &fw_ext_map);
  179. if (err < 0)
  180. return err;
  181. err = -EINVAL;
  182. if (tb[TCA_FW_CLASSID]) {
  183. f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
  184. tcf_bind_filter(tp, &f->res, base);
  185. }
  186. #ifdef CONFIG_NET_CLS_IND
  187. if (tb[TCA_FW_INDEV]) {
  188. err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV]);
  189. if (err < 0)
  190. goto errout;
  191. }
  192. #endif /* CONFIG_NET_CLS_IND */
  193. if (tb[TCA_FW_MASK]) {
  194. mask = nla_get_u32(tb[TCA_FW_MASK]);
  195. if (mask != head->mask)
  196. goto errout;
  197. } else if (head->mask != 0xFFFFFFFF)
  198. goto errout;
  199. tcf_exts_change(tp, &f->exts, &e);
  200. return 0;
  201. errout:
  202. tcf_exts_destroy(tp, &e);
  203. return err;
  204. }
  205. static int fw_change(struct tcf_proto *tp, unsigned long base,
  206. u32 handle,
  207. struct nlattr **tca,
  208. unsigned long *arg)
  209. {
  210. struct fw_head *head = (struct fw_head *)tp->root;
  211. struct fw_filter *f = (struct fw_filter *) *arg;
  212. struct nlattr *opt = tca[TCA_OPTIONS];
  213. struct nlattr *tb[TCA_FW_MAX + 1];
  214. int err;
  215. if (!opt)
  216. return handle ? -EINVAL : 0;
  217. err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy);
  218. if (err < 0)
  219. return err;
  220. if (f != NULL) {
  221. if (f->id != handle && handle)
  222. return -EINVAL;
  223. return fw_change_attrs(tp, f, tb, tca, base);
  224. }
  225. if (!handle)
  226. return -EINVAL;
  227. if (head == NULL) {
  228. u32 mask = 0xFFFFFFFF;
  229. if (tb[TCA_FW_MASK])
  230. mask = nla_get_u32(tb[TCA_FW_MASK]);
  231. head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
  232. if (head == NULL)
  233. return -ENOBUFS;
  234. head->mask = mask;
  235. tcf_tree_lock(tp);
  236. tp->root = head;
  237. tcf_tree_unlock(tp);
  238. }
  239. f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  240. if (f == NULL)
  241. return -ENOBUFS;
  242. f->id = handle;
  243. err = fw_change_attrs(tp, f, tb, tca, base);
  244. if (err < 0)
  245. goto errout;
  246. f->next = head->ht[fw_hash(handle)];
  247. tcf_tree_lock(tp);
  248. head->ht[fw_hash(handle)] = f;
  249. tcf_tree_unlock(tp);
  250. *arg = (unsigned long)f;
  251. return 0;
  252. errout:
  253. kfree(f);
  254. return err;
  255. }
  256. static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  257. {
  258. struct fw_head *head = (struct fw_head *)tp->root;
  259. int h;
  260. if (head == NULL)
  261. arg->stop = 1;
  262. if (arg->stop)
  263. return;
  264. for (h = 0; h < HTSIZE; h++) {
  265. struct fw_filter *f;
  266. for (f = head->ht[h]; f; f = f->next) {
  267. if (arg->count < arg->skip) {
  268. arg->count++;
  269. continue;
  270. }
  271. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  272. arg->stop = 1;
  273. return;
  274. }
  275. arg->count++;
  276. }
  277. }
  278. }
  279. static int fw_dump(struct tcf_proto *tp, unsigned long fh,
  280. struct sk_buff *skb, struct tcmsg *t)
  281. {
  282. struct fw_head *head = (struct fw_head *)tp->root;
  283. struct fw_filter *f = (struct fw_filter *)fh;
  284. unsigned char *b = skb_tail_pointer(skb);
  285. struct nlattr *nest;
  286. if (f == NULL)
  287. return skb->len;
  288. t->tcm_handle = f->id;
  289. if (!f->res.classid && !tcf_exts_is_available(&f->exts))
  290. return skb->len;
  291. nest = nla_nest_start(skb, TCA_OPTIONS);
  292. if (nest == NULL)
  293. goto nla_put_failure;
  294. if (f->res.classid)
  295. NLA_PUT_U32(skb, TCA_FW_CLASSID, f->res.classid);
  296. #ifdef CONFIG_NET_CLS_IND
  297. if (strlen(f->indev))
  298. NLA_PUT_STRING(skb, TCA_FW_INDEV, f->indev);
  299. #endif /* CONFIG_NET_CLS_IND */
  300. if (head->mask != 0xFFFFFFFF)
  301. NLA_PUT_U32(skb, TCA_FW_MASK, head->mask);
  302. if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
  303. goto nla_put_failure;
  304. nla_nest_end(skb, nest);
  305. if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
  306. goto nla_put_failure;
  307. return skb->len;
  308. nla_put_failure:
  309. nlmsg_trim(skb, b);
  310. return -1;
  311. }
  312. static struct tcf_proto_ops cls_fw_ops __read_mostly = {
  313. .kind = "fw",
  314. .classify = fw_classify,
  315. .init = fw_init,
  316. .destroy = fw_destroy,
  317. .get = fw_get,
  318. .put = fw_put,
  319. .change = fw_change,
  320. .delete = fw_delete,
  321. .walk = fw_walk,
  322. .dump = fw_dump,
  323. .owner = THIS_MODULE,
  324. };
  325. static int __init init_fw(void)
  326. {
  327. return register_tcf_proto_ops(&cls_fw_ops);
  328. }
  329. static void __exit exit_fw(void)
  330. {
  331. unregister_tcf_proto_ops(&cls_fw_ops);
  332. }
  333. module_init(init_fw)
  334. module_exit(exit_fw)
  335. MODULE_LICENSE("GPL");