cls_api.c 55 KB

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