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