cls_api.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  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. static const struct tcf_proto_ops *
  54. tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
  55. {
  56. const struct tcf_proto_ops *ops;
  57. ops = __tcf_proto_lookup_ops(kind);
  58. if (ops)
  59. return ops;
  60. #ifdef CONFIG_MODULES
  61. rtnl_unlock();
  62. request_module("cls_%s", kind);
  63. rtnl_lock();
  64. ops = __tcf_proto_lookup_ops(kind);
  65. /* We dropped the RTNL semaphore in order to perform
  66. * the module load. So, even if we succeeded in loading
  67. * the module we have to replay the request. We indicate
  68. * this using -EAGAIN.
  69. */
  70. if (ops) {
  71. module_put(ops->owner);
  72. return ERR_PTR(-EAGAIN);
  73. }
  74. #endif
  75. NL_SET_ERR_MSG(extack, "TC classifier not found");
  76. return ERR_PTR(-ENOENT);
  77. }
  78. /* Register(unregister) new classifier type */
  79. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  80. {
  81. struct tcf_proto_ops *t;
  82. int rc = -EEXIST;
  83. write_lock(&cls_mod_lock);
  84. list_for_each_entry(t, &tcf_proto_base, head)
  85. if (!strcmp(ops->kind, t->kind))
  86. goto out;
  87. list_add_tail(&ops->head, &tcf_proto_base);
  88. rc = 0;
  89. out:
  90. write_unlock(&cls_mod_lock);
  91. return rc;
  92. }
  93. EXPORT_SYMBOL(register_tcf_proto_ops);
  94. static struct workqueue_struct *tc_filter_wq;
  95. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  96. {
  97. struct tcf_proto_ops *t;
  98. int rc = -ENOENT;
  99. /* Wait for outstanding call_rcu()s, if any, from a
  100. * tcf_proto_ops's destroy() handler.
  101. */
  102. rcu_barrier();
  103. flush_workqueue(tc_filter_wq);
  104. write_lock(&cls_mod_lock);
  105. list_for_each_entry(t, &tcf_proto_base, head) {
  106. if (t == ops) {
  107. list_del(&t->head);
  108. rc = 0;
  109. break;
  110. }
  111. }
  112. write_unlock(&cls_mod_lock);
  113. return rc;
  114. }
  115. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  116. bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
  117. {
  118. INIT_RCU_WORK(rwork, func);
  119. return queue_rcu_work(tc_filter_wq, rwork);
  120. }
  121. EXPORT_SYMBOL(tcf_queue_work);
  122. /* Select new prio value from the range, managed by kernel. */
  123. static inline u32 tcf_auto_prio(struct tcf_proto *tp)
  124. {
  125. u32 first = TC_H_MAKE(0xC0000000U, 0U);
  126. if (tp)
  127. first = tp->prio - 1;
  128. return TC_H_MAJ(first);
  129. }
  130. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  131. u32 prio, struct tcf_chain *chain,
  132. struct netlink_ext_ack *extack)
  133. {
  134. struct tcf_proto *tp;
  135. int err;
  136. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  137. if (!tp)
  138. return ERR_PTR(-ENOBUFS);
  139. tp->ops = tcf_proto_lookup_ops(kind, extack);
  140. if (IS_ERR(tp->ops)) {
  141. err = PTR_ERR(tp->ops);
  142. goto errout;
  143. }
  144. tp->classify = tp->ops->classify;
  145. tp->protocol = protocol;
  146. tp->prio = prio;
  147. tp->chain = chain;
  148. err = tp->ops->init(tp);
  149. if (err) {
  150. module_put(tp->ops->owner);
  151. goto errout;
  152. }
  153. return tp;
  154. errout:
  155. kfree(tp);
  156. return ERR_PTR(err);
  157. }
  158. static void tcf_proto_destroy(struct tcf_proto *tp,
  159. struct netlink_ext_ack *extack)
  160. {
  161. tp->ops->destroy(tp, extack);
  162. module_put(tp->ops->owner);
  163. kfree_rcu(tp, rcu);
  164. }
  165. struct tcf_filter_chain_list_item {
  166. struct list_head list;
  167. tcf_chain_head_change_t *chain_head_change;
  168. void *chain_head_change_priv;
  169. };
  170. static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
  171. u32 chain_index)
  172. {
  173. struct tcf_chain *chain;
  174. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  175. if (!chain)
  176. return NULL;
  177. list_add_tail(&chain->list, &block->chain_list);
  178. chain->block = block;
  179. chain->index = chain_index;
  180. chain->refcnt = 1;
  181. if (!chain->index)
  182. block->chain0.chain = chain;
  183. return chain;
  184. }
  185. static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
  186. struct tcf_proto *tp_head)
  187. {
  188. if (item->chain_head_change)
  189. item->chain_head_change(tp_head, item->chain_head_change_priv);
  190. }
  191. static void tcf_chain0_head_change(struct tcf_chain *chain,
  192. struct tcf_proto *tp_head)
  193. {
  194. struct tcf_filter_chain_list_item *item;
  195. struct tcf_block *block = chain->block;
  196. if (chain->index)
  197. return;
  198. list_for_each_entry(item, &block->chain0.filter_chain_list, list)
  199. tcf_chain_head_change_item(item, tp_head);
  200. }
  201. static void tcf_chain_destroy(struct tcf_chain *chain)
  202. {
  203. struct tcf_block *block = chain->block;
  204. list_del(&chain->list);
  205. if (!chain->index)
  206. block->chain0.chain = NULL;
  207. kfree(chain);
  208. if (list_empty(&block->chain_list) && block->refcnt == 0)
  209. kfree(block);
  210. }
  211. static void tcf_chain_hold(struct tcf_chain *chain)
  212. {
  213. ++chain->refcnt;
  214. }
  215. static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
  216. {
  217. /* In case all the references are action references, this
  218. * chain should not be shown to the user.
  219. */
  220. return chain->refcnt == chain->action_refcnt;
  221. }
  222. static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
  223. u32 chain_index)
  224. {
  225. struct tcf_chain *chain;
  226. list_for_each_entry(chain, &block->chain_list, list) {
  227. if (chain->index == chain_index)
  228. return chain;
  229. }
  230. return NULL;
  231. }
  232. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  233. u32 seq, u16 flags, int event, bool unicast);
  234. static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
  235. u32 chain_index, bool create,
  236. bool by_act)
  237. {
  238. struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
  239. if (chain) {
  240. tcf_chain_hold(chain);
  241. } else {
  242. if (!create)
  243. return NULL;
  244. chain = tcf_chain_create(block, chain_index);
  245. if (!chain)
  246. return NULL;
  247. }
  248. if (by_act)
  249. ++chain->action_refcnt;
  250. /* Send notification only in case we got the first
  251. * non-action reference. Until then, the chain acts only as
  252. * a placeholder for actions pointing to it and user ought
  253. * not know about them.
  254. */
  255. if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
  256. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  257. RTM_NEWCHAIN, false);
  258. return chain;
  259. }
  260. static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  261. bool create)
  262. {
  263. return __tcf_chain_get(block, chain_index, create, false);
  264. }
  265. struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
  266. {
  267. return __tcf_chain_get(block, chain_index, true, true);
  268. }
  269. EXPORT_SYMBOL(tcf_chain_get_by_act);
  270. static void tc_chain_tmplt_del(struct tcf_chain *chain);
  271. static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
  272. {
  273. if (by_act)
  274. chain->action_refcnt--;
  275. chain->refcnt--;
  276. /* The last dropped non-action reference will trigger notification. */
  277. if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
  278. tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
  279. if (chain->refcnt == 0) {
  280. tc_chain_tmplt_del(chain);
  281. tcf_chain_destroy(chain);
  282. }
  283. }
  284. static void tcf_chain_put(struct tcf_chain *chain)
  285. {
  286. __tcf_chain_put(chain, false);
  287. }
  288. void tcf_chain_put_by_act(struct tcf_chain *chain)
  289. {
  290. __tcf_chain_put(chain, true);
  291. }
  292. EXPORT_SYMBOL(tcf_chain_put_by_act);
  293. static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
  294. {
  295. if (chain->explicitly_created)
  296. tcf_chain_put(chain);
  297. }
  298. static void tcf_chain_flush(struct tcf_chain *chain)
  299. {
  300. struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
  301. tcf_chain0_head_change(chain, NULL);
  302. while (tp) {
  303. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  304. tcf_proto_destroy(tp, NULL);
  305. tp = rtnl_dereference(chain->filter_chain);
  306. tcf_chain_put(chain);
  307. }
  308. }
  309. static bool tcf_block_offload_in_use(struct tcf_block *block)
  310. {
  311. return block->offloadcnt;
  312. }
  313. static int tcf_block_offload_cmd(struct tcf_block *block,
  314. struct net_device *dev,
  315. struct tcf_block_ext_info *ei,
  316. enum tc_block_command command,
  317. struct netlink_ext_ack *extack)
  318. {
  319. struct tc_block_offload bo = {};
  320. bo.command = command;
  321. bo.binder_type = ei->binder_type;
  322. bo.block = block;
  323. bo.extack = extack;
  324. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
  325. }
  326. static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
  327. struct tcf_block_ext_info *ei,
  328. struct netlink_ext_ack *extack)
  329. {
  330. struct net_device *dev = q->dev_queue->dev;
  331. int err;
  332. if (!dev->netdev_ops->ndo_setup_tc)
  333. goto no_offload_dev_inc;
  334. /* If tc offload feature is disabled and the block we try to bind
  335. * to already has some offloaded filters, forbid to bind.
  336. */
  337. if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
  338. NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
  339. return -EOPNOTSUPP;
  340. }
  341. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
  342. if (err == -EOPNOTSUPP)
  343. goto no_offload_dev_inc;
  344. return err;
  345. no_offload_dev_inc:
  346. if (tcf_block_offload_in_use(block))
  347. return -EOPNOTSUPP;
  348. block->nooffloaddevcnt++;
  349. return 0;
  350. }
  351. static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
  352. struct tcf_block_ext_info *ei)
  353. {
  354. struct net_device *dev = q->dev_queue->dev;
  355. int err;
  356. if (!dev->netdev_ops->ndo_setup_tc)
  357. goto no_offload_dev_dec;
  358. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
  359. if (err == -EOPNOTSUPP)
  360. goto no_offload_dev_dec;
  361. return;
  362. no_offload_dev_dec:
  363. WARN_ON(block->nooffloaddevcnt-- == 0);
  364. }
  365. static int
  366. tcf_chain0_head_change_cb_add(struct tcf_block *block,
  367. struct tcf_block_ext_info *ei,
  368. struct netlink_ext_ack *extack)
  369. {
  370. struct tcf_chain *chain0 = block->chain0.chain;
  371. struct tcf_filter_chain_list_item *item;
  372. item = kmalloc(sizeof(*item), GFP_KERNEL);
  373. if (!item) {
  374. NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
  375. return -ENOMEM;
  376. }
  377. item->chain_head_change = ei->chain_head_change;
  378. item->chain_head_change_priv = ei->chain_head_change_priv;
  379. if (chain0 && chain0->filter_chain)
  380. tcf_chain_head_change_item(item, chain0->filter_chain);
  381. list_add(&item->list, &block->chain0.filter_chain_list);
  382. return 0;
  383. }
  384. static void
  385. tcf_chain0_head_change_cb_del(struct tcf_block *block,
  386. struct tcf_block_ext_info *ei)
  387. {
  388. struct tcf_chain *chain0 = block->chain0.chain;
  389. struct tcf_filter_chain_list_item *item;
  390. list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
  391. if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
  392. (item->chain_head_change == ei->chain_head_change &&
  393. item->chain_head_change_priv == ei->chain_head_change_priv)) {
  394. if (chain0)
  395. tcf_chain_head_change_item(item, NULL);
  396. list_del(&item->list);
  397. kfree(item);
  398. return;
  399. }
  400. }
  401. WARN_ON(1);
  402. }
  403. struct tcf_net {
  404. struct idr idr;
  405. };
  406. static unsigned int tcf_net_id;
  407. static int tcf_block_insert(struct tcf_block *block, struct net *net,
  408. struct netlink_ext_ack *extack)
  409. {
  410. struct tcf_net *tn = net_generic(net, tcf_net_id);
  411. return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
  412. GFP_KERNEL);
  413. }
  414. static void tcf_block_remove(struct tcf_block *block, struct net *net)
  415. {
  416. struct tcf_net *tn = net_generic(net, tcf_net_id);
  417. idr_remove(&tn->idr, block->index);
  418. }
  419. static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
  420. u32 block_index,
  421. struct netlink_ext_ack *extack)
  422. {
  423. struct tcf_block *block;
  424. block = kzalloc(sizeof(*block), GFP_KERNEL);
  425. if (!block) {
  426. NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
  427. return ERR_PTR(-ENOMEM);
  428. }
  429. INIT_LIST_HEAD(&block->chain_list);
  430. INIT_LIST_HEAD(&block->cb_list);
  431. INIT_LIST_HEAD(&block->owner_list);
  432. INIT_LIST_HEAD(&block->chain0.filter_chain_list);
  433. block->refcnt = 1;
  434. block->net = net;
  435. block->index = block_index;
  436. /* Don't store q pointer for blocks which are shared */
  437. if (!tcf_block_shared(block))
  438. block->q = q;
  439. return block;
  440. }
  441. static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
  442. {
  443. struct tcf_net *tn = net_generic(net, tcf_net_id);
  444. return idr_find(&tn->idr, block_index);
  445. }
  446. /* Find tcf block.
  447. * Set q, parent, cl when appropriate.
  448. */
  449. static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
  450. u32 *parent, unsigned long *cl,
  451. int ifindex, u32 block_index,
  452. struct netlink_ext_ack *extack)
  453. {
  454. struct tcf_block *block;
  455. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  456. block = tcf_block_lookup(net, block_index);
  457. if (!block) {
  458. NL_SET_ERR_MSG(extack, "Block of given index was not found");
  459. return ERR_PTR(-EINVAL);
  460. }
  461. } else {
  462. const struct Qdisc_class_ops *cops;
  463. struct net_device *dev;
  464. /* Find link */
  465. dev = __dev_get_by_index(net, ifindex);
  466. if (!dev)
  467. return ERR_PTR(-ENODEV);
  468. /* Find qdisc */
  469. if (!*parent) {
  470. *q = dev->qdisc;
  471. *parent = (*q)->handle;
  472. } else {
  473. *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
  474. if (!*q) {
  475. NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
  476. return ERR_PTR(-EINVAL);
  477. }
  478. }
  479. /* Is it classful? */
  480. cops = (*q)->ops->cl_ops;
  481. if (!cops) {
  482. NL_SET_ERR_MSG(extack, "Qdisc not classful");
  483. return ERR_PTR(-EINVAL);
  484. }
  485. if (!cops->tcf_block) {
  486. NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
  487. return ERR_PTR(-EOPNOTSUPP);
  488. }
  489. /* Do we search for filter, attached to class? */
  490. if (TC_H_MIN(*parent)) {
  491. *cl = cops->find(*q, *parent);
  492. if (*cl == 0) {
  493. NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
  494. return ERR_PTR(-ENOENT);
  495. }
  496. }
  497. /* And the last stroke */
  498. block = cops->tcf_block(*q, *cl, extack);
  499. if (!block)
  500. return ERR_PTR(-EINVAL);
  501. if (tcf_block_shared(block)) {
  502. NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
  503. return ERR_PTR(-EOPNOTSUPP);
  504. }
  505. }
  506. return block;
  507. }
  508. struct tcf_block_owner_item {
  509. struct list_head list;
  510. struct Qdisc *q;
  511. enum tcf_block_binder_type binder_type;
  512. };
  513. static void
  514. tcf_block_owner_netif_keep_dst(struct tcf_block *block,
  515. struct Qdisc *q,
  516. enum tcf_block_binder_type binder_type)
  517. {
  518. if (block->keep_dst &&
  519. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
  520. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
  521. netif_keep_dst(qdisc_dev(q));
  522. }
  523. void tcf_block_netif_keep_dst(struct tcf_block *block)
  524. {
  525. struct tcf_block_owner_item *item;
  526. block->keep_dst = true;
  527. list_for_each_entry(item, &block->owner_list, list)
  528. tcf_block_owner_netif_keep_dst(block, item->q,
  529. item->binder_type);
  530. }
  531. EXPORT_SYMBOL(tcf_block_netif_keep_dst);
  532. static int tcf_block_owner_add(struct tcf_block *block,
  533. struct Qdisc *q,
  534. enum tcf_block_binder_type binder_type)
  535. {
  536. struct tcf_block_owner_item *item;
  537. item = kmalloc(sizeof(*item), GFP_KERNEL);
  538. if (!item)
  539. return -ENOMEM;
  540. item->q = q;
  541. item->binder_type = binder_type;
  542. list_add(&item->list, &block->owner_list);
  543. return 0;
  544. }
  545. static void tcf_block_owner_del(struct tcf_block *block,
  546. struct Qdisc *q,
  547. enum tcf_block_binder_type binder_type)
  548. {
  549. struct tcf_block_owner_item *item;
  550. list_for_each_entry(item, &block->owner_list, list) {
  551. if (item->q == q && item->binder_type == binder_type) {
  552. list_del(&item->list);
  553. kfree(item);
  554. return;
  555. }
  556. }
  557. WARN_ON(1);
  558. }
  559. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  560. struct tcf_block_ext_info *ei,
  561. struct netlink_ext_ack *extack)
  562. {
  563. struct net *net = qdisc_net(q);
  564. struct tcf_block *block = NULL;
  565. bool created = false;
  566. int err;
  567. if (ei->block_index) {
  568. /* block_index not 0 means the shared block is requested */
  569. block = tcf_block_lookup(net, ei->block_index);
  570. if (block)
  571. block->refcnt++;
  572. }
  573. if (!block) {
  574. block = tcf_block_create(net, q, ei->block_index, extack);
  575. if (IS_ERR(block))
  576. return PTR_ERR(block);
  577. created = true;
  578. if (tcf_block_shared(block)) {
  579. err = tcf_block_insert(block, net, extack);
  580. if (err)
  581. goto err_block_insert;
  582. }
  583. }
  584. err = tcf_block_owner_add(block, q, ei->binder_type);
  585. if (err)
  586. goto err_block_owner_add;
  587. tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
  588. err = tcf_chain0_head_change_cb_add(block, ei, extack);
  589. if (err)
  590. goto err_chain0_head_change_cb_add;
  591. err = tcf_block_offload_bind(block, q, ei, extack);
  592. if (err)
  593. goto err_block_offload_bind;
  594. *p_block = block;
  595. return 0;
  596. err_block_offload_bind:
  597. tcf_chain0_head_change_cb_del(block, ei);
  598. err_chain0_head_change_cb_add:
  599. tcf_block_owner_del(block, q, ei->binder_type);
  600. err_block_owner_add:
  601. if (created) {
  602. if (tcf_block_shared(block))
  603. tcf_block_remove(block, net);
  604. err_block_insert:
  605. kfree(block);
  606. } else {
  607. block->refcnt--;
  608. }
  609. return err;
  610. }
  611. EXPORT_SYMBOL(tcf_block_get_ext);
  612. static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
  613. {
  614. struct tcf_proto __rcu **p_filter_chain = priv;
  615. rcu_assign_pointer(*p_filter_chain, tp_head);
  616. }
  617. int tcf_block_get(struct tcf_block **p_block,
  618. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  619. struct netlink_ext_ack *extack)
  620. {
  621. struct tcf_block_ext_info ei = {
  622. .chain_head_change = tcf_chain_head_change_dflt,
  623. .chain_head_change_priv = p_filter_chain,
  624. };
  625. WARN_ON(!p_filter_chain);
  626. return tcf_block_get_ext(p_block, q, &ei, extack);
  627. }
  628. EXPORT_SYMBOL(tcf_block_get);
  629. /* XXX: Standalone actions are not allowed to jump to any chain, and bound
  630. * actions should be all removed after flushing.
  631. */
  632. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  633. struct tcf_block_ext_info *ei)
  634. {
  635. struct tcf_chain *chain, *tmp;
  636. if (!block)
  637. return;
  638. tcf_chain0_head_change_cb_del(block, ei);
  639. tcf_block_owner_del(block, q, ei->binder_type);
  640. if (block->refcnt == 1) {
  641. if (tcf_block_shared(block))
  642. tcf_block_remove(block, block->net);
  643. /* Hold a refcnt for all chains, so that they don't disappear
  644. * while we are iterating.
  645. */
  646. list_for_each_entry(chain, &block->chain_list, list)
  647. tcf_chain_hold(chain);
  648. list_for_each_entry(chain, &block->chain_list, list)
  649. tcf_chain_flush(chain);
  650. }
  651. tcf_block_offload_unbind(block, q, ei);
  652. if (block->refcnt == 1) {
  653. /* At this point, all the chains should have refcnt >= 1. */
  654. list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
  655. tcf_chain_put_explicitly_created(chain);
  656. tcf_chain_put(chain);
  657. }
  658. block->refcnt--;
  659. if (list_empty(&block->chain_list))
  660. kfree(block);
  661. } else {
  662. block->refcnt--;
  663. }
  664. }
  665. EXPORT_SYMBOL(tcf_block_put_ext);
  666. void tcf_block_put(struct tcf_block *block)
  667. {
  668. struct tcf_block_ext_info ei = {0, };
  669. if (!block)
  670. return;
  671. tcf_block_put_ext(block, block->q, &ei);
  672. }
  673. EXPORT_SYMBOL(tcf_block_put);
  674. struct tcf_block_cb {
  675. struct list_head list;
  676. tc_setup_cb_t *cb;
  677. void *cb_ident;
  678. void *cb_priv;
  679. unsigned int refcnt;
  680. };
  681. void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
  682. {
  683. return block_cb->cb_priv;
  684. }
  685. EXPORT_SYMBOL(tcf_block_cb_priv);
  686. struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
  687. tc_setup_cb_t *cb, void *cb_ident)
  688. { struct tcf_block_cb *block_cb;
  689. list_for_each_entry(block_cb, &block->cb_list, list)
  690. if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
  691. return block_cb;
  692. return NULL;
  693. }
  694. EXPORT_SYMBOL(tcf_block_cb_lookup);
  695. void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
  696. {
  697. block_cb->refcnt++;
  698. }
  699. EXPORT_SYMBOL(tcf_block_cb_incref);
  700. unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
  701. {
  702. return --block_cb->refcnt;
  703. }
  704. EXPORT_SYMBOL(tcf_block_cb_decref);
  705. static int
  706. tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
  707. void *cb_priv, bool add, bool offload_in_use,
  708. struct netlink_ext_ack *extack)
  709. {
  710. struct tcf_chain *chain;
  711. struct tcf_proto *tp;
  712. int err;
  713. list_for_each_entry(chain, &block->chain_list, list) {
  714. for (tp = rtnl_dereference(chain->filter_chain); tp;
  715. tp = rtnl_dereference(tp->next)) {
  716. if (tp->ops->reoffload) {
  717. err = tp->ops->reoffload(tp, add, cb, cb_priv,
  718. extack);
  719. if (err && add)
  720. goto err_playback_remove;
  721. } else if (add && offload_in_use) {
  722. err = -EOPNOTSUPP;
  723. NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
  724. goto err_playback_remove;
  725. }
  726. }
  727. }
  728. return 0;
  729. err_playback_remove:
  730. tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
  731. extack);
  732. return err;
  733. }
  734. struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
  735. tc_setup_cb_t *cb, void *cb_ident,
  736. void *cb_priv,
  737. struct netlink_ext_ack *extack)
  738. {
  739. struct tcf_block_cb *block_cb;
  740. int err;
  741. /* Replay any already present rules */
  742. err = tcf_block_playback_offloads(block, cb, cb_priv, true,
  743. tcf_block_offload_in_use(block),
  744. extack);
  745. if (err)
  746. return ERR_PTR(err);
  747. block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
  748. if (!block_cb)
  749. return ERR_PTR(-ENOMEM);
  750. block_cb->cb = cb;
  751. block_cb->cb_ident = cb_ident;
  752. block_cb->cb_priv = cb_priv;
  753. list_add(&block_cb->list, &block->cb_list);
  754. return block_cb;
  755. }
  756. EXPORT_SYMBOL(__tcf_block_cb_register);
  757. int tcf_block_cb_register(struct tcf_block *block,
  758. tc_setup_cb_t *cb, void *cb_ident,
  759. void *cb_priv, struct netlink_ext_ack *extack)
  760. {
  761. struct tcf_block_cb *block_cb;
  762. block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
  763. extack);
  764. return PTR_ERR_OR_ZERO(block_cb);
  765. }
  766. EXPORT_SYMBOL(tcf_block_cb_register);
  767. void __tcf_block_cb_unregister(struct tcf_block *block,
  768. struct tcf_block_cb *block_cb)
  769. {
  770. tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
  771. false, tcf_block_offload_in_use(block),
  772. NULL);
  773. list_del(&block_cb->list);
  774. kfree(block_cb);
  775. }
  776. EXPORT_SYMBOL(__tcf_block_cb_unregister);
  777. void tcf_block_cb_unregister(struct tcf_block *block,
  778. tc_setup_cb_t *cb, void *cb_ident)
  779. {
  780. struct tcf_block_cb *block_cb;
  781. block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
  782. if (!block_cb)
  783. return;
  784. __tcf_block_cb_unregister(block, block_cb);
  785. }
  786. EXPORT_SYMBOL(tcf_block_cb_unregister);
  787. static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
  788. void *type_data, bool err_stop)
  789. {
  790. struct tcf_block_cb *block_cb;
  791. int ok_count = 0;
  792. int err;
  793. /* Make sure all netdevs sharing this block are offload-capable. */
  794. if (block->nooffloaddevcnt && err_stop)
  795. return -EOPNOTSUPP;
  796. list_for_each_entry(block_cb, &block->cb_list, list) {
  797. err = block_cb->cb(type, type_data, block_cb->cb_priv);
  798. if (err) {
  799. if (err_stop)
  800. return err;
  801. } else {
  802. ok_count++;
  803. }
  804. }
  805. return ok_count;
  806. }
  807. /* Main classifier routine: scans classifier chain attached
  808. * to this qdisc, (optionally) tests for protocol and asks
  809. * specific classifiers.
  810. */
  811. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  812. struct tcf_result *res, bool compat_mode)
  813. {
  814. __be16 protocol = tc_skb_protocol(skb);
  815. #ifdef CONFIG_NET_CLS_ACT
  816. const int max_reclassify_loop = 4;
  817. const struct tcf_proto *orig_tp = tp;
  818. const struct tcf_proto *first_tp;
  819. int limit = 0;
  820. reclassify:
  821. #endif
  822. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  823. int err;
  824. if (tp->protocol != protocol &&
  825. tp->protocol != htons(ETH_P_ALL))
  826. continue;
  827. err = tp->classify(skb, tp, res);
  828. #ifdef CONFIG_NET_CLS_ACT
  829. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  830. first_tp = orig_tp;
  831. goto reset;
  832. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  833. first_tp = res->goto_tp;
  834. goto reset;
  835. }
  836. #endif
  837. if (err >= 0)
  838. return err;
  839. }
  840. return TC_ACT_UNSPEC; /* signal: continue lookup */
  841. #ifdef CONFIG_NET_CLS_ACT
  842. reset:
  843. if (unlikely(limit++ >= max_reclassify_loop)) {
  844. net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
  845. tp->chain->block->index,
  846. tp->prio & 0xffff,
  847. ntohs(tp->protocol));
  848. return TC_ACT_SHOT;
  849. }
  850. tp = first_tp;
  851. protocol = tc_skb_protocol(skb);
  852. goto reclassify;
  853. #endif
  854. }
  855. EXPORT_SYMBOL(tcf_classify);
  856. struct tcf_chain_info {
  857. struct tcf_proto __rcu **pprev;
  858. struct tcf_proto __rcu *next;
  859. };
  860. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  861. {
  862. return rtnl_dereference(*chain_info->pprev);
  863. }
  864. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  865. struct tcf_chain_info *chain_info,
  866. struct tcf_proto *tp)
  867. {
  868. if (*chain_info->pprev == chain->filter_chain)
  869. tcf_chain0_head_change(chain, tp);
  870. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  871. rcu_assign_pointer(*chain_info->pprev, tp);
  872. tcf_chain_hold(chain);
  873. }
  874. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  875. struct tcf_chain_info *chain_info,
  876. struct tcf_proto *tp)
  877. {
  878. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  879. if (tp == chain->filter_chain)
  880. tcf_chain0_head_change(chain, next);
  881. RCU_INIT_POINTER(*chain_info->pprev, next);
  882. tcf_chain_put(chain);
  883. }
  884. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  885. struct tcf_chain_info *chain_info,
  886. u32 protocol, u32 prio,
  887. bool prio_allocate)
  888. {
  889. struct tcf_proto **pprev;
  890. struct tcf_proto *tp;
  891. /* Check the chain for existence of proto-tcf with this priority */
  892. for (pprev = &chain->filter_chain;
  893. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  894. if (tp->prio >= prio) {
  895. if (tp->prio == prio) {
  896. if (prio_allocate ||
  897. (tp->protocol != protocol && protocol))
  898. return ERR_PTR(-EINVAL);
  899. } else {
  900. tp = NULL;
  901. }
  902. break;
  903. }
  904. }
  905. chain_info->pprev = pprev;
  906. chain_info->next = tp ? tp->next : NULL;
  907. return tp;
  908. }
  909. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  910. struct tcf_proto *tp, struct tcf_block *block,
  911. struct Qdisc *q, u32 parent, void *fh,
  912. u32 portid, u32 seq, u16 flags, int event)
  913. {
  914. struct tcmsg *tcm;
  915. struct nlmsghdr *nlh;
  916. unsigned char *b = skb_tail_pointer(skb);
  917. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  918. if (!nlh)
  919. goto out_nlmsg_trim;
  920. tcm = nlmsg_data(nlh);
  921. tcm->tcm_family = AF_UNSPEC;
  922. tcm->tcm__pad1 = 0;
  923. tcm->tcm__pad2 = 0;
  924. if (q) {
  925. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  926. tcm->tcm_parent = parent;
  927. } else {
  928. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  929. tcm->tcm_block_index = block->index;
  930. }
  931. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  932. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  933. goto nla_put_failure;
  934. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  935. goto nla_put_failure;
  936. if (!fh) {
  937. tcm->tcm_handle = 0;
  938. } else {
  939. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  940. goto nla_put_failure;
  941. }
  942. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  943. return skb->len;
  944. out_nlmsg_trim:
  945. nla_put_failure:
  946. nlmsg_trim(skb, b);
  947. return -1;
  948. }
  949. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  950. struct nlmsghdr *n, struct tcf_proto *tp,
  951. struct tcf_block *block, struct Qdisc *q,
  952. u32 parent, void *fh, int event, bool unicast)
  953. {
  954. struct sk_buff *skb;
  955. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  956. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  957. if (!skb)
  958. return -ENOBUFS;
  959. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  960. n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
  961. kfree_skb(skb);
  962. return -EINVAL;
  963. }
  964. if (unicast)
  965. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  966. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  967. n->nlmsg_flags & NLM_F_ECHO);
  968. }
  969. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  970. struct nlmsghdr *n, struct tcf_proto *tp,
  971. struct tcf_block *block, struct Qdisc *q,
  972. u32 parent, void *fh, bool unicast, bool *last,
  973. struct netlink_ext_ack *extack)
  974. {
  975. struct sk_buff *skb;
  976. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  977. int err;
  978. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  979. if (!skb)
  980. return -ENOBUFS;
  981. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  982. n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
  983. NL_SET_ERR_MSG(extack, "Failed to build del event notification");
  984. kfree_skb(skb);
  985. return -EINVAL;
  986. }
  987. err = tp->ops->delete(tp, fh, last, extack);
  988. if (err) {
  989. kfree_skb(skb);
  990. return err;
  991. }
  992. if (unicast)
  993. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  994. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  995. n->nlmsg_flags & NLM_F_ECHO);
  996. if (err < 0)
  997. NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
  998. return err;
  999. }
  1000. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  1001. struct tcf_block *block, struct Qdisc *q,
  1002. u32 parent, struct nlmsghdr *n,
  1003. struct tcf_chain *chain, int event)
  1004. {
  1005. struct tcf_proto *tp;
  1006. for (tp = rtnl_dereference(chain->filter_chain);
  1007. tp; tp = rtnl_dereference(tp->next))
  1008. tfilter_notify(net, oskb, n, tp, block,
  1009. q, parent, NULL, event, false);
  1010. }
  1011. static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1012. struct netlink_ext_ack *extack)
  1013. {
  1014. struct net *net = sock_net(skb->sk);
  1015. struct nlattr *tca[TCA_MAX + 1];
  1016. struct tcmsg *t;
  1017. u32 protocol;
  1018. u32 prio;
  1019. bool prio_allocate;
  1020. u32 parent;
  1021. u32 chain_index;
  1022. struct Qdisc *q = NULL;
  1023. struct tcf_chain_info chain_info;
  1024. struct tcf_chain *chain = NULL;
  1025. struct tcf_block *block;
  1026. struct tcf_proto *tp;
  1027. unsigned long cl;
  1028. void *fh;
  1029. int err;
  1030. int tp_created;
  1031. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1032. return -EPERM;
  1033. replay:
  1034. tp_created = 0;
  1035. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1036. if (err < 0)
  1037. return err;
  1038. t = nlmsg_data(n);
  1039. protocol = TC_H_MIN(t->tcm_info);
  1040. prio = TC_H_MAJ(t->tcm_info);
  1041. prio_allocate = false;
  1042. parent = t->tcm_parent;
  1043. cl = 0;
  1044. if (prio == 0) {
  1045. /* If no priority is provided by the user,
  1046. * we allocate one.
  1047. */
  1048. if (n->nlmsg_flags & NLM_F_CREATE) {
  1049. prio = TC_H_MAKE(0x80000000U, 0U);
  1050. prio_allocate = true;
  1051. } else {
  1052. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1053. return -ENOENT;
  1054. }
  1055. }
  1056. /* Find head of filter chain. */
  1057. block = tcf_block_find(net, &q, &parent, &cl,
  1058. t->tcm_ifindex, t->tcm_block_index, extack);
  1059. if (IS_ERR(block)) {
  1060. err = PTR_ERR(block);
  1061. goto errout;
  1062. }
  1063. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1064. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1065. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1066. err = -EINVAL;
  1067. goto errout;
  1068. }
  1069. chain = tcf_chain_get(block, chain_index, true);
  1070. if (!chain) {
  1071. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1072. err = -ENOMEM;
  1073. goto errout;
  1074. }
  1075. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1076. prio, prio_allocate);
  1077. if (IS_ERR(tp)) {
  1078. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1079. err = PTR_ERR(tp);
  1080. goto errout;
  1081. }
  1082. if (tp == NULL) {
  1083. /* Proto-tcf does not exist, create new one */
  1084. if (tca[TCA_KIND] == NULL || !protocol) {
  1085. NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
  1086. err = -EINVAL;
  1087. goto errout;
  1088. }
  1089. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1090. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1091. err = -ENOENT;
  1092. goto errout;
  1093. }
  1094. if (prio_allocate)
  1095. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  1096. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  1097. protocol, prio, chain, extack);
  1098. if (IS_ERR(tp)) {
  1099. err = PTR_ERR(tp);
  1100. goto errout;
  1101. }
  1102. tp_created = 1;
  1103. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1104. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1105. err = -EINVAL;
  1106. goto errout;
  1107. }
  1108. fh = tp->ops->get(tp, t->tcm_handle);
  1109. if (!fh) {
  1110. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1111. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1112. err = -ENOENT;
  1113. goto errout;
  1114. }
  1115. } else if (n->nlmsg_flags & NLM_F_EXCL) {
  1116. NL_SET_ERR_MSG(extack, "Filter already exists");
  1117. err = -EEXIST;
  1118. goto errout;
  1119. }
  1120. if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
  1121. NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
  1122. err = -EINVAL;
  1123. goto errout;
  1124. }
  1125. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  1126. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
  1127. extack);
  1128. if (err == 0) {
  1129. if (tp_created)
  1130. tcf_chain_tp_insert(chain, &chain_info, tp);
  1131. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  1132. RTM_NEWTFILTER, false);
  1133. } else {
  1134. if (tp_created)
  1135. tcf_proto_destroy(tp, NULL);
  1136. }
  1137. errout:
  1138. if (chain)
  1139. tcf_chain_put(chain);
  1140. if (err == -EAGAIN)
  1141. /* Replay the request. */
  1142. goto replay;
  1143. return err;
  1144. }
  1145. static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1146. struct netlink_ext_ack *extack)
  1147. {
  1148. struct net *net = sock_net(skb->sk);
  1149. struct nlattr *tca[TCA_MAX + 1];
  1150. struct tcmsg *t;
  1151. u32 protocol;
  1152. u32 prio;
  1153. u32 parent;
  1154. u32 chain_index;
  1155. struct Qdisc *q = NULL;
  1156. struct tcf_chain_info chain_info;
  1157. struct tcf_chain *chain = NULL;
  1158. struct tcf_block *block;
  1159. struct tcf_proto *tp = NULL;
  1160. unsigned long cl = 0;
  1161. void *fh = NULL;
  1162. int err;
  1163. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1164. return -EPERM;
  1165. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1166. if (err < 0)
  1167. return err;
  1168. t = nlmsg_data(n);
  1169. protocol = TC_H_MIN(t->tcm_info);
  1170. prio = TC_H_MAJ(t->tcm_info);
  1171. parent = t->tcm_parent;
  1172. if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
  1173. NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
  1174. return -ENOENT;
  1175. }
  1176. /* Find head of filter chain. */
  1177. block = tcf_block_find(net, &q, &parent, &cl,
  1178. t->tcm_ifindex, t->tcm_block_index, extack);
  1179. if (IS_ERR(block)) {
  1180. err = PTR_ERR(block);
  1181. goto errout;
  1182. }
  1183. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1184. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1185. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1186. err = -EINVAL;
  1187. goto errout;
  1188. }
  1189. chain = tcf_chain_get(block, chain_index, false);
  1190. if (!chain) {
  1191. /* User requested flush on non-existent chain. Nothing to do,
  1192. * so just return success.
  1193. */
  1194. if (prio == 0) {
  1195. err = 0;
  1196. goto errout;
  1197. }
  1198. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1199. err = -EINVAL;
  1200. goto errout;
  1201. }
  1202. if (prio == 0) {
  1203. tfilter_notify_chain(net, skb, block, q, parent, n,
  1204. chain, RTM_DELTFILTER);
  1205. tcf_chain_flush(chain);
  1206. err = 0;
  1207. goto errout;
  1208. }
  1209. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1210. prio, false);
  1211. if (!tp || IS_ERR(tp)) {
  1212. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1213. err = tp ? PTR_ERR(tp) : -ENOENT;
  1214. goto errout;
  1215. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1216. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1217. err = -EINVAL;
  1218. goto errout;
  1219. }
  1220. fh = tp->ops->get(tp, t->tcm_handle);
  1221. if (!fh) {
  1222. if (t->tcm_handle == 0) {
  1223. tcf_chain_tp_remove(chain, &chain_info, tp);
  1224. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  1225. RTM_DELTFILTER, false);
  1226. tcf_proto_destroy(tp, extack);
  1227. err = 0;
  1228. } else {
  1229. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1230. err = -ENOENT;
  1231. }
  1232. } else {
  1233. bool last;
  1234. err = tfilter_del_notify(net, skb, n, tp, block,
  1235. q, parent, fh, false, &last,
  1236. extack);
  1237. if (err)
  1238. goto errout;
  1239. if (last) {
  1240. tcf_chain_tp_remove(chain, &chain_info, tp);
  1241. tcf_proto_destroy(tp, extack);
  1242. }
  1243. }
  1244. errout:
  1245. if (chain)
  1246. tcf_chain_put(chain);
  1247. return err;
  1248. }
  1249. static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1250. struct netlink_ext_ack *extack)
  1251. {
  1252. struct net *net = sock_net(skb->sk);
  1253. struct nlattr *tca[TCA_MAX + 1];
  1254. struct tcmsg *t;
  1255. u32 protocol;
  1256. u32 prio;
  1257. u32 parent;
  1258. u32 chain_index;
  1259. struct Qdisc *q = NULL;
  1260. struct tcf_chain_info chain_info;
  1261. struct tcf_chain *chain = NULL;
  1262. struct tcf_block *block;
  1263. struct tcf_proto *tp = NULL;
  1264. unsigned long cl = 0;
  1265. void *fh = NULL;
  1266. int err;
  1267. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1268. if (err < 0)
  1269. return err;
  1270. t = nlmsg_data(n);
  1271. protocol = TC_H_MIN(t->tcm_info);
  1272. prio = TC_H_MAJ(t->tcm_info);
  1273. parent = t->tcm_parent;
  1274. if (prio == 0) {
  1275. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1276. return -ENOENT;
  1277. }
  1278. /* Find head of filter chain. */
  1279. block = tcf_block_find(net, &q, &parent, &cl,
  1280. t->tcm_ifindex, t->tcm_block_index, extack);
  1281. if (IS_ERR(block)) {
  1282. err = PTR_ERR(block);
  1283. goto errout;
  1284. }
  1285. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1286. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1287. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1288. err = -EINVAL;
  1289. goto errout;
  1290. }
  1291. chain = tcf_chain_get(block, chain_index, false);
  1292. if (!chain) {
  1293. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1294. err = -EINVAL;
  1295. goto errout;
  1296. }
  1297. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1298. prio, false);
  1299. if (!tp || IS_ERR(tp)) {
  1300. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1301. err = tp ? PTR_ERR(tp) : -ENOENT;
  1302. goto errout;
  1303. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1304. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1305. err = -EINVAL;
  1306. goto errout;
  1307. }
  1308. fh = tp->ops->get(tp, t->tcm_handle);
  1309. if (!fh) {
  1310. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1311. err = -ENOENT;
  1312. } else {
  1313. err = tfilter_notify(net, skb, n, tp, block, q, parent,
  1314. fh, RTM_NEWTFILTER, true);
  1315. if (err < 0)
  1316. NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
  1317. }
  1318. errout:
  1319. if (chain)
  1320. tcf_chain_put(chain);
  1321. return err;
  1322. }
  1323. struct tcf_dump_args {
  1324. struct tcf_walker w;
  1325. struct sk_buff *skb;
  1326. struct netlink_callback *cb;
  1327. struct tcf_block *block;
  1328. struct Qdisc *q;
  1329. u32 parent;
  1330. };
  1331. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  1332. {
  1333. struct tcf_dump_args *a = (void *)arg;
  1334. struct net *net = sock_net(a->skb->sk);
  1335. return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
  1336. n, NETLINK_CB(a->cb->skb).portid,
  1337. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1338. RTM_NEWTFILTER);
  1339. }
  1340. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  1341. struct sk_buff *skb, struct netlink_callback *cb,
  1342. long index_start, long *p_index)
  1343. {
  1344. struct net *net = sock_net(skb->sk);
  1345. struct tcf_block *block = chain->block;
  1346. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1347. struct tcf_dump_args arg;
  1348. struct tcf_proto *tp;
  1349. for (tp = rtnl_dereference(chain->filter_chain);
  1350. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  1351. if (*p_index < index_start)
  1352. continue;
  1353. if (TC_H_MAJ(tcm->tcm_info) &&
  1354. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  1355. continue;
  1356. if (TC_H_MIN(tcm->tcm_info) &&
  1357. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  1358. continue;
  1359. if (*p_index > index_start)
  1360. memset(&cb->args[1], 0,
  1361. sizeof(cb->args) - sizeof(cb->args[0]));
  1362. if (cb->args[1] == 0) {
  1363. if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
  1364. NETLINK_CB(cb->skb).portid,
  1365. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1366. RTM_NEWTFILTER) <= 0)
  1367. return false;
  1368. cb->args[1] = 1;
  1369. }
  1370. if (!tp->ops->walk)
  1371. continue;
  1372. arg.w.fn = tcf_node_dump;
  1373. arg.skb = skb;
  1374. arg.cb = cb;
  1375. arg.block = block;
  1376. arg.q = q;
  1377. arg.parent = parent;
  1378. arg.w.stop = 0;
  1379. arg.w.skip = cb->args[1] - 1;
  1380. arg.w.count = 0;
  1381. arg.w.cookie = cb->args[2];
  1382. tp->ops->walk(tp, &arg.w);
  1383. cb->args[2] = arg.w.cookie;
  1384. cb->args[1] = arg.w.count + 1;
  1385. if (arg.w.stop)
  1386. return false;
  1387. }
  1388. return true;
  1389. }
  1390. /* called with RTNL */
  1391. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  1392. {
  1393. struct net *net = sock_net(skb->sk);
  1394. struct nlattr *tca[TCA_MAX + 1];
  1395. struct Qdisc *q = NULL;
  1396. struct tcf_block *block;
  1397. struct tcf_chain *chain;
  1398. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1399. long index_start;
  1400. long index;
  1401. u32 parent;
  1402. int err;
  1403. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1404. return skb->len;
  1405. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1406. if (err)
  1407. return err;
  1408. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1409. block = tcf_block_lookup(net, tcm->tcm_block_index);
  1410. if (!block)
  1411. goto out;
  1412. /* If we work with block index, q is NULL and parent value
  1413. * will never be used in the following code. The check
  1414. * in tcf_fill_node prevents it. However, compiler does not
  1415. * see that far, so set parent to zero to silence the warning
  1416. * about parent being uninitialized.
  1417. */
  1418. parent = 0;
  1419. } else {
  1420. const struct Qdisc_class_ops *cops;
  1421. struct net_device *dev;
  1422. unsigned long cl = 0;
  1423. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1424. if (!dev)
  1425. return skb->len;
  1426. parent = tcm->tcm_parent;
  1427. if (!parent) {
  1428. q = dev->qdisc;
  1429. parent = q->handle;
  1430. } else {
  1431. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  1432. }
  1433. if (!q)
  1434. goto out;
  1435. cops = q->ops->cl_ops;
  1436. if (!cops)
  1437. goto out;
  1438. if (!cops->tcf_block)
  1439. goto out;
  1440. if (TC_H_MIN(tcm->tcm_parent)) {
  1441. cl = cops->find(q, tcm->tcm_parent);
  1442. if (cl == 0)
  1443. goto out;
  1444. }
  1445. block = cops->tcf_block(q, cl, NULL);
  1446. if (!block)
  1447. goto out;
  1448. if (tcf_block_shared(block))
  1449. q = NULL;
  1450. }
  1451. index_start = cb->args[0];
  1452. index = 0;
  1453. list_for_each_entry(chain, &block->chain_list, list) {
  1454. if (tca[TCA_CHAIN] &&
  1455. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  1456. continue;
  1457. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  1458. index_start, &index)) {
  1459. err = -EMSGSIZE;
  1460. break;
  1461. }
  1462. }
  1463. cb->args[0] = index;
  1464. out:
  1465. /* If we did no progress, the error (EMSGSIZE) is real */
  1466. if (skb->len == 0 && err)
  1467. return err;
  1468. return skb->len;
  1469. }
  1470. static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
  1471. struct sk_buff *skb, struct tcf_block *block,
  1472. u32 portid, u32 seq, u16 flags, int event)
  1473. {
  1474. unsigned char *b = skb_tail_pointer(skb);
  1475. const struct tcf_proto_ops *ops;
  1476. struct nlmsghdr *nlh;
  1477. struct tcmsg *tcm;
  1478. void *priv;
  1479. ops = chain->tmplt_ops;
  1480. priv = chain->tmplt_priv;
  1481. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  1482. if (!nlh)
  1483. goto out_nlmsg_trim;
  1484. tcm = nlmsg_data(nlh);
  1485. tcm->tcm_family = AF_UNSPEC;
  1486. tcm->tcm__pad1 = 0;
  1487. tcm->tcm__pad2 = 0;
  1488. tcm->tcm_handle = 0;
  1489. if (block->q) {
  1490. tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
  1491. tcm->tcm_parent = block->q->handle;
  1492. } else {
  1493. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  1494. tcm->tcm_block_index = block->index;
  1495. }
  1496. if (nla_put_u32(skb, TCA_CHAIN, chain->index))
  1497. goto nla_put_failure;
  1498. if (ops) {
  1499. if (nla_put_string(skb, TCA_KIND, ops->kind))
  1500. goto nla_put_failure;
  1501. if (ops->tmplt_dump(skb, net, priv) < 0)
  1502. goto nla_put_failure;
  1503. }
  1504. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1505. return skb->len;
  1506. out_nlmsg_trim:
  1507. nla_put_failure:
  1508. nlmsg_trim(skb, b);
  1509. return -EMSGSIZE;
  1510. }
  1511. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  1512. u32 seq, u16 flags, int event, bool unicast)
  1513. {
  1514. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1515. struct tcf_block *block = chain->block;
  1516. struct net *net = block->net;
  1517. struct sk_buff *skb;
  1518. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1519. if (!skb)
  1520. return -ENOBUFS;
  1521. if (tc_chain_fill_node(chain, net, skb, block, portid,
  1522. seq, flags, event) <= 0) {
  1523. kfree_skb(skb);
  1524. return -EINVAL;
  1525. }
  1526. if (unicast)
  1527. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  1528. return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
  1529. }
  1530. static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  1531. struct nlattr **tca,
  1532. struct netlink_ext_ack *extack)
  1533. {
  1534. const struct tcf_proto_ops *ops;
  1535. void *tmplt_priv;
  1536. /* If kind is not set, user did not specify template. */
  1537. if (!tca[TCA_KIND])
  1538. return 0;
  1539. ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
  1540. if (IS_ERR(ops))
  1541. return PTR_ERR(ops);
  1542. if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
  1543. NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
  1544. return -EOPNOTSUPP;
  1545. }
  1546. tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
  1547. if (IS_ERR(tmplt_priv)) {
  1548. module_put(ops->owner);
  1549. return PTR_ERR(tmplt_priv);
  1550. }
  1551. chain->tmplt_ops = ops;
  1552. chain->tmplt_priv = tmplt_priv;
  1553. return 0;
  1554. }
  1555. static void tc_chain_tmplt_del(struct tcf_chain *chain)
  1556. {
  1557. const struct tcf_proto_ops *ops = chain->tmplt_ops;
  1558. /* If template ops are set, no work to do for us. */
  1559. if (!ops)
  1560. return;
  1561. ops->tmplt_destroy(chain->tmplt_priv);
  1562. module_put(ops->owner);
  1563. }
  1564. /* Add/delete/get a chain */
  1565. static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
  1566. struct netlink_ext_ack *extack)
  1567. {
  1568. struct net *net = sock_net(skb->sk);
  1569. struct nlattr *tca[TCA_MAX + 1];
  1570. struct tcmsg *t;
  1571. u32 parent;
  1572. u32 chain_index;
  1573. struct Qdisc *q = NULL;
  1574. struct tcf_chain *chain = NULL;
  1575. struct tcf_block *block;
  1576. unsigned long cl;
  1577. int err;
  1578. if (n->nlmsg_type != RTM_GETCHAIN &&
  1579. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1580. return -EPERM;
  1581. replay:
  1582. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1583. if (err < 0)
  1584. return err;
  1585. t = nlmsg_data(n);
  1586. parent = t->tcm_parent;
  1587. cl = 0;
  1588. block = tcf_block_find(net, &q, &parent, &cl,
  1589. t->tcm_ifindex, t->tcm_block_index, extack);
  1590. if (IS_ERR(block))
  1591. return PTR_ERR(block);
  1592. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1593. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1594. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1595. return -EINVAL;
  1596. }
  1597. chain = tcf_chain_lookup(block, chain_index);
  1598. if (n->nlmsg_type == RTM_NEWCHAIN) {
  1599. if (chain) {
  1600. if (tcf_chain_held_by_acts_only(chain)) {
  1601. /* The chain exists only because there is
  1602. * some action referencing it.
  1603. */
  1604. tcf_chain_hold(chain);
  1605. } else {
  1606. NL_SET_ERR_MSG(extack, "Filter chain already exists");
  1607. return -EEXIST;
  1608. }
  1609. } else {
  1610. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1611. NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
  1612. return -ENOENT;
  1613. }
  1614. chain = tcf_chain_create(block, chain_index);
  1615. if (!chain) {
  1616. NL_SET_ERR_MSG(extack, "Failed to create filter chain");
  1617. return -ENOMEM;
  1618. }
  1619. }
  1620. } else {
  1621. if (!chain || tcf_chain_held_by_acts_only(chain)) {
  1622. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1623. return -EINVAL;
  1624. }
  1625. tcf_chain_hold(chain);
  1626. }
  1627. switch (n->nlmsg_type) {
  1628. case RTM_NEWCHAIN:
  1629. err = tc_chain_tmplt_add(chain, net, tca, extack);
  1630. if (err)
  1631. goto errout;
  1632. /* In case the chain was successfully added, take a reference
  1633. * to the chain. This ensures that an empty chain
  1634. * does not disappear at the end of this function.
  1635. */
  1636. tcf_chain_hold(chain);
  1637. chain->explicitly_created = true;
  1638. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  1639. RTM_NEWCHAIN, false);
  1640. break;
  1641. case RTM_DELCHAIN:
  1642. /* Flush the chain first as the user requested chain removal. */
  1643. tcf_chain_flush(chain);
  1644. /* In case the chain was successfully deleted, put a reference
  1645. * to the chain previously taken during addition.
  1646. */
  1647. tcf_chain_put_explicitly_created(chain);
  1648. chain->explicitly_created = false;
  1649. break;
  1650. case RTM_GETCHAIN:
  1651. err = tc_chain_notify(chain, skb, n->nlmsg_seq,
  1652. n->nlmsg_seq, n->nlmsg_type, true);
  1653. if (err < 0)
  1654. NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
  1655. break;
  1656. default:
  1657. err = -EOPNOTSUPP;
  1658. NL_SET_ERR_MSG(extack, "Unsupported message type");
  1659. goto errout;
  1660. }
  1661. errout:
  1662. tcf_chain_put(chain);
  1663. if (err == -EAGAIN)
  1664. /* Replay the request. */
  1665. goto replay;
  1666. return err;
  1667. }
  1668. /* called with RTNL */
  1669. static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
  1670. {
  1671. struct net *net = sock_net(skb->sk);
  1672. struct nlattr *tca[TCA_MAX + 1];
  1673. struct Qdisc *q = NULL;
  1674. struct tcf_block *block;
  1675. struct tcf_chain *chain;
  1676. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1677. long index_start;
  1678. long index;
  1679. u32 parent;
  1680. int err;
  1681. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1682. return skb->len;
  1683. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1684. if (err)
  1685. return err;
  1686. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1687. block = tcf_block_lookup(net, tcm->tcm_block_index);
  1688. if (!block)
  1689. goto out;
  1690. /* If we work with block index, q is NULL and parent value
  1691. * will never be used in the following code. The check
  1692. * in tcf_fill_node prevents it. However, compiler does not
  1693. * see that far, so set parent to zero to silence the warning
  1694. * about parent being uninitialized.
  1695. */
  1696. parent = 0;
  1697. } else {
  1698. const struct Qdisc_class_ops *cops;
  1699. struct net_device *dev;
  1700. unsigned long cl = 0;
  1701. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1702. if (!dev)
  1703. return skb->len;
  1704. parent = tcm->tcm_parent;
  1705. if (!parent) {
  1706. q = dev->qdisc;
  1707. parent = q->handle;
  1708. } else {
  1709. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  1710. }
  1711. if (!q)
  1712. goto out;
  1713. cops = q->ops->cl_ops;
  1714. if (!cops)
  1715. goto out;
  1716. if (!cops->tcf_block)
  1717. goto out;
  1718. if (TC_H_MIN(tcm->tcm_parent)) {
  1719. cl = cops->find(q, tcm->tcm_parent);
  1720. if (cl == 0)
  1721. goto out;
  1722. }
  1723. block = cops->tcf_block(q, cl, NULL);
  1724. if (!block)
  1725. goto out;
  1726. if (tcf_block_shared(block))
  1727. q = NULL;
  1728. }
  1729. index_start = cb->args[0];
  1730. index = 0;
  1731. list_for_each_entry(chain, &block->chain_list, list) {
  1732. if ((tca[TCA_CHAIN] &&
  1733. nla_get_u32(tca[TCA_CHAIN]) != chain->index))
  1734. continue;
  1735. if (index < index_start) {
  1736. index++;
  1737. continue;
  1738. }
  1739. if (tcf_chain_held_by_acts_only(chain))
  1740. continue;
  1741. err = tc_chain_fill_node(chain, net, skb, block,
  1742. NETLINK_CB(cb->skb).portid,
  1743. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1744. RTM_NEWCHAIN);
  1745. if (err <= 0)
  1746. break;
  1747. index++;
  1748. }
  1749. cb->args[0] = index;
  1750. out:
  1751. /* If we did no progress, the error (EMSGSIZE) is real */
  1752. if (skb->len == 0 && err)
  1753. return err;
  1754. return skb->len;
  1755. }
  1756. void tcf_exts_destroy(struct tcf_exts *exts)
  1757. {
  1758. #ifdef CONFIG_NET_CLS_ACT
  1759. tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
  1760. kfree(exts->actions);
  1761. exts->nr_actions = 0;
  1762. #endif
  1763. }
  1764. EXPORT_SYMBOL(tcf_exts_destroy);
  1765. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  1766. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
  1767. struct netlink_ext_ack *extack)
  1768. {
  1769. #ifdef CONFIG_NET_CLS_ACT
  1770. {
  1771. struct tc_action *act;
  1772. size_t attr_size = 0;
  1773. if (exts->police && tb[exts->police]) {
  1774. act = tcf_action_init_1(net, tp, tb[exts->police],
  1775. rate_tlv, "police", ovr,
  1776. TCA_ACT_BIND, true, extack);
  1777. if (IS_ERR(act))
  1778. return PTR_ERR(act);
  1779. act->type = exts->type = TCA_OLD_COMPAT;
  1780. exts->actions[0] = act;
  1781. exts->nr_actions = 1;
  1782. } else if (exts->action && tb[exts->action]) {
  1783. int err;
  1784. err = tcf_action_init(net, tp, tb[exts->action],
  1785. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  1786. exts->actions, &attr_size, true,
  1787. extack);
  1788. if (err < 0)
  1789. return err;
  1790. exts->nr_actions = err;
  1791. }
  1792. exts->net = net;
  1793. }
  1794. #else
  1795. if ((exts->action && tb[exts->action]) ||
  1796. (exts->police && tb[exts->police])) {
  1797. NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
  1798. return -EOPNOTSUPP;
  1799. }
  1800. #endif
  1801. return 0;
  1802. }
  1803. EXPORT_SYMBOL(tcf_exts_validate);
  1804. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  1805. {
  1806. #ifdef CONFIG_NET_CLS_ACT
  1807. struct tcf_exts old = *dst;
  1808. *dst = *src;
  1809. tcf_exts_destroy(&old);
  1810. #endif
  1811. }
  1812. EXPORT_SYMBOL(tcf_exts_change);
  1813. #ifdef CONFIG_NET_CLS_ACT
  1814. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  1815. {
  1816. if (exts->nr_actions == 0)
  1817. return NULL;
  1818. else
  1819. return exts->actions[0];
  1820. }
  1821. #endif
  1822. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  1823. {
  1824. #ifdef CONFIG_NET_CLS_ACT
  1825. struct nlattr *nest;
  1826. if (exts->action && tcf_exts_has_actions(exts)) {
  1827. /*
  1828. * again for backward compatible mode - we want
  1829. * to work with both old and new modes of entering
  1830. * tc data even if iproute2 was newer - jhs
  1831. */
  1832. if (exts->type != TCA_OLD_COMPAT) {
  1833. nest = nla_nest_start(skb, exts->action);
  1834. if (nest == NULL)
  1835. goto nla_put_failure;
  1836. if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
  1837. goto nla_put_failure;
  1838. nla_nest_end(skb, nest);
  1839. } else if (exts->police) {
  1840. struct tc_action *act = tcf_exts_first_act(exts);
  1841. nest = nla_nest_start(skb, exts->police);
  1842. if (nest == NULL || !act)
  1843. goto nla_put_failure;
  1844. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  1845. goto nla_put_failure;
  1846. nla_nest_end(skb, nest);
  1847. }
  1848. }
  1849. return 0;
  1850. nla_put_failure:
  1851. nla_nest_cancel(skb, nest);
  1852. return -1;
  1853. #else
  1854. return 0;
  1855. #endif
  1856. }
  1857. EXPORT_SYMBOL(tcf_exts_dump);
  1858. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  1859. {
  1860. #ifdef CONFIG_NET_CLS_ACT
  1861. struct tc_action *a = tcf_exts_first_act(exts);
  1862. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  1863. return -1;
  1864. #endif
  1865. return 0;
  1866. }
  1867. EXPORT_SYMBOL(tcf_exts_dump_stats);
  1868. static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
  1869. enum tc_setup_type type,
  1870. void *type_data, bool err_stop)
  1871. {
  1872. int ok_count = 0;
  1873. #ifdef CONFIG_NET_CLS_ACT
  1874. const struct tc_action *a;
  1875. struct net_device *dev;
  1876. int i, ret;
  1877. if (!tcf_exts_has_actions(exts))
  1878. return 0;
  1879. for (i = 0; i < exts->nr_actions; i++) {
  1880. a = exts->actions[i];
  1881. if (!a->ops->get_dev)
  1882. continue;
  1883. dev = a->ops->get_dev(a);
  1884. if (!dev)
  1885. continue;
  1886. ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
  1887. a->ops->put_dev(dev);
  1888. if (ret < 0)
  1889. return ret;
  1890. ok_count += ret;
  1891. }
  1892. #endif
  1893. return ok_count;
  1894. }
  1895. int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
  1896. enum tc_setup_type type, void *type_data, bool err_stop)
  1897. {
  1898. int ok_count;
  1899. int ret;
  1900. ret = tcf_block_cb_call(block, type, type_data, err_stop);
  1901. if (ret < 0)
  1902. return ret;
  1903. ok_count = ret;
  1904. if (!exts || ok_count)
  1905. return ok_count;
  1906. ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
  1907. if (ret < 0)
  1908. return ret;
  1909. ok_count += ret;
  1910. return ok_count;
  1911. }
  1912. EXPORT_SYMBOL(tc_setup_cb_call);
  1913. static __net_init int tcf_net_init(struct net *net)
  1914. {
  1915. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1916. idr_init(&tn->idr);
  1917. return 0;
  1918. }
  1919. static void __net_exit tcf_net_exit(struct net *net)
  1920. {
  1921. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1922. idr_destroy(&tn->idr);
  1923. }
  1924. static struct pernet_operations tcf_net_ops = {
  1925. .init = tcf_net_init,
  1926. .exit = tcf_net_exit,
  1927. .id = &tcf_net_id,
  1928. .size = sizeof(struct tcf_net),
  1929. };
  1930. static int __init tc_filter_init(void)
  1931. {
  1932. int err;
  1933. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  1934. if (!tc_filter_wq)
  1935. return -ENOMEM;
  1936. err = register_pernet_subsys(&tcf_net_ops);
  1937. if (err)
  1938. goto err_register_pernet_subsys;
  1939. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
  1940. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
  1941. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
  1942. tc_dump_tfilter, 0);
  1943. rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
  1944. rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
  1945. rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
  1946. tc_dump_chain, 0);
  1947. return 0;
  1948. err_register_pernet_subsys:
  1949. destroy_workqueue(tc_filter_wq);
  1950. return err;
  1951. }
  1952. subsys_initcall(tc_filter_init);