cls_fw.c 8.8 KB

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