act_api.c 23 KB

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