cls_api.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * net/sched/cls_api.c Packet classifier API.
  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. *
  13. * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/init.h>
  24. #include <linux/kmod.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <net/net_namespace.h>
  28. #include <net/sock.h>
  29. #include <net/netlink.h>
  30. #include <net/pkt_sched.h>
  31. #include <net/pkt_cls.h>
  32. /* The list of all installed classifier types */
  33. static LIST_HEAD(tcf_proto_base);
  34. /* Protects list of registered TC modules. It is pure SMP lock. */
  35. static DEFINE_RWLOCK(cls_mod_lock);
  36. /* Find classifier type by string name */
  37. static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
  38. {
  39. const struct tcf_proto_ops *t, *res = NULL;
  40. if (kind) {
  41. read_lock(&cls_mod_lock);
  42. list_for_each_entry(t, &tcf_proto_base, head) {
  43. if (strcmp(kind, t->kind) == 0) {
  44. if (try_module_get(t->owner))
  45. res = t;
  46. break;
  47. }
  48. }
  49. read_unlock(&cls_mod_lock);
  50. }
  51. return res;
  52. }
  53. /* Register(unregister) new classifier type */
  54. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  55. {
  56. struct tcf_proto_ops *t;
  57. int rc = -EEXIST;
  58. write_lock(&cls_mod_lock);
  59. list_for_each_entry(t, &tcf_proto_base, head)
  60. if (!strcmp(ops->kind, t->kind))
  61. goto out;
  62. list_add_tail(&ops->head, &tcf_proto_base);
  63. rc = 0;
  64. out:
  65. write_unlock(&cls_mod_lock);
  66. return rc;
  67. }
  68. EXPORT_SYMBOL(register_tcf_proto_ops);
  69. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  70. {
  71. struct tcf_proto_ops *t;
  72. int rc = -ENOENT;
  73. /* Wait for outstanding call_rcu()s, if any, from a
  74. * tcf_proto_ops's destroy() handler.
  75. */
  76. rcu_barrier();
  77. write_lock(&cls_mod_lock);
  78. list_for_each_entry(t, &tcf_proto_base, head) {
  79. if (t == ops) {
  80. list_del(&t->head);
  81. rc = 0;
  82. break;
  83. }
  84. }
  85. write_unlock(&cls_mod_lock);
  86. return rc;
  87. }
  88. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  89. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  90. struct nlmsghdr *n, struct tcf_proto *tp,
  91. unsigned long fh, int event, bool unicast);
  92. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  93. struct nlmsghdr *n,
  94. struct tcf_chain *chain, int event)
  95. {
  96. struct tcf_proto *tp;
  97. for (tp = rtnl_dereference(chain->filter_chain);
  98. tp; tp = rtnl_dereference(tp->next))
  99. tfilter_notify(net, oskb, n, tp, 0, event, false);
  100. }
  101. /* Select new prio value from the range, managed by kernel. */
  102. static inline u32 tcf_auto_prio(struct tcf_proto *tp)
  103. {
  104. u32 first = TC_H_MAKE(0xC0000000U, 0U);
  105. if (tp)
  106. first = tp->prio - 1;
  107. return TC_H_MAJ(first);
  108. }
  109. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  110. u32 prio, u32 parent, struct Qdisc *q,
  111. struct tcf_block *block)
  112. {
  113. struct tcf_proto *tp;
  114. int err;
  115. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  116. if (!tp)
  117. return ERR_PTR(-ENOBUFS);
  118. err = -ENOENT;
  119. tp->ops = tcf_proto_lookup_ops(kind);
  120. if (!tp->ops) {
  121. #ifdef CONFIG_MODULES
  122. rtnl_unlock();
  123. request_module("cls_%s", kind);
  124. rtnl_lock();
  125. tp->ops = tcf_proto_lookup_ops(kind);
  126. /* We dropped the RTNL semaphore in order to perform
  127. * the module load. So, even if we succeeded in loading
  128. * the module we have to replay the request. We indicate
  129. * this using -EAGAIN.
  130. */
  131. if (tp->ops) {
  132. module_put(tp->ops->owner);
  133. err = -EAGAIN;
  134. } else {
  135. err = -ENOENT;
  136. }
  137. goto errout;
  138. #endif
  139. }
  140. tp->classify = tp->ops->classify;
  141. tp->protocol = protocol;
  142. tp->prio = prio;
  143. tp->classid = parent;
  144. tp->q = q;
  145. tp->block = block;
  146. err = tp->ops->init(tp);
  147. if (err) {
  148. module_put(tp->ops->owner);
  149. goto errout;
  150. }
  151. return tp;
  152. errout:
  153. kfree(tp);
  154. return ERR_PTR(err);
  155. }
  156. static void tcf_proto_destroy(struct tcf_proto *tp)
  157. {
  158. tp->ops->destroy(tp);
  159. module_put(tp->ops->owner);
  160. kfree_rcu(tp, rcu);
  161. }
  162. static struct tcf_chain *tcf_chain_create(void)
  163. {
  164. return kzalloc(sizeof(struct tcf_chain), GFP_KERNEL);
  165. }
  166. static void tcf_chain_destroy(struct tcf_chain *chain)
  167. {
  168. struct tcf_proto *tp;
  169. while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
  170. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  171. tcf_proto_destroy(tp);
  172. }
  173. kfree(chain);
  174. }
  175. static void
  176. tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
  177. struct tcf_proto __rcu **p_filter_chain)
  178. {
  179. chain->p_filter_chain = p_filter_chain;
  180. }
  181. int tcf_block_get(struct tcf_block **p_block,
  182. struct tcf_proto __rcu **p_filter_chain)
  183. {
  184. struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  185. int err;
  186. if (!block)
  187. return -ENOMEM;
  188. block->chain = tcf_chain_create();
  189. if (!block->chain) {
  190. err = -ENOMEM;
  191. goto err_chain_create;
  192. }
  193. tcf_chain_filter_chain_ptr_set(block->chain, p_filter_chain);
  194. *p_block = block;
  195. return 0;
  196. err_chain_create:
  197. kfree(block);
  198. return err;
  199. }
  200. EXPORT_SYMBOL(tcf_block_get);
  201. void tcf_block_put(struct tcf_block *block)
  202. {
  203. if (!block)
  204. return;
  205. tcf_chain_destroy(block->chain);
  206. kfree(block);
  207. }
  208. EXPORT_SYMBOL(tcf_block_put);
  209. /* Main classifier routine: scans classifier chain attached
  210. * to this qdisc, (optionally) tests for protocol and asks
  211. * specific classifiers.
  212. */
  213. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  214. struct tcf_result *res, bool compat_mode)
  215. {
  216. __be16 protocol = tc_skb_protocol(skb);
  217. #ifdef CONFIG_NET_CLS_ACT
  218. const int max_reclassify_loop = 4;
  219. const struct tcf_proto *old_tp = tp;
  220. int limit = 0;
  221. reclassify:
  222. #endif
  223. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  224. int err;
  225. if (tp->protocol != protocol &&
  226. tp->protocol != htons(ETH_P_ALL))
  227. continue;
  228. err = tp->classify(skb, tp, res);
  229. #ifdef CONFIG_NET_CLS_ACT
  230. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode))
  231. goto reset;
  232. #endif
  233. if (err >= 0)
  234. return err;
  235. }
  236. return TC_ACT_UNSPEC; /* signal: continue lookup */
  237. #ifdef CONFIG_NET_CLS_ACT
  238. reset:
  239. if (unlikely(limit++ >= max_reclassify_loop)) {
  240. net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
  241. tp->q->ops->id, tp->prio & 0xffff,
  242. ntohs(tp->protocol));
  243. return TC_ACT_SHOT;
  244. }
  245. tp = old_tp;
  246. protocol = tc_skb_protocol(skb);
  247. goto reclassify;
  248. #endif
  249. }
  250. EXPORT_SYMBOL(tcf_classify);
  251. struct tcf_chain_info {
  252. struct tcf_proto __rcu **pprev;
  253. struct tcf_proto __rcu *next;
  254. };
  255. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  256. {
  257. return rtnl_dereference(*chain_info->pprev);
  258. }
  259. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  260. struct tcf_chain_info *chain_info,
  261. struct tcf_proto *tp)
  262. {
  263. if (chain->p_filter_chain &&
  264. *chain_info->pprev == chain->filter_chain)
  265. *chain->p_filter_chain = tp;
  266. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  267. rcu_assign_pointer(*chain_info->pprev, tp);
  268. }
  269. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  270. struct tcf_chain_info *chain_info,
  271. struct tcf_proto *tp)
  272. {
  273. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  274. if (chain->p_filter_chain && tp == chain->filter_chain)
  275. *chain->p_filter_chain = next;
  276. RCU_INIT_POINTER(*chain_info->pprev, next);
  277. }
  278. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  279. struct tcf_chain_info *chain_info,
  280. u32 protocol, u32 prio,
  281. bool prio_allocate)
  282. {
  283. struct tcf_proto **pprev;
  284. struct tcf_proto *tp;
  285. /* Check the chain for existence of proto-tcf with this priority */
  286. for (pprev = &chain->filter_chain;
  287. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  288. if (tp->prio >= prio) {
  289. if (tp->prio == prio) {
  290. if (prio_allocate ||
  291. (tp->protocol != protocol && protocol))
  292. return ERR_PTR(-EINVAL);
  293. } else {
  294. tp = NULL;
  295. }
  296. break;
  297. }
  298. }
  299. chain_info->pprev = pprev;
  300. chain_info->next = tp ? tp->next : NULL;
  301. return tp;
  302. }
  303. /* Add/change/delete/get a filter node */
  304. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  305. struct netlink_ext_ack *extack)
  306. {
  307. struct net *net = sock_net(skb->sk);
  308. struct nlattr *tca[TCA_MAX + 1];
  309. struct tcmsg *t;
  310. u32 protocol;
  311. u32 prio;
  312. bool prio_allocate;
  313. u32 parent;
  314. struct net_device *dev;
  315. struct Qdisc *q;
  316. struct tcf_chain_info chain_info;
  317. struct tcf_chain *chain;
  318. struct tcf_block *block;
  319. struct tcf_proto *tp;
  320. const struct Qdisc_class_ops *cops;
  321. unsigned long cl;
  322. unsigned long fh;
  323. int err;
  324. int tp_created;
  325. if ((n->nlmsg_type != RTM_GETTFILTER) &&
  326. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  327. return -EPERM;
  328. replay:
  329. tp_created = 0;
  330. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  331. if (err < 0)
  332. return err;
  333. t = nlmsg_data(n);
  334. protocol = TC_H_MIN(t->tcm_info);
  335. prio = TC_H_MAJ(t->tcm_info);
  336. prio_allocate = false;
  337. parent = t->tcm_parent;
  338. cl = 0;
  339. if (prio == 0) {
  340. switch (n->nlmsg_type) {
  341. case RTM_DELTFILTER:
  342. if (protocol || t->tcm_handle || tca[TCA_KIND])
  343. return -ENOENT;
  344. break;
  345. case RTM_NEWTFILTER:
  346. /* If no priority is provided by the user,
  347. * we allocate one.
  348. */
  349. if (n->nlmsg_flags & NLM_F_CREATE) {
  350. prio = TC_H_MAKE(0x80000000U, 0U);
  351. prio_allocate = true;
  352. break;
  353. }
  354. /* fall-through */
  355. default:
  356. return -ENOENT;
  357. }
  358. }
  359. /* Find head of filter chain. */
  360. /* Find link */
  361. dev = __dev_get_by_index(net, t->tcm_ifindex);
  362. if (dev == NULL)
  363. return -ENODEV;
  364. /* Find qdisc */
  365. if (!parent) {
  366. q = dev->qdisc;
  367. parent = q->handle;
  368. } else {
  369. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  370. if (q == NULL)
  371. return -EINVAL;
  372. }
  373. /* Is it classful? */
  374. cops = q->ops->cl_ops;
  375. if (!cops)
  376. return -EINVAL;
  377. if (!cops->tcf_block)
  378. return -EOPNOTSUPP;
  379. /* Do we search for filter, attached to class? */
  380. if (TC_H_MIN(parent)) {
  381. cl = cops->get(q, parent);
  382. if (cl == 0)
  383. return -ENOENT;
  384. }
  385. /* And the last stroke */
  386. block = cops->tcf_block(q, cl);
  387. if (!block) {
  388. err = -EINVAL;
  389. goto errout;
  390. }
  391. chain = block->chain;
  392. if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
  393. tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
  394. tcf_chain_destroy(chain);
  395. err = 0;
  396. goto errout;
  397. }
  398. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  399. prio, prio_allocate);
  400. if (IS_ERR(tp)) {
  401. err = PTR_ERR(tp);
  402. goto errout;
  403. }
  404. if (tp == NULL) {
  405. /* Proto-tcf does not exist, create new one */
  406. if (tca[TCA_KIND] == NULL || !protocol) {
  407. err = -EINVAL;
  408. goto errout;
  409. }
  410. if (n->nlmsg_type != RTM_NEWTFILTER ||
  411. !(n->nlmsg_flags & NLM_F_CREATE)) {
  412. err = -ENOENT;
  413. goto errout;
  414. }
  415. if (prio_allocate)
  416. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  417. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  418. protocol, prio, parent, q, block);
  419. if (IS_ERR(tp)) {
  420. err = PTR_ERR(tp);
  421. goto errout;
  422. }
  423. tp_created = 1;
  424. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  425. err = -EINVAL;
  426. goto errout;
  427. }
  428. fh = tp->ops->get(tp, t->tcm_handle);
  429. if (fh == 0) {
  430. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  431. tcf_chain_tp_remove(chain, &chain_info, tp);
  432. tfilter_notify(net, skb, n, tp, fh,
  433. RTM_DELTFILTER, false);
  434. tcf_proto_destroy(tp);
  435. err = 0;
  436. goto errout;
  437. }
  438. if (n->nlmsg_type != RTM_NEWTFILTER ||
  439. !(n->nlmsg_flags & NLM_F_CREATE)) {
  440. err = -ENOENT;
  441. goto errout;
  442. }
  443. } else {
  444. bool last;
  445. switch (n->nlmsg_type) {
  446. case RTM_NEWTFILTER:
  447. if (n->nlmsg_flags & NLM_F_EXCL) {
  448. if (tp_created)
  449. tcf_proto_destroy(tp);
  450. err = -EEXIST;
  451. goto errout;
  452. }
  453. break;
  454. case RTM_DELTFILTER:
  455. err = tp->ops->delete(tp, fh, &last);
  456. if (err)
  457. goto errout;
  458. tfilter_notify(net, skb, n, tp, t->tcm_handle,
  459. RTM_DELTFILTER, false);
  460. if (last) {
  461. tcf_chain_tp_remove(chain, &chain_info, tp);
  462. tcf_proto_destroy(tp);
  463. }
  464. goto errout;
  465. case RTM_GETTFILTER:
  466. err = tfilter_notify(net, skb, n, tp, fh,
  467. RTM_NEWTFILTER, true);
  468. goto errout;
  469. default:
  470. err = -EINVAL;
  471. goto errout;
  472. }
  473. }
  474. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  475. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
  476. if (err == 0) {
  477. if (tp_created)
  478. tcf_chain_tp_insert(chain, &chain_info, tp);
  479. tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
  480. } else {
  481. if (tp_created)
  482. tcf_proto_destroy(tp);
  483. }
  484. errout:
  485. if (cl)
  486. cops->put(q, cl);
  487. if (err == -EAGAIN)
  488. /* Replay the request. */
  489. goto replay;
  490. return err;
  491. }
  492. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  493. struct tcf_proto *tp, unsigned long fh, u32 portid,
  494. u32 seq, u16 flags, int event)
  495. {
  496. struct tcmsg *tcm;
  497. struct nlmsghdr *nlh;
  498. unsigned char *b = skb_tail_pointer(skb);
  499. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  500. if (!nlh)
  501. goto out_nlmsg_trim;
  502. tcm = nlmsg_data(nlh);
  503. tcm->tcm_family = AF_UNSPEC;
  504. tcm->tcm__pad1 = 0;
  505. tcm->tcm__pad2 = 0;
  506. tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
  507. tcm->tcm_parent = tp->classid;
  508. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  509. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  510. goto nla_put_failure;
  511. tcm->tcm_handle = fh;
  512. if (RTM_DELTFILTER != event) {
  513. tcm->tcm_handle = 0;
  514. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  515. goto nla_put_failure;
  516. }
  517. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  518. return skb->len;
  519. out_nlmsg_trim:
  520. nla_put_failure:
  521. nlmsg_trim(skb, b);
  522. return -1;
  523. }
  524. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  525. struct nlmsghdr *n, struct tcf_proto *tp,
  526. unsigned long fh, int event, bool unicast)
  527. {
  528. struct sk_buff *skb;
  529. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  530. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  531. if (!skb)
  532. return -ENOBUFS;
  533. if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
  534. n->nlmsg_flags, event) <= 0) {
  535. kfree_skb(skb);
  536. return -EINVAL;
  537. }
  538. if (unicast)
  539. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  540. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  541. n->nlmsg_flags & NLM_F_ECHO);
  542. }
  543. struct tcf_dump_args {
  544. struct tcf_walker w;
  545. struct sk_buff *skb;
  546. struct netlink_callback *cb;
  547. };
  548. static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
  549. struct tcf_walker *arg)
  550. {
  551. struct tcf_dump_args *a = (void *)arg;
  552. struct net *net = sock_net(a->skb->sk);
  553. return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
  554. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  555. RTM_NEWTFILTER);
  556. }
  557. /* called with RTNL */
  558. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  559. {
  560. struct net *net = sock_net(skb->sk);
  561. int t;
  562. int s_t;
  563. struct net_device *dev;
  564. struct Qdisc *q;
  565. struct tcf_block *block;
  566. struct tcf_proto *tp;
  567. struct tcf_chain *chain;
  568. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  569. unsigned long cl = 0;
  570. const struct Qdisc_class_ops *cops;
  571. struct tcf_dump_args arg;
  572. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  573. return skb->len;
  574. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  575. if (!dev)
  576. return skb->len;
  577. if (!tcm->tcm_parent)
  578. q = dev->qdisc;
  579. else
  580. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  581. if (!q)
  582. goto out;
  583. cops = q->ops->cl_ops;
  584. if (!cops)
  585. goto errout;
  586. if (!cops->tcf_block)
  587. goto errout;
  588. if (TC_H_MIN(tcm->tcm_parent)) {
  589. cl = cops->get(q, tcm->tcm_parent);
  590. if (cl == 0)
  591. goto errout;
  592. }
  593. block = cops->tcf_block(q, cl);
  594. if (!block)
  595. goto errout;
  596. chain = block->chain;
  597. s_t = cb->args[0];
  598. for (tp = rtnl_dereference(chain->filter_chain), t = 0;
  599. tp; tp = rtnl_dereference(tp->next), t++) {
  600. if (t < s_t)
  601. continue;
  602. if (TC_H_MAJ(tcm->tcm_info) &&
  603. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  604. continue;
  605. if (TC_H_MIN(tcm->tcm_info) &&
  606. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  607. continue;
  608. if (t > s_t)
  609. memset(&cb->args[1], 0,
  610. sizeof(cb->args)-sizeof(cb->args[0]));
  611. if (cb->args[1] == 0) {
  612. if (tcf_fill_node(net, skb, tp, 0,
  613. NETLINK_CB(cb->skb).portid,
  614. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  615. RTM_NEWTFILTER) <= 0)
  616. break;
  617. cb->args[1] = 1;
  618. }
  619. if (tp->ops->walk == NULL)
  620. continue;
  621. arg.w.fn = tcf_node_dump;
  622. arg.skb = skb;
  623. arg.cb = cb;
  624. arg.w.stop = 0;
  625. arg.w.skip = cb->args[1] - 1;
  626. arg.w.count = 0;
  627. tp->ops->walk(tp, &arg.w);
  628. cb->args[1] = arg.w.count + 1;
  629. if (arg.w.stop)
  630. break;
  631. }
  632. cb->args[0] = t;
  633. errout:
  634. if (cl)
  635. cops->put(q, cl);
  636. out:
  637. return skb->len;
  638. }
  639. void tcf_exts_destroy(struct tcf_exts *exts)
  640. {
  641. #ifdef CONFIG_NET_CLS_ACT
  642. LIST_HEAD(actions);
  643. tcf_exts_to_list(exts, &actions);
  644. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  645. kfree(exts->actions);
  646. exts->nr_actions = 0;
  647. #endif
  648. }
  649. EXPORT_SYMBOL(tcf_exts_destroy);
  650. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  651. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
  652. {
  653. #ifdef CONFIG_NET_CLS_ACT
  654. {
  655. struct tc_action *act;
  656. if (exts->police && tb[exts->police]) {
  657. act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
  658. "police", ovr, TCA_ACT_BIND);
  659. if (IS_ERR(act))
  660. return PTR_ERR(act);
  661. act->type = exts->type = TCA_OLD_COMPAT;
  662. exts->actions[0] = act;
  663. exts->nr_actions = 1;
  664. } else if (exts->action && tb[exts->action]) {
  665. LIST_HEAD(actions);
  666. int err, i = 0;
  667. err = tcf_action_init(net, tb[exts->action], rate_tlv,
  668. NULL, ovr, TCA_ACT_BIND,
  669. &actions);
  670. if (err)
  671. return err;
  672. list_for_each_entry(act, &actions, list)
  673. exts->actions[i++] = act;
  674. exts->nr_actions = i;
  675. }
  676. }
  677. #else
  678. if ((exts->action && tb[exts->action]) ||
  679. (exts->police && tb[exts->police]))
  680. return -EOPNOTSUPP;
  681. #endif
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(tcf_exts_validate);
  685. void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  686. struct tcf_exts *src)
  687. {
  688. #ifdef CONFIG_NET_CLS_ACT
  689. struct tcf_exts old = *dst;
  690. tcf_tree_lock(tp);
  691. dst->nr_actions = src->nr_actions;
  692. dst->actions = src->actions;
  693. dst->type = src->type;
  694. tcf_tree_unlock(tp);
  695. tcf_exts_destroy(&old);
  696. #endif
  697. }
  698. EXPORT_SYMBOL(tcf_exts_change);
  699. #ifdef CONFIG_NET_CLS_ACT
  700. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  701. {
  702. if (exts->nr_actions == 0)
  703. return NULL;
  704. else
  705. return exts->actions[0];
  706. }
  707. #endif
  708. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  709. {
  710. #ifdef CONFIG_NET_CLS_ACT
  711. struct nlattr *nest;
  712. if (exts->action && exts->nr_actions) {
  713. /*
  714. * again for backward compatible mode - we want
  715. * to work with both old and new modes of entering
  716. * tc data even if iproute2 was newer - jhs
  717. */
  718. if (exts->type != TCA_OLD_COMPAT) {
  719. LIST_HEAD(actions);
  720. nest = nla_nest_start(skb, exts->action);
  721. if (nest == NULL)
  722. goto nla_put_failure;
  723. tcf_exts_to_list(exts, &actions);
  724. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  725. goto nla_put_failure;
  726. nla_nest_end(skb, nest);
  727. } else if (exts->police) {
  728. struct tc_action *act = tcf_exts_first_act(exts);
  729. nest = nla_nest_start(skb, exts->police);
  730. if (nest == NULL || !act)
  731. goto nla_put_failure;
  732. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  733. goto nla_put_failure;
  734. nla_nest_end(skb, nest);
  735. }
  736. }
  737. return 0;
  738. nla_put_failure:
  739. nla_nest_cancel(skb, nest);
  740. return -1;
  741. #else
  742. return 0;
  743. #endif
  744. }
  745. EXPORT_SYMBOL(tcf_exts_dump);
  746. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  747. {
  748. #ifdef CONFIG_NET_CLS_ACT
  749. struct tc_action *a = tcf_exts_first_act(exts);
  750. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  751. return -1;
  752. #endif
  753. return 0;
  754. }
  755. EXPORT_SYMBOL(tcf_exts_dump_stats);
  756. int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
  757. struct net_device **hw_dev)
  758. {
  759. #ifdef CONFIG_NET_CLS_ACT
  760. const struct tc_action *a;
  761. LIST_HEAD(actions);
  762. if (tc_no_actions(exts))
  763. return -EINVAL;
  764. tcf_exts_to_list(exts, &actions);
  765. list_for_each_entry(a, &actions, list) {
  766. if (a->ops->get_dev) {
  767. a->ops->get_dev(a, dev_net(dev), hw_dev);
  768. break;
  769. }
  770. }
  771. if (*hw_dev)
  772. return 0;
  773. #endif
  774. return -EOPNOTSUPP;
  775. }
  776. EXPORT_SYMBOL(tcf_exts_get_dev);
  777. static int __init tc_filter_init(void)
  778. {
  779. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
  780. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
  781. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
  782. tc_dump_tfilter, NULL);
  783. return 0;
  784. }
  785. subsys_initcall(tc_filter_init);