act_api.c 24 KB

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