cls_api.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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 bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
  227. {
  228. /* In case all the references are action references, this
  229. * chain should not be shown to the user.
  230. */
  231. return chain->refcnt == chain->action_refcnt;
  232. }
  233. static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
  234. u32 chain_index)
  235. {
  236. struct tcf_chain *chain;
  237. list_for_each_entry(chain, &block->chain_list, list) {
  238. if (chain->index == chain_index)
  239. return chain;
  240. }
  241. return NULL;
  242. }
  243. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  244. u32 seq, u16 flags, int event, bool unicast);
  245. static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
  246. u32 chain_index, bool create,
  247. bool by_act)
  248. {
  249. struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
  250. if (chain) {
  251. tcf_chain_hold(chain);
  252. } else {
  253. if (!create)
  254. return NULL;
  255. chain = tcf_chain_create(block, chain_index);
  256. if (!chain)
  257. return NULL;
  258. }
  259. if (by_act)
  260. ++chain->action_refcnt;
  261. /* Send notification only in case we got the first
  262. * non-action reference. Until then, the chain acts only as
  263. * a placeholder for actions pointing to it and user ought
  264. * not know about them.
  265. */
  266. if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
  267. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  268. RTM_NEWCHAIN, false);
  269. return chain;
  270. }
  271. struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  272. bool create)
  273. {
  274. return __tcf_chain_get(block, chain_index, create, false);
  275. }
  276. EXPORT_SYMBOL(tcf_chain_get);
  277. struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
  278. {
  279. return __tcf_chain_get(block, chain_index, true, true);
  280. }
  281. EXPORT_SYMBOL(tcf_chain_get_by_act);
  282. static void tc_chain_tmplt_del(struct tcf_chain *chain);
  283. static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
  284. {
  285. if (by_act)
  286. chain->action_refcnt--;
  287. chain->refcnt--;
  288. /* The last dropped non-action reference will trigger notification. */
  289. if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
  290. tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
  291. if (chain->refcnt == 0) {
  292. tc_chain_tmplt_del(chain);
  293. tcf_chain_destroy(chain);
  294. }
  295. }
  296. void tcf_chain_put(struct tcf_chain *chain)
  297. {
  298. __tcf_chain_put(chain, false);
  299. }
  300. EXPORT_SYMBOL(tcf_chain_put);
  301. void tcf_chain_put_by_act(struct tcf_chain *chain)
  302. {
  303. __tcf_chain_put(chain, true);
  304. }
  305. EXPORT_SYMBOL(tcf_chain_put_by_act);
  306. static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
  307. {
  308. if (chain->explicitly_created)
  309. tcf_chain_put(chain);
  310. }
  311. static bool tcf_block_offload_in_use(struct tcf_block *block)
  312. {
  313. return block->offloadcnt;
  314. }
  315. static int tcf_block_offload_cmd(struct tcf_block *block,
  316. struct net_device *dev,
  317. struct tcf_block_ext_info *ei,
  318. enum tc_block_command command,
  319. struct netlink_ext_ack *extack)
  320. {
  321. struct tc_block_offload bo = {};
  322. bo.command = command;
  323. bo.binder_type = ei->binder_type;
  324. bo.block = block;
  325. bo.extack = extack;
  326. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
  327. }
  328. static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
  329. struct tcf_block_ext_info *ei,
  330. struct netlink_ext_ack *extack)
  331. {
  332. struct net_device *dev = q->dev_queue->dev;
  333. int err;
  334. if (!dev->netdev_ops->ndo_setup_tc)
  335. goto no_offload_dev_inc;
  336. /* If tc offload feature is disabled and the block we try to bind
  337. * to already has some offloaded filters, forbid to bind.
  338. */
  339. if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
  340. NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
  341. return -EOPNOTSUPP;
  342. }
  343. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
  344. if (err == -EOPNOTSUPP)
  345. goto no_offload_dev_inc;
  346. return err;
  347. no_offload_dev_inc:
  348. if (tcf_block_offload_in_use(block))
  349. return -EOPNOTSUPP;
  350. block->nooffloaddevcnt++;
  351. return 0;
  352. }
  353. static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
  354. struct tcf_block_ext_info *ei)
  355. {
  356. struct net_device *dev = q->dev_queue->dev;
  357. int err;
  358. if (!dev->netdev_ops->ndo_setup_tc)
  359. goto no_offload_dev_dec;
  360. err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
  361. if (err == -EOPNOTSUPP)
  362. goto no_offload_dev_dec;
  363. return;
  364. no_offload_dev_dec:
  365. WARN_ON(block->nooffloaddevcnt-- == 0);
  366. }
  367. static int
  368. tcf_chain0_head_change_cb_add(struct tcf_block *block,
  369. struct tcf_block_ext_info *ei,
  370. struct netlink_ext_ack *extack)
  371. {
  372. struct tcf_chain *chain0 = block->chain0.chain;
  373. struct tcf_filter_chain_list_item *item;
  374. item = kmalloc(sizeof(*item), GFP_KERNEL);
  375. if (!item) {
  376. NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
  377. return -ENOMEM;
  378. }
  379. item->chain_head_change = ei->chain_head_change;
  380. item->chain_head_change_priv = ei->chain_head_change_priv;
  381. if (chain0 && chain0->filter_chain)
  382. tcf_chain_head_change_item(item, chain0->filter_chain);
  383. list_add(&item->list, &block->chain0.filter_chain_list);
  384. return 0;
  385. }
  386. static void
  387. tcf_chain0_head_change_cb_del(struct tcf_block *block,
  388. struct tcf_block_ext_info *ei)
  389. {
  390. struct tcf_chain *chain0 = block->chain0.chain;
  391. struct tcf_filter_chain_list_item *item;
  392. list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
  393. if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
  394. (item->chain_head_change == ei->chain_head_change &&
  395. item->chain_head_change_priv == ei->chain_head_change_priv)) {
  396. if (chain0)
  397. tcf_chain_head_change_item(item, NULL);
  398. list_del(&item->list);
  399. kfree(item);
  400. return;
  401. }
  402. }
  403. WARN_ON(1);
  404. }
  405. struct tcf_net {
  406. struct idr idr;
  407. };
  408. static unsigned int tcf_net_id;
  409. static int tcf_block_insert(struct tcf_block *block, struct net *net,
  410. struct netlink_ext_ack *extack)
  411. {
  412. struct tcf_net *tn = net_generic(net, tcf_net_id);
  413. return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
  414. GFP_KERNEL);
  415. }
  416. static void tcf_block_remove(struct tcf_block *block, struct net *net)
  417. {
  418. struct tcf_net *tn = net_generic(net, tcf_net_id);
  419. idr_remove(&tn->idr, block->index);
  420. }
  421. static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
  422. u32 block_index,
  423. struct netlink_ext_ack *extack)
  424. {
  425. struct tcf_block *block;
  426. block = kzalloc(sizeof(*block), GFP_KERNEL);
  427. if (!block) {
  428. NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
  429. return ERR_PTR(-ENOMEM);
  430. }
  431. INIT_LIST_HEAD(&block->chain_list);
  432. INIT_LIST_HEAD(&block->cb_list);
  433. INIT_LIST_HEAD(&block->owner_list);
  434. INIT_LIST_HEAD(&block->chain0.filter_chain_list);
  435. block->refcnt = 1;
  436. block->net = net;
  437. block->index = block_index;
  438. /* Don't store q pointer for blocks which are shared */
  439. if (!tcf_block_shared(block))
  440. block->q = q;
  441. return block;
  442. }
  443. static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
  444. {
  445. struct tcf_net *tn = net_generic(net, tcf_net_id);
  446. return idr_find(&tn->idr, block_index);
  447. }
  448. /* Find tcf block.
  449. * Set q, parent, cl when appropriate.
  450. */
  451. static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
  452. u32 *parent, unsigned long *cl,
  453. int ifindex, u32 block_index,
  454. struct netlink_ext_ack *extack)
  455. {
  456. struct tcf_block *block;
  457. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  458. block = tcf_block_lookup(net, block_index);
  459. if (!block) {
  460. NL_SET_ERR_MSG(extack, "Block of given index was not found");
  461. return ERR_PTR(-EINVAL);
  462. }
  463. } else {
  464. const struct Qdisc_class_ops *cops;
  465. struct net_device *dev;
  466. /* Find link */
  467. dev = __dev_get_by_index(net, ifindex);
  468. if (!dev)
  469. return ERR_PTR(-ENODEV);
  470. /* Find qdisc */
  471. if (!*parent) {
  472. *q = dev->qdisc;
  473. *parent = (*q)->handle;
  474. } else {
  475. *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
  476. if (!*q) {
  477. NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
  478. return ERR_PTR(-EINVAL);
  479. }
  480. }
  481. /* Is it classful? */
  482. cops = (*q)->ops->cl_ops;
  483. if (!cops) {
  484. NL_SET_ERR_MSG(extack, "Qdisc not classful");
  485. return ERR_PTR(-EINVAL);
  486. }
  487. if (!cops->tcf_block) {
  488. NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
  489. return ERR_PTR(-EOPNOTSUPP);
  490. }
  491. /* Do we search for filter, attached to class? */
  492. if (TC_H_MIN(*parent)) {
  493. *cl = cops->find(*q, *parent);
  494. if (*cl == 0) {
  495. NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
  496. return ERR_PTR(-ENOENT);
  497. }
  498. }
  499. /* And the last stroke */
  500. block = cops->tcf_block(*q, *cl, extack);
  501. if (!block)
  502. return ERR_PTR(-EINVAL);
  503. if (tcf_block_shared(block)) {
  504. NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
  505. return ERR_PTR(-EOPNOTSUPP);
  506. }
  507. }
  508. return block;
  509. }
  510. struct tcf_block_owner_item {
  511. struct list_head list;
  512. struct Qdisc *q;
  513. enum tcf_block_binder_type binder_type;
  514. };
  515. static void
  516. tcf_block_owner_netif_keep_dst(struct tcf_block *block,
  517. struct Qdisc *q,
  518. enum tcf_block_binder_type binder_type)
  519. {
  520. if (block->keep_dst &&
  521. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
  522. binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
  523. netif_keep_dst(qdisc_dev(q));
  524. }
  525. void tcf_block_netif_keep_dst(struct tcf_block *block)
  526. {
  527. struct tcf_block_owner_item *item;
  528. block->keep_dst = true;
  529. list_for_each_entry(item, &block->owner_list, list)
  530. tcf_block_owner_netif_keep_dst(block, item->q,
  531. item->binder_type);
  532. }
  533. EXPORT_SYMBOL(tcf_block_netif_keep_dst);
  534. static int tcf_block_owner_add(struct tcf_block *block,
  535. struct Qdisc *q,
  536. enum tcf_block_binder_type binder_type)
  537. {
  538. struct tcf_block_owner_item *item;
  539. item = kmalloc(sizeof(*item), GFP_KERNEL);
  540. if (!item)
  541. return -ENOMEM;
  542. item->q = q;
  543. item->binder_type = binder_type;
  544. list_add(&item->list, &block->owner_list);
  545. return 0;
  546. }
  547. static void tcf_block_owner_del(struct tcf_block *block,
  548. struct Qdisc *q,
  549. enum tcf_block_binder_type binder_type)
  550. {
  551. struct tcf_block_owner_item *item;
  552. list_for_each_entry(item, &block->owner_list, list) {
  553. if (item->q == q && item->binder_type == binder_type) {
  554. list_del(&item->list);
  555. kfree(item);
  556. return;
  557. }
  558. }
  559. WARN_ON(1);
  560. }
  561. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  562. struct tcf_block_ext_info *ei,
  563. struct netlink_ext_ack *extack)
  564. {
  565. struct net *net = qdisc_net(q);
  566. struct tcf_block *block = NULL;
  567. bool created = false;
  568. int err;
  569. if (ei->block_index) {
  570. /* block_index not 0 means the shared block is requested */
  571. block = tcf_block_lookup(net, ei->block_index);
  572. if (block)
  573. block->refcnt++;
  574. }
  575. if (!block) {
  576. block = tcf_block_create(net, q, ei->block_index, extack);
  577. if (IS_ERR(block))
  578. return PTR_ERR(block);
  579. created = true;
  580. if (tcf_block_shared(block)) {
  581. err = tcf_block_insert(block, net, extack);
  582. if (err)
  583. goto err_block_insert;
  584. }
  585. }
  586. err = tcf_block_owner_add(block, q, ei->binder_type);
  587. if (err)
  588. goto err_block_owner_add;
  589. tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
  590. err = tcf_chain0_head_change_cb_add(block, ei, extack);
  591. if (err)
  592. goto err_chain0_head_change_cb_add;
  593. err = tcf_block_offload_bind(block, q, ei, extack);
  594. if (err)
  595. goto err_block_offload_bind;
  596. *p_block = block;
  597. return 0;
  598. err_block_offload_bind:
  599. tcf_chain0_head_change_cb_del(block, ei);
  600. err_chain0_head_change_cb_add:
  601. tcf_block_owner_del(block, q, ei->binder_type);
  602. err_block_owner_add:
  603. if (created) {
  604. if (tcf_block_shared(block))
  605. tcf_block_remove(block, net);
  606. err_block_insert:
  607. kfree(block);
  608. } else {
  609. block->refcnt--;
  610. }
  611. return err;
  612. }
  613. EXPORT_SYMBOL(tcf_block_get_ext);
  614. static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
  615. {
  616. struct tcf_proto __rcu **p_filter_chain = priv;
  617. rcu_assign_pointer(*p_filter_chain, tp_head);
  618. }
  619. int tcf_block_get(struct tcf_block **p_block,
  620. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  621. struct netlink_ext_ack *extack)
  622. {
  623. struct tcf_block_ext_info ei = {
  624. .chain_head_change = tcf_chain_head_change_dflt,
  625. .chain_head_change_priv = p_filter_chain,
  626. };
  627. WARN_ON(!p_filter_chain);
  628. return tcf_block_get_ext(p_block, q, &ei, extack);
  629. }
  630. EXPORT_SYMBOL(tcf_block_get);
  631. /* XXX: Standalone actions are not allowed to jump to any chain, and bound
  632. * actions should be all removed after flushing.
  633. */
  634. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  635. struct tcf_block_ext_info *ei)
  636. {
  637. struct tcf_chain *chain, *tmp;
  638. if (!block)
  639. return;
  640. tcf_chain0_head_change_cb_del(block, ei);
  641. tcf_block_owner_del(block, q, ei->binder_type);
  642. if (block->refcnt == 1) {
  643. if (tcf_block_shared(block))
  644. tcf_block_remove(block, block->net);
  645. /* Hold a refcnt for all chains, so that they don't disappear
  646. * while we are iterating.
  647. */
  648. list_for_each_entry(chain, &block->chain_list, list)
  649. tcf_chain_hold(chain);
  650. list_for_each_entry(chain, &block->chain_list, list)
  651. tcf_chain_flush(chain);
  652. }
  653. tcf_block_offload_unbind(block, q, ei);
  654. if (block->refcnt == 1) {
  655. /* At this point, all the chains should have refcnt >= 1. */
  656. list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
  657. tcf_chain_put_explicitly_created(chain);
  658. tcf_chain_put(chain);
  659. }
  660. block->refcnt--;
  661. if (list_empty(&block->chain_list))
  662. kfree(block);
  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. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1192. err = -EINVAL;
  1193. goto errout;
  1194. }
  1195. if (prio == 0) {
  1196. tfilter_notify_chain(net, skb, block, q, parent, n,
  1197. chain, RTM_DELTFILTER);
  1198. tcf_chain_flush(chain);
  1199. err = 0;
  1200. goto errout;
  1201. }
  1202. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1203. prio, false);
  1204. if (!tp || IS_ERR(tp)) {
  1205. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1206. err = tp ? PTR_ERR(tp) : -ENOENT;
  1207. goto errout;
  1208. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1209. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1210. err = -EINVAL;
  1211. goto errout;
  1212. }
  1213. fh = tp->ops->get(tp, t->tcm_handle);
  1214. if (!fh) {
  1215. if (t->tcm_handle == 0) {
  1216. tcf_chain_tp_remove(chain, &chain_info, tp);
  1217. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  1218. RTM_DELTFILTER, false);
  1219. tcf_proto_destroy(tp, extack);
  1220. err = 0;
  1221. } else {
  1222. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1223. err = -ENOENT;
  1224. }
  1225. } else {
  1226. bool last;
  1227. err = tfilter_del_notify(net, skb, n, tp, block,
  1228. q, parent, fh, false, &last,
  1229. extack);
  1230. if (err)
  1231. goto errout;
  1232. if (last) {
  1233. tcf_chain_tp_remove(chain, &chain_info, tp);
  1234. tcf_proto_destroy(tp, extack);
  1235. }
  1236. }
  1237. errout:
  1238. if (chain)
  1239. tcf_chain_put(chain);
  1240. return err;
  1241. }
  1242. static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1243. struct netlink_ext_ack *extack)
  1244. {
  1245. struct net *net = sock_net(skb->sk);
  1246. struct nlattr *tca[TCA_MAX + 1];
  1247. struct tcmsg *t;
  1248. u32 protocol;
  1249. u32 prio;
  1250. u32 parent;
  1251. u32 chain_index;
  1252. struct Qdisc *q = NULL;
  1253. struct tcf_chain_info chain_info;
  1254. struct tcf_chain *chain = NULL;
  1255. struct tcf_block *block;
  1256. struct tcf_proto *tp = NULL;
  1257. unsigned long cl = 0;
  1258. void *fh = NULL;
  1259. int err;
  1260. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1261. if (err < 0)
  1262. return err;
  1263. t = nlmsg_data(n);
  1264. protocol = TC_H_MIN(t->tcm_info);
  1265. prio = TC_H_MAJ(t->tcm_info);
  1266. parent = t->tcm_parent;
  1267. if (prio == 0) {
  1268. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1269. return -ENOENT;
  1270. }
  1271. /* Find head of filter chain. */
  1272. block = tcf_block_find(net, &q, &parent, &cl,
  1273. t->tcm_ifindex, t->tcm_block_index, extack);
  1274. if (IS_ERR(block)) {
  1275. err = PTR_ERR(block);
  1276. goto errout;
  1277. }
  1278. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1279. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1280. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1281. err = -EINVAL;
  1282. goto errout;
  1283. }
  1284. chain = tcf_chain_get(block, chain_index, false);
  1285. if (!chain) {
  1286. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1287. err = -EINVAL;
  1288. goto errout;
  1289. }
  1290. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1291. prio, false);
  1292. if (!tp || IS_ERR(tp)) {
  1293. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1294. err = tp ? PTR_ERR(tp) : -ENOENT;
  1295. goto errout;
  1296. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1297. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1298. err = -EINVAL;
  1299. goto errout;
  1300. }
  1301. fh = tp->ops->get(tp, t->tcm_handle);
  1302. if (!fh) {
  1303. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  1304. err = -ENOENT;
  1305. } else {
  1306. err = tfilter_notify(net, skb, n, tp, block, q, parent,
  1307. fh, RTM_NEWTFILTER, true);
  1308. if (err < 0)
  1309. NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
  1310. }
  1311. errout:
  1312. if (chain)
  1313. tcf_chain_put(chain);
  1314. return err;
  1315. }
  1316. struct tcf_dump_args {
  1317. struct tcf_walker w;
  1318. struct sk_buff *skb;
  1319. struct netlink_callback *cb;
  1320. struct tcf_block *block;
  1321. struct Qdisc *q;
  1322. u32 parent;
  1323. };
  1324. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  1325. {
  1326. struct tcf_dump_args *a = (void *)arg;
  1327. struct net *net = sock_net(a->skb->sk);
  1328. return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
  1329. n, NETLINK_CB(a->cb->skb).portid,
  1330. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1331. RTM_NEWTFILTER);
  1332. }
  1333. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  1334. struct sk_buff *skb, struct netlink_callback *cb,
  1335. long index_start, long *p_index)
  1336. {
  1337. struct net *net = sock_net(skb->sk);
  1338. struct tcf_block *block = chain->block;
  1339. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1340. struct tcf_dump_args arg;
  1341. struct tcf_proto *tp;
  1342. for (tp = rtnl_dereference(chain->filter_chain);
  1343. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  1344. if (*p_index < index_start)
  1345. continue;
  1346. if (TC_H_MAJ(tcm->tcm_info) &&
  1347. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  1348. continue;
  1349. if (TC_H_MIN(tcm->tcm_info) &&
  1350. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  1351. continue;
  1352. if (*p_index > index_start)
  1353. memset(&cb->args[1], 0,
  1354. sizeof(cb->args) - sizeof(cb->args[0]));
  1355. if (cb->args[1] == 0) {
  1356. if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
  1357. NETLINK_CB(cb->skb).portid,
  1358. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1359. RTM_NEWTFILTER) <= 0)
  1360. return false;
  1361. cb->args[1] = 1;
  1362. }
  1363. if (!tp->ops->walk)
  1364. continue;
  1365. arg.w.fn = tcf_node_dump;
  1366. arg.skb = skb;
  1367. arg.cb = cb;
  1368. arg.block = block;
  1369. arg.q = q;
  1370. arg.parent = parent;
  1371. arg.w.stop = 0;
  1372. arg.w.skip = cb->args[1] - 1;
  1373. arg.w.count = 0;
  1374. arg.w.cookie = cb->args[2];
  1375. tp->ops->walk(tp, &arg.w);
  1376. cb->args[2] = arg.w.cookie;
  1377. cb->args[1] = arg.w.count + 1;
  1378. if (arg.w.stop)
  1379. return false;
  1380. }
  1381. return true;
  1382. }
  1383. /* called with RTNL */
  1384. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  1385. {
  1386. struct net *net = sock_net(skb->sk);
  1387. struct nlattr *tca[TCA_MAX + 1];
  1388. struct Qdisc *q = NULL;
  1389. struct tcf_block *block;
  1390. struct tcf_chain *chain;
  1391. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1392. long index_start;
  1393. long index;
  1394. u32 parent;
  1395. int err;
  1396. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1397. return skb->len;
  1398. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1399. if (err)
  1400. return err;
  1401. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1402. block = tcf_block_lookup(net, tcm->tcm_block_index);
  1403. if (!block)
  1404. goto out;
  1405. /* If we work with block index, q is NULL and parent value
  1406. * will never be used in the following code. The check
  1407. * in tcf_fill_node prevents it. However, compiler does not
  1408. * see that far, so set parent to zero to silence the warning
  1409. * about parent being uninitialized.
  1410. */
  1411. parent = 0;
  1412. } else {
  1413. const struct Qdisc_class_ops *cops;
  1414. struct net_device *dev;
  1415. unsigned long cl = 0;
  1416. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1417. if (!dev)
  1418. return skb->len;
  1419. parent = tcm->tcm_parent;
  1420. if (!parent) {
  1421. q = dev->qdisc;
  1422. parent = q->handle;
  1423. } else {
  1424. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  1425. }
  1426. if (!q)
  1427. goto out;
  1428. cops = q->ops->cl_ops;
  1429. if (!cops)
  1430. goto out;
  1431. if (!cops->tcf_block)
  1432. goto out;
  1433. if (TC_H_MIN(tcm->tcm_parent)) {
  1434. cl = cops->find(q, tcm->tcm_parent);
  1435. if (cl == 0)
  1436. goto out;
  1437. }
  1438. block = cops->tcf_block(q, cl, NULL);
  1439. if (!block)
  1440. goto out;
  1441. if (tcf_block_shared(block))
  1442. q = NULL;
  1443. }
  1444. index_start = cb->args[0];
  1445. index = 0;
  1446. list_for_each_entry(chain, &block->chain_list, list) {
  1447. if (tca[TCA_CHAIN] &&
  1448. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  1449. continue;
  1450. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  1451. index_start, &index)) {
  1452. err = -EMSGSIZE;
  1453. break;
  1454. }
  1455. }
  1456. cb->args[0] = index;
  1457. out:
  1458. /* If we did no progress, the error (EMSGSIZE) is real */
  1459. if (skb->len == 0 && err)
  1460. return err;
  1461. return skb->len;
  1462. }
  1463. static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
  1464. struct sk_buff *skb, struct tcf_block *block,
  1465. u32 portid, u32 seq, u16 flags, int event)
  1466. {
  1467. unsigned char *b = skb_tail_pointer(skb);
  1468. const struct tcf_proto_ops *ops;
  1469. struct nlmsghdr *nlh;
  1470. struct tcmsg *tcm;
  1471. void *priv;
  1472. ops = chain->tmplt_ops;
  1473. priv = chain->tmplt_priv;
  1474. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  1475. if (!nlh)
  1476. goto out_nlmsg_trim;
  1477. tcm = nlmsg_data(nlh);
  1478. tcm->tcm_family = AF_UNSPEC;
  1479. tcm->tcm__pad1 = 0;
  1480. tcm->tcm__pad2 = 0;
  1481. tcm->tcm_handle = 0;
  1482. if (block->q) {
  1483. tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
  1484. tcm->tcm_parent = block->q->handle;
  1485. } else {
  1486. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  1487. tcm->tcm_block_index = block->index;
  1488. }
  1489. if (nla_put_u32(skb, TCA_CHAIN, chain->index))
  1490. goto nla_put_failure;
  1491. if (ops) {
  1492. if (nla_put_string(skb, TCA_KIND, ops->kind))
  1493. goto nla_put_failure;
  1494. if (ops->tmplt_dump(skb, net, priv) < 0)
  1495. goto nla_put_failure;
  1496. }
  1497. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1498. return skb->len;
  1499. out_nlmsg_trim:
  1500. nla_put_failure:
  1501. nlmsg_trim(skb, b);
  1502. return -EMSGSIZE;
  1503. }
  1504. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  1505. u32 seq, u16 flags, int event, bool unicast)
  1506. {
  1507. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1508. struct tcf_block *block = chain->block;
  1509. struct net *net = block->net;
  1510. struct sk_buff *skb;
  1511. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1512. if (!skb)
  1513. return -ENOBUFS;
  1514. if (tc_chain_fill_node(chain, net, skb, block, portid,
  1515. seq, flags, event) <= 0) {
  1516. kfree_skb(skb);
  1517. return -EINVAL;
  1518. }
  1519. if (unicast)
  1520. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  1521. return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
  1522. }
  1523. static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  1524. struct nlattr **tca,
  1525. struct netlink_ext_ack *extack)
  1526. {
  1527. const struct tcf_proto_ops *ops;
  1528. void *tmplt_priv;
  1529. /* If kind is not set, user did not specify template. */
  1530. if (!tca[TCA_KIND])
  1531. return 0;
  1532. ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
  1533. if (IS_ERR(ops))
  1534. return PTR_ERR(ops);
  1535. if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
  1536. NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
  1537. return -EOPNOTSUPP;
  1538. }
  1539. tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
  1540. if (IS_ERR(tmplt_priv)) {
  1541. module_put(ops->owner);
  1542. return PTR_ERR(tmplt_priv);
  1543. }
  1544. chain->tmplt_ops = ops;
  1545. chain->tmplt_priv = tmplt_priv;
  1546. return 0;
  1547. }
  1548. static void tc_chain_tmplt_del(struct tcf_chain *chain)
  1549. {
  1550. const struct tcf_proto_ops *ops = chain->tmplt_ops;
  1551. /* If template ops are set, no work to do for us. */
  1552. if (!ops)
  1553. return;
  1554. ops->tmplt_destroy(chain->tmplt_priv);
  1555. module_put(ops->owner);
  1556. }
  1557. /* Add/delete/get a chain */
  1558. static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
  1559. struct netlink_ext_ack *extack)
  1560. {
  1561. struct net *net = sock_net(skb->sk);
  1562. struct nlattr *tca[TCA_MAX + 1];
  1563. struct tcmsg *t;
  1564. u32 parent;
  1565. u32 chain_index;
  1566. struct Qdisc *q = NULL;
  1567. struct tcf_chain *chain = NULL;
  1568. struct tcf_block *block;
  1569. unsigned long cl;
  1570. int err;
  1571. if (n->nlmsg_type != RTM_GETCHAIN &&
  1572. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1573. return -EPERM;
  1574. replay:
  1575. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  1576. if (err < 0)
  1577. return err;
  1578. t = nlmsg_data(n);
  1579. parent = t->tcm_parent;
  1580. cl = 0;
  1581. block = tcf_block_find(net, &q, &parent, &cl,
  1582. t->tcm_ifindex, t->tcm_block_index, extack);
  1583. if (IS_ERR(block))
  1584. return PTR_ERR(block);
  1585. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1586. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1587. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1588. return -EINVAL;
  1589. }
  1590. chain = tcf_chain_lookup(block, chain_index);
  1591. if (n->nlmsg_type == RTM_NEWCHAIN) {
  1592. if (chain) {
  1593. if (tcf_chain_held_by_acts_only(chain)) {
  1594. /* The chain exists only because there is
  1595. * some action referencing it.
  1596. */
  1597. tcf_chain_hold(chain);
  1598. } else {
  1599. NL_SET_ERR_MSG(extack, "Filter chain already exists");
  1600. return -EEXIST;
  1601. }
  1602. } else {
  1603. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1604. NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
  1605. return -ENOENT;
  1606. }
  1607. chain = tcf_chain_create(block, chain_index);
  1608. if (!chain) {
  1609. NL_SET_ERR_MSG(extack, "Failed to create filter chain");
  1610. return -ENOMEM;
  1611. }
  1612. }
  1613. } else {
  1614. if (!chain || tcf_chain_held_by_acts_only(chain)) {
  1615. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  1616. return -EINVAL;
  1617. }
  1618. tcf_chain_hold(chain);
  1619. }
  1620. switch (n->nlmsg_type) {
  1621. case RTM_NEWCHAIN:
  1622. err = tc_chain_tmplt_add(chain, net, tca, extack);
  1623. if (err)
  1624. goto errout;
  1625. /* In case the chain was successfully added, take a reference
  1626. * to the chain. This ensures that an empty chain
  1627. * does not disappear at the end of this function.
  1628. */
  1629. tcf_chain_hold(chain);
  1630. chain->explicitly_created = true;
  1631. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  1632. RTM_NEWCHAIN, false);
  1633. break;
  1634. case RTM_DELCHAIN:
  1635. /* Flush the chain first as the user requested chain removal. */
  1636. tcf_chain_flush(chain);
  1637. /* In case the chain was successfully deleted, put a reference
  1638. * to the chain previously taken during addition.
  1639. */
  1640. tcf_chain_put_explicitly_created(chain);
  1641. chain->explicitly_created = false;
  1642. break;
  1643. case RTM_GETCHAIN:
  1644. err = tc_chain_notify(chain, skb, n->nlmsg_seq,
  1645. n->nlmsg_seq, n->nlmsg_type, true);
  1646. if (err < 0)
  1647. NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
  1648. break;
  1649. default:
  1650. err = -EOPNOTSUPP;
  1651. NL_SET_ERR_MSG(extack, "Unsupported message type");
  1652. goto errout;
  1653. }
  1654. errout:
  1655. tcf_chain_put(chain);
  1656. if (err == -EAGAIN)
  1657. /* Replay the request. */
  1658. goto replay;
  1659. return err;
  1660. }
  1661. /* called with RTNL */
  1662. static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
  1663. {
  1664. struct net *net = sock_net(skb->sk);
  1665. struct nlattr *tca[TCA_MAX + 1];
  1666. struct Qdisc *q = NULL;
  1667. struct tcf_block *block;
  1668. struct tcf_chain *chain;
  1669. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1670. long index_start;
  1671. long index;
  1672. u32 parent;
  1673. int err;
  1674. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1675. return skb->len;
  1676. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1677. if (err)
  1678. return err;
  1679. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1680. block = tcf_block_lookup(net, tcm->tcm_block_index);
  1681. if (!block)
  1682. goto out;
  1683. /* If we work with block index, q is NULL and parent value
  1684. * will never be used in the following code. The check
  1685. * in tcf_fill_node prevents it. However, compiler does not
  1686. * see that far, so set parent to zero to silence the warning
  1687. * about parent being uninitialized.
  1688. */
  1689. parent = 0;
  1690. } else {
  1691. const struct Qdisc_class_ops *cops;
  1692. struct net_device *dev;
  1693. unsigned long cl = 0;
  1694. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1695. if (!dev)
  1696. return skb->len;
  1697. parent = tcm->tcm_parent;
  1698. if (!parent) {
  1699. q = dev->qdisc;
  1700. parent = q->handle;
  1701. } else {
  1702. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  1703. }
  1704. if (!q)
  1705. goto out;
  1706. cops = q->ops->cl_ops;
  1707. if (!cops)
  1708. goto out;
  1709. if (!cops->tcf_block)
  1710. goto out;
  1711. if (TC_H_MIN(tcm->tcm_parent)) {
  1712. cl = cops->find(q, tcm->tcm_parent);
  1713. if (cl == 0)
  1714. goto out;
  1715. }
  1716. block = cops->tcf_block(q, cl, NULL);
  1717. if (!block)
  1718. goto out;
  1719. if (tcf_block_shared(block))
  1720. q = NULL;
  1721. }
  1722. index_start = cb->args[0];
  1723. index = 0;
  1724. list_for_each_entry(chain, &block->chain_list, list) {
  1725. if ((tca[TCA_CHAIN] &&
  1726. nla_get_u32(tca[TCA_CHAIN]) != chain->index))
  1727. continue;
  1728. if (index < index_start) {
  1729. index++;
  1730. continue;
  1731. }
  1732. if (tcf_chain_held_by_acts_only(chain))
  1733. continue;
  1734. err = tc_chain_fill_node(chain, net, skb, block,
  1735. NETLINK_CB(cb->skb).portid,
  1736. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1737. RTM_NEWCHAIN);
  1738. if (err <= 0)
  1739. break;
  1740. index++;
  1741. }
  1742. cb->args[0] = index;
  1743. out:
  1744. /* If we did no progress, the error (EMSGSIZE) is real */
  1745. if (skb->len == 0 && err)
  1746. return err;
  1747. return skb->len;
  1748. }
  1749. void tcf_exts_destroy(struct tcf_exts *exts)
  1750. {
  1751. #ifdef CONFIG_NET_CLS_ACT
  1752. tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
  1753. kfree(exts->actions);
  1754. exts->nr_actions = 0;
  1755. #endif
  1756. }
  1757. EXPORT_SYMBOL(tcf_exts_destroy);
  1758. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  1759. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
  1760. struct netlink_ext_ack *extack)
  1761. {
  1762. #ifdef CONFIG_NET_CLS_ACT
  1763. {
  1764. struct tc_action *act;
  1765. size_t attr_size = 0;
  1766. if (exts->police && tb[exts->police]) {
  1767. act = tcf_action_init_1(net, tp, tb[exts->police],
  1768. rate_tlv, "police", ovr,
  1769. TCA_ACT_BIND, true, extack);
  1770. if (IS_ERR(act))
  1771. return PTR_ERR(act);
  1772. act->type = exts->type = TCA_OLD_COMPAT;
  1773. exts->actions[0] = act;
  1774. exts->nr_actions = 1;
  1775. } else if (exts->action && tb[exts->action]) {
  1776. int err;
  1777. err = tcf_action_init(net, tp, tb[exts->action],
  1778. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  1779. exts->actions, &attr_size, true,
  1780. extack);
  1781. if (err < 0)
  1782. return err;
  1783. exts->nr_actions = err;
  1784. }
  1785. exts->net = net;
  1786. }
  1787. #else
  1788. if ((exts->action && tb[exts->action]) ||
  1789. (exts->police && tb[exts->police])) {
  1790. NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
  1791. return -EOPNOTSUPP;
  1792. }
  1793. #endif
  1794. return 0;
  1795. }
  1796. EXPORT_SYMBOL(tcf_exts_validate);
  1797. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  1798. {
  1799. #ifdef CONFIG_NET_CLS_ACT
  1800. struct tcf_exts old = *dst;
  1801. *dst = *src;
  1802. tcf_exts_destroy(&old);
  1803. #endif
  1804. }
  1805. EXPORT_SYMBOL(tcf_exts_change);
  1806. #ifdef CONFIG_NET_CLS_ACT
  1807. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  1808. {
  1809. if (exts->nr_actions == 0)
  1810. return NULL;
  1811. else
  1812. return exts->actions[0];
  1813. }
  1814. #endif
  1815. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  1816. {
  1817. #ifdef CONFIG_NET_CLS_ACT
  1818. struct nlattr *nest;
  1819. if (exts->action && tcf_exts_has_actions(exts)) {
  1820. /*
  1821. * again for backward compatible mode - we want
  1822. * to work with both old and new modes of entering
  1823. * tc data even if iproute2 was newer - jhs
  1824. */
  1825. if (exts->type != TCA_OLD_COMPAT) {
  1826. nest = nla_nest_start(skb, exts->action);
  1827. if (nest == NULL)
  1828. goto nla_put_failure;
  1829. if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
  1830. goto nla_put_failure;
  1831. nla_nest_end(skb, nest);
  1832. } else if (exts->police) {
  1833. struct tc_action *act = tcf_exts_first_act(exts);
  1834. nest = nla_nest_start(skb, exts->police);
  1835. if (nest == NULL || !act)
  1836. goto nla_put_failure;
  1837. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  1838. goto nla_put_failure;
  1839. nla_nest_end(skb, nest);
  1840. }
  1841. }
  1842. return 0;
  1843. nla_put_failure:
  1844. nla_nest_cancel(skb, nest);
  1845. return -1;
  1846. #else
  1847. return 0;
  1848. #endif
  1849. }
  1850. EXPORT_SYMBOL(tcf_exts_dump);
  1851. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  1852. {
  1853. #ifdef CONFIG_NET_CLS_ACT
  1854. struct tc_action *a = tcf_exts_first_act(exts);
  1855. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  1856. return -1;
  1857. #endif
  1858. return 0;
  1859. }
  1860. EXPORT_SYMBOL(tcf_exts_dump_stats);
  1861. static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
  1862. enum tc_setup_type type,
  1863. void *type_data, bool err_stop)
  1864. {
  1865. int ok_count = 0;
  1866. #ifdef CONFIG_NET_CLS_ACT
  1867. const struct tc_action *a;
  1868. struct net_device *dev;
  1869. int i, ret;
  1870. if (!tcf_exts_has_actions(exts))
  1871. return 0;
  1872. for (i = 0; i < exts->nr_actions; i++) {
  1873. a = exts->actions[i];
  1874. if (!a->ops->get_dev)
  1875. continue;
  1876. dev = a->ops->get_dev(a);
  1877. if (!dev)
  1878. continue;
  1879. ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
  1880. if (ret < 0)
  1881. return ret;
  1882. ok_count += ret;
  1883. }
  1884. #endif
  1885. return ok_count;
  1886. }
  1887. int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
  1888. enum tc_setup_type type, void *type_data, bool err_stop)
  1889. {
  1890. int ok_count;
  1891. int ret;
  1892. ret = tcf_block_cb_call(block, type, type_data, err_stop);
  1893. if (ret < 0)
  1894. return ret;
  1895. ok_count = ret;
  1896. if (!exts || ok_count)
  1897. return ok_count;
  1898. ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
  1899. if (ret < 0)
  1900. return ret;
  1901. ok_count += ret;
  1902. return ok_count;
  1903. }
  1904. EXPORT_SYMBOL(tc_setup_cb_call);
  1905. static __net_init int tcf_net_init(struct net *net)
  1906. {
  1907. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1908. idr_init(&tn->idr);
  1909. return 0;
  1910. }
  1911. static void __net_exit tcf_net_exit(struct net *net)
  1912. {
  1913. struct tcf_net *tn = net_generic(net, tcf_net_id);
  1914. idr_destroy(&tn->idr);
  1915. }
  1916. static struct pernet_operations tcf_net_ops = {
  1917. .init = tcf_net_init,
  1918. .exit = tcf_net_exit,
  1919. .id = &tcf_net_id,
  1920. .size = sizeof(struct tcf_net),
  1921. };
  1922. static int __init tc_filter_init(void)
  1923. {
  1924. int err;
  1925. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  1926. if (!tc_filter_wq)
  1927. return -ENOMEM;
  1928. err = register_pernet_subsys(&tcf_net_ops);
  1929. if (err)
  1930. goto err_register_pernet_subsys;
  1931. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
  1932. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
  1933. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
  1934. tc_dump_tfilter, 0);
  1935. rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
  1936. rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
  1937. rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
  1938. tc_dump_chain, 0);
  1939. return 0;
  1940. err_register_pernet_subsys:
  1941. destroy_workqueue(tc_filter_wq);
  1942. return err;
  1943. }
  1944. subsys_initcall(tc_filter_init);