cls_fw.c 9.4 KB

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