cls_fw.c 10.0 KB

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