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 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 = (struct fw_head*)xchg(&tp->root, NULL);
  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 int
  167. fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
  168. struct rtattr **tb, struct rtattr **tca, unsigned long base)
  169. {
  170. struct fw_head *head = (struct fw_head *)tp->root;
  171. struct tcf_exts e;
  172. u32 mask;
  173. int err;
  174. err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
  175. if (err < 0)
  176. return err;
  177. err = -EINVAL;
  178. if (tb[TCA_FW_CLASSID-1]) {
  179. if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
  180. goto errout;
  181. f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
  182. tcf_bind_filter(tp, &f->res, base);
  183. }
  184. #ifdef CONFIG_NET_CLS_IND
  185. if (tb[TCA_FW_INDEV-1]) {
  186. err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
  187. if (err < 0)
  188. goto errout;
  189. }
  190. #endif /* CONFIG_NET_CLS_IND */
  191. if (tb[TCA_FW_MASK-1]) {
  192. if (RTA_PAYLOAD(tb[TCA_FW_MASK-1]) != sizeof(u32))
  193. goto errout;
  194. mask = *(u32*)RTA_DATA(tb[TCA_FW_MASK-1]);
  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 rtattr **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 rtattr *opt = tca[TCA_OPTIONS-1];
  213. struct rtattr *tb[TCA_FW_MAX];
  214. int err;
  215. if (!opt)
  216. return handle ? -EINVAL : 0;
  217. if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
  218. return -EINVAL;
  219. if (f != NULL) {
  220. if (f->id != handle && handle)
  221. return -EINVAL;
  222. return fw_change_attrs(tp, f, tb, tca, base);
  223. }
  224. if (!handle)
  225. return -EINVAL;
  226. if (head == NULL) {
  227. u32 mask = 0xFFFFFFFF;
  228. if (tb[TCA_FW_MASK-1]) {
  229. if (RTA_PAYLOAD(tb[TCA_FW_MASK-1]) != sizeof(u32))
  230. return -EINVAL;
  231. mask = *(u32*)RTA_DATA(tb[TCA_FW_MASK-1]);
  232. }
  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 rtattr *rta;
  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. rta = (struct rtattr*)b;
  294. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  295. if (f->res.classid)
  296. RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
  297. #ifdef CONFIG_NET_CLS_IND
  298. if (strlen(f->indev))
  299. RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
  300. #endif /* CONFIG_NET_CLS_IND */
  301. if (head->mask != 0xFFFFFFFF)
  302. RTA_PUT(skb, TCA_FW_MASK, 4, &head->mask);
  303. if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
  304. goto rtattr_failure;
  305. rta->rta_len = skb_tail_pointer(skb) - b;
  306. if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
  307. goto rtattr_failure;
  308. return skb->len;
  309. rtattr_failure:
  310. nlmsg_trim(skb, b);
  311. return -1;
  312. }
  313. static struct tcf_proto_ops cls_fw_ops = {
  314. .next = NULL,
  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");