cls_fw.c 8.5 KB

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