cls_api.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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_chain *chain)
  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->chain = chain;
  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(struct tcf_block *block,
  163. u32 chain_index)
  164. {
  165. struct tcf_chain *chain;
  166. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  167. if (!chain)
  168. return NULL;
  169. list_add_tail(&chain->list, &block->chain_list);
  170. chain->block = block;
  171. chain->index = chain_index;
  172. chain->refcnt = 1;
  173. return chain;
  174. }
  175. static void tcf_chain_destroy(struct tcf_chain *chain)
  176. {
  177. struct tcf_proto *tp;
  178. list_del(&chain->list);
  179. while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
  180. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  181. tcf_proto_destroy(tp);
  182. }
  183. kfree(chain);
  184. }
  185. struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
  186. {
  187. struct tcf_chain *chain;
  188. list_for_each_entry(chain, &block->chain_list, list) {
  189. if (chain->index == chain_index) {
  190. chain->refcnt++;
  191. return chain;
  192. }
  193. }
  194. return tcf_chain_create(block, chain_index);
  195. }
  196. EXPORT_SYMBOL(tcf_chain_get);
  197. void tcf_chain_put(struct tcf_chain *chain)
  198. {
  199. /* Destroy unused chain, with exception of chain 0, which is the
  200. * default one and has to be always present.
  201. */
  202. if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
  203. tcf_chain_destroy(chain);
  204. }
  205. EXPORT_SYMBOL(tcf_chain_put);
  206. static void
  207. tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
  208. struct tcf_proto __rcu **p_filter_chain)
  209. {
  210. chain->p_filter_chain = p_filter_chain;
  211. }
  212. int tcf_block_get(struct tcf_block **p_block,
  213. struct tcf_proto __rcu **p_filter_chain)
  214. {
  215. struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  216. struct tcf_chain *chain;
  217. int err;
  218. if (!block)
  219. return -ENOMEM;
  220. INIT_LIST_HEAD(&block->chain_list);
  221. /* Create chain 0 by default, it has to be always present. */
  222. chain = tcf_chain_create(block, 0);
  223. if (!chain) {
  224. err = -ENOMEM;
  225. goto err_chain_create;
  226. }
  227. tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
  228. *p_block = block;
  229. return 0;
  230. err_chain_create:
  231. kfree(block);
  232. return err;
  233. }
  234. EXPORT_SYMBOL(tcf_block_get);
  235. void tcf_block_put(struct tcf_block *block)
  236. {
  237. struct tcf_chain *chain, *tmp;
  238. if (!block)
  239. return;
  240. list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
  241. tcf_chain_destroy(chain);
  242. kfree(block);
  243. }
  244. EXPORT_SYMBOL(tcf_block_put);
  245. /* Main classifier routine: scans classifier chain attached
  246. * to this qdisc, (optionally) tests for protocol and asks
  247. * specific classifiers.
  248. */
  249. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  250. struct tcf_result *res, bool compat_mode)
  251. {
  252. __be16 protocol = tc_skb_protocol(skb);
  253. #ifdef CONFIG_NET_CLS_ACT
  254. const int max_reclassify_loop = 4;
  255. const struct tcf_proto *old_tp = tp;
  256. int limit = 0;
  257. reclassify:
  258. #endif
  259. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  260. int err;
  261. if (tp->protocol != protocol &&
  262. tp->protocol != htons(ETH_P_ALL))
  263. continue;
  264. err = tp->classify(skb, tp, res);
  265. #ifdef CONFIG_NET_CLS_ACT
  266. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  267. goto reset;
  268. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  269. old_tp = res->goto_tp;
  270. goto reset;
  271. }
  272. #endif
  273. if (err >= 0)
  274. return err;
  275. }
  276. return TC_ACT_UNSPEC; /* signal: continue lookup */
  277. #ifdef CONFIG_NET_CLS_ACT
  278. reset:
  279. if (unlikely(limit++ >= max_reclassify_loop)) {
  280. net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
  281. tp->q->ops->id, tp->prio & 0xffff,
  282. ntohs(tp->protocol));
  283. return TC_ACT_SHOT;
  284. }
  285. tp = old_tp;
  286. protocol = tc_skb_protocol(skb);
  287. goto reclassify;
  288. #endif
  289. }
  290. EXPORT_SYMBOL(tcf_classify);
  291. struct tcf_chain_info {
  292. struct tcf_proto __rcu **pprev;
  293. struct tcf_proto __rcu *next;
  294. };
  295. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  296. {
  297. return rtnl_dereference(*chain_info->pprev);
  298. }
  299. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  300. struct tcf_chain_info *chain_info,
  301. struct tcf_proto *tp)
  302. {
  303. if (chain->p_filter_chain &&
  304. *chain_info->pprev == chain->filter_chain)
  305. *chain->p_filter_chain = tp;
  306. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  307. rcu_assign_pointer(*chain_info->pprev, tp);
  308. }
  309. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  310. struct tcf_chain_info *chain_info,
  311. struct tcf_proto *tp)
  312. {
  313. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  314. if (chain->p_filter_chain && tp == chain->filter_chain)
  315. *chain->p_filter_chain = next;
  316. RCU_INIT_POINTER(*chain_info->pprev, next);
  317. }
  318. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  319. struct tcf_chain_info *chain_info,
  320. u32 protocol, u32 prio,
  321. bool prio_allocate)
  322. {
  323. struct tcf_proto **pprev;
  324. struct tcf_proto *tp;
  325. /* Check the chain for existence of proto-tcf with this priority */
  326. for (pprev = &chain->filter_chain;
  327. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  328. if (tp->prio >= prio) {
  329. if (tp->prio == prio) {
  330. if (prio_allocate ||
  331. (tp->protocol != protocol && protocol))
  332. return ERR_PTR(-EINVAL);
  333. } else {
  334. tp = NULL;
  335. }
  336. break;
  337. }
  338. }
  339. chain_info->pprev = pprev;
  340. chain_info->next = tp ? tp->next : NULL;
  341. return tp;
  342. }
  343. /* Add/change/delete/get a filter node */
  344. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  345. struct netlink_ext_ack *extack)
  346. {
  347. struct net *net = sock_net(skb->sk);
  348. struct nlattr *tca[TCA_MAX + 1];
  349. struct tcmsg *t;
  350. u32 protocol;
  351. u32 prio;
  352. bool prio_allocate;
  353. u32 parent;
  354. u32 chain_index;
  355. struct net_device *dev;
  356. struct Qdisc *q;
  357. struct tcf_chain_info chain_info;
  358. struct tcf_chain *chain = NULL;
  359. struct tcf_block *block;
  360. struct tcf_proto *tp;
  361. const struct Qdisc_class_ops *cops;
  362. unsigned long cl;
  363. unsigned long fh;
  364. int err;
  365. int tp_created;
  366. if ((n->nlmsg_type != RTM_GETTFILTER) &&
  367. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  368. return -EPERM;
  369. replay:
  370. tp_created = 0;
  371. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  372. if (err < 0)
  373. return err;
  374. t = nlmsg_data(n);
  375. protocol = TC_H_MIN(t->tcm_info);
  376. prio = TC_H_MAJ(t->tcm_info);
  377. prio_allocate = false;
  378. parent = t->tcm_parent;
  379. cl = 0;
  380. if (prio == 0) {
  381. switch (n->nlmsg_type) {
  382. case RTM_DELTFILTER:
  383. if (protocol || t->tcm_handle || tca[TCA_KIND])
  384. return -ENOENT;
  385. break;
  386. case RTM_NEWTFILTER:
  387. /* If no priority is provided by the user,
  388. * we allocate one.
  389. */
  390. if (n->nlmsg_flags & NLM_F_CREATE) {
  391. prio = TC_H_MAKE(0x80000000U, 0U);
  392. prio_allocate = true;
  393. break;
  394. }
  395. /* fall-through */
  396. default:
  397. return -ENOENT;
  398. }
  399. }
  400. /* Find head of filter chain. */
  401. /* Find link */
  402. dev = __dev_get_by_index(net, t->tcm_ifindex);
  403. if (dev == NULL)
  404. return -ENODEV;
  405. /* Find qdisc */
  406. if (!parent) {
  407. q = dev->qdisc;
  408. parent = q->handle;
  409. } else {
  410. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  411. if (q == NULL)
  412. return -EINVAL;
  413. }
  414. /* Is it classful? */
  415. cops = q->ops->cl_ops;
  416. if (!cops)
  417. return -EINVAL;
  418. if (!cops->tcf_block)
  419. return -EOPNOTSUPP;
  420. /* Do we search for filter, attached to class? */
  421. if (TC_H_MIN(parent)) {
  422. cl = cops->get(q, parent);
  423. if (cl == 0)
  424. return -ENOENT;
  425. }
  426. /* And the last stroke */
  427. block = cops->tcf_block(q, cl);
  428. if (!block) {
  429. err = -EINVAL;
  430. goto errout;
  431. }
  432. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  433. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  434. err = -EINVAL;
  435. goto errout;
  436. }
  437. chain = tcf_chain_get(block, chain_index);
  438. if (!chain) {
  439. err = -ENOMEM;
  440. goto errout;
  441. }
  442. if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
  443. tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
  444. tcf_chain_destroy(chain);
  445. err = 0;
  446. goto errout;
  447. }
  448. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  449. prio, prio_allocate);
  450. if (IS_ERR(tp)) {
  451. err = PTR_ERR(tp);
  452. goto errout;
  453. }
  454. if (tp == NULL) {
  455. /* Proto-tcf does not exist, create new one */
  456. if (tca[TCA_KIND] == NULL || !protocol) {
  457. err = -EINVAL;
  458. goto errout;
  459. }
  460. if (n->nlmsg_type != RTM_NEWTFILTER ||
  461. !(n->nlmsg_flags & NLM_F_CREATE)) {
  462. err = -ENOENT;
  463. goto errout;
  464. }
  465. if (prio_allocate)
  466. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  467. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  468. protocol, prio, parent, q, chain);
  469. if (IS_ERR(tp)) {
  470. err = PTR_ERR(tp);
  471. goto errout;
  472. }
  473. tp_created = 1;
  474. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  475. err = -EINVAL;
  476. goto errout;
  477. }
  478. fh = tp->ops->get(tp, t->tcm_handle);
  479. if (fh == 0) {
  480. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  481. tcf_chain_tp_remove(chain, &chain_info, tp);
  482. tfilter_notify(net, skb, n, tp, fh,
  483. RTM_DELTFILTER, false);
  484. tcf_proto_destroy(tp);
  485. err = 0;
  486. goto errout;
  487. }
  488. if (n->nlmsg_type != RTM_NEWTFILTER ||
  489. !(n->nlmsg_flags & NLM_F_CREATE)) {
  490. err = -ENOENT;
  491. goto errout;
  492. }
  493. } else {
  494. bool last;
  495. switch (n->nlmsg_type) {
  496. case RTM_NEWTFILTER:
  497. if (n->nlmsg_flags & NLM_F_EXCL) {
  498. if (tp_created)
  499. tcf_proto_destroy(tp);
  500. err = -EEXIST;
  501. goto errout;
  502. }
  503. break;
  504. case RTM_DELTFILTER:
  505. err = tp->ops->delete(tp, fh, &last);
  506. if (err)
  507. goto errout;
  508. tfilter_notify(net, skb, n, tp, t->tcm_handle,
  509. RTM_DELTFILTER, false);
  510. if (last) {
  511. tcf_chain_tp_remove(chain, &chain_info, tp);
  512. tcf_proto_destroy(tp);
  513. }
  514. goto errout;
  515. case RTM_GETTFILTER:
  516. err = tfilter_notify(net, skb, n, tp, fh,
  517. RTM_NEWTFILTER, true);
  518. goto errout;
  519. default:
  520. err = -EINVAL;
  521. goto errout;
  522. }
  523. }
  524. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  525. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
  526. if (err == 0) {
  527. if (tp_created)
  528. tcf_chain_tp_insert(chain, &chain_info, tp);
  529. tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
  530. } else {
  531. if (tp_created)
  532. tcf_proto_destroy(tp);
  533. }
  534. errout:
  535. if (chain)
  536. tcf_chain_put(chain);
  537. if (cl)
  538. cops->put(q, cl);
  539. if (err == -EAGAIN)
  540. /* Replay the request. */
  541. goto replay;
  542. return err;
  543. }
  544. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  545. struct tcf_proto *tp, unsigned long fh, u32 portid,
  546. u32 seq, u16 flags, int event)
  547. {
  548. struct tcmsg *tcm;
  549. struct nlmsghdr *nlh;
  550. unsigned char *b = skb_tail_pointer(skb);
  551. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  552. if (!nlh)
  553. goto out_nlmsg_trim;
  554. tcm = nlmsg_data(nlh);
  555. tcm->tcm_family = AF_UNSPEC;
  556. tcm->tcm__pad1 = 0;
  557. tcm->tcm__pad2 = 0;
  558. tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
  559. tcm->tcm_parent = tp->classid;
  560. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  561. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  562. goto nla_put_failure;
  563. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  564. goto nla_put_failure;
  565. tcm->tcm_handle = fh;
  566. if (RTM_DELTFILTER != event) {
  567. tcm->tcm_handle = 0;
  568. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  569. goto nla_put_failure;
  570. }
  571. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  572. return skb->len;
  573. out_nlmsg_trim:
  574. nla_put_failure:
  575. nlmsg_trim(skb, b);
  576. return -1;
  577. }
  578. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  579. struct nlmsghdr *n, struct tcf_proto *tp,
  580. unsigned long fh, int event, bool unicast)
  581. {
  582. struct sk_buff *skb;
  583. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  584. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  585. if (!skb)
  586. return -ENOBUFS;
  587. if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
  588. n->nlmsg_flags, event) <= 0) {
  589. kfree_skb(skb);
  590. return -EINVAL;
  591. }
  592. if (unicast)
  593. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  594. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  595. n->nlmsg_flags & NLM_F_ECHO);
  596. }
  597. struct tcf_dump_args {
  598. struct tcf_walker w;
  599. struct sk_buff *skb;
  600. struct netlink_callback *cb;
  601. };
  602. static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
  603. struct tcf_walker *arg)
  604. {
  605. struct tcf_dump_args *a = (void *)arg;
  606. struct net *net = sock_net(a->skb->sk);
  607. return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
  608. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  609. RTM_NEWTFILTER);
  610. }
  611. static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
  612. struct netlink_callback *cb,
  613. long index_start, long *p_index)
  614. {
  615. struct net *net = sock_net(skb->sk);
  616. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  617. struct tcf_dump_args arg;
  618. struct tcf_proto *tp;
  619. for (tp = rtnl_dereference(chain->filter_chain);
  620. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  621. if (*p_index < index_start)
  622. continue;
  623. if (TC_H_MAJ(tcm->tcm_info) &&
  624. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  625. continue;
  626. if (TC_H_MIN(tcm->tcm_info) &&
  627. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  628. continue;
  629. if (*p_index > index_start)
  630. memset(&cb->args[1], 0,
  631. sizeof(cb->args) - sizeof(cb->args[0]));
  632. if (cb->args[1] == 0) {
  633. if (tcf_fill_node(net, skb, tp, 0,
  634. NETLINK_CB(cb->skb).portid,
  635. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  636. RTM_NEWTFILTER) <= 0)
  637. return false;
  638. cb->args[1] = 1;
  639. }
  640. if (!tp->ops->walk)
  641. continue;
  642. arg.w.fn = tcf_node_dump;
  643. arg.skb = skb;
  644. arg.cb = cb;
  645. arg.w.stop = 0;
  646. arg.w.skip = cb->args[1] - 1;
  647. arg.w.count = 0;
  648. tp->ops->walk(tp, &arg.w);
  649. cb->args[1] = arg.w.count + 1;
  650. if (arg.w.stop)
  651. return false;
  652. }
  653. return true;
  654. }
  655. /* called with RTNL */
  656. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  657. {
  658. struct net *net = sock_net(skb->sk);
  659. struct nlattr *tca[TCA_MAX + 1];
  660. struct net_device *dev;
  661. struct Qdisc *q;
  662. struct tcf_block *block;
  663. struct tcf_chain *chain;
  664. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  665. unsigned long cl = 0;
  666. const struct Qdisc_class_ops *cops;
  667. long index_start;
  668. long index;
  669. int err;
  670. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  671. return skb->len;
  672. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  673. if (err)
  674. return err;
  675. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  676. if (!dev)
  677. return skb->len;
  678. if (!tcm->tcm_parent)
  679. q = dev->qdisc;
  680. else
  681. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  682. if (!q)
  683. goto out;
  684. cops = q->ops->cl_ops;
  685. if (!cops)
  686. goto errout;
  687. if (!cops->tcf_block)
  688. goto errout;
  689. if (TC_H_MIN(tcm->tcm_parent)) {
  690. cl = cops->get(q, tcm->tcm_parent);
  691. if (cl == 0)
  692. goto errout;
  693. }
  694. block = cops->tcf_block(q, cl);
  695. if (!block)
  696. goto errout;
  697. index_start = cb->args[0];
  698. index = 0;
  699. list_for_each_entry(chain, &block->chain_list, list) {
  700. if (tca[TCA_CHAIN] &&
  701. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  702. continue;
  703. if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
  704. break;
  705. }
  706. cb->args[0] = index;
  707. errout:
  708. if (cl)
  709. cops->put(q, cl);
  710. out:
  711. return skb->len;
  712. }
  713. void tcf_exts_destroy(struct tcf_exts *exts)
  714. {
  715. #ifdef CONFIG_NET_CLS_ACT
  716. LIST_HEAD(actions);
  717. tcf_exts_to_list(exts, &actions);
  718. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  719. kfree(exts->actions);
  720. exts->nr_actions = 0;
  721. #endif
  722. }
  723. EXPORT_SYMBOL(tcf_exts_destroy);
  724. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  725. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
  726. {
  727. #ifdef CONFIG_NET_CLS_ACT
  728. {
  729. struct tc_action *act;
  730. if (exts->police && tb[exts->police]) {
  731. act = tcf_action_init_1(net, tp, tb[exts->police],
  732. rate_tlv, "police", ovr,
  733. TCA_ACT_BIND);
  734. if (IS_ERR(act))
  735. return PTR_ERR(act);
  736. act->type = exts->type = TCA_OLD_COMPAT;
  737. exts->actions[0] = act;
  738. exts->nr_actions = 1;
  739. } else if (exts->action && tb[exts->action]) {
  740. LIST_HEAD(actions);
  741. int err, i = 0;
  742. err = tcf_action_init(net, tp, tb[exts->action],
  743. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  744. &actions);
  745. if (err)
  746. return err;
  747. list_for_each_entry(act, &actions, list)
  748. exts->actions[i++] = act;
  749. exts->nr_actions = i;
  750. }
  751. }
  752. #else
  753. if ((exts->action && tb[exts->action]) ||
  754. (exts->police && tb[exts->police]))
  755. return -EOPNOTSUPP;
  756. #endif
  757. return 0;
  758. }
  759. EXPORT_SYMBOL(tcf_exts_validate);
  760. void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  761. struct tcf_exts *src)
  762. {
  763. #ifdef CONFIG_NET_CLS_ACT
  764. struct tcf_exts old = *dst;
  765. tcf_tree_lock(tp);
  766. dst->nr_actions = src->nr_actions;
  767. dst->actions = src->actions;
  768. dst->type = src->type;
  769. tcf_tree_unlock(tp);
  770. tcf_exts_destroy(&old);
  771. #endif
  772. }
  773. EXPORT_SYMBOL(tcf_exts_change);
  774. #ifdef CONFIG_NET_CLS_ACT
  775. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  776. {
  777. if (exts->nr_actions == 0)
  778. return NULL;
  779. else
  780. return exts->actions[0];
  781. }
  782. #endif
  783. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  784. {
  785. #ifdef CONFIG_NET_CLS_ACT
  786. struct nlattr *nest;
  787. if (exts->action && exts->nr_actions) {
  788. /*
  789. * again for backward compatible mode - we want
  790. * to work with both old and new modes of entering
  791. * tc data even if iproute2 was newer - jhs
  792. */
  793. if (exts->type != TCA_OLD_COMPAT) {
  794. LIST_HEAD(actions);
  795. nest = nla_nest_start(skb, exts->action);
  796. if (nest == NULL)
  797. goto nla_put_failure;
  798. tcf_exts_to_list(exts, &actions);
  799. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  800. goto nla_put_failure;
  801. nla_nest_end(skb, nest);
  802. } else if (exts->police) {
  803. struct tc_action *act = tcf_exts_first_act(exts);
  804. nest = nla_nest_start(skb, exts->police);
  805. if (nest == NULL || !act)
  806. goto nla_put_failure;
  807. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  808. goto nla_put_failure;
  809. nla_nest_end(skb, nest);
  810. }
  811. }
  812. return 0;
  813. nla_put_failure:
  814. nla_nest_cancel(skb, nest);
  815. return -1;
  816. #else
  817. return 0;
  818. #endif
  819. }
  820. EXPORT_SYMBOL(tcf_exts_dump);
  821. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  822. {
  823. #ifdef CONFIG_NET_CLS_ACT
  824. struct tc_action *a = tcf_exts_first_act(exts);
  825. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  826. return -1;
  827. #endif
  828. return 0;
  829. }
  830. EXPORT_SYMBOL(tcf_exts_dump_stats);
  831. int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
  832. struct net_device **hw_dev)
  833. {
  834. #ifdef CONFIG_NET_CLS_ACT
  835. const struct tc_action *a;
  836. LIST_HEAD(actions);
  837. if (tc_no_actions(exts))
  838. return -EINVAL;
  839. tcf_exts_to_list(exts, &actions);
  840. list_for_each_entry(a, &actions, list) {
  841. if (a->ops->get_dev) {
  842. a->ops->get_dev(a, dev_net(dev), hw_dev);
  843. break;
  844. }
  845. }
  846. if (*hw_dev)
  847. return 0;
  848. #endif
  849. return -EOPNOTSUPP;
  850. }
  851. EXPORT_SYMBOL(tcf_exts_get_dev);
  852. static int __init tc_filter_init(void)
  853. {
  854. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
  855. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
  856. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
  857. tc_dump_tfilter, NULL);
  858. return 0;
  859. }
  860. subsys_initcall(tc_filter_init);