cls_api.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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/slab.h>
  26. #include <linux/idr.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. static struct workqueue_struct *tc_filter_wq;
  70. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  71. {
  72. struct tcf_proto_ops *t;
  73. int rc = -ENOENT;
  74. /* Wait for outstanding call_rcu()s, if any, from a
  75. * tcf_proto_ops's destroy() handler.
  76. */
  77. rcu_barrier();
  78. flush_workqueue(tc_filter_wq);
  79. write_lock(&cls_mod_lock);
  80. list_for_each_entry(t, &tcf_proto_base, head) {
  81. if (t == ops) {
  82. list_del(&t->head);
  83. rc = 0;
  84. break;
  85. }
  86. }
  87. write_unlock(&cls_mod_lock);
  88. return rc;
  89. }
  90. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  91. bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
  92. {
  93. INIT_RCU_WORK(rwork, func);
  94. return queue_rcu_work(tc_filter_wq, rwork);
  95. }
  96. EXPORT_SYMBOL(tcf_queue_work);
  97. /* Select new prio value from the range, managed by kernel. */
  98. static inline u32 tcf_auto_prio(struct tcf_proto *tp)
  99. {
  100. u32 first = TC_H_MAKE(0xC0000000U, 0U);
  101. if (tp)
  102. first = tp->prio - 1;
  103. return TC_H_MAJ(first);
  104. }
  105. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  106. u32 prio, struct tcf_chain *chain,
  107. struct netlink_ext_ack *extack)
  108. {
  109. struct tcf_proto *tp;
  110. int err;
  111. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  112. if (!tp)
  113. return ERR_PTR(-ENOBUFS);
  114. err = -ENOENT;
  115. tp->ops = tcf_proto_lookup_ops(kind);
  116. if (!tp->ops) {
  117. #ifdef CONFIG_MODULES
  118. rtnl_unlock();
  119. request_module("cls_%s", kind);
  120. rtnl_lock();
  121. tp->ops = tcf_proto_lookup_ops(kind);
  122. /* We dropped the RTNL semaphore in order to perform
  123. * the module load. So, even if we succeeded in loading
  124. * the module we have to replay the request. We indicate
  125. * this using -EAGAIN.
  126. */
  127. if (tp->ops) {
  128. module_put(tp->ops->owner);
  129. err = -EAGAIN;
  130. } else {
  131. NL_SET_ERR_MSG(extack, "TC classifier not found");
  132. err = -ENOENT;
  133. }
  134. #endif
  135. goto errout;
  136. }
  137. tp->classify = tp->ops->classify;
  138. tp->protocol = protocol;
  139. tp->prio = prio;
  140. tp->chain = chain;
  141. err = tp->ops->init(tp);
  142. if (err) {
  143. module_put(tp->ops->owner);
  144. goto errout;
  145. }
  146. return tp;
  147. errout:
  148. kfree(tp);
  149. return ERR_PTR(err);
  150. }
  151. static void tcf_proto_destroy(struct tcf_proto *tp,
  152. struct netlink_ext_ack *extack)
  153. {
  154. tp->ops->destroy(tp, extack);
  155. module_put(tp->ops->owner);
  156. kfree_rcu(tp, rcu);
  157. }
  158. struct tcf_filter_chain_list_item {
  159. struct list_head list;
  160. tcf_chain_head_change_t *chain_head_change;
  161. void *chain_head_change_priv;
  162. };
  163. static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
  164. u32 chain_index)
  165. {
  166. struct tcf_chain *chain;
  167. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  168. if (!chain)
  169. return NULL;
  170. INIT_LIST_HEAD(&chain->filter_chain_list);
  171. list_add_tail(&chain->list, &block->chain_list);
  172. chain->block = block;
  173. chain->index = chain_index;
  174. chain->refcnt = 1;
  175. return chain;
  176. }
  177. static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
  178. struct tcf_proto *tp_head)
  179. {
  180. if (item->chain_head_change)
  181. item->chain_head_change(tp_head, item->chain_head_change_priv);
  182. }
  183. static void tcf_chain_head_change(struct tcf_chain *chain,
  184. struct tcf_proto *tp_head)
  185. {
  186. struct tcf_filter_chain_list_item *item;
  187. list_for_each_entry(item, &chain->filter_chain_list, list)
  188. tcf_chain_head_change_item(item, tp_head);
  189. }
  190. static void tcf_chain_flush(struct tcf_chain *chain)
  191. {
  192. struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
  193. tcf_chain_head_change(chain, NULL);
  194. while (tp) {
  195. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  196. tcf_proto_destroy(tp, NULL);
  197. tp = rtnl_dereference(chain->filter_chain);
  198. tcf_chain_put(chain);
  199. }
  200. }
  201. static void tcf_chain_destroy(struct tcf_chain *chain)
  202. {
  203. struct tcf_block *block = chain->block;
  204. list_del(&chain->list);
  205. kfree(chain);
  206. if (list_empty(&block->chain_list))
  207. kfree(block);
  208. }
  209. static void tcf_chain_hold(struct tcf_chain *chain)
  210. {
  211. ++chain->refcnt;
  212. }
  213. struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  214. bool create)
  215. {
  216. struct tcf_chain *chain;
  217. list_for_each_entry(chain, &block->chain_list, list) {
  218. if (chain->index == chain_index) {
  219. tcf_chain_hold(chain);
  220. return chain;
  221. }
  222. }
  223. return create ? tcf_chain_create(block, chain_index) : NULL;
  224. }
  225. EXPORT_SYMBOL(tcf_chain_get);
  226. void tcf_chain_put(struct tcf_chain *chain)
  227. {
  228. if (--chain->refcnt == 0)
  229. tcf_chain_destroy(chain);
  230. }
  231. EXPORT_SYMBOL(tcf_chain_put);
  232. static bool tcf_block_offload_in_use(struct tcf_block *block)
  233. {
  234. return block->offloadcnt;
  235. }
  236. static int tcf_block_offload_cmd(struct tcf_block *block,
  237. struct net_device *dev,
  238. struct tcf_block_ext_info *ei,
  239. enum tc_block_command command)
  240. {
  241. struct tc_block_offload bo = {};
  242. bo.command = command;
  243. bo.binder_type = ei->binder_type;
  244. bo.block = block;
  245. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
  246. }
  247. static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
  248. struct tcf_block_ext_info *ei)
  249. {
  250. struct net_device *dev = q->dev_queue->dev;
  251. int err;
  252. if (!dev->netdev_ops->ndo_setup_tc)
  253. goto no_offload_dev_inc;
  254. /* If tc offload feature is disabled and the block we try to bind
  255. * to already has some offloaded filters, forbid to bind.
  256. */
  257. if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
  258. return -EOPNOTSUPP;
  259. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
  260. if (err == -EOPNOTSUPP)
  261. goto no_offload_dev_inc;
  262. return err;
  263. no_offload_dev_inc:
  264. if (tcf_block_offload_in_use(block))
  265. return -EOPNOTSUPP;
  266. block->nooffloaddevcnt++;
  267. return 0;
  268. }
  269. static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
  270. struct tcf_block_ext_info *ei)
  271. {
  272. struct net_device *dev = q->dev_queue->dev;
  273. int err;
  274. if (!dev->netdev_ops->ndo_setup_tc)
  275. goto no_offload_dev_dec;
  276. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
  277. if (err == -EOPNOTSUPP)
  278. goto no_offload_dev_dec;
  279. return;
  280. no_offload_dev_dec:
  281. WARN_ON(block->nooffloaddevcnt-- == 0);
  282. }
  283. static int
  284. tcf_chain_head_change_cb_add(struct tcf_chain *chain,
  285. struct tcf_block_ext_info *ei,
  286. struct netlink_ext_ack *extack)
  287. {
  288. struct tcf_filter_chain_list_item *item;
  289. item = kmalloc(sizeof(*item), GFP_KERNEL);
  290. if (!item) {
  291. NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
  292. return -ENOMEM;
  293. }
  294. item->chain_head_change = ei->chain_head_change;
  295. item->chain_head_change_priv = ei->chain_head_change_priv;
  296. if (chain->filter_chain)
  297. tcf_chain_head_change_item(item, chain->filter_chain);
  298. list_add(&item->list, &chain->filter_chain_list);
  299. return 0;
  300. }
  301. static void
  302. tcf_chain_head_change_cb_del(struct tcf_chain *chain,
  303. struct tcf_block_ext_info *ei)
  304. {
  305. struct tcf_filter_chain_list_item *item;
  306. list_for_each_entry(item, &chain->filter_chain_list, list) {
  307. if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
  308. (item->chain_head_change == ei->chain_head_change &&
  309. item->chain_head_change_priv == ei->chain_head_change_priv)) {
  310. tcf_chain_head_change_item(item, NULL);
  311. list_del(&item->list);
  312. kfree(item);
  313. return;
  314. }
  315. }
  316. WARN_ON(1);
  317. }
  318. struct tcf_net {
  319. struct idr idr;
  320. };
  321. static unsigned int tcf_net_id;
  322. static int tcf_block_insert(struct tcf_block *block, struct net *net,
  323. struct netlink_ext_ack *extack)
  324. {
  325. struct tcf_net *tn = net_generic(net, tcf_net_id);
  326. return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
  327. GFP_KERNEL);
  328. }
  329. static void tcf_block_remove(struct tcf_block *block, struct net *net)
  330. {
  331. struct tcf_net *tn = net_generic(net, tcf_net_id);
  332. idr_remove(&tn->idr, block->index);
  333. }
  334. static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
  335. u32 block_index,
  336. struct netlink_ext_ack *extack)
  337. {
  338. struct tcf_block *block;
  339. struct tcf_chain *chain;
  340. int err;
  341. block = kzalloc(sizeof(*block), GFP_KERNEL);
  342. if (!block) {
  343. NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
  344. return ERR_PTR(-ENOMEM);
  345. }
  346. INIT_LIST_HEAD(&block->chain_list);
  347. INIT_LIST_HEAD(&block->cb_list);
  348. INIT_LIST_HEAD(&block->owner_list);
  349. /* Create chain 0 by default, it has to be always present. */
  350. chain = tcf_chain_create(block, 0);
  351. if (!chain) {
  352. NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
  353. err = -ENOMEM;
  354. goto err_chain_create;
  355. }
  356. block->refcnt = 1;
  357. block->net = net;
  358. block->index = block_index;
  359. /* Don't store q pointer for blocks which are shared */
  360. if (!tcf_block_shared(block))
  361. block->q = q;
  362. return block;
  363. err_chain_create:
  364. kfree(block);
  365. return ERR_PTR(err);
  366. }
  367. static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
  368. {
  369. struct tcf_net *tn = net_generic(net, tcf_net_id);
  370. return idr_find(&tn->idr, block_index);
  371. }
  372. /* Find tcf block.
  373. * Set q, parent, cl when appropriate.
  374. */
  375. static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
  376. u32 *parent, unsigned long *cl,
  377. int ifindex, u32 block_index,
  378. struct netlink_ext_ack *extack)
  379. {
  380. struct tcf_block *block;
  381. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  382. block = tcf_block_lookup(net, block_index);
  383. if (!block) {
  384. NL_SET_ERR_MSG(extack, "Block of given index was not found");
  385. return ERR_PTR(-EINVAL);
  386. }
  387. } else {
  388. const struct Qdisc_class_ops *cops;
  389. struct net_device *dev;
  390. /* Find link */
  391. dev = __dev_get_by_index(net, ifindex);
  392. if (!dev)
  393. return ERR_PTR(-ENODEV);
  394. /* Find qdisc */
  395. if (!*parent) {
  396. *q = dev->qdisc;
  397. *parent = (*q)->handle;
  398. } else {
  399. *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
  400. if (!*q) {
  401. NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
  402. return ERR_PTR(-EINVAL);
  403. }
  404. }
  405. /* Is it classful? */
  406. cops = (*q)->ops->cl_ops;
  407. if (!cops) {
  408. NL_SET_ERR_MSG(extack, "Qdisc not classful");
  409. return ERR_PTR(-EINVAL);
  410. }
  411. if (!cops->tcf_block) {
  412. NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
  413. return ERR_PTR(-EOPNOTSUPP);
  414. }
  415. /* Do we search for filter, attached to class? */
  416. if (TC_H_MIN(*parent)) {
  417. *cl = cops->find(*q, *parent);
  418. if (*cl == 0) {
  419. NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
  420. return ERR_PTR(-ENOENT);
  421. }
  422. }
  423. /* And the last stroke */
  424. block = cops->tcf_block(*q, *cl, extack);
  425. if (!block)
  426. return ERR_PTR(-EINVAL);
  427. if (tcf_block_shared(block)) {
  428. NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
  429. return ERR_PTR(-EOPNOTSUPP);
  430. }
  431. }
  432. return block;
  433. }
  434. static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
  435. {
  436. return list_first_entry(&block->chain_list, struct tcf_chain, list);
  437. }
  438. struct tcf_block_owner_item {
  439. struct list_head list;
  440. struct Qdisc *q;
  441. enum tcf_block_binder_type binder_type;
  442. };
  443. static void
  444. tcf_block_owner_netif_keep_dst(struct tcf_block *block,
  445. struct Qdisc *q,
  446. enum tcf_block_binder_type binder_type)
  447. {
  448. if (block->keep_dst &&
  449. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
  450. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
  451. netif_keep_dst(qdisc_dev(q));
  452. }
  453. void tcf_block_netif_keep_dst(struct tcf_block *block)
  454. {
  455. struct tcf_block_owner_item *item;
  456. block->keep_dst = true;
  457. list_for_each_entry(item, &block->owner_list, list)
  458. tcf_block_owner_netif_keep_dst(block, item->q,
  459. item->binder_type);
  460. }
  461. EXPORT_SYMBOL(tcf_block_netif_keep_dst);
  462. static int tcf_block_owner_add(struct tcf_block *block,
  463. struct Qdisc *q,
  464. enum tcf_block_binder_type binder_type)
  465. {
  466. struct tcf_block_owner_item *item;
  467. item = kmalloc(sizeof(*item), GFP_KERNEL);
  468. if (!item)
  469. return -ENOMEM;
  470. item->q = q;
  471. item->binder_type = binder_type;
  472. list_add(&item->list, &block->owner_list);
  473. return 0;
  474. }
  475. static void tcf_block_owner_del(struct tcf_block *block,
  476. struct Qdisc *q,
  477. enum tcf_block_binder_type binder_type)
  478. {
  479. struct tcf_block_owner_item *item;
  480. list_for_each_entry(item, &block->owner_list, list) {
  481. if (item->q == q && item->binder_type == binder_type) {
  482. list_del(&item->list);
  483. kfree(item);
  484. return;
  485. }
  486. }
  487. WARN_ON(1);
  488. }
  489. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  490. struct tcf_block_ext_info *ei,
  491. struct netlink_ext_ack *extack)
  492. {
  493. struct net *net = qdisc_net(q);
  494. struct tcf_block *block = NULL;
  495. bool created = false;
  496. int err;
  497. if (ei->block_index) {
  498. /* block_index not 0 means the shared block is requested */
  499. block = tcf_block_lookup(net, ei->block_index);
  500. if (block)
  501. block->refcnt++;
  502. }
  503. if (!block) {
  504. block = tcf_block_create(net, q, ei->block_index, extack);
  505. if (IS_ERR(block))
  506. return PTR_ERR(block);
  507. created = true;
  508. if (tcf_block_shared(block)) {
  509. err = tcf_block_insert(block, net, extack);
  510. if (err)
  511. goto err_block_insert;
  512. }
  513. }
  514. err = tcf_block_owner_add(block, q, ei->binder_type);
  515. if (err)
  516. goto err_block_owner_add;
  517. tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
  518. err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
  519. ei, extack);
  520. if (err)
  521. goto err_chain_head_change_cb_add;
  522. err = tcf_block_offload_bind(block, q, ei);
  523. if (err)
  524. goto err_block_offload_bind;
  525. *p_block = block;
  526. return 0;
  527. err_block_offload_bind:
  528. tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
  529. err_chain_head_change_cb_add:
  530. tcf_block_owner_del(block, q, ei->binder_type);
  531. err_block_owner_add:
  532. if (created) {
  533. if (tcf_block_shared(block))
  534. tcf_block_remove(block, net);
  535. err_block_insert:
  536. kfree(tcf_block_chain_zero(block));
  537. kfree(block);
  538. } else {
  539. block->refcnt--;
  540. }
  541. return err;
  542. }
  543. EXPORT_SYMBOL(tcf_block_get_ext);
  544. static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
  545. {
  546. struct tcf_proto __rcu **p_filter_chain = priv;
  547. rcu_assign_pointer(*p_filter_chain, tp_head);
  548. }
  549. int tcf_block_get(struct tcf_block **p_block,
  550. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  551. struct netlink_ext_ack *extack)
  552. {
  553. struct tcf_block_ext_info ei = {
  554. .chain_head_change = tcf_chain_head_change_dflt,
  555. .chain_head_change_priv = p_filter_chain,
  556. };
  557. WARN_ON(!p_filter_chain);
  558. return tcf_block_get_ext(p_block, q, &ei, extack);
  559. }
  560. EXPORT_SYMBOL(tcf_block_get);
  561. /* XXX: Standalone actions are not allowed to jump to any chain, and bound
  562. * actions should be all removed after flushing.
  563. */
  564. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  565. struct tcf_block_ext_info *ei)
  566. {
  567. struct tcf_chain *chain, *tmp;
  568. if (!block)
  569. return;
  570. tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
  571. tcf_block_owner_del(block, q, ei->binder_type);
  572. if (--block->refcnt == 0) {
  573. if (tcf_block_shared(block))
  574. tcf_block_remove(block, block->net);
  575. /* Hold a refcnt for all chains, so that they don't disappear
  576. * while we are iterating.
  577. */
  578. list_for_each_entry(chain, &block->chain_list, list)
  579. tcf_chain_hold(chain);
  580. list_for_each_entry(chain, &block->chain_list, list)
  581. tcf_chain_flush(chain);
  582. }
  583. tcf_block_offload_unbind(block, q, ei);
  584. if (block->refcnt == 0) {
  585. /* At this point, all the chains should have refcnt >= 1. */
  586. list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
  587. tcf_chain_put(chain);
  588. /* Finally, put chain 0 and allow block to be freed. */
  589. tcf_chain_put(tcf_block_chain_zero(block));
  590. }
  591. }
  592. EXPORT_SYMBOL(tcf_block_put_ext);
  593. void tcf_block_put(struct tcf_block *block)
  594. {
  595. struct tcf_block_ext_info ei = {0, };
  596. if (!block)
  597. return;
  598. tcf_block_put_ext(block, block->q, &ei);
  599. }
  600. EXPORT_SYMBOL(tcf_block_put);
  601. struct tcf_block_cb {
  602. struct list_head list;
  603. tc_setup_cb_t *cb;
  604. void *cb_ident;
  605. void *cb_priv;
  606. unsigned int refcnt;
  607. };
  608. void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
  609. {
  610. return block_cb->cb_priv;
  611. }
  612. EXPORT_SYMBOL(tcf_block_cb_priv);
  613. struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
  614. tc_setup_cb_t *cb, void *cb_ident)
  615. { struct tcf_block_cb *block_cb;
  616. list_for_each_entry(block_cb, &block->cb_list, list)
  617. if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
  618. return block_cb;
  619. return NULL;
  620. }
  621. EXPORT_SYMBOL(tcf_block_cb_lookup);
  622. void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
  623. {
  624. block_cb->refcnt++;
  625. }
  626. EXPORT_SYMBOL(tcf_block_cb_incref);
  627. unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
  628. {
  629. return --block_cb->refcnt;
  630. }
  631. EXPORT_SYMBOL(tcf_block_cb_decref);
  632. struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
  633. tc_setup_cb_t *cb, void *cb_ident,
  634. void *cb_priv)
  635. {
  636. struct tcf_block_cb *block_cb;
  637. /* At this point, playback of previous block cb calls is not supported,
  638. * so forbid to register to block which already has some offloaded
  639. * filters present.
  640. */
  641. if (tcf_block_offload_in_use(block))
  642. return ERR_PTR(-EOPNOTSUPP);
  643. block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
  644. if (!block_cb)
  645. return ERR_PTR(-ENOMEM);
  646. block_cb->cb = cb;
  647. block_cb->cb_ident = cb_ident;
  648. block_cb->cb_priv = cb_priv;
  649. list_add(&block_cb->list, &block->cb_list);
  650. return block_cb;
  651. }
  652. EXPORT_SYMBOL(__tcf_block_cb_register);
  653. int tcf_block_cb_register(struct tcf_block *block,
  654. tc_setup_cb_t *cb, void *cb_ident,
  655. void *cb_priv)
  656. {
  657. struct tcf_block_cb *block_cb;
  658. block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
  659. return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
  660. }
  661. EXPORT_SYMBOL(tcf_block_cb_register);
  662. void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
  663. {
  664. list_del(&block_cb->list);
  665. kfree(block_cb);
  666. }
  667. EXPORT_SYMBOL(__tcf_block_cb_unregister);
  668. void tcf_block_cb_unregister(struct tcf_block *block,
  669. tc_setup_cb_t *cb, void *cb_ident)
  670. {
  671. struct tcf_block_cb *block_cb;
  672. block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
  673. if (!block_cb)
  674. return;
  675. __tcf_block_cb_unregister(block_cb);
  676. }
  677. EXPORT_SYMBOL(tcf_block_cb_unregister);
  678. static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
  679. void *type_data, bool err_stop)
  680. {
  681. struct tcf_block_cb *block_cb;
  682. int ok_count = 0;
  683. int err;
  684. /* Make sure all netdevs sharing this block are offload-capable. */
  685. if (block->nooffloaddevcnt && err_stop)
  686. return -EOPNOTSUPP;
  687. list_for_each_entry(block_cb, &block->cb_list, list) {
  688. err = block_cb->cb(type, type_data, block_cb->cb_priv);
  689. if (err) {
  690. if (err_stop)
  691. return err;
  692. } else {
  693. ok_count++;
  694. }
  695. }
  696. return ok_count;
  697. }
  698. /* Main classifier routine: scans classifier chain attached
  699. * to this qdisc, (optionally) tests for protocol and asks
  700. * specific classifiers.
  701. */
  702. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  703. struct tcf_result *res, bool compat_mode)
  704. {
  705. __be16 protocol = tc_skb_protocol(skb);
  706. #ifdef CONFIG_NET_CLS_ACT
  707. const int max_reclassify_loop = 4;
  708. const struct tcf_proto *orig_tp = tp;
  709. const struct tcf_proto *first_tp;
  710. int limit = 0;
  711. reclassify:
  712. #endif
  713. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  714. int err;
  715. if (tp->protocol != protocol &&
  716. tp->protocol != htons(ETH_P_ALL))
  717. continue;
  718. err = tp->classify(skb, tp, res);
  719. #ifdef CONFIG_NET_CLS_ACT
  720. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  721. first_tp = orig_tp;
  722. goto reset;
  723. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  724. first_tp = res->goto_tp;
  725. goto reset;
  726. }
  727. #endif
  728. if (err >= 0)
  729. return err;
  730. }
  731. return TC_ACT_UNSPEC; /* signal: continue lookup */
  732. #ifdef CONFIG_NET_CLS_ACT
  733. reset:
  734. if (unlikely(limit++ >= max_reclassify_loop)) {
  735. net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
  736. tp->chain->block->index,
  737. tp->prio & 0xffff,
  738. ntohs(tp->protocol));
  739. return TC_ACT_SHOT;
  740. }
  741. tp = first_tp;
  742. protocol = tc_skb_protocol(skb);
  743. goto reclassify;
  744. #endif
  745. }
  746. EXPORT_SYMBOL(tcf_classify);
  747. struct tcf_chain_info {
  748. struct tcf_proto __rcu **pprev;
  749. struct tcf_proto __rcu *next;
  750. };
  751. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  752. {
  753. return rtnl_dereference(*chain_info->pprev);
  754. }
  755. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  756. struct tcf_chain_info *chain_info,
  757. struct tcf_proto *tp)
  758. {
  759. if (*chain_info->pprev == chain->filter_chain)
  760. tcf_chain_head_change(chain, tp);
  761. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  762. rcu_assign_pointer(*chain_info->pprev, tp);
  763. tcf_chain_hold(chain);
  764. }
  765. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  766. struct tcf_chain_info *chain_info,
  767. struct tcf_proto *tp)
  768. {
  769. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  770. if (tp == chain->filter_chain)
  771. tcf_chain_head_change(chain, next);
  772. RCU_INIT_POINTER(*chain_info->pprev, next);
  773. tcf_chain_put(chain);
  774. }
  775. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  776. struct tcf_chain_info *chain_info,
  777. u32 protocol, u32 prio,
  778. bool prio_allocate)
  779. {
  780. struct tcf_proto **pprev;
  781. struct tcf_proto *tp;
  782. /* Check the chain for existence of proto-tcf with this priority */
  783. for (pprev = &chain->filter_chain;
  784. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  785. if (tp->prio >= prio) {
  786. if (tp->prio == prio) {
  787. if (prio_allocate ||
  788. (tp->protocol != protocol && protocol))
  789. return ERR_PTR(-EINVAL);
  790. } else {
  791. tp = NULL;
  792. }
  793. break;
  794. }
  795. }
  796. chain_info->pprev = pprev;
  797. chain_info->next = tp ? tp->next : NULL;
  798. return tp;
  799. }
  800. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  801. struct tcf_proto *tp, struct tcf_block *block,
  802. struct Qdisc *q, u32 parent, void *fh,
  803. u32 portid, u32 seq, u16 flags, int event)
  804. {
  805. struct tcmsg *tcm;
  806. struct nlmsghdr *nlh;
  807. unsigned char *b = skb_tail_pointer(skb);
  808. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  809. if (!nlh)
  810. goto out_nlmsg_trim;
  811. tcm = nlmsg_data(nlh);
  812. tcm->tcm_family = AF_UNSPEC;
  813. tcm->tcm__pad1 = 0;
  814. tcm->tcm__pad2 = 0;
  815. if (q) {
  816. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  817. tcm->tcm_parent = parent;
  818. } else {
  819. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  820. tcm->tcm_block_index = block->index;
  821. }
  822. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  823. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  824. goto nla_put_failure;
  825. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  826. goto nla_put_failure;
  827. if (!fh) {
  828. tcm->tcm_handle = 0;
  829. } else {
  830. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  831. goto nla_put_failure;
  832. }
  833. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  834. return skb->len;
  835. out_nlmsg_trim:
  836. nla_put_failure:
  837. nlmsg_trim(skb, b);
  838. return -1;
  839. }
  840. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  841. struct nlmsghdr *n, struct tcf_proto *tp,
  842. struct tcf_block *block, struct Qdisc *q,
  843. u32 parent, void *fh, int event, bool unicast)
  844. {
  845. struct sk_buff *skb;
  846. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  847. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  848. if (!skb)
  849. return -ENOBUFS;
  850. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  851. n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
  852. kfree_skb(skb);
  853. return -EINVAL;
  854. }
  855. if (unicast)
  856. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  857. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  858. n->nlmsg_flags & NLM_F_ECHO);
  859. }
  860. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  861. struct nlmsghdr *n, struct tcf_proto *tp,
  862. struct tcf_block *block, struct Qdisc *q,
  863. u32 parent, void *fh, bool unicast, bool *last,
  864. struct netlink_ext_ack *extack)
  865. {
  866. struct sk_buff *skb;
  867. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  868. int err;
  869. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  870. if (!skb)
  871. return -ENOBUFS;
  872. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  873. n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
  874. NL_SET_ERR_MSG(extack, "Failed to build del event notification");
  875. kfree_skb(skb);
  876. return -EINVAL;
  877. }
  878. err = tp->ops->delete(tp, fh, last, extack);
  879. if (err) {
  880. kfree_skb(skb);
  881. return err;
  882. }
  883. if (unicast)
  884. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  885. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  886. n->nlmsg_flags & NLM_F_ECHO);
  887. if (err < 0)
  888. NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
  889. return err;
  890. }
  891. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  892. struct tcf_block *block, struct Qdisc *q,
  893. u32 parent, struct nlmsghdr *n,
  894. struct tcf_chain *chain, int event)
  895. {
  896. struct tcf_proto *tp;
  897. for (tp = rtnl_dereference(chain->filter_chain);
  898. tp; tp = rtnl_dereference(tp->next))
  899. tfilter_notify(net, oskb, n, tp, block,
  900. q, parent, 0, event, false);
  901. }
  902. static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  903. struct netlink_ext_ack *extack)
  904. {
  905. struct net *net = sock_net(skb->sk);
  906. struct nlattr *tca[TCA_MAX + 1];
  907. struct tcmsg *t;
  908. u32 protocol;
  909. u32 prio;
  910. bool prio_allocate;
  911. u32 parent;
  912. u32 chain_index;
  913. struct Qdisc *q = NULL;
  914. struct tcf_chain_info chain_info;
  915. struct tcf_chain *chain = NULL;
  916. struct tcf_block *block;
  917. struct tcf_proto *tp;
  918. unsigned long cl;
  919. void *fh;
  920. int err;
  921. int tp_created;
  922. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  923. return -EPERM;
  924. replay:
  925. tp_created = 0;
  926. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  927. if (err < 0)
  928. return err;
  929. t = nlmsg_data(n);
  930. protocol = TC_H_MIN(t->tcm_info);
  931. prio = TC_H_MAJ(t->tcm_info);
  932. prio_allocate = false;
  933. parent = t->tcm_parent;
  934. cl = 0;
  935. if (prio == 0) {
  936. /* If no priority is provided by the user,
  937. * we allocate one.
  938. */
  939. if (n->nlmsg_flags & NLM_F_CREATE) {
  940. prio = TC_H_MAKE(0x80000000U, 0U);
  941. prio_allocate = true;
  942. } else {
  943. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  944. return -ENOENT;
  945. }
  946. }
  947. /* Find head of filter chain. */
  948. block = tcf_block_find(net, &q, &parent, &cl,
  949. t->tcm_ifindex, t->tcm_block_index, extack);
  950. if (IS_ERR(block)) {
  951. err = PTR_ERR(block);
  952. goto errout;
  953. }
  954. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  955. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  956. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  957. err = -EINVAL;
  958. goto errout;
  959. }
  960. chain = tcf_chain_get(block, chain_index, true);
  961. if (!chain) {
  962. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  963. err = -ENOMEM;
  964. goto errout;
  965. }
  966. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  967. prio, prio_allocate);
  968. if (IS_ERR(tp)) {
  969. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  970. err = PTR_ERR(tp);
  971. goto errout;
  972. }
  973. if (tp == NULL) {
  974. /* Proto-tcf does not exist, create new one */
  975. if (tca[TCA_KIND] == NULL || !protocol) {
  976. NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
  977. err = -EINVAL;
  978. goto errout;
  979. }
  980. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  981. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  982. err = -ENOENT;
  983. goto errout;
  984. }
  985. if (prio_allocate)
  986. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  987. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  988. protocol, prio, chain, extack);
  989. if (IS_ERR(tp)) {
  990. err = PTR_ERR(tp);
  991. goto errout;
  992. }
  993. tp_created = 1;
  994. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  995. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  996. err = -EINVAL;
  997. goto errout;
  998. }
  999. fh = tp->ops->get(tp, t->tcm_handle);
  1000. if (!fh) {
  1001. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1002. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1003. err = -ENOENT;
  1004. goto errout;
  1005. }
  1006. } else if (n->nlmsg_flags & NLM_F_EXCL) {
  1007. NL_SET_ERR_MSG(extack, "Filter already exists");
  1008. err = -EEXIST;
  1009. goto errout;
  1010. }
  1011. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  1012. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
  1013. extack);
  1014. if (err == 0) {
  1015. if (tp_created)
  1016. tcf_chain_tp_insert(chain, &chain_info, tp);
  1017. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  1018. RTM_NEWTFILTER, false);
  1019. } else {
  1020. if (tp_created)
  1021. tcf_proto_destroy(tp, NULL);
  1022. }
  1023. errout:
  1024. if (chain)
  1025. tcf_chain_put(chain);
  1026. if (err == -EAGAIN)
  1027. /* Replay the request. */
  1028. goto replay;
  1029. return err;
  1030. }
  1031. static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1032. struct netlink_ext_ack *extack)
  1033. {
  1034. struct net *net = sock_net(skb->sk);
  1035. struct nlattr *tca[TCA_MAX + 1];
  1036. struct tcmsg *t;
  1037. u32 protocol;
  1038. u32 prio;
  1039. u32 parent;
  1040. u32 chain_index;
  1041. struct Qdisc *q = NULL;
  1042. struct tcf_chain_info chain_info;
  1043. struct tcf_chain *chain = NULL;
  1044. struct tcf_block *block;
  1045. struct tcf_proto *tp = NULL;
  1046. unsigned long cl = 0;
  1047. void *fh = NULL;
  1048. int err;
  1049. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1050. return -EPERM;
  1051. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1052. if (err < 0)
  1053. return err;
  1054. t = nlmsg_data(n);
  1055. protocol = TC_H_MIN(t->tcm_info);
  1056. prio = TC_H_MAJ(t->tcm_info);
  1057. parent = t->tcm_parent;
  1058. if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
  1059. NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
  1060. return -ENOENT;
  1061. }
  1062. /* Find head of filter chain. */
  1063. block = tcf_block_find(net, &q, &parent, &cl,
  1064. t->tcm_ifindex, t->tcm_block_index, extack);
  1065. if (IS_ERR(block)) {
  1066. err = PTR_ERR(block);
  1067. goto errout;
  1068. }
  1069. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1070. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1071. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1072. err = -EINVAL;
  1073. goto errout;
  1074. }
  1075. chain = tcf_chain_get(block, chain_index, false);
  1076. if (!chain) {
  1077. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1078. err = -EINVAL;
  1079. goto errout;
  1080. }
  1081. if (prio == 0) {
  1082. tfilter_notify_chain(net, skb, block, q, parent, n,
  1083. chain, RTM_DELTFILTER);
  1084. tcf_chain_flush(chain);
  1085. err = 0;
  1086. goto errout;
  1087. }
  1088. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1089. prio, false);
  1090. if (!tp || IS_ERR(tp)) {
  1091. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1092. err = tp ? PTR_ERR(tp) : -ENOENT;
  1093. goto errout;
  1094. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1095. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1096. err = -EINVAL;
  1097. goto errout;
  1098. }
  1099. fh = tp->ops->get(tp, t->tcm_handle);
  1100. if (!fh) {
  1101. if (t->tcm_handle == 0) {
  1102. tcf_chain_tp_remove(chain, &chain_info, tp);
  1103. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  1104. RTM_DELTFILTER, false);
  1105. tcf_proto_destroy(tp, extack);
  1106. err = 0;
  1107. } else {
  1108. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1109. err = -ENOENT;
  1110. }
  1111. } else {
  1112. bool last;
  1113. err = tfilter_del_notify(net, skb, n, tp, block,
  1114. q, parent, fh, false, &last,
  1115. extack);
  1116. if (err)
  1117. goto errout;
  1118. if (last) {
  1119. tcf_chain_tp_remove(chain, &chain_info, tp);
  1120. tcf_proto_destroy(tp, extack);
  1121. }
  1122. }
  1123. errout:
  1124. if (chain)
  1125. tcf_chain_put(chain);
  1126. return err;
  1127. }
  1128. static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1129. struct netlink_ext_ack *extack)
  1130. {
  1131. struct net *net = sock_net(skb->sk);
  1132. struct nlattr *tca[TCA_MAX + 1];
  1133. struct tcmsg *t;
  1134. u32 protocol;
  1135. u32 prio;
  1136. u32 parent;
  1137. u32 chain_index;
  1138. struct Qdisc *q = NULL;
  1139. struct tcf_chain_info chain_info;
  1140. struct tcf_chain *chain = NULL;
  1141. struct tcf_block *block;
  1142. struct tcf_proto *tp = NULL;
  1143. unsigned long cl = 0;
  1144. void *fh = NULL;
  1145. int err;
  1146. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1147. if (err < 0)
  1148. return err;
  1149. t = nlmsg_data(n);
  1150. protocol = TC_H_MIN(t->tcm_info);
  1151. prio = TC_H_MAJ(t->tcm_info);
  1152. parent = t->tcm_parent;
  1153. if (prio == 0) {
  1154. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1155. return -ENOENT;
  1156. }
  1157. /* Find head of filter chain. */
  1158. block = tcf_block_find(net, &q, &parent, &cl,
  1159. t->tcm_ifindex, t->tcm_block_index, extack);
  1160. if (IS_ERR(block)) {
  1161. err = PTR_ERR(block);
  1162. goto errout;
  1163. }
  1164. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1165. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1166. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1167. err = -EINVAL;
  1168. goto errout;
  1169. }
  1170. chain = tcf_chain_get(block, chain_index, false);
  1171. if (!chain) {
  1172. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1173. err = -EINVAL;
  1174. goto errout;
  1175. }
  1176. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1177. prio, false);
  1178. if (!tp || IS_ERR(tp)) {
  1179. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1180. err = tp ? PTR_ERR(tp) : -ENOENT;
  1181. goto errout;
  1182. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1183. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1184. err = -EINVAL;
  1185. goto errout;
  1186. }
  1187. fh = tp->ops->get(tp, t->tcm_handle);
  1188. if (!fh) {
  1189. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1190. err = -ENOENT;
  1191. } else {
  1192. err = tfilter_notify(net, skb, n, tp, block, q, parent,
  1193. fh, RTM_NEWTFILTER, true);
  1194. if (err < 0)
  1195. NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
  1196. }
  1197. errout:
  1198. if (chain)
  1199. tcf_chain_put(chain);
  1200. return err;
  1201. }
  1202. struct tcf_dump_args {
  1203. struct tcf_walker w;
  1204. struct sk_buff *skb;
  1205. struct netlink_callback *cb;
  1206. struct tcf_block *block;
  1207. struct Qdisc *q;
  1208. u32 parent;
  1209. };
  1210. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  1211. {
  1212. struct tcf_dump_args *a = (void *)arg;
  1213. struct net *net = sock_net(a->skb->sk);
  1214. return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
  1215. n, NETLINK_CB(a->cb->skb).portid,
  1216. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1217. RTM_NEWTFILTER);
  1218. }
  1219. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  1220. struct sk_buff *skb, struct netlink_callback *cb,
  1221. long index_start, long *p_index)
  1222. {
  1223. struct net *net = sock_net(skb->sk);
  1224. struct tcf_block *block = chain->block;
  1225. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1226. struct tcf_dump_args arg;
  1227. struct tcf_proto *tp;
  1228. for (tp = rtnl_dereference(chain->filter_chain);
  1229. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  1230. if (*p_index < index_start)
  1231. continue;
  1232. if (TC_H_MAJ(tcm->tcm_info) &&
  1233. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  1234. continue;
  1235. if (TC_H_MIN(tcm->tcm_info) &&
  1236. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  1237. continue;
  1238. if (*p_index > index_start)
  1239. memset(&cb->args[1], 0,
  1240. sizeof(cb->args) - sizeof(cb->args[0]));
  1241. if (cb->args[1] == 0) {
  1242. if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
  1243. NETLINK_CB(cb->skb).portid,
  1244. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1245. RTM_NEWTFILTER) <= 0)
  1246. return false;
  1247. cb->args[1] = 1;
  1248. }
  1249. if (!tp->ops->walk)
  1250. continue;
  1251. arg.w.fn = tcf_node_dump;
  1252. arg.skb = skb;
  1253. arg.cb = cb;
  1254. arg.block = block;
  1255. arg.q = q;
  1256. arg.parent = parent;
  1257. arg.w.stop = 0;
  1258. arg.w.skip = cb->args[1] - 1;
  1259. arg.w.count = 0;
  1260. tp->ops->walk(tp, &arg.w);
  1261. cb->args[1] = arg.w.count + 1;
  1262. if (arg.w.stop)
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. /* called with RTNL */
  1268. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  1269. {
  1270. struct net *net = sock_net(skb->sk);
  1271. struct nlattr *tca[TCA_MAX + 1];
  1272. struct Qdisc *q = NULL;
  1273. struct tcf_block *block;
  1274. struct tcf_chain *chain;
  1275. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1276. long index_start;
  1277. long index;
  1278. u32 parent;
  1279. int err;
  1280. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1281. return skb->len;
  1282. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1283. if (err)
  1284. return err;
  1285. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1286. block = tcf_block_lookup(net, tcm->tcm_block_index);
  1287. if (!block)
  1288. goto out;
  1289. /* If we work with block index, q is NULL and parent value
  1290. * will never be used in the following code. The check
  1291. * in tcf_fill_node prevents it. However, compiler does not
  1292. * see that far, so set parent to zero to silence the warning
  1293. * about parent being uninitialized.
  1294. */
  1295. parent = 0;
  1296. } else {
  1297. const struct Qdisc_class_ops *cops;
  1298. struct net_device *dev;
  1299. unsigned long cl = 0;
  1300. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1301. if (!dev)
  1302. return skb->len;
  1303. parent = tcm->tcm_parent;
  1304. if (!parent) {
  1305. q = dev->qdisc;
  1306. parent = q->handle;
  1307. } else {
  1308. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  1309. }
  1310. if (!q)
  1311. goto out;
  1312. cops = q->ops->cl_ops;
  1313. if (!cops)
  1314. goto out;
  1315. if (!cops->tcf_block)
  1316. goto out;
  1317. if (TC_H_MIN(tcm->tcm_parent)) {
  1318. cl = cops->find(q, tcm->tcm_parent);
  1319. if (cl == 0)
  1320. goto out;
  1321. }
  1322. block = cops->tcf_block(q, cl, NULL);
  1323. if (!block)
  1324. goto out;
  1325. if (tcf_block_shared(block))
  1326. q = NULL;
  1327. }
  1328. index_start = cb->args[0];
  1329. index = 0;
  1330. list_for_each_entry(chain, &block->chain_list, list) {
  1331. if (tca[TCA_CHAIN] &&
  1332. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  1333. continue;
  1334. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  1335. index_start, &index)) {
  1336. err = -EMSGSIZE;
  1337. break;
  1338. }
  1339. }
  1340. cb->args[0] = index;
  1341. out:
  1342. /* If we did no progress, the error (EMSGSIZE) is real */
  1343. if (skb->len == 0 && err)
  1344. return err;
  1345. return skb->len;
  1346. }
  1347. void tcf_exts_destroy(struct tcf_exts *exts)
  1348. {
  1349. #ifdef CONFIG_NET_CLS_ACT
  1350. LIST_HEAD(actions);
  1351. ASSERT_RTNL();
  1352. tcf_exts_to_list(exts, &actions);
  1353. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  1354. kfree(exts->actions);
  1355. exts->nr_actions = 0;
  1356. #endif
  1357. }
  1358. EXPORT_SYMBOL(tcf_exts_destroy);
  1359. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  1360. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
  1361. struct netlink_ext_ack *extack)
  1362. {
  1363. #ifdef CONFIG_NET_CLS_ACT
  1364. {
  1365. struct tc_action *act;
  1366. size_t attr_size = 0;
  1367. if (exts->police && tb[exts->police]) {
  1368. act = tcf_action_init_1(net, tp, tb[exts->police],
  1369. rate_tlv, "police", ovr,
  1370. TCA_ACT_BIND, extack);
  1371. if (IS_ERR(act))
  1372. return PTR_ERR(act);
  1373. act->type = exts->type = TCA_OLD_COMPAT;
  1374. exts->actions[0] = act;
  1375. exts->nr_actions = 1;
  1376. } else if (exts->action && tb[exts->action]) {
  1377. LIST_HEAD(actions);
  1378. int err, i = 0;
  1379. err = tcf_action_init(net, tp, tb[exts->action],
  1380. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  1381. &actions, &attr_size, extack);
  1382. if (err)
  1383. return err;
  1384. list_for_each_entry(act, &actions, list)
  1385. exts->actions[i++] = act;
  1386. exts->nr_actions = i;
  1387. }
  1388. exts->net = net;
  1389. }
  1390. #else
  1391. if ((exts->action && tb[exts->action]) ||
  1392. (exts->police && tb[exts->police])) {
  1393. NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
  1394. return -EOPNOTSUPP;
  1395. }
  1396. #endif
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(tcf_exts_validate);
  1400. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  1401. {
  1402. #ifdef CONFIG_NET_CLS_ACT
  1403. struct tcf_exts old = *dst;
  1404. *dst = *src;
  1405. tcf_exts_destroy(&old);
  1406. #endif
  1407. }
  1408. EXPORT_SYMBOL(tcf_exts_change);
  1409. #ifdef CONFIG_NET_CLS_ACT
  1410. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  1411. {
  1412. if (exts->nr_actions == 0)
  1413. return NULL;
  1414. else
  1415. return exts->actions[0];
  1416. }
  1417. #endif
  1418. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  1419. {
  1420. #ifdef CONFIG_NET_CLS_ACT
  1421. struct nlattr *nest;
  1422. if (exts->action && tcf_exts_has_actions(exts)) {
  1423. /*
  1424. * again for backward compatible mode - we want
  1425. * to work with both old and new modes of entering
  1426. * tc data even if iproute2 was newer - jhs
  1427. */
  1428. if (exts->type != TCA_OLD_COMPAT) {
  1429. LIST_HEAD(actions);
  1430. nest = nla_nest_start(skb, exts->action);
  1431. if (nest == NULL)
  1432. goto nla_put_failure;
  1433. tcf_exts_to_list(exts, &actions);
  1434. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  1435. goto nla_put_failure;
  1436. nla_nest_end(skb, nest);
  1437. } else if (exts->police) {
  1438. struct tc_action *act = tcf_exts_first_act(exts);
  1439. nest = nla_nest_start(skb, exts->police);
  1440. if (nest == NULL || !act)
  1441. goto nla_put_failure;
  1442. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  1443. goto nla_put_failure;
  1444. nla_nest_end(skb, nest);
  1445. }
  1446. }
  1447. return 0;
  1448. nla_put_failure:
  1449. nla_nest_cancel(skb, nest);
  1450. return -1;
  1451. #else
  1452. return 0;
  1453. #endif
  1454. }
  1455. EXPORT_SYMBOL(tcf_exts_dump);
  1456. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  1457. {
  1458. #ifdef CONFIG_NET_CLS_ACT
  1459. struct tc_action *a = tcf_exts_first_act(exts);
  1460. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  1461. return -1;
  1462. #endif
  1463. return 0;
  1464. }
  1465. EXPORT_SYMBOL(tcf_exts_dump_stats);
  1466. static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
  1467. enum tc_setup_type type,
  1468. void *type_data, bool err_stop)
  1469. {
  1470. int ok_count = 0;
  1471. #ifdef CONFIG_NET_CLS_ACT
  1472. const struct tc_action *a;
  1473. struct net_device *dev;
  1474. int i, ret;
  1475. if (!tcf_exts_has_actions(exts))
  1476. return 0;
  1477. for (i = 0; i < exts->nr_actions; i++) {
  1478. a = exts->actions[i];
  1479. if (!a->ops->get_dev)
  1480. continue;
  1481. dev = a->ops->get_dev(a);
  1482. if (!dev)
  1483. continue;
  1484. ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
  1485. if (ret < 0)
  1486. return ret;
  1487. ok_count += ret;
  1488. }
  1489. #endif
  1490. return ok_count;
  1491. }
  1492. int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
  1493. enum tc_setup_type type, void *type_data, bool err_stop)
  1494. {
  1495. int ok_count;
  1496. int ret;
  1497. ret = tcf_block_cb_call(block, type, type_data, err_stop);
  1498. if (ret < 0)
  1499. return ret;
  1500. ok_count = ret;
  1501. if (!exts || ok_count)
  1502. return ok_count;
  1503. ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
  1504. if (ret < 0)
  1505. return ret;
  1506. ok_count += ret;
  1507. return ok_count;
  1508. }
  1509. EXPORT_SYMBOL(tc_setup_cb_call);
  1510. static __net_init int tcf_net_init(struct net *net)
  1511. {
  1512. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1513. idr_init(&tn->idr);
  1514. return 0;
  1515. }
  1516. static void __net_exit tcf_net_exit(struct net *net)
  1517. {
  1518. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1519. idr_destroy(&tn->idr);
  1520. }
  1521. static struct pernet_operations tcf_net_ops = {
  1522. .init = tcf_net_init,
  1523. .exit = tcf_net_exit,
  1524. .id = &tcf_net_id,
  1525. .size = sizeof(struct tcf_net),
  1526. };
  1527. static int __init tc_filter_init(void)
  1528. {
  1529. int err;
  1530. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  1531. if (!tc_filter_wq)
  1532. return -ENOMEM;
  1533. err = register_pernet_subsys(&tcf_net_ops);
  1534. if (err)
  1535. goto err_register_pernet_subsys;
  1536. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
  1537. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
  1538. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
  1539. tc_dump_tfilter, 0);
  1540. return 0;
  1541. err_register_pernet_subsys:
  1542. destroy_workqueue(tc_filter_wq);
  1543. return err;
  1544. }
  1545. subsys_initcall(tc_filter_init);