act_api.c 24 KB

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