act_api.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * net/sched/act_api.c Packet action 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. * Author: Jamal Hadi Salim
  10. *
  11. *
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/init.h>
  20. #include <linux/kmod.h>
  21. #include <linux/err.h>
  22. #include <linux/module.h>
  23. #include <net/net_namespace.h>
  24. #include <net/sock.h>
  25. #include <net/sch_generic.h>
  26. #include <net/pkt_cls.h>
  27. #include <net/act_api.h>
  28. #include <net/netlink.h>
  29. static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
  30. {
  31. u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
  32. if (!tp)
  33. return -EINVAL;
  34. a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
  35. if (!a->goto_chain)
  36. return -ENOMEM;
  37. return 0;
  38. }
  39. static void tcf_action_goto_chain_fini(struct tc_action *a)
  40. {
  41. tcf_chain_put(a->goto_chain);
  42. }
  43. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  44. struct tcf_result *res)
  45. {
  46. const struct tcf_chain *chain = a->goto_chain;
  47. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  48. }
  49. /* XXX: For standalone actions, we don't need a RCU grace period either, because
  50. * actions are always connected to filters and filters are already destroyed in
  51. * RCU callbacks, so after a RCU grace period actions are already disconnected
  52. * from filters. Readers later can not find us.
  53. */
  54. static void free_tcf(struct tc_action *p)
  55. {
  56. free_percpu(p->cpu_bstats);
  57. free_percpu(p->cpu_qstats);
  58. if (p->act_cookie) {
  59. kfree(p->act_cookie->data);
  60. kfree(p->act_cookie);
  61. }
  62. if (p->goto_chain)
  63. tcf_action_goto_chain_fini(p);
  64. kfree(p);
  65. }
  66. static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
  67. {
  68. spin_lock_bh(&idrinfo->lock);
  69. idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
  70. spin_unlock_bh(&idrinfo->lock);
  71. gen_kill_estimator(&p->tcfa_rate_est);
  72. free_tcf(p);
  73. }
  74. int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
  75. {
  76. int ret = 0;
  77. ASSERT_RTNL();
  78. if (p) {
  79. if (bind)
  80. p->tcfa_bindcnt--;
  81. else if (strict && p->tcfa_bindcnt > 0)
  82. return -EPERM;
  83. p->tcfa_refcnt--;
  84. if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
  85. if (p->ops->cleanup)
  86. p->ops->cleanup(p, bind);
  87. tcf_idr_remove(p->idrinfo, p);
  88. ret = ACT_P_DELETED;
  89. }
  90. }
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(__tcf_idr_release);
  94. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  95. struct netlink_callback *cb)
  96. {
  97. int err = 0, index = -1, s_i = 0, n_i = 0;
  98. u32 act_flags = cb->args[2];
  99. unsigned long jiffy_since = cb->args[3];
  100. struct nlattr *nest;
  101. struct idr *idr = &idrinfo->action_idr;
  102. struct tc_action *p;
  103. unsigned long id = 1;
  104. spin_lock_bh(&idrinfo->lock);
  105. s_i = cb->args[0];
  106. idr_for_each_entry_ext(idr, p, id) {
  107. index++;
  108. if (index < s_i)
  109. continue;
  110. if (jiffy_since &&
  111. time_after(jiffy_since,
  112. (unsigned long)p->tcfa_tm.lastuse))
  113. continue;
  114. nest = nla_nest_start(skb, n_i);
  115. if (!nest) {
  116. index--;
  117. goto nla_put_failure;
  118. }
  119. err = tcf_action_dump_1(skb, p, 0, 0);
  120. if (err < 0) {
  121. index--;
  122. nlmsg_trim(skb, nest);
  123. goto done;
  124. }
  125. nla_nest_end(skb, nest);
  126. n_i++;
  127. if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
  128. n_i >= TCA_ACT_MAX_PRIO)
  129. goto done;
  130. }
  131. done:
  132. if (index >= 0)
  133. cb->args[0] = index + 1;
  134. spin_unlock_bh(&idrinfo->lock);
  135. if (n_i) {
  136. if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
  137. cb->args[1] = n_i;
  138. }
  139. return n_i;
  140. nla_put_failure:
  141. nla_nest_cancel(skb, nest);
  142. goto done;
  143. }
  144. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  145. const struct tc_action_ops *ops)
  146. {
  147. struct nlattr *nest;
  148. int n_i = 0;
  149. int ret = -EINVAL;
  150. struct idr *idr = &idrinfo->action_idr;
  151. struct tc_action *p;
  152. unsigned long id = 1;
  153. nest = nla_nest_start(skb, 0);
  154. if (nest == NULL)
  155. goto nla_put_failure;
  156. if (nla_put_string(skb, TCA_KIND, ops->kind))
  157. goto nla_put_failure;
  158. idr_for_each_entry_ext(idr, p, id) {
  159. ret = __tcf_idr_release(p, false, true);
  160. if (ret == ACT_P_DELETED) {
  161. module_put(ops->owner);
  162. n_i++;
  163. } else if (ret < 0) {
  164. goto nla_put_failure;
  165. }
  166. }
  167. if (nla_put_u32(skb, TCA_FCNT, n_i))
  168. goto nla_put_failure;
  169. nla_nest_end(skb, nest);
  170. return n_i;
  171. nla_put_failure:
  172. nla_nest_cancel(skb, nest);
  173. return ret;
  174. }
  175. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  176. struct netlink_callback *cb, int type,
  177. const struct tc_action_ops *ops)
  178. {
  179. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  180. if (type == RTM_DELACTION) {
  181. return tcf_del_walker(idrinfo, skb, ops);
  182. } else if (type == RTM_GETACTION) {
  183. return tcf_dump_walker(idrinfo, skb, cb);
  184. } else {
  185. WARN(1, "tcf_generic_walker: unknown action %d\n", type);
  186. return -EINVAL;
  187. }
  188. }
  189. EXPORT_SYMBOL(tcf_generic_walker);
  190. static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
  191. {
  192. struct tc_action *p = NULL;
  193. spin_lock_bh(&idrinfo->lock);
  194. p = idr_find_ext(&idrinfo->action_idr, index);
  195. spin_unlock_bh(&idrinfo->lock);
  196. return p;
  197. }
  198. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  199. {
  200. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  201. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  202. if (p) {
  203. *a = p;
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(tcf_idr_search);
  209. bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  210. int bind)
  211. {
  212. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  213. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  214. if (index && p) {
  215. if (bind)
  216. p->tcfa_bindcnt++;
  217. p->tcfa_refcnt++;
  218. *a = p;
  219. return true;
  220. }
  221. return false;
  222. }
  223. EXPORT_SYMBOL(tcf_idr_check);
  224. void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
  225. {
  226. if (est)
  227. gen_kill_estimator(&a->tcfa_rate_est);
  228. free_tcf(a);
  229. }
  230. EXPORT_SYMBOL(tcf_idr_cleanup);
  231. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  232. struct tc_action **a, const struct tc_action_ops *ops,
  233. int bind, bool cpustats)
  234. {
  235. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  236. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  237. struct idr *idr = &idrinfo->action_idr;
  238. int err = -ENOMEM;
  239. unsigned long idr_index;
  240. if (unlikely(!p))
  241. return -ENOMEM;
  242. p->tcfa_refcnt = 1;
  243. if (bind)
  244. p->tcfa_bindcnt = 1;
  245. if (cpustats) {
  246. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  247. if (!p->cpu_bstats) {
  248. err1:
  249. kfree(p);
  250. return err;
  251. }
  252. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  253. if (!p->cpu_qstats) {
  254. err2:
  255. free_percpu(p->cpu_bstats);
  256. goto err1;
  257. }
  258. }
  259. spin_lock_init(&p->tcfa_lock);
  260. /* user doesn't specify an index */
  261. if (!index) {
  262. idr_preload(GFP_KERNEL);
  263. spin_lock_bh(&idrinfo->lock);
  264. err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
  265. GFP_ATOMIC);
  266. spin_unlock_bh(&idrinfo->lock);
  267. idr_preload_end();
  268. if (err) {
  269. err3:
  270. free_percpu(p->cpu_qstats);
  271. goto err2;
  272. }
  273. p->tcfa_index = idr_index;
  274. } else {
  275. idr_preload(GFP_KERNEL);
  276. spin_lock_bh(&idrinfo->lock);
  277. err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
  278. GFP_ATOMIC);
  279. spin_unlock_bh(&idrinfo->lock);
  280. idr_preload_end();
  281. if (err)
  282. goto err3;
  283. p->tcfa_index = index;
  284. }
  285. p->tcfa_tm.install = jiffies;
  286. p->tcfa_tm.lastuse = jiffies;
  287. p->tcfa_tm.firstuse = 0;
  288. if (est) {
  289. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  290. &p->tcfa_rate_est,
  291. &p->tcfa_lock, NULL, est);
  292. if (err) {
  293. goto err3;
  294. }
  295. }
  296. p->idrinfo = idrinfo;
  297. p->ops = ops;
  298. INIT_LIST_HEAD(&p->list);
  299. *a = p;
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(tcf_idr_create);
  303. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
  304. {
  305. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  306. spin_lock_bh(&idrinfo->lock);
  307. idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index);
  308. spin_unlock_bh(&idrinfo->lock);
  309. }
  310. EXPORT_SYMBOL(tcf_idr_insert);
  311. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  312. struct tcf_idrinfo *idrinfo)
  313. {
  314. struct idr *idr = &idrinfo->action_idr;
  315. struct tc_action *p;
  316. int ret;
  317. unsigned long id = 1;
  318. idr_for_each_entry_ext(idr, p, id) {
  319. ret = __tcf_idr_release(p, false, true);
  320. if (ret == ACT_P_DELETED)
  321. module_put(ops->owner);
  322. else if (ret < 0)
  323. return;
  324. }
  325. idr_destroy(&idrinfo->action_idr);
  326. }
  327. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  328. static LIST_HEAD(act_base);
  329. static DEFINE_RWLOCK(act_mod_lock);
  330. int tcf_register_action(struct tc_action_ops *act,
  331. struct pernet_operations *ops)
  332. {
  333. struct tc_action_ops *a;
  334. int ret;
  335. if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
  336. return -EINVAL;
  337. /* We have to register pernet ops before making the action ops visible,
  338. * otherwise tcf_action_init_1() could get a partially initialized
  339. * netns.
  340. */
  341. ret = register_pernet_subsys(ops);
  342. if (ret)
  343. return ret;
  344. write_lock(&act_mod_lock);
  345. list_for_each_entry(a, &act_base, head) {
  346. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  347. write_unlock(&act_mod_lock);
  348. unregister_pernet_subsys(ops);
  349. return -EEXIST;
  350. }
  351. }
  352. list_add_tail(&act->head, &act_base);
  353. write_unlock(&act_mod_lock);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(tcf_register_action);
  357. int tcf_unregister_action(struct tc_action_ops *act,
  358. struct pernet_operations *ops)
  359. {
  360. struct tc_action_ops *a;
  361. int err = -ENOENT;
  362. write_lock(&act_mod_lock);
  363. list_for_each_entry(a, &act_base, head) {
  364. if (a == act) {
  365. list_del(&act->head);
  366. err = 0;
  367. break;
  368. }
  369. }
  370. write_unlock(&act_mod_lock);
  371. if (!err)
  372. unregister_pernet_subsys(ops);
  373. return err;
  374. }
  375. EXPORT_SYMBOL(tcf_unregister_action);
  376. /* lookup by name */
  377. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  378. {
  379. struct tc_action_ops *a, *res = NULL;
  380. if (kind) {
  381. read_lock(&act_mod_lock);
  382. list_for_each_entry(a, &act_base, head) {
  383. if (strcmp(kind, a->kind) == 0) {
  384. if (try_module_get(a->owner))
  385. res = a;
  386. break;
  387. }
  388. }
  389. read_unlock(&act_mod_lock);
  390. }
  391. return res;
  392. }
  393. /* lookup by nlattr */
  394. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  395. {
  396. struct tc_action_ops *a, *res = NULL;
  397. if (kind) {
  398. read_lock(&act_mod_lock);
  399. list_for_each_entry(a, &act_base, head) {
  400. if (nla_strcmp(kind, a->kind) == 0) {
  401. if (try_module_get(a->owner))
  402. res = a;
  403. break;
  404. }
  405. }
  406. read_unlock(&act_mod_lock);
  407. }
  408. return res;
  409. }
  410. /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
  411. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  412. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  413. int nr_actions, struct tcf_result *res)
  414. {
  415. u32 jmp_prgcnt = 0;
  416. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  417. int i;
  418. int ret = TC_ACT_OK;
  419. if (skb_skip_tc_classify(skb))
  420. return TC_ACT_OK;
  421. restart_act_graph:
  422. for (i = 0; i < nr_actions; i++) {
  423. const struct tc_action *a = actions[i];
  424. if (jmp_prgcnt > 0) {
  425. jmp_prgcnt -= 1;
  426. continue;
  427. }
  428. repeat:
  429. ret = a->ops->act(skb, a, res);
  430. if (ret == TC_ACT_REPEAT)
  431. goto repeat; /* we need a ttl - JHS */
  432. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  433. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  434. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  435. /* faulty opcode, stop pipeline */
  436. return TC_ACT_OK;
  437. } else {
  438. jmp_ttl -= 1;
  439. if (jmp_ttl > 0)
  440. goto restart_act_graph;
  441. else /* faulty graph, stop pipeline */
  442. return TC_ACT_OK;
  443. }
  444. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  445. tcf_action_goto_chain_exec(a, res);
  446. }
  447. if (ret != TC_ACT_PIPE)
  448. break;
  449. }
  450. return ret;
  451. }
  452. EXPORT_SYMBOL(tcf_action_exec);
  453. int tcf_action_destroy(struct list_head *actions, int bind)
  454. {
  455. const struct tc_action_ops *ops;
  456. struct tc_action *a, *tmp;
  457. int ret = 0;
  458. list_for_each_entry_safe(a, tmp, actions, list) {
  459. ops = a->ops;
  460. ret = __tcf_idr_release(a, bind, true);
  461. if (ret == ACT_P_DELETED)
  462. module_put(ops->owner);
  463. else if (ret < 0)
  464. return ret;
  465. }
  466. return ret;
  467. }
  468. int
  469. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  470. {
  471. return a->ops->dump(skb, a, bind, ref);
  472. }
  473. int
  474. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  475. {
  476. int err = -EINVAL;
  477. unsigned char *b = skb_tail_pointer(skb);
  478. struct nlattr *nest;
  479. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  480. goto nla_put_failure;
  481. if (tcf_action_copy_stats(skb, a, 0))
  482. goto nla_put_failure;
  483. if (a->act_cookie) {
  484. if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
  485. a->act_cookie->data))
  486. goto nla_put_failure;
  487. }
  488. nest = nla_nest_start(skb, TCA_OPTIONS);
  489. if (nest == NULL)
  490. goto nla_put_failure;
  491. err = tcf_action_dump_old(skb, a, bind, ref);
  492. if (err > 0) {
  493. nla_nest_end(skb, nest);
  494. return err;
  495. }
  496. nla_put_failure:
  497. nlmsg_trim(skb, b);
  498. return -1;
  499. }
  500. EXPORT_SYMBOL(tcf_action_dump_1);
  501. int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
  502. int bind, int ref)
  503. {
  504. struct tc_action *a;
  505. int err = -EINVAL;
  506. struct nlattr *nest;
  507. list_for_each_entry(a, actions, list) {
  508. nest = nla_nest_start(skb, a->order);
  509. if (nest == NULL)
  510. goto nla_put_failure;
  511. err = tcf_action_dump_1(skb, a, bind, ref);
  512. if (err < 0)
  513. goto errout;
  514. nla_nest_end(skb, nest);
  515. }
  516. return 0;
  517. nla_put_failure:
  518. err = -EINVAL;
  519. errout:
  520. nla_nest_cancel(skb, nest);
  521. return err;
  522. }
  523. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  524. {
  525. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  526. if (!c)
  527. return NULL;
  528. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  529. if (!c->data) {
  530. kfree(c);
  531. return NULL;
  532. }
  533. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  534. return c;
  535. }
  536. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  537. struct nlattr *nla, struct nlattr *est,
  538. char *name, int ovr, int bind)
  539. {
  540. struct tc_action *a;
  541. struct tc_action_ops *a_o;
  542. struct tc_cookie *cookie = NULL;
  543. char act_name[IFNAMSIZ];
  544. struct nlattr *tb[TCA_ACT_MAX + 1];
  545. struct nlattr *kind;
  546. int err;
  547. if (name == NULL) {
  548. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  549. if (err < 0)
  550. goto err_out;
  551. err = -EINVAL;
  552. kind = tb[TCA_ACT_KIND];
  553. if (kind == NULL)
  554. goto err_out;
  555. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  556. goto err_out;
  557. if (tb[TCA_ACT_COOKIE]) {
  558. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  559. if (cklen > TC_COOKIE_MAX_SIZE)
  560. goto err_out;
  561. cookie = nla_memdup_cookie(tb);
  562. if (!cookie) {
  563. err = -ENOMEM;
  564. goto err_out;
  565. }
  566. }
  567. } else {
  568. err = -EINVAL;
  569. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  570. goto err_out;
  571. }
  572. a_o = tc_lookup_action_n(act_name);
  573. if (a_o == NULL) {
  574. #ifdef CONFIG_MODULES
  575. rtnl_unlock();
  576. request_module("act_%s", act_name);
  577. rtnl_lock();
  578. a_o = tc_lookup_action_n(act_name);
  579. /* We dropped the RTNL semaphore in order to
  580. * perform the module load. So, even if we
  581. * succeeded in loading the module we have to
  582. * tell the caller to replay the request. We
  583. * indicate this using -EAGAIN.
  584. */
  585. if (a_o != NULL) {
  586. err = -EAGAIN;
  587. goto err_mod;
  588. }
  589. #endif
  590. err = -ENOENT;
  591. goto err_out;
  592. }
  593. /* backward compatibility for policer */
  594. if (name == NULL)
  595. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
  596. else
  597. err = a_o->init(net, nla, est, &a, ovr, bind);
  598. if (err < 0)
  599. goto err_mod;
  600. if (name == NULL && tb[TCA_ACT_COOKIE]) {
  601. if (a->act_cookie) {
  602. kfree(a->act_cookie->data);
  603. kfree(a->act_cookie);
  604. }
  605. a->act_cookie = cookie;
  606. }
  607. /* module count goes up only when brand new policy is created
  608. * if it exists and is only bound to in a_o->init() then
  609. * ACT_P_CREATED is not returned (a zero is).
  610. */
  611. if (err != ACT_P_CREATED)
  612. module_put(a_o->owner);
  613. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  614. err = tcf_action_goto_chain_init(a, tp);
  615. if (err) {
  616. LIST_HEAD(actions);
  617. list_add_tail(&a->list, &actions);
  618. tcf_action_destroy(&actions, bind);
  619. return ERR_PTR(err);
  620. }
  621. }
  622. return a;
  623. err_mod:
  624. module_put(a_o->owner);
  625. err_out:
  626. if (cookie) {
  627. kfree(cookie->data);
  628. kfree(cookie);
  629. }
  630. return ERR_PTR(err);
  631. }
  632. static void cleanup_a(struct list_head *actions, int ovr)
  633. {
  634. struct tc_action *a;
  635. if (!ovr)
  636. return;
  637. list_for_each_entry(a, actions, list)
  638. a->tcfa_refcnt--;
  639. }
  640. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  641. struct nlattr *est, char *name, int ovr, int bind,
  642. struct list_head *actions)
  643. {
  644. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  645. struct tc_action *act;
  646. int err;
  647. int i;
  648. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  649. if (err < 0)
  650. return err;
  651. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  652. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
  653. if (IS_ERR(act)) {
  654. err = PTR_ERR(act);
  655. goto err;
  656. }
  657. act->order = i;
  658. if (ovr)
  659. act->tcfa_refcnt++;
  660. list_add_tail(&act->list, actions);
  661. }
  662. /* Remove the temp refcnt which was necessary to protect against
  663. * destroying an existing action which was being replaced
  664. */
  665. cleanup_a(actions, ovr);
  666. return 0;
  667. err:
  668. tcf_action_destroy(actions, bind);
  669. return err;
  670. }
  671. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  672. int compat_mode)
  673. {
  674. int err = 0;
  675. struct gnet_dump d;
  676. if (p == NULL)
  677. goto errout;
  678. /* compat_mode being true specifies a call that is supposed
  679. * to add additional backward compatibility statistic TLVs.
  680. */
  681. if (compat_mode) {
  682. if (p->type == TCA_OLD_COMPAT)
  683. err = gnet_stats_start_copy_compat(skb, 0,
  684. TCA_STATS,
  685. TCA_XSTATS,
  686. &p->tcfa_lock, &d,
  687. TCA_PAD);
  688. else
  689. return 0;
  690. } else
  691. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  692. &p->tcfa_lock, &d, TCA_ACT_PAD);
  693. if (err < 0)
  694. goto errout;
  695. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  696. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  697. gnet_stats_copy_queue(&d, p->cpu_qstats,
  698. &p->tcfa_qstats,
  699. p->tcfa_qstats.qlen) < 0)
  700. goto errout;
  701. if (gnet_stats_finish_copy(&d) < 0)
  702. goto errout;
  703. return 0;
  704. errout:
  705. return -1;
  706. }
  707. static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
  708. u32 portid, u32 seq, u16 flags, int event, int bind,
  709. int ref)
  710. {
  711. struct tcamsg *t;
  712. struct nlmsghdr *nlh;
  713. unsigned char *b = skb_tail_pointer(skb);
  714. struct nlattr *nest;
  715. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  716. if (!nlh)
  717. goto out_nlmsg_trim;
  718. t = nlmsg_data(nlh);
  719. t->tca_family = AF_UNSPEC;
  720. t->tca__pad1 = 0;
  721. t->tca__pad2 = 0;
  722. nest = nla_nest_start(skb, TCA_ACT_TAB);
  723. if (nest == NULL)
  724. goto out_nlmsg_trim;
  725. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  726. goto out_nlmsg_trim;
  727. nla_nest_end(skb, nest);
  728. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  729. return skb->len;
  730. out_nlmsg_trim:
  731. nlmsg_trim(skb, b);
  732. return -1;
  733. }
  734. static int
  735. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  736. struct list_head *actions, int event)
  737. {
  738. struct sk_buff *skb;
  739. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  740. if (!skb)
  741. return -ENOBUFS;
  742. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  743. 0, 0) <= 0) {
  744. kfree_skb(skb);
  745. return -EINVAL;
  746. }
  747. return rtnl_unicast(skb, net, portid);
  748. }
  749. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  750. struct nlmsghdr *n, u32 portid)
  751. {
  752. struct nlattr *tb[TCA_ACT_MAX + 1];
  753. const struct tc_action_ops *ops;
  754. struct tc_action *a;
  755. int index;
  756. int err;
  757. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  758. if (err < 0)
  759. goto err_out;
  760. err = -EINVAL;
  761. if (tb[TCA_ACT_INDEX] == NULL ||
  762. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  763. goto err_out;
  764. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  765. err = -EINVAL;
  766. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  767. if (!ops) /* could happen in batch of actions */
  768. goto err_out;
  769. err = -ENOENT;
  770. if (ops->lookup(net, &a, index) == 0)
  771. goto err_mod;
  772. module_put(ops->owner);
  773. return a;
  774. err_mod:
  775. module_put(ops->owner);
  776. err_out:
  777. return ERR_PTR(err);
  778. }
  779. static int tca_action_flush(struct net *net, struct nlattr *nla,
  780. struct nlmsghdr *n, u32 portid)
  781. {
  782. struct sk_buff *skb;
  783. unsigned char *b;
  784. struct nlmsghdr *nlh;
  785. struct tcamsg *t;
  786. struct netlink_callback dcb;
  787. struct nlattr *nest;
  788. struct nlattr *tb[TCA_ACT_MAX + 1];
  789. const struct tc_action_ops *ops;
  790. struct nlattr *kind;
  791. int err = -ENOMEM;
  792. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  793. if (!skb) {
  794. pr_debug("tca_action_flush: failed skb alloc\n");
  795. return err;
  796. }
  797. b = skb_tail_pointer(skb);
  798. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  799. if (err < 0)
  800. goto err_out;
  801. err = -EINVAL;
  802. kind = tb[TCA_ACT_KIND];
  803. ops = tc_lookup_action(kind);
  804. if (!ops) /*some idjot trying to flush unknown action */
  805. goto err_out;
  806. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  807. sizeof(*t), 0);
  808. if (!nlh)
  809. goto out_module_put;
  810. t = nlmsg_data(nlh);
  811. t->tca_family = AF_UNSPEC;
  812. t->tca__pad1 = 0;
  813. t->tca__pad2 = 0;
  814. nest = nla_nest_start(skb, TCA_ACT_TAB);
  815. if (nest == NULL)
  816. goto out_module_put;
  817. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
  818. if (err <= 0)
  819. goto out_module_put;
  820. nla_nest_end(skb, nest);
  821. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  822. nlh->nlmsg_flags |= NLM_F_ROOT;
  823. module_put(ops->owner);
  824. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  825. n->nlmsg_flags & NLM_F_ECHO);
  826. if (err > 0)
  827. return 0;
  828. return err;
  829. out_module_put:
  830. module_put(ops->owner);
  831. err_out:
  832. kfree_skb(skb);
  833. return err;
  834. }
  835. static int
  836. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  837. u32 portid)
  838. {
  839. int ret;
  840. struct sk_buff *skb;
  841. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  842. if (!skb)
  843. return -ENOBUFS;
  844. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  845. 0, 1) <= 0) {
  846. kfree_skb(skb);
  847. return -EINVAL;
  848. }
  849. /* now do the delete */
  850. ret = tcf_action_destroy(actions, 0);
  851. if (ret < 0) {
  852. kfree_skb(skb);
  853. return ret;
  854. }
  855. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  856. n->nlmsg_flags & NLM_F_ECHO);
  857. if (ret > 0)
  858. return 0;
  859. return ret;
  860. }
  861. static int
  862. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  863. u32 portid, int event)
  864. {
  865. int i, ret;
  866. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  867. struct tc_action *act;
  868. LIST_HEAD(actions);
  869. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  870. if (ret < 0)
  871. return ret;
  872. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  873. if (tb[1] != NULL)
  874. return tca_action_flush(net, tb[1], n, portid);
  875. else
  876. return -EINVAL;
  877. }
  878. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  879. act = tcf_action_get_1(net, tb[i], n, portid);
  880. if (IS_ERR(act)) {
  881. ret = PTR_ERR(act);
  882. goto err;
  883. }
  884. act->order = i;
  885. list_add_tail(&act->list, &actions);
  886. }
  887. if (event == RTM_GETACTION)
  888. ret = tcf_get_notify(net, portid, n, &actions, event);
  889. else { /* delete */
  890. ret = tcf_del_notify(net, n, &actions, portid);
  891. if (ret)
  892. goto err;
  893. return ret;
  894. }
  895. err:
  896. if (event != RTM_GETACTION)
  897. tcf_action_destroy(&actions, 0);
  898. return ret;
  899. }
  900. static int
  901. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  902. u32 portid)
  903. {
  904. struct sk_buff *skb;
  905. int err = 0;
  906. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  907. if (!skb)
  908. return -ENOBUFS;
  909. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  910. RTM_NEWACTION, 0, 0) <= 0) {
  911. kfree_skb(skb);
  912. return -EINVAL;
  913. }
  914. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  915. n->nlmsg_flags & NLM_F_ECHO);
  916. if (err > 0)
  917. err = 0;
  918. return err;
  919. }
  920. static int tcf_action_add(struct net *net, struct nlattr *nla,
  921. struct nlmsghdr *n, u32 portid, int ovr)
  922. {
  923. int loop, ret;
  924. LIST_HEAD(actions);
  925. for (loop = 0; loop < 10; loop++) {
  926. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
  927. &actions);
  928. if (ret != -EAGAIN)
  929. break;
  930. }
  931. if (ret)
  932. return ret;
  933. return tcf_add_notify(net, n, &actions, portid);
  934. }
  935. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  936. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  937. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  938. .validation_data = &tcaa_root_flags_allowed },
  939. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  940. };
  941. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  942. struct netlink_ext_ack *extack)
  943. {
  944. struct net *net = sock_net(skb->sk);
  945. struct nlattr *tca[TCA_ROOT_MAX + 1];
  946. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  947. int ret = 0, ovr = 0;
  948. if ((n->nlmsg_type != RTM_GETACTION) &&
  949. !netlink_capable(skb, CAP_NET_ADMIN))
  950. return -EPERM;
  951. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  952. extack);
  953. if (ret < 0)
  954. return ret;
  955. if (tca[TCA_ACT_TAB] == NULL) {
  956. pr_notice("tc_ctl_action: received NO action attribs\n");
  957. return -EINVAL;
  958. }
  959. /* n->nlmsg_flags & NLM_F_CREATE */
  960. switch (n->nlmsg_type) {
  961. case RTM_NEWACTION:
  962. /* we are going to assume all other flags
  963. * imply create only if it doesn't exist
  964. * Note that CREATE | EXCL implies that
  965. * but since we want avoid ambiguity (eg when flags
  966. * is zero) then just set this
  967. */
  968. if (n->nlmsg_flags & NLM_F_REPLACE)
  969. ovr = 1;
  970. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
  971. break;
  972. case RTM_DELACTION:
  973. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  974. portid, RTM_DELACTION);
  975. break;
  976. case RTM_GETACTION:
  977. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  978. portid, RTM_GETACTION);
  979. break;
  980. default:
  981. BUG();
  982. }
  983. return ret;
  984. }
  985. static struct nlattr *find_dump_kind(struct nlattr **nla)
  986. {
  987. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  988. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  989. struct nlattr *kind;
  990. tb1 = nla[TCA_ACT_TAB];
  991. if (tb1 == NULL)
  992. return NULL;
  993. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  994. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  995. return NULL;
  996. if (tb[1] == NULL)
  997. return NULL;
  998. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  999. return NULL;
  1000. kind = tb2[TCA_ACT_KIND];
  1001. return kind;
  1002. }
  1003. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1004. {
  1005. struct net *net = sock_net(skb->sk);
  1006. struct nlmsghdr *nlh;
  1007. unsigned char *b = skb_tail_pointer(skb);
  1008. struct nlattr *nest;
  1009. struct tc_action_ops *a_o;
  1010. int ret = 0;
  1011. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1012. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1013. struct nlattr *count_attr = NULL;
  1014. unsigned long jiffy_since = 0;
  1015. struct nlattr *kind = NULL;
  1016. struct nla_bitfield32 bf;
  1017. u32 msecs_since = 0;
  1018. u32 act_count = 0;
  1019. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1020. tcaa_policy, NULL);
  1021. if (ret < 0)
  1022. return ret;
  1023. kind = find_dump_kind(tb);
  1024. if (kind == NULL) {
  1025. pr_info("tc_dump_action: action bad kind\n");
  1026. return 0;
  1027. }
  1028. a_o = tc_lookup_action(kind);
  1029. if (a_o == NULL)
  1030. return 0;
  1031. cb->args[2] = 0;
  1032. if (tb[TCA_ROOT_FLAGS]) {
  1033. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1034. cb->args[2] = bf.value;
  1035. }
  1036. if (tb[TCA_ROOT_TIME_DELTA]) {
  1037. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1038. }
  1039. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1040. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1041. if (!nlh)
  1042. goto out_module_put;
  1043. if (msecs_since)
  1044. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1045. t = nlmsg_data(nlh);
  1046. t->tca_family = AF_UNSPEC;
  1047. t->tca__pad1 = 0;
  1048. t->tca__pad2 = 0;
  1049. cb->args[3] = jiffy_since;
  1050. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1051. if (!count_attr)
  1052. goto out_module_put;
  1053. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1054. if (nest == NULL)
  1055. goto out_module_put;
  1056. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
  1057. if (ret < 0)
  1058. goto out_module_put;
  1059. if (ret > 0) {
  1060. nla_nest_end(skb, nest);
  1061. ret = skb->len;
  1062. act_count = cb->args[1];
  1063. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1064. cb->args[1] = 0;
  1065. } else
  1066. nlmsg_trim(skb, b);
  1067. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1068. if (NETLINK_CB(cb->skb).portid && ret)
  1069. nlh->nlmsg_flags |= NLM_F_MULTI;
  1070. module_put(a_o->owner);
  1071. return skb->len;
  1072. out_module_put:
  1073. module_put(a_o->owner);
  1074. nlmsg_trim(skb, b);
  1075. return skb->len;
  1076. }
  1077. static int __init tc_action_init(void)
  1078. {
  1079. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1080. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1081. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1082. 0);
  1083. return 0;
  1084. }
  1085. subsys_initcall(tc_action_init);