cls_fw.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <asm/uaccess.h>
  22. #include <asm/system.h>
  23. #include <linux/bitops.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/string.h>
  28. #include <linux/mm.h>
  29. #include <linux/socket.h>
  30. #include <linux/sockios.h>
  31. #include <linux/in.h>
  32. #include <linux/errno.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/if_ether.h>
  35. #include <linux/inet.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/notifier.h>
  39. #include <linux/netfilter.h>
  40. #include <net/ip.h>
  41. #include <net/route.h>
  42. #include <linux/skbuff.h>
  43. #include <net/sock.h>
  44. #include <net/act_api.h>
  45. #include <net/pkt_cls.h>
  46. #define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
  47. struct fw_head
  48. {
  49. struct fw_filter *ht[HTSIZE];
  50. };
  51. struct fw_filter
  52. {
  53. struct fw_filter *next;
  54. u32 id;
  55. struct tcf_result res;
  56. #ifdef CONFIG_NET_CLS_IND
  57. char indev[IFNAMSIZ];
  58. #endif /* CONFIG_NET_CLS_IND */
  59. struct tcf_exts exts;
  60. };
  61. static struct tcf_ext_map fw_ext_map = {
  62. .action = TCA_FW_ACT,
  63. .police = TCA_FW_POLICE
  64. };
  65. static __inline__ int fw_hash(u32 handle)
  66. {
  67. if (HTSIZE == 4096)
  68. return ((handle >> 24) & 0xFFF) ^
  69. ((handle >> 12) & 0xFFF) ^
  70. (handle & 0xFFF);
  71. else if (HTSIZE == 2048)
  72. return ((handle >> 22) & 0x7FF) ^
  73. ((handle >> 11) & 0x7FF) ^
  74. (handle & 0x7FF);
  75. else if (HTSIZE == 1024)
  76. return ((handle >> 20) & 0x3FF) ^
  77. ((handle >> 10) & 0x3FF) ^
  78. (handle & 0x3FF);
  79. else if (HTSIZE == 512)
  80. return (handle >> 27) ^
  81. ((handle >> 18) & 0x1FF) ^
  82. ((handle >> 9) & 0x1FF) ^
  83. (handle & 0x1FF);
  84. else if (HTSIZE == 256) {
  85. u8 *t = (u8 *) &handle;
  86. return t[0] ^ t[1] ^ t[2] ^ t[3];
  87. } else
  88. return handle & (HTSIZE - 1);
  89. }
  90. static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
  91. struct tcf_result *res)
  92. {
  93. struct fw_head *head = (struct fw_head*)tp->root;
  94. struct fw_filter *f;
  95. int r;
  96. #ifdef CONFIG_NETFILTER
  97. u32 id = skb->nfmark;
  98. #else
  99. u32 id = 0;
  100. #endif
  101. if (head != NULL) {
  102. for (f=head->ht[fw_hash(id)]; f; f=f->next) {
  103. if (f->id == id) {
  104. *res = f->res;
  105. #ifdef CONFIG_NET_CLS_IND
  106. if (!tcf_match_indev(skb, f->indev))
  107. continue;
  108. #endif /* CONFIG_NET_CLS_IND */
  109. r = tcf_exts_exec(skb, &f->exts, res);
  110. if (r < 0)
  111. continue;
  112. return r;
  113. }
  114. }
  115. } else {
  116. /* old method */
  117. if (id && (TC_H_MAJ(id) == 0 || !(TC_H_MAJ(id^tp->q->handle)))) {
  118. res->classid = id;
  119. res->class = 0;
  120. return 0;
  121. }
  122. }
  123. return -1;
  124. }
  125. static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
  126. {
  127. struct fw_head *head = (struct fw_head*)tp->root;
  128. struct fw_filter *f;
  129. if (head == NULL)
  130. return 0;
  131. for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
  132. if (f->id == handle)
  133. return (unsigned long)f;
  134. }
  135. return 0;
  136. }
  137. static void fw_put(struct tcf_proto *tp, unsigned long f)
  138. {
  139. }
  140. static int fw_init(struct tcf_proto *tp)
  141. {
  142. return 0;
  143. }
  144. static inline void
  145. fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
  146. {
  147. tcf_unbind_filter(tp, &f->res);
  148. tcf_exts_destroy(tp, &f->exts);
  149. kfree(f);
  150. }
  151. static void fw_destroy(struct tcf_proto *tp)
  152. {
  153. struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
  154. struct fw_filter *f;
  155. int h;
  156. if (head == NULL)
  157. return;
  158. for (h=0; h<HTSIZE; h++) {
  159. while ((f=head->ht[h]) != NULL) {
  160. head->ht[h] = f->next;
  161. fw_delete_filter(tp, f);
  162. }
  163. }
  164. kfree(head);
  165. }
  166. static int fw_delete(struct tcf_proto *tp, unsigned long arg)
  167. {
  168. struct fw_head *head = (struct fw_head*)tp->root;
  169. struct fw_filter *f = (struct fw_filter*)arg;
  170. struct fw_filter **fp;
  171. if (head == NULL || f == NULL)
  172. goto out;
  173. for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
  174. if (*fp == f) {
  175. tcf_tree_lock(tp);
  176. *fp = f->next;
  177. tcf_tree_unlock(tp);
  178. fw_delete_filter(tp, f);
  179. return 0;
  180. }
  181. }
  182. out:
  183. return -EINVAL;
  184. }
  185. static int
  186. fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
  187. struct rtattr **tb, struct rtattr **tca, unsigned long base)
  188. {
  189. struct tcf_exts e;
  190. int err;
  191. err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
  192. if (err < 0)
  193. return err;
  194. err = -EINVAL;
  195. if (tb[TCA_FW_CLASSID-1]) {
  196. if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
  197. goto errout;
  198. f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
  199. tcf_bind_filter(tp, &f->res, base);
  200. }
  201. #ifdef CONFIG_NET_CLS_IND
  202. if (tb[TCA_FW_INDEV-1]) {
  203. err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
  204. if (err < 0)
  205. goto errout;
  206. }
  207. #endif /* CONFIG_NET_CLS_IND */
  208. tcf_exts_change(tp, &f->exts, &e);
  209. return 0;
  210. errout:
  211. tcf_exts_destroy(tp, &e);
  212. return err;
  213. }
  214. static int fw_change(struct tcf_proto *tp, unsigned long base,
  215. u32 handle,
  216. struct rtattr **tca,
  217. unsigned long *arg)
  218. {
  219. struct fw_head *head = (struct fw_head*)tp->root;
  220. struct fw_filter *f = (struct fw_filter *) *arg;
  221. struct rtattr *opt = tca[TCA_OPTIONS-1];
  222. struct rtattr *tb[TCA_FW_MAX];
  223. int err;
  224. if (!opt)
  225. return handle ? -EINVAL : 0;
  226. if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
  227. return -EINVAL;
  228. if (f != NULL) {
  229. if (f->id != handle && handle)
  230. return -EINVAL;
  231. return fw_change_attrs(tp, f, tb, tca, base);
  232. }
  233. if (!handle)
  234. return -EINVAL;
  235. if (head == NULL) {
  236. head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
  237. if (head == NULL)
  238. return -ENOBUFS;
  239. tcf_tree_lock(tp);
  240. tp->root = head;
  241. tcf_tree_unlock(tp);
  242. }
  243. f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  244. if (f == NULL)
  245. return -ENOBUFS;
  246. f->id = handle;
  247. err = fw_change_attrs(tp, f, tb, tca, base);
  248. if (err < 0)
  249. goto errout;
  250. f->next = head->ht[fw_hash(handle)];
  251. tcf_tree_lock(tp);
  252. head->ht[fw_hash(handle)] = f;
  253. tcf_tree_unlock(tp);
  254. *arg = (unsigned long)f;
  255. return 0;
  256. errout:
  257. kfree(f);
  258. return err;
  259. }
  260. static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  261. {
  262. struct fw_head *head = (struct fw_head*)tp->root;
  263. int h;
  264. if (head == NULL)
  265. arg->stop = 1;
  266. if (arg->stop)
  267. return;
  268. for (h = 0; h < HTSIZE; h++) {
  269. struct fw_filter *f;
  270. for (f = head->ht[h]; f; f = f->next) {
  271. if (arg->count < arg->skip) {
  272. arg->count++;
  273. continue;
  274. }
  275. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  276. arg->stop = 1;
  277. return;
  278. }
  279. arg->count++;
  280. }
  281. }
  282. }
  283. static int fw_dump(struct tcf_proto *tp, unsigned long fh,
  284. struct sk_buff *skb, struct tcmsg *t)
  285. {
  286. struct fw_filter *f = (struct fw_filter*)fh;
  287. unsigned char *b = skb->tail;
  288. struct rtattr *rta;
  289. if (f == NULL)
  290. return skb->len;
  291. t->tcm_handle = f->id;
  292. if (!f->res.classid && !tcf_exts_is_available(&f->exts))
  293. return skb->len;
  294. rta = (struct rtattr*)b;
  295. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  296. if (f->res.classid)
  297. RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
  298. #ifdef CONFIG_NET_CLS_IND
  299. if (strlen(f->indev))
  300. RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
  301. #endif /* CONFIG_NET_CLS_IND */
  302. if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
  303. goto rtattr_failure;
  304. rta->rta_len = skb->tail - b;
  305. if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
  306. goto rtattr_failure;
  307. return skb->len;
  308. rtattr_failure:
  309. skb_trim(skb, b - skb->data);
  310. return -1;
  311. }
  312. static struct tcf_proto_ops cls_fw_ops = {
  313. .next = NULL,
  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");