cls_fw.c 8.7 KB

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