cls_fw.c 10 KB

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