cls_api.c 17 KB

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