cls_api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/skbuff.h>
  22. #include <linux/init.h>
  23. #include <linux/kmod.h>
  24. #include <linux/err.h>
  25. #include <linux/slab.h>
  26. #include <net/net_namespace.h>
  27. #include <net/sock.h>
  28. #include <net/netlink.h>
  29. #include <net/pkt_sched.h>
  30. #include <net/pkt_cls.h>
  31. /* The list of all installed classifier types */
  32. static LIST_HEAD(tcf_proto_base);
  33. /* Protects list of registered TC modules. It is pure SMP lock. */
  34. static DEFINE_RWLOCK(cls_mod_lock);
  35. /* Find classifier type by string name */
  36. static const struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
  37. {
  38. const struct tcf_proto_ops *t, *res = NULL;
  39. if (kind) {
  40. read_lock(&cls_mod_lock);
  41. list_for_each_entry(t, &tcf_proto_base, head) {
  42. if (nla_strcmp(kind, t->kind) == 0) {
  43. if (try_module_get(t->owner))
  44. res = t;
  45. break;
  46. }
  47. }
  48. read_unlock(&cls_mod_lock);
  49. }
  50. return res;
  51. }
  52. /* Register(unregister) new classifier type */
  53. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  54. {
  55. struct tcf_proto_ops *t;
  56. int rc = -EEXIST;
  57. write_lock(&cls_mod_lock);
  58. list_for_each_entry(t, &tcf_proto_base, head)
  59. if (!strcmp(ops->kind, t->kind))
  60. goto out;
  61. list_add_tail(&ops->head, &tcf_proto_base);
  62. rc = 0;
  63. out:
  64. write_unlock(&cls_mod_lock);
  65. return rc;
  66. }
  67. EXPORT_SYMBOL(register_tcf_proto_ops);
  68. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  69. {
  70. struct tcf_proto_ops *t;
  71. int rc = -ENOENT;
  72. /* Wait for outstanding call_rcu()s, if any, from a
  73. * tcf_proto_ops's destroy() handler.
  74. */
  75. rcu_barrier();
  76. write_lock(&cls_mod_lock);
  77. list_for_each_entry(t, &tcf_proto_base, head) {
  78. if (t == ops) {
  79. list_del(&t->head);
  80. rc = 0;
  81. break;
  82. }
  83. }
  84. write_unlock(&cls_mod_lock);
  85. return rc;
  86. }
  87. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  88. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  89. struct nlmsghdr *n, struct tcf_proto *tp,
  90. unsigned long fh, int event, bool unicast);
  91. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  92. struct nlmsghdr *n,
  93. struct tcf_proto __rcu **chain, int event)
  94. {
  95. struct tcf_proto __rcu **it_chain;
  96. struct tcf_proto *tp;
  97. for (it_chain = chain; (tp = rtnl_dereference(*it_chain)) != NULL;
  98. it_chain = &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 first;
  108. }
  109. /* Add/change/delete/get a filter node */
  110. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
  111. {
  112. struct net *net = sock_net(skb->sk);
  113. struct nlattr *tca[TCA_MAX + 1];
  114. struct tcmsg *t;
  115. u32 protocol;
  116. u32 prio;
  117. u32 nprio;
  118. u32 parent;
  119. struct net_device *dev;
  120. struct Qdisc *q;
  121. struct tcf_proto __rcu **back;
  122. struct tcf_proto __rcu **chain;
  123. struct tcf_proto *tp;
  124. const struct tcf_proto_ops *tp_ops;
  125. const struct Qdisc_class_ops *cops;
  126. unsigned long cl;
  127. unsigned long fh;
  128. int err;
  129. int tp_created = 0;
  130. if ((n->nlmsg_type != RTM_GETTFILTER) &&
  131. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  132. return -EPERM;
  133. replay:
  134. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
  135. if (err < 0)
  136. return err;
  137. t = nlmsg_data(n);
  138. protocol = TC_H_MIN(t->tcm_info);
  139. prio = TC_H_MAJ(t->tcm_info);
  140. nprio = prio;
  141. parent = t->tcm_parent;
  142. cl = 0;
  143. if (prio == 0) {
  144. switch (n->nlmsg_type) {
  145. case RTM_DELTFILTER:
  146. if (protocol || t->tcm_handle || tca[TCA_KIND])
  147. return -ENOENT;
  148. break;
  149. case RTM_NEWTFILTER:
  150. /* If no priority is provided by the user,
  151. * we allocate one.
  152. */
  153. if (n->nlmsg_flags & NLM_F_CREATE) {
  154. prio = TC_H_MAKE(0x80000000U, 0U);
  155. break;
  156. }
  157. /* fall-through */
  158. default:
  159. return -ENOENT;
  160. }
  161. }
  162. /* Find head of filter chain. */
  163. /* Find link */
  164. dev = __dev_get_by_index(net, t->tcm_ifindex);
  165. if (dev == NULL)
  166. return -ENODEV;
  167. /* Find qdisc */
  168. if (!parent) {
  169. q = dev->qdisc;
  170. parent = q->handle;
  171. } else {
  172. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  173. if (q == NULL)
  174. return -EINVAL;
  175. }
  176. /* Is it classful? */
  177. cops = q->ops->cl_ops;
  178. if (!cops)
  179. return -EINVAL;
  180. if (cops->tcf_chain == NULL)
  181. return -EOPNOTSUPP;
  182. /* Do we search for filter, attached to class? */
  183. if (TC_H_MIN(parent)) {
  184. cl = cops->get(q, parent);
  185. if (cl == 0)
  186. return -ENOENT;
  187. }
  188. /* And the last stroke */
  189. chain = cops->tcf_chain(q, cl);
  190. err = -EINVAL;
  191. if (chain == NULL)
  192. goto errout;
  193. if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
  194. tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
  195. tcf_destroy_chain(chain);
  196. err = 0;
  197. goto errout;
  198. }
  199. /* Check the chain for existence of proto-tcf with this priority */
  200. for (back = chain;
  201. (tp = rtnl_dereference(*back)) != NULL;
  202. back = &tp->next) {
  203. if (tp->prio >= prio) {
  204. if (tp->prio == prio) {
  205. if (!nprio ||
  206. (tp->protocol != protocol && protocol))
  207. goto errout;
  208. } else
  209. tp = NULL;
  210. break;
  211. }
  212. }
  213. if (tp == NULL) {
  214. /* Proto-tcf does not exist, create new one */
  215. if (tca[TCA_KIND] == NULL || !protocol)
  216. goto errout;
  217. err = -ENOENT;
  218. if (n->nlmsg_type != RTM_NEWTFILTER ||
  219. !(n->nlmsg_flags & NLM_F_CREATE))
  220. goto errout;
  221. /* Create new proto tcf */
  222. err = -ENOBUFS;
  223. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  224. if (tp == NULL)
  225. goto errout;
  226. err = -ENOENT;
  227. tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
  228. if (tp_ops == NULL) {
  229. #ifdef CONFIG_MODULES
  230. struct nlattr *kind = tca[TCA_KIND];
  231. char name[IFNAMSIZ];
  232. if (kind != NULL &&
  233. nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
  234. rtnl_unlock();
  235. request_module("cls_%s", name);
  236. rtnl_lock();
  237. tp_ops = tcf_proto_lookup_ops(kind);
  238. /* We dropped the RTNL semaphore in order to
  239. * perform the module load. So, even if we
  240. * succeeded in loading the module we have to
  241. * replay the request. We indicate this using
  242. * -EAGAIN.
  243. */
  244. if (tp_ops != NULL) {
  245. module_put(tp_ops->owner);
  246. err = -EAGAIN;
  247. }
  248. }
  249. #endif
  250. kfree(tp);
  251. goto errout;
  252. }
  253. tp->ops = tp_ops;
  254. tp->protocol = protocol;
  255. tp->prio = nprio ? :
  256. TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
  257. tp->q = q;
  258. tp->classify = tp_ops->classify;
  259. tp->classid = parent;
  260. err = tp_ops->init(tp);
  261. if (err != 0) {
  262. module_put(tp_ops->owner);
  263. kfree(tp);
  264. goto errout;
  265. }
  266. tp_created = 1;
  267. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
  268. goto errout;
  269. fh = tp->ops->get(tp, t->tcm_handle);
  270. if (fh == 0) {
  271. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  272. struct tcf_proto *next = rtnl_dereference(tp->next);
  273. RCU_INIT_POINTER(*back, next);
  274. tfilter_notify(net, skb, n, tp, fh,
  275. RTM_DELTFILTER, false);
  276. tcf_destroy(tp, true);
  277. err = 0;
  278. goto errout;
  279. }
  280. err = -ENOENT;
  281. if (n->nlmsg_type != RTM_NEWTFILTER ||
  282. !(n->nlmsg_flags & NLM_F_CREATE))
  283. goto errout;
  284. } else {
  285. switch (n->nlmsg_type) {
  286. case RTM_NEWTFILTER:
  287. err = -EEXIST;
  288. if (n->nlmsg_flags & NLM_F_EXCL) {
  289. if (tp_created)
  290. tcf_destroy(tp, true);
  291. goto errout;
  292. }
  293. break;
  294. case RTM_DELTFILTER:
  295. err = tp->ops->delete(tp, fh);
  296. if (err == 0) {
  297. struct tcf_proto *next = rtnl_dereference(tp->next);
  298. tfilter_notify(net, skb, n, tp,
  299. t->tcm_handle,
  300. RTM_DELTFILTER, false);
  301. if (tcf_destroy(tp, false))
  302. RCU_INIT_POINTER(*back, next);
  303. }
  304. goto errout;
  305. case RTM_GETTFILTER:
  306. err = tfilter_notify(net, skb, n, tp, fh,
  307. RTM_NEWTFILTER, true);
  308. goto errout;
  309. default:
  310. err = -EINVAL;
  311. goto errout;
  312. }
  313. }
  314. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  315. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
  316. if (err == 0) {
  317. if (tp_created) {
  318. RCU_INIT_POINTER(tp->next, rtnl_dereference(*back));
  319. rcu_assign_pointer(*back, tp);
  320. }
  321. tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
  322. } else {
  323. if (tp_created)
  324. tcf_destroy(tp, true);
  325. }
  326. errout:
  327. if (cl)
  328. cops->put(q, cl);
  329. if (err == -EAGAIN)
  330. /* Replay the request. */
  331. goto replay;
  332. return err;
  333. }
  334. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  335. struct tcf_proto *tp, unsigned long fh, u32 portid,
  336. u32 seq, u16 flags, int event)
  337. {
  338. struct tcmsg *tcm;
  339. struct nlmsghdr *nlh;
  340. unsigned char *b = skb_tail_pointer(skb);
  341. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  342. if (!nlh)
  343. goto out_nlmsg_trim;
  344. tcm = nlmsg_data(nlh);
  345. tcm->tcm_family = AF_UNSPEC;
  346. tcm->tcm__pad1 = 0;
  347. tcm->tcm__pad2 = 0;
  348. tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
  349. tcm->tcm_parent = tp->classid;
  350. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  351. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  352. goto nla_put_failure;
  353. tcm->tcm_handle = fh;
  354. if (RTM_DELTFILTER != event) {
  355. tcm->tcm_handle = 0;
  356. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  357. goto nla_put_failure;
  358. }
  359. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  360. return skb->len;
  361. out_nlmsg_trim:
  362. nla_put_failure:
  363. nlmsg_trim(skb, b);
  364. return -1;
  365. }
  366. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  367. struct nlmsghdr *n, struct tcf_proto *tp,
  368. unsigned long fh, int event, bool unicast)
  369. {
  370. struct sk_buff *skb;
  371. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  372. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  373. if (!skb)
  374. return -ENOBUFS;
  375. if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
  376. n->nlmsg_flags, event) <= 0) {
  377. kfree_skb(skb);
  378. return -EINVAL;
  379. }
  380. if (unicast)
  381. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  382. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  383. n->nlmsg_flags & NLM_F_ECHO);
  384. }
  385. struct tcf_dump_args {
  386. struct tcf_walker w;
  387. struct sk_buff *skb;
  388. struct netlink_callback *cb;
  389. };
  390. static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
  391. struct tcf_walker *arg)
  392. {
  393. struct tcf_dump_args *a = (void *)arg;
  394. struct net *net = sock_net(a->skb->sk);
  395. return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
  396. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  397. RTM_NEWTFILTER);
  398. }
  399. /* called with RTNL */
  400. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  401. {
  402. struct net *net = sock_net(skb->sk);
  403. int t;
  404. int s_t;
  405. struct net_device *dev;
  406. struct Qdisc *q;
  407. struct tcf_proto *tp, __rcu **chain;
  408. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  409. unsigned long cl = 0;
  410. const struct Qdisc_class_ops *cops;
  411. struct tcf_dump_args arg;
  412. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  413. return skb->len;
  414. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  415. if (!dev)
  416. return skb->len;
  417. if (!tcm->tcm_parent)
  418. q = dev->qdisc;
  419. else
  420. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  421. if (!q)
  422. goto out;
  423. cops = q->ops->cl_ops;
  424. if (!cops)
  425. goto errout;
  426. if (cops->tcf_chain == NULL)
  427. goto errout;
  428. if (TC_H_MIN(tcm->tcm_parent)) {
  429. cl = cops->get(q, tcm->tcm_parent);
  430. if (cl == 0)
  431. goto errout;
  432. }
  433. chain = cops->tcf_chain(q, cl);
  434. if (chain == NULL)
  435. goto errout;
  436. s_t = cb->args[0];
  437. for (tp = rtnl_dereference(*chain), t = 0;
  438. tp; tp = rtnl_dereference(tp->next), t++) {
  439. if (t < s_t)
  440. continue;
  441. if (TC_H_MAJ(tcm->tcm_info) &&
  442. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  443. continue;
  444. if (TC_H_MIN(tcm->tcm_info) &&
  445. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  446. continue;
  447. if (t > s_t)
  448. memset(&cb->args[1], 0,
  449. sizeof(cb->args)-sizeof(cb->args[0]));
  450. if (cb->args[1] == 0) {
  451. if (tcf_fill_node(net, skb, tp, 0,
  452. NETLINK_CB(cb->skb).portid,
  453. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  454. RTM_NEWTFILTER) <= 0)
  455. break;
  456. cb->args[1] = 1;
  457. }
  458. if (tp->ops->walk == NULL)
  459. continue;
  460. arg.w.fn = tcf_node_dump;
  461. arg.skb = skb;
  462. arg.cb = cb;
  463. arg.w.stop = 0;
  464. arg.w.skip = cb->args[1] - 1;
  465. arg.w.count = 0;
  466. tp->ops->walk(tp, &arg.w);
  467. cb->args[1] = arg.w.count + 1;
  468. if (arg.w.stop)
  469. break;
  470. }
  471. cb->args[0] = t;
  472. errout:
  473. if (cl)
  474. cops->put(q, cl);
  475. out:
  476. return skb->len;
  477. }
  478. void tcf_exts_destroy(struct tcf_exts *exts)
  479. {
  480. #ifdef CONFIG_NET_CLS_ACT
  481. LIST_HEAD(actions);
  482. tcf_exts_to_list(exts, &actions);
  483. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  484. kfree(exts->actions);
  485. exts->nr_actions = 0;
  486. #endif
  487. }
  488. EXPORT_SYMBOL(tcf_exts_destroy);
  489. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  490. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
  491. {
  492. #ifdef CONFIG_NET_CLS_ACT
  493. {
  494. struct tc_action *act;
  495. if (exts->police && tb[exts->police]) {
  496. act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
  497. "police", ovr, TCA_ACT_BIND);
  498. if (IS_ERR(act))
  499. return PTR_ERR(act);
  500. act->type = exts->type = TCA_OLD_COMPAT;
  501. exts->actions[0] = act;
  502. exts->nr_actions = 1;
  503. } else if (exts->action && tb[exts->action]) {
  504. LIST_HEAD(actions);
  505. int err, i = 0;
  506. err = tcf_action_init(net, tb[exts->action], rate_tlv,
  507. NULL, ovr, TCA_ACT_BIND,
  508. &actions);
  509. if (err)
  510. return err;
  511. list_for_each_entry(act, &actions, list)
  512. exts->actions[i++] = act;
  513. exts->nr_actions = i;
  514. }
  515. }
  516. #else
  517. if ((exts->action && tb[exts->action]) ||
  518. (exts->police && tb[exts->police]))
  519. return -EOPNOTSUPP;
  520. #endif
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(tcf_exts_validate);
  524. void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  525. struct tcf_exts *src)
  526. {
  527. #ifdef CONFIG_NET_CLS_ACT
  528. struct tcf_exts old = *dst;
  529. tcf_tree_lock(tp);
  530. dst->nr_actions = src->nr_actions;
  531. dst->actions = src->actions;
  532. dst->type = src->type;
  533. tcf_tree_unlock(tp);
  534. tcf_exts_destroy(&old);
  535. #endif
  536. }
  537. EXPORT_SYMBOL(tcf_exts_change);
  538. #ifdef CONFIG_NET_CLS_ACT
  539. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  540. {
  541. if (exts->nr_actions == 0)
  542. return NULL;
  543. else
  544. return exts->actions[0];
  545. }
  546. #endif
  547. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  548. {
  549. #ifdef CONFIG_NET_CLS_ACT
  550. struct nlattr *nest;
  551. if (exts->action && exts->nr_actions) {
  552. /*
  553. * again for backward compatible mode - we want
  554. * to work with both old and new modes of entering
  555. * tc data even if iproute2 was newer - jhs
  556. */
  557. if (exts->type != TCA_OLD_COMPAT) {
  558. LIST_HEAD(actions);
  559. nest = nla_nest_start(skb, exts->action);
  560. if (nest == NULL)
  561. goto nla_put_failure;
  562. tcf_exts_to_list(exts, &actions);
  563. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  564. goto nla_put_failure;
  565. nla_nest_end(skb, nest);
  566. } else if (exts->police) {
  567. struct tc_action *act = tcf_exts_first_act(exts);
  568. nest = nla_nest_start(skb, exts->police);
  569. if (nest == NULL || !act)
  570. goto nla_put_failure;
  571. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  572. goto nla_put_failure;
  573. nla_nest_end(skb, nest);
  574. }
  575. }
  576. return 0;
  577. nla_put_failure:
  578. nla_nest_cancel(skb, nest);
  579. return -1;
  580. #else
  581. return 0;
  582. #endif
  583. }
  584. EXPORT_SYMBOL(tcf_exts_dump);
  585. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  586. {
  587. #ifdef CONFIG_NET_CLS_ACT
  588. struct tc_action *a = tcf_exts_first_act(exts);
  589. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  590. return -1;
  591. #endif
  592. return 0;
  593. }
  594. EXPORT_SYMBOL(tcf_exts_dump_stats);
  595. int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
  596. struct net_device **hw_dev)
  597. {
  598. #ifdef CONFIG_NET_CLS_ACT
  599. const struct tc_action *a;
  600. LIST_HEAD(actions);
  601. if (tc_no_actions(exts))
  602. return -EINVAL;
  603. tcf_exts_to_list(exts, &actions);
  604. list_for_each_entry(a, &actions, list) {
  605. if (a->ops->get_dev) {
  606. a->ops->get_dev(a, dev_net(dev), hw_dev);
  607. break;
  608. }
  609. }
  610. if (*hw_dev)
  611. return 0;
  612. #endif
  613. return -EOPNOTSUPP;
  614. }
  615. EXPORT_SYMBOL(tcf_exts_get_dev);
  616. static int __init tc_filter_init(void)
  617. {
  618. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
  619. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
  620. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
  621. tc_dump_tfilter, NULL);
  622. return 0;
  623. }
  624. subsys_initcall(tc_filter_init);