act_api.c 23 KB

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