act_api.c 24 KB

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