act_api.c 25 KB

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