cls_fw.c 9.6 KB

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