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