cls_api.c 18 KB

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