act_api.c 23 KB

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