cls_api.c 29 KB

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