act_api.c 24 KB

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