cls_api.c 39 KB

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