cls_fw.c 7.9 KB

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