act_api.c 28 KB

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