cls_api.c 17 KB

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