cls_api.c 39 KB

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