cls_api.c 54 KB

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