act_api.c 23 KB

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