cls_api.c 23 KB

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