cls_api.c 54 KB

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