cls_api.c 53 KB

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