cls_fw.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Changes:
  8. * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
  9. * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
  10. * Alex <alex@pilotsoft.com> : 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, bool ovr,
  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, ovr,
  180. true, extack);
  181. if (err < 0)
  182. return err;
  183. if (tb[TCA_FW_CLASSID]) {
  184. f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
  185. tcf_bind_filter(tp, &f->res, base);
  186. }
  187. if (tb[TCA_FW_INDEV]) {
  188. int ret;
  189. ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack);
  190. if (ret < 0)
  191. return ret;
  192. f->ifindex = ret;
  193. }
  194. err = -EINVAL;
  195. if (tb[TCA_FW_MASK]) {
  196. mask = nla_get_u32(tb[TCA_FW_MASK]);
  197. if (mask != head->mask)
  198. return err;
  199. } else if (head->mask != 0xFFFFFFFF)
  200. return err;
  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. bool ovr, bool rtnl_held,
  207. struct netlink_ext_ack *extack)
  208. {
  209. struct fw_head *head = rtnl_dereference(tp->root);
  210. struct fw_filter *f = *arg;
  211. struct nlattr *opt = tca[TCA_OPTIONS];
  212. struct nlattr *tb[TCA_FW_MAX + 1];
  213. int err;
  214. if (!opt)
  215. return handle ? -EINVAL : 0; /* Succeed if it is old method. */
  216. err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy,
  217. NULL);
  218. if (err < 0)
  219. return err;
  220. if (f) {
  221. struct fw_filter *pfp, *fnew;
  222. struct fw_filter __rcu **fp;
  223. if (f->id != handle && handle)
  224. return -EINVAL;
  225. fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  226. if (!fnew)
  227. return -ENOBUFS;
  228. fnew->id = f->id;
  229. fnew->res = f->res;
  230. fnew->ifindex = f->ifindex;
  231. fnew->tp = f->tp;
  232. err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT,
  233. TCA_FW_POLICE);
  234. if (err < 0) {
  235. kfree(fnew);
  236. return err;
  237. }
  238. err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack);
  239. if (err < 0) {
  240. tcf_exts_destroy(&fnew->exts);
  241. kfree(fnew);
  242. return err;
  243. }
  244. fp = &head->ht[fw_hash(fnew->id)];
  245. for (pfp = rtnl_dereference(*fp); pfp;
  246. fp = &pfp->next, pfp = rtnl_dereference(*fp))
  247. if (pfp == f)
  248. break;
  249. RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
  250. rcu_assign_pointer(*fp, fnew);
  251. tcf_unbind_filter(tp, &f->res);
  252. tcf_exts_get_net(&f->exts);
  253. tcf_queue_work(&f->rwork, fw_delete_filter_work);
  254. *arg = fnew;
  255. return err;
  256. }
  257. if (!handle)
  258. return -EINVAL;
  259. if (!head) {
  260. u32 mask = 0xFFFFFFFF;
  261. if (tb[TCA_FW_MASK])
  262. mask = nla_get_u32(tb[TCA_FW_MASK]);
  263. head = kzalloc(sizeof(*head), GFP_KERNEL);
  264. if (!head)
  265. return -ENOBUFS;
  266. head->mask = mask;
  267. rcu_assign_pointer(tp->root, head);
  268. }
  269. f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
  270. if (f == NULL)
  271. return -ENOBUFS;
  272. err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE);
  273. if (err < 0)
  274. goto errout;
  275. f->id = handle;
  276. f->tp = tp;
  277. err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack);
  278. if (err < 0)
  279. goto errout;
  280. RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
  281. rcu_assign_pointer(head->ht[fw_hash(handle)], f);
  282. *arg = f;
  283. return 0;
  284. errout:
  285. tcf_exts_destroy(&f->exts);
  286. kfree(f);
  287. return err;
  288. }
  289. static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  290. bool rtnl_held)
  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, 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, void *fh,
  315. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  316. {
  317. struct fw_head *head = rtnl_dereference(tp->root);
  318. struct fw_filter *f = 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_has_actions(&f->exts))
  324. return skb->len;
  325. nest = nla_nest_start_noflag(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. if (f->ifindex) {
  332. struct net_device *dev;
  333. dev = __dev_get_by_index(net, f->ifindex);
  334. if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
  335. goto nla_put_failure;
  336. }
  337. if (head->mask != 0xFFFFFFFF &&
  338. nla_put_u32(skb, TCA_FW_MASK, head->mask))
  339. goto nla_put_failure;
  340. if (tcf_exts_dump(skb, &f->exts) < 0)
  341. goto nla_put_failure;
  342. nla_nest_end(skb, nest);
  343. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  344. goto nla_put_failure;
  345. return skb->len;
  346. nla_put_failure:
  347. nla_nest_cancel(skb, nest);
  348. return -1;
  349. }
  350. static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  351. unsigned long base)
  352. {
  353. struct fw_filter *f = fh;
  354. if (f && f->res.classid == classid) {
  355. if (cl)
  356. __tcf_bind_filter(q, &f->res, base);
  357. else
  358. __tcf_unbind_filter(q, &f->res);
  359. }
  360. }
  361. static struct tcf_proto_ops cls_fw_ops __read_mostly = {
  362. .kind = "fw",
  363. .classify = fw_classify,
  364. .init = fw_init,
  365. .destroy = fw_destroy,
  366. .get = fw_get,
  367. .change = fw_change,
  368. .delete = fw_delete,
  369. .walk = fw_walk,
  370. .dump = fw_dump,
  371. .bind_class = fw_bind_class,
  372. .owner = THIS_MODULE,
  373. };
  374. static int __init init_fw(void)
  375. {
  376. return register_tcf_proto_ops(&cls_fw_ops);
  377. }
  378. static void __exit exit_fw(void)
  379. {
  380. unregister_tcf_proto_ops(&cls_fw_ops);
  381. }
  382. module_init(init_fw)
  383. module_exit(exit_fw)
  384. MODULE_LICENSE("GPL");