cls_api.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 <net/net_namespace.h>
  27. #include <net/sock.h>
  28. #include <net/netlink.h>
  29. #include <net/pkt_sched.h>
  30. #include <net/pkt_cls.h>
  31. /* The list of all installed classifier types */
  32. static LIST_HEAD(tcf_proto_base);
  33. /* Protects list of registered TC modules. It is pure SMP lock. */
  34. static DEFINE_RWLOCK(cls_mod_lock);
  35. /* Find classifier type by string name */
  36. static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
  37. {
  38. const struct tcf_proto_ops *t, *res = NULL;
  39. if (kind) {
  40. read_lock(&cls_mod_lock);
  41. list_for_each_entry(t, &tcf_proto_base, head) {
  42. if (strcmp(kind, t->kind) == 0) {
  43. if (try_module_get(t->owner))
  44. res = t;
  45. break;
  46. }
  47. }
  48. read_unlock(&cls_mod_lock);
  49. }
  50. return res;
  51. }
  52. /* Register(unregister) new classifier type */
  53. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  54. {
  55. struct tcf_proto_ops *t;
  56. int rc = -EEXIST;
  57. write_lock(&cls_mod_lock);
  58. list_for_each_entry(t, &tcf_proto_base, head)
  59. if (!strcmp(ops->kind, t->kind))
  60. goto out;
  61. list_add_tail(&ops->head, &tcf_proto_base);
  62. rc = 0;
  63. out:
  64. write_unlock(&cls_mod_lock);
  65. return rc;
  66. }
  67. EXPORT_SYMBOL(register_tcf_proto_ops);
  68. static struct workqueue_struct *tc_filter_wq;
  69. int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  70. {
  71. struct tcf_proto_ops *t;
  72. int rc = -ENOENT;
  73. /* Wait for outstanding call_rcu()s, if any, from a
  74. * tcf_proto_ops's destroy() handler.
  75. */
  76. rcu_barrier();
  77. flush_workqueue(tc_filter_wq);
  78. write_lock(&cls_mod_lock);
  79. list_for_each_entry(t, &tcf_proto_base, head) {
  80. if (t == ops) {
  81. list_del(&t->head);
  82. rc = 0;
  83. break;
  84. }
  85. }
  86. write_unlock(&cls_mod_lock);
  87. return rc;
  88. }
  89. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  90. bool tcf_queue_work(struct work_struct *work)
  91. {
  92. return queue_work(tc_filter_wq, work);
  93. }
  94. EXPORT_SYMBOL(tcf_queue_work);
  95. /* Select new prio value from the range, managed by kernel. */
  96. static inline u32 tcf_auto_prio(struct tcf_proto *tp)
  97. {
  98. u32 first = TC_H_MAKE(0xC0000000U, 0U);
  99. if (tp)
  100. first = tp->prio - 1;
  101. return TC_H_MAJ(first);
  102. }
  103. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  104. u32 prio, u32 parent, struct Qdisc *q,
  105. struct tcf_chain *chain)
  106. {
  107. struct tcf_proto *tp;
  108. int err;
  109. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  110. if (!tp)
  111. return ERR_PTR(-ENOBUFS);
  112. err = -ENOENT;
  113. tp->ops = tcf_proto_lookup_ops(kind);
  114. if (!tp->ops) {
  115. #ifdef CONFIG_MODULES
  116. rtnl_unlock();
  117. request_module("cls_%s", kind);
  118. rtnl_lock();
  119. tp->ops = tcf_proto_lookup_ops(kind);
  120. /* We dropped the RTNL semaphore in order to perform
  121. * the module load. So, even if we succeeded in loading
  122. * the module we have to replay the request. We indicate
  123. * this using -EAGAIN.
  124. */
  125. if (tp->ops) {
  126. module_put(tp->ops->owner);
  127. err = -EAGAIN;
  128. } else {
  129. err = -ENOENT;
  130. }
  131. goto errout;
  132. #endif
  133. }
  134. tp->classify = tp->ops->classify;
  135. tp->protocol = protocol;
  136. tp->prio = prio;
  137. tp->classid = parent;
  138. tp->q = q;
  139. tp->chain = chain;
  140. err = tp->ops->init(tp);
  141. if (err) {
  142. module_put(tp->ops->owner);
  143. goto errout;
  144. }
  145. return tp;
  146. errout:
  147. kfree(tp);
  148. return ERR_PTR(err);
  149. }
  150. static void tcf_proto_destroy(struct tcf_proto *tp)
  151. {
  152. tp->ops->destroy(tp);
  153. module_put(tp->ops->owner);
  154. kfree_rcu(tp, rcu);
  155. }
  156. static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
  157. u32 chain_index)
  158. {
  159. struct tcf_chain *chain;
  160. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  161. if (!chain)
  162. return NULL;
  163. list_add_tail(&chain->list, &block->chain_list);
  164. chain->block = block;
  165. chain->index = chain_index;
  166. chain->refcnt = 1;
  167. return chain;
  168. }
  169. static void tcf_chain_head_change(struct tcf_chain *chain,
  170. struct tcf_proto *tp_head)
  171. {
  172. if (chain->chain_head_change)
  173. chain->chain_head_change(tp_head,
  174. chain->chain_head_change_priv);
  175. }
  176. static void tcf_chain_flush(struct tcf_chain *chain)
  177. {
  178. struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
  179. tcf_chain_head_change(chain, NULL);
  180. while (tp) {
  181. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  182. tcf_proto_destroy(tp);
  183. tp = rtnl_dereference(chain->filter_chain);
  184. tcf_chain_put(chain);
  185. }
  186. }
  187. static void tcf_chain_destroy(struct tcf_chain *chain)
  188. {
  189. list_del(&chain->list);
  190. kfree(chain);
  191. }
  192. static void tcf_chain_hold(struct tcf_chain *chain)
  193. {
  194. ++chain->refcnt;
  195. }
  196. struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  197. bool create)
  198. {
  199. struct tcf_chain *chain;
  200. list_for_each_entry(chain, &block->chain_list, list) {
  201. if (chain->index == chain_index) {
  202. tcf_chain_hold(chain);
  203. return chain;
  204. }
  205. }
  206. return create ? tcf_chain_create(block, chain_index) : NULL;
  207. }
  208. EXPORT_SYMBOL(tcf_chain_get);
  209. void tcf_chain_put(struct tcf_chain *chain)
  210. {
  211. if (--chain->refcnt == 0)
  212. tcf_chain_destroy(chain);
  213. }
  214. EXPORT_SYMBOL(tcf_chain_put);
  215. static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
  216. struct tcf_block_ext_info *ei,
  217. enum tc_block_command command)
  218. {
  219. struct net_device *dev = q->dev_queue->dev;
  220. struct tc_block_offload bo = {};
  221. if (!dev->netdev_ops->ndo_setup_tc)
  222. return;
  223. bo.command = command;
  224. bo.binder_type = ei->binder_type;
  225. bo.block = block;
  226. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
  227. }
  228. static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
  229. struct tcf_block_ext_info *ei)
  230. {
  231. tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
  232. }
  233. static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
  234. struct tcf_block_ext_info *ei)
  235. {
  236. tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
  237. }
  238. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  239. struct tcf_block_ext_info *ei)
  240. {
  241. struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  242. struct tcf_chain *chain;
  243. int err;
  244. if (!block)
  245. return -ENOMEM;
  246. INIT_LIST_HEAD(&block->chain_list);
  247. INIT_LIST_HEAD(&block->cb_list);
  248. /* Create chain 0 by default, it has to be always present. */
  249. chain = tcf_chain_create(block, 0);
  250. if (!chain) {
  251. err = -ENOMEM;
  252. goto err_chain_create;
  253. }
  254. WARN_ON(!ei->chain_head_change);
  255. chain->chain_head_change = ei->chain_head_change;
  256. chain->chain_head_change_priv = ei->chain_head_change_priv;
  257. block->net = qdisc_net(q);
  258. block->q = q;
  259. tcf_block_offload_bind(block, q, ei);
  260. *p_block = block;
  261. return 0;
  262. err_chain_create:
  263. kfree(block);
  264. return err;
  265. }
  266. EXPORT_SYMBOL(tcf_block_get_ext);
  267. static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
  268. {
  269. struct tcf_proto __rcu **p_filter_chain = priv;
  270. rcu_assign_pointer(*p_filter_chain, tp_head);
  271. }
  272. int tcf_block_get(struct tcf_block **p_block,
  273. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
  274. {
  275. struct tcf_block_ext_info ei = {
  276. .chain_head_change = tcf_chain_head_change_dflt,
  277. .chain_head_change_priv = p_filter_chain,
  278. };
  279. WARN_ON(!p_filter_chain);
  280. return tcf_block_get_ext(p_block, q, &ei);
  281. }
  282. EXPORT_SYMBOL(tcf_block_get);
  283. static void tcf_block_put_final(struct work_struct *work)
  284. {
  285. struct tcf_block *block = container_of(work, struct tcf_block, work);
  286. struct tcf_chain *chain, *tmp;
  287. rtnl_lock();
  288. /* At this point, all the chains should have refcnt == 1. */
  289. list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
  290. tcf_chain_put(chain);
  291. rtnl_unlock();
  292. kfree(block);
  293. }
  294. /* XXX: Standalone actions are not allowed to jump to any chain, and bound
  295. * actions should be all removed after flushing.
  296. */
  297. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  298. struct tcf_block_ext_info *ei)
  299. {
  300. struct tcf_chain *chain;
  301. if (!block)
  302. return;
  303. /* Hold a refcnt for all chains, except 0, so that they don't disappear
  304. * while we are iterating.
  305. */
  306. list_for_each_entry(chain, &block->chain_list, list)
  307. if (chain->index)
  308. tcf_chain_hold(chain);
  309. list_for_each_entry(chain, &block->chain_list, list)
  310. tcf_chain_flush(chain);
  311. tcf_block_offload_unbind(block, q, ei);
  312. INIT_WORK(&block->work, tcf_block_put_final);
  313. /* Wait for existing RCU callbacks to cool down, make sure their works
  314. * have been queued before this. We can not flush pending works here
  315. * because we are holding the RTNL lock.
  316. */
  317. rcu_barrier();
  318. tcf_queue_work(&block->work);
  319. }
  320. EXPORT_SYMBOL(tcf_block_put_ext);
  321. void tcf_block_put(struct tcf_block *block)
  322. {
  323. struct tcf_block_ext_info ei = {0, };
  324. if (!block)
  325. return;
  326. tcf_block_put_ext(block, block->q, &ei);
  327. }
  328. EXPORT_SYMBOL(tcf_block_put);
  329. struct tcf_block_cb {
  330. struct list_head list;
  331. tc_setup_cb_t *cb;
  332. void *cb_ident;
  333. void *cb_priv;
  334. unsigned int refcnt;
  335. };
  336. void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
  337. {
  338. return block_cb->cb_priv;
  339. }
  340. EXPORT_SYMBOL(tcf_block_cb_priv);
  341. struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
  342. tc_setup_cb_t *cb, void *cb_ident)
  343. { struct tcf_block_cb *block_cb;
  344. list_for_each_entry(block_cb, &block->cb_list, list)
  345. if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
  346. return block_cb;
  347. return NULL;
  348. }
  349. EXPORT_SYMBOL(tcf_block_cb_lookup);
  350. void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
  351. {
  352. block_cb->refcnt++;
  353. }
  354. EXPORT_SYMBOL(tcf_block_cb_incref);
  355. unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
  356. {
  357. return --block_cb->refcnt;
  358. }
  359. EXPORT_SYMBOL(tcf_block_cb_decref);
  360. struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
  361. tc_setup_cb_t *cb, void *cb_ident,
  362. void *cb_priv)
  363. {
  364. struct tcf_block_cb *block_cb;
  365. block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
  366. if (!block_cb)
  367. return NULL;
  368. block_cb->cb = cb;
  369. block_cb->cb_ident = cb_ident;
  370. block_cb->cb_priv = cb_priv;
  371. list_add(&block_cb->list, &block->cb_list);
  372. return block_cb;
  373. }
  374. EXPORT_SYMBOL(__tcf_block_cb_register);
  375. int tcf_block_cb_register(struct tcf_block *block,
  376. tc_setup_cb_t *cb, void *cb_ident,
  377. void *cb_priv)
  378. {
  379. struct tcf_block_cb *block_cb;
  380. block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
  381. return block_cb ? 0 : -ENOMEM;
  382. }
  383. EXPORT_SYMBOL(tcf_block_cb_register);
  384. void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
  385. {
  386. list_del(&block_cb->list);
  387. kfree(block_cb);
  388. }
  389. EXPORT_SYMBOL(__tcf_block_cb_unregister);
  390. void tcf_block_cb_unregister(struct tcf_block *block,
  391. tc_setup_cb_t *cb, void *cb_ident)
  392. {
  393. struct tcf_block_cb *block_cb;
  394. block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
  395. if (!block_cb)
  396. return;
  397. __tcf_block_cb_unregister(block_cb);
  398. }
  399. EXPORT_SYMBOL(tcf_block_cb_unregister);
  400. static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
  401. void *type_data, bool err_stop)
  402. {
  403. struct tcf_block_cb *block_cb;
  404. int ok_count = 0;
  405. int err;
  406. list_for_each_entry(block_cb, &block->cb_list, list) {
  407. err = block_cb->cb(type, type_data, block_cb->cb_priv);
  408. if (err) {
  409. if (err_stop)
  410. return err;
  411. } else {
  412. ok_count++;
  413. }
  414. }
  415. return ok_count;
  416. }
  417. /* Main classifier routine: scans classifier chain attached
  418. * to this qdisc, (optionally) tests for protocol and asks
  419. * specific classifiers.
  420. */
  421. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  422. struct tcf_result *res, bool compat_mode)
  423. {
  424. __be16 protocol = tc_skb_protocol(skb);
  425. #ifdef CONFIG_NET_CLS_ACT
  426. const int max_reclassify_loop = 4;
  427. const struct tcf_proto *orig_tp = tp;
  428. const struct tcf_proto *first_tp;
  429. int limit = 0;
  430. reclassify:
  431. #endif
  432. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  433. int err;
  434. if (tp->protocol != protocol &&
  435. tp->protocol != htons(ETH_P_ALL))
  436. continue;
  437. err = tp->classify(skb, tp, res);
  438. #ifdef CONFIG_NET_CLS_ACT
  439. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  440. first_tp = orig_tp;
  441. goto reset;
  442. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  443. first_tp = res->goto_tp;
  444. goto reset;
  445. }
  446. #endif
  447. if (err >= 0)
  448. return err;
  449. }
  450. return TC_ACT_UNSPEC; /* signal: continue lookup */
  451. #ifdef CONFIG_NET_CLS_ACT
  452. reset:
  453. if (unlikely(limit++ >= max_reclassify_loop)) {
  454. net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
  455. tp->q->ops->id, tp->prio & 0xffff,
  456. ntohs(tp->protocol));
  457. return TC_ACT_SHOT;
  458. }
  459. tp = first_tp;
  460. protocol = tc_skb_protocol(skb);
  461. goto reclassify;
  462. #endif
  463. }
  464. EXPORT_SYMBOL(tcf_classify);
  465. struct tcf_chain_info {
  466. struct tcf_proto __rcu **pprev;
  467. struct tcf_proto __rcu *next;
  468. };
  469. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  470. {
  471. return rtnl_dereference(*chain_info->pprev);
  472. }
  473. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  474. struct tcf_chain_info *chain_info,
  475. struct tcf_proto *tp)
  476. {
  477. if (*chain_info->pprev == chain->filter_chain)
  478. tcf_chain_head_change(chain, tp);
  479. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  480. rcu_assign_pointer(*chain_info->pprev, tp);
  481. tcf_chain_hold(chain);
  482. }
  483. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  484. struct tcf_chain_info *chain_info,
  485. struct tcf_proto *tp)
  486. {
  487. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  488. if (tp == chain->filter_chain)
  489. tcf_chain_head_change(chain, next);
  490. RCU_INIT_POINTER(*chain_info->pprev, next);
  491. tcf_chain_put(chain);
  492. }
  493. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  494. struct tcf_chain_info *chain_info,
  495. u32 protocol, u32 prio,
  496. bool prio_allocate)
  497. {
  498. struct tcf_proto **pprev;
  499. struct tcf_proto *tp;
  500. /* Check the chain for existence of proto-tcf with this priority */
  501. for (pprev = &chain->filter_chain;
  502. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  503. if (tp->prio >= prio) {
  504. if (tp->prio == prio) {
  505. if (prio_allocate ||
  506. (tp->protocol != protocol && protocol))
  507. return ERR_PTR(-EINVAL);
  508. } else {
  509. tp = NULL;
  510. }
  511. break;
  512. }
  513. }
  514. chain_info->pprev = pprev;
  515. chain_info->next = tp ? tp->next : NULL;
  516. return tp;
  517. }
  518. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  519. struct tcf_proto *tp, struct Qdisc *q, u32 parent,
  520. void *fh, u32 portid, u32 seq, u16 flags, int event)
  521. {
  522. struct tcmsg *tcm;
  523. struct nlmsghdr *nlh;
  524. unsigned char *b = skb_tail_pointer(skb);
  525. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  526. if (!nlh)
  527. goto out_nlmsg_trim;
  528. tcm = nlmsg_data(nlh);
  529. tcm->tcm_family = AF_UNSPEC;
  530. tcm->tcm__pad1 = 0;
  531. tcm->tcm__pad2 = 0;
  532. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  533. tcm->tcm_parent = parent;
  534. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  535. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  536. goto nla_put_failure;
  537. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  538. goto nla_put_failure;
  539. if (!fh) {
  540. tcm->tcm_handle = 0;
  541. } else {
  542. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  543. goto nla_put_failure;
  544. }
  545. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  546. return skb->len;
  547. out_nlmsg_trim:
  548. nla_put_failure:
  549. nlmsg_trim(skb, b);
  550. return -1;
  551. }
  552. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  553. struct nlmsghdr *n, struct tcf_proto *tp,
  554. struct Qdisc *q, u32 parent,
  555. void *fh, int event, bool unicast)
  556. {
  557. struct sk_buff *skb;
  558. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  559. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  560. if (!skb)
  561. return -ENOBUFS;
  562. if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
  563. n->nlmsg_flags, event) <= 0) {
  564. kfree_skb(skb);
  565. return -EINVAL;
  566. }
  567. if (unicast)
  568. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  569. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  570. n->nlmsg_flags & NLM_F_ECHO);
  571. }
  572. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  573. struct nlmsghdr *n, struct tcf_proto *tp,
  574. struct Qdisc *q, u32 parent,
  575. void *fh, bool unicast, bool *last)
  576. {
  577. struct sk_buff *skb;
  578. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  579. int err;
  580. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  581. if (!skb)
  582. return -ENOBUFS;
  583. if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
  584. n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
  585. kfree_skb(skb);
  586. return -EINVAL;
  587. }
  588. err = tp->ops->delete(tp, fh, last);
  589. if (err) {
  590. kfree_skb(skb);
  591. return err;
  592. }
  593. if (unicast)
  594. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  595. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  596. n->nlmsg_flags & NLM_F_ECHO);
  597. }
  598. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  599. struct Qdisc *q, u32 parent,
  600. struct nlmsghdr *n,
  601. struct tcf_chain *chain, int event)
  602. {
  603. struct tcf_proto *tp;
  604. for (tp = rtnl_dereference(chain->filter_chain);
  605. tp; tp = rtnl_dereference(tp->next))
  606. tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
  607. }
  608. /* Add/change/delete/get a filter node */
  609. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  610. struct netlink_ext_ack *extack)
  611. {
  612. struct net *net = sock_net(skb->sk);
  613. struct nlattr *tca[TCA_MAX + 1];
  614. struct tcmsg *t;
  615. u32 protocol;
  616. u32 prio;
  617. bool prio_allocate;
  618. u32 parent;
  619. u32 chain_index;
  620. struct net_device *dev;
  621. struct Qdisc *q;
  622. struct tcf_chain_info chain_info;
  623. struct tcf_chain *chain = NULL;
  624. struct tcf_block *block;
  625. struct tcf_proto *tp;
  626. const struct Qdisc_class_ops *cops;
  627. unsigned long cl;
  628. void *fh;
  629. int err;
  630. int tp_created;
  631. if ((n->nlmsg_type != RTM_GETTFILTER) &&
  632. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  633. return -EPERM;
  634. replay:
  635. tp_created = 0;
  636. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  637. if (err < 0)
  638. return err;
  639. t = nlmsg_data(n);
  640. protocol = TC_H_MIN(t->tcm_info);
  641. prio = TC_H_MAJ(t->tcm_info);
  642. prio_allocate = false;
  643. parent = t->tcm_parent;
  644. cl = 0;
  645. if (prio == 0) {
  646. switch (n->nlmsg_type) {
  647. case RTM_DELTFILTER:
  648. if (protocol || t->tcm_handle || tca[TCA_KIND])
  649. return -ENOENT;
  650. break;
  651. case RTM_NEWTFILTER:
  652. /* If no priority is provided by the user,
  653. * we allocate one.
  654. */
  655. if (n->nlmsg_flags & NLM_F_CREATE) {
  656. prio = TC_H_MAKE(0x80000000U, 0U);
  657. prio_allocate = true;
  658. break;
  659. }
  660. /* fall-through */
  661. default:
  662. return -ENOENT;
  663. }
  664. }
  665. /* Find head of filter chain. */
  666. /* Find link */
  667. dev = __dev_get_by_index(net, t->tcm_ifindex);
  668. if (dev == NULL)
  669. return -ENODEV;
  670. /* Find qdisc */
  671. if (!parent) {
  672. q = dev->qdisc;
  673. parent = q->handle;
  674. } else {
  675. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  676. if (q == NULL)
  677. return -EINVAL;
  678. }
  679. /* Is it classful? */
  680. cops = q->ops->cl_ops;
  681. if (!cops)
  682. return -EINVAL;
  683. if (!cops->tcf_block)
  684. return -EOPNOTSUPP;
  685. /* Do we search for filter, attached to class? */
  686. if (TC_H_MIN(parent)) {
  687. cl = cops->find(q, parent);
  688. if (cl == 0)
  689. return -ENOENT;
  690. }
  691. /* And the last stroke */
  692. block = cops->tcf_block(q, cl);
  693. if (!block) {
  694. err = -EINVAL;
  695. goto errout;
  696. }
  697. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  698. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  699. err = -EINVAL;
  700. goto errout;
  701. }
  702. chain = tcf_chain_get(block, chain_index,
  703. n->nlmsg_type == RTM_NEWTFILTER);
  704. if (!chain) {
  705. err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
  706. goto errout;
  707. }
  708. if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
  709. tfilter_notify_chain(net, skb, q, parent, n,
  710. chain, RTM_DELTFILTER);
  711. tcf_chain_flush(chain);
  712. err = 0;
  713. goto errout;
  714. }
  715. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  716. prio, prio_allocate);
  717. if (IS_ERR(tp)) {
  718. err = PTR_ERR(tp);
  719. goto errout;
  720. }
  721. if (tp == NULL) {
  722. /* Proto-tcf does not exist, create new one */
  723. if (tca[TCA_KIND] == NULL || !protocol) {
  724. err = -EINVAL;
  725. goto errout;
  726. }
  727. if (n->nlmsg_type != RTM_NEWTFILTER ||
  728. !(n->nlmsg_flags & NLM_F_CREATE)) {
  729. err = -ENOENT;
  730. goto errout;
  731. }
  732. if (prio_allocate)
  733. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  734. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  735. protocol, prio, parent, q, chain);
  736. if (IS_ERR(tp)) {
  737. err = PTR_ERR(tp);
  738. goto errout;
  739. }
  740. tp_created = 1;
  741. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  742. err = -EINVAL;
  743. goto errout;
  744. }
  745. fh = tp->ops->get(tp, t->tcm_handle);
  746. if (!fh) {
  747. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  748. tcf_chain_tp_remove(chain, &chain_info, tp);
  749. tfilter_notify(net, skb, n, tp, q, parent, fh,
  750. RTM_DELTFILTER, false);
  751. tcf_proto_destroy(tp);
  752. err = 0;
  753. goto errout;
  754. }
  755. if (n->nlmsg_type != RTM_NEWTFILTER ||
  756. !(n->nlmsg_flags & NLM_F_CREATE)) {
  757. err = -ENOENT;
  758. goto errout;
  759. }
  760. } else {
  761. bool last;
  762. switch (n->nlmsg_type) {
  763. case RTM_NEWTFILTER:
  764. if (n->nlmsg_flags & NLM_F_EXCL) {
  765. if (tp_created)
  766. tcf_proto_destroy(tp);
  767. err = -EEXIST;
  768. goto errout;
  769. }
  770. break;
  771. case RTM_DELTFILTER:
  772. err = tfilter_del_notify(net, skb, n, tp, q, parent,
  773. fh, false, &last);
  774. if (err)
  775. goto errout;
  776. if (last) {
  777. tcf_chain_tp_remove(chain, &chain_info, tp);
  778. tcf_proto_destroy(tp);
  779. }
  780. goto errout;
  781. case RTM_GETTFILTER:
  782. err = tfilter_notify(net, skb, n, tp, q, parent, fh,
  783. RTM_NEWTFILTER, true);
  784. goto errout;
  785. default:
  786. err = -EINVAL;
  787. goto errout;
  788. }
  789. }
  790. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  791. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
  792. if (err == 0) {
  793. if (tp_created)
  794. tcf_chain_tp_insert(chain, &chain_info, tp);
  795. tfilter_notify(net, skb, n, tp, q, parent, fh,
  796. RTM_NEWTFILTER, false);
  797. } else {
  798. if (tp_created)
  799. tcf_proto_destroy(tp);
  800. }
  801. errout:
  802. if (chain)
  803. tcf_chain_put(chain);
  804. if (err == -EAGAIN)
  805. /* Replay the request. */
  806. goto replay;
  807. return err;
  808. }
  809. struct tcf_dump_args {
  810. struct tcf_walker w;
  811. struct sk_buff *skb;
  812. struct netlink_callback *cb;
  813. struct Qdisc *q;
  814. u32 parent;
  815. };
  816. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  817. {
  818. struct tcf_dump_args *a = (void *)arg;
  819. struct net *net = sock_net(a->skb->sk);
  820. return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
  821. n, NETLINK_CB(a->cb->skb).portid,
  822. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  823. RTM_NEWTFILTER);
  824. }
  825. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  826. struct sk_buff *skb, struct netlink_callback *cb,
  827. long index_start, long *p_index)
  828. {
  829. struct net *net = sock_net(skb->sk);
  830. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  831. struct tcf_dump_args arg;
  832. struct tcf_proto *tp;
  833. for (tp = rtnl_dereference(chain->filter_chain);
  834. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  835. if (*p_index < index_start)
  836. continue;
  837. if (TC_H_MAJ(tcm->tcm_info) &&
  838. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  839. continue;
  840. if (TC_H_MIN(tcm->tcm_info) &&
  841. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  842. continue;
  843. if (*p_index > index_start)
  844. memset(&cb->args[1], 0,
  845. sizeof(cb->args) - sizeof(cb->args[0]));
  846. if (cb->args[1] == 0) {
  847. if (tcf_fill_node(net, skb, tp, q, parent, 0,
  848. NETLINK_CB(cb->skb).portid,
  849. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  850. RTM_NEWTFILTER) <= 0)
  851. return false;
  852. cb->args[1] = 1;
  853. }
  854. if (!tp->ops->walk)
  855. continue;
  856. arg.w.fn = tcf_node_dump;
  857. arg.skb = skb;
  858. arg.cb = cb;
  859. arg.q = q;
  860. arg.parent = parent;
  861. arg.w.stop = 0;
  862. arg.w.skip = cb->args[1] - 1;
  863. arg.w.count = 0;
  864. tp->ops->walk(tp, &arg.w);
  865. cb->args[1] = arg.w.count + 1;
  866. if (arg.w.stop)
  867. return false;
  868. }
  869. return true;
  870. }
  871. /* called with RTNL */
  872. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  873. {
  874. struct net *net = sock_net(skb->sk);
  875. struct nlattr *tca[TCA_MAX + 1];
  876. struct net_device *dev;
  877. struct Qdisc *q;
  878. struct tcf_block *block;
  879. struct tcf_chain *chain;
  880. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  881. unsigned long cl = 0;
  882. const struct Qdisc_class_ops *cops;
  883. long index_start;
  884. long index;
  885. u32 parent;
  886. int err;
  887. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  888. return skb->len;
  889. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  890. if (err)
  891. return err;
  892. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  893. if (!dev)
  894. return skb->len;
  895. parent = tcm->tcm_parent;
  896. if (!parent) {
  897. q = dev->qdisc;
  898. parent = q->handle;
  899. } else {
  900. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  901. }
  902. if (!q)
  903. goto out;
  904. cops = q->ops->cl_ops;
  905. if (!cops)
  906. goto out;
  907. if (!cops->tcf_block)
  908. goto out;
  909. if (TC_H_MIN(tcm->tcm_parent)) {
  910. cl = cops->find(q, tcm->tcm_parent);
  911. if (cl == 0)
  912. goto out;
  913. }
  914. block = cops->tcf_block(q, cl);
  915. if (!block)
  916. goto out;
  917. index_start = cb->args[0];
  918. index = 0;
  919. list_for_each_entry(chain, &block->chain_list, list) {
  920. if (tca[TCA_CHAIN] &&
  921. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  922. continue;
  923. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  924. index_start, &index))
  925. break;
  926. }
  927. cb->args[0] = index;
  928. out:
  929. return skb->len;
  930. }
  931. void tcf_exts_destroy(struct tcf_exts *exts)
  932. {
  933. #ifdef CONFIG_NET_CLS_ACT
  934. LIST_HEAD(actions);
  935. ASSERT_RTNL();
  936. tcf_exts_to_list(exts, &actions);
  937. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  938. kfree(exts->actions);
  939. exts->nr_actions = 0;
  940. #endif
  941. }
  942. EXPORT_SYMBOL(tcf_exts_destroy);
  943. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  944. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
  945. {
  946. #ifdef CONFIG_NET_CLS_ACT
  947. {
  948. struct tc_action *act;
  949. if (exts->police && tb[exts->police]) {
  950. act = tcf_action_init_1(net, tp, tb[exts->police],
  951. rate_tlv, "police", ovr,
  952. TCA_ACT_BIND);
  953. if (IS_ERR(act))
  954. return PTR_ERR(act);
  955. act->type = exts->type = TCA_OLD_COMPAT;
  956. exts->actions[0] = act;
  957. exts->nr_actions = 1;
  958. } else if (exts->action && tb[exts->action]) {
  959. LIST_HEAD(actions);
  960. int err, i = 0;
  961. err = tcf_action_init(net, tp, tb[exts->action],
  962. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  963. &actions);
  964. if (err)
  965. return err;
  966. list_for_each_entry(act, &actions, list)
  967. exts->actions[i++] = act;
  968. exts->nr_actions = i;
  969. }
  970. exts->net = net;
  971. }
  972. #else
  973. if ((exts->action && tb[exts->action]) ||
  974. (exts->police && tb[exts->police]))
  975. return -EOPNOTSUPP;
  976. #endif
  977. return 0;
  978. }
  979. EXPORT_SYMBOL(tcf_exts_validate);
  980. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  981. {
  982. #ifdef CONFIG_NET_CLS_ACT
  983. struct tcf_exts old = *dst;
  984. *dst = *src;
  985. tcf_exts_destroy(&old);
  986. #endif
  987. }
  988. EXPORT_SYMBOL(tcf_exts_change);
  989. #ifdef CONFIG_NET_CLS_ACT
  990. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  991. {
  992. if (exts->nr_actions == 0)
  993. return NULL;
  994. else
  995. return exts->actions[0];
  996. }
  997. #endif
  998. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  999. {
  1000. #ifdef CONFIG_NET_CLS_ACT
  1001. struct nlattr *nest;
  1002. if (exts->action && tcf_exts_has_actions(exts)) {
  1003. /*
  1004. * again for backward compatible mode - we want
  1005. * to work with both old and new modes of entering
  1006. * tc data even if iproute2 was newer - jhs
  1007. */
  1008. if (exts->type != TCA_OLD_COMPAT) {
  1009. LIST_HEAD(actions);
  1010. nest = nla_nest_start(skb, exts->action);
  1011. if (nest == NULL)
  1012. goto nla_put_failure;
  1013. tcf_exts_to_list(exts, &actions);
  1014. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  1015. goto nla_put_failure;
  1016. nla_nest_end(skb, nest);
  1017. } else if (exts->police) {
  1018. struct tc_action *act = tcf_exts_first_act(exts);
  1019. nest = nla_nest_start(skb, exts->police);
  1020. if (nest == NULL || !act)
  1021. goto nla_put_failure;
  1022. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  1023. goto nla_put_failure;
  1024. nla_nest_end(skb, nest);
  1025. }
  1026. }
  1027. return 0;
  1028. nla_put_failure:
  1029. nla_nest_cancel(skb, nest);
  1030. return -1;
  1031. #else
  1032. return 0;
  1033. #endif
  1034. }
  1035. EXPORT_SYMBOL(tcf_exts_dump);
  1036. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  1037. {
  1038. #ifdef CONFIG_NET_CLS_ACT
  1039. struct tc_action *a = tcf_exts_first_act(exts);
  1040. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  1041. return -1;
  1042. #endif
  1043. return 0;
  1044. }
  1045. EXPORT_SYMBOL(tcf_exts_dump_stats);
  1046. static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
  1047. enum tc_setup_type type,
  1048. void *type_data, bool err_stop)
  1049. {
  1050. int ok_count = 0;
  1051. #ifdef CONFIG_NET_CLS_ACT
  1052. const struct tc_action *a;
  1053. struct net_device *dev;
  1054. int i, ret;
  1055. if (!tcf_exts_has_actions(exts))
  1056. return 0;
  1057. for (i = 0; i < exts->nr_actions; i++) {
  1058. a = exts->actions[i];
  1059. if (!a->ops->get_dev)
  1060. continue;
  1061. dev = a->ops->get_dev(a);
  1062. if (!dev)
  1063. continue;
  1064. ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
  1065. if (ret < 0)
  1066. return ret;
  1067. ok_count += ret;
  1068. }
  1069. #endif
  1070. return ok_count;
  1071. }
  1072. int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
  1073. enum tc_setup_type type, void *type_data, bool err_stop)
  1074. {
  1075. int ok_count;
  1076. int ret;
  1077. ret = tcf_block_cb_call(block, type, type_data, err_stop);
  1078. if (ret < 0)
  1079. return ret;
  1080. ok_count = ret;
  1081. if (!exts)
  1082. return ok_count;
  1083. ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
  1084. if (ret < 0)
  1085. return ret;
  1086. ok_count += ret;
  1087. return ok_count;
  1088. }
  1089. EXPORT_SYMBOL(tc_setup_cb_call);
  1090. static int __init tc_filter_init(void)
  1091. {
  1092. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  1093. if (!tc_filter_wq)
  1094. return -ENOMEM;
  1095. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
  1096. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
  1097. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
  1098. tc_dump_tfilter, 0);
  1099. return 0;
  1100. }
  1101. subsys_initcall(tc_filter_init);