cls_api.c 55 KB

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