cls_fw.c 8.3 KB

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