cls_fw.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
  4. *
  5. * Authors: Alexey Kuznetsov, <[email protected]>
  6. *
  7. * Changes:
  8. * Karlis Peisenieks <[email protected]> : 990415 : fw_walk off by one
  9. * Karlis Peisenieks <[email protected]> : 990415 : fw_delete killed all the filter (and kernel).
  10. * Alex <[email protected]> : 2004xxyy: Added Action extension
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/skbuff.h>
  19. #include <net/netlink.h>
  20. #include <net/act_api.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/sch_generic.h>
  23. #define HTSIZE 256
  24. struct fw_head {
  25. u32 mask;
  26. struct fw_filter __rcu *ht[HTSIZE];
  27. struct rcu_head rcu;
  28. };
  29. struct fw_filter {
  30. struct fw_filter __rcu *next;
  31. u32 id;
  32. struct tcf_result res;
  33. int ifindex;
  34. struct tcf_exts exts;
  35. struct tcf_proto *tp;
  36. struct rcu_work rwork;
  37. };
  38. static u32 fw_hash(u32 handle)
  39. {
  40. handle ^= (handle >> 16);
  41. handle ^= (handle >> 8);
  42. return handle % HTSIZE;
  43. }
  44. static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  45. struct tcf_result *res)
  46. {
  47. struct fw_head *head = rcu_dereference_bh(tp->root);
  48. struct fw_filter *f;
  49. int r;
  50. u32 id = skb->mark;
  51. if (head != NULL) {
  52. id &= head->mask;
  53. for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
  54. f = rcu_dereference_bh(f->next)) {
  55. if (f->id == id) {
  56. *res = f->res;
  57. if (!tcf_match_indev(skb, f->ifindex))
  58. continue;
  59. r = tcf_exts_exec(skb, &f->exts, res);
  60. if (r < 0)
  61. continue;
  62. return r;
  63. }
  64. }
  65. } else {
  66. struct Qdisc *q = tcf_block_q(tp->chain->block);
  67. /* Old method: classify the packet using its skb mark. */
  68. if (id && (TC_H_MAJ(id) == 0 ||
  69. !(TC_H_MAJ(id ^ q->handle)))) {
  70. res->classid = id;
  71. res->class = 0;
  72. return 0;
  73. }
  74. }
  75. return -1;
  76. }
  77. static void *fw_get(struct tcf_proto *tp, u32 handle)
  78. {
  79. struct fw_head *head = rtnl_dereference(tp->root);
  80. struct fw_filter *f;
  81. if (head == NULL)
  82. return NULL;
  83. f = rtnl_dereference(head->ht[fw_hash(handle)]);
  84. for (; f; f = rtnl_dereference(f->next)) {
  85. if (f->id == handle)
  86. return f;
  87. }
  88. return NULL;
  89. }
  90. static int fw_init(struct tcf_proto *tp)
  91. {
  92. /* We don't allocate fw_head here, because in the old method
  93. * we don't need it at all.
  94. */
  95. return 0;
  96. }
  97. static void __fw_delete_filter(struct fw_filter *f)
  98. {
  99. tcf_exts_destroy(&f->exts);
  100. tcf_exts_put_net(&f->exts);
  101. kfree(f);
  102. }
  103. static void fw_delete_filter_work(struct work_struct *work)
  104. {
  105. struct fw_filter *f = container_of(to_rcu_work(work),
  106. struct fw_filter,
  107. rwork);
  108. rtnl_lock();
  109. __fw_delete_filter(f);
  110. rtnl_unlock();
  111. }
  112. static void fw_destroy(struct tcf_proto *tp, bool rtnl_held,
  113. struct netlink_ext_ack *extack)
  114. {
  115. struct fw_head *head = rtnl_dereference(tp->root);
  116. struct fw_filter *f;
  117. int h;
  118. if (head == NULL)
  119. return;
  120. for (h = 0; h < HTSIZE; h++) {
  121. while ((f = rtnl_dereference(head->ht[h])) != NULL) {
  122. RCU_INIT_POINTER(head->ht[h],
  123. rtnl_dereference(f->next));
  124. tcf_unbind_filter(tp, &f->res);
  125. if (tcf_exts_get_net(&f->exts))
  126. tcf_queue_work(&f->rwork, fw_delete_filter_work);
  127. else
  128. __fw_delete_filter(f);
  129. }
  130. }
  131. kfree_rcu(head, rcu);
  132. }
  133. static int fw_delete(struct tcf_proto *tp, void *arg, bool *last,
  134. bool rtnl_held, struct netlink_ext_ack *extack)
  135. {
  136. struct fw_head *head = rtnl_dereference(tp->root);
  137. struct fw_filter *f = arg;
  138. struct fw_filter __rcu **fp;
  139. struct fw_filter *pfp;
  140. int ret = -EINVAL;
  141. int h;
  142. if (head == NULL || f == NULL)
  143. goto out;
  144. fp = &head->ht[fw_hash(f->id)];
  145. for (pfp = rtnl_dereference(*fp); pfp;
  146. fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
  147. if (pfp == f) {
  148. RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
  149. tcf_unbind_filter(tp, &f->res);
  150. tcf_exts_get_net(&f->exts);
  151. tcf_queue_work(&f->rwork, fw_delete_filter_work);
  152. ret = 0;
  153. break;
  154. }
  155. }
  156. *last = true;
  157. for (h = 0; h < HTSIZE; h++) {
  158. if (rcu_access_pointer(head->ht[h])) {
  159. *last = false;
  160. break;
  161. }
  162. }
  163. out:
  164. return ret;
  165. }
  166. static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
  167. [TCA_FW_CLASSID] = { .type = NLA_U32 },
  168. [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  169. [TCA_FW_MASK] = { .type = NLA_U32 },
  170. };
  171. static int fw_set_parms(struct net *net, struct tcf_proto *tp,
  172. struct fw_filter *f, struct nlattr **tb,
  173. struct nlattr **tca, unsigned long base, u32 flags,
  174. struct netlink_ext_ack *extack)
  175. {
  176. struct fw_head *head = rtnl_dereference(tp->root);
  177. u32 mask;
  178. int err;
  179. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, flags,
  180. extack);
  181. if (err < 0)
  182. return err;
  183. if (tb[TCA_FW_INDEV]) {
  184. int ret;
  185. ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
  186. if (ret < 0)
  187. return ret;
  188. f->ifindex = ret;
  189. }
  190. err = -EINVAL;
  191. if (tb[TCA_FW_MASK]) {
  192. mask = nla_get_u32(tb[TCA_FW_MASK]);
  193. if (mask != head->mask)
  194. return err;
  195. } else if (head->mask != 0xFFFFFFFF)
  196. return err;
  197. if (tb[TCA_FW_CLASSID]) {
  198. f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
  199. tcf_bind_filter(tp, &f->res, base);
  200. }
  201. return 0;
  202. }
  203. static int fw_change(struct net *net, struct sk_buff *in_skb,
  204. struct tcf_proto *tp, unsigned long base,
  205. u32 handle, struct nlattr **tca, void **arg,
  206. u32 flags, struct netlink_ext_ack *extack)
  207. {
  208. struct fw_head *head = rtnl_dereference(tp->root);
  209. struct fw_filter *f = *arg;
  210. struct nlattr *opt = tca[TCA_OPTIONS];
  211. struct nlattr *tb[TCA_FW_MAX + 1];
  212. int err;
  213. if (!opt)
  214. return handle ? -EINVAL : 0; /* Succeed if it is old method. */
  215. err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
  216. NULL);
  217. if (err < 0)
  218. return err;
  219. if (f) {
  220. struct fw_filter *pfp, *fnew;
  221. struct fw_filter __rcu **fp;
  222. if (f->id != handle && handle)
  223. return -EINVAL;
  224. fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  225. if (!fnew)
  226. return -ENOBUFS;
  227. fnew->id = f->id;
  228. fnew->ifindex = f->ifindex;
  229. fnew->tp = f->tp;
  230. err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
  231. TCA_FW_POLICE);
  232. if (err < 0) {
  233. kfree(fnew);
  234. return err;
  235. }
  236. err = fw_set_parms(net, tp, fnew, tb, tca, base, flags, extack);
  237. if (err < 0) {
  238. tcf_exts_destroy(&fnew->exts);
  239. kfree(fnew);
  240. return err;
  241. }
  242. fp = &head->ht[fw_hash(fnew->id)];
  243. for (pfp = rtnl_dereference(*fp); pfp;
  244. fp = &pfp->next, pfp = rtnl_dereference(*fp))
  245. if (pfp == f)
  246. break;
  247. RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
  248. rcu_assign_pointer(*fp, fnew);
  249. tcf_unbind_filter(tp, &f->res);
  250. tcf_exts_get_net(&f->exts);
  251. tcf_queue_work(&f->rwork, fw_delete_filter_work);
  252. *arg = fnew;
  253. return err;
  254. }
  255. if (!handle)
  256. return -EINVAL;
  257. if (!head) {
  258. u32 mask = 0xFFFFFFFF;
  259. if (tb[TCA_FW_MASK])
  260. mask = nla_get_u32(tb[TCA_FW_MASK]);
  261. head = kzalloc(sizeof(*head), GFP_KERNEL);
  262. if (!head)
  263. return -ENOBUFS;
  264. head->mask = mask;
  265. rcu_assign_pointer(tp->root, head);
  266. }
  267. f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  268. if (f == NULL)
  269. return -ENOBUFS;
  270. err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
  271. if (err < 0)
  272. goto errout;
  273. f->id = handle;
  274. f->tp = tp;
  275. err = fw_set_parms(net, tp, f, tb, tca, base, flags, extack);
  276. if (err < 0)
  277. goto errout;
  278. RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
  279. rcu_assign_pointer(head->ht[fw_hash(handle)], f);
  280. *arg = f;
  281. return 0;
  282. errout:
  283. tcf_exts_destroy(&f->exts);
  284. kfree(f);
  285. return err;
  286. }
  287. static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  288. bool rtnl_held)
  289. {
  290. struct fw_head *head = rtnl_dereference(tp->root);
  291. int h;
  292. if (head == NULL)
  293. arg->stop = 1;
  294. if (arg->stop)
  295. return;
  296. for (h = 0; h < HTSIZE; h++) {
  297. struct fw_filter *f;
  298. for (f = rtnl_dereference(head->ht[h]); f;
  299. f = rtnl_dereference(f->next)) {
  300. if (!tc_cls_stats_dump(tp, arg, f))
  301. return;
  302. }
  303. }
  304. }
  305. static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
  306. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  307. {
  308. struct fw_head *head = rtnl_dereference(tp->root);
  309. struct fw_filter *f = fh;
  310. struct nlattr *nest;
  311. if (f == NULL)
  312. return skb->len;
  313. t->tcm_handle = f->id;
  314. if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
  315. return skb->len;
  316. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  317. if (nest == NULL)
  318. goto nla_put_failure;
  319. if (f->res.classid &&
  320. nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
  321. goto nla_put_failure;
  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. if (head->mask != 0xFFFFFFFF &&
  329. nla_put_u32(skb, TCA_FW_MASK, head->mask))
  330. goto nla_put_failure;
  331. if (tcf_exts_dump(skb, &f->exts) < 0)
  332. goto nla_put_failure;
  333. nla_nest_end(skb, nest);
  334. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  335. goto nla_put_failure;
  336. return skb->len;
  337. nla_put_failure:
  338. nla_nest_cancel(skb, nest);
  339. return -1;
  340. }
  341. static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  342. unsigned long base)
  343. {
  344. struct fw_filter *f = fh;
  345. tc_cls_bind_class(classid, cl, q, &f->res, base);
  346. }
  347. static struct tcf_proto_ops cls_fw_ops __read_mostly = {
  348. .kind = "fw",
  349. .classify = fw_classify,
  350. .init = fw_init,
  351. .destroy = fw_destroy,
  352. .get = fw_get,
  353. .change = fw_change,
  354. .delete = fw_delete,
  355. .walk = fw_walk,
  356. .dump = fw_dump,
  357. .bind_class = fw_bind_class,
  358. .owner = THIS_MODULE,
  359. };
  360. static int __init init_fw(void)
  361. {
  362. return register_tcf_proto_ops(&cls_fw_ops);
  363. }
  364. static void __exit exit_fw(void)
  365. {
  366. unregister_tcf_proto_ops(&cls_fw_ops);
  367. }
  368. module_init(init_fw)
  369. module_exit(exit_fw)
  370. MODULE_LICENSE("GPL");